forked from franrogers/curtains
/
curtains.c
114 lines (101 loc) · 3.35 KB
/
curtains.c
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
#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFBase.h>
#include <ApplicationServices/ApplicationServices.h>
typedef int CGSSessionID;
CG_EXTERN CFDictionaryRef CGSCopyCurrentSessionDictionary(void);
CG_EXTERN CGError
CGSSessionCreateSessionIDWithOptions(CFStringRef path,
CFArrayRef argv,
int flags,
CGSSessionID *outSession);
CG_EXTERN CGError CGSReleaseSession(CGSSessionID session);
CGError lockScreen(CFStringRef lockMessage, CGSSessionID *outSession)
{
CFDictionaryRef dict;
CFNumberRef number;
CGSSessionID currentSession, lockSession;
CFStringRef lockCmd, currentSessionStr;
size_t lockMsgLen;
char *lockMsgC;
CFMutableStringRef lockMsgHex;
CFMutableArrayRef lockCmdArgs;
if (lockMessage == NULL)
lockMessage = CFSTR("");
lockMsgLen = CFStringGetLength(lockMessage);
lockMsgC = malloc(sizeof(char) * (lockMsgLen + 1));
CFStringGetCString(lockMessage,
lockMsgC,
lockMsgLen + 1,
kCFStringEncodingUTF8);
lockMsgHex = CFStringCreateMutable(NULL, lockMsgLen * 2);
for (int i = 0; i < lockMsgLen; i++) {
CFStringAppendFormat(lockMsgHex,
NULL,
CFSTR("%02x"),
lockMsgC[i]);
}
free(lockMsgC);
dict = CGSCopyCurrentSessionDictionary();
number = (CFNumberRef)
CFDictionaryGetValue(dict, CFSTR("kCGSSessionIDKey"));
CFNumberGetValue(number, kCFNumberIntType, ¤tSession);
lockCmd = CFSTR("/System/Library/CoreServices/RemoteManagement"
"/AppleVNCServer.bundle/Contents/Support"
"/LockScreen.app/Contents/MacOS/LockScreen");
lockCmdArgs = CFArrayCreateMutable(NULL, 0, NULL);
CFArrayAppendValue(lockCmdArgs, lockCmd);
CFArrayAppendValue(lockCmdArgs, CFSTR("-session"));
currentSessionStr =
CFStringCreateWithFormat(NULL,
NULL,
CFSTR("%i"),
currentSession);
CFArrayAppendValue(lockCmdArgs, currentSessionStr);
CFArrayAppendValue(lockCmdArgs, CFSTR("-msgHex"));
CFArrayAppendValue(lockCmdArgs, lockMsgHex);
CGSSessionCreateSessionIDWithOptions(lockCmd,
lockCmdArgs,
3,
&lockSession);
if (outSession)
*outSession = lockSession;
CFRelease(currentSessionStr);
CFRelease(lockCmdArgs);
CFRelease(lockCmd);
CFRelease(dict);
CFRelease(lockMsgHex);
return 0;
}
void unlockScreen()
{
CFNotificationCenterRef dc =
CFNotificationCenterGetDistributedCenter();
CFNotificationCenterPostNotificationWithOptions(
dc,
CFSTR("com.apple.remotedesktop.stopLockScreen"),
NULL,
NULL,
kCFNotificationPostToAllSessions
);
}
int main(int argc, char **argv)
{
if (argc == 2 && !strcmp(argv[1], "off")) {
unlockScreen();
} else if (argc == 3 && !strcmp(argv[1], "on")) {
CFStringRef str =
CFStringCreateWithCString(NULL,
argv[2],
kCFStringEncodingUTF8);
lockScreen(str, NULL);
CFRelease(str);
} else if (argc == 2 && !strcmp(argv[1], "on")) {
lockScreen(NULL, NULL);
} else {
printf("usage: %s on [MESSAGE]\n", argv[0]);
printf(" %s off\n", argv[0]);
return 1;
}
return 0;
}