-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXScreenSize.hpp
More file actions
374 lines (305 loc) · 11.7 KB
/
Copy pathXScreenSize.hpp
File metadata and controls
374 lines (305 loc) · 11.7 KB
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
#pragma once
#include <cstdarg>
#include <cmath>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
namespace XScreenSize {
class XScreen {
public:
int scr, width, height, mmWidth, mmHeight;
XScreen() : scr(-1), width(-1), height(-1), mmWidth(-1), mmHeight(-1) {}
XScreen(Display* dpy, int newScr) {
scr = newScr;
width = DisplayWidth(dpy, scr);
height = DisplayHeight(dpy, scr);
mmWidth = DisplayWidthMM(dpy, scr);
mmHeight = DisplayHeightMM(dpy, scr);
}
friend std::ostream& operator<<(std::ostream& os, const XScreen& m) {
os << "screen " << m.scr << ": " << m.width << "px x " << m.height << "px";
return os;
}
};
enum class XrandrNameKind : int {
name_none = 0,
name_string = (1 << 0),
name_xid = (1 << 1),
name_index = (1 << 2),
name_preferred = (1 << 3)
};
inline constexpr XrandrNameKind operator&(XrandrNameKind x, XrandrNameKind y) {
return static_cast<XrandrNameKind>(static_cast<int>(x) & static_cast<int>(y));
}
inline constexpr XrandrNameKind operator|(XrandrNameKind x, XrandrNameKind y) {
return static_cast<XrandrNameKind>(static_cast<int>(x) | static_cast<int>(y));
}
inline XrandrNameKind& operator&=(XrandrNameKind& x, XrandrNameKind y){
x = x & y;
return x;
}
inline XrandrNameKind& operator|=(XrandrNameKind& x, XrandrNameKind y) {
x = x | y;
return x;
}
class XrandrName {
public:
XrandrNameKind kind;
std::string str;
XID xid;
int index;
XrandrName() : kind(XrandrNameKind::name_none) {}
XrandrName(XID newXID) { setXID(newXID); }
void setString(const std::string& newStr) {
kind |= XrandrNameKind::name_string;
str = newStr;
}
void setXID(XID newXid) {
kind |= XrandrNameKind::name_xid;
xid = newXid;
}
void setIndex(int newIndex) {
kind |= XrandrNameKind::name_index;
index = newIndex;
}
bool matches(const XrandrName& other) {
XrandrNameKind common = other.kind & kind;
if(static_cast<int>(common & XrandrNameKind::name_xid) && other.xid == xid) return true;
if(static_cast<int>(common & XrandrNameKind::name_string) && other.str == str) return true;
if(static_cast<int>(common & XrandrNameKind::name_index) && other.index == index) return true;
return false;
}
};
class XrandrCRTController {
public:
XrandrName crtc;
XRRCrtcInfo* crtc_info;
XRRModeInfo* mode_info;
};
class XrandrOutput {
public:
XrandrName output;
XRROutputInfo* output_info;
XrandrName crtc;
XrandrCRTController* crtc_info;
XrandrName mode;
XRRModeInfo *mode_info;
std::string name, connection;
bool has_details;
double refresh;
int x, y, width, height, mmWidth, mmHeight;
XrandrOutput() : output_info(nullptr), crtc_info(nullptr), mode_info(nullptr), has_details(false), refresh(0.0), x(-1), y(-1), width(-1), height(-1), mmWidth(-1), mmHeight(-1) {}
bool can_use_crtc(const XrandrCRTController& crtc) {
for(int c = 0; c < output_info->ncrtc; c++)
if(output_info->crtcs[c] == crtc.crtc.xid)
return true;
return false;
}
friend std::ostream& operator<<(std::ostream& os, const XrandrOutput& m) {
os << m.name << " [" << m.connection << "]";
if(m.has_details)
os << " at " << m.refresh << "Hz: " << m.width << "x" << m.height << "+" << m.x << "+" << m.y << ", " << m.mmWidth << "mm x " << m.mmHeight << "mm";
return os;
}
};
class Getter {
private:
Display* dpy;
Window root;
XRRScreenResources* res;
bool current = false;
XScreen screen;
std::vector<XrandrCRTController> crtcs;
std::vector<XrandrOutput> outputs;
std::runtime_error fatal(const char *format, ...) {
char buf[1024];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
return std::runtime_error(buf);
}
// vertical refresh frequency in Hz
double mode_refresh(XRRModeInfo *mode_info) {
double rate;
if (mode_info->hTotal && mode_info->vTotal)
rate = ((double)mode_info->dotClock / ((double)mode_info->hTotal * (double)mode_info->vTotal));
else
rate = 0;
return rate;
}
XrandrCRTController* find_crtc(const XrandrName& name) {
for(size_t c = 0; c < crtcs.size(); c++)
if(crtcs[c].crtc.matches(name))
return &crtcs[c];
return nullptr;
}
XrandrCRTController* find_crtc_by_xid(RRCrtc crtc) {
return find_crtc(XrandrName(crtc));
}
XRRModeInfo* find_mode(const XrandrName& name, double refresh) {
XRRModeInfo *best = nullptr;
double bestDist = 0;
for (int m = 0; m < res->nmode; m++) {
XRRModeInfo *mode = &res->modes[m];
if(static_cast<int>(name.kind & XrandrNameKind::name_xid) && name.xid == mode->id) {
best = mode;
break;
}
if(static_cast<int>(name.kind & XrandrNameKind::name_string) && name.str == mode->name) {
double dist = refresh ? fabs(mode_refresh(mode) - refresh) : 0;
if(!best || dist < bestDist) {
bestDist = dist;
best = mode;
}
}
}
return best;
}
XRRModeInfo* find_mode_by_xid(RRMode mode) {
XrandrName mode_name{};
mode_name.setXID(mode);
return find_mode(mode_name, 0);
}
XRRModeInfo* find_mode_for_output(const XrandrOutput& output, const XrandrName& name) {
XRROutputInfo *output_info = output.output_info;
XRRModeInfo *best = nullptr;
double bestDist = 0;
for(int m = 0; m < output_info->nmode; m++) {
XRRModeInfo *mode;
mode = find_mode_by_xid (output_info->modes[m]);
if(!mode) continue;
if(static_cast<int>(name.kind & XrandrNameKind::name_xid) && name.xid == mode->id) {
best = mode;
break;
}
if(static_cast<int>(name.kind & XrandrNameKind::name_string) && name.str == mode->name) {
// stay away from doublescan modes unless refresh rate is specified
if(!output.refresh && (mode->modeFlags & RR_DoubleScan))
continue;
double dist = output.refresh ? fabs(mode_refresh(mode) - output.refresh) : 0;
if(!best || dist < bestDist) {
bestDist = dist;
best = mode;
}
}
}
return best;
}
XrandrOutput create_output_info(RROutput xid, XRROutputInfo* output_info) {
XrandrOutput output;
std::vector<std::string> connection = {
"connected",
"disconnected",
"unknown connection"
};
// set output name and info
if (!static_cast<int>(output.output.kind & XrandrNameKind::name_xid))
output.output.setXID(xid);
if (!static_cast<int>(output.output.kind & XrandrNameKind::name_string))
output.output.setString(output_info->name);
output.output_info = output_info;
// set crtc name and info
output.crtc.setXID(output_info->crtc);
if(output.crtc.kind == XrandrNameKind::name_xid && output.crtc.xid == None)
output.crtc_info = nullptr;
else {
output.crtc_info = find_crtc(output.crtc);
if(!output.crtc_info) {
if(static_cast<int>(output.crtc.kind & XrandrNameKind::name_xid))
throw fatal("cannot find crtc 0x%x", output.crtc.xid);
if(static_cast<int>(output.crtc.kind & XrandrNameKind::name_index))
throw fatal("cannot find crtc %d", output.crtc.index);
}
if(!output.can_use_crtc(*(output.crtc_info)))
throw fatal("output %s cannot use crtc 0x%x", output.output.str.c_str(), output.crtc_info->crtc.xid);
}
// set mode name and info
XrandrCRTController* crtc = find_crtc_by_xid(output_info->crtc);
if(crtc && crtc->crtc_info)
output.mode.setXID(crtc->crtc_info->mode);
else if(output.crtc_info)
output.mode.setXID(output.crtc_info->crtc_info->mode);
else
output.mode.setXID(None);
if(output.mode.xid) {
output.mode_info = find_mode_by_xid(output.mode.xid);
if (!output.mode_info)
throw fatal("server did not report mode 0x%x for output %s", output.mode.xid, output.output.str.c_str());
}
else
output.mode_info = nullptr;
// extract information
output.name = output_info->name;
output.connection = connection[output_info->connection];
output.x = output.crtc_info ? output.crtc_info->crtc_info->x : 0;
output.y = output.crtc_info ? output.crtc_info->crtc_info->y : 0;
if(output.mode_info != nullptr) {
output.has_details = true;
output.refresh = mode_refresh(output.mode_info);
output.width = output.mode_info->width;
output.height = output.mode_info->height;
output.mmWidth = (int)output_info->mm_width;
output.mmHeight = (int)output_info->mm_height;
}
return output;
}
void get_screen(bool current) {
if(current)
res = XRRGetScreenResourcesCurrent(dpy, root);
else
res = XRRGetScreenResources(dpy, root);
if(!res) throw fatal("could not get screen resources");
}
void get_crtcs() {
for(int c = 0; c < res->ncrtc; c++) {
XrandrCRTController crtc{};
crtc.crtc.setXID(res->crtcs[c]);
crtc.crtc.setIndex(c);
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(dpy, res, res->crtcs[c]);
if(!crtc_info) throw fatal("could not get crtc 0x%x information", res->crtcs[c]);
crtc.crtc_info = crtc_info;
if(crtc_info->mode == None)
crtc.mode_info = nullptr;
crtcs.push_back(crtc);
}
}
// use current output state to complete the output list
void get_outputs() {
for (int o = 0; o < res->noutput; o++) {
XRROutputInfo *output_info = XRRGetOutputInfo(dpy, res, res->outputs[o]);
if (!output_info) throw fatal("could not get output 0x%x information", res->outputs[o]);
outputs.push_back(create_output_info(res->outputs[o], output_info));
}
}
public:
Getter(char* display_name = nullptr, int screenNum = -1) {
int event_base, error_base;
int major, minor;
dpy = XOpenDisplay(display_name);
if(dpy == nullptr)
throw fatal("can't open display %s", XDisplayName(display_name));
if(screenNum < 0)
screenNum = DefaultScreen(dpy);
if(screenNum >= ScreenCount(dpy))
throw fatal("invalid screen number %d, display has %d", screenNum, ScreenCount(dpy));
root = RootWindow(dpy, screenNum);
if(!XRRQueryExtension(dpy, &event_base, &error_base) || !XRRQueryVersion(dpy, &major, &minor))
throw fatal("RandR extension missing");
if(!(major > 1 || (major == 1 && minor >= 2)))
throw fatal("RandR version >1.2 is required, having %d.%d", major, minor);
screen = XScreen(dpy, screenNum);
get_screen(current);
get_crtcs();
get_outputs();
}
const XScreen& getScreen() {
return screen;
}
const std::vector<XrandrOutput>& getOutputs() {
return outputs;
}
};
}