intf.m 72.7 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
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
 *          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

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

71
#import <AddressBook/AddressBook.h>         /* for crashlog send mechanism */
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
static void MsgCallback(void *data, int type, const vlc_log_t *item, const char *format, va_list ap);
86

87
88
89
90
91
92
93
94
95
96
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 *);
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
static int WindowControl(vout_window_t *, int i_query, va_list);
134

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

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    VLCVoutWindowController *o_vout_controller = [[VLCMain sharedInstance] voutController];
    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) {
160
        msg_Err(p_wnd, "got no video view from the interface");
161
        [o_pool release];
162
163
164
        return VLC_EGENERIC;
    }

165
166
167
    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;

168

169
170
171
172
173
    // 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:);
174
175
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[[VLCMain sharedInstance] voutController] methodSignatureForSelector:sel]];
        [inv setTarget:[[VLCMain sharedInstance] voutController]];
176
177
178
179
180
181
182
        [inv setSelector:sel];
        [inv setArgument:&i_full atIndex:2];
        [inv setArgument:&p_wnd atIndex:3];
        [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                           waitUntilDone:NO];
    }

183
    [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
184
    p_wnd->control = WindowControl;
185

186
    [o_pool release];
187
188
189
    return VLC_SUCCESS;
}

190
static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
191
{
192
    switch(i_query) {
193
194
        case VOUT_WINDOW_SET_STATE:
        {
195
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
196
            unsigned i_state = va_arg(args, unsigned);
197
198
199
200
201
202
203
204
205
206
207
208
209
210

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

            SEL sel = @selector(setWindowLevel:forWindow:);
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[[VLCMain sharedInstance] voutController] methodSignatureForSelector:sel]];
            [inv setTarget:[[VLCMain sharedInstance] voutController]];
            [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];

211
            [o_pool release];
212
213
214
215
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_SIZE:
        {
216
217
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

218
219
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
220

221
            NSSize newSize = NSMakeSize(i_width, i_height);
222
223
224
225
226
227
228
229
230
231
            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];
232
233
234
235
236
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
237
            int i_full = va_arg(args, int);
238
239

            SEL sel = @selector(setFullscreen:forWindow:);
240
241
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[[VLCMain sharedInstance] voutController] methodSignatureForSelector:sel]];
            [inv setTarget:[[VLCMain sharedInstance] voutController]];
242
243
244
245
246
247
            [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];

248
249
250
251
            [o_pool release];
            return VLC_SUCCESS;
        }
        default:
252
            msg_Warn(p_wnd, "unsupported control query");
253
            return VLC_EGENERIC;
254
    }
255
256
}

257
void WindowClose(vout_window_t *p_wnd)
258
{
259
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
260

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

263
    [o_pool release];
264
265
}

266
267
268
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
269
static NSLock * o_appLock = nil;    // controls access to f_appExit
270
static NSLock * o_plItemChangedLock = nil;
271

272
static void Run(intf_thread_t *p_intf)
273
{
274
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
275
    [VLCApplication sharedApplication];
276

277
    o_appLock = [[NSLock alloc] init];
278
    o_plItemChangedLock = [[NSLock alloc] init];
279

hartman's avatar
-    
hartman committed
280
    [[VLCMain sharedInstance] setIntf: p_intf];
281
282
283
284

    /* subscribe to LibVLCCore's messages */
    vlc_LogSet(p_intf->p_libvlc, MsgCallback, NULL);

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

287
288
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
289
    [o_plItemChangedLock release];
290
    [o_appLock release];
291
    [o_pool release];
292
293

    raise(SIGTERM);
294
295
}

296
297
#pragma mark -
#pragma mark Variables Callback
298

299
300
301
/*****************************************************************************
 * 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
302
 * of this file.
303
 *****************************************************************************/
304
static void MsgCallback(void *data, int type, const vlc_log_t *item, const char *format, va_list ap)
305
306
{
    int canc = vlc_savecancel();
307
    char *str;
308

309
310
    if (vasprintf(&str, format, ap) == -1) {
        vlc_restorecancel(canc);
311
        return;
312
    }
313

314
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
315
    [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
316
    [o_pool release];
317

318
319
    vlc_restorecancel(canc);
    free(str);
320
321
}

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

        case INPUT_EVENT_ITEM_NAME:
374
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
375
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
376
377
378
379
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
380
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
381
382
383
            break;

        case INPUT_EVENT_DEAD:
384
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
385
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
386
387
388
            break;

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

        default:
394
            //msg_Warn(p_this, "unhandled input event (%lld)", new_val.i_int);
395
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
396
    }
397

398
    [o_pool release];
399
400
401
    return VLC_SUCCESS;
}

402
403
static int PLItemChanged(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
404
{
405
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
406
407
408
409
410
411

    /* 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];
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(PlaylistItemChanged) withObject:nil waitUntilDone:YES];
412
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(informInputChanged) withObject:nil waitUntilDone:YES];
413
    [o_plItemChangedLock unlock];
414

415
    [o_pool release];
416
417
418
    return VLC_SUCCESS;
}

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

    /* 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];
        }
    }
434

435
    [o_pool release];
436
437
438
    return VLC_SUCCESS;
}

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

445
    [o_pool release];
446
447
448
    return VLC_SUCCESS;
}

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

455
    [o_pool release];
456
457
458
459
460
461
462
463
    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
 *****************************************************************************/
464
465
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
466
467
{
    intf_thread_t * p_intf = VLCIntf;
468
469
470
471
    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)
472
473
474
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
475
    }
476
477
478
    return VLC_SUCCESS;
}

479
/*****************************************************************************
480
 * DialogCallback: Callback triggered by the "dialog-*" variables
481
 * to let the intf display error and interaction dialogs
482
 *****************************************************************************/
483
static int DialogCallback(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
484
485
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
486
487
    VLCMain *interface = (VLCMain *)data;

488
    if ([[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"]) {
489
490
491
492
493
494
495
496
        /* 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;
    }
497

498
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
499
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
500

501
502
503
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
504

505
506
507
508
509
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
510
    if (text != NULL)
511
512
513
514
515
516
517
518
519
520
521
522
        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];
523
524
525
526

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

527
528
529
530
531
532
533
534
535
536
    [o_pool release];
}

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

537
538
539
540
541
542
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
543
544
    if (!p_intf)
        return NULL;
545
    return pl_CurrentInput(p_intf);
546
547
548
549
550
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
551
552
    if (!p_input)
        return NULL;
553
554
555
556
557
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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;
}

576
audio_output_t *getAout(void)
577
{
578
579
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf)
580
        return NULL;
581
    return playlist_GetAout(pl_Get(p_intf));
582
583
}

584
#pragma mark -
585
586
587
#pragma mark Private

@interface VLCMain ()
588
589
590
591
592
593
594
- (void)removeOldPreferences;
@end

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

597
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
598
 * VLCMain implementation
599
600
601
 *****************************************************************************/
@implementation VLCMain

602
@synthesize voutController=o_vout_controller;
603
@synthesize nativeFullscreenMode=b_nativeFullscreenMode;
604
@synthesize playlistUpdatedSelectorInQueue=b_playlist_updated_selector_in_queue;
605

606
607
608
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
609
610
611
612
613
614
615
static VLCMain *_o_sharedMainInstance = nil;

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

Jérome Decoodt's avatar
Jérome Decoodt committed
616
- (id)init
hartman's avatar
hartman committed
617
{
618
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
619
        [self dealloc];
620
        return _o_sharedMainInstance;
621
    } else
hartman's avatar
hartman committed
622
        _o_sharedMainInstance = [super init];
623

624
    p_intf = NULL;
625
    p_current_input = p_input_changed = NULL;
626

627
    o_msg_lock = [[NSLock alloc] init];
628
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
629

630
    o_open = [[VLCOpen alloc] init];
631
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
632
    o_info = [[VLCInfo alloc] init];
633
    o_mainmenu = [[VLCMainMenu alloc] init];
634
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
635
    o_eyetv = [[VLCEyeTVController alloc] init];
636
    o_mainwindow = [[VLCMainWindow alloc] init];
637
638
639
640
641
642

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
643
644
645
646
647

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

648
649
    o_vout_controller = [[VLCVoutWindowController alloc] init];

hartman's avatar
hartman committed
650
651
652
    return _o_sharedMainInstance;
}

653
654
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
655
656
657
    p_intf = p_mainintf;
}

658
659
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
660
661
662
    return p_intf;
}

663
664
- (void)awakeFromNib
{
665
    playlist_t *p_playlist;
666
    vlc_value_t val;
667
668
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
669

670
671
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
672
673
    if (nib_main_loaded)
        return;
674

675
676
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
677

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

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

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

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

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

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

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

722
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
723

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
746
    [self initStrings];

747
    nib_main_loaded = TRUE;
748
749
}

750
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
751
{
752
753
    if (!p_intf)
        return;
754

755
756
    [self updateCurrentlyUsedHotkeys];

757
    /* init media key support */
758
759
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
760
761
762
763
764
765
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [o_mediaKeyController startWatchingMediaKeys];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
766
767
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

768
    [self removeOldPreferences];
769

770
771
772
773
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

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

776
777
    /* 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];
778
779
780
781
782

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
783
784
}

hartman's avatar
hartman committed
785
- (void)initStrings
786
{
787
788
    if (!p_intf)
        return;
789

Jérome Decoodt's avatar
Jérome Decoodt committed
790
    /* messages panel */
791
    [o_msgs_panel setTitle: _NS("Messages")];
792
793
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
794

795
796
797
798
799
800
    /* 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, ...")];
801
802
    [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.")];
803
    [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
804
805
}

806
807
808
809
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
810
{
811
812
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
813
814
    static bool f_appExit = false;
    bool isTerminating;
815
816

    [o_appLock lock];
817
818
    isTerminating = f_appExit;
    f_appExit = true;
819
820
821
822
    [o_appLock unlock];

    if (isTerminating)
        return;
823

824
825
    [self resumeItunesPlayback:nil];

826
827
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
828

829
    playlist_t * p_playlist = pl_Get(p_intf);
830
831
    int returnedValue = 0;

832
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
833
    if (b_nativeFullscreenMode) {
834
835
836
837
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

838
    /* save current video and audio profiles */
839
    [[VLCVideoEffects sharedInstance] saveCurrentProfile];
840
    [[VLCAudioEffects sharedInstance] saveCurrentProfile];
841

842
    /* Save some interface state in configuration, at module quit */
843
844
845
    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"));
846

847
    msg_Dbg(p_intf, "Terminating");
848

849
    /* unsubscribe from the interactive dialogues */
850
851
852
853
854
855
    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);
856
857
858
859
860
861
862
863
864
865
    //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);
866
    var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
867
    var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
868

869
870
871
    if (p_current_input) {
        var_DelCallback(p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance]);
        vlc_object_release(p_current_input);
872
        p_current_input = NULL;
873
874
    }

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

878
879
880
881
    // release before o_info!
    [o_vout_controller release];
    o_vout_controller = nil;

882
883
    /* release some other objects here, because it isn't sure whether dealloc
     * will be called later on */
884
    if (o_sprefs)
885
886
        [o_sprefs release];

887
    if (o_prefs)
888
        [o_prefs release];
889

890
    [o_open release];
891

892
    if (o_info)
893
        [o_info release];
hartman's avatar
hartman committed
894

895
    if (o_wizard)