intf.m 63.5 KB
Newer Older
1
/*****************************************************************************
2
 * intf.m: MacOS X interface module
3
 *****************************************************************************
4
 * Copyright (C) 2002-2013 VLC authors and VideoLAN
5
 * $Id$
6
7
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8
 *          Derk-Jan Hartman <hartman at videolan.org>
9
 *          Felix Paul Kühne <fkuehne at videolan dot org>
10
 *          Pierre d'Herbemont <pdherbemont # videolan org>
11
 *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
12
13
14
15
16
 *
 * 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.
17
 *
18
19
20
21
22
23
24
 * 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
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26
27
28
29
30
 *****************************************************************************/

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

35
36
37
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/param.h>                                    /* for MAXPATHLEN */
#include <string.h>
hartman's avatar
hartman committed
38
#include <vlc_common.h>
39
#include <vlc_keys.h>
40
#include <vlc_dialog.h>
41
#include <vlc_url.h>
42
#include <vlc_modules.h>
43
#include <vlc_plugin.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
#import "ExtensionsManager.h"
67
#import "BWQuincyManager.h"
68

69
#import "VideoEffects.h"
70
#import "AudioEffects.h"
71

Rafaël Carré's avatar
Rafaël Carré committed
72
#import <Sparkle/Sparkle.h>                 /* we're the update delegate */
73

74
75
#import "iTunes.h"

76
77
78
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
79
static void Run (intf_thread_t *p_intf);
80

81
82
83
84
static void updateProgressPanel (void *, const char *, float);
static bool checkProgressPanel (void *);
static void destroyProgressPanel (void *);

85
86
87
88
89
90
91
92
93
94
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 *);
95
96
static int BossCallback(vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void *);
97

98
#pragma mark -
99
#pragma mark VLC Interface Object Callbacks
100

101
102
103
/*****************************************************************************
 * OpenIntf: initialize interface
 *****************************************************************************/
104
int OpenIntf (vlc_object_t *p_this)
Jérome Decoodt's avatar
Jérome Decoodt committed
105
{
106
107
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    [VLCApplication sharedApplication];
108

109
110
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

111
112
    p_intf->p_sys = malloc(sizeof(intf_sys_t));
    if (p_intf->p_sys == NULL)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
113
        return VLC_ENOMEM;
114

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

117
    Run(p_intf);
118

119
    [o_pool release];
Jean-Paul Saman's avatar
Jean-Paul Saman committed
120
    return VLC_SUCCESS;
121
122
123
124
125
}

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

130
    free(p_intf->p_sys);
131
132
}

133
134
static NSLock * o_vout_provider_lock = nil;

135
static int WindowControl(vout_window_t *, int i_query, va_list);
136

137
int WindowOpen(vout_window_t *p_wnd, const vout_window_cfg_t *cfg)
138
{
139
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
140
141
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf) {
142
        msg_Err(p_wnd, "Mac OS X interface not found");
143
        [o_pool release];
144
145
        return VLC_EGENERIC;
    }
146
    NSRect proposedVideoViewPosition = NSMakeRect(cfg->x, cfg->y, cfg->width, cfg->height);
147

148
    [o_vout_provider_lock lock];
149
    VLCVoutWindowController *o_vout_controller = [[VLCMain sharedInstance] voutController];
150
151
152
153
154
155
    if (!o_vout_controller) {
        [o_vout_provider_lock unlock];
        [o_pool release];
        return VLC_EGENERIC;
    }

156
157
158
159
160
161
162
163
164
165
166
167
168
169
    SEL sel = @selector(setupVoutForWindow:withProposedVideoViewPosition:);
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
    [inv setTarget:o_vout_controller];
    [inv setSelector:sel];
    [inv setArgument:&p_wnd atIndex:2]; // starting at 2!
    [inv setArgument:&proposedVideoViewPosition atIndex:3];

    [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                       waitUntilDone:YES];

    VLCVoutView *videoView = nil;
    [inv getReturnValue:&videoView];

    if (!videoView) {
170
        msg_Err(p_wnd, "got no video view from the interface");
171
        [o_vout_provider_lock unlock];
172
        [o_pool release];
173
174
175
        return VLC_EGENERIC;
    }

176
177
178
    msg_Dbg(VLCIntf, "returning videoview with proposed position x=%i, y=%i, width=%i, height=%i", cfg->x, cfg->y, cfg->width, cfg->height);
    p_wnd->handle.nsobject = videoView;

179
    [o_vout_provider_lock unlock];
180

181
    [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
182
    p_wnd->control = WindowControl;
183

184
    [o_pool release];
185
186
187
    return VLC_SUCCESS;
}

188
static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
189
{
190
191
192
193
194
195
196
197
198
199
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    [o_vout_provider_lock lock];
    VLCVoutWindowController *o_vout_controller = [[VLCMain sharedInstance] voutController];
    if (!o_vout_controller) {
        [o_vout_provider_lock unlock];
        [o_pool release];
        return VLC_EGENERIC;
    }

200
    switch(i_query) {
201
202
        case VOUT_WINDOW_SET_STATE:
        {
203
            unsigned i_state = va_arg(args, unsigned);
204
205
206
207
208
209

            NSInteger i_cooca_level = NSNormalWindowLevel;
            if (i_state & VOUT_WINDOW_STATE_ABOVE)
                i_cooca_level = NSStatusWindowLevel;

            SEL sel = @selector(setWindowLevel:forWindow:);
210
211
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
212
213
214
215
216
217
            [inv setSelector:sel];
            [inv setArgument:&i_cooca_level atIndex:2]; // starting at 2!
            [inv setArgument:&p_wnd atIndex:3];
            [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                               waitUntilDone:NO];

218
            break;
219
220
221
        }
        case VOUT_WINDOW_SET_SIZE:
        {
222

223
224
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
225

226
            NSSize newSize = NSMakeSize(i_width, i_height);
227
            SEL sel = @selector(setNativeVideoSize:forWindow:);
228
229
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
230
231
232
233
234
235
            [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];

236
            break;
237
238
239
240
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
241
            int i_full = va_arg(args, int);
242
243

            SEL sel = @selector(setFullscreen:forWindow:);
244
245
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
246
247
248
249
250
251
            [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];

252
            break;
253
254
        }
        default:
255
        {
256
            msg_Warn(p_wnd, "unsupported control query");
257
258
            [o_vout_provider_lock unlock];
            [o_pool release];
259
            return VLC_EGENERIC;
260
        }
261
    }
262
263
264
265

    [o_vout_provider_lock unlock];
    [o_pool release];
    return VLC_SUCCESS;
266
267
}

268
void WindowClose(vout_window_t *p_wnd)
269
{
270
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
271

272
273
274
275
276
277
278
279
280
281
    [o_vout_provider_lock lock];
    VLCVoutWindowController *o_vout_controller = [[VLCMain sharedInstance] voutController];
    if (!o_vout_controller) {
        [o_vout_provider_lock unlock];
        [o_pool release];
        return;
    }

    [o_vout_controller performSelectorOnMainThread:@selector(removeVoutforDisplay:) withObject:[NSValue valueWithPointer:p_wnd] waitUntilDone:NO];
    [o_vout_provider_lock unlock];
282

283
    [o_pool release];
284
285
}

286
287
288
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
289
static NSLock * o_appLock = nil;    // controls access to f_appExit
290
static NSLock * o_plItemChangedLock = nil;
291

292
static void Run(intf_thread_t *p_intf)
293
{
294
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
295
    [VLCApplication sharedApplication];
296

297
    o_appLock = [[NSLock alloc] init];
298
    o_plItemChangedLock = [[NSLock alloc] init];
299
    o_vout_provider_lock = [[NSLock alloc] init];
300

hartman's avatar
-    
hartman committed
301
    [[VLCMain sharedInstance] setIntf: p_intf];
302

hartman's avatar
hartman committed
303
    [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
304

305
306
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
307
    [o_plItemChangedLock release];
308
    [o_appLock release];
309
310
    [o_vout_provider_lock release];
    o_vout_provider_lock = nil;
311
    [o_pool release];
312
313

    raise(SIGTERM);
314
315
}

316
317
#pragma mark -
#pragma mark Variables Callback
318

319
320
static int InputEvent(vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t new_val, void *param)
321
{
322
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
323
324
    switch (new_val.i_int) {
        case INPUT_EVENT_STATE:
325
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackStatusUpdated) withObject: nil waitUntilDone:NO];
326
327
            break;
        case INPUT_EVENT_RATE:
328
            [[[VLCMain sharedInstance] mainMenu] performSelectorOnMainThread:@selector(updatePlaybackRate) withObject: nil waitUntilDone:NO];
329
330
            break;
        case INPUT_EVENT_POSITION:
331
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject: nil waitUntilDone:NO];
332
333
334
            break;
        case INPUT_EVENT_TITLE:
        case INPUT_EVENT_CHAPTER:
335
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
336
337
            break;
        case INPUT_EVENT_CACHE:
338
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
339
340
341
342
343
344
345
346
347
348
349
350
351
352
            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:
353
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
354
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
355
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateInfoandMetaPanel) withObject: nil waitUntilDone:NO];
356
357
358
359
            break;
        case INPUT_EVENT_BOOKMARK:
            break;
        case INPUT_EVENT_RECORD:
360
            [[VLCMain sharedInstance] updateRecordState: var_GetBool(p_this, "record")];
361
362
            break;
        case INPUT_EVENT_PROGRAM:
363
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
364
365
366
367
368
369
370
            break;
        case INPUT_EVENT_ITEM_EPG:
            break;
        case INPUT_EVENT_SIGNAL:
            break;

        case INPUT_EVENT_ITEM_NAME:
371
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
372
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
373
374
375
376
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
377
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
378
379
380
            break;

        case INPUT_EVENT_DEAD:
381
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
382
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
383
384
385
            break;

        case INPUT_EVENT_ABORT:
386
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
387
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
388
389
390
391
            break;

        default:
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
392
    }
393

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

398
399
static int PLItemChanged(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
400
{
401
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
402
403
404
405
406

    /* Due to constraints within NSAttributedString's main loop runtime handling
     * and other issues, we need to wait for -PlaylistItemChanged to finish and
     * then -informInputChanged on this non-main thread. */
    [o_plItemChangedLock lock];
407
408
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(PlaylistItemChanged) withObject:nil waitUntilDone:YES]; // MUST BE ON MAIN THREAD
    [[VLCMain sharedInstance] informInputChanged]; // DO NOT MOVE TO MAIN THREAD
409
    [o_plItemChangedLock unlock];
410

411
    [o_pool release];
412
413
414
    return VLC_SUCCESS;
}

415
416
static int PlaylistUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
417
{
418
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
419
420
421
422
423
424
425
426
427
428
429

    /* Avoid event queue flooding with playlistUpdated selectors, leading to UI freezes.
     * Therefore, only enqueue if no selector already enqueued.
     */
    VLCMain *o_main = [VLCMain sharedInstance];
    @synchronized(o_main) {
        if(![o_main playlistUpdatedSelectorInQueue]) {
            [o_main setPlaylistUpdatedSelectorInQueue:YES];
            [o_main performSelectorOnMainThread:@selector(playlistUpdated) withObject:nil waitUntilDone:NO];
        }
    }
430

431
    [o_pool release];
432
433
434
    return VLC_SUCCESS;
}

435
436
static int PlaybackModeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
437
{
438
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
439
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackModeUpdated) withObject:nil waitUntilDone:NO];
440

441
    [o_pool release];
442
443
444
    return VLC_SUCCESS;
}

445
446
static int VolumeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
447
{
448
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
449
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateVolume) withObject:nil waitUntilDone:NO];
450

451
    [o_pool release];
452
453
454
    return VLC_SUCCESS;
}

455
456
457
458
459
460
461
462
463
464
465
466
static int BossCallback(vlc_object_t *p_this, const char *psz_var,
                        vlc_value_t oldval, vlc_value_t new_val, void *param)
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];

    [[VLCCoreInteraction sharedInstance] performSelectorOnMainThread:@selector(pause) withObject:nil waitUntilDone:NO];
    [[VLCApplication sharedApplication] hide:nil];

    [o_pool release];
    return VLC_SUCCESS;
}

467
468
469
470
471
/*****************************************************************************
 * 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
 *****************************************************************************/
472
473
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
474
475
{
    intf_thread_t * p_intf = VLCIntf;
476
477
478
479
    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)
480
481
482
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
483
    }
484
485
486
    return VLC_SUCCESS;
}

487
/*****************************************************************************
488
 * DialogCallback: Callback triggered by the "dialog-*" variables
489
 * to let the intf display error and interaction dialogs
490
 *****************************************************************************/
491
static int DialogCallback(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
492
493
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
494
495
    VLCMain *interface = (VLCMain *)data;

496
    if ([[NSString stringWithUTF8String:type] isEqualToString: @"dialog-progress-bar"]) {
497
498
499
500
501
502
503
504
        /* 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;
    }
505

506
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
507
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
508

509
510
511
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
512

513
514
515
516
517
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
518
    if (text != NULL)
519
        o_txt = [NSString stringWithUTF8String:text];
520
521
522
523
524
525
526
527
528
529
530
    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];
531
532
533
534

    if ([[NSApplication sharedApplication] isRunning])
        [[[VLCMain sharedInstance] coreDialogProvider] performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];

535
536
537
538
539
540
541
542
543
544
    [o_pool release];
}

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

545
546
547
548
549
550
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
551
552
    if (!p_intf)
        return NULL;
553
    return pl_CurrentInput(p_intf);
554
555
556
557
558
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
559
560
    if (!p_input)
        return NULL;
561
562
563
564
565
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
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;
}

584
audio_output_t *getAout(void)
585
{
586
587
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf)
588
        return NULL;
589
    return playlist_GetAout(pl_Get(p_intf));
590
591
}

592
#pragma mark -
593
594
#pragma mark Private

595
@interface VLCMain () <BWQuincyManagerDelegate>
596
597
598
599
600
601
602
- (void)removeOldPreferences;
@end

@interface VLCMain (Internal)
- (void)handlePortMessage:(NSPortMessage *)o_msg;
- (void)resetMediaKeyJump;
- (void)coreChangedMediaKeySupportSetting: (NSNotification *)o_notification;
603
@end
604

605
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
606
 * VLCMain implementation
607
608
609
 *****************************************************************************/
@implementation VLCMain

610
@synthesize voutController=o_vout_controller;
611
@synthesize nativeFullscreenMode=b_nativeFullscreenMode;
612
@synthesize playlistUpdatedSelectorInQueue=b_playlist_updated_selector_in_queue;
613

614
615
616
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
617
618
619
620
621
622
623
static VLCMain *_o_sharedMainInstance = nil;

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

Jérome Decoodt's avatar
Jérome Decoodt committed
624
- (id)init
hartman's avatar
hartman committed
625
{
626
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
627
        [self dealloc];
628
        return _o_sharedMainInstance;
629
    } else
hartman's avatar
hartman committed
630
        _o_sharedMainInstance = [super init];
631

632
    p_intf = NULL;
633
    p_current_input = p_input_changed = NULL;
634

635
    o_open = [[VLCOpen alloc] init];
636
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
637
    o_info = [[VLCInfo alloc] init];
638
    o_mainmenu = [[VLCMainMenu alloc] init];
639
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
640
641
642
643
644
645
646
    o_eyetv = [[VLCEyeTVController alloc] init];

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
647
648
649
650
651

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

652
653
    o_vout_controller = [[VLCVoutWindowController alloc] init];

hartman's avatar
hartman committed
654
655
656
    return _o_sharedMainInstance;
}

657
658
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
659
660
661
    p_intf = p_mainintf;
}

662
663
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
664
665
666
    return p_intf;
}

667
668
- (void)awakeFromNib
{
669
    playlist_t *p_playlist;
670
    vlc_value_t val;
671
672
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
673

674
675
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
676
677
    if (nib_main_loaded)
        return;
678

679
    p_playlist = pl_Get(p_intf);
hartman's avatar
hartman committed
680

681
    val.b_bool = false;
bigben's avatar
bigben committed
682

683
684
    var_AddCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
    var_AddCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
685
    var_AddCallback(p_intf->p_libvlc, "intf-boss", BossCallback, self);
686
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
687
688
689
690
691
692
693
694
695
    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
696

697
    if (!OSX_SNOW_LEOPARD) {
698
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
699
            var_SetBool(p_playlist, "fullscreen", YES);
700
    }
701

702
    /* load our Core and Shared Dialogs nibs */
703
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
704
    [NSBundle loadNibNamed:@"SharedDialogs" owner: NSApp];
705

706
    /* subscribe to various interactive dialogues */
707
708
709
710
711
712
713
714
715
716
717
    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);
718

719
720
721
722
723
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

724
    /* yeah, we are done */
725
726
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
727
    if (!OSX_SNOW_LEOPARD)
728
        b_nativeFullscreenMode = var_InheritBool(p_intf, "macosx-nativefullscreenmode");
729
#endif
730

731
    if (config_GetInt(VLCIntf, "macosx-icon-change")) {
732
733
734
735
736
737
738
739
740
741
742
743
744
        /* 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"]];
    }

745
    nib_main_loaded = TRUE;
746
747
}

Rafaël Carré's avatar
Rafaël Carré committed
748
749
750
751
752
753
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
    playlist_t * p_playlist = pl_Get(VLCIntf);
    PL_LOCK;
    items_at_launch = p_playlist->p_local_category->i_children;
    PL_UNLOCK;
754

755
    [NSBundle loadNibNamed:@"MainWindow" owner: self];
756
    [o_mainwindow makeKeyAndOrderFront:nil];
757
758

    [[SUUpdater sharedUpdater] setDelegate:self];
Rafaël Carré's avatar
Rafaël Carré committed
759
760
}

761
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
762
{
Rafaël Carré's avatar
Rafaël Carré committed
763
764
    launched = YES;

765
766
    if (!p_intf)
        return;
767

768
769
770
771
772
773
774
775
776
777
778
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey: @"CFBundleVersion"];
    NSRange endRande = [appVersion rangeOfString:@"-"];
    if (endRande.location != NSNotFound)
        appVersion = [appVersion substringToIndex:endRande.location];

    BWQuincyManager *quincyManager = [BWQuincyManager sharedQuincyManager];
    [quincyManager setApplicationVersion:appVersion];
    [quincyManager setSubmissionURL:@"http://crash.videolan.org/crash_v200.php"];
    [quincyManager setDelegate:self];
    [quincyManager setCompanyName:@"VideoLAN"];

779
780
    [self updateCurrentlyUsedHotkeys];

781
    /* init media key support */
782
783
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
784
785
786
787
788
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
789
790
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

791
    [self removeOldPreferences];
792

793
794
795
796
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

797
798
    /* 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];
799
800
801
802
803

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
804
805
806
807
808
809
810

    playlist_t * p_playlist = pl_Get(VLCIntf);
    PL_LOCK;
    BOOL kidsAround = p_playlist->p_local_category->i_children;
    PL_UNLOCK;
    if (kidsAround && var_GetBool(p_playlist, "playlist-autostart"))
        [[self playlist] playItem:nil];
811
812
}

813
814
815
816
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
817
{
818
819
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
820
821
    static bool f_appExit = false;
    bool isTerminating;
822
823

    [o_appLock lock];
824
825
    isTerminating = f_appExit;
    f_appExit = true;
826
827
828
829
    [o_appLock unlock];

    if (isTerminating)
        return;
830

831
832
    [self resumeItunesPlayback:nil];

833
834
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
835

836
    playlist_t * p_playlist = pl_Get(p_intf);
837
838
    int returnedValue = 0;

839
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
840
    if (b_nativeFullscreenMode && [o_mainwindow fullscreen]) {
841
842
843
844
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

845
    /* save current video and audio profiles */
846
    [[VLCVideoEffects sharedInstance] saveCurrentProfile];
847
    [[VLCAudioEffects sharedInstance] saveCurrentProfile];
848

849
    /* Save some interface state in configuration, at module quit */
850
851
852
    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"));
853

854
    msg_Dbg(p_intf, "Terminating");
855

856
    /* unsubscribe from the interactive dialogues */
857
858
859
860
861
862
    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);
863
864
865
866
867
868
869
870
871
872
    //var_DelCallback(p_playlist, "item-change", 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);
873
    var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
874
    var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
875
    var_DelCallback(p_intf->p_libvlc, "intf-boss", BossCallback, self);
876

877
878
879
    if (p_current_input) {
        var_DelCallback(p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance]);
        vlc_object_release(p_current_input);
880
        p_current_input = NULL;
881
882
    }

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

886
    [o_vout_provider_lock lock];
887
888
889
    // release before o_info!
    [o_vout_controller release];
    o_vout_controller = nil;
890
    [o_vout_provider_lock unlock];
891

892
893
    /* release some other objects here, because it isn't sure whether dealloc
     * will be called later on */
894
    if (o_sprefs)
895
896
        [o_sprefs release];

897
    if (o_prefs)
898
        [o_prefs release];
899

900
    [o_open release];
901

902
    if (o_info)
903
        [o_info release];
hartman's avatar
hartman committed
904

905
    if (o_wizard)
906
907
        [o_wizard release];

908
    [o_coredialogs release];
909
910
    [o_eyetv release];

911
    /* unsubscribe from libvlc's debug messages */
912
    vlc_LogSet(p_intf->p_libvlc, NULL, NULL);
913