2018-10-21 23:28:24 +02:00
|
|
|
/*
|
|
|
|
vid_sdl.c - SDL input component
|
|
|
|
Copyright (C) 2018 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 !XASH_DEDICATED
|
2018-10-21 23:28:24 +02:00
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
static SDL_Joystick *joy;
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
static SDL_GameController *gamecontroller;
|
2019-10-25 16:11:27 +02:00
|
|
|
#else // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
|
|
|
|
|
|
|
#define SDL_WarpMouseInWindow( win, x, y ) SDL_WarpMouse( ( x ), ( y ) )
|
|
|
|
|
|
|
|
#endif
|
2018-10-21 23:28:24 +02:00
|
|
|
|
2018-10-22 00:09:43 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_GetMousePos
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
2020-01-19 02:15:54 +01:00
|
|
|
void GAME_EXPORT Platform_GetMousePos( int *x, int *y )
|
2018-10-22 00:09:43 +02:00
|
|
|
{
|
|
|
|
SDL_GetMouseState( x, y );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_SetMousePos
|
|
|
|
|
|
|
|
============
|
|
|
|
*/
|
2020-01-19 02:15:54 +01:00
|
|
|
void GAME_EXPORT Platform_SetMousePos( int x, int y )
|
2018-10-22 00:09:43 +02:00
|
|
|
{
|
|
|
|
SDL_WarpMouseInWindow( host.hWnd, x, y );
|
|
|
|
}
|
|
|
|
|
2018-10-22 00:25:29 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_GetClipobardText
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void Platform_GetClipboardText( char *buffer, size_t size )
|
|
|
|
{
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-22 00:25:29 +02:00
|
|
|
char *sdlbuffer = SDL_GetClipboardText();
|
|
|
|
|
|
|
|
if( !sdlbuffer )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Q_strncpy( buffer, sdlbuffer, size );
|
|
|
|
SDL_free( sdlbuffer );
|
2019-10-25 16:11:27 +02:00
|
|
|
#else // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2019-10-24 11:43:17 +02:00
|
|
|
buffer[0] = 0;
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-22 00:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_SetClipobardText
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
2019-05-02 17:07:03 +02:00
|
|
|
void Platform_SetClipboardText( const char *buffer, size_t size )
|
2018-10-22 00:25:29 +02:00
|
|
|
{
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-22 00:25:29 +02:00
|
|
|
SDL_SetClipboardText( buffer );
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-22 00:25:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-21 23:28:24 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_Vibrate
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void Platform_Vibrate( float time, char flags )
|
|
|
|
{
|
|
|
|
// stub
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_EnableTextInput
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void Platform_EnableTextInput( qboolean enable )
|
|
|
|
{
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
enable ? SDL_StartTextInput() : SDL_StopTextInput();
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_JoyInit_Old
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static int SDLash_JoyInit_Old( int numjoy )
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
int i;
|
|
|
|
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Joystick: SDL\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
|
|
|
|
if( SDL_WasInit( SDL_INIT_JOYSTICK ) != SDL_INIT_JOYSTICK &&
|
|
|
|
SDL_InitSubSystem( SDL_INIT_JOYSTICK ) )
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Failed to initialize SDL Joysitck: %s\n", SDL_GetError() );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( joy )
|
|
|
|
{
|
|
|
|
SDL_JoystickClose( joy );
|
|
|
|
}
|
|
|
|
|
|
|
|
num = SDL_NumJoysticks();
|
|
|
|
|
|
|
|
if( num > 0 )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "%i joysticks found:\n", num );
|
2018-10-21 23:28:24 +02:00
|
|
|
else
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "No joystick found.\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
for( i = 0; i < num; i++ )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "%i\t: %s\n", i, SDL_JoystickNameForIndex( i ) );
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Pass +set joy_index N to command line, where N is number, to select active joystick\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
|
|
|
|
joy = SDL_JoystickOpen( numjoy );
|
|
|
|
|
|
|
|
if( !joy )
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Failed to select joystick: %s\n", SDL_GetError( ) );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Selected joystick: %s\n"
|
2018-10-21 23:28:24 +02:00
|
|
|
"\tAxes: %i\n"
|
|
|
|
"\tHats: %i\n"
|
|
|
|
"\tButtons: %i\n"
|
|
|
|
"\tBalls: %i\n",
|
|
|
|
SDL_JoystickName( joy ), SDL_JoystickNumAxes( joy ), SDL_JoystickNumHats( joy ),
|
|
|
|
SDL_JoystickNumButtons( joy ), SDL_JoystickNumBalls( joy ) );
|
|
|
|
|
|
|
|
SDL_GameControllerEventState( SDL_DISABLE );
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
SDL_JoystickEventState( SDL_ENABLE );
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
SDLash_JoyInit_New
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static int SDLash_JoyInit_New( int numjoy )
|
|
|
|
{
|
|
|
|
int temp, num;
|
|
|
|
int i;
|
|
|
|
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Joystick: SDL GameController API\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
|
|
|
|
if( SDL_WasInit( SDL_INIT_GAMECONTROLLER ) != SDL_INIT_GAMECONTROLLER &&
|
|
|
|
SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ) )
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Failed to initialize SDL GameController API: %s\n", SDL_GetError() );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// chance to add mappings from file
|
|
|
|
SDL_GameControllerAddMappingsFromFile( "controllermappings.txt" );
|
|
|
|
|
|
|
|
if( gamecontroller )
|
|
|
|
{
|
|
|
|
SDL_GameControllerClose( gamecontroller );
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = SDL_NumJoysticks();
|
|
|
|
num = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < temp; i++ )
|
|
|
|
{
|
|
|
|
if( SDL_IsGameController( i ))
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( num > 0 )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "%i joysticks found:\n", num );
|
2018-10-21 23:28:24 +02:00
|
|
|
else
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "No joystick found.\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < num; i++ )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "%i\t: %s\n", i, SDL_GameControllerNameForIndex( i ) );
|
2018-10-21 23:28:24 +02:00
|
|
|
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Pass +set joy_index N to command line, where N is number, to select active joystick\n" );
|
2018-10-21 23:28:24 +02:00
|
|
|
|
|
|
|
gamecontroller = SDL_GameControllerOpen( numjoy );
|
|
|
|
|
|
|
|
if( !gamecontroller )
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Failed to select joystick: %s\n", SDL_GetError( ) );
|
2018-10-21 23:28:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// was added in SDL2-2.0.6, allow build with earlier versions just in case
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 6 )
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Selected joystick: %s (%i:%i:%i)\n",
|
2018-10-21 23:28:24 +02:00
|
|
|
SDL_GameControllerName( gamecontroller ),
|
|
|
|
SDL_GameControllerGetVendor( gamecontroller ),
|
|
|
|
SDL_GameControllerGetProduct( gamecontroller ),
|
|
|
|
SDL_GameControllerGetProductVersion( gamecontroller ));
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 6 )
|
2018-10-21 23:28:24 +02:00
|
|
|
SDL_GameControllerEventState( SDL_ENABLE );
|
|
|
|
SDL_JoystickEventState( SDL_DISABLE );
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Platform_JoyInit
|
|
|
|
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
int Platform_JoyInit( int numjoy )
|
|
|
|
{
|
2019-10-25 16:11:27 +02:00
|
|
|
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2018-10-21 23:28:24 +02:00
|
|
|
// SDL_Joystick is now an old API
|
|
|
|
// SDL_GameController is preferred
|
2019-10-24 11:43:17 +02:00
|
|
|
if( !Sys_CheckParm( "-sdl_joy_old_api" ) )
|
|
|
|
return SDLash_JoyInit_New(numjoy);
|
2019-10-25 16:11:27 +02:00
|
|
|
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 )
|
2019-10-24 11:43:17 +02:00
|
|
|
return SDLash_JoyInit_Old(numjoy);
|
2018-10-21 23:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // XASH_DEDICATED
|