/* ______ ___ ___ * /\ _ \ /\_ \ /\_ \ * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ * /\____/ * \_/__/ * * Old high level joystick input framework. * * By Shawn Hargreaves. * * Converted into an emulation layer by Peter Wang. * * See readme.txt for copyright information. */ #include "allegro.h" #include "allegro/internal/aintern.h" /* dummy driver for this emulation */ static JOYSTICK_DRIVER joystick_emu = { AL_ID('J','E','M','U'), empty_string, empty_string, "Old joystick API emulation" }; int _joystick_installed = FALSE; JOYSTICK_DRIVER *joystick_driver = NULL; int _joy_type = JOY_TYPE_NONE; JOYSTICK_INFO joy[MAX_JOYSTICKS]; int num_joysticks = 0; static int joy_loading = FALSE; static AL_JOYSTICK *new_joy[MAX_JOYSTICKS]; /* clear_joystick_vars: * Resets the joystick state variables to their default values. */ static void clear_joystick_vars(void) { AL_CONST char *unused = get_config_text("unused"); int i, j, k; #define ARRAY_SIZE(a) ((int)sizeof((a)) / (int)sizeof((a)[0])) for (i=0; i 0) { num_joysticks--; al_release_joystick(new_joy[num_joysticks]); new_joy[num_joysticks] = NULL; } al_uninstall_joystick(); joystick_driver = NULL; _joy_type = JOY_TYPE_NONE; clear_joystick_vars(); _remove_exit_func(remove_joystick); _joystick_installed = FALSE; } } /* convert_joystick_state: * Stores the information in a AL_JOYSTATE structure into an * older-style JOYSTICK_INFO structure. */ static void convert_joystick_state(JOYSTICK_INFO *old_info, AL_JOYSTATE *state) { int axis, i, j; /* axes */ axis = 0; for (i=0; i < old_info->num_sticks; i++) { for (j=0; j < old_info->stick[i].num_axis; j++) { if (old_info->stick[i].flags & JOYFLAG_SIGNED) old_info->stick[i].axis[j].pos = state->stick[i].axis[j] * 128; else old_info->stick[i].axis[j].pos = (state->stick[i].axis[j] + 1.0) * 127.5; if (state->stick[i].axis[j] < -0.5) { old_info->stick[i].axis[j].d1 = TRUE; old_info->stick[i].axis[j].d2 = FALSE; } else if (state->stick[i].axis[j] > +0.5) { old_info->stick[i].axis[j].d1 = FALSE; old_info->stick[i].axis[j].d2 = TRUE; } else { old_info->stick[i].axis[j].d1 = FALSE; old_info->stick[i].axis[j].d2 = FALSE; } } } /* buttons */ for (i = 0; i < old_info->num_buttons; i++) { old_info->button[i].b = state->button[i] ? TRUE : FALSE; } } /* poll_joystick: * Reads the current input state into the joystick status variables. */ int poll_joystick() { AL_JOYSTATE state; int c; if (!_joystick_installed) return -1; for (c=0; csave_data)) joystick_driver->save_data(); */ if (filename) pop_config_state(); return 0; } /* load_joystick_data: * Restores a set of joystick calibration data previously saved by * save_joystick_data(). */ int load_joystick_data(AL_CONST char *filename) { char tmp1[64], tmp2[64]; int ret, c; joy_loading = TRUE; if (_joystick_installed) remove_joystick(); if (filename) { push_config_state(); set_config_file(filename); } _joy_type = get_config_id(uconvert_ascii("joystick", tmp1), uconvert_ascii("joytype", tmp2), -1); if (_joy_type < 0) { _joy_type = JOY_TYPE_NONE; ret = -1; } else { ret = install_joystick(_joy_type); if (ret == 0) { /* if (joystick_driver->load_data) ret = joystick_driver->load_data(); */ } else ret = -2; } if (filename) pop_config_state(); if (ret == 0) { for (c=0; c