intf.m 75.4 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

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
static int BossCallback(vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void *);
99

100
#pragma mark -
101
#pragma mark VLC Interface Object Callbacks
102

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

111
112
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

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

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

119
    Run(p_intf);
120

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

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

132
    free(p_intf->p_sys);
133
134
}

135
136
static NSLock * o_vout_provider_lock = nil;

137
static int WindowControl(vout_window_t *, int i_query, va_list);
138

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

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

158
159
160
161
162
163
164
165
166
167
168
169
170
171
    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) {
172
        msg_Err(p_wnd, "got no video view from the interface");
173
        [o_vout_provider_lock unlock];
174
        [o_pool release];
175
176
177
        return VLC_EGENERIC;
    }

178
179
180
    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;

181
    // TODO: find a cleaner way for "start in fullscreen"
182
183
184
185
186
187
188
    // either prefs settings, or fullscreen button was pressed before
    if (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen")) {

        // this is not set when we start in fullscreen because of
        // fullscreen settings in video prefs the second time
        var_SetBool(p_wnd->p_parent, "fullscreen", 1);

189
190
191
        int i_full = 1;

        SEL sel = @selector(setFullscreen:forWindow:);
192
193
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
        [inv setTarget:o_vout_controller];
194
195
196
197
198
199
        [inv setSelector:sel];
        [inv setArgument:&i_full atIndex:2];
        [inv setArgument:&p_wnd atIndex:3];
        [inv performSelectorOnMainThread:@selector(invoke) withObject:nil
                           waitUntilDone:NO];
    }
200
    [o_vout_provider_lock unlock];
201

202
    [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
203
    p_wnd->control = WindowControl;
204

205
    [o_pool release];
206
207
208
    return VLC_SUCCESS;
}

209
static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
210
{
211
212
213
214
215
216
217
218
219
220
    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;
    }

221
    switch(i_query) {
222
223
        case VOUT_WINDOW_SET_STATE:
        {
224
            unsigned i_state = va_arg(args, unsigned);
225
226
227
228
229
230

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

            SEL sel = @selector(setWindowLevel:forWindow:);
231
232
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
233
234
235
236
237
238
            [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];

239
            break;
240
241
242
        }
        case VOUT_WINDOW_SET_SIZE:
        {
243

244
245
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
246

247
            NSSize newSize = NSMakeSize(i_width, i_height);
248
            SEL sel = @selector(setNativeVideoSize:forWindow:);
249
250
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
251
252
253
254
255
256
            [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];

257
            break;
258
259
260
261
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
262
            int i_full = va_arg(args, int);
263
264

            SEL sel = @selector(setFullscreen:forWindow:);
265
266
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[o_vout_controller methodSignatureForSelector:sel]];
            [inv setTarget:o_vout_controller];
267
268
269
270
271
272
            [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];

273
            break;
274
275
        }
        default:
276
        {
277
            msg_Warn(p_wnd, "unsupported control query");
278
279
            [o_vout_provider_lock unlock];
            [o_pool release];
280
            return VLC_EGENERIC;
281
        }
282
    }
283
284
285
286

    [o_vout_provider_lock unlock];
    [o_pool release];
    return VLC_SUCCESS;
287
288
}

289
void WindowClose(vout_window_t *p_wnd)
290
{
291
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
292

293
294
295
296
297
298
299
300
301
302
    [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];
303

304
    [o_pool release];
305
306
}

307
308
309
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
310
static NSLock * o_appLock = nil;    // controls access to f_appExit
311
static NSLock * o_plItemChangedLock = nil;
312

313
static void Run(intf_thread_t *p_intf)
314
{
315
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
316
    [VLCApplication sharedApplication];
317

318
    o_appLock = [[NSLock alloc] init];
319
    o_plItemChangedLock = [[NSLock alloc] init];
320
    o_vout_provider_lock = [[NSLock alloc] init];
321

hartman's avatar
-    
hartman committed
322
    [[VLCMain sharedInstance] setIntf: p_intf];
323

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

326
327
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
328
    [o_plItemChangedLock release];
329
    [o_appLock release];
330
331
    [o_vout_provider_lock release];
    o_vout_provider_lock = nil;
332
    [o_pool release];
333
334

    raise(SIGTERM);
335
336
}

337
338
#pragma mark -
#pragma mark Variables Callback
339

340
341
342
/*****************************************************************************
 * 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
343
 * of this file.
344
 *****************************************************************************/
345
static void MsgCallback(void *data, int type, const vlc_log_t *item, const char *format, va_list ap)
346
347
{
    int canc = vlc_savecancel();
348
    char *str;
349

350
351
    if (vasprintf(&str, format, ap) == -1) {
        vlc_restorecancel(canc);
352
        return;
353
    }
354

355
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
356
    [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
357
    [o_pool release];
358

359
360
    vlc_restorecancel(canc);
    free(str);
361
362
}

363
364
static int InputEvent(vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t new_val, void *param)
365
{
366
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
367
368
    switch (new_val.i_int) {
        case INPUT_EVENT_STATE:
369
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackStatusUpdated) withObject: nil waitUntilDone:NO];
370
371
            break;
        case INPUT_EVENT_RATE:
372
            [[[VLCMain sharedInstance] mainMenu] performSelectorOnMainThread:@selector(updatePlaybackRate) withObject: nil waitUntilDone:NO];
373
374
            break;
        case INPUT_EVENT_POSITION:
375
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject: nil waitUntilDone:NO];
376
377
378
            break;
        case INPUT_EVENT_TITLE:
        case INPUT_EVENT_CHAPTER:
379
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
380
381
            break;
        case INPUT_EVENT_CACHE:
382
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
383
384
385
386
387
388
389
390
391
392
393
394
395
396
            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:
397
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
398
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
399
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateInfoandMetaPanel) withObject: nil waitUntilDone:NO];
400
401
402
403
            break;
        case INPUT_EVENT_BOOKMARK:
            break;
        case INPUT_EVENT_RECORD:
404
            [[VLCMain sharedInstance] updateRecordState: var_GetBool(p_this, "record")];
405
406
            break;
        case INPUT_EVENT_PROGRAM:
407
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
408
409
410
411
412
413
414
            break;
        case INPUT_EVENT_ITEM_EPG:
            break;
        case INPUT_EVENT_SIGNAL:
            break;

        case INPUT_EVENT_ITEM_NAME:
415
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
416
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
417
418
419
420
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
421
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
422
423
424
            break;

        case INPUT_EVENT_DEAD:
425
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
426
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
427
428
429
            break;

        case INPUT_EVENT_ABORT:
430
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
431
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
432
433
434
            break;

        default:
435
            //msg_Warn(p_this, "unhandled input event (%lld)", new_val.i_int);
436
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
437
    }
438

439
    [o_pool release];
440
441
442
    return VLC_SUCCESS;
}

443
444
static int PLItemChanged(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
445
{
446
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
447
448
449
450
451
452

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

456
    [o_pool release];
457
458
459
    return VLC_SUCCESS;
}

460
461
static int PlaylistUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
462
{
463
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
464
465
466
467
468
469
470
471
472
473
474

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

476
    [o_pool release];
477
478
479
    return VLC_SUCCESS;
}

480
481
static int PlaybackModeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
482
{
483
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
484
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackModeUpdated) withObject:nil waitUntilDone:NO];
485

486
    [o_pool release];
487
488
489
    return VLC_SUCCESS;
}

490
491
static int VolumeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
492
{
493
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
494
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateVolume) withObject:nil waitUntilDone:NO];
495

496
    [o_pool release];
497
498
499
    return VLC_SUCCESS;
}

500
501
502
503
504
505
506
507
508
509
510
511
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;
}

512
513
514
515
516
/*****************************************************************************
 * 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
 *****************************************************************************/
517
518
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
519
520
{
    intf_thread_t * p_intf = VLCIntf;
521
522
523
524
    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)
525
526
527
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
528
    }
529
530
531
    return VLC_SUCCESS;
}

532
/*****************************************************************************
533
 * DialogCallback: Callback triggered by the "dialog-*" variables
534
 * to let the intf display error and interaction dialogs
535
 *****************************************************************************/
536
static int DialogCallback(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
537
538
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
539
540
    VLCMain *interface = (VLCMain *)data;

541
    if ([@(type) isEqualToString: @"dialog-progress-bar"]) {
542
543
544
545
546
547
548
549
        /* 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;
    }
550

551
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
552
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
553

554
555
556
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
557

558
559
560
561
562
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
563
    if (text != NULL)
564
        o_txt = @(text);
565
566
567
568
569
570
571
572
573
574
575
    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];
576
577
578
579

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

580
581
582
583
584
585
586
587
588
589
    [o_pool release];
}

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

590
591
592
593
594
595
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
596
597
    if (!p_intf)
        return NULL;
598
    return pl_CurrentInput(p_intf);
599
600
601
602
603
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
604
605
    if (!p_input)
        return NULL;
606
607
608
609
610
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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;
}

629
audio_output_t *getAout(void)
630
{
631
632
    intf_thread_t *p_intf = VLCIntf;
    if (!p_intf)
633
        return NULL;
634
    return playlist_GetAout(pl_Get(p_intf));
635
636
}

637
#pragma mark -
638
639
640
#pragma mark Private

@interface VLCMain ()
641
642
643
644
645
646
647
- (void)removeOldPreferences;
@end

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

650
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
651
 * VLCMain implementation
652
653
654
 *****************************************************************************/
@implementation VLCMain

655
@synthesize voutController=o_vout_controller;
656
@synthesize nativeFullscreenMode=b_nativeFullscreenMode;
657
@synthesize playlistUpdatedSelectorInQueue=b_playlist_updated_selector_in_queue;
658

659
660
661
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
662
663
664
665
666
667
668
static VLCMain *_o_sharedMainInstance = nil;

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

Jérome Decoodt's avatar
Jérome Decoodt committed
669
- (id)init
hartman's avatar
hartman committed
670
{
671
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
672
        [self dealloc];
673
        return _o_sharedMainInstance;
674
    } else
hartman's avatar
hartman committed
675
        _o_sharedMainInstance = [super init];
676

677
    p_intf = NULL;
678
    p_current_input = p_input_changed = NULL;
679

680
    o_msg_lock = [[NSLock alloc] init];
681
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
682

683
    o_open = [[VLCOpen alloc] init];
684
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
685
    o_info = [[VLCInfo alloc] init];
686
    o_mainmenu = [[VLCMainMenu alloc] init];
687
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
688
    o_eyetv = [[VLCEyeTVController alloc] init];
689
    o_mainwindow = [[VLCMainWindow alloc] init];
690
691
692
693
694
695

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
696
697
698
699
700

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

701
702
    o_vout_controller = [[VLCVoutWindowController alloc] init];

hartman's avatar
hartman committed
703
704
705
    return _o_sharedMainInstance;
}

706
707
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
708
709
710
    p_intf = p_mainintf;
}

711
712
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
713
714
715
    return p_intf;
}

716
717
- (void)awakeFromNib
{
718
    playlist_t *p_playlist;
719
    vlc_value_t val;
720
721
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
722

723
724
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
725
726
    if (nib_main_loaded)
        return;
727

728
729
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
730

731
    p_playlist = pl_Get(p_intf);
hartman's avatar
hartman committed
732

733
    val.b_bool = false;
bigben's avatar
bigben committed
734

735
736
    var_AddCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
    var_AddCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
737
    var_AddCallback(p_intf->p_libvlc, "intf-boss", BossCallback, self);
738
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
739
740
741
742
743
744
745
746
747
    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
748

749
    if (!OSX_SNOW_LEOPARD) {
750
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
751
            var_SetBool(p_playlist, "fullscreen", YES);
752
    }
753

754
    /* load our Core and Shared Dialogs nibs */
755
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
756
    [NSBundle loadNibNamed:@"SharedDialogs" owner: NSApp];
757

758
    /* subscribe to various interactive dialogues */
759
760
761
762
763
764
765
766
767
768
769
    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);
770

771
772
773
774
775
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

776
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
777

778
    /* yeah, we are done */
779
780
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
781
    if (!OSX_SNOW_LEOPARD)
782
        b_nativeFullscreenMode = var_InheritBool(p_intf, "macosx-nativefullscreenmode");
783
#endif
784

785
    if (config_GetInt(VLCIntf, "macosx-icon-change")) {
786
787
788
789
790
791
792
793
794
795
796
797
798
        /* 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"]];
    }

799
800
    [self initStrings];

801
    nib_main_loaded = TRUE;
802
803
}

804
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
805
{
806
807
    if (!p_intf)
        return;
808

809
810
    [self updateCurrentlyUsedHotkeys];

811
    /* init media key support */
812
813
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
814
815
816
817
818
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
819
820
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

821
    [self removeOldPreferences];
822

823
824
825
826
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

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

829
830
    /* 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];
831
832
833
834
835

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
836
837
}

hartman's avatar
hartman committed
838
- (void)initStrings
839
{
840
841
    if (!p_intf)
        return;
842

Jérome Decoodt's avatar
Jérome Decoodt committed
843
    /* messages panel */
844
    [o_msgs_panel setTitle: _NS("Messages")];
845
846
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
847

848
849
850
851
852
853
    /* 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, ...")];
854
855
    [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.")];
856
    [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
857
858
}

859
860
861
862
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
863
{
864
865
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
866
867
    static bool f_appExit = false;
    bool isTerminating;
868
869

    [o_appLock lock];
870
871
    isTerminating = f_appExit;
    f_appExit = true;
872
873
874
875
    [o_appLock unlock];

    if (isTerminating)
        return;
876

877
878
    [self resumeItunesPlayback:nil];

879
880
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
881

882
    playlist_t * p_playlist = pl_Get(p_intf);
883
884
    int returnedValue = 0;