-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAppController.m
928 lines (769 loc) · 26.5 KB
/
AppController.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
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
814
815
816
817
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//
// AppController.m
// Spotify Menubar
//
// Created by Ruaridh Thomson on 06/06/2010.
// Copyright 2010 Life Up North/Ruaridh Thomson. All rights reserved.
//
// This software is distributed under licence. Use of this software
// implies agreement with all terms and conditions of the accompanying
// software licence.
#import "AppController.h"
#import <sys/sysctl.h>
#include "GetPID.h"
#import "MyView.h"
#import <ShortcutRecorder/ShortcutRecorder.h>
#import <SDGlobalShortcuts/SDGlobalShortcuts.h>
typedef struct kinfo_proc kinfo_proc;
@implementation AppController
- (id) init
{
if (![super init])
{
return nil;
}
numberOfProcesses = -1; // means "not initialized"
processList = NULL;
processID = -1; // Means the process doesn't exist
theSource = NULL;
[NSApp setDelegate:self];
queue = [[NSOperationQueue alloc] init];
spotifyChecker = [NSTimer scheduledTimerWithTimeInterval:10.0 target: self selector: @selector(checkIsSpotifyActive) userInfo:nil repeats:YES];
return self;
}
- (void)awakeFromNib
{
// Shortcut Recorders
[playPauseRecorder setCanCaptureGlobalHotKeys:YES];
[skipForwardRecorder setCanCaptureGlobalHotKeys:YES];
[skipBackRecorder setCanCaptureGlobalHotKeys:YES];
NSString *path = [NSString stringWithFormat:@"%@%@%@", @"/Users/", NSUserName(), @"/Library/Preferences/com.lifeupnorth.Spotify-Menubar.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if ( !dict ) {
NSDictionary *ppGH = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:49], @"keyCode", [NSNumber numberWithInt:2560], @"modifiers", nil];
NSDictionary *sfGH = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:124], @"keyCode", [NSNumber numberWithInt:8391168], @"modifiers", nil];
NSDictionary *sbGH = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:123], @"keyCode", [NSNumber numberWithInt:8391168], @"modifiers", nil];
[[NSUserDefaults standardUserDefaults] setObject:ppGH forKey:@"ppGlobalHotkey"];
[[NSUserDefaults standardUserDefaults] setObject:sfGH forKey:@"sfGlobalHotkey"];
[[NSUserDefaults standardUserDefaults] setObject:sbGH forKey:@"sbGlobalHotkey"];
}
SDGlobalShortcutsController *shortcutsController = [SDGlobalShortcutsController sharedShortcutsController];
[shortcutsController addShortcutFromDefaultsKey:@"ppGlobalHotkey"
withControl:playPauseRecorder
target:self
selector:@selector(sendPlayPauseThreaded)];
[shortcutsController addShortcutFromDefaultsKey:@"sfGlobalHotkey"
withControl:skipForwardRecorder
target:self
selector:@selector(sendSkipForwardThreaded)];
[shortcutsController addShortcutFromDefaultsKey:@"sbGlobalHotkey"
withControl:skipBackRecorder
target:self
selector:@selector(sendSkipBackThreaded)];
// Welcome Window
if ([showWelcomeWindow state] == NSOffState) {
[NSApp activateIgnoringOtherApps:YES];
[welcomeWindow makeKeyAndOrderFront:self];
}
[self switchMenubarIconStyle:self];
// Setup the preferences window.
[self loadView:generalView];
//NSLog(@"SM Launched");
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
//NSLog(@"SM Launched");
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
}
- (void)dealloc
{
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
[super dealloc];
}
- (NSOperationQueue *)operationQueue
{
return queue;
}
- (void)checkIsSpotifyActive
{
if (![self isSpotifyActive]) {
if ([[menubarIconStyle selectedItem] tag] == 1) {
[statusItem setImage:[NSImage imageNamed:@"statusOff.png"]];
} else {
[statusItem setImage:[NSImage imageNamed:@"statusOffGrey.png"]];
}
} else {
[statusItem setImage:[NSImage imageNamed:@"statusOn.png"]];
}
}
- (BOOL)isSpotifyActive
{
int procID = GetPIDForProcessName([@"Spotify" UTF8String]);
if (procID == -1) {
return NO;
} else {
return YES;
}
}
#pragma mark -
#pragma mark IBActions
- (IBAction)switchMenubarIconStyle:(id)sender
{
if ([showMenubarIcon state]) {
if ([[menubarIconStyle selectedItem] tag] == 3) {
if (statusItem) {
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
[statusItem release];
statusItem = nil;
}
if ( !playItem )
[self toggleMiniControls:self];
} else {
if ( playItem )
[self toggleMiniControls:self];
//usleep(10000);
if ( !statusItem )
[self setStatusItem];
[self checkIsSpotifyActive];
}
} else {
[statusItem release];
statusItem = nil;
[playItem release];
playItem = nil;
[fwrdItem release];
fwrdItem = nil;
[backItem release];
backItem = nil;
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"runAsUIAgent"]) {
[showMenubarIcon setEnabled:NO];
[showMenubarIcon setState:1];
}
}
- (IBAction)switchPreferenceView:(id)sender
{
[prefToolbar setSelectedItemIdentifier:[sender itemIdentifier]];
if ([sender tag] == 0) {
[self loadView: generalView];
} else if ([sender tag] == 1) {
[self loadView: shortcutsView];
} else if ([sender tag] == 3) {
[self loadView: helpView];
} else if ([sender tag] == 4) {
[self loadView: aboutView];
} else if ([sender tag] == 5) {
[self loadView: simbleView];
}
}
- (void)loadView:(NSView *)theView
{
NSView *tempView = [[NSView alloc] initWithFrame: [[prefWindow contentView] frame]];
[prefWindow setContentView: tempView];
[tempView release];
NSRect newFrame = [prefWindow frame];
newFrame.size.height = [theView frame].size.height + ([prefWindow frame].size.height - [[prefWindow contentView] frame].size.height); // Compensates for toolbar
newFrame.size.width = [theView frame].size.width;
newFrame.origin.y += ([[prefWindow contentView] frame].size.height - [theView frame].size.height); // Origin moves by difference in two views
newFrame.origin.x += ([[prefWindow contentView] frame].size.width - newFrame.size.width)/2; // Origin moves by difference in two views, halved to keep center alignment
[prefWindow setFrame: newFrame display: YES animate: YES];
[prefWindow setContentView: theView];
}
- (IBAction)openAboutWindow:(id)sender
{
[self loadView:aboutView];
[self openPreferences:self];
}
- (IBAction)openPreferences:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[prefWindow makeKeyAndOrderFront:self];
}
- (IBAction)openSpotifyPreferences:(id)sender
{
// Send the keystroke to open preferences
[self pressHotkey:43 withModifier:NSCommandKeyMask];
// Send the keystroke to bring Spotify to the front
[self pressHotkey:18 withModifier:(NSCommandKeyMask | NSAlternateKeyMask)];
}
- (IBAction)resetKeybinds:(id)sender
{
KeyCombo combo1 = { (NSShiftKeyMask | NSAlternateKeyMask), (CGKeyCode)49 };
KeyCombo combo2 = { (NSShiftKeyMask | NSAlternateKeyMask), (CGKeyCode)124 };
KeyCombo combo3 = { (NSShiftKeyMask | NSAlternateKeyMask), (CGKeyCode)123 };
[playPauseRecorder setKeyCombo:combo1];
[skipForwardRecorder setKeyCombo:combo2];
[skipBackRecorder setKeyCombo:combo3];
}
- (IBAction)toggleOpenAtLogin:(id)sender
{
if ([openAtLogin state]) {
[self addAppAsLoginItem];
} else {
[self deleteAppFromLoginItem];
}
}
- (void)toggleMenuForMiniControls
{
[playItem popUpStatusItemMenu:statusMenu];
}
- (IBAction)toggleMiniControls:(id)sender
{
float width = 25.0;
if ( !playItem ) {
fwrdItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
MyView *fwrdView = [MyView new];
fwrdView.image = [NSImage imageNamed:@"skip_forward_up"];
fwrdView.target = self;
fwrdView.action = @selector(sendSkipForwardThreaded);
fwrdView.rightAction = @selector(toggleMenuForMiniControls);
[fwrdItem setView:fwrdView];
[fwrdView release];
playItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
MyView *playView = [MyView new];
playView.image = [NSImage imageNamed:@"play_up"];
playView.target = self;
playView.action = @selector(sendPlayPauseThreaded);
playView.rightAction = @selector(toggleMenuForMiniControls);
[playItem setView:playView];
[playView release];
backItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
MyView *backView = [MyView new];
backView.image = [NSImage imageNamed:@"skip_back_up"];
backView.target = self;
backView.action = @selector(sendSkipBackThreaded);
backView.rightAction = @selector(toggleMenuForMiniControls);
[backItem setView:backView];
[backView release];
} else {
[[NSStatusBar systemStatusBar] removeStatusItem:playItem];
[[NSStatusBar systemStatusBar] removeStatusItem:fwrdItem];
[[NSStatusBar systemStatusBar] removeStatusItem:backItem];
[playItem release];
playItem = nil;
[fwrdItem release];
fwrdItem = nil;
[backItem release];
backItem = nil;
[playItem release];
playItem = nil;
[fwrdItem release];
fwrdItem = nil;
[backItem release];
backItem = nil;
}
}
- (IBAction)checkForProcesses:(id)sender
{
// What are we doing here eh?
}
- (IBAction)findProcessesWithName:(id)sender
{
NSString *processName = [textfield stringValue];
int procID = GetPIDForProcessName([processName UTF8String]);
if (procID == -1) {
NSLog(@"%@ process not found: %i", processName, procID);
} else {
NSLog(@"%@ PID: %i", processName, procID);
processID = procID;
}
}
- (IBAction)sendPlayPause:(id)sender
{
[self pressHotkey:49 withModifier:0];
}
- (IBAction)sendSkipForward:(id)sender
{
[self pressHotkey:124 withModifier:NSCommandKeyMask];
}
- (IBAction)sendSkipBack:(id)sender
{
[self pressHotkey:123 withModifier:NSCommandKeyMask];
}
- (IBAction)sendKeystroke:(id)sender
{
[self pressHotkey:49 withModifier:0];
}
- (IBAction)syncroniseUserDefaults:(id)sender
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (IBAction)openURLLifeUpNorth:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://lifeupnorth.co.uk/lun/"]];
}
- (IBAction)sendLUNemail:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"mailto:[email protected]"]];
}
- (IBAction)openUrlSimbl:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.culater.net/software/SIMBL/SIMBL.php"]];
}
- (IBAction)openUrlLunPlugin:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://lifeupnorth.co.uk/lun/#5"]];
}
#pragma mark -
#pragma mark KeyEvents
- (void)sendPlayPauseHard
{
usleep(200000);
[self pressHotkey:49 withModifier:0];
}
- (void)sendPlayPauseThreaded
{
NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sendPlayPauseHard) object:nil];
[[[NSApp delegate] operationQueue] addOperation:theOp];
}
- (void)sendSkipForwardHard
{
usleep(200000);
[self pressHotkey:124 withModifier:NSCommandKeyMask];
}
- (void)sendSkipForwardThreaded
{
NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sendSkipForwardHard) object:nil];
[[[NSApp delegate] operationQueue] addOperation:theOp];
}
- (void)sendSkipBackHard
{
usleep(200000);
[self pressHotkey:123 withModifier:NSCommandKeyMask];
}
- (void)sendSkipBackThreaded
{
NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sendSkipBackHard) object:nil];
[[[NSApp delegate] operationQueue] addOperation:theOp];
}
// We should not really be using usleep() here. Rather use separate methods
// with [self performSelector: withObject: afterDelay:].
- (void)pressHotkey: (int)hotkey withModifier: (unsigned int)modifier
{
if (![self isSpotifyActive]) {
// If Spotify isn't open, we don't want to try and send it an event.
// If the user wants Spotify to open, we'll do that now.
if ([openSpotifyOnKeybind state]) {
[[NSWorkspace sharedWorkspace] launchApplication:@"Spotify"];
}
return;
}
if((hotkey < 0) || (hotkey > 128)) return;
unsigned int flags = modifier;
ProcessSerialNumber spotPSN = [self getSpotifyProcessSerialNumber];
if(spotPSN.lowLongOfPSN == kNoProcess && spotPSN.highLongOfPSN == kNoProcess) return;
CGEventRef tempEvent = CGEventCreate(NULL);
if(tempEvent) CFRelease(tempEvent);
// create the key down.up
CGEventRef keyDn = NULL, keyUp = NULL;
// create our source
CGEventSourceRef source = [self source];
if (source) {
// KLGetCurrentKeyboardLayout?
// TISCopyCurrentKeyboardLayoutInputSource?
keyDn = CGEventCreateKeyboardEvent(source, (CGKeyCode)hotkey, TRUE);
keyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)hotkey, FALSE);
// set flags for the event (does this even matter? No.)
CGEventSetFlags(keyDn, modifier);
CGEventSetFlags(keyUp, modifier);
// hit any specified modifier keys
if( flags & NSAlternateKeyMask) {
CGEventRef altKeyDn = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Option, TRUE);
if(altKeyDn) {
CGEventPostToPSN(&spotPSN, altKeyDn);
CFRelease(altKeyDn);
usleep(10000);
}
}
if( flags & NSShiftKeyMask) {
CGEventRef sftKeyDn = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Shift, TRUE);
if(sftKeyDn) {
CGEventPostToPSN(&spotPSN, sftKeyDn);
CFRelease(sftKeyDn);
usleep(10000);
}
}
if( flags & NSControlKeyMask) {
CGEventRef ctlKeyDn = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Control, TRUE);
if(ctlKeyDn) {
CGEventPostToPSN(&spotPSN, ctlKeyDn);
CFRelease(ctlKeyDn);
usleep(10000);
}
}
if ( flags & NSCommandKeyMask) {
CGEventRef cmdKeyDn = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Command, TRUE);
if(cmdKeyDn) {
CGEventPostToPSN(&spotPSN, cmdKeyDn);
CFRelease(cmdKeyDn);
usleep(10000);
}
}
// post the actual event
CGEventPostToPSN(&spotPSN, keyDn);
usleep(30000);
CGEventPostToPSN(&spotPSN, keyUp);
usleep(10000);
// undo the modifier keys
if( flags & NSControlKeyMask) {
CGEventRef ctlKeyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Control, FALSE);
if(ctlKeyUp) {
CGEventPostToPSN(&spotPSN, ctlKeyUp);
CFRelease(ctlKeyUp);
usleep(10000);
}
}
if( flags & NSShiftKeyMask) {
CGEventRef sftKeyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Shift, false);
if(sftKeyUp) {
CGEventPostToPSN(&spotPSN, sftKeyUp);
CFRelease(sftKeyUp);
usleep(10000);
}
}
if( flags & NSAlternateKeyMask) {
CGEventRef altKeyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Option, FALSE);
if(altKeyUp) {
CGEventPostToPSN(&spotPSN, altKeyUp);
CFRelease(altKeyUp);
usleep(10000);
}
}
if( flags & NSCommandKeyMask) {
CGEventRef cmdKeyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Command, FALSE);
if(cmdKeyUp) {
CGEventPostToPSN(&spotPSN, cmdKeyUp);
CFRelease(cmdKeyUp);
usleep(10000);
}
}
if(keyDn) CFRelease(keyDn);
if(keyUp) CFRelease(keyUp);
} else {
// We can't post the event. Notify the user?
}
}
- (CGEventSourceRef)source
{
if(theSource == NULL) {
theSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
if(theSource) {
CGEventSourceSetKeyboardType(theSource, kCGAnyInputEventType);
CGEventSourceSetLocalEventsSuppressionInterval(theSource, 0.0);
CGEventSourceSetLocalEventsFilterDuringSuppressionState(theSource, kCGEventFilterMaskPermitLocalMouseEvents, kCGEventSuppressionStateSuppressionInterval);
}
}
return theSource;
}
#pragma mark -
#pragma mark Process Methods
- (int)numberOfProcesses
{
return numberOfProcesses;
}
- (void)setNumberOfProcesses:(int)num
{
numberOfProcesses = num;
}
- (int)getBSDProcessList:(kinfo_proc **)procList
withNumberOfProcesses:(size_t *)procCount
{
int err;
kinfo_proc * result;
bool done;
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t length;
// a valid pointer procList holder should be passed
assert( procList != NULL );
// But it should not be pre-allocated
assert( *procList == NULL );
// a valid pointer to procCount should be passed
assert( procCount != NULL );
*procCount = 0;
result = NULL;
done = false;
do
{
assert( result == NULL );
// Call sysctl with a NULL buffer to get proper length
length = 0;
err = sysctl((int *)name,(sizeof(name)/sizeof(*name))-1,NULL,&length,NULL,0);
if( err == -1 )
err = errno;
// Now, proper length is optained
if( err == 0 )
{
result = malloc(length);
if( result == NULL )
err = ENOMEM; // not allocated
}
if( err == 0 )
{
err = sysctl( (int *)name, (sizeof(name)/sizeof(*name))-1, result, &length, NULL, 0);
if( err == -1 )
err = errno;
if( err == 0 )
done = true;
else if( err == ENOMEM )
{
assert( result != NULL );
free( result );
result = NULL;
err = 0;
}
}
}while ( err == 0 && !done );
// Clean up and establish post condition
if( err != 0 && result != NULL )
{
free(result);
result = NULL;
}
*procList = result; // will return the result as procList
if( err == 0 )
*procCount = length / sizeof( kinfo_proc );
assert( (err == 0) == (*procList != NULL ) );
return err;
}
- (void)obtainFreshProcessList
{
int i;
kinfo_proc *allProcs = 0;
size_t numProcs;
NSString *procName;
NSString *procID;
int err = [self getBSDProcessList:&allProcs withNumberOfProcesses:&numProcs];
if( err )
{
numberOfProcesses = -1;
processList = NULL;
return;
}
// Construct an array for ( process name )
processList = [NSMutableArray arrayWithCapacity:numProcs];
NSLog(@"Number of processes: %i", numProcs);
for( i = 0; i < numProcs; i++ )
{
procName = [NSString stringWithFormat:@"%s", allProcs[i].kp_proc.p_comm];
procID = [NSString stringWithFormat:@"%s", allProcs[i].kp_proc.p_pid];
[processList addObject:procName];
NSLog(@"Process %i : %@", i, procName);
NSLog(@"With PID: %@", procID);
NSLog(@"----------------------------------------");
}
[self setNumberOfProcesses:numProcs];
free( allProcs );
}
- (BOOL)findProcessWithName:(NSString *)procNameToSearch
{
int index;
index = [processList indexOfObject:procNameToSearch];
if( index == -1 ) {
NSLog(@"Process not found");
return NO;
} else {
NSLog(@"Process found");
return YES;
}
}
- (ProcessSerialNumber)getSpotifyProcessSerialNumber
{
ProcessSerialNumber pSN = {kNoProcess, kNoProcess};
pid_t spotPID = 0;
for(NSDictionary *processDict in [[NSWorkspace sharedWorkspace] launchedApplications]) {
if( [[processDict objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: @"com.spotify.client"] ) {
pSN.highLongOfPSN = [[processDict objectForKey: @"NSApplicationProcessSerialNumberHigh"] longValue];
pSN.lowLongOfPSN = [[processDict objectForKey: @"NSApplicationProcessSerialNumberLow"] longValue];
OSStatus err = GetProcessPID(&pSN, &spotPID);
_lastAttachedPID = spotPID;
if( err == noErr && spotPID > 0 && spotPID == _selectedPID) {
return pSN;
}
}
}
return pSN;
}
// Left in as a source of information
/*
#pragma mark -
#pragma mark Alternate Methods
static int GetBSDProcessList(kinfo_proc **procList, size_t *procCount)
// Returns a list of all BSD processes on the system. This routine
// allocates the list and puts it in *procList and a count of the
// number of entries in *procCount. You are responsible for freeing
// this list (use "free" from System framework).
// On success, the function returns 0.
// On error, the function returns a BSD errno value.
{
int err;
kinfo_proc * result;
bool done;
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
// Declaring name as const requires us to cast it when passing it to
// sysctl because the prototype doesn't include the const modifier.
size_t length;
assert( procList != NULL);
assert(*procList == NULL);
assert(procCount != NULL);
*procCount = 0;
// We start by calling sysctl with result == NULL and length == 0.
// That will succeed, and set length to the appropriate length.
// We then allocate a buffer of that size and call sysctl again
// with that buffer. If that succeeds, we're done. If that fails
// with ENOMEM, we have to throw away our buffer and loop. Note
// that the loop causes use to call sysctl with NULL again; this
// is necessary because the ENOMEM failure case sets length to
// the amount of data returned, not the amount of data that
// could have been returned.
result = NULL;
done = false;
do {
assert(result == NULL);
// Call sysctl with a NULL buffer.
length = 0;
err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1,
NULL, &length,
NULL, 0);
if (err == -1) {
err = errno;
}
// Allocate an appropriately sized buffer based on the results
// from the previous call.
if (err == 0) {
result = malloc(length);
if (result == NULL) {
err = ENOMEM;
}
}
// Call sysctl again with the new buffer. If we get an ENOMEM
// error, toss away our buffer and start again.
if (err == 0) {
err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1,
result, &length,
NULL, 0);
if (err == -1) {
err = errno;
}
if (err == 0) {
done = true;
} else if (err == ENOMEM) {
assert(result != NULL);
free(result);
result = NULL;
err = 0;
}
}
} while (err == 0 && ! done);
// Clean up and establish post conditions.
if (err != 0 && result != NULL) {
free(result);
result = NULL;
}
*procList = result;
if (err == 0) {
*procCount = length / sizeof(kinfo_proc);
}
assert( (err == 0) == (*procList != NULL) );
return err;
}
*/
#pragma mark -
#pragma mark Login Methods
-(void) addAppAsLoginItem
{
NSString * appPath = [[NSBundle mainBundle] bundlePath];
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath];
// Create a reference to the shared file list.
// We are adding it to the current user only.
// If we want to add it all users, use
// kLSSharedFileListGlobalLoginItems instead of
//kLSSharedFileListSessionLoginItems
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,
kLSSharedFileListItemLast, NULL, NULL,
url, NULL, NULL);
if (item){
CFRelease(item);
}
}
CFRelease(loginItems);
}
-(void) deleteAppFromLoginItem
{
NSString * appPath = [[NSBundle mainBundle] bundlePath];
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath];
// Create a reference to the shared file list.
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seedValue;
//Retrieve the list of Login Items and cast them to
// a NSArray so that it will be easier to iterate.
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
int i = 0;
for(i ; i< [loginItemsArray count]; i++){
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)[loginItemsArray
objectAtIndex:i];
//Resolve the item with URL
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
LSSharedFileListItemRemove(loginItems,itemRef);
}
}
}
[loginItemsArray release];
}
}
#pragma mark -
#pragma mark Preferences
- (void)setStatusItem
{
// Create an NSStatusItem.
float width = 25.0;
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
[statusItem setMenu:statusMenu];
[statusItem setImage:[NSImage imageNamed:@"statusOffGrey.png"]];
[statusItem setAlternateImage:[NSImage imageNamed:@"statusHighlight.png"]];
[statusItem setHighlightMode:YES];
}
- (IBAction)setApplicationIsAgent:(id)sender
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"runAsUIAgent"]) {
[self setShouldBeUIElement:NO];
[showMenubarIcon setEnabled:YES];
} else {
[self setShouldBeUIElement:YES];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showMenubarIcon"];
[showMenubarIcon setEnabled:NO];
[showMenubarIcon setState:1];
[self switchMenubarIconStyle:self];
}
}
- (BOOL)shouldBeUIElement
{
return [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"LSUIElement"] boolValue];
}
- (void)setShouldBeUIElement:(BOOL)hidden
{
NSString * plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Info.plist"]) {
if ([manager isWritableFileAtPath:plistPath]) {
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"LSUIElement"];
[infoDict writeToFile:plistPath atomically:NO];
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
}
}
}
@end