/
SDL_x11modes.c
1142 lines (1014 loc) · 35.6 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
/* Utilities for getting and setting the X display mode */
26
#include <stdio.h>
27
28
29
#include "SDL_timer.h"
#include "SDL_events.h"
30
#include "../../events/SDL_events_c.h"
31
32
33
#include "SDL_x11video.h"
#include "SDL_x11wm_c.h"
#include "SDL_x11modes_c.h"
34
#include "SDL_x11image_c.h"
35
36
37
/*#define X11MODES_DEBUG*/
38
#define MAX(a, b) (a > b ? a : b)
39
40
41
42
43
44
45
46
47
48
49
50
51
#if SDL_VIDEO_DRIVER_X11_XRANDR
static int cmpmodelist(const void *va, const void *vb)
{
const SDL_Rect *a = *(const SDL_Rect **)va;
const SDL_Rect *b = *(const SDL_Rect **)vb;
if ( a->w == b->w )
return b->h - a->h;
else
return b->w - a->w;
}
#endif
52
#if SDL_VIDEO_DRIVER_X11_VIDMODE
53
Bool SDL_NAME(XF86VidModeGetModeInfo)(Display *dpy, int scr, SDL_NAME(XF86VidModeModeInfo) *info)
54
{
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Bool retval;
int dotclock;
SDL_NAME(XF86VidModeModeLine) l;
SDL_memset(&l, 0, sizeof(l));
retval = SDL_NAME(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;
74
}
75
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
76
77
#if SDL_VIDEO_DRIVER_X11_VIDMODE
78
79
static void save_mode(_THIS)
{
80
SDL_memset(&saved_mode, 0, sizeof(saved_mode));
81
82
SDL_NAME(XF86VidModeGetModeInfo)(SDL_Display,SDL_Screen,&saved_mode);
SDL_NAME(XF86VidModeGetViewPort)(SDL_Display,SDL_Screen,&saved_view.x,&saved_view.y);
83
84
85
}
#endif
86
#if SDL_VIDEO_DRIVER_X11_VIDMODE
87
88
static void restore_mode(_THIS)
{
89
SDL_NAME(XF86VidModeModeLine) mode;
90
91
int unused;
92
if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
93
94
if ( (saved_mode.hdisplay != mode.hdisplay) ||
(saved_mode.vdisplay != mode.vdisplay) ) {
95
SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, &saved_mode);
96
97
98
}
}
if ( (saved_view.x != 0) || (saved_view.y != 0) ) {
99
SDL_NAME(XF86VidModeSetViewPort)(SDL_Display, SDL_Screen, saved_view.x, saved_view.y);
100
101
102
103
}
}
#endif
104
#if SDL_VIDEO_DRIVER_X11_VIDMODE
105
106
static int cmpmodes(const void *va, const void *vb)
{
107
108
const SDL_NAME(XF86VidModeModeInfo) *a = *(const SDL_NAME(XF86VidModeModeInfo)**)va;
const SDL_NAME(XF86VidModeModeInfo) *b = *(const SDL_NAME(XF86VidModeModeInfo)**)vb;
109
110
111
112
if ( a->hdisplay == b->hdisplay )
return b->vdisplay - a->vdisplay;
else
return b->hdisplay - a->hdisplay;
113
114
115
}
#endif
116
117
static void get_real_resolution(_THIS, int* w, int* h);
118
119
static void set_best_resolution(_THIS, int width, int height)
{
120
#if SDL_VIDEO_DRIVER_X11_VIDMODE
121
if ( use_vidmode ) {
122
123
SDL_NAME(XF86VidModeModeLine) mode;
SDL_NAME(XF86VidModeModeInfo) **modes;
124
125
int i;
int nmodes;
126
int best = -1;
127
128
if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &i, &mode) &&
129
SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes) ) {
130
for ( i = 0; i < nmodes ; i++ ) {
131
if ( (modes[i]->hdisplay == width) &&
132
133
134
135
136
137
138
139
140
141
142
143
(modes[i]->vdisplay == height) ) {
best = i;
break;
}
if ( modes[i]->hdisplay >= width &&
modes[i]->vdisplay >= height ) {
if ( best < 0 ||
(modes[i]->hdisplay < modes[best]->hdisplay &&
modes[i]->vdisplay <= modes[best]->vdisplay) ||
(modes[i]->vdisplay < modes[best]->vdisplay &&
modes[i]->hdisplay <= modes[best]->hdisplay) ) {
best = i;
144
145
}
}
146
}
147
148
149
150
151
152
153
154
155
if ( best >= 0 &&
((modes[best]->hdisplay != mode.hdisplay) ||
(modes[best]->vdisplay != mode.vdisplay)) ) {
#ifdef X11MODES_DEBUG
printf("Best Mode %d: %d x %d @ %d\n", best,
modes[best]->hdisplay, modes[best]->vdisplay,
(modes[best]->htotal && modes[best]->vtotal) ? (1000 * modes[best]->dotclock / (modes[best]->htotal * modes[best]->vtotal)) : 0 );
#endif
SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, modes[best]);
156
}
157
XFree(modes);
158
159
}
}
160
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
161
162
/* XiG */
163
#if SDL_VIDEO_DRIVER_X11_XME
164
if ( use_xme && SDL_modelist ) {
165
int i;
166
167
168
169
170
#ifdef X11MODES_DEBUG
fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n",
width, height);
#endif
171
for ( i=0; SDL_modelist[i]; ++i ) {
172
if ( (SDL_modelist[i]->w >= width) &&
173
(SDL_modelist[i]->h >= height) ) {
174
175
break;
}
176
}
177
178
179
if ( SDL_modelist[i] ) { /* found one, lets try it */
int w, h;
180
181
/* check current mode so we can avoid uneccessary mode changes */
182
get_real_resolution(this, &w, &h);
183
184
if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
185
#ifdef X11MODES_DEBUG
186
187
fprintf(stderr, "XME: set_best_resolution: "
"XiGMiscChangeResolution: %d %d\n",
188
189
SDL_modelist[i]->w, SDL_modelist[i]->h);
#endif
190
191
192
193
194
195
XiGMiscChangeResolution(SDL_Display,
SDL_Screen,
0, /* view */
SDL_modelist[i]->w,
SDL_modelist[i]->h,
0);
196
XSync(SDL_Display, False);
197
198
199
}
}
}
200
#endif /* SDL_VIDEO_DRIVER_X11_XME */
201
202
#if SDL_VIDEO_DRIVER_X11_XRANDR
203
if ( use_xrandr && SDL_modelist ) {
204
#ifdef X11MODES_DEBUG
205
206
207
fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
width, height);
#endif
208
209
210
211
212
213
214
215
216
int i, nsizes;
XRRScreenSize *sizes;
/* find the smallest resolution that is at least as big as the user requested */
sizes = XRRConfigSizes(screen_config, &nsizes);
for ( i = (nsizes-1); i >= 0; i-- ) {
if ( (SDL_modelist[i]->w >= width) &&
(SDL_modelist[i]->h >= height) ) {
break;
217
}
219
220
221
if ( i >= 0 && SDL_modelist[i] ) { /* found one, lets try it */
int w, h;
222
223
224
/* check current mode so we can avoid uneccessary mode changes */
get_real_resolution(this, &w, &h);
225
226
227
if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
int size_id;
228
229
#ifdef X11MODES_DEBUG
230
231
232
fprintf(stderr, "XRANDR: set_best_resolution: "
"XXRSetScreenConfig: %d %d\n",
SDL_modelist[i]->w, SDL_modelist[i]->h);
233
234
#endif
235
236
237
238
239
/* find the matching size entry index */
for ( size_id = 0; size_id < nsizes; ++size_id ) {
if ( (sizes[size_id].width == SDL_modelist[i]->w) &&
(sizes[size_id].height == SDL_modelist[i]->h) )
break;
240
}
241
242
243
XRRSetScreenConfig(SDL_Display, screen_config, SDL_Root,
size_id, saved_rotation, CurrentTime);
244
245
246
247
}
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
248
249
250
251
}
static void get_real_resolution(_THIS, int* w, int* h)
{
252
#if SDL_VIDEO_DRIVER_X11_XME
253
254
255
256
257
if ( use_xme ) {
int ractive;
XiGMiscResolutionInfo *modelist;
XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
258
259
0, /* view */
&ractive, &modelist);
260
261
*w = modelist[ractive].width;
*h = modelist[ractive].height;
262
#ifdef X11MODES_DEBUG
263
264
265
266
267
fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h);
#endif
XFree(modelist);
return;
}
268
269
#endif /* SDL_VIDEO_DRIVER_X11_XME */
270
271
272
273
274
275
276
277
278
279
280
281
282
#if SDL_VIDEO_DRIVER_X11_VIDMODE
if ( use_vidmode ) {
SDL_NAME(XF86VidModeModeLine) mode;
int unused;
if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
*w = mode.hdisplay;
*h = mode.vdisplay;
return;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#if SDL_VIDEO_DRIVER_X11_XRANDR
if ( use_xrandr ) {
int nsizes;
XRRScreenSize* sizes;
sizes = XRRConfigSizes(screen_config, &nsizes);
if ( nsizes > 0 ) {
int cur_size;
Rotation cur_rotation;
cur_size = XRRConfigCurrentConfiguration(screen_config, &cur_rotation);
if ( cur_size >= 0 && cur_size < nsizes ) {
*w = sizes[cur_size].width;
*h = sizes[cur_size].height;
}
298
#ifdef X11MODES_DEBUG
299
300
301
302
303
304
fprintf(stderr, "XRANDR: get_real_resolution: w = %d h = %d\n", *w, *h);
#endif
return;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
305
306
307
308
309
310
311
312
313
#if SDL_VIDEO_DRIVER_X11_XINERAMA
if ( use_xinerama ) {
*w = xinerama_info.width;
*h = xinerama_info.height;
return;
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
314
315
316
317
318
319
320
321
322
*w = DisplayWidth(SDL_Display, SDL_Screen);
*h = DisplayHeight(SDL_Display, SDL_Screen);
}
/* Called after mapping a window - waits until the window is mapped */
void X11_WaitMapped(_THIS, Window win)
{
XEvent event;
do {
323
XMaskEvent(SDL_Display, StructureNotifyMask, &event);
324
325
326
327
328
329
330
331
} while ( (event.type != MapNotify) || (event.xmap.event != win) );
}
/* Called after unmapping a window - waits until the window is unmapped */
void X11_WaitUnmapped(_THIS, Window win)
{
XEvent event;
do {
332
XMaskEvent(SDL_Display, StructureNotifyMask, &event);
333
334
335
336
337
} while ( (event.type != UnmapNotify) || (event.xunmap.event != win) );
}
static void move_cursor_to(_THIS, int x, int y)
{
338
XWarpPointer(SDL_Display, None, SDL_Root, 0, 0, 0, 0, x, y);
339
340
}
341
342
343
344
345
346
347
348
349
350
351
352
static int add_default_visual(_THIS)
{
int i;
int n = this->hidden->nvisuals;
for (i=0; i<n; i++) {
if (this->hidden->visuals[i].visual == DefaultVisual(SDL_Display, SDL_Screen)) return n;
}
this->hidden->visuals[n].depth = DefaultDepth(SDL_Display, SDL_Screen);;
this->hidden->visuals[n].visual = DefaultVisual(SDL_Display, SDL_Screen);;
this->hidden->nvisuals++;
return(this->hidden->nvisuals);
}
353
354
355
static int add_visual(_THIS, int depth, int class)
{
XVisualInfo vi;
356
if(XMatchVisualInfo(SDL_Display, SDL_Screen, depth, class, &vi)) {
357
358
359
360
int n = this->hidden->nvisuals;
this->hidden->visuals[n].depth = vi.depth;
this->hidden->visuals[n].visual = vi.visual;
this->hidden->nvisuals++;
361
362
363
364
365
366
367
368
369
}
return(this->hidden->nvisuals);
}
static int add_visual_byid(_THIS, const char *visual_id)
{
XVisualInfo *vi, template;
int nvis;
if ( visual_id ) {
370
371
SDL_memset(&template, 0, (sizeof template));
template.visualid = SDL_strtol(visual_id, NULL, 0);
372
vi = XGetVisualInfo(SDL_Display, VisualIDMask, &template, &nvis);
373
if ( vi ) {
374
375
376
377
int n = this->hidden->nvisuals;
this->hidden->visuals[n].depth = vi->depth;
this->hidden->visuals[n].visual = vi->visual;
this->hidden->nvisuals++;
378
XFree(vi);
379
380
381
382
383
384
385
386
}
}
return(this->hidden->nvisuals);
}
/* Global for the error handler */
int vm_event, vm_error = -1;
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#if SDL_VIDEO_DRIVER_X11_XINERAMA
static int CheckXinerama(_THIS, int *major, int *minor)
{
const char *env;
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
env = getenv("SDL_VIDEO_X11_XINERAMA");
if ( env && !SDL_atoi(env) ) {
return 0;
}
/* Query the extension version */
if ( !SDL_NAME(XineramaQueryExtension)(SDL_Display, major, minor) ||
!SDL_NAME(XineramaIsActive)(SDL_Display) ) {
return 0;
}
return 1;
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
410
411
412
#if SDL_VIDEO_DRIVER_X11_XRANDR
static int CheckXRandR(_THIS, int *major, int *minor)
{
413
const char *env;
414
415
416
417
418
419
420
421
422
423
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
env = getenv("SDL_VIDEO_X11_XRANDR");
if ( env && !SDL_atoi(env) ) {
return 0;
}
424
425
426
427
428
429
430
/* This used to default off, due to KDE window maximize problems */
/* Reactivated since I haven't encountered such problems with KDE, but if
one does encounter such problems he/she can just set
SDL_VIDEO_X11_XRANDR to 0
Closes Debian bug: #450689
*/
/* if ( !env ) {
431
432
return 0;
}
433
*/
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
if ( !SDL_X11_HAVE_XRANDR ) {
return 0;
}
/* Query the extension version */
if ( !XRRQueryVersion(SDL_Display, major, minor) ) {
return 0;
}
return 1;
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
#if SDL_VIDEO_DRIVER_X11_VIDMODE
static int CheckVidMode(_THIS, int *major, int *minor)
{
449
const char *env;
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
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
env = getenv("SDL_VIDEO_X11_VIDMODE");
if ( env && !SDL_atoi(env) ) {
return 0;
}
/* Metro-X 4.3.0 and earlier has a broken implementation of
XF86VidModeGetAllModeLines() - it hangs the client.
*/
if ( SDL_strcmp(ServerVendor(SDL_Display), "Metro Link Incorporated") == 0 ) {
FILE *metro_fp;
metro_fp = fopen("/usr/X11R6/lib/X11/Metro/.version", "r");
if ( metro_fp != NULL ) {
int major, minor, patch, version;
major = 0; minor = 0; patch = 0;
fscanf(metro_fp, "%d.%d.%d", &major, &minor, &patch);
fclose(metro_fp);
version = major*100+minor*10+patch;
if ( version < 431 ) {
return 0;
}
}
}
/* Query the extension version */
481
482
483
484
485
486
487
488
489
490
491
if ( !SDL_NAME(XF86VidModeQueryExtension)(SDL_Display, &vm_event, &vm_error) ||
!SDL_NAME(XF86VidModeQueryVersion)(SDL_Display, major, minor) ) {
return 0;
}
return 1;
}
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
#if SDL_VIDEO_DRIVER_X11_XME
static int CheckXME(_THIS, int *major, int *minor)
{
492
const char *env;
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* Default the extension not available */
*major = *minor = 0;
/* Allow environment override */
env = getenv("SDL_VIDEO_X11_VIDMODE");
if ( env && !SDL_atoi(env) ) {
return 0;
}
/* Query the extension version */
if ( !XiGMiscQueryVersion(SDL_Display, major, minor) ) {
return 0;
}
return 1;
}
#endif /* SDL_VIDEO_DRIVER_X11_XME */
511
512
int X11_GetVideoModes(_THIS)
{
513
514
515
#if SDL_VIDEO_DRIVER_X11_XINERAMA
int xinerama_major, xinerama_minor;
#endif
516
517
518
519
520
#if SDL_VIDEO_DRIVER_X11_XRANDR
int xrandr_major, xrandr_minor;
int nsizes;
XRRScreenSize *sizes;
#endif
521
#if SDL_VIDEO_DRIVER_X11_VIDMODE
522
523
int vm_major, vm_minor;
int nmodes;
524
SDL_NAME(XF86VidModeModeInfo) **modes;
525
#endif
526
#if SDL_VIDEO_DRIVER_X11_XME
527
528
529
int xme_major, xme_minor;
int ractive, nummodes;
XiGMiscResolutionInfo *modelist;
530
#endif
531
532
533
int i, n;
int screen_w;
int screen_h;
534
535
use_xinerama = 0;
536
use_xrandr = 0;
537
538
use_vidmode = 0;
use_xme = 0;
539
540
541
screen_w = DisplayWidth(SDL_Display, SDL_Screen);
screen_h = DisplayHeight(SDL_Display, SDL_Screen);
542
543
544
545
#if SDL_VIDEO_DRIVER_X11_XINERAMA
/* Query Xinerama extention */
if ( CheckXinerama(this, &xinerama_major, &xinerama_minor) ) {
/* Find out which screen is the desired one */
546
int desired = -1;
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
int screens;
int w, h;
SDL_NAME(XineramaScreenInfo) *xinerama;
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
if ( variable ) {
desired = SDL_atoi(variable);
}
#ifdef X11MODES_DEBUG
printf("X11 detected Xinerama:\n");
#endif
xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
for ( i = 0; i < screens; i++ ) {
#ifdef X11MODES_DEBUG
printf("xinerama %d: %dx%d+%d+%d\n",
xinerama[i].screen_number,
xinerama[i].width, xinerama[i].height,
xinerama[i].x_org, xinerama[i].y_org);
#endif
if ( xinerama[i].screen_number == desired ) {
use_xinerama = 1;
xinerama_info = xinerama[i];
}
}
XFree(xinerama);
if ( use_xinerama ) {
SDL_modelist = (SDL_Rect **)SDL_malloc(3*sizeof(SDL_Rect *));
if ( !SDL_modelist ) {
SDL_OutOfMemory();
return -1;
}
/* Add the full xinerama mode */
n = 0;
w = xinerama_info.width;
h = xinerama_info.height;
if ( screen_w > w || screen_h > h) {
SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
if ( SDL_modelist[n] ) {
SDL_modelist[n]->x = 0;
SDL_modelist[n]->y = 0;
SDL_modelist[n]->w = screen_w;
SDL_modelist[n]->h = screen_h;
++n;
}
}
/* Add the head xinerama mode */
SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
if ( SDL_modelist[n] ) {
SDL_modelist[n]->x = 0;
SDL_modelist[n]->y = 0;
SDL_modelist[n]->w = w;
SDL_modelist[n]->h = h;
++n;
}
SDL_modelist[n] = NULL;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
609
#if SDL_VIDEO_DRIVER_X11_XRANDR
610
/* XRandR */
611
/* require at least XRandR v1.0 (arbitrary) */
612
613
if ( CheckXRandR(this, &xrandr_major, &xrandr_minor) && (xrandr_major >= 1) )
{
614
#ifdef X11MODES_DEBUG
615
616
617
618
619
620
621
622
623
624
625
626
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.
*/
screen_config = XRRGetScreenInfo(SDL_Display, SDL_Root);
/* retrieve the list of resolution */
sizes = XRRConfigSizes(screen_config, &nsizes);
if (nsizes > 0) {
627
628
629
630
631
632
if ( SDL_modelist ) {
for ( i = 0; SDL_modelist[i]; ++i ) {
SDL_free(SDL_modelist[i]);
}
SDL_free(SDL_modelist);
}
633
SDL_modelist = (SDL_Rect **)malloc((nsizes+1)*sizeof(SDL_Rect *));
634
635
636
637
638
639
640
641
642
643
644
if ( !SDL_modelist ) {
SDL_OutOfMemory();
return -1;
}
for ( i=0; i < nsizes; i++ ) {
if ((SDL_modelist[i] =
(SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL)
break;
#ifdef X11MODES_DEBUG
fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n",
i, sizes[i].width, sizes[i].height);
645
646
#endif
647
648
649
650
SDL_modelist[i]->x = 0;
SDL_modelist[i]->y = 0;
SDL_modelist[i]->w = sizes[i].width;
SDL_modelist[i]->h = sizes[i].height;
651
652
}
653
/* sort the mode list descending as SDL expects */
654
SDL_qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist);
655
656
SDL_modelist[i] = NULL; /* terminator */
657
658
659
660
661
662
use_xrandr = xrandr_major * 100 + xrandr_minor;
saved_size_id = XRRConfigCurrentConfiguration(screen_config, &saved_rotation);
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
663
#if SDL_VIDEO_DRIVER_X11_VIDMODE
664
/* XVidMode */
665
if ( !use_xrandr &&
666
#if SDL_VIDEO_DRIVER_X11_XINERAMA
667
(!use_xinerama || xinerama_info.screen_number == -1) &&
669
CheckVidMode(this, &vm_major, &vm_minor) &&
670
671
SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) )
{
672
#ifdef X11MODES_DEBUG
673
printf("VidMode modes: (unsorted)\n");
674
675
676
for ( i = 0; i < nmodes; ++i ) {
printf("Mode %d: %d x %d @ %d\n", i,
modes[i]->hdisplay, modes[i]->vdisplay,
677
(modes[i]->htotal && modes[i]->vtotal) ? (1000 * modes[i]->dotclock / (modes[i]->htotal * modes[i]->vtotal)) : 0 );
678
679
}
#endif
680
if ( SDL_modelist ) {
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
for ( i = 0; SDL_modelist[i]; ++i ) {
SDL_free(SDL_modelist[i]);
}
SDL_free(SDL_modelist);
}
SDL_modelist = (SDL_Rect **)SDL_malloc((nmodes+2)*sizeof(SDL_Rect *));
if ( !SDL_modelist ) {
SDL_OutOfMemory();
return -1;
}
SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes);
n = 0;
for ( i=0; i<nmodes; ++i ) {
int w, h;
/* Eliminate duplicate modes with different refresh rates */
if ( i > 0 &&
modes[i]->hdisplay == modes[i-1]->hdisplay &&
modes[i]->vdisplay == modes[i-1]->vdisplay ) {
continue;
}
702
703
704
705
706
707
708
709
710
711
712
713
714
/* Check to see if we should add the screen size (Xinerama) */
w = modes[i]->hdisplay;
h = modes[i]->vdisplay;
if ( (screen_w * screen_h) >= (w * h) ) {
if ( (screen_w != w) || (screen_h != h) ) {
SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
if ( SDL_modelist[n] ) {
SDL_modelist[n]->x = 0;
SDL_modelist[n]->y = 0;
SDL_modelist[n]->w = screen_w;
SDL_modelist[n]->h = screen_h;
++n;
715
716
}
}
717
718
719
screen_w = 0;
screen_h = 0;
}
720
721
722
723
724
/* Add the size from the video mode list */
SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
if ( SDL_modelist[n] == NULL ) {
break;
725
}
726
727
728
729
730
SDL_modelist[n]->x = 0;
SDL_modelist[n]->y = 0;
SDL_modelist[n]->w = w;
SDL_modelist[n]->h = h;
++n;
731
}
732
SDL_modelist[n] = NULL;
733
XFree(modes);
734
735
use_vidmode = vm_major * 100 + vm_minor;
736
737
save_mode(this);
}
738
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
739
740
#if SDL_VIDEO_DRIVER_X11_XME
741
/* XiG */
742
modelist = NULL;
743
744
745
746
747
/* first lets make sure we have the extension, and it's at least v2.0 */
if ( CheckXME(this, &xme_major, &xme_minor) && xme_major >= 2 &&
(nummodes = XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
0, /* view */
&ractive, &modelist)) > 1 )
748
749
{ /* then we actually have some */
int j;
750
751
752
753
/* We get the list already sorted in descending order.
We'll copy it in reverse order so SDL is happy */
#ifdef X11MODES_DEBUG
754
755
fprintf(stderr, "XME: nummodes = %d, active mode = %d\n",
nummodes, ractive);
756
#endif
757
758
759
760
761
762
if ( SDL_modelist ) {
for ( i = 0; SDL_modelist[i]; ++i ) {
SDL_free(SDL_modelist[i]);
}
SDL_free(SDL_modelist);
}
763
SDL_modelist = (SDL_Rect **)SDL_malloc((nummodes+1)*sizeof(SDL_Rect *));
764
765
766
767
768
769
770
771
772
773
774
if ( !SDL_modelist ) {
SDL_OutOfMemory();
return -1;
}
for ( i=0, j=nummodes-1; j>=0; i++, j-- ) {
if ((SDL_modelist[i] =
(SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL)
break;
#ifdef X11MODES_DEBUG
fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n",
i, modelist[i].width, modelist[i].height);
775
#endif
776
777
778
779
780
781
SDL_modelist[i]->x = 0;
SDL_modelist[i]->y = 0;
SDL_modelist[i]->w = modelist[j].width;
SDL_modelist[i]->h = modelist[j].height;
782
}
783
784
785
SDL_modelist[i] = NULL; /* terminator */
use_xme = xme_major * 100 + xme_minor;
786
saved_res = modelist[ractive]; /* save the current resolution */
787
788
}
if ( modelist ) {
789
XFree(modelist);
790
}
791
#endif /* SDL_VIDEO_DRIVER_X11_XME */
792
793
{
794
795
/* It's interesting to note that if we allow 32 bit depths,
we get a visual with an alpha mask on composite servers.
796
static int depth_list[] = { 32, 24, 16, 15, 8 };
797
798
*/
static int depth_list[] = { 24, 16, 15, 8 };
799
800
801
802
803
804
int j, np;
int use_directcolor = 1;
XPixmapFormatValues *pf;
/* Search for the visuals in deepest-first order, so that the first
will be the richest one */
805
if ( SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
806
807
808
use_directcolor = 0;
}
this->hidden->nvisuals = 0;
809
if ( ! add_visual_byid(this, SDL_getenv("SDL_VIDEO_X11_VISUALID")) ) {
810
for ( i=0; i<SDL_arraysize(depth_list); ++i ) {
811
812
813
814
815
816
817
818
819
820
if ( depth_list[i] > 8 ) {
if ( use_directcolor ) {
add_visual(this, depth_list[i], DirectColor);
}
add_visual(this, depth_list[i], TrueColor);
} else {
add_visual(this, depth_list[i], PseudoColor);
add_visual(this, depth_list[i], StaticColor);
}
}
821
add_default_visual(this);
822
823
824
825
826
827
828
}
if ( this->hidden->nvisuals == 0 ) {
SDL_SetError("Found no sufficiently capable X11 visuals");
return -1;
}
/* look up the pixel quantum for each depth */
829
pf = XListPixmapFormats(SDL_Display, &np);
830
831
832
833
834
835
836
837
for(i = 0; i < this->hidden->nvisuals; i++) {
int d = this->hidden->visuals[i].depth;
for(j = 0; j < np; j++)
if(pf[j].depth == d)
break;
this->hidden->visuals[i].bpp = j < np ? pf[j].bits_per_pixel : d;
}
838
XFree(pf);
839
840
841
}
if ( SDL_modelist == NULL ) {
842
SDL_modelist = (SDL_Rect **)SDL_malloc((1+1)*sizeof(SDL_Rect *));
843
844
845
if ( !SDL_modelist ) {
SDL_OutOfMemory();
return -1;
846
}
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
n = 0;
SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
if ( SDL_modelist[n] ) {
SDL_modelist[n]->x = 0;
SDL_modelist[n]->y = 0;
SDL_modelist[n]->w = screen_w;
SDL_modelist[n]->h = screen_h;
++n;
}
SDL_modelist[n] = NULL;
}
#ifdef X11MODES_DEBUG
if ( use_xinerama ) {
printf("Xinerama is enabled\n");
862
863
}
864
865
866
867
if ( use_xrandr ) {
printf("XRandR is enabled\n");
}
868
if ( use_vidmode ) {
869
printf("VidMode is enabled\n");
870
}
871
872
if ( use_xme ) {
873
printf("Xi Graphics XME fullscreen is enabled\n");
875
876
if ( SDL_modelist ) {
877
printf("X11 video mode list:\n");
878
for ( i=0; SDL_modelist[i]; ++i ) {
879
printf("\t%dx%d\n", SDL_modelist[i]->w, SDL_modelist[i]->h);
880
881
}
}
882
#endif /* X11MODES_DEBUG */
883
884
885
886
887
888
889
890
return 0;
}
int X11_SupportedVisual(_THIS, SDL_PixelFormat *format)
{
int i;
for(i = 0; i < this->hidden->nvisuals; i++)
891
892
if(this->hidden->visuals[i].bpp == format->BitsPerPixel)
return 1;
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
return 0;
}
SDL_Rect **X11_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
{
if ( X11_SupportedVisual(this, format) ) {
if ( flags & SDL_FULLSCREEN ) {
return(SDL_modelist);
} else {
return((SDL_Rect **)-1);
}
} else {
return((SDL_Rect **)0);
}
}
void X11_FreeVideoModes(_THIS)
{
int i;
if ( SDL_modelist ) {
for ( i=0; SDL_modelist[i]; ++i ) {
915
SDL_free(SDL_modelist[i]);
916
}
917
SDL_free(SDL_modelist);
918
919
SDL_modelist = NULL;
}
920
921
922
923
924
925
926
927
#if SDL_VIDEO_DRIVER_X11_XRANDR
/* Free the Xrandr screen configuration */
if ( screen_config ) {
XRRFreeScreenConfigInfo(screen_config);
screen_config = NULL;
}
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
928
929
930
931
}
int X11_ResizeFullScreen(_THIS)
{
932
int x = 0, y = 0;
933
int real_w, real_h;
934
935
936
937
938
int screen_w;
int screen_h;
screen_w = DisplayWidth(SDL_Display, SDL_Screen);
screen_h = DisplayHeight(SDL_Display, SDL_Screen);
939
940
#if SDL_VIDEO_DRIVER_X11_XINERAMA
941
942
943
944
945
946
if ( use_xinerama &&
window_w <= xinerama_info.width &&
window_h <= xinerama_info.height ) {
x = xinerama_info.x_org;
y = xinerama_info.y_org;
}
947
948
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
949
950
if ( currently_fullscreen ) {
/* Switch resolution and cover it with the FSwindow */
951
move_cursor_to(this, x, y);
952
set_best_resolution(this, window_w, window_h);
953
move_cursor_to(this, x, y);
954
get_real_resolution(this, &real_w, &real_h);
955
if ( window_w > real_w ) {
956
957
real_w = MAX(real_w, screen_w);
}
958
if ( window_h > real_h ) {
959
960
real_h = MAX(real_h, screen_h);
}
961
XMoveResizeWindow(SDL_Display, FSwindow, x, y, real_w, real_h);
962
963
964
move_cursor_to(this, real_w/2, real_h/2);
/* Center and reparent the drawing window */
965
966
x = (real_w - window_w)/2;
y = (real_h - window_h)/2;
967
XReparentWindow(SDL_Display, SDL_Window, FSwindow, x, y);
968
/* FIXME: move the mouse to the old relative location */
969
XSync(SDL_Display, True); /* Flush spurious mode change events */
970
971
972
973
974
975
976
977
978
}
return(1);
}
void X11_QueueEnterFullScreen(_THIS)
{
switch_waiting = 0x01 | SDL_FULLSCREEN;
switch_time = SDL_GetTicks() + 1500;
#if 0 /* This causes a BadMatch error if the window is iconified (not needed) */
979
XSetInputFocus(SDL_Display, WMwindow, RevertToNone, CurrentTime);
980
981
982
983
984
985
986
987
988
989
#endif
}
int X11_EnterFullScreen(_THIS)
{
int okay;
#if 0
Window tmpwin, *windows;
int i, nwindows;
#endif
990
int x = 0, y = 0;
991
int real_w, real_h;
992
993
int screen_w;
int screen_h;
994
995
okay = 1;
996
997
998
if ( currently_fullscreen ) {
return(okay);
}
999
1000
/* Ungrab the input so that we can move the mouse around */