2018-04-17 02:53:01 +02:00
|
|
|
/*
|
|
|
|
events.c - SDL event system handlers
|
|
|
|
Copyright (C) 2015-2017 a1batross
|
|
|
|
|
|
|
|
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 3 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.
|
|
|
|
*/
|
2019-11-24 01:52:08 +01:00
|
|
|
#if defined( XASH_SDL ) && !XASH_DEDICATED
|
2018-04-17 02:53:01 +02:00
|
|
|
#include <SDL.h>
|
2019-10-26 03:36:43 +02:00
|
|
|
#include <ctype.h>
|
2018-04-17 02:53:01 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "keydefs.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "vgui_draw.h"
|
|
|
|
#include "events.h"
|
|
|
|
#include "sound.h"
|
|
|
|
#include "vid_common.h"
|
|
|
|
|
2019-10-25 16:11:27 +02:00
|
|
|
#if ! SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
#define SDL_SCANCODE_A SDLK_a
|
|
|
|
#define SDL_SCANCODE_Z SDLK_z
|
|
|
|
#define SDL_SCANCODE_1 SDLK_1
|
|
|
|
#define SDL_SCANCODE_9 SDLK_9
|
|
|
|
#define SDL_SCANCODE_F1 SDLK_F1
|
|
|
|
#define SDL_SCANCODE_F12 SDLK_F12
|
|
|
|
#define SDL_SCANCODE_GRAVE SDLK_BACKQUOTE
|
|
|
|
#define SDL_SCANCODE_0 SDLK_0
|
|
|
|
#define SDL_SCANCODE_BACKSLASH SDLK_BACKSLASH
|
|
|
|
#define SDL_SCANCODE_LEFTBRACKET SDLK_LEFTBRACKET
|
|
|
|
#define SDL_SCANCODE_RIGHTBRACKET SDLK_RIGHTBRACKET
|
2019-10-26 03:36:43 +02:00
|
|
|
#define SDL_SCANCODE_EQUALS SDLK_EQUALS
|
|
|
|
#define SDL_SCANCODE_MINUS SDLK_MINUS
|
|
|
|
#define SDL_SCANCODE_TAB SDLK_TAB
|
|
|
|
#define SDL_SCANCODE_RETURN SDLK_RETURN
|
|
|
|
#define SDL_SCANCODE_ESCAPE SDLK_ESCAPE
|
|
|
|
#define SDL_SCANCODE_SPACE SDLK_SPACE
|
|
|
|
#define SDL_SCANCODE_BACKSPACE SDLK_BACKSPACE
|
|
|
|
#define SDL_SCANCODE_UP SDLK_UP
|
|
|
|
#define SDL_SCANCODE_LEFT SDLK_LEFT
|
|
|
|
#define SDL_SCANCODE_DOWN SDLK_DOWN
|
|
|
|
#define SDL_SCANCODE_RIGHT SDLK_RIGHT
|
|
|
|
#define SDL_SCANCODE_LALT SDLK_LALT
|
|
|
|
#define SDL_SCANCODE_RALT SDLK_RALT
|
|
|
|
#define SDL_SCANCODE_LCTRL SDLK_LCTRL
|
|
|
|
#define SDL_SCANCODE_RCTRL SDLK_RCTRL
|
|
|
|
#define SDL_SCANCODE_LSHIFT SDLK_LSHIFT
|
|
|
|
#define SDL_SCANCODE_RSHIFT SDLK_RSHIFT
|
|
|
|
#define SDL_SCANCODE_LGUI SDLK_LMETA
|
|
|
|
#define SDL_SCANCODE_RGUI SDLK_RMETA
|
|
|
|
#define SDL_SCANCODE_INSERT SDLK_INSERT
|
|
|
|
#define SDL_SCANCODE_DELETE SDLK_DELETE
|
|
|
|
#define SDL_SCANCODE_PAGEDOWN SDLK_PAGEDOWN
|
|
|
|
#define SDL_SCANCODE_PAGEUP SDLK_PAGEUP
|
|
|
|
#define SDL_SCANCODE_HOME SDLK_HOME
|
|
|
|
#define SDL_SCANCODE_END SDLK_END
|
|
|
|
#define SDL_SCANCODE_KP_1 SDLK_KP1
|
|
|
|
#define SDL_SCANCODE_KP_2 SDLK_KP2
|
|
|
|
#define SDL_SCANCODE_KP_3 SDLK_KP3
|
|
|
|
#define SDL_SCANCODE_KP_4 SDLK_KP4
|
|
|
|
#define SDL_SCANCODE_KP_5 SDLK_KP5
|
|
|
|
#define SDL_SCANCODE_KP_6 SDLK_KP6
|
|
|
|
#define SDL_SCANCODE_KP_7 SDLK_KP7
|
|
|
|
#define SDL_SCANCODE_KP_8 SDLK_KP8
|
|
|
|
#define SDL_SCANCODE_KP_9 SDLK_KP9
|
|
|
|
#define SDL_SCANCODE_KP_0 SDLK_KP0
|
|
|
|
#define SDL_SCANCODE_KP_PERIOD SDLK_KP_PERIOD
|
|
|
|
#define SDL_SCANCODE_KP_ENTER SDLK_KP_ENTER
|
|
|
|
#define SDL_SCANCODE_KP_PLUS SDLK_KP_PLUS
|
|
|
|
#define SDL_SCANCODE_KP_MINUS SDLK_KP_MINUS
|
|
|
|
#define SDL_SCANCODE_KP_DIVIDE SDLK_KP_DIVIDE
|
|
|
|
#define SDL_SCANCODE_KP_MULTIPLY SDLK_KP_MULTIPLY
|
|
|
|
#define SDL_SCANCODE_NUMLOCKCLEAR SDLK_NUMLOCK
|
|
|
|
#define SDL_SCANCODE_CAPSLOCK SDLK_CAPSLOCK
|
|
|
|
#define SDL_SCANCODE_SLASH SDLK_SLASH
|
|
|
|
#define SDL_SCANCODE_PERIOD SDLK_PERIOD
|
|
|
|
#define SDL_SCANCODE_SEMICOLON SDLK_SEMICOLON
|
|
|
|
#define SDL_SCANCODE_APOSTROPHE SDLK_QUOTE
|
|
|
|
#define SDL_SCANCODE_COMMA SDLK_COMMA
|
|
|
|
#define SDL_SCANCODE_PRINTSCREEN SDLK_PRINT
|
|
|
|
#define SDL_SCANCODE_UNKNOWN SDLK_UNKNOWN
|
|
|
|
#define SDL_GetScancodeName( x ) "unknown"
|
2021-11-27 03:26:09 +01:00
|
|
|
#define SDL_JoystickID Uint8
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif
|
|
|
|
|
2023-02-09 04:31:19 +01:00
|
|
|
static int SDLash_GameControllerButtonMapping[] =
|
|
|
|
{
|
|
|
|
#if XASH_NSWITCH // devkitPro/SDL has inverted Nintendo layout for SDL_GameController
|
|
|
|
K_B_BUTTON, K_A_BUTTON, K_Y_BUTTON, K_X_BUTTON,
|
|
|
|
#else
|
|
|
|
K_A_BUTTON, K_B_BUTTON, K_X_BUTTON, K_Y_BUTTON,
|
|
|
|
#endif
|
|
|
|
K_BACK_BUTTON, K_MODE_BUTTON, K_START_BUTTON,
|
|
|
|
K_LSTICK, K_RSTICK,
|
|
|
|
K_L1_BUTTON, K_R1_BUTTON,
|
|
|
|
K_DPAD_UP, K_DPAD_DOWN, K_DPAD_LEFT, K_DPAD_RIGHT,
|
|
|
|
K_MISC_BUTTON,
|
|
|
|
K_PADDLE1_BUTTON, K_PADDLE2_BUTTON, K_PADDLE3_BUTTON, K_PADDLE4_BUTTON,
|
|
|
|
K_TOUCHPAD,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Swap axis to follow default axis binding:
|
|
|
|
// LeftX, LeftY, RightX, RightY, TriggerRight, TriggerLeft
|
|
|
|
static int SDLash_GameControllerAxisMapping[] =
|
|
|
|
{
|
|
|
|
JOY_AXIS_SIDE, // SDL_CONTROLLER_AXIS_LEFTX,
|
|
|
|
JOY_AXIS_FWD, // SDL_CONTROLLER_AXIS_LEFTY,
|
|
|
|
JOY_AXIS_PITCH, // SDL_CONTROLLER_AXIS_RIGHTX,
|
|
|
|
JOY_AXIS_YAW, // SDL_CONTROLLER_AXIS_RIGHTY,
|
|
|
|
JOY_AXIS_LT, // SDL_CONTROLLER_AXIS_TRIGGERLEFT,
|
|
|
|
JOY_AXIS_RT, // SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
|
|
|
|
};
|
|
|
|
|
2021-03-01 19:48:31 +01:00
|
|
|
static qboolean SDLash_IsInstanceIDAGameController( SDL_JoystickID joyId )
|
|
|
|
{
|
|
|
|
#if !SDL_VERSION_ATLEAST( 2, 0, 4 )
|
|
|
|
// HACKHACK: if we're not initialized g_joy, then we're probably using gamecontroller api
|
|
|
|
// so return true
|
|
|
|
if( !g_joy )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if( SDL_GameControllerFromInstanceID( joyId ) )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:53:01 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_KeyEvent
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
2018-04-26 01:52:49 +02:00
|
|
|
static void SDLash_KeyEvent( SDL_KeyboardEvent key )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
2018-04-26 01:52:49 +02:00
|
|
|
int down = key.state != SDL_RELEASED;
|
2019-10-26 03:36:43 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-04-17 02:53:01 +02:00
|
|
|
int keynum = key.keysym.scancode;
|
2019-10-26 03:36:43 +02:00
|
|
|
#else
|
|
|
|
int keynum = key.keysym.sym;
|
|
|
|
#endif
|
2018-04-17 02:53:01 +02:00
|
|
|
qboolean numLock = SDL_GetModState() & KMOD_NUM;
|
|
|
|
|
2022-04-22 19:37:01 +02:00
|
|
|
if( SDL_IsTextInputActive() && down && cls.key_dest != key_game )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
|
|
|
if( SDL_GetModState() & KMOD_CTRL )
|
|
|
|
{
|
|
|
|
if( keynum >= SDL_SCANCODE_A && keynum <= SDL_SCANCODE_Z )
|
|
|
|
{
|
|
|
|
keynum = keynum - SDL_SCANCODE_A + 1;
|
|
|
|
CL_CharEvent( keynum );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-10-26 03:36:43 +02:00
|
|
|
|
|
|
|
#if !SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
if( keynum >= SDLK_KP0 && keynum <= SDLK_KP9 )
|
|
|
|
keynum -= SDLK_KP0 + '0';
|
|
|
|
|
|
|
|
if( isprint( keynum ) )
|
|
|
|
{
|
|
|
|
if( SDL_GetModState() & KMOD_SHIFT )
|
|
|
|
{
|
|
|
|
keynum = Key_ToUpper( keynum );
|
|
|
|
}
|
|
|
|
|
|
|
|
CL_CharEvent( keynum );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DECLARE_KEY_RANGE( min, max, repl ) \
|
|
|
|
if( keynum >= (min) && keynum <= (max) ) \
|
|
|
|
{ \
|
|
|
|
keynum = keynum - (min) + (repl); \
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_KEY_RANGE( SDL_SCANCODE_A, SDL_SCANCODE_Z, 'a' )
|
|
|
|
else DECLARE_KEY_RANGE( SDL_SCANCODE_1, SDL_SCANCODE_9, '1' )
|
|
|
|
else DECLARE_KEY_RANGE( SDL_SCANCODE_F1, SDL_SCANCODE_F12, K_F1 )
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch( keynum )
|
|
|
|
{
|
|
|
|
case SDL_SCANCODE_GRAVE: keynum = '`'; break;
|
|
|
|
case SDL_SCANCODE_0: keynum = '0'; break;
|
|
|
|
case SDL_SCANCODE_BACKSLASH: keynum = '\\'; break;
|
|
|
|
case SDL_SCANCODE_LEFTBRACKET: keynum = '['; break;
|
|
|
|
case SDL_SCANCODE_RIGHTBRACKET: keynum = ']'; break;
|
|
|
|
case SDL_SCANCODE_EQUALS: keynum = '='; break;
|
|
|
|
case SDL_SCANCODE_MINUS: keynum = '-'; break;
|
|
|
|
case SDL_SCANCODE_TAB: keynum = K_TAB; break;
|
|
|
|
case SDL_SCANCODE_RETURN: keynum = K_ENTER; break;
|
|
|
|
case SDL_SCANCODE_ESCAPE: keynum = K_ESCAPE; break;
|
|
|
|
case SDL_SCANCODE_SPACE: keynum = K_SPACE; break;
|
|
|
|
case SDL_SCANCODE_BACKSPACE: keynum = K_BACKSPACE; break;
|
|
|
|
case SDL_SCANCODE_UP: keynum = K_UPARROW; break;
|
|
|
|
case SDL_SCANCODE_LEFT: keynum = K_LEFTARROW; break;
|
|
|
|
case SDL_SCANCODE_DOWN: keynum = K_DOWNARROW; break;
|
|
|
|
case SDL_SCANCODE_RIGHT: keynum = K_RIGHTARROW; break;
|
|
|
|
case SDL_SCANCODE_LALT:
|
|
|
|
case SDL_SCANCODE_RALT: keynum = K_ALT; break;
|
|
|
|
case SDL_SCANCODE_LCTRL:
|
|
|
|
case SDL_SCANCODE_RCTRL: keynum = K_CTRL; break;
|
|
|
|
case SDL_SCANCODE_LSHIFT:
|
|
|
|
case SDL_SCANCODE_RSHIFT: keynum = K_SHIFT; break;
|
|
|
|
case SDL_SCANCODE_LGUI:
|
|
|
|
case SDL_SCANCODE_RGUI: keynum = K_WIN; break;
|
|
|
|
case SDL_SCANCODE_INSERT: keynum = K_INS; break;
|
|
|
|
case SDL_SCANCODE_DELETE: keynum = K_DEL; break;
|
|
|
|
case SDL_SCANCODE_PAGEDOWN: keynum = K_PGDN; break;
|
|
|
|
case SDL_SCANCODE_PAGEUP: keynum = K_PGUP; break;
|
|
|
|
case SDL_SCANCODE_HOME: keynum = K_HOME; break;
|
|
|
|
case SDL_SCANCODE_END: keynum = K_END; break;
|
|
|
|
case SDL_SCANCODE_KP_1: keynum = numLock ? '1' : K_KP_END; break;
|
|
|
|
case SDL_SCANCODE_KP_2: keynum = numLock ? '2' : K_KP_DOWNARROW; break;
|
|
|
|
case SDL_SCANCODE_KP_3: keynum = numLock ? '3' : K_KP_PGDN; break;
|
|
|
|
case SDL_SCANCODE_KP_4: keynum = numLock ? '4' : K_KP_LEFTARROW; break;
|
|
|
|
case SDL_SCANCODE_KP_5: keynum = numLock ? '5' : K_KP_5; break;
|
|
|
|
case SDL_SCANCODE_KP_6: keynum = numLock ? '6' : K_KP_RIGHTARROW; break;
|
|
|
|
case SDL_SCANCODE_KP_7: keynum = numLock ? '7' : K_KP_HOME; break;
|
|
|
|
case SDL_SCANCODE_KP_8: keynum = numLock ? '8' : K_KP_UPARROW; break;
|
|
|
|
case SDL_SCANCODE_KP_9: keynum = numLock ? '9' : K_KP_PGUP; break;
|
|
|
|
case SDL_SCANCODE_KP_0: keynum = numLock ? '0' : K_KP_INS; break;
|
|
|
|
case SDL_SCANCODE_KP_PERIOD: keynum = K_KP_DEL; break;
|
|
|
|
case SDL_SCANCODE_KP_ENTER: keynum = K_KP_ENTER; break;
|
|
|
|
case SDL_SCANCODE_KP_PLUS: keynum = K_KP_PLUS; break;
|
|
|
|
case SDL_SCANCODE_KP_MINUS: keynum = K_KP_MINUS; break;
|
|
|
|
case SDL_SCANCODE_KP_DIVIDE: keynum = K_KP_SLASH; break;
|
|
|
|
case SDL_SCANCODE_KP_MULTIPLY: keynum = '*'; break;
|
|
|
|
case SDL_SCANCODE_NUMLOCKCLEAR: keynum = K_KP_NUMLOCK; break;
|
|
|
|
case SDL_SCANCODE_CAPSLOCK: keynum = K_CAPSLOCK; break;
|
|
|
|
case SDL_SCANCODE_SLASH: keynum = '/'; break;
|
|
|
|
case SDL_SCANCODE_PERIOD: keynum = '.'; break;
|
|
|
|
case SDL_SCANCODE_SEMICOLON: keynum = ';'; break;
|
|
|
|
case SDL_SCANCODE_APOSTROPHE: keynum = '\''; break;
|
|
|
|
case SDL_SCANCODE_COMMA: keynum = ','; break;
|
|
|
|
case SDL_SCANCODE_PRINTSCREEN:
|
|
|
|
{
|
|
|
|
host.force_draw_version = true;
|
|
|
|
host.force_draw_version_time = host.realtime + FORCE_DRAW_VERSION_TIME;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-06 09:50:38 +01:00
|
|
|
case SDL_SCANCODE_PAUSE: keynum = K_PAUSE; break;
|
|
|
|
case SDL_SCANCODE_SCROLLLOCK: keynum = K_SCROLLOCK; break;
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2019-10-26 03:36:43 +02:00
|
|
|
case SDL_SCANCODE_APPLICATION: keynum = K_WIN; break; // (compose key) ???
|
2022-12-06 09:50:38 +01:00
|
|
|
// don't console spam on known functional buttons, not used in engine
|
2018-04-17 02:53:01 +02:00
|
|
|
case SDL_SCANCODE_MUTE:
|
|
|
|
case SDL_SCANCODE_VOLUMEUP:
|
|
|
|
case SDL_SCANCODE_VOLUMEDOWN:
|
|
|
|
case SDL_SCANCODE_BRIGHTNESSDOWN:
|
|
|
|
case SDL_SCANCODE_BRIGHTNESSUP:
|
|
|
|
return;
|
2019-10-26 03:36:43 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-04-17 02:53:01 +02:00
|
|
|
case SDL_SCANCODE_UNKNOWN:
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
if( down ) Con_Reportf( "SDLash_KeyEvent: Unknown scancode\n" );
|
2018-04-17 02:53:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
2018-11-16 13:25:04 +01:00
|
|
|
if( down ) Con_Reportf( "SDLash_KeyEvent: Unknown key: %s = %i\n", SDL_GetScancodeName( keynum ), keynum );
|
2018-04-17 02:53:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 11:43:17 +02:00
|
|
|
|
|
|
|
#undef DECLARE_KEY_RANGE
|
2018-04-17 02:53:01 +02:00
|
|
|
|
|
|
|
Key_Event( keynum, down );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_MouseEvent
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static void SDLash_MouseEvent( SDL_MouseButtonEvent button )
|
|
|
|
{
|
2022-10-12 04:18:19 +02:00
|
|
|
int down;
|
2019-09-26 21:14:47 +02:00
|
|
|
|
2022-01-04 06:26:41 +01:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2021-06-02 16:42:24 +02:00
|
|
|
if( button.which == SDL_TOUCH_MOUSEID )
|
|
|
|
return;
|
2022-01-04 06:26:41 +01:00
|
|
|
#endif
|
2019-10-26 03:36:43 +02:00
|
|
|
|
2022-10-12 04:18:19 +02:00
|
|
|
if( button.state == SDL_RELEASED )
|
|
|
|
down = 0;
|
|
|
|
else if( button.clicks >= 2 )
|
|
|
|
down = 2; // special state for double-click in UI
|
|
|
|
else
|
|
|
|
down = 1;
|
|
|
|
|
2019-10-26 03:36:43 +02:00
|
|
|
switch( button.button )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
2019-10-26 03:36:43 +02:00
|
|
|
case SDL_BUTTON_LEFT:
|
2022-01-13 13:52:59 +01:00
|
|
|
IN_MouseEvent( 0, down );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
2022-04-10 18:39:57 +02:00
|
|
|
case SDL_BUTTON_RIGHT:
|
2022-01-13 13:52:59 +01:00
|
|
|
IN_MouseEvent( 1, down );
|
2021-06-02 16:42:24 +02:00
|
|
|
break;
|
2022-04-10 18:39:57 +02:00
|
|
|
case SDL_BUTTON_MIDDLE:
|
2022-01-13 13:52:59 +01:00
|
|
|
IN_MouseEvent( 2, down );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X1:
|
2022-01-13 13:52:59 +01:00
|
|
|
IN_MouseEvent( 3, down );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X2:
|
2022-01-13 13:52:59 +01:00
|
|
|
IN_MouseEvent( 4, down );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
#if ! SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
case SDL_BUTTON_WHEELUP:
|
2022-10-12 04:18:19 +02:00
|
|
|
IN_MWheelEvent( -1 );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
case SDL_BUTTON_WHEELDOWN:
|
2022-10-12 04:18:19 +02:00
|
|
|
IN_MWheelEvent( 1 );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
#endif // ! SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
default:
|
|
|
|
Con_Printf( "Unknown mouse button ID: %d\n", button.button );
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_InputEvent
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
2019-10-26 03:36:43 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-04-17 02:53:01 +02:00
|
|
|
static void SDLash_InputEvent( SDL_TextInputEvent input )
|
|
|
|
{
|
2018-11-18 21:04:47 +01:00
|
|
|
char *text;
|
2022-04-27 20:32:20 +02:00
|
|
|
VGui_ReportTextInput( input.text );
|
2018-11-18 21:04:47 +01:00
|
|
|
for( text = input.text; *text; text++ )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if( !Q_stricmp( cl_charset->string, "utf-8" ) )
|
2018-04-22 13:01:56 +02:00
|
|
|
ch = (unsigned char)*text;
|
2018-04-17 02:53:01 +02:00
|
|
|
else
|
2018-04-22 13:01:56 +02:00
|
|
|
ch = Con_UtfProcessCharForce( (unsigned char)*text );
|
2018-04-17 02:53:01 +02:00
|
|
|
|
|
|
|
if( !ch )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CL_CharEvent( ch );
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 03:36:43 +02:00
|
|
|
#endif // SDL_VERSION_AT_LEAST( 2, 0, 0 )
|
|
|
|
|
|
|
|
static void SDLash_ActiveEvent( int gain )
|
|
|
|
{
|
|
|
|
if( gain )
|
|
|
|
{
|
|
|
|
host.status = HOST_FRAME;
|
2022-01-27 23:20:14 +01:00
|
|
|
if( cls.key_dest == key_game )
|
|
|
|
{
|
|
|
|
IN_ActivateMouse( );
|
|
|
|
}
|
|
|
|
|
2021-03-09 22:03:12 +01:00
|
|
|
if( dma.initialized && snd_mute_losefocus.value )
|
2019-10-26 03:36:43 +02:00
|
|
|
{
|
|
|
|
SNDDMA_Activate( true );
|
|
|
|
}
|
|
|
|
host.force_draw_version = true;
|
|
|
|
host.force_draw_version_time = host.realtime + FORCE_DRAW_VERSION_TIME;
|
|
|
|
if( vid_fullscreen->value )
|
|
|
|
VID_SetMode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
{
|
|
|
|
// Keep running if ftp server enabled
|
|
|
|
void IOS_StartBackgroundTask( void );
|
|
|
|
IOS_StartBackgroundTask();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
host.status = HOST_NOFOCUS;
|
2022-01-27 23:20:14 +01:00
|
|
|
if( cls.key_dest == key_game )
|
|
|
|
{
|
|
|
|
IN_DeactivateMouse();
|
|
|
|
}
|
|
|
|
|
2021-03-09 22:03:12 +01:00
|
|
|
if( dma.initialized && snd_mute_losefocus.value )
|
2019-10-26 03:36:43 +02:00
|
|
|
{
|
|
|
|
SNDDMA_Activate( false );
|
|
|
|
}
|
|
|
|
host.force_draw_version = true;
|
|
|
|
host.force_draw_version_time = host.realtime + 2;
|
|
|
|
VID_RestoreScreenResolution();
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 02:53:01 +02:00
|
|
|
|
2021-01-02 03:08:32 +01:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
static size_t num_open_game_controllers = 0;
|
|
|
|
|
|
|
|
static void SDLash_GameController_Add( int index )
|
|
|
|
{
|
|
|
|
extern convar_t *joy_enable; // private to input system
|
|
|
|
SDL_GameController *controller;
|
|
|
|
if( !joy_enable->value )
|
|
|
|
return;
|
|
|
|
controller = SDL_GameControllerOpen( index );
|
|
|
|
if( !controller )
|
|
|
|
{
|
|
|
|
Con_Reportf( "Failed to open SDL GameController %d: %s\n", index, SDL_GetError( ) );
|
|
|
|
SDL_ClearError( );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 6 )
|
|
|
|
Con_Reportf( "Added controller: %s (%i:%i:%i)\n",
|
|
|
|
SDL_GameControllerName( controller ),
|
|
|
|
SDL_GameControllerGetVendor( controller ),
|
|
|
|
SDL_GameControllerGetProduct( controller ),
|
|
|
|
SDL_GameControllerGetProductVersion( controller ));
|
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 6 )
|
|
|
|
|
|
|
|
++num_open_game_controllers;
|
|
|
|
if( num_open_game_controllers == 1 )
|
|
|
|
Joy_AddEvent( );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SDLash_GameController_Remove( SDL_JoystickID joystick_id )
|
|
|
|
{
|
|
|
|
Con_Reportf( "Removed controller %i\n", joystick_id );
|
|
|
|
|
|
|
|
// `Joy_RemoveEvent` sets `joy_found` to `0`.
|
|
|
|
// We only want to do this when all the game controllers have been removed.
|
|
|
|
--num_open_game_controllers;
|
|
|
|
if( num_open_game_controllers == 0 )
|
|
|
|
Joy_RemoveEvent( );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-17 02:53:01 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_EventFilter
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static void SDLash_EventFilter( SDL_Event *event )
|
|
|
|
{
|
|
|
|
switch ( event->type )
|
|
|
|
{
|
|
|
|
/* Mouse events */
|
|
|
|
case SDL_MOUSEMOTION:
|
2022-01-04 06:19:57 +01:00
|
|
|
if( host.mouse_visible )
|
|
|
|
SDL_GetRelativeMouseState( NULL, NULL );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
SDLash_MouseEvent( event->button );
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Keyboard events */
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
2018-04-26 01:52:49 +02:00
|
|
|
SDLash_KeyEvent( event->key );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
|
2019-10-26 03:36:43 +02:00
|
|
|
/* Joystick events */
|
|
|
|
case SDL_JOYAXISMOTION:
|
2021-03-01 19:48:31 +01:00
|
|
|
if ( !SDLash_IsInstanceIDAGameController( event->jaxis.which ))
|
2020-12-28 10:46:27 +01:00
|
|
|
Joy_AxisMotionEvent( event->jaxis.axis, event->jaxis.value );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYBALLMOTION:
|
2021-03-01 19:48:31 +01:00
|
|
|
if ( !SDLash_IsInstanceIDAGameController( event->jball.which ))
|
2020-12-28 10:46:27 +01:00
|
|
|
Joy_BallMotionEvent( event->jball.ball, event->jball.xrel, event->jball.yrel );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYHATMOTION:
|
2021-03-01 19:48:31 +01:00
|
|
|
if ( !SDLash_IsInstanceIDAGameController( event->jhat.which ))
|
2020-12-28 10:46:27 +01:00
|
|
|
Joy_HatMotionEvent( event->jhat.hat, event->jhat.value );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
case SDL_JOYBUTTONUP:
|
2021-03-01 19:48:31 +01:00
|
|
|
if ( !SDLash_IsInstanceIDAGameController( event->jbutton.which ))
|
2020-12-28 10:46:27 +01:00
|
|
|
Joy_ButtonEvent( event->jbutton.button, event->jbutton.state );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
Sys_Quit();
|
|
|
|
break;
|
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
case SDL_MOUSEWHEEL:
|
2022-10-12 04:18:19 +02:00
|
|
|
IN_MWheelEvent( event->wheel.y );
|
2019-10-26 03:36:43 +02:00
|
|
|
break;
|
|
|
|
|
2018-04-17 02:53:01 +02:00
|
|
|
/* Touch events */
|
|
|
|
case SDL_FINGERDOWN:
|
|
|
|
case SDL_FINGERUP:
|
|
|
|
case SDL_FINGERMOTION:
|
|
|
|
{
|
|
|
|
static int scale = 0;
|
2018-04-29 08:21:23 +02:00
|
|
|
touchEventType type;
|
2018-04-17 02:53:01 +02:00
|
|
|
float x, y, dx, dy;
|
|
|
|
|
|
|
|
if( event->type == SDL_FINGERDOWN )
|
|
|
|
type = event_down;
|
|
|
|
else if( event->type == SDL_FINGERUP )
|
|
|
|
type = event_up ;
|
2019-10-26 03:36:43 +02:00
|
|
|
else if( event->type == SDL_FINGERMOTION )
|
2018-04-17 02:53:01 +02:00
|
|
|
type = event_motion;
|
|
|
|
else break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
SDL sends coordinates in [0..width],[0..height] values
|
|
|
|
on some devices
|
|
|
|
*/
|
|
|
|
if( !scale )
|
|
|
|
{
|
|
|
|
if( ( event->tfinger.x > 0 ) && ( event->tfinger.y > 0 ) )
|
|
|
|
{
|
|
|
|
if( ( event->tfinger.x > 2 ) && ( event->tfinger.y > 2 ) )
|
|
|
|
{
|
|
|
|
scale = 2;
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "SDL reports screen coordinates, workaround enabled!\n");
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scale = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x = event->tfinger.x;
|
|
|
|
y = event->tfinger.y;
|
|
|
|
dx = event->tfinger.dx;
|
|
|
|
dy = event->tfinger.dy;
|
|
|
|
|
|
|
|
if( scale == 2 )
|
|
|
|
{
|
2019-02-18 19:25:26 +01:00
|
|
|
x /= (float)refState.width;
|
|
|
|
y /= (float)refState.height;
|
|
|
|
dx /= (float)refState.width;
|
|
|
|
dy /= (float)refState.height;
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 21:14:47 +02:00
|
|
|
IN_TouchEvent( type, event->tfinger.fingerId, x, y, dx, dy );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IME */
|
|
|
|
case SDL_TEXTINPUT:
|
|
|
|
SDLash_InputEvent( event->text );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYDEVICEADDED:
|
2019-07-09 02:00:54 +02:00
|
|
|
Joy_AddEvent();
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
case SDL_JOYDEVICEREMOVED:
|
2019-07-09 02:00:54 +02:00
|
|
|
Joy_RemoveEvent();
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* GameController API */
|
|
|
|
case SDL_CONTROLLERAXISMOTION:
|
2021-01-03 03:23:19 +01:00
|
|
|
{
|
2023-02-09 04:31:19 +01:00
|
|
|
if( !Joy_IsActive( ))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( event->caxis.axis >= 0 && event->caxis.axis < ARRAYSIZE( SDLash_GameControllerAxisMapping ))
|
2022-10-12 04:18:19 +02:00
|
|
|
{
|
2023-02-09 04:31:19 +01:00
|
|
|
Joy_KnownAxisMotionEvent( SDLash_GameControllerAxisMapping[event->caxis.axis], event->caxis.value );
|
|
|
|
}
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
2021-01-03 03:23:19 +01:00
|
|
|
}
|
2018-04-17 02:53:01 +02:00
|
|
|
|
|
|
|
case SDL_CONTROLLERBUTTONDOWN:
|
|
|
|
case SDL_CONTROLLERBUTTONUP:
|
|
|
|
{
|
2023-02-09 04:31:19 +01:00
|
|
|
if( !Joy_IsActive( ))
|
|
|
|
break;
|
2018-04-17 02:53:01 +02:00
|
|
|
|
|
|
|
// TODO: Use joyinput funcs, for future multiple gamepads support
|
2023-02-09 04:31:19 +01:00
|
|
|
if( event->cbutton.button >= 0 && event->cbutton.button < ARRAYSIZE( SDLash_GameControllerButtonMapping ))
|
|
|
|
{
|
|
|
|
Key_Event( SDLash_GameControllerButtonMapping[event->cbutton.button], event->cbutton.state );
|
|
|
|
}
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SDL_CONTROLLERDEVICEADDED:
|
2021-01-02 03:08:32 +01:00
|
|
|
SDLash_GameController_Add( event->cdevice.which );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_CONTROLLERDEVICEREMOVED:
|
2021-01-02 03:08:32 +01:00
|
|
|
SDLash_GameController_Remove( event->cdevice.which );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if( event->window.windowID != SDL_GetWindowID( host.hWnd ) )
|
|
|
|
return;
|
|
|
|
|
2018-11-29 19:43:51 +01:00
|
|
|
if( host.status == HOST_SHUTDOWN || Host_IsDedicated() )
|
2018-04-17 02:53:01 +02:00
|
|
|
break; // no need to activate
|
2018-11-29 19:43:51 +01:00
|
|
|
|
2018-04-17 02:53:01 +02:00
|
|
|
switch( event->window.event )
|
|
|
|
{
|
|
|
|
case SDL_WINDOWEVENT_MOVED:
|
|
|
|
if( !vid_fullscreen->value )
|
|
|
|
{
|
|
|
|
Cvar_SetValue( "_window_xpos", (float)event->window.data1 );
|
2019-06-29 22:25:00 +02:00
|
|
|
Cvar_SetValue( "_window_ypos", (float)event->window.data2 );
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-11-29 19:43:51 +01:00
|
|
|
case SDL_WINDOWEVENT_MINIMIZED:
|
|
|
|
host.status = HOST_SLEEP;
|
|
|
|
VID_RestoreScreenResolution( );
|
|
|
|
break;
|
2018-04-17 02:53:01 +02:00
|
|
|
case SDL_WINDOWEVENT_RESTORED:
|
|
|
|
host.status = HOST_FRAME;
|
|
|
|
host.force_draw_version = true;
|
2018-11-29 19:43:51 +01:00
|
|
|
host.force_draw_version_time = host.realtime + FORCE_DRAW_VERSION_TIME;
|
2018-04-17 02:53:01 +02:00
|
|
|
if( vid_fullscreen->value )
|
|
|
|
VID_SetMode();
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
2019-10-26 03:36:43 +02:00
|
|
|
SDLash_ActiveEvent( true );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
2019-10-26 03:36:43 +02:00
|
|
|
SDLash_ActiveEvent( false );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_RESIZED:
|
|
|
|
{
|
2018-11-29 19:43:51 +01:00
|
|
|
if( vid_fullscreen->value )
|
|
|
|
break;
|
2018-04-17 02:53:01 +02:00
|
|
|
|
2019-11-12 00:04:18 +01:00
|
|
|
VID_SaveWindowSize( event->window.data1, event->window.data2 );
|
2018-04-17 02:53:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-10-26 03:36:43 +02:00
|
|
|
#else
|
|
|
|
case SDL_VIDEORESIZE:
|
2019-11-02 06:54:58 +01:00
|
|
|
VID_SaveWindowSize( event->resize.w, event->resize.h );
|
2019-11-26 00:47:48 +01:00
|
|
|
break;
|
2019-10-26 03:36:43 +02:00
|
|
|
case SDL_ACTIVEEVENT:
|
|
|
|
SDLash_ActiveEvent( event->active.gain );
|
|
|
|
break;
|
|
|
|
#endif
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_RunEvents
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
2018-10-21 23:28:24 +02:00
|
|
|
void Platform_RunEvents( void )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while( !host.crashed && !host.shutdown_issued && SDL_PollEvent( &event ) )
|
|
|
|
SDLash_EventFilter( &event );
|
|
|
|
}
|
|
|
|
|
2018-10-22 00:09:43 +02:00
|
|
|
void* Platform_GetNativeObject( const char *name )
|
2018-04-17 02:53:01 +02:00
|
|
|
{
|
2018-10-21 23:28:24 +02:00
|
|
|
return NULL; // SDL don't have it
|
2018-04-17 02:53:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 21:14:47 +02:00
|
|
|
/*
|
|
|
|
========================
|
|
|
|
Platform_PreCreateMove
|
|
|
|
|
|
|
|
this should disable mouse look on client when m_ignore enabled
|
|
|
|
TODO: kill mouse in win32 clients too
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void Platform_PreCreateMove( void )
|
|
|
|
{
|
2022-10-13 13:49:48 +02:00
|
|
|
if( CVAR_TO_BOOL( m_ignore ))
|
2019-09-26 21:14:47 +02:00
|
|
|
{
|
|
|
|
SDL_GetRelativeMouseState( NULL, NULL );
|
|
|
|
SDL_ShowCursor( SDL_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#endif // defined( XASH_SDL ) && !XASH_DEDICATED
|