]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_glx.c
added GAME_FNIGGIUM and GAME_SETHERAL
[divverent/darkplaces.git] / vid_glx.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 //#include <termios.h>
21 //#include <sys/ioctl.h>
22 //#include <sys/stat.h>
23 //#include <sys/vt.h>
24 //#include <stdarg.h>
25 //#include <stdio.h>
26 #include <signal.h>
27
28 #include <dlfcn.h>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <GL/glx.h>
33
34 #include <X11/keysym.h>
35 #include <X11/cursorfont.h>
36
37 #include <X11/extensions/XShm.h>
38 #include <X11/extensions/xf86dga.h>
39 #include <X11/extensions/xf86vmode.h>
40
41 #include "quakedef.h"
42
43 int cl_available = true;
44
45 //GLX prototypes
46 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
47 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
48 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
49 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
50 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
51 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
52
53 //GLX_ARB_get_proc_address
54 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
55
56 static dllfunction_t getprocaddressfuncs[] =
57 {
58         {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
59         {NULL, NULL}
60 };
61
62 //GLX_SGI_video_sync
63 GLint (GLAPIENTRY *qglXGetVideoSyncSGI)(GLuint *count);
64 GLint (GLAPIENTRY *qglXWaitVideoSyncSGI)(GLint divisor, GLint remainder, GLuint *count);
65
66 static dllfunction_t videosyncfuncs[] =
67 {
68         {"glXGetVideoSyncSGI", (void **) &qglXGetVideoSyncSGI},
69         {"glXWaitVideoSyncSGI", (void **) &qglXWaitVideoSyncSGI},
70         {NULL, NULL}
71 };
72
73 static Display *vidx11_display = NULL;
74 static int vidx11_screen;
75 static Window win;
76 static GLXContext ctx = NULL;
77
78 Atom wm_delete_window_atom;
79
80 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
81 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
82                     PointerMotionMask | ButtonMotionMask)
83 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
84                 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
85                 LeaveWindowMask)
86
87
88 static qboolean         mouse_avail = true;
89 static qboolean         mouse_active = false, usingmouse = false;
90 static float    mouse_x, mouse_y;
91 static int p_mouse_x, p_mouse_y;
92
93 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
94 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
95
96 qboolean vidmode_ext = false;
97
98 static int win_x, win_y;
99
100 static int scr_width, scr_height;
101
102 static XF86VidModeModeInfo **vidmodes;
103 static int num_vidmodes;
104 static qboolean vidmode_active = false;
105
106 static Visual *vidx11_visual;
107 static Colormap vidx11_colormap;
108
109 /*-----------------------------------------------------------------------*/
110
111 static int XLateKey(XKeyEvent *ev, char *ascii)
112 {
113         int key = 0;
114         char buf[64];
115         KeySym keysym, shifted;
116
117         keysym = XLookupKeysym (ev, 0);
118         XLookupString(ev, buf, sizeof buf, &shifted, 0);
119         *ascii = buf[0];
120
121         switch(keysym)
122         {
123                 case XK_KP_Page_Up:      key = K_KP_PGUP; break;
124                 case XK_Page_Up:         key = K_PGUP; break;
125
126                 case XK_KP_Page_Down: key = K_KP_PGDN; break;
127                 case XK_Page_Down:       key = K_PGDN; break;
128
129                 case XK_KP_Home: key = K_KP_HOME; break;
130                 case XK_Home:    key = K_HOME; break;
131
132                 case XK_KP_End:  key = K_KP_END; break;
133                 case XK_End:     key = K_END; break;
134
135                 case XK_KP_Left: key = K_KP_LEFTARROW; break;
136                 case XK_Left:    key = K_LEFTARROW; break;
137
138                 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
139                 case XK_Right:  key = K_RIGHTARROW;             break;
140
141                 case XK_KP_Down: key = K_KP_DOWNARROW; break;
142                 case XK_Down:    key = K_DOWNARROW; break;
143
144                 case XK_KP_Up:   key = K_KP_UPARROW; break;
145                 case XK_Up:              key = K_UPARROW;        break;
146
147                 case XK_Escape: key = K_ESCAPE;         break;
148
149                 case XK_KP_Enter: key = K_KP_ENTER;     break;
150                 case XK_Return: key = K_ENTER;           break;
151
152                 case XK_Tab:            key = K_TAB;                     break;
153
154                 case XK_F1:              key = K_F1;                            break;
155
156                 case XK_F2:              key = K_F2;                            break;
157
158                 case XK_F3:              key = K_F3;                            break;
159
160                 case XK_F4:              key = K_F4;                            break;
161
162                 case XK_F5:              key = K_F5;                            break;
163
164                 case XK_F6:              key = K_F6;                            break;
165
166                 case XK_F7:              key = K_F7;                            break;
167
168                 case XK_F8:              key = K_F8;                            break;
169
170                 case XK_F9:              key = K_F9;                            break;
171
172                 case XK_F10:            key = K_F10;                     break;
173
174                 case XK_F11:            key = K_F11;                     break;
175
176                 case XK_F12:            key = K_F12;                     break;
177
178                 case XK_BackSpace: key = K_BACKSPACE; break;
179
180                 case XK_KP_Delete: key = K_KP_DEL; break;
181                 case XK_Delete: key = K_DEL; break;
182
183                 case XK_Pause:  key = K_PAUSE;           break;
184
185                 case XK_Shift_L:
186                 case XK_Shift_R:        key = K_SHIFT;          break;
187
188                 case XK_Execute:
189                 case XK_Control_L:
190                 case XK_Control_R:      key = K_CTRL;            break;
191
192                 case XK_Alt_L:
193                 case XK_Meta_L:
194                 case XK_Alt_R:
195                 case XK_Meta_R: key = K_ALT;                    break;
196
197                 case XK_KP_Begin: key = K_KP_5; break;
198
199                 case XK_Insert:key = K_INS; break;
200                 case XK_KP_Insert: key = K_KP_INS; break;
201
202                 case XK_KP_Multiply: key = '*'; break;
203                 case XK_KP_Add:  key = K_KP_PLUS; break;
204                 case XK_KP_Subtract: key = K_KP_MINUS; break;
205                 case XK_KP_Divide: key = K_KP_SLASH; break;
206
207                 default:
208                         if (keysym < 32 && keysym > 126)
209                                 break;
210
211                         if (keysym >= 'A' && keysym <= 'Z')
212                                 key = keysym - 'A' + 'a';
213                         else
214                                 key = keysym;
215
216                         break;
217         }
218
219         return key;
220 }
221
222 static Cursor CreateNullCursor(Display *display, Window root)
223 {
224         Pixmap cursormask;
225         XGCValues xgc;
226         GC gc;
227         XColor dummycolour;
228         Cursor cursor;
229
230         cursormask = XCreatePixmap(display, root, 1, 1, 1);
231         xgc.function = GXclear;
232         gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
233         XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
234         dummycolour.pixel = 0;
235         dummycolour.red = 0;
236         dummycolour.flags = 04;
237         cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
238         XFreePixmap(display,cursormask);
239         XFreeGC(display,gc);
240         return cursor;
241 }
242
243 static void install_grabs(void)
244 {
245         XWindowAttributes attribs_1;
246         XSetWindowAttributes attribs_2;
247
248         XGetWindowAttributes(vidx11_display, win, &attribs_1);
249         attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
250         XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
251
252 // inviso cursor
253         XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
254
255         XGrabPointer(vidx11_display, win,  True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
256
257         if (vid_dga.integer)
258         {
259                 int MajorVersion, MinorVersion;
260
261                 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
262                 {
263                         // unable to query, probalby not supported
264                         Con_Printf( "Failed to detect XF86DGA Mouse\n" );
265                         vid_dga.integer = 0;
266                 }
267                 else
268                 {
269                         vid_dga.integer = 1;
270                         XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
271                         XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
272                 }
273         }
274         else
275                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
276
277         XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
278
279         mouse_active = true;
280         mouse_x = mouse_y = 0;
281 }
282
283 static void uninstall_grabs(void)
284 {
285         if (!vidx11_display || !win)
286                 return;
287
288         if (vid_dga.integer == 1)
289                 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
290
291         XUngrabPointer(vidx11_display, CurrentTime);
292         XUngrabKeyboard(vidx11_display, CurrentTime);
293
294 // inviso cursor
295         XUndefineCursor(vidx11_display, win);
296
297         mouse_active = false;
298 }
299
300 static void HandleEvents(void)
301 {
302         XEvent event;
303         int key;
304         char ascii;
305         qboolean dowarp = false;
306
307         if (!vidx11_display)
308                 return;
309
310         while (XPending(vidx11_display))
311         {
312                 XNextEvent(vidx11_display, &event);
313
314                 switch (event.type)
315                 {
316                 case KeyPress:
317                         // key pressed
318                         key = XLateKey (&event.xkey, &ascii);
319                         Key_Event(key, ascii, true);
320                         break;
321
322                 case KeyRelease:
323                         // key released
324                         key = XLateKey (&event.xkey, &ascii);
325                         Key_Event(key, ascii, false);
326                         break;
327
328                 case MotionNotify:
329                         // mouse moved
330                         if (usingmouse)
331                         {
332                                 if (vid_dga.integer == 1)
333                                 {
334                                         mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
335                                         mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
336                                 }
337                                 else
338                                 {
339
340                                         if (!event.xmotion.send_event)
341                                         {
342                                                 mouse_x += event.xmotion.x - p_mouse_x;
343                                                 mouse_y += event.xmotion.y - p_mouse_y;
344                                                 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
345                                                         dowarp = true;
346                                         }
347                                         p_mouse_x = event.xmotion.x;
348                                         p_mouse_y = event.xmotion.y;
349                                 }
350                         }
351                         else
352                                 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
353                         break;
354
355                 case ButtonPress:
356                         // mouse button pressed
357                         switch(event.xbutton.button)
358                         {
359                         case 1:
360                                 Key_Event(K_MOUSE1, 0, true);
361                                 break;
362                         case 2:
363                                 Key_Event(K_MOUSE3, 0, true);
364                                 break;
365                         case 3:
366                                 Key_Event(K_MOUSE2, 0, true);
367                                 break;
368                         case 4:
369                                 Key_Event(K_MWHEELUP, 0, true);
370                                 break;
371                         case 5:
372                                 Key_Event(K_MWHEELDOWN, 0, true);
373                                 break;
374                         case 6:
375                                 Key_Event(K_MOUSE4, 0, true);
376                                 break;
377                         case 7:
378                                 Key_Event(K_MOUSE5, 0, true);
379                                 break;
380                         case 8:
381                                 Key_Event(K_MOUSE6, 0, true);
382                                 break;
383                         case 9:
384                                 Key_Event(K_MOUSE7, 0, true);
385                                 break;
386                         case 10:
387                                 Key_Event(K_MOUSE8, 0, true);
388                                 break;
389                         case 11:
390                                 Key_Event(K_MOUSE9, 0, true);
391                                 break;
392                         case 12:
393                                 Key_Event(K_MOUSE10, 0, true);
394                                 break;
395                         default:
396                                 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
397                                 break;
398                         }
399                         break;
400
401                 case ButtonRelease:
402                         // mouse button released
403                         switch(event.xbutton.button)
404                         {
405                         case 1:
406                                 Key_Event(K_MOUSE1, 0, false);
407                                 break;
408                         case 2:
409                                 Key_Event(K_MOUSE3, 0, false);
410                                 break;
411                         case 3:
412                                 Key_Event(K_MOUSE2, 0, false);
413                                 break;
414                         case 4:
415                                 Key_Event(K_MWHEELUP, 0, false);
416                                 break;
417                         case 5:
418                                 Key_Event(K_MWHEELDOWN, 0, false);
419                                 break;
420                         case 6:
421                                 Key_Event(K_MOUSE4, 0, false);
422                                 break;
423                         case 7:
424                                 Key_Event(K_MOUSE5, 0, false);
425                                 break;
426                         case 8:
427                                 Key_Event(K_MOUSE6, 0, false);
428                                 break;
429                         case 9:
430                                 Key_Event(K_MOUSE7, 0, false);
431                                 break;
432                         case 10:
433                                 Key_Event(K_MOUSE8, 0, false);
434                                 break;
435                         case 11:
436                                 Key_Event(K_MOUSE9, 0, false);
437                                 break;
438                         case 12:
439                                 Key_Event(K_MOUSE10, 0, false);
440                                 break;
441                         default:
442                                 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
443                                 break;
444                         }
445                         break;
446
447                 case CreateNotify:
448                         // window created
449                         win_x = event.xcreatewindow.x;
450                         win_y = event.xcreatewindow.y;
451                         break;
452
453                 case ConfigureNotify:
454                         // window changed size/location
455                         win_x = event.xconfigure.x;
456                         win_y = event.xconfigure.y;
457                         break;
458                 case DestroyNotify:
459                         // window has been destroyed
460                         Sys_Quit();
461                         break;
462                 case ClientMessage:
463                         // window manager messages
464                         if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
465                                 Sys_Quit();
466                         break;
467                 case MapNotify:
468                         // window restored
469                         vid_hidden = false;
470                         vid_allowhwgamma = true;
471                         break;
472                 case UnmapNotify:
473                         // window iconified/rolledup/whatever
474                         vid_hidden = true;
475                         vid_allowhwgamma = false;
476                         VID_RestoreSystemGamma();
477                         break;
478                 case FocusIn:
479                         // window is now the input focus
480                         vid_allowhwgamma = true;
481                         break;
482                 case FocusOut:
483                         // window is no longer the input focus
484                         vid_allowhwgamma = false;
485                         VID_RestoreSystemGamma();
486                         break;
487                 case EnterNotify:
488                         // mouse entered window
489                         vid_allowhwgamma = true;
490                         break;
491                 case LeaveNotify:
492                         // mouse left window
493                         vid_allowhwgamma = false;
494                         VID_RestoreSystemGamma();
495                         break;
496                 }
497         }
498
499         if (dowarp)
500         {
501                 /* move the mouse to the window center again */
502                 p_mouse_x = scr_width / 2;
503                 p_mouse_y = scr_height / 2;
504                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
505         }
506
507 }
508
509 static void IN_DeactivateMouse( void )
510 {
511         if (!mouse_avail || !vidx11_display || !win)
512                 return;
513
514         if (mouse_active)
515         {
516                 uninstall_grabs();
517                 mouse_active = false;
518         }
519 }
520
521 static void IN_ActivateMouse( void )
522 {
523         if (!mouse_avail || !vidx11_display || !win)
524                 return;
525
526         if (!mouse_active)
527         {
528                 mouse_x = mouse_y = 0; // don't spazz
529                 install_grabs();
530                 mouse_active = true;
531         }
532 }
533
534 void VID_Shutdown(void)
535 {
536         if (!ctx || !vidx11_display)
537                 return;
538
539         vid_hidden = true;
540         usingmouse = false;
541         if (vidx11_display)
542         {
543                 VID_RestoreSystemGamma();
544                 uninstall_grabs();
545
546                 // FIXME: glXDestroyContext here?
547                 if (vidmode_active)
548                         XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
549                 if (win)
550                         XDestroyWindow(vidx11_display, win);
551                 XCloseDisplay(vidx11_display);
552         }
553         vidmode_active = false;
554         vidx11_display = NULL;
555         win = 0;
556         ctx = NULL;
557
558         GL_CloseLibrary();
559         Key_ClearStates ();
560 }
561
562 void signal_handler(int sig)
563 {
564         printf("Received signal %d, exiting...\n", sig);
565         VID_RestoreSystemGamma();
566         Sys_Quit();
567         exit(0);
568 }
569
570 void InitSig(void)
571 {
572         signal(SIGHUP, signal_handler);
573         signal(SIGINT, signal_handler);
574         signal(SIGQUIT, signal_handler);
575         signal(SIGILL, signal_handler);
576         signal(SIGTRAP, signal_handler);
577         signal(SIGIOT, signal_handler);
578         signal(SIGBUS, signal_handler);
579         signal(SIGFPE, signal_handler);
580         signal(SIGSEGV, signal_handler);
581         signal(SIGTERM, signal_handler);
582 }
583
584 /*
585 =================
586 VID_GetWindowSize
587 =================
588 */
589 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
590 {
591         *x = *y = 0;
592         *width = scr_width;
593         *height = scr_height;
594 }
595
596 void VID_Finish (void)
597 {
598         int usemouse;
599         if (r_render.integer)
600         {
601                 qglFinish();
602                 qglXSwapBuffers(vidx11_display, win);
603         }
604
605 // handle the mouse state when windowed if that's changed
606         usemouse = false;
607         if (vid_mouse.integer && !key_consoleactive)
608                 usemouse = true;
609         if (vidmode_active)
610                 usemouse = true;
611         if (usemouse)
612         {
613                 if (!usingmouse)
614                 {
615                         usingmouse = true;
616                         IN_ActivateMouse ();
617                 }
618         }
619         else
620         {
621                 if (usingmouse)
622                 {
623                         usingmouse = false;
624                         IN_DeactivateMouse ();
625                 }
626         }
627 }
628
629 int VID_SetGamma(unsigned short *ramps)
630 {
631         return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
632 }
633
634 int VID_GetGamma(unsigned short *ramps)
635 {
636         return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
637 }
638
639 void VID_Init(void)
640 {
641         Cvar_RegisterVariable (&vid_dga);
642         Cvar_RegisterVariable (&vid_dga_mouseaccel);
643         InitSig(); // trap evil signals
644         if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
645                 mouse_avail = false;
646 }
647
648 void VID_BuildGLXAttrib(int *attrib, int stencil)
649 {
650         *attrib++ = GLX_RGBA;
651         *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
652         *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
653         *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
654         *attrib++ = GLX_DOUBLEBUFFER;
655         *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
656         // if stencil is enabled, ask for alpha too
657         if (stencil)
658         {
659                 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
660                 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
661         }
662         *attrib++ = None;
663 }
664
665 int VID_InitMode(int fullscreen, int width, int height, int bpp, int stencil)
666 {
667         int i;
668         int attrib[32];
669         XSetWindowAttributes attr;
670         unsigned long mask;
671         Window root;
672         XVisualInfo *visinfo;
673         int MajorVersion, MinorVersion;
674         const char *drivername;
675
676         drivername = "libGL.so.1";
677         i = COM_CheckParm("-gl_driver");
678         if (i && i < com_argc - 1)
679                 drivername = com_argv[i + 1];
680         if (!GL_OpenLibrary(drivername))
681         {
682                 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
683                 return false;
684         }
685
686         if (!(vidx11_display = XOpenDisplay(NULL)))
687         {
688                 Con_Printf("Couldn't open the X display\n");
689                 return false;
690         }
691
692         vidx11_screen = DefaultScreen(vidx11_display);
693         root = RootWindow(vidx11_display, vidx11_screen);
694
695         // Get video mode list
696         MajorVersion = MinorVersion = 0;
697         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
698                 vidmode_ext = false;
699         else
700         {
701                 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
702                 vidmode_ext = true;
703         }
704
705         if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
706          || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
707          || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
708          || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
709          || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
710         {
711                 Con_Printf("glX functions not found in %s\n", gl_driver);
712                 return false;
713         }
714
715         VID_BuildGLXAttrib(attrib, stencil);
716         visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
717         if (!visinfo)
718         {
719                 Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
720                 return false;
721         }
722
723         if (vidmode_ext)
724         {
725                 int best_fit, best_dist, dist, x, y;
726
727                 // Are we going fullscreen?  If so, let's change video mode
728                 if (fullscreen)
729                 {
730                         XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
731                         best_dist = 9999999;
732                         best_fit = -1;
733
734                         for (i = 0; i < num_vidmodes; i++)
735                         {
736                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
737                                         continue;
738
739                                 x = width - vidmodes[i]->hdisplay;
740                                 y = height - vidmodes[i]->vdisplay;
741                                 dist = (x * x) + (y * y);
742                                 if (dist < best_dist)
743                                 {
744                                         best_dist = dist;
745                                         best_fit = i;
746                                 }
747                         }
748
749                         if (best_fit != -1)
750                         {
751                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
752                                 // to width/height =, so the window will take the full area of
753                                 // the mode chosen
754                                 width = vidmodes[best_fit]->hdisplay;
755                                 height = vidmodes[best_fit]->vdisplay;
756
757                                 // change to the mode
758                                 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
759                                 vidmode_active = true;
760
761                                 // Move the viewport to top left
762                                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
763                         }
764                         else
765                                 fullscreen = 0;
766                 }
767         }
768
769         // LordHavoc: save the visual for use in gamma ramp settings later
770         vidx11_visual = visinfo->visual;
771
772         /* window attributes */
773         attr.background_pixel = 0;
774         attr.border_pixel = 0;
775         // LordHavoc: save the colormap for later, too
776         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
777         attr.event_mask = X_MASK;
778         if (vidmode_active)
779         {
780                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
781                 attr.override_redirect = True;
782                 attr.backing_store = NotUseful;
783                 attr.save_under = False;
784         }
785         else
786                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
787
788         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
789         XStoreName(vidx11_display, win, gamename);
790         XMapWindow(vidx11_display, win);
791
792         // LordHavoc: making the close button on a window do the right thing
793         // seems to involve this mess, sigh...
794         wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
795         XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
796
797         if (vidmode_active)
798         {
799                 XMoveWindow(vidx11_display, win, 0, 0);
800                 XRaiseWindow(vidx11_display, win);
801                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
802                 XFlush(vidx11_display);
803                 // Move the viewport to top left
804                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
805         }
806
807         //XSync(vidx11_display, False);
808
809         ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
810         if (!ctx)
811                 Sys_Error ("glXCreateContext failed\n");
812
813         if (!qglXMakeCurrent(vidx11_display, win, ctx))
814                 Sys_Error ("glXMakeCurrent failed\n");
815
816         XSync(vidx11_display, False);
817
818         scr_width = width;
819         scr_height = height;
820
821         if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
822                 Sys_Error("glGetString not found in %s", gl_driver);
823
824         gl_renderer = qglGetString(GL_RENDERER);
825         gl_vendor = qglGetString(GL_VENDOR);
826         gl_version = qglGetString(GL_VERSION);
827         gl_extensions = qglGetString(GL_EXTENSIONS);
828         gl_platform = "GLX";
829         gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
830
831         GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
832         gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
833
834         usingmouse = false;
835         vid_hidden = false;
836         vid_allowhwgamma = true;
837         GL_Init();
838         return true;
839 }
840
841 void Sys_SendKeyEvents(void)
842 {
843         HandleEvents();
844 }
845
846 /*
847 ===========
848 IN_Commands
849 ===========
850 */
851 void IN_Commands (void)
852 {
853 }
854
855 void IN_Move (usercmd_t *cmd)
856 {
857         if (mouse_avail)
858                 IN_Mouse(cmd, mouse_x, mouse_y);
859         mouse_x = 0;
860         mouse_y = 0;
861 }
862
863 static void *prjobj = NULL;
864
865 int GL_OpenLibrary(const char *name)
866 {
867         Con_Printf("Loading OpenGL driver %s\n", name);
868         GL_CloseLibrary();
869         if (!(prjobj = dlopen(name, RTLD_LAZY)))
870         {
871                 Con_Printf("Unable to open symbol list for %s\n", name);
872                 return false;
873         }
874         strcpy(gl_driver, name);
875         return true;
876 }
877
878 void GL_CloseLibrary(void)
879 {
880         if (prjobj)
881                 dlclose(prjobj);
882         prjobj = NULL;
883         gl_driver[0] = 0;
884         qglXGetProcAddressARB = NULL;
885         gl_extensions = "";
886         gl_platform = "";
887         gl_platformextensions = "";
888 }
889
890 void *GL_GetProcAddress(const char *name)
891 {
892         void *p = NULL;
893         if (qglXGetProcAddressARB != NULL)
894                 p = (void *) qglXGetProcAddressARB(name);
895         if (p == NULL)
896                 p = (void *) dlsym(prjobj, name);
897         return p;
898 }
899