forked from libthinkpad/dockd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crtc.cpp
533 lines (378 loc) · 13.8 KB
/
crtc.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "crtc.h"
#include <cstring>
#include <unistd.h>
#include <syslog.h>
//#define DRYRUN
CRTControllerManager::CRTControllerManager()
{
connectToX();
}
CRTControllerManager::~CRTControllerManager()
{
XRRFreeScreenResources(resources);
XCloseDisplay(display);
}
bool CRTControllerManager::applyConfiguration(CRTControllerManager::DockState state)
{
/* Reconnect to the server */
disconnectFromX();
connectToX();
/*
* Step 0: Check config files
* Step 1: Open the right config file
* Step 3: Apply the crtc configuration
* Step 4: Apply the screen configuration
*/
/* Step 0 */
if (access(CONFIG_LOCATION_DOCKED, R_OK) != 0) {
syslog(LOG_ERR, "Can't open config file %s, aborting\n", CONFIG_LOCATION_DOCKED);
return false;
}
if (access(CONFIG_LOCATION_UNDOCKED, R_OK) != 0) {
syslog(LOG_ERR, "Can't open config file %s, aborting\n", CONFIG_LOCATION_UNDOCKED);
return false;
}
/* Step 1 */
Ini config;
switch (state) {
case DOCKED:
config.readIni(CONFIG_LOCATION_DOCKED);
break;
case UNDOCKED:
config.readIni(CONFIG_LOCATION_UNDOCKED);
break;
}
/* Step 3 */
vector<IniSection*> configControllers = config.getSections("CRTC");
if (configControllers.size() > resources->ncrtc) {
syslog(LOG_ERR, "Not enough CRT controllers to set config, aborting\n");
return false;
}
/*
* Let's match a CRTC to some controller,
* to see if the list of underlying controllers has changed
*/
int matched = 0;
for (int i = 0; i < resources->ncrtc; i++) {
RRCrtc *rrCrtc = (resources->crtcs + i);
for (IniSection* section : configControllers) {
RRCrtc crtc = (RRCrtc) section->getInt("crtc");
if (*rrCrtc == crtc) {
matched++;
}
}
}
if (matched != resources->ncrtc) {
syslog(LOG_ERR, "CRTC map changed, please re-run the configuration utlity\n");
return false;
}
vector<CRTConfig*> controllerConfigs;
bool isControllerConfigValid = true;
/*
* For each section of CRTC's in the configuration
* file we need to check if the output is still
* there and connected, and also to check
* if the specified output supports the
* output mode specified in the configuration file
*/
for (IniSection *configSection : configControllers) {
/*
* Get the stored outputs in the configuration
* file and check if there are any, if there are
* not we don't really need to configure anything
*/
vector<const char *> configOutputNames = configSection->getStringArray("outputs");
OutputConfigs configs;
configs.mode = None;
configs.error = 0;
bool outputOff = false;
if (configOutputNames.size() == 0) {
outputOff = true;
}
if (!outputOff) {
configs = getOutputConfigs(&configOutputNames, configSection);
}
if (configs.error != 0) {
syslog(LOG_ERR, "Mode lookup error: %s\n", strerror(-configs.error));
isControllerConfigValid = false;
break;
}
CRTConfig *crtcConfig = new CRTConfig;
crtcConfig->crtc = (RRCrtc) configSection->getInt("crtc");
crtcConfig->x = configSection->getInt("x");
crtcConfig->y = configSection->getInt("y");
crtcConfig->mode = configs.mode;
crtcConfig->rotation = (Rotation) configSection->getInt("rotation");
crtcConfig->outputs = (RROutput *) calloc(configs.outputs.size(), sizeof(RROutput));
crtcConfig->noutputs = configs.outputs.size();
for (size_t i = 0; i < crtcConfig->noutputs; i++) {
*(crtcConfig->outputs + i) = configs.outputs.at(i);
}
controllerConfigs.push_back(crtcConfig);
}
/* Apply the configs */
if (!isControllerConfigValid) {
syslog(LOG_ERR, "Controller config is not valid, not committing changes to X\n");
return false;
}
XGrabServer(display);
for (CRTConfig *controller : controllerConfigs) {
syslog(LOG_INFO, "Applying config to %4lu: mode: %4lu, outputs: %4zu, x: %4d, y: %4d\n",
controller->crtc, controller->mode, controller->noutputs, controller->x, controller->y);
#ifndef DRYRUN
XRRSetCrtcConfig(display,
resources,
controller->crtc,
CurrentTime,
controller->x,
controller->y,
controller->mode,
controller->rotation,
controller->outputs,
(int) controller->noutputs); // cast: stack smashing: size_t (ul) copy into noutputs: int (d)
#endif // DRYRUN
free(controller->outputs);
delete controller;
}
XUngrabServer(display);
XSync(display, 0);
XGrabServer(display);
/* Step 4 */
IniSection *screenSection = config.getSection("Screen");
int width = screenSection->getInt("width");
int height = screenSection->getInt("height");
int mm_height = screenSection->getInt("mm_height");
int mm_width = screenSection->getInt("mm_width");
syslog(LOG_INFO, "Setting screen size: height: %d, width: %d\n", height, width);
#ifndef DRYRUN
XRRSetScreenSize(display, window, width, height, mm_width, mm_height);
#endif // DRYRUN
XUngrabServer(display);
XSync(display, 0);
return true;
}
bool CRTControllerManager::writeConfigToDisk(CRTControllerManager::DockState state)
{
Ini ini;
/*
* Step 1: Reload the X11 resources
* Step 2: Serialize the screen
* Step 3: Serialize the XRRCrtcInfo and friends file structure
* Step 4: Write the config files
*/
/* Step 1 */
XRRFreeScreenResources(resources);
resources = XRRGetScreenResources(display, window);
XSync(display, 0);
/* Step 2 */
int height = DisplayHeight(display, screen);
int width = DisplayWidth(display, screen);
int mm_width = DisplayWidthMM(display, screen);
int mm_height = DisplayHeightMM(display, screen);
IniSection *screen = new IniSection("Screen");
screen->setInt("height", height);
screen->setInt("width", width);
screen->setInt("mm_height", mm_height);
screen->setInt("mm_width", mm_width);
ini.addSection(screen);
/* Step 3 */
for (int i = 0; i < resources->ncrtc; i++) {
IniSection *section = new IniSection("CRTC");
RRCrtc *crtc = (resources->crtcs + i);
XRRCrtcInfo *info = XRRGetCrtcInfo(display, resources, *crtc);
/* Set basic info */
section->setInt("crtc", (int) *crtc);
section->setInt("x", info->x);
section->setInt("y", info->y);
section->setInt("rotation", (int) info->rotation);
/* Set the output name */
bool modeFound = false;
// the mode is none, write none
if (info->mode == 0) {
section->setString("mode", "None");
modeFound = true;
}
// the mode is active, find name
if (!modeFound) {
for (int j = 0; j < resources->nmode; j++) {
XRRModeInfo *mode = (resources->modes + j);
if (info->mode == mode->id) {
section->setString("mode", mode->name);
modeFound = true;
}
}
}
if (!modeFound) {
syslog(LOG_ERR, "Failed to find output mode name!\n");
return false;
}
/* Set the outputs */
vector<const char*> names;
for (int k = 0; k < info->noutput; k++) {
RROutput *output = (info->outputs + k);
XRROutputInfo *info = XRRGetOutputInfo(display, resources, *output);
/* XRROutputInfo goes out of scope here,
* we need to copy the string to the heap,
* add it to the ini and free it later
*/
char *name_st = (char*) calloc(strlen(info->name) + 1, sizeof(char));
strcpy(name_st, info->name);
names.push_back(name_st);
XRRFreeOutputInfo(info);
}
section->setStringArray("outputs", &names);
for (const char *ptr : names) {
free((void*)ptr);
}
ini.addSection(section);
XRRFreeCrtcInfo(info);
}
/* Step 4 */
switch (state) {
case DOCKED:
return ini.writeIni(CONFIG_LOCATION_DOCKED);
case UNDOCKED:
return ini.writeIni(CONFIG_LOCATION_UNDOCKED);
}
return false;
}
CRTControllerManager::OutputConfigs CRTControllerManager::getOutputConfigs(vector<const char *> *configOutputNames,
IniSection *configSection) {
OutputConfigs configs;
configs.mode = None;
configs.error = 0;
/* The output is off */
if (strcmp(configSection->getString("mode"), "None") == 0) {
configs.error = 0;
configs.mode = None;
return configs;
}
for (const char *configOutput : *configOutputNames) {
RROutput output;
int attempts = 0;
while ((output = getRROutputByName(configOutput)) == ((XID) -EAGAIN)) {
syslog(LOG_ERR, "Trying to find (%s) again: (%s)\n", configOutput, strerror(EAGAIN));
/* Wait for the hardware to sanitize */
usleep(250000);
if (resources) {
XRRFreeScreenResources(resources);
}
resources = XRRGetScreenResources(display, window);
if (attempts > 10) {
output = None;
break;
}
attempts++;
}
if (output == None) {
syslog(LOG_ERR, "Error: output from config (%s) not found on this machine\n", configOutput);
configs.error = -ENODEV;
return configs;
}
configs.outputs.push_back(output);
RRMode configMode;
attempts = 0;
const char *configOutputMode = configSection->getString("mode");
while ((configMode = getRRModeByNameSupported(configOutputMode, output)) == ((XID) -EAGAIN)) {
syslog(LOG_ERR, "Trying to find (%s) again: (%s)\n", configOutputMode, strerror(EAGAIN));
if (attempts > 10) {
configMode = None;
break;
}
/* Wait for the hardware to sanitize */
usleep(250000);
if (resources) {
XRRFreeScreenResources(resources);
}
resources = XRRGetScreenResources(display, window);
attempts++;
}
if (configMode == None) {
syslog(LOG_ERR, "Output mode %s not found for output %s\n", configSection->getString("mode"), configOutput);
configs.error = -ENODEV;
return configs;
}
if (configs.mode == None) {
configs.mode = configMode;
} else if (configMode != configs.mode) {
syslog(LOG_ERR, "Mode mismatch between monitors, did you change monitors? Re-run the config.\n");
configs.error = -ENODEV;
configs.mode = None;
return configs;
}
}
if (configs.mode == None && configs.outputs.size() > 0) {
syslog(LOG_ERR, "runtime error\n");
}
return configs;
}
bool CRTControllerManager::isOutputModeSupported(RROutput output, RRMode mode) {
XRROutputInfo *outputInfo = XRRGetOutputInfo(display, resources, output);
for (int i = 0; i < outputInfo->nmode; i++) {
RRMode temp = *(outputInfo->modes + i);
if (mode == temp) {
XRRFreeOutputInfo(outputInfo);
return true;
}
}
XRRFreeOutputInfo(outputInfo);
return false;
}
void CRTControllerManager::connectToX() {
display = XOpenDisplay(NULL);
if (!display) {
syslog(LOG_ERR, "Error opening display!\n");
exit(EXIT_FAILURE);
}
screen = DefaultScreen(display);
window = RootWindow(display, screen);
resources = XRRGetScreenResources(display, window);
if (!resources) {
syslog(LOG_ERR, "Failed to get resources!\n");
exit(EXIT_FAILURE);
}
}
void CRTControllerManager::disconnectFromX() {
if (display) {
XCloseDisplay(display);
}
if (resources) {
XRRFreeScreenResources(resources);
}
}
RROutput CRTControllerManager::getRROutputByName(const char *outputName) {
for (int i = 0; i < resources->noutput; i++) {
RROutput rrOutput = *(resources->outputs + i);
XRROutputInfo *outputInfo = XRRGetOutputInfo(display, resources, rrOutput);
if (strcmp(outputInfo->name, outputName) == 0) {
/* Output is there but not connected, try again */
if (outputInfo->connection != RR_Connected) {
XRRFreeOutputInfo(outputInfo);
/* Do an intentional overflow to 18446744073709551605 to signal EAGAIN */
return -EAGAIN;
}
XRRFreeOutputInfo(outputInfo);
return rrOutput;
}
XRRFreeOutputInfo(outputInfo);
}
return None;
}
RRMode CRTControllerManager::getRRModeByNameSupported(const char *modeName, RROutput output)
{
for (int i = 0; i < resources->nmode; i++) {
XRRModeInfo *modeInfo = (resources->modes + i);
if (strcmp(modeName, modeInfo->name) == 0) {
if (isOutputModeSupported(output, modeInfo->id)) {
return modeInfo->id;
}
}
}
/*
* The output mode maybe is not there? Here, we
* actually overflow the unsigned int to 18446744073709551605
* to signal an EAGAIN to refresh the screen resources and
* maybe find the resources we are looking for.
*/
return -EAGAIN;
}