-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
285 lines (231 loc) · 7.9 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
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
284
285
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <CoreGraphics/CGDisplayConfiguration.h>
#include <cjson/cJSON.h>
void print_usage()
{
printf("Usage: macd <apply-configuration|list-displays|list-configurations|save-configuration>\n");
}
FILE *get_config_file(const char *mode)
{
char *homedir = getenv("HOME");
char filename[strlen(homedir) + strlen("/.macd.json")];
sprintf(filename, "%s/.macd.json", homedir);
return fopen(filename, mode);
}
cJSON *read_configurations()
{
FILE *config_file_ptr;
long config_file_size;
char *buffer;
size_t read_size;
cJSON *configurations;
config_file_ptr = get_config_file("rt");
if (config_file_ptr == NULL)
return cJSON_CreateArray();
fseek(config_file_ptr, 0, SEEK_END);
config_file_size = ftell(config_file_ptr);
rewind(config_file_ptr);
buffer = (char *)malloc(sizeof(char) * config_file_size);
if (buffer == NULL)
{
fputs("Memory error.\n", stderr);
exit(1);
}
read_size = fread(buffer, sizeof(char), config_file_size, config_file_ptr);
if ((long) read_size != config_file_size)
{
fputs("Read error.\n", stderr);
exit(1);
}
configurations = cJSON_Parse(buffer);
if (configurations == NULL)
{
fputs("Parse error.\n", stderr);
exit(1);
}
free(buffer);
fclose(config_file_ptr);
return configurations;
}
cJSON *new_configuration(const char *name)
{
cJSON *configuration = cJSON_CreateObject();
cJSON *displays = cJSON_CreateArray();
cJSON_AddStringToObject(configuration, "name", name);
cJSON_AddItemToObject(configuration, "displays", displays);
return configuration;
}
cJSON *get_configuration(cJSON *configurations, const char *name)
{
cJSON *current_configuration;
cJSON_ArrayForEach(current_configuration, configurations)
{
cJSON *current_name_item = cJSON_GetObjectItem(current_configuration, "name");
const char *current_name = cJSON_GetStringValue(current_name_item);
if (strcmp(name, current_name) == 0)
return current_configuration;
}
return NULL;
}
cJSON *get_or_create_configuration(cJSON *configurations, const char *name)
{
cJSON *current_configuration;
cJSON_ArrayForEach(current_configuration, configurations)
{
cJSON *current_name_item = cJSON_GetObjectItem(current_configuration, "name");
const char *current_name = cJSON_GetStringValue(current_name_item);
if (strcmp(name, current_name) == 0)
{
cJSON_DeleteItemFromObject(current_configuration, "displays");
cJSON_AddItemToObject(current_configuration, "displays", cJSON_CreateArray());
return current_configuration;
}
}
cJSON *result_configuration = new_configuration(name);
cJSON_AddItemToArray(configurations, result_configuration);
return result_configuration;
}
void command_list_displays()
{
CGDisplayCount display_count;
CGGetOnlineDisplayList(INT_MAX, NULL, &display_count);
CGDirectDisplayID display_list[display_count];
CGGetOnlineDisplayList(INT_MAX, display_list, &display_count);
for (unsigned int i = 0; i < display_count; i++)
{
CGDirectDisplayID current_display = display_list[i];
CGRect rect = CGDisplayBounds(current_display);
bool is_builtin = CGDisplayIsBuiltin(current_display);
bool is_main = CGDisplayIsMain(current_display);
printf("Display #%d\n", i + 1);
printf("ID: %u\n", current_display);
printf("X: %d\n", (int)rect.origin.x);
printf("Y: %d\n", (int)rect.origin.y);
printf("Width: %d\n", (int)rect.size.width);
printf("Height: %d\n", (int)rect.size.height);
printf("Built-in: %s\n", is_builtin ? "true" : "false");
printf("Main: %s\n", is_main ? "true" : "false");
printf("\n");
}
}
void command_save_configuration(const char *name)
{
CGDisplayCount display_count;
CGGetOnlineDisplayList(INT_MAX, NULL, &display_count);
CGDirectDisplayID display_list[display_count];
CGGetOnlineDisplayList(INT_MAX, display_list, &display_count);
cJSON *configurations = read_configurations();
cJSON *configuration = get_or_create_configuration(configurations, name);
cJSON *displays = cJSON_GetObjectItem(configuration, "displays");
for (unsigned int i = 0; i < display_count; i++)
{
CGDirectDisplayID current_display = display_list[i];
CGRect rect = CGDisplayBounds(current_display);
char display_id[8];
char display_x[8];
char display_y[8];
sprintf(display_id, "%d", current_display);
sprintf(display_x, "%d", (int)rect.origin.x);
sprintf(display_y, "%d", (int)rect.origin.y);
cJSON *display = cJSON_CreateObject();
cJSON_AddStringToObject(display, "id", display_id);
cJSON_AddStringToObject(display, "x", display_x);
cJSON_AddStringToObject(display, "y", display_y);
cJSON_AddItemToArray(displays, display);
}
FILE *config_file = get_config_file("wt");
fprintf(config_file, "%s\n", cJSON_Print(configurations));
fclose(config_file);
cJSON_Delete(configurations);
}
void command_apply_configuration(const char *name)
{
CGError error;
cJSON *configurations = read_configurations();
cJSON *configuration = get_configuration(configurations, name);
if (configuration == NULL)
{
fputs("Configuration not found.\n", stderr);
exit(1);
}
CGDisplayConfigRef config_ref;
error = CGBeginDisplayConfiguration(&config_ref);
if (error != kCGErrorSuccess)
{
fputs("Configuration error.\n", stderr);
exit(1);
}
cJSON *displays = cJSON_GetObjectItem(configuration, "displays");
cJSON *current_display;
cJSON_ArrayForEach(current_display, displays)
{
cJSON *json_display_id = cJSON_GetObjectItem(current_display, "id");
cJSON *json_display_x = cJSON_GetObjectItem(current_display, "x");
cJSON *json_display_y = cJSON_GetObjectItem(current_display, "y");
int display_id = atoi(cJSON_GetStringValue(json_display_id));
int display_x = atoi(cJSON_GetStringValue(json_display_x));
int display_y = atoi(cJSON_GetStringValue(json_display_y));
error = CGConfigureDisplayOrigin(config_ref, display_id, display_x, display_y);
if (error != kCGErrorSuccess)
{
fputs("Configuration error.\n", stderr);
exit(1);
}
}
error = CGCompleteDisplayConfiguration(config_ref, kCGConfigurePermanently);
if (error != kCGErrorSuccess)
{
fputs("Configuration error.\n", stderr);
exit(1);
}
cJSON_Delete(configurations);
}
void command_list_configurations()
{
cJSON *configurations;
configurations = read_configurations();
fputs(cJSON_Print(configurations), stdout);
cJSON_Delete(configurations);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Invalid number of arguments.\n");
print_usage();
return 1;
}
if (strcmp(argv[1], "list-configurations") == 0)
command_list_configurations();
else if (strcmp(argv[1], "list-displays") == 0)
command_list_displays();
else if (strcmp(argv[1], "save-configuration") == 0)
{
if (argc < 3)
{
printf("Invalid number of arguments.\n");
printf("Usage: macd save-configuration <name>\n");
return 1;
}
command_save_configuration(argv[2]);
}
else if (strcmp(argv[1], "apply-configuration") == 0)
{
if (argc < 3)
{
printf("Invalid numnber of arguments.\n");
printf("Usage: macd apply-configuration <name>\n");
return 1;
}
command_apply_configuration(argv[2]);
}
else
{
printf("Invalid argument.\n");
print_usage();
return 1;
}
return 0;
}