--- uae/src/sdl-joystick.c 2018/04/24 17:01:59 1.1.1.1 +++ uae/src/sdl-joystick.c 2018/04/24 17:16:35 1.1.1.3 @@ -1,97 +1,183 @@ - /* + /* * UAE - The Un*x Amiga Emulator - * - * Joystick emulation for Linux and BSD. They share too much code to - * split this file. - * + * + * SDL Joystick code + * * Copyright 1997 Bernd Schmidt * Copyright 1998 Krister Walfridsson + * Copyright 2003-2005 Richard Drummond */ #include "sysconfig.h" #include "sysdeps.h" -#include "config.h" #include "options.h" -#include "memory.h" -#include "custom.h" -#include "joystick.h" -#include "SDL.h" - -int nr_joysticks; - -static SDL_Joystick *joy0, *joy1; - -struct joy_range -{ - int minx, maxx, miny, maxy; -} range0, range1; - -void read_joystick(int nr, unsigned int *dir, int *button) -{ - int x_axis, y_axis; - int left = 0, right = 0, top = 0, bot = 0; - int len, i, num; - SDL_Joystick *joy = nr == 0 ? joy0 : joy1; - struct joy_range *r = nr == 0 ? &range0 : &range1; - - *dir = 0; - *button = 0; - if (nr >= nr_joysticks) - return; - - SDL_JoystickUpdate (); - - x_axis = SDL_JoystickGetAxis (joy, 0); - y_axis = SDL_JoystickGetAxis (joy, 1); - - if (x_axis < r->minx) r->minx = x_axis; - if (y_axis < r->miny) r->miny = y_axis; - if (x_axis > r->maxx) r->maxx = x_axis; - if (y_axis > r->maxy) r->maxy = y_axis; - - if (x_axis < (r->minx + (r->maxx - r->minx)/3)) - left = 1; - else if (x_axis > (r->minx + 2*(r->maxx - r->minx)/3)) - right = 1; - - if (y_axis < (r->miny + (r->maxy - r->miny)/3)) - top = 1; - else if (y_axis > (r->miny + 2*(r->maxy - r->miny)/3)) - bot = 1; - - if (left) top = !top; - if (right) bot = !bot; - *dir = bot | (right << 1) | (top << 8) | (left << 9); +#include "inputdevice.h" +#include + +static unsigned int nr_joysticks; +static int initialized; + +struct joyinfo { + SDL_Joystick *joy; + unsigned int axles; + unsigned int buttons; +}; + +static struct joyinfo joys[MAX_INPUT_DEVICES]; + + +static void read_joy (unsigned int nr) +{ + unsigned int num, i, axes, axis; + SDL_Joystick *joy; + + if (currprefs.input_selected_setting == 0) { + if (jsem_isjoy (0, &currprefs) != (int)nr && jsem_isjoy (1, &currprefs) != (int)nr) + return; + } + joy = joys[nr].joy; + axes = SDL_JoystickNumAxes (joy); + for (i = 0; i < axes; i++) { + axis = SDL_JoystickGetAxis (joy, i); + setjoystickstate (nr, i, axis, 32767); + } + num = SDL_JoystickNumButtons (joy); - if (num > 16) - num = 16; - for (i = 0; i < num; i++) - *button |= (SDL_JoystickGetButton (joy, i) & 1) << i; -} - -void init_joystick(void) -{ - int i; - nr_joysticks = SDL_NumJoysticks (); - if (nr_joysticks > 0) - joy0 = SDL_JoystickOpen (0); - if (nr_joysticks > 1) - joy1 = SDL_JoystickOpen (1); - range0.minx = INT_MAX; - range0.maxx = INT_MIN; - range0.miny = INT_MAX; - range0.maxy = INT_MIN; - range1.minx = INT_MAX; - range1.maxx = INT_MIN; - range1.miny = INT_MAX; - range1.maxy = INT_MIN; -} - -void close_joystick(void) -{ - if (nr_joysticks > 0) - SDL_JoystickClose (joy0); - if (nr_joysticks > 1) - SDL_JoystickClose (joy1); + for (i = 0; i < num; i++) { + int bs = SDL_JoystickGetButton (joy, i) ? 1 : 0; + setjoybuttonstate (nr, i, bs); + } +} + +static unsigned int get_joystick_num (void) +{ + return nr_joysticks; +} + +static unsigned int get_joystick_widget_num (unsigned int joy) +{ + return joys[joy].axles + joys[joy].buttons; +} + +static int get_joystick_widget_type (unsigned int joy, unsigned int num, char *name, uae_u32 *code) +{ + if (num >= joys[joy].axles && num < joys[joy].axles + joys[joy].buttons) { + if (name) + sprintf (name, "Button %d", num + 1 - joys[joy].axles); + return IDEV_WIDGET_BUTTON; + } else if (num < joys[joy].axles) { + if (name) + sprintf (name, "Axis %d", num + 1); + return IDEV_WIDGET_AXIS; + } + return IDEV_WIDGET_NONE; +} + +static int get_joystick_widget_first (unsigned int joy, int type) +{ + switch (type) { + case IDEV_WIDGET_BUTTON: + return joys[joy].axles; + case IDEV_WIDGET_AXIS: + return 0; + } + return -1; +} + +static const char *get_joystick_name (unsigned int joy) +{ + return SDL_JoystickName (joy); +} + +static void read_joysticks (void) +{ + if (get_joystick_num ()) { + unsigned int i; + SDL_JoystickUpdate (); + for (i = 0; i < get_joystick_num (); i++) + read_joy (i); + } +} + +static int init_joysticks (void) +{ + int success = 0; + + if (!initialized) { + if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) == 0) { + unsigned int i; + + nr_joysticks = SDL_NumJoysticks (); + write_log ("Found %d joystick(s)\n", nr_joysticks); + + if (nr_joysticks > MAX_INPUT_DEVICES) + nr_joysticks = MAX_INPUT_DEVICES; + + for (i = 0; i < get_joystick_num (); i++) { + joys[i].joy = SDL_JoystickOpen (i); + joys[i].axles = SDL_JoystickNumAxes (joys[i].joy); + joys[i].buttons = SDL_JoystickNumButtons (joys[i].joy); + } + success = initialized = 1; + } else + write_log ("Failed to initialize joysticks\n"); + } + + return success; +} + +static void close_joysticks (void) +{ + unsigned int i; + for (i = 0; i < nr_joysticks; i++) { + SDL_JoystickClose (joys[i].joy); + joys[i].joy = 0; + } + nr_joysticks = 0; + + if (initialized) { + SDL_QuitSubSystem (SDL_INIT_JOYSTICK); + initialized = 0; + } +} + +static int acquire_joy (unsigned int num, int flags) +{ + return num < get_joystick_num (); +} + +static void unacquire_joy (unsigned int num) +{ +} + +struct inputdevice_functions inputdevicefunc_joystick = { + init_joysticks, + close_joysticks, + acquire_joy, + unacquire_joy, + read_joysticks, + get_joystick_num, + get_joystick_name, + get_joystick_widget_num, + get_joystick_widget_type, + get_joystick_widget_first +}; + +/* + * Set default inputdevice config for SDL joysticks + */ +void input_get_default_joystick (struct uae_input_device *uid) +{ + unsigned int i, port; + + for (i = 0; i < nr_joysticks; i++) { + port = i & 1; + uid[i].eventid[ID_AXIS_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_HORIZ : INPUTEVENT_JOY1_HORIZ; + uid[i].eventid[ID_AXIS_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_VERT : INPUTEVENT_JOY1_VERT; + uid[i].eventid[ID_BUTTON_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON : INPUTEVENT_JOY1_FIRE_BUTTON; + uid[i].eventid[ID_BUTTON_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_2ND_BUTTON : INPUTEVENT_JOY1_2ND_BUTTON; + uid[i].eventid[ID_BUTTON_OFFSET + 2][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON_AF : INPUTEVENT_JOY1_FIRE_BUTTON_AF; + } + uid[0].enabled = 1; }