-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.m
80 lines (54 loc) · 1.6 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
//
// AppController.m
// GogoLock
//
// Created by gogo on 7/16/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#include <Carbon/Carbon.h>
#import "AppController.h"
OSStatus globalShortCutForLockingHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
@implementation AppController
- (id)init
{
NSLog(@"AppController::init");
if( ![super init] )
return nil;
[self registerGlobalShortCutForLocking];
return self;
}
- (void)registerGlobalShortCutForLocking
{
NSLog(@"Registering global shortcut to 'lock screen'");
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&globalShortCutForLockingHandler, 1, &eventType, NULL, NULL);
EventHotKeyID g_HotKeyID;
g_HotKeyID.signature = 'htk1';
g_HotKeyID.id = 1;
EventHotKeyRef g_HotKeyRef;
RegisterEventHotKey(124, cmdKey + optionKey, g_HotKeyID, GetApplicationEventTarget(), 0, &g_HotKeyRef);
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
NSLog(@"dock clicked");
lockScreen();
return YES;
}
@end
OSStatus globalShortCutForLockingHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
{
lockScreen();
return noErr;
}
void lockScreen()
{
NSLog(@"Lock screen!");
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"];
NSArray *args = [NSArray arrayWithObject:@"-suspend"];
[task setArguments:args];
[task launch];
[task release];
}