Skip to content

Latest commit

 

History

History
1108 lines (976 loc) · 36.6 KB

SDL_x11modes.c

File metadata and controls

1108 lines (976 loc) · 36.6 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_X11
#include "SDL_hints.h"
#include "SDL_x11video.h"
#include "SDL_timer.h"
#include "edid.h"
/* #define X11MODES_DEBUG */
/* I'm becoming more and more convinced that the application should never
* use XRandR, and it's the window manager's responsibility to track and
* manage display modes for fullscreen windows. Right now XRandR is completely
* broken with respect to window manager behavior on every window manager that
* I can find. For example, on Unity 3D if you show a fullscreen window while
* the resolution is changing (within ~250 ms) your window will retain the
* fullscreen state hint but be decorated and windowed.
*
* However, many people swear by it, so let them swear at it. :)
*/
/* #define XRANDR_DISABLED_BY_DEFAULT */
static int
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
{
const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
int depth;
/* Look for an exact visual, if requested */
if (visual_id) {
XVisualInfo *vi, template;
int nvis;
SDL_zero(template);
template.visualid = SDL_strtol(visual_id, NULL, 0);
vi = X11_XGetVisualInfo(display, VisualIDMask, &template, &nvis);
if (vi) {
*vinfo = *vi;
X11_XFree(vi);
return 0;
}
}
depth = DefaultDepth(display, screen);
if ((X11_UseDirectColorVisuals() &&
X11_XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
X11_XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
X11_XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
X11_XMatchVisualInfo(display, screen, depth, StaticColor, vinfo)) {
return 0;
}
return -1;
}
int
X11_GetVisualInfoFromVisual(Display * display, Visual * visual, XVisualInfo * vinfo)
{
XVisualInfo *vi;
int nvis;
vinfo->visualid = X11_XVisualIDFromVisual(visual);
vi = X11_XGetVisualInfo(display, VisualIDMask, vinfo, &nvis);
if (vi) {
*vinfo = *vi;
X11_XFree(vi);
return 0;
}
return -1;
}
Uint32
X11_GetPixelFormatFromVisualInfo(Display * display, XVisualInfo * vinfo)
{
if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
Rmask = vinfo->visual->red_mask;
Gmask = vinfo->visual->green_mask;
Bmask = vinfo->visual->blue_mask;
if (vinfo->depth == 32) {
Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
} else {
Amask = 0;
}
bpp = vinfo->depth;
if (bpp == 24) {
int i, n;
XPixmapFormatValues *p = X11_XListPixmapFormats(display, &n);
if (p) {
for (i = 0; i < n; ++i) {
if (p[i].depth == 24) {
bpp = p[i].bits_per_pixel;
break;
}
}
X11_XFree(p);
}
}
return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
}
if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
switch (vinfo->depth) {
case 8:
return SDL_PIXELTYPE_INDEX8;
case 4:
if (BitmapBitOrder(display) == LSBFirst) {
return SDL_PIXELFORMAT_INDEX4LSB;
} else {
return SDL_PIXELFORMAT_INDEX4MSB;
}
Nov 14, 2016
Nov 14, 2016
137
/* break; -Wunreachable-code-break */
138
139
140
141
142
143
case 1:
if (BitmapBitOrder(display) == LSBFirst) {
return SDL_PIXELFORMAT_INDEX1LSB;
} else {
return SDL_PIXELFORMAT_INDEX1MSB;
}
Nov 14, 2016
Nov 14, 2016
144
/* break; -Wunreachable-code-break */
145
146
147
148
149
150
151
}
}
return SDL_PIXELFORMAT_UNKNOWN;
}
/* Global for the error handler */
Nov 14, 2016
Nov 14, 2016
152
static int vm_event, vm_error = -1;
153
154
155
156
157
158
159
160
161
162
163
164
#if SDL_VIDEO_DRIVER_X11_XINERAMA
static SDL_bool
CheckXinerama(Display * display, int *major, int *minor)
{
int event_base = 0;
int error_base = 0;
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
Oct 8, 2016
Oct 8, 2016
165
if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XINERAMA, SDL_TRUE)) {
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
#ifdef X11MODES_DEBUG
printf("Xinerama disabled due to hint\n");
#endif
return SDL_FALSE;
}
if (!SDL_X11_HAVE_XINERAMA) {
#ifdef X11MODES_DEBUG
printf("Xinerama support not available\n");
#endif
return SDL_FALSE;
}
/* Query the extension version */
if (!X11_XineramaQueryExtension(display, &event_base, &error_base) ||
!X11_XineramaQueryVersion(display, major, minor) ||
!X11_XineramaIsActive(display)) {
#ifdef X11MODES_DEBUG
printf("Xinerama not active on the display\n");
#endif
return SDL_FALSE;
}
#ifdef X11MODES_DEBUG
printf("Xinerama available at version %d.%d!\n", *major, *minor);
#endif
return SDL_TRUE;
}
Jun 30, 2015
Jun 30, 2015
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* !!! FIXME: remove this later. */
/* we have a weird bug where XineramaQueryScreens() throws an X error, so this
is here to help track it down (and not crash, too!). */
static SDL_bool xinerama_triggered_error = SDL_FALSE;
static int
X11_XineramaFailed(Display * d, XErrorEvent * e)
{
xinerama_triggered_error = SDL_TRUE;
fprintf(stderr, "XINERAMA X ERROR: type=%d serial=%lu err=%u req=%u minor=%u\n",
e->type, e->serial, (unsigned int) e->error_code,
(unsigned int) e->request_code, (unsigned int) e->minor_code);
fflush(stderr);
return 0;
}
208
209
210
211
212
213
214
215
216
217
218
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
#if SDL_VIDEO_DRIVER_X11_XRANDR
static SDL_bool
CheckXRandR(Display * display, int *major, int *minor)
{
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
#ifdef XRANDR_DISABLED_BY_DEFAULT
Oct 8, 2016
Oct 8, 2016
219
if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XRANDR, SDL_FALSE)) {
220
221
222
223
224
225
#ifdef X11MODES_DEBUG
printf("XRandR disabled by default due to window manager issues\n");
#endif
return SDL_FALSE;
}
#else
Oct 8, 2016
Oct 8, 2016
226
if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XRANDR, SDL_TRUE)) {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifdef X11MODES_DEBUG
printf("XRandR disabled due to hint\n");
#endif
return SDL_FALSE;
}
#endif /* XRANDR_ENABLED_BY_DEFAULT */
if (!SDL_X11_HAVE_XRANDR) {
#ifdef X11MODES_DEBUG
printf("XRandR support not available\n");
#endif
return SDL_FALSE;
}
/* Query the extension version */
Aug 7, 2015
Aug 7, 2015
242
*major = 1; *minor = 3; /* we want 1.3 */
243
244
245
246
if (!X11_XRRQueryVersion(display, major, minor)) {
#ifdef X11MODES_DEBUG
printf("XRandR not active on the display\n");
#endif
Aug 7, 2015
Aug 7, 2015
247
*major = *minor = 0;
248
249
250
251
252
253
254
255
256
257
258
259
260
261
return SDL_FALSE;
}
#ifdef X11MODES_DEBUG
printf("XRandR available at version %d.%d!\n", *major, *minor);
#endif
return SDL_TRUE;
}
#define XRANDR_ROTATION_LEFT (1 << 1)
#define XRANDR_ROTATION_RIGHT (1 << 3)
static int
CalculateXRandRRefreshRate(const XRRModeInfo *info)
{
Oct 1, 2016
Oct 1, 2016
262
263
return (info->hTotal && info->vTotal) ?
round(((double)info->dotClock / (double)(info->hTotal * info->vTotal))) : 0;
264
265
266
}
static SDL_bool
Aug 7, 2015
Aug 7, 2015
267
SetXRandRModeInfo(Display *display, XRRScreenResources *res, RRCrtc crtc,
268
269
270
271
RRMode modeID, SDL_DisplayMode *mode)
{
int i;
for (i = 0; i < res->nmode; ++i) {
Aug 7, 2015
Aug 7, 2015
272
273
274
const XRRModeInfo *info = &res->modes[i];
if (info->id == modeID) {
XRRCrtcInfo *crtcinfo;
275
276
Rotation rotation = 0;
Aug 7, 2015
Aug 7, 2015
277
278
279
280
crtcinfo = X11_XRRGetCrtcInfo(display, res, crtc);
if (crtcinfo) {
rotation = crtcinfo->rotation;
X11_XRRFreeCrtcInfo(crtcinfo);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
}
if (rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT)) {
mode->w = info->height;
mode->h = info->width;
} else {
mode->w = info->width;
mode->h = info->height;
}
mode->refresh_rate = CalculateXRandRRefreshRate(info);
((SDL_DisplayModeData*)mode->driverdata)->xrandr_mode = modeID;
#ifdef X11MODES_DEBUG
printf("XRandR mode %d: %dx%d@%dHz\n", (int) modeID, mode->w, mode->h, mode->refresh_rate);
#endif
return SDL_TRUE;
}
}
return SDL_FALSE;
}
Aug 7, 2015
Aug 7, 2015
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
static void
SetXRandRDisplayName(Display *dpy, Atom EDID, char *name, const size_t namelen, RROutput output, const unsigned long widthmm, const unsigned long heightmm)
{
/* See if we can get the EDID data for the real monitor name */
int inches;
int nprop;
Atom *props = X11_XRRListOutputProperties(dpy, output, &nprop);
int i;
for (i = 0; i < nprop; ++i) {
unsigned char *prop;
int actual_format;
unsigned long nitems, bytes_after;
Atom actual_type;
if (props[i] == EDID) {
if (X11_XRRGetOutputProperty(dpy, output, props[i], 0, 100, False,
False, AnyPropertyType, &actual_type,
&actual_format, &nitems, &bytes_after,
&prop) == Success) {
MonitorInfo *info = decode_edid(prop);
if (info) {
#ifdef X11MODES_DEBUG
printf("Found EDID data for %s\n", name);
dump_monitor_info(info);
#endif
SDL_strlcpy(name, info->dsc_product_name, namelen);
free(info);
}
X11_XFree(prop);
}
break;
}
}
if (props) {
X11_XFree(props);
}
Jan 5, 2016
Jan 5, 2016
340
inches = (int)((SDL_sqrtf(widthmm * widthmm + heightmm * heightmm) / 25.4f) + 0.5f);
Aug 7, 2015
Aug 7, 2015
341
342
343
344
345
346
347
348
349
350
351
if (*name && inches) {
const size_t len = SDL_strlen(name);
SDL_snprintf(&name[len], namelen-len, " %d\"", inches);
}
#ifdef X11MODES_DEBUG
printf("Display name: %s\n", name);
#endif
}
Nov 14, 2016
Nov 14, 2016
352
static int
Aug 7, 2015
Aug 7, 2015
353
354
355
356
X11_InitModes_XRandR(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Display *dpy = data->display;
Nov 17, 2015
Nov 17, 2015
357
358
359
const int screencount = ScreenCount(dpy);
const int default_screen = DefaultScreen(dpy);
RROutput primary = X11_XRRGetOutputPrimary(dpy, RootWindow(dpy, default_screen));
Aug 7, 2015
Aug 7, 2015
360
361
362
363
364
365
366
367
Atom EDID = X11_XInternAtom(dpy, "EDID", False);
XRRScreenResources *res = NULL;
Uint32 pixelformat;
XVisualInfo vinfo;
XPixmapFormatValues *pixmapformats;
int looking_for_primary;
int scanline_pad;
int output;
Nov 17, 2015
Nov 17, 2015
368
int screen, i, n;
Aug 7, 2015
Aug 7, 2015
369
Nov 17, 2015
Nov 17, 2015
370
371
for (looking_for_primary = 1; looking_for_primary >= 0; looking_for_primary--) {
for (screen = 0; screen < screencount; screen++) {
Aug 7, 2015
Aug 7, 2015
372
Nov 17, 2015
Nov 17, 2015
373
/* we want the primary output first, and then skipped later. */
Dec 29, 2015
Dec 29, 2015
374
if (looking_for_primary && (screen != default_screen)) {
Nov 17, 2015
Nov 17, 2015
375
continue;
Aug 7, 2015
Aug 7, 2015
376
377
}
Nov 17, 2015
Nov 17, 2015
378
379
380
if (get_visualinfo(dpy, screen, &vinfo) < 0) {
continue; /* uh, skip this screen? */
}
Aug 7, 2015
Aug 7, 2015
381
Nov 17, 2015
Nov 17, 2015
382
383
384
385
pixelformat = X11_GetPixelFormatFromVisualInfo(dpy, &vinfo);
if (SDL_ISPIXELFORMAT_INDEXED(pixelformat)) {
continue; /* Palettized video modes are no longer supported */
}
Aug 7, 2015
Aug 7, 2015
386
Nov 17, 2015
Nov 17, 2015
387
388
389
390
391
392
393
394
395
396
scanline_pad = SDL_BYTESPERPIXEL(pixelformat) * 8;
pixmapformats = X11_XListPixmapFormats(dpy, &n);
if (pixmapformats) {
for (i = 0; i < n; ++i) {
if (pixmapformats[i].depth == vinfo.depth) {
scanline_pad = pixmapformats[i].scanline_pad;
break;
}
}
X11_XFree(pixmapformats);
Aug 7, 2015
Aug 7, 2015
397
398
}
Nov 17, 2015
Nov 17, 2015
399
400
res = X11_XRRGetScreenResources(dpy, RootWindow(dpy, screen));
if (!res) {
Aug 7, 2015
Aug 7, 2015
401
402
403
continue;
}
Nov 17, 2015
Nov 17, 2015
404
405
406
407
408
409
410
411
412
413
414
415
416
417
for (output = 0; output < res->noutput; output++) {
XRROutputInfo *output_info;
int display_x, display_y;
unsigned long display_mm_width, display_mm_height;
SDL_DisplayData *displaydata;
char display_name[128];
SDL_DisplayMode mode;
SDL_DisplayModeData *modedata;
SDL_VideoDisplay display;
RRMode modeID;
RRCrtc output_crtc;
XRRCrtcInfo *crtc;
/* The primary output _should_ always be sorted first, but just in case... */
Dec 29, 2015
Dec 29, 2015
418
if ((looking_for_primary && (res->outputs[output] != primary)) ||
Nov 17, 2015
Nov 17, 2015
419
420
421
(!looking_for_primary && (screen == default_screen) && (res->outputs[output] == primary))) {
continue;
}
Aug 7, 2015
Aug 7, 2015
422
Nov 17, 2015
Nov 17, 2015
423
424
425
426
427
output_info = X11_XRRGetOutputInfo(dpy, res, res->outputs[output]);
if (!output_info || !output_info->crtc || output_info->connection == RR_Disconnected) {
X11_XRRFreeOutputInfo(output_info);
continue;
}
Aug 7, 2015
Aug 7, 2015
428
Nov 17, 2015
Nov 17, 2015
429
430
431
432
433
SDL_strlcpy(display_name, output_info->name, sizeof(display_name));
display_mm_width = output_info->mm_width;
display_mm_height = output_info->mm_height;
output_crtc = output_info->crtc;
X11_XRRFreeOutputInfo(output_info);
Aug 7, 2015
Aug 7, 2015
434
Nov 17, 2015
Nov 17, 2015
435
436
437
438
crtc = X11_XRRGetCrtcInfo(dpy, res, output_crtc);
if (!crtc) {
continue;
}
Aug 7, 2015
Aug 7, 2015
439
Nov 17, 2015
Nov 17, 2015
440
441
442
443
444
SDL_zero(mode);
modeID = crtc->mode;
mode.w = crtc->width;
mode.h = crtc->height;
mode.format = pixelformat;
Aug 7, 2015
Aug 7, 2015
445
Nov 17, 2015
Nov 17, 2015
446
447
display_x = crtc->x;
display_y = crtc->y;
Aug 7, 2015
Aug 7, 2015
448
Nov 17, 2015
Nov 17, 2015
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
X11_XRRFreeCrtcInfo(crtc);
displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
if (!displaydata) {
return SDL_OutOfMemory();
}
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (!modedata) {
SDL_free(displaydata);
return SDL_OutOfMemory();
}
modedata->xrandr_mode = modeID;
mode.driverdata = modedata;
displaydata->screen = screen;
displaydata->visual = vinfo.visual;
displaydata->depth = vinfo.depth;
Jan 4, 2017
Jan 4, 2017
467
468
displaydata->hdpi = display_mm_width ? (((float) mode.w) * 25.4f / display_mm_width) : 0.0f;
displaydata->vdpi = display_mm_height ? (((float) mode.h) * 25.4f / display_mm_height) : 0.0f;
Nov 17, 2015
Nov 17, 2015
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
displaydata->ddpi = SDL_ComputeDiagonalDPI(mode.w, mode.h, ((float) display_mm_width) / 25.4f,((float) display_mm_height) / 25.4f);
displaydata->scanline_pad = scanline_pad;
displaydata->x = display_x;
displaydata->y = display_y;
displaydata->use_xrandr = 1;
displaydata->xrandr_output = res->outputs[output];
SetXRandRModeInfo(dpy, res, output_crtc, modeID, &mode);
SetXRandRDisplayName(dpy, EDID, display_name, sizeof (display_name), res->outputs[output], display_mm_width, display_mm_height);
SDL_zero(display);
if (*display_name) {
display.name = display_name;
}
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = displaydata;
SDL_AddVideoDisplay(&display);
Aug 7, 2015
Aug 7, 2015
487
}
Dec 5, 2015
Dec 5, 2015
488
489
X11_XRRFreeScreenResources(res);
Aug 7, 2015
Aug 7, 2015
490
491
492
493
494
495
496
497
498
}
}
if (_this->num_displays == 0) {
return SDL_SetError("No available displays");
}
return 0;
}
499
500
501
502
503
504
505
506
507
508
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
static SDL_bool
CheckVidMode(Display * display, int *major, int *minor)
{
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
Oct 8, 2016
Oct 8, 2016
509
if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XVIDMODE, SDL_TRUE)) {
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#ifdef X11MODES_DEBUG
printf("XVidMode disabled due to hint\n");
#endif
return SDL_FALSE;
}
if (!SDL_X11_HAVE_XVIDMODE) {
#ifdef X11MODES_DEBUG
printf("XVidMode support not available\n");
#endif
return SDL_FALSE;
}
/* Query the extension version */
vm_error = -1;
if (!X11_XF86VidModeQueryExtension(display, &vm_event, &vm_error)
|| !X11_XF86VidModeQueryVersion(display, major, minor)) {
#ifdef X11MODES_DEBUG
printf("XVidMode not active on the display\n");
#endif
return SDL_FALSE;
}
#ifdef X11MODES_DEBUG
printf("XVidMode available at version %d.%d!\n", *major, *minor);
#endif
return SDL_TRUE;
}
static
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
XF86VidModeModeInfo* info)
{
Bool retval;
int dotclock;
XF86VidModeModeLine l;
SDL_zerop(info);
SDL_zero(l);
retval = X11_XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
info->dotclock = dotclock;
info->hdisplay = l.hdisplay;
info->hsyncstart = l.hsyncstart;
info->hsyncend = l.hsyncend;
info->htotal = l.htotal;
info->hskew = l.hskew;
info->vdisplay = l.vdisplay;
info->vsyncstart = l.vsyncstart;
info->vsyncend = l.vsyncend;
info->vtotal = l.vtotal;
info->flags = l.flags;
info->privsize = l.privsize;
info->private = l.private;
return retval;
}
static int
CalculateXVidModeRefreshRate(const XF86VidModeModeInfo * info)
{
return (info->htotal
&& info->vtotal) ? (1000 * info->dotclock / (info->htotal *
info->vtotal)) : 0;
}
Nov 14, 2016
Nov 14, 2016
572
static SDL_bool
573
574
575
576
577
578
579
580
581
582
583
584
585
586
SetXVidModeModeInfo(const XF86VidModeModeInfo *info, SDL_DisplayMode *mode)
{
mode->w = info->hdisplay;
mode->h = info->vdisplay;
mode->refresh_rate = CalculateXVidModeRefreshRate(info);
((SDL_DisplayModeData*)mode->driverdata)->vm_mode = *info;
return SDL_TRUE;
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
int
X11_InitModes(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Nov 14, 2016
Nov 14, 2016
587
int snum, screen, screencount = 0;
588
589
590
591
592
593
594
595
596
597
598
599
600
#if SDL_VIDEO_DRIVER_X11_XINERAMA
int xinerama_major, xinerama_minor;
int use_xinerama = 0;
XineramaScreenInfo *xinerama = NULL;
#endif
#if SDL_VIDEO_DRIVER_X11_XRANDR
int xrandr_major, xrandr_minor;
#endif
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
int vm_major, vm_minor;
int use_vidmode = 0;
#endif
Aug 7, 2015
Aug 7, 2015
601
602
603
604
605
606
/* XRandR is the One True Modern Way to do this on X11. If it's enabled and
available, don't even look at other ways of doing things. */
#if SDL_VIDEO_DRIVER_X11_XRANDR
/* require at least XRandR v1.3 */
if (CheckXRandR(data->display, &xrandr_major, &xrandr_minor) &&
(xrandr_major >= 2 || (xrandr_major == 1 && xrandr_minor >= 3))) {
Aug 2, 2017
Aug 2, 2017
607
608
if (X11_InitModes_XRandR(_this) == 0)
return 0;
Aug 7, 2015
Aug 7, 2015
609
610
611
612
613
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
/* !!! FIXME: eventually remove support for Xinerama and XVidMode (everything below here). */
Jan 5, 2016
Jan 5, 2016
614
615
616
617
/* This is a workaround for some apps (UnrealEngine4, for example) until
we sort out the ramifications of removing XVidMode support outright.
This block should be removed with the XVidMode support. */
{
Oct 8, 2016
Oct 8, 2016
618
if (SDL_GetHintBoolean("SDL_VIDEO_X11_REQUIRE_XRANDR", SDL_FALSE)) {
Jan 5, 2016
Jan 5, 2016
619
620
621
622
623
624
625
626
#if SDL_VIDEO_DRIVER_X11_XRANDR
return SDL_SetError("XRandR support is required but not available");
#else
return SDL_SetError("XRandR support is required but not built into SDL!");
#endif
}
}
627
628
629
630
631
632
#if SDL_VIDEO_DRIVER_X11_XINERAMA
/* Query Xinerama extention
* NOTE: This works with Nvidia Twinview correctly, but you need version 302.17 (released on June 2012)
* or newer of the Nvidia binary drivers
*/
if (CheckXinerama(data->display, &xinerama_major, &xinerama_minor)) {
Jun 30, 2015
Jun 30, 2015
633
634
635
int (*handler) (Display *, XErrorEvent *);
X11_XSync(data->display, False);
handler = X11_XSetErrorHandler(X11_XineramaFailed);
636
xinerama = X11_XineramaQueryScreens(data->display, &screencount);
Jun 30, 2015
Jun 30, 2015
637
638
639
640
641
X11_XSync(data->display, False);
X11_XSetErrorHandler(handler);
if (xinerama_triggered_error) {
xinerama = 0;
}
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
if (xinerama) {
use_xinerama = xinerama_major * 100 + xinerama_minor;
}
}
if (!xinerama) {
screencount = ScreenCount(data->display);
}
#else
screencount = ScreenCount(data->display);
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
if (CheckVidMode(data->display, &vm_major, &vm_minor)) {
use_vidmode = vm_major * 100 + vm_minor;
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
for (snum = 0; snum < screencount; ++snum) {
XVisualInfo vinfo;
SDL_VideoDisplay display;
SDL_DisplayData *displaydata;
SDL_DisplayMode mode;
SDL_DisplayModeData *modedata;
XPixmapFormatValues *pixmapFormats;
char display_name[128];
int i, n;
/* Re-order screens to always put default screen first */
if (snum == 0) {
screen = DefaultScreen(data->display);
} else if (snum == DefaultScreen(data->display)) {
screen = 0;
} else {
screen = snum;
}
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (xinerama) {
if (get_visualinfo(data->display, 0, &vinfo) < 0) {
continue;
}
} else {
if (get_visualinfo(data->display, screen, &vinfo) < 0) {
continue;
}
}
#else
if (get_visualinfo(data->display, screen, &vinfo) < 0) {
continue;
}
#endif
displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
if (!displaydata) {
continue;
}
display_name[0] = '\0';
mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
/* We don't support palettized modes now */
SDL_free(displaydata);
continue;
}
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (xinerama) {
mode.w = xinerama[screen].width;
mode.h = xinerama[screen].height;
} else {
mode.w = DisplayWidth(data->display, screen);
mode.h = DisplayHeight(data->display, screen);
}
#else
mode.w = DisplayWidth(data->display, screen);
mode.h = DisplayHeight(data->display, screen);
#endif
mode.refresh_rate = 0;
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (!modedata) {
SDL_free(displaydata);
continue;
}
mode.driverdata = modedata;
#if SDL_VIDEO_DRIVER_X11_XINERAMA
/* Most of SDL's calls to X11 are unwaware of Xinerama, and to X11 standard calls, when Xinerama is active,
* there's only one screen available. So we force the screen number to zero and
* let Xinerama specific code handle specific functionality using displaydata->xinerama_info
*/
if (use_xinerama) {
displaydata->screen = 0;
displaydata->use_xinerama = use_xinerama;
displaydata->xinerama_info = xinerama[screen];
displaydata->xinerama_screen = screen;
}
else displaydata->screen = screen;
#else
displaydata->screen = screen;
#endif
displaydata->visual = vinfo.visual;
displaydata->depth = vinfo.depth;
Jan 7, 2017
Jan 7, 2017
745
746
747
/* We use the displaydata screen index here so that this works
for both the Xinerama case, where we get the overall DPI,
and the regular X11 screen info case. */
Jul 30, 2015
Jul 30, 2015
748
749
750
751
752
753
754
755
756
displaydata->hdpi = (float)DisplayWidth(data->display, displaydata->screen) * 25.4f /
DisplayWidthMM(data->display, displaydata->screen);
displaydata->vdpi = (float)DisplayHeight(data->display, displaydata->screen) * 25.4f /
DisplayHeightMM(data->display, displaydata->screen);
displaydata->ddpi = SDL_ComputeDiagonalDPI(DisplayWidth(data->display, displaydata->screen),
DisplayHeight(data->display, displaydata->screen),
(float)DisplayWidthMM(data->display, displaydata->screen) / 25.4f,
(float)DisplayHeightMM(data->display, displaydata->screen) / 25.4f);
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
pixmapFormats = X11_XListPixmapFormats(data->display, &n);
if (pixmapFormats) {
for (i = 0; i < n; ++i) {
if (pixmapFormats[i].depth == displaydata->depth) {
displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
break;
}
}
X11_XFree(pixmapFormats);
}
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (use_xinerama) {
displaydata->x = xinerama[screen].x_org;
displaydata->y = xinerama[screen].y_org;
}
else
#endif
{
displaydata->x = 0;
displaydata->y = 0;
}
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
if (!displaydata->use_xrandr &&
#if SDL_VIDEO_DRIVER_X11_XINERAMA
/* XVidMode only works on the screen at the origin */
(!displaydata->use_xinerama ||
(displaydata->x == 0 && displaydata->y == 0)) &&
#endif
use_vidmode) {
displaydata->use_vidmode = use_vidmode;
if (displaydata->use_xinerama) {
displaydata->vidmode_screen = 0;
} else {
displaydata->vidmode_screen = screen;
}
XF86VidModeGetModeInfo(data->display, displaydata->vidmode_screen, &modedata->vm_mode);
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
SDL_zero(display);
if (*display_name) {
display.name = display_name;
}
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = displaydata;
SDL_AddVideoDisplay(&display);
}
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (xinerama) X11_XFree(xinerama);
#endif
if (_this->num_displays == 0) {
return SDL_SetError("No available displays");
}
return 0;
}
void
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
{
Display *display = ((SDL_VideoData *) _this->driverdata)->display;
SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
int nmodes;
XF86VidModeModeInfo ** modes;
#endif
SDL_DisplayMode mode;
/* Unfortunately X11 requires the window to be created with the correct
* visual and depth ahead of time, but the SDL API allows you to create
* a window before setting the fullscreen display mode. This means that
* we have to use the same format for all windows and all display modes.
* (or support recreating the window with a new visual behind the scenes)
*/
mode.format = sdl_display->current_mode.format;
mode.driverdata = NULL;
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (data->use_xinerama) {
Aug 22, 2017
Aug 22, 2017
841
842
843
844
845
846
int screen_w;
int screen_h;
screen_w = DisplayWidth(display, data->screen);
screen_h = DisplayHeight(display, data->screen);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
if (data->use_vidmode && !data->xinerama_info.x_org && !data->xinerama_info.y_org &&
(screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
SDL_DisplayModeData *modedata;
/* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0
* if we're using vidmode.
*/
mode.w = screen_w;
mode.h = screen_h;
mode.refresh_rate = 0;
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (modedata) {
*modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
}
mode.driverdata = modedata;
Aug 7, 2015
Aug 7, 2015
861
862
863
if (!SDL_AddDisplayMode(sdl_display, &mode)) {
SDL_free(modedata);
}
864
865
866
867
868
869
870
871
872
873
874
875
876
}
else if (!data->use_xrandr)
{
SDL_DisplayModeData *modedata;
/* Add the current mode of each monitor otherwise if we can't get them from xrandr */
mode.w = data->xinerama_info.width;
mode.h = data->xinerama_info.height;
mode.refresh_rate = 0;
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (modedata) {
*modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
}
mode.driverdata = modedata;
Aug 7, 2015
Aug 7, 2015
877
878
879
if (!SDL_AddDisplayMode(sdl_display, &mode)) {
SDL_free(modedata);
}
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
#if SDL_VIDEO_DRIVER_X11_XRANDR
if (data->use_xrandr) {
XRRScreenResources *res;
res = X11_XRRGetScreenResources (display, RootWindow(display, data->screen));
if (res) {
SDL_DisplayModeData *modedata;
XRROutputInfo *output_info;
int i;
output_info = X11_XRRGetOutputInfo(display, res, data->xrandr_output);
if (output_info && output_info->connection != RR_Disconnected) {
for (i = 0; i < output_info->nmode; ++i) {
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (!modedata) {
continue;
}
mode.driverdata = modedata;
Aug 7, 2015
Aug 7, 2015
904
if (!SetXRandRModeInfo(display, res, output_info->crtc, output_info->modes[i], &mode) ||
Aug 7, 2015
Aug 7, 2015
905
!SDL_AddDisplayMode(sdl_display, &mode)) {
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
SDL_free(modedata);
}
}
}
X11_XRRFreeOutputInfo(output_info);
X11_XRRFreeScreenResources(res);
}
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
if (data->use_vidmode &&
X11_XF86VidModeGetAllModeLines(display, data->vidmode_screen, &nmodes, &modes)) {
int i;
SDL_DisplayModeData *modedata;
#ifdef X11MODES_DEBUG
printf("VidMode modes: (unsorted)\n");
for (i = 0; i < nmodes; ++i) {
printf("Mode %d: %d x %d @ %d, flags: 0x%x\n", i,
modes[i]->hdisplay, modes[i]->vdisplay,
CalculateXVidModeRefreshRate(modes[i]), modes[i]->flags);
}
#endif
for (i = 0; i < nmodes; ++i) {
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (!modedata) {
continue;
}
mode.driverdata = modedata;
Aug 7, 2015
Aug 7, 2015
938
if (!SetXVidModeModeInfo(modes[i], &mode) || !SDL_AddDisplayMode(sdl_display, &mode)) {
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
SDL_free(modedata);
}
}
X11_XFree(modes);
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
if (!data->use_xrandr && !data->use_vidmode) {
SDL_DisplayModeData *modedata;
/* Add the desktop mode */
mode = sdl_display->desktop_mode;
modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
if (modedata) {
*modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
}
mode.driverdata = modedata;
Aug 7, 2015
Aug 7, 2015
956
957
958
if (!SDL_AddDisplayMode(sdl_display, &mode)) {
SDL_free(modedata);
}
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
}
int
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
{
SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
Display *display = viddata->display;
SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
viddata->last_mode_change_deadline = SDL_GetTicks() + (PENDING_FOCUS_TIME * 2);
#if SDL_VIDEO_DRIVER_X11_XRANDR
if (data->use_xrandr) {
XRRScreenResources *res;
XRROutputInfo *output_info;
XRRCrtcInfo *crtc;
Status status;
res = X11_XRRGetScreenResources (display, RootWindow(display, data->screen));
if (!res) {
return SDL_SetError("Couldn't get XRandR screen resources");
}
output_info = X11_XRRGetOutputInfo(display, res, data->xrandr_output);
if (!output_info || output_info->connection == RR_Disconnected) {
X11_XRRFreeScreenResources(res);
return SDL_SetError("Couldn't get XRandR output info");
}
crtc = X11_XRRGetCrtcInfo(display, res, output_info->crtc);
if (!crtc) {
X11_XRRFreeOutputInfo(output_info);
X11_XRRFreeScreenResources(res);
return SDL_SetError("Couldn't get XRandR crtc info");
}
status = X11_XRRSetCrtcConfig (display, res, output_info->crtc, CurrentTime,
crtc->x, crtc->y, modedata->xrandr_mode, crtc->rotation,
&data->xrandr_output, 1);