-
Notifications
You must be signed in to change notification settings - Fork 12
/
mirror.m
196 lines (173 loc) · 6.28 KB
/
mirror.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
/*
* mirror.m
* mirror
*
* Created by Fabian Canas on 2/4/09.
* Copyright 2009-2018 Fabián Cañas. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
typedef NS_ENUM(NSUInteger, MirrorAction) {
help,
on,
off,
toggle,
query,
linkDiplays,
};
MirrorAction getAction(int *masterIndex, int *slaveIndex)
{
NSArray<NSString *> *args = [[NSProcessInfo processInfo] arguments];
MirrorAction action = toggle;
for (int idx = 0; idx < args.count; idx++) {
NSString *arg = args[idx];
if ([arg isEqualToString:@"-h"]){
action = help;
break;
}
if ([arg isEqualToString:@"-t"]){
action = toggle;
break;
}
if ([arg isEqualToString:@"-on"]){
action = on;
break;
}
if ([arg isEqualToString:@"-off"]){
action = off;
break;
}
if ([arg isEqualToString:@"-q"]){
action = query;
break;
}
if ([arg isEqualToString:@"-l"]) {
if (args.count < idx + 2) {
break;
}
NSString *masterIndexString = args[idx + 1];
NSString *slaveIndexString = args[idx + 2];
if (masterIndex != NULL) {
*masterIndex = [masterIndexString intValue];
}
if (slaveIndex != NULL) {
*slaveIndex = [slaveIndexString intValue];
}
action = linkDiplays;
break;
}
}
return action;
}
#define MAX_DISPLAYS 10
#define SECONDARY_DISPLAY_COUNT 9
static CGDisplayCount numberOfTotalDspys = MAX_DISPLAYS;
static CGDirectDisplayID activeDspys[MAX_DISPLAYS];
static CGDirectDisplayID onlineDspys[MAX_DISPLAYS];
static CGDirectDisplayID secondaryDspys[SECONDARY_DISPLAY_COUNT];
CGError multiConfigureDisplays(CGDisplayConfigRef configRef, CGDirectDisplayID *secondaryDspys, int count, CGDirectDisplayID master) {
CGError error = kCGErrorSuccess;
for (int i = 0; i<count; i++) {
error = error ?: CGConfigureDisplayMirrorOfDisplay(configRef, secondaryDspys[i], master);
}
return error;
}
int main (int argc, const char * argv[]) {
int masterIndex, slaveIndex;
MirrorAction action = getAction(&masterIndex, &slaveIndex);
if (action == help){
printf("Mirror Displays version 1.2\nCopyright 2009-2018, Fabián Cañas\n");
printf("usage: mirror [option]\tOnly the first option passed will be applied");
printf("\n -h\t\tPrint this usage and exit.");
printf("\n -t\t\tToggle mirroring (default behavior)");
printf("\n -on\t\tTurn Mirroring On");
printf("\n -off\t\tTurn Mirroring Off");
printf("\n -q\t\tQuery the Mirroring state and write \"on\" or \"off\" to stdout");
printf("\n -l A B\t Makes display at index B mirror the display at index A");
printf("\n");
return 0;
}
CGDisplayCount numberOfActiveDspys;
CGDisplayCount numberOfOnlineDspys;
CGDisplayErr activeError = CGGetActiveDisplayList(numberOfTotalDspys,activeDspys,&numberOfActiveDspys);
if (activeError!=0) {
printf("Error in obtaining active diplay list: %d\n",activeError);
return activeError;
}
CGDisplayErr onlineError = CGGetOnlineDisplayList (numberOfTotalDspys,onlineDspys,&numberOfOnlineDspys);
if (onlineError!=0) {
printf("Error in obtaining online diplay list: %d\n",onlineError);
return onlineError;
}
if (numberOfOnlineDspys<2) {
printf("No secondary display detected.\n");
return 1;
}
bool displaysMirrored = CGDisplayIsInMirrorSet(CGMainDisplayID());
int secondaryDisplayIndex = 0;
for (int displayIndex = 0; displayIndex<numberOfOnlineDspys; displayIndex++) {
if (onlineDspys[displayIndex] != CGMainDisplayID()) {
secondaryDspys[secondaryDisplayIndex] = onlineDspys[displayIndex];
secondaryDisplayIndex++;
}
}
if (action == toggle) {
if (displaysMirrored) {
action = off;
} else {
action = on;
}
}
CGDisplayConfigRef configRef;
CGError err = CGBeginDisplayConfiguration (&configRef);
if (err != 0) {
printf("Error with CGBeginDisplayConfiguration: %d\n",err);
return err;
}
switch (action) {
case on:
err = multiConfigureDisplays(configRef, secondaryDspys, numberOfOnlineDspys - 1, CGMainDisplayID());
break;
case off:
err = multiConfigureDisplays(configRef, secondaryDspys, numberOfOnlineDspys - 1, kCGNullDirectDisplay);
break;
case query:
if (displaysMirrored) {
printf("on\n");
} else {
printf("off\n");
}
break;
case linkDiplays:
if (numberOfOnlineDspys <= masterIndex) {
printf("Index of specified master display out of bounds\n");
return 1;
}
if (numberOfOnlineDspys <= slaveIndex) {
printf("Index of slave display out of bounds\n");
return 1;
}
err = CGConfigureDisplayMirrorOfDisplay(configRef, onlineDspys[slaveIndex], onlineDspys[masterIndex]);
break;
default:
break;
}
if (err != 0) printf("Error configuring displays: %d\n",err);
// Apply the changes
err = CGCompleteDisplayConfiguration (configRef,kCGConfigurePermanently);
if (err != 0) printf("Error applying configuration: %d\n",err);
return err;
}