-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
69 lines (57 loc) · 1.97 KB
/
main.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
#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>
void GetDisplays(CGDirectDisplayID *mainDisplayId, CGDirectDisplayID *secondaryDisplayId) {
*mainDisplayId = CGMainDisplayID();
CGDirectDisplayID *onlineDisplays;
uint32_t nOnlineDisplays;
CGDisplayErr err = CGGetOnlineDisplayList(10, onlineDisplays, &nOnlineDisplays);
if (err != 0) {
printf("Error getting online displays: %d\n", err);
exit(EXIT_FAILURE);
}
for (int i = 0; i < nOnlineDisplays; i++) {
if (onlineDisplays[i] != *mainDisplayId) {
*secondaryDisplayId = onlineDisplays[i];
break;
}
}
}
void ToggleMirroring(bool on, CGDirectDisplayID mainDisplayId, CGDirectDisplayID secondaryDisplayId) {
// Begin configuration
CGDisplayConfigRef configRef;
CGError err = CGBeginDisplayConfiguration(&configRef);
if (err != 0) {
printf("Error initiating display configuration: %d\n", err);
exit(EXIT_FAILURE);
}
// Mirror displays or cancel mirroring
if (!on)
mainDisplayId = kCGNullDirectDisplay;
err = CGConfigureDisplayMirrorOfDisplay(configRef, secondaryDisplayId, mainDisplayId);
if (err != 0) {
printf("Error configuring displays: %d\n", err);
exit(EXIT_FAILURE);
}
// Apply the changes
err = CGCompleteDisplayConfiguration(configRef, kCGConfigurePermanently);
if (err != 0) {
printf("Error applying configuration: %d\n", err);
exit(EXIT_FAILURE);
}
}
int main() {
CGDirectDisplayID mainDisplayId, secondaryDisplayId;
GetDisplays(&mainDisplayId, &secondaryDisplayId);
bool alreadyMirrored = CGDisplayIsInMirrorSet(mainDisplayId);
if (alreadyMirrored) {
printf("Displays mirrored, will not toggle\n");
exit(EXIT_SUCCESS);
}
if (mainDisplayId <= 0 || secondaryDisplayId <= 0) {
printf("Can't find a main and a secondary display\n");
exit(EXIT_FAILURE);
}
ToggleMirroring(true, mainDisplayId, secondaryDisplayId);
ToggleMirroring(false, mainDisplayId, secondaryDisplayId);
exit(EXIT_SUCCESS);
}