* Added a Reset prefs option
[vlc.git] / modules / gui / macosx / intf.m
1 /*****************************************************************************
2  * intf.m: MacOS X interface plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: intf.m,v 1.84 2003/05/20 15:23:25 hartman Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32
33 #include "intf.h"
34 #include "vout.h"
35 #include "prefs.h"
36 #include "playlist.h"
37 #include "info.h"
38
39 /*****************************************************************************
40  * Local prototypes.
41  *****************************************************************************/
42 static void Run ( intf_thread_t *p_intf );
43
44 /*****************************************************************************
45  * OpenIntf: initialize interface
46  *****************************************************************************/
47 int E_(OpenIntf) ( vlc_object_t *p_this )
48 {   
49     intf_thread_t *p_intf = (intf_thread_t*) p_this;
50
51     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
52     if( p_intf->p_sys == NULL )
53     {
54         return( 1 );
55     }
56
57     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
58     
59     p_intf->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
60     
61     /* Put Cocoa into multithread mode as soon as possible.
62      * http://developer.apple.com/techpubs/macosx/Cocoa/
63      * TasksAndConcepts/ProgrammingTopics/Multithreading/index.html
64      * This thread does absolutely nothing at all. */
65     [NSThread detachNewThreadSelector:@selector(self) toTarget:[NSString string] withObject:nil];
66     
67     p_intf->p_sys->o_sendport = [[NSPort port] retain];
68     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
69     p_intf->pf_run = Run;
70     
71     [[VLCApplication sharedApplication] autorelease];
72     [NSApp initIntlSupport];
73     [NSApp setIntf: p_intf];
74
75     [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
76
77     return( 0 );
78 }
79
80 /*****************************************************************************
81  * CloseIntf: destroy interface
82  *****************************************************************************/
83 void E_(CloseIntf) ( vlc_object_t *p_this )
84 {
85     intf_thread_t *p_intf = (intf_thread_t*) p_this;
86
87     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
88
89     [p_intf->p_sys->o_sendport release];
90     [p_intf->p_sys->o_pool release];
91
92     free( p_intf->p_sys );
93 }
94
95 /*****************************************************************************
96  * Run: main loop
97  *****************************************************************************/
98 static void Run( intf_thread_t *p_intf )
99 {
100     /* Do it again - for some unknown reason, vlc_thread_create() often
101      * fails to go to real-time priority with the first launched thread
102      * (???) --Meuuh */
103     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
104
105     [NSApp run];
106 }
107
108 /*****************************************************************************
109  * VLCApplication implementation 
110  *****************************************************************************/
111 @implementation VLCApplication
112
113 - (id)init
114 {
115     /* default encoding: ISO-8859-1 */
116     i_encoding = NSISOLatin1StringEncoding;
117
118     return( [super init] );
119 }
120
121 - (void)initIntlSupport
122 {
123     char *psz_lang = getenv( "LANG" );
124
125     if( psz_lang == NULL )
126     {
127         return;
128     }
129
130     if( strncmp( psz_lang, "pl", 2 ) == 0 )
131     {
132         i_encoding = NSISOLatin2StringEncoding;
133     }
134     else if( strncmp( psz_lang, "ja", 2 ) == 0 ) 
135     {
136         i_encoding = NSJapaneseEUCStringEncoding;
137     }
138     else if( strncmp( psz_lang, "ru", 2 ) == 0 )
139     {
140 #define CFSENC2NSSENC(e) CFStringConvertEncodingToNSStringEncoding(e)
141         i_encoding = CFSENC2NSSENC( kCFStringEncodingKOI8_R ); 
142 #undef CFSENC2NSSENC
143     }
144 }
145
146 - (NSString *)localizedString:(char *)psz
147 {
148     NSString * o_str = nil;
149
150     if( psz != NULL )
151     {
152         UInt32 uiLength = (UInt32)strlen( psz );
153         NSData * o_data = [NSData dataWithBytes: psz length: uiLength];
154         o_str = [[[NSString alloc] initWithData: o_data
155                                        encoding: i_encoding] autorelease];
156     }
157
158     return( o_str );
159 }
160
161 - (char *)delocalizeString:(NSString *)id
162 {
163     NSData * o_data = [id dataUsingEncoding: i_encoding
164                           allowLossyConversion: NO];
165     char * psz_string;
166
167     if ( o_data == nil )
168     {
169         o_data = [id dataUsingEncoding: i_encoding
170                      allowLossyConversion: YES];
171         psz_string = malloc( [o_data length] + 1 ); 
172         [o_data getBytes: psz_string];
173         psz_string[ [o_data length] ] = '\0';
174         msg_Err( p_intf, "cannot convert to wanted encoding: %s",
175                  psz_string );
176     }
177     else
178     {
179         psz_string = malloc( [o_data length] + 1 ); 
180         [o_data getBytes: psz_string];
181         psz_string[ [o_data length] ] = '\0';
182     }
183
184     return psz_string;
185 }
186
187 - (NSStringEncoding)getEncoding
188 {
189     return i_encoding;
190 }
191
192 - (void)setIntf:(intf_thread_t *)_p_intf
193 {
194     p_intf = _p_intf;
195 }
196
197 - (intf_thread_t *)getIntf
198 {
199     return( p_intf );
200 }
201
202 - (void)terminate:(id)sender
203 {
204     p_intf->p_vlc->b_die = VLC_TRUE;
205 }
206
207 @end
208
209 int ExecuteOnMainThread( id target, SEL sel, void * p_arg )
210 {
211     int i_ret = 0;
212
213     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
214
215     if( [target respondsToSelector: @selector(performSelectorOnMainThread:
216                                              withObject:waitUntilDone:)] )
217     {
218         [target performSelectorOnMainThread: sel
219                 withObject: [NSValue valueWithPointer: p_arg]
220                 waitUntilDone: YES];
221     }
222     else if( NSApp != nil && [NSApp respondsToSelector: @selector(getIntf)] ) 
223     {
224         NSValue * o_v1;
225         NSValue * o_v2;
226         NSArray * o_array;
227         NSPort * o_recv_port;
228         NSInvocation * o_inv;
229         NSPortMessage * o_msg;
230         intf_thread_t * p_intf;
231         NSConditionLock * o_lock;
232         NSMethodSignature * o_sig;
233
234         id * val[] = { &o_lock, &o_v2 };
235
236         p_intf = (intf_thread_t *)[NSApp getIntf];
237
238         o_recv_port = [[NSPort port] retain];
239         o_v1 = [NSValue valueWithPointer: val]; 
240         o_v2 = [NSValue valueWithPointer: p_arg];
241
242         o_sig = [target methodSignatureForSelector: sel];
243         o_inv = [NSInvocation invocationWithMethodSignature: o_sig];
244         [o_inv setArgument: &o_v1 atIndex: 2];
245         [o_inv setTarget: target];
246         [o_inv setSelector: sel];
247
248         o_array = [NSArray arrayWithObject:
249             [NSData dataWithBytes: &o_inv length: sizeof(o_inv)]];
250         o_msg = [[NSPortMessage alloc]
251             initWithSendPort: p_intf->p_sys->o_sendport
252             receivePort: o_recv_port components: o_array];
253
254         o_lock = [[NSConditionLock alloc] initWithCondition: 0];
255         [o_msg sendBeforeDate: [NSDate distantPast]];
256         [o_lock lockWhenCondition: 1];
257         [o_lock unlock];
258         [o_lock release];
259
260         [o_msg release];
261         [o_recv_port release];
262     } 
263     else
264     {
265         i_ret = 1;
266     }
267
268     [o_pool release];
269
270     return( i_ret );
271 }
272
273 /*****************************************************************************
274  * playlistChanged: Callback triggered by the intf-change playlist
275  * variable, to let the intf update the playlist.
276  *****************************************************************************/
277 int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
278                      vlc_value_t old_val, vlc_value_t new_val, void *param )
279 {
280     intf_thread_t * p_intf = [NSApp getIntf];
281     p_intf->p_sys->b_playlist_update = VLC_TRUE;
282     p_intf->p_sys->b_intf_update = VLC_TRUE;
283     return VLC_SUCCESS;
284 }
285
286 /*****************************************************************************
287  * VLCMain implementation 
288  *****************************************************************************/
289 @implementation VLCMain
290
291 - (void)awakeFromNib
292 {
293     [o_window setTitle: _NS("VLC - Controller")];
294     [o_window setExcludedFromWindowsMenu: TRUE];
295
296     /* button controls */
297     [o_btn_playlist setToolTip: _NS("Playlist")];
298     [o_btn_prev setToolTip: _NS("Previous")];
299     [o_btn_slower setToolTip: _NS("Slower")];
300     [o_btn_play setToolTip: _NS("Play")];
301     [o_btn_stop setToolTip: _NS("Stop")];
302     [o_btn_faster setToolTip: _NS("Faster")];
303     [o_btn_next setToolTip: _NS("Next")];
304     [o_btn_prefs setToolTip: _NS("Preferences")];
305     [o_volumeslider setToolTip: _NS("Volume")];
306     [o_timeslider setToolTip: _NS("Position")];
307
308     /* messages panel */ 
309     [o_msgs_panel setDelegate: self];
310     [o_msgs_panel setTitle: _NS("Messages")];
311     [o_msgs_panel setExcludedFromWindowsMenu: TRUE];
312     [o_msgs_btn_crashlog setTitle: _NS("Open CrashLog")];
313
314     /* main menu */
315     [o_mi_about setTitle: _NS("About VLC media player")];
316     [o_mi_prefs setTitle: _NS("Preferences...")];
317     [o_mi_hide setTitle: _NS("Hide VLC")];
318     [o_mi_hide_others setTitle: _NS("Hide Others")];
319     [o_mi_show_all setTitle: _NS("Show All")];
320     [o_mi_quit setTitle: _NS("Quit VLC")];
321
322     [o_mu_file setTitle: _ANS("1:File")];
323     [o_mi_open_generic setTitle: _NS("Open...")];
324     [o_mi_open_file setTitle: _NS("Open File...")];
325     [o_mi_open_disc setTitle: _NS("Open Disc...")];
326     [o_mi_open_net setTitle: _NS("Open Network...")];
327     [o_mi_open_recent setTitle: _NS("Open Recent")];
328     [o_mi_open_recent_cm setTitle: _NS("Clear Menu")];
329
330     [o_mu_edit setTitle: _NS("Edit")];
331     [o_mi_cut setTitle: _NS("Cut")];
332     [o_mi_copy setTitle: _NS("Copy")];
333     [o_mi_paste setTitle: _NS("Paste")];
334     [o_mi_clear setTitle: _NS("Clear")];
335     [o_mi_select_all setTitle: _NS("Select All")];
336
337     [o_mu_controls setTitle: _NS("Controls")];
338     [o_mi_play setTitle: _NS("Play")];
339     [o_mi_stop setTitle: _NS("Stop")];
340     [o_mi_faster setTitle: _NS("Faster")];
341     [o_mi_slower setTitle: _NS("Slower")];
342     [o_mi_previous setTitle: _NS("Previous")];
343     [o_mi_next setTitle: _NS("Next")];
344     [o_mi_loop setTitle: _NS("Loop")];
345     [o_mi_fwd setTitle: _NS("Step Forward")];
346     [o_mi_bwd setTitle: _NS("Step Backward")];
347     [o_mi_program setTitle: _NS("Program")];
348     [o_mu_program setTitle: _NS("Program")];
349     [o_mi_title setTitle: _NS("Title")];
350     [o_mu_title setTitle: _NS("Title")];
351     [o_mi_chapter setTitle: _NS("Chapter")];
352     [o_mu_chapter setTitle: _NS("Chapter")];
353     
354     [o_mu_audio setTitle: _NS("Audio")];
355     [o_mi_vol_up setTitle: _NS("Volume Up")];
356     [o_mi_vol_down setTitle: _NS("Volume Down")];
357     [o_mi_mute setTitle: _NS("Mute")];
358     [o_mi_audiotrack setTitle: _NS("Audio Track")];
359     [o_mu_audiotrack setTitle: _NS("Audio Track")];
360     [o_mi_channels setTitle: _NS("Channels")];
361     [o_mu_channels setTitle: _NS("Channels")];
362     [o_mi_device setTitle: _NS("Device")];
363     [o_mu_device setTitle: _NS("Device")];
364     
365     [o_mu_video setTitle: _NS("Video")];
366     [o_mi_half_window setTitle: _NS("Half Size")];
367     [o_mi_normal_window setTitle: _NS("Normal Size")];
368     [o_mi_double_window setTitle: _NS("Double Size")];
369     [o_mi_fittoscreen setTitle: _NS("Fit To Screen")];
370     [o_mi_fullscreen setTitle: _NS("Fullscreen")];
371     [o_mi_floatontop setTitle: _NS("Float On Top")];
372     [o_mi_videotrack setTitle: _NS("Video Track")];
373     [o_mu_videotrack setTitle: _NS("Video Track")];
374     [o_mi_screen setTitle: _NS("Screen")];
375     [o_mu_screen setTitle: _NS("Screen")];
376     [o_mi_subtitle setTitle: _NS("Subtitles")];
377     [o_mu_subtitle setTitle: _NS("Subtitles")];
378     [o_mi_deinterlace setTitle: _NS("Deinterlace")];
379     [o_mu_deinterlace setTitle: _NS("Deinterlace")];
380
381     [o_mu_window setTitle: _NS("Window")];
382     [o_mi_minimize setTitle: _NS("Minimize Window")];
383     [o_mi_close_window setTitle: _NS("Close Window")];
384     [o_mi_controller setTitle: _NS("Controller")];
385     [o_mi_playlist setTitle: _NS("Playlist")];
386     [o_mi_info setTitle: _NS("Info")];
387     [o_mi_messages setTitle: _NS("Messages")];
388
389     [o_mi_bring_atf setTitle: _NS("Bring All to Front")];
390
391     [o_mu_help setTitle: _NS("Help")];
392     [o_mi_readme setTitle: _NS("ReadMe...")];
393     [o_mi_reportabug setTitle: _NS("Report a Bug")];
394     [o_mi_website setTitle: _NS("VideoLAN Website")];
395     [o_mi_license setTitle: _NS("License")];
396
397     /* dock menu */
398     [o_dmi_play setTitle: _NS("Play")];
399     [o_dmi_stop setTitle: _NS("Stop")];
400     [o_dmi_next setTitle: _NS("Next")];
401     [o_dmi_previous setTitle: _NS("Previous")];
402
403     /* error panel */
404     [o_error setTitle: _NS("Error")];
405     [o_err_lbl setStringValue: _NS("An error has occurred which probably prevented the execution of your request:")];
406     [o_err_bug_lbl setStringValue: _NS("If you believe that it is a bug, please follow the instructions at:")]; 
407     [o_err_btn_msgs setTitle: _NS("Open Messages Window")];
408     [o_err_btn_dismiss setTitle: _NS("Dismiss")];
409
410     [o_info_window setTitle: _NS("Info")];
411
412     [self setSubmenusEnabled: FALSE];
413     [self manageVolumeSlider];
414 }
415
416 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
417 {
418     intf_thread_t * p_intf = [NSApp getIntf];
419
420     o_msg_lock = [[NSLock alloc] init];
421     o_msg_arr = [[NSMutableArray arrayWithCapacity: 200] retain];
422
423     o_img_play = [[NSImage imageNamed: @"play"] retain];
424     o_img_pause = [[NSImage imageNamed: @"pause"] retain];
425
426     [p_intf->p_sys->o_sendport setDelegate: self];
427     [[NSRunLoop currentRunLoop] 
428         addPort: p_intf->p_sys->o_sendport
429         forMode: NSDefaultRunLoopMode];
430
431     [NSTimer scheduledTimerWithTimeInterval: 0.5
432         target: self selector: @selector(manageIntf:)
433         userInfo: nil repeats: FALSE];
434
435     [NSThread detachNewThreadSelector: @selector(manage)
436         toTarget: self withObject: nil];
437
438     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
439 }
440
441 - (BOOL)application:(NSApplication *)o_app openFile:(NSString *)o_filename
442 {
443     intf_thread_t * p_intf = [NSApp getIntf];
444     
445     config_PutPsz( p_intf, "sub-file", "" );
446     config_PutInt( p_intf, "sub-delay", 0 );
447     config_PutFloat( p_intf, "sub-fps", 0.0 );
448     config_PutPsz( p_intf, "sout", "" );
449     
450     [o_playlist appendArray:
451         [NSArray arrayWithObject: o_filename] atPos: -1 enqueue: NO];
452             
453     return( TRUE );
454 }
455
456 - (id)getControls
457 {
458     if ( o_controls )
459     {
460         return o_controls;
461     }
462     return nil;
463 }
464
465 - (void)manage
466 {
467     NSDate * o_sleep_date;
468     intf_thread_t * p_intf = [NSApp getIntf];
469     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
470
471     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
472
473     while( !p_intf->b_die )
474     {
475         playlist_t * p_playlist;
476
477         vlc_mutex_lock( &p_intf->change_lock );
478
479         p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, 
480                                               FIND_ANYWHERE );
481
482         if( p_playlist != NULL )
483         {
484             var_AddCallback( p_playlist, "intf-change", PlaylistChanged, self );
485             vlc_mutex_lock( &p_playlist->object_lock );
486             
487             [self manage: p_playlist];
488             
489             vlc_mutex_unlock( &p_playlist->object_lock );
490             vlc_object_release( p_playlist );
491         }
492
493         vlc_mutex_unlock( &p_intf->change_lock );
494
495         o_sleep_date = [NSDate dateWithTimeIntervalSinceNow: .5];
496         [NSThread sleepUntilDate: o_sleep_date];
497     }
498
499     [self terminate];
500
501     [o_pool release];
502 }
503
504 - (void)manage:(playlist_t *)p_playlist
505 {
506     intf_thread_t * p_intf = [NSApp getIntf];
507
508 #define p_input p_playlist->p_input
509
510     if( p_input )
511     {
512         vout_thread_t   * p_vout  = NULL;
513         aout_instance_t * p_aout  = NULL; 
514
515         vlc_mutex_lock( &p_input->stream.stream_lock );
516
517         if( !p_input->b_die )
518         {
519             audio_volume_t i_volume;
520
521             /* New input or stream map change */
522             if( p_input->stream.b_changed )
523             {
524                 p_intf->p_sys->b_playing = TRUE;
525                 [self manageMode: p_playlist];
526             }
527
528             vlc_value_t val;
529
530             if( var_Get( (vlc_object_t *)p_input, "intf-change", &val )
531                 >= 0 && val.b_bool )
532             {
533                 p_intf->p_sys->b_input_update = TRUE;
534             }
535
536             p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
537                                               FIND_ANYWHERE );
538             if( p_aout != NULL )
539             {
540                 vlc_value_t val;
541
542                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val )
543                     >= 0 && val.b_bool )
544                 {
545                     p_intf->p_sys->b_aout_update = TRUE;
546                 }
547
548                 vlc_object_release( (vlc_object_t *)p_aout );
549             }
550
551             aout_VolumeGet( p_intf, &i_volume );
552             p_intf->p_sys->b_mute = ( i_volume == 0 );
553
554             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
555                                               FIND_ANYWHERE );
556             if( p_vout != NULL )
557             {
558                 vlc_value_t val;
559
560                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val )
561                     >= 0 && val.b_bool )
562                 {
563                     p_intf->p_sys->b_vout_update = TRUE;
564                 }
565
566                 vlc_object_release( (vlc_object_t *)p_vout );
567             }
568             [self setupMenus: p_input];
569         }
570         vlc_mutex_unlock( &p_input->stream.stream_lock );
571     }
572     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
573     {
574         p_intf->p_sys->b_playing = FALSE;
575         [self manageMode: p_playlist];
576     }
577
578 #undef p_input
579 }
580
581 - (void)manageMode:(playlist_t *)p_playlist
582 {
583     intf_thread_t * p_intf = [NSApp getIntf];
584
585     if( p_playlist->p_input != NULL )
586     {
587         p_intf->p_sys->b_current_title_update = 1;
588         p_playlist->p_input->stream.b_changed = 0;
589         
590         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
591     }
592     else
593     {
594         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
595                                                           FIND_ANYWHERE );
596         if( p_vout != NULL )
597         {
598             vlc_object_detach( p_vout );
599             vlc_object_release( p_vout );
600
601             vlc_mutex_unlock( &p_playlist->object_lock );
602             vout_Destroy( p_vout );
603             vlc_mutex_lock( &p_playlist->object_lock );
604         }
605     }
606
607     p_intf->p_sys->b_intf_update = VLC_TRUE;
608 }
609
610 - (void)manageIntf:(NSTimer *)o_timer
611 {
612     intf_thread_t * p_intf = [NSApp getIntf];
613
614     if( p_intf->p_vlc->b_die == VLC_TRUE )
615     {
616         [o_timer invalidate];
617         return;
618     }
619
620     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
621                                                        FIND_ANYWHERE );
622
623     if( p_playlist == NULL )
624     {
625         return;
626     }
627
628     if ( p_intf->p_sys->b_playlist_update )
629     {
630         [o_playlist playlistUpdated];
631         p_intf->p_sys->b_playlist_update = VLC_FALSE;
632     }
633
634     if( p_intf->p_sys->b_current_title_update )
635     {
636         vout_thread_t   *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
637                                               FIND_ANYWHERE );
638         if( p_vout != NULL )
639         {
640             id o_vout_wnd;
641             NSEnumerator * o_enum = [[NSApp windows] objectEnumerator];
642             
643             while( ( o_vout_wnd = [o_enum nextObject] ) )
644             {
645                 if( [[o_vout_wnd className] isEqualToString: @"VLCWindow"] )
646                 {
647                     [o_vout_wnd updateTitle];
648                 }
649             }
650             vlc_object_release( (vlc_object_t *)p_vout );
651         }
652         [o_playlist updateRowSelection];
653         [o_info updateInfo];
654
655         p_intf->p_sys->b_current_title_update = FALSE;
656     }
657
658     vlc_mutex_lock( &p_playlist->object_lock );
659
660 #define p_input p_playlist->p_input
661
662     if( p_input != NULL )
663     {
664         vlc_mutex_lock( &p_input->stream.stream_lock );
665     }
666
667     if( p_intf->p_sys->b_intf_update )
668     {
669         vlc_bool_t b_input = VLC_FALSE;
670         vlc_bool_t b_plmul = VLC_FALSE;
671         vlc_bool_t b_control = VLC_FALSE;
672         vlc_bool_t b_seekable = VLC_FALSE;
673         vlc_bool_t b_chapters = VLC_FALSE;
674
675         b_plmul = p_playlist->i_size > 1;
676
677         if( ( b_input = ( p_input != NULL ) ) )
678         {
679             /* seekable streams */
680             b_seekable = p_input->stream.b_seekable;
681
682             /* control buttons for free pace streams */
683             b_control = p_input->stream.b_pace_control; 
684
685             /* chapters */
686             b_chapters = p_input->stream.p_selected_area->i_part_nb > 1; 
687
688             /* play status */
689             p_intf->p_sys->b_play_status = 
690                 p_input->stream.control.i_status != PAUSE_S;
691         }
692         else
693         {
694             /* play status */
695             p_intf->p_sys->b_play_status = FALSE;
696             [self setSubmenusEnabled: FALSE];
697         }
698
699         [self playStatusUpdated: p_intf->p_sys->b_play_status];
700
701         [o_btn_stop setEnabled: b_input];
702         [o_btn_faster setEnabled: b_control];
703         [o_btn_slower setEnabled: b_control];
704         [o_btn_prev setEnabled: (b_plmul || b_chapters)];
705         [o_btn_next setEnabled: (b_plmul || b_chapters)];
706
707         [o_timeslider setFloatValue: 0.0];
708         [o_timeslider setEnabled: b_seekable];
709         [o_timefield setStringValue: @"0:00:00"];
710
711         [self manageVolumeSlider];
712
713         p_intf->p_sys->b_intf_update = VLC_FALSE;
714     }
715
716 #define p_area p_input->stream.p_selected_area
717
718     if( p_intf->p_sys->b_playing && p_input != NULL )
719     {
720         vlc_bool_t b_field_update = TRUE;
721
722         if( !p_input->b_die && ( p_intf->p_sys->b_play_status !=
723             ( p_input->stream.control.i_status != PAUSE_S ) ) ) 
724         {
725             p_intf->p_sys->b_play_status =
726                 !p_intf->p_sys->b_play_status;
727
728             [self playStatusUpdated: p_intf->p_sys->b_play_status]; 
729         }
730
731         if( p_input->stream.b_seekable )
732         {
733             if( f_slider == f_slider_old )
734             {
735                 float f_updated = ( 10000. * p_area->i_tell ) /
736                                            p_area->i_size;
737
738                 if( f_slider != f_updated )
739                 {
740                     [o_timeslider setFloatValue: f_updated];
741                 }
742             }
743             else
744             {
745                 off_t i_seek = ( f_slider * p_area->i_size ) / 10000;
746
747                 /* release the lock to be able to seek */
748                 vlc_mutex_unlock( &p_input->stream.stream_lock );
749                 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
750                 vlc_mutex_lock( &p_input->stream.stream_lock );
751
752                 /* update the old value */
753                 f_slider_old = f_slider; 
754
755                 b_field_update = VLC_FALSE;
756             }
757         }
758
759         if( b_field_update )
760         {
761             NSString * o_time;
762             char psz_time[ OFFSETTOTIME_MAX_SIZE ];
763
764             input_OffsetToTime( p_input, psz_time, p_area->i_tell );
765
766             o_time = [NSString stringWithCString: psz_time];
767             [o_timefield setStringValue: o_time];
768         }
769
770         /* disable screen saver */
771         UpdateSystemActivity( UsrActivity );
772     }
773
774 #undef p_area
775
776     if( p_input != NULL )
777     {
778         vlc_mutex_unlock( &p_input->stream.stream_lock );
779     }
780
781 #undef p_input
782
783     vlc_mutex_unlock( &p_playlist->object_lock );
784     vlc_object_release( p_playlist );
785
786     [self updateMessageArray];
787
788     [NSTimer scheduledTimerWithTimeInterval: 0.5
789         target: self selector: @selector(manageIntf:)
790         userInfo: nil repeats: FALSE];
791 }
792
793 - (void)updateMessageArray
794 {
795     int i_start, i_stop;
796     intf_thread_t * p_intf = [NSApp getIntf];
797
798     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
799     i_stop = *p_intf->p_sys->p_sub->pi_stop;
800     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
801
802     if( p_intf->p_sys->p_sub->i_start != i_stop )
803     {
804         NSColor *o_white = [NSColor whiteColor];
805         NSColor *o_red = [NSColor redColor];
806         NSColor *o_yellow = [NSColor yellowColor];
807         NSColor *o_gray = [NSColor grayColor];
808
809         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
810         static const char * ppsz_type[4] = { ": ", " error: ",
811                                              " warning: ", " debug: " };
812
813         for( i_start = p_intf->p_sys->p_sub->i_start;
814              i_start != i_stop;
815              i_start = (i_start+1) % VLC_MSG_QSIZE )
816         {
817             NSString *o_msg;
818             NSDictionary *o_attr;
819             NSAttributedString *o_msg_color;
820
821             int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
822
823             [o_msg_lock lock];
824
825             if( [o_msg_arr count] + 2 > 400 )
826             {
827                 unsigned rid[] = { 0, 1 };
828                 [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
829                            numIndices: sizeof(rid)/sizeof(rid[0])];
830             }
831
832             o_attr = [NSDictionary dictionaryWithObject: o_gray
833                 forKey: NSForegroundColorAttributeName];
834             o_msg = [NSString stringWithFormat: @"%s%s",
835                 p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
836                 ppsz_type[i_type]];
837             o_msg_color = [[NSAttributedString alloc]
838                 initWithString: o_msg attributes: o_attr];
839             [o_msg_arr addObject: [o_msg_color autorelease]];
840
841             o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
842                 forKey: NSForegroundColorAttributeName];
843             o_msg = [NSString stringWithFormat: @"%s\n",
844                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
845             o_msg_color = [[NSAttributedString alloc]
846                 initWithString: o_msg attributes: o_attr];
847             [o_msg_arr addObject: [o_msg_color autorelease]];
848
849             [o_msg_lock unlock];
850
851             if( i_type == 1 )
852             {
853                 NSString *o_my_msg = [NSString stringWithFormat: @"%s: %s\n",
854                     p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
855                     p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
856
857                 NSRange s_r = NSMakeRange( [[o_err_msg string] length], 0 );
858                 [o_err_msg setEditable: YES];
859                 [o_err_msg setSelectedRange: s_r];
860                 [o_err_msg insertText: o_my_msg];
861
862                 [o_error makeKeyAndOrderFront: self];
863                 [o_err_msg setEditable: NO];
864             }
865         }
866
867         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
868         p_intf->p_sys->p_sub->i_start = i_start;
869         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
870     }
871 }
872
873 - (void)playStatusUpdated:(BOOL)b_pause
874 {
875     if( b_pause )
876     {
877         [o_btn_play setImage: o_img_pause];
878         [o_btn_play setToolTip: _NS("Pause")];
879         [o_mi_play setTitle: _NS("Pause")];
880         [o_dmi_play setTitle: _NS("Pause")];
881     }
882     else
883     {
884         [o_btn_play setImage: o_img_play];
885         [o_btn_play setToolTip: _NS("Play")];
886         [o_mi_play setTitle: _NS("Play")];
887         [o_dmi_play setTitle: _NS("Play")];
888     }
889 }
890
891 - (void)setSubmenusEnabled:(BOOL)b_enabled
892 {
893     [o_mi_program setEnabled: b_enabled];
894     [o_mi_title setEnabled: b_enabled];
895     [o_mi_chapter setEnabled: b_enabled];
896     [o_mi_audiotrack setEnabled: b_enabled];
897     [o_mi_videotrack setEnabled: b_enabled];
898     [o_mi_subtitle setEnabled: b_enabled];
899     [o_mi_channels setEnabled: b_enabled];
900     [o_mi_device setEnabled: b_enabled];
901     [o_mi_screen setEnabled: b_enabled];
902 }
903
904 - (void)manageVolumeSlider
905 {
906     audio_volume_t i_volume;
907     intf_thread_t * p_intf = [NSApp getIntf];
908
909     aout_VolumeGet( p_intf, &i_volume );
910
911     [o_volumeslider setFloatValue: (float)i_volume / AOUT_VOLUME_STEP]; 
912     [o_volumeslider setEnabled: TRUE];
913
914     p_intf->p_sys->b_mute = ( i_volume == 0 );
915 }
916
917 - (void)terminate
918 {
919     NSEvent * o_event;
920     vout_thread_t * p_vout;
921     playlist_t * p_playlist;
922     intf_thread_t * p_intf = [NSApp getIntf];
923
924     /*
925      * Free playlists
926      */
927     msg_Dbg( p_intf, "removing all playlists" );
928     while( (p_playlist = vlc_object_find( p_intf->p_vlc, VLC_OBJECT_PLAYLIST,
929                                           FIND_CHILD )) )
930     {
931         vlc_object_detach( p_playlist );
932         vlc_object_release( p_playlist );
933         playlist_Destroy( p_playlist );
934     }
935
936     /*
937      * Free video outputs
938      */
939     msg_Dbg( p_intf, "removing all video outputs" );
940     while( (p_vout = vlc_object_find( p_intf->p_vlc, 
941                                       VLC_OBJECT_VOUT, FIND_CHILD )) )
942     {
943         vlc_object_detach( p_vout );
944         vlc_object_release( p_vout );
945         vout_Destroy( p_vout );
946     }
947
948     if( o_img_pause != nil )
949     {
950         [o_img_pause release];
951         o_img_pause = nil;
952     }
953
954     if( o_img_play != nil )
955     {
956         [o_img_play release];
957         o_img_play = nil;
958     }
959
960     if( o_msg_arr != nil )
961     {
962         [o_msg_arr removeAllObjects];
963         [o_msg_arr release];
964         o_msg_arr = nil;
965     }
966
967     if( o_msg_lock != nil )
968     {
969         [o_msg_lock release];
970         o_msg_lock = nil;
971     }
972
973     [NSApp stop: nil];
974
975     /* write cached user defaults to disk */
976     [[NSUserDefaults standardUserDefaults] synchronize];
977
978     /* send a dummy event to break out of the event loop */
979     o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
980                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
981                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
982                 context: [NSGraphicsContext currentContext] eventNumber: 1
983                 clickCount: 1 pressure: 0.0];
984     [NSApp postEvent: o_event atStart: YES];
985 }
986
987 - (void)setupMenus:(input_thread_t *)p_input
988 {
989     intf_thread_t * p_intf = [NSApp getIntf];
990     vlc_value_t val;
991
992     if( p_intf->p_sys->b_input_update )
993     {
994         val.b_bool = FALSE;
995
996         [self setupVarMenu: o_mi_program target: (vlc_object_t *)p_input
997             var: "program" selector: @selector(toggleVar:)];
998
999         [self setupVarMenu: o_mi_title target: (vlc_object_t *)p_input
1000             var: "title" selector: @selector(toggleVar:)];
1001
1002         [self setupVarMenu: o_mi_chapter target: (vlc_object_t *)p_input
1003             var: "chapter" selector: @selector(toggleVar:)];
1004
1005         [self setupVarMenu: o_mi_audiotrack target: (vlc_object_t *)p_input
1006             var: "audio-es" selector: @selector(toggleVar:)];
1007
1008         [self setupVarMenu: o_mi_videotrack target: (vlc_object_t *)p_input
1009             var: "video-es" selector: @selector(toggleVar:)];
1010
1011         [self setupVarMenu: o_mi_subtitle target: (vlc_object_t *)p_input
1012             var: "spu-es" selector: @selector(toggleVar:)];
1013
1014         p_intf->p_sys->b_input_update = FALSE;
1015         var_Set( (vlc_object_t *)p_input, "intf-change", val );
1016     }
1017
1018     if ( p_intf->p_sys->b_aout_update )
1019     {
1020         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
1021                                                     FIND_ANYWHERE );
1022
1023         if ( p_aout != NULL )
1024         {
1025             val.b_bool = FALSE;
1026             
1027             [self setupVarMenu: o_mi_channels target: (vlc_object_t *)p_aout
1028                 var: "audio-channels" selector: @selector(toggleVar:)];
1029
1030             [self setupVarMenu: o_mi_device target: (vlc_object_t *)p_aout
1031                 var: "audio-device" selector: @selector(toggleVar:)];
1032             var_Set( (vlc_object_t *)p_aout, "intf-change", val );
1033             
1034             vlc_object_release( (vlc_object_t *)p_aout );
1035         }
1036         p_intf->p_sys->b_aout_update = FALSE;
1037     }
1038
1039     if( p_intf->p_sys->b_vout_update )
1040     {
1041         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1042                                                           FIND_ANYWHERE );
1043
1044         if ( p_vout != NULL )
1045         {
1046             val.b_bool = FALSE;
1047
1048             [self setupVarMenu: o_mi_screen target: (vlc_object_t *)p_vout
1049                 var: "video-device" selector: @selector(toggleVar:)];
1050             var_Set( (vlc_object_t *)p_vout, "intf-change", val );
1051             
1052             vlc_object_release( (vlc_object_t *)p_vout );
1053             [o_mi_close_window setEnabled: TRUE];
1054         }
1055         
1056         p_intf->p_sys->b_vout_update = FALSE;
1057     }
1058 }
1059
1060 - (void)setupVarMenu:(NSMenuItem *)o_mi
1061                      target:(vlc_object_t *)p_object
1062                      var:(const char *)psz_variable
1063                      selector:(SEL)pf_callback
1064 {
1065     int i, i_nb_items, i_value;
1066     NSMenu * o_menu = [o_mi submenu];
1067     vlc_value_t val, text;
1068     
1069     [o_mi setTag: (int)psz_variable];
1070     
1071     /* remove previous items */
1072     i_nb_items = [o_menu numberOfItems];
1073     for( i = 0; i < i_nb_items; i++ )
1074     {
1075         [o_menu removeItemAtIndex: 0];
1076     }
1077
1078     if ( var_Get( p_object, psz_variable, &val ) < 0 )
1079     {
1080         return;
1081     }
1082     i_value = val.i_int;
1083
1084     if ( var_Change( p_object, psz_variable,
1085                      VLC_VAR_GETLIST, &val, &text ) < 0 )
1086     {
1087         return;
1088     }
1089
1090     /* make (un)sensitive */
1091     [o_mi setEnabled: ( val.p_list->i_count > 1 )];
1092
1093     for ( i = 0; i < val.p_list->i_count; i++ )
1094     {
1095         NSMenuItem * o_lmi;
1096         NSString * o_title;
1097
1098         if ( text.p_list->p_values[i].psz_string )
1099         {
1100             o_title = [NSApp localizedString: text.p_list->p_values[i].psz_string];
1101         }
1102         else
1103         {
1104             o_title = [NSString stringWithFormat: @"%s - %d",
1105                         strdup(psz_variable), val.p_list->p_values[i].i_int ];
1106         }
1107         o_lmi = [o_menu addItemWithTitle: [o_title copy]
1108                 action: pf_callback keyEquivalent: @""];
1109         
1110         /* FIXME: this isn't 64-bit clean ! */
1111         [o_lmi setTag: val.p_list->p_values[i].i_int];
1112         [o_lmi setRepresentedObject:
1113             [NSValue valueWithPointer: p_object]];
1114         [o_lmi setTarget: o_controls];
1115
1116         if ( i_value == val.p_list->p_values[i].i_int )
1117             [o_lmi setState: NSOnState];
1118     }
1119
1120     var_Change( p_object, psz_variable, VLC_VAR_FREELIST,
1121                 &val, &text );
1122
1123 }
1124
1125 - (IBAction)clearRecentItems:(id)sender
1126 {
1127     [[NSDocumentController sharedDocumentController]
1128                           clearRecentDocuments: nil];
1129 }
1130
1131 - (void)openRecentItem:(id)sender
1132 {
1133     [self application: nil openFile: [sender title]]; 
1134 }
1135
1136 - (IBAction)viewPreferences:(id)sender
1137 {
1138     [o_prefs showPrefs];
1139 }
1140
1141 - (IBAction)timesliderUpdate:(id)sender
1142 {
1143     float f_updated;
1144
1145     switch( [[NSApp currentEvent] type] )
1146     {
1147         case NSLeftMouseUp:
1148         case NSLeftMouseDown:
1149             f_slider = [sender floatValue];
1150             return;
1151
1152         case NSLeftMouseDragged:
1153             f_updated = [sender floatValue];
1154             break;
1155
1156         default:
1157             return;
1158     }
1159
1160     intf_thread_t * p_intf = [NSApp getIntf];
1161
1162     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1163                                                        FIND_ANYWHERE );
1164
1165     if( p_playlist == NULL )
1166     {
1167         return;
1168     }
1169
1170     vlc_mutex_lock( &p_playlist->object_lock );
1171
1172     if( p_playlist->p_input != NULL )
1173     {
1174         off_t i_tell;
1175         NSString * o_time;
1176         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1177
1178 #define p_area p_playlist->p_input->stream.p_selected_area
1179         vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
1180         i_tell = f_updated / 10000. * p_area->i_size;
1181         input_OffsetToTime( p_playlist->p_input, psz_time, i_tell );
1182         vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
1183 #undef p_area
1184
1185         o_time = [NSString stringWithCString: psz_time];
1186         [o_timefield setStringValue: o_time]; 
1187     }
1188
1189     vlc_mutex_unlock( &p_playlist->object_lock );
1190     vlc_object_release( p_playlist );
1191 }
1192
1193 - (IBAction)closeError:(id)sender
1194 {
1195     [o_err_msg setString: @""];
1196     [o_error performClose: self];
1197 }
1198
1199 - (IBAction)openReadMe:(id)sender
1200 {
1201     NSString * o_path = [[NSBundle mainBundle] 
1202         pathForResource: @"README.MacOSX" ofType: @"rtf"]; 
1203
1204     [[NSWorkspace sharedWorkspace] openFile: o_path 
1205                                    withApplication: @"TextEdit"];
1206 }
1207
1208 - (IBAction)reportABug:(id)sender
1209 {
1210     NSURL * o_url = [NSURL URLWithString: 
1211         @"http://www.videolan.org/support/bug-reporting.html"];
1212
1213     [[NSWorkspace sharedWorkspace] openURL: o_url];
1214 }
1215
1216 - (IBAction)openWebsite:(id)sender
1217 {
1218     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org"];
1219
1220     [[NSWorkspace sharedWorkspace] openURL: o_url];
1221 }
1222
1223 - (IBAction)openLicense:(id)sender
1224 {
1225     NSString * o_path = [[NSBundle mainBundle] 
1226         pathForResource: @"COPYING" ofType: nil];
1227
1228     [[NSWorkspace sharedWorkspace] openFile: o_path 
1229                                    withApplication: @"TextEdit"];
1230 }
1231
1232 - (IBAction)openCrashLog:(id)sender
1233 {
1234     NSString * o_path = [@"~/Library/Logs/CrashReporter/VLC.crash.log"
1235                                     stringByExpandingTildeInPath]; 
1236
1237     
1238     if ( [[NSFileManager defaultManager] fileExistsAtPath: o_path ] )
1239     {
1240         [[NSWorkspace sharedWorkspace] openFile: o_path 
1241                                     withApplication: @"Console"];
1242     }
1243     else
1244     {
1245         NSBeginInformationalAlertSheet(_NS("No CrashLog found"), @"Continue", nil, nil, o_msgs_panel, self, NULL, NULL, nil, _NS("Either you are running Mac OS X pre 10.2 or you haven't experienced any heavy crashes yet.") );
1246
1247     }
1248 }
1249
1250 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1251 {
1252     if( [o_notification object] == o_msgs_panel )
1253     {
1254         id o_msg;
1255         NSEnumerator * o_enum;
1256
1257         [o_messages setString: @""]; 
1258
1259         [o_msg_lock lock];
1260
1261         o_enum = [o_msg_arr objectEnumerator];
1262
1263         while( ( o_msg = [o_enum nextObject] ) != nil )
1264         {
1265             [o_messages insertText: o_msg];
1266         }
1267
1268         [o_msg_lock unlock];
1269     }
1270 }
1271
1272 @end
1273
1274 @implementation VLCMain (NSMenuValidation)
1275
1276 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1277 {
1278     BOOL bEnabled = TRUE;
1279
1280     /* Recent Items Menu */
1281
1282     if( [[o_mi title] isEqualToString: _NS("Clear Menu")] )
1283     {
1284         NSMenu * o_menu = [o_mi_open_recent submenu];
1285         int i_nb_items = [o_menu numberOfItems];
1286         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1287                                                        recentDocumentURLs];
1288         UInt32 i_nb_docs = [o_docs count];
1289
1290         if( i_nb_items > 1 )
1291         {
1292             while( --i_nb_items )
1293             {
1294                 [o_menu removeItemAtIndex: 0];
1295             }
1296         }
1297
1298         if( i_nb_docs > 0 )
1299         {
1300             NSURL * o_url;
1301             NSString * o_doc;
1302
1303             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1304
1305             while( TRUE )
1306             {
1307                 i_nb_docs--;
1308
1309                 o_url = [o_docs objectAtIndex: i_nb_docs];
1310
1311                 if( [o_url isFileURL] )
1312                 {
1313                     o_doc = [o_url path];
1314                 }
1315                 else
1316                 {
1317                     o_doc = [o_url absoluteString];
1318                 }
1319
1320                 [o_menu insertItemWithTitle: o_doc
1321                     action: @selector(openRecentItem:)
1322                     keyEquivalent: @"" atIndex: 0]; 
1323
1324                 if( i_nb_docs == 0 )
1325                 {
1326                     break;
1327                 }
1328             } 
1329         }
1330         else
1331         {
1332             bEnabled = FALSE;
1333         }
1334     }
1335
1336     return( bEnabled );
1337 }
1338
1339 @end
1340
1341 @implementation VLCMain (Internal)
1342
1343 - (void)handlePortMessage:(NSPortMessage *)o_msg
1344 {
1345     id ** val;
1346     NSData * o_data;
1347     NSValue * o_value;
1348     NSInvocation * o_inv;
1349     NSConditionLock * o_lock;
1350  
1351     o_data = [[o_msg components] lastObject];
1352     o_inv = *((NSInvocation **)[o_data bytes]); 
1353     [o_inv getArgument: &o_value atIndex: 2];
1354     val = (id **)[o_value pointerValue];
1355     [o_inv setArgument: val[1] atIndex: 2];
1356     o_lock = *(val[0]);
1357
1358     [o_lock lock];
1359     [o_inv invoke];
1360     [o_lock unlockWithCondition: 1];
1361 }
1362
1363 @end