2 Copyright (C) 2003 T. Joseph Carter
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.
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.
13 See the GNU General Public License for more details.
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.
24 // Tell startup code that we have a client
25 int cl_available = true;
26 static int vid_usingmouse;
27 static int vid_isfullscreen;
29 static SDL_Surface *screen;
31 static void IN_Init( void );
32 static void IN_Shutdown( void );
34 /////////////////////////
37 //TODO: Add joystick support
38 //TODO: Add error checking
41 //keysym to quake keysym mapping
42 #define tenoh 0,0,0,0,0, 0,0,0,0,0
43 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
44 #define hundredoh fiftyoh, fiftyoh
45 static unsigned int tbl_sdltoquake[] =
47 0,0,0,0, //SDLK_UNKNOWN = 0,
48 0,0,0,0, //SDLK_FIRST = 0,
49 K_BACKSPACE, //SDLK_BACKSPACE = 8,
50 K_TAB, //SDLK_TAB = 9,
53 K_ENTER, //SDLK_RETURN = 13,
55 K_PAUSE, //SDLK_PAUSE = 19,
57 K_ESCAPE, //SDLK_ESCAPE = 27,
59 K_SPACE, //SDLK_SPACE = 32,
60 '!', //SDLK_EXCLAIM = 33,
61 '"', //SDLK_QUOTEDBL = 34,
62 '#', //SDLK_HASH = 35,
63 '$', //SDLK_DOLLAR = 36,
65 '&', //SDLK_AMPERSAND = 38,
66 '\'', //SDLK_QUOTE = 39,
67 '(', //SDLK_LEFTPAREN = 40,
68 ')', //SDLK_RIGHTPAREN = 41,
69 '*', //SDLK_ASTERISK = 42,
70 '+', //SDLK_PLUS = 43,
71 ',', //SDLK_COMMA = 44,
72 '-', //SDLK_MINUS = 45,
73 '.', //SDLK_PERIOD = 46,
74 '/', //SDLK_SLASH = 47,
85 ':', //SDLK_COLON = 58,
86 ';', //SDLK_SEMICOLON = 59,
87 '<', //SDLK_LESS = 60,
88 '=', //SDLK_EQUALS = 61,
89 '>', //SDLK_GREATER = 62,
90 '?', //SDLK_QUESTION = 63,
92 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,
93 '[', //SDLK_LEFTBRACKET = 91,
94 '\\', //SDLK_BACKSLASH = 92,
95 ']', //SDLK_RIGHTBRACKET = 93,
96 '^', //SDLK_CARET = 94,
97 '_', //SDLK_UNDERSCORE = 95,
98 '`', //SDLK_BACKQUOTE = 96,
126 K_DEL, //SDLK_DELETE = 127,
127 hundredoh /*227*/, tenoh, tenoh, 0,0,0,0,0,0,0,0,
128 K_KP_0, //SDLK_KP0 = 256,
129 K_KP_1, //SDLK_KP1 = 257,
130 K_KP_2, //SDLK_KP2 = 258,
131 K_KP_3, //SDLK_KP3 = 259,
132 K_KP_4, //SDLK_KP4 = 260,
133 K_KP_5, //SDLK_KP5 = 261,
134 K_KP_6, //SDLK_KP6 = 262,
135 K_KP_7, //SDLK_KP7 = 263,
136 K_KP_8, //SDLK_KP8 = 264,
137 K_KP_9, //SDLK_KP9 = 265,
138 K_KP_PERIOD,//SDLK_KP_PERIOD = 266,
139 K_KP_DIVIDE,//SDLK_KP_DIVIDE = 267,
140 K_KP_MULTIPLY,//SDLK_KP_MULTIPLY= 268,
141 K_KP_MINUS, //SDLK_KP_MINUS = 269,
142 K_KP_PLUS, //SDLK_KP_PLUS = 270,
143 K_KP_ENTER, //SDLK_KP_ENTER = 271,
144 K_KP_EQUALS,//SDLK_KP_EQUALS = 272,
145 K_UPARROW, //SDLK_UP = 273,
146 K_DOWNARROW,//SDLK_DOWN = 274,
147 K_RIGHTARROW,//SDLK_RIGHT = 275,
148 K_LEFTARROW,//SDLK_LEFT = 276,
149 K_INS, //SDLK_INSERT = 277,
150 K_HOME, //SDLK_HOME = 278,
151 K_END, //SDLK_END = 279,
152 K_PGUP, //SDLK_PAGEUP = 280,
153 K_PGDN, //SDLK_PAGEDOWN = 281,
154 K_F1, //SDLK_F1 = 282,
155 K_F2, //SDLK_F2 = 283,
156 K_F3, //SDLK_F3 = 284,
157 K_F4, //SDLK_F4 = 285,
158 K_F5, //SDLK_F5 = 286,
159 K_F6, //SDLK_F6 = 287,
160 K_F7, //SDLK_F7 = 288,
161 K_F8, //SDLK_F8 = 289,
162 K_F9, //SDLK_F9 = 290,
163 K_F10, //SDLK_F10 = 291,
164 K_F11, //SDLK_F11 = 292,
165 K_F12, //SDLK_F12 = 293,
170 K_NUMLOCK, //SDLK_NUMLOCK = 300,
171 K_CAPSLOCK, //SDLK_CAPSLOCK = 301,
172 K_SCROLLOCK,//SDLK_SCROLLOCK= 302,
173 K_SHIFT, //SDLK_RSHIFT = 303,
174 K_SHIFT, //SDLK_LSHIFT = 304,
175 K_CTRL, //SDLK_RCTRL = 305,
176 K_CTRL, //SDLK_LCTRL = 306,
177 K_ALT, //SDLK_RALT = 307,
178 K_ALT, //SDLK_LALT = 308,
179 0, //SDLK_RMETA = 309,
180 0, //SDLK_LMETA = 310,
181 0, //SDLK_LSUPER = 311, /* Left "Windows" key */
182 0, //SDLK_RSUPER = 312, /* Right "Windows" key */
183 0, //SDLK_MODE = 313, /* "Alt Gr" key */
184 0, //SDLK_COMPOSE = 314, /* Multi-key compose key */
185 0, //SDLK_HELP = 315,
186 0, //SDLK_PRINT = 316,
187 0, //SDLK_SYSREQ = 317,
188 K_PAUSE, //SDLK_BREAK = 318,
189 0, //SDLK_MENU = 319,
190 0, //SDLK_POWER = 320, /* Power Macintosh power key */
191 'e', //SDLK_EURO = 321, /* Some european keyboards */
192 0 //SDLK_UNDO = 322, /* Atari keyboard has Undo */
198 static int MapKey( int sdlkey )
200 if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
202 return tbl_sdltoquake[ sdlkey ];
205 static void IN_Activate( void )
207 SDL_WM_GrabInput( SDL_GRAB_ON );
208 SDL_ShowCursor( SDL_DISABLE );
211 static void IN_Deactivate( void )
213 SDL_WM_GrabInput( SDL_GRAB_OFF );
214 SDL_ShowCursor( SDL_ENABLE );
217 void IN_Commands (void)
221 static void IN_MouseMove (usercmd_t *cmd)
225 if( !vid_usingmouse ) {
226 IN_Mouse( cmd, 0, 0 );
230 SDL_GetRelativeMouseState( &x, &y );
231 IN_Mouse( cmd, x, y );
234 void IN_Move( usercmd_t *cmd )
239 static void IN_Init( void )
242 SDL_EnableUNICODE( SDL_ENABLE );
245 vid_usingmouse = false;
248 static void IN_Shutdown( void )
252 /////////////////////
256 static int Sys_EventFilter( SDL_Event *event )
258 //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
260 if( event->type == SDL_QUIT && MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO )
269 void Sys_SendKeyEvents( void )
273 while( SDL_PollEvent( &event ) )
274 switch( event.type ) {
280 Key_Event( MapKey( event.key.keysym.sym ), event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
282 case SDL_ACTIVEEVENT:
283 if( event.active.state == SDL_APPACTIVE )
284 if( event.active.gain )
289 case SDL_MOUSEBUTTONDOWN:
290 if( event.button.button == SDL_BUTTON_MIDDLE )
291 event.button.button = SDL_BUTTON_RIGHT;
292 else if( event.button.button == SDL_BUTTON_RIGHT )
293 event.button.button = SDL_BUTTON_MIDDLE;
294 Key_Event( K_MOUSE1 + event.button.button - 1, 0, true );
296 case SDL_MOUSEBUTTONUP:
297 if( event.button.button == SDL_BUTTON_MIDDLE )
298 event.button.button = SDL_BUTTON_RIGHT;
299 else if( event.button.button == SDL_BUTTON_RIGHT )
300 event.button.button = SDL_BUTTON_MIDDLE;
301 Key_Event( K_MOUSE1 + event.button.button - 1, 0, false );
310 void *GL_GetProcAddress(const char *name)
313 p = SDL_GL_GetProcAddress(name);
317 static int Sys_EventFilter( SDL_Event *event );
320 if (SDL_Init(SDL_INIT_VIDEO) < 0)
321 Sys_Error ("Failed to init video: %s\n", SDL_GetError());
322 vid_isfullscreen = false;
324 SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
328 // set the icon (we dont use SDL here since it would be too much a PITA)
330 #include "resource.h"
331 #include <SDL_syswm.h>
332 static void VID_SetCaption()
338 //SDL_WM_SetCaption( gamename, NULL );
340 // get the HWND handle
341 SDL_VERSION( &info.version );
342 if( !SDL_GetWMInfo( &info ) )
345 icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
346 //icon = LoadIcon( NULL, IDI_ERROR );
347 SetClassLong( info.window, GCL_HICONSM, (LONG) icon );
350 static void VID_SetCaption()
352 SDL_WM_SetCaption( gamename, NULL );
356 static void VID_OutputVersion()
358 const SDL_version *version;
359 version = SDL_Linked_Version();
360 Con_DPrintf( "Linked against SDL version %d.%d.%d\n"
361 "Using SDL library version %d.%d.%d\n",
362 SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
363 version->major, version->minor, version->patch );
366 int VID_InitMode(int fullscreen, int width, int height, int bpp)
369 int flags = SDL_OPENGL;
370 const char *drivername;
376 We cant switch from one OpenGL video mode to another.
377 Thus we first switch to some stupid 2D mode and then back to OpenGL.
379 SDL_SetVideoMode( 0, 0, 0, 0 );
382 drivername = "opengl32.dll";
383 #elif defined(__APPLE__) && defined(__MACH__)
384 drivername = "OpenGL.framework";
386 drivername = "libGL.so.1";
389 i = COM_CheckParm("-gl_driver");
390 if (i && i < com_argc - 1)
391 drivername = com_argv[i + 1];
392 if (SDL_GL_LoadLibrary(drivername))
394 Con_Printf("Unable to load GL driver \"%s\": ", drivername, SDL_GetError());
398 qglGetString = GL_GetProcAddress("glGetString");
400 // Knghtbrd: should do platform-specific extension string function here
402 if (qglGetString == NULL)
405 Con_Print("Required OpenGL function glGetString not found\n");
409 vid_isfullscreen = false;
411 flags |= SDL_FULLSCREEN;
412 vid_isfullscreen = true;
415 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1);
416 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1);
417 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1);
418 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
419 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
421 screen = SDL_SetVideoMode(width, height, bpp, flags);
424 Con_Printf("Failed to set video mode to %ix%i: %s\n", width, height, SDL_GetError);
430 gl_renderer = qglGetString(GL_RENDERER);
431 gl_vendor = qglGetString(GL_VENDOR);
432 gl_version = qglGetString(GL_VERSION);
433 gl_extensions = qglGetString(GL_EXTENSIONS);
435 // Knghtbrd: should assign platform-specific extensions here
437 gl_platformextensions = "";
438 gl_videosyncavailable = false;
443 vid_activewindow = false;
444 vid_usingmouse = false;
450 void VID_Shutdown (void)
453 SDL_QuitSubSystem(SDL_INIT_VIDEO);
456 int VID_SetGamma (unsigned short *ramps)
458 return !SDL_SetGammaRamp (ramps, ramps + 256, ramps + 512);
461 int VID_GetGamma (unsigned short *ramps)
463 return !SDL_GetGammaRamp( ramps, ramps + 256, ramps + 512);
466 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
473 void VID_Finish (void)
479 SDL_GL_SwapBuffers();
481 //react on appstate changes
482 appstate = SDL_GetAppState();
484 if( !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
485 vid_activewindow = false;
487 vid_activewindow = true;
489 vid_usemouse = false;
490 if( vid_mouse.integer && !key_consoleactive )
492 if( vid_isfullscreen )
494 if( !vid_activewindow )
495 vid_usemouse = false;
497 if( vid_usemouse && !vid_usingmouse ) {
498 vid_usingmouse = true;
500 } else if( !vid_usemouse && vid_usingmouse ) {
501 vid_usingmouse = false;