intf.m 70.6 KB
Newer Older
1
/*****************************************************************************
2
 * intf.m: MacOS X interface module
3
 *****************************************************************************
4
 * Copyright (C) 2002-2012 VLC authors and VideoLAN
5
 * $Id$
6
7
8
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
 *          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
13
14
15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
16
 *
17
18
19
20
21
22
23
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
35
36
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/param.h>                                    /* for MAXPATHLEN */
#include <string.h>
hartman's avatar
hartman committed
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_display.h>
45
#include <unistd.h> /* execl() */
46

47
#import "CompatibilityFixes.h"
48
#import "intf.h"
49
#import "StringUtility.h"
50
#import "MainMenu.h"
51
#import "VideoView.h"
52
53
#import "prefs.h"
#import "playlist.h"
54
#import "playlistinfo.h"
55
56
57
58
#import "controls.h"
#import "open.h"
#import "wizard.h"
#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
#import "VLCVoutWindowController.h"
66

67
#import <AddressBook/AddressBook.h>         /* for crashlog send mechanism */
Rafaël Carré's avatar
Rafaël Carré committed
68
#import <Sparkle/Sparkle.h>                 /* we're the update delegate */
69

70
71
72
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
73
static void Run (intf_thread_t *p_intf);
74

75
76
77
78
static void updateProgressPanel (void *, const char *, float);
static bool checkProgressPanel (void *);
static void destroyProgressPanel (void *);

79
static void MsgCallback(void *data, int type, const msg_item_t *item, const char *format, va_list ap);
80

81
82
83
84
85
86
87
88
89
90
static int InputEvent(vlc_object_t *, const char *,
                      vlc_value_t, vlc_value_t, void *);
static int PLItemChanged(vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void *);
static int PlaylistUpdated(vlc_object_t *, const char *,
                           vlc_value_t, vlc_value_t, void *);
static int PlaybackModeUpdated(vlc_object_t *, const char *,
                               vlc_value_t, vlc_value_t, void *);
static int VolumeUpdated(vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void *);
91

92
#pragma mark -
93
#pragma mark VLC Interface Object Callbacks
94

95
96
97
/*****************************************************************************
 * OpenIntf: initialize interface
 *****************************************************************************/
98
int OpenIntf (vlc_object_t *p_this)
Jérome Decoodt's avatar
Jérome Decoodt committed
99
{
100
101
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    [VLCApplication sharedApplication];
102

103
104
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

105
106
    p_intf->p_sys = malloc(sizeof(intf_sys_t));
    if (p_intf->p_sys == NULL)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
107
        return VLC_ENOMEM;
108

109
    memset(p_intf->p_sys, 0, sizeof(*p_intf->p_sys));
Jérome Decoodt's avatar
Jérome Decoodt committed
110

111
    /* subscribe to LibVLCCore's messages */
112
    vlc_Subscribe(&p_intf->p_sys->sub, MsgCallback, NULL);
113

114
    Run(p_intf);
115

116
    [o_pool release];
Jean-Paul Saman's avatar
Jean-Paul Saman committed
117
    return VLC_SUCCESS;
118
119
120
121
122
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
123
void CloseIntf (vlc_object_t *p_this)
124
125
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;
Jérome Decoodt's avatar
Jérome Decoodt committed
126

127
    free(p_intf->p_sys);
128
129
}

130
static int WindowControl(vout_window_t *, int i_query, va_list);
131

132
int WindowOpen(vout_window_t *p_wnd, const vout_window_cfg_t *cfg)
133
{
134
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
135
136
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf) {
137
        msg_Err(p_wnd, "Mac OS X interface not found");
138
139
140
141
142
143
144
        return VLC_EGENERIC;
    }

    int i_x = cfg->x;
    int i_y = cfg->y;
    unsigned i_width = cfg->width;
    unsigned i_height = cfg->height;
145
    p_wnd->handle.nsobject = [[VLCMain sharedInstance] getVideoViewAtPositionX: &i_x Y: &i_y withWidth: &i_width andHeight: &i_height forWindow: p_wnd];
146

147
148
    if (!p_wnd->handle.nsobject) {
        msg_Err(p_wnd, "got no video view from the interface");
149
        [o_pool release];
150
151
152
        return VLC_EGENERIC;
    }

153
154
155
156
157
158
159
160
161
162
163
164
    // TODO: this seems to be strange. Why not just allocating in the right size?
    // This could avoid strange resize-animations...
    NSSize newSize = NSMakeSize(cfg->width, cfg->height);
    SEL sel = @selector(setNativeVideoSize:forWindow:);
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[[VLCMain sharedInstance] voutController] methodSignatureForSelector:sel]];
    [inv setTarget:[[VLCMain sharedInstance] voutController]];
    [inv setSelector:sel];
    [inv setArgument:&newSize atIndex:2]; // starting at 2!
    [inv setArgument:&p_wnd atIndex:3];
    [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                       waitUntilDone:NO];

165
166
167
168
169
170
171
172
173
174
175
176
177
178
    // TODO: find a cleaner way for "start in fullscreen"
    if (var_GetBool(pl_Get(VLCIntf), "fullscreen")) {
        int i_full = 1;

        SEL sel = @selector(setFullscreen:forWindow:);
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[VLCMain sharedInstance] methodSignatureForSelector:sel]];
        [inv setTarget:[VLCMain sharedInstance]];
        [inv setSelector:sel];
        [inv setArgument:&i_full atIndex:2];
        [inv setArgument:&p_wnd atIndex:3];
        [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                           waitUntilDone:NO];
    }

179
    [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
180
181
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t *)VLCIntf;
182
    [o_pool release];
183
184
185
    return VLC_SUCCESS;
}

186
static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
187
{
188
    switch(i_query) {
189
190
        case VOUT_WINDOW_SET_STATE:
        {
191
            unsigned i_state = va_arg(args, unsigned);
192
193
194
195
196
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(setWindowLevel:) withObject:[NSNumber numberWithUnsignedInt:i_state] waitUntilDone:NO];
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_SIZE:
        {
197
198
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

199
200
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
201
202
203
204
205
206
207
208
209
210
211
212

            NSSize newSize = NSMakeSize(i_width, i_height);            
            SEL sel = @selector(setNativeVideoSize:forWindow:);
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[[VLCMain sharedInstance] voutController] methodSignatureForSelector:sel]];
            [inv setTarget:[[VLCMain sharedInstance] voutController]];
            [inv setSelector:sel];
            [inv setArgument:&newSize atIndex:2]; // starting at 2!
            [inv setArgument:&p_wnd atIndex:3];
            [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                               waitUntilDone:NO];

            [o_pool release];
213
214
215
216
217
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
218
            int i_full = va_arg(args, int);
219
220
221
222
223
224
225
226
227
228
229
230

            SEL sel = @selector(setFullscreen:forWindow:);
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[VLCMain sharedInstance] methodSignatureForSelector:sel]];
            [inv setTarget:[VLCMain sharedInstance]];
            [inv setSelector:sel];
            [inv setArgument:&i_full atIndex:2]; // starting at 2!
            [inv setArgument:&p_wnd atIndex:3];
            [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                               waitUntilDone:NO];


            //[[VLCMain sharedInstance] performSelectorOnMainThread:@selector(fullscreenChanged:) withObject:[NSValue valueWithPointer:p_wnd] waitUntilDone:NO];
231
232
233
234
            [o_pool release];
            return VLC_SUCCESS;
        }
        default:
235
            msg_Warn(p_wnd, "unsupported control query");
236
            return VLC_EGENERIC;
237
    }
238
239
}

240
void WindowClose(vout_window_t *p_wnd)
241
{
242
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
243
    [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
244

245
246
    [[[VLCMain sharedInstance] voutController] performSelectorOnMainThread:@selector(removeVoutforDisplay:) withObject:[NSValue valueWithPointer:p_wnd] waitUntilDone:NO];

247
    [o_pool release];
248
249
}

250
251
252
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
253
static NSLock * o_appLock = nil;    // controls access to f_appExit
254

255
static void Run(intf_thread_t *p_intf)
256
{
257
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
258
    [VLCApplication sharedApplication];
259

260
261
    o_appLock = [[NSLock alloc] init];

hartman's avatar
-    
hartman committed
262
    [[VLCMain sharedInstance] setIntf: p_intf];
hartman's avatar
hartman committed
263
    [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
264

265
266
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
267
    [o_appLock release];
268
    [o_pool release];
269
270

    raise(SIGTERM);
271
272
}

273
274
#pragma mark -
#pragma mark Variables Callback
275

276
277
278
/*****************************************************************************
 * MsgCallback: Callback triggered by the core once a new debug message is
 * ready to be displayed. We store everything in a NSArray in our Cocoa part
279
 * of this file.
280
 *****************************************************************************/
281
static void MsgCallback(void *data, int type, const msg_item_t *item, const char *format, va_list ap)
282
283
{
    int canc = vlc_savecancel();
284
    char *str;
285

286
287
    if (vasprintf(&str, format, ap) == -1) {
        vlc_restorecancel(canc);
288
        return;
289
    }
290

291
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
292
    [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
293
    [o_pool release];
294

295
296
    vlc_restorecancel(canc);
    free(str);
297
298
}

299
300
static int InputEvent(vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t new_val, void *param)
301
{
302
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
303
304
    switch (new_val.i_int) {
        case INPUT_EVENT_STATE:
305
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackStatusUpdated) withObject: nil waitUntilDone:NO];
306
307
            break;
        case INPUT_EVENT_RATE:
308
            [[[VLCMain sharedInstance] mainMenu] performSelectorOnMainThread:@selector(updatePlaybackRate) withObject: nil waitUntilDone:NO];
309
310
            break;
        case INPUT_EVENT_POSITION:
311
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject: nil waitUntilDone:NO];
312
313
314
            break;
        case INPUT_EVENT_TITLE:
        case INPUT_EVENT_CHAPTER:
315
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
316
317
            break;
        case INPUT_EVENT_CACHE:
318
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
319
320
321
322
323
324
325
326
327
328
329
330
331
332
            break;
        case INPUT_EVENT_STATISTICS:
            [[[VLCMain sharedInstance] info] performSelectorOnMainThread:@selector(updateStatistics) withObject: nil waitUntilDone: NO];
            break;
        case INPUT_EVENT_ES:
            break;
        case INPUT_EVENT_TELETEXT:
            break;
        case INPUT_EVENT_AOUT:
            break;
        case INPUT_EVENT_VOUT:
            break;
        case INPUT_EVENT_ITEM_META:
        case INPUT_EVENT_ITEM_INFO:
333
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
334
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
335
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateInfoandMetaPanel) withObject: nil waitUntilDone:NO];
336
337
338
339
            break;
        case INPUT_EVENT_BOOKMARK:
            break;
        case INPUT_EVENT_RECORD:
340
            [[VLCMain sharedInstance] updateRecordState: var_GetBool(p_this, "record")];
341
342
            break;
        case INPUT_EVENT_PROGRAM:
343
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
344
345
346
347
348
349
350
            break;
        case INPUT_EVENT_ITEM_EPG:
            break;
        case INPUT_EVENT_SIGNAL:
            break;

        case INPUT_EVENT_ITEM_NAME:
351
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
352
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
353
354
355
356
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
357
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
358
359
360
            break;

        case INPUT_EVENT_DEAD:
361
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
362
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
363
364
365
            break;

        case INPUT_EVENT_ABORT:
366
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
367
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
368
369
370
            break;

        default:
371
            //msg_Warn(p_this, "unhandled input event (%lld)", new_val.i_int);
372
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
373
    }
374

375
    [o_pool release];
376
377
378
    return VLC_SUCCESS;
}

379
380
static int PLItemChanged(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
381
{
382
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
383
384
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(PlaylistItemChanged) withObject:nil waitUntilDone:NO];

385
    [o_pool release];
386
387
388
    return VLC_SUCCESS;
}

389
390
static int PlaylistUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
391
{
392
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
393
394
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject:nil waitUntilDone:NO];

395
    [o_pool release];
396
397
398
    return VLC_SUCCESS;
}

399
400
static int PlaybackModeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
401
{
402
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
403
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackModeUpdated) withObject:nil waitUntilDone:NO];
404

405
    [o_pool release];
406
407
408
    return VLC_SUCCESS;
}

409
410
static int VolumeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
411
{
412
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
413
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateVolume) withObject:nil waitUntilDone:NO];
414

415
    [o_pool release];
416
417
418
419
420
421
422
423
    return VLC_SUCCESS;
}

/*****************************************************************************
 * ShowController: Callback triggered by the show-intf playlist variable
 * through the ShowIntf-control-intf, to let us show the controller-win;
 * usually when in fullscreen-mode
 *****************************************************************************/
424
425
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
426
427
{
    intf_thread_t * p_intf = VLCIntf;
428
429
430
431
    if (p_intf && p_intf->p_sys) {
        playlist_t * p_playlist = pl_Get(p_intf);
        BOOL b_fullscreen = var_GetBool(p_playlist, "fullscreen");
        if (strcmp(psz_variable, "intf-toggle-fscontrol") || b_fullscreen)
432
433
434
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
435
    }
436
437
438
    return VLC_SUCCESS;
}

439
/*****************************************************************************
440
 * DialogCallback: Callback triggered by the "dialog-*" variables
441
 * to let the intf display error and interaction dialogs
442
 *****************************************************************************/
443
static int DialogCallback(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
444
445
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
446
447
    VLCMain *interface = (VLCMain *)data;

448
    if ([[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"]) {
449
450
451
452
453
454
455
456
        /* the progress panel needs to update itself and therefore wants special treatment within this context */
        dialog_progress_bar_t *p_dialog = (dialog_progress_bar_t *)value.p_address;

        p_dialog->pf_update = updateProgressPanel;
        p_dialog->pf_check = checkProgressPanel;
        p_dialog->pf_destroy = destroyProgressPanel;
        p_dialog->p_sys = VLCIntf->p_libvlc;
    }
457

458
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
459
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
460

461
462
463
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
464

465
466
467
468
469
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
470
    if (text != NULL)
471
472
473
474
475
476
477
478
479
480
481
482
        o_txt = [NSString stringWithUTF8String: text];
    else
        o_txt = @"";

    [[[VLCMain sharedInstance] coreDialogProvider] updateProgressPanelWithText: o_txt andNumber: (double)(value * 1000.)];

    [o_pool release];
}

void destroyProgressPanel (void *priv)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
483
    [[[VLCMain sharedInstance] coreDialogProvider] destroyProgressPanel];
484
485
486
487
488
489
490
491
492
493
    [o_pool release];
}

bool checkProgressPanel (void *priv)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    return [[[VLCMain sharedInstance] coreDialogProvider] progressCancelled];
    [o_pool release];
}

494
495
496
497
498
499
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
500
501
    if (!p_intf)
        return NULL;
502
    return pl_CurrentInput(p_intf);
503
504
505
506
507
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
508
509
    if (!p_input)
        return NULL;
510
511
512
513
514
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

515
audio_output_t *getAout(void)
516
517
{
    input_thread_t *p_input = getInput();
518
519
    if (!p_input)
        return NULL;
520
    audio_output_t *p_aout = input_GetAout(p_input);
521
522
523
524
    vlc_object_release(p_input);
    return p_aout;
}

525
#pragma mark -
526
527
528
529
530
#pragma mark Private

@interface VLCMain ()
- (void)_removeOldPreferences;
@end
531

532
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
533
 * VLCMain implementation
534
535
536
 *****************************************************************************/
@implementation VLCMain

537
538
@synthesize voutController=o_vout_controller;

539
540
541
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
542
543
544
545
546
547
548
static VLCMain *_o_sharedMainInstance = nil;

+ (VLCMain *)sharedInstance
{
    return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
}

Jérome Decoodt's avatar
Jérome Decoodt committed
549
- (id)init
hartman's avatar
hartman committed
550
{
551
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
552
        [self dealloc];
553
        return _o_sharedMainInstance;
554
    } else
hartman's avatar
hartman committed
555
        _o_sharedMainInstance = [super init];
556

557
    p_intf = NULL;
558
    p_current_input = NULL;
559

560
    o_msg_lock = [[NSLock alloc] init];
561
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
562

563
    o_open = [[VLCOpen alloc] init];
564
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
565
    o_info = [[VLCInfo alloc] init];
566
    o_mainmenu = [[VLCMainMenu alloc] init];
567
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
568
    o_eyetv = [[VLCEyeTVController alloc] init];
569
    o_mainwindow = [[VLCMainWindow alloc] init];
570
571
572
573
574
575

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
576
577
578
579
580

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"LiveUpdateTheMessagesPanel"];
    [defaults registerDefaults:appDefaults];

581
582
    o_vout_controller = [[VLCVoutWindowController alloc] init];

hartman's avatar
hartman committed
583
584
585
    return _o_sharedMainInstance;
}

586
587
588
589
590
591
- (void)dealloc
{
    [o_vout_controller release];
    [super dealloc];
}

592
593
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
594
595
596
    p_intf = p_mainintf;
}

597
598
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
599
600
601
    return p_intf;
}

602
603
- (void)awakeFromNib
{
604
    playlist_t *p_playlist;
605
    vlc_value_t val;
606
607
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
608

609
610
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
611
612
    if (nib_main_loaded)
        return;
613

614
615
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
616

617
    p_playlist = pl_Get(p_intf);
hartman's avatar
hartman committed
618

619
    val.b_bool = false;
bigben's avatar
bigben committed
620

621
622
    var_AddCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
    var_AddCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
623
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
624
625
626
627
628
629
630
631
632
633
    var_AddCallback(p_playlist, "item-current", PLItemChanged, self);
    var_AddCallback(p_playlist, "activity", PLItemChanged, self);
    var_AddCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "random", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "loop", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "volume", VolumeUpdated, self);
    var_AddCallback(p_playlist, "mute", VolumeUpdated, self);
bigben's avatar
bigben committed
634

635
    if (!OSX_SNOW_LEOPARD) {
636
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
637
            var_SetBool(p_playlist, "fullscreen", YES);
638
    }
639

640
    /* load our Core and Shared Dialogs nibs */
641
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
642
    [NSBundle loadNibNamed:@"SharedDialogs" owner: NSApp];
643

644
    /* subscribe to various interactive dialogues */
645
646
647
648
649
650
651
652
653
654
655
    var_Create(p_intf, "dialog-error", VLC_VAR_ADDRESS);
    var_AddCallback(p_intf, "dialog-error", DialogCallback, self);
    var_Create(p_intf, "dialog-critical", VLC_VAR_ADDRESS);
    var_AddCallback(p_intf, "dialog-critical", DialogCallback, self);
    var_Create(p_intf, "dialog-login", VLC_VAR_ADDRESS);
    var_AddCallback(p_intf, "dialog-login", DialogCallback, self);
    var_Create(p_intf, "dialog-question", VLC_VAR_ADDRESS);
    var_AddCallback(p_intf, "dialog-question", DialogCallback, self);
    var_Create(p_intf, "dialog-progress-bar", VLC_VAR_ADDRESS);
    var_AddCallback(p_intf, "dialog-progress-bar", DialogCallback, self);
    dialog_Register(p_intf);
656

657
658
659
660
661
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

662
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
663

664
    /* yeah, we are done */
665
666
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
667
    if (!OSX_SNOW_LEOPARD)
668
        b_nativeFullscreenMode = var_InheritBool(p_intf, "macosx-nativefullscreenmode");
669
#endif
670
671
672

    /* recover stored audio device, if set
     * in case it was unplugged in the meantime, auhal will fall back on the default */
673
    int i_value = config_GetInt(p_intf, "macosx-audio-device");
674
    if (i_value > 0)
675
        var_SetInteger(pl_Get(VLCIntf), "audio-device", i_value);
676

677
    if (config_GetInt(VLCIntf, "macosx-icon-change")) {
678
679
680
681
682
683
684
685
686
687
688
689
690
        /* After day 354 of the year, the usual VLC cone is replaced by another cone
         * wearing a Father Xmas hat.
         * Note: this icon doesn't represent an endorsement of The Coca-Cola Company.
         */
        NSCalendar *gregorian =
        [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
        [gregorian release];

        if (dayOfYear >= 354)
            [[VLCApplication sharedApplication] setApplicationIconImage: [NSImage imageNamed:@"vlc-xmas"]];
    }

691
692
    [self initStrings];

693
    nib_main_loaded = TRUE;
694
695
}

696
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
697
{
698
699
    if (!p_intf)
        return;
700

701
702
    [self updateCurrentlyUsedHotkeys];

703
    /* init media key support */
704
705
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
706
707
708
709
710
711
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [o_mediaKeyController startWatchingMediaKeys];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
712
713
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

714
715
    [self _removeOldPreferences];

716
717
718
719
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

720
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(lookForCrashLog) withObject:nil waitUntilDone:NO];
721

722
723
    /* we will need this, so let's load it here so the interface appears to be more responsive */
    nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner: NSApp];
724
725
726
727
728

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
729
730
}

hartman's avatar
hartman committed
731
- (void)initStrings
732
{
733
734
    if (!p_intf)
        return;
735

Jérome Decoodt's avatar
Jérome Decoodt committed
736
    /* messages panel */
737
    [o_msgs_panel setTitle: _NS("Messages")];
738
739
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
740

741
742
743
744
745
746
    /* crash reporter panel */
    [o_crashrep_send_btn setTitle: _NS("Send")];
    [o_crashrep_dontSend_btn setTitle: _NS("Don't Send")];
    [o_crashrep_title_txt setStringValue: _NS("VLC crashed previously")];
    [o_crashrep_win setTitle: _NS("VLC crashed previously")];
    [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, ...")];
747
748
    [o_crashrep_includeEmail_ckb setTitle: _NS("I agree to be possibly contacted about this bugreport.")];
    [o_crashrep_includeEmail_txt setStringValue: _NS("Only your default E-Mail address will be submitted, including no further information.")];
749
    [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
750
751
}

752
753
754
755
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
756
{
757
758
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
759
760
    static bool f_appExit = false;
    bool isTerminating;
761
762

    [o_appLock lock];
763
764
    isTerminating = f_appExit;
    f_appExit = true;
765
766
767
768
    [o_appLock unlock];

    if (isTerminating)
        return;
769

770
771
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
772

773
    playlist_t * p_playlist = pl_Get(p_intf);
774
775
    int returnedValue = 0;

776
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
777
    if (b_nativeFullscreenMode) {
778
779
780
781
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

782
    /* Save some interface state in configuration, at module quit */
783
784
785
    config_PutInt(p_intf, "random", var_GetBool(p_playlist, "random"));
    config_PutInt(p_intf, "loop", var_GetBool(p_playlist, "loop"));
    config_PutInt(p_intf, "repeat", var_GetBool(p_playlist, "repeat"));
786

787
    msg_Dbg(p_intf, "Terminating");
788

789
    /* unsubscribe from the interactive dialogues */
790
791
792
793
794
795
    dialog_Unregister(p_intf);
    var_DelCallback(p_intf, "dialog-error", DialogCallback, self);
    var_DelCallback(p_intf, "dialog-critical", DialogCallback, self);
    var_DelCallback(p_intf, "dialog-login", DialogCallback, self);
    var_DelCallback(p_intf, "dialog-question", DialogCallback, self);
    var_DelCallback(p_intf, "dialog-progress-bar", DialogCallback, self);
796
797
798
799
800
801
802
803
804
805
806
    //var_DelCallback(p_playlist, "item-change", PLItemChanged, self);
    var_DelCallback(p_playlist, "item-current", PLItemChanged, self);
    var_DelCallback(p_playlist, "activity", PLItemChanged, self);
    var_DelCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "random", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "loop", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "volume", VolumeUpdated, self);
    var_DelCallback(p_playlist, "mute", VolumeUpdated, self);
807
    var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
808
    var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
809

810
811
812
    if (p_current_input) {
        var_DelCallback(p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance]);
        vlc_object_release(p_current_input);
813
        p_current_input = NULL;
814
815
    }

816
817
    /* remove global observer watching for vout device changes correctly */
    [[NSNotificationCenter defaultCenter] removeObserver: self];
Jérome Decoodt's avatar
Jérome Decoodt committed
818

819
820
    /* release some other objects here, because it isn't sure whether dealloc
     * will be called later on */
821
    if (o_sprefs)
822
823
        [o_sprefs release];

824
    if (o_prefs)
825
        [o_prefs release];
826

827
    [o_open release];
828

829
    if (o_info)
830
        [o_info release];
hartman's avatar
hartman committed
831

832
    if (o_wizard)
833
834
835
836
        [o_wizard release];

    [crashLogURLConnection cancel];
    [crashLogURLConnection release];
837

838
    [o_coredialogs release];
839
840
    [o_eyetv release];

841
    /* unsubscribe from libvlc's debug messages */
842
    vlc_Unsubscribe(&p_intf->p_sys->sub);
843

844
845
    [o_msg_arr removeAllObjects];
    [o_msg_arr release];
846
    o_msg_arr = NULL;
847
848
    [o_usedHotkeys release];
    o_usedHotkeys = NULL;
849
850
851
852
853
854

    [o_msg_lock release];

    /* write cached user defaults to disk */
    [[NSUserDefaults standardUserDefaults] synchronize];

855
    [o_mainmenu release];
hartman's avatar
hartman committed
856

857
    libvlc_Quit(p_intf->p_libvlc);
858

859
860
861
    [o_mainwindow release];
    o_mainwindow = NULL;

862
    [self setIntf:nil];
hartman's avatar
hartman committed
863
864
}

865
866
867
868