Index: dev/vice/trunk/src/arch/sdl/joy.c =================================================================== --- dev/vice/trunk/src/arch/sdl/joy.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/joy.c (copia di lavoro) @@ -48,8 +48,6 @@ #include "log.h" #include "mouse.h" #include "resources.h" -#include "sysfile.h" -#include "util.h" #include "uihotkey.h" #include "uimenu.h" #include "vkbd.h" @@ -64,9 +62,6 @@ static ui_menu_action_t autorepeat; static int autorepeat_delay; -/* Joystick mapping filename */ -static char *joymap_file = NULL; - /* Total number of joysticks */ static int num_joysticks; @@ -76,86 +71,7 @@ /* Joystick fuzz (0..32767) */ static int joystick_fuzz; -/* Different types of joystick input */ -typedef enum { - AXIS = 0, - BUTTON = 1, - HAT = 2, - BALL = 3, - NUM_INPUT_TYPES -} sdljoystick_input_t; -/* Number of actions per input source */ -static const int input_mult[NUM_INPUT_TYPES] = { - 2, /* Axis: actions for positive and negative */ - 1, /* Button */ - 4, /* Hat: actions for all 4 directions */ - 1 /* Ball */ -}; - -/* Actions to perform on joystick input */ -typedef enum { - NONE = 0, - - /* Joystick movement or button press */ - JOYSTICK = 1, - - /* Keyboard key press */ - KEYBOARD = 2, - - /* Map button */ - MAP = 3, - - /* (De)Activate UI */ - UI_ACTIVATE = 4, - - /* Call UI function */ - UI_FUNCTION = 5, - - /* Joystick axis used for potentiometers */ - POT_AXIS = 6 -} sdljoystick_action_t; - -/* Input mapping for each direction/button/etc */ -struct sdljoystick_mapping_s { - /* Action to perform */ - sdljoystick_action_t action; - - /* Previous state of input */ - uint8_t prev; - - union { - /* joy[0] = port number (0,1), joy[1] = pin number */ - uint16_t joy[2]; - - /* axis[0] = port number (0,1), axis[1] = pot number (x=0/y=1) */ - uint16_t axis[2]; - - /* key[0] = row, key[1] = column */ - int key[2]; - - /* pointer to the menu item */ - ui_menu_entry_t *ui_function; - } value; -}; -typedef struct sdljoystick_mapping_s sdljoystick_mapping_t; - -struct sdljoystick_s { - SDL_Joystick *joyptr; - char *name; - int input_max[NUM_INPUT_TYPES]; - sdljoystick_mapping_t *input[NUM_INPUT_TYPES]; -}; -typedef struct sdljoystick_s sdljoystick_t; - -static sdljoystick_t *sdljoystick = NULL; - -/** \brief Temporary copy of the default joymap file name - * - * Avoids silly casting away of const - */ -static char *joymap_factory = NULL; - /* joystick axis mapping for pot-x/y, high byte is the joystick nr, low byte is the axis nr, @@ -194,21 +110,6 @@ return 0; } -static int joymap_file_set(const char *val, void *param) -{ - if (util_string_set(&joymap_file, val)) { - return 0; - } - - return joy_arch_mapping_load(joymap_file); -} - -static resource_string_t resources_string[] = { - { "JoyMapFile", NULL, RES_EVENT_NO, NULL, - &joymap_file, joymap_file_set, (void *)0 }, - RESOURCE_STRING_LIST_END -}; - static const resource_int_t resources_int[] = { { "JoyThreshold", DEFAULT_JOYSTICK_THRESHOLD, RES_EVENT_NO, NULL, &joystick_threshold, set_joystick_threshold, NULL }, @@ -287,12 +188,6 @@ } #ifdef HAVE_SDL_NUMJOYSTICKS - joymap_factory = archdep_default_joymap_file_name(); - resources_string[0].factory_value = joymap_factory; - - if (resources_register_string(resources_string) < 0) { - return -1; - } if (resources_register_int(resources_int) < 0) { return -1; } @@ -301,20 +196,6 @@ return 0; } -void joy_arch_resources_shutdown(void) -{ -#ifdef HAVE_SDL_NUMJOYSTICKS - if (joymap_factory) { - lib_free(joymap_factory); - joymap_factory = NULL; - } - if (joymap_file) { - lib_free(joymap_file); - joymap_file = NULL; - } -#endif -} - int joy_sdl_cmdline_options_init(void) { #ifdef HAVE_SDL_NUMJOYSTICKS @@ -358,6 +239,17 @@ /* ------------------------------------------------------------------------- */ +static void sdl_joystick_poll(int joyport, void* joystick) {} +static void sdl_joystick_close(void* joystick) +{ + SDL_JoystickClose(joystick); +} + +static joystick_driver_t sdl_joystick_driver = { + .poll = sdl_joystick_poll, + .close = sdl_joystick_close +}; + #ifdef HAVE_SDL_NUMJOYSTICKS /********************************************************** @@ -366,11 +258,9 @@ int joy_sdl_init(void) { int i, axis, button, hat, ball; - sdljoystick_input_t j; SDL_Joystick *joy; + char *name; - sdljoystick = NULL; - sdljoy_log = log_open("SDLJoystick"); if (SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { @@ -387,568 +277,43 @@ log_message(sdljoy_log, "%i joysticks found", num_joysticks); - sdljoystick = lib_malloc(sizeof(sdljoystick_t) * num_joysticks); - for (i = 0; i < num_joysticks; ++i) { - joy = sdljoystick[i].joyptr = SDL_JoystickOpen(i); + joy = SDL_JoystickOpen(i); if (joy) { #ifndef USE_SDLUI2 - sdljoystick[i].name = lib_strdup(SDL_JoystickName(i)); + name = lib_strdup(SDL_JoystickName(i)); #else - sdljoystick[i].name = lib_strdup(SDL_JoystickName(sdljoystick[i].joyptr)); + name = lib_strdup(SDL_JoystickName(joy)); #endif - axis = sdljoystick[i].input_max[AXIS] = SDL_JoystickNumAxes(joy); - button = sdljoystick[i].input_max[BUTTON] = SDL_JoystickNumButtons(joy); - hat = sdljoystick[i].input_max[HAT] = SDL_JoystickNumHats(joy); - ball = sdljoystick[i].input_max[BALL] = SDL_JoystickNumBalls(joy); + axis = SDL_JoystickNumAxes(joy); + button = SDL_JoystickNumButtons(joy); + hat = SDL_JoystickNumHats(joy); + ball = SDL_JoystickNumBalls(joy); - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - if (sdljoystick[i].input_max[j] > 0) { - sdljoystick[i].input[j] = lib_malloc(sizeof(sdljoystick_mapping_t) * sdljoystick[i].input_max[j] * input_mult[j]); - } else { - sdljoystick[i].input[j] = NULL; - } - } - - log_message(sdljoy_log, "Device %i \"%s\" (%i axes, %i buttons, %i hats, %i balls)", i, sdljoystick[i].name, axis, button, hat, ball); - - joy_arch_init_default_mapping(i); + log_message(sdljoy_log, "Device %i \"%s\" (%i axes, %i buttons, %i hats, %i balls)", i, name, axis, button, hat, ball); + register_joystick_driver(&sdl_joystick_driver, + name, + joy, + axis, + button, + hat); } else { log_warning(sdljoy_log, "Couldn't open joystick %i", i); } } - joy_arch_mapping_load(joymap_file); - SDL_JoystickEventState(SDL_ENABLE); return 0; } -void joystick(void) -{ - /* Provided only for archdep joy.h. TODO: Migrate joystick polling in here if any needed? */ -} -void joystick_close(void) -{ - int i; - sdljoystick_input_t j; - -#ifdef SDL_DEBUG - fprintf(stderr, "%s\n", __func__); -#endif - - if (sdljoystick == NULL) { - return; - } - - for (i = 0; i < num_joysticks; ++i) { - lib_free(sdljoystick[i].name); - sdljoystick[i].name = NULL; - - if (sdljoystick[i].joyptr) { - SDL_JoystickClose(sdljoystick[i].joyptr); - } - - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - lib_free(sdljoystick[i].input[j]); - sdljoystick[i].input[j] = NULL; - } - } - - lib_free(sdljoystick); - sdljoystick = NULL; -} - /* ------------------------------------------------------------------------- */ -void joy_arch_init_default_mapping(int joynum) +static inline joystick_axis_value_t sdljoy_axis_direction(Sint16 value, joystick_axis_value_t prev) { - int i, j, joyport, pin, state = 0; - - SDL_JoystickUpdate(); - - /* FIXME some joysticks seem to need some time to self-calibrate after the - first time it's opened/polled/read, otherwise the reported axis values - are incorrect and get mapped to NONE. Quitting and restarting seems to - fix this. Loading the joymap later in the init gets around this issue. */ - - for (i = 0; i < sdljoystick[joynum].input_max[AXIS] * input_mult[AXIS]; ++i) { - joyport = ((1 + joynum + ((i & 4) >> 2)) & 1); - pin = 8 >> (i & 3); - - /* Poll each joystick axis once */ - if ((i % input_mult[AXIS]) == 0) { - state = SDL_JoystickGetAxis(sdljoystick[joynum].joyptr, i / input_mult[AXIS]); - } - - /* Check that the default joystick value is within the threshold. - Some devices have axes that are +/-32767 when idle; mapping - those to NONE (by default) avoids some problems. */ - if ((state > joystick_threshold) || (state < -joystick_threshold)) { - log_warning(sdljoy_log, "Axis %i exceeds threshold, mapping to NONE", i / input_mult[AXIS]); - for (j = 0; j < input_mult[AXIS]; ++j) { - sdljoystick[joynum].input[AXIS][i + j].action = NONE; - } - i += (j - 1); - } else { - sdljoystick[joynum].input[AXIS][i].action = JOYSTICK; - sdljoystick[joynum].input[AXIS][i].value.joy[0] = joyport; - sdljoystick[joynum].input[AXIS][i].value.joy[1] = pin; - } - } - - for (i = 0; i < sdljoystick[joynum].input_max[BUTTON] * input_mult[BUTTON]; ++i) { - switch (i & 3) { - case 0: - case 3: - default: - joyport = ((1 + joynum + (i & 1)) & 1); - pin = 1 << 4; - sdljoystick[joynum].input[BUTTON][i].action = JOYSTICK; - sdljoystick[joynum].input[BUTTON][i].value.joy[0] = joyport; - sdljoystick[joynum].input[BUTTON][i].value.joy[1] = pin; - break; - case 1: - sdljoystick[joynum].input[BUTTON][i].action = UI_ACTIVATE; - break; - case 2: - sdljoystick[joynum].input[BUTTON][i].action = MAP; - break; - } - } - - for (i = 0; i < sdljoystick[joynum].input_max[HAT] * input_mult[HAT]; ++i) { - joyport = ((1 + joynum + ((i & 4) >> 2)) & 1); - pin = 1 << (i & 3); - - sdljoystick[joynum].input[HAT][i].action = JOYSTICK; - sdljoystick[joynum].input[HAT][i].value.joy[0] = joyport; - sdljoystick[joynum].input[HAT][i].value.joy[1] = pin; - } - - for (i = 0; i < sdljoystick[joynum].input_max[BALL] * input_mult[BALL]; ++i) { - sdljoystick[joynum].input[BALL][i].action = NONE; - } -} - -static char mapping_retval[50]; - -char *get_joy_pot_mapping_string(int joynr, int pot) -{ - uint16_t portaxis = 0xffff; - uint8_t port = portaxis >> 8; - uint8_t axis = portaxis & 0xff; - char *retval = NULL; - - if (joynr <= 1 && pot <= 1) { - portaxis = sdljoystick_axis_mapping[(joynr << 1) | pot]; - } - - if (port != 255 && axis != 255) { - snprintf(mapping_retval, 50, "J%d, Ax%d", port, axis); - retval = mapping_retval; - } - return retval; -} - -char *get_joy_pin_mapping_string(int joynr, int pin) -{ - int i, k; - sdljoystick_input_t j; - sdljoystick_action_t t; - int valid = 0; - int joy = 0; - int type = 0; - int index = 0; - int sub_index = 0; - char *retval = NULL; - char *type_string = NULL; - char *index_string = NULL; - - for (i = 0; i < num_joysticks; ++i) { - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - t = sdljoystick[i].input[j][k].action; - if (t == JOYSTICK) { - if (sdljoystick[i].input[j][k].value.joy[0] == joynr && sdljoystick[i].input[j][k].value.joy[1] == pin) { - valid++; - joy = i; - type = j; - switch (type) { - case AXIS: - type_string = "Ax"; - index_string = "I"; - index = k / 2; - sub_index = k % 2; - break; - case BUTTON: - type_string = "Bt"; - index_string = NULL; - index = k; - sub_index = 0; - break; - case HAT: - type_string = "Ht"; - index_string = "I"; - index = k / 4; - sub_index = k % 4; - break; - case BALL: - type_string = "Bl"; - index_string = NULL; - index = k; - sub_index = 0; - break; - } - } - } - } - } - } - if (valid > 1) { - retval = "Multiple"; - } - if (valid == 1) { - if (index_string != NULL ) { - snprintf(mapping_retval, 50, "J%d, %s%d, %s%d", joy, type_string, index, index_string, sub_index); - } else { - snprintf(mapping_retval, 50, "J%d, %s%d", joy, type_string, index); - } - retval = mapping_retval; - } - - return retval; -} - -void sdljoy_delete_pot_mapping(int port, int pot) -{ - int i, k; - sdljoystick_input_t j; - sdljoystick_action_t t; - - for (i = 0; i < num_joysticks; ++i) { - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - t = sdljoystick[i].input[j][k].action; - if (t == POT_AXIS) { - if (sdljoystick[i].input[j][k].value.axis[0] == port && sdljoystick[i].input[j][k].value.axis[1] == pot) { - sdljoystick[i].input[j][k].action = NONE; - sdljoystick[i].input[j][k].value.axis[0] = 0; - sdljoystick[i].input[j][k].value.axis[1] = 0; - } - if (port <= 1 && pot <= 1) { - sdljoystick_axis_mapping[(port << 1) | pot] = 0xffff; - } - } - } - } - } -} - -void sdljoy_delete_pin_mapping(int port, int pin) -{ - int i, k; - sdljoystick_input_t j; - sdljoystick_action_t t; - - for (i = 0; i < num_joysticks; ++i) { - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - t = sdljoystick[i].input[j][k].action; - if (t == JOYSTICK) { - if (sdljoystick[i].input[j][k].value.joy[0] == port && sdljoystick[i].input[j][k].value.joy[1] == pin) { - sdljoystick[i].input[j][k].action = NONE; - sdljoystick[i].input[j][k].value.joy[0] = 0; - sdljoystick[i].input[j][k].value.joy[1] = 0; - } - } - } - } - } -} - -int joy_arch_mapping_dump(const char *filename) -{ - FILE *fp; - int i, k; - sdljoystick_input_t j; - sdljoystick_action_t t; - char *hotkey_path = NULL; - -#ifdef SDL_DEBUG - fprintf(stderr, "%s\n", __func__); -#endif - - if (filename == NULL) { - return -1; - } - - fp = fopen(filename, MODE_WRITE_TEXT); - - if (fp == NULL) { - return -1; - } - - fprintf(fp, "# VICE joystick mapping file\n" - "#\n" - "# A joystick map is read in as patch to the current map.\n" - "#\n" - "# File format:\n" - "# - comment lines start with '#'\n" - "# - keyword lines start with '!keyword'\n" - "# - normal line has 'joynum inputtype inputindex action'\n" - "#\n" - "# Keywords and their lines are:\n" - "# '!CLEAR' clear all mappings\n" - "#\n" - ); - - fprintf(fp, "# inputtype:\n" - "# 0 axis\n" - "# 1 button\n" - "# 2 hat\n" - "# 3 ball\n" - "#\n" - "# Note that each axis has 2 inputindex entries and each hat has 4.\n" - "#\n" - "# action [action_parameters]:\n" - "# 0 none\n" - "# 1 port pin joystick (pin: 1/2/4/8/16/32/64/128/256/512/1024/2048 = u/d/l/r/fire(A)/fire2(B)/fire3(X)/Y/LB/RB/select/start)\n" - "# 2 row col keyboard\n" - "# 3 map\n" - "# 4 UI activate\n" - "# 5 path&to&item UI function\n" - "# 6 pot axis joystick (pot: 1/2/3/4 = x1/y1/x2/y2)\n" - "#\n\n" - ); - - fprintf(fp, "!CLEAR\n\n"); - - for (i = 0; i < num_joysticks; ++i) { - fprintf(fp, "# %s\n", sdljoystick[i].name); - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - t = sdljoystick[i].input[j][k].action; - fprintf(fp, "%i %u %i %u", i, j, k, t); - switch (t) { - case JOYSTICK: - fprintf(fp, " %i %i", - sdljoystick[i].input[j][k].value.joy[0], - sdljoystick[i].input[j][k].value.joy[1] - ); - break; - case KEYBOARD: - fprintf(fp, " %i %i", - sdljoystick[i].input[j][k].value.key[0], - sdljoystick[i].input[j][k].value.key[1] - ); - break; - case UI_FUNCTION: - hotkey_path = sdl_ui_hotkey_path(sdljoystick[i].input[j][k].value.ui_function); - fprintf(fp, " %s", hotkey_path); - lib_free(hotkey_path); - break; - case POT_AXIS: - fprintf(fp, " %i %i", - sdljoystick[i].input[j][k].value.axis[0], - sdljoystick[i].input[j][k].value.axis[1] - ); - break; - default: - break; - } - fprintf(fp, "\n"); - } - fprintf(fp, "\n"); - } - fprintf(fp, "\n\n"); - } - - fclose(fp); - - return 0; -} - -static void joy_arch_keyword_clear(void) -{ - int i, k; - sdljoystick_input_t j; - - for (i = 0; i < num_joysticks; ++i) { - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - sdljoystick[i].input[j][k].action = NONE; - } - } - } -} - -static void joy_arch_parse_keyword(char *buffer) -{ - char *key; - - key = strtok(buffer + 1, " \t:"); - - if (!strcmp(key, "CLEAR")) { - joy_arch_keyword_clear(); - } -} - -static void joy_arch_parse_entry(char *buffer) -{ - char *p; - int joynum, inputindex, data1 = 0, data2 = 0; - sdljoystick_input_t inputtype; - sdljoystick_action_t action; - - p = strtok(buffer, " \t:"); - - joynum = atoi(p); - - if (joynum >= num_joysticks) { - log_error(sdljoy_log, "Could not find joystick %i!", joynum); - return; - } - - p = strtok(NULL, " \t,"); - if (p != NULL) { - inputtype = (sdljoystick_input_t)atoi(p); - p = strtok(NULL, " \t,"); - if (p != NULL) { - inputindex = atoi(p); - p = strtok(NULL, " \t"); - if (p != NULL) { - action = (sdljoystick_action_t)atoi(p); - - switch (action) { - case UI_FUNCTION: - p = strtok(NULL, "\t\r\n"); - break; - case JOYSTICK: - case POT_AXIS: - case KEYBOARD: - p = strtok(NULL, " \t"); - data1 = atoi(p); - p = strtok(NULL, " \t"); - data2 = atoi(p); - break; - default: - break; - } - - if (inputindex < sdljoystick[joynum].input_max[inputtype] * input_mult[inputtype]) { - sdljoystick[joynum].input[inputtype][inputindex].action = action; - - switch (action) { - case JOYSTICK: - sdljoystick[joynum].input[inputtype][inputindex].value.joy[0] = data1; - sdljoystick[joynum].input[inputtype][inputindex].value.joy[1] = data2; - break; - case POT_AXIS: - sdljoystick[joynum].input[inputtype][inputindex].value.axis[0] = data1; - sdljoystick[joynum].input[inputtype][inputindex].value.axis[1] = data2; - if (data1 <= 1 && data2 <= 1) { - sdljoystick_axis_mapping[(data1 << 1) | data2] = (joynum << 8) | (inputindex / 2); - resources_set_int_sprintf("PaddlesInput%d", PADDLES_INPUT_JOY_AXIS, data1 + 1); - } - break; - case KEYBOARD: - sdljoystick[joynum].input[inputtype][inputindex].value.key[0] = data1; - sdljoystick[joynum].input[inputtype][inputindex].value.key[1] = data2; - break; - case UI_FUNCTION: - sdljoystick[joynum].input[inputtype][inputindex].value.ui_function = sdl_ui_hotkey_action(p); - break; - default: - break; - } - } else { - log_warning(sdljoy_log, "inputindex %i too large for inputtype %u, joynum %i!", inputindex, inputtype, joynum); - } - } - } - } -} - -int joy_arch_mapping_load(const char *filename) -{ - FILE *fp; - char *complete_path; - char buffer[1000]; - -#ifdef SDL_DEBUG - fprintf(stderr, "%s, %s\n", __func__, filename); -#endif - - /* Silently ignore keymap load on resource & cmdline init */ - if (sdljoystick == NULL) { - return 0; - } - - if (filename == NULL) { - return -1; - } - - fp = sysfile_open(filename, NULL, &complete_path, MODE_READ_TEXT); - - if (fp == NULL) { - log_warning(sdljoy_log, "Failed to open `%s'.", filename); - return -1; - } - - log_message(sdljoy_log, "Loading joystick map `%s'.", complete_path); - - lib_free(complete_path); - - do { - buffer[0] = 0; - if (fgets(buffer, 999, fp)) { - char *p; - - if (strlen(buffer) == 0) { - break; - } - - buffer[strlen(buffer) - 1] = 0; /* remove newline */ - - /* remove comments */ - if ((p = strchr(buffer, '#'))) { - *p = 0; - } - - /* remove whitespace at the beginning of the line */ - p = buffer; - while (((*p == ' ') || (*p == '\t')) && (*p != 0)) { - ++p; - } - - switch (*p) { - case 0: - break; - case '!': - /* keyword handling */ - joy_arch_parse_keyword(p); - break; - default: - /* table entry handling */ - joy_arch_parse_entry(p); - break; - } - } - } while (!feof(fp)); - fclose(fp); - - return 0; -} - -/* ------------------------------------------------------------------------- */ - -static inline uint8_t sdljoy_axis_direction(Sint16 value, uint8_t prev) -{ int thres = joystick_threshold; - if (prev == 0) { + if (prev == JOY_AXIS_MIDDLE) { thres += joystick_fuzz; } else { thres -= joystick_fuzz; @@ -955,11 +320,11 @@ } if (value < -thres) { - return 2; + return JOY_AXIS_NEGATIVE; } else if (value > thres) { - return 1; + return JOY_AXIS_POSITIVE; } else if ((value < thres) && (value > -thres)) { - return 0; + return JOY_AXIS_MIDDLE; } return prev; @@ -989,28 +354,26 @@ return 0; } -static sdljoystick_mapping_t *sdljoy_get_mapping(SDL_Event e) +static joystick_mapping_t *sdljoy_get_mapping(SDL_Event e) { - sdljoystick_mapping_t *retval = NULL; + joystick_mapping_t *retval = NULL; uint8_t cur; switch (e.type) { case SDL_JOYAXISMOTION: cur = sdljoy_axis_direction(e.jaxis.value, 0); - if (cur > 0) { - --cur; - retval = &(sdljoystick[e.jaxis.which].input[AXIS][e.jaxis.axis * input_mult[AXIS] + cur]); + if (cur != JOY_AXIS_MIDDLE) { + retval = joy_get_axis_mapping(e.jaxis.which, e.jaxis.value, cur); } break; case SDL_JOYHATMOTION: cur = sdljoy_hat_direction(e.jhat.value, 0); if (cur > 0) { - --cur; - retval = &(sdljoystick[e.jhat.which].input[HAT][e.jhat.hat * input_mult[HAT] + cur]); + retval = joy_get_hat_mapping(e.jhat.which, e.jhat.hat, cur); } break; case SDL_JOYBUTTONDOWN: - retval = &(sdljoystick[e.jbutton.which].input[BUTTON][e.jbutton.button]); + retval = joy_get_button_mapping(e.jbutton.which, e.jbutton.button); break; default: break; @@ -1018,7 +381,7 @@ return retval; } -static ui_menu_action_t sdljoy_perform_event(sdljoystick_mapping_t *event, int value) +static joystick_action_t sdljoy_perform_event(joystick_mapping_t *event, int value) { uint8_t t; ui_menu_action_t retval = MENU_ACTION_NONE; @@ -1025,9 +388,8 @@ autorepeat = MENU_ACTION_NONE; - if (sdl_menu_state || (sdl_vkbd_state & SDL_VKBD_ACTIVE)) { if (event->action == JOYSTICK) { - switch (event->value.joy[1]) { + switch (event->value.joy_pin) { case 0x01: retval = autorepeat = MENU_ACTION_UP; break; @@ -1057,40 +419,6 @@ retval += MENU_ACTION_NONE_RELEASE; } return retval; - } - - autorepeat_delay = 30; - - switch (event->action) { - case JOYSTICK: - t = event->value.joy[0]; - if (joystick_port_map[t] == JOYDEV_JOYSTICK) { - if (value) { - joystick_set_value_or(t, (uint16_t)event->value.joy[1]); - } else { - joystick_set_value_and(t, (uint16_t) ~(event->value.joy[1])); - } - } - break; - case KEYBOARD: - keyboard_set_keyarr_any(event->value.key[0], event->value.key[1], value); - break; - case UI_ACTIVATE: - if (value) { - sdl_ui_activate(); - } - break; - case UI_FUNCTION: - if (value && event->value.ui_function) { - sdl_ui_hotkey(event->value.ui_function); - } - break; - case NONE: - default: - break; - } - - return retval; } /* ------------------------------------------------------------------------- */ @@ -1110,156 +438,58 @@ uint8_t sdljoy_check_axis_movement(SDL_Event e) { - uint8_t cur, prev; - int index; - Uint8 joynum; - Uint8 axis; - Sint16 value; - - joynum = e.jaxis.which; - axis = e.jaxis.axis; - value = e.jaxis.value; - - index = axis * input_mult[AXIS]; - prev = sdljoystick[joynum].input[AXIS][index].prev; - - cur = sdljoy_axis_direction(value, prev); - - if (cur == prev) { - return 0; - } - - sdljoystick[joynum].input[AXIS][index].prev = cur; - return cur; + joy_axis_event(e.jaxis.which, e.jaxis.axis, e.jaxis.value); + return e.jaxis.value; } uint8_t sdljoy_check_hat_movement(SDL_Event e) { - uint8_t cur, prev; - int index; - Uint8 joynum; - Uint8 hat; - Uint8 value; - - joynum = e.jhat.which; - hat = e.jhat.hat; - value = e.jhat.value; - - index = hat * input_mult[HAT]; - prev = sdljoystick[joynum].input[HAT][index].prev; - - if (value == prev) { - return 0; - } - - cur = sdljoy_hat_direction(value, prev); - - sdljoystick[joynum].input[HAT][index].prev = value; - return cur; + joy_hat_event(e.jhat.which, e.jhat.hat, e.jhat.value); + return e.jhat.value; } -ui_menu_action_t sdljoy_axis_event(Uint8 joynum, Uint8 axis, Sint16 value) +void sdljoy_axis_event(Uint8 joynum, Uint8 axis, Sint16 value) { int i; - uint8_t cur, prev; + joystick_axis_value_t cur, prev; int index; ui_menu_action_t retval = MENU_ACTION_NONE; Sint16 val = ~value; - for (i = 0; i < 4; i++) { - if (sdljoystick_axis_mapping[i] == ((joynum << 8) | axis)) { - joystick_set_axis_value(i, (uint8_t)((val + 32768) >> 8)); - } - } + prev = joy_axis_prev(joynum, index); - index = axis * input_mult[AXIS]; - prev = sdljoystick[joynum].input[AXIS][index].prev; - cur = sdljoy_axis_direction(value, prev); + + joy_axis_event(joynum, axis, cur); +} - if (cur == prev) { - return retval; - } +joystick_action_t sdljoy_axis_event_for_menu_action(Uint8 joynum, Uint8 axis, Sint16 value) +{ + int i; + joystick_axis_value_t cur, prev; + int index; + joystick_action_t retval = MENU_ACTION_NONE; + Sint16 val = ~value; + joystick_mapping_t* mapping; - if (cur == 1) { - if (prev == 2) { - sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index + 1]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index]), 1); - } else if (cur == 2) { - if (prev == 1) { - sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index + 1]), 1); - } else { - if (prev == 1) { - sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index]), 0); - } else { - sdljoy_perform_event(&(sdljoystick[joynum].input[AXIS][index + 1]), 0); - } - } + prev = joy_axis_prev(joynum, index); - sdljoystick[joynum].input[AXIS][index].prev = cur; - return retval; + cur = sdljoy_axis_direction(value, prev); + + mapping = joy_get_axis_mapping(joynum, axis, cur); + return sdljoy_perform_event(mapping, cur); } -ui_menu_action_t sdljoy_button_event(Uint8 joynum, Uint8 button, Uint8 value) +joystick_action_t sdljoy_button_event(Uint8 joynum, Uint8 button, Uint8 value) { - return sdljoy_perform_event(&(sdljoystick[joynum].input[BUTTON][button]), value); + joystick_mapping_t* mapping = joy_get_button_mapping(joynum, button); + return sdljoy_perform_event(mapping, value); } -ui_menu_action_t sdljoy_hat_event(Uint8 joynum, Uint8 hat, Uint8 value) +joystick_action_t sdljoy_hat_event(Uint8 joynum, Uint8 hat, Uint8 value) { - uint8_t prev; - int index; - ui_menu_action_t retval = MENU_ACTION_NONE; - - index = hat * input_mult[HAT]; - prev = sdljoystick[joynum].input[HAT][index].prev; - - if (value == prev) { - return retval; - } - - if ((value & SDL_HAT_UP) && (!(prev & SDL_HAT_UP))) { - if (prev & SDL_HAT_DOWN) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 1]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index]), 1); - } else if ((value & SDL_HAT_DOWN) && (!(prev & SDL_HAT_DOWN))) { - if (prev & SDL_HAT_UP) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 1]), 1); - } else { - if ((!(value & SDL_HAT_UP)) && (prev & SDL_HAT_UP)) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index]), 0); - } else if ((!(value & SDL_HAT_DOWN)) && (prev & SDL_HAT_DOWN)) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 1]), 0); - } - } - - /* TODO a retval is missed on simultaneous X/Y events */ - if ((value & SDL_HAT_LEFT) && (!(prev & SDL_HAT_LEFT))) { - if (prev & SDL_HAT_RIGHT) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 3]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 2]), 1); - } else if ((value & SDL_HAT_RIGHT) && (!(prev & SDL_HAT_RIGHT))) { - if (prev & SDL_HAT_LEFT) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 2]), 0); - } - retval = sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 3]), 1); - } else { - if ((!(value & SDL_HAT_LEFT)) && (prev & SDL_HAT_LEFT)) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 2]), 0); - } else if ((!(value & SDL_HAT_RIGHT)) && (prev & SDL_HAT_RIGHT)) { - sdljoy_perform_event(&(sdljoystick[joynum].input[HAT][index + 3]), 0); - } - } - - sdljoystick[joynum].input[HAT][index].prev = value; - return retval; + joystick_mapping_t* mapping = joy_get_hat_mapping(joynum, hat, value); + return sdljoy_perform_event(mapping, 1); } /* ------------------------------------------------------------------------- */ @@ -1279,38 +509,11 @@ } #endif -void sdljoy_set_joystick(SDL_Event e, int port, int bits) -{ - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); +/* ------------------------------------------------------------------------- */ - if (joyevent != NULL) { - joyevent->action = JOYSTICK; - joyevent->value.joy[0] = (uint16_t)port; - joyevent->value.joy[1] = (uint16_t)bits; - } -} - -void sdljoy_set_joystick_axis(SDL_Event e, int port, int pot) -{ - int index = (port << 1) | pot; - uint8_t stick = e.jaxis.which; - uint8_t axis = e.jaxis.axis; - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); - - if (joyevent != NULL) { - joyevent->action = POT_AXIS; - joyevent->value.axis[0] = (uint16_t)port; - joyevent->value.axis[1] = (uint16_t)pot; - } - - if (index <= 3) { - sdljoystick_axis_mapping[index] = (stick << 8) | axis; - } -} - void sdljoy_set_hotkey(SDL_Event e, ui_menu_entry_t *value) { - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); + joystick_mapping_t *joyevent = sdljoy_get_mapping(e); if (joyevent != NULL) { joyevent->action = UI_FUNCTION; @@ -1320,7 +523,7 @@ void sdljoy_set_keypress(SDL_Event e, int row, int col) { - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); + joystick_mapping_t *joyevent = sdljoy_get_mapping(e); if (joyevent != NULL) { joyevent->action = KEYBOARD; @@ -1329,18 +532,9 @@ } } -void sdljoy_set_extra(SDL_Event e, int type) -{ - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); - - if (joyevent != NULL) { - joyevent->action = type ? MAP : UI_ACTIVATE; - } -} - void sdljoy_unset(SDL_Event e) { - sdljoystick_mapping_t *joyevent = sdljoy_get_mapping(e); + joystick_mapping_t *joyevent = sdljoy_get_mapping(e); if (joyevent != NULL) { joyevent->action = NONE; @@ -1347,39 +541,6 @@ } } -/* ------------------------------------------------------------------------- */ - -static int _sdljoy_swap_ports = 0; - -void sdljoy_swap_ports(void) -{ - int i, k; - sdljoystick_input_t j; - - resources_get_int("JoyDevice1", &i); - resources_get_int("JoyDevice2", &k); - resources_set_int("JoyDevice1", k); - resources_set_int("JoyDevice2", i); - _sdljoy_swap_ports ^= 1; - - for (i = 0; i < num_joysticks; ++i) { - for (j = AXIS; j < NUM_INPUT_TYPES; ++j) { - for (k = 0; k < sdljoystick[i].input_max[j] * input_mult[j]; ++k) { - if (sdljoystick[i].input[j][k].action == JOYSTICK) { - sdljoystick[i].input[j][k].value.joy[0] ^= 1; - } - } - } - } -} - -int sdljoy_get_swap_ports(void) -{ - return _sdljoy_swap_ports; -} - -/* ------------------------------------------------------------------------- */ - #else /* !HAVE_SDL_NUMJOYSTICKS */ Index: dev/vice/trunk/src/arch/sdl/joy.h =================================================================== --- dev/vice/trunk/src/arch/sdl/joy.h (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/joy.h (copia di lavoro) @@ -40,15 +40,17 @@ extern void joystick_close(void); extern void joystick(void); -extern void joy_arch_resources_shutdown(void); +#ifdef HAVE_SDL_NUMJOYSTICKS +struct joystick_mapping_s; +enum joystick_action_e; -#ifdef HAVE_SDL_NUMJOYSTICKS extern void joy_arch_init_default_mapping(int joynum); extern int joy_arch_mapping_load(const char *filename); extern int joy_arch_mapping_dump(const char *filename); -extern ui_menu_action_t sdljoy_axis_event(Uint8 joynum, Uint8 axis, Sint16 value); -extern ui_menu_action_t sdljoy_button_event(Uint8 joynum, Uint8 button, Uint8 value); -extern ui_menu_action_t sdljoy_hat_event(Uint8 joynum, Uint8 hat, Uint8 value); +extern void sdljoy_axis_event(Uint8 joynum, Uint8 axis, Sint16 value); +extern enum joystick_action_e sdljoy_button_event(Uint8 joynum, Uint8 button, Uint8 value); +extern enum joystick_action_e sdljoy_hat_event(Uint8 joynum, Uint8 hat, Uint8 value); +extern enum joystick_action_e sdljoy_axis_event_for_menu_action(Uint8 joynum, Uint8 axis, Sint16 value); extern ui_menu_action_t sdljoy_autorepeat(void); extern uint8_t sdljoy_check_axis_movement(SDL_Event e); extern uint8_t sdljoy_check_hat_movement(SDL_Event e); Index: dev/vice/trunk/src/arch/sdl/kbd.c =================================================================== --- dev/vice/trunk/src/arch/sdl/kbd.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/kbd.c (copia di lavoro) @@ -474,7 +474,7 @@ return ret; } -ui_menu_action_t sdlkbd_press(SDLKey key, SDLMod mod) +void sdlkbd_press(SDLKey key, SDLMod mod) { ui_menu_action_t i, retval = MENU_ACTION_NONE; ui_menu_entry_t *hotkey_action = NULL; @@ -500,36 +500,20 @@ } #endif - if (sdl_menu_state || (sdl_vkbd_state & SDL_VKBD_ACTIVE)) { - if (key != SDLK_UNKNOWN) { - for (i = MENU_ACTION_UP; i < MENU_ACTION_NUM; ++i) { - if (sdl_ui_menukeys[i] == (int)key) { - retval = i; - break; - } - } - if ((int)(key) == sdl_ui_menukeys[0]) { - retval = MENU_ACTION_EXIT; - } - } - return retval; - } - if ((int)(key) == sdl_ui_menukeys[0]) { sdl_ui_activate(); - return retval; + return; } if ((hotkey_action = sdlkbd_get_hotkey(key, mod)) != NULL) { sdl_ui_hotkey(hotkey_action); - return retval; + return; } keyboard_key_pressed((unsigned long)key, sdlkbd_get_modifier(mod)); - return retval; } -ui_menu_action_t sdlkbd_release(SDLKey key, SDLMod mod) +void sdlkbd_release(SDLKey key, SDLMod mod) { ui_menu_action_t i, retval = MENU_ACTION_NONE_RELEASE; @@ -549,7 +533,18 @@ } #endif - if (sdl_vkbd_state & SDL_VKBD_ACTIVE) { + keyboard_key_released((unsigned long)key, sdlkbd_get_modifier(mod)); +} + +ui_menu_action_t sdlkbd_press_for_menu_action(SDLKey key, SDLMod mod) +{ + ui_menu_action_t i, retval = MENU_ACTION_NONE; + ui_menu_entry_t *hotkey_action = NULL; + +#ifdef SDL_DEBUG + log_debug("%s: %i (%s),%04x", __func__, key, SDL_GetKeyName(key), mod); +#endif + if (key != SDLK_UNKNOWN) { for (i = MENU_ACTION_UP; i < MENU_ACTION_NUM; ++i) { if (sdl_ui_menukeys[i] == (int)key) { @@ -557,12 +552,30 @@ break; } } + if ((int)(key) == sdl_ui_menukeys[0]) { + retval = MENU_ACTION_EXIT; + } } + return retval; +} + +ui_menu_action_t sdlkbd_release_for_menu_action(SDLKey key, SDLMod mod) +{ + ui_menu_action_t i, retval = MENU_ACTION_NONE_RELEASE; + +#ifdef SDL_DEBUG + log_debug("%s: %i (%s),%04x", __func__, key, SDL_GetKeyName(key), mod); +#endif + + if (key != SDLK_UNKNOWN) { + for (i = MENU_ACTION_UP; i < MENU_ACTION_NUM; ++i) { + if (sdl_ui_menukeys[i] == (int)key) { + retval = i; + break; + } + } + } return retval + MENU_ACTION_NONE_RELEASE; - } - - keyboard_key_released((unsigned long)key, sdlkbd_get_modifier(mod)); - return retval; } /* ------------------------------------------------------------------------ */ Index: dev/vice/trunk/src/arch/sdl/kbd.h =================================================================== --- dev/vice/trunk/src/arch/sdl/kbd.h (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/kbd.h (copia di lavoro) @@ -76,8 +76,10 @@ extern SDLKey SDL2x_to_SDL1x_Keys(SDLKey key); extern SDLKey SDL1x_to_SDL2x_Keys(SDLKey key); -extern ui_menu_action_t sdlkbd_press(SDLKey key, SDLMod mod); -extern ui_menu_action_t sdlkbd_release(SDLKey key, SDLMod mod); +extern void sdlkbd_press(SDLKey key, SDLMod mod); +extern void sdlkbd_release(SDLKey key, SDLMod mod); +ui_menu_action_t sdlkbd_press_for_menu_action(SDLKey key, SDLMod mod); +ui_menu_action_t sdlkbd_release_for_menu_action(SDLKey key, SDLMod mod); extern void sdlkbd_set_hotkey(SDLKey key, SDLMod mod, ui_menu_entry_t *value); Index: dev/vice/trunk/src/arch/sdl/menu_settings.c =================================================================== --- dev/vice/trunk/src/arch/sdl/menu_settings.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/menu_settings.c (copia di lavoro) @@ -30,7 +30,7 @@ #include "vice_sdl.h" #include -#include "joy.h" +#include "joystick.h" #include "kbd.h" #include "keyboard.h" #include "lib.h" Index: dev/vice/trunk/src/arch/sdl/ui.c =================================================================== --- dev/vice/trunk/src/arch/sdl/ui.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/ui.c (copia di lavoro) @@ -41,6 +41,7 @@ #include "color.h" #include "fullscreenarch.h" #include "joy.h" +#include "joystick.h" #include "kbd.h" #include "keyboard.h" #include "lib.h" @@ -206,268 +207,72 @@ #endif /* Main event handler */ -ui_menu_action_t ui_dispatch_events(void) +void ui_dispatch_events(void) { SDL_Event e; - ui_menu_action_t retval = MENU_ACTION_NONE; - -#ifdef ANDROID_COMPILE - struct locnet_al_event event1; - - if (loader_showinfo) { - int value = loader_showinfo; - - loader_showinfo = 0; - loader_set_statusbar((value == 1) ? 1 : 0); - } - if (loader_true_drive) { - int value = loader_true_drive; - - loader_true_drive = 0; - loader_set_drive_true_emulation((value == 1) ? 1 : 0); - } - if (loader_turbo) { - int value = loader_turbo; - - loader_turbo = 0; - loader_set_warpmode((value == 1) ? 1 : 0); - } - if (loadf->abort) { - loadf->abort = 0; - ui_pause_enable(); - ui_sdl_quit(); - ui_pause_disable(); /* does this even get called after ui_sdl_quit()? */ - return MENU_ACTION_NONE; - } else if (loader_loadstate) { - loader_loadstate = 0; - loader_load_snapshot(savestate_filename); - ui_pause_disable(); - return MENU_ACTION_NONE; - } else if (loader_savestate) { - loader_savestate = 0; - loader_save_snapshot(savestate_filename); - ui_pause_disable(); - return MENU_ACTION_NONE; - } - - int stopPoll = 0; - - while ((!stopPoll) && Android_PollEvent(&event1)) { - struct locnet_al_event *event = &event1; - - switch (event->eventType) { - case SDL_MOUSEMOTION: - { - /* detect auto calibrate */ - if ((event->x == -2048) && (event->y == -2048)) { - down_x = -1; - down_y = -1; - oldx = 0; - oldy = 0; - stopPoll = 1; - /* detect pure relative move */ - } else if ((event->down_x == -1024) && (event->down_y == -1024)) { - down_x = 0; - down_y = 0; - oldx = 0; - oldy = 0; - } else if ((down_x != event->down_x) || (down_y != event->down_y)) { - down_x = event->down_x; - down_y = event->down_y; - oldx = down_x; - oldy = down_y; - } - mouse_move((int)(event->x - oldx), (int)(event->y - oldy)); - oldx = event->x; - oldy = event->y; - } + while (SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_KEYDOWN: + ui_display_kbd_status(&e); + sdlkbd_press(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); break; - case SDL_MOUSEBUTTONDOWN: - { - if ((event->down_x >= 0) && (event->down_y >= 0)) { - mouse_x = 640 * event->down_x / 1000.0f - 64; - mouse_y = 400 * (1000 - event->down_y) / 1000.0f - 200; - } - if (event->keycode >= 0) { - mouse_button((int)(event->keycode) ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT, 1); - } - stopPoll = 1; - } + case SDL_KEYUP: + ui_display_kbd_status(&e); + sdlkbd_release(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); break; - case SDL_MOUSEBUTTONUP: - { - if (event->keycode >= 0) { - mouse_button((int)(event->keycode) ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT, 0); - } - stopPoll = 1; - } - break; +#ifdef HAVE_SDL_NUMJOYSTICKS case SDL_JOYAXISMOTION: - { - float x = event->x / 256.0f; - float y = event->y / 256.0f; - int left = 0, top = 0, right = 0, bot = 0; - int value; - - if (y < -DEAD_ZONE) { - top = 1; - } - if (y > DEAD_ZONE) { - bot = 1; - } - if (x < -DEAD_ZONE) { - left = 1; - } - if (x > DEAD_ZONE) { - right = 1; - } - - value = 0; - - if (left) { - value |= 4; - } - if (right) { - value |= 8; - } - if (top) { - value |= 1; - } - if (bot) { - value |= 2; - } - retval = sdljoy_axis_event(0, 0, event->x / 256.0f * 32767); - ui_menu_action_t retval2 = sdljoy_axis_event(0, 1, event->y / 256.0f * 32767); - if (retval == MENU_ACTION_NONE) { - retval = retval2; - } - old_joy_direction = value; - stopPoll = 1; - } + sdljoy_axis_event(e.jaxis.which, e.jaxis.axis, e.jaxis.value); break; case SDL_JOYBUTTONDOWN: - { - retval = sdljoy_button_event(0, event->keycode, 1); - - /* buffer overflow when autofire if stopPoll */ - if (!Android_HasRepeatEvent(SDL_JOYBUTTONDOWN, event->keycode)) { - stopPoll = 1; - } - } + joy_button_event(e.jbutton.which, e.jbutton.button, 1); break; case SDL_JOYBUTTONUP: - { - retval = sdljoy_button_event(0, event->keycode, 0); - - /* buffer overflow when autofire if stopPoll */ - if (!Android_HasRepeatEvent(SDL_JOYBUTTONUP, event->keycode)) { - stopPoll = 1; - } + joy_button_event(e.jbutton.which, e.jbutton.button, 0); + break; + case SDL_JOYHATMOTION: + joy_hat_event(e.jhat.which, e.jhat.hat, e.jhat.value); + break; +#endif + case SDL_MOUSEMOTION: + sdl_ui_consume_mouse_event(&e); + if (_mouse_enabled) { + mouse_move(e.motion.xrel, e.motion.yrel); } break; - case SDL_KEYUP: - case SDL_KEYDOWN: - { - static int ctrl_down = 0; - static int alt_down = 0; - static int shift_down = 0; - - int down = (event->eventType == SDL_KEYDOWN); - unsigned long modifier = event->modifier; - int ctrl = ((modifier & KEYBOARD_CTRL_FLAG) != 0); - int alt = ((modifier & KEYBOARD_ALT_FLAG) != 0); - int shift = ((modifier & KEYBOARD_SHIFT_FLAG) != 0); - unsigned long kcode = (unsigned long)event->keycode; - - int kmod = 0; - - if (ctrl) { - kmod |= KMOD_LCTRL; - } - if (alt) { - kmod |= KMOD_LALT; - } - if (shift) { - kmod |= KMOD_LSHIFT; - } - if (down) { - if (ctrl || (kcode == SDLK_TAB)) { - if (!ctrl_down) { - keyboard_key_pressed((unsigned long)SDLK_TAB); - } - ctrl_down++; - } - if (alt || (kcode == SDLK_LCTRL)) { - if (!alt_down) { - keyboard_key_pressed((unsigned long)SDLK_LCTRL); - } - alt_down++; - } - if (shift || (kcode == SDLK_LSHIFT)) { - if (!shift_down) { - keyboard_key_pressed((unsigned long)SDLK_LSHIFT); - } - shift_down++; - } - ui_display_kbd_status(event); - retval = sdlkbd_press(kcode, 0); - } else { - ui_display_kbd_status(event); - retval = sdlkbd_release(kcode, 0); - - if (ctrl || (kcode == SDLK_TAB)) { - if (kcode == SDLK_TAB) { - ctrl_down = 0; - } - if (ctrl_down) { - ctrl_down--; - } - if (!ctrl_down) { - keyboard_key_released((unsigned long)SDLK_TAB); - } - } - if (alt || (kcode == SDLK_LCTRL)) { - if (kcode == SDLK_LCTRL) { - alt_down = 0; - } - if (alt_down) { - alt_down--; - } - if (!alt_down) { - keyboard_key_released((unsigned long)SDLK_LCTRL); - } - } - if (shift || (kcode == SDLK_LSHIFT)) { - if (kcode == SDLK_LSHIFT) { - shift_down = 0; - } - if (shift_down) { - shift_down--; - } - if (!shift_down) { - keyboard_key_released((unsigned long)SDLK_LSHIFT); - } - } - } - stopPoll = 1; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + if (_mouse_enabled) { + mouse_button((int)(e.button.button), (e.button.state == SDL_PRESSED)); } break; + default: + /* SDL_EventState(SDL_VIDEORESIZE, SDL_IGNORE); */ + ui_handle_misc_sdl_event(e); + /* SDL_EventState(SDL_VIDEORESIZE, SDL_ENABLE); */ + break; } } -#else +} + +joystick_action_t ui_dispatch_events_for_menu_action(void) +{ + SDL_Event e; + ui_menu_action_t retval = MENU_ACTION_NONE; + while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_KEYDOWN: ui_display_kbd_status(&e); - retval = sdlkbd_press(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); + retval = sdlkbd_press_for_menu_action(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); break; case SDL_KEYUP: ui_display_kbd_status(&e); - retval = sdlkbd_release(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); + retval = sdlkbd_release_for_menu_action(SDL2x_to_SDL1x_Keys(e.key.keysym.sym), e.key.keysym.mod); break; #ifdef HAVE_SDL_NUMJOYSTICKS case SDL_JOYAXISMOTION: - retval = sdljoy_axis_event(e.jaxis.which, e.jaxis.axis, e.jaxis.value); + retval = sdljoy_axis_event_for_menu_action(e.jaxis.which, e.jaxis.axis, e.jaxis.value); break; case SDL_JOYBUTTONDOWN: retval = sdljoy_button_event(e.jbutton.which, e.jbutton.button, 1); @@ -503,7 +308,6 @@ break; } } -#endif return retval; } @@ -782,7 +586,6 @@ void ui_resources_shutdown(void) { DBG(("%s", __func__)); - joy_arch_resources_shutdown(); sdlkbd_resources_shutdown(); } Index: dev/vice/trunk/src/arch/sdl/ui.h =================================================================== --- dev/vice/trunk/src/arch/sdl/ui.h (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/ui.h (copia di lavoro) @@ -50,9 +50,11 @@ struct video_canvas_s; struct palette_s; +enum joystick_action_e; extern void ui_handle_misc_sdl_event(SDL_Event e); -extern ui_menu_action_t ui_dispatch_events(void); +extern void ui_dispatch_events(void); +extern enum joystick_action_e ui_dispatch_events_for_menu_action(void); extern void ui_exit(void); extern void ui_message(const char *format, ...); extern char *ui_select_file(const char *title, char *(*read_contents_func)(const char *, unsigned int unit), unsigned int unit, Index: dev/vice/trunk/src/arch/sdl/uimenu.c =================================================================== --- dev/vice/trunk/src/arch/sdl/uimenu.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/uimenu.c (copia di lavoro) @@ -36,6 +36,7 @@ #include "interrupt.h" #include "ioutil.h" #include "joy.h" +#include "joystick.h" #include "kbd.h" #include "lib.h" #include "machine.h" @@ -952,7 +953,7 @@ { SDL_Event e; int got_key = 0; - ui_menu_action_t action = MENU_ACTION_NONE; + joystick_action_t action = NONE; #ifdef USE_SDLUI2 int i; #endif @@ -1059,7 +1060,7 @@ #ifdef HAVE_SDL_NUMJOYSTICKS case SDL_JOYAXISMOTION: - action = sdljoy_axis_event(e.jaxis.which, e.jaxis.axis, e.jaxis.value); + action = sdljoy_axis_event_for_menu_action(e.jaxis.which, e.jaxis.axis, e.jaxis.value); break; case SDL_JOYBUTTONDOWN: action = sdljoy_button_event(e.jbutton.which, e.jbutton.button, 1); @@ -1374,7 +1375,7 @@ do { SDL_Delay(20); - retval = ui_dispatch_events(); + retval = ui_dispatch_events_for_menu_action(); #ifdef HAVE_SDL_NUMJOYSTICKS if (retval == MENU_ACTION_NONE || retval == MENU_ACTION_NONE_RELEASE) { retval = sdljoy_autorepeat(); Index: dev/vice/trunk/src/arch/sdl/video_sdl1.c =================================================================== --- dev/vice/trunk/src/arch/sdl/video_sdl1.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/video_sdl1.c (copia di lavoro) @@ -402,10 +402,6 @@ { DBG(("%s", __func__)); - if (machine_class == VICE_MACHINE_VSID) { - joy_arch_resources_shutdown(); - } - #if defined(HAVE_HWSCALE) lib_free(aspect_ratio_s); #endif Index: dev/vice/trunk/src/arch/sdl/video_sdl2.c =================================================================== --- dev/vice/trunk/src/arch/sdl/video_sdl2.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/video_sdl2.c (copia di lavoro) @@ -440,10 +440,6 @@ { DBG(("%s", __func__)); - if (machine_class == VICE_MACHINE_VSID) { - joy_arch_resources_shutdown(); - } - lib_free(aspect_ratio_s); lib_free(sdl2_renderer_name); lib_free(aspect_ratio_factory_value_s); Index: dev/vice/trunk/src/arch/sdl/vsyncarch.c =================================================================== --- dev/vice/trunk/src/arch/sdl/vsyncarch.c (revisione 41010) +++ dev/vice/trunk/src/arch/sdl/vsyncarch.c (copia di lavoro) @@ -31,6 +31,7 @@ #include "vice.h" #include "joy.h" +#include "joystick.h" #include "kbdbuf.h" #include "lightpendrv.h" #include "machine.h" @@ -88,7 +89,7 @@ void vsyncarch_presync(void) { if (sdl_vkbd_state & SDL_VKBD_ACTIVE) { - while (sdl_vkbd_process(ui_dispatch_events())) { + while (sdl_vkbd_process(ui_dispatch_events_for_menu_action())) { } #ifdef HAVE_SDL_NUMJOYSTICKS sdl_vkbd_process(sdljoy_autorepeat()); Index: dev/vice/trunk/src/joyport/joystick.c =================================================================== --- dev/vice/trunk/src/joyport/joystick.c (revisione 41010) +++ dev/vice/trunk/src/joyport/joystick.c (copia di lavoro) @@ -44,14 +44,17 @@ #include "joystick.h" #include "kbd.h" #include "lib.h" +#include "log.h" #include "machine.h" #include "maincpu.h" #include "network.h" #include "resources.h" #include "snapshot.h" +#include "sysfile.h" #include "types.h" #include "uiapi.h" #include "userport_joystick.h" +#include "util.h" #include "vice-event.h" /* Control port <--> Joystick connections: @@ -171,6 +174,18 @@ /*! \todo SRT: offset is unused! */ +/* Joystick mapping filename */ +static char *joymap_file = NULL; + +/** \brief Temporary copy of the default joymap file name + * + * Avoids silly casting away of const + */ +static char *joymap_factory = NULL; + + +static log_t joy_log = LOG_ERR; + static void joystick_latch_matrix(CLOCK offset) { uint8_t idx; @@ -823,31 +838,7 @@ return 0; } -/* Actions to perform on joystick input */ -typedef enum { - NONE = 0, - /* Joystick movement or button press */ - JOYSTICK = 1, - - /* Keyboard key press */ - KEYBOARD = 2, -} joystick_action_t; - - -/* Input mapping for each direction/button/etc */ -typedef struct joystick_mapping_s { - /* Action to perform */ - joystick_action_t action; - - union { - uint16_t joy_pin; - - /* key[0] = row, key[1] = column */ - int key[2]; - } value; -} joystick_mapping_t; - typedef struct joystick_axis_mapping_s { /* Previous state of input */ uint8_t prev; @@ -864,9 +855,7 @@ struct joystick_mapping_s right; } joystick_hat_mapping_t; -#ifndef HAVE_SDL_NUMJOYSTICKS static int num_joystick_devices = 0; -#endif /** \brief Joystick device name length (including 0) */ @@ -881,12 +870,13 @@ joystick_axis_mapping_t *axis_mapping; joystick_mapping_t *button_mapping; joystick_hat_mapping_t *hat_mapping; + int num_axes; + int num_buttons; + int num_hats; } joystick_device_t; -#ifndef HAVE_SDL_NUMJOYSTICKS static struct joystick_device_s *joystick_devices = NULL; -#endif static int set_joystick_device(int val, void *param) { @@ -921,6 +911,309 @@ return 0; } +int joy_arch_mapping_dump(const char *filename) +{ + FILE *fp; + int i, k; + int j; + joystick_action_t t; + char *hotkey_path = NULL; + +#ifdef SDL_DEBUG + fprintf(stderr, "%s\n", __func__); +#endif + + if (filename == NULL) { + return -1; + } + + fp = fopen(filename, MODE_WRITE_TEXT); + + if (fp == NULL) { + return -1; + } + + fprintf(fp, "# VICE joystick mapping file\n" + "#\n" + "# A joystick map is read in as patch to the current map.\n" + "#\n" + "# File format:\n" + "# - comment lines start with '#'\n" + "# - keyword lines start with '!keyword'\n" + "# - normal line has 'joynum inputtype inputindex action'\n" + "#\n" + "# Keywords and their lines are:\n" + "# '!CLEAR' clear all mappings\n" + "#\n" + ); + + fprintf(fp, "# inputtype:\n" + "# 0 axis\n" + "# 1 button\n" + "# 2 hat\n" + "#\n" + "# Note that each axis has 2 inputindex entries and each hat has 4.\n" + "#\n" + "# action [action_parameters]:\n" + "# 0 none\n" + "# 1 port pin joystick (pin: 1/2/4/8/16/32/64 = u/d/l/r/fire/fire2/fire3)\n" + "# 2 row col keyboard\n" + "# 3 map\n" + "# 4 UI activate\n" + "# 5 path&to&item UI function\n" + "#\n\n" + ); + + fprintf(fp, "!CLEAR\n\n"); + + for (i = 0; i < num_joystick_devices; ++i) { + k = 0; + fprintf(fp, "# %s\n", joystick_devices[i].jname); + for (j = 0; j < joystick_devices[i].num_axes; j++) { + t = joystick_devices[i].axis_mapping[j].positive_direction.action; + fprintf(fp, "%i %u %i %u", i, 0, k++, t); + switch (t) { + case JOYSTICK: + fprintf(fp, " %i", + joystick_devices[i].axis_mapping[j].positive_direction.value.joy_pin + ); + break; + case KEYBOARD: + fprintf(fp, " %i %i", + joystick_devices[i].axis_mapping[j].positive_direction.value.key[0], + joystick_devices[i].axis_mapping[j].positive_direction.value.key[1] + ); + break; + /*case UI_FUNCTION: + hotkey_path = sdl_ui_hotkey_path(sdljoystick[i].input[j][k].value.ui_function); + fprintf(fp, " %s", hotkey_path); + lib_free(hotkey_path); + break;*/ + default: + break; + } + fprintf(fp, "\n"); + t = joystick_devices[i].axis_mapping[j].negative_direction.action; + fprintf(fp, "%i %u %i %u", i, 0, k++, t); + switch (t) { + case JOYSTICK: + fprintf(fp, " %i", + joystick_devices[i].axis_mapping[j].negative_direction.value.joy_pin + ); + break; + case KEYBOARD: + fprintf(fp, " %i %i", + joystick_devices[i].axis_mapping[j].negative_direction.value.key[0], + joystick_devices[i].axis_mapping[j].negative_direction.value.key[1] + ); + break; + /*case UI_FUNCTION: + hotkey_path = sdl_ui_hotkey_path(sdljoystick[i].input[j][k].value.ui_function); + fprintf(fp, " %s", hotkey_path); + lib_free(hotkey_path); + break;*/ + default: + break; + } + fprintf(fp, "\n"); + + fprintf(fp, "\n"); + } + fprintf(fp, "\n\n"); + } + + fclose(fp); + + return 0; +} + + +static void joy_arch_keyword_clear(void) +{ + int i, k; + + for (i = 0; i < num_joystick_devices; ++i) { + for (k = 0; k < joystick_devices[i].num_axes; ++k) { + joystick_devices[i].axis_mapping[k].positive_direction.action = NONE; + joystick_devices[i].axis_mapping[k].negative_direction.action = NONE; + } + for (k = 0; k < joystick_devices[i].num_buttons; ++k) { + joystick_devices[i].button_mapping[k].action = NONE; + } + for (k = 0; k < joystick_devices[i].num_hats; ++k) { + joystick_devices[i].hat_mapping[k].up.action = NONE; + joystick_devices[i].hat_mapping[k].down.action = NONE; + joystick_devices[i].hat_mapping[k].left.action = NONE; + joystick_devices[i].hat_mapping[k].right.action = NONE; + } + } +} + +static void joy_arch_parse_keyword(char *buffer) +{ + char *key; + + key = strtok(buffer + 1, " \t:"); + + if (!strcmp(key, "CLEAR")) { + joy_arch_keyword_clear(); + } +} + +static void joy_arch_parse_entry(char *buffer) +{ + char *p; + int joynum, inputindex, data1 = 0, data2 = 0; + int inputtype; + joystick_action_t action; + int axis_or_hat_index, direction; + + p = strtok(buffer, " \t:"); + + joynum = atoi(p); + + if (joynum >= num_joystick_devices) { + log_error(joy_log, "Could not find joystick %i!", joynum); + return; + } + + p = strtok(NULL, " \t,"); + if (p != NULL) { + inputtype = atoi(p); + p = strtok(NULL, " \t,"); + if (p != NULL) { + inputindex = atoi(p); + p = strtok(NULL, " \t"); + if (p != NULL) { + action = (joystick_action_t)atoi(p); + + switch (action) { + case JOYSTICK: + case POT_AXIS: + case KEYBOARD: + p = strtok(NULL, " \t"); + data1 = atoi(p); + p = strtok(NULL, " \t"); + data2 = atoi(p); + break; + default: + break; + } + + switch (inputtype) { + case 0: + axis_or_hat_index = inputindex / 2; + direction = inputindex % 2; + if (axis_or_hat_index < joystick_devices[joynum].num_axes) { + if (direction == 0) { + joystick_devices[joynum].axis_mapping[axis_or_hat_index].positive_direction.action = action; + switch (action) { + case JOYSTICK: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].positive_direction.value.joy_pin = data1; + break; + case POT_AXIS: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].positive_direction.value.joy_pin = data1; + break; + case KEYBOARD: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].positive_direction.value.key[0] = data1; + joystick_devices[joynum].axis_mapping[axis_or_hat_index].positive_direction.value.key[1] = data2; + break; + default: + break; + } + } else { + joystick_devices[joynum].axis_mapping[axis_or_hat_index].negative_direction.action = action; + switch (action) { + case JOYSTICK: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].negative_direction.value.joy_pin = data1; + break; + case POT_AXIS: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].negative_direction.value.joy_pin = data1; + break; + case KEYBOARD: + joystick_devices[joynum].axis_mapping[axis_or_hat_index].negative_direction.value.key[0] = data1; + joystick_devices[joynum].axis_mapping[axis_or_hat_index].negative_direction.value.key[1] = data2; + break; + default: + break; + } + } + } else { + log_warning(joy_log, "inputindex %i too large for inputtype %u, joynum %i!", inputindex, inputtype, joynum); + } + } + } + } + } +} + +int joy_arch_mapping_load(const char *filename) +{ + FILE *fp; + char *complete_path; + char buffer[1000]; + + /* Silently ignore keymap load on resource & cmdline init */ + if (joystick_devices == NULL) { + return 0; + } + + if (filename == NULL) { + return -1; + } + + fp = sysfile_open(filename, NULL, &complete_path, MODE_READ_TEXT); + + if (fp == NULL) { + log_warning(joy_log, "Failed to open `%s'.", filename); + return -1; + } + + log_message(joy_log, "Loading joystick map `%s'.", complete_path); + + lib_free(complete_path); + + do { + buffer[0] = 0; + if (fgets(buffer, 999, fp)) { + char *p; + + if (strlen(buffer) == 0) { + break; + } + + buffer[strlen(buffer) - 1] = 0; /* remove newline */ + + /* remove comments */ + if ((p = strchr(buffer, '#'))) { + *p = 0; + } + + /* remove whitespace at the beginning of the line */ + p = buffer; + while (((*p == ' ') || (*p == '\t')) && (*p != 0)) { + ++p; + } + + switch (*p) { + case 0: + break; + case '!': + /* keyword handling */ + joy_arch_parse_keyword(p); + break; + default: + /* table entry handling */ + joy_arch_parse_entry(p); + break; + } + } + } while (!feof(fp)); + fclose(fp); + + return 0; +} + static int set_joystick_autofire(int val, void *param) { int port_idx = vice_ptr_to_int(param); @@ -952,6 +1245,15 @@ return 0; } +static int joymap_file_set(const char *val, void *param) +{ + if (util_string_set(&joymap_file, val)) { + return 0; + } + + return joy_arch_mapping_load(joymap_file); +} + static const resource_int_t joyopposite_resources_int[] = { { "JoyOpposite", 0, RES_EVENT_NO, NULL, &joystick_opposite_enable, set_joystick_opposite_enable, NULL }, @@ -1078,6 +1380,13 @@ RESOURCE_INT_LIST_END }; +static resource_string_t resources_string[] = { + { "JoyMapFile", NULL, RES_EVENT_NO, NULL, + &joymap_file, joymap_file_set, (void *)0 }, + RESOURCE_STRING_LIST_END +}; + + /** \brief Initialize joystick resources * * \return 0 on success, -1 on failure @@ -1176,7 +1485,13 @@ return -1; } } + joymap_factory = archdep_default_joymap_file_name(); + resources_string[0].factory_value = joymap_factory; + if (resources_register_string(resources_string) < 0) { + return -1; + } + #ifdef HAVE_SDL_NUMJOYSTICKS return joy_sdl_resources_init(); #else @@ -1599,6 +1914,8 @@ } #endif + joy_arch_mapping_load(joymap_file); + return 1; } @@ -1655,7 +1972,6 @@ /* ------------------------------------------------------------------------- */ -#ifndef HAVE_SDL_NUMJOYSTICKS void register_joystick_driver( struct joystick_driver_s *driver, const char *jname, @@ -1699,6 +2015,9 @@ new_joystick_device->hat_mapping = (joystick_hat_mapping_t *)lib_calloc(num_hats, sizeof(joystick_hat_mapping_t)); new_joystick_device->joyport = -1; new_joystick_device->priv = priv; + new_joystick_device->num_axes = num_axes; + new_joystick_device->num_buttons = num_buttons; + new_joystick_device->num_hats = num_hats; } static void joy_perform_event(joystick_mapping_t *event, int joyport, int value) @@ -1804,6 +2123,49 @@ joystick_devices[joynum].hat_mapping[hat].prev = value; } +#ifdef HAVE_SDL_NUMJOYSTICKS +joystick_axis_value_t joy_axis_prev(uint8_t joynum, uint8_t axis) { + return joystick_devices[joynum].axis_mapping[axis].prev; +} + +joystick_mapping_t* joy_get_axis_mapping(uint8_t joynum, uint8_t axis, joystick_axis_value_t value) { + joystick_devices[joynum].axis_mapping[axis].prev = value; + if (value == JOY_AXIS_POSITIVE) { + return &joystick_devices[joynum].axis_mapping[axis].positive_direction; + } + if (value == JOY_AXIS_NEGATIVE) { + return &joystick_devices[joynum].axis_mapping[axis].negative_direction; + } + return NULL; +} + +joystick_mapping_t* joy_get_button_mapping(uint8_t joynum, uint8_t button) { + return &joystick_devices[joynum].button_mapping[button]; +} + +joystick_axis_value_t joy_hat_prev(uint8_t joynum, uint8_t hat) { + return joystick_devices[joynum].hat_mapping[hat].prev; +} + +joystick_mapping_t* joy_get_hat_mapping(uint8_t joynum, uint8_t hat, uint8_t value) { + uint8_t prev = joystick_devices[joynum].hat_mapping[hat].prev; + joystick_mapping_t* mapping = NULL; + if ((value & JOYSTICK_DIRECTION_UP) && !(prev & JOYSTICK_DIRECTION_UP)) { + mapping = &joystick_devices[joynum].hat_mapping[hat].up; + } else if ((value & JOYSTICK_DIRECTION_DOWN) && !(prev & JOYSTICK_DIRECTION_DOWN)) { + mapping = &joystick_devices[joynum].hat_mapping[hat].down; + } + if ((value & JOYSTICK_DIRECTION_LEFT) && !(prev & JOYSTICK_DIRECTION_LEFT)) { + mapping = &joystick_devices[joynum].hat_mapping[hat].left; + } else if ((value & JOYSTICK_DIRECTION_RIGHT) && !(prev & JOYSTICK_DIRECTION_RIGHT)) { + mapping = &joystick_devices[joynum].hat_mapping[hat].right; + } + + joystick_devices[joynum].hat_mapping[hat].prev = value; + return mapping; +} +#endif + static int joystickdeviceidx = 0; void joystick_ui_reset_device_list(void) @@ -1840,6 +2202,13 @@ } lib_free(joystick_devices); + if (joymap_factory) { + lib_free(joymap_factory); + joymap_factory = NULL; + } + if (joymap_file) { + lib_free(joymap_file); + joymap_file = NULL; + } } -#endif Index: dev/vice/trunk/src/joyport/joystick.h =================================================================== --- dev/vice/trunk/src/joyport/joystick.h (revisione 41010) +++ dev/vice/trunk/src/joyport/joystick.h (copia di lavoro) @@ -30,6 +30,9 @@ #include "types.h" #include "joyport.h" /* for JOYPORT_MAX_PORTS */ +#if (defined USE_SDLUI ||defined USE_SDLUI2) +#include "uimenu.h" +#endif extern int joystick_init(void); extern int joystick_resources_init(void); @@ -142,6 +145,46 @@ JOY_AXIS_NEGATIVE } joystick_axis_value_t; +/* Actions to perform on joystick input */ +typedef enum joystick_action_e { + NONE = 0, + + /* Joystick movement or button press */ + JOYSTICK = 1, + + /* Keyboard key press */ + KEYBOARD = 2, + + /* Map button */ + MAP = 3, + + /* (De)Activate UI */ + UI_ACTIVATE = 4, + + /* Call UI function */ + UI_FUNCTION = 5, + + /* Joystick axis used for potentiometers */ + POT_AXIS = 6 +} joystick_action_t; + +/* Input mapping for each direction/button/etc */ +typedef struct joystick_mapping_s { + /* Action to perform */ + joystick_action_t action; + + union { + uint16_t joy_pin; + + /* key[0] = row, key[1] = column */ + int key[2]; +#if (defined USE_SDLUI ||defined USE_SDLUI2) + /* pointer to the menu item */ + ui_menu_entry_t *ui_function; +#endif + } value; +} joystick_mapping_t; + extern void joy_axis_event(uint8_t joynum, uint8_t axis, joystick_axis_value_t value); extern void joy_button_event(uint8_t joynum, uint8_t button, uint8_t value); extern void joy_hat_event(uint8_t joynum, uint8_t button, uint8_t value); @@ -149,6 +192,13 @@ extern void joystick_close(void); extern void joystick_ui_reset_device_list(void); extern const char *joystick_ui_get_next_device_name(int *id); +extern int joy_arch_mapping_dump(const char *filename); +extern int joy_arch_mapping_load(const char *filename); +extern joystick_axis_value_t joy_axis_prev(uint8_t joynum, uint8_t axis); +extern joystick_mapping_t* joy_get_axis_mapping(uint8_t joynum, uint8_t axis, joystick_axis_value_t value); +extern joystick_mapping_t* joy_get_button_mapping(uint8_t joynum, uint8_t button); +extern joystick_axis_value_t joy_hat_prev(uint8_t joynum, uint8_t hat); +extern joystick_mapping_t* joy_get_hat_mapping(uint8_t joynum, uint8_t hat, uint8_t value); #define JOYSTICK_DIRECTION_DOWN 1 #define JOYSTICK_DIRECTION_UP 2