2 Copyright (C) 1996-1997 Id Software, Inc.
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.
20 // gl_vidnt.c -- NT GL vid component
27 int cl_available = true;
29 int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *);
30 int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
31 //int (WINAPI *qwglGetPixelFormat)(HDC);
32 BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *);
33 BOOL (WINAPI *qwglSwapBuffers)(HDC);
34 HGLRC (WINAPI *qwglCreateContext)(HDC);
35 BOOL (WINAPI *qwglDeleteContext)(HGLRC);
36 HGLRC (WINAPI *qwglGetCurrentContext)(VOID);
37 HDC (WINAPI *qwglGetCurrentDC)(VOID);
38 PROC (WINAPI *qwglGetProcAddress)(LPCSTR);
39 BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC);
40 BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
41 const char *(WINAPI *qwglGetExtensionsStringARB)(HDC hdc);
43 static dllfunction_t wglfuncs[] =
45 {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat},
46 {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat},
47 // {"wglGetPixelFormat", (void **) &qwglGetPixelFormat},
48 {"wglSetPixelFormat", (void **) &qwglSetPixelFormat},
49 {"wglSwapBuffers", (void **) &qwglSwapBuffers},
50 {"wglCreateContext", (void **) &qwglCreateContext},
51 {"wglDeleteContext", (void **) &qwglDeleteContext},
52 {"wglGetProcAddress", (void **) &qwglGetProcAddress},
53 {"wglMakeCurrent", (void **) &qwglMakeCurrent},
54 {"wglGetCurrentContext", (void **) &qwglGetCurrentContext},
55 {"wglGetCurrentDC", (void **) &qwglGetCurrentDC},
59 static dllfunction_t wglswapintervalfuncs[] =
61 {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT},
65 qboolean scr_skipupdate;
67 static DEVMODE gdevmode;
68 static qboolean vid_initialized = false;
69 static qboolean vid_wassuspended = false;
70 static int vid_usingmouse;
71 extern qboolean mouseactive; // from in_win.c
76 //HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
78 static int vid_isfullscreen;
80 //void VID_MenuDraw (void);
81 //void VID_MenuKey (int key);
83 //LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
84 //void AppActivate(BOOL fActive, BOOL minimize);
85 //void ClearAllStates (void);
86 //void VID_UpdateWindowStatus (void);
88 //====================================
90 int window_x, window_y, window_width, window_height;
91 int window_center_x, window_center_y;
93 void IN_ShowMouse (void);
94 void IN_DeactivateMouse (void);
95 void IN_HideMouse (void);
96 void IN_ActivateMouse (void);
97 void IN_MouseEvent (int mstate);
98 void IN_UpdateClipCursor (void);
100 qboolean mouseinitialized;
101 static qboolean dinput;
107 #define DINPUT_BUFFERSIZE 16
108 #define iDirectInputCreate(a,b,c,d) pDirectInputCreate(a,b,c,d)
110 HRESULT (WINAPI *pDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion,
111 LPDIRECTINPUT * lplpDirectInput, LPUNKNOWN punkOuter);
115 int mouse_oldbuttonstate;
117 int mouse_x, mouse_y, old_mouse_x, old_mouse_y, mx_accum, my_accum;
119 static qboolean restore_spi;
120 static int originalmouseparms[3], newmouseparms[3] = {0, 0, 1};
122 unsigned int uiWheelMessage;
123 qboolean mouseactive;
124 //qboolean mouseinitialized;
125 static qboolean mouseparmsvalid, mouseactivatetoggle;
126 static qboolean mouseshowtoggle = 1;
127 static qboolean dinput_acquired;
129 static unsigned int mstate_di;
131 // joystick defines and variables
132 // where should defines be moved?
133 #define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick
134 #define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball
135 #define JOY_MAX_AXES 6 // X, Y, Z, R, U, V
145 AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn
148 DWORD dwAxisFlags[JOY_MAX_AXES] =
150 JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
153 DWORD dwAxisMap[JOY_MAX_AXES];
154 DWORD dwControlMap[JOY_MAX_AXES];
155 PDWORD pdwRawValue[JOY_MAX_AXES];
157 // none of these cvars are saved over a session
158 // this means that advanced controller configuration needs to be executed
159 // each time. this avoids any problems with getting back to a default usage
160 // or when changing from one controller to another. this way at least something
162 cvar_t in_joystick = {CVAR_SAVE, "joystick","0"};
163 cvar_t joy_name = {0, "joyname", "joystick"};
164 cvar_t joy_advanced = {0, "joyadvanced", "0"};
165 cvar_t joy_advaxisx = {0, "joyadvaxisx", "0"};
166 cvar_t joy_advaxisy = {0, "joyadvaxisy", "0"};
167 cvar_t joy_advaxisz = {0, "joyadvaxisz", "0"};
168 cvar_t joy_advaxisr = {0, "joyadvaxisr", "0"};
169 cvar_t joy_advaxisu = {0, "joyadvaxisu", "0"};
170 cvar_t joy_advaxisv = {0, "joyadvaxisv", "0"};
171 cvar_t joy_forwardthreshold = {0, "joyforwardthreshold", "0.15"};
172 cvar_t joy_sidethreshold = {0, "joysidethreshold", "0.15"};
173 cvar_t joy_pitchthreshold = {0, "joypitchthreshold", "0.15"};
174 cvar_t joy_yawthreshold = {0, "joyyawthreshold", "0.15"};
175 cvar_t joy_forwardsensitivity = {0, "joyforwardsensitivity", "-1.0"};
176 cvar_t joy_sidesensitivity = {0, "joysidesensitivity", "-1.0"};
177 cvar_t joy_pitchsensitivity = {0, "joypitchsensitivity", "1.0"};
178 cvar_t joy_yawsensitivity = {0, "joyyawsensitivity", "-1.0"};
179 cvar_t joy_wwhack1 = {0, "joywwhack1", "0.0"};
180 cvar_t joy_wwhack2 = {0, "joywwhack2", "0.0"};
182 qboolean joy_avail, joy_advancedinit, joy_haspov;
183 DWORD joy_oldbuttonstate, joy_oldpovstate;
187 DWORD joy_numbuttons;
189 static LPDIRECTINPUT g_pdi;
190 static LPDIRECTINPUTDEVICE g_pMouse;
194 static HINSTANCE hInstDI;
196 //static qboolean dinput;
198 typedef struct MYDATA {
199 LONG lX; // X axis goes here
200 LONG lY; // Y axis goes here
201 LONG lZ; // Z axis goes here
202 BYTE bButtonA; // One button goes here
203 BYTE bButtonB; // Another button goes here
204 BYTE bButtonC; // Another button goes here
205 BYTE bButtonD; // Another button goes here
208 static DIOBJECTDATAFORMAT rgodf[] = {
209 { &GUID_XAxis, FIELD_OFFSET(MYDATA, lX), DIDFT_AXIS | DIDFT_ANYINSTANCE, 0,},
210 { &GUID_YAxis, FIELD_OFFSET(MYDATA, lY), DIDFT_AXIS | DIDFT_ANYINSTANCE, 0,},
211 { &GUID_ZAxis, FIELD_OFFSET(MYDATA, lZ), 0x80000000 | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0,},
212 { 0, FIELD_OFFSET(MYDATA, bButtonA), DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
213 { 0, FIELD_OFFSET(MYDATA, bButtonB), DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
214 { 0, FIELD_OFFSET(MYDATA, bButtonC), 0x80000000 | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
215 { 0, FIELD_OFFSET(MYDATA, bButtonD), 0x80000000 | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0,},
218 #define NUM_OBJECTS (sizeof(rgodf) / sizeof(rgodf[0]))
220 static DIDATAFORMAT df = {
221 sizeof(DIDATAFORMAT), // this structure
222 sizeof(DIOBJECTDATAFORMAT), // size of object data format
223 DIDF_RELAXIS, // absolute axis coordinates
224 sizeof(MYDATA), // device data size
225 NUM_OBJECTS, // number of objects
226 rgodf, // and here they are
229 // forward-referenced functions
230 void IN_StartupJoystick (void);
231 void Joy_AdvancedUpdate_f (void);
232 void IN_JoyMove (usercmd_t *cmd);
233 void IN_StartupMouse (void);
237 VID_UpdateWindowStatus
240 void VID_UpdateWindowStatus (void)
242 window_center_x = window_x + window_width / 2;
243 window_center_y = window_y + window_height / 2;
245 if (mouseinitialized && mouseactive && !dinput)
248 window_rect.left = window_x;
249 window_rect.top = window_y;
250 window_rect.right = window_x + window_width;
251 window_rect.bottom = window_y + window_height;
252 ClipCursor (&window_rect);
257 //====================================
264 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
268 *width = window_width;
269 *height = window_height;
273 void VID_Finish (void)
277 if (r_render.integer && !scr_skipupdate)
280 hdc = GetDC(mainwindow);
282 ReleaseDC(mainwindow, hdc);
285 // handle the mouse state when windowed if that's changed
286 vid_usemouse = false;
287 if (vid_mouse.integer && !key_consoleactive)
289 if (vid_isfullscreen)
291 if (!vid_activewindow)
292 vid_usemouse = false;
297 vid_usingmouse = true;
306 vid_usingmouse = false;
307 IN_DeactivateMouse ();
313 //==========================================================================
318 qbyte scantokey[128] =
320 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
321 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 ,//0
322 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' ,//1
323 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT ,'\\' ,'z' ,'x' ,'c' ,'v' ,//2
324 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 ,//3
325 K_F6 ,K_F7 ,K_F8 ,K_F9 ,K_F10,K_PAUSE,0 ,K_HOME,K_UPARROW,K_PGUP,K_KP_MINUS,K_LEFTARROW,K_KP_5,K_RIGHTARROW,K_KP_PLUS ,K_END,//4
326 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//5
327 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//6
328 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 //7
336 Map from windows to quake keynums
339 int MapKey (int key, int virtualkey)
342 int modified = (key >> 16) & 255;
343 qboolean is_extended = false;
345 if (modified < 128 && scantokey[modified])
346 result = scantokey[modified];
350 Con_DPrintf("key 0x%02x (0x%8x, 0x%8x) has no translation\n", modified, key, virtualkey);
367 return K_KP_LEFTARROW;
369 return K_KP_RIGHTARROW;
373 return K_KP_DOWNARROW;
400 ===================================================================
404 ===================================================================
412 void ClearAllStates (void)
416 // send an up event for each key, to make sure the server clears them all
417 for (i=0 ; i<256 ; i++)
419 Key_Event (i, false);
426 extern qboolean host_loopactive;
428 void AppActivate(BOOL fActive, BOOL minimize)
429 /****************************************************************************
431 * Function: AppActivate
432 * Parameters: fActive - True if app is activating
434 * Description: If the application is activating, then swap the system
435 * into SYSPAL_NOSTATIC mode so that our palettes will display
438 ****************************************************************************/
440 static BOOL sound_active;
442 vid_activewindow = fActive;
443 vid_hidden = minimize;
445 // enable/disable sound on focus gain/loss
446 if (!vid_activewindow && sound_active)
449 sound_active = false;
451 else if (vid_activewindow && !sound_active)
459 vid_allowhwgamma = true;
460 if (vid_isfullscreen)
462 if (vid_wassuspended)
464 vid_wassuspended = false;
465 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
466 ShowWindow(mainwindow, SW_SHOWNORMAL);
469 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
470 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
476 vid_allowhwgamma = false;
477 vid_usingmouse = false;
478 IN_DeactivateMouse ();
480 if (vid_isfullscreen)
482 ChangeDisplaySettings (NULL, 0);
483 vid_wassuspended = true;
485 VID_RestoreSystemGamma();
489 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
491 /* main window procedure */
492 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
495 int fActive, fMinimized, temp;
496 extern unsigned int uiWheelMessage;
498 if ( uMsg == uiWheelMessage )
499 uMsg = WM_MOUSEWHEEL;
504 if (vid_isfullscreen)
505 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
512 window_x = (int) LOWORD(lParam);
513 window_y = (int) HIWORD(lParam);
514 VID_UpdateWindowStatus ();
519 Key_Event (MapKey(lParam, wParam), true);
524 Key_Event (MapKey(lParam, wParam), false);
528 // keep Alt-Space from happening
531 // this is complicated because Win32 seems to pack multiple mouse events into
532 // one update sometimes, so we always check all states and look for events
542 if (wParam & MK_LBUTTON)
545 if (wParam & MK_RBUTTON)
548 if (wParam & MK_MBUTTON)
551 IN_MouseEvent (temp);
555 // JACK: This is the mouse wheel with the Intellimouse
556 // Its delta is either positive or neg, and we generate the proper
559 if ((short) HIWORD(wParam) > 0) {
560 Key_Event(K_MWHEELUP, true);
561 Key_Event(K_MWHEELUP, false);
563 Key_Event(K_MWHEELDOWN, true);
564 Key_Event(K_MWHEELDOWN, false);
572 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
578 fActive = LOWORD(wParam);
579 fMinimized = (BOOL) HIWORD(wParam);
580 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
582 // fix the leftover Alt from any Alt-Tab or the like that switched us away
588 // PostQuitMessage (0);
592 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
596 /* pass all unhandled messages to DefWindowProc */
597 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
601 /* return 1 if handled message, 0 if not */
605 int VID_SetGamma(unsigned short *ramps)
607 HDC hdc = GetDC (NULL);
608 int i = SetDeviceGammaRamp(hdc, ramps);
609 ReleaseDC (NULL, hdc);
610 return i; // return success or failure
613 int VID_GetGamma(unsigned short *ramps)
615 HDC hdc = GetDC (NULL);
616 int i = GetDeviceGammaRamp(hdc, ramps);
617 ReleaseDC (NULL, hdc);
618 return i; // return success or failure
621 static HINSTANCE gldll;
623 int GL_OpenLibrary(const char *name)
625 Con_Printf("Loading GL driver %s\n", name);
627 if (!(gldll = LoadLibrary(name)))
629 Con_Printf("Unable to LoadLibrary %s\n", name);
632 strcpy(gl_driver, name);
636 void GL_CloseLibrary(void)
641 qwglGetProcAddress = NULL;
644 gl_platformextensions = "";
647 void *GL_GetProcAddress(const char *name)
650 if (qwglGetProcAddress != NULL)
651 p = (void *) qwglGetProcAddress(name);
653 p = (void *) GetProcAddress(gldll, name);
657 static void IN_Init(void);
662 InitCommonControls();
663 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
665 // Register the frame class
667 wc.lpfnWndProc = (WNDPROC)MainWndProc;
670 wc.hInstance = global_hInstance;
672 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
673 wc.hbrBackground = NULL;
675 wc.lpszClassName = "DarkPlacesWindowClass";
677 if (!RegisterClass (&wc))
678 Sys_Error("Couldn't register window class\n");
683 int VID_InitMode (int fullscreen, int width, int height, int bpp, int stencil)
689 PIXELFORMATDESCRIPTOR pfd =
691 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
693 PFD_DRAW_TO_WINDOW // support window
694 | PFD_SUPPORT_OPENGL // support OpenGL
695 | PFD_DOUBLEBUFFER , // double buffered
696 PFD_TYPE_RGBA, // RGBA type
697 24, // 24-bit color depth
698 0, 0, 0, 0, 0, 0, // color bits ignored
699 0, // no alpha buffer
700 0, // shift bit ignored
701 0, // no accumulation buffer
702 0, 0, 0, 0, // accum bits ignored
703 32, // 32-bit z-buffer
704 0, // no stencil buffer
705 0, // no auxiliary buffer
706 PFD_MAIN_PLANE, // main layer
708 0, 0, 0 // layer masks ignored
711 DWORD WindowStyle, ExWindowStyle;
713 int CenterX, CenterY;
714 const char *gldrivername;
717 Sys_Error("VID_InitMode called when video is already initialised\n");
719 // if stencil is enabled, ask for alpha too
722 pfd.cStencilBits = 8;
727 pfd.cStencilBits = 0;
731 gldrivername = "opengl32.dll";
732 i = COM_CheckParm("-gl_driver");
733 if (i && i < com_argc - 1)
734 gldrivername = com_argv[i + 1];
735 if (!GL_OpenLibrary(gldrivername))
737 Con_Printf("Unable to load GL driver %s\n", gldrivername);
741 memset(&gdevmode, 0, sizeof(gdevmode));
743 vid_isfullscreen = false;
746 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
747 gdevmode.dmBitsPerPel = bpp;
748 gdevmode.dmPelsWidth = width;
749 gdevmode.dmPelsHeight = height;
750 gdevmode.dmSize = sizeof (gdevmode);
751 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
754 Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp);
758 vid_isfullscreen = true;
759 WindowStyle = WS_POPUP;
765 i = GetDeviceCaps(hdc, RASTERCAPS);
766 ReleaseDC (NULL, hdc);
770 Con_Printf ("Can't run in non-RGB mode\n");
774 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
781 rect.bottom = height;
782 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
791 CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
792 CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
794 CenterX = max(0, CenterX);
795 CenterY = max(0, CenterY);
797 // x and y may be changed by WM_MOVE messages
800 window_width = width;
801 window_height = height;
802 rect.left += CenterX;
803 rect.right += CenterX;
805 rect.bottom += CenterY;
807 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
810 Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %d, %p) failed\n", ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
817 SetWindowPos (mainwindow, NULL, CenterX, CenterY, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
820 ShowWindow (mainwindow, SW_SHOWDEFAULT);
821 UpdateWindow (mainwindow);
823 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
824 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
826 VID_UpdateWindowStatus ();
828 // now we try to make sure we get the focus on the mode switch, because
829 // sometimes in some systems we don't. We grab the foreground, then
830 // finish setting up, pump all our messages, and sleep for a little while
831 // to let messages finish bouncing around the system, then we put
832 // ourselves at the top of the z order, then grab the foreground again,
833 // Who knows if it helps, but it probably doesn't hurt
834 SetForegroundWindow (mainwindow);
836 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
838 TranslateMessage (&msg);
839 DispatchMessage (&msg);
844 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
846 SetForegroundWindow (mainwindow);
848 // fix the leftover Alt from any Alt-Tab or the like that switched us away
851 hdc = GetDC(mainwindow);
853 if ((pixelformat = ChoosePixelFormat(hdc, &pfd)) == 0)
856 Con_Printf("ChoosePixelFormat(%d, %p) failed\n", hdc, &pfd);
860 if (SetPixelFormat(hdc, pixelformat, &pfd) == false)
863 Con_Printf("SetPixelFormat(%d, %d, %p) failed\n", hdc, pixelformat, &pfd);
867 if (!GL_CheckExtension("wgl", wglfuncs, NULL, false))
870 Con_Printf("wgl functions not found\n");
874 baseRC = qwglCreateContext(hdc);
878 Con_Printf("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.\n");
881 if (!qwglMakeCurrent(hdc, baseRC))
884 Con_Printf("wglMakeCurrent(%d, %d) failed\n", hdc, baseRC);
888 qglGetString = GL_GetProcAddress("glGetString");
889 qwglGetExtensionsStringARB = GL_GetProcAddress("wglGetExtensionsStringARB");
890 if (qglGetString == NULL)
893 Con_Printf("glGetString not found\n");
896 gl_renderer = qglGetString(GL_RENDERER);
897 gl_vendor = qglGetString(GL_VENDOR);
898 gl_version = qglGetString(GL_VERSION);
899 gl_extensions = qglGetString(GL_EXTENSIONS);
901 gl_platformextensions = "";
903 if (qwglGetExtensionsStringARB)
904 gl_platformextensions = qwglGetExtensionsStringARB(hdc);
906 gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, NULL, false);
907 ReleaseDC(mainwindow, hdc);
911 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
912 if (strncasecmp(gl_vendor,"ATI",3)==0)
914 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
917 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
920 //vid_menudrawfn = VID_MenuDraw;
921 //vid_menukeyfn = VID_MenuKey;
923 vid_initialized = true;
926 IN_StartupJoystick ();
931 static void IN_Shutdown(void);
932 void VID_Shutdown (void)
937 VID_RestoreSystemGamma();
939 vid_initialized = false;
941 if (qwglGetCurrentContext)
942 hRC = qwglGetCurrentContext();
943 if (qwglGetCurrentDC)
944 hDC = qwglGetCurrentDC();
946 qwglMakeCurrent(NULL, NULL);
947 if (hRC && qwglDeleteContext)
948 qwglDeleteContext(hRC);
949 // close the library before we get rid of the window
951 if (hDC && mainwindow)
952 ReleaseDC(mainwindow, hDC);
953 if (vid_isfullscreen)
954 ChangeDisplaySettings (NULL, 0);
955 vid_isfullscreen = false;
956 AppActivate(false, false);
958 DestroyWindow(mainwindow);
968 void IN_ShowMouse (void)
970 if (!mouseshowtoggle)
983 void IN_HideMouse (void)
998 void IN_ActivateMouse (void)
1001 mouseactivatetoggle = true;
1003 if (mouseinitialized)
1009 if (!dinput_acquired)
1011 IDirectInputDevice_Acquire(g_pMouse);
1012 dinput_acquired = true;
1023 window_rect.left = window_x;
1024 window_rect.top = window_y;
1025 window_rect.right = window_x + window_width;
1026 window_rect.bottom = window_y + window_height;
1028 if (mouseparmsvalid)
1029 restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0);
1031 SetCursorPos (window_center_x, window_center_y);
1032 SetCapture (mainwindow);
1033 ClipCursor (&window_rect);
1046 void IN_DeactivateMouse (void)
1049 mouseactivatetoggle = false;
1051 if (mouseinitialized)
1057 if (dinput_acquired)
1059 IDirectInputDevice_Unacquire(g_pMouse);
1060 dinput_acquired = false;
1067 SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0);
1073 mouseactive = false;
1083 qboolean IN_InitDInput (void)
1086 DIPROPDWORD dipdw = {
1088 sizeof(DIPROPDWORD), // diph.dwSize
1089 sizeof(DIPROPHEADER), // diph.dwHeaderSize
1091 DIPH_DEVICE, // diph.dwHow
1093 DINPUT_BUFFERSIZE, // dwData
1098 hInstDI = LoadLibrary("dinput.dll");
1100 if (hInstDI == NULL)
1102 Con_SafePrintf ("Couldn't load dinput.dll\n");
1107 if (!pDirectInputCreate)
1109 pDirectInputCreate = (void *)GetProcAddress(hInstDI,"DirectInputCreateA");
1111 if (!pDirectInputCreate)
1113 Con_SafePrintf ("Couldn't get DI proc addr\n");
1118 // register with DirectInput and get an IDirectInput to play with.
1119 hr = iDirectInputCreate(global_hInstance, DIRECTINPUT_VERSION, &g_pdi, NULL);
1126 // obtain an interface to the system mouse device.
1127 hr = IDirectInput_CreateDevice(g_pdi, &GUID_SysMouse, &g_pMouse, NULL);
1131 Con_SafePrintf ("Couldn't open DI mouse device\n");
1135 // set the data format to "mouse format".
1136 hr = IDirectInputDevice_SetDataFormat(g_pMouse, &df);
1140 Con_SafePrintf ("Couldn't set DI mouse format\n");
1144 // set the cooperativity level.
1145 hr = IDirectInputDevice_SetCooperativeLevel(g_pMouse, mainwindow,
1146 DISCL_EXCLUSIVE | DISCL_FOREGROUND);
1150 Con_SafePrintf ("Couldn't set DI coop level\n");
1155 // set the buffer size to DINPUT_BUFFERSIZE elements.
1156 // the buffer size is a DWORD property associated with the device
1157 hr = IDirectInputDevice_SetProperty(g_pMouse, DIPROP_BUFFERSIZE, &dipdw.diph);
1161 Con_SafePrintf ("Couldn't set DI buffersize\n");
1174 void IN_StartupMouse (void)
1176 if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
1179 mouseinitialized = true;
1181 if (COM_CheckParm ("-dinput"))
1183 dinput = IN_InitDInput ();
1187 Con_SafePrintf ("DirectInput initialized\n");
1191 Con_SafePrintf ("DirectInput not initialized\n");
1197 mouseparmsvalid = SystemParametersInfo (SPI_GETMOUSE, 0, originalmouseparms, 0);
1199 if (mouseparmsvalid)
1201 if ( COM_CheckParm ("-noforcemspd") )
1202 newmouseparms[2] = originalmouseparms[2];
1204 if ( COM_CheckParm ("-noforcemaccel") )
1206 newmouseparms[0] = originalmouseparms[0];
1207 newmouseparms[1] = originalmouseparms[1];
1210 if ( COM_CheckParm ("-noforcemparms") )
1212 newmouseparms[0] = originalmouseparms[0];
1213 newmouseparms[1] = originalmouseparms[1];
1214 newmouseparms[2] = originalmouseparms[2];
1221 // if a fullscreen video mode was set before the mouse was initialized,
1222 // set the mouse state appropriately
1223 if (mouseactivatetoggle)
1224 IN_ActivateMouse ();
1233 void IN_MouseEvent (int mstate)
1237 if (mouseactive && !dinput)
1239 // perform button actions
1240 for (i=0 ; i<mouse_buttons ; i++)
1242 if ( (mstate & (1<<i)) &&
1243 !(mouse_oldbuttonstate & (1<<i)) )
1245 Key_Event (K_MOUSE1 + i, true);
1248 if ( !(mstate & (1<<i)) &&
1249 (mouse_oldbuttonstate & (1<<i)) )
1251 Key_Event (K_MOUSE1 + i, false);
1255 mouse_oldbuttonstate = mstate;
1265 void IN_MouseMove (usercmd_t *cmd)
1268 DIDEVICEOBJECTDATA od;
1274 GetCursorPos (¤t_pos);
1275 ui_mouseupdate(current_pos.x - window_x, current_pos.y - window_y);
1288 hr = IDirectInputDevice_GetDeviceData(g_pMouse,
1289 sizeof(DIDEVICEOBJECTDATA), &od, &dwElements, 0);
1291 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED))
1293 dinput_acquired = true;
1294 IDirectInputDevice_Acquire(g_pMouse);
1298 /* Unable to read data or no data available */
1299 if (FAILED(hr) || dwElements == 0)
1302 /* Look at the element to see what happened */
1314 case DIMOFS_BUTTON0:
1315 if (od.dwData & 0x80)
1321 case DIMOFS_BUTTON1:
1322 if (od.dwData & 0x80)
1323 mstate_di |= (1<<1);
1325 mstate_di &= ~(1<<1);
1328 case DIMOFS_BUTTON2:
1329 if (od.dwData & 0x80)
1330 mstate_di |= (1<<2);
1332 mstate_di &= ~(1<<2);
1337 // perform button actions
1338 for (i=0 ; i<mouse_buttons ; i++)
1340 if ( (mstate_di & (1<<i)) &&
1341 !(mouse_oldbuttonstate & (1<<i)) )
1343 Key_Event (K_MOUSE1 + i, true);
1346 if ( !(mstate_di & (1<<i)) &&
1347 (mouse_oldbuttonstate & (1<<i)) )
1349 Key_Event (K_MOUSE1 + i, false);
1353 mouse_oldbuttonstate = mstate_di;
1357 GetCursorPos (¤t_pos);
1358 mx = current_pos.x - window_center_x + mx_accum;
1359 my = current_pos.y - window_center_y + my_accum;
1364 IN_Mouse(cmd, mx, my);
1366 // if the mouse has moved, force it to the center, so there's room to move
1367 if (!dinput && (mx || my))
1368 SetCursorPos (window_center_x, window_center_y);
1377 void IN_Move (usercmd_t *cmd)
1379 if (vid_activewindow && !vid_hidden)
1392 void IN_Accumulate (void)
1398 GetCursorPos (¤t_pos);
1400 mx_accum += current_pos.x - window_center_x;
1401 my_accum += current_pos.y - window_center_y;
1403 // force the mouse to the center, so there's room to move
1404 SetCursorPos (window_center_x, window_center_y);
1415 void IN_ClearStates (void)
1421 mouse_oldbuttonstate = 0;
1431 void IN_StartupJoystick (void)
1438 // assume no joystick
1441 // abort startup if user requests no joystick
1442 if (COM_CheckParm ("-nojoy") || COM_CheckParm("-safe"))
1445 // verify joystick driver is present
1446 if ((numdevs = joyGetNumDevs ()) == 0)
1448 Con_Printf ("\njoystick not found -- driver not present\n\n");
1452 // cycle through the joystick ids for the first valid one
1453 for (joy_id=0 ; joy_id<numdevs ; joy_id++)
1455 memset (&ji, 0, sizeof(ji));
1456 ji.dwSize = sizeof(ji);
1457 ji.dwFlags = JOY_RETURNCENTERED;
1459 if ((mmr = joyGetPosEx (joy_id, &ji)) == JOYERR_NOERROR)
1463 // abort startup if we didn't find a valid joystick
1464 if (mmr != JOYERR_NOERROR)
1466 Con_Printf ("\njoystick not found -- no valid joysticks (%x)\n\n", mmr);
1470 // get the capabilities of the selected joystick
1471 // abort startup if command fails
1472 memset (&jc, 0, sizeof(jc));
1473 if ((mmr = joyGetDevCaps (joy_id, &jc, sizeof(jc))) != JOYERR_NOERROR)
1475 Con_Printf ("\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr);
1479 // save the joystick's number of buttons and POV status
1480 joy_numbuttons = jc.wNumButtons;
1481 joy_haspov = jc.wCaps & JOYCAPS_HASPOV;
1483 // old button and POV states default to no buttons pressed
1484 joy_oldbuttonstate = joy_oldpovstate = 0;
1486 // mark the joystick as available and advanced initialization not completed
1487 // this is needed as cvars are not available during initialization
1490 joy_advancedinit = false;
1492 Con_Printf ("\njoystick detected\n\n");
1501 PDWORD RawValuePointer (int axis)
1518 return NULL; // LordHavoc: hush compiler warning
1524 Joy_AdvancedUpdate_f
1527 void Joy_AdvancedUpdate_f (void)
1530 // called once by IN_ReadJoystick and by user whenever an update is needed
1531 // cvars are now available
1535 // initialize all the maps
1536 for (i = 0; i < JOY_MAX_AXES; i++)
1538 dwAxisMap[i] = AxisNada;
1539 dwControlMap[i] = JOY_ABSOLUTE_AXIS;
1540 pdwRawValue[i] = RawValuePointer(i);
1543 if( joy_advanced.integer == 0)
1545 // default joystick initialization
1546 // 2 axes only with joystick control
1547 dwAxisMap[JOY_AXIS_X] = AxisTurn;
1548 // dwControlMap[JOY_AXIS_X] = JOY_ABSOLUTE_AXIS;
1549 dwAxisMap[JOY_AXIS_Y] = AxisForward;
1550 // dwControlMap[JOY_AXIS_Y] = JOY_ABSOLUTE_AXIS;
1554 if (strcmp (joy_name.string, "joystick") != 0)
1556 // notify user of advanced controller
1557 Con_Printf ("\n%s configured\n\n", joy_name.string);
1560 // advanced initialization here
1561 // data supplied by user via joy_axisn cvars
1562 dwTemp = (DWORD) joy_advaxisx.value;
1563 dwAxisMap[JOY_AXIS_X] = dwTemp & 0x0000000f;
1564 dwControlMap[JOY_AXIS_X] = dwTemp & JOY_RELATIVE_AXIS;
1565 dwTemp = (DWORD) joy_advaxisy.value;
1566 dwAxisMap[JOY_AXIS_Y] = dwTemp & 0x0000000f;
1567 dwControlMap[JOY_AXIS_Y] = dwTemp & JOY_RELATIVE_AXIS;
1568 dwTemp = (DWORD) joy_advaxisz.value;
1569 dwAxisMap[JOY_AXIS_Z] = dwTemp & 0x0000000f;
1570 dwControlMap[JOY_AXIS_Z] = dwTemp & JOY_RELATIVE_AXIS;
1571 dwTemp = (DWORD) joy_advaxisr.value;
1572 dwAxisMap[JOY_AXIS_R] = dwTemp & 0x0000000f;
1573 dwControlMap[JOY_AXIS_R] = dwTemp & JOY_RELATIVE_AXIS;
1574 dwTemp = (DWORD) joy_advaxisu.value;
1575 dwAxisMap[JOY_AXIS_U] = dwTemp & 0x0000000f;
1576 dwControlMap[JOY_AXIS_U] = dwTemp & JOY_RELATIVE_AXIS;
1577 dwTemp = (DWORD) joy_advaxisv.value;
1578 dwAxisMap[JOY_AXIS_V] = dwTemp & 0x0000000f;
1579 dwControlMap[JOY_AXIS_V] = dwTemp & JOY_RELATIVE_AXIS;
1582 // compute the axes to collect from DirectInput
1583 joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
1584 for (i = 0; i < JOY_MAX_AXES; i++)
1586 if (dwAxisMap[i] != AxisNada)
1588 joy_flags |= dwAxisFlags[i];
1599 void IN_Commands (void)
1602 DWORD buttonstate, povstate;
1610 // loop through the joystick buttons
1611 // key a joystick event or auxillary event for higher number buttons for each state change
1612 buttonstate = ji.dwButtons;
1613 for (i=0 ; i < (int) joy_numbuttons ; i++)
1615 if ( (buttonstate & (1<<i)) && !(joy_oldbuttonstate & (1<<i)) )
1617 key_index = (i < 4) ? K_JOY1 : K_AUX1;
1618 Key_Event (key_index + i, true);
1621 if ( !(buttonstate & (1<<i)) && (joy_oldbuttonstate & (1<<i)) )
1623 key_index = (i < 4) ? K_JOY1 : K_AUX1;
1624 Key_Event (key_index + i, false);
1627 joy_oldbuttonstate = buttonstate;
1631 // convert POV information into 4 bits of state information
1632 // this avoids any potential problems related to moving from one
1633 // direction to another without going through the center position
1635 if(ji.dwPOV != JOY_POVCENTERED)
1637 if (ji.dwPOV == JOY_POVFORWARD)
1639 if (ji.dwPOV == JOY_POVRIGHT)
1641 if (ji.dwPOV == JOY_POVBACKWARD)
1643 if (ji.dwPOV == JOY_POVLEFT)
1646 // determine which bits have changed and key an auxillary event for each change
1647 for (i=0 ; i < 4 ; i++)
1649 if ( (povstate & (1<<i)) && !(joy_oldpovstate & (1<<i)) )
1651 Key_Event (K_AUX29 + i, true);
1654 if ( !(povstate & (1<<i)) && (joy_oldpovstate & (1<<i)) )
1656 Key_Event (K_AUX29 + i, false);
1659 joy_oldpovstate = povstate;
1669 qboolean IN_ReadJoystick (void)
1672 memset (&ji, 0, sizeof(ji));
1673 ji.dwSize = sizeof(ji);
1674 ji.dwFlags = joy_flags;
1676 if (joyGetPosEx (joy_id, &ji) == JOYERR_NOERROR)
1678 // this is a hack -- there is a bug in the Logitech WingMan Warrior DirectInput Driver
1679 // rather than having 32768 be the zero point, they have the zero point at 32668
1680 // go figure -- anyway, now we get the full resolution out of the device
1681 if (joy_wwhack1.integer != 0.0)
1689 // read error occurred
1690 // turning off the joystick seems too harsh for 1 read error,
1691 // but what should be done?
1702 void IN_JoyMove (usercmd_t *cmd)
1704 float speed, aspeed;
1705 float fAxisValue, fTemp;
1706 int i, mouselook = (in_mlook.state & 1) || freelook.integer;
1708 // complete initialization if first time in
1709 // this is needed as cvars are not available at initialization time
1710 if( joy_advancedinit != true )
1712 Joy_AdvancedUpdate_f();
1713 joy_advancedinit = true;
1716 // verify joystick is available and that the user wants to use it
1717 if (!joy_avail || !in_joystick.integer)
1722 // collect the joystick data, if possible
1723 if (IN_ReadJoystick () != true)
1728 if (in_speed.state & 1)
1729 speed = cl_movespeedkey.value;
1732 // LordHavoc: viewzoom affects sensitivity for sniping
1733 aspeed = speed * host_realframetime * cl.viewzoom;
1735 // loop through the axes
1736 for (i = 0; i < JOY_MAX_AXES; i++)
1738 // get the floating point zero-centered, potentially-inverted data for the current axis
1739 fAxisValue = (float) *pdwRawValue[i];
1740 // move centerpoint to zero
1741 fAxisValue -= 32768.0;
1743 if (joy_wwhack2.integer != 0.0)
1745 if (dwAxisMap[i] == AxisTurn)
1747 // this is a special formula for the Logitech WingMan Warrior
1748 // y=ax^b; where a = 300 and b = 1.3
1749 // also x values are in increments of 800 (so this is factored out)
1750 // then bounds check result to level out excessively high spin rates
1751 fTemp = 300.0 * pow(abs(fAxisValue) / 800.0, 1.3);
1752 if (fTemp > 14000.0)
1754 // restore direction information
1755 fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp;
1759 // convert range from -32768..32767 to -1..1
1760 fAxisValue /= 32768.0;
1762 switch (dwAxisMap[i])
1765 if ((joy_advanced.integer == 0) && mouselook)
1767 // user wants forward control to become look control
1768 if (fabs(fAxisValue) > joy_pitchthreshold.value)
1770 // if mouse invert is on, invert the joystick pitch value
1771 // only absolute control support here (joy_advanced is false)
1772 if (m_pitch.value < 0.0)
1774 cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1778 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1784 // no pitch movement
1785 // disable pitch return-to-center unless requested by user
1786 // *** this code can be removed when the lookspring bug is fixed
1787 // *** the bug always has the lookspring feature on
1788 if(lookspring.value == 0.0)
1794 // user wants forward control to be forward control
1795 if (fabs(fAxisValue) > joy_forwardthreshold.value)
1797 cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
1803 if (fabs(fAxisValue) > joy_sidethreshold.value)
1805 cmd->sidemove += (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value;
1810 if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
1812 // user wants turn control to become side control
1813 if (fabs(fAxisValue) > joy_sidethreshold.value)
1815 cmd->sidemove -= (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value;
1820 // user wants turn control to be turn control
1821 if (fabs(fAxisValue) > joy_yawthreshold.value)
1823 if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
1825 cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * aspeed * cl_yawspeed.value;
1829 cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * speed * 180.0;
1839 if (fabs(fAxisValue) > joy_pitchthreshold.value)
1841 // pitch movement detected and pitch movement desired by user
1842 if(dwControlMap[i] == JOY_ABSOLUTE_AXIS)
1844 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
1848 cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * speed * 180.0;
1854 // no pitch movement
1855 // disable pitch return-to-center unless requested by user
1856 // *** this code can be removed when the lookspring bug is fixed
1857 // *** the bug always has the lookspring feature on
1858 if(lookspring.integer == 0)
1870 static void IN_Init(void)
1872 uiWheelMessage = RegisterWindowMessage ( "MSWHEEL_ROLLMSG" );
1874 // joystick variables
1875 Cvar_RegisterVariable (&in_joystick);
1876 Cvar_RegisterVariable (&joy_name);
1877 Cvar_RegisterVariable (&joy_advanced);
1878 Cvar_RegisterVariable (&joy_advaxisx);
1879 Cvar_RegisterVariable (&joy_advaxisy);
1880 Cvar_RegisterVariable (&joy_advaxisz);
1881 Cvar_RegisterVariable (&joy_advaxisr);
1882 Cvar_RegisterVariable (&joy_advaxisu);
1883 Cvar_RegisterVariable (&joy_advaxisv);
1884 Cvar_RegisterVariable (&joy_forwardthreshold);
1885 Cvar_RegisterVariable (&joy_sidethreshold);
1886 Cvar_RegisterVariable (&joy_pitchthreshold);
1887 Cvar_RegisterVariable (&joy_yawthreshold);
1888 Cvar_RegisterVariable (&joy_forwardsensitivity);
1889 Cvar_RegisterVariable (&joy_sidesensitivity);
1890 Cvar_RegisterVariable (&joy_pitchsensitivity);
1891 Cvar_RegisterVariable (&joy_yawsensitivity);
1892 Cvar_RegisterVariable (&joy_wwhack1);
1893 Cvar_RegisterVariable (&joy_wwhack2);
1894 Cmd_AddCommand ("joyadvancedupdate", Joy_AdvancedUpdate_f);
1897 static void IN_Shutdown(void)
1899 IN_DeactivateMouse ();
1903 IDirectInputDevice_Release(g_pMouse);
1907 IDirectInput_Release(g_pdi);