intf.m 71.1 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_vout_display.h>
44
#include <unistd.h> /* execl() */
45

46
#import "CompatibilityFixes.h"
47
#import "intf.h"
48
#import "StringUtility.h"
49
#import "MainMenu.h"
50
#import "VideoView.h"
51
52
#import "prefs.h"
#import "playlist.h"
53
#import "playlistinfo.h"
54
55
56
57
#import "controls.h"
#import "open.h"
#import "wizard.h"
#import "bookmarks.h"
58
#import "coredialogs.h"
59
#import "AppleRemote.h"
60
#import "eyetv.h"
61
#import "simple_prefs.h"
62
#import "CoreInteraction.h"
63
#import "TrackSynchronization.h"
64
#import "VLCVoutWindowController.h"
65

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

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

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

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

80
81
82
83
84
85
86
87
88
89
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 *);
90

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

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

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

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

108
    memset(p_intf->p_sys, 0, sizeof(*p_intf->p_sys));
Jérome Decoodt's avatar
Jérome Decoodt committed
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];
Jean-Paul Saman's avatar
Jean-Paul Saman committed
116
    return VLC_SUCCESS;
117
118
119
120
121
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
122
void CloseIntf (vlc_object_t *p_this)
123
124
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;
Jérome Decoodt's avatar
Jérome Decoodt committed
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
135
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf) {
136
        msg_Err(p_wnd, "Mac OS X interface not found");
137
138
139
140
141
142
143
        return VLC_EGENERIC;
    }

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

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

152
153
154
155
156
157
158
159
160
161
162
163
    // 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];

164
165
166
167
168
169
170
171
172
173
174
175
176
177
    // 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];
    }

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

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

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

            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];
212
213
214
215
216
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
217
            int i_full = va_arg(args, int);
218
219
220
221
222
223
224
225
226
227
228
229

            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];
230
231
232
233
            [o_pool release];
            return VLC_SUCCESS;
        }
        default:
234
            msg_Warn(p_wnd, "unsupported control query");
235
            return VLC_EGENERIC;
236
    }
237
238
}

239
void WindowClose(vout_window_t *p_wnd)
240
{
241
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
242

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

245
    [o_pool release];
246
247
}

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

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

258
259
    o_appLock = [[NSLock alloc] init];

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

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

    raise(SIGTERM);
269
270
}

271
272
#pragma mark -
#pragma mark Variables Callback
273

274
275
276
/*****************************************************************************
 * 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
277
 * of this file.
278
 *****************************************************************************/
279
static void MsgCallback(void *data, int type, const msg_item_t *item, const char *format, va_list ap)
280
281
{
    int canc = vlc_savecancel();
282
    char *str;
283

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

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

293
294
    vlc_restorecancel(canc);
    free(str);
295
296
}

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

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

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

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

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

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

373
    [o_pool release];
374
375
376
    return VLC_SUCCESS;
}

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

383
    [o_pool release];
384
385
386
    return VLC_SUCCESS;
}

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

393
    [o_pool release];
394
395
396
    return VLC_SUCCESS;
}

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

403
    [o_pool release];
404
405
406
    return VLC_SUCCESS;
}

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

413
    [o_pool release];
414
415
416
417
418
419
420
421
    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
 *****************************************************************************/
422
423
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
424
425
{
    intf_thread_t * p_intf = VLCIntf;
426
427
428
429
    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)
430
431
432
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
433
    }
434
435
436
    return VLC_SUCCESS;
}

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

446
    if ([[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"]) {
447
448
449
450
451
452
453
454
        /* 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;
    }
455

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

459
460
461
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
462

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

    NSString *o_txt;
468
    if (text != NULL)
469
470
471
472
473
474
475
476
477
478
479
480
        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];
481
    [[[VLCMain sharedInstance] coreDialogProvider] performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
482
483
484
485
486
487
488
489
490
491
    [o_pool release];
}

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

492
493
494
495
496
497
#pragma mark -
#pragma mark Helpers

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

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

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
vout_thread_t *getVoutForActiveWindow(void)
{
    vout_thread_t *p_vout = nil;

    id currentWindow = [NSApp keyWindow];
    if ([currentWindow respondsToSelector:@selector(videoView)]) {
        VLCVoutView *videoView = [currentWindow videoView];
        if (videoView) {
            p_vout = [videoView voutThread];
        }
    }

    if (!p_vout)
        p_vout = getVout();

    return p_vout;
}

531
audio_output_t *getAout(void)
532
533
{
    input_thread_t *p_input = getInput();
534
535
    if (!p_input)
        return NULL;
536
    audio_output_t *p_aout = input_GetAout(p_input);
537
538
539
540
    vlc_object_release(p_input);
    return p_aout;
}

541
#pragma mark -
542
543
544
545
546
#pragma mark Private

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

548
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
549
 * VLCMain implementation
550
551
552
 *****************************************************************************/
@implementation VLCMain

553
@synthesize voutController=o_vout_controller;
554
@synthesize nativeFullscreenMode=b_nativeFullscreenMode;
555

556
557
558
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
559
560
561
562
563
564
565
static VLCMain *_o_sharedMainInstance = nil;

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

Jérome Decoodt's avatar
Jérome Decoodt committed
566
- (id)init
hartman's avatar
hartman committed
567
{
568
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
569
        [self dealloc];
570
        return _o_sharedMainInstance;
571
    } else
hartman's avatar
hartman committed
572
        _o_sharedMainInstance = [super init];
573

574
    p_intf = NULL;
575
    p_current_input = NULL;
576

577
    o_msg_lock = [[NSLock alloc] init];
578
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
579

580
    o_open = [[VLCOpen alloc] init];
581
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
582
    o_info = [[VLCInfo alloc] init];
583
    o_mainmenu = [[VLCMainMenu alloc] init];
584
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
585
    o_eyetv = [[VLCEyeTVController alloc] init];
586
    o_mainwindow = [[VLCMainWindow alloc] init];
587
588
589
590
591
592

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
593
594
595
596
597

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

598
599
    o_vout_controller = [[VLCVoutWindowController alloc] init];

hartman's avatar
hartman committed
600
601
602
    return _o_sharedMainInstance;
}

603
604
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
605
606
607
    p_intf = p_mainintf;
}

608
609
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
610
611
612
    return p_intf;
}

613
614
- (void)awakeFromNib
{
615
    playlist_t *p_playlist;
616
    vlc_value_t val;
617
618
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
619

620
621
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
622
623
    if (nib_main_loaded)
        return;
624

625
626
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
627

628
    p_playlist = pl_Get(p_intf);
hartman's avatar
hartman committed
629

630
    val.b_bool = false;
bigben's avatar
bigben committed
631

632
633
    var_AddCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
    var_AddCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
634
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
635
636
637
638
639
640
641
642
643
644
    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
645

646
    if (!OSX_SNOW_LEOPARD) {
647
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
648
            var_SetBool(p_playlist, "fullscreen", YES);
649
    }
650

651
    /* load our Core and Shared Dialogs nibs */
652
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
653
    [NSBundle loadNibNamed:@"SharedDialogs" owner: NSApp];
654

655
    /* subscribe to various interactive dialogues */
656
657
658
659
660
661
662
663
664
665
666
    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);
667

668
669
670
671
672
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

673
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
674

675
    /* yeah, we are done */
676
677
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
678
    if (!OSX_SNOW_LEOPARD)
679
        b_nativeFullscreenMode = var_InheritBool(p_intf, "macosx-nativefullscreenmode");
680
#endif
681
682
683

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

688
    if (config_GetInt(VLCIntf, "macosx-icon-change")) {
689
690
691
692
693
694
695
696
697
698
699
700
701
        /* 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"]];
    }

702
703
    [self initStrings];

704
    nib_main_loaded = TRUE;
705
706
}

707
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
708
{
709
710
    if (!p_intf)
        return;
711

712
713
    [self updateCurrentlyUsedHotkeys];

714
    /* init media key support */
715
716
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
717
718
719
720
721
722
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [o_mediaKeyController startWatchingMediaKeys];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
723
724
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

725
726
    [self _removeOldPreferences];

727
728
729
730
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

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

733
734
    /* 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];
735
736
737
738
739

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
740
741
}

hartman's avatar
hartman committed
742
- (void)initStrings
743
{
744
745
    if (!p_intf)
        return;
746

Jérome Decoodt's avatar
Jérome Decoodt committed
747
    /* messages panel */
748
    [o_msgs_panel setTitle: _NS("Messages")];
749
750
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
751

752
753
754
755
756
757
    /* 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, ...")];
758
759
    [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.")];
760
    [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
761
762
}

763
764
765
766
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
767
{
768
769
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
770
771
    static bool f_appExit = false;
    bool isTerminating;
772
773

    [o_appLock lock];
774
775
    isTerminating = f_appExit;
    f_appExit = true;
776
777
778
779
    [o_appLock unlock];

    if (isTerminating)
        return;
780

781
782
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
783

784
    playlist_t * p_playlist = pl_Get(p_intf);
785
786
    int returnedValue = 0;

787
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
788
    if (b_nativeFullscreenMode) {
789
790
791
792
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

793
    /* Save some interface state in configuration, at module quit */
794
795
796
    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"));
797

798
    msg_Dbg(p_intf, "Terminating");
799

800
    /* unsubscribe from the interactive dialogues */
801
802
803
804
805
806
    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);
807
808
809
810
811
812
813
814
815
816
817
    //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);
818
    var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
819
    var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
820

821
822
823
    if (p_current_input) {
        var_DelCallback(p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance]);
        vlc_object_release(p_current_input);
824
        p_current_input = NULL;
825
826
    }

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

830
831
    /* release some other objects here, because it isn't sure whether dealloc
     * will be called later on */
832
    if (o_sprefs)
833
834
        [o_sprefs release];

835
    if (o_prefs)
836
        [o_prefs release];
837

838
    [o_open release];
839

840
    if (o_info)
841
        [o_info release];
hartman's avatar
hartman committed
842

843
    if (o_wizard)
844
845
846
847
        [o_wizard release];

    [crashLogURLConnection cancel];
    [crashLogURLConnection release];
848

849
    [o_coredialogs release];
850
851
    [o_eyetv release];

852
    /* unsubscribe from libvlc's debug messages */
853
    vlc_Unsubscribe(&p_intf->p_sys->sub);
854

855
856
    [o_msg_arr removeAllObjects];
    [o_msg_arr release];
857
    o_msg_arr = NULL;
858
859
    [o_usedHotkeys release];
    o_usedHotkeys = NULL;
860
861
862
863
864
865

    [o_msg_lock release];

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

866
    [o_mainmenu release];
hartman's avatar
hartman committed
867

868
869
870
    [o_vout_controller release];
    o_vout_controller = nil;

871
    libvlc_Quit(p_intf->p_libvlc);
872

873
874