Skip to content

Commit 5e08884

Browse files
committed
maintenance: add clang-format support
1 parent 7793065 commit 5e08884

3 files changed

Lines changed: 123 additions & 88 deletions

File tree

.clang-format

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
BasedOnStyle: LLVM
2+
3+
IndentWidth: 4
4+
UseTab: Never
5+
6+
ColumnLimit: 80
7+
8+
BreakBeforeBraces: Custom
9+
10+
BraceWrapping:
11+
AfterFunction: true
12+
AfterControlStatement: false
13+
AfterStruct: false
14+
AfterUnion: false
15+
16+
PointerAlignment: Right
17+
DerivePointerAlignment: false
18+
19+
AlignConsecutiveDeclarations:
20+
Enabled: true
21+
22+
AlignConsecutiveAssignments:
23+
Enabled: true
24+
25+
AlignConsecutiveMacros:
26+
Enabled: true
27+
28+
SpaceBeforeParens: ControlStatements
29+
SpaceAfterCStyleCast: true
30+
31+
PenaltyExcessCharacter: 2
32+
PenaltyBreakBeforeFirstCallParameter: 100
33+
34+
BinPackArguments: true
35+
BinPackParameters: true
36+
37+
AllowShortIfStatementsOnASingleLine: false
38+
AllowShortLoopsOnASingleLine: false
39+
AllowShortFunctionsOnASingleLine: Empty
40+
41+
IncludeBlocks: Preserve
42+
SortIncludes: false

src/screen_sharing.c

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
#include "screen_sharing.h"
22

3-
#include <stdio.h>
43
#include <stdarg.h>
4+
#include <stdio.h>
55
#include <stdlib.h>
66
#include <strings.h>
77

8-
#include <os/log.h>
8+
#include <CoreFoundation/CoreFoundation.h>
99
#include <Security/Security.h>
1010
#include <ServiceManagement/ServiceManagement.h>
11-
#include <CoreFoundation/CoreFoundation.h>
12-
11+
#include <os/log.h>
1312

1413
#ifdef SCREEN_SHARING_EXECUTABLE
1514

1615
int main(int argc, const char **argv)
1716
{
1817
ss_context_t context = ScreenSharing_ContextCreateFromArgs(argc, argv);
19-
bool result = ScreenSharing_Toggle(&context);
18+
bool result = ScreenSharing_Toggle(&context);
2019
ScreenSharing_ContextDestroy(&context);
2120
return !result;
2221
}
2322

2423
#else
2524

26-
__attribute__((constructor))
27-
static void constructor(void)
25+
__attribute__((constructor)) static void constructor(void)
2826
{
2927
ss_context_t context = ScreenSharing_ContextCreateFromEnv();
30-
bool result = ScreenSharing_Toggle(&context);
28+
bool result = ScreenSharing_Toggle(&context);
3129
ScreenSharing_ContextDestroy(&context);
3230
exit(!result);
3331
}
3432

35-
#endif // SCREEN_SHARING_EXECUTABLE
33+
#endif // SCREEN_SHARING_EXECUTABLE
3634

3735
bool ScreenSharing_Toggle(ss_context_t *context)
3836
{
39-
context->log("toggling screen sharing to %s", (context->screen_sharing_toggle) ? "on" : "off");
40-
context->log("toggling remote login to %s", (context->remote_login_toggle) ? "on" : "off");
37+
context->log("toggling screen sharing to %s",
38+
(context->screen_sharing_toggle) ? "on" : "off");
39+
context->log("toggling remote login to %s",
40+
(context->remote_login_toggle) ? "on" : "off");
4141

4242
const char *service_names[] = {
4343
"com.apple.tccd.system",
@@ -57,42 +57,39 @@ bool ScreenSharing_Toggle(ss_context_t *context)
5757

5858
if (!post_event_success) {
5959
ss_request_t request =
60-
ScreenSharing_ScreenRequest(
61-
"kTCCServicePostEvent", context->screen_sharing_toggle
62-
);
60+
ScreenSharing_ScreenRequest("kTCCServicePostEvent",
61+
context->screen_sharing_toggle);
6362
post_event_success = ScreenSharing_ServiceSendRequest(context, request);
6463
}
6564

6665
if (!screen_capture_success) {
6766
ss_request_t request =
68-
ScreenSharing_ScreenRequest(
69-
"kTCCServiceScreenCapture", context->screen_sharing_toggle
70-
);
71-
screen_capture_success = ScreenSharing_ServiceSendRequest(context, request);
67+
ScreenSharing_ScreenRequest("kTCCServiceScreenCapture",
68+
context->screen_sharing_toggle);
69+
screen_capture_success =
70+
ScreenSharing_ServiceSendRequest(context, request);
7271
}
7372

7473
if (!remote_login_success) {
75-
ss_request_t request =
76-
ScreenSharing_RemoteLoginRequest(
77-
"kTCCServiceSystemPolicyAllFiles", context->remote_login_toggle
78-
);
74+
ss_request_t request = ScreenSharing_RemoteLoginRequest(
75+
"kTCCServiceSystemPolicyAllFiles", context->remote_login_toggle);
7976
remote_login_success = ScreenSharing_ServiceSendRequest(context, request);
8077
}
8178

82-
requests_success = post_event_success && screen_capture_success & remote_login_success;
79+
requests_success =
80+
post_event_success && screen_capture_success & remote_login_success;
8381
}
8482

8583
bool remote_login_serivce_success =
86-
ScreenSharing_ServiceSet(
87-
context, "com.openssh.sshd", context->remote_login_toggle
88-
);
84+
ScreenSharing_ServiceSet(context, "com.openssh.sshd",
85+
context->remote_login_toggle);
8986
bool screen_sharing_serivce_success =
90-
ScreenSharing_ServiceSet(
91-
context, "com.apple.screensharing", context->screen_sharing_toggle
92-
);
87+
ScreenSharing_ServiceSet(context, "com.apple.screensharing",
88+
context->screen_sharing_toggle);
9389

9490
bool result = false;
95-
if (requests_success & remote_login_serivce_success && screen_sharing_serivce_success) {
91+
if (requests_success & remote_login_serivce_success &&
92+
screen_sharing_serivce_success) {
9693
context->log("successfuly toggled screen sharing & remote login");
9794
result = true;
9895
} else {
@@ -101,7 +98,8 @@ bool ScreenSharing_Toggle(ss_context_t *context)
10198
context->log("screen_capture_success = %d", screen_capture_success);
10299
context->log("remote_login_success = %d", remote_login_success);
103100
context->log("remote_login_serivce_success = %d", remote_login_serivce_success);
104-
context->log("screen_sharing_serivce_success = %d", screen_sharing_serivce_success);
101+
context->log("screen_sharing_serivce_success = %d",
102+
screen_sharing_serivce_success);
105103
}
106104

107105
return result;
@@ -145,7 +143,7 @@ static void ScreenSharing__LogStdout(const char *format, ...)
145143

146144
static void ScreenSharing__LogQuiet(const char *format, ...)
147145
{
148-
(void)format;
146+
(void) format;
149147
}
150148

151149
static ss_log_fn ScreenSharing__LogFnFromString(const char *name, ss_log_fn default_fn)
@@ -187,8 +185,8 @@ ss_context_t ScreenSharing_ContextCreateFromEnv(void)
187185
ss_context_t context = {0};
188186

189187
char *screen_sharing_log = getenv("SCREEN_SHARING_LOG");
190-
context.log =
191-
ScreenSharing__LogFnFromString(screen_sharing_log, ScreenSharing__LogStderr);
188+
context.log = ScreenSharing__LogFnFromString(screen_sharing_log,
189+
ScreenSharing__LogStderr);
192190

193191
char *screen_sharing_toggle = getenv("SCREEN_SHARING_TOGGLE");
194192
if (!screen_sharing_toggle || !strcasecmp(screen_sharing_toggle, "on")) {
@@ -197,7 +195,8 @@ ss_context_t ScreenSharing_ContextCreateFromEnv(void)
197195
context.screen_sharing_toggle = false;
198196
} else {
199197
context.remote_login_toggle = true;
200-
context.log("unknown screen sharing toggle \"%s\", defaulting to on", screen_sharing_toggle);
198+
context.log("unknown screen sharing toggle \"%s\", defaulting to on",
199+
screen_sharing_toggle);
201200
}
202201

203202
char *remote_login_toggle = getenv("REMOTE_LOGIN_TOGGLE");
@@ -207,7 +206,8 @@ ss_context_t ScreenSharing_ContextCreateFromEnv(void)
207206
context.remote_login_toggle = false;
208207
} else {
209208
context.remote_login_toggle = true;
210-
context.log("unknown remote login toggle \"%s\", defaulting to on", remote_login_toggle);
209+
context.log("unknown remote login toggle \"%s\", defaulting to on",
210+
remote_login_toggle);
211211
}
212212

213213
return context;
@@ -216,9 +216,9 @@ ss_context_t ScreenSharing_ContextCreateFromEnv(void)
216216
ss_context_t ScreenSharing_ContextCreateFromArgs(int argc, const char **argv)
217217
{
218218
ss_context_t context = {
219-
.log = ScreenSharing__LogStdout,
219+
.log = ScreenSharing__LogStdout,
220220
.screen_sharing_toggle = true,
221-
.remote_login_toggle = true,
221+
.remote_login_toggle = true,
222222
};
223223

224224
if (argc < 1) {
@@ -230,15 +230,14 @@ ss_context_t ScreenSharing_ContextCreateFromArgs(int argc, const char **argv)
230230

231231
for (int i = 1; i < argc && !show_usage; i++) {
232232
if (!strcmp(argv[i], "--log")) {
233-
if (i + 1 >= argc || argv[i+1][0] == '-') {
233+
if (i + 1 >= argc || argv[i + 1][0] == '-') {
234234
ScreenSharing__LogStderr("missing value for log");
235235
show_usage = true;
236236
} else {
237-
context.log =
238-
ScreenSharing__LogFnFromString(argv[++i], context.log);
237+
context.log = ScreenSharing__LogFnFromString(argv[++i], context.log);
239238
}
240239
} else if (!strcmp(argv[i], "--screen-sharing")) {
241-
if (i + 1 >= argc || argv[i+1][0] == '-') {
240+
if (i + 1 >= argc || argv[i + 1][0] == '-') {
242241
ScreenSharing__LogStderr("missing value for screen-sharing");
243242
show_usage = true;
244243
} else if (!strcasecmp(argv[++i], "on")) {
@@ -249,7 +248,7 @@ ss_context_t ScreenSharing_ContextCreateFromArgs(int argc, const char **argv)
249248
ScreenSharing__LogStderr("unknown value for screen-sharing");
250249
}
251250
} else if (!strcmp(argv[i], "--remote-login")) {
252-
if (i + 1 >= argc || argv[i+1][0] == '-') {
251+
if (i + 1 >= argc || argv[i + 1][0] == '-') {
253252
ScreenSharing__LogStderr("missing value for remote-login");
254253
show_usage = true;
255254
} else if (!strcasecmp(argv[++i], "on")) {
@@ -259,8 +258,7 @@ ss_context_t ScreenSharing_ContextCreateFromArgs(int argc, const char **argv)
259258
} else {
260259
ScreenSharing__LogStderr("unknown value for remote-login");
261260
}
262-
} else if (!strcmp(argv[i], "--help") ||
263-
!strcmp(argv[i], "-h")) {
261+
} else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
264262
show_usage = true;
265263
} else {
266264
ScreenSharing__LogStderr("unknown argument '%s'", argv[i]);
@@ -274,15 +272,18 @@ ss_context_t ScreenSharing_ContextCreateFromArgs(int argc, const char **argv)
274272
"Toggle on/off screen sharing.\n\n"
275273
"Options:\n"
276274
" --log <mode> Set logging output.\n"
277-
" Supported modes stdout/stderr/os/quiet.\n"
275+
" Supported modes "
276+
"stdout/stderr/os/quiet.\n"
278277
" Default: stdout\n"
279-
" --screen-sharing <on|off> Enable or disable screen sharing toggle.\n"
278+
" --screen-sharing <on|off> Enable or disable screen sharing "
279+
"toggle.\n"
280280
" Default: on\n"
281-
" --remote-login <on|off> Enable or disable remote login toggle.\n"
281+
" --remote-login <on|off> Enable or disable remote login "
282+
"toggle.\n"
282283
" Default: on\n"
283-
" -h, --help Show this help message and exit.\n\n",
284-
program_name
285-
);
284+
" -h, --help Show this help message and "
285+
"exit.\n\n",
286+
program_name);
286287
exit(1);
287288
}
288289

@@ -311,24 +312,22 @@ bool ScreenSharing_ServiceConnect(ss_context_t *context, const char *service_nam
311312
dispatch_queue_create("com.screen-sharing.toggle.queue", DISPATCH_QUEUE_SERIAL);
312313
if (!dispatch_queue) {
313314
context->log("failed to create dispatch queue");
314-
315315
} else {
316316
xpc_connection_t connection =
317317
xpc_connection_create_mach_service(service_name, dispatch_queue, 0);
318318
if (!connection) {
319319
context->log("failed to create connection");
320320
dispatch_release(dispatch_queue);
321-
322321
} else {
323322
xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
324-
(void)event;
323+
(void) event;
325324
});
326325
xpc_connection_resume(connection);
327326

328327
ScreenSharing_ContextDestroy(context);
329-
context->connection = connection;
328+
context->connection = connection;
330329
context->dispatch_queue = dispatch_queue;
331-
result = true;
330+
result = true;
332331
}
333332
}
334333

@@ -338,7 +337,7 @@ bool ScreenSharing_ServiceConnect(ss_context_t *context, const char *service_nam
338337
bool ScreenSharing_ServiceSendRequest(ss_context_t *context, ss_request_t request)
339338
{
340339
xpc_object_t xpc_request = xpc_dictionary_create_empty();
341-
bool result = false;
340+
bool result = false;
342341

343342
if (xpc_request) {
344343
xpc_dictionary_set_string(xpc_request, "function", "TCCAccessSetInternal");
@@ -354,7 +353,8 @@ bool ScreenSharing_ServiceSendRequest(ss_context_t *context, ss_request_t reques
354353
context->log("failed to send request with missing reply");
355354
} else {
356355
if (xpc_get_type(reply) == XPC_TYPE_ERROR) {
357-
ScreenSharing__LogXpcObject(context->log, "failed to send request with: ", reply);
356+
ScreenSharing__LogXpcObject(context->log,
357+
"failed to send request with: ", reply);
358358
} else {
359359
ScreenSharing__LogXpcObject(context->log, "reply: ", reply);
360360
result = xpc_dictionary_get_bool(reply, "result");
@@ -367,14 +367,11 @@ bool ScreenSharing_ServiceSendRequest(ss_context_t *context, ss_request_t reques
367367
return result;
368368
}
369369

370-
bool ScreenSharing_ServiceSet(ss_context_t *context, const char *service_name,
371-
bool toggle)
370+
bool ScreenSharing_ServiceSet(ss_context_t *context, const char *service_name, bool toggle)
372371
{
373-
extern Boolean SMJobSetEnabled(
374-
CFStringRef domain, AuthorizationRef auth,
375-
CFStringRef service_name, Boolean, int unknown,
376-
CFErrorRef *error
377-
);
372+
extern Boolean SMJobSetEnabled(CFStringRef domain, AuthorizationRef auth,
373+
CFStringRef service_name, Boolean,
374+
int unknown, CFErrorRef *error);
378375

379376
static AuthorizationItem auth_item = {
380377
.name = "com.apple.ServiceManagement.daemons.modify",
@@ -386,19 +383,17 @@ bool ScreenSharing_ServiceSet(ss_context_t *context, const char *service_name,
386383

387384
bool result = false;
388385

389-
AuthorizationRef auth = NULL;
390-
OSStatus status =
391-
AuthorizationCreate(&auth_rigths, NULL,
392-
kAuthorizationFlagPartialRights, &auth);
386+
AuthorizationRef auth = NULL;
387+
OSStatus status = AuthorizationCreate(&auth_rigths, NULL,
388+
kAuthorizationFlagPartialRights, &auth);
393389
if (status) {
394390
CFStringRef error_desc = SecCopyErrorMessageString(status, NULL);
395391
if (error_desc) {
396392
char error_buffer[1024] = {0};
397393
CFStringGetCString(error_desc, error_buffer, sizeof(error_buffer),
398394
kCFStringEncodingUTF8);
399395

400-
context->log("failed to create authorization with: %d - %s",
401-
status,
396+
context->log("failed to create authorization with: %d - %s", status,
402397
error_buffer);
403398
CFRelease(error_desc);
404399
} else {
@@ -407,17 +402,15 @@ bool ScreenSharing_ServiceSet(ss_context_t *context, const char *service_name,
407402
return false;
408403
}
409404

410-
CFErrorRef error = NULL;
405+
CFErrorRef error = NULL;
411406
CFStringRef service_name_cf =
412-
CFStringCreateWithCString(
413-
kCFAllocatorSystemDefault, service_name, kCFStringEncodingUTF8
414-
);
407+
CFStringCreateWithCString(kCFAllocatorSystemDefault, service_name,
408+
kCFStringEncodingUTF8);
415409

416410
if (!service_name_cf) {
417411
context->log("failed to allocate service name cfstring");
418-
419-
} else if (!SMJobSetEnabled(kSMDomainSystemLaunchd, auth,
420-
service_name_cf, toggle, 1, &error)) {
412+
} else if (!SMJobSetEnabled(kSMDomainSystemLaunchd, auth, service_name_cf,
413+
toggle, 1, &error)) {
421414
CFStringRef error_desc = CFErrorCopyDescription(error);
422415
if (error_desc) {
423416
char error_buffer[1024] = {0};

0 commit comments

Comments
 (0)