VLCCoreInteraction.m 37.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * CoreInteraction.m: MacOS X interface module
 *****************************************************************************
4
 * Copyright (C) 2011-2015 Felix Paul Kühne
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
 *
 * 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.
 *
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

24
#import "VLCCoreInteraction.h"
25
#import "VLCMain.h"
26
#import "VLCOpenWindowController.h"
27
#import "VLCPlaylist.h"
28
#import <math.h>
29 30 31
#import <vlc_playlist.h>
#import <vlc_input.h>
#import <vlc_keys.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
32 33
#import <vlc_vout.h>
#import <vlc_vout_osd.h>
34 35
#import <vlc/vlc.h>
#import <vlc_strings.h>
36
#import <vlc_url.h>
37 38
#import <vlc_modules.h>
#import <vlc_charset.h>
39 40 41
#include <vlc_plugin.h>
#import "SPMediaKeyTap.h"
#import "AppleRemote.h"
42
#import "VLCInputManager.h"
43 44 45 46 47 48 49 50 51 52 53 54 55

static int BossCallback(vlc_object_t *p_this, const char *psz_var,
                        vlc_value_t oldval, vlc_value_t new_val, void *param)
{
    @autoreleasepool {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[VLCCoreInteraction sharedInstance] pause];
            [[NSApplication sharedApplication] hide:nil];
        });

        return VLC_SUCCESS;
    }
}
56

57 58 59 60 61 62
@interface VLCCoreInteraction ()
{
    int i_currentPlaybackRate;
    mtime_t timeA, timeB;

    float f_maxVolume;
63 64 65 66 67 68 69 70 71 72 73

    /* media key support */
    BOOL b_mediaKeySupport;
    BOOL b_mediakeyJustJumped;
    SPMediaKeyTap *_mediaKeyController;
    BOOL b_mediaKeyTrapEnabled;

    AppleRemote *_remote;
    BOOL b_remote_button_hold; /* true as long as the user holds the left,right,plus or minus on the remote control */

    NSArray *_usedHotkeys;
74 75
}
@end
76

77 78
@implementation VLCCoreInteraction

79
#pragma mark - Initialization
80

81
+ (VLCCoreInteraction *)sharedInstance
82
{
83 84 85 86 87 88
    static VLCCoreInteraction *sharedInstance = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [VLCCoreInteraction new];
    });
89

90
    return sharedInstance;
91 92
}

93 94 95 96
- (instancetype)init
{
    self = [super init];
    if (self) {
97
        intf_thread_t *p_intf = getIntf();
98 99 100 101 102 103 104 105 106

        /* init media key support */
        b_mediaKeySupport = var_InheritBool(p_intf, "macosx-mediakeys");
        if (b_mediaKeySupport) {
            _mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
            [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey,
                                                                     nil]];
        }
107
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(coreChangedMediaKeySupportSetting:) name:VLCMediaKeySupportSettingChangedNotification object: nil];
108 109 110 111 112 113

        /* init Apple Remote support */
        _remote = [[AppleRemote alloc] init];
        [_remote setClickCountEnabledButtons: kRemoteButtonPlay];
        [_remote setDelegate: self];

114
        var_AddCallback(p_intf->obj.libvlc, "intf-boss", BossCallback, (__bridge void *)self);
115 116 117 118
    }
    return self;
}

119 120
- (void)dealloc
{
121
    intf_thread_t *p_intf = getIntf();
122
    var_DelCallback(p_intf->obj.libvlc, "intf-boss", BossCallback, (__bridge void *)self);
123 124 125 126
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}


127
#pragma mark - Playback Controls
128

129
- (void)playOrPause
130
{
131 132
    input_thread_t *p_input = pl_CurrentInput(getIntf());
    playlist_t *p_playlist = pl_Get(getIntf());
133

134
    if (p_input) {
135
        playlist_TogglePause(p_playlist);
136
        vlc_object_release(p_input);
137

138
    } else {
139 140
        PLRootType root = [[[[VLCMain sharedInstance] playlist] model] currentRootType];
        if ([[[VLCMain sharedInstance] playlist] isSelectionEmpty] && (root == ROOT_TYPE_PLAYLIST || root == ROOT_TYPE_MEDIALIBRARY))
141 142 143 144
            [[[VLCMain sharedInstance] open] openFileGeneric];
        else
            [[[VLCMain sharedInstance] playlist] playItem:nil];
    }
145 146
}

147 148
- (void)pause
{
149
    playlist_t *p_playlist = pl_Get(getIntf());
150

151
    playlist_Pause(p_playlist);
152 153
}

154 155
- (void)stop
{
156
    playlist_Stop(pl_Get(getIntf()));
157 158 159 160
}

- (void)faster
{
161
    var_TriggerCallback(pl_Get(getIntf()), "rate-faster");
162 163 164 165
}

- (void)slower
{
166
    var_TriggerCallback(pl_Get(getIntf()), "rate-slower");
167 168 169 170
}

- (void)normalSpeed
{
171
    var_SetFloat(pl_Get(getIntf()), "rate", 1.);
172 173
}

174 175
- (void)toggleRecord
{
176
    intf_thread_t *p_intf = getIntf();
177
    if (!p_intf)
178
        return;
179

180
    input_thread_t * p_input;
181 182 183 184
    p_input = pl_CurrentInput(p_intf);
    if (p_input) {
        var_ToggleBool(p_input, "record");
        vlc_object_release(p_input);
185 186 187
    }
}

188 189
- (void)setPlaybackRate:(int)i_value
{
190
    playlist_t * p_playlist = pl_Get(getIntf());
191

192
    double speed = pow(2, (double)i_value / 17);
193
    int rate = INPUT_RATE_DEFAULT / speed;
194 195
    if (i_currentPlaybackRate != rate)
        var_SetFloat(p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate);
196 197 198 199 200
    i_currentPlaybackRate = rate;
}

- (int)playbackRate
{
201 202
    float f_rate;

203
    intf_thread_t *p_intf = getIntf();
204
    if (!p_intf)
205
        return 0;
206

207
    input_thread_t * p_input;
208 209 210 211
    p_input = pl_CurrentInput(p_intf);
    if (p_input) {
        f_rate = var_GetFloat(p_input, "rate");
        vlc_object_release(p_input);
212 213 214
    }
    else
    {
215
        playlist_t * p_playlist = pl_Get(getIntf());
216
        f_rate = var_GetFloat(p_playlist, "rate");
217
    }
218

219 220
    double value = 17 * log(f_rate) / log(2.);
    int returnValue = (int) ((value > 0) ? value + .5 : value - .5);
221

222
    if (returnValue < -34)
223
        returnValue = -34;
224
    else if (returnValue > 34)
225
        returnValue = 34;
226

227 228 229 230
    i_currentPlaybackRate = returnValue;
    return returnValue;
}

231 232
- (void)previous
{
233
    playlist_Prev(pl_Get(getIntf()));
234 235 236 237
}

- (void)next
{
238
    playlist_Next(pl_Get(getIntf()));
239 240
}

241 242
- (int)durationOfCurrentPlaylistItem
{
243
    intf_thread_t *p_intf = getIntf();
244
    if (!p_intf)
245
        return 0;
246

247
    input_thread_t * p_input = pl_CurrentInput(p_intf);
248
    int64_t i_duration = -1;
249
    if (!p_input)
250
        return i_duration;
251

252 253
    input_Control(p_input, INPUT_GET_LENGTH, &i_duration);
    vlc_object_release(p_input);
254

255
    return (int)(i_duration / 1000000);
256 257 258 259
}

- (NSURL*)URLOfCurrentPlaylistItem
{
260
    intf_thread_t *p_intf = getIntf();
261
    if (!p_intf)
262
        return nil;
263

264 265
    input_thread_t *p_input = pl_CurrentInput(p_intf);
    if (!p_input)
266
        return nil;
267

268 269 270
    input_item_t *p_item = input_GetItem(p_input);
    if (!p_item) {
        vlc_object_release(p_input);
271 272
        return nil;
    }
273

274 275 276
    char *psz_uri = input_item_GetURI(p_item);
    if (!psz_uri) {
        vlc_object_release(p_input);
277 278
        return nil;
    }
279 280

    NSURL *o_url;
281
    o_url = [NSURL URLWithString:toNSStr(psz_uri)];
282
    vlc_object_release(p_input);
283 284

    return o_url;
285 286 287 288
}

- (NSString*)nameOfCurrentPlaylistItem
{
289
    intf_thread_t *p_intf = getIntf();
290
    if (!p_intf)
291
        return nil;
292

293 294
    input_thread_t *p_input = pl_CurrentInput(p_intf);
    if (!p_input)
295
        return nil;
296

297 298 299
    input_item_t *p_item = input_GetItem(p_input);
    if (!p_item) {
        vlc_object_release(p_input);
300 301
        return nil;
    }
302

303 304 305
    char *psz_uri = input_item_GetURI(p_item);
    if (!psz_uri) {
        vlc_object_release(p_input);
306 307
        return nil;
    }
308

309
    NSString *o_name = @"";
310
    char *format = var_InheritString(getIntf(), "input-title-format");
311
    if (format) {
312
        char *formated = vlc_strfinput(p_input, format);
313 314 315 316
        free(format);
        o_name = toNSStr(formated);
        free(formated);
    }
317

318
    NSURL * o_url = [NSURL URLWithString:toNSStr(psz_uri)];
319
    free(psz_uri);
320

321 322
    if ([o_name isEqualToString:@""]) {
        if ([o_url isFileURL])
323
            o_name = [[NSFileManager defaultManager] displayNameAtPath:[o_url path]];
324 325 326
        else
            o_name = [o_url absoluteString];
    }
327
    vlc_object_release(p_input);
328
    return o_name;
329 330
}

331 332
- (void)forward
{
333 334
    //LEGACY SUPPORT
    [self forwardShort];
335 336 337 338
}

- (void)backward
{
339 340
    //LEGACY SUPPORT
    [self backwardShort];
341 342
}

343 344
- (void)jumpWithValue:(char *)p_value forward:(BOOL)b_value
{
345
    input_thread_t *p_input = pl_CurrentInput(getIntf());
346 347 348 349 350 351 352 353
    if (!p_input)
        return;

    int i_interval = var_InheritInteger( p_input, p_value );
    if (i_interval > 0) {
        mtime_t val = CLOCK_FREQ * i_interval;
        if (!b_value)
            val = val * -1;
354
        var_SetInteger( p_input, "time-offset", val );
355 356 357 358
    }
    vlc_object_release(p_input);
}

359
- (void)forwardExtraShort
360
{
361
    [self jumpWithValue:"extrashort-jump-size" forward:YES];
362
}
363

364 365
- (void)backwardExtraShort
{
366
    [self jumpWithValue:"extrashort-jump-size" forward:NO];
367 368 369 370
}

- (void)forwardShort
{
371
    [self jumpWithValue:"short-jump-size" forward:YES];
372 373 374 375
}

- (void)backwardShort
{
376
    [self jumpWithValue:"short-jump-size" forward:NO];
377 378 379 380
}

- (void)forwardMedium
{
381
    [self jumpWithValue:"medium-jump-size" forward:YES];
382 383 384 385
}

- (void)backwardMedium
{
386
    [self jumpWithValue:"medium-jump-size" forward:NO];
387 388 389 390
}

- (void)forwardLong
{
391
    [self jumpWithValue:"long-jump-size" forward:YES];
392 393 394 395
}

- (void)backwardLong
{
396
    [self jumpWithValue:"long-jump-size" forward:NO];
397 398 399 400
}

- (void)shuffle
{
401
    intf_thread_t *p_intf = getIntf();
402
    if (!p_intf)
403
        return;
404

405
    vlc_value_t val;
406
    playlist_t * p_playlist = pl_Get(p_intf);
407 408
    vout_thread_t *p_vout = getVout();

409
    var_Get(p_playlist, "random", &val);
410
    val.b_bool = !val.b_bool;
411 412 413 414 415
    var_Set(p_playlist, "random", val);
    if (val.b_bool) {
        if (p_vout) {
            vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Random On"));
            vlc_object_release(p_vout);
416
        }
417
        config_PutInt(p_playlist, "random", 1);
418 419 420
    }
    else
    {
421 422 423
        if (p_vout) {
            vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Random Off"));
            vlc_object_release(p_vout);
424
        }
425
        config_PutInt(p_playlist, "random", 0);
426
    }
427 428 429 430
}

- (void)repeatAll
{
431
    intf_thread_t *p_intf = getIntf();
432
    if (!p_intf)
433
        return;
434

435
    playlist_t * p_playlist = pl_Get(p_intf);
436

437 438 439 440
    var_SetBool(p_playlist, "repeat", NO);
    var_SetBool(p_playlist, "loop", YES);
    config_PutInt(p_playlist, "repeat", NO);
    config_PutInt(p_playlist, "loop", YES);
441 442

    vout_thread_t *p_vout = getVout();
443 444 445
    if (p_vout) {
        vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat All"));
        vlc_object_release(p_vout);
446
    }
447 448 449 450
}

- (void)repeatOne
{
451
    intf_thread_t *p_intf = getIntf();
452
    if (!p_intf)
453
        return;
454

455
    playlist_t * p_playlist = pl_Get(p_intf);
456

457 458 459 460
    var_SetBool(p_playlist, "repeat", YES);
    var_SetBool(p_playlist, "loop", NO);
    config_PutInt(p_playlist, "repeat", YES);
    config_PutInt(p_playlist, "loop", NO);
461 462

    vout_thread_t *p_vout = getVout();
463 464 465
    if (p_vout) {
        vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat One"));
        vlc_object_release(p_vout);
466
    }
467 468 469 470
}

- (void)repeatOff
{
471
    intf_thread_t *p_intf = getIntf();
472
    if (!p_intf)
473
        return;
474

475
    playlist_t * p_playlist = pl_Get(p_intf);
476

477 478 479 480
    var_SetBool(p_playlist, "repeat", NO);
    var_SetBool(p_playlist, "loop", NO);
    config_PutInt(p_playlist, "repeat", NO);
    config_PutInt(p_playlist, "loop", NO);
481 482

    vout_thread_t *p_vout = getVout();
483 484 485
    if (p_vout) {
        vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat Off"));
        vlc_object_release(p_vout);
486
    }
487 488
}

489 490 491
- (void)setAtoB
{
    if (!timeA) {
492
        input_thread_t * p_input = pl_CurrentInput(getIntf());
493
        if (p_input) {
494
            timeA = var_GetInteger(p_input, "time");
495 496 497
            vlc_object_release(p_input);
        }
    } else if (!timeB) {
498
        input_thread_t * p_input = pl_CurrentInput(getIntf());
499
        if (p_input) {
500
            timeB = var_GetInteger(p_input, "time");
501 502
            vlc_object_release(p_input);
        }
503 504 505 506 507 508 509 510
    } else
        [self resetAtoB];
}

- (void)resetAtoB
{
    timeA = 0;
    timeB = 0;
511 512 513 514 515
}

- (void)updateAtoB
{
    if (timeB) {
516
        input_thread_t * p_input = pl_CurrentInput(getIntf());
517
        if (p_input) {
518
            mtime_t currentTime = var_GetInteger(p_input, "time");
519
            if ( currentTime >= timeB || currentTime < timeA)
520
                var_SetInteger(p_input, "time", timeA);
521 522 523 524 525
            vlc_object_release(p_input);
        }
    }
}

526 527
- (void)volumeUp
{
528
    intf_thread_t *p_intf = getIntf();
529
    if (!p_intf)
530
        return;
531

532
    playlist_VolumeUp(pl_Get(p_intf), 1, NULL);
533 534 535 536
}

- (void)volumeDown
{
537
    intf_thread_t *p_intf = getIntf();
538
    if (!p_intf)
539
        return;
540

541
    playlist_VolumeDown(pl_Get(p_intf), 1, NULL);
542 543
}

544 545
- (void)toggleMute
{
546
    intf_thread_t *p_intf = getIntf();
547
    if (!p_intf)
548 549
        return;

550
    playlist_MuteToggle(pl_Get(p_intf));
551 552
}

553
- (BOOL)mute
554
{
555
    intf_thread_t *p_intf = getIntf();
556
    if (!p_intf)
557
        return NO;
558

559
    BOOL b_is_muted = NO;
560
    b_is_muted = playlist_MuteGet(pl_Get(p_intf)) > 0;
561 562

    return b_is_muted;
563 564 565
}

- (int)volume
566
{
567
    intf_thread_t *p_intf = getIntf();
568
    if (!p_intf)
569
        return 0;
570

571
    float volume = playlist_VolumeGet(pl_Get(p_intf));
572

573
    return lroundf(volume * AOUT_VOLUME_DEFAULT);
574
}
575

576 577
- (void)setVolume: (int)i_value
{
578
    intf_thread_t *p_intf = getIntf();
579
    if (!p_intf)
580
        return;
581

582 583 584
    if (i_value >= self.maxVolume)
        i_value = self.maxVolume;

585 586 587
    float f_value = i_value / (float)AOUT_VOLUME_DEFAULT;

    playlist_VolumeSet(pl_Get(p_intf), f_value);
588 589
}

590 591
- (float)maxVolume
{
592
    if (f_maxVolume == 0.) {
593
        f_maxVolume = (float)var_InheritInteger(getIntf(), "macosx-max-volume") / 100. * AOUT_VOLUME_DEFAULT;
594 595 596
    }

    return f_maxVolume;
597 598
}

599 600
- (void)addSubtitlesToCurrentInput:(NSArray *)paths
{
601
    input_thread_t * p_input = pl_CurrentInput(getIntf());
602 603 604 605 606 607 608
    if (!p_input)
        return;

    NSUInteger count = [paths count];

    for (int i = 0; i < count ; i++) {
        const char *path = [[[paths objectAtIndex:i] path] UTF8String];
609
        msg_Dbg(getIntf(), "loading subs from %s", path);
610 611 612

        int i_result = input_AddSubtitleOSD(p_input, path, true, true);
        if (i_result != VLC_SUCCESS)
613
            msg_Warn(getIntf(), "unable to load subtitles from '%s'", path);
614 615 616 617
    }
    vlc_object_release(p_input);
}

618 619
- (void)showPosition
{
620
    input_thread_t *p_input = pl_CurrentInput(getIntf());
621 622 623
    if (p_input != NULL) {
        vout_thread_t *p_vout = input_GetVout(p_input);
        if (p_vout != NULL) {
624
            var_SetInteger(getIntf()->obj.libvlc, "key-action", ACTIONID_POSITION);
625 626 627 628 629 630
            vlc_object_release(p_vout);
        }
        vlc_object_release(p_input);
    }
}

631
#pragma mark - Drop support for files into the video, controls bar or drop box
632

633 634 635
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
{
    NSArray *items = [[[VLCMain sharedInstance] playlist] createItemsFromExternalPasteboard:[sender draggingPasteboard]];
636

637 638
    if (items.count == 0)
        return NO;
639

640
    // Try to add file as a subtitle
641
    input_thread_t *p_input = pl_CurrentInput(getIntf());
642 643 644 645 646 647 648 649
    if (items.count == 1 && p_input) {
        NSString *url = [[items firstObject] valueForKey:@"ITEM_URL"];
        char *path = vlc_uri2path([url UTF8String]);

        if (path) {
            int i_result = input_AddSubtitleOSD(p_input, path, true, true);
            free(path);
            if (i_result == VLC_SUCCESS) {
650
                vlc_object_release(p_input);
651
                return YES;
652 653 654
            }
        }
    }
655 656 657 658 659 660

    if (p_input)
        vlc_object_release(p_input);

    [[[VLCMain sharedInstance] playlist] addPlaylistItems:items];
    return YES;
661 662
}

663
#pragma mark - video output stuff
664

665
- (void)setAspectRatioIsLocked:(BOOL)b_value
666
{
667
    config_PutInt(getIntf(), "macosx-lock-aspect-ratio", b_value);
668 669 670 671
}

- (BOOL)aspectRatioIsLocked
{
672
    return config_GetInt(getIntf(), "macosx-lock-aspect-ratio");
673 674 675 676
}

- (void)toggleFullscreen
{
677
    intf_thread_t *p_intf = getIntf();
678
    if (!p_intf)
679
        return;
680

681
    vout_thread_t *p_vout = getVoutForActiveWindow();
682
    if (p_vout) {
683 684
        BOOL b_fs = var_ToggleBool(p_vout, "fullscreen");
        var_SetBool(pl_Get(p_intf), "fullscreen", b_fs);
685
        vlc_object_release(p_vout);
686
    } else { // e.g. lion fullscreen toggle
687
        BOOL b_fs = var_ToggleBool(pl_Get(p_intf), "fullscreen");
688
        [[[VLCMain sharedInstance] voutController] setFullscreen:b_fs forWindow:nil withAnimation:YES];
689
    }
690
}
691

692
#pragma mark - uncommon stuff
693 694 695 696 697 698 699 700

- (BOOL)fixPreferences
{
    NSMutableString * o_workString;
    NSRange returnedRange;
    NSRange fullRange;
    BOOL b_needsRestart = NO;

701
    #define fixpref(pref) \
702
    o_workString = [[NSMutableString alloc] initWithFormat:@"%s", config_GetPsz(getIntf(), pref)]; \
703 704 705 706 707 708 709
    if ([o_workString length] > 0) \
    { \
        returnedRange = [o_workString rangeOfString:@"macosx" options: NSCaseInsensitiveSearch]; \
        if (returnedRange.location != NSNotFound) \
        { \
            if ([o_workString isEqualToString:@"macosx"]) \
                [o_workString setString:@""]; \
710
            fullRange = NSMakeRange(0, [o_workString length]); \
711
            [o_workString replaceOccurrencesOfString:@":macosx" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
712
            fullRange = NSMakeRange(0, [o_workString length]); \
713 714
            [o_workString replaceOccurrencesOfString:@"macosx:" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
            \
715
            config_PutPsz(getIntf(), pref, [o_workString UTF8String]); \
716 717
            b_needsRestart = YES; \
        } \
718
    }
719

720 721
    fixpref("control");
    fixpref("extraintf");
722 723 724 725 726
    #undef fixpref

    return b_needsRestart;
}

727 728
#pragma mark - video filter handling

729
- (const char *)getFilterType:(const char *)psz_name
730 731 732 733 734 735 736 737
{
    module_t *p_obj = module_find(psz_name);
    if (!p_obj) {
        return NULL;
    }

    if (module_provides(p_obj, "video splitter")) {
        return "video-splitter";
738
    } else if (module_provides(p_obj, "video filter")) {
739 740 741 742 743 744
        return "video-filter";
    } else if (module_provides(p_obj, "sub source")) {
        return "sub-source";
    } else if (module_provides(p_obj, "sub filter")) {
        return "sub-filter";
    } else {
745
        msg_Err(getIntf(), "Unknown video filter type.");
746 747 748 749
        return NULL;
    }
}

750
- (void)setVideoFilter: (const char *)psz_name on:(BOOL)b_on
751
{
752
    intf_thread_t *p_intf = getIntf();
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    if (!p_intf)
        return;
    char *psz_string, *psz_parser;

    const char *psz_filter_type = [self getFilterType:psz_name];
    if (!psz_filter_type) {
        msg_Err(p_intf, "Unable to find filter module \"%s\".", psz_name);
        return;
    }

    msg_Dbg(p_intf, "will set filter '%s'", psz_name);


    psz_string = config_GetPsz(p_intf, psz_filter_type);

    if (b_on) {
        if (psz_string == NULL) {
            psz_string = strdup(psz_name);
        } else if (strstr(psz_string, psz_name) == NULL) {
            char *psz_tmp = strdup([[NSString stringWithFormat: @"%s:%s", psz_string, psz_name] UTF8String]);
            free(psz_string);
            psz_string = psz_tmp;
        }
    } else {
        if (!psz_string)
            return;

        psz_parser = strstr(psz_string, psz_name);
        if (psz_parser) {
            if (*(psz_parser + strlen(psz_name)) == ':') {
                memmove(psz_parser, psz_parser + strlen(psz_name) + 1,
                        strlen(psz_parser + strlen(psz_name) + 1) + 1);
            } else {
                *psz_parser = '\0';
            }

            /* Remove trailing : : */
            if (strlen(psz_string) > 0 && *(psz_string + strlen(psz_string) -1) == ':')
                *(psz_string + strlen(psz_string) -1) = '\0';
        } else {
            free(psz_string);
            return;
        }
    }
    config_PutPsz(p_intf, psz_filter_type, psz_string);

    /* Try to set on the fly */
    if (!strcmp(psz_filter_type, "video-splitter")) {
        playlist_t *p_playlist = pl_Get(p_intf);
        var_SetString(p_playlist, psz_filter_type, psz_string);
    } else {
        vout_thread_t *p_vout = getVout();
        if (p_vout) {
            var_SetString(p_vout, psz_filter_type, psz_string);
            vlc_object_release(p_vout);
        }
    }

    free(psz_string);
}

814
- (void)restartFilterIfNeeded: (const char *)psz_filter option: (const char *)psz_name
815 816
{
    vout_thread_t *p_vout = getVout();
817
    intf_thread_t *p_intf = getIntf();
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    if (!p_intf)
        return;

    if (p_vout == NULL)
        return;
    else
        vlc_object_release(p_vout);

    vlc_object_t *p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
    if (p_filter) {

        /* we cannot rely on the p_filter existence.
         This filter might be just
         disabled, but the object still exists. Therefore, the string
         is checked, additionally.
         */
        const char *psz_filter_type = [self getFilterType:psz_filter];
        if (!psz_filter_type) {
            msg_Err(p_intf, "Unable to find filter module \"%s\".", psz_name);
            goto out;
        }

        char *psz_string = config_GetPsz(p_intf, psz_filter_type);
        if (!psz_string) {
            goto out;
        }
        if (strstr(psz_string, psz_filter) == NULL) {
            free(psz_string);
            goto out;
        }
        free(psz_string);

        int i_type;
        i_type = var_Type(p_filter, psz_name);
        if (i_type == 0)
            i_type = config_GetType(p_intf, psz_name);

        if (!(i_type & VLC_VAR_ISCOMMAND)) {
            msg_Warn(p_intf, "Brute-restarting filter '%s', because the last changed option isn't a command", psz_name);

            [self setVideoFilter: psz_filter on: NO];
            [self setVideoFilter: psz_filter on: YES];
        } else
            msg_Dbg(p_intf, "restart not needed");

        out:
        vlc_object_release(p_filter);
    }
}

868
- (void)setVideoFilterProperty: (const char *)psz_name forFilter: (const char *)psz_filter integer: (int)i_value
869 870 871
{
    vout_thread_t *p_vout = getVout();
    vlc_object_t *p_filter;
872
    intf_thread_t *p_intf = getIntf();
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
    if (!p_intf)
        return;

    config_PutInt(p_intf, psz_name, i_value);

    if (p_vout) {
        p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);

        if (!p_filter) {
            msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
            vlc_object_release(p_vout);
            return;
        }
        var_SetInteger(p_filter, psz_name, i_value);
        vlc_object_release(p_vout);
        vlc_object_release(p_filter);

        [self restartFilterIfNeeded: psz_filter option: psz_name];
    }
}

894
- (void)setVideoFilterProperty: (const char *)psz_name forFilter: (const char *)psz_filter float: (float)f_value
895 896 897
{
    vout_thread_t *p_vout = getVout();
    vlc_object_t *p_filter;
898
    intf_thread_t *p_intf = getIntf();
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    if (!p_intf)
        return;

    config_PutFloat(p_intf, psz_name, f_value);

    if (p_vout) {
        p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);

        if (!p_filter) {
            msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
            vlc_object_release(p_vout);
            return;
        }
        var_SetFloat(p_filter, psz_name, f_value);
        vlc_object_release(p_vout);
        vlc_object_release(p_filter);

        [self restartFilterIfNeeded: psz_filter option: psz_name];
    }
}

920
- (void)setVideoFilterProperty: (const char *)psz_name forFilter: (const char *)psz_filter string: (const char *)psz_value
921 922 923 924
{
    char *psz_new_value = strdup(psz_value);
    vout_thread_t *p_vout = getVout();
    vlc_object_t *p_filter;
925
    intf_thread_t *p_intf = getIntf();
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    if (!p_intf)
        return;

    config_PutPsz(p_intf, psz_name, EnsureUTF8(psz_new_value));

    if (p_vout) {
        p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);

        if (!p_filter) {
            msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
            vlc_object_release(p_vout);
            return;
        }
        var_SetString(p_filter, psz_name, EnsureUTF8(psz_new_value));
        vlc_object_release(p_vout);
        vlc_object_release(p_filter);

        [self restartFilterIfNeeded: psz_filter option: psz_name];
    }

    free(psz_new_value);
}

949
- (void)setVideoFilterProperty: (const char *)psz_name forFilter: (const char *)psz_filter boolean: (BOOL)b_value
950 951 952
{
    vout_thread_t *p_vout = getVout();
    vlc_object_t *p_filter;
953
    intf_thread_t *p_intf = getIntf();
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    if (!p_intf)
        return;

    config_PutInt(p_intf, psz_name, b_value);

    if (p_vout) {
        p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);

        if (!p_filter) {
            msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
            vlc_object_release(p_vout);
            return;
        }
        var_SetBool(p_filter, psz_name, b_value);
        vlc_object_release(p_vout);
        vlc_object_release(p_filter);
    }
}

973 974 975 976 977 978 979 980 981 982
#pragma mark -
#pragma mark Media Key support

- (void)resetMediaKeyJump
{
    b_mediakeyJustJumped = NO;
}

- (void)coreChangedMediaKeySupportSetting: (NSNotification *)o_notification
{
983
    intf_thread_t *p_intf = getIntf();
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    if (!p_intf)
        return;

    b_mediaKeySupport = var_InheritBool(p_intf, "macosx-mediakeys");
    if (b_mediaKeySupport && !_mediaKeyController)
        _mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];

    VLCMain *main = [VLCMain sharedInstance];
    if (b_mediaKeySupport && ([[[main playlist] model] hasChildren] ||
                              [[main inputManager] hasInput])) {
        if (!b_mediaKeyTrapEnabled) {
            b_mediaKeyTrapEnabled = YES;
            msg_Dbg(p_intf, "Enable media key support");
            [_mediaKeyController startWatchingMediaKeys];
        }
    } else {
        if (b_mediaKeyTrapEnabled) {
            b_mediaKeyTrapEnabled = NO;
            msg_Dbg(p_intf, "Disable media key support");
            [_mediaKeyController stopWatchingMediaKeys];
        }
    }
}

-(void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event
{
    if (b_mediaKeySupport) {
        assert([event type] == NSSystemDefined && [event subtype] == SPSystemDefinedEventMediaKeys);

        int keyCode = (([event data1] & 0xFFFF0000) >> 16);
        int keyFlags = ([event data1] & 0x0000FFFF);
        int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
        int keyRepeat = (keyFlags & 0x1);

        if (keyCode == NX_KEYTYPE_PLAY && keyState == 0)
            [self playOrPause];

        if ((keyCode == NX_KEYTYPE_FAST || keyCode == NX_KEYTYPE_NEXT) && !b_mediakeyJustJumped) {
            if (keyState == 0 && keyRepeat == 0)
                [self next];
            else if (keyRepeat == 1) {
                [self forwardShort];
                b_mediakeyJustJumped = YES;
                [self performSelector:@selector(resetMediaKeyJump)
                           withObject: NULL
                           afterDelay:0.25];
            }
        }

        if ((keyCode == NX_KEYTYPE_REWIND || keyCode == NX_KEYTYPE_PREVIOUS) && !b_mediakeyJustJumped) {
            if (keyState == 0 && keyRepeat == 0)
                [self previous];
            else if (keyRepeat == 1) {
                [self backwardShort];
                b_mediakeyJustJumped = YES;
                [self performSelector:@selector(resetMediaKeyJump)
                           withObject: NULL
                           afterDelay:0.25];
            }
        }
    }
}

#pragma mark -
#pragma mark Apple Remote Control

- (void)startListeningWithAppleRemote
{
    [_remote startListening: self];
}

- (void)stopListeningWithAppleRemote
{
    [_remote stopListening:self];
}

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
#pragma mark - menu navigation
- (void)menuFocusActivate
{
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());
    if (p_input_thread == NULL)
        return;

    input_Control(p_input_thread, INPUT_NAV_ACTIVATE, NULL );
    vlc_object_release(p_input_thread);
}

- (void)moveMenuFocusLeft
{
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());
    if (p_input_thread == NULL)
        return;

    input_Control(p_input_thread, INPUT_NAV_LEFT, NULL );
    vlc_object_release(p_input_thread);
}

- (void)moveMenuFocusRight
{
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());
    if (p_input_thread == NULL)
        return;

    input_Control(p_input_thread, INPUT_NAV_RIGHT, NULL );
    vlc_object_release(p_input_thread);
}

- (void)moveMenuFocusUp
{
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());
    if (p_input_thread == NULL)
        return;

    input_Control(p_input_thread, INPUT_NAV_UP, NULL );
    vlc_object_release(p_input_thread);
}

- (void)moveMenuFocusDown
{
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());
    if (p_input_thread == NULL)
        return;

    input_Control(p_input_thread, INPUT_NAV_DOWN, NULL );
    vlc_object_release(p_input_thread);
}

1111 1112 1113 1114
/* Helper method for the remote control interface in order to trigger forward/backward and volume
 increase/decrease as long as the user holds the left/right, plus/minus button */
- (void) executeHoldActionForRemoteButton: (NSNumber*) buttonIdentifierNumber
{
1115
    intf_thread_t *p_intf = getIntf();
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    if (!p_intf)
        return;

    if (b_remote_button_hold) {
        switch([buttonIdentifierNumber intValue]) {
            case kRemoteButtonRight_Hold:
                [self forward];
                break;
            case kRemoteButtonLeft_Hold:
                [self backward];
                break;
            case kRemoteButtonVolume_Plus_Hold:
                if (p_intf)
1129
                    var_SetInteger(p_intf->obj.libvlc, "key-action", ACTIONID_VOL_UP);
1130 1131 1132
                break;
            case kRemoteButtonVolume_Minus_Hold:
                if (p_intf)
1133
                    var_SetInteger(p_intf->obj.libvlc, "key-action", ACTIONID_VOL_DOWN);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
                break;
        }
        if (b_remote_button_hold) {
            /* trigger event */
            [self performSelector:@selector(executeHoldActionForRemoteButton:)
                       withObject:buttonIdentifierNumber
                       afterDelay:0.25];
        }
    }
}

/* Apple Remote callback */
- (void) appleRemoteButton: (AppleRemoteEventIdentifier)buttonIdentifier
               pressedDown: (BOOL) pressedDown
                clickCount: (unsigned int) count
{
1150
    intf_thread_t *p_intf = getIntf();
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    if (!p_intf)
        return;

    switch(buttonIdentifier) {
        case k2009RemoteButtonFullscreen:
            [self toggleFullscreen];
            break;
        case k2009RemoteButtonPlay:
            [self playOrPause];
            break;
        case kRemoteButtonPlay:
            if (count >= 2)
                [self toggleFullscreen];
            else
                [self playOrPause];
            break;
        case kRemoteButtonVolume_Plus:
1168
            if (config_GetInt(getIntf(), "macosx-appleremote-sysvol"))
1169 1170 1171
                [NSSound increaseSystemVolume];
            else
                if (p_intf)
1172
                    var_SetInteger(p_intf->obj.libvlc, "key-action", ACTIONID_VOL_UP);
1173 1174
            break;
        case kRemoteButtonVolume_Minus:
1175
            if (config_GetInt(getIntf(), "macosx-appleremote-sysvol"))
1176 1177 1178
                [NSSound decreaseSystemVolume];
            else
                if (p_intf)
1179
                    var_SetInteger(p_intf->obj.libvlc, "key-action", ACTIONID_VOL_DOWN);
1180 1181
            break;
        case kRemoteButtonRight:
1182
            if (config_GetInt(getIntf(), "macosx-appleremote-prevnext"))
1183 1184 1185 1186 1187
                [self forward];
            else
                [self next];
            break;
        case kRemoteButtonLeft:
1188
            if (config_GetInt(getIntf(), "macosx-appleremote-prevnext"))
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
                [self backward];
            else
                [self previous];
            break;
        case kRemoteButtonRight_Hold:
        case kRemoteButtonLeft_Hold:
        case kRemoteButtonVolume_Plus_Hold:
        case kRemoteButtonVolume_Minus_Hold:
            /* simulate an event as long as the user holds the button */
            b_remote_button_hold = pressedDown;
            if (pressedDown) {
                NSNumber* buttonIdentifierNumber = [NSNumber numberWithInt:buttonIdentifier];
                [self performSelector:@selector(executeHoldActionForRemoteButton:)
                           withObject:buttonIdentifierNumber];
            }
            break;
        case kRemoteButtonMenu:
1206
            [self showPosition];
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
            break;
        case kRemoteButtonPlay_Sleep:
        {
            NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
            [script executeAndReturnError:nil];
            break;
        }
        default:
            /* Add here whatever you want other buttons to do */
            break;
    }
}

#pragma mark -
#pragma mark Key Shortcuts

/*****************************************************************************
 * hasDefinedShortcutKey: Check to see if the key press is a defined VLC
 * shortcut key.  If it is, pass it off to VLC for handling and return YES,
 * otherwise ignore it and return NO (where it will get handled by Cocoa).
 *****************************************************************************/
1228 1229 1230 1231 1232 1233 1234 1235 1236

- (BOOL)keyEvent:(NSEvent *)o_event
{
    BOOL eventHandled = NO;
    NSString * characters = [o_event charactersIgnoringModifiers];
    if ([characters length] > 0) {
        unichar key = [characters characterAtIndex: 0];

        if (key) {
1237
            input_thread_t * p_input = pl_CurrentInput(getIntf());
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
            if (p_input != NULL) {
                vout_thread_t *p_vout = input_GetVout(p_input);

                if (p_vout != NULL) {
                    /* Escape */
                    if (key == (unichar) 0x1b) {
                        if (var_GetBool(p_vout, "fullscreen")) {
                            [self toggleFullscreen];
                            eventHandled = YES;
                        }
                    }
                    vlc_object_release(p_vout);
                }
                vlc_object_release(p_input);
            }
        }
    }
    return eventHandled;
}

1258 1259
- (BOOL)hasDefinedShortcutKey:(NSEvent *)o_event force:(BOOL)b_force
{
1260
    intf_thread_t *p_intf = getIntf();
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    if (!p_intf)
        return NO;

    unichar key = 0;
    vlc_value_t val;
    unsigned int i_pressed_modifiers = 0;

    val.i_int = 0;
    i_pressed_modifiers = [o_event modifierFlags];

    if (i_pressed_modifiers & NSControlKeyMask)
        val.i_int |= KEY_MODIFIER_CTRL;

    if (i_pressed_modifiers & NSAlternateKeyMask)
        val.i_int |= KEY_MODIFIER_ALT;

    if (i_pressed_modifiers & NSShiftKeyMask)
        val.i_int |= KEY_MODIFIER_SHIFT;

    if (i_pressed_modifiers & NSCommandKeyMask)
        val.i_int |= KEY_MODIFIER_COMMAND;

    NSString * characters = [o_event charactersIgnoringModifiers];
    if ([characters length] > 0) {
        key = [[characters lowercaseString] characterAtIndex: 0];

        /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
        if (key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask) {
            [self toggleFullscreen];
            return YES;
        }

        if (!b_force) {
            switch(key) {
                case NSDeleteCharacter:
                case NSDeleteFunctionKey:
                case NSDeleteCharFunctionKey:
                case NSBackspaceCharacter:
                case NSUpArrowFunctionKey:
                case NSDownArrowFunctionKey:
                case NSEnterCharacter:
                case NSCarriageReturnCharacter:
                    return NO;
            }
        }

        val.i_int |= CocoaKeyToVLC(key);

        BOOL b_found_key = NO;
        for (NSUInteger i = 0; i < [_usedHotkeys count]; i++) {
            NSString *str = [_usedHotkeys objectAtIndex:i];
            unsigned int i_keyModifiers = [[VLCStringUtility sharedInstance] VLCModifiersToCocoa: str];

            if ([[characters lowercaseString] isEqualToString: [[VLCStringUtility sharedInstance] VLCKeyToString: str]] &&
                (i_keyModifiers & NSShiftKeyMask)     == (i_pressed_modifiers & NSShiftKeyMask) &&
                (i_keyModifiers & NSControlKeyMask)   == (i_pressed_modifiers & NSControlKeyMask) &&
                (i_keyModifiers & NSAlternateKeyMask) == (i_pressed_modifiers & NSAlternateKeyMask) &&
                (i_keyModifiers & NSCommandKeyMask)   == (i_pressed_modifiers & NSCommandKeyMask)) {
                b_found_key = YES;
                break;
            }
        }

        if (b_found_key) {
1325
            var_SetInteger(p_intf->obj.libvlc, "key-pressed", val.i_int);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
            return YES;
        }
    }

    return NO;
}

- (void)updateCurrentlyUsedHotkeys
{
    NSMutableArray *mutArray = [[NSMutableArray alloc] init];
    /* Get the main Module */
    module_t *p_main = module_get_main();
    assert(p_main);
    unsigned confsize;
    module_config_t *p_config;

    p_config = module_config_get (p_main, &confsize);

    for (size_t i = 0; i < confsize; i++) {
        module_config_t *p_item = p_config + i;

        if (CONFIG_ITEM(p_item->i_type) && p_item->psz_name != NULL
            && !strncmp(p_item->psz_name , "key-", 4)
            && !EMPTY_STR(p_item->psz_text)) {
            if (p_item->value.psz)
1351
                [mutArray addObject:toNSStr(p_item->value.psz)];
1352 1353 1354 1355 1356 1357 1358
        }
    }
    module_config_free (p_config);

    _usedHotkeys = [[NSArray alloc] initWithArray:mutArray copyItems:YES];
}

1359
@end