aboutsummaryrefslogtreecommitdiff
path: root/VKPC/CatchMediaButtons.m
blob: 34db688abf06acb47e2d747547085eb4d23e9063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
//  CatchMediaButtons.m
//  VKPC
//
//  Created by Eugene on 10/22/14.
//  Copyright (c) 2014 Eugene Z. All rights reserved.

#import "CatchMediaButtons.h"
#import "MultiClickRemoteBehavior.h"
#import "SPMediaKeyTap.h"
#import "Controller.h"

static SPMediaKeyTap *keyTap;
static MultiClickRemoteBehavior *remoteBehavior;
static RemoteControl *remoteControl;
static BOOL started = NO;
static BOOL initialized = NO;

@implementation CatchMediaButtons

+ (void)initialize {
    if (initialized) {
        return;
    }
    
    keyTap = nil;
    [[NSUserDefaults standardUserDefaults] addObserver:(id)[self class]
                                            forKeyPath:VKPCPreferencesCatchMediaButtons
                                               options:NSKeyValueObservingOptionNew
                                               context:NULL];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:VKPCPreferencesCatchMediaButtons]) {
        [CatchMediaButtons start];
    }
    
    initialized = YES;
}

+ (void)start {
    NSLog(@"[CatchMediaButtons] start");
    
    if (started) {
        NSLog(@"[CatchMediaButtons] start: already started, calling stop first");
        [self stop];
    }
    
    if (keyTap == nil) {
        NSLog(@"[CatchMediaButtons] start: keyTap == nil, creating instance");
        
        keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self];
        
        remoteControl = [[AppleRemote alloc] initWithDelegate:self];
        [remoteControl setDelegate:self];
        
        remoteBehavior = [MultiClickRemoteBehavior new];
        [remoteBehavior setDelegate:self];
        [remoteControl setDelegate:remoteBehavior];
    }
    
    [keyTap startWatchingMediaKeys];
    [remoteControl startListening:self];
    
    NSLog(@"[CatchMediaButtons] started");
    started = YES;
}

+ (void)stop {
    NSLog(@"[CatchMediaButtons] stop");

    if (!started) {
        NSLog(@"[CatchMediaButtons] stop: not started");
        return;
    }
    
    [keyTap stopWatchingMediaKeys];
    [remoteControl stopListening:self];
    
    NSLog(@"[CatchMediaButtons] stopped");
    started = NO;
}

+ (void)remoteButton:(RemoteControlEventIdentifier)buttonIdentifier pressedDown:(BOOL)pressedDown clickCount:(unsigned int)clickCount {
    if (!pressedDown) {
        return;
    }
    
    switch(buttonIdentifier) {
        case kRemoteButtonPlay:
//            [self forAllPlay];
            break;
            
        case kRemoteButtonRight:
//            [self forAllNext];
            break;
            
        case kRemoteButtonLeft:
//            [self forAllPrev];
            break;
            
        default:
            break;
    }
}

+ (void)mediaKeyTap:(SPMediaKeyTap *)keyTap receivedMediaKeyEvent:(NSEvent *)event; {
    NSAssert(event.type == NSSystemDefined && [event subtype] == SPSystemDefinedEventMediaKeys, @"Unexpected NSEvent in mediaKeyTap:receivedMediaKeyEvent:");
    int keyCode = (([event data1] & 0xFFFF0000) >> 16);
    int keyFlags = ([event data1] & 0x0000FFFF);
    BOOL keyIsPressed = (((keyFlags & 0xFF00) >> 8)) == 0xA;
    
    if (keyIsPressed) {
        switch (keyCode) {
            case NX_KEYTYPE_PLAY:
                [Controller playpause];
                break;
                
            case NX_KEYTYPE_FAST:
                [Controller next];
                break;
                
            case NX_KEYTYPE_REWIND:
                [Controller prev];
                break;
                
            default:
                // More cases defined in hidsystem/ev_keymap.h
                break;
        }
    }
}

// KVO
+ (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:VKPCPreferencesCatchMediaButtons]) {
        NSNumber *new = change[NSKeyValueChangeKindKey];
        if ([new integerValue] == NSKeyValueChangeSetting) {
            BOOL value = [(NSNumber *)change[NSKeyValueChangeNewKey] boolValue];
            if (!value) {
                [CatchMediaButtons stop];
            } else {
                [CatchMediaButtons start];
            }
        }
    }
}

@end