Fix setinfo.
[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 <stdio.h>
22
23 #include "quakedef.h"
24 #include "image.h"
25 #include "utf8lib.h"
26
27 #ifndef __IPHONEOS__
28 #ifdef MACOSX
29 #include <Carbon/Carbon.h>
30 #include <IOKit/hidsystem/IOHIDLib.h>
31 #include <IOKit/hidsystem/IOHIDParameter.h>
32 #include <IOKit/hidsystem/event_status_driver.h>
33 static cvar_t apple_mouse_noaccel = {CVAR_SAVE, "apple_mouse_noaccel", "1", "disables mouse acceleration while DarkPlaces is active"};
34 static qboolean vid_usingnoaccel;
35 static double originalMouseSpeed = -1.0;
36 io_connect_t IN_GetIOHandle(void)
37 {
38         io_connect_t iohandle = MACH_PORT_NULL;
39         kern_return_t status;
40         io_service_t iohidsystem = MACH_PORT_NULL;
41         mach_port_t masterport;
42
43         status = IOMasterPort(MACH_PORT_NULL, &masterport);
44         if(status != KERN_SUCCESS)
45                 return 0;
46
47         iohidsystem = IORegistryEntryFromPath(masterport, kIOServicePlane ":/IOResources/IOHIDSystem");
48         if(!iohidsystem)
49                 return 0;
50
51         status = IOServiceOpen(iohidsystem, mach_task_self(), kIOHIDParamConnectType, &iohandle);
52         IOObjectRelease(iohidsystem);
53
54         return iohandle;
55 }
56 #endif
57 #endif
58
59 #ifdef WIN32
60 #define SDL_R_RESTART
61 #endif
62
63 // Tell startup code that we have a client
64 int cl_available = true;
65
66 qboolean vid_supportrefreshrate = false;
67
68 static qboolean vid_usingmouse = false;
69 static qboolean vid_usingmouse_relativeworks = false; // SDL2 workaround for unimplemented RelativeMouse mode
70 static qboolean vid_usinghidecursor = false;
71 static qboolean vid_hasfocus = false;
72 static qboolean vid_isfullscreen;
73 static qboolean vid_usingvsync = false;
74 static SDL_Joystick *vid_sdljoystick = NULL;
75 // GAME_STEELSTORM specific
76 static cvar_t *steelstorm_showing_map = NULL; // detect but do not create the cvar
77 static cvar_t *steelstorm_showing_mousecursor = NULL; // detect but do not create the cvar
78
79 static int win_half_width = 50;
80 static int win_half_height = 50;
81 static int video_bpp;
82
83 static SDL_GLContext context;
84 static SDL_Window *window;
85 static int window_flags;
86 static vid_mode_t desktop_mode;
87
88 // Input handling
89
90 #ifndef SDLK_PERCENT
91 #define SDLK_PERCENT '%'
92 #endif
93
94 static int MapKey( unsigned int sdlkey )
95 {
96         switch(sdlkey)
97         {
98         default: return 0;
99 //      case SDLK_UNKNOWN:            return K_UNKNOWN;
100         case SDLK_RETURN:             return K_ENTER;
101         case SDLK_ESCAPE:             return K_ESCAPE;
102         case SDLK_BACKSPACE:          return K_BACKSPACE;
103         case SDLK_TAB:                return K_TAB;
104         case SDLK_SPACE:              return K_SPACE;
105         case SDLK_EXCLAIM:            return '!';
106         case SDLK_QUOTEDBL:           return '"';
107         case SDLK_HASH:               return '#';
108         case SDLK_PERCENT:            return '%';
109         case SDLK_DOLLAR:             return '$';
110         case SDLK_AMPERSAND:          return '&';
111         case SDLK_QUOTE:              return '\'';
112         case SDLK_LEFTPAREN:          return '(';
113         case SDLK_RIGHTPAREN:         return ')';
114         case SDLK_ASTERISK:           return '*';
115         case SDLK_PLUS:               return '+';
116         case SDLK_COMMA:              return ',';
117         case SDLK_MINUS:              return '-';
118         case SDLK_PERIOD:             return '.';
119         case SDLK_SLASH:              return '/';
120         case SDLK_0:                  return '0';
121         case SDLK_1:                  return '1';
122         case SDLK_2:                  return '2';
123         case SDLK_3:                  return '3';
124         case SDLK_4:                  return '4';
125         case SDLK_5:                  return '5';
126         case SDLK_6:                  return '6';
127         case SDLK_7:                  return '7';
128         case SDLK_8:                  return '8';
129         case SDLK_9:                  return '9';
130         case SDLK_COLON:              return ':';
131         case SDLK_SEMICOLON:          return ';';
132         case SDLK_LESS:               return '<';
133         case SDLK_EQUALS:             return '=';
134         case SDLK_GREATER:            return '>';
135         case SDLK_QUESTION:           return '?';
136         case SDLK_AT:                 return '@';
137         case SDLK_LEFTBRACKET:        return '[';
138         case SDLK_BACKSLASH:          return '\\';
139         case SDLK_RIGHTBRACKET:       return ']';
140         case SDLK_CARET:              return '^';
141         case SDLK_UNDERSCORE:         return '_';
142         case SDLK_BACKQUOTE:          return '`';
143         case SDLK_a:                  return 'a';
144         case SDLK_b:                  return 'b';
145         case SDLK_c:                  return 'c';
146         case SDLK_d:                  return 'd';
147         case SDLK_e:                  return 'e';
148         case SDLK_f:                  return 'f';
149         case SDLK_g:                  return 'g';
150         case SDLK_h:                  return 'h';
151         case SDLK_i:                  return 'i';
152         case SDLK_j:                  return 'j';
153         case SDLK_k:                  return 'k';
154         case SDLK_l:                  return 'l';
155         case SDLK_m:                  return 'm';
156         case SDLK_n:                  return 'n';
157         case SDLK_o:                  return 'o';
158         case SDLK_p:                  return 'p';
159         case SDLK_q:                  return 'q';
160         case SDLK_r:                  return 'r';
161         case SDLK_s:                  return 's';
162         case SDLK_t:                  return 't';
163         case SDLK_u:                  return 'u';
164         case SDLK_v:                  return 'v';
165         case SDLK_w:                  return 'w';
166         case SDLK_x:                  return 'x';
167         case SDLK_y:                  return 'y';
168         case SDLK_z:                  return 'z';
169         case SDLK_CAPSLOCK:           return K_CAPSLOCK;
170         case SDLK_F1:                 return K_F1;
171         case SDLK_F2:                 return K_F2;
172         case SDLK_F3:                 return K_F3;
173         case SDLK_F4:                 return K_F4;
174         case SDLK_F5:                 return K_F5;
175         case SDLK_F6:                 return K_F6;
176         case SDLK_F7:                 return K_F7;
177         case SDLK_F8:                 return K_F8;
178         case SDLK_F9:                 return K_F9;
179         case SDLK_F10:                return K_F10;
180         case SDLK_F11:                return K_F11;
181         case SDLK_F12:                return K_F12;
182         case SDLK_PRINTSCREEN:        return K_PRINTSCREEN;
183         case SDLK_SCROLLLOCK:         return K_SCROLLOCK;
184         case SDLK_PAUSE:              return K_PAUSE;
185         case SDLK_INSERT:             return K_INS;
186         case SDLK_HOME:               return K_HOME;
187         case SDLK_PAGEUP:             return K_PGUP;
188 #ifdef __IPHONEOS__
189         case SDLK_DELETE:             return K_BACKSPACE;
190 #else
191         case SDLK_DELETE:             return K_DEL;
192 #endif
193         case SDLK_END:                return K_END;
194         case SDLK_PAGEDOWN:           return K_PGDN;
195         case SDLK_RIGHT:              return K_RIGHTARROW;
196         case SDLK_LEFT:               return K_LEFTARROW;
197         case SDLK_DOWN:               return K_DOWNARROW;
198         case SDLK_UP:                 return K_UPARROW;
199         case SDLK_NUMLOCKCLEAR:       return K_NUMLOCK;
200         case SDLK_KP_DIVIDE:          return K_KP_DIVIDE;
201         case SDLK_KP_MULTIPLY:        return K_KP_MULTIPLY;
202         case SDLK_KP_MINUS:           return K_KP_MINUS;
203         case SDLK_KP_PLUS:            return K_KP_PLUS;
204         case SDLK_KP_ENTER:           return K_KP_ENTER;
205         case SDLK_KP_1:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_1 : K_END);
206         case SDLK_KP_2:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_2 : K_DOWNARROW);
207         case SDLK_KP_3:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_3 : K_PGDN);
208         case SDLK_KP_4:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_4 : K_LEFTARROW);
209         case SDLK_KP_5:               return K_KP_5;
210         case SDLK_KP_6:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_6 : K_RIGHTARROW);
211         case SDLK_KP_7:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_7 : K_HOME);
212         case SDLK_KP_8:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_8 : K_UPARROW);
213         case SDLK_KP_9:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_9 : K_PGUP);
214         case SDLK_KP_0:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_0 : K_INS);
215         case SDLK_KP_PERIOD:          return ((SDL_GetModState() & KMOD_NUM) ? K_KP_PERIOD : K_DEL);
216 //      case SDLK_APPLICATION:        return K_APPLICATION;
217 //      case SDLK_POWER:              return K_POWER;
218         case SDLK_KP_EQUALS:          return K_KP_EQUALS;
219 //      case SDLK_F13:                return K_F13;
220 //      case SDLK_F14:                return K_F14;
221 //      case SDLK_F15:                return K_F15;
222 //      case SDLK_F16:                return K_F16;
223 //      case SDLK_F17:                return K_F17;
224 //      case SDLK_F18:                return K_F18;
225 //      case SDLK_F19:                return K_F19;
226 //      case SDLK_F20:                return K_F20;
227 //      case SDLK_F21:                return K_F21;
228 //      case SDLK_F22:                return K_F22;
229 //      case SDLK_F23:                return K_F23;
230 //      case SDLK_F24:                return K_F24;
231 //      case SDLK_EXECUTE:            return K_EXECUTE;
232 //      case SDLK_HELP:               return K_HELP;
233 //      case SDLK_MENU:               return K_MENU;
234 //      case SDLK_SELECT:             return K_SELECT;
235 //      case SDLK_STOP:               return K_STOP;
236 //      case SDLK_AGAIN:              return K_AGAIN;
237 //      case SDLK_UNDO:               return K_UNDO;
238 //      case SDLK_CUT:                return K_CUT;
239 //      case SDLK_COPY:               return K_COPY;
240 //      case SDLK_PASTE:              return K_PASTE;
241 //      case SDLK_FIND:               return K_FIND;
242 //      case SDLK_MUTE:               return K_MUTE;
243 //      case SDLK_VOLUMEUP:           return K_VOLUMEUP;
244 //      case SDLK_VOLUMEDOWN:         return K_VOLUMEDOWN;
245 //      case SDLK_KP_COMMA:           return K_KP_COMMA;
246 //      case SDLK_KP_EQUALSAS400:     return K_KP_EQUALSAS400;
247 //      case SDLK_ALTERASE:           return K_ALTERASE;
248 //      case SDLK_SYSREQ:             return K_SYSREQ;
249 //      case SDLK_CANCEL:             return K_CANCEL;
250 //      case SDLK_CLEAR:              return K_CLEAR;
251 //      case SDLK_PRIOR:              return K_PRIOR;
252 //      case SDLK_RETURN2:            return K_RETURN2;
253 //      case SDLK_SEPARATOR:          return K_SEPARATOR;
254 //      case SDLK_OUT:                return K_OUT;
255 //      case SDLK_OPER:               return K_OPER;
256 //      case SDLK_CLEARAGAIN:         return K_CLEARAGAIN;
257 //      case SDLK_CRSEL:              return K_CRSEL;
258 //      case SDLK_EXSEL:              return K_EXSEL;
259 //      case SDLK_KP_00:              return K_KP_00;
260 //      case SDLK_KP_000:             return K_KP_000;
261 //      case SDLK_THOUSANDSSEPARATOR: return K_THOUSANDSSEPARATOR;
262 //      case SDLK_DECIMALSEPARATOR:   return K_DECIMALSEPARATOR;
263 //      case SDLK_CURRENCYUNIT:       return K_CURRENCYUNIT;
264 //      case SDLK_CURRENCYSUBUNIT:    return K_CURRENCYSUBUNIT;
265 //      case SDLK_KP_LEFTPAREN:       return K_KP_LEFTPAREN;
266 //      case SDLK_KP_RIGHTPAREN:      return K_KP_RIGHTPAREN;
267 //      case SDLK_KP_LEFTBRACE:       return K_KP_LEFTBRACE;
268 //      case SDLK_KP_RIGHTBRACE:      return K_KP_RIGHTBRACE;
269 //      case SDLK_KP_TAB:             return K_KP_TAB;
270 //      case SDLK_KP_BACKSPACE:       return K_KP_BACKSPACE;
271 //      case SDLK_KP_A:               return K_KP_A;
272 //      case SDLK_KP_B:               return K_KP_B;
273 //      case SDLK_KP_C:               return K_KP_C;
274 //      case SDLK_KP_D:               return K_KP_D;
275 //      case SDLK_KP_E:               return K_KP_E;
276 //      case SDLK_KP_F:               return K_KP_F;
277 //      case SDLK_KP_XOR:             return K_KP_XOR;
278 //      case SDLK_KP_POWER:           return K_KP_POWER;
279 //      case SDLK_KP_PERCENT:         return K_KP_PERCENT;
280 //      case SDLK_KP_LESS:            return K_KP_LESS;
281 //      case SDLK_KP_GREATER:         return K_KP_GREATER;
282 //      case SDLK_KP_AMPERSAND:       return K_KP_AMPERSAND;
283 //      case SDLK_KP_DBLAMPERSAND:    return K_KP_DBLAMPERSAND;
284 //      case SDLK_KP_VERTICALBAR:     return K_KP_VERTICALBAR;
285 //      case SDLK_KP_DBLVERTICALBAR:  return K_KP_DBLVERTICALBAR;
286 //      case SDLK_KP_COLON:           return K_KP_COLON;
287 //      case SDLK_KP_HASH:            return K_KP_HASH;
288 //      case SDLK_KP_SPACE:           return K_KP_SPACE;
289 //      case SDLK_KP_AT:              return K_KP_AT;
290 //      case SDLK_KP_EXCLAM:          return K_KP_EXCLAM;
291 //      case SDLK_KP_MEMSTORE:        return K_KP_MEMSTORE;
292 //      case SDLK_KP_MEMRECALL:       return K_KP_MEMRECALL;
293 //      case SDLK_KP_MEMCLEAR:        return K_KP_MEMCLEAR;
294 //      case SDLK_KP_MEMADD:          return K_KP_MEMADD;
295 //      case SDLK_KP_MEMSUBTRACT:     return K_KP_MEMSUBTRACT;
296 //      case SDLK_KP_MEMMULTIPLY:     return K_KP_MEMMULTIPLY;
297 //      case SDLK_KP_MEMDIVIDE:       return K_KP_MEMDIVIDE;
298 //      case SDLK_KP_PLUSMINUS:       return K_KP_PLUSMINUS;
299 //      case SDLK_KP_CLEAR:           return K_KP_CLEAR;
300 //      case SDLK_KP_CLEARENTRY:      return K_KP_CLEARENTRY;
301 //      case SDLK_KP_BINARY:          return K_KP_BINARY;
302 //      case SDLK_KP_OCTAL:           return K_KP_OCTAL;
303 //      case SDLK_KP_DECIMAL:         return K_KP_DECIMAL;
304 //      case SDLK_KP_HEXADECIMAL:     return K_KP_HEXADECIMAL;
305         case SDLK_LCTRL:              return K_CTRL;
306         case SDLK_LSHIFT:             return K_SHIFT;
307         case SDLK_LALT:               return K_ALT;
308 //      case SDLK_LGUI:               return K_LGUI;
309         case SDLK_RCTRL:              return K_CTRL;
310         case SDLK_RSHIFT:             return K_SHIFT;
311         case SDLK_RALT:               return K_ALT;
312 //      case SDLK_RGUI:               return K_RGUI;
313 //      case SDLK_MODE:               return K_MODE;
314 //      case SDLK_AUDIONEXT:          return K_AUDIONEXT;
315 //      case SDLK_AUDIOPREV:          return K_AUDIOPREV;
316 //      case SDLK_AUDIOSTOP:          return K_AUDIOSTOP;
317 //      case SDLK_AUDIOPLAY:          return K_AUDIOPLAY;
318 //      case SDLK_AUDIOMUTE:          return K_AUDIOMUTE;
319 //      case SDLK_MEDIASELECT:        return K_MEDIASELECT;
320 //      case SDLK_WWW:                return K_WWW;
321 //      case SDLK_MAIL:               return K_MAIL;
322 //      case SDLK_CALCULATOR:         return K_CALCULATOR;
323 //      case SDLK_COMPUTER:           return K_COMPUTER;
324 //      case SDLK_AC_SEARCH:          return K_AC_SEARCH; // Android button
325 //      case SDLK_AC_HOME:            return K_AC_HOME; // Android button
326         case SDLK_AC_BACK:            return K_ESCAPE; // Android button
327 //      case SDLK_AC_FORWARD:         return K_AC_FORWARD; // Android button
328 //      case SDLK_AC_STOP:            return K_AC_STOP; // Android button
329 //      case SDLK_AC_REFRESH:         return K_AC_REFRESH; // Android button
330 //      case SDLK_AC_BOOKMARKS:       return K_AC_BOOKMARKS; // Android button
331 //      case SDLK_BRIGHTNESSDOWN:     return K_BRIGHTNESSDOWN;
332 //      case SDLK_BRIGHTNESSUP:       return K_BRIGHTNESSUP;
333 //      case SDLK_DISPLAYSWITCH:      return K_DISPLAYSWITCH;
334 //      case SDLK_KBDILLUMTOGGLE:     return K_KBDILLUMTOGGLE;
335 //      case SDLK_KBDILLUMDOWN:       return K_KBDILLUMDOWN;
336 //      case SDLK_KBDILLUMUP:         return K_KBDILLUMUP;
337 //      case SDLK_EJECT:              return K_EJECT;
338 //      case SDLK_SLEEP:              return K_SLEEP;
339         }
340 }
341
342 qboolean VID_HasScreenKeyboardSupport(void)
343 {
344         return SDL_HasScreenKeyboardSupport() != SDL_FALSE;
345 }
346
347 void VID_ShowKeyboard(qboolean show)
348 {
349         if (!SDL_HasScreenKeyboardSupport())
350                 return;
351
352         if (show)
353         {
354                 if (!SDL_IsTextInputActive())
355                         SDL_StartTextInput();
356         }
357         else
358         {
359                 if (SDL_IsTextInputActive())
360                         SDL_StopTextInput();
361         }
362 }
363
364 qboolean VID_ShowingKeyboard(void)
365 {
366         return SDL_IsTextInputActive() != 0;
367 }
368
369 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
370 {
371 #ifndef DP_MOBILETOUCH
372 #ifdef MACOSX
373         if(relative)
374                 if(vid_usingmouse && (vid_usingnoaccel != !!apple_mouse_noaccel.integer))
375                         VID_SetMouse(false, false, false); // ungrab first!
376 #endif
377         if (vid_usingmouse != relative)
378         {
379                 vid_usingmouse = relative;
380                 cl_ignoremousemoves = 2;
381                 vid_usingmouse_relativeworks = SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE) == 0;
382 //              Con_Printf("VID_SetMouse(%i, %i, %i) relativeworks = %i\n", (int)fullscreengrab, (int)relative, (int)hidecursor, (int)vid_usingmouse_relativeworks);
383 #ifdef MACOSX
384                 if(relative)
385                 {
386                         // Save the status of mouse acceleration
387                         originalMouseSpeed = -1.0; // in case of error
388                         if(apple_mouse_noaccel.integer)
389                         {
390                                 io_connect_t mouseDev = IN_GetIOHandle();
391                                 if(mouseDev != 0)
392                                 {
393                                         if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
394                                         {
395                                                 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
396                                                 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
397                                                 {
398                                                         Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
399                                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
400                                                 }
401                                         }
402                                         else
403                                         {
404                                                 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
405                                                 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
406                                         }
407                                         IOServiceClose(mouseDev);
408                                 }
409                                 else
410                                 {
411                                         Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
412                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
413                                 }
414                         }
415
416                         vid_usingnoaccel = !!apple_mouse_noaccel.integer;
417                 }
418                 else
419                 {
420                         if(originalMouseSpeed != -1.0)
421                         {
422                                 io_connect_t mouseDev = IN_GetIOHandle();
423                                 if(mouseDev != 0)
424                                 {
425                                         Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
426                                         if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
427                                                 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
428                                         IOServiceClose(mouseDev);
429                                 }
430                                 else
431                                         Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
432                         }
433                 }
434 #endif
435         }
436         if (vid_usinghidecursor != hidecursor)
437         {
438                 vid_usinghidecursor = hidecursor;
439                 SDL_ShowCursor( hidecursor ? SDL_DISABLE : SDL_ENABLE);
440         }
441 #endif
442 }
443
444 // multitouch[10][] represents the mouse pointer
445 // multitouch[][0]: finger active
446 // multitouch[][1]: Y
447 // multitouch[][2]: Y
448 // X and Y coordinates are 0-1.
449 #define MAXFINGERS 11
450 float multitouch[MAXFINGERS][3];
451
452 // this one stores how many areas this finger has touched
453 int multitouchs[MAXFINGERS];
454
455 // modified heavily by ELUAN
456 static qboolean VID_TouchscreenArea(int corner, float px, float py, float pwidth, float pheight, const char *icon, float textheight, const char *text, float *resultmove, qboolean *resultbutton, keynum_t key, const char *typedtext, float deadzone, float oversizepixels_x, float oversizepixels_y, qboolean iamexclusive)
457 {
458         int finger;
459         float fx, fy, fwidth, fheight;
460         float overfx, overfy, overfwidth, overfheight;
461         float rel[3];
462         float sqsum;
463         qboolean button = false;
464         VectorClear(rel);
465         if (pwidth > 0 && pheight > 0)
466         {
467                 if (corner & 1) px += vid_conwidth.value;
468                 if (corner & 2) py += vid_conheight.value;
469                 if (corner & 4) px += vid_conwidth.value * 0.5f;
470                 if (corner & 8) py += vid_conheight.value * 0.5f;
471                 if (corner & 16) {px *= vid_conwidth.value * (1.0f / 640.0f);py *= vid_conheight.value * (1.0f / 480.0f);pwidth *= vid_conwidth.value * (1.0f / 640.0f);pheight *= vid_conheight.value * (1.0f / 480.0f);}
472                 fx = px / vid_conwidth.value;
473                 fy = py / vid_conheight.value;
474                 fwidth = pwidth / vid_conwidth.value;
475                 fheight = pheight / vid_conheight.value;
476
477                 // try to prevent oversizepixels_* from interfering with the iamexclusive cvar by not letting we start controlling from too far of the actual touch area (areas without resultbuttons should NEVER have the oversizepixels_* parameters set to anything other than 0)
478                 if (resultbutton)
479                         if (!(*resultbutton))
480                         {
481                                 oversizepixels_x *= 0.2;
482                                 oversizepixels_y *= 0.2;
483                         }
484
485                 oversizepixels_x /= vid_conwidth.value;
486                 oversizepixels_y /= vid_conheight.value;
487
488                 overfx = fx - oversizepixels_x;
489                 overfy = fy - oversizepixels_y;
490                 overfwidth = fwidth + 2*oversizepixels_x;
491                 overfheight = fheight + 2*oversizepixels_y;
492
493                 for (finger = 0;finger < MAXFINGERS;finger++)
494                 {
495                         if (multitouchs[finger] && iamexclusive) // for this to work correctly, you must call touch areas in order of highest to lowest priority
496                                 continue;
497
498                         if (multitouch[finger][0] && multitouch[finger][1] >= overfx && multitouch[finger][2] >= overfy && multitouch[finger][1] < overfx + overfwidth && multitouch[finger][2] < overfy + overfheight)
499                         {
500                                 multitouchs[finger]++;
501
502                                 rel[0] = bound(-1, (multitouch[finger][1] - (fx + 0.5f * fwidth)) * (2.0f / fwidth), 1);
503                                 rel[1] = bound(-1, (multitouch[finger][2] - (fy + 0.5f * fheight)) * (2.0f / fheight), 1);
504                                 rel[2] = 0;
505
506                                 sqsum = rel[0]*rel[0] + rel[1]*rel[1];
507                                 // 2d deadzone
508                                 if (sqsum < deadzone*deadzone)
509                                 {
510                                         rel[0] = 0;
511                                         rel[1] = 0;
512                                 }
513                                 else if (sqsum > 1)
514                                 {
515                                         // ignore the third component
516                                         Vector2Normalize2(rel, rel);
517                                 }
518                                 button = true;
519                                 break;
520                         }
521                 }
522                 if (scr_numtouchscreenareas < 128)
523                 {
524                         scr_touchscreenareas[scr_numtouchscreenareas].pic = icon;
525                         scr_touchscreenareas[scr_numtouchscreenareas].text = text;
526                         scr_touchscreenareas[scr_numtouchscreenareas].textheight = textheight;
527                         scr_touchscreenareas[scr_numtouchscreenareas].rect[0] = px;
528                         scr_touchscreenareas[scr_numtouchscreenareas].rect[1] = py;
529                         scr_touchscreenareas[scr_numtouchscreenareas].rect[2] = pwidth;
530                         scr_touchscreenareas[scr_numtouchscreenareas].rect[3] = pheight;
531                         scr_touchscreenareas[scr_numtouchscreenareas].active = button;
532                         // the pics may have alpha too.
533                         scr_touchscreenareas[scr_numtouchscreenareas].activealpha = 1.f;
534                         scr_touchscreenareas[scr_numtouchscreenareas].inactivealpha = 0.95f;
535                         scr_numtouchscreenareas++;
536                 }
537         }
538         if (resultmove)
539         {
540                 if (button)
541                         VectorCopy(rel, resultmove);
542                 else
543                         VectorClear(resultmove);
544         }
545         if (resultbutton)
546         {
547                 if (*resultbutton != button)
548                 {
549                         if ((int)key > 0)
550                                 Key_Event(key, 0, button);
551                         if (typedtext && typedtext[0] && !*resultbutton)
552                         {
553                                 // FIXME: implement UTF8 support - nothing actually specifies a UTF8 string here yet, but should support it...
554                                 int i;
555                                 for (i = 0;typedtext[i];i++)
556                                 {
557                                         Key_Event(K_TEXT, typedtext[i], true);
558                                         Key_Event(K_TEXT, typedtext[i], false);
559                                 }
560                         }
561                 }
562                 *resultbutton = button;
563         }
564         return button;
565 }
566
567 // ELUAN:
568 // not reentrant, but we only need one mouse cursor anyway...
569 static void VID_TouchscreenCursor(float px, float py, float pwidth, float pheight, qboolean *resultbutton, keynum_t key)
570 {
571         int finger;
572         float fx, fy, fwidth, fheight;
573         qboolean button = false;
574         static int cursorfinger = -1;
575         static int cursorfreemovement = false;
576         static int canclick = false;
577         static int clickxy[2];
578         static int relclickxy[2];
579         static double clickrealtime = 0;
580
581         if (steelstorm_showing_mousecursor && steelstorm_showing_mousecursor->integer)
582         if (pwidth > 0 && pheight > 0)
583         {
584                 fx = px / vid_conwidth.value;
585                 fy = py / vid_conheight.value;
586                 fwidth = pwidth / vid_conwidth.value;
587                 fheight = pheight / vid_conheight.value;
588                 for (finger = 0;finger < MAXFINGERS;finger++)
589                 {
590                         if (multitouch[finger][0] && multitouch[finger][1] >= fx && multitouch[finger][2] >= fy && multitouch[finger][1] < fx + fwidth && multitouch[finger][2] < fy + fheight)
591                         {
592                                 if (cursorfinger == -1)
593                                 {
594                                         clickxy[0] =  multitouch[finger][1] * vid_width.value - 0.5f * pwidth;
595                                         clickxy[1] =  multitouch[finger][2] * vid_height.value - 0.5f * pheight;
596                                         relclickxy[0] =  (multitouch[finger][1] - fx) * vid_width.value - 0.5f * pwidth;
597                                         relclickxy[1] =  (multitouch[finger][2] - fy) * vid_height.value - 0.5f * pheight;
598                                 }
599                                 cursorfinger = finger;
600                                 button = true;
601                                 canclick = true;
602                                 cursorfreemovement = false;
603                                 break;
604                         }
605                 }
606                 if (scr_numtouchscreenareas < 128)
607                 {
608                         if (clickrealtime + 1 > realtime)
609                         {
610                                 scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_click.tga";
611                         }
612                         else if (button)
613                         {
614                                 scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_touch.tga";
615                         }
616                         else
617                         {
618                                 switch ((int)realtime * 10 % 20)
619                                 {
620                                 case 0:
621                                         scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_touch.tga";
622                                         break;
623                                 default:
624                                         scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_idle.tga";
625                                 }
626                         }
627                         scr_touchscreenareas[scr_numtouchscreenareas].text = "";
628                         scr_touchscreenareas[scr_numtouchscreenareas].textheight = 0;
629                         scr_touchscreenareas[scr_numtouchscreenareas].rect[0] = px;
630                         scr_touchscreenareas[scr_numtouchscreenareas].rect[1] = py;
631                         scr_touchscreenareas[scr_numtouchscreenareas].rect[2] = pwidth;
632                         scr_touchscreenareas[scr_numtouchscreenareas].rect[3] = pheight;
633                         scr_touchscreenareas[scr_numtouchscreenareas].active = button;
634                         scr_touchscreenareas[scr_numtouchscreenareas].activealpha = 1.0f;
635                         scr_touchscreenareas[scr_numtouchscreenareas].inactivealpha = 1.0f;
636                         scr_numtouchscreenareas++;
637                 }
638         }
639
640         if (cursorfinger != -1)
641         {
642                 if (multitouch[cursorfinger][0])
643                 {
644                         if (multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth < clickxy[0] - 1 ||
645                                 multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth > clickxy[0] + 1 ||
646                                 multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight< clickxy[1] - 1 ||
647                                 multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight> clickxy[1] + 1) // finger drifted more than the allowed amount
648                         {
649                                 cursorfreemovement = true;
650                         }
651                         if (cursorfreemovement)
652                         {
653                                 // in_windowmouse_x* is in screen resolution coordinates, not console resolution
654                                 in_windowmouse_x = multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth - relclickxy[0];
655                                 in_windowmouse_y = multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight - relclickxy[1];
656                         }
657                 }
658                 else
659                 {
660                         cursorfinger = -1;
661                 }
662         }
663
664         if (resultbutton)
665         {
666                 if (/**resultbutton != button && */(int)key > 0)
667                 {
668                         if (!button && !cursorfreemovement && canclick)
669                         {
670                                 Key_Event(key, 0, true);
671                                 canclick = false;
672                                 clickrealtime = realtime;
673                         }
674
675                         // SS:BR can't qc can't cope with presses and releases on the same frame
676                         if (clickrealtime && clickrealtime + 0.1 < realtime)
677                         {
678                                 Key_Event(key, 0, false);
679                                 clickrealtime = 0;
680                         }
681                 }
682
683                 *resultbutton = button;
684         }
685 }
686
687 void VID_BuildJoyState(vid_joystate_t *joystate)
688 {
689         VID_Shared_BuildJoyState_Begin(joystate);
690
691         if (vid_sdljoystick)
692         {
693                 SDL_Joystick *joy = vid_sdljoystick;
694                 int j;
695                 int numaxes;
696                 int numbuttons;
697                 numaxes = SDL_JoystickNumAxes(joy);
698                 for (j = 0;j < numaxes;j++)
699                         joystate->axis[j] = SDL_JoystickGetAxis(joy, j) * (1.0f / 32767.0f);
700                 numbuttons = SDL_JoystickNumButtons(joy);
701                 for (j = 0;j < numbuttons;j++)
702                         joystate->button[j] = SDL_JoystickGetButton(joy, j);
703         }
704
705         VID_Shared_BuildJoyState_Finish(joystate);
706 }
707
708 // clear every touch screen area, except the one with button[skip]
709 #define Vid_ClearAllTouchscreenAreas(skip) \
710         if (skip != 0) \
711                 VID_TouchscreenCursor(0, 0, 0, 0, &buttons[0], K_MOUSE1); \
712         if (skip != 1) \
713                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[1], K_MOUSE4, NULL, 0, 0, 0, false); \
714         if (skip != 2) \
715                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[2], K_MOUSE5, NULL, 0, 0, 0, false); \
716         if (skip != 3) \
717                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SHIFT, NULL, 0, 0, 0, false); \
718         if (skip != 4) \
719                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, false); \
720         if (skip != 9) \
721                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[9], K_MOUSE3, NULL, 0, 0, 0, false); \
722         if (skip != 10) \
723                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, false); \
724         if (skip != 11) \
725                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[11], (keynum_t)'b', NULL, 0, 0, 0, false); \
726         if (skip != 12) \
727                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[12], (keynum_t)'q', NULL, 0, 0, 0, false); \
728         if (skip != 13) \
729                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, false); \
730         if (skip != 14) \
731                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false); \
732         if (skip != 15) \
733                 VID_TouchscreenArea( 0,  0,  0,  0,  0, NULL                         , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, false); \
734
735 /////////////////////
736 // Movement handling
737 ////
738
739 static void IN_Move_TouchScreen_SteelStorm(void)
740 {
741         // ELUAN
742         int i, numfingers;
743         float xscale, yscale;
744         float move[3], aim[3];
745         static qboolean oldbuttons[128];
746         static qboolean buttons[128];
747         keydest_t keydest = (key_consoleactive & KEY_CONSOLEACTIVE_USER) ? key_console : key_dest;
748         memcpy(oldbuttons, buttons, sizeof(oldbuttons));
749         memset(multitouchs, 0, sizeof(multitouchs));
750
751         for (i = 0, numfingers = 0; i < MAXFINGERS - 1; i++)
752                 if (multitouch[i][0])
753                         numfingers++;
754
755         /*
756         Enable this to use a mouse as a touch device (it may conflict with the iamexclusive parameter if a finger is also reported as a mouse at the same location
757         if (numfingers == 1)
758         {
759                 multitouch[MAXFINGERS-1][0] = SDL_GetMouseState(&x, &y) ? 11 : 0;
760                 multitouch[MAXFINGERS-1][1] = (float)x / vid.width;
761                 multitouch[MAXFINGERS-1][2] = (float)y / vid.height;
762         }
763         else
764         {
765                 // disable it so it doesn't get stuck, because SDL seems to stop updating it if there are more than 1 finger on screen
766                 multitouch[MAXFINGERS-1][0] = 0;
767         }*/
768
769         // TODO: make touchscreen areas controlled by a config file or the VMs. THIS IS A MESS!
770         // TODO: can't just clear buttons[] when entering a new keydest, some keys would remain pressed
771         // SS:BR menuqc has many peculiarities, including that it can't accept more than one command per frame and pressing and releasing on the same frame
772
773         // Tuned for the SGS3, use it's value as a base. CLEAN THIS.
774         xscale = vid_touchscreen_density.value / 2.0f;
775         yscale = vid_touchscreen_density.value / 2.0f;
776         switch(keydest)
777         {
778         case key_console:
779                 Vid_ClearAllTouchscreenAreas(14);
780                 VID_TouchscreenArea( 0,   0, 160,  64,  64, "gfx/gui/touch_menu_button.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false);
781                 break;
782         case key_game:
783                 if (steelstorm_showing_map && steelstorm_showing_map->integer) // FIXME: another hack to be removed when touchscreen areas go to QC
784                 {
785                         VID_TouchscreenArea( 0,   0,   0, vid_conwidth.value, vid_conheight.value, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, false);
786                         Vid_ClearAllTouchscreenAreas(10);
787                 }
788                 else if (steelstorm_showing_mousecursor && steelstorm_showing_mousecursor->integer)
789                 {
790                         // in_windowmouse_x* is in screen resolution coordinates, not console resolution
791                         VID_TouchscreenCursor((float)in_windowmouse_x/vid_width.value*vid_conwidth.value, (float)in_windowmouse_y/vid_height.value*vid_conheight.value, 192*xscale, 192*yscale, &buttons[0], K_MOUSE1);
792                         Vid_ClearAllTouchscreenAreas(0);
793                 }
794                 else
795                 {
796                         VID_TouchscreenCursor(0, 0, 0, 0, &buttons[0], K_MOUSE1);
797
798                         VID_TouchscreenArea( 2,16*xscale,-240*yscale, 224*xscale, 224*yscale, "gfx/gui/touch_l_thumb_dpad.tga", 0.0f, NULL, move, &buttons[1], (keynum_t)0, NULL, 0.15, 112*xscale, 112*yscale, false);
799
800                         VID_TouchscreenArea( 3,-240*xscale,-160*yscale, 224*xscale, 128*yscale, "gfx/gui/touch_r_thumb_turn_n_shoot.tga"    , 0.0f, NULL, NULL,  0, (keynum_t)0, NULL, 0, 56*xscale, 0, false);
801                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale, 224*yscale, NULL    , 0.0f, NULL, aim,  &buttons[2], (keynum_t)0, NULL, 0.2, 56*xscale, 0, false);
802
803                         VID_TouchscreenArea( 2, (vid_conwidth.value / 2) - 128,-80,  256,  80, NULL, 0.0f, NULL, NULL, &buttons[3], K_SHIFT, NULL, 0, 0, 0, true);
804
805                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale,  64*yscale, "gfx/gui/touch_secondary_slide.tga", 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 56*xscale, 0, false);
806                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale,  160*yscale, NULL , 0.0f, NULL, NULL, &buttons[9], K_MOUSE3, NULL, 0.2, 56*xscale, 0, false);
807
808                         VID_TouchscreenArea( 1,-100,   0, 100, 100, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, true);
809                         VID_TouchscreenArea( 1,-100, 120, 100, 100, NULL                         , 0.0f, NULL, NULL, &buttons[11], (keynum_t)'b', NULL, 0, 0, 0, true);
810                         VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[12], (keynum_t)'q', NULL, 0, 0, 0, true);
811                         if (developer.integer)
812                                 VID_TouchscreenArea( 0,   0,  96,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
813                         else
814                                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, false);
815                         VID_TouchscreenArea( 0,   0, 160,  64,  64, "gfx/gui/touch_menu_button.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
816                         switch(cl.activeweapon)
817                         {
818                         case 14:
819                                 VID_TouchscreenArea( 2,  16*xscale,-320*yscale, 224*xscale, 64*yscale, "gfx/gui/touch_booster.tga" , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, true);
820                                 break;
821                         case 12:
822                                 VID_TouchscreenArea( 2,  16*xscale,-320*yscale, 224*xscale, 64*yscale, "gfx/gui/touch_shockwave.tga" , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, true);
823                                 break;
824                         default:
825                                 VID_TouchscreenArea( 0,  0,  0,  0,  0, NULL , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, false);
826                         }
827                 }
828                 break;
829         default:
830                 if (!steelstorm_showing_mousecursor || !steelstorm_showing_mousecursor->integer)
831                 {
832                         Vid_ClearAllTouchscreenAreas(14);
833                         // this way we can skip cutscenes
834                         VID_TouchscreenArea( 0,   0,   0, vid_conwidth.value, vid_conheight.value, NULL                         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false);
835                 }
836                 else
837                 {
838                         // in_windowmouse_x* is in screen resolution coordinates, not console resolution
839                         VID_TouchscreenCursor((float)in_windowmouse_x/vid_width.value*vid_conwidth.value, (float)in_windowmouse_y/vid_height.value*vid_conheight.value, 192*xscale, 192*yscale, &buttons[0], K_MOUSE1);
840                         Vid_ClearAllTouchscreenAreas(0);
841                 }
842                 break;
843         }
844
845         if (VID_ShowingKeyboard() && (float)in_windowmouse_y > vid_height.value / 2 - 10)
846                 in_windowmouse_y = 128;
847
848         cl.cmd.forwardmove -= move[1] * cl_forwardspeed.value;
849         cl.cmd.sidemove += move[0] * cl_sidespeed.value;
850         cl.viewangles[0] += aim[1] * cl_pitchspeed.value * cl.realframetime;
851         cl.viewangles[1] -= aim[0] * cl_yawspeed.value * cl.realframetime;
852 }
853
854 static void IN_Move_TouchScreen_Quake(void)
855 {
856         int x, y;
857         float move[3], aim[3], click[3];
858         static qboolean oldbuttons[128];
859         static qboolean buttons[128];
860         keydest_t keydest = (key_consoleactive & KEY_CONSOLEACTIVE_USER) ? key_console : key_dest;
861         memcpy(oldbuttons, buttons, sizeof(oldbuttons));
862         memset(multitouchs, 0, sizeof(multitouchs));
863
864         // simple quake controls
865         multitouch[MAXFINGERS-1][0] = SDL_GetMouseState(&x, &y);
866         multitouch[MAXFINGERS-1][1] = x * 32768 / vid.width;
867         multitouch[MAXFINGERS-1][2] = y * 32768 / vid.height;
868
869         // top of screen is toggleconsole and K_ESCAPE
870         switch(keydest)
871         {
872         case key_console:
873                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
874                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
875                 if (!VID_ShowingKeyboard())
876                 {
877                         // user entered a command, close the console now
878                         Con_ToggleConsole_f();
879                 }
880                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[15], (keynum_t)0, NULL, 0, 0, 0, true);
881                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
882                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
883                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, click,&buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
884                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
885                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
886                 break;
887         case key_game:
888                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
889                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
890                 VID_TouchscreenArea( 2,   0,-128, 128, 128, "gfx/touch_movebutton.tga"   , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
891                 VID_TouchscreenArea( 3,-128,-128, 128, 128, "gfx/touch_aimbutton.tga"    , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
892                 VID_TouchscreenArea( 2,   0,-160,  64,  32, "gfx/touch_jumpbutton.tga"   , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
893                 VID_TouchscreenArea( 3,-128,-160,  64,  32, "gfx/touch_attackbutton.tga" , 0.0f, NULL, NULL, &buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
894                 VID_TouchscreenArea( 3, -64,-160,  64,  32, "gfx/touch_attack2button.tga", 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
895                 buttons[15] = false;
896                 break;
897         default:
898                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
899                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
900                 // in menus, an icon in the corner activates keyboard
901                 VID_TouchscreenArea( 2,   0, -32,  32,  32, "gfx/touch_keyboard.tga"     , 0.0f, NULL, NULL, &buttons[15], (keynum_t)0, NULL, 0, 0, 0, true);
902                 if (buttons[15])
903                         VID_ShowKeyboard(true);
904                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
905                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
906                 VID_TouchscreenArea(16, -320,-480,640, 960, NULL                         , 0.0f, NULL, click,&buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
907                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
908                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
909                 if (buttons[2])
910                 {
911                         in_windowmouse_x = x;
912                         in_windowmouse_y = y;
913                 }
914                 break;
915         }
916
917         cl.cmd.forwardmove -= move[1] * cl_forwardspeed.value;
918         cl.cmd.sidemove += move[0] * cl_sidespeed.value;
919         cl.viewangles[0] += aim[1] * cl_pitchspeed.value * cl.realframetime;
920         cl.viewangles[1] -= aim[0] * cl_yawspeed.value * cl.realframetime;
921 }
922
923 void IN_Move( void )
924 {
925         static int old_x = 0, old_y = 0;
926         static int stuck = 0;
927         static keydest_t oldkeydest;
928         static qboolean oldshowkeyboard;
929         int x, y;
930         vid_joystate_t joystate;
931         keydest_t keydest = (key_consoleactive & KEY_CONSOLEACTIVE_USER) ? key_console : key_dest;
932
933         scr_numtouchscreenareas = 0;
934
935         // Only apply the new keyboard state if the input changes.
936         if (keydest != oldkeydest || !!vid_touchscreen_showkeyboard.integer != oldshowkeyboard)
937         {
938                 switch(keydest)
939                 {
940                         case key_console: VID_ShowKeyboard(true);break;
941                         case key_message: VID_ShowKeyboard(true);break;
942                         default: VID_ShowKeyboard(!!vid_touchscreen_showkeyboard.integer); break;
943                 }
944         }
945         oldkeydest = keydest;
946         oldshowkeyboard = !!vid_touchscreen_showkeyboard.integer;
947
948         if (vid_touchscreen.integer)
949         {
950                 switch(gamemode)
951                 {
952                 case GAME_STEELSTORM:
953                         IN_Move_TouchScreen_SteelStorm();
954                         break;
955                 default:
956                         IN_Move_TouchScreen_Quake();
957                         break;
958                 }
959         }
960         else
961         {
962                 if (vid_usingmouse)
963                 {
964                         if (vid_stick_mouse.integer || !vid_usingmouse_relativeworks)
965                         {
966                                 // have the mouse stuck in the middle, example use: prevent expose effect of beryl during the game when not using
967                                 // window grabbing. --blub
968         
969                                 // we need 2 frames to initialize the center position
970                                 if(!stuck)
971                                 {
972                                         SDL_WarpMouseInWindow(window, win_half_width, win_half_height);
973                                         SDL_GetMouseState(&x, &y);
974                                         SDL_GetRelativeMouseState(&x, &y);
975                                         ++stuck;
976                                 } else {
977                                         SDL_GetRelativeMouseState(&x, &y);
978                                         in_mouse_x = x + old_x;
979                                         in_mouse_y = y + old_y;
980                                         SDL_GetMouseState(&x, &y);
981                                         old_x = x - win_half_width;
982                                         old_y = y - win_half_height;
983                                         SDL_WarpMouseInWindow(window, win_half_width, win_half_height);
984                                 }
985                         } else {
986                                 SDL_GetRelativeMouseState( &x, &y );
987                                 in_mouse_x = x;
988                                 in_mouse_y = y;
989                         }
990                 }
991
992                 SDL_GetMouseState(&x, &y);
993                 in_windowmouse_x = x;
994                 in_windowmouse_y = y;
995         }
996
997         VID_BuildJoyState(&joystate);
998         VID_ApplyJoyState(&joystate);
999 }
1000
1001 /////////////////////
1002 // Message Handling
1003 ////
1004
1005 #ifdef SDL_R_RESTART
1006 static qboolean sdl_needs_restart;
1007 static void sdl_start(void)
1008 {
1009 }
1010 static void sdl_shutdown(void)
1011 {
1012         sdl_needs_restart = false;
1013 }
1014 static void sdl_newmap(void)
1015 {
1016 }
1017 #endif
1018
1019 static keynum_t buttonremap[] =
1020 {
1021         K_MOUSE1,
1022         K_MOUSE3,
1023         K_MOUSE2,
1024         K_MOUSE4,
1025         K_MOUSE5,
1026         K_MOUSE6,
1027         K_MOUSE7,
1028         K_MOUSE8,
1029         K_MOUSE9,
1030         K_MOUSE10,
1031         K_MOUSE11,
1032         K_MOUSE12,
1033         K_MOUSE13,
1034         K_MOUSE14,
1035         K_MOUSE15,
1036         K_MOUSE16,
1037 };
1038
1039 //#define DEBUGSDLEVENTS
1040
1041 // SDL2
1042 void Sys_SendKeyEvents( void )
1043 {
1044         static qboolean sound_active = true;
1045         int keycode;
1046         int i;
1047         qboolean isdown;
1048         Uchar unicode;
1049         SDL_Event event;
1050
1051         VID_EnableJoystick(true);
1052
1053         while( SDL_PollEvent( &event ) )
1054                 loop_start:
1055                 switch( event.type ) {
1056                         case SDL_QUIT:
1057 #ifdef DEBUGSDLEVENTS
1058                                 Con_DPrintf("SDL_Event: SDL_QUIT\n");
1059 #endif
1060                                 Sys_Quit(0);
1061                                 break;
1062                         case SDL_KEYDOWN:
1063                         case SDL_KEYUP:
1064 #ifdef DEBUGSDLEVENTS
1065                                 if (event.type == SDL_KEYDOWN)
1066                                         Con_DPrintf("SDL_Event: SDL_KEYDOWN %i\n", event.key.keysym.sym);
1067                                 else
1068                                         Con_DPrintf("SDL_Event: SDL_KEYUP %i\n", event.key.keysym.sym);
1069 #endif
1070                                 keycode = MapKey(event.key.keysym.sym);
1071                                 isdown = (event.key.state == SDL_PRESSED);
1072                                 unicode = 0;
1073                                 if(isdown)
1074                                 {
1075                                         if(SDL_PollEvent(&event))
1076                                         {
1077                                                 if(event.type == SDL_TEXTINPUT)
1078                                                 {
1079                                                         // combine key code from SDL_KEYDOWN event and character
1080                                                         // from SDL_TEXTINPUT event in a single Key_Event call
1081 #ifdef DEBUGSDLEVENTS
1082                                                         Con_DPrintf("SDL_Event: SDL_TEXTINPUT - text: %s\n", event.text.text);
1083 #endif
1084                                                         unicode = u8_getchar_utf8_enabled(event.text.text + (int)u8_bytelen(event.text.text, 0), NULL);
1085                                                 }
1086                                                 else
1087                                                 {
1088                                                         if (!VID_JoyBlockEmulatedKeys(keycode))
1089                                                                 Key_Event(keycode, 0, isdown);
1090                                                         goto loop_start;
1091                                                 }
1092                                         }
1093                                 }
1094                                 if (!VID_JoyBlockEmulatedKeys(keycode))
1095                                         Key_Event(keycode, unicode, isdown);
1096                                 break;
1097                         case SDL_MOUSEBUTTONDOWN:
1098                         case SDL_MOUSEBUTTONUP:
1099 #ifdef DEBUGSDLEVENTS
1100                                 if (event.type == SDL_MOUSEBUTTONDOWN)
1101                                         Con_DPrintf("SDL_Event: SDL_MOUSEBUTTONDOWN\n");
1102                                 else
1103                                         Con_DPrintf("SDL_Event: SDL_MOUSEBUTTONUP\n");
1104 #endif
1105                                 if (!vid_touchscreen.integer)
1106                                 if (event.button.button > 0 && event.button.button <= ARRAY_SIZE(buttonremap))
1107                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
1108                                 break;
1109                         case SDL_MOUSEWHEEL:
1110                                 // TODO support wheel x direction.
1111                                 i = event.wheel.y;
1112                                 while (i > 0) {
1113                                         --i;
1114                                         Key_Event( K_MWHEELUP, 0, true );
1115                                         Key_Event( K_MWHEELUP, 0, false );
1116                                 }
1117                                 while (i < 0) {
1118                                         ++i;
1119                                         Key_Event( K_MWHEELDOWN, 0, true );
1120                                         Key_Event( K_MWHEELDOWN, 0, false );
1121                                 }
1122                                 break;
1123                         case SDL_JOYBUTTONDOWN:
1124                         case SDL_JOYBUTTONUP:
1125                         case SDL_JOYAXISMOTION:
1126                         case SDL_JOYBALLMOTION:
1127                         case SDL_JOYHATMOTION:
1128 #ifdef DEBUGSDLEVENTS
1129                                 Con_DPrintf("SDL_Event: SDL_JOY*\n");
1130 #endif
1131                                 break;
1132                         case SDL_WINDOWEVENT:
1133 #ifdef DEBUGSDLEVENTS
1134                                 Con_DPrintf("SDL_Event: SDL_WINDOWEVENT %i\n", (int)event.window.event);
1135 #endif
1136                                 //if (event.window.windowID == window) // how to compare?
1137                                 {
1138                                         switch(event.window.event)
1139                                         {
1140                                         case SDL_WINDOWEVENT_SHOWN:
1141                                                 vid_hidden = false;
1142                                                 break;
1143                                         case  SDL_WINDOWEVENT_HIDDEN:
1144                                                 vid_hidden = true;
1145                                                 break;
1146                                         case SDL_WINDOWEVENT_EXPOSED:
1147 #ifdef DEBUGSDLEVENTS
1148                                                 Con_DPrintf("SDL_Event: SDL_WINDOWEVENT_EXPOSED\n");
1149 #endif
1150                                                 break;
1151                                         case SDL_WINDOWEVENT_MOVED:
1152                                                 break;
1153                                         case SDL_WINDOWEVENT_RESIZED:
1154                                                 if(vid_resizable.integer < 2)
1155                                                 {
1156                                                         vid.width = event.window.data1;
1157                                                         vid.height = event.window.data2;
1158 #ifdef SDL_R_RESTART
1159                                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
1160                                                         // so, let's better queue it for next frame
1161                                                         if(!sdl_needs_restart)
1162                                                         {
1163                                                                 Cbuf_AddText("\nr_restart\n");
1164                                                                 sdl_needs_restart = true;
1165                                                         }
1166 #endif
1167                                                 }
1168                                                 break;
1169                                         case SDL_WINDOWEVENT_MINIMIZED:
1170                                                 break;
1171                                         case SDL_WINDOWEVENT_MAXIMIZED:
1172                                                 break;
1173                                         case SDL_WINDOWEVENT_RESTORED:
1174                                                 break;
1175                                         case SDL_WINDOWEVENT_ENTER:
1176                                                 break;
1177                                         case SDL_WINDOWEVENT_LEAVE:
1178                                                 break;
1179                                         case SDL_WINDOWEVENT_FOCUS_GAINED:
1180                                                 vid_hasfocus = true;
1181                                                 break;
1182                                         case SDL_WINDOWEVENT_FOCUS_LOST:
1183                                                 vid_hasfocus = false;
1184                                                 break;
1185                                         case SDL_WINDOWEVENT_CLOSE:
1186                                                 Sys_Quit(0);
1187                                                 break;
1188                                         }
1189                                 }
1190                                 break;
1191                         case SDL_TEXTEDITING:
1192 #ifdef DEBUGSDLEVENTS
1193                                 Con_DPrintf("SDL_Event: SDL_TEXTEDITING - composition = %s, cursor = %d, selection lenght = %d\n", event.edit.text, event.edit.start, event.edit.length);
1194 #endif
1195                                 // FIXME!  this is where composition gets supported
1196                                 break;
1197                         case SDL_TEXTINPUT:
1198 #ifdef DEBUGSDLEVENTS
1199                                 Con_DPrintf("SDL_Event: SDL_TEXTINPUT - text: %s\n", event.text.text);
1200 #endif
1201                                 // convert utf8 string to char
1202                                 // NOTE: this code is supposed to run even if utf8enable is 0
1203                                 unicode = u8_getchar_utf8_enabled(event.text.text + (int)u8_bytelen(event.text.text, 0), NULL);
1204                                 Key_Event(K_TEXT, unicode, true);
1205                                 Key_Event(K_TEXT, unicode, false);
1206                                 break;
1207                         case SDL_MOUSEMOTION:
1208                                 break;
1209                         case SDL_FINGERDOWN:
1210 #ifdef DEBUGSDLEVENTS
1211                                 Con_DPrintf("SDL_FINGERDOWN for finger %i\n", (int)event.tfinger.fingerId);
1212 #endif
1213                                 for (i = 0;i < MAXFINGERS-1;i++)
1214                                 {
1215                                         if (!multitouch[i][0])
1216                                         {
1217                                                 multitouch[i][0] = event.tfinger.fingerId + 1;
1218                                                 multitouch[i][1] = event.tfinger.x;
1219                                                 multitouch[i][2] = event.tfinger.y;
1220                                                 // TODO: use event.tfinger.pressure?
1221                                                 break;
1222                                         }
1223                                 }
1224                                 if (i == MAXFINGERS-1)
1225                                         Con_DPrintf("Too many fingers at once!\n");
1226                                 break;
1227                         case SDL_FINGERUP:
1228 #ifdef DEBUGSDLEVENTS
1229                                 Con_DPrintf("SDL_FINGERUP for finger %i\n", (int)event.tfinger.fingerId);
1230 #endif
1231                                 for (i = 0;i < MAXFINGERS-1;i++)
1232                                 {
1233                                         if (multitouch[i][0] == event.tfinger.fingerId + 1)
1234                                         {
1235                                                 multitouch[i][0] = 0;
1236                                                 break;
1237                                         }
1238                                 }
1239                                 if (i == MAXFINGERS-1)
1240                                         Con_DPrintf("No SDL_FINGERDOWN event matches this SDL_FINGERMOTION event\n");
1241                                 break;
1242                         case SDL_FINGERMOTION:
1243 #ifdef DEBUGSDLEVENTS
1244                                 Con_DPrintf("SDL_FINGERMOTION for finger %i\n", (int)event.tfinger.fingerId);
1245 #endif
1246                                 for (i = 0;i < MAXFINGERS-1;i++)
1247                                 {
1248                                         if (multitouch[i][0] == event.tfinger.fingerId + 1)
1249                                         {
1250                                                 multitouch[i][1] = event.tfinger.x;
1251                                                 multitouch[i][2] = event.tfinger.y;
1252                                                 break;
1253                                         }
1254                                 }
1255                                 if (i == MAXFINGERS-1)
1256                                         Con_DPrintf("No SDL_FINGERDOWN event matches this SDL_FINGERMOTION event\n");
1257                                 break;
1258                         default:
1259 #ifdef DEBUGSDLEVENTS
1260                                 Con_DPrintf("Received unrecognized SDL_Event type 0x%x\n", event.type);
1261 #endif
1262                                 break;
1263                 }
1264
1265         // enable/disable sound on focus gain/loss
1266         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1267         {
1268                 if (!sound_active)
1269                 {
1270                         S_UnblockSound ();
1271                         sound_active = true;
1272                 }
1273         }
1274         else
1275         {
1276                 if (sound_active)
1277                 {
1278                         S_BlockSound ();
1279                         sound_active = false;
1280                 }
1281         }
1282 }
1283
1284 /////////////////
1285 // Video system
1286 ////
1287
1288 void *GL_GetProcAddress(const char *name)
1289 {
1290         void *p = NULL;
1291         p = SDL_GL_GetProcAddress(name);
1292         return p;
1293 }
1294
1295 qboolean GL_ExtensionSupported(const char *name)
1296 {
1297         return SDL_GL_ExtensionSupported(name);
1298 }
1299
1300 static qboolean vid_sdl_initjoysticksystem = false;
1301
1302 void VID_Init (void)
1303 {
1304 #ifndef __IPHONEOS__
1305 #ifdef MACOSX
1306         Cvar_RegisterVariable(&apple_mouse_noaccel);
1307 #endif
1308 #endif
1309 #ifdef DP_MOBILETOUCH
1310         Cvar_SetValueQuick(&vid_touchscreen, 1);
1311 #endif
1312
1313 #ifdef SDL_R_RESTART
1314         R_RegisterModule("SDL", sdl_start, sdl_shutdown, sdl_newmap, NULL, NULL);
1315 #endif
1316
1317         if (SDL_Init(SDL_INIT_VIDEO) < 0)
1318                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
1319         vid_sdl_initjoysticksystem = SDL_InitSubSystem(SDL_INIT_JOYSTICK) >= 0;
1320         if (vid_sdl_initjoysticksystem)
1321                 Con_Printf("Failed to init SDL joystick subsystem: %s\n", SDL_GetError());
1322         vid_isfullscreen = false;
1323 }
1324
1325 static int vid_sdljoystickindex = -1;
1326 void VID_EnableJoystick(qboolean enable)
1327 {
1328         int index = joy_enable.integer > 0 ? joy_index.integer : -1;
1329         int numsdljoysticks;
1330         qboolean success = false;
1331         int sharedcount = 0;
1332         int sdlindex = -1;
1333         sharedcount = VID_Shared_SetJoystick(index);
1334         if (index >= 0 && index < sharedcount)
1335                 success = true;
1336         sdlindex = index - sharedcount;
1337
1338         numsdljoysticks = SDL_NumJoysticks();
1339         if (sdlindex < 0 || sdlindex >= numsdljoysticks)
1340                 sdlindex = -1;
1341
1342         // update cvar containing count of XInput joysticks + SDL joysticks
1343         if (joy_detected.integer != sharedcount + numsdljoysticks)
1344                 Cvar_SetValueQuick(&joy_detected, sharedcount + numsdljoysticks);
1345
1346         if (vid_sdljoystickindex != sdlindex)
1347         {
1348                 vid_sdljoystickindex = sdlindex;
1349                 // close SDL joystick if active
1350                 if (vid_sdljoystick)
1351                         SDL_JoystickClose(vid_sdljoystick);
1352                 vid_sdljoystick = NULL;
1353                 if (sdlindex >= 0)
1354                 {
1355                         vid_sdljoystick = SDL_JoystickOpen(sdlindex);
1356                         if (vid_sdljoystick)
1357                         {
1358                                 const char *joystickname = SDL_JoystickName(vid_sdljoystick);
1359                                 Con_Printf("Joystick %i opened (SDL_Joystick %i is \"%s\" with %i axes, %i buttons, %i balls)\n", index, sdlindex, joystickname, (int)SDL_JoystickNumAxes(vid_sdljoystick), (int)SDL_JoystickNumButtons(vid_sdljoystick), (int)SDL_JoystickNumBalls(vid_sdljoystick));
1360                         }
1361                         else
1362                         {
1363                                 Con_Printf("Joystick %i failed (SDL_JoystickOpen(%i) returned: %s)\n", index, sdlindex, SDL_GetError());
1364                                 sdlindex = -1;
1365                         }
1366                 }
1367         }
1368
1369         if (sdlindex >= 0)
1370                 success = true;
1371
1372         if (joy_active.integer != (success ? 1 : 0))
1373                 Cvar_SetValueQuick(&joy_active, success ? 1 : 0);
1374 }
1375
1376 static void VID_OutputVersion(void)
1377 {
1378         SDL_version version;
1379         SDL_GetVersion(&version);
1380         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
1381                                         "Using SDL library version %d.%d.%d\n",
1382                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
1383                                         version.major, version.minor, version.patch );
1384 }
1385
1386 #ifdef WIN32
1387 static void AdjustWindowBounds(viddef_mode_t *mode, RECT *rect)
1388 {
1389         LONG width = mode->width; // vid_width
1390         LONG height = mode->height; // vid_height
1391
1392         // adjust width and height for the space occupied by window decorators (title bar, borders)
1393         rect->top = 0;
1394         rect->left = 0;
1395         rect->right = width;
1396         rect->bottom = height;
1397         AdjustWindowRectEx(rect, WS_CAPTION|WS_THICKFRAME, false, 0);
1398
1399         RECT workArea;
1400         SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
1401         int workWidth = workArea.right - workArea.left;
1402         int workHeight = workArea.bottom - workArea.top;
1403
1404         // SDL forces the window height to be <= screen height - 27px (on Win8.1 - probably intended for the title bar) 
1405         // If the task bar is docked to the the left screen border and we move the window to negative y,
1406         // there would be some part of the regular desktop visible on the bottom of the screen.
1407         int titleBarPixels = 2;
1408         int screenHeight = GetSystemMetrics(SM_CYSCREEN);
1409         if (screenHeight == workHeight)
1410                 titleBarPixels = -rect->top;
1411
1412         //Con_Printf("window mode: %dx%d, workArea: %d/%d-%d/%d (%dx%d), title: %d\n", width, height, workArea.left, workArea.top, workArea.right, workArea.bottom, workArea.right - workArea.left, workArea.bottom - workArea.top, titleBarPixels);
1413
1414         // if height and width matches the physical or previously adjusted screen height and width, adjust it to available desktop area
1415         if ((width == GetSystemMetrics(SM_CXSCREEN) || width == workWidth) && (height == screenHeight || height == workHeight - titleBarPixels))
1416         {
1417                 rect->left = workArea.left;
1418                 mode->width = workWidth;
1419                 rect->top = workArea.top + titleBarPixels;
1420                 mode->height = workHeight - titleBarPixels;
1421         }
1422         else 
1423         {
1424                 rect->left = workArea.left + max(0, (workWidth - width) / 2);
1425                 rect->top = workArea.top + max(0, (workHeight - height) / 2);
1426         }
1427 }
1428 #endif
1429
1430 static qboolean VID_InitModeGL(viddef_mode_t *mode)
1431 {
1432         int windowflags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;
1433         int xPos = SDL_WINDOWPOS_UNDEFINED;
1434         int yPos = SDL_WINDOWPOS_UNDEFINED;
1435 #ifndef USE_GLES2
1436         int i;
1437         const char *drivername;
1438 #endif
1439
1440         win_half_width = mode->width>>1;
1441         win_half_height = mode->height>>1;
1442
1443         if(vid_resizable.integer)
1444                 windowflags |= SDL_WINDOW_RESIZABLE;
1445
1446         VID_OutputVersion();
1447
1448 #ifndef USE_GLES2
1449         // SDL usually knows best
1450         drivername = NULL;
1451
1452 // 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
1453         i = COM_CheckParm("-gl_driver");
1454         if (i && i < com_argc - 1)
1455                 drivername = com_argv[i + 1];
1456         if (SDL_GL_LoadLibrary(drivername) < 0)
1457         {
1458                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
1459                 return false;
1460         }
1461 #endif
1462
1463 #ifdef DP_MOBILETOUCH
1464         // mobile platforms are always fullscreen, we'll get the resolution after opening the window
1465         mode->fullscreen = true;
1466         // hide the menu with SDL_WINDOW_BORDERLESS
1467         windowflags |= SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
1468 #endif
1469
1470         vid_isfullscreen = false;
1471         {
1472                 if (mode->fullscreen) {
1473                         if (vid_desktopfullscreen.integer)
1474                         {
1475                                 vid_mode_t *m = VID_GetDesktopMode();
1476                                 mode->width = m->width;
1477                                 mode->height = m->height;
1478                                 windowflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
1479                         }
1480                         else
1481                                 windowflags |= SDL_WINDOW_FULLSCREEN;
1482                         vid_isfullscreen = true;
1483                 }
1484                 else {
1485 #ifdef WIN32
1486                         RECT rect;
1487                         AdjustWindowBounds(mode, &rect);
1488                         xPos = rect.left;
1489                         yPos = rect.top;
1490 #endif
1491                 }
1492         }
1493         //flags |= SDL_HWSURFACE;
1494
1495         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
1496         SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
1497         SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
1498         SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
1499         SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
1500         SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
1501         SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
1502         if (mode->stereobuffer)
1503                 SDL_GL_SetAttribute (SDL_GL_STEREO, 1);
1504         if (mode->samples > 1)
1505         {
1506                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
1507                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, mode->samples);
1508         }
1509
1510 #ifdef USE_GLES2
1511         SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
1512         SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0);
1513         SDL_GL_SetAttribute (SDL_GL_RETAINED_BACKING, 1);
1514 #else
1515         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
1516         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
1517         SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, (gl_debug.integer > 0 ? SDL_GL_CONTEXT_DEBUG_FLAG : 0));
1518 #endif
1519
1520         video_bpp = mode->bitsperpixel;
1521         window_flags = windowflags;
1522         window = SDL_CreateWindow(gamename, xPos, yPos, mode->width, mode->height, windowflags);
1523         if (window == NULL)
1524         {
1525                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
1526                 VID_Shutdown();
1527                 return false;
1528         }
1529         SDL_GetWindowSize(window, &mode->width, &mode->height);
1530         context = SDL_GL_CreateContext(window);
1531         if (context == NULL)
1532         {
1533                 Con_Printf("Failed to initialize OpenGL context: %s\n", SDL_GetError());
1534                 VID_Shutdown();
1535                 return false;
1536         }
1537
1538         SDL_GL_SetSwapInterval(bound(-1, vid_vsync.integer, 1));
1539         vid_usingvsync = (vid_vsync.integer != 0);
1540
1541         gl_platform = "SDL";
1542
1543         GL_Setup();
1544
1545         // VorteX: set other info
1546         extern cvar_t gl_info_vendor;
1547         extern cvar_t gl_info_renderer;
1548         extern cvar_t gl_info_version;
1549         extern cvar_t gl_info_platform;
1550         extern cvar_t gl_info_driver;
1551         Cvar_SetQuick(&gl_info_vendor, gl_vendor);
1552         Cvar_SetQuick(&gl_info_renderer, gl_renderer);
1553         Cvar_SetQuick(&gl_info_version, gl_version);
1554         Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
1555         Cvar_SetQuick(&gl_info_driver, gl_driver);
1556
1557         // LordHavoc: report supported extensions
1558 #ifdef CONFIG_MENU
1559         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions);
1560 #else
1561         Con_DPrintf("\nQuakeC extensions for server and client: %s\n", vm_sv_extensions);
1562 #endif
1563
1564         // clear to black (loading plaque will be seen over this)
1565         GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 0);
1566
1567         vid_hidden = false;
1568         vid_activewindow = false;
1569         vid_hasfocus = true;
1570         vid_usingmouse = false;
1571         vid_usinghidecursor = false;
1572                 
1573         return true;
1574 }
1575
1576 extern cvar_t gl_info_extensions;
1577 extern cvar_t gl_info_vendor;
1578 extern cvar_t gl_info_renderer;
1579 extern cvar_t gl_info_version;
1580 extern cvar_t gl_info_platform;
1581 extern cvar_t gl_info_driver;
1582
1583 qboolean VID_InitMode(viddef_mode_t *mode)
1584 {
1585         // GAME_STEELSTORM specific
1586         steelstorm_showing_map = Cvar_FindVar("steelstorm_showing_map");
1587         steelstorm_showing_mousecursor = Cvar_FindVar("steelstorm_showing_mousecursor");
1588
1589         if (!SDL_WasInit(SDL_INIT_VIDEO) && SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
1590                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
1591
1592         Cvar_SetValueQuick(&vid_touchscreen_supportshowkeyboard, SDL_HasScreenKeyboardSupport() ? 1 : 0);
1593         return VID_InitModeGL(mode);
1594 }
1595
1596 void VID_Shutdown (void)
1597 {
1598         VID_EnableJoystick(false);
1599         VID_SetMouse(false, false, false);
1600
1601         SDL_DestroyWindow(window);
1602         window = NULL;
1603
1604         SDL_QuitSubSystem(SDL_INIT_VIDEO);
1605
1606         gl_driver[0] = 0;
1607         gl_extensions = "";
1608         gl_platform = "";
1609 }
1610
1611 void VID_Finish (void)
1612 {
1613         qboolean vid_usevsync;
1614         vid_activewindow = !vid_hidden && vid_hasfocus;
1615
1616         VID_UpdateGamma();
1617
1618         if (!vid_hidden)
1619         {
1620                 switch(vid.renderpath)
1621                 {
1622                 case RENDERPATH_GL32:
1623                 case RENDERPATH_GLES2:
1624                         CHECKGLERROR
1625                         if (r_speeds.integer == 2 || gl_finish.integer)
1626                                 GL_Finish();
1627
1628                         vid_usevsync = (vid_vsync.integer && !cls.timedemo);
1629                         if (vid_usingvsync != vid_usevsync)
1630                         {
1631                                 vid_usingvsync = vid_usevsync;
1632                                 if (SDL_GL_SetSwapInterval(vid_usevsync != 0) >= 0)
1633                                         Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
1634                                 else
1635                                         Con_DPrintf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
1636                         }
1637                         SDL_GL_SwapWindow(window);
1638                         break;
1639                 }
1640         }
1641 }
1642
1643 vid_mode_t *VID_GetDesktopMode(void)
1644 {
1645         SDL_DisplayMode mode;
1646         int bpp;
1647         Uint32 rmask, gmask, bmask, amask;
1648         SDL_GetDesktopDisplayMode(0, &mode);
1649         SDL_PixelFormatEnumToMasks(mode.format, &bpp, &rmask, &gmask, &bmask, &amask);
1650         desktop_mode.width = mode.w;
1651         desktop_mode.height = mode.h;
1652         desktop_mode.bpp = bpp;
1653         desktop_mode.refreshrate = mode.refresh_rate;
1654         desktop_mode.pixelheight_num = 1;
1655         desktop_mode.pixelheight_denom = 1; // SDL does not provide this
1656         // TODO check whether this actually works, or whether we do still need
1657         // a read-window-size-after-entering-desktop-fullscreen hack for
1658         // multiscreen setups.
1659         return &desktop_mode;
1660 }
1661
1662 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1663 {
1664         size_t k = 0;
1665         int modenum;
1666         int nummodes = SDL_GetNumDisplayModes(0);
1667         SDL_DisplayMode mode;
1668         for (modenum = 0;modenum < nummodes;modenum++)
1669         {
1670                 if (k >= maxcount)
1671                         break;
1672                 if (SDL_GetDisplayMode(0, modenum, &mode))
1673                         continue;
1674                 modes[k].width = mode.w;
1675                 modes[k].height = mode.h;
1676                 // FIXME bpp?
1677                 modes[k].refreshrate = mode.refresh_rate;
1678                 modes[k].pixelheight_num = 1;
1679                 modes[k].pixelheight_denom = 1; // SDL does not provide this
1680                 k++;
1681         }
1682         return k;
1683 }