This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_x11modes.c
940 lines (843 loc) · 31 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2012 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
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_config.h"
#if SDL_VIDEO_DRIVER_X11
25
#include "SDL_hints.h"
26
27
#include "SDL_x11video.h"
28
#define X11MODES_DEBUG
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
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 = XGetVisualInfo(display, VisualIDMask, &template, &nvis);
if (vi) {
*vinfo = *vi;
XFree(vi);
return 0;
}
}
depth = DefaultDepth(display, screen);
if ((X11_UseDirectColorVisuals() &&
XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
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 = XVisualIDFromVisual(visual);
vi = XGetVisualInfo(display, VisualIDMask, vinfo, &nvis);
if (vi) {
*vinfo = *vi;
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 = XListPixmapFormats(display, &n);
if (p) {
for (i = 0; i < n; ++i) {
if (p[i].depth == 24) {
bpp = p[i].bits_per_pixel;
break;
}
}
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;
}
break;
case 1:
if (BitmapBitOrder(display) == LSBFirst) {
return SDL_PIXELFORMAT_INDEX1LSB;
} else {
return SDL_PIXELFORMAT_INDEX1MSB;
}
break;
}
}
return SDL_PIXELFORMAT_UNKNOWN;
}
135
136
137
#if SDL_VIDEO_DRIVER_X11_XINERAMA
static SDL_bool CheckXinerama(Display * display, int *major, int *minor);
#endif
138
139
140
141
142
int
X11_InitModes(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
143
int screen, screencount;
144
145
146
147
148
149
150
151
152
153
154
#if SDL_VIDEO_DRIVER_X11_XINERAMA
int xinerama_major, xinerama_minor;
XineramaScreenInfo * xinerama = NULL;
/* 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)) {
xinerama = XineramaQueryScreens(data->display, &screencount);
}
155
if (!xinerama) {
156
157
158
159
160
161
162
screencount = ScreenCount(data->display);
}
#else
screencount = ScreenCount(data->display);
#endif
for (screen = 0; screen < screencount; ++screen) {
163
164
165
166
167
168
XVisualInfo vinfo;
SDL_VideoDisplay display;
SDL_DisplayData *displaydata;
SDL_DisplayMode mode;
XPixmapFormatValues *pixmapFormats;
int i, n;
169
170
171
172
173
174
175
176
177
178
179
180
#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
181
182
183
if (get_visualinfo(data->display, screen, &vinfo) < 0) {
continue;
}
184
#endif
185
186
187
188
189
190
mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
/* We don't support palettized modes now */
continue;
}
191
192
193
194
195
196
197
198
199
200
#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
201
202
mode.w = DisplayWidth(data->display, screen);
mode.h = DisplayHeight(data->display, screen);
203
#endif
204
205
206
mode.refresh_rate = 0;
mode.driverdata = NULL;
207
displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
208
209
210
if (!displaydata) {
continue;
}
211
212
213
214
215
216
217
218
219
#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 (xinerama) {
displaydata->screen = 0;
displaydata->use_xinerama = xinerama_major * 100 + xinerama_minor;
displaydata->xinerama_info = xinerama[screen];
220
displaydata->xinerama_screen = screen;
221
222
223
}
else displaydata->screen = screen;
#else
224
displaydata->screen = screen;
225
#endif
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
displaydata->visual = vinfo.visual;
displaydata->depth = vinfo.depth;
displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
pixmapFormats = 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;
}
}
XFree(pixmapFormats);
}
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = displaydata;
SDL_AddVideoDisplay(&display);
}
247
248
249
250
251
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (xinerama) XFree(xinerama);
#endif
252
253
254
255
256
257
258
259
260
261
262
263
264
265
if (_this->num_displays == 0) {
SDL_SetError("No available displays");
return -1;
}
return 0;
}
/* Global for the error handler */
int vm_event, vm_error = -1;
#if SDL_VIDEO_DRIVER_X11_XINERAMA
static SDL_bool
CheckXinerama(Display * display, int *major, int *minor)
{
266
267
int event_base = 0;
int error_base = 0;
268
269
270
271
272
273
const char *env;
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
274
env = SDL_GetHint(SDL_HINT_VIDEO_X11_XINERAMA);
275
if (env && !SDL_atoi(env)) {
276
#ifdef X11MODES_DEBUG
277
printf("Xinerama disabled due to hint\n");
278
#endif
279
280
281
282
return SDL_FALSE;
}
if (!SDL_X11_HAVE_XINERAMA) {
283
284
285
#ifdef X11MODES_DEBUG
printf("Xinerama support not available\n");
#endif
286
287
288
289
return SDL_FALSE;
}
/* Query the extension version */
290
291
if (!XineramaQueryExtension(display, &event_base, &error_base) ||
!XineramaQueryVersion(display, major, minor) ||
292
!XineramaIsActive(display)) {
293
294
295
#ifdef X11MODES_DEBUG
printf("Xinerama not active on the display\n");
#endif
296
297
return SDL_FALSE;
}
298
299
300
#ifdef X11MODES_DEBUG
printf("Xinerama available!\n");
#endif
301
302
303
304
305
306
307
308
309
310
311
312
313
314
return SDL_TRUE;
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
#if SDL_VIDEO_DRIVER_X11_XRANDR
static SDL_bool
CheckXRandR(Display * display, int *major, int *minor)
{
const char *env;
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
315
env = SDL_GetHint(SDL_HINT_VIDEO_X11_XRANDR);
316
if (env && !SDL_atoi(env)) {
317
#ifdef X11MODES_DEBUG
318
printf("XRandR disabled due to hint\n");
319
#endif
320
321
322
323
return SDL_FALSE;
}
if (!SDL_X11_HAVE_XRANDR) {
324
325
326
#ifdef X11MODES_DEBUG
printf("XRandR support not available\n");
#endif
327
328
329
330
331
return SDL_FALSE;
}
/* Query the extension version */
if (!XRRQueryVersion(display, major, minor)) {
332
333
334
#ifdef X11MODES_DEBUG
printf("XRandR not active on the display\n");
#endif
335
336
return SDL_FALSE;
}
337
338
339
#ifdef X11MODES_DEBUG
printf("XRandR available!\n");
#endif
340
341
return SDL_TRUE;
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#define XRANDR_ROTATION_LEFT (1 << 1)
#define XRANDR_ROTATION_RIGHT (1 << 3)
static void
get_xrandr_mode_size(XRRModeInfo *mode, Rotation rotation, int *w, int *h)
{
if (rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT)) {
*w = mode->height;
*h = mode->width;
} else {
*w = mode->width;
*h = mode->height;
}
}
357
358
359
360
361
362
363
364
365
366
367
368
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
static SDL_bool
CheckVidMode(Display * display, int *major, int *minor)
{
const char *env;
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
369
env = SDL_GetHint(SDL_HINT_VIDEO_X11_XVIDMODE);
370
if (env && !SDL_atoi(env)) {
371
#ifdef X11MODES_DEBUG
372
printf("XVidMode disabled due to hint\n");
373
#endif
374
375
376
377
return SDL_FALSE;
}
if (!SDL_X11_HAVE_XVIDMODE) {
378
379
380
#ifdef X11MODES_DEBUG
printf("XVidMode support not available\n");
#endif
381
382
383
384
385
386
387
return SDL_FALSE;
}
/* Query the extension version */
vm_error = -1;
if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
|| !XF86VidModeQueryVersion(display, major, minor)) {
388
389
390
#ifdef X11MODES_DEBUG
printf("XVidMode not active on the display\n");
#endif
391
392
return SDL_FALSE;
}
393
394
395
#ifdef X11MODES_DEBUG
printf("XVidMode available!\n");
#endif
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
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 = 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
calculate_rate(XF86VidModeModeInfo * info)
{
return (info->htotal
&& info->vtotal) ? (1000 * info->dotclock / (info->htotal *
info->vtotal)) : 0;
}
static void
save_mode(Display * display, SDL_DisplayData * data)
{
XF86VidModeGetModeInfo(display, data->screen,
&data->saved_mode);
XF86VidModeGetViewPort(display, data->screen,
&data->saved_view.x,
&data->saved_view.y);
}
/*
static void
restore_mode(Display * display, SDL_DisplayData * data)
{
XF86VidModeModeInfo mode;
if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
if (SDL_memcmp(&mode, &data->saved_mode, sizeof(mode)) != 0) {
XF86VidModeSwitchToMode(display, data->screen, &data->saved_mode);
}
}
if ((data->saved_view.x != 0) || (data->saved_view.y != 0)) {
XF86VidModeSetViewPort(display, data->screen,
data->saved_view.x,
data->saved_view.y);
}
}
*/
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
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_XRANDR
int xrandr_major, xrandr_minor;
int nsizes, nrates;
XRRScreenSize *sizes;
short *rates;
#endif
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
int vm_major, vm_minor;
int nmodes;
XF86VidModeModeInfo ** modes;
#endif
int screen_w;
int screen_h;
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;
screen_w = DisplayWidth(display, data->screen);
screen_h = DisplayHeight(display, data->screen);
#if SDL_VIDEO_DRIVER_X11_XINERAMA
496
497
498
499
500
501
if (data->use_xinerama) {
/* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0 */
if (!data->xinerama_info.x_org && !data->xinerama_info.y_org &&
(screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
mode.w = screen_w;
mode.h = screen_h;
502
503
504
mode.refresh_rate = 0;
SDL_AddDisplayMode(sdl_display, &mode);
}
505
506
507
508
509
510
/* Add the head xinerama mode */
mode.w = data->xinerama_info.width;
mode.h = data->xinerama_info.height;
mode.refresh_rate = 0;
SDL_AddDisplayMode(sdl_display, &mode);
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
#if SDL_VIDEO_DRIVER_X11_XRANDR
/* XRandR */
/* require at least XRandR v1.0 (arbitrary) */
if (CheckXRandR(display, &xrandr_major, &xrandr_minor)
&& xrandr_major >= 1) {
#ifdef X11MODES_DEBUG
fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
xrandr_major, xrandr_minor);
#endif
/* save the screen configuration since we must reference it
each time we toggle modes.
*/
data->screen_config =
XRRGetScreenInfo(display, RootWindow(display, data->screen));
/* retrieve the list of resolution */
sizes = XRRConfigSizes(data->screen_config, &nsizes);
if (nsizes > 0) {
int i, j;
for (i = 0; i < nsizes; i++) {
535
get_xrandr_mode_size(XRRModeInfo *mode, Rotation rotation, int *w, int *h)
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
mode.w = sizes[i].width;
mode.h = sizes[i].height;
rates = XRRConfigRates(data->screen_config, i, &nrates);
for (j = 0; j < nrates; ++j) {
mode.refresh_rate = rates[j];
#ifdef X11MODES_DEBUG
fprintf(stderr,
"XRANDR: mode = %4d[%d], w = %4d, h = %4d, rate = %4d\n",
i, j, mode.w, mode.h, mode.refresh_rate);
#endif
SDL_AddDisplayMode(sdl_display, &mode);
}
}
data->use_xrandr = xrandr_major * 100 + xrandr_minor;
data->saved_size =
XRRConfigCurrentConfiguration(data->screen_config,
&data->saved_rotation);
data->saved_rate = XRRConfigCurrentRate(data->screen_config);
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
/* XVidMode */
if (!data->use_xrandr &&
#if SDL_VIDEO_DRIVER_X11_XINERAMA
(!data->use_xinerama || data->xinerama_info.screen_number == 0) &&
#endif
CheckVidMode(display, &vm_major, &vm_minor) &&
XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
int i;
#ifdef X11MODES_DEBUG
printf("VidMode modes: (unsorted)\n");
for (i = 0; i < nmodes; ++i) {
printf("Mode %d: %d x %d @ %d\n", i,
modes[i]->hdisplay, modes[i]->vdisplay,
calculate_rate(modes[i]));
}
#endif
for (i = 0; i < nmodes; ++i) {
mode.w = modes[i]->hdisplay;
mode.h = modes[i]->vdisplay;
mode.refresh_rate = calculate_rate(modes[i]);
SDL_AddDisplayMode(sdl_display, &mode);
}
XFree(modes);
data->use_vidmode = vm_major * 100 + vm_minor;
save_mode(display, data);
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
if (!data->use_xrandr && !data->use_vidmode) {
mode.w = screen_w;
mode.h = screen_h;
mode.refresh_rate = 0;
SDL_AddDisplayMode(sdl_display, &mode);
}
#ifdef X11MODES_DEBUG
if (data->use_xinerama) {
printf("Xinerama is enabled\n");
}
if (data->use_xrandr) {
printf("XRandR is enabled\n");
}
if (data->use_vidmode) {
printf("VidMode is enabled\n");
}
#endif /* X11MODES_DEBUG */
}
static void
get_real_resolution(Display * display, SDL_DisplayData * data, int *w, int *h,
int *rate)
{
#if SDL_VIDEO_DRIVER_X11_XRANDR
if (data->use_xrandr) {
int nsizes;
XRRScreenSize *sizes;
sizes = XRRConfigSizes(data->screen_config, &nsizes);
if (nsizes > 0) {
int cur_size;
Rotation cur_rotation;
cur_size =
XRRConfigCurrentConfiguration(data->screen_config,
&cur_rotation);
*w = sizes[cur_size].width;
*h = sizes[cur_size].height;
*rate = XRRConfigCurrentRate(data->screen_config);
#ifdef X11MODES_DEBUG
fprintf(stderr,
"XRANDR: get_real_resolution: w = %d, h = %d, rate = %d\n",
*w, *h, *rate);
#endif
return;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
if (data->use_vidmode) {
XF86VidModeModeInfo mode;
if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
*w = mode.hdisplay;
*h = mode.vdisplay;
*rate = calculate_rate(&mode);
return;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
655
656
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (data->use_xinerama) {
657
658
659
660
661
int screencount;
XineramaScreenInfo *xinerama;
/* Update the current screen layout information */
xinerama = XineramaQueryScreens(display, &screencount);
662
663
if (xinerama && data->xinerama_screen < screencount) {
data->xinerama_info = xinerama[data->xinerama_screen];
664
665
666
}
if (xinerama) XFree(xinerama);
667
668
669
670
671
672
673
*w = data->xinerama_info.width;
*h = data->xinerama_info.height;
*rate = 0;
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
674
675
676
677
678
679
680
681
682
683
684
685
686
*w = DisplayWidth(display, data->screen);
*h = DisplayHeight(display, data->screen);
*rate = 0;
}
static void
set_best_resolution(Display * display, SDL_DisplayData * data, int w, int h,
int rate)
{
int real_w, real_h, real_rate;
/* check current mode so we can avoid uneccessary mode changes */
get_real_resolution(display, data, &real_w, &real_h, &real_rate);
687
688
if (w == real_w && h == real_h && (!rate || !real_rate || rate == real_rate)) {
689
690
691
return;
}
692
693
694
695
696
697
698
699
700
701
702
703
#if SDL_VIDEO_DRIVER_X11_XRANDR
if (data->use_xrandr) {
#ifdef X11MODES_DEBUG
fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
w, h);
#endif
int i, nsizes, nrates;
int best;
int best_rate;
XRRScreenSize *sizes;
short *rates;
704
705
706
707
708
709
710
711
712
713
714
715
716
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if (data->use_xrandr >= 102 && data->use_xinerama) {
/* See http://cgit.freedesktop.org/xorg/app/xrandr/tree/xrandr.c for the rationale behind this */
/* Note by Gabriel: the refresh rate is ignored in this code, and seeing that both xrandr
* and nvidia-settings don't provide a way to set it when Xinerama is enabled, it may not be possible to set it */
int screencount;
XineramaScreenInfo * xinerama = NULL;
XRRScreenResources *res;
/* Update the current screen layout information */
xinerama = XineramaQueryScreens(display, &screencount);
if (xinerama && data->xinerama_screen < screencount) {
data->xinerama_info = xinerama[data->xinerama_screen];
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
745
746
747
748
749
750
751
752
753
754
755
756
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
if (xinerama) XFree(xinerama);
res = XRRGetScreenResources (display, RootWindow(display, data->screen));
if (res) {
XRROutputInfo *output_info = NULL;
XRRCrtcInfo *crtc = NULL;
XRRModeInfo *mode_info, *best_mode_info = NULL;
int mode_index = 0, output_mode_index = 0, output;
int mode_w, mode_h, best_w = 0, best_h = 0;
for ( output = 0; output < res->noutput; output++) {
output_info = XRRGetOutputInfo (display, res, res->outputs[output]);
if (!output_info || !output_info->crtc) {
XRRFreeOutputInfo(output_info);
continue;
}
crtc = XRRGetCrtcInfo (display, res, output_info->crtc);
if (!crtc || data->xinerama_info.x_org != crtc->x || data->xinerama_info.y_org != crtc->y) {
XRRFreeCrtcInfo(crtc);
continue;
}
/* The CRT offset matches the Xinerama mode, let's hope it's the right one! */
#ifdef X11MODES_DEBUG
fprintf(stderr, "XRANDR: set_best_resolution, matched Xinerama screen %d to CRT %d at %d,%d\n",
data->xinerama_info.screen_number, output_info->crtc, crtc->x, crtc->y);
#endif
/* Find out the best mode we can use */
for (mode_index = 0; mode_index < res->nmode; mode_index++) {
mode_info = &res->modes[mode_index];
get_xrandr_mode_size(mode_info, crtc->rotation, &mode_w, &mode_h);
if (mode_w >= w && mode_h >= h) {
/* This may be a useful mode, check out if it belongs to the correct output */
#ifdef X11MODES_DEBUG
fprintf(stderr, "Evaluating valid mode %d, w: %d, h: %d\n", mode_index, mode_info->width, mode_info->height);
#endif
for ( output_mode_index = 0; output_mode_index < output_info->nmode; output_mode_index++) {
if (output_info->modes[output_mode_index] == mode_info->id) {
break;
}
}
if (output_mode_index < output_info->nmode) {
#ifdef X11MODES_DEBUG
fprintf(stderr, "Mode belongs to the desired output %d w: %d, h: %d, rotation: %x\n", mode_index, mode_info->width, mode_info->height, crtc->rotation);
#endif
/* We have a mode that belongs to the right output and that can contain the w,h required, see if it's smaller than the current one */
if ( !best_mode_info || mode_w < best_w || (mode_w == best_w && mode_h < best_h) ) {
best_mode_info = mode_info;
best_w = mode_w;
best_h = mode_h;
}
}
#ifdef X11MODES_DEBUG
else {
fprintf(stderr, "Discarding mode because it does not belong to the desired output %d, w: %d, h: %d, rotation: %d\n", mode_index, mode_info->width, mode_info->height, crtc->rotation);
}
#endif
}
}
if (best_mode_info) {
#ifdef X11MODES_DEBUG
fprintf(stderr, "XRANDR: set_best_resolution, setting mode w = %d, h = %d on output: %d, CRT with offsets: %d,%d for Xinerama screen: %d\n",
best_mode_info->width, best_mode_info->height, output, crtc->x, crtc->y, data->xinerama_info.screen_number);
#endif
XGrabServer (display);
XRRSetCrtcConfig (display, res, output_info->crtc, CurrentTime,
crtc->x, crtc->y, best_mode_info->id, crtc->rotation,
&res->outputs[output], 1);
/* TODO: Handle screen rotations, should we call XRRSetCrtcTransform if Xrandr >=1.3 ? */
XUngrabServer (display);
} else {
/* If we reach here, we have found the right screen but no valid best mode */
SDL_SetError("The selected screen can't support a resolution of the size desired");
}
XRRFreeCrtcInfo(crtc);
XRRFreeOutputInfo(output_info);
break;
}
XRRFreeScreenResources(res);
799
800
}
}
801
802
803
804
805
806
807
808
809
810
811
812
813
else
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
{
/* Use older xrandr functions that don't play along very nicely with multi monitors setups */
/* find the smallest resolution that is at least as big as the user requested */
best = -1;
sizes = XRRConfigSizes(data->screen_config, &nsizes);
for (i = 0; i < nsizes; ++i) {
if (sizes[i].width < w || sizes[i].height < h) {
continue;
}
if (sizes[i].width == w && sizes[i].height == h) {
best = i;
814
815
break;
}
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
if (best == -1 ||
(sizes[i].width < sizes[best].width) ||
(sizes[i].width == sizes[best].width
&& sizes[i].height < sizes[best].height)) {
best = i;
}
}
if (best >= 0) {
best_rate = 0;
rates = XRRConfigRates(data->screen_config, best, &nrates);
for (i = 0; i < nrates; ++i) {
if (rates[i] == rate) {
best_rate = rate;
break;
831
}
832
833
834
835
836
837
838
839
840
if (!rate) {
/* Higher is better, right? */
if (rates[i] > best_rate) {
best_rate = rates[i];
}
} else {
if (SDL_abs(rates[i] - rate) < SDL_abs(best_rate - rate)) {
best_rate = rates[i];
}
841
842
}
}
843
844
845
846
XRRSetScreenConfigAndRate(display, data->screen_config,
RootWindow(display, data->screen), best,
data->saved_rotation, best_rate,
CurrentTime);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
}
}
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
if (data->use_vidmode) {
XF86VidModeModeInfo ** modes;
int i, nmodes;
int best;
if (XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
best = -1;
for (i = 0; i < nmodes; ++i) {
if (modes[i]->hdisplay < w || modes[i]->vdisplay < h) {
continue;
}
if (best == -1 ||
(modes[i]->hdisplay < modes[best]->hdisplay) ||
(modes[i]->hdisplay == modes[best]->hdisplay
&& modes[i]->vdisplay < modes[best]->vdisplay)) {
best = i;
continue;
}
if ((modes[i]->hdisplay == modes[best]->hdisplay) &&
(modes[i]->vdisplay == modes[best]->vdisplay)) {
if (!rate) {
/* Higher is better, right? */
if (calculate_rate(modes[i]) >
calculate_rate(modes[best])) {
best = i;
}
} else {
if (SDL_abs(calculate_rate(modes[i]) - rate) <
SDL_abs(calculate_rate(modes[best]) - rate)) {
best = i;
}
}
}
}
if (best >= 0) {
#ifdef X11MODES_DEBUG
printf("Best Mode %d: %d x %d @ %d\n", best,
modes[best]->hdisplay, modes[best]->vdisplay,
calculate_rate(modes[best]));
#endif
XF86VidModeSwitchToMode(display, data->screen, modes[best]);
}
XFree(modes);
}
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
}
int
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
{
Display *display = ((SDL_VideoData *) _this->driverdata)->display;
SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
set_best_resolution(display, data, mode->w, mode->h, mode->refresh_rate);
return 0;
}
void
X11_QuitModes(_THIS)
{
}
918
919
920
int
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
{
921
Display *display = ((SDL_VideoData *) _this->driverdata)->display;
922
SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
923
924
925
926
927
int real_rate;
rect->x = 0;
rect->y = 0;
get_real_resolution(display, data, &rect->w, &rect->h, &real_rate);
928
929
#if SDL_VIDEO_DRIVER_X11_XINERAMA
930
if (data->use_xinerama) {
931
932
933
934
rect->x = data->xinerama_info.x_org;
rect->y = data->xinerama_info.y_org;
}
#endif
935
return 0;
936
937
}
938
939
940
#endif /* SDL_VIDEO_DRIVER_X11 */
/* vi: set ts=4 sw=4 expandtab: */