Skip to content

Commit

Permalink
- fixed bug: num of plugged joystick > max num supported joystick_scan
Browse files Browse the repository at this point in the history
- improved detection of joystick for android in SDLActivity.java mapping DPAD to keyboard buttons
- improved Android Hats/Axes detection add it into PollEvent() events
- added plug and play feature
  • Loading branch information
White Dragon committed May 13, 2018
1 parent 60162b6 commit 46a500b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
8 changes: 8 additions & 0 deletions engine/android/src/org/libsdl/app/SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,14 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
keyCode=KeyEvent.KEYCODE_T; break;
case KeyEvent.KEYCODE_BUTTON_Z: //key code constant: Z Button key.
keyCode=KeyEvent.KEYCODE_U; break;
case KeyEvent.KEYCODE_DPAD_UP: //key code constant: D-Pad Up key.
keyCode=KeyEvent.KEYCODE_V; break;
case KeyEvent.KEYCODE_DPAD_RIGHT: //key code constant: D-Pad Right key.
keyCode=KeyEvent.KEYCODE_X; break;
case KeyEvent.KEYCODE_DPAD_DOWN: //key code constant: D-Pad Down key.
keyCode=KeyEvent.KEYCODE_Y; break;
case KeyEvent.KEYCODE_DPAD_LEFT: //key code constant: D-Pad Left key.
keyCode=KeyEvent.KEYCODE_W; break;
}

// Dispatch the different events depending on where they come from
Expand Down
110 changes: 91 additions & 19 deletions engine/sdl/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Copyright (c) 2004 - 2014 OpenBOR Team
*/

// Generic control stuff (keyboard+joystick)
// Generic control stuff (keyboard+joystick)

#include "video.h"
#include "globals.h"
#include "control.h"
Expand Down Expand Up @@ -157,7 +158,14 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
if(ev.jbutton.button == 4 || ev.jbutton.button == 3 || ev.jbutton.button == 5) joysticks[i].Hats &= ~(JoystickBits[3]);
if(ev.jbutton.button == 2 || ev.jbutton.button == 1 || ev.jbutton.button == 3) joysticks[i].Hats &= ~(JoystickBits[4]);
if(ev.jbutton.button >= 8 && ev.jbutton.button <= 18) joysticks[i].Buttons &= ~(JoystickBits[ev.jbutton.button - 3]);
}
}
/*else
{
// add key flag from event
#ifdef ANDROID
joysticks[i].Buttons &= 0x00 << ev.jbutton.button;
#endif
}*/
}
}
break;
Expand All @@ -168,7 +176,12 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
{
if(ev.jbutton.which == i)
{
lastjoy = 1 + i * JOY_MAX_INPUTS + ev.jbutton.button;
lastjoy = 1 + i * JOY_MAX_INPUTS + ev.jbutton.button;

// add key flag from event
/*#ifdef ANDROID
joysticks[i].Buttons |= 0x01 << ev.jbutton.button;
#endif*/
}
}
break;
Expand All @@ -180,11 +193,19 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
{
int hatfirst = 1 + i * JOY_MAX_INPUTS + joysticks[i].NumButtons + 2*joysticks[i].NumAxes + 4*ev.jhat.hat;
x = (joysticks[i].Hats >> (4*ev.jhat.hat)) & 0x0F; // hat's previous state
if(ev.jhat.value & SDL_HAT_UP && !(x & SDL_HAT_UP)) lastjoy = hatfirst;
if(ev.jhat.value & SDL_HAT_UP && !(x & SDL_HAT_UP)) lastjoy = hatfirst;
if(ev.jhat.value & SDL_HAT_RIGHT && !(x & SDL_HAT_RIGHT)) lastjoy = hatfirst + 1;
if(ev.jhat.value & SDL_HAT_DOWN && !(x & SDL_HAT_DOWN)) lastjoy = hatfirst + 2;
if(ev.jhat.value & SDL_HAT_LEFT && !(x & SDL_HAT_LEFT)) lastjoy = hatfirst + 3;
//if(lastjoy) fprintf(stderr, "SDL_JOYHATMOTION - Joystick %i Hat %i (Index %i)\n", i, ev.jhat.hat, lastjoy);
if(ev.jhat.value & SDL_HAT_DOWN && !(x & SDL_HAT_DOWN)) lastjoy = hatfirst + 2;
if(ev.jhat.value & SDL_HAT_LEFT && !(x & SDL_HAT_LEFT)) lastjoy = hatfirst + 3;
//if(lastjoy) fprintf(stderr, "SDL_JOYHATMOTION - Joystick %i Hat %i (Index %i)\n", i, ev.jhat.hat, lastjoy);

// add key flag from event (0x01 0x02 0x04 0x08)
#ifdef ANDROID
if(ev.jhat.value & SDL_HAT_UP) joysticks[i].Hats |= SDL_HAT_UP << (ev.jhat.hat*4);
if(ev.jhat.value & SDL_HAT_RIGHT) joysticks[i].Hats |= SDL_HAT_RIGHT << (ev.jhat.hat*4);
if(ev.jhat.value & SDL_HAT_DOWN) joysticks[i].Hats |= SDL_HAT_DOWN << (ev.jhat.hat*4);
if(ev.jhat.value & SDL_HAT_LEFT) joysticks[i].Hats |= SDL_HAT_LEFT << (ev.jhat.hat*4);
#endif
}
}
break;
Expand All @@ -198,10 +219,44 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
x = (joysticks[i].Axes >> (2*ev.jaxis.axis)) & 0x03; // previous state of axis
if(ev.jaxis.value < -1*T_AXIS && !(x & 0x01)) lastjoy = axisfirst;
if(ev.jaxis.value > T_AXIS && !(x & 0x02)) lastjoy = axisfirst + 1;
//if(lastjoy) fprintf(stderr, "SDL_JOYAXISMOTION - Joystick %i Axis %i = Position %i (Index %i)\n", i, ev.jaxis.axis, ev.jaxis.value, lastjoy);
//if(lastjoy) fprintf(stderr, "SDL_JOYAXISMOTION - Joystick %i Axis %i = Position %i (Index %i)\n", i, ev.jaxis.axis, ev.jaxis.value, lastjoy);

// add key flag from event
#ifdef ANDROID
if(ev.jaxis.value < -1*T_AXIS) { joysticks[i].Axes |= 0x01 << (ev.jaxis.axis*2); }
if(ev.jaxis.value > T_AXIS) { joysticks[i].Axes |= 0x02 << (ev.jaxis.axis*2); }
#endif
}
}
break;
break;

case SDL_JOYDEVICEADDED:
if (ev.jdevice.which < JOY_LIST_TOTAL)
{
int i = ev.jdevice.which;
char buffer[26];
joystick[i] = SDL_JoystickOpen(i);
get_time_string(buffer, 26, (time_t)ev.jdevice.timestamp, "%Y-%m-%d %H:%M:%S");
printf("Joystick connected at port: %d at %s\n",i,buffer);
}
break;

case SDL_JOYDEVICEREMOVED:
if (ev.jdevice.which < JOY_LIST_TOTAL)
{
int i = ev.jdevice.which;
if(joystick[i])
{
char buffer[26];
SDL_JoystickClose(joystick[i]);
get_time_string(buffer, 26, (time_t)ev.jdevice.timestamp, "%Y-%m-%d %H:%M:%S");
printf("Joystick disconnected from port: %d at %s\n",i,buffer);
}
}
break;

default:
break;
}

}
Expand All @@ -216,8 +271,10 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
joysticks[i].Axes = joysticks[i].Hats = joysticks[i].Buttons = 0;

// check buttons
for(j=0; j<joysticks[i].NumButtons; j++)
joysticks[i].Buttons |= SDL_JoystickGetButton(joystick[i], j) << j;
for(j=0; j<joysticks[i].NumButtons; j++)
{
joysticks[i].Buttons |= SDL_JoystickGetButton(joystick[i], j) << j;
}

// check axes
for(j=0; j<joysticks[i].NumAxes; j++)
Expand All @@ -228,8 +285,16 @@ void getPads(Uint8* keystate, Uint8* keystate_def)
}

// check hats
for(j=0; j<joysticks[i].NumHats; j++)
joysticks[i].Hats |= SDL_JoystickGetHat(joystick[i], j) << (j*4);
for(j=0; j<joysticks[i].NumHats; j++)
{
//joysticks[i].Hats |= SDL_JoystickGetHat(joystick[i], j) << (j*4);

Uint8 hat_value = SDL_JoystickGetHat(joystick[i], j);
if(hat_value & SDL_HAT_UP) joysticks[i].Hats |= SDL_HAT_UP << (j*4);
if(hat_value & SDL_HAT_RIGHT) joysticks[i].Hats |= SDL_HAT_RIGHT << (j*4);
if(hat_value & SDL_HAT_DOWN) joysticks[i].Hats |= SDL_HAT_DOWN << (j*4);
if(hat_value & SDL_HAT_LEFT) joysticks[i].Hats |= SDL_HAT_LEFT << (j*4);
}

// combine axis, hat, and button state into a single value
joysticks[i].Data = joysticks[i].Buttons;
Expand All @@ -240,7 +305,6 @@ void getPads(Uint8* keystate, Uint8* keystate_def)

}


/*
Convert binary masked data to indexes
*/
Expand All @@ -259,9 +323,12 @@ types, defaults and keynames.
*/
void joystick_scan(int scan)
{
int i, j, k;
if(!scan) return;
numjoy = SDL_NumJoysticks();
int i, j, k;

if(!scan) return;

numjoy = SDL_NumJoysticks();

if(!numjoy && scan != 2)
{
printf("No Joystick(s) Found!\n");
Expand All @@ -270,7 +337,10 @@ void joystick_scan(int scan)
else
{
printf("\n%d joystick(s) found!\n", numjoy);
}
}

if (numjoy > JOY_LIST_TOTAL) numjoy = JOY_LIST_TOTAL; // avoid overflow bug

for(i=0, k=0; i<numjoy; i++, k+=JOY_MAX_INPUTS)
{
joystick[i] = SDL_JoystickOpen(i);
Expand Down Expand Up @@ -620,7 +690,9 @@ void control_update(s_playercontrols ** playercontrols, int numplayers)
}
pcontrols->kb_break = 0;
pcontrols->newkeyflags = k & (~pcontrols->keyflags);
pcontrols->keyflags = k;
pcontrols->keyflags = k;

//if (player <= 0) debug_printf("hats: %d, axes: %d, data: %d",joysticks[0].Hats,joysticks[0].Axes,joysticks[0].Data);
}
}

Expand Down
15 changes: 13 additions & 2 deletions engine/source/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <malloc.h>
#include <locale.h>
#include <math.h>
#include <time.h>

#include "stringptr.h"
#include "utils.h"
Expand Down Expand Up @@ -651,8 +652,7 @@ char* multistrcatsp(char* buf, ...)
\param curr_size_allocated : current allocated size to the array (in BYTE)
\param grow_step : bloc size of expansion of the array (in BYTE)
*/
void
Array_Check_Size( const char *f_caller, char **array, int new_size, int *curr_size_allocated, int grow_step )
void Array_Check_Size( const char *f_caller, char **array, int new_size, int *curr_size_allocated, int grow_step )
{
// Deallocation
if( new_size <= 0 )
Expand Down Expand Up @@ -715,3 +715,14 @@ Array_Check_Size( const char *f_caller, char **array, int new_size, int *curr_si
*array = copy;
}

void get_time_string(char buffer[], unsigned buffer_size, time_t timestamp, char* pattern)
{
struct tm* tm_info;

tm_info = localtime(&timestamp);

strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", tm_info);

This comment has been minimized.

Copy link
@whitedragon0000

whitedragon0000 May 13, 2018

Author Contributor

I need to fix this line..
Missing to use pattern string

return;
}

3 changes: 2 additions & 1 deletion engine/source/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ unsigned readlsb32(const unsigned char *src);
int searchList(const char *list[], const char *value, int length);
//int searchListB(const char *list[], const char *value, int length);
char *commaprint(u64 n);
char* multistrcatsp(char* buf, ...);
char* multistrcatsp(char* buf, ...);
void get_time_string(char buffer[], unsigned buffer_size, time_t timestamp, char* pattern); // ex. "%Y-%m-%d %H:%M:%S"

void Array_Check_Size( const char *f_caller, char **array, int new_size, int *curr_size_allocated, int grow_step );

Expand Down

0 comments on commit 46a500b

Please sign in to comment.