forked from mikeakers/hmscreens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmscreens.m
283 lines (241 loc) · 10.2 KB
/
hmscreens.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Created 22 July 2010 by Hank McShane
// version 0.1
// requires Mac OS X 10.4 or higher
//
// Use hmscreens to either get information about your screens
// or for setting the main screen (the screen with the menu bar).
//
// Usage: hmscreens
// [-h] shows the help text
// [-info] shows information about the connected screens
// [-screenIDs] returns only the screen IDs for the connected screens
// [-setMainID <Screen ID>] Screen ID of the screen that you want to make the main screen
// [-othersStartingPosition <position>] left, right, top, or bottom... with -setMainID, this determines placement of other screens
//
// Examples:
// hmscreens -info
// returns information about your attached screens including the Screen ID
//
// hmscreens -setMainID 69670848 -othersStartingPosition left
// makes the screen with the Screen ID 69670848 the main screen.
// Also positions other screens to the left of the main screen as shown
// under the "Arrangement" section of the Displays preference pane.
//
// NOTE: Global Position {0, 0} coordinate (as shown under -info)
// is the lower left corner of the main screen
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
void printHelp();
void displaysInfo();
void screenIDs();
void setMainScreen(NSString* screenID, NSString* othersStartingPosition);
void swapDisplays();
#define MAX_DISPLAYS 32
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// get command line arguments
NSArray* pInfo = [[NSArray alloc] initWithArray:[[NSProcessInfo processInfo] arguments]];
if ([pInfo count] == 1) {
printHelp();
} else if ([[pInfo objectAtIndex:1] isEqualToString:@"-h"]) {
printHelp();
} else if ([[pInfo objectAtIndex:1] isEqualToString:@"-info"]) {
displaysInfo();
} else if ([[pInfo objectAtIndex:1] isEqualToString:@"-screenIDs"]) {
screenIDs();
} else if ([[pInfo objectAtIndex:1] isEqualToString:@"-setMainID"]) {
NSString* screenID = [[NSUserDefaults standardUserDefaults] stringForKey:@"setMainID"];
NSString* othersStartingPosition = [[NSUserDefaults standardUserDefaults] stringForKey:@"othersStartingPosition"];
setMainScreen(screenID, othersStartingPosition);
} else if ([[pInfo objectAtIndex:1] isEqualToString:@"-swapDisplays"]) {
swapDisplays();
} else {
printHelp();
}
[pInfo release];
[pool drain];
return 0;
}
//----------------------------------------
// FUNCTIONS
//----------------------------------------
#pragma mark -
#pragma mark FUNCTIONS
void screenIDs() {
CGDirectDisplayID activeDisplays[MAX_DISPLAYS];
CGDisplayErr err;
CGDisplayCount displayCount;
// get the active displays
err = CGGetActiveDisplayList(MAX_DISPLAYS, activeDisplays, &displayCount);
if ( err != kCGErrorSuccess ) {
printf("Error: cannot get displays:\n%d\n", err);
return;
}
int i;
for (i=0; i<displayCount; i++) {
printf("%i\n", activeDisplays[i]);
}
}
void setMainScreen(NSString* screenID, NSString* othersStartingPosition) {
CGDirectDisplayID activeDisplays[MAX_DISPLAYS];
CGDisplayErr err;
CGDisplayCount displayCount;
CGDisplayConfigRef config;
// get the active displays
err = CGGetActiveDisplayList(MAX_DISPLAYS, activeDisplays, &displayCount);
if ( err != kCGErrorSuccess ) {
printf("Error: cannot get displays:\n%d\n", err);
return;
}
// error if more than 5 displays
// we only handle 5 because we set the main and left/right/top/bottom positions
if (displayCount > 5) {
printf("Error: hmscreens can only handle a max of 5 screens when adjusting the main screen\n");
return;
}
// validate that the screenID exists and get the index number of it
int i, newMainScreenIndex;
BOOL foundScreenID = NO;
for (i=0; i<displayCount; i++) {
CGDirectDisplayID thisDisplayID = activeDisplays[i];
NSString* thisDisplayIDString = [NSString stringWithFormat:@"%i", thisDisplayID];
if ([thisDisplayIDString isEqualToString:screenID]) {
foundScreenID = YES;
break;
}
}
if (foundScreenID) {
newMainScreenIndex = i;
} else {
printf("Error: Screen ID %s could not be found\n", [screenID UTF8String]);
return;
}
// construct othersPos array which determines how we position the other displays
NSArray* othersPos;
if ([othersStartingPosition isEqualToString:@"left"]) {
othersPos = [NSArray arrayWithObjects:@"left", @"right", @"top", @"bottom", nil];
} else if ([othersStartingPosition isEqualToString:@"right"]) {
othersPos = [NSArray arrayWithObjects:@"right", @"left", @"top", @"bottom", nil];
} else if ([othersStartingPosition isEqualToString:@"top"]) {
othersPos = [NSArray arrayWithObjects:@"top", @"bottom", @"left", @"right", nil];
} else if ([othersStartingPosition isEqualToString:@"bottom"]) {
othersPos = [NSArray arrayWithObjects:@"bottom", @"top", @"left", @"right", nil];
} else {
othersPos = [NSArray arrayWithObjects:@"left", @"right", @"top", @"bottom", nil];
}
// configure the displays
int othersCount = 0;
CGBeginDisplayConfiguration(&config);
for(i=0; i<displayCount; i++) {
if (i == newMainScreenIndex) { // make this one the main screen
CGConfigureDisplayOrigin(config, activeDisplays[i], 0, 0); //Set the as the new main display by positioning at 0,0
} else {
NSString* thisPos = [othersPos objectAtIndex:othersCount];
if ([thisPos isEqualToString:@"left"]) {
CGConfigureDisplayOrigin(config, activeDisplays[i], -1*CGDisplayPixelsWide(activeDisplays[i]), 0);
} else if ([thisPos isEqualToString:@"right"]) {
CGConfigureDisplayOrigin(config, activeDisplays[i], CGDisplayPixelsWide(activeDisplays[newMainScreenIndex]), 0);
} else if ([thisPos isEqualToString:@"top"]) {
CGConfigureDisplayOrigin(config, activeDisplays[i], 0, -1*CGDisplayPixelsHigh(activeDisplays[i]));
} else if ([thisPos isEqualToString:@"bottom"]) {
CGConfigureDisplayOrigin(config, activeDisplays[i], 0, CGDisplayPixelsHigh(activeDisplays[newMainScreenIndex]));
}
othersCount++;
}
}
CGCompleteDisplayConfiguration(config, kCGConfigureForSession);
}
void printHelp() {
NSString* a = @"Use hmscreens to either get information about your screens";
NSString* b = @"or for setting the main screen (the screen with the menu bar).";
NSString* c = @"Usage: hmscreens";
NSString* d = @"[-h] shows the help text";
NSString* e = @"[-info] shows information about the connected screens";
NSString* f = @"[-screenIDs] returns only the screen IDs for the connected screens";
NSString* g = @"[-setMainID <Screen ID>] Screen ID of the screen that you want to make the main screen";
NSString* h = @"[-othersStartingPosition <position>] left, right, top, or bottom";
NSString* i = @"\t\tuse this with -setMainID to determine placement of other screens";
NSString* j = @"Examples:";
NSString* k = @"hmscreens -info";
NSString* l = @"\treturns information about your attached screens including the Screen ID";
NSString* m = @"hmscreens -setMainID 69670848 -othersStartingPosition left";
NSString* n = @"\tmakes the screen with the Screen ID 69670848 the main screen.";
NSString* o = @"\tAlso positions other screens to the left of the main screen as shown";
NSString* p = @"\tunder the \"Arrangement\" section of the Displays preference pane.";
NSString* q = @"NOTE: Global Position {0, 0} coordinate (as shown under -info)";
NSString* r = @"\tis the lower left corner of the main screen";
NSString* z = [NSString stringWithFormat:@"%@\n%@\n\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n\n%@\n%@\n%@\n\n%@\n%@\n%@\n%@\n\n%@\n%@\n",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r];
printf("%s\n", [z UTF8String]);
}
void displaysInfo() {
NSArray* allScreens = [NSScreen screens];
int i;
for (i=0; i<[allScreens count]; i++) {
NSScreen* thisScreen = [allScreens objectAtIndex:i];
NSDictionary* deviceDescription = [thisScreen deviceDescription];
//NSLog(@"deviceDescription: %@", deviceDescription);
// screen id
NSNumber* screenID = [deviceDescription valueForKey:@"NSScreenNumber"];
CGDirectDisplayID cgScreenID = (CGDirectDisplayID)[screenID intValue];
printf("Screen ID: %i\n", [screenID intValue]);
// size
NSSize size = [[deviceDescription objectForKey:NSDeviceSize] sizeValue];
printf("Size: %s\n", [NSStringFromSize(size) UTF8String]);
// global position
NSRect frame = [thisScreen frame];
int x1 = frame.origin.x;
int y1 = frame.origin.y;
int x2 = x1 + frame.size.width;
int y2 = y1 + frame.size.height;
printf("Global Position: {{%i, %i}, {%i, %i}}\n", x1, y1, x2, y2);
// color space
NSString* colorSpace = [deviceDescription valueForKey:NSDeviceColorSpaceName];
printf("Color Space: %s\n", [colorSpace UTF8String]);
// depth ie. 32 is millions of colors, 16 is thousands, 8 is 256
long bpp = CGDisplayBitsPerPixel(cgScreenID);
printf("BitsPerPixel: %d\n", bpp);
// resolution
NSSize resolution = [[deviceDescription objectForKey:NSDeviceResolution] sizeValue];
printf("Resolution(dpi): %s\n", [NSStringFromSize(resolution) UTF8String]);
// refresh rate
long refresh;
CFNumberRef number = CFDictionaryGetValue(CGDisplayCurrentMode(cgScreenID), kCGDisplayRefreshRate);
CFNumberGetValue(number, kCFNumberLongType, &refresh);
printf("Refresh Rate: %d\n", refresh);
// usesQuartzExtreme
BOOL usesQuartzExtreme = CGDisplayUsesOpenGLAcceleration(cgScreenID);
if (usesQuartzExtreme) {
printf("Uses Quartz Extreme: YES\n");
} else {
printf("Uses Quartz Extreme: NO\n");
}
printf("\n");
}
}
void swapDisplays() {
NSArray* allScreens = [NSScreen screens];
if (allScreens.count != 2) {
printf("ERROR: swapDisplays only supports 2 screens\n");
exit(1);
}
for (int i = 0; i < [allScreens count]; i++) {
NSScreen* thisScreen = [allScreens objectAtIndex:i];
NSDictionary* deviceDescription = [thisScreen deviceDescription];
//NSLog(@"deviceDescription: %@", deviceDescription);
// screen id
NSNumber* screenID = [deviceDescription valueForKey:@"NSScreenNumber"];
// global position
NSRect frame = [thisScreen frame];
int x1 = frame.origin.x;
int y1 = frame.origin.y;
if (x1 == 0 && y1 == 0) {
printf("%d is the main display\n", screenID.intValue);
} else {
printf("%d is the second display\n", screenID.intValue);
printf("making %d the main display\n", screenID.intValue);
setMainScreen(screenID.stringValue, @"left");
}
}
}