comparison src/video/x11/SDL_x11modes.c @ 1765:c2c6ff414ef5

Fixed bug #45 Improved Xinerama support. Added support for the SDL_VIDEO_FULLSCREEN_HEAD environment variable, currently supported on X11 Xinerama configurations. Only use the VidMode extension on the primary head.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 04 May 2006 16:51:07 +0000
parents e75d0cbcf3c8
children 410b1ed7fe28
comparison
equal deleted inserted replaced
1764:4b2f27334dce 1765:c2c6ff414ef5
31 #include "SDL_x11video.h" 31 #include "SDL_x11video.h"
32 #include "SDL_x11wm_c.h" 32 #include "SDL_x11wm_c.h"
33 #include "SDL_x11modes_c.h" 33 #include "SDL_x11modes_c.h"
34 #include "SDL_x11image_c.h" 34 #include "SDL_x11image_c.h"
35 35
36 #if SDL_VIDEO_DRIVER_X11_XINERAMA
37 #include "../Xext/extensions/Xinerama.h"
38 #endif
39
40 #define MAX(a, b) (a > b ? a : b) 36 #define MAX(a, b) (a > b ? a : b)
37
38 #if SDL_VIDEO_DRIVER_X11_XRANDR
39 static int cmpmodelist(const void *va, const void *vb)
40 {
41 const SDL_Rect *a = *(const SDL_Rect **)va;
42 const SDL_Rect *b = *(const SDL_Rect **)vb;
43 if ( a->w == b->w )
44 return b->h - a->h;
45 else
46 return b->w - a->w;
47 }
48 #endif
41 49
42 #if SDL_VIDEO_DRIVER_X11_VIDMODE 50 #if SDL_VIDEO_DRIVER_X11_VIDMODE
43 Bool SDL_NAME(XF86VidModeGetModeInfo)(Display *dpy, int scr, SDL_NAME(XF86VidModeModeInfo) *info) 51 Bool SDL_NAME(XF86VidModeGetModeInfo)(Display *dpy, int scr, SDL_NAME(XF86VidModeModeInfo) *info)
44 { 52 {
45 SDL_NAME(XF86VidModeModeLine) *l = (SDL_NAME(XF86VidModeModeLine)*)((char*)info + sizeof info->dotclock); 53 SDL_NAME(XF86VidModeModeLine) *l = (SDL_NAME(XF86VidModeModeLine)*)((char*)info + sizeof info->dotclock);
84 else 92 else
85 return b->hdisplay - a->hdisplay; 93 return b->hdisplay - a->hdisplay;
86 } 94 }
87 #endif 95 #endif
88 96
89 #if SDL_VIDEO_DRIVER_X11_XRANDR
90 static int cmpmodelist(const void *va, const void *vb)
91 {
92 const SDL_Rect *a = *(const SDL_Rect **)va;
93 const SDL_Rect *b = *(const SDL_Rect **)vb;
94 if ( a->w == b->w )
95 return b->h - a->h;
96 else
97 return b->w - a->w;
98 }
99 #endif
100
101 static void get_real_resolution(_THIS, int* w, int* h); 97 static void get_real_resolution(_THIS, int* w, int* h);
102 98
103 static void set_best_resolution(_THIS, int width, int height) 99 static void set_best_resolution(_THIS, int width, int height)
104 { 100 {
105 #if SDL_VIDEO_DRIVER_X11_VIDMODE 101 #if SDL_VIDEO_DRIVER_X11_VIDMODE
110 int best_width = 0, best_height = 0; 106 int best_width = 0, best_height = 0;
111 int nmodes; 107 int nmodes;
112 108
113 if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &i, &mode) && 109 if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &i, &mode) &&
114 SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes)){ 110 SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes)){
115 #ifdef XFREE86_DEBUG 111 #ifdef X11MODES_DEBUG
116 printf("Available modes (unsorted):\n"); 112 printf("Available modes (unsorted):\n");
117 for ( i = 0; i < nmodes; ++i ) { 113 for ( i = 0; i < nmodes; ++i ) {
118 printf("Mode %d: %d x %d @ %d\n", i, 114 printf("Mode %d: %d x %d @ %d\n", i,
119 modes[i]->hdisplay, modes[i]->vdisplay, 115 modes[i]->hdisplay, modes[i]->vdisplay,
120 1000 * modes[i]->dotclock / (modes[i]->htotal * 116 1000 * modes[i]->dotclock / (modes[i]->htotal *
152 } 148 }
153 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */ 149 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
154 150
155 /* XiG */ 151 /* XiG */
156 #if SDL_VIDEO_DRIVER_X11_XME 152 #if SDL_VIDEO_DRIVER_X11_XME
157 #ifdef XIG_DEBUG 153 #ifdef X11MODES_DEBUG
158 fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n", 154 fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n",
159 width, height); 155 width, height);
160 #endif 156 #endif
161 if ( SDL_modelist ) { 157 if ( SDL_modelist ) {
162 int i; 158 int i;
173 169
174 /* check current mode so we can avoid uneccessary mode changes */ 170 /* check current mode so we can avoid uneccessary mode changes */
175 get_real_resolution(this, &w, &h); 171 get_real_resolution(this, &w, &h);
176 172
177 if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) { 173 if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
178 # ifdef XIG_DEBUG 174 #ifdef X11MODES_DEBUG
179 fprintf(stderr, "XME: set_best_resolution: " 175 fprintf(stderr, "XME: set_best_resolution: "
180 "XiGMiscChangeResolution: %d %d\n", 176 "XiGMiscChangeResolution: %d %d\n",
181 SDL_modelist[s]->w, SDL_modelist[s]->h); 177 SDL_modelist[s]->w, SDL_modelist[s]->h);
182 # endif 178 # endif
183 XiGMiscChangeResolution(SDL_Display, 179 XiGMiscChangeResolution(SDL_Display,
192 } 188 }
193 #endif /* SDL_VIDEO_DRIVER_X11_XME */ 189 #endif /* SDL_VIDEO_DRIVER_X11_XME */
194 190
195 #if SDL_VIDEO_DRIVER_X11_XRANDR 191 #if SDL_VIDEO_DRIVER_X11_XRANDR
196 if ( use_xrandr ) { 192 if ( use_xrandr ) {
197 #ifdef XRANDR_DEBUG 193 #ifdef X11MODES_DEBUG
198 fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n", 194 fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
199 width, height); 195 width, height);
200 #endif 196 #endif
201 if ( SDL_modelist ) { 197 if ( SDL_modelist ) {
202 int i, nsizes; 198 int i, nsizes;
218 get_real_resolution(this, &w, &h); 214 get_real_resolution(this, &w, &h);
219 215
220 if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) { 216 if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
221 int size_id; 217 int size_id;
222 218
223 #ifdef XRANDR_DEBUG 219 #ifdef X11MODES_DEBUG
224 fprintf(stderr, "XRANDR: set_best_resolution: " 220 fprintf(stderr, "XRANDR: set_best_resolution: "
225 "XXRSetScreenConfig: %d %d\n", 221 "XXRSetScreenConfig: %d %d\n",
226 SDL_modelist[i]->w, SDL_modelist[i]->h); 222 SDL_modelist[i]->w, SDL_modelist[i]->h);
227 #endif 223 #endif
228 224
243 239
244 } 240 }
245 241
246 static void get_real_resolution(_THIS, int* w, int* h) 242 static void get_real_resolution(_THIS, int* w, int* h)
247 { 243 {
248 #if SDL_VIDEO_DRIVER_X11_VIDMODE
249 if ( use_vidmode ) {
250 SDL_NAME(XF86VidModeModeLine) mode;
251 int unused;
252
253 if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
254 *w = mode.hdisplay;
255 *h = mode.vdisplay;
256 return;
257 }
258 }
259 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
260
261 #if SDL_VIDEO_DRIVER_X11_XME 244 #if SDL_VIDEO_DRIVER_X11_XME
262 if ( use_xme ) { 245 if ( use_xme ) {
263 int ractive; 246 int ractive;
264 XiGMiscResolutionInfo *modelist; 247 XiGMiscResolutionInfo *modelist;
265 248
266 XiGMiscQueryResolutions(SDL_Display, SDL_Screen, 249 XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
267 0, /* view */ 250 0, /* view */
268 &ractive, &modelist); 251 &ractive, &modelist);
269 *w = modelist[ractive].width; 252 *w = modelist[ractive].width;
270 *h = modelist[ractive].height; 253 *h = modelist[ractive].height;
271 #ifdef XIG_DEBUG 254 #ifdef X11MODES_DEBUG
272 fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h); 255 fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h);
273 #endif 256 #endif
274 XFree(modelist); 257 XFree(modelist);
275 return; 258 return;
276 } 259 }
277 #endif /* SDL_VIDEO_DRIVER_X11_XME */ 260 #endif /* SDL_VIDEO_DRIVER_X11_XME */
261
262 #if SDL_VIDEO_DRIVER_X11_VIDMODE
263 if ( use_vidmode ) {
264 SDL_NAME(XF86VidModeModeLine) mode;
265 int unused;
266
267 if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
268 *w = mode.hdisplay;
269 *h = mode.vdisplay;
270 return;
271 }
272 }
273 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
278 274
279 #if SDL_VIDEO_DRIVER_X11_XRANDR 275 #if SDL_VIDEO_DRIVER_X11_XRANDR
280 if ( use_xrandr ) { 276 if ( use_xrandr ) {
281 int nsizes; 277 int nsizes;
282 XRRScreenSize* sizes; 278 XRRScreenSize* sizes;
289 cur_size = XRRConfigCurrentConfiguration(screen_config, &cur_rotation); 285 cur_size = XRRConfigCurrentConfiguration(screen_config, &cur_rotation);
290 if ( cur_size >= 0 && cur_size < nsizes ) { 286 if ( cur_size >= 0 && cur_size < nsizes ) {
291 *w = sizes[cur_size].width; 287 *w = sizes[cur_size].width;
292 *h = sizes[cur_size].height; 288 *h = sizes[cur_size].height;
293 } 289 }
294 #ifdef XRANDR_DEBUG 290 #ifdef X11MODES_DEBUG
295 fprintf(stderr, "XRANDR: get_real_resolution: w = %d h = %d\n", *w, *h); 291 fprintf(stderr, "XRANDR: get_real_resolution: w = %d h = %d\n", *w, *h);
296 #endif 292 #endif
297 return; 293 return;
298 } 294 }
299 } 295 }
300 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */ 296 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
297
298 #if SDL_VIDEO_DRIVER_X11_XINERAMA
299 if ( use_xinerama ) {
300 *w = xinerama_info.width;
301 *h = xinerama_info.height;
302 return;
303 }
304 #endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
301 305
302 *w = DisplayWidth(SDL_Display, SDL_Screen); 306 *w = DisplayWidth(SDL_Display, SDL_Screen);
303 *h = DisplayHeight(SDL_Display, SDL_Screen); 307 *h = DisplayHeight(SDL_Display, SDL_Screen);
304 } 308 }
305 309
358 } 362 }
359 363
360 /* Global for the error handler */ 364 /* Global for the error handler */
361 int vm_event, vm_error = -1; 365 int vm_event, vm_error = -1;
362 366
367 #if SDL_VIDEO_DRIVER_X11_XINERAMA
368 static int CheckXinerama(_THIS, int *major, int *minor)
369 {
370 const char *env;
371
372 /* Default the extension not available */
373 *major = *minor = 0;
374
375 /* Allow environment override */
376 env = getenv("SDL_VIDEO_X11_XINERAMA");
377 if ( env && !SDL_atoi(env) ) {
378 return 0;
379 }
380
381 /* Query the extension version */
382 if ( !SDL_NAME(XineramaQueryExtension)(SDL_Display, major, minor) ||
383 !SDL_NAME(XineramaIsActive)(SDL_Display) ) {
384 return 0;
385 }
386 return 1;
387 }
388 #endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
389
363 #if SDL_VIDEO_DRIVER_X11_XRANDR 390 #if SDL_VIDEO_DRIVER_X11_XRANDR
364 static int CheckXRandR(_THIS, int *major, int *minor) 391 static int CheckXRandR(_THIS, int *major, int *minor)
365 { 392 {
366 char *env; 393 const char *env;
367 394
368 /* Default the extension not available */ 395 /* Default the extension not available */
369 *major = *minor = 0; 396 *major = *minor = 0;
370 397
371 /* Allow environment override */ 398 /* Allow environment override */
392 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */ 419 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
393 420
394 #if SDL_VIDEO_DRIVER_X11_VIDMODE 421 #if SDL_VIDEO_DRIVER_X11_VIDMODE
395 static int CheckVidMode(_THIS, int *major, int *minor) 422 static int CheckVidMode(_THIS, int *major, int *minor)
396 { 423 {
397 char *env; 424 const char *env;
398 425
399 /* Default the extension not available */ 426 /* Default the extension not available */
400 *major = *minor = 0; 427 *major = *minor = 0;
401 428
402 /* Allow environment override */ 429 /* Allow environment override */
435 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */ 462 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
436 463
437 #if SDL_VIDEO_DRIVER_X11_XME 464 #if SDL_VIDEO_DRIVER_X11_XME
438 static int CheckXME(_THIS, int *major, int *minor) 465 static int CheckXME(_THIS, int *major, int *minor)
439 { 466 {
440 char *env; 467 const char *env;
441 468
442 /* Default the extension not available */ 469 /* Default the extension not available */
443 *major = *minor = 0; 470 *major = *minor = 0;
444 471
445 /* Allow environment override */ 472 /* Allow environment override */
456 } 483 }
457 #endif /* SDL_VIDEO_DRIVER_X11_XME */ 484 #endif /* SDL_VIDEO_DRIVER_X11_XME */
458 485
459 int X11_GetVideoModes(_THIS) 486 int X11_GetVideoModes(_THIS)
460 { 487 {
488 #if SDL_VIDEO_DRIVER_X11_XINERAMA
489 int xinerama_major, xinerama_minor;
490 #endif
461 #if SDL_VIDEO_DRIVER_X11_XRANDR 491 #if SDL_VIDEO_DRIVER_X11_XRANDR
462 int xrandr_major, xrandr_minor; 492 int xrandr_major, xrandr_minor;
463 int nsizes; 493 int nsizes;
464 XRRScreenSize *sizes; 494 XRRScreenSize *sizes;
465 #endif 495 #endif
475 #endif 505 #endif
476 int i, n; 506 int i, n;
477 int screen_w; 507 int screen_w;
478 int screen_h; 508 int screen_h;
479 509
510 use_xinerama = 0;
511 use_xrandr = 0;
480 use_vidmode = 0; 512 use_vidmode = 0;
481 use_xrandr = 0; 513 use_xme = 0;
482 screen_w = DisplayWidth(SDL_Display, SDL_Screen); 514 screen_w = DisplayWidth(SDL_Display, SDL_Screen);
483 screen_h = DisplayHeight(SDL_Display, SDL_Screen); 515 screen_h = DisplayHeight(SDL_Display, SDL_Screen);
516
517 #if SDL_VIDEO_DRIVER_X11_XINERAMA
518 /* Query Xinerama extention */
519 if ( CheckXinerama(this, &xinerama_major, &xinerama_minor) ) {
520 /* Find out which screen is the desired one */
521 int desired = 0;
522 int screens;
523 int w, h;
524 SDL_NAME(XineramaScreenInfo) *xinerama;
525
526 const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
527 if ( variable ) {
528 desired = SDL_atoi(variable);
529 }
530 #ifdef X11MODES_DEBUG
531 printf("X11 detected Xinerama:\n");
532 #endif
533 xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
534 for ( i = 0; i < screens; i++ ) {
535 #ifdef X11MODES_DEBUG
536 printf("xinerama %d: %dx%d+%d+%d\n",
537 xinerama[i].screen_number,
538 xinerama[i].width, xinerama[i].height,
539 xinerama[i].x_org, xinerama[i].y_org);
540 #endif
541 if ( xinerama[i].screen_number == desired ) {
542 use_xinerama = 1;
543 xinerama_info = xinerama[i];
544 }
545 }
546 XFree(xinerama);
547
548 if ( use_xinerama ) {
549 SDL_modelist = (SDL_Rect **)SDL_malloc(3*sizeof(SDL_Rect *));
550 if ( !SDL_modelist ) {
551 SDL_OutOfMemory();
552 return -1;
553 }
554
555 /* Add the full xinerama mode */
556 n = 0;
557 w = xinerama_info.width;
558 h = xinerama_info.height;
559 if ( screen_w > w || screen_h > h) {
560 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
561 if ( SDL_modelist[n] ) {
562 SDL_modelist[n]->x = 0;
563 SDL_modelist[n]->y = 0;
564 SDL_modelist[n]->w = screen_w;
565 SDL_modelist[n]->h = screen_h;
566 ++n;
567 }
568 }
569
570 /* Add the head xinerama mode */
571 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
572 if ( SDL_modelist[n] ) {
573 SDL_modelist[n]->x = 0;
574 SDL_modelist[n]->y = 0;
575 SDL_modelist[n]->w = w;
576 SDL_modelist[n]->h = h;
577 ++n;
578 }
579 SDL_modelist[n] = NULL;
580 }
581 }
582 #endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
484 583
485 #if SDL_VIDEO_DRIVER_X11_XRANDR 584 #if SDL_VIDEO_DRIVER_X11_XRANDR
486 /* XRandR */ 585 /* XRandR */
487 /* require at least XRandR v1.0 (arbitrary) */ 586 /* require at least XRandR v1.0 (arbitrary) */
488 if ( CheckXRandR(this, &xrandr_major, &xrandr_minor) && (xrandr_major >= 1) ) 587 if ( CheckXRandR(this, &xrandr_major, &xrandr_minor) && (xrandr_major >= 1) )
489 { 588 {
490 #ifdef XRANDR_DEBUG 589 #ifdef X11MODES_DEBUG
491 fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n", 590 fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
492 xrandr_major, xrandr_minor); 591 xrandr_major, xrandr_minor);
493 #endif 592 #endif
494 593
495 /* save the screen configuration since we must reference it 594 /* save the screen configuration since we must reference it
498 screen_config = XRRGetScreenInfo(SDL_Display, SDL_Root); 597 screen_config = XRRGetScreenInfo(SDL_Display, SDL_Root);
499 598
500 /* retrieve the list of resolution */ 599 /* retrieve the list of resolution */
501 sizes = XRRConfigSizes(screen_config, &nsizes); 600 sizes = XRRConfigSizes(screen_config, &nsizes);
502 if (nsizes > 0) { 601 if (nsizes > 0) {
602 if ( SDL_modelist ) {
603 for ( i = 0; SDL_modelist[i]; ++i ) {
604 SDL_free(SDL_modelist[i]);
605 }
606 SDL_free(SDL_modelist);
607 }
503 SDL_modelist = (SDL_Rect **)malloc((nsizes+1)*sizeof(SDL_Rect *)); 608 SDL_modelist = (SDL_Rect **)malloc((nsizes+1)*sizeof(SDL_Rect *));
504 if (SDL_modelist) { 609 if ( !SDL_modelist ) {
505 for ( i=0; i < nsizes; i++ ) { 610 SDL_OutOfMemory();
506 if ((SDL_modelist[i] = 611 return -1;
507 (SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL) 612 }
508 break; 613 for ( i=0; i < nsizes; i++ ) {
509 #ifdef XRANDR_DEBUG 614 if ((SDL_modelist[i] =
510 fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n", 615 (SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL)
511 i, sizes[i].width, sizes[i].height); 616 break;
512 #endif 617 #ifdef X11MODES_DEBUG
513 618 fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n",
514 SDL_modelist[i]->x = 0; 619 i, sizes[i].width, sizes[i].height);
515 SDL_modelist[i]->y = 0; 620 #endif
516 SDL_modelist[i]->w = sizes[i].width; 621
517 SDL_modelist[i]->h = sizes[i].height; 622 SDL_modelist[i]->x = 0;
518 623 SDL_modelist[i]->y = 0;
519 } 624 SDL_modelist[i]->w = sizes[i].width;
520 /* sort the mode list descending as SDL expects */ 625 SDL_modelist[i]->h = sizes[i].height;
521 qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist); 626
522 SDL_modelist[i] = NULL; /* terminator */ 627 }
523 } 628 /* sort the mode list descending as SDL expects */
629 qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist);
630 SDL_modelist[i] = NULL; /* terminator */
631
524 use_xrandr = xrandr_major * 100 + xrandr_minor; 632 use_xrandr = xrandr_major * 100 + xrandr_minor;
525 saved_size_id = XRRConfigCurrentConfiguration(screen_config, &saved_rotation); 633 saved_size_id = XRRConfigCurrentConfiguration(screen_config, &saved_rotation);
526 } 634 }
527 } 635 }
528 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */ 636 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
529 637
530 #if SDL_VIDEO_DRIVER_X11_VIDMODE 638 #if SDL_VIDEO_DRIVER_X11_VIDMODE
531 /* XVidMode */ 639 /* XVidMode */
532 if ( !use_xrandr && CheckVidMode(this, &vm_major, &vm_minor) && 640 if ( !use_xrandr &&
641 (!use_xinerama || xinerama_info.screen_number == 0) &&
642 CheckVidMode(this, &vm_major, &vm_minor) &&
533 SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) ) 643 SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) )
534 { 644 {
535 #ifdef XFREE86_DEBUG 645 #ifdef X11MODES_DEBUG
536 printf("Available modes: (sorted)\n"); 646 printf("Available modes: (sorted)\n");
537 for ( i = 0; i < nmodes; ++i ) { 647 for ( i = 0; i < nmodes; ++i ) {
538 printf("Mode %d: %d x %d @ %d\n", i, 648 printf("Mode %d: %d x %d @ %d\n", i,
539 modes[i]->hdisplay, modes[i]->vdisplay, 649 modes[i]->hdisplay, modes[i]->vdisplay,
540 1000 * modes[i]->dotclock / (modes[i]->htotal * 650 (modes[i]->htotal && modes[i]->vtotal) ? (1000 * modes[i]->dotclock / (modes[i]->htotal * modes[i]->vtotal)) : 0 );
541 modes[i]->vtotal) ); 651 }
542 } 652 #endif
543 #endif 653 if ( SDL_modelist ) {
544 654 for ( i = 0; SDL_modelist[i]; ++i ) {
655 SDL_free(SDL_modelist[i]);
656 }
657 SDL_free(SDL_modelist);
658 }
659 SDL_modelist = (SDL_Rect **)SDL_malloc((nmodes+2)*sizeof(SDL_Rect *));
660 if ( !SDL_modelist ) {
661 SDL_OutOfMemory();
662 return -1;
663 }
545 SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes); 664 SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes);
546 SDL_modelist = (SDL_Rect **)SDL_malloc((nmodes+2)*sizeof(SDL_Rect *)); 665 n = 0;
547 if ( SDL_modelist ) { 666 for ( i=0; i<nmodes; ++i ) {
548 n = 0; 667 int w, h;
549 for ( i=0; i<nmodes; ++i ) { 668
550 int w, h; 669 /* Eliminate duplicate modes with different refresh rates */
551 670 if ( i > 0 &&
552 /* Eliminate duplicate modes with different refresh rates */ 671 modes[i]->hdisplay == modes[i-1]->hdisplay &&
553 if ( i > 0 && 672 modes[i]->vdisplay == modes[i-1]->vdisplay ) {
554 modes[i]->hdisplay == modes[i-1]->hdisplay && 673 continue;
555 modes[i]->vdisplay == modes[i-1]->vdisplay ) { 674 }
556 continue; 675
557 } 676 /* Check to see if we should add the screen size (Xinerama) */
558 677 w = modes[i]->hdisplay;
559 /* Check to see if we should add the screen size (Xinerama) */ 678 h = modes[i]->vdisplay;
560 w = modes[i]->hdisplay; 679 if ( (screen_w * screen_h) >= (w * h) ) {
561 h = modes[i]->vdisplay; 680 if ( (screen_w != w) || (screen_h != h) ) {
562 if ( (screen_w * screen_h) >= (w * h) ) { 681 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
563 if ( (screen_w != w) || (screen_h != h) ) { 682 if ( SDL_modelist[n] ) {
564 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect)); 683 SDL_modelist[n]->x = 0;
565 if ( SDL_modelist[n] ) { 684 SDL_modelist[n]->y = 0;
566 SDL_modelist[n]->x = 0; 685 SDL_modelist[n]->w = screen_w;
567 SDL_modelist[n]->y = 0; 686 SDL_modelist[n]->h = screen_h;
568 SDL_modelist[n]->w = screen_w; 687 ++n;
569 SDL_modelist[n]->h = screen_h;
570 ++n;
571 }
572 } 688 }
573 screen_w = 0;
574 screen_h = 0;
575 } 689 }
576 690 screen_w = 0;
577 /* Add the size from the video mode list */ 691 screen_h = 0;
578 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect)); 692 }
579 if ( SDL_modelist[n] == NULL ) { 693
580 break; 694 /* Add the size from the video mode list */
581 } 695 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
582 SDL_modelist[n]->x = 0; 696 if ( SDL_modelist[n] == NULL ) {
583 SDL_modelist[n]->y = 0; 697 break;
584 SDL_modelist[n]->w = w; 698 }
585 SDL_modelist[n]->h = h; 699 SDL_modelist[n]->x = 0;
586 ++n; 700 SDL_modelist[n]->y = 0;
587 } 701 SDL_modelist[n]->w = w;
588 SDL_modelist[n] = NULL; 702 SDL_modelist[n]->h = h;
589 } 703 ++n;
704 }
705 SDL_modelist[n] = NULL;
590 XFree(modes); 706 XFree(modes);
591 707
592 use_vidmode = vm_major * 100 + vm_minor; 708 use_vidmode = vm_major * 100 + vm_minor;
593 save_mode(this); 709 save_mode(this);
594 } 710 }
603 0, /* view */ 719 0, /* view */
604 &ractive, &modelist)) > 1 ) 720 &ractive, &modelist)) > 1 )
605 { /* then we actually have some */ 721 { /* then we actually have some */
606 int j; 722 int j;
607 723
608 #ifdef XIG_DEBUG 724 /* We get the list already sorted in descending order.
725 We'll copy it in reverse order so SDL is happy */
726 #ifdef X11MODES_DEBUG
609 fprintf(stderr, "XME: nummodes = %d, active mode = %d\n", 727 fprintf(stderr, "XME: nummodes = %d, active mode = %d\n",
610 nummodes, ractive); 728 nummodes, ractive);
611 #endif 729 #endif
612 730 if ( SDL_modelist ) {
731 for ( i = 0; SDL_modelist[i]; ++i ) {
732 SDL_free(SDL_modelist[i]);
733 }
734 SDL_free(SDL_modelist);
735 }
613 SDL_modelist = (SDL_Rect **)SDL_malloc((nummodes+1)*sizeof(SDL_Rect *)); 736 SDL_modelist = (SDL_Rect **)SDL_malloc((nummodes+1)*sizeof(SDL_Rect *));
614 737 if ( !SDL_modelist ) {
615 /* we get the list already sorted in */ 738 SDL_OutOfMemory();
616 /* descending order. We'll copy it in */ 739 return -1;
617 /* reverse order so SDL is happy */ 740 }
618 if (SDL_modelist) { 741 for ( i=0, j=nummodes-1; j>=0; i++, j-- ) {
619 for ( i=0, j=nummodes-1; j>=0; i++, j-- ) { 742 if ((SDL_modelist[i] =
620 if ((SDL_modelist[i] = 743 (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL)
621 (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL) 744 break;
622 break; 745 #ifdef X11MODES_DEBUG
623 #ifdef XIG_DEBUG 746 fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n",
624 fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n", 747 i, modelist[i].width, modelist[i].height);
625 i, modelist[i].width, modelist[i].height); 748 #endif
626 #endif 749
627 750 SDL_modelist[i]->x = 0;
628 SDL_modelist[i]->x = 0; 751 SDL_modelist[i]->y = 0;
629 SDL_modelist[i]->y = 0; 752 SDL_modelist[i]->w = modelist[j].width;
630 SDL_modelist[i]->w = modelist[j].width; 753 SDL_modelist[i]->h = modelist[j].height;
631 SDL_modelist[i]->h = modelist[j].height; 754
632 755 }
633 } 756 SDL_modelist[i] = NULL; /* terminator */
634 SDL_modelist[i] = NULL; /* terminator */ 757
635 } 758 use_xme = xme_major * 100 + xme_minor;
636 use_xme = 1;
637 saved_res = modelist[ractive]; /* save the current resolution */ 759 saved_res = modelist[ractive]; /* save the current resolution */
638 } else {
639 use_xme = 0;
640 } 760 }
641 if ( modelist ) { 761 if ( modelist ) {
642 XFree(modelist); 762 XFree(modelist);
643 } 763 }
644 #endif /* SDL_VIDEO_DRIVER_X11_XME */ 764 #endif /* SDL_VIDEO_DRIVER_X11_XME */
690 XFree(pf); 810 XFree(pf);
691 } 811 }
692 812
693 if ( SDL_modelist == NULL ) { 813 if ( SDL_modelist == NULL ) {
694 SDL_modelist = (SDL_Rect **)SDL_malloc((1+1)*sizeof(SDL_Rect *)); 814 SDL_modelist = (SDL_Rect **)SDL_malloc((1+1)*sizeof(SDL_Rect *));
695 if ( SDL_modelist ) { 815 if ( !SDL_modelist ) {
696 n = 0; 816 SDL_OutOfMemory();
697 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect)); 817 return -1;
698 if ( SDL_modelist[n] ) { 818 }
699 SDL_modelist[n]->x = 0; 819 n = 0;
700 SDL_modelist[n]->y = 0; 820 SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
701 SDL_modelist[n]->w = screen_w; 821 if ( SDL_modelist[n] ) {
702 SDL_modelist[n]->h = screen_h; 822 SDL_modelist[n]->x = 0;
703 ++n; 823 SDL_modelist[n]->y = 0;
704 } 824 SDL_modelist[n]->w = screen_w;
705 SDL_modelist[n] = NULL; 825 SDL_modelist[n]->h = screen_h;
706 } 826 ++n;
707 } 827 }
708 828 SDL_modelist[n] = NULL;
709 #if defined(XFREE86_DEBUG) || defined(XIG_DEBUG) 829 }
830
831 #ifdef X11MODES_DEBUG
832 if ( use_xinerama ) {
833 printf("Xinerama is enabled\n");
834 }
835
710 if ( use_xrandr ) { 836 if ( use_xrandr ) {
711 printf("XRandR is enabled\n"); 837 printf("XRandR is enabled\n");
712 } 838 }
713 839
714 if ( use_vidmode ) { 840 if ( use_vidmode ) {
715 printf("XFree86 VidMode is enabled\n"); 841 printf("XFree86 VidMode is enabled\n");
716 } 842 }
717 843
718 #if SDL_VIDEO_DRIVER_X11_XME 844 if ( use_xme ) {
719 if ( use_xme )
720 printf("Xi Graphics XME fullscreen is enabled\n"); 845 printf("Xi Graphics XME fullscreen is enabled\n");
721 else 846 }
722 printf("Xi Graphics XME fullscreen is not available\n");
723 #endif
724 847
725 if ( SDL_modelist ) { 848 if ( SDL_modelist ) {
726 printf("X11 video mode list:\n"); 849 printf("X11 video mode list:\n");
727 for ( i=0; SDL_modelist[i]; ++i ) { 850 for ( i=0; SDL_modelist[i]; ++i ) {
728 printf("\t%dx%d\n", SDL_modelist[i]->w, SDL_modelist[i]->h); 851 printf("\t%dx%d\n", SDL_modelist[i]->w, SDL_modelist[i]->h);
729 } 852 }
730 } 853 }
731 #endif /* XFREE86_DEBUG || XIG_DEBUG */ 854 #endif /* X11MODES_DEBUG */
732
733 /* The default X/Y fullscreen offset is 0/0 */
734 xinerama_x = 0;
735 xinerama_y = 0;
736
737 #if SDL_VIDEO_DRIVER_X11_XINERAMA
738 /* Query Xinerama extention */
739 if ( SDL_NAME(XineramaQueryExtension)(SDL_Display, &i, &i) &&
740 SDL_NAME(XineramaIsActive)(SDL_Display) ) {
741 /* Find out which screen is the desired one */
742 int desired = 0;
743 int screens;
744 SDL_NAME(XineramaScreenInfo) *xinerama;
745
746 #ifdef XINERAMA_DEBUG
747 printf("X11 detected Xinerama:\n");
748 #endif
749 #if 0 /* Apparently the vidmode extension doesn't work with Xinerama */
750 const char *variable = SDL_getenv("SDL_VIDEO_X11_XINERAMA_SCREEN");
751 if ( variable ) {
752 desired = atoi(variable);
753 }
754 #endif
755 xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
756 for ( i = 0; i < screens; i++ ) {
757 #ifdef XINERAMA_DEBUG
758 printf("xinerama %d: %dx%d+%d+%d\n",
759 xinerama[i].screen_number,
760 xinerama[i].width, xinerama[i].height,
761 xinerama[i].x_org, xinerama[i].y_org);
762 #endif
763 if ( xinerama[i].screen_number == desired ) {
764 xinerama_x = xinerama[i].x_org;
765 xinerama_y = xinerama[i].y_org;
766 }
767 }
768 XFree(xinerama);
769 }
770 #endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
771 855
772 return 0; 856 return 0;
773 } 857 }
774 858
775 int X11_SupportedVisual(_THIS, SDL_PixelFormat *format) 859 int X11_SupportedVisual(_THIS, SDL_PixelFormat *format)
815 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */ 899 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
816 } 900 }
817 901
818 int X11_ResizeFullScreen(_THIS) 902 int X11_ResizeFullScreen(_THIS)
819 { 903 {
820 int x, y; 904 int x = 0, y = 0;
821 int real_w, real_h; 905 int real_w, real_h;
822 int screen_w; 906 int screen_w;
823 int screen_h; 907 int screen_h;
824 908
825 screen_w = DisplayWidth(SDL_Display, SDL_Screen); 909 screen_w = DisplayWidth(SDL_Display, SDL_Screen);
826 screen_h = DisplayHeight(SDL_Display, SDL_Screen); 910 screen_h = DisplayHeight(SDL_Display, SDL_Screen);
827 911
828 x = xinerama_x; 912 #if SDL_VIDEO_DRIVER_X11_VIDMODE
829 y = xinerama_y; 913 if ( use_xinerama &&
914 window_w <= xinerama_info.width &&
915 window_h <= xinerama_info.height ) {
916 x = xinerama_info.x_org;
917 y = xinerama_info.y_org;
918 }
919 #endif
830 if ( currently_fullscreen ) { 920 if ( currently_fullscreen ) {
831 /* Switch resolution and cover it with the FSwindow */ 921 /* Switch resolution and cover it with the FSwindow */
832 move_cursor_to(this, x, y); 922 move_cursor_to(this, x, y);
833 set_best_resolution(this, window_w, window_h); 923 set_best_resolution(this, window_w, window_h);
834 move_cursor_to(this, x, y); 924 move_cursor_to(this, x, y);
866 int okay; 956 int okay;
867 #if 0 957 #if 0
868 Window tmpwin, *windows; 958 Window tmpwin, *windows;
869 int i, nwindows; 959 int i, nwindows;
870 #endif 960 #endif
961 int x = 0, y = 0;
871 int real_w, real_h; 962 int real_w, real_h;
872 int screen_w; 963 int screen_w;
873 int screen_h; 964 int screen_h;
874 965
875 okay = 1; 966 okay = 1;
878 } 969 }
879 970
880 /* Ungrab the input so that we can move the mouse around */ 971 /* Ungrab the input so that we can move the mouse around */
881 X11_GrabInputNoLock(this, SDL_GRAB_OFF); 972 X11_GrabInputNoLock(this, SDL_GRAB_OFF);
882 973
974 #if SDL_VIDEO_DRIVER_X11_VIDMODE
975 if ( use_xinerama &&
976 window_w <= xinerama_info.width &&
977 window_h <= xinerama_info.height ) {
978 x = xinerama_info.x_org;
979 y = xinerama_info.y_org;
980 }
981 #endif
883 /* Map the fullscreen window to blank the screen */ 982 /* Map the fullscreen window to blank the screen */
884 screen_w = DisplayWidth(SDL_Display, SDL_Screen); 983 screen_w = DisplayWidth(SDL_Display, SDL_Screen);
885 screen_h = DisplayHeight(SDL_Display, SDL_Screen); 984 screen_h = DisplayHeight(SDL_Display, SDL_Screen);
886 get_real_resolution(this, &real_w, &real_h); 985 get_real_resolution(this, &real_w, &real_h);
887 if ( window_w > real_w ) { 986 if ( window_w > real_w ) {
889 } 988 }
890 if ( window_h > real_h ) { 989 if ( window_h > real_h ) {
891 real_h = MAX(real_h, screen_h); 990 real_h = MAX(real_h, screen_h);
892 } 991 }
893 XMoveResizeWindow(SDL_Display, FSwindow, 992 XMoveResizeWindow(SDL_Display, FSwindow,
894 xinerama_x, xinerama_y, real_w, real_h); 993 x, y, real_w, real_h);
895 XMapRaised(SDL_Display, FSwindow); 994 XMapRaised(SDL_Display, FSwindow);
896 X11_WaitMapped(this, FSwindow); 995 X11_WaitMapped(this, FSwindow);
897 996
898 #if 0 /* This seems to break WindowMaker in focus-follows-mouse mode */ 997 #if 0 /* This seems to break WindowMaker in focus-follows-mouse mode */
899 /* Make sure we got to the top of the window stack */ 998 /* Make sure we got to the top of the window stack */
934 } 1033 }
935 /* Set the colormap */ 1034 /* Set the colormap */
936 if ( SDL_XColorMap ) { 1035 if ( SDL_XColorMap ) {
937 XInstallColormap(SDL_Display, SDL_XColorMap); 1036 XInstallColormap(SDL_Display, SDL_XColorMap);
938 } 1037 }
939 if ( okay ) 1038 if ( okay ) {
940 X11_GrabInputNoLock(this, this->input_grab | SDL_GRAB_FULLSCREEN); 1039 X11_GrabInputNoLock(this, this->input_grab | SDL_GRAB_FULLSCREEN);
1040 }
941 1041
942 /* We may need to refresh the screen at this point (no backing store) 1042 /* We may need to refresh the screen at this point (no backing store)
943 We also don't get an event, which is why we explicitly refresh. */ 1043 We also don't get an event, which is why we explicitly refresh. */
944 if ( this->screen ) { 1044 if ( this->screen ) {
945 if ( this->screen->flags & SDL_OPENGL ) { 1045 if ( this->screen->flags & SDL_OPENGL ) {