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

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

34 35 36
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/param.h>                                    /* for MAXPATHLEN */
#include <string.h>
hartman's avatar
hartman committed
37
#include <vlc_common.h>
38
#include <vlc_keys.h>
39
#include <vlc_dialog.h>
40
#include <vlc_url.h>
41
#include <vlc_modules.h>
42
#include <vlc_plugin.h>
43
#include <vlc_aout_intf.h>
44
#include <vlc_vout_display.h>
45
#include <unistd.h> /* execl() */
46

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

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

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

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

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

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

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

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

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

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

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

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

113
    Run(p_intf);
114

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

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

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

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

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

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

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

152
    [[VLCMain sharedInstance] setNativeVideoSize:NSMakeSize(cfg->width, cfg->height)];
153
    [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
154 155
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t *)VLCIntf;
156
    [o_pool release];
157 158 159
    return VLC_SUCCESS;
}

160
static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
161
{
162
    switch(i_query) {
163 164
        case VOUT_WINDOW_SET_STATE:
        {
165
            unsigned i_state = va_arg(args, unsigned);
166 167 168 169 170
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(setWindowLevel:) withObject:[NSNumber numberWithUnsignedInt:i_state] waitUntilDone:NO];
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_SIZE:
        {
171 172 173
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
            [[VLCMain sharedInstance] setNativeVideoSize:NSMakeSize(i_width, i_height)];
174 175 176 177 178
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
            NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
179
            int i_full = va_arg(args, int);
180 181 182 183 184
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(checkFullscreenChange:) withObject:[NSNumber numberWithInt: i_full] waitUntilDone:NO];
            [o_pool release];
            return VLC_SUCCESS;
        }
        default:
185
            msg_Warn(p_wnd, "unsupported control query");
186
            return VLC_EGENERIC;
187
    }
188 189
}

190
void WindowClose(vout_window_t *p_wnd)
191
{
192
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
193
    [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
194

195
    [o_pool release];
196 197
}

198 199 200
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
201
static NSLock * o_appLock = nil;    // controls access to f_appExit
202

203
static void Run(intf_thread_t *p_intf)
204
{
205
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
206
    [VLCApplication sharedApplication];
207

208 209
    o_appLock = [[NSLock alloc] init];

hartman's avatar
-  
hartman committed
210
    [[VLCMain sharedInstance] setIntf: p_intf];
hartman's avatar
hartman committed
211
    [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
212

213 214
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
215
    [o_appLock release];
216
    [o_pool release];
217 218

    raise(SIGTERM);
219 220
}

221 222
#pragma mark -
#pragma mark Variables Callback
223

224 225 226
/*****************************************************************************
 * 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
227
 * of this file.
228
 *****************************************************************************/
229
static void MsgCallback(void *data, int type, const msg_item_t *item, const char *format, va_list ap)
230 231
{
    int canc = vlc_savecancel();
232
    char *str;
233

234 235
    if (vasprintf(&str, format, ap) == -1) {
        vlc_restorecancel(canc);
236
        return;
237
    }
238

239
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
240
    [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
241
    [o_pool release];
242

243 244
    vlc_restorecancel(canc);
    free(str);
245 246
}

247 248
static int InputEvent(vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t new_val, void *param)
249
{
250
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
251 252
    switch (new_val.i_int) {
        case INPUT_EVENT_STATE:
253
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackStatusUpdated) withObject: nil waitUntilDone:NO];
254 255
            break;
        case INPUT_EVENT_RATE:
256
            [[[VLCMain sharedInstance] mainMenu] performSelectorOnMainThread:@selector(updatePlaybackRate) withObject: nil waitUntilDone:NO];
257 258
            break;
        case INPUT_EVENT_POSITION:
259
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject: nil waitUntilDone:NO];
260 261 262
            break;
        case INPUT_EVENT_TITLE:
        case INPUT_EVENT_CHAPTER:
263
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
264 265
            break;
        case INPUT_EVENT_CACHE:
266
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainWindow) withObject: nil waitUntilDone: NO];
267 268 269 270 271 272 273 274 275 276 277 278 279 280
            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:
281
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
282
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
283
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateInfoandMetaPanel) withObject: nil waitUntilDone:NO];
284 285 286 287
            break;
        case INPUT_EVENT_BOOKMARK:
            break;
        case INPUT_EVENT_RECORD:
288
            [[VLCMain sharedInstance] updateRecordState: var_GetBool(p_this, "record")];
289 290
            break;
        case INPUT_EVENT_PROGRAM:
291
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateMainMenu) withObject: nil waitUntilDone:NO];
292 293 294 295 296 297 298
            break;
        case INPUT_EVENT_ITEM_EPG:
            break;
        case INPUT_EVENT_SIGNAL:
            break;

        case INPUT_EVENT_ITEM_NAME:
299
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
300
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
301 302 303 304
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
305
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
306 307 308
            break;

        case INPUT_EVENT_DEAD:
309
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
310
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
311 312 313
            break;

        case INPUT_EVENT_ABORT:
314
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
315
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
316 317 318
            break;

        default:
319
            //msg_Warn(p_this, "unhandled input event (%lld)", new_val.i_int);
320
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
321
    }
322

323
    [o_pool release];
324 325 326
    return VLC_SUCCESS;
}

327 328
static int PLItemChanged(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
329
{
330
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
331 332
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(PlaylistItemChanged) withObject:nil waitUntilDone:NO];

333
    [o_pool release];
334 335 336
    return VLC_SUCCESS;
}

337 338
static int PlaylistUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
339
{
340
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
341 342
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject:nil waitUntilDone:NO];

343
    [o_pool release];
344 345 346
    return VLC_SUCCESS;
}

347 348
static int PlaybackModeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
349
{
350
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
351
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playbackModeUpdated) withObject:nil waitUntilDone:NO];
352

353
    [o_pool release];
354 355 356
    return VLC_SUCCESS;
}

357 358
static int VolumeUpdated(vlc_object_t *p_this, const char *psz_var,
                         vlc_value_t oldval, vlc_value_t new_val, void *param)
359
{
360
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
361
    [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateVolume) withObject:nil waitUntilDone:NO];
362

363
    [o_pool release];
364 365 366 367 368 369 370 371
    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
 *****************************************************************************/
372 373
static int ShowController(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
374 375
{
    intf_thread_t * p_intf = VLCIntf;
376 377 378 379
    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)
380 381 382
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        else
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
383
    }
384 385 386
    return VLC_SUCCESS;
}

bigben's avatar
bigben committed
387 388 389 390
/*****************************************************************************
 * FullscreenChanged: Callback triggered by the fullscreen-change playlist
 * variable, to let the intf update the controller.
 *****************************************************************************/
391 392
static int FullscreenChanged(vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param)
bigben's avatar
bigben committed
393 394
{
    intf_thread_t * p_intf = VLCIntf;
395
    if (p_intf) {
396
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
397
        [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(fullscreenChanged) withObject:nil waitUntilDone:NO];
398 399
        [o_pool release];
    }
bigben's avatar
bigben committed
400 401 402
    return VLC_SUCCESS;
}

403
/*****************************************************************************
404
 * DialogCallback: Callback triggered by the "dialog-*" variables
405
 * to let the intf display error and interaction dialogs
406
 *****************************************************************************/
407
static int DialogCallback(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
408 409
{
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
410 411
    VLCMain *interface = (VLCMain *)data;

412
    if ([[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"]) {
413 414 415 416 417 418 419 420
        /* 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;
    }
421

422
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
423
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
424

425 426 427
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
428

429 430 431 432 433
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
434
    if (text != NULL)
435 436 437 438 439 440 441 442 443 444 445 446
        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];
447
    [[[VLCMain sharedInstance] coreDialogProvider] destroyProgressPanel];
448 449 450 451 452 453 454 455 456 457
    [o_pool release];
}

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

458 459 460 461 462 463
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
464 465
    if (!p_intf)
        return NULL;
466
    return pl_CurrentInput(p_intf);
467 468 469 470 471
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
472 473
    if (!p_input)
        return NULL;
474 475 476 477 478
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

479
audio_output_t *getAout(void)
480 481
{
    input_thread_t *p_input = getInput();
482 483
    if (!p_input)
        return NULL;
484
    audio_output_t *p_aout = input_GetAout(p_input);
485 486 487 488
    vlc_object_release(p_input);
    return p_aout;
}

489
#pragma mark -
490 491 492 493 494
#pragma mark Private

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

496
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
497
 * VLCMain implementation
498 499 500
 *****************************************************************************/
@implementation VLCMain

501 502 503
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
504 505 506 507 508 509 510
static VLCMain *_o_sharedMainInstance = nil;

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

Jérome Decoodt's avatar
Jérome Decoodt committed
511
- (id)init
hartman's avatar
hartman committed
512
{
513
    if (_o_sharedMainInstance) {
hartman's avatar
hartman committed
514
        [self dealloc];
515
        return _o_sharedMainInstance;
516
    } else
hartman's avatar
hartman committed
517
        _o_sharedMainInstance = [super init];
518

519
    p_intf = NULL;
520
    p_current_input = NULL;
521

522
    o_msg_lock = [[NSLock alloc] init];
523
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
524

525
    o_open = [[VLCOpen alloc] init];
526
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
527
    o_info = [[VLCInfo alloc] init];
528
    o_mainmenu = [[VLCMainMenu alloc] init];
529
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
530
    o_eyetv = [[VLCEyeTVController alloc] init];
531
    o_mainwindow = [[VLCMainWindow alloc] init];
532 533 534 535 536 537

    /* announce our launch to a potential eyetv plugin */
    [[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"VLCOSXGUIInit"
                                                                   object: @"VLCEyeTVSupport"
                                                                 userInfo: NULL
                                                       deliverImmediately: YES];
538 539 540 541 542

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

hartman's avatar
hartman committed
543 544 545
    return _o_sharedMainInstance;
}

546 547
- (void)setIntf: (intf_thread_t *)p_mainintf
{
hartman's avatar
hartman committed
548 549 550
    p_intf = p_mainintf;
}

551 552
- (intf_thread_t *)intf
{
hartman's avatar
hartman committed
553 554 555
    return p_intf;
}

556 557
- (void)awakeFromNib
{
558
    playlist_t *p_playlist;
559
    vlc_value_t val;
560 561
    if (!p_intf) return;
    var_Create(p_intf, "intf-change", VLC_VAR_BOOL);
562

563 564
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
565 566
    if (nib_main_loaded)
        return;
567

568 569
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
570

571
    p_playlist = pl_Get(p_intf);
hartman's avatar
hartman committed
572

573
    val.b_bool = false;
bigben's avatar
bigben committed
574

575
    var_AddCallback(p_playlist, "fullscreen", FullscreenChanged, self);
576 577
    var_AddCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
    var_AddCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
578
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
579 580 581 582 583 584 585 586 587 588
    var_AddCallback(p_playlist, "item-current", PLItemChanged, self);
    var_AddCallback(p_playlist, "activity", PLItemChanged, self);
    var_AddCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
    var_AddCallback(p_playlist, "random", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "loop", PlaybackModeUpdated, self);
    var_AddCallback(p_playlist, "volume", VolumeUpdated, self);
    var_AddCallback(p_playlist, "mute", VolumeUpdated, self);
bigben's avatar
bigben committed
589

590
    if (!OSX_SNOW_LEOPARD) {
591
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
592
            var_SetBool(p_playlist, "fullscreen", YES);
593
    }
594

595
    /* load our Core and Shared Dialogs nibs */
596
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
597
    [NSBundle loadNibNamed:@"SharedDialogs" owner: NSApp];
598

599
    /* subscribe to various interactive dialogues */
600 601 602 603 604 605 606 607 608 609 610
    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);
611

612 613 614 615 616
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

617
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
618

619
    /* yeah, we are done */
620 621
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
622
    if (!OSX_SNOW_LEOPARD)
623
        b_nativeFullscreenMode = var_InheritBool(p_intf, "macosx-nativefullscreenmode");
624
#endif
625 626 627

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

632
    if (config_GetInt(VLCIntf, "macosx-icon-change")) {
633 634 635 636 637 638 639 640 641 642 643 644 645
        /* 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"]];
    }

646 647
    [self initStrings];

648
    nib_main_loaded = TRUE;
649 650
}

651
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
652
{
653 654
    if (!p_intf)
        return;
655

656 657
    [self updateCurrentlyUsedHotkeys];

658
    /* init media key support */
659 660
    b_mediaKeySupport = var_InheritBool(VLCIntf, "macosx-mediakeys");
    if (b_mediaKeySupport) {
661 662 663 664 665 666
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [o_mediaKeyController startWatchingMediaKeys];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
667 668
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

669 670
    [self _removeOldPreferences];

671 672 673 674
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

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

677 678
    /* 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];
679 680 681 682 683

    /* update the main window */
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
684 685
}

hartman's avatar
hartman committed
686
- (void)initStrings
687
{
688 689
    if (!p_intf)
        return;
690

Jérome Decoodt's avatar
Jérome Decoodt committed
691
    /* messages panel */
692
    [o_msgs_panel setTitle: _NS("Messages")];
693 694
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
695

696 697 698 699 700 701
    /* 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, ...")];
702 703
    [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.")];
704
    [o_crashrep_dontaskagain_ckb setTitle: _NS("Don't ask again")];
705 706
}

707 708 709 710
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
711
{
712 713
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
714 715
    static bool f_appExit = false;
    bool isTerminating;
716 717

    [o_appLock lock];
718 719
    isTerminating = f_appExit;
    f_appExit = true;
720 721 722 723
    [o_appLock unlock];

    if (isTerminating)
        return;
724

725 726
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
727

728
    playlist_t * p_playlist = pl_Get(p_intf);
729 730
    int returnedValue = 0;

731
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
732
    if (b_nativeFullscreenMode) {
733 734 735 736
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

737
    /* Save some interface state in configuration, at module quit */
738 739 740
    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"));
741

742
    msg_Dbg(p_intf, "Terminating");
743

744
    /* unsubscribe from the interactive dialogues */
745 746 747 748 749 750
    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);
751 752 753 754 755 756 757 758 759 760 761
    //var_DelCallback(p_playlist, "item-change", PLItemChanged, self);
    var_DelCallback(p_playlist, "item-current", PLItemChanged, self);
    var_DelCallback(p_playlist, "activity", PLItemChanged, self);
    var_DelCallback(p_playlist, "leaf-to-parent", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "playlist-item-append", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "playlist-item-deleted", PlaylistUpdated, self);
    var_DelCallback(p_playlist, "random", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "repeat", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "loop", PlaybackModeUpdated, self);
    var_DelCallback(p_playlist, "volume", VolumeUpdated, self);
    var_DelCallback(p_playlist, "mute", VolumeUpdated, self);