Split SDL joystick events processing out into a seperate function.

This commit is contained in:
masscat 2007-05-23 16:35:55 +00:00
parent 29f5eac6e8
commit 38edaf280f
4 changed files with 195 additions and 120 deletions

View File

@ -22,6 +22,8 @@
#include "ctrlssdl.h"
static SDL_Joystick **open_joysticks = NULL;
/* Keypad key names */
const char *key_names[NB_KEYS] =
{
@ -68,15 +70,16 @@ const u16 default_keyboard_cfg[NB_KEYS] =
};
/* Load default joystick and keyboard configurations */
void load_default_config()
void load_default_config( void)
{
memcpy(keyboard_cfg, default_keyboard_cfg, sizeof(keyboard_cfg));
memcpy(joypad_cfg, default_joypad_cfg, sizeof(joypad_cfg));
}
/* Initialize joysticks */
BOOL init_joy(u16 joyCfg[]) {
BOOL init_joy( void) {
int i;
BOOL joy_init_good = TRUE;
set_joy_keys(default_joypad_cfg);
@ -90,17 +93,27 @@ BOOL init_joy(u16 joyCfg[]) {
nbr_joy = SDL_NumJoysticks();
printf("Nbr of joysticks: %d\n\n", nbr_joy);
for (i = 0; i < nbr_joy; i++)
{
SDL_Joystick * joy = SDL_JoystickOpen(i);
printf("Joystick %s\n", i, SDL_JoystickName(i));
printf("Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy));
printf("Hats: %d\n\n", SDL_JoystickNumHats(joy));
}
if ( nbr_joy > 0) {
open_joysticks =
calloc( sizeof ( SDL_Joystick *), nbr_joy);
return TRUE;
if ( open_joysticks != NULL) {
for (i = 0; i < nbr_joy; i++)
{
SDL_Joystick * joy = SDL_JoystickOpen(i);
printf("Joystick %d %s\n", i, SDL_JoystickName(i));
printf("Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy));
printf("Hats: %d\n\n", SDL_JoystickNumHats(joy));
}
}
else {
joy_init_good = FALSE;
}
}
return joy_init_good;
}
/* Set all buttons at once */
@ -116,17 +129,25 @@ void set_kb_keys(u16 kbCfg[])
}
/* Unload joysticks */
void uninit_joy()
void uninit_joy( void)
{
int i;
printf("Disabling joystick support.\n");
for (i = 0; i < SDL_NumJoysticks(); i++)
SDL_JoystickClose(i);
if ( open_joysticks != NULL) {
for (i = 0; i < SDL_NumJoysticks(); i++) {
SDL_JoystickClose( open_joysticks[i]);
}
free( open_joysticks);
}
open_joysticks = NULL;
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
}
/* Return keypad vector with given key set to 1 */
u16 inline lookup_joy_key (u16 keyval) {
u16 lookup_joy_key (u16 keyval) {
int i;
u16 Key = 0;
for(i = 0; i < NB_KEYS; i++)
@ -136,7 +157,7 @@ u16 inline lookup_joy_key (u16 keyval) {
}
/* Return keypad vector with given key set to 1 */
u16 inline lookup_key (u16 keyval) {
u16 lookup_key (u16 keyval) {
int i;
u16 Key = 0;
for(i = 0; i < NB_KEYS; i++)
@ -146,28 +167,16 @@ u16 inline lookup_key (u16 keyval) {
}
/* Empty SDL Events' queue */
void clear_events()
static void clear_events( void)
{
SDL_Event event;
/* IMPORTANT: Reenable joystick events iif needed. */
if(SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE)
SDL_JoystickEventState(SDL_ENABLE);
#ifndef GTK_UI
sdl_quit = FALSE;
#endif
/* There's an event waiting to be processed? */
while (SDL_PollEvent(&event))
{
#ifndef GTK_UI
switch (event.type)
{
case SDL_QUIT:
sdl_quit = TRUE;
break;
}
#endif // !GTK_UI
}
return;
@ -249,7 +258,20 @@ void get_set_joy_axis(int index, int index_o) {
joypad_cfg[index_o] = joypad_cfg[index];
}
#ifndef GTK_UI
static signed long
screen_to_touch_range_x( signed long scr_x, float size_ratio) {
signed long touch_x = (signed long)((float)scr_x * size_ratio);
return touch_x;
}
static signed long
screen_to_touch_range_y( signed long scr_y, float size_ratio) {
signed long touch_y = (signed long)((float)scr_y * size_ratio);
return touch_y;
}
/* Set mouse coordinates */
void set_mouse_coord(signed long x,signed long y)
{
@ -258,7 +280,6 @@ void set_mouse_coord(signed long x,signed long y)
mouse.x = x;
mouse.y = y;
}
#endif // !GTK_UI
/* Update NDS keypad */
void update_keypad(u16 keys)
@ -270,7 +291,7 @@ void update_keypad(u16 keys)
}
/* Retrieve current NDS keypad */
u16 get_keypad()
u16 get_keypad( void)
{
u16 keypad;
keypad = ~((u16 *)MMU.ARM7_REG)[0x130>>1];
@ -279,100 +300,150 @@ u16 get_keypad()
return keypad;
}
/* Manage input events */
u16 process_ctrls_events(u16 keypad)
{
/*
* The internal joystick events processing function
*/
static int
do_process_joystick_events( u16 *keypad, SDL_Event *event) {
int processed = 1;
u16 key;
switch ( event->type)
{
/* Joystick axis motion
Note: button constants have a 1bit offset. */
case SDL_JOYAXISMOTION:
key = lookup_joy_key( JOY_AXIS_(event->jaxis.axis) );
if( key == 0 ) break; /* Not an axis of interest? */
/* Axis is back to initial position */
if( event->jaxis.value == 0 )
RM_KEY( *keypad, key | get_joy_axis_twin(key) );
/* Key should have been down but its currently set to up? */
else if( (event->jaxis.value > 0) &&
(key == KEYMASK_( KEY_UP-1 )) )
key = KEYMASK_( KEY_DOWN-1 );
/* Key should have been left but its currently set to right? */
else if( (event->jaxis.value < 0) &&
(key == KEYMASK_( KEY_RIGHT-1 )) )
key = KEYMASK_( KEY_LEFT-1 );
/* Remove some sensitivity before checking if different than zero...
Fixes some badly behaving joypads [like one of mine]. */
if( (event->jaxis.value >> 5) != 0 )
ADD_KEY( *keypad, key );
break;
/* Joystick button pressed */
/* FIXME: Add support for BOOST */
case SDL_JOYBUTTONDOWN:
key = lookup_joy_key( event->jbutton.button );
ADD_KEY( *keypad, key );
break;
/* Joystick button released */
case SDL_JOYBUTTONUP:
key = lookup_joy_key(event->jbutton.button);
RM_KEY( *keypad, key );
break;
default:
processed = 0;
break;
}
return processed;
}
/*
* Process only the joystick events
*/
void
process_joystick_events( u16 *keypad) {
SDL_Event event;
/* IMPORTANT: Reenable joystick events iif needed. */
if(SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE)
SDL_JoystickEventState(SDL_ENABLE);
#ifndef GTK_UI
sdl_quit = FALSE;
#endif
/* There's an event waiting to be processed? */
while (SDL_PollEvent(&event))
{
do_process_joystick_events( keypad, &event);
}
}
/* Manage input events */
int
process_ctrls_events( u16 *keypad,
void (*external_videoResizeFn)( u16 width, u16 height),
float nds_screen_size_ratio)
{
u16 key;
int cause_quit = 0;
SDL_Event event;
/* IMPORTANT: Reenable joystick events iif needed. */
if(SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE)
SDL_JoystickEventState(SDL_ENABLE);
/* There's an event waiting to be processed? */
while (SDL_PollEvent(&event))
{
switch (event.type)
{
/* Joystick axis motion
Note: button constants have a 1bit offset. */
case SDL_JOYAXISMOTION:
key = lookup_joy_key( JOY_AXIS_(event.jaxis.axis) );
if( key == 0 ) break; /* Not an axis of interest? */
if ( !do_process_joystick_events( keypad, &event)) {
switch (event.type)
{
case SDL_VIDEORESIZE:
if ( external_videoResizeFn != NULL) {
external_videoResizeFn( event.resize.w, event.resize.h);
}
break;
/* Axis is back to initial position */
if( event.jaxis.value == 0 )
RM_KEY( keypad, key | get_joy_axis_twin(key) );
/* Key should have been down but its currently set to up? */
else if( (event.jaxis.value > 0) &&
(key == KEYMASK_( KEY_UP-1 )) )
key = KEYMASK_( KEY_DOWN-1 );
/* Key should have been left but its currently set to right? */
else if( (event.jaxis.value < 0) &&
(key == KEYMASK_( KEY_RIGHT-1 )) )
key = KEYMASK_( KEY_LEFT-1 );
/* Remove some sensitivity before checking if different than zero...
Fixes some badly behaving joypads [like one of mine]. */
if( (event.jaxis.value >> 5) != 0 )
ADD_KEY( keypad, key );
break;
case SDL_KEYDOWN:
key = lookup_key(event.key.keysym.sym);
ADD_KEY( *keypad, key );
break;
/* Joystick button pressed */
/* FIXME: Add support for BOOST */
case SDL_JOYBUTTONDOWN:
key = lookup_joy_key( event.jbutton.button );
ADD_KEY( keypad, key );
break;
case SDL_KEYUP:
key = lookup_key(event.key.keysym.sym);
RM_KEY( *keypad, key );
break;
/* Joystick button released */
case SDL_JOYBUTTONUP:
key = lookup_joy_key(event.jbutton.button);
RM_KEY( keypad, key );
break;
/* GTK only needs joystick support. For others, we here provide the
mouse, keyboard and quit events. */
#ifndef GTK_UI
case SDL_KEYDOWN:
key = lookup_key(event.key.keysym.sym);
ADD_KEY( keypad, key );
break;
case SDL_KEYUP:
key = lookup_key(event.key.keysym.sym);
RM_KEY( keypad, key );
break;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button==1)
mouse.down = TRUE;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button==1)
mouse.down = TRUE;
case SDL_MOUSEMOTION:
if(!mouse.down) break;
if(event.button.y>=192)
set_mouse_coord(event.button.x, event.button.y - 192);
break;
case SDL_MOUSEMOTION:
if(!mouse.down)
break;
else {
signed long scaled_x =
screen_to_touch_range_x( event.button.x,
nds_screen_size_ratio);
signed long scaled_y =
screen_to_touch_range_y( event.button.y,
nds_screen_size_ratio);
case SDL_MOUSEBUTTONUP:
if(mouse.down) mouse.click = TRUE;
mouse.down = FALSE;
break;
if( scaled_y >= 192)
set_mouse_coord( scaled_x, scaled_y - 192);
}
break;
case SDL_QUIT:
sdl_quit = TRUE;
break;
#endif // !GTK_UI
case SDL_MOUSEBUTTONUP:
if(mouse.down) mouse.click = TRUE;
mouse.down = FALSE;
break;
default:
break;
case SDL_QUIT:
cause_quit = 1;
break;
default:
break;
}
}
}
return keypad;
return cause_quit;
}

View File

@ -34,8 +34,8 @@
#define ADD_KEY(keypad,key) ( (keypad) |= (key) )
#define RM_KEY(keypad,key) ( (keypad) &= ~(key) )
#define KEYMASK_(k) (1 << k)
#define JOY_AXIS_(k) ((k+1) << 8)
#define KEYMASK_(k) (1 << (k))
#define JOY_AXIS_(k) (((k)+1) << 8)
#define NB_KEYS 14
#define KEY_NONE 0
@ -74,22 +74,26 @@ struct mouse_status
struct mouse_status mouse;
BOOL sdl_quit;
void set_mouse_coord(signed long x,signed long y);
#endif // !GTK_UI
void load_default_config();
BOOL init_joy();
void uninit_joy();
void load_default_config( void);
BOOL init_joy( void);
void uninit_joy( void);
void set_joy_keys(const u16 joyCfg[]);
void set_kb_keys(u16 kbCfg[]);
u16 get_set_joy_key(int index);
void get_set_joy_axis(int index, int index_opp);
void update_keypad(u16 keys);
u16 get_keypad();
u16 inline lookup_key (u16 keyval);
u16 inline lookup_joy_key (u16 keyval);
u16 process_ctrls_events(u16 oldkeypad);
u16 get_keypad( void);
u16 lookup_key (u16 keyval);
u16 lookup_joy_key (u16 keyval);
int
process_ctrls_events( u16 *keypad,
void (*external_videoResizeFn)( u16 width, u16 height),
float nds_screen_size_ratio);
void
process_joystick_events( u16 *keypad);
#endif /* CTRLSSDL_H */

View File

@ -104,7 +104,7 @@ void desmume_cycle()
/* Retrieve old value: can use joysticks w/ another device (from our side) */
keypad = get_keypad();
/* Process joystick events if any */
keypad = process_ctrls_events(keypad);
process_joystick_events( &keypad);
/* Update keypad value */
update_keypad(keypad);

View File

@ -68,7 +68,7 @@ void desmume_cycle( void)
/* Retrieve old value: can use joysticks w/ another device (from our side) */
keypad = get_keypad();
/* Look for queued events */
keypad = process_ctrls_events(keypad);
process_joystick_events( &keypad);
/* Update keypad value */
update_keypad(keypad);