Refactor joystick handling

* Removes Linux-specific joystick handling
    * Adds cross-platform GLUT joystick handling; Caveats:
        - GLUT is not robust to joystick hot plugging like my old Linux-specific code was.  (Fix should be made in the
          GLUT driver and pushed upstream if we really care)
        - Deprecated X11 renderer now can only use keypad joystick
This commit is contained in:
Aaron Culliney 2014-11-09 13:13:27 -08:00
parent 2c12aa6112
commit e5e087c4e0
13 changed files with 76 additions and 444 deletions

View File

@ -101,8 +101,8 @@ testcpu_SOURCES = src/test/testcpu.c $(A2_TEST_SOURCES) $(META_SRC)
testcpu_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC -DHEADLESS=1
testcpu_CCASFLAGS = $(testcpu_CFLAGS)
testcpu_LDFLAGS = $(apple2ix_LDFLAGS)
testcpu_LDADD = @ASM_O@
testcpu_DEPENDENCIES = @ASM_O@ @META_O@
testcpu_LDADD = @ASM_O@ @VIDEO_O@
testcpu_DEPENDENCIES = @ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testcpu_SOURCES = $(ASM_SRC_x86)

View File

@ -280,15 +280,6 @@ dnl Misc ...
AC_DEFINE(APPLE2IX, 1, [Denotes a section of code as Apple//ix sourced, used with external sources])
AC_DEFINE(KEYPAD_JOYSTICK, 1, [Joystick emulated on keyboard ... should not be true on mobile devices])
dnl Joystick device
AC_CHECK_HEADER(linux/joystick.h, [
AC_DEFINE(LINUX_JOYSTICK, 1, [Enable Linux joystick device])
], [
AC_MSG_WARN([Could not find Linux PC Joystick/GamePad header...])
AC_MSG_WARN([Emulator will be built without Joystick device support, but this may diminish the experience!])
])
dnl ---------------------------------------------------------------------------
AC_CONFIG_FILES([Makefile])

View File

@ -823,8 +823,7 @@ void c_interface_parameters()
#endif
case OPT_JOYSTICK:
snprintf(temp, TEMPSIZE, "%s", (joy_mode == JOY_KPAD) ? "Emulated on Keypad" :
(joy_mode == JOY_PCJOY) ? "PC Joystick " : "Off ");
snprintf(temp, TEMPSIZE, "%s", (joy_mode == JOY_KPAD) ? "Emulated on Keypad" : "PC Joystick ");
break;
case OPT_VOLUME:
@ -1021,11 +1020,6 @@ void c_interface_parameters()
break;
case OPT_JOYSTICK:
if (joy_mode == JOY_PCJOY)
{
c_close_joystick();
}
if (joy_mode == 0)
{
joy_mode = NUM_JOYOPTS-1;
@ -1034,11 +1028,6 @@ void c_interface_parameters()
{
--joy_mode;
}
if (joy_mode == JOY_PCJOY)
{
c_open_joystick();
}
break;
case OPT_CALIBRATE:
@ -1128,11 +1117,6 @@ void c_interface_parameters()
break;
case OPT_JOYSTICK:
if (joy_mode == JOY_PCJOY)
{
c_close_joystick();
}
if (joy_mode == NUM_JOYOPTS-1)
{
joy_mode = 0;
@ -1141,11 +1125,6 @@ void c_interface_parameters()
{
++joy_mode;
}
if (joy_mode == JOY_PCJOY)
{
c_open_joystick();
}
break;
case OPT_CALIBRATE:
@ -1252,13 +1231,11 @@ void c_interface_parameters()
/* calibrate joystick */
if ((ch == 13) && (option == OPT_CALIBRATE))
{
c_close_joystick();
c_open_joystick();
c_joystick_reset();
c_calibrate_joystick();
c_interface_print_screen( screen );
}
#define QUIT_SUBMENU_H 10
#define QUIT_SUBMENU_W 40
{
@ -1294,7 +1271,6 @@ void c_interface_parameters()
c_interface_print_screen( screen );
c_eject_6( 1 );
c_interface_print_screen( screen );
c_close_joystick();
#ifdef __linux__
LOG("Back to Linux, w00t!\n");
#endif

View File

@ -16,8 +16,9 @@
#include "common.h"
#ifdef LINUX_JOYSTICK
#include <linux/joystick.h>
#ifdef INTERFACE_CLASSIC
#define TEMPSIZE 256
extern void copy_and_pad_string(char *dest, const char* src, const char c, const int len, const char cap);
#endif
/* parameters for generic and keyboard-simulated joysticks */
@ -32,238 +33,16 @@ short joy_step = 1;
uint8_t joy_auto_recenter = 0;
#endif
#ifdef LINUX_JOYSTICK
int js_center_x;
int js_center_y;
int js_max_x;
int js_max_y;
int js_min_x;
int js_min_y;
int js_fd = -1; /* joystick file descriptor */
struct JS_DATA_TYPE js; /* joystick data struct */
int raw_js_x;
int raw_js_y;
int js_lowerrange_x,
js_upperrange_x,
js_lowerrange_y,
js_upperrange_y,
js_offset_x,
js_offset_y;
float
js_adjustlow_x,
js_adjustlow_y,
js_adjusthigh_x,
js_adjusthigh_y;
/* -------------------------------------------------------------------------
c_open_pc_joystick() - opens joystick device
------------------------------------------------------------------------- */
static void c_calculate_pc_joystick_parms();
int c_open_pc_joystick()
{
if (js_fd < 0)
{
if ((js_fd = open("/dev/js0", O_RDONLY)) < 0)
{
/* try again with another name */
if ((js_fd = open("/dev/joystick", O_RDONLY)) < 0)
{
if ((js_fd = open("/dev/input/js0", O_RDONLY)) < 0)
{
return 1; /* problem */
}
}
}
}
c_calculate_pc_joystick_parms();
return 0; /* no problem */
}
/* -------------------------------------------------------------------------
c_close_pc_joystick() - closes joystick device
------------------------------------------------------------------------- */
void c_close_pc_joystick()
{
if (js_fd < 0)
{
return;
}
close(js_fd);
js_fd = -1;
}
/* -------------------------------------------------------------------------
* c_calculate_pc_joystick_parms() - calculates parameters for joystick
* device. assumes that device extremes have already been determined.
* ------------------------------------------------------------------------- */
static void c_calculate_pc_joystick_parms()
{
js_lowerrange_x = js_center_x - js_min_x;
js_upperrange_x = js_max_x - js_center_x;
js_lowerrange_y = js_center_y - js_min_y;
js_upperrange_y = js_max_y - js_center_y;
js_offset_x = js_min_x;
js_offset_y = js_min_y;
js_adjustlow_x = (float)HALF_JOY_RANGE / (float)js_lowerrange_x;
js_adjustlow_y = (float)HALF_JOY_RANGE / (float)js_lowerrange_y;
js_adjusthigh_x = (float)HALF_JOY_RANGE / (float)js_upperrange_x;
js_adjusthigh_y = (float)HALF_JOY_RANGE / (float)js_upperrange_y;
}
#ifdef INTERFACE_CLASSIC
/* -------------------------------------------------------------------------
c_calibrate_pc_joystick() - calibrates joystick. determines extreme
and center coordinates. assumes that it can write to the interface
screen.
------------------------------------------------------------------------- */
extern void copy_and_pad_string(char *dest, const char* src, const char c, const int len, const char cap);
static void c_calibrate_pc_joystick()
{
#define JOYERR_PAD 35
#define JOYERR_SUBMENU_H 9
#define JOYERR_SUBMENU_W 40
char errmenu[JOYERR_SUBMENU_H][JOYERR_SUBMENU_W+1] =
//1. 5. 10. 15. 20. 25. 30. 35. 40.
{ "||||||||||||||||||||||||||||||||||||||||",
"| |",
"| An error occurred: |",
"| |",
"| |",
"| Is a joystick device connected? |",
"| Is the proper kernel module loaded? |",
"| |",
"||||||||||||||||||||||||||||||||||||||||" };
/* reset all the extremes */
js_max_x = INT_MIN;
js_max_y = INT_MIN;
js_min_x = INT_MAX;
js_min_y = INT_MAX;
/* open joystick device if not open */
if (js_fd < 0)
{
if (c_open_pc_joystick())
{
const char *err = strerror(errno);
ERRLOG("OOPS, cannot open pc joystick : %s", err);
#ifdef INTERFACE_CLASSIC
copy_and_pad_string(&errmenu[3][2], err, ' ', JOYERR_PAD, ' ');
c_interface_print_submenu_centered(errmenu[0], JOYERR_SUBMENU_W, JOYERR_SUBMENU_H);
while (c_mygetch(1) == -1) {
// ...
}
#endif
return;
}
}
#define TEMPSIZE 256
char temp[TEMPSIZE];
#define CALIBRATE_SUBMENU_H 9
#define CALIBRATE_SUBMENU_W 40
char submenu[CALIBRATE_SUBMENU_H][CALIBRATE_SUBMENU_W+1] =
//1. 5. 10. 15. 20. 25. 30. 35. 40.
{ "||||||||||||||||||||||||||||||||||||||||",
"| |",
"| Move joystick to extremes of x and y |",
"| axes and then center it. |",
"| |",
"| btn1:@ btn2:@ x:@@@@ y:@@@@ |",
"| |",
"| ESC to continue... |",
"||||||||||||||||||||||||||||||||||||||||" };
#define SHOW_JOYSTICK_AXES(MENU, WIDTH, HEIGHT, X, Y) \
snprintf(temp, TEMPSIZE, "%04x", (short)(X)); \
copy_and_pad_string(&MENU[HEIGHT-4][24], temp, ' ', 5, ' '); \
snprintf(temp, TEMPSIZE, "%04x", (short)(Y)); \
copy_and_pad_string(&MENU[HEIGHT-4][32], temp, ' ', 5, ' '); \
c_interface_print_submenu_centered(MENU[0], WIDTH, HEIGHT);
#define SHOW_BUTTONS(MENU, HEIGHT) \
MENU[HEIGHT-4][8] = joy_button0 ? 'X' : ' '; \
MENU[HEIGHT-4][15] = joy_button1 ? 'X' : ' ';
for (;;)
{
int ch = c_mygetch(0);
SHOW_BUTTONS(submenu, CALIBRATE_SUBMENU_H);
SHOW_JOYSTICK_AXES(submenu, CALIBRATE_SUBMENU_W, CALIBRATE_SUBMENU_H, raw_js_x, raw_js_y);
if (js_max_x < raw_js_x)
{
js_max_x = raw_js_x;
}
if (js_max_y < raw_js_y)
{
js_max_y = raw_js_y;
}
if (js_min_x > raw_js_x)
{
js_min_x = raw_js_x;
}
if (js_min_y > raw_js_y)
{
js_min_y = raw_js_y;
}
if (ch == kESC)
{
break;
}
static struct timespec ts = { .tv_sec=0, .tv_nsec=33333333 };
nanosleep(&ts, NULL);
}
js_center_x = raw_js_x;
js_center_y = raw_js_y;
#ifndef NDEBUG
LOG("js_min_x = %d", js_min_x);
LOG("js_min_y = %d", js_min_y);
LOG("js_max_x = %d", js_max_x);
LOG("js_max_y = %d", js_max_y);
LOG("js_center_x = %d", js_center_x);
LOG("js_center_y = %d", js_center_y);
LOG(" ");
#endif
c_calculate_pc_joystick_parms();
#ifndef NDEBUG
LOG("js_lowerrange_x = %d", js_lowerrange_x);
LOG("js_lowerrange_y = %d", js_lowerrange_y);
LOG("js_upperrange_x = %d", js_upperrange_x);
LOG("js_upperrange_y = %d", js_upperrange_y);
LOG(" ");
LOG("js_offset_x = %d", js_offset_x);
LOG("js_offset_y = %d", js_offset_y);
LOG(" ");
LOG("js_adjustlow_x = %f", js_adjustlow_x);
LOG("js_adjustlow_y = %f", js_adjustlow_y);
LOG("js_adjusthigh_x = %f", js_adjusthigh_x);
LOG("js_adjusthigh_y = %f", js_adjusthigh_y);
LOG(" ");
#endif
#define CALIBRATE_JOYMENU_H 20
#define CALIBRATE_JOYMENU_W 40
#define CALIBRATE_TURTLE_X0 4
@ -309,8 +88,14 @@ static void c_calibrate_pc_joystick()
x_last = x_plot;
y_last = y_plot;
SHOW_BUTTONS(joymenu, CALIBRATE_JOYMENU_H);
SHOW_JOYSTICK_AXES(joymenu, CALIBRATE_JOYMENU_W, CALIBRATE_JOYMENU_H, joy_x, joy_y);
joymenu[CALIBRATE_JOYMENU_H-4][8] = joy_button0 ? 'X' : ' ';
joymenu[CALIBRATE_JOYMENU_H-4][15] = joy_button1 ? 'X' : ' ';
snprintf(temp, TEMPSIZE, "%04x", (short)(joy_x));
copy_and_pad_string(&joymenu[CALIBRATE_JOYMENU_H-4][24], temp, ' ', 5, ' ');
snprintf(temp, TEMPSIZE, "%04x", (short)(joy_y));
copy_and_pad_string(&joymenu[CALIBRATE_JOYMENU_H-4][32], temp, ' ', 5, ' ');
c_interface_print_submenu_centered(joymenu[0], CALIBRATE_JOYMENU_W, CALIBRATE_JOYMENU_H);
spinney_idx = (spinney_idx+1) % 4;
@ -323,16 +108,8 @@ static void c_calibrate_pc_joystick()
nanosleep(&ts, NULL);
}
}
#endif // INTERFACE_CLASSIC
#endif // LINUX_JOYSTICK
void c_initialize_joystick(void) {
joy_button0 = 0x0;
joy_button1 = 0x0;
joy_button2 = 0x0;
}
#if defined(KEYPAD_JOYSTICK) && defined(INTERFACE_CLASSIC)
#if defined(KEYPAD_JOYSTICK)
static void c_calibrate_keypad_joystick()
{
@ -440,7 +217,8 @@ static void c_calibrate_keypad_joystick()
nanosleep(&ts, NULL);
}
}
#endif // KEYPAD_JOYSTICK && INTERFACE_CLASSIC
#endif // KEYPAD_JOYSTICK
#endif // INTERFACE_CLASSIC
#ifdef TOUCH_JOYSTICK
// TBD ...
@ -448,46 +226,13 @@ static void c_calibrate_keypad_joystick()
/* ---------------------------------------------------------------------- */
void c_open_joystick()
{
#ifdef LINUX_JOYSTICK
if (joy_mode == JOY_PCJOY)
{
c_open_pc_joystick();
}
#endif
#ifdef KEYPAD_JOYSTICK
if (joy_mode == JOY_KPAD)
{
// NOP
}
#endif
}
void c_close_joystick()
{
#ifdef LINUX_JOYSTICK
c_close_pc_joystick();
#endif
#ifdef KEYPAD_JOYSTICK
if (joy_mode == JOY_KPAD)
{
// NOP
}
#endif
}
#ifdef INTERFACE_CLASSIC
void c_calibrate_joystick()
{
#ifdef LINUX_JOYSTICK
if (joy_mode == JOY_PCJOY)
{
c_calibrate_pc_joystick();
}
#endif
#ifdef KEYPAD_JOYSTICK
if (joy_mode == JOY_KPAD)
@ -498,8 +243,17 @@ void c_calibrate_joystick()
}
#endif // INTERFACE_CLASSIC
extern void gldriver_joystick_reset(void);
void c_joystick_reset()
{
#if !HEADLESS
if (!is_headless) {
gldriver_joystick_reset();
}
#endif
joy_button0 = 0x0;
joy_button1 = 0x0;
joy_button2 = 0x0;
#ifdef KEYPAD_JOYSTICK
if (joy_mode == JOY_KPAD)
{

View File

@ -33,10 +33,9 @@ extern uint8_t joy_auto_recenter;
extern short joy_step;
#endif
void c_initialize_joystick(void);
void c_open_joystick(void);
void c_close_joystick(void);
void c_calibrate_joystick(void);
void c_joystick_reset(void);
#ifdef INTERFACE_CLASSIC
void c_calibrate_joystick(void);
#endif
#endif // whole file

View File

@ -24,12 +24,6 @@ pthread_mutex_t interface_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ui_thread_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cpu_thread_cond = PTHREAD_COND_INITIALIZER;
#ifdef LINUX_JOYSTICK
#include <linux/joystick.h>
extern int raw_js_x;
extern int raw_js_y;
#endif
static int next_key = -1;
static int last_scancode = -1;
bool caps_lock = true; // default enabled because so much breaks otherwise
@ -284,15 +278,9 @@ void c_keys_handle_input(int scancode, int pressed, int is_cooked)
} while(0);
}
// joystick processing
if (joy_mode == JOY_OFF)
{
joy_x = joy_y = 0xFF;
}
#if defined(KEYPAD_JOYSTICK)
// Keypad emulated joystick relies on "raw" keyboard input
else if (joy_mode == JOY_KPAD)
if (joy_mode == JOY_KPAD)
{
bool joy_axis_unpressed = !( key_pressed[SCODE_KPAD_U] || key_pressed[SCODE_KPAD_D] || key_pressed[SCODE_KPAD_L] || key_pressed[SCODE_KPAD_R] ||
key_pressed[SCODE_KPAD_UL] || key_pressed[SCODE_KPAD_DL] || key_pressed[SCODE_KPAD_UR] || key_pressed[SCODE_KPAD_DR] ||
@ -368,51 +356,6 @@ void c_keys_handle_input(int scancode, int pressed, int is_cooked)
}
}
#endif
#if defined(LINUX_JOYSTICK)
else if ((joy_mode == JOY_PCJOY) && !(js_fd < 0))
{
if (read(js_fd, &js, JS_RETURN) == -1)
{
// error
}
raw_js_x = js.x;
raw_js_y = js.y;
int x_val = (js.x < js_center_x)
? (js.x - js_offset_x) * js_adjustlow_x
: (js.x - js_center_x) * js_adjusthigh_x + HALF_JOY_RANGE;
int y_val = (js.y < js_center_y)
? (js.y - js_offset_y) * js_adjustlow_y
: (js.y - js_center_y) * js_adjusthigh_y + HALF_JOY_RANGE;
joy_y = (y_val > 0xff) ? 0xff : (y_val < 0) ? 0 : y_val;
joy_x = (x_val > 0xff) ? 0xff : (x_val < 0) ? 0 : x_val;
/* almost_x = (x_val > 0xff) ? 0xff : (x_val < 0) ? 0 : x_val; */
/* adj_x = (3-(joy_y/0x40)) + 10; */
/* turnpt_x = joy_y + adj_x; */
/* almost_x = (almost_x < turnpt_x) */
/* ? almost_x */
/* : (almost_x - turnpt_x) * adjusthigh_x; */
/* joy_x = (almost_x > 0xff) ? 0xff : (almost_x < 0) ? 0 : almost_x; */
/* sample buttons only if apple keys aren't pressed. keys get set to
* 0xff, and js buttons are set to 0x80. */
if (!(joy_button0 & 0x7f))
{
joy_button0 = (js.buttons & 0x01) ? 0x80 : 0x0;
}
if (!(joy_button1 & 0x7f))
{
joy_button1 = (js.buttons & 0x02) ? 0x80 : 0x0;
}
}
#endif
}
int c_rawkey()

View File

@ -141,13 +141,6 @@ extern pthread_cond_t cpu_thread_cond;
extern pthread_cond_t ui_thread_cond;
extern bool caps_lock;
#ifdef LINUX_JOYSTICK
extern int js_fd;
extern struct JS_DATA_TYPE js;
extern int js_offset_x, js_offset_y;
extern float js_adjustlow_x, js_adjustlow_y, js_adjusthigh_x, js_adjusthigh_y;
#endif
int c_mygetch(int block);
int c_rawkey();
void c_keys_set_key(int key);

View File

@ -582,7 +582,7 @@ void c_initialize_vm() {
c_initialize_sound_hooks(); /* sound system */
c_init_6(); /* drive ][, slot 6 */
c_initialize_iie_switches(); /* set the //e softswitches */
c_initialize_joystick(); /* reset joystick buttons */
c_joystick_reset(); /* reset joystick */
}
/* -------------------------------------------------------------------------

View File

@ -27,7 +27,6 @@
#define PRM_VOLUME 5
#define PRM_JOY_INPUT 6
#define PRM_VIDEO_MODE 7
#define PRM_JOY_PC_CALIBRATE 10
#define PRM_JOY_KPAD_CALIBRATE 11
#define PRM_ROM_PATH 12
#define PRM_CAPSLOCK 102
@ -42,7 +41,7 @@ int sound_volume = 2;
bool is_headless = false;
color_mode_t color_mode = COLOR;
a2_video_mode_t a2_video_mode = VIDEO_1X;
joystick_mode_t joy_mode = JOY_OFF;
joystick_mode_t joy_mode = JOY_PCJOY;
static char *config_filename = NULL;
@ -67,8 +66,6 @@ static const struct match_table prefs_table[] =
{ "caps_lock", PRM_CAPSLOCK },
{ "caps lock", PRM_CAPSLOCK },
{ "joystick", PRM_JOY_INPUT },
{ "pc joystick parms", PRM_JOY_PC_CALIBRATE },
{ "pc_joystick_parms", PRM_JOY_PC_CALIBRATE },
{ "keypad joystick parms", PRM_JOY_KPAD_CALIBRATE },
{ "keypad_joystick_parms", PRM_JOY_KPAD_CALIBRATE },
{ "system path", PRM_ROM_PATH },
@ -120,16 +117,12 @@ static const struct match_table capslock_table[] =
static const struct match_table joy_input_table[] =
{
{ "off", JOY_OFF },
{ "pc joystick", JOY_PCJOY },
#ifdef KEYPAD_JOYSTICK
{ "joy keypad", JOY_KPAD },
{ "joy_keypad", JOY_KPAD },
#endif
#ifdef LINUX_JOYSTICK
{ "pc joystick", JOY_PCJOY },
{ "pc_joystick", JOY_PCJOY },
#endif /* LINUX_JOYSTICK */
{ 0, JOY_OFF }
{ 0, JOY_PCJOY }
};
/* Find the number assigned to KEYWORD in a match table PARADIGM. If no match,
@ -295,41 +288,6 @@ void load_settings(void)
joy_mode = match(joy_input_table, argument);
break;
case PRM_JOY_PC_CALIBRATE:
#ifdef LINUX_JOYSTICK
/* pc joystick parms generated by the calibration routine
(shouldn't need to hand tweak these) = origin_x origin_y max_x
min_x max_y min_y */
js_center_x = strtol(argument, &argument, 10);
js_center_y = strtol(argument, &argument, 10);
js_max_x = strtol(argument, &argument, 10);
if (js_max_x < 0)
{
js_max_x = 0;
}
js_min_x = strtol(argument, &argument, 10);
if (js_min_x < 0)
{
js_min_x = 0;
}
js_max_y = strtol(argument, &argument, 10);
if (js_max_y < 0)
{
js_max_y = 0;
}
js_min_y = strtol(argument, &argument, 10);
if (js_min_y < 0)
{
js_min_y = 0;
}
c_open_joystick();
break;
#endif
#ifdef KEYPAD_JOYSTICK
case PRM_JOY_KPAD_CALIBRATE:
joy_step = strtol(argument, &argument, 10);
@ -422,14 +380,6 @@ bool save_settings(void)
system_path);
anErr = anErr || (err < 0);
#ifdef LINUX_JOYSTICK
err = fprintf(config_file,
"pc joystick parms = %d %d %d %d %d %d\n",
js_center_x, js_center_y, js_max_x, js_min_x,
js_max_y, js_min_y);
#endif
anErr = anErr || (err < 0);
#ifdef KEYPAD_JOYSTICK
err = fprintf(config_file,
"keypad joystick parms = %d %u\n", joy_step, joy_auto_recenter ? 1 : 0);

View File

@ -20,12 +20,9 @@
#include "common.h"
typedef enum joystick_mode_t {
JOY_OFF = 0,
JOY_PCJOY = 0,
#ifdef KEYPAD_JOYSTICK
JOY_KPAD,
#endif
#ifdef LINUX_JOYSTICK
JOY_PCJOY,
#endif
NUM_JOYOPTS
} joystick_mode_t;
@ -60,16 +57,6 @@ extern a2_video_mode_t a2_video_mode;
/* generic joystick settings */
extern joystick_mode_t joy_mode;
#ifdef LINUX_JOYSTICK
/* real joystick settings */
extern int js_center_x;
extern int js_center_y;
extern int js_max_x;
extern int js_max_y;
extern int js_min_x;
extern int js_min_y;
#endif /* LINUX_JOYSTICK */
/* functions in prefs.c */
extern void load_settings(void);
extern bool save_settings(void);

View File

@ -13,6 +13,7 @@
#include "common.h"
#include "video/glinput.h"
#include <math.h>
//----------------------------------------------------------------------------
//
@ -170,6 +171,37 @@ void gldriver_on_key_special_up(int key, int x, int y) {
//LOG("onKeySpecialDown %08x(%d) -> %02X(%d)", key, key, scancode, scancode);
c_keys_handle_input(scancode, 0, 0);
}
#define JOYSTICK_POLL_INTERVAL_MILLIS (ceilf(1000.f/60))
static void gldriver_joystick_callback(unsigned int buttonMask, int x, int y, int z) {
#ifdef KEYPAD_JOYSTICK
if (joy_mode == JOY_KPAD) {
return;
}
#endif
// sample buttons only if apple keys aren't pressed. keys get set to 0xff, and js buttons are set to 0x80.
if (!(joy_button0 & 0x7f)) {
joy_button0 = (buttonMask & 0x01) ? 0x80 : 0x0;
}
if (!(joy_button1 & 0x7f)) {
joy_button1 = (buttonMask & 0x02) ? 0x80 : 0x0;
}
// normalize GLUT range
static const float normalizer = 256.f/2000.f;
joy_x = (uint16_t)((x+1000)*normalizer);
joy_y = (uint16_t)((y+1000)*normalizer);
if (joy_x > 0xFF) {
joy_x = 0xFF;
}
if (joy_y > 0xFF) {
joy_y = 0xFF;
}
}
#endif
//----------------------------------------------------------------------------
@ -198,3 +230,8 @@ void gldriver_mouse(int button, int state, int x, int y) {
}
#endif
void gldriver_joystick_reset(void) {
glutJoystickFunc(NULL, 0);
glutJoystickFunc(gldriver_joystick_callback, (int)JOYSTICK_POLL_INTERVAL_MILLIS);
}

View File

@ -27,3 +27,6 @@ void gldriver_on_key_special_down(int key, int x, int y);
// special key up function
void gldriver_on_key_special_up(int key, int x, int y);
// reset joystick function
void gldriver_joystick_reset(void);

View File

@ -581,7 +581,6 @@ static void gldriver_shutdown(void) {
//
#if USE_GLUT
static void gldriver_update(void) {
// HACK MAYBE FIXME : pumps the joystick sampling code that is currently integrated into the keys routine
c_keys_handle_input(-1, 0, 0);
glutPostRedisplay();
}