]> git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_sdl.c
now that performance of cl_decals_newsystem is not terrible, enable it
[xonotic/darkplaces.git] / vid_sdl.c
1 /*
2 Copyright (C) 2003  T. Joseph Carter
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 #undef WIN32_LEAN_AND_MEAN  //hush a warning, SDL.h redefines this
20 #include <SDL.h>
21 #include <SDL_syswm.h>
22 #include <stdio.h>
23
24 #include "quakedef.h"
25 #include "image.h"
26
27 #ifdef WIN32
28 #define SDL_R_RESTART
29 #endif
30
31 // Tell startup code that we have a client
32 int cl_available = true;
33
34 qboolean vid_supportrefreshrate = false;
35
36 cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"};
37 cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"};
38 cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple"};
39 cvar_t joy_axisforward = {0, "joy_axisforward", "1", "which joystick axis to query for forward/backward movement"};
40 cvar_t joy_axisside = {0, "joy_axisside", "0", "which joystick axis to query for right/left movement"};
41 cvar_t joy_axisup = {0, "joy_axisup", "-1", "which joystick axis to query for up/down movement"};
42 cvar_t joy_axispitch = {0, "joy_axispitch", "3", "which joystick axis to query for looking up/down"};
43 cvar_t joy_axisyaw = {0, "joy_axisyaw", "2", "which joystick axis to query for looking right/left"};
44 cvar_t joy_axisroll = {0, "joy_axisroll", "-1", "which joystick axis to query for tilting head right/left"};
45 cvar_t joy_deadzoneforward = {0, "joy_deadzoneforward", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
46 cvar_t joy_deadzoneside = {0, "joy_deadzoneside", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
47 cvar_t joy_deadzoneup = {0, "joy_deadzoneup", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
48 cvar_t joy_deadzonepitch = {0, "joy_deadzonepitch", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
49 cvar_t joy_deadzoneyaw = {0, "joy_deadzoneyaw", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
50 cvar_t joy_deadzoneroll = {0, "joy_deadzoneroll", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
51 cvar_t joy_sensitivityforward = {0, "joy_sensitivityforward", "-1", "movement multiplier"};
52 cvar_t joy_sensitivityside = {0, "joy_sensitivityside", "1", "movement multiplier"};
53 cvar_t joy_sensitivityup = {0, "joy_sensitivityup", "1", "movement multiplier"};
54 cvar_t joy_sensitivitypitch = {0, "joy_sensitivitypitch", "1", "movement multiplier"};
55 cvar_t joy_sensitivityyaw = {0, "joy_sensitivityyaw", "-1", "movement multiplier"};
56 cvar_t joy_sensitivityroll = {0, "joy_sensitivityroll", "1", "movement multiplier"};
57 cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"};
58
59 static qboolean vid_usingmouse = false;
60 static qboolean vid_usinghidecursor = false;
61 static qboolean vid_isfullscreen;
62 static int vid_numjoysticks = 0;
63 #define MAX_JOYSTICKS 8
64 static SDL_Joystick *vid_joysticks[MAX_JOYSTICKS];
65
66 static int win_half_width = 50;
67 static int win_half_height = 50;
68 static int video_bpp, video_flags;
69
70 static SDL_Surface *screen;
71
72 // joystick axes state
73 #define MAX_JOYSTICK_AXES       16
74 typedef struct
75 {
76         float oldmove;
77         float move;
78         double keytime;
79 }joy_axiscache_t;
80 static joy_axiscache_t joy_axescache[MAX_JOYSTICK_AXES];
81
82 /////////////////////////
83 // Input handling
84 ////
85 //TODO: Add joystick support
86 //TODO: Add error checking
87
88
89 //keysym to quake keysym mapping
90 #define tenoh   0,0,0,0,0, 0,0,0,0,0
91 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
92 #define hundredoh fiftyoh, fiftyoh
93 static unsigned int tbl_sdltoquake[] =
94 {
95         0,0,0,0,                //SDLK_UNKNOWN          = 0,
96         0,0,0,0,                //SDLK_FIRST            = 0,
97         K_BACKSPACE,    //SDLK_BACKSPACE        = 8,
98         K_TAB,                  //SDLK_TAB                      = 9,
99         0,0,
100         0,                              //SDLK_CLEAR            = 12,
101         K_ENTER,                //SDLK_RETURN           = 13,
102     0,0,0,0,0,
103         K_PAUSE,                //SDLK_PAUSE            = 19,
104         0,0,0,0,0,0,0,
105         K_ESCAPE,               //SDLK_ESCAPE           = 27,
106         0,0,0,0,
107         K_SPACE,                //SDLK_SPACE            = 32,
108         '!',                    //SDLK_EXCLAIM          = 33,
109         '"',                    //SDLK_QUOTEDBL         = 34,
110         '#',                    //SDLK_HASH                     = 35,
111         '$',                    //SDLK_DOLLAR           = 36,
112         0,
113         '&',                    //SDLK_AMPERSAND        = 38,
114         '\'',                   //SDLK_QUOTE            = 39,
115         '(',                    //SDLK_LEFTPAREN        = 40,
116         ')',                    //SDLK_RIGHTPAREN       = 41,
117         '*',                    //SDLK_ASTERISK         = 42,
118         '+',                    //SDLK_PLUS                     = 43,
119         ',',                    //SDLK_COMMA            = 44,
120         '-',                    //SDLK_MINUS            = 45,
121         '.',                    //SDLK_PERIOD           = 46,
122         '/',                    //SDLK_SLASH            = 47,
123         '0',                    //SDLK_0                        = 48,
124         '1',                    //SDLK_1                        = 49,
125         '2',                    //SDLK_2                        = 50,
126         '3',                    //SDLK_3                        = 51,
127         '4',                    //SDLK_4                        = 52,
128         '5',                    //SDLK_5                        = 53,
129         '6',                    //SDLK_6                        = 54,
130         '7',                    //SDLK_7                        = 55,
131         '8',                    //SDLK_8                        = 56,
132         '9',                    //SDLK_9                        = 57,
133         ':',                    //SDLK_COLON            = 58,
134         ';',                    //SDLK_SEMICOLON        = 59,
135         '<',                    //SDLK_LESS                     = 60,
136         '=',                    //SDLK_EQUALS           = 61,
137         '>',                    //SDLK_GREATER          = 62,
138         '?',                    //SDLK_QUESTION         = 63,
139         '@',                    //SDLK_AT                       = 64,
140         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
141         '[',            //SDLK_LEFTBRACKET      = 91,
142         '\\',           //SDLK_BACKSLASH        = 92,
143         ']',            //SDLK_RIGHTBRACKET     = 93,
144         '^',            //SDLK_CARET            = 94,
145         '_',            //SDLK_UNDERSCORE       = 95,
146         '`',            //SDLK_BACKQUOTE        = 96,
147         'a',            //SDLK_a                        = 97,
148         'b',            //SDLK_b                        = 98,
149         'c',            //SDLK_c                        = 99,
150         'd',            //SDLK_d                        = 100,
151         'e',            //SDLK_e                        = 101,
152         'f',            //SDLK_f                        = 102,
153         'g',            //SDLK_g                        = 103,
154         'h',            //SDLK_h                        = 104,
155         'i',            //SDLK_i                        = 105,
156         'j',            //SDLK_j                        = 106,
157         'k',            //SDLK_k                        = 107,
158         'l',            //SDLK_l                        = 108,
159         'm',            //SDLK_m                        = 109,
160         'n',            //SDLK_n                        = 110,
161         'o',            //SDLK_o                        = 111,
162         'p',            //SDLK_p                        = 112,
163         'q',            //SDLK_q                        = 113,
164         'r',            //SDLK_r                        = 114,
165         's',            //SDLK_s                        = 115,
166         't',            //SDLK_t                        = 116,
167         'u',            //SDLK_u                        = 117,
168         'v',            //SDLK_v                        = 118,
169         'w',            //SDLK_w                        = 119,
170         'x',            //SDLK_x                        = 120,
171         'y',            //SDLK_y                        = 121,
172         'z',            //SDLK_z                        = 122,
173         0,0,0,0,
174         K_DEL,          //SDLK_DELETE           = 127,
175         hundredoh /*227*/, tenoh, tenoh, 0,0,0,0,0,0,0,0,
176         K_KP_0,         //SDLK_KP0              = 256,
177         K_KP_1,         //SDLK_KP1              = 257,
178         K_KP_2,         //SDLK_KP2              = 258,
179         K_KP_3,         //SDLK_KP3              = 259,
180         K_KP_4,         //SDLK_KP4              = 260,
181         K_KP_5,         //SDLK_KP5              = 261,
182         K_KP_6,         //SDLK_KP6              = 262,
183         K_KP_7,         //SDLK_KP7              = 263,
184         K_KP_8,         //SDLK_KP8              = 264,
185         K_KP_9,         //SDLK_KP9              = 265,
186         K_KP_PERIOD,//SDLK_KP_PERIOD    = 266,
187         K_KP_DIVIDE,//SDLK_KP_DIVIDE    = 267,
188         K_KP_MULTIPLY,//SDLK_KP_MULTIPLY= 268,
189         K_KP_MINUS,     //SDLK_KP_MINUS         = 269,
190         K_KP_PLUS,      //SDLK_KP_PLUS          = 270,
191         K_KP_ENTER,     //SDLK_KP_ENTER         = 271,
192         K_KP_EQUALS,//SDLK_KP_EQUALS    = 272,
193         K_UPARROW,      //SDLK_UP               = 273,
194         K_DOWNARROW,//SDLK_DOWN         = 274,
195         K_RIGHTARROW,//SDLK_RIGHT       = 275,
196         K_LEFTARROW,//SDLK_LEFT         = 276,
197         K_INS,          //SDLK_INSERT   = 277,
198         K_HOME,         //SDLK_HOME             = 278,
199         K_END,          //SDLK_END              = 279,
200         K_PGUP,         //SDLK_PAGEUP   = 280,
201         K_PGDN,         //SDLK_PAGEDOWN = 281,
202         K_F1,           //SDLK_F1               = 282,
203         K_F2,           //SDLK_F2               = 283,
204         K_F3,           //SDLK_F3               = 284,
205         K_F4,           //SDLK_F4               = 285,
206         K_F5,           //SDLK_F5               = 286,
207         K_F6,           //SDLK_F6               = 287,
208         K_F7,           //SDLK_F7               = 288,
209         K_F8,           //SDLK_F8               = 289,
210         K_F9,           //SDLK_F9               = 290,
211         K_F10,          //SDLK_F10              = 291,
212         K_F11,          //SDLK_F11              = 292,
213         K_F12,          //SDLK_F12              = 293,
214         0,                      //SDLK_F13              = 294,
215         0,                      //SDLK_F14              = 295,
216         0,                      //SDLK_F15              = 296,
217         0,0,0,
218         K_NUMLOCK,      //SDLK_NUMLOCK  = 300,
219         K_CAPSLOCK,     //SDLK_CAPSLOCK = 301,
220         K_SCROLLOCK,//SDLK_SCROLLOCK= 302,
221         K_SHIFT,        //SDLK_RSHIFT   = 303,
222         K_SHIFT,        //SDLK_LSHIFT   = 304,
223         K_CTRL,         //SDLK_RCTRL    = 305,
224         K_CTRL,         //SDLK_LCTRL    = 306,
225         K_ALT,          //SDLK_RALT             = 307,
226         K_ALT,          //SDLK_LALT             = 308,
227         0,                      //SDLK_RMETA    = 309,
228         0,                      //SDLK_LMETA    = 310,
229         0,                      //SDLK_LSUPER   = 311,          /* Left "Windows" key */
230         0,                      //SDLK_RSUPER   = 312,          /* Right "Windows" key */
231         K_ALT,                  //SDLK_MODE             = 313,          /* "Alt Gr" key */
232         0,                      //SDLK_COMPOSE  = 314,          /* Multi-key compose key */
233         0,                      //SDLK_HELP             = 315,
234         0,                      //SDLK_PRINT    = 316,
235         0,                      //SDLK_SYSREQ   = 317,
236         K_PAUSE,        //SDLK_BREAK    = 318,
237         0,                      //SDLK_MENU             = 319,
238         0,                      //SDLK_POWER    = 320,          /* Power Macintosh power key */
239         'e',            //SDLK_EURO             = 321,          /* Some european keyboards */
240         0                       //SDLK_UNDO             = 322,          /* Atari keyboard has Undo */
241 };
242 #undef tenoh
243 #undef fiftyoh
244 #undef hundredoh
245
246 static int MapKey( unsigned int sdlkey )
247 {
248         if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
249                 return 0;
250     return tbl_sdltoquake[ sdlkey ];
251 }
252
253 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
254 {
255         if (vid_usingmouse != relative)
256         {
257                 vid_usingmouse = relative;
258                 cl_ignoremousemoves = 2;
259                 SDL_WM_GrabInput( relative ? SDL_GRAB_ON : SDL_GRAB_OFF );
260         }
261         if (vid_usinghidecursor != hidecursor)
262         {
263                 vid_usinghidecursor = hidecursor;
264                 SDL_ShowCursor( hidecursor ? SDL_DISABLE : SDL_ENABLE);
265         }
266 }
267
268 static double IN_JoystickGetAxis(SDL_Joystick *joy, int axis, double sensitivity, double deadzone)
269 {
270         double value;
271         if (axis < 0 || axis >= SDL_JoystickNumAxes(joy))
272                 return 0; // no such axis on this joystick
273         value = SDL_JoystickGetAxis(joy, axis) * (1.0 / 32767.0);
274         value = bound(-1, value, 1);
275         if (fabs(value) < deadzone)
276                 return 0; // within deadzone around center
277         return value * sensitivity;
278 }
279
280 /////////////////////
281 // Joystick axis keyevents
282 // a sort of hack emulating Arrow keys for joystick axises
283 // as some drives dont send such keyevents for them
284 // additionally we should block drivers that do send arrow keyevents to prevent double events
285 ////
286
287 static void IN_JoystickKeyeventForAxis(SDL_Joystick *joy, int axis, int key_pos, int key_neg)
288 {
289         double joytime;
290
291         if (axis < 0 || axis >= SDL_JoystickNumAxes(joy))
292                 return; // no such axis on this joystick
293
294         joytime = Sys_DoubleTime();
295         // no key event, continuous keydown event
296         if (joy_axescache[axis].move == joy_axescache[axis].oldmove)
297         {
298                 if (joy_axescache[axis].move != 0 && joytime > joy_axescache[axis].keytime)
299                 {
300                         //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
301                         Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
302                         joy_axescache[axis].keytime = joytime + 0.5 / 20;
303                 }
304                 return;
305         }
306         // generate key up event
307         if (joy_axescache[axis].oldmove)
308         {
309                 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg), 1, cl.time);
310                 Key_Event((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg, 0, 0);
311         }
312         // generate key down event
313         if (joy_axescache[axis].move)
314         {
315                 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
316                 Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
317                 joy_axescache[axis].keytime = joytime + 0.5;
318         }
319 }
320
321 static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode)
322 {
323         if (!joy_axiskeyevents.integer)
324                 return false;
325
326         // block keyevent if it's going to be provided by joystick keyevent system
327         if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks)
328         {
329                 SDL_Joystick *joy = vid_joysticks[joy_index.integer];
330
331                 if (keycode == K_UPARROW || keycode == K_DOWNARROW)
332                         if (IN_JoystickGetAxis(joy, joy_axisforward.integer, 1, 0.01) || joy_axescache[joy_axisforward.integer].move || joy_axescache[joy_axisforward.integer].oldmove)
333                                 return true;
334                 if (keycode == K_RIGHTARROW || keycode == K_LEFTARROW)
335                         if (IN_JoystickGetAxis(joy, joy_axisside.integer, 1, 0.01) || joy_axescache[joy_axisside.integer].move || joy_axescache[joy_axisside.integer].oldmove)
336                                 return true;
337         }
338
339         return false;
340 }
341
342 /////////////////////
343 // Movement handling
344 ////
345
346 void IN_Move( void )
347 {
348         int j;
349         static int old_x = 0, old_y = 0;
350         static int stuck = 0;
351         int x, y, numaxes, numballs;
352
353         if (vid_usingmouse)
354         {
355                 if(vid_stick_mouse.integer)
356                 {
357                         // have the mouse stuck in the middle, example use: prevent expose effect of beryl during the game when not using
358                         // window grabbing. --blub
359
360                         // we need 2 frames to initialize the center position
361                         if(!stuck)
362                         {
363                                 SDL_WarpMouse(win_half_width, win_half_height);
364                                 SDL_GetMouseState(&x, &y);
365                                 SDL_GetRelativeMouseState(&x, &y);
366                                 ++stuck;
367                         } else {
368                                 SDL_GetRelativeMouseState(&x, &y);
369                                 in_mouse_x = x + old_x;
370                                 in_mouse_y = y + old_y;
371                                 SDL_GetMouseState(&x, &y);
372                                 old_x = x - win_half_width;
373                                 old_y = y - win_half_height;
374                                 SDL_WarpMouse(win_half_width, win_half_height);
375                         }
376                 } else {
377                         SDL_GetRelativeMouseState( &x, &y );
378                         in_mouse_x = x;
379                         in_mouse_y = y;
380                 }
381         }
382
383         SDL_GetMouseState(&x, &y);
384         in_windowmouse_x = x;
385         in_windowmouse_y = y;
386
387         if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks)
388         {
389                 SDL_Joystick *joy = vid_joysticks[joy_index.integer];
390
391                 // balls convert to mousemove
392                 numballs = SDL_JoystickNumBalls(joy);
393                 for (j = 0;j < numballs;j++)
394                 {
395                         SDL_JoystickGetBall(joy, j, &x, &y);
396                         in_mouse_x += x;
397                         in_mouse_y += y;
398                 }
399
400                 // axes
401                 cl.cmd.forwardmove += IN_JoystickGetAxis(joy, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value;
402                 cl.cmd.sidemove    += IN_JoystickGetAxis(joy, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value;
403                 cl.cmd.upmove      += IN_JoystickGetAxis(joy, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value;
404                 cl.viewangles[0]   += IN_JoystickGetAxis(joy, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value;
405                 cl.viewangles[1]   += IN_JoystickGetAxis(joy, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value;
406                 //cl.viewangles[2]   += IN_JoystickGetAxis(joy, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value;
407         
408                 // cache state of axes to emulate button events for them
409                 numaxes = min(MAX_JOYSTICK_AXES, SDL_JoystickNumAxes(joy));
410                 for (j = 0; j < numaxes; j++)
411                 {
412                         joy_axescache[j].oldmove = joy_axescache[j].move;
413                         joy_axescache[j].move = IN_JoystickGetAxis(joy, j, 1, 0.01);
414                 }
415
416                 // run keyevents
417                 if (joy_axiskeyevents.integer)
418                 {
419                         IN_JoystickKeyeventForAxis(joy, joy_axisforward.integer, K_DOWNARROW, K_UPARROW);
420                         IN_JoystickKeyeventForAxis(joy, joy_axisside.integer, K_RIGHTARROW, K_LEFTARROW);
421                 }
422         }
423 }
424
425 /////////////////////
426 // Message Handling
427 ////
428
429 static int Sys_EventFilter( SDL_Event *event )
430 {
431         //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
432         if (event->type == SDL_QUIT)
433         {
434 #ifdef WIN32
435                 if (MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO)
436                         return 0;
437 #endif
438         }
439         return 1;
440 }
441
442 #ifdef SDL_R_RESTART
443 static qboolean sdl_needs_restart;
444 static void sdl_start(void)
445 {
446 }
447 static void sdl_shutdown(void)
448 {
449         sdl_needs_restart = false;
450 }
451 static void sdl_newmap(void)
452 {
453 }
454 #endif
455
456 static keynum_t buttonremap[18] =
457 {
458         K_MOUSE1,
459         K_MOUSE3,
460         K_MOUSE2,
461         K_MWHEELUP,
462         K_MWHEELDOWN,
463         K_MOUSE4,
464         K_MOUSE5,
465         K_MOUSE6,
466         K_MOUSE7,
467         K_MOUSE8,
468         K_MOUSE9,
469         K_MOUSE10,
470         K_MOUSE11,
471         K_MOUSE12,
472         K_MOUSE13,
473         K_MOUSE14,
474         K_MOUSE15,
475         K_MOUSE16,
476 };
477
478 void Sys_SendKeyEvents( void )
479 {
480         static qboolean sound_active = true;
481         int keycode;
482         SDL_Event event;
483
484         while( SDL_PollEvent( &event ) )
485                 switch( event.type ) {
486                         case SDL_QUIT:
487                                 Sys_Quit(0);
488                                 break;
489                         case SDL_KEYDOWN:
490                         case SDL_KEYUP:
491                                 keycode = MapKey(event.key.keysym.sym);
492                                 if (!IN_JoystickBlockDoubledKeyEvents(keycode))
493                                         Key_Event(keycode, event.key.keysym.unicode, (event.key.state == SDL_PRESSED));
494                                 break;
495                         case SDL_ACTIVEEVENT:
496                                 if( event.active.state & SDL_APPACTIVE )
497                                 {
498                                         if( event.active.gain )
499                                                 vid_hidden = false;
500                                         else
501                                                 vid_hidden = true;
502                                 }
503                                 break;
504                         case SDL_MOUSEBUTTONDOWN:
505                         case SDL_MOUSEBUTTONUP:
506                                 if (event.button.button <= 18)
507                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
508                                 break;
509                         case SDL_JOYBUTTONDOWN:
510                                 if (!joy_enable.integer)
511                                         break; // ignore down events if joystick has been disabled
512                         case SDL_JOYBUTTONUP:
513                                 if (event.jbutton.button < 48)
514                                         Key_Event( event.jbutton.button + (event.jbutton.button < 16 ? K_JOY1 : K_AUX1 - 16), 0, (event.jbutton.state == SDL_PRESSED) );
515                                 break;
516                         case SDL_VIDEORESIZE:
517                                 if(vid_resizable.integer < 2)
518                                 {
519                                         vid.width = event.resize.w;
520                                         vid.height = event.resize.h;
521                                         SDL_SetVideoMode(vid.width, vid.height, video_bpp, video_flags);
522 #ifdef SDL_R_RESTART
523                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
524                                         // so, let's better queue it for next frame
525                                         if(!sdl_needs_restart)
526                                         {
527                                                 Cbuf_AddText("\nr_restart\n");
528                                                 sdl_needs_restart = true;
529                                         }
530 #endif
531                                 }
532                                 break;
533                 }
534
535         // enable/disable sound on focus gain/loss
536         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
537         {
538                 if (!sound_active)
539                 {
540                         S_UnblockSound ();
541                         sound_active = true;
542                 }
543         }
544         else
545         {
546                 if (sound_active)
547                 {
548                         S_BlockSound ();
549                         sound_active = false;
550                 }
551         }
552 }
553
554 /////////////////
555 // Video system
556 ////
557
558 void *GL_GetProcAddress(const char *name)
559 {
560         void *p = NULL;
561         p = SDL_GL_GetProcAddress(name);
562         return p;
563 }
564
565 static int Sys_EventFilter( SDL_Event *event );
566 static qboolean vid_sdl_initjoysticksystem = false;
567
568 void VID_Init (void)
569 {
570         Cvar_RegisterVariable(&joy_detected);
571         Cvar_RegisterVariable(&joy_enable);
572         Cvar_RegisterVariable(&joy_index);
573         Cvar_RegisterVariable(&joy_axisforward);
574         Cvar_RegisterVariable(&joy_axisside);
575         Cvar_RegisterVariable(&joy_axisup);
576         Cvar_RegisterVariable(&joy_axispitch);
577         Cvar_RegisterVariable(&joy_axisyaw);
578         //Cvar_RegisterVariable(&joy_axisroll);
579         Cvar_RegisterVariable(&joy_deadzoneforward);
580         Cvar_RegisterVariable(&joy_deadzoneside);
581         Cvar_RegisterVariable(&joy_deadzoneup);
582         Cvar_RegisterVariable(&joy_deadzonepitch);
583         Cvar_RegisterVariable(&joy_deadzoneyaw);
584         //Cvar_RegisterVariable(&joy_deadzoneroll);
585         Cvar_RegisterVariable(&joy_sensitivityforward);
586         Cvar_RegisterVariable(&joy_sensitivityside);
587         Cvar_RegisterVariable(&joy_sensitivityup);
588         Cvar_RegisterVariable(&joy_sensitivitypitch);
589         Cvar_RegisterVariable(&joy_sensitivityyaw);
590         //Cvar_RegisterVariable(&joy_sensitivityroll);
591         Cvar_RegisterVariable(&joy_axiskeyevents);
592         
593 #ifdef SDL_R_RESTART
594         R_RegisterModule("SDL", sdl_start, sdl_shutdown, sdl_newmap, NULL, NULL);
595 #endif
596
597         if (SDL_Init(SDL_INIT_VIDEO) < 0)
598                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
599         vid_sdl_initjoysticksystem = SDL_Init(SDL_INIT_JOYSTICK) >= 0;
600         if (vid_sdl_initjoysticksystem)
601                 Con_Printf("Failed to init SDL joystick subsystem: %s\n", SDL_GetError());
602         vid_isfullscreen = false;
603 }
604
605 // set the icon (we dont use SDL here since it would be too much a PITA)
606 #ifdef WIN32
607 #include "resource.h"
608 #include <SDL_syswm.h>
609 static void VID_SetCaption(void)
610 {
611     SDL_SysWMinfo       info;
612         HICON                   icon;
613
614         // set the caption
615         SDL_WM_SetCaption( gamename, NULL );
616
617         // get the HWND handle
618     SDL_VERSION( &info.version );
619         if( !SDL_GetWMInfo( &info ) )
620                 return;
621
622         icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
623 #ifndef _W64 //If Windows 64bit data types don't exist
624 #ifndef SetClassLongPtr
625 #define SetClassLongPtr SetClassLong
626 #endif
627 #ifndef GCLP_HICON
628 #define GCLP_HICON GCL_HICON
629 #endif
630 #ifndef LONG_PTR
631 #define LONG_PTR LONG
632 #endif
633 #endif
634         SetClassLongPtr( info.window, GCLP_HICON, (LONG_PTR)icon );
635 }
636 static void VID_SetIcon_Pre(void)
637 {
638 }
639 static void VID_SetIcon_Post(void)
640 {
641 }
642 #else
643 // Adding the OS independent XPM version --blub
644 #include "darkplaces.xpm"
645 #include "nexuiz.xpm"
646 static SDL_Surface *icon = NULL;
647 static void VID_SetIcon_Pre(void)
648 {
649         /*
650          * Somewhat restricted XPM reader. Only supports XPMs saved by GIMP 2.4 at
651          * default settings with less than 91 colors and transparency.
652          */
653
654         int width, height, colors, isize, i, j;
655         int thenone = -1;
656         static SDL_Color palette[256];
657         unsigned short palenc[256]; // store color id by char
658         char *xpm;
659         char **idata, *data;
660         const SDL_version *version;
661
662         version = SDL_Linked_Version();
663         // only use non-XPM icon support in SDL v1.3 and higher
664         // SDL v1.2 does not support "smooth" transparency, and thus is better
665         // off the xpm way
666         if(version->major >= 2 || (version->major == 1 && version->minor >= 3))
667         {
668                 data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
669                 if(data)
670                 {
671                         unsigned int red = 0x00FF0000;
672                         unsigned int green = 0x0000FF00;
673                         unsigned int blue = 0x000000FF;
674                         unsigned int alpha = 0xFF000000;
675                         width = image_width;
676                         height = image_height;
677
678                         // reallocate with malloc, as this is in tempmempool (do not want)
679                         xpm = data;
680                         data = malloc(width * height * 4);
681                         memcpy(data, xpm, width * height * 4);
682                         Mem_Free(xpm);
683                         xpm = NULL;
684
685                         icon = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, LittleLong(red), LittleLong(green), LittleLong(blue), LittleLong(alpha));
686
687                         if(icon == NULL) {
688                                 Con_Printf(     "Failed to create surface for the window Icon!\n"
689                                                 "%s\n", SDL_GetError());
690                                 free(data);
691                                 return;
692                         }
693
694                         icon->pixels = data;
695                 }
696         }
697
698         // we only get here if non-XPM icon was missing, or SDL version is not
699         // sufficient for transparent non-XPM icons
700         if(!icon)
701         {
702                 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
703                 idata = NULL;
704                 if(xpm)
705                         idata = XPM_DecodeString(xpm);
706                 if(!idata)
707                         idata = ENGINE_ICON;
708                 if(xpm)
709                         Mem_Free(xpm);
710
711                 data = idata[0];
712
713                 if(sscanf(data, "%i %i %i %i", &width, &height, &colors, &isize) != 4)
714                 {
715                         // NOTE: Only 1-char colornames are supported
716                         Con_Printf("Sorry, but this does not even look similar to an XPM.\n");
717                         return;
718                 }
719
720                 if(isize != 1)
721                 {
722                         // NOTE: Only 1-char colornames are supported
723                         Con_Printf("This XPM's palette is either huge or idiotically unoptimized. It's key size is %i\n", isize);
724                         return;
725                 }
726
727                 for(i = 0; i < colors; ++i)
728                 {
729                         unsigned int r, g, b;
730                         char idx;
731
732                         if(sscanf(idata[i+1], "%c c #%02x%02x%02x", &idx, &r, &g, &b) != 4)
733                         {
734                                 char foo[2];
735                                 if(sscanf(idata[i+1], "%c c Non%1[e]", &idx, foo) != 2) // I take the DailyWTF credit for this. --div0
736                                 {
737                                         Con_Printf("This XPM's palette looks odd. Can't continue.\n");
738                                         return;
739                                 }
740                                 else
741                                 {
742                                         palette[i].r = 255; // color key
743                                         palette[i].g = 0;
744                                         palette[i].b = 255;
745                                         thenone = i; // weeeee
746                                 }
747                         }
748                         else
749                         {
750                                 palette[i].r = r - (r == 255 && g == 0 && b == 255); // change 255/0/255 pink to 254/0/255 for color key
751                                 palette[i].g = g;
752                                 palette[i].b = b;
753                         }
754
755                         palenc[(unsigned char) idx] = i;
756                 }
757
758                 // allocate the image data
759                 data = (char*) malloc(width*height);
760
761                 for(j = 0; j < height; ++j)
762                 {
763                         for(i = 0; i < width; ++i)
764                         {
765                                 // casting to the safest possible datatypes ^^
766                                 data[j * width + i] = palenc[((unsigned char*)idata[colors+j+1])[i]];
767                         }
768                 }
769
770                 if(icon != NULL)
771                 {
772                         // SDL_FreeSurface should free the data too
773                         // but for completeness' sake...
774                         if(icon->flags & SDL_PREALLOC)
775                         {
776                                 free(icon->pixels);
777                                 icon->pixels = NULL; // safety
778                         }
779                         SDL_FreeSurface(icon);
780                 }
781
782                 icon = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, width, height, 8, 0,0,0,0);// rmask, gmask, bmask, amask); no mask needed
783                 // 8 bit surfaces get an empty palette allocated according to the docs
784                 // so it's a palette image for sure :) no endian check necessary for the mask
785
786                 if(icon == NULL) {
787                         Con_Printf(     "Failed to create surface for the window Icon!\n"
788                                         "%s\n", SDL_GetError());
789                         free(data);
790                         return;
791                 }
792
793                 icon->pixels = data;
794                 SDL_SetPalette(icon, SDL_PHYSPAL|SDL_LOGPAL, palette, 0, colors);
795                 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, thenone);
796         }
797
798         SDL_WM_SetIcon(icon, NULL);
799 }
800 static void VID_SetIcon_Post(void)
801 {
802 #if SDL_VIDEO_DRIVER_X11 && !SDL_VIDEO_DRIVER_QUARTZ
803         int j;
804         char *data;
805         const SDL_version *version;
806
807         version = SDL_Linked_Version();
808         // only use non-XPM icon support in SDL v1.3 and higher
809         // SDL v1.2 does not support "smooth" transparency, and thus is better
810         // off the xpm way
811         if(!(version->major >= 2 || (version->major == 1 && version->minor >= 3)))
812         {
813                 // in this case, we did not set the good icon yet
814                 SDL_SysWMinfo info;
815                 SDL_VERSION(&info.version);
816                 if(SDL_GetWMInfo(&info) == 1 && info.subsystem == SDL_SYSWM_X11)
817                 {
818                         data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
819                         if(data)
820                         {
821                                 // use _NET_WM_ICON too
822                                 static long netwm_icon[MAX_NETWM_ICON];
823                                 int pos = 0;
824                                 int i = 1;
825
826                                 while(data)
827                                 {
828                                         if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
829                                         {
830                                                 netwm_icon[pos++] = image_width;
831                                                 netwm_icon[pos++] = image_height;
832                                                 for(i = 0; i < image_height; ++i)
833                                                         for(j = 0; j < image_width; ++j)
834                                                                 netwm_icon[pos++] = BuffLittleLong((unsigned char *) &data[(i*image_width+j)*4]);
835                                         }
836                                         else
837                                         {
838                                                 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
839                                         }
840                                         ++i;
841                                         Mem_Free(data);
842                                         data = (char *) loadimagepixelsbgra(va("darkplaces-icon%d", i), false, false, false, NULL);
843                                 }
844
845                                 info.info.x11.lock_func();
846                                 {
847                                         Atom net_wm_icon = XInternAtom(info.info.x11.display, "_NET_WM_ICON", false);
848                                         XChangeProperty(info.info.x11.display, info.info.x11.wmwindow, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
849                                 }
850                                 info.info.x11.unlock_func();
851                         }
852                 }
853         }
854 #endif
855 }
856
857
858 static void VID_SetCaption(void)
859 {
860         SDL_WM_SetCaption( gamename, NULL );
861 }
862 #endif
863
864 static void VID_OutputVersion(void)
865 {
866         const SDL_version *version;
867         version = SDL_Linked_Version();
868         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
869                                         "Using SDL library version %d.%d.%d\n",
870                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
871                                         version->major, version->minor, version->patch );
872 }
873
874 qboolean VID_InitMode(viddef_mode_t *mode)
875 {
876         int i;
877         static int notfirstvideomode = false;
878         int flags = SDL_OPENGL;
879         const char *drivername;
880
881         win_half_width = mode->width>>1;
882         win_half_height = mode->height>>1;
883
884         if(vid_resizable.integer)
885                 flags |= SDL_RESIZABLE;
886
887         VID_OutputVersion();
888
889         /*
890         SDL Hack
891                 We cant switch from one OpenGL video mode to another.
892                 Thus we first switch to some stupid 2D mode and then back to OpenGL.
893         */
894         if (notfirstvideomode)
895                 SDL_SetVideoMode( 0, 0, 0, 0 );
896         notfirstvideomode = true;
897
898         // SDL usually knows best
899         drivername = NULL;
900
901 // COMMANDLINEOPTION: SDL GL: -gl_driver <drivername> selects a GL driver library, default is whatever SDL recommends, useful only for 3dfxogl.dll/3dfxvgl.dll or fxmesa or similar, if you don't know what this is for, you don't need it
902         i = COM_CheckParm("-gl_driver");
903         if (i && i < com_argc - 1)
904                 drivername = com_argv[i + 1];
905         if (SDL_GL_LoadLibrary(drivername) < 0)
906         {
907                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
908                 return false;
909         }
910
911         if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
912         {
913                 VID_Shutdown();
914                 Con_Print("Required OpenGL function glGetString not found\n");
915                 return false;
916         }
917
918         // Knghtbrd: should do platform-specific extension string function here
919
920         vid_isfullscreen = false;
921         if (mode->fullscreen) {
922                 flags |= SDL_FULLSCREEN;
923                 vid_isfullscreen = true;
924         }
925         //flags |= SDL_HWSURFACE;
926
927         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
928         if (mode->bitsperpixel >= 32)
929         {
930                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
931                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
932                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
933                 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
934                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
935                 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
936         }
937         else
938         {
939                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
940                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
941                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
942                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
943         }
944         if (mode->stereobuffer)
945                 SDL_GL_SetAttribute (SDL_GL_STEREO, 1);
946         if (vid_vsync.integer)
947                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1);
948         else
949                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 0);
950         if (mode->samples > 1)
951         {
952                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
953                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, mode->samples);
954         }
955
956         video_bpp = mode->bitsperpixel;
957         video_flags = flags;
958         VID_SetIcon_Pre();
959         screen = SDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
960         VID_SetIcon_Post();
961
962         if (screen == NULL)
963         {
964                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
965                 VID_Shutdown();
966                 return false;
967         }
968
969         // set window title
970         VID_SetCaption();
971         // set up an event filter to ask confirmation on close button in WIN32
972         SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
973         // init keyboard
974         SDL_EnableUNICODE( SDL_ENABLE );
975         // enable key repeat since everyone expects it
976         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
977
978         gl_platform = "SDL";
979         gl_platformextensions = "";
980
981         GL_Init();
982
983         vid_numjoysticks = SDL_NumJoysticks();
984         vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS);
985         Cvar_SetValueQuick(&joy_detected, vid_numjoysticks);
986         Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks);
987         memset(vid_joysticks, 0, sizeof(vid_joysticks));
988         for (i = 0;i < vid_numjoysticks;i++)
989         {
990                 SDL_Joystick *joy;
991                 joy = vid_joysticks[i] = SDL_JoystickOpen(i);
992                 if (!joy)
993                 {
994                         Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError());
995                         continue;
996                 }
997                 Con_Printf("joystick #%i: opened \"%s\" with %i axes, %i buttons, %i balls\n", i, SDL_JoystickName(i), (int)SDL_JoystickNumAxes(joy), (int)SDL_JoystickNumButtons(joy), (int)SDL_JoystickNumBalls(joy));
998         }
999
1000         vid_hidden = false;
1001         vid_activewindow = false;
1002         vid_usingmouse = false;
1003         vid_usinghidecursor = false;
1004
1005         SDL_WM_GrabInput(SDL_GRAB_OFF);
1006         return true;
1007 }
1008
1009 void VID_Shutdown (void)
1010 {
1011         VID_SetMouse(false, false, false);
1012         VID_RestoreSystemGamma();
1013
1014         SDL_QuitSubSystem(SDL_INIT_VIDEO);
1015
1016         gl_driver[0] = 0;
1017         gl_extensions = "";
1018         gl_platform = "";
1019         gl_platformextensions = "";
1020 }
1021
1022 int VID_SetGamma (unsigned short *ramps, int rampsize)
1023 {
1024         return !SDL_SetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
1025 }
1026
1027 int VID_GetGamma (unsigned short *ramps, int rampsize)
1028 {
1029         return !SDL_GetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
1030 }
1031
1032 void VID_Finish (void)
1033 {
1034         Uint8 appstate;
1035
1036         //react on appstate changes
1037         appstate = SDL_GetAppState();
1038
1039         vid_hidden = !(appstate & SDL_APPACTIVE);
1040
1041         if( vid_hidden || !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
1042                 vid_activewindow = false;
1043         else
1044                 vid_activewindow = true;
1045
1046         VID_UpdateGamma(false, 256);
1047
1048         if (!vid_hidden)
1049         {
1050                 CHECKGLERROR
1051                 if (r_speeds.integer == 2 || gl_finish.integer)
1052                 {
1053                         qglFinish();CHECKGLERROR
1054                 }
1055                 SDL_GL_SwapBuffers();
1056         }
1057 }
1058
1059 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1060 {
1061         size_t k;
1062         SDL_Rect **vidmodes;
1063         int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
1064
1065         k = 0;
1066         for(vidmodes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); *vidmodes; ++vidmodes)
1067         {
1068                 if(k >= maxcount)
1069                         break;
1070                 modes[k].width = (*vidmodes)->w;
1071                 modes[k].height = (*vidmodes)->h;
1072                 modes[k].bpp = bpp;
1073                 modes[k].refreshrate = 60; // no support for refresh rate in SDL
1074                 modes[k].pixelheight_num = 1;
1075                 modes[k].pixelheight_denom = 1; // SDL does not provide this
1076                 ++k;
1077         }
1078         return k;
1079 }