intf.m 72.5 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_aout_intf.h>
43
#include <vlc_vout_window.h>
44
#include <unistd.h> /* execl() */
45

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

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

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

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

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

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

89
#pragma mark -
90
#pragma mark VLC Interface Object Callbacks
91

92 93 94
/*****************************************************************************
 * OpenIntf: initialize interface
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
int OpenIntf ( vlc_object_t *p_this )
Jérome Decoodt's avatar
Jérome Decoodt committed
96
{
97 98
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
    [VLCApplication sharedApplication];
99 100 101 102
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

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

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

107
    /* subscribe to LibVLCCore's messages */
108
    p_intf->p_sys->p_sub = vlc_Subscribe( MsgCallback, NULL );
109
    p_intf->pf_run = Run;
110
    p_intf->b_should_run_on_first_thread = true;
111

112
    [o_pool release];
Jean-Paul Saman's avatar
Jean-Paul Saman committed
113
    return VLC_SUCCESS;
114 115 116 117 118
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
119
void CloseIntf ( vlc_object_t *p_this )
120 121
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;
Jérome Decoodt's avatar
Jérome Decoodt committed
122

123 124 125
    free( p_intf->p_sys );
}

126 127 128 129
static int WindowControl( vout_window_t *, int i_query, va_list );

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

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

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

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

static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
{
    /* TODO */
    if( i_query == VOUT_WINDOW_SET_STATE )
161
        msg_Dbg( p_wnd, "WindowControl:VOUT_WINDOW_SET_STATE" );
162
    else if( i_query == VOUT_WINDOW_SET_SIZE )
163 164 165 166 167
    {
        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 )];
    }
168
    else if( i_query == VOUT_WINDOW_SET_FULLSCREEN )
169 170 171 172 173
    {
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
        [[VLCMain sharedInstance] fullscreenChanged];
        [o_pool release];
    }
174
    else
175
        msg_Dbg( p_wnd, "WindowControl: unknown query" );
176 177 178 179 180
    return VLC_SUCCESS;
}

void WindowClose( vout_window_t *p_wnd )
{
181
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
182
    [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
183

184
    [o_pool release];
185 186
}

187 188 189
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
190
static NSLock * o_appLock = nil;    // controls access to f_appExit
191

192 193
static void Run( intf_thread_t *p_intf )
{
194
    NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
195
    [VLCApplication sharedApplication];
196

197 198
    o_appLock = [[NSLock alloc] init];

hartman's avatar
-  
hartman committed
199
    [[VLCMain sharedInstance] setIntf: p_intf];
hartman's avatar
hartman committed
200
    [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
201

202 203
    [NSApp run];
    [[VLCMain sharedInstance] applicationWillTerminate:nil];
204
    [o_appLock release];
205
    [o_pool release];
206 207
}

208 209
#pragma mark -
#pragma mark Variables Callback
210

211 212 213
/*****************************************************************************
 * 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
214
 * of this file.
215
 *****************************************************************************/
216
static void MsgCallback( void *data, int type, const msg_item_t *item, const char *format, va_list ap )
217 218
{
    int canc = vlc_savecancel();
219
    char *str;
220

221
    if (vasprintf( &str, format, ap ) == -1)
222 223
    {
        vlc_restorecancel( canc );
224
        return;
225
    }
226

227
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
228
    [[VLCMain sharedInstance] processReceivedlibvlcMessage: item ofType: type withStr: str];
229
    [o_pool release];
230

231
    vlc_restorecancel( canc );
232
    free( str );
233 234
}

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

        case INPUT_EVENT_ITEM_NAME:
287
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
288
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(playlistUpdated) withObject: nil waitUntilDone:NO];
289 290 291 292
            break;

        case INPUT_EVENT_AUDIO_DELAY:
        case INPUT_EVENT_SUBTITLE_DELAY:
293
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateDelays) withObject:nil waitUntilDone:NO];
294 295 296
            break;

        case INPUT_EVENT_DEAD:
297
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
298
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
299 300 301
            break;

        case INPUT_EVENT_ABORT:
302
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updateName) withObject: nil waitUntilDone:NO];
303
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(updatePlaybackPosition) withObject:nil waitUntilDone:NO];
304 305 306 307 308
            break;

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

311
    [o_pool release];
312 313 314 315 316 317
    return VLC_SUCCESS;
}

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

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

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

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

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

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

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

351
    [o_pool release];
352 353 354 355 356 357 358 359 360 361 362 363
    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
 *****************************************************************************/
static int ShowController( vlc_object_t *p_this, const char *psz_variable,
                     vlc_value_t old_val, vlc_value_t new_val, void *param )
{
    intf_thread_t * p_intf = VLCIntf;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
364
    if( p_intf && p_intf->p_sys )
365
    {
366 367 368 369 370 371 372 373 374 375
        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 )
        {
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showFullscreenController) withObject:nil waitUntilDone:NO];
        }
        else
        {
            [[VLCMain sharedInstance] performSelectorOnMainThread:@selector(showMainWindow) withObject:nil waitUntilDone:NO];
        }
376
    }
377 378 379
    return VLC_SUCCESS;
}

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

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

406 407 408 409 410 411 412 413 414 415
    if( [[NSString stringWithUTF8String: type] isEqualToString: @"dialog-progress-bar"] )
    {
        /* 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;
    }
416

417
    NSValue *o_value = [NSValue valueWithPointer:value.p_address];
418
    [[VLCCoreDialogProvider sharedInstance] performEventWithObject: o_value ofType: type];
419

420 421 422
    [o_pool release];
    return VLC_SUCCESS;
}
bigben's avatar
bigben committed
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
void updateProgressPanel (void *priv, const char *text, float value)
{
    NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

    NSString *o_txt;
    if( text != NULL )
        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];
442
    [[[VLCMain sharedInstance] coreDialogProvider] performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:NO];
443 444 445 446 447 448 449 450 451 452
    [o_pool release];
}

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

453 454 455 456 457 458
#pragma mark -
#pragma mark Helpers

input_thread_t *getInput(void)
{
    intf_thread_t *p_intf = VLCIntf;
459 460
    if (!p_intf)
        return NULL;
461
    return pl_CurrentInput(p_intf);
462 463 464 465 466
}

vout_thread_t *getVout(void)
{
    input_thread_t *p_input = getInput();
467 468
    if (!p_input)
        return NULL;
469 470 471 472 473
    vout_thread_t *p_vout = input_GetVout(p_input);
    vlc_object_release(p_input);
    return p_vout;
}

474
audio_output_t *getAout(void)
475 476
{
    input_thread_t *p_input = getInput();
477 478
    if (!p_input)
        return NULL;
479
    audio_output_t *p_aout = input_GetAout(p_input);
480 481 482 483
    vlc_object_release(p_input);
    return p_aout;
}

484
#pragma mark -
485 486 487 488 489
#pragma mark Private

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

491
/*****************************************************************************
Jérome Decoodt's avatar
Jérome Decoodt committed
492
 * VLCMain implementation
493 494 495
 *****************************************************************************/
@implementation VLCMain

496 497 498
#pragma mark -
#pragma mark Initialization

hartman's avatar
hartman committed
499 500 501 502 503 504 505
static VLCMain *_o_sharedMainInstance = nil;

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

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

516
    p_intf = NULL;
517
    p_current_input = NULL;
518

519
    o_msg_lock = [[NSLock alloc] init];
520
    o_msg_arr = [[NSMutableArray arrayWithCapacity: 600] retain];
521

522
    o_open = [[VLCOpen alloc] init];
523
    //o_embedded_list = [[VLCEmbeddedList alloc] init];
524
    o_coredialogs = [[VLCCoreDialogProvider alloc] init];
525
    o_info = [[VLCInfo alloc] init];
526
    o_mainmenu = [[VLCMainMenu alloc] init];
527
    o_coreinteraction = [[VLCCoreInteraction alloc] init];
528
    o_eyetv = [[VLCEyeTVController alloc] init];
529
    o_mainwindow = [[VLCMainWindow alloc] init];
530 531 532 533 534 535

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

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

hartman's avatar
hartman committed
541 542 543 544 545 546 547
    return _o_sharedMainInstance;
}

- (void)setIntf: (intf_thread_t *)p_mainintf {
    p_intf = p_mainintf;
}

548
- (intf_thread_t *)intf {
hartman's avatar
hartman committed
549 550 551
    return p_intf;
}

552 553
- (void)awakeFromNib
{
554
    playlist_t *p_playlist;
555
    vlc_value_t val;
556
    if( !p_intf ) return;
557
    var_Create( p_intf, "intf-change", VLC_VAR_BOOL );
558

559 560 561 562
    /* Check if we already did this once. Opening the other nibs calls it too,
     because VLCMain is the owner */
    if( nib_main_loaded ) return;

563 564
    [o_msgs_panel setExcludedFromWindowsMenu: YES];
    [o_msgs_panel setDelegate: self];
565

566
    p_playlist = pl_Get( p_intf );
hartman's avatar
hartman committed
567

568
    val.b_bool = false;
bigben's avatar
bigben committed
569

570
    var_AddCallback(p_playlist, "fullscreen", FullscreenChanged, self);
571
    var_AddCallback( p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
572 573
    var_AddCallback( p_intf->p_libvlc, "intf-show", ShowController, self);
    //    var_AddCallback(p_playlist, "item-change", PLItemChanged, self);
574 575 576 577 578 579 580 581 582 583
    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
584

585 586
    if (OSX_LION)
    {
587
        if ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen)
588 589
            var_SetBool( p_playlist, "fullscreen", YES );
    }
590

591 592
    /* load our Core Dialogs nib */
    nib_coredialogs_loaded = [NSBundle loadNibNamed:@"CoreDialogs" owner: NSApp];
593

594
    /* subscribe to various interactive dialogues */
595 596 597 598
    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 );
599 600 601 602 603 604
    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 );
605
    dialog_Register( p_intf );
606

607 608 609 610 611
    /* init Apple Remote support */
    o_remote = [[AppleRemote alloc] init];
    [o_remote setClickCountEnabledButtons: kRemoteButtonPlay];
    [o_remote setDelegate: _o_sharedMainInstance];

612
    [o_msgs_refresh_btn setImage: [NSImage imageNamed: NSImageNameRefreshTemplate]];
613

614
    /* yeah, we are done */
615 616
    b_nativeFullscreenMode = NO;
#ifdef MAC_OS_X_VERSION_10_7
617 618
    if( config_GetInt( VLCIntf, "embedded-video" ))
        b_nativeFullscreenMode = config_GetInt( p_intf, "macosx-nativefullscreenmode" );
619
#endif
620
    nib_main_loaded = TRUE;
621 622
}

623
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
624
{
625 626
    if( !p_intf ) return;

627 628 629 630 631
    [o_mainwindow updateWindow];
    [o_mainwindow updateTimeSlider];
    [o_mainwindow updateVolumeSlider];
    [o_mainwindow makeKeyAndOrderFront: self];

632 633
    /* init media key support */
    b_mediaKeySupport = config_GetInt( VLCIntf, "macosx-mediakeys" );
634 635 636 637 638 639 640 641
    if( b_mediaKeySupport )
    {
        o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
        [o_mediaKeyController startWatchingMediaKeys];
        [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                 nil]];
    }
642 643
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name: @"VLCMediaKeySupportSettingChanged" object: nil];

644 645
    [self _removeOldPreferences];

646 647 648 649
    /* Handle sleep notification */
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:)
           name:NSWorkspaceWillSleepNotification object:nil];

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

652 653
    /* 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];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
654
    [self initStrings];
655 656
}

hartman's avatar
hartman committed
657
- (void)initStrings
658
{
659 660
    if( !p_intf ) return;

Jérome Decoodt's avatar
Jérome Decoodt committed
661
    /* messages panel */
662
    [o_msgs_panel setTitle: _NS("Messages")];
663 664
    [o_msgs_crashlog_btn setTitle: _NS("Open CrashLog...")];
    [o_msgs_save_btn setTitle: _NS("Save this Log...")];
665

666 667 668 669 670 671
    /* 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, ...")];
672 673
    [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.")];
674 675
}

676 677 678 679
#pragma mark -
#pragma mark Termination

- (void)applicationWillTerminate:(NSNotification *)notification
680
{
681 682
    /* don't allow a double termination call. If the user has
     * already invoked the quit then simply return this time. */
683 684
    static bool f_appExit = false;
    bool isTerminating;
685 686

    [o_appLock lock];
687 688
    isTerminating = f_appExit;
    f_appExit = true;
689 690 691 692
    [o_appLock unlock];

    if (isTerminating)
        return;
693

694 695
    if (notification == nil)
        [[NSNotificationCenter defaultCenter] postNotificationName: NSApplicationWillTerminateNotification object: nil];
696

697
    playlist_t * p_playlist = pl_Get( p_intf );
698 699
    int returnedValue = 0;

700 701 702 703 704 705 706
    /* always exit fullscreen on quit, otherwise we get ugly artifacts on the next launch */
    if (OSX_LION && b_nativeFullscreenMode)
    {
        [o_mainwindow toggleFullScreen: self];
        [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
    }

707 708 709 710 711 712
    /* Save some interface state in configuration, at module quit */
    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" ) );

    msg_Dbg( p_intf, "Terminating" );
713

714 715
    /* unsubscribe from the interactive dialogues */
    dialog_Unregister( p_intf );
716 717
    var_DelCallback( p_intf, "dialog-error", DialogCallback, self );
    var_DelCallback( p_intf, "dialog-critical", DialogCallback, self );
718 719 720
    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 );
721 722 723 724 725 726 727 728 729 730 731
    //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);
732 733
    var_DelCallback(p_playlist, "fullscreen", FullscreenChanged, self);
    var_DelCallback(p_intf->p_libvlc, "intf-toggle-fscontrol", ShowController, self);
734
    var_DelCallback(p_intf->p_libvlc, "intf-show", ShowController, self);
735

736
    if( p_current_input )
737
    {
738 739 740
        var_DelCallback( p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance] );
        vlc_object_release( p_current_input );
        p_current_input = NULL;
741 742
    }

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

746 747
    /* release some other objects here, because it isn't sure whether dealloc
     * will be called later on */
748
    if( o_sprefs )
749 750
        [o_sprefs release];

751 752
    if( o_prefs )
        [o_prefs release];
753

754
    [o_open release];
755 756 757

    if( o_info )
        [o_info release];
hartman's avatar
hartman committed
758

759
    if( o_wizard )
760 761 762 763
        [o_wizard release];

    [crashLogURLConnection cancel];
    [crashLogURLConnection release];
764

765
    [o_embedded_list release];
766
    [o_coredialogs release];
767
    [o_eyetv release];
768
    [o_mainwindow release];
769

770
    /* unsubscribe from libvlc's debug messages */
771
    vlc_Unsubscribe( p_intf->p_sys->p_sub );
772

773 774 775 776 777 778 779 780
    [o_msg_arr removeAllObjects];
    [o_msg_arr release];

    [o_msg_lock release];

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

hartman's avatar
hartman committed
781
    /* Make sure the Menu doesn't have any references to vlc objects anymore */
782 783
    //FIXME: this should be moved to VLCMainMenu
    [o_mainmenu releaseRepresentedObjects:[NSApp mainMenu]];
784
    [o_mainmenu release];
hartman's avatar
hartman committed
785

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
786
    libvlc_Quit( p_intf->p_libvlc );
787

788
    [self setIntf:nil];
hartman's avatar
hartman committed
789 790
}

791 792 793 794 795
#pragma mark -
#pragma mark Sparkle delegate
/* received directly before the update gets installed, so let's shut down a bit */
- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
{
796
    [NSApp activateIgnoringOtherApps:YES];
797
    [o_remote stopListening: self];
798
    [[VLCCoreInteraction sharedInstance] stop];
799 800
}

801 802 803 804 805