]> git.sesse.net Git - vlc/blob - modules/gui/macosx/intf.m
macosx: improve main window initialization
[vlc] / modules / gui / macosx / intf.m
1 /*****************************************************************************
2  * intf.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <hartman at videolan.org>
10  *          Felix Paul Kühne <fkuehne at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <sys/param.h>                                    /* for MAXPATHLEN */
36 #include <string.h>
37 #include <vlc_common.h>
38 #include <vlc_keys.h>
39 #include <vlc_dialog.h>
40 #include <vlc_url.h>
41 #include <vlc_modules.h>
42 #include <vlc_plugin.h>
43 #include <vlc_aout_intf.h>
44 #include <vlc_vout_window.h>
45 #include <vlc_vout_display.h>
46 #include <unistd.h> /* execl() */
47
48 #import "CompatibilityFixes.h"
49 #import "intf.h"
50 #import "MainMenu.h"
51 #import "VideoView.h"
52 #import "prefs.h"
53 #import "playlist.h"
54 #import "playlistinfo.h"
55 #import "controls.h"
56 #import "open.h"
57 #import "wizard.h"
58 #import "bookmarks.h"
59 #import "coredialogs.h"
60 #import "AppleRemote.h"
61 #import "eyetv.h"
62 #import "simple_prefs.h"
63 #import "CoreInteraction.h"
64 #import "TrackSynchronization.h"
65
66 #import <AddressBook/AddressBook.h>         /* for crashlog send mechanism */
67 #import <Sparkle/Sparkle.h>                 /* we're the update delegate */
68
69 /*****************************************************************************
70  * Local prototypes.
71  *****************************************************************************/
72 static void Run ( intf_thread_t *p_intf );
73
74 static void updateProgressPanel (void *, const char *, float);
75 static bool checkProgressPanel (void *);
76 static void destroyProgressPanel (void *);
77
78 static void MsgCallback( void *data, int type, const msg_item_t *item, const char *format, va_list ap );
79
80 static int InputEvent( vlc_object_t *, const char *,
81                       vlc_value_t, vlc_value_t, void * );
82 static int PLItemChanged( vlc_object_t *, const char *,
83                          vlc_value_t, vlc_value_t, void * );
84 static int PlaylistUpdated( vlc_object_t *, const char *,
85                            vlc_value_t, vlc_value_t, void * );
86 static int PlaybackModeUpdated( vlc_object_t *, const char *,
87                                vlc_value_t, vlc_value_t, void * );
88 static int VolumeUpdated( vlc_object_t *, const char *,
89                          vlc_value_t, vlc_value_t, void * );
90
91 #pragma mark -
92 #pragma mark VLC Interface Object Callbacks
93
94 /*****************************************************************************
95  * OpenIntf: initialize interface
96  *****************************************************************************/
97 int OpenIntf ( vlc_object_t *p_this )
98 {
99     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
100     [VLCApplication sharedApplication];
101
102     intf_thread_t *p_intf = (intf_thread_t*) p_this;
103
104     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
105     if( p_intf->p_sys == NULL )
106         return VLC_ENOMEM;
107
108     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
109
110     /* subscribe to LibVLCCore's messages */
111     vlc_Subscribe( &p_intf->p_sys->sub, MsgCallback, NULL );
112
113     Run( p_intf );
114
115     [o_pool release];
116     return VLC_SUCCESS;
117 }
118
119 /*****************************************************************************
120  * CloseIntf: destroy interface
121  *****************************************************************************/
122 void CloseIntf ( vlc_object_t *p_this )
123 {
124     intf_thread_t *p_intf = (intf_thread_t*) p_this;
125
126     free( p_intf->p_sys );
127 }
128
129 static int WindowControl( vout_window_t *, int i_query, va_list );
130
131 int WindowOpen( vout_window_t *p_wnd, const vout_window_cfg_t *cfg )
132 {
133     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
134     intf_thread_t *p_intf = VLCIntf;
135     if (!p_intf) {
136         msg_Err( p_wnd, "Mac OS X interface not found" );
137         return VLC_EGENERIC;
138     }
139
140     int i_x = cfg->x;
141     int i_y = cfg->y;
142     unsigned i_width = cfg->width;
143     unsigned i_height = cfg->height;
144     p_wnd->handle.nsobject = [[VLCMain sharedInstance] getVideoViewAtPositionX: &i_x Y: &i_y withWidth: &i_width andHeight: &i_height];
145
146     if ( !p_wnd->handle.nsobject ) {
147         msg_Err( p_wnd, "got no video view from the interface" );
148         [o_pool release];
149         return VLC_EGENERIC;
150     }
151
152     [[VLCMain sharedInstance] setNativeVideoSize:NSMakeSize( cfg->width, cfg->height )];
153     [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
154     p_wnd->control = WindowControl;
155     p_wnd->sys = (vout_window_sys_t *)VLCIntf;
156     [o_pool release];
157     return VLC_SUCCESS;
158 }
159
160 static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
161 {
162     switch( i_query )
163     {
164         case VOUT_WINDOW_SET_STATE:
165         {
166             unsigned i_state = va_arg( args, unsigned );
167             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(setWindowLevel:) withObject:[NSNumber numberWithUnsignedInt:i_state] waitUntilDone:NO];
168             return VLC_SUCCESS;
169         }
170         case VOUT_WINDOW_SET_SIZE:
171         {
172             unsigned int i_width  = va_arg( args, unsigned int );
173             unsigned int i_height = va_arg( args, unsigned int );
174             [[VLCMain sharedInstance] setNativeVideoSize:NSMakeSize( i_width, i_height )];
175             return VLC_SUCCESS;
176         }
177         case VOUT_WINDOW_SET_FULLSCREEN:
178         {
179             NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
180             int i_full = va_arg( args, int );
181             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(checkFullscreenChange:) withObject:[NSNumber numberWithInt: i_full] waitUntilDone:NO];
182             [o_pool release];
183             return VLC_SUCCESS;
184         }
185         default:
186             msg_Warn( p_wnd, "unsupported control query" );
187             return VLC_EGENERIC;
188     }
189 }
190
191 void WindowClose( vout_window_t *p_wnd )
192 {
193     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
194     [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
195
196     [o_pool release];
197 }
198
199 /*****************************************************************************
200  * Run: main loop
201  *****************************************************************************/
202 static NSLock * o_appLock = nil;    // controls access to f_appExit
203
204 static void Run( intf_thread_t *p_intf )
205 {
206     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
207     [VLCApplication sharedApplication];
208
209     o_appLock = [[NSLock alloc] init];
210
211     [[VLCMain sharedInstance] setIntf: p_intf];
212     [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
213
214     [NSApp run];
215     [[VLCMain sharedInstance] applicationWillTerminate:nil];
216     [o_appLock release];
217     [o_pool release];
218
219     raise(SIGTERM);
220 }
221
222 #pragma mark -
223 #pragma mark Variables Callback
224
225 /*****************************************************************************
226  * MsgCallback: Callback triggered by the core once a new debug message is
227  * ready to be displayed. We store everything in a NSArray in our Cocoa part
228  * of this file.
229  *****************************************************************************/
230 static void MsgCallback( void *data, int type, const msg_item_t *item, const char *format, va_list ap )
231 {
232     int canc = vlc_savecancel();
233     char *str;
234
235     if (vasprintf( &str, format, ap ) == -1)
236     {
237         vlc_restorecancel( canc );
238         return;
239     }
240
241     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
242     [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
243     [o_pool release];
244
245     vlc_restorecancel( canc );
246     free( str );
247 }
248
249 static int InputEvent( vlc_object_t *p_this, const char *psz_var,
250                        vlc_value_t oldval, vlc_value_t new_val, void *param )
251 {
252     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
253     switch (new_val.i_int) {
254         case INPUT_EVENT_STATE:
255             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackStatusUpdated) withObject: nil waitUntilDone:NO];
256             break;
257         case INPUT_EVENT_RATE:
258             [[[VLCMain sharedInstance] mainMenu] performSelectorOnMainThread:@selector(updatePlaybackRate) withObject: nil waitUntilDone:NO];
259             break;
260         case INPUT_EVENT_POSITION:
261             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject: nil waitUntilDone:NO];
262             break;
263         case INPUT_EVENT_TITLE:
264         case INPUT_EVENT_CHAPTER:
265             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
266             break;
267         case INPUT_EVENT_CACHE:
268             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
269             break;
270         case INPUT_EVENT_STATISTICS:
271             [[[VLCMain sharedInstance] info] performSelectorOnMainThread:@selector(updateStatistics) withObject: nil waitUntilDone: NO];
272             break;
273         case INPUT_EVENT_ES:
274             break;
275         case INPUT_EVENT_TELETEXT:
276             break;
277         case INPUT_EVENT_AOUT:
278             break;
279         case INPUT_EVENT_VOUT:
280             break;
281         case INPUT_EVENT_ITEM_META:
282         case INPUT_EVENT_ITEM_INFO:
283             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
284             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
285             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateInfoandMetaPanel) withObject: nil waitUntilDone:NO];
286             break;
287         case INPUT_EVENT_BOOKMARK:
288             break;
289         case INPUT_EVENT_RECORD:
290             [[VLCMain sharedInstance] updateRecordState: var_GetBool( p_this, "record" )];
291             break;
292         case INPUT_EVENT_PROGRAM:
293             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
294             break;
295         case INPUT_EVENT_ITEM_EPG:
296             break;
297         case INPUT_EVENT_SIGNAL:
298             break;
299
300         case INPUT_EVENT_ITEM_NAME:
301             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
302             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
303             break;
304
305         case INPUT_EVENT_AUDIO_DELAY:
306         case INPUT_EVENT_SUBTITLE_DELAY:
307             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
308             break;
309
310         case INPUT_EVENT_DEAD:
311             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
312             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
313             break;
314
315         case INPUT_EVENT_ABORT:
316             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
317             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
318             break;
319
320         default:
321             //msg_Warn( p_this, "unhandled input event (%lld)", new_val.i_int );
322             break;
323     }
324
325     [o_pool release];
326     return VLC_SUCCESS;
327 }
328
329 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
330                          vlc_value_t oldval, vlc_value_t new_val, void *param )
331 {
332     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
333     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(PlaylistItemChanged) withObject:nil waitUntilDone:NO];
334
335     [o_pool release];
336     return VLC_SUCCESS;
337 }
338
339 static int PlaylistUpdated( vlc_object_t *p_this, const char *psz_var,
340                          vlc_value_t oldval, vlc_value_t new_val, void *param )
341 {
342     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
343     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject:nil waitUntilDone:NO];
344
345     [o_pool release];
346     return VLC_SUCCESS;
347 }
348
349 static int PlaybackModeUpdated( vlc_object_t *p_this, const char *psz_var,
350                          vlc_value_t oldval, vlc_value_t new_val, void *param )
351 {
352     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
353     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackModeUpdated) withObject:nil waitUntilDone:NO];
354
355     [o_pool release];
356     return VLC_SUCCESS;
357 }
358
359 static int VolumeUpdated( vlc_object_t *p_this, const char *psz_var,
360                          vlc_value_t oldval, vlc_value_t new_val, void *param )
361 {
362     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
363     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateVolume) withObject:nil waitUntilDone:NO];
364
365     [o_pool release];
366     return VLC_SUCCESS;
367 }
368
369 /*****************************************************************************
370  * ShowController: Callback triggered by the show-intf playlist variable
371  * through the ShowIntf-control-intf, to let us show the controller-win;
372  * usually when in fullscreen-mode
373  *****************************************************************************/
374 static int ShowController( vlc_object_t *p_this, const char *psz_variable,
375                      vlc_value_t old_val, vlc_value_t new_val, void *param )
376 {
377     intf_thread_t * p_intf = VLCIntf;
378     if( p_intf && p_intf->p_sys )
379     {
380         playlist_t * p_playlist = pl_Get( p_intf );
381         BOOL b_fullscreen = var_GetBool( p_playlist, "fullscreen" );
382         if( strcmp(psz_variable, "intf-toggle-fscontrol") || b_fullscreen )
383         {
384             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
385         }
386         else
387         {
388             [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
389         }
390     }
391     return VLC_SUCCESS;
392 }
393
394 /*****************************************************************************
395  * FullscreenChanged: Callback triggered by the fullscreen-change playlist
396  * variable, to let the intf update the controller.
397  *****************************************************************************/
398 static int FullscreenChanged( vlc_object_t *p_this, const char *psz_variable,
399                      vlc_value_t old_val, vlc_value_t new_val, void *param )
400 {
401     intf_thread_t * p_intf = VLCIntf;
402     if (p_intf)
403     {
404         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
405         [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(fullscreenChanged) withObject:nil waitUntilDone:NO];
406         [o_pool release];
407     }
408     return VLC_SUCCESS;
409 }
410
411 /*****************************************************************************
412  * DialogCallback: Callback triggered by the "dialog-*" variables
413  * to let the intf display error and interaction dialogs
414  *****************************************************************************/
415 static int DialogCallback( vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data )
416 {
417     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
418     VLCMain *interface = (VLCMain *)data;
419
420     if( [[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"] )
421     {
422         /* the progress panel needs to update itself and therefore wants special treatment within this context */
423         dialog_progress_bar_t *p_dialog = (dialog_progress_bar_t *)value.p_address;
424
425         p_dialog->pf_update = updateProgressPanel;
426         p_dialog->pf_check = checkProgressPanel;
427         p_dialog->pf_destroy = destroyProgressPanel;
428         p_dialog->p_sys = VLCIntf->p_libvlc;
429     }
430
431     NSValue *o_value = [NSValue valueWithPointer:value.p_address];
432     [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
433
434     [o_pool release];
435     return VLC_SUCCESS;
436 }
437
438 void updateProgressPanel (void *priv, const char *text, float value)
439 {
440     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
441
442     NSString *o_txt;
443     if( text != NULL )
444         o_txt = [NSString stringWithUTF8String: text];
445     else
446         o_txt = @"";
447
448     [[[VLCMain sharedInstance] coreDialogProvider] updateProgressPanelWithText: o_txt andNumber: (double)(value * 1000.)];
449
450     [o_pool release];
451 }
452
453 void destroyProgressPanel (void *priv)
454 {
455     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
456     [[[VLCMain sharedInstance] coreDialogProvider] destroyProgressPanel];
457     [o_pool release];
458 }
459
460 bool checkProgressPanel (void *priv)
461 {
462     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
463     return [[[VLCMain sharedInstance] coreDialogProvider] progressCancelled];
464     [o_pool release];
465 }
466
467 #pragma mark -
468 #pragma mark Helpers
469
470 input_thread_t *getInput(void)
471 {
472     intf_thread_t *p_intf = VLCIntf;
473     if (!p_intf)
474         return NULL;
475     return pl_CurrentInput(p_intf);
476 }
477
478 vout_thread_t *getVout(void)
479 {
480     input_thread_t *p_input = getInput();
481     if (!p_input)
482         return NULL;
483     vout_thread_t *p_vout = input_GetVout(p_input);
484     vlc_object_release(p_input);
485     return p_vout;
486 }
487
488 audio_output_t *getAout(void)
489 {
490     input_thread_t *p_input = getInput();
491     if (!p_input)
492         return NULL;
493     audio_output_t *p_aout = input_GetAout(p_input);
494     vlc_object_release(p_input);
495     return p_aout;
496 }
497
498 #pragma mark -
499 #pragma mark Private
500
501 @interface VLCMain ()
502 - (void)_removeOldPreferences;
503 @end
504
505 /*****************************************************************************
506  * VLCMain implementation
507  *****************************************************************************/
508 @implementation VLCMain
509
510 #pragma mark -
511 #pragma mark Initialization
512
513 static VLCMain *_o_sharedMainInstance = nil;
514
515 + (VLCMain *)sharedInstance
516 {
517     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
518 }
519
520 - (id)init
521 {
522     if( _o_sharedMainInstance)
523     {
524         [self dealloc];
525         return _o_sharedMainInstance;
526     }
527     else
528         _o_sharedMainInstance = [super init];
529
530     p_intf = NULL;
531     p_current_input = NULL;
532
533     o_msg_lock = [[NSLock alloc] init];
534     o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
535
536     o_open = [[VLCOpen alloc] init];
537     //o_embedded_list = [[VLCEmbeddedList alloc] init];
538     o_coredialogs = [[VLCCoreDialogProvider alloc] init];
539     o_info = [[VLCInfo alloc] init];
540     o_mainmenu = [[VLCMainMenu alloc] init];
541     o_coreinteraction = [[VLCCoreInteraction alloc] init];
542     o_eyetv = [[VLCEyeTVController alloc] init];
543     o_mainwindow = [[VLCMainWindow alloc] init];
544
545     /* announce our launch to a potential eyetv plugin */
546     [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
547                                                                    object: @"VLCEyeTVSupport"
548                                                                  userInfo: NULL
549                                                        deliverImmediately: YES];
550
551     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
552     NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"LiveUpdateTheMessagesPanel"];
553     [defaults registerDefaults:appDefaults];
554
555     return _o_sharedMainInstance;
556 }
557
558 - (void)setIntf: (intf_thread_t *)p_mainintf {
559     p_intf = p_mainintf;
560 }
561
562 - (intf_thread_t *)intf {
563     return p_intf;
564 }
565
566 - (void)awakeFromNib
567 {
568     playlist_t *p_playlist;
569     vlc_value_t val;
570     if( !p_intf ) return;
571     var_Create( p_intf, "intf-change", VLC_VAR_BOOL );
572
573     /* Check if we already did this once. Opening the other nibs calls it too,
574      because VLCMain is the owner */
575     if( nib_main_loaded ) return;
576
577     [o_msgs_panel setExcludedFromWindowsMenu: YES];
578     [o_msgs_panel setDelegate: self];
579
580     p_playlist = pl_Get( p_intf );
581
582     val.b_bool = false;
583
584     var_AddCallback(p_playlist, "fullscreen", FullscreenChanged, self);
585     var_AddCallback( p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
586     var_AddCallback( p_intf->p_libvlc, "intf-show", ShowController, self);
587     //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
588     var_AddCallback(p_playlist, "item-current", PLItemChanged, self);
589     var_AddCallback(p_playlist, "activity", PLItemChanged, self);
590     var_AddCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
591     var_AddCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
592     var_AddCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
593     var_AddCallback(p_playlist, "random", PlaybackModeUpdated, self);
594     var_AddCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
595     var_AddCallback(p_playlist, "loop", PlaybackModeUpdated, self);
596     var_AddCallback(p_playlist, "volume", VolumeUpdated, self);
597     var_AddCallback(p_playlist, "mute", VolumeUpdated, self);
598
599     if (OSX_LION)
600     {
601         if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
602             var_SetBool( p_playlist, "fullscreen", YES );
603     }
604
605     /* load our Core Dialogs nib */
606     nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
607
608     /* subscribe to various interactive dialogues */
609     var_Create( p_intf, "dialog-error", VLC_VAR_ADDRESS );
610     var_AddCallback( p_intf, "dialog-error", DialogCallback, self );
611     var_Create( p_intf, "dialog-critical", VLC_VAR_ADDRESS );
612     var_AddCallback( p_intf, "dialog-critical", DialogCallback, self );
613     var_Create( p_intf, "dialog-login", VLC_VAR_ADDRESS );
614     var_AddCallback( p_intf, "dialog-login", DialogCallback, self );
615     var_Create( p_intf, "dialog-question", VLC_VAR_ADDRESS );
616     var_AddCallback( p_intf, "dialog-question", DialogCallback, self );
617     var_Create( p_intf, "dialog-progress-bar", VLC_VAR_ADDRESS );
618     var_AddCallback( p_intf, "dialog-progress-bar", DialogCallback, self );
619     dialog_Register( p_intf );
620
621     /* init Apple Remote support */
622     o_remote = [[AppleRemote alloc] init];
623     [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
624     [o_remote setDelegate: _o_sharedMainInstance];
625
626     [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
627
628      BOOL b_video_deco = var_InheritBool( VLCIntf, "video-deco" );
629     /* yeah, we are done */
630     b_nativeFullscreenMode = NO;
631 #ifdef MAC_OS_X_VERSION_10_7
632     if( OSX_LION && b_video_deco )
633         b_nativeFullscreenMode = var_InheritBool( p_intf, "macosx-nativefullscreenmode" );
634 #endif
635
636     /* recover stored audio device, if set
637      * in case it was unplugged in the meantime, auhal will fall back on the default */
638     int i_value = config_GetInt( p_intf, "macosx-audio-device" );
639     if (i_value > 0)
640         var_SetInteger( pl_Get( VLCIntf ), "audio-device", i_value );
641
642     if (config_GetInt( VLCIntf, "macosx-icon-change"))
643     {
644         /* After day 354 of the year, the usual VLC cone is replaced by another cone
645          * wearing a Father Xmas hat.
646          * Note: this icon doesn't represent an endorsement of The Coca-Cola Company.
647          */
648         NSCalendar *gregorian =
649         [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
650         NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
651         [gregorian release];
652
653         if (dayOfYear >= 354)
654             [[VLCApplication sharedApplication] setApplicationIconImage: [NSImage imageNamed:@"vlc-xmas"]];
655     }
656
657     [self initStrings];
658
659     nib_main_loaded = TRUE;
660 }
661
662 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
663 {
664     if( !p_intf ) return;
665
666     [self updateCurrentlyUsedHotkeys];
667
668     /* init media key support */
669     b_mediaKeySupport = var_InheritBool( VLCIntf, "macosx-mediakeys" );
670     if( b_mediaKeySupport )
671     {
672         o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
673         [o_mediaKeyController startWatchingMediaKeys];
674         [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
675                                                                  [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
676                                                                  nil]];
677     }
678     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];
679
680     [self _removeOldPreferences];
681
682     /* Handle sleep notification */
683     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
684            name:NSWorkspaceWillSleepNotification object:nil];
685
686     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(lookForCrashLog) withObject:nil waitUntilDone:NO];
687
688     /* we will need this, so let's load it here so the interface appears to be more responsive */
689     nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner: NSApp];
690
691     /* update the main window */
692     [o_mainwindow updateWindow];
693     [o_mainwindow updateTimeSlider];
694     [o_mainwindow updateVolumeSlider];
695 }
696
697 - (void)initStrings
698 {
699     if( !p_intf ) return;
700
701     /* messages panel */
702     [o_msgs_panel setTitle: _NS("Messages")];
703     [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
704     [o_msgs_save_btn setTitle: _NS("Save this Log...")];
705
706     /* crash reporter panel */
707     [o_crashrep_send_btn setTitle: _NS("Send")];
708     [o_crashrep_dontSend_btn setTitle: _NS("Don't Send")];
709     [o_crashrep_title_txt setStringValue: _NS("VLC crashed previously")];
710     [o_crashrep_win setTitle: _NS("VLC crashed previously")];
711     [o_crashrep_desc_txt setStringValue: _NS("Do you want to send details on the crash to VLC's development team?\n\nIf you want, you can enter a few lines on what you did before VLC crashed along with other helpful information: a link to download a sample file, a URL of a network stream, ...")];
712     [o_crashrep_includeEmail_ckb setTitle: _NS("I agree to be possibly contacted about this bugreport.")];
713     [o_crashrep_includeEmail_txt setStringValue: _NS("Only your default E-Mail address will be submitted, including no further information.")];
714     [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
715 }
716
717 #pragma mark -
718 #pragma mark Termination
719
720 - (void)applicationWillTerminate:(NSNotification *)notification
721 {
722     /* don't allow a double termination call. If the user has
723      * already invoked the quit then simply return this time. */
724     static bool f_appExit = false;
725     bool isTerminating;
726
727     [o_appLock lock];
728     isTerminating = f_appExit;
729     f_appExit = true;
730     [o_appLock unlock];
731
732     if (isTerminating)
733         return;
734
735     if (notification == nil)
736         [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
737
738     playlist_t * p_playlist = pl_Get( p_intf );
739     int returnedValue = 0;
740
741     /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
742     if (b_nativeFullscreenMode)
743     {
744         [o_mainwindow toggleFullScreen: self];
745         [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
746     }
747
748     /* Save some interface state in configuration, at module quit */
749     config_PutInt( p_intf, "random", var_GetBool( p_playlist, "random" ) );
750     config_PutInt( p_intf, "loop", var_GetBool( p_playlist, "loop" ) );
751     config_PutInt( p_intf, "repeat", var_GetBool( p_playlist, "repeat" ) );
752
753     msg_Dbg( p_intf, "Terminating" );
754
755     /* unsubscribe from the interactive dialogues */
756     dialog_Unregister( p_intf );
757     var_DelCallback( p_intf, "dialog-error", DialogCallback, self );
758     var_DelCallback( p_intf, "dialog-critical", DialogCallback, self );
759     var_DelCallback( p_intf, "dialog-login", DialogCallback, self );
760     var_DelCallback( p_intf, "dialog-question", DialogCallback, self );
761     var_DelCallback( p_intf, "dialog-progress-bar", DialogCallback, self );
762     //var_DelCallback(p_playlist, "item-change", PLItemChanged, self);
763     var_DelCallback(p_playlist, "item-current", PLItemChanged, self);
764     var_DelCallback(p_playlist, "activity", PLItemChanged, self);
765     var_DelCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
766     var_DelCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
767     var_DelCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
768     var_DelCallback(p_playlist, "random", PlaybackModeUpdated, self);
769     var_DelCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
770     var_DelCallback(p_playlist, "loop", PlaybackModeUpdated, self);
771     var_DelCallback(p_playlist, "volume", VolumeUpdated, self);
772     var_DelCallback(p_playlist, "mute", VolumeUpdated, self);
773     var_DelCallback(p_playlist, "fullscreen", FullscreenChanged, self);
774     var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
775     var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
776
777     if( p_current_input )
778     {
779         var_DelCallback( p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance] );
780         vlc_object_release( p_current_input );
781         p_current_input = NULL;
782     }
783
784     /* remove global observer watching for vout device changes correctly */
785     [[NSNotificationCenter defaultCenter] removeObserver: self];
786
787     /* release some other objects here, because it isn't sure whether dealloc
788      * will be called later on */
789     if( o_sprefs )
790         [o_sprefs release];
791
792     if( o_prefs )
793         [o_prefs release];
794
795     [o_open release];
796
797     if( o_info )
798         [o_info release];
799
800     if( o_wizard )
801         [o_wizard release];
802
803     [crashLogURLConnection cancel];
804     [crashLogURLConnection release];
805
806     [o_embedded_list release];
807     [o_coredialogs release];
808     [o_eyetv release];
809
810     /* unsubscribe from libvlc's debug messages */
811     vlc_Unsubscribe( &p_intf->p_sys->sub );
812
813     [o_msg_arr removeAllObjects];
814     [o_msg_arr release];
815     o_msg_arr = NULL;
816     [o_usedHotkeys release];
817     o_usedHotkeys = NULL;
818
819     [o_msg_lock release];
820
821     /* write cached user defaults to disk */
822     [[NSUserDefaults standardUserDefaults] synchronize];
823
824     /* Make sure the Menu doesn't have any references to vlc objects anymore */
825     //FIXME: this should be moved to VLCMainMenu
826     [o_mainmenu releaseRepresentedObjects:[NSApp mainMenu]];
827     [o_mainmenu release];
828
829     libvlc_Quit( p_intf->p_libvlc );
830
831     [o_mainwindow release];
832     o_mainwindow = NULL;
833
834     [self setIntf:nil];
835 }
836
837 #pragma mark -
838 #pragma mark Sparkle delegate
839 /* received directly before the update gets installed, so let's shut down a bit */
840 - (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
841 {
842     [NSApp activateIgnoringOtherApps:YES];
843     [o_remote stopListening: self];
844     [[VLCCoreInteraction sharedInstance] stop];
845 }
846
847 #pragma mark -
848 #pragma mark Media Key support
849
850 -(void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event
851 {
852     if( b_mediaKeySupport )
853        {
854         assert([event type] == NSSystemDefined && [event subtype] == SPSystemDefinedEventMediaKeys);
855
856         int keyCode = (([event data1] & 0xFFFF0000) >> 16);
857         int keyFlags = ([event data1] & 0x0000FFFF);
858         int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
859         int keyRepeat = (keyFlags & 0x1);
860
861         if( keyCode == NX_KEYTYPE_PLAY && keyState == 0 )
862             [[VLCCoreInteraction sharedInstance] play];
863
864         if( (keyCode == NX_KEYTYPE_FAST || keyCode == NX_KEYTYPE_NEXT) && !b_mediakeyJustJumped )
865         {
866             if( keyState == 0 && keyRepeat == 0 )
867                 [[VLCCoreInteraction sharedInstance] next];
868             else if( keyRepeat == 1 )
869             {
870                 [[VLCCoreInteraction sharedInstance] forwardShort];
871                 b_mediakeyJustJumped = YES;
872                 [self performSelector:@selector(resetMediaKeyJump)
873                            withObject: NULL
874                            afterDelay:0.25];
875             }
876         }
877
878         if( (keyCode == NX_KEYTYPE_REWIND || keyCode == NX_KEYTYPE_PREVIOUS) && !b_mediakeyJustJumped )
879         {
880             if( keyState == 0 && keyRepeat == 0 )
881                 [[VLCCoreInteraction sharedInstance] previous];
882             else if( keyRepeat == 1 )
883             {
884                 [[VLCCoreInteraction sharedInstance] backwardShort];
885                 b_mediakeyJustJumped = YES;
886                 [self performSelector:@selector(resetMediaKeyJump)
887                            withObject: NULL
888                            afterDelay:0.25];
889             }
890         }
891     }
892 }
893
894 #pragma mark -
895 #pragma mark Other notification
896
897 /* Listen to the remote in exclusive mode, only when VLC is the active
898    application */
899 - (void)applicationDidBecomeActive:(NSNotification *)aNotification
900 {
901     if( !p_intf ) return;
902     if( var_InheritBool( p_intf, "macosx-appleremote" ) == YES )
903         [o_remote startListening: self];
904 }
905 - (void)applicationDidResignActive:(NSNotification *)aNotification
906 {
907     if( !p_intf ) return;
908     [o_remote stopListening: self];
909 }
910
911 /* Triggered when the computer goes to sleep */
912 - (void)computerWillSleep: (NSNotification *)notification
913 {
914     [[VLCCoreInteraction sharedInstance] pause];
915 }
916
917 #pragma mark -
918 #pragma mark File opening over dock icon
919
920 - (void)application:(NSApplication *)o_app openFiles:(NSArray *)o_names
921 {
922     BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
923     char *psz_uri = vlc_path2uri([[o_names objectAtIndex:0] UTF8String], "file" );
924
925     // try to add file as subtitle
926     if( [o_names count] == 1 && psz_uri )
927     {
928         input_thread_t * p_input = pl_CurrentInput( VLCIntf );
929         if( p_input )
930         {
931             BOOL b_returned = NO;
932             b_returned = input_AddSubtitle( p_input, psz_uri, true );
933             vlc_object_release( p_input );
934             if( !b_returned )
935             {
936                 free( psz_uri );
937                 return;
938             }
939         }
940     }
941     free( psz_uri );
942
943     NSArray *o_sorted_names = [o_names sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
944     NSMutableArray *o_result = [NSMutableArray arrayWithCapacity: [o_sorted_names count]];
945     for( int i = 0; i < [o_sorted_names count]; i++ )
946     {
947         psz_uri = vlc_path2uri([[o_sorted_names objectAtIndex: i] UTF8String], "file" );
948         if( !psz_uri )
949             continue;
950
951         NSDictionary *o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
952         free( psz_uri );
953         [o_result addObject: o_dic];
954     }
955
956     if( b_autoplay )
957         [o_playlist appendArray: o_result atPos: -1 enqueue: NO];
958     else
959         [o_playlist appendArray: o_result atPos: -1 enqueue: YES];
960
961     return;
962 }
963
964 /* When user click in the Dock icon our double click in the finder */
965 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)hasVisibleWindows
966 {
967     if(!hasVisibleWindows)
968         [o_mainwindow makeKeyAndOrderFront:self];
969
970     return YES;
971 }
972
973 #pragma mark -
974 #pragma mark Apple Remote Control
975
976 /* Helper method for the remote control interface in order to trigger forward/backward and volume
977    increase/decrease as long as the user holds the left/right, plus/minus button */
978 - (void) executeHoldActionForRemoteButton: (NSNumber*) buttonIdentifierNumber
979 {
980     if(b_remote_button_hold)
981     {
982         switch([buttonIdentifierNumber intValue])
983         {
984             case kRemoteButtonRight_Hold:
985                 [[VLCCoreInteraction sharedInstance] forward];
986                 break;
987             case kRemoteButtonLeft_Hold:
988                 [[VLCCoreInteraction sharedInstance] backward];
989                 break;
990             case kRemoteButtonVolume_Plus_Hold:
991                 if( p_intf )
992                     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_UP );
993                 break;
994             case kRemoteButtonVolume_Minus_Hold:
995                 if( p_intf )
996                     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_DOWN );
997                 break;
998         }
999         if(b_remote_button_hold)
1000         {
1001             /* trigger event */
1002             [self performSelector:@selector(executeHoldActionForRemoteButton:)
1003                          withObject:buttonIdentifierNumber
1004                          afterDelay:0.25];
1005         }
1006     }
1007 }
1008
1009 /* Apple Remote callback */
1010 - (void) appleRemoteButton: (AppleRemoteEventIdentifier)buttonIdentifier
1011                pressedDown: (BOOL) pressedDown
1012                 clickCount: (unsigned int) count
1013 {
1014     switch( buttonIdentifier )
1015     {
1016         case k2009RemoteButtonFullscreen:
1017             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
1018             break;
1019         case k2009RemoteButtonPlay:
1020             [[VLCCoreInteraction sharedInstance] play];
1021             break;
1022         case kRemoteButtonPlay:
1023             if(count >= 2) {
1024                 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
1025             } else {
1026                 [[VLCCoreInteraction sharedInstance] play];
1027             }
1028             break;
1029         case kRemoteButtonVolume_Plus:
1030             if (config_GetInt( VLCIntf, "macosx-appleremote-sysvol"))
1031                 [NSSound increaseSystemVolume];
1032             else
1033                 if( p_intf )
1034                     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_UP );
1035             break;
1036         case kRemoteButtonVolume_Minus:
1037             if (config_GetInt( VLCIntf, "macosx-appleremote-sysvol"))
1038                 [NSSound decreaseSystemVolume];
1039             else
1040                 if( p_intf )
1041                     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_DOWN );
1042             break;
1043         case kRemoteButtonRight:
1044             [[VLCCoreInteraction sharedInstance] next];
1045             break;
1046         case kRemoteButtonLeft:
1047             [[VLCCoreInteraction sharedInstance] previous];
1048             break;
1049         case kRemoteButtonRight_Hold:
1050         case kRemoteButtonLeft_Hold:
1051         case kRemoteButtonVolume_Plus_Hold:
1052         case kRemoteButtonVolume_Minus_Hold:
1053             /* simulate an event as long as the user holds the button */
1054             b_remote_button_hold = pressedDown;
1055             if( pressedDown )
1056             {
1057                 NSNumber* buttonIdentifierNumber = [NSNumber numberWithInt: buttonIdentifier];
1058                 [self performSelector:@selector(executeHoldActionForRemoteButton:)
1059                            withObject:buttonIdentifierNumber];
1060             }
1061             break;
1062         case kRemoteButtonMenu:
1063             [o_controls showPosition: self]; //FIXME
1064             break;
1065         case kRemoteButtonPlay_Sleep:
1066         {
1067             NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
1068             [script executeAndReturnError:nil];
1069             [script release];
1070             break;
1071         }
1072         default:
1073             /* Add here whatever you want other buttons to do */
1074             break;
1075     }
1076 }
1077
1078 #pragma mark -
1079 #pragma mark String utility
1080 // FIXME: this has nothing to do here
1081
1082 - (NSString *)localizedString:(const char *)psz
1083 {
1084     NSString * o_str = nil;
1085
1086     if( psz != NULL )
1087     {
1088         o_str = [NSString stringWithCString: _(psz) encoding:NSUTF8StringEncoding];
1089
1090         if( o_str == NULL )
1091         {
1092             msg_Err( VLCIntf, "could not translate: %s", psz );
1093             return( @"" );
1094         }
1095     }
1096     else
1097     {
1098         msg_Warn( VLCIntf, "can't translate empty strings" );
1099         return( @"" );
1100     }
1101
1102     return( o_str );
1103 }
1104
1105
1106
1107 - (char *)delocalizeString:(NSString *)id
1108 {
1109     NSData * o_data = [id dataUsingEncoding: NSUTF8StringEncoding
1110                           allowLossyConversion: NO];
1111     char * psz_string;
1112
1113     if( o_data == nil )
1114     {
1115         o_data = [id dataUsingEncoding: NSUTF8StringEncoding
1116                      allowLossyConversion: YES];
1117         psz_string = malloc( [o_data length] + 1 );
1118         [o_data getBytes: psz_string];
1119         psz_string[ [o_data length] ] = '\0';
1120         msg_Err( VLCIntf, "cannot convert to the requested encoding: %s",
1121                  psz_string );
1122     }
1123     else
1124     {
1125         psz_string = malloc( [o_data length] + 1 );
1126         [o_data getBytes: psz_string];
1127         psz_string[ [o_data length] ] = '\0';
1128     }
1129
1130     return psz_string;
1131 }
1132
1133 /* i_width is in pixels */
1134 - (NSString *)wrapString: (NSString *)o_in_string toWidth: (int) i_width
1135 {
1136     NSMutableString *o_wrapped;
1137     NSString *o_out_string;
1138     NSRange glyphRange, effectiveRange, charRange;
1139     NSRect lineFragmentRect;
1140     unsigned glyphIndex, breaksInserted = 0;
1141
1142     NSTextStorage *o_storage = [[NSTextStorage alloc] initWithString: o_in_string
1143         attributes: [NSDictionary dictionaryWithObjectsAndKeys:
1144         [NSFont labelFontOfSize: 0.0], NSFontAttributeName, nil]];
1145     NSLayoutManager *o_layout_manager = [[NSLayoutManager alloc] init];
1146     NSTextContainer *o_container = [[NSTextContainer alloc]
1147         initWithContainerSize: NSMakeSize(i_width, 2000)];
1148
1149     [o_layout_manager addTextContainer: o_container];
1150     [o_container release];
1151     [o_storage addLayoutManager: o_layout_manager];
1152     [o_layout_manager release];
1153
1154     o_wrapped = [o_in_string mutableCopy];
1155     glyphRange = [o_layout_manager glyphRangeForTextContainer: o_container];
1156
1157     for( glyphIndex = glyphRange.location ; glyphIndex < NSMaxRange(glyphRange) ;
1158             glyphIndex += effectiveRange.length) {
1159         lineFragmentRect = [o_layout_manager lineFragmentRectForGlyphAtIndex: glyphIndex
1160                                             effectiveRange: &effectiveRange];
1161         charRange = [o_layout_manager characterRangeForGlyphRange: effectiveRange
1162                                     actualGlyphRange: &effectiveRange];
1163         if([o_wrapped lineRangeForRange:
1164                 NSMakeRange(charRange.location + breaksInserted, charRange.length)].length > charRange.length) {
1165             [o_wrapped insertString: @"\n" atIndex: NSMaxRange(charRange) + breaksInserted];
1166             breaksInserted++;
1167         }
1168     }
1169     o_out_string = [NSString stringWithString: o_wrapped];
1170     [o_wrapped release];
1171     [o_storage release];
1172
1173     return o_out_string;
1174 }
1175
1176
1177 #pragma mark -
1178 #pragma mark Key Shortcuts
1179
1180 static struct
1181 {
1182     unichar i_nskey;
1183     unsigned int i_vlckey;
1184 } nskeys_to_vlckeys[] =
1185 {
1186     { NSUpArrowFunctionKey, KEY_UP },
1187     { NSDownArrowFunctionKey, KEY_DOWN },
1188     { NSLeftArrowFunctionKey, KEY_LEFT },
1189     { NSRightArrowFunctionKey, KEY_RIGHT },
1190     { NSF1FunctionKey, KEY_F1 },
1191     { NSF2FunctionKey, KEY_F2 },
1192     { NSF3FunctionKey, KEY_F3 },
1193     { NSF4FunctionKey, KEY_F4 },
1194     { NSF5FunctionKey, KEY_F5 },
1195     { NSF6FunctionKey, KEY_F6 },
1196     { NSF7FunctionKey, KEY_F7 },
1197     { NSF8FunctionKey, KEY_F8 },
1198     { NSF9FunctionKey, KEY_F9 },
1199     { NSF10FunctionKey, KEY_F10 },
1200     { NSF11FunctionKey, KEY_F11 },
1201     { NSF12FunctionKey, KEY_F12 },
1202     { NSInsertFunctionKey, KEY_INSERT },
1203     { NSHomeFunctionKey, KEY_HOME },
1204     { NSEndFunctionKey, KEY_END },
1205     { NSPageUpFunctionKey, KEY_PAGEUP },
1206     { NSPageDownFunctionKey, KEY_PAGEDOWN },
1207     { NSMenuFunctionKey, KEY_MENU },
1208     { NSTabCharacter, KEY_TAB },
1209     { NSCarriageReturnCharacter, KEY_ENTER },
1210     { NSEnterCharacter, KEY_ENTER },
1211     { NSBackspaceCharacter, KEY_BACKSPACE },
1212     { NSDeleteCharacter, KEY_DELETE },
1213     {0,0}
1214 };
1215
1216 unsigned int CocoaKeyToVLC( unichar i_key )
1217 {
1218     unsigned int i;
1219
1220     for( i = 0; nskeys_to_vlckeys[i].i_nskey != 0; i++ )
1221     {
1222         if( nskeys_to_vlckeys[i].i_nskey == i_key )
1223         {
1224             return nskeys_to_vlckeys[i].i_vlckey;
1225         }
1226     }
1227     return (unsigned int)i_key;
1228 }
1229
1230 - (unsigned int)VLCModifiersToCocoa:(NSString *)theString
1231 {
1232     unsigned int new = 0;
1233
1234     if([theString rangeOfString:@"Command"].location != NSNotFound)
1235         new |= NSCommandKeyMask;
1236     if([theString rangeOfString:@"Alt"].location != NSNotFound)
1237         new |= NSAlternateKeyMask;
1238     if([theString rangeOfString:@"Shift"].location != NSNotFound)
1239         new |= NSShiftKeyMask;
1240     if([theString rangeOfString:@"Ctrl"].location != NSNotFound)
1241         new |= NSControlKeyMask;
1242     return new;
1243 }
1244
1245 - (NSString *)VLCKeyToString:(NSString *)theString
1246 {
1247     if (![theString isEqualToString:@""]) {
1248         if ([theString characterAtIndex:([theString length] - 1)] != 0x2b)
1249             theString = [theString stringByReplacingOccurrencesOfString:@"+" withString:@""];
1250         else
1251         {
1252             theString = [theString stringByReplacingOccurrencesOfString:@"+" withString:@""];
1253             theString = [NSString stringWithFormat:@"%@+", theString];
1254         }
1255         if ([theString characterAtIndex:([theString length] - 1)] != 0x2d)
1256             theString = [theString stringByReplacingOccurrencesOfString:@"-" withString:@""];
1257         else
1258         {
1259             theString = [theString stringByReplacingOccurrencesOfString:@"-" withString:@""];
1260             theString = [NSString stringWithFormat:@"%@-", theString];
1261         }
1262         theString = [theString stringByReplacingOccurrencesOfString:@"Command" withString:@""];
1263         theString = [theString stringByReplacingOccurrencesOfString:@"Alt" withString:@""];
1264         theString = [theString stringByReplacingOccurrencesOfString:@"Shift" withString:@""];
1265         theString = [theString stringByReplacingOccurrencesOfString:@"Ctrl" withString:@""];
1266     }
1267     if ([theString length] > 1)
1268     {
1269         if([theString rangeOfString:@"Up"].location != NSNotFound)
1270             return [NSString stringWithFormat:@"%C", NSUpArrowFunctionKey];
1271         else if([theString rangeOfString:@"Down"].location != NSNotFound)
1272             return [NSString stringWithFormat:@"%C", NSDownArrowFunctionKey];
1273         else if([theString rangeOfString:@"Right"].location != NSNotFound)
1274             return [NSString stringWithFormat:@"%C", NSRightArrowFunctionKey];
1275         else if([theString rangeOfString:@"Left"].location != NSNotFound)
1276             return [NSString stringWithFormat:@"%C", NSLeftArrowFunctionKey];
1277         else if([theString rangeOfString:@"Enter"].location != NSNotFound)
1278             return [NSString stringWithFormat:@"%C", NSEnterCharacter]; // we treat NSCarriageReturnCharacter as aquivalent
1279         else if([theString rangeOfString:@"Insert"].location != NSNotFound)
1280             return [NSString stringWithFormat:@"%C", NSInsertFunctionKey];
1281         else if([theString rangeOfString:@"Home"].location != NSNotFound)
1282             return [NSString stringWithFormat:@"%C", NSHomeFunctionKey];
1283         else if([theString rangeOfString:@"End"].location != NSNotFound)
1284             return [NSString stringWithFormat:@"%C", NSEndFunctionKey];
1285         else if([theString rangeOfString:@"Pageup"].location != NSNotFound)
1286             return [NSString stringWithFormat:@"%C", NSPageUpFunctionKey];
1287         else if([theString rangeOfString:@"Pagedown"].location != NSNotFound)
1288             return [NSString stringWithFormat:@"%C", NSPageDownFunctionKey];
1289         else if([theString rangeOfString:@"Menu"].location != NSNotFound)
1290             return [NSString stringWithFormat:@"%C", NSMenuFunctionKey];
1291         else if([theString rangeOfString:@"Tab"].location != NSNotFound)
1292             return [NSString stringWithFormat:@"%C", NSTabCharacter];
1293         else if([theString rangeOfString:@"Backspace"].location != NSNotFound)
1294             return [NSString stringWithFormat:@"%C", NSBackspaceCharacter];
1295         else if([theString rangeOfString:@"Delete"].location != NSNotFound)
1296             return [NSString stringWithFormat:@"%C", NSDeleteCharacter];
1297         else if([theString rangeOfString:@"F12"].location != NSNotFound)
1298             return [NSString stringWithFormat:@"%C", NSF12FunctionKey];
1299         else if([theString rangeOfString:@"F11"].location != NSNotFound)
1300             return [NSString stringWithFormat:@"%C", NSF11FunctionKey];
1301         else if([theString rangeOfString:@"F10"].location != NSNotFound)
1302             return [NSString stringWithFormat:@"%C", NSF10FunctionKey];
1303         else if([theString rangeOfString:@"F9"].location != NSNotFound)
1304             return [NSString stringWithFormat:@"%C", NSF9FunctionKey];
1305         else if([theString rangeOfString:@"F8"].location != NSNotFound)
1306             return [NSString stringWithFormat:@"%C", NSF8FunctionKey];
1307         else if([theString rangeOfString:@"F7"].location != NSNotFound)
1308             return [NSString stringWithFormat:@"%C", NSF7FunctionKey];
1309         else if([theString rangeOfString:@"F6"].location != NSNotFound)
1310             return [NSString stringWithFormat:@"%C", NSF6FunctionKey];
1311         else if([theString rangeOfString:@"F5"].location != NSNotFound)
1312             return [NSString stringWithFormat:@"%C", NSF5FunctionKey];
1313         else if([theString rangeOfString:@"F4"].location != NSNotFound)
1314             return [NSString stringWithFormat:@"%C", NSF4FunctionKey];
1315         else if([theString rangeOfString:@"F3"].location != NSNotFound)
1316             return [NSString stringWithFormat:@"%C", NSF3FunctionKey];
1317         else if([theString rangeOfString:@"F2"].location != NSNotFound)
1318             return [NSString stringWithFormat:@"%C", NSF2FunctionKey];
1319         else if([theString rangeOfString:@"F1"].location != NSNotFound)
1320             return [NSString stringWithFormat:@"%C", NSF1FunctionKey];
1321         /* note that we don't support esc here, since it is reserved for leaving fullscreen */
1322     }
1323
1324     return theString;
1325 }
1326
1327
1328 /*****************************************************************************
1329  * hasDefinedShortcutKey: Check to see if the key press is a defined VLC
1330  * shortcut key.  If it is, pass it off to VLC for handling and return YES,
1331  * otherwise ignore it and return NO (where it will get handled by Cocoa).
1332  *****************************************************************************/
1333 - (BOOL)hasDefinedShortcutKey:(NSEvent *)o_event force:(BOOL)b_force
1334 {
1335     unichar key = 0;
1336     vlc_value_t val;
1337     unsigned int i_pressed_modifiers = 0;
1338
1339     val.i_int = 0;
1340     i_pressed_modifiers = [o_event modifierFlags];
1341
1342     if( i_pressed_modifiers & NSControlKeyMask ) {
1343         val.i_int |= KEY_MODIFIER_CTRL;
1344     }
1345     if( i_pressed_modifiers & NSAlternateKeyMask ) {
1346         val.i_int |= KEY_MODIFIER_ALT;
1347     }
1348     if( i_pressed_modifiers & NSShiftKeyMask ) {
1349         val.i_int |= KEY_MODIFIER_SHIFT;
1350     }
1351     if( i_pressed_modifiers & NSCommandKeyMask ) {
1352         val.i_int |= KEY_MODIFIER_COMMAND;
1353     }
1354
1355     NSString * characters = [o_event charactersIgnoringModifiers];
1356     if ([characters length] > 0)
1357     {
1358         key = [[characters lowercaseString] characterAtIndex: 0];
1359
1360         /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
1361         if( key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask )
1362         {
1363             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
1364             return YES;
1365         }
1366
1367         if( !b_force )
1368         {
1369             switch( key )
1370             {
1371                 case NSDeleteCharacter:
1372                 case NSDeleteFunctionKey:
1373                 case NSDeleteCharFunctionKey:
1374                 case NSBackspaceCharacter:
1375                 case NSUpArrowFunctionKey:
1376                 case NSDownArrowFunctionKey:
1377                 case NSRightArrowFunctionKey:
1378                 case NSLeftArrowFunctionKey:
1379                 case NSEnterCharacter:
1380                 case NSCarriageReturnCharacter:
1381                     return NO;
1382             }
1383         }
1384
1385         if( key == 0x0020 ) // space key
1386         {
1387             [[VLCCoreInteraction sharedInstance] play];
1388             return YES;
1389         }
1390
1391         val.i_int |= CocoaKeyToVLC( key );
1392
1393         BOOL b_found_key = NO;
1394         for( int i = 0; i < [o_usedHotkeys count]; i++ )
1395         {
1396             NSString *str = [o_usedHotkeys objectAtIndex: i];
1397             unsigned int i_keyModifiers = [self VLCModifiersToCocoa: str];
1398
1399             if( [[characters lowercaseString] isEqualToString: [self VLCKeyToString: str]] &&
1400                (i_keyModifiers & NSShiftKeyMask)     == (i_pressed_modifiers & NSShiftKeyMask) &&
1401                (i_keyModifiers & NSControlKeyMask)   == (i_pressed_modifiers & NSControlKeyMask) &&
1402                (i_keyModifiers & NSAlternateKeyMask) == (i_pressed_modifiers & NSAlternateKeyMask) &&
1403                (i_keyModifiers & NSCommandKeyMask)   == (i_pressed_modifiers & NSCommandKeyMask) )
1404             {
1405                 b_found_key = YES;
1406                 break;
1407             }
1408         }
1409
1410         if( b_found_key )
1411         {
1412             var_SetInteger( p_intf->p_libvlc, "key-pressed", val.i_int );
1413             return YES;
1414         }
1415     }
1416
1417     return NO;
1418 }
1419
1420 - (void)updateCurrentlyUsedHotkeys
1421 {
1422     NSMutableArray *o_tempArray = [[NSMutableArray alloc] init];
1423     /* Get the main Module */
1424     module_t *p_main = module_get_main();
1425     assert( p_main );
1426     unsigned confsize;
1427     module_config_t *p_config;
1428
1429     p_config = module_config_get (p_main, &confsize);
1430
1431     for (size_t i = 0; i < confsize; i++)
1432     {
1433         module_config_t *p_item = p_config + i;
1434
1435         if( CONFIG_ITEM(p_item->i_type) && p_item->psz_name != NULL
1436            && !strncmp( p_item->psz_name , "key-", 4 )
1437            && !EMPTY_STR( p_item->psz_text ) )
1438         {
1439             if (p_item->value.psz)
1440             {
1441                 [o_tempArray addObject: [NSString stringWithUTF8String:p_item->value.psz]];
1442             }
1443         }
1444     }
1445     module_config_free (p_config);
1446
1447     if( o_usedHotkeys )
1448         [o_usedHotkeys release];
1449     o_usedHotkeys = [[NSArray alloc] initWithArray: o_tempArray copyItems: YES];
1450     [o_tempArray release];
1451 }
1452
1453 #pragma mark -
1454 #pragma mark Interface updaters
1455 - (void)fullscreenChanged
1456 {
1457     playlist_t * p_playlist = pl_Get( VLCIntf );
1458     BOOL b_fullscreen = var_GetBool( p_playlist, "fullscreen" );
1459
1460     if (b_nativeFullscreenMode)
1461     {
1462         // this is called twice in certain situations, so only toogle if we really need to
1463         if( (  b_fullscreen && !([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen) ) ||
1464             ( !b_fullscreen &&  ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen) ) )
1465             [o_mainwindow toggleFullScreen: self];
1466
1467         if(b_fullscreen)
1468             [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1469         else
1470             [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
1471     }
1472     else
1473     {
1474         if( b_fullscreen )
1475         {
1476             input_thread_t * p_input = pl_CurrentInput( VLCIntf );
1477             if( p_input != NULL && [self activeVideoPlayback] )
1478             {
1479                 // activate app, as method can also be triggered from outside the app (prevents nasty window layout)
1480                 [NSApp activateIgnoringOtherApps:YES];
1481                 [o_mainwindow performSelectorOnMainThread:@selector(enterFullscreen) withObject:nil waitUntilDone:NO];
1482             }
1483             if (p_input)
1484                 vlc_object_release( p_input );
1485         }
1486         else
1487         {
1488             // leaving fullscreen is always allowed
1489             [o_mainwindow performSelectorOnMainThread:@selector(leaveFullscreen) withObject:nil waitUntilDone:NO];
1490         }
1491     }
1492 }
1493
1494 - (void)checkFullscreenChange:(NSNumber *)o_full
1495 {
1496     BOOL b_full = [o_full boolValue];
1497     if( p_intf && !var_GetBool( pl_Get( p_intf ), "fullscreen" ) != !b_full )
1498     {
1499         var_SetBool( pl_Get(p_intf), "fullscreen", b_full );
1500     }
1501 }
1502
1503 - (void)PlaylistItemChanged
1504 {
1505     if( p_current_input && ( p_current_input->b_dead || !vlc_object_alive( p_current_input ) ))
1506     {
1507         var_DelCallback( p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance] );
1508         vlc_object_release( p_current_input );
1509         p_current_input = NULL;
1510
1511         [o_mainmenu setRateControlsEnabled: NO];
1512     }
1513     else if( !p_current_input )
1514     {
1515         // object is hold here and released then it is dead
1516         p_current_input = playlist_CurrentInput( pl_Get( VLCIntf ));
1517         if( p_current_input )
1518         {
1519             var_AddCallback( p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance] );
1520             [self playbackStatusUpdated];
1521             [o_mainmenu setRateControlsEnabled: YES];
1522             if ( [self activeVideoPlayback] && [[o_mainwindow videoView] isHidden] )
1523                 [o_mainwindow performSelectorOnMainThread:@selector(togglePlaylist:) withObject: nil waitUntilDone:NO];
1524         }
1525     }
1526
1527     [o_playlist updateRowSelection];
1528     [o_mainwindow updateWindow];
1529     [self updateDelays];
1530     [self updateMainMenu];
1531 }
1532
1533 - (void)updateMainMenu
1534 {
1535     [o_mainmenu setupMenus];
1536     [o_mainmenu updatePlaybackRate];
1537 }
1538
1539 - (void)updateMainWindow
1540 {
1541     [o_mainwindow updateWindow];
1542 }
1543
1544 - (void)showMainWindow
1545 {
1546     [o_mainwindow performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:nil waitUntilDone:NO];
1547 }
1548
1549 - (void)showFullscreenController
1550 {
1551     [o_mainwindow performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
1552 }
1553
1554 - (void)updateDelays
1555 {
1556     [[VLCTrackSynchronization sharedInstance] performSelectorOnMainThread: @selector(updateValues) withObject: nil waitUntilDone:NO];
1557 }
1558
1559 - (void)updateName
1560 {
1561     [o_mainwindow updateName];
1562 }
1563
1564 - (void)updatePlaybackPosition
1565 {
1566     [o_mainwindow updateTimeSlider];
1567
1568     input_thread_t * p_input;
1569     p_input = pl_CurrentInput( p_intf );
1570     if( p_input )
1571     {
1572         if( var_GetInteger( p_input, "state" ) == PLAYING_S && [self activeVideoPlayback] )
1573             UpdateSystemActivity( UsrActivity );
1574         vlc_object_release( p_input );
1575     }
1576 }
1577
1578 - (void)updateVolume
1579 {
1580     [o_mainwindow updateVolumeSlider];
1581 }
1582
1583 - (void)playlistUpdated
1584 {
1585     [self playbackStatusUpdated];
1586     [o_playlist playlistUpdated];
1587     [o_mainwindow updateWindow];
1588     [o_mainwindow updateName];
1589 }
1590
1591 - (void)updateRecordState: (BOOL)b_value
1592 {
1593     [o_mainmenu updateRecordState:b_value];
1594 }
1595
1596 - (void)updateInfoandMetaPanel
1597 {
1598     [o_playlist outlineViewSelectionDidChange:nil];
1599 }
1600
1601 - (void)playbackStatusUpdated
1602 {
1603     input_thread_t * p_input;
1604
1605     p_input = pl_CurrentInput( p_intf );
1606     if( p_input )
1607     {
1608         int state = var_GetInteger( p_input, "state" );
1609         if( state == PLAYING_S )
1610         {
1611             [[self mainMenu] setPause];
1612             [o_mainwindow setPause];
1613         }
1614         else
1615         {
1616             if (state == END_S)
1617                 [o_mainmenu setSubmenusEnabled: FALSE];
1618             [[self mainMenu] setPlay];
1619             [o_mainwindow setPlay];
1620         }
1621         vlc_object_release( p_input );
1622     }
1623
1624     [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
1625     [self performSelectorOnMainThread:@selector(sendDistributedNotificationWithUpdatedPlaybackStatus) withObject: nil waitUntilDone: NO];
1626 }
1627
1628 - (void)sendDistributedNotificationWithUpdatedPlaybackStatus
1629 {
1630     [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"VLCPlayerStateDidChange"
1631                                                                    object:nil
1632                                                                  userInfo:nil
1633                                                        deliverImmediately:YES];
1634 }
1635
1636 - (void)playbackModeUpdated
1637 {
1638     vlc_value_t looping,repeating;
1639     playlist_t * p_playlist = pl_Get( VLCIntf );
1640
1641     bool loop = var_GetBool( p_playlist, "loop" );
1642     bool repeat = var_GetBool( p_playlist, "repeat" );
1643     if( repeat ) {
1644         [o_mainwindow setRepeatOne];
1645         [o_mainmenu setRepeatOne];
1646     } else if( loop ) {
1647         [o_mainwindow setRepeatAll];
1648         [o_mainmenu setRepeatAll];
1649     } else {
1650         [o_mainwindow setRepeatOff];
1651         [o_mainmenu setRepeatOff];
1652     }
1653
1654     [o_mainwindow setShuffle];
1655     [o_mainmenu setShuffle];
1656 }
1657
1658
1659 #pragma mark -
1660 #pragma mark Window updater
1661
1662 - (void)setWindowLevel:(NSNumber*)state
1663 {
1664     if( var_InheritBool( p_intf, "macosx-background" ) )
1665         return;
1666
1667     if ([state unsignedIntValue] & VOUT_WINDOW_STATE_ABOVE)
1668         [[[[VLCMainWindow sharedInstance] videoView] window] setLevel: NSStatusWindowLevel];
1669     else
1670         [[[[VLCMainWindow sharedInstance] videoView] window] setLevel: NSNormalWindowLevel];
1671 }
1672
1673 - (void)setActiveVideoPlayback:(BOOL)b_value
1674 {
1675     b_active_videoplayback = b_value;
1676     if( o_mainwindow )
1677     {
1678         [o_mainwindow performSelectorOnMainThread:@selector(setVideoplayEnabled) withObject:nil waitUntilDone:YES];
1679         [o_mainwindow performSelectorOnMainThread:@selector(togglePlaylist:) withObject:nil waitUntilDone:NO];
1680     }
1681 }
1682
1683 - (void)setNativeVideoSize:(NSSize)size
1684 {
1685     [o_mainwindow setNativeVideoSize:size];
1686 }
1687
1688 #pragma mark -
1689 #pragma mark Other objects getters
1690
1691 - (id)mainMenu
1692 {
1693     return o_mainmenu;
1694 }
1695
1696 - (id)mainWindow
1697 {
1698     return o_mainwindow;
1699 }
1700
1701 - (id)controls
1702 {
1703     if( o_controls )
1704         return o_controls;
1705
1706     return nil;
1707 }
1708
1709 - (id)bookmarks
1710 {
1711     if (!o_bookmarks )
1712         o_bookmarks = [[VLCBookmarks alloc] init];
1713
1714     if( !nib_bookmarks_loaded )
1715         nib_bookmarks_loaded = [NSBundle loadNibNamed:@"Bookmarks" owner: NSApp];
1716
1717     return o_bookmarks;
1718 }
1719
1720 - (id)open
1721 {
1722     if (!o_open)
1723         return nil;
1724
1725     if (!nib_open_loaded)
1726         nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner: NSApp];
1727
1728     return o_open;
1729 }
1730
1731 - (id)simplePreferences
1732 {
1733     if (!o_sprefs)
1734         o_sprefs = [[VLCSimplePrefs alloc] init];
1735
1736     if (!nib_prefs_loaded)
1737         nib_prefs_loaded = [NSBundle loadNibNamed:@"Preferences" owner: NSApp];
1738
1739     return o_sprefs;
1740 }
1741
1742 - (id)preferences
1743 {
1744     if( !o_prefs )
1745         o_prefs = [[VLCPrefs alloc] init];
1746
1747     if( !nib_prefs_loaded )
1748         nib_prefs_loaded = [NSBundle loadNibNamed:@"Preferences" owner: NSApp];
1749
1750     return o_prefs;
1751 }
1752
1753 - (id)playlist
1754 {
1755     if( o_playlist )
1756         return o_playlist;
1757
1758     return nil;
1759 }
1760
1761 - (id)info
1762 {
1763     if(! nib_info_loaded )
1764         nib_info_loaded = [NSBundle loadNibNamed:@"MediaInfo" owner: NSApp];
1765
1766     if( o_info )
1767         return o_info;
1768
1769     return nil;
1770 }
1771
1772 - (id)wizard
1773 {
1774     if( !o_wizard )
1775         o_wizard = [[VLCWizard alloc] init];
1776
1777     if( !nib_wizard_loaded )
1778     {
1779         nib_wizard_loaded = [NSBundle loadNibNamed:@"Wizard" owner: NSApp];
1780         [o_wizard initStrings];
1781     }
1782     return o_wizard;
1783 }
1784
1785 - (id)getVideoViewAtPositionX: (int *)pi_x Y: (int *)pi_y withWidth: (unsigned int*)pi_width andHeight: (unsigned int*)pi_height
1786 {
1787     [o_mainwindow performSelectorOnMainThread:@selector(setupVideoView) withObject:nil waitUntilDone:YES];
1788     id videoView = [o_mainwindow videoView];
1789     NSRect videoRect = [videoView frame];
1790     int i_x = (int)videoRect.origin.x;
1791     int i_y = (int)videoRect.origin.y;
1792     unsigned int i_width = (int)videoRect.size.width;
1793     unsigned int i_height = (int)videoRect.size.height;
1794     pi_x = &i_x;
1795     pi_y = &i_y;
1796     pi_width = &i_width;
1797     pi_height = &i_height;
1798     msg_Dbg( VLCIntf, "returning videoview with x=%i, y=%i, width=%i, height=%i", i_x, i_y, i_width, i_height );
1799     return videoView;
1800 }
1801
1802 - (id)embeddedList
1803 {
1804     if( o_embedded_list )
1805         return o_embedded_list;
1806
1807     return nil;
1808 }
1809
1810 - (id)coreDialogProvider
1811 {
1812     if( o_coredialogs )
1813         return o_coredialogs;
1814
1815     return nil;
1816 }
1817
1818 - (id)eyeTVController
1819 {
1820     if( o_eyetv )
1821         return o_eyetv;
1822
1823     return nil;
1824 }
1825
1826 - (id)appleRemoteController
1827 {
1828     return o_remote;
1829 }
1830
1831 - (BOOL)activeVideoPlayback
1832 {
1833     return b_active_videoplayback;
1834 }
1835
1836 #pragma mark -
1837 #pragma mark Crash Log
1838 - (void)sendCrashLog:(NSString *)crashLog withUserComment:(NSString *)userComment
1839 {
1840     NSString *urlStr = @"http://crash.videolan.org/crashlog/sendcrashreport.php";
1841     NSURL *url = [NSURL URLWithString:urlStr];
1842
1843     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
1844     [req setHTTPMethod:@"POST"];
1845
1846     NSString * email;
1847     if( [o_crashrep_includeEmail_ckb state] == NSOnState )
1848     {
1849         ABPerson * contact = [[ABAddressBook sharedAddressBook] me];
1850         ABMultiValue *emails = [contact valueForProperty:kABEmailProperty];
1851         email = [emails valueAtIndex:[emails indexForIdentifier:
1852                     [emails primaryIdentifier]]];
1853     }
1854     else
1855         email = [NSString string];
1856
1857     NSString *postBody;
1858     postBody = [NSString stringWithFormat:@"CrashLog=%@&Comment=%@&Email=%@\r\n",
1859             [crashLog stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
1860             [userComment stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
1861             [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
1862
1863     [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
1864
1865     /* Released from delegate */
1866     crashLogURLConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
1867 }
1868
1869 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
1870 {
1871     msg_Dbg( p_intf, "crash report successfully sent" );
1872     [crashLogURLConnection release];
1873     crashLogURLConnection = nil;
1874 }
1875
1876 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
1877 {
1878     msg_Warn (p_intf, "Error when sending the crash report: %s (%li)", [[error localizedDescription] UTF8String], [error code]);
1879     [crashLogURLConnection release];
1880     crashLogURLConnection = nil;
1881 }
1882
1883 - (NSString *)latestCrashLogPathPreviouslySeen:(BOOL)previouslySeen
1884 {
1885     NSString * crashReporter;
1886     if( OSX_MOUNTAIN_LION )
1887         crashReporter = [@"~/Library/Logs/DiagnosticReports" stringByExpandingTildeInPath];
1888     else
1889         crashReporter = [@"~/Library/Logs/CrashReporter" stringByExpandingTildeInPath];
1890     NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:crashReporter];
1891     NSString *fname;
1892     NSString * latestLog = nil;
1893     int year  = !previouslySeen ? [[NSUserDefaults standardUserDefaults] integerForKey:@"LatestCrashReportYear"] : 0;
1894     int month = !previouslySeen ? [[NSUserDefaults standardUserDefaults] integerForKey:@"LatestCrashReportMonth"]: 0;
1895     int day   = !previouslySeen ? [[NSUserDefaults standardUserDefaults] integerForKey:@"LatestCrashReportDay"]  : 0;
1896     int hours = !previouslySeen ? [[NSUserDefaults standardUserDefaults] integerForKey:@"LatestCrashReportHours"]: 0;
1897
1898     while (fname = [direnum nextObject])
1899     {
1900         [direnum skipDescendents];
1901         if([fname hasPrefix:@"VLC"] && [fname hasSuffix:@"crash"])
1902         {
1903             NSArray * compo = [fname componentsSeparatedByString:@"_"];
1904             if( [compo count] < 3 ) continue;
1905             compo = [[compo objectAtIndex:1] componentsSeparatedByString:@"-"];
1906             if( [compo count] < 4 ) continue;
1907
1908             // Dooh. ugly.
1909             if( year < [[compo objectAtIndex:0] intValue] ||
1910                 (year ==[[compo objectAtIndex:0] intValue] &&
1911                  (month < [[compo objectAtIndex:1] intValue] ||
1912                   (month ==[[compo objectAtIndex:1] intValue] &&
1913                    (day   < [[compo objectAtIndex:2] intValue] ||
1914                     (day   ==[[compo objectAtIndex:2] intValue] &&
1915                       hours < [[compo objectAtIndex:3] intValue] ))))))
1916             {
1917                 year  = [[compo objectAtIndex:0] intValue];
1918                 month = [[compo objectAtIndex:1] intValue];
1919                 day   = [[compo objectAtIndex:2] intValue];
1920                 hours = [[compo objectAtIndex:3] intValue];
1921                 latestLog = [crashReporter stringByAppendingPathComponent:fname];
1922             }
1923         }
1924     }
1925
1926     if(!(latestLog && [[NSFileManager defaultManager] fileExistsAtPath:latestLog]))
1927         return nil;
1928
1929     if( !previouslySeen )
1930     {
1931         [[NSUserDefaults standardUserDefaults] setInteger:year  forKey:@"LatestCrashReportYear"];
1932         [[NSUserDefaults standardUserDefaults] setInteger:month forKey:@"LatestCrashReportMonth"];
1933         [[NSUserDefaults standardUserDefaults] setInteger:day   forKey:@"LatestCrashReportDay"];
1934         [[NSUserDefaults standardUserDefaults] setInteger:hours forKey:@"LatestCrashReportHours"];
1935     }
1936     return latestLog;
1937 }
1938
1939 - (NSString *)latestCrashLogPath
1940 {
1941     return [self latestCrashLogPathPreviouslySeen:YES];
1942 }
1943
1944 - (void)lookForCrashLog
1945 {
1946     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
1947     // This pref key doesn't exists? this VLC is an upgrade, and this crash log come from previous version
1948     BOOL areCrashLogsTooOld = ![[NSUserDefaults standardUserDefaults] integerForKey:@"LatestCrashReportYear"];
1949     NSString * latestLog = [self latestCrashLogPathPreviouslySeen:NO];
1950     if( latestLog && !areCrashLogsTooOld )
1951     {
1952         if ([[NSUserDefaults standardUserDefaults] integerForKey:@"AlwaysSendCrashReports"] > 0)
1953             [self sendCrashLog:[NSString stringWithContentsOfFile: [self latestCrashLogPath] encoding: NSUTF8StringEncoding error: NULL] withUserComment: [o_crashrep_fld string]];
1954         else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"AlwaysSendCrashReports"] == 0)
1955             [NSApp runModalForWindow: o_crashrep_win];
1956         // bail out, the user doesn't want us to send reports
1957     }
1958
1959     [o_pool release];
1960 }
1961
1962 - (IBAction)crashReporterAction:(id)sender
1963 {
1964     if( sender == o_crashrep_send_btn ) {
1965         [self sendCrashLog:[NSString stringWithContentsOfFile: [self latestCrashLogPath] encoding: NSUTF8StringEncoding error: NULL] withUserComment: [o_crashrep_fld string]];
1966         if ([o_crashrep_dontaskagain_ckb state])
1967             [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"AlwaysSendCrashReports"];
1968     } else {
1969         if ([o_crashrep_dontaskagain_ckb state])
1970             [[NSUserDefaults standardUserDefaults] setInteger:-1 forKey:@"AlwaysSendCrashReports"];
1971     }
1972
1973     [NSApp stopModal];
1974     [o_crashrep_win orderOut: sender];
1975 }
1976
1977 - (IBAction)openCrashLog:(id)sender
1978 {
1979     NSString * latestLog = [self latestCrashLogPath];
1980     if( latestLog )
1981     {
1982         [[NSWorkspace sharedWorkspace] openFile: latestLog withApplication: @"Console"];
1983     }
1984     else
1985     {
1986         NSBeginInformationalAlertSheet(_NS("No CrashLog found"), _NS("Continue"), nil, nil, o_msgs_panel, self, NULL, NULL, nil, _NS("Couldn't find any trace of a previous crash.") );
1987     }
1988 }
1989
1990 #pragma mark -
1991 #pragma mark Remove old prefs
1992
1993 - (void)_removeOldPreferences
1994 {
1995     static NSString * kVLCPreferencesVersion = @"VLCPreferencesVersion";
1996     static const int kCurrentPreferencesVersion = 2;
1997     int version = [[NSUserDefaults standardUserDefaults] integerForKey:kVLCPreferencesVersion];
1998     if( version >= kCurrentPreferencesVersion ) return;
1999
2000     if( version == 1 )
2001     {
2002         [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
2003         [[NSUserDefaults standardUserDefaults] synchronize];
2004
2005         if (![[VLCCoreInteraction sharedInstance] fixPreferences])
2006             return;
2007         else
2008             config_SaveConfigFile( VLCIntf ); // we need to do manually, since we won't quit libvlc cleanly
2009     }
2010     else
2011     {
2012         NSArray *libraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
2013             NSUserDomainMask, YES);
2014         if( !libraries || [libraries count] == 0) return;
2015         NSString * preferences = [[libraries objectAtIndex:0] stringByAppendingPathComponent:@"Preferences"];
2016
2017         /* File not found, don't attempt anything */
2018         if(![[NSFileManager defaultManager] fileExistsAtPath:[preferences stringByAppendingPathComponent:@"org.videolan.vlc"]] &&
2019            ![[NSFileManager defaultManager] fileExistsAtPath:[preferences stringByAppendingPathComponent:@"org.videolan.vlc.plist"]] )
2020         {
2021             [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
2022             return;
2023         }
2024
2025         int res = NSRunInformationalAlertPanel(_NS("Remove old preferences?"),
2026                     _NS("We just found an older version of VLC's preferences files."),
2027                     _NS("Move To Trash and Relaunch VLC"), _NS("Ignore"), nil, nil);
2028         if( res != NSOKButton )
2029         {
2030             [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
2031             return;
2032         }
2033
2034         NSArray * ourPreferences = [NSArray arrayWithObjects:@"org.videolan.vlc.plist", @"VLC", @"org.videolan.vlc", nil];
2035
2036         /* Move the file to trash so that user can find them later */
2037         [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:preferences destination:nil files:ourPreferences tag:0];
2038
2039         /* really reset the defaults from now on */
2040         [NSUserDefaults resetStandardUserDefaults];
2041
2042         [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
2043         [[NSUserDefaults standardUserDefaults] synchronize];
2044     }
2045
2046     /* Relaunch now */
2047     const char * path = [[[NSBundle mainBundle] executablePath] UTF8String];
2048
2049     /* For some reason we need to fork(), not just execl(), which reports a ENOTSUP then. */
2050     if(fork() != 0)
2051     {
2052         exit(0);
2053         return;
2054     }
2055     execl(path, path, NULL);
2056 }
2057
2058 #pragma mark -
2059 #pragma mark Errors, warnings and messages
2060 - (IBAction)updateMessagesPanel:(id)sender
2061 {
2062     [self windowDidBecomeKey:nil];
2063 }
2064
2065 - (IBAction)showMessagesPanel:(id)sender
2066 {
2067     [o_msgs_panel makeKeyAndOrderFront: sender];
2068 }
2069
2070 - (void)windowDidBecomeKey:(NSNotification *)o_notification
2071 {
2072     [o_msgs_table reloadData];
2073     [o_msgs_table scrollRowToVisible: [o_msg_arr count] - 1];
2074 }
2075
2076 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
2077 {
2078     if (aTableView == o_msgs_table)
2079         return [o_msg_arr count];
2080     return 0;
2081 }
2082
2083 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
2084 {
2085     NSMutableAttributedString *result = NULL;
2086
2087     [o_msg_lock lock];
2088     if( rowIndex < [o_msg_arr count] )
2089         result = [o_msg_arr objectAtIndex: rowIndex];
2090     [o_msg_lock unlock];
2091
2092     if( result != NULL )
2093         return result;
2094     else
2095         return @"";
2096 }
2097
2098 - (void)processReceivedlibvlcMessage:(const msg_item_t *) item ofType: (int)i_type withStr: (char *)str
2099 {
2100     if (o_msg_arr)
2101     {
2102         NSColor *o_white = [NSColor whiteColor];
2103         NSColor *o_red = [NSColor redColor];
2104         NSColor *o_yellow = [NSColor yellowColor];
2105         NSColor *o_gray = [NSColor grayColor];
2106         NSString * firstString, * secondString;
2107
2108         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
2109         static const char * ppsz_type[4] = { ": ", " error: ", " warning: ", " debug: " };
2110
2111         NSDictionary *o_attr;
2112         NSMutableAttributedString *o_msg_color;
2113
2114         [o_msg_lock lock];
2115
2116         if( [o_msg_arr count] > 600 )
2117         {
2118             [o_msg_arr removeObjectAtIndex: 0];
2119             [o_msg_arr removeObjectAtIndex: 1];
2120         }
2121         firstString = [NSString stringWithFormat:@"%s%s", item->psz_module, ppsz_type[i_type]];
2122         secondString = [NSString stringWithFormat:@"%@%s\n", firstString, str];
2123
2124         o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]  forKey: NSForegroundColorAttributeName];
2125         o_msg_color = [[NSMutableAttributedString alloc] initWithString: secondString attributes: o_attr];
2126         o_attr = [NSDictionary dictionaryWithObject: pp_color[3] forKey: NSForegroundColorAttributeName];
2127         [o_msg_color setAttributes: o_attr range: NSMakeRange( 0, [firstString length] )];
2128         [o_msg_arr addObject: [o_msg_color autorelease]];
2129
2130         b_msg_arr_changed = YES;
2131         [o_msg_lock unlock];
2132     }
2133 }
2134
2135 - (IBAction)saveDebugLog:(id)sender
2136 {
2137     NSSavePanel * saveFolderPanel = [[NSSavePanel alloc] init];
2138
2139     [saveFolderPanel setCanSelectHiddenExtension: NO];
2140     [saveFolderPanel setCanCreateDirectories: YES];
2141     [saveFolderPanel setAllowedFileTypes: [NSArray arrayWithObject:@"rtf"]];
2142     [saveFolderPanel beginSheetForDirectory:nil file: [NSString stringWithFormat: _NS("VLC Debug Log (%s).rtf"), VERSION_MESSAGE] modalForWindow: o_msgs_panel modalDelegate:self didEndSelector:@selector(saveDebugLogAsRTF:returnCode:contextInfo:) contextInfo:nil];
2143 }
2144
2145 - (void)saveDebugLogAsRTF: (NSSavePanel *)sheet returnCode: (int)returnCode contextInfo: (void *)contextInfo
2146 {
2147     if( returnCode == NSOKButton )
2148     {
2149         NSUInteger count = [o_msg_arr count];
2150         NSMutableAttributedString * string = [[NSMutableAttributedString alloc] init];
2151         for (NSUInteger i = 0; i < count; i++)
2152         {
2153             [string appendAttributedString: [o_msg_arr objectAtIndex: i]];
2154         }
2155
2156         NSData *data = [string RTFFromRange:NSMakeRange( 0, [string length] )
2157                          documentAttributes:[NSDictionary dictionaryWithObject: NSRTFTextDocumentType forKey: NSDocumentTypeDocumentAttribute]];
2158
2159         if( [data writeToFile: [[sheet URL] path] atomically: YES] == NO )
2160             msg_Warn( p_intf, "Error while saving the debug log" );
2161
2162         [string release];
2163     }
2164 }
2165
2166 #pragma mark -
2167 #pragma mark Playlist toggling
2168
2169 - (void)updateTogglePlaylistState
2170 {
2171     [[self playlist] outlineViewSelectionDidChange: NULL];
2172 }
2173
2174 #pragma mark -
2175
2176 @end
2177
2178 @implementation VLCMain (Internal)
2179
2180 - (void)handlePortMessage:(NSPortMessage *)o_msg
2181 {
2182     id ** val;
2183     NSData * o_data;
2184     NSValue * o_value;
2185     NSInvocation * o_inv;
2186     NSConditionLock * o_lock;
2187
2188     o_data = [[o_msg components] lastObject];
2189     o_inv = *((NSInvocation **)[o_data bytes]);
2190     [o_inv getArgument: &o_value atIndex: 2];
2191     val = (id **)[o_value pointerValue];
2192     [o_inv setArgument: val[1] atIndex: 2];
2193     o_lock = *(val[0]);
2194
2195     [o_lock lock];
2196     [o_inv invoke];
2197     [o_lock unlockWithCondition: 1];
2198 }
2199 - (void)resetMediaKeyJump
2200 {
2201     b_mediakeyJustJumped = NO;
2202 }
2203 - (void)coreChangedMediaKeySupportSetting: (NSNotification *)o_notification
2204 {
2205     b_mediaKeySupport = var_InheritBool( VLCIntf, "macosx-mediakeys" );
2206     if (b_mediaKeySupport)
2207     {
2208         if (!o_mediaKeyController)
2209             o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
2210         [o_mediaKeyController startWatchingMediaKeys];
2211     }
2212     else if (!b_mediaKeySupport && o_mediaKeyController)
2213         [o_mediaKeyController stopWatchingMediaKeys];
2214 }
2215
2216 @end
2217
2218 /*****************************************************************************
2219  * VLCApplication interface
2220  *****************************************************************************/
2221
2222 @implementation VLCApplication
2223 // when user selects the quit menu from dock it sends a terminate:
2224 // but we need to send a stop: to properly exits libvlc.
2225 // However, we are not able to change the action-method sent by this standard menu item.
2226 // thus we override terminate: to send a stop:
2227 // see [af97f24d528acab89969d6541d83f17ce1ecd580] that introduced the removal of setjmp() and longjmp()
2228 - (void)terminate:(id)sender
2229 {
2230     [self activateIgnoringOtherApps:YES];
2231     [self stop:sender];
2232 }
2233
2234 @end