/* ______ ___ ___ * /\ _ \ /\_ \ /\_ \ * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ * /\____/ * \_/__/ * By Shawn Hargreaves, * 1 Salisbury Road, * Market Drayton, * Shropshire, * England, TF9 1AJ. * * Joystick routines (the actual polling function is in misc.s). * * Based on code provided by Jonathan Tarbox and Marcel de Kogel. * * CH Flightstick Pro support by Fabian Nunez. * * See readme.txt for copyright information. */ #include #include #include #include "allegro.h" #include "internal.h" /* global joystick position variables */ int joy_x = 0; int joy_y = 0; int joy_left = FALSE; int joy_right = FALSE; int joy_up = FALSE; int joy_down = FALSE; int joy_b1 = FALSE; int joy_b2 = FALSE; int joy_b[5] = {0, 0, 0, 0, 0}; int joy_type = JOY_TYPE_STANDARD; /* CH Flightstick Pro variables */ int joy_b3 = FALSE; int joy_b4 = FALSE; int joy_hat = JOY_HAT_CENTRE; int joy_throttle = 0; /* joystick state information */ #define JOYSTICK_PRESENT 1 #define JOYSTICK_CALIB_TL 2 #define JOYSTICK_CALIB_BR 4 #define JOYSTICK_CALIB_THRTL_MIN 8 #define JOYSTICK_CALIB_THRTL_MAX 16 static int joystick_flags = 0; /* calibrated position values */ static int joycentre_x, joycentre_y; static int joyx_min, joyx_low_margin, joyx_high_margin, joyx_max; static int joyy_min, joyy_low_margin, joyy_high_margin, joyy_max; static int joy_thr_min, joy_thr_max; /* for filtering out bad input values */ static int joy_old_x, joy_old_y; #define MASK_1X 1 #define MASK_1Y 2 #define MASK_2X 4 #define MASK_2Y 8 #define MASK_PLAIN (MASK_1X | MASK_1Y) /* poll_mask: * Returns a mask indicating which axes to poll. */ static int poll_mask() { switch (joy_type) { case JOY_TYPE_FSPRO: return MASK_PLAIN | MASK_2Y; default: return MASK_PLAIN; } } /* poll: * Wrapper function for joystick polling. */ static int poll(int *x, int *y, int *x2, int *y2, int poll_mask) { int ret; enter_critical(); ret = _poll_joystick(x, y, x2, y2, poll_mask); exit_critical(); return ret; } /* averaged_poll: * For calibration it is crucial that we get the right results, so we * average out several attempts. */ static int averaged_poll(int *x, int *y, int *x2, int *y2, int mask) { #define AVERAGE_COUNT 4 int x_tmp, y_tmp, x2_tmp, y2_tmp; int x_total, y_total, x2_total, y2_total; int c; x_total = y_total = x2_total = y2_total = 0; for (c=0; c (joycentre_x*3/2)); joy_up = (y < (joycentre_y/2)); joy_down = (y > ((joycentre_y*3)/2)); } joy_b[1] = ((status & 0x10) == 0); joy_b[2] = ((status & 0x20) == 0); joy_b[3] = ((status & 0x40) == 0); joy_b[4] = ((status & 0x80) == 0); joy_old_x = x; joy_old_y = y; } } #define JOYSTICK_OLD_MAGIC 0x6F6A6C61 #define JOYSTICK_MAGIC 0x796A6C61 /* save_joystick_data: * After calibrating a joystick, this function can be used to save the * information into the specified file, from where it can later be * restored by calling load_joystick_data(). */ int save_joystick_data(char *filename) { PACKFILE *f; f = pack_fopen(filename, F_WRITE); if (!f) return -1; pack_iputl(JOYSTICK_MAGIC, f); pack_iputl(joy_type, f); pack_iputl(joystick_flags, f); pack_iputl(joycentre_x, f); pack_iputl(joycentre_y, f); pack_iputl(joyx_min, f); pack_iputl(joyx_low_margin, f); pack_iputl(joyx_high_margin, f); pack_iputl(joyx_max, f); pack_iputl(joyy_min, f); pack_iputl(joyy_low_margin, f); pack_iputl(joyy_high_margin, f); pack_iputl(joyy_max, f); pack_iputl(joy_thr_min, f); pack_iputl(joy_thr_max, f); pack_fclose(f); return errno; } /* load_joystick_data: * Restores a set of joystick calibration data previously saved by * save_joystick_data(). */ int load_joystick_data(char *filename) { PACKFILE *f; long type; f = pack_fopen(filename, F_READ); if (!f) return -1; type = pack_igetl(f); if ((type != JOYSTICK_MAGIC) && (type != JOYSTICK_OLD_MAGIC)) { pack_fclose(f); return -1; } if (type != JOYSTICK_OLD_MAGIC) joy_type = pack_igetl(f); else joy_type = JOY_TYPE_STANDARD; joystick_flags = pack_igetl(f); joycentre_x = pack_igetl(f); joycentre_y = pack_igetl(f); joyx_min = pack_igetl(f); joyx_low_margin = pack_igetl(f); joyx_high_margin = pack_igetl(f); joyx_max = pack_igetl(f); joyy_min = pack_igetl(f); joyy_low_margin = pack_igetl(f); joyy_high_margin = pack_igetl(f); joyy_max = pack_igetl(f); if (type != JOYSTICK_OLD_MAGIC) { joy_thr_min = pack_igetl(f); joy_thr_max = pack_igetl(f); } pack_fclose(f); joy_old_x = joy_old_y = 0; return errno; }