This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
OverlayWindow.m
1055 lines (830 loc) · 29.6 KB
/
OverlayWindow.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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OverlayWindow.m
*
* Copyright 2009 SwiftRing. All rights reserved.
*
* Description:
* - Traps hotkeys and mouse events.
* - Brings up / down the main window.
* - Creates / destroys Ring objects.
* - Deals with application-wide configuration stuff.
*/
#import "OverlayWindow.h"
#import <Carbon/Carbon.h>
#import <ApplicationServices/ApplicationServices.h>
#import "RingView.h"
#import "Debug.h"
#import "RecordingPanel.h"
#import "RingFactory.h"
#import "Ring.h"
#define FADE_IN_TIMER 0.6
#define DISMISS_TIMER 0.01
#define SCROLL_QUIESCE 300
// A bunch of defines to handle hotkeys
const UInt32 kMyHotKeyIdentifier = 'ring';
const UInt32 kMyHotKey = 50; //the ` key
EventHotKeyRef gMyHotKeyRef;
EventHotKeyID gMyHotKeyID;
EventHandlerUPP gAppHotKeyFunction;
// Event tap variables
CFRunLoopSourceRef gRunLoopSource;
CFMachPortRef gEventTap;
// Global for use in the event tap callback (not a method of OverlayWindow)
OverlayWindow *gpOverlayWin;
BOOL sentFake = NO;
int launchMethod = 0;
BOOL allowArrows = NO;
float menuDelay = FADE_IN_TIMER;
unsigned int arrowStickyBitfield = 0;
NSTimeInterval timeSinceLastScroll = 0;
// This routine is called when the command-return hotkey is pressed.
pascal OSStatus HotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
{
[gpOverlayWin toggleDisabled];
if ([gpOverlayWin isDisabled])
{
[gpOverlayWin displayMessage: @"SwiftRing is disabled."];
}
else
{
[gpOverlayWin displayMessage: @"SwiftRing is enabled."];
}
return noErr;
}
BOOL TapDidLaunchMenu(CGEventType type, CGEventRef event)
{
switch (launchMethod)
{
case 0:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskAlternate & CGEventGetFlags(event));
break;
case 1:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskCommand & CGEventGetFlags(event));
break;
case 2:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskControl & CGEventGetFlags(event));
break;
case 3:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskSecondaryFn & CGEventGetFlags(event));
break;
case 4:
return (kCGEventOtherMouseDown == type);
break;
case 5:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskAlternate & CGEventGetFlags(event)) &&
(kCGEventFlagMaskShift & CGEventGetFlags(event));
break;
case 6:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskCommand & CGEventGetFlags(event)) &&
(kCGEventFlagMaskShift & CGEventGetFlags(event));
break;
case 7:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskSecondaryFn & CGEventGetFlags(event)) &&
(kCGEventFlagMaskShift & CGEventGetFlags(event));
break;
case 8:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskAlternate & CGEventGetFlags(event)) &&
(kCGEventFlagMaskCommand & CGEventGetFlags(event));
break;
case 9:
return (kCGEventFlagsChanged == type) &&
(kCGEventFlagMaskControl & CGEventGetFlags(event)) &&
(kCGEventFlagMaskAlternate & CGEventGetFlags(event));
break;
}
return NO;
}
BOOL TapDidCloseMenu(CGEventType type, CGEventRef event)
{
switch (launchMethod)
{
case 0:
return (kCGEventFlagsChanged == type) &&
((kCGEventFlagMaskAlternate & CGEventGetFlags(event)) == 0);
break;
case 1:
return (kCGEventFlagsChanged == type) &&
((kCGEventFlagMaskCommand & CGEventGetFlags(event)) == 0);
break;
case 2:
return (kCGEventFlagsChanged == type) &&
((kCGEventFlagMaskControl & CGEventGetFlags(event)) == 0);
break;
case 3:
return (kCGEventFlagsChanged == type) &&
((kCGEventFlagMaskSecondaryFn & CGEventGetFlags(event)) == 0);
break;
case 4:
return (kCGEventOtherMouseUp == type);
break;
case 5:
return (kCGEventFlagsChanged == type) &&
(((kCGEventFlagMaskAlternate & CGEventGetFlags(event)) == 0 ) ||
((kCGEventFlagMaskShift & CGEventGetFlags(event)) == 0 ));
break;
case 6:
return (kCGEventFlagsChanged == type) &&
(((kCGEventFlagMaskCommand & CGEventGetFlags(event)) == 0 ) ||
((kCGEventFlagMaskShift & CGEventGetFlags(event)) == 0 ));
break;
case 7:
return (kCGEventFlagsChanged == type) &&
(((kCGEventFlagMaskSecondaryFn & CGEventGetFlags(event)) == 0 ) ||
((kCGEventFlagMaskShift & CGEventGetFlags(event)) == 0 ));
break;
case 8:
return (kCGEventFlagsChanged == type) &&
(((kCGEventFlagMaskAlternate & CGEventGetFlags(event)) == 0 ) ||
((kCGEventFlagMaskCommand & CGEventGetFlags(event)) == 0 ));
break;
case 9:
return (kCGEventFlagsChanged == type) &&
(((kCGEventFlagMaskControl & CGEventGetFlags(event)) == 0 ) ||
((kCGEventFlagMaskAlternate & CGEventGetFlags(event)) == 0 ));
break;
}
return NO;
}
CGEventRef TapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
NSPoint mouseLocation;
//CGEventRef simMouseRef;
BOOL eatEvent = NO;
DebugLog(@"Type = %i, Flags = %x ignore = %i", type, CGEventGetFlags(event), [gpOverlayWin getIgnoreKeyEvents]);
// Check first for any disable events
if (kCGEventTapDisabledByTimeout == type ||
kCGEventTapDisabledByUserInput == type)
{
if (kCGEventTapDisabledByTimeout == type)
{
DebugLog(@"Got kCGEventTapDisabledByTimeout");
}
else
{
DebugLog(@"Got kCGEventTapDisabledByUserInput");
}
if (gEventTap)
{
if (!CGEventTapIsEnabled(gEventTap))
{
DebugLog(@"Re-enabling event tap");
CGEventTapEnable(gEventTap, true);
}
}
}
// Disabled - do nothing, skip all handling
if ([gpOverlayWin isDisabled])
{
DebugLog(@"Disabled, skipped processing");
}
// Need to ignore this event if it is a keyboard one
else if ([gpOverlayWin getIgnoreKeyEvents])
{
if (kCGEventKeyUp == type ||
kCGEventKeyDown == type ||
kCGEventFlagsChanged == type)
{
[gpOverlayWin setIgnoreKeyEvents: [gpOverlayWin getIgnoreKeyEvents] - 1];
DebugLog(@"Ignored an action");
}
}
/*
else if (sentFake && (kCGEventRightMouseDown == type))
{
DebugLog(@"Skipping processing the fake mouse down event");
sentFake = NO;
}
*/
// Not in Quasimode handling
else if (![gpOverlayWin inQuasimode])
{
if ([gpOverlayWin isRecording])
{
// Trying to record keystrokes from the recording panel
if (kCGEventKeyUp == type ||
kCGEventKeyDown == type ||
kCGEventFlagsChanged == type)
{
// Ok, these are actual keys - pass them to the recording panel, but eat them so they aren't passed thru
[gpOverlayWin processRecordingKeys: event ofType: type];
eatEvent = YES;
}
}
else if (TapDidLaunchMenu(type, event))
{
// If key goes down, enter quasimode and eat the event
//DebugLog(@"Right Mouse down, not in quasimode! sentFake = %i didSomething = %i", sentFake, [gpOverlayWin didSomething]);
DebugLog(@"Key down, not in quasimode! ignoreEvents = %i didSomething = %i", [gpOverlayWin getIgnoreKeyEvents],
[gpOverlayWin didSomething]);
[gpOverlayWin enterRingQuasimode];
arrowStickyBitfield = 0;
timeSinceLastScroll = 0;
//eatEvent = YES;
}
}
// In Quasimode handling
else
{
// Localize mouse location to this window
mouseLocation = NSPointFromCGPoint(CGEventGetUnflippedLocation(event));
mouseLocation.x -= [gpOverlayWin frame].origin.x;
mouseLocation.y -= [gpOverlayWin frame].origin.y;
if (TapDidCloseMenu(type, event))
{
DebugLog(@"Key up, in quasimode! ignoreEvents = %i didSomething = %i", [gpOverlayWin getIgnoreKeyEvents],
[gpOverlayWin didSomething]);
/*
// If the user didn't do anything, simulate a normal right click and release
if (![gpOverlayWin didSomething])
{
DebugLog(@"Sending Fake mouse down!");
simMouseRef = CGEventCreateMouseEvent(NULL, kCGEventRightMouseDown, CGEventGetLocation(event), kCGMouseButtonRight);
CGEventPost(kCGSessionEventTap, simMouseRef);
CFRelease(simMouseRef);
simMouseRef = CGEventCreateMouseEvent(NULL, kCGEventRightMouseUp, CGEventGetLocation(event), kCGMouseButtonRight);
CGEventPost(kCGSessionEventTap, simMouseRef);
CFRelease(simMouseRef);
sentFake = YES;
eatEvent = YES;
}
*/
[gpOverlayWin exitRingQuasimode];
}
else if (kCGEventKeyUp == type || kCGEventKeyDown == type)
{
unsigned short keyCode;
keyCode = (unsigned short) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
DebugLog(@"Key pressed: %i", keyCode);
// Check for arrow keys
if (allowArrows && (keyCode >= 123) && (keyCode <= 126))
{
if (kCGEventKeyUp == type)
{
// Send the sticky arrow bitfield off to detect for hits
DebugLog(@"Sending the arrow bitfield: %x", arrowStickyBitfield);
[gpOverlayWin detectHit: mouseLocation: NO: NO: arrowStickyBitfield];
arrowStickyBitfield = 0;
}
else if (kCGEventKeyDown)
{
// Accumulate arrow combos into the bitfield
arrowStickyBitfield |= 1 << (keyCode - 123);
}
eatEvent = YES;
}
else
{
// If non-arrow keys are pressed while in quasi-mode, stall the menu display
[gpOverlayWin restartDisplayTimer: YES];
}
}
else if (kCGEventMouseMoved == type || kCGEventOtherMouseDragged == type)
{
[gpOverlayWin detectHit: mouseLocation: NO: NO: 0];
}
else if (kCGEventScrollWheel == type)
{
DebugLog(@"Scroll detected");
if ((([NSDate timeIntervalSinceReferenceDate] * 1000) - timeSinceLastScroll) > SCROLL_QUIESCE)
{
timeSinceLastScroll = [NSDate timeIntervalSinceReferenceDate] * 1000;
if (CGEventGetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1) > 0)
{
// Mouse wheel scrolled up
[gpOverlayWin detectHit: mouseLocation: YES: NO: 0];
}
else
{
// Mouse wheel scrolled down
[gpOverlayWin detectHit: mouseLocation: NO: YES: 0];
}
}
// Eat scroll wheel operations
eatEvent = YES;
//
// All flags are normally cleared when executing actions - this would exit us
// out of quasimode - we dont want this behavior for scroll operations!
// 59 is the code for 'option' / 'alternate'
//
// CGEventRef event = CGEventCreateKeyboardEvent(0, 59, TRUE);
// CGEventSetFlags(event, kCGEventFlagMaskAlternate);
// CGEventPost(kCGSessionEventTap, event);
// CFRelease(event);
}
}
if (eatEvent)
{
return NULL;
}
return event;
}
@implementation OverlayWindow
// We override this initializer so we can set the NSBorderlessWindowMask styleMask, and set a few other important settings
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
{
if (self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:backingType defer:flag])
{
[self setOpaque:NO]; // Needed so we can see through it when we have clear stuff on top
[self setHasShadow: YES];
[self setLevel:NSScreenSaverWindowLevel]; // Let's make it sit on top of everything else
[self setBackgroundColor:[NSColor clearColor]]; // Only show the stuff on top, not the window itself
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[self setAlphaValue:0.0];
}
gpOverlayWin = self;
return self;
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
// Show preferences if the app is run twice
[self preferences: nil];
DebugLog(@"Relaunch Detected!");
return NO;
}
- (void)awakeFromNib
{
CGEventMask eventMask;
inQuasimode = NO;
isDisabled = NO;
ignoreKeyEvents = 0;
didSomethingCount = 0;
//
// Setup the hotkey handler, using Carbon APIs (there is no ObjC Cocoa HotKey API as of 10.2.x)
//
/*
EventTypeSpec eventType;
gAppHotKeyFunction = NewEventHandlerUPP(HotKeyHandler);
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(gAppHotKeyFunction,1,&eventType,NULL,NULL);
gMyHotKeyID.signature = kMyHotKeyIdentifier;
gMyHotKeyID.id = 1;
RegisterEventHotKey(kMyHotKey, cmdKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
*/
//
// Tap into mouse events.
//
// Create an event tap.
eventMask = CGEventMaskBit(kCGEventOtherMouseDown) |
CGEventMaskBit(kCGEventOtherMouseUp) |
CGEventMaskBit(kCGEventOtherMouseDragged) |
CGEventMaskBit(kCGEventMouseMoved) |
CGEventMaskBit(kCGEventScrollWheel) |
CGEventMaskBit(kCGEventFlagsChanged) |
CGEventMaskBit(kCGEventKeyDown) |
CGEventMaskBit(kCGEventKeyUp);
DebugLog(@"Event before: %x", eventMask);
gEventTap = CGEventTapCreate(/*kCGHIDEventTap*/kCGSessionEventTap,
kCGHeadInsertEventTap, kCGEventTapOptionDefault, eventMask, TapCallback, self);
DebugLog(@"Event after: %x", eventMask);
if (!gEventTap) {
fprintf(stderr, "Failed to create event tap\n");
exit(1);
}
// Create a run loop source.
gRunLoopSource = CFMachPortCreateRunLoopSource(/*kCFAllocatorDefault*/NULL, gEventTap, 0);
//CFRelease(eventTap);
// Add to the current run loop.
CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop], gRunLoopSource, kCFRunLoopCommonModes);
// Enable the event tap.
CGEventTapEnable(gEventTap, true);
//CFRelease(runLoopSource);
}
// Windows created with NSBorderlessWindowMask normally can't be key, but we want ours to be
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (void) dealloc
{
[super dealloc];
}
- (void) detectHit:(NSPoint) location: (BOOL) scrollUp: (BOOL) scrollDown: (unsigned int) arrowBitfield
{
RingActionStatus status;
// Tell the Ring to do hit detection.
status = [ringView detectHit: location: scrollUp: scrollDown: arrowBitfield: [self alphaValue] > 0];
// If the hit was detected we want to mark didSomething true (sticky) and move the
// window to the mouse location just in case this is a sub-menu
if (status > RING_ACTION_NONE)
{
didSomething = YES;
// If we're done, get out of quasimode
if (RING_ACTION_DONE == status)
{
[self exitRingQuasimode];
}
// If we're not done, refresh the view and reset the display timer
else if (RING_ACTION_REFRESH == status)
{
[self restartDisplayTimer: YES];
}
else if (RING_ACTION_STAY == status)
{
if (0 == [self alphaValue])
{
// Window is not visible, keep it that way
[self restartDisplayTimer: YES];
}
}
[self centerWindowOnMouse];
}
else if ([self alphaValue] == 0)
{
// If not currently displaying anything, restart the display timer
[self restartDisplayTimer: NO];
}
DebugLog(@"detectHit %f %f", location.x, location.y);
}
// Need to pull up the Ring and make the window visible.
- (void)enterRingQuasimode
{
if (didSomething)
{
didSomethingCount++;
if ((NO == validKey) && (didSomethingCount >= 6))
{
[self displayMessage:@"Buy SwiftRing for only $5!"];
didSomethingCount = 0;
return;
}
}
didSomething = NO;
inQuasimode = YES;
// Create a new ring
[ringView createRing: NO];
[self centerWindowOnMouse];
// Invalidate any outstanding timers
[pDisplayTimer invalidate];
pDisplayTimer = nil;
[pDismissTimer invalidate];
pDismissTimer = nil;
DebugLog(@"Making timer in enterRingQuasimode");
pDisplayTimer = [NSTimer scheduledTimerWithTimeInterval:menuDelay
target:self
selector:@selector(displayRing:)
userInfo:nil
repeats:NO];
// This is also done in dismissRing in case there was a display timer thread running already.
[self setAlphaValue:0.0];
DebugLog(@"enteredRingQuasimode %i", [self acceptsMouseMovedEvents]);
}
// Need to execute any outstanding actions, destroy the Ring and make the window invisible
- (void) exitRingQuasimode
{
inQuasimode = NO;
// Invalidate any outstanding timers
[pDisplayTimer invalidate];
pDisplayTimer = nil;
[pDismissTimer invalidate];
pDismissTimer = nil;
DebugLog(@"Making timer in exitRingQuasimode");
pDismissTimer = [NSTimer scheduledTimerWithTimeInterval:DISMISS_TIMER
target:self
selector:@selector(dismissRing:)
userInfo:nil
repeats:NO];
// This is also done in dismissRing in case there was a display timer thread running already.
[self setAlphaValue:0.0];
DebugLog(@"exitRingQuasiMode %i", [self acceptsMouseMovedEvents]);
}
- (void) centerWindowOnMouse
{
NSPoint windowLoc = [NSEvent mouseLocation];
// Put the main window at the center of the mouse
windowLoc.x -= [self frame].size.width / 2;
windowLoc.y -= [self frame].size.height / 2;
[self setFrameOrigin:windowLoc];
}
- (void) restartDisplayTimer: (bool) clearDisplay
{
if (clearDisplay)
{
// First stop displaying
[self setAlphaValue:0.0];
}
// Invalidate any outstanding timer
[pDisplayTimer invalidate];
pDisplayTimer = nil;
DebugLog(@"Making timer in restartDisplayTimer");
pDisplayTimer = [NSTimer scheduledTimerWithTimeInterval:menuDelay
target:self
selector:@selector(displayRing:)
userInfo:nil
repeats:NO];
}
- (void) displayRing: (NSTimer*) timer
{
DebugLog(@"displayRing called");
// Window is faded in after some time, start out invisible
[self setAlphaValue:0.0];
// Reset the timer pointer as it is no longer valid
pDisplayTimer = nil;
[self fadeIn];
}
- (void) dismissRing: (NSTimer *) time
{
DebugLog(@"dismissRing called");
// Make the window invisible while the ring is destroyed to avoid artifacts
[self setAlphaValue:0.0];
// Reset the timer pointer as it is no longer valid
pDismissTimer = nil;
// Destroy the ring
[ringView destroyRing];
}
- (void) fadeIn
{
// If user waited long enough for the window to pop up, they 'did something'
didSomething = YES;
// Reset any segmet hits that may have happened before the ring became visible
[ringView resetHits];
[[self animator] setAlphaValue:1.0];
[self centerWindowOnMouse];
}
- (void) fadeOut
{
[[self animator] setAlphaValue:0.0];
}
- (void) trueCenter
{
NSRect frame = [self frame];
NSRect screen = [[self screen] frame];
frame.origin.x = (screen.size.width - frame.size. width) / 2;
frame.origin.y = (screen.size.height - frame.size.height) / 2;
[self setFrameOrigin: frame.origin];
}
- (bool) isRunOnStartup
{
UInt32 seedValue;
bool isRunOnStartup = NO;
CFURLRef thePath = (CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
// and pop it in an array so we can iterate through it to find our item.
NSArray *loginItemsArray = (NSArray *) LSSharedFileListCopySnapshot(loginItems, &seedValue);
for (id item in loginItemsArray)
{
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr)
{
if ([[(NSURL *)thePath path] hasPrefix:[[NSBundle mainBundle] bundlePath]])
{
isRunOnStartup = YES;
}
}
}
[loginItemsArray release];
// Update the menuitem state
if (isRunOnStartup)
{
[runOnStartupMenuItem setState: NSOnState];
}
else
{
[runOnStartupMenuItem setState: NSOffState];
}
return isRunOnStartup;
}
- (void) setLaunchKey: (int) newKey
{
static BOOL startup = YES;
NSString *keyString = NULL;
launchMethod = newKey;
switch (newKey)
{
case 0:
keyString = [NSString stringWithFormat:@" the 'Option' key."];
break;
case 1:
keyString = [NSString stringWithFormat:@" the 'Command' key."];
break;
case 2:
keyString = [NSString stringWithFormat:@" the 'Control' key."];
break;
case 3:
keyString = [NSString stringWithFormat:@" the 'Function' key."];
break;
case 4:
keyString = [NSString stringWithFormat:@" the 'other' Mouse button."];
break;
case 5:
keyString = [NSString stringWithFormat:@" the 'Shift + Option' keys."];
break;
case 6:
keyString = [NSString stringWithFormat:@" the 'Shift + Command' keys."];
break;
case 7:
keyString = [NSString stringWithFormat:@" the 'Shift + Function' keys."];
break;
case 8:
keyString = [NSString stringWithFormat:@"the 'Option + Command' keys."];
break;
case 9:
keyString = [NSString stringWithFormat:@" the 'Control + Option' keys."];
break;
}
//
// Check if SwiftRing is launching automatically on startup
//
if (startup && ![self isRunOnStartup])
{
startup = NO;
if (AXAPIEnabled())
{
// Not running on startup, show the welcome message
[self displayMessage: [NSString stringWithFormat: @"To start SwiftRing hold down\n%@", keyString]];
}
else
{
[self displayMessage: @" Welcome to SwiftRing!\nYou must enable access for assistive devices\n in System Preferences -> Universal Access."];
}
}
}
- (void) setAllowArrows: (BOOL) newAllowArrows
{
allowArrows = newAllowArrows;
}
- (void) setMenuDelay: (float) newDelay
{
DebugLog(@"Menu Delay = %f", newDelay);
menuDelay = newDelay;
}
- (void) setEnableMenuBar: (BOOL) enable
{
DebugLog(@"Before: Enable %i, statusBarItem %i", enable, statusBarItem);
if (enable)
{
if (nil == statusBarItem)
{
//Create the NSStatusBar and set its length
statusBarItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
[statusBarItem retain];
//Used to detect where our files are
NSBundle *bundle = [NSBundle mainBundle];
//Allocates and loads the images into the application which will be used for our NSStatusItem
statusBarImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"SwiftRingStatusIcon" ofType:@"png"]];
statusBarHiImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"SwiftRingStatusIcon" ofType:@"png"]];
//Sets the images in our NSStatusItem
[statusBarItem setImage:statusBarImage];
[statusBarItem setAlternateImage:statusBarHiImage];
//Tells the NSStatusItem what menu to load
[statusBarItem setMenu:statusBarMenu];
//Sets the tooptip for our item
[statusBarItem setToolTip:@"SwiftRing"];
//Enables highlighting
[statusBarItem setHighlightMode:YES];
}
}
else
{
if (nil != statusBarItem)
{
// Nuke the menu icon
[[NSStatusBar systemStatusBar] removeStatusItem: statusBarItem];
[statusBarItem release];
[statusBarImage release];
[statusBarHiImage release];
statusBarItem = nil;
statusBarImage = nil;
statusBarHiImage = nil;
}
}
DebugLog(@"After: Enable %i, statusBarItem %i", enable, statusBarItem);
}
- (void) setValidKey: (BOOL) valid
{
DebugLog(@"Valid Key = %i", valid);
validKey = valid;
}
// Returns value of inQuasimode
- (bool) inQuasimode
{
return inQuasimode;
}
// Returns value of didSomething
- (bool) didSomething
{
return didSomething;
}
// Sets ignoreEvents
- (void) setIgnoreKeyEvents: (int) value
{
//DebugLog(@"Ignore: %i", value);
ignoreKeyEvents = value;
}
// Returns value of ignoreEvents
- (int) getIgnoreKeyEvents
{
return ignoreKeyEvents;
}
// Returns value of isDisabled
- (bool) isDisabled
{
return isDisabled;
}
// Returns the recording panel's isRecording
- (bool) isRecording
{
return [recordingPanel isRecording];
}
- (void) processRecordingKeys: (CGEventRef) event ofType: (CGEventType) type
{
[recordingPanel processRecordingKeys: event ofType: type];
}
- (void) toggleDisabled
{
isDisabled = ~isDisabled;
}
- (void) displayMessage: (NSString *) pMessageString
{
[ringView displayMessage: pMessageString];
}
- (IBAction) about: (id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[aboutWindow center];
[aboutWindow makeKeyAndOrderFront: nil];
}
- (IBAction) aboutWebpage: (id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.swiftringapp.com"]];
}
- (IBAction) help: (id) sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.swiftringapp.com"]];
}
- (IBAction) preferences: (id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[preferencesPanel center];
[preferencesPanel makeKeyAndOrderFront: nil];
}
- (IBAction) disable: (id) sender
{
[self toggleDisabled];
if ([self isDisabled])
{
[sender setState: NSOnState];
[gpOverlayWin displayMessage: @"SwiftRing is disabled."];
}
else
{
[sender setState: NSOffState];
[gpOverlayWin displayMessage: @"SwiftRing is enabled."];
}
}
- (IBAction) runOnStartup: (id) sender
{