quakespasm/Quake/in_sdl.c

490 lines
9.8 KiB
C
Raw Normal View History

/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2005 John Fitzgibbons and others
Copyright (C) 2007-2008 Kristian Duske
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "SDL.h"
static keydest_t prev_key_dest;
static qboolean no_mouse = false;
static int buttonremap[] =
{
K_MOUSE1,
K_MOUSE3, /* right button */
K_MOUSE2, /* middle button */
K_MWHEELUP,
K_MWHEELDOWN,
K_MOUSE4,
K_MOUSE5
};
/* mouse variables */
cvar_t m_filter = {"m_filter","0"};
/* total accumulated mouse movement since last frame,
* gets updated from the main game loop via IN_MouseMove */
static int total_dx, total_dy = 0;
static int FilterMouseEvents (const SDL_Event *event)
{
switch (event->type)
{
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
return 0;
}
return 1;
}
void IN_Activate (void)
{
if (no_mouse)
return;
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) != SDL_GRAB_ON)
{
SDL_WM_GrabInput(SDL_GRAB_ON);
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) != SDL_GRAB_ON)
Con_Printf("WARNING: SDL_WM_GrabInput(SDL_GRAB_ON) failed.\n");
}
if (SDL_ShowCursor(SDL_QUERY) != SDL_DISABLE)
{
SDL_ShowCursor(SDL_DISABLE);
if (SDL_ShowCursor(SDL_QUERY) != SDL_DISABLE)
Con_Printf("WARNING: SDL_ShowCursor(SDL_DISABLE) failed.\n");
}
if (SDL_GetEventFilter() != NULL)
SDL_SetEventFilter(NULL);
total_dx = 0;
total_dy = 0;
}
void IN_Deactivate (qboolean free_cursor)
{
if (no_mouse)
return;
if (free_cursor)
{
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) != SDL_GRAB_OFF)
{
SDL_WM_GrabInput(SDL_GRAB_OFF);
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) != SDL_GRAB_OFF)
Con_Printf("WARNING: SDL_WM_GrabInput(SDL_GRAB_OFF) failed.\n");
}
if (SDL_ShowCursor(SDL_QUERY) != SDL_ENABLE)
{
SDL_ShowCursor(SDL_ENABLE);
if (SDL_ShowCursor(SDL_QUERY) != SDL_ENABLE)
Con_Printf("WARNING: SDL_ShowCursor(SDL_ENABLE) failed.\n");
}
}
/* discard all mouse events when input is deactivated */
if (SDL_GetEventFilter() != FilterMouseEvents)
SDL_SetEventFilter(FilterMouseEvents);
}
void IN_Init (void)
{
SDL_EnableUNICODE (0); /* frame updates will change this as key_dest changes */
if (SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL) == -1)
Con_Printf("Warning: SDL_EnableKeyRepeat() failed.\n");
if (safemode || COM_CheckParm("-nomouse"))
{
no_mouse = true;
/* discard all mouse events when input is deactivated */
SDL_SetEventFilter(FilterMouseEvents);
}
IN_Activate();
}
void IN_Shutdown (void)
{
IN_Deactivate(true);
}
void IN_Commands (void)
{
/* TODO: implement this for joystick support */
}
extern cvar_t cl_maxpitch; /* johnfitz -- variable pitch clamping */
extern cvar_t cl_minpitch; /* johnfitz -- variable pitch clamping */
void IN_MouseMove(int dx, int dy)
{
total_dx += dx;
total_dy += dy;
}
void IN_Move (usercmd_t *cmd)
{
int dmx, dmy;
/* TODO: fix this
if (m_filter.value)
{
dmx = (2*mx - dmx) * 0.5;
dmy = (2*my - dmy) * 0.5;
}
*/
dmx = total_dx * sensitivity.value;
dmy = total_dy * sensitivity.value;
total_dx = 0;
total_dy = 0;
if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
cmd->sidemove += m_side.value * dmx;
else
cl.viewangles[YAW] -= m_yaw.value * dmx;
if (in_mlook.state & 1)
{
if (dmx || dmy)
V_StopPitchDrift ();
}
if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
{
cl.viewangles[PITCH] += m_pitch.value * dmy;
/* johnfitz -- variable pitch clamping */
if (cl.viewangles[PITCH] > cl_maxpitch.value)
cl.viewangles[PITCH] = cl_maxpitch.value;
if (cl.viewangles[PITCH] < cl_minpitch.value)
cl.viewangles[PITCH] = cl_minpitch.value;
}
else
{
if ((in_strafe.state & 1) && noclip_anglehack)
cmd->upmove -= m_forward.value * dmy;
else
cmd->forwardmove -= m_forward.value * dmy;
}
}
void IN_ClearStates (void)
{
}
void IN_SendKeyEvents (void)
{
SDL_Event event;
int sym, state;
int modstate;
if (key_dest != prev_key_dest)
{
SDL_EnableUNICODE((key_dest == key_console || key_dest == key_message));
Key_ClearStates();
prev_key_dest = key_dest;
}
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_ACTIVEEVENT:
if (event.active.state & (SDL_APPINPUTFOCUS|SDL_APPACTIVE))
{
if (event.active.gain)
{
/* Sys_Printf("FOCUS GAIN\n");*/
S_UnblockSound();
}
else
{
/* Sys_Printf("FOCUS LOSS\n");*/
S_BlockSound();
}
}
break;
case SDL_KEYDOWN:
if ((event.key.keysym.sym == SDLK_RETURN) &&
(event.key.keysym.mod & KMOD_ALT))
{
VID_Toggle();
break;
}
else if ((event.key.keysym.sym == SDLK_ESCAPE) &&
(event.key.keysym.mod & KMOD_SHIFT))
{
Con_ToggleConsole_f();
break;
}
case SDL_KEYUP:
sym = event.key.keysym.sym;
state = event.key.state;
modstate = SDL_GetModState();
switch (key_dest)
{
case key_game:
if ((event.key.keysym.unicode != 0) || (modstate & KMOD_SHIFT))
{ /* only use unicode for ~ and ` in game mode */
if ((event.key.keysym.unicode & 0xFF80) == 0)
{
if (((event.key.keysym.unicode & 0x7F) == '`') ||
((event.key.keysym.unicode & 0x7F) == '~') )
sym = event.key.keysym.unicode & 0x7F;
}
}
break;
case key_message:
case key_console:
if ((event.key.keysym.unicode != 0) || (modstate & KMOD_SHIFT))
{
#if defined(__QNX__)
if ((sym == SDLK_BACKSPACE) || (sym == SDLK_RETURN))
break; /* S.A: fixes QNX weirdness */
#endif /* __QNX__ */
if ((event.key.keysym.unicode & 0xFF80) == 0)
sym = event.key.keysym.unicode & 0x7F;
/* else: it's an international character */
}
/* printf("You pressed %s (%d) (%c)\n", SDL_GetKeyName(sym), sym, sym);*/
break;
default:
break;
}
switch (sym)
{
case SDLK_DELETE:
sym = K_DEL;
break;
case SDLK_BACKSPACE:
sym = K_BACKSPACE;
break;
case SDLK_F1:
sym = K_F1;
break;
case SDLK_F2:
sym = K_F2;
break;
case SDLK_F3:
sym = K_F3;
break;
case SDLK_F4:
sym = K_F4;
break;
case SDLK_F5:
sym = K_F5;
break;
case SDLK_F6:
sym = K_F6;
break;
case SDLK_F7:
sym = K_F7;
break;
case SDLK_F8:
sym = K_F8;
break;
case SDLK_F9:
sym = K_F9;
break;
case SDLK_F10:
sym = K_F10;
break;
case SDLK_F11:
sym = K_F11;
break;
case SDLK_F12:
sym = K_F12;
break;
case SDLK_BREAK:
case SDLK_PAUSE:
sym = K_PAUSE;
break;
case SDLK_UP:
sym = K_UPARROW;
break;
case SDLK_DOWN:
sym = K_DOWNARROW;
break;
case SDLK_RIGHT:
sym = K_RIGHTARROW;
break;
case SDLK_LEFT:
sym = K_LEFTARROW;
break;
case SDLK_INSERT:
sym = K_INS;
break;
case SDLK_HOME:
sym = K_HOME;
break;
case SDLK_END:
sym = K_END;
break;
case SDLK_PAGEUP:
sym = K_PGUP;
break;
case SDLK_PAGEDOWN:
sym = K_PGDN;
break;
case SDLK_RSHIFT:
case SDLK_LSHIFT:
sym = K_SHIFT;
break;
case SDLK_RCTRL:
case SDLK_LCTRL:
sym = K_CTRL;
break;
case SDLK_RALT:
case SDLK_LALT:
sym = K_ALT;
break;
case SDLK_KP0:
if (modstate & KMOD_NUM)
sym = K_INS;
else
sym = SDLK_0;
break;
case SDLK_KP1:
if (modstate & KMOD_NUM)
sym = K_END;
else
sym = SDLK_1;
break;
case SDLK_KP2:
if (modstate & KMOD_NUM)
sym = K_DOWNARROW;
else
sym = SDLK_2;
break;
case SDLK_KP3:
if (modstate & KMOD_NUM)
sym = K_PGDN;
else
sym = SDLK_3;
break;
case SDLK_KP4:
if (modstate & KMOD_NUM)
sym = K_LEFTARROW;
else
sym = SDLK_4;
break;
case SDLK_KP5:
sym = SDLK_5;
break;
case SDLK_KP6:
if (modstate & KMOD_NUM)
sym = K_RIGHTARROW;
else
sym = SDLK_6;
break;
case SDLK_KP7:
if (modstate & KMOD_NUM)
sym = K_HOME;
else
sym = SDLK_7;
break;
case SDLK_KP8:
if (modstate & KMOD_NUM)
sym = K_UPARROW;
else
sym = SDLK_8;
break;
case SDLK_KP9:
if (modstate & KMOD_NUM)
sym = K_PGUP;
else
sym = SDLK_9;
break;
case SDLK_KP_PERIOD:
if (modstate & KMOD_NUM)
sym = K_DEL;
else
sym = SDLK_PERIOD;
break;
case SDLK_KP_DIVIDE:
sym = SDLK_SLASH;
break;
case SDLK_KP_MULTIPLY:
sym = SDLK_ASTERISK;
break;
case SDLK_KP_MINUS:
sym = SDLK_MINUS;
break;
case SDLK_KP_PLUS:
sym = SDLK_PLUS;
break;
case SDLK_KP_ENTER:
sym = SDLK_RETURN;
break;
case SDLK_KP_EQUALS:
sym = SDLK_EQUALS;
break;
case 178: /* the '<27>' key */
sym = '~';
break;
}
/* If we're not directly handled and still
* above 255, just force it to 0 */
if (sym > 255)
sym = 0;
Key_Event (sym, state);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (event.button.button < 1 ||
event.button.button > sizeof(buttonremap) / sizeof(buttonremap[0]))
{
Con_Printf ("Ignored event for mouse button %d\n",
event.button.button);
break;
}
Key_Event(buttonremap[event.button.button - 1], event.button.state == SDL_PRESSED);
break;
case SDL_MOUSEMOTION:
IN_MouseMove(event.motion.xrel, event.motion.yrel);
break;
case SDL_QUIT:
CL_Disconnect ();
Sys_Quit ();
break;
default:
break;
}
}
}