--- uae/src/od-linux/joystick.c 2018/04/24 16:40:31 1.1 +++ uae/src/od-linux/joystick.c 2018/04/24 17:03:11 1.1.1.3 @@ -1,9 +1,11 @@ /* * UAE - The Un*x Amiga Emulator * - * Joystick emulation for Linux + * Joystick emulation for Linux and BSD. They share too much code to + * split this file. * * Copyright 1997 Bernd Schmidt + * Copyright 1998 Krister Walfridsson */ #include "sysconfig.h" @@ -15,30 +17,51 @@ #include "custom.h" #include "joystick.h" -int nr_joysticks; +#ifdef HAVE_MACHINE_SOUNDCARD_H -static int js0, js1; +/* The BSD way. */ + +# include +typedef struct joystick uae_joystick_t + +#define JS_DEVNAME_PREFIX "joy" + +#else + +/* The Linux way. */ /* There are too many different versions of . Rather * than trying to work correctly with all of them, we duplicate the * necessary definitions here. */ -struct JS_DATA_TYPE +typedef struct { int buttons; int x; int y; -}; +} uae_joystick_t; + +#define JS_DEVNAME_PREFIX "js" -static struct JS_DATA_TYPE jscal; +#endif + +int nr_joysticks; + +static int js0, js1; + +struct joy_range +{ + int minx, maxx, miny, maxy; +} range0, range1; void read_joystick(int nr, unsigned int *dir, int *button) { - static int minx = MAXINT, maxx = MININT, - miny = MAXINT, maxy = MININT; + static int minx = INT_MAX, maxx = INT_MIN; + static int miny = INT_MAX, maxy = INT_MIN; int left = 0, right = 0, top = 0, bot = 0; - struct JS_DATA_TYPE buffer; + uae_joystick_t buffer; int len; int fd = nr == 0 ? js0 : js1; + struct joy_range *r = nr == 0 ? &range0 : &range1; *dir = 0; *button = 0; @@ -49,19 +72,19 @@ void read_joystick(int nr, unsigned int if (len != sizeof(buffer)) return; - if (buffer.x < minx) minx = buffer.x; - if (buffer.y < miny) miny = buffer.y; - if (buffer.x > maxx) maxx = buffer.x; - if (buffer.y > maxy) maxy = buffer.y; + if (buffer.x < r->minx) r->minx = buffer.x; + if (buffer.y < r->miny) r->miny = buffer.y; + if (buffer.x > r->maxx) r->maxx = buffer.x; + if (buffer.y > r->maxy) r->maxy = buffer.y; - if (buffer.x < (minx + (maxx-minx)/3)) + if (buffer.x < (r->minx + (r->maxx - r->minx)/3)) left = 1; - else if (buffer.x > (minx + 2*(maxx-minx)/3)) + else if (buffer.x > (r->minx + 2*(r->maxx - r->minx)/3)) right = 1; - if (buffer.y < (miny + (maxy-miny)/3)) + if (buffer.y < (r->miny + (r->maxy - r->miny)/3)) top = 1; - else if (buffer.y > (miny + 2*(maxy-miny)/3)) + else if (buffer.y > (r->miny + 2*(r->maxy - r->miny)/3)) bot = 1; if (left) top = !top; @@ -74,25 +97,34 @@ void init_joystick(void) { nr_joysticks = 0; js0 = -1; js1 = -1; - js0 = open("/dev/js0", O_RDONLY); + js0 = open("/dev/" JS_DEVNAME_PREFIX "0", O_RDONLY); if (js0 < 0) { write_log ("No joysticks found\n"); return; } nr_joysticks = 1; - js1 = open("/dev/js1", O_RDONLY); + js1 = open("/dev/" JS_DEVNAME_PREFIX "1", O_RDONLY); if (js1 < 0) { write_log ("Found one joystick\n"); return; } write_log ("Found two joysticks\n"); nr_joysticks = 2; + + 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 (js0 >= 0) - close(js0); + close (js0); if (js1 >= 0) - close(js1); + close (js1); }