forked from grooveshark/GSDesktopHelperMac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSDesktopHelperAppDelegate.m
315 lines (284 loc) · 11.9 KB
/
GSDesktopHelperAppDelegate.m
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// GSDesktopHelperAppDelegate.m
// GSDesktopHelper
//
// Created by Terin Stock on 5/22/10.
// Copyright 2010 Three Strange Days Development. All rights reserved.
//
#import "GSDesktopHelperAppDelegate.h"
#import <Carbon/Carbon.h>
#import <ShortcutRecorder/ShortcutRecorder.h>
@implementation GSDesktopHelperAppDelegate
void printToAPIFile(NSString *withAction) {
NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = @"~/Library/Preferences/GroovesharkDesktop.7F9BF17D6D9CB2159C78A6A6AB076EA0B1E0497C.1/Local Store/shortcutAction.txt";
path = [path stringByExpandingTildeInPath];
NSError *error;
if ([fm fileExistsAtPath:[path stringByDeletingLastPathComponent]] == NO) {
if (![fm createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Could not create directory: %@", [error localizedFailureReason]);
};
}
BOOL fileExists = [fm fileExistsAtPath:path];
if (!fileExists) {
if (![withAction writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"Could not write to file: %@", [error localizedFailureReason]);
return;
}
} else if ([fm isWritableFileAtPath:[path stringByDeletingLastPathComponent]]) {
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:path];
[output seekToEndOfFile];
[output writeData:[withAction dataUsingEncoding:NSUTF8StringEncoding]];
[output closeFile];
}
}
OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
EventHotKeyID hkCom;
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hkCom), NULL, &hkCom);
int eventID = hkCom.id;
switch (eventID) {
case 1:
printToAPIFile(@"previous\n");
break;
case 2:
printToAPIFile(@"next\n");
break;
case 3:
printToAPIFile(@"playpause\n");
break;
case 4:
printToAPIFile(@"shuffle\n");
break;
case 5:
printToAPIFile(@"favorite\n");
break;
case 6:
printToAPIFile(@"showsongtoast\n");
break;
default:
NSLog(@"%@", @"Key registered, no action defined");
break;
}
return noErr;
}
+ (void)initialize {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *file = [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"];
NSDictionary *appDefaults = [NSDictionary dictionaryWithContentsOfFile:file];
[defaults registerDefaults:appDefaults];
}
- (BOOL)isSetForLogin:(LSSharedFileListRef)loginItems ForPath:(NSString *)path {
BOOL exists = NO;
UInt32 seedValue;
CFURLRef thePath;
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in (NSArray *)loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef *)&thePath, NULL) == noErr) {
if ([[(NSURL *)thePath path] hasPrefix:path]) {
exists = YES;
break;
}
}
}
// CFRelease(thePath);
CFRelease(loginItemsArray);
return exists;
}
- (void)enableLoginItem:(LSSharedFileListRef)loginItems ForPath:(NSString *)path {
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:path];
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, url, NULL, NULL);
if (item) {
CFRelease(item);
}
}
- (void)disableLoginItem:(LSSharedFileListRef)loginItems ForPath:(NSString *)path {
UInt32 seedValue;
CFURLRef thePath;
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in (NSArray *)loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef *)&thePath, NULL) == noErr) {
if ([[(NSURL *)thePath path] hasPrefix:path]) {
LSSharedFileListItemRemove(loginItems, itemRef);
}
CFRelease(thePath);
}
}
CFRelease(loginItemsArray);
}
- (void)registerHotKeys:(BOOL)registerKeys {
static EventHotKeyRef eventHotKeyRef[6];
EventTypeSpec eventType;
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,NULL,NULL);
if (registerKeys == YES) {
if (hotKeyRegistered == NO) {
EventHotKeyID previousSong = {'gsur', 1};
EventHotKeyID nextSong = {'gsur', 2};
EventHotKeyID playPause = {'gsur', 3};
EventHotKeyID toggleShuffle = {'gsur', 4};
EventHotKeyID favoriteSong = {'gsur', 5};
EventHotKeyID songToast = {'gsur', 6};
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"PreviousKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"PreviousKey"] objectAtIndex:1] intValue]), previousSong, GetApplicationEventTarget(), 0, &eventHotKeyRef[0]);
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"NextKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"NextKey"] objectAtIndex:1] intValue]), nextSong, GetApplicationEventTarget(), 0, &eventHotKeyRef[1]);
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"PlayPauseKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"PlayPauseKey"] objectAtIndex:1] intValue]), playPause, GetApplicationEventTarget(), 0, &eventHotKeyRef[2]);
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"ShuffleKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"ShuffleKey"] objectAtIndex:1] intValue]), toggleShuffle, GetApplicationEventTarget(), 0, &eventHotKeyRef[3]);
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"FavoriteKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"FavoriteKey"] objectAtIndex:1] intValue]), favoriteSong, GetApplicationEventTarget(), 0, &eventHotKeyRef[4]);
RegisterEventHotKey([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"SongToastKey"] objectAtIndex:0] intValue], SRCocoaToCarbonFlags([[[[NSUserDefaults standardUserDefaults] arrayForKey:@"SongToastKey"] objectAtIndex:1] intValue]), songToast, GetApplicationEventTarget(), 0, &eventHotKeyRef[5]);
hotKeyRegistered = YES;
[[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"EnableGlobalKeys"];
[mi_globalKeys setState:NSOnState];
}
} else if (registerKeys == NO) {
if (hotKeyRegistered == YES) {
for (int i = 0; i < 8; i++) {
UnregisterEventHotKey(eventHotKeyRef[i]);
}
[mi_globalKeys setState:NSOffState];
[[NSUserDefaults standardUserDefaults] setBool:0 forKey:@"EnableGlobalKeys"];
hotKeyRegistered = NO;
}
}
}
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
GSDesktopHelperAppDelegate *appDelegate = (GSDesktopHelperAppDelegate *)refcon;
return [appDelegate processEvent:event withType:type];
}
- (CGEventRef) processEvent:(CGEventRef)event withType:(CGEventType)type {
//Paranoid sanity check.
if (type == kCGEventTapDisabledByTimeout) {
CGEventTapEnable(eventTap, YES);
return event;
} else if (type != NX_SYSDEFINED) {
return event;
}
NSEvent *e = [NSEvent eventWithCGEvent:event];
//We're getting a special event
if ([e type] == NSSystemDefined && [e subtype] == 8) {
if ([e data1] == 1051136) {
printToAPIFile(@"playpause\n");
return NULL;
} else if ([e data1] == 1313280) {
printToAPIFile(@"previous\n");
return NULL;
} else if ([e data1] == 1247744) {
printToAPIFile(@"next\n");
return NULL;
}
}
return event;
}
- (void) setupEvents {
// Create an event tap. We are interested in system defined keys.
eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, CGEventMaskBit(NX_SYSDEFINED), myCGEventCallback, self);
// Create a run loop source
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
//Add to the current run loop.
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
}
- (void)registerMediaKeys:(BOOL)registerKeys {
if (registerKeys == YES) {
if (mediaKeyRegistered == NO) {
mediaKeyRegistered = YES;
CGEventTapEnable(eventTap, true);
[[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"EnableMediaKeys"];
[mi_mediaKeys setState:NSOnState];
}
} else if (registerKeys == NO) {
if (mediaKeyRegistered == YES) {
mediaKeyRegistered = NO;
CGEventTapEnable(eventTap, false);
[[NSUserDefaults standardUserDefaults] setBool:0 forKey:@"EnableMediaKeys"];
[mi_mediaKeys setState:NSOffState];
}
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
bundlePath = [[NSBundle mainBundle] bundlePath];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if ([self isSetForLogin:loginItems ForPath:bundlePath]) {
loadAtLogin = YES;
[mi_startAtLogin setState:NSOnState];
}
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
NSImage *statusImage = [NSImage imageNamed:@"gs16.png"];
[statusItem setImage:statusImage];
[statusItem setHighlightMode:YES];
[statusItem setMenu:statusMenu];
hotKeyRegistered = NO;
mediaKeyRegistered = YES;
[mi_mediaKeys setState:NSOnState];
[self setupEvents];
[self registerHotKeys:[[NSUserDefaults standardUserDefaults] boolForKey:@"EnableGlobalKeys"]];
[self registerMediaKeys:[[NSUserDefaults standardUserDefaults] boolForKey:@"EnableMediaKeys"]];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
[mi_firstLaunch makeKeyAndOrderFront:self];
[[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
printToAPIFile([NSString stringWithFormat:@"Protocol %@\n", url]);
[self showGrooveshark:self];
}
- (IBAction)showGrooveshark:(id)sender {
NSTask *gsdesktop = [[NSTask alloc] init];
[gsdesktop setLaunchPath:@"/usr/bin/open"];
[gsdesktop setArguments:[NSArray arrayWithObjects:@"-b",@"GroovesharkDesktop.7F9BF17D6D9CB2159C78A6A6AB076EA0B1E0497C.1", nil]];
[gsdesktop launch];
[gsdesktop release];
}
- (IBAction)startAtLogin:(id)sender {
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
if ([sender state] == NSOnState) {
[self disableLoginItem:loginItems ForPath:bundlePath];
[sender setState:NSOffState];
} else {
[self enableLoginItem:loginItems ForPath:bundlePath];
[sender setState:NSOnState];
}
}
CFRelease(loginItems);
}
- (IBAction)mediaKeys:(id)sender {
if ([mi_mediaKeys state] == NSOffState) {
[self registerMediaKeys:YES];
} else {
[self registerMediaKeys:NO];
}
if ([[NSUserDefaults standardUserDefaults] synchronize]) {
}
}
- (IBAction)globalKeys:(id)sender {
if ([mi_globalKeys state] == NSOffState) {
[self registerHotKeys:YES];
} else {
[self registerHotKeys:NO];
}
if ([[NSUserDefaults standardUserDefaults] synchronize]) {
}
}
- (IBAction)appleRemote:(id)sender {
//Not Yet Created
//But probably simple to support.
//TODO: Terin Stock
}
- (void)printToAPIFile:(NSString *)withAction {
printToAPIFile(withAction);
}
- (IBAction)openLink:(id)sender {
[mi_firstLaunch close];
NSURL *moreInfoURL = [[NSURL alloc] initWithString:@"http://threestrangedays.net/gsdesktophelper"];
[[NSWorkspace sharedWorkspace] openURL:[moreInfoURL absoluteURL]];
[moreInfoURL release];
}
- (IBAction)openPreferencesWindow:(id)sender {
[NSBundle loadNibNamed:@"PreferencePane" owner:self];
}
@end