From 3b092bcdb2ec8e2b9194c4c509a99ac9fed4b607 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Sat, 11 Jan 2025 01:31:57 -0500 Subject: [PATCH] Cleaning --- src/config.c | 100 ++++++++++++++---------------- src/game/joystick_standard.c | 21 +++---- src/game/joystick_sw_pad.c | 25 ++++---- src/qt/qt_settingsinput.cpp | 16 ++--- src/qt/sdl_joystick.c | 108 ++++++++++++++++----------------- src/qt/win_joystick_rawinput.c | 59 +++++++++--------- 6 files changed, 155 insertions(+), 174 deletions(-) diff --git a/src/config.c b/src/config.c index 2f1561f18..5119d45ff 100644 --- a/src/config.c +++ b/src/config.c @@ -475,8 +475,6 @@ load_input_devices(void) { ini_section_t cat = ini_find_section(config, "Input devices"); char temp[512]; - int c; - int d; char *p; p = ini_section_get_string(cat, "mouse_type", NULL); @@ -495,8 +493,8 @@ load_input_devices(void) /* Workaround for ini_section_get_int returning 0 on non-integer data */ joystick_type = joystick_get_from_internal_name("2axis_2button"); else { - c = ini_section_get_int(cat, "joystick_type", 8); - switch (c) { + int js = ini_section_get_int(cat, "joystick_type", 8); + switch (js) { case JS_TYPE_2AXIS_4BUTTON: joystick_type = joystick_get_from_internal_name("2axis_4button"); break; @@ -527,25 +525,25 @@ load_input_devices(void) } else joystick_type = JS_TYPE_NONE; - for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { - sprintf(temp, "joystick_%i_nr", c); - joystick_state[c].plat_joystick_nr = ini_section_get_int(cat, temp, 0); + for (int js = 0; js < joystick_get_max_joysticks(joystick_type); js++) { + sprintf(temp, "joystick_%i_nr", js); + joystick_state[js].plat_joystick_nr = ini_section_get_int(cat, temp, 0); - if (joystick_state[c].plat_joystick_nr) { - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) { - sprintf(temp, "joystick_%i_axis_%i", c, d); - joystick_state[c].axis_mapping[d] = ini_section_get_int(cat, temp, d); + if (joystick_state[js].plat_joystick_nr) { + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) { + sprintf(temp, "joystick_%i_axis_%i", js, axis_nr); + joystick_state[js].axis_mapping[axis_nr] = ini_section_get_int(cat, temp, axis_nr); } - for (d = 0; d < joystick_get_button_count(joystick_type); d++) { - sprintf(temp, "joystick_%i_button_%i", c, d); - joystick_state[c].button_mapping[d] = ini_section_get_int(cat, temp, d); + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) { + sprintf(temp, "joystick_%i_button_%i", js, button_nr); + joystick_state[js].button_mapping[button_nr] = ini_section_get_int(cat, temp, button_nr); } - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) { - sprintf(temp, "joystick_%i_pov_%i", c, d); + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) { + sprintf(temp, "joystick_%i_pov_%i", js, pov_nr); p = ini_section_get_string(cat, temp, "0, 0"); - joystick_state[c].pov_mapping[d][0] = joystick_state[c].pov_mapping[d][1] = 0; - sscanf(p, "%i, %i", &joystick_state[c].pov_mapping[d][0], - &joystick_state[c].pov_mapping[d][1]); + joystick_state[js].pov_mapping[js][pov_nr] = joystick_state[js].pov_mapping[pov_nr][1] = 0; + sscanf(p, "%i, %i", &joystick_state[js].pov_mapping[pov_nr][0], + &joystick_state[js].pov_mapping[pov_nr][1]); } } } @@ -743,11 +741,9 @@ load_ports(void) ini_section_t cat = ini_find_section(config, "Ports (COM & LPT)"); char *p; char temp[512]; - int c; - memset(temp, 0, sizeof(temp)); - for (c = 0; c < SERIAL_MAX; c++) { + for (int c = 0; c < SERIAL_MAX; c++) { sprintf(temp, "serial%d_enabled", c + 1); com_ports[c].enabled = !!ini_section_get_int(cat, temp, (c >= 2) ? 0 : 1); @@ -758,7 +754,7 @@ load_ports(void) config_log("Serial Port %d: passthrough enabled.\n\n", c + 1); } - for (c = 0; c < PARALLEL_MAX; c++) { + for (int c = 0; c < PARALLEL_MAX; c++) { sprintf(temp, "lpt%d_enabled", c + 1); lpt_ports[c].enabled = !!ini_section_get_int(cat, temp, (c == 0) ? 1 : 0); @@ -776,11 +772,10 @@ load_storage_controllers(void) ini_section_t migration_cat; char *p; char temp[512]; - int c; int min = 0; int free_p = 0; - for (c = min; c < SCSI_CARD_MAX; c++) { + for (int c = min; c < SCSI_CARD_MAX; c++) { sprintf(temp, "scsicard_%d", c + 1); p = ini_section_get_string(cat, temp, NULL); @@ -932,7 +927,7 @@ load_storage_controllers(void) ini_section_delete_var(cat, "cassette_ui_writeprot"); } - for (c = 0; c < 2; c++) { + for (int c = 0; c < 2; c++) { sprintf(temp, "cartridge_%02i_fn", c + 1); p = ini_section_get_string(cat, temp, ""); @@ -2140,50 +2135,49 @@ save_input_devices(void) ini_section_t cat = ini_find_or_create_section(config, "Input devices"); char temp[512]; char tmp2[512]; - int c; - int d; ini_section_set_string(cat, "mouse_type", mouse_get_internal_name(mouse_type)); if (!joystick_type) { ini_section_delete_var(cat, "joystick_type"); - for (c = 0; c < 16; c++) { - sprintf(tmp2, "joystick_%i_nr", c); + for (int js = 0; js < MAX_PLAT_JOYSTICKS; js++) { + sprintf(tmp2, "joystick_%i_nr", js); ini_section_delete_var(cat, tmp2); - for (d = 0; d < 16; d++) { - sprintf(tmp2, "joystick_%i_axis_%i", c, d); + for (int axis_nr = 0; axis_nr < 16; axis_nr++) { + sprintf(tmp2, "joystick_%i_axis_%i", js, axis_nr); ini_section_delete_var(cat, tmp2); } - for (d = 0; d < 16; d++) { - sprintf(tmp2, "joystick_%i_button_%i", c, d); + for (int button_nr = 0; button_nr < 16; button_nr++) { + sprintf(tmp2, "joystick_%i_button_%i", js, button_nr); ini_section_delete_var(cat, tmp2); } - for (d = 0; d < 16; d++) { - sprintf(tmp2, "joystick_%i_pov_%i", c, d); + for (int pov_nr = 0; pov_nr < 16; pov_nr++) { + sprintf(tmp2, "joystick_%i_pov_%i", js, pov_nr); ini_section_delete_var(cat, tmp2); } } } else { ini_section_set_string(cat, "joystick_type", joystick_get_internal_name(joystick_type)); - for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { - sprintf(tmp2, "joystick_%i_nr", c); - ini_section_set_int(cat, tmp2, joystick_state[c].plat_joystick_nr); + for (int js = 0; js < joystick_get_max_joysticks(joystick_type); js++) { + sprintf(tmp2, "joystick_%i_nr", js); + ini_section_set_int(cat, tmp2, joystick_state[js].plat_joystick_nr); - if (joystick_state[c].plat_joystick_nr) { - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) { - sprintf(tmp2, "joystick_%i_axis_%i", c, d); - ini_section_set_int(cat, tmp2, joystick_state[c].axis_mapping[d]); + if (joystick_state[js].plat_joystick_nr) { + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) { + sprintf(tmp2, "joystick_%i_axis_%i", js, axis_nr); + ini_section_set_int(cat, tmp2, joystick_state[js].axis_mapping[axis_nr]); } - for (d = 0; d < joystick_get_button_count(joystick_type); d++) { - sprintf(tmp2, "joystick_%i_button_%i", c, d); - ini_section_set_int(cat, tmp2, joystick_state[c].button_mapping[d]); + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) { + sprintf(tmp2, "joystick_%i_button_%i", js, button_nr); + ini_section_set_int(cat, tmp2, joystick_state[js].button_mapping[button_nr]); } - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) { - sprintf(tmp2, "joystick_%i_pov_%i", c, d); - sprintf(temp, "%i, %i", joystick_state[c].pov_mapping[d][0], joystick_state[c].pov_mapping[d][1]); + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) { + sprintf(tmp2, "joystick_%i_pov_%i", js, pov_nr); + sprintf(temp, "%i, %i", joystick_state[js].pov_mapping[pov_nr][0], + joystick_state[js].pov_mapping[pov_nr][1]); ini_section_set_string(cat, tmp2, temp); } } @@ -2341,10 +2335,8 @@ save_ports(void) { ini_section_t cat = ini_find_or_create_section(config, "Ports (COM & LPT)"); char temp[512]; - int c; - int d; - for (c = 0; c < SERIAL_MAX; c++) { + for (int c = 0; c < SERIAL_MAX; c++) { sprintf(temp, "serial%d_enabled", c + 1); if (((c < 2) && com_ports[c].enabled) || ((c >= 2) && !com_ports[c].enabled)) ini_section_delete_var(cat, temp); @@ -2358,9 +2350,9 @@ save_ports(void) ini_section_delete_var(cat, temp); } - for (c = 0; c < PARALLEL_MAX; c++) { + for (int c = 0; c < PARALLEL_MAX; c++) { sprintf(temp, "lpt%d_enabled", c + 1); - d = (c == 0) ? 1 : 0; + int d = (c == 0) ? 1 : 0; if (lpt_ports[c].enabled == d) ini_section_delete_var(cat, temp); else diff --git a/src/game/joystick_standard.c b/src/game/joystick_standard.c index 1d1568738..201574126 100644 --- a/src/game/joystick_standard.c +++ b/src/game/joystick_standard.c @@ -62,17 +62,13 @@ joystick_standard_read(UNUSED(void *priv)) { uint8_t ret = 0xf0; - if (JOYSTICK_PRESENT(0)) { - if (joystick_state[0].button[0]) - ret &= ~0x10; - if (joystick_state[0].button[1]) - ret &= ~0x20; - } - if (JOYSTICK_PRESENT(1)) { - if (joystick_state[1].button[0]) - ret &= ~0x40; - if (joystick_state[1].button[1]) - ret &= ~0x80; + for (int js = 0; js < 2; js++) { + if (JOYSTICK_PRESENT(js)) { + if (joystick_state[js].button[0]) + ret &= ~0x10; + if (joystick_state[js].button[1]) + ret &= ~0x20; + } } return ret; @@ -140,9 +136,7 @@ joystick_standard_read_axis_4button(UNUSED(void *priv), int axis) case 1: return joystick_state[0].axis[1]; case 2: - return 0; case 3: - return 0; default: return 0; } @@ -162,7 +156,6 @@ joystick_standard_read_axis_3axis(UNUSED(void *priv), int axis) case 2: return joystick_state[0].axis[2]; case 3: - return 0; default: return 0; } diff --git a/src/game/joystick_sw_pad.c b/src/game/joystick_sw_pad.c index cab008d0a..711a5fa1e 100644 --- a/src/game/joystick_sw_pad.c +++ b/src/game/joystick_sw_pad.c @@ -119,8 +119,7 @@ sw_parity(uint16_t data) static void * sw_init(void) { - sw_data *sw = (sw_data *) malloc(sizeof(sw_data)); - memset(sw, 0, sizeof(sw_data)); + sw_data *sw = (sw_data *) calloc(1, sizeof(sw_data)); timer_add(&sw->poll_timer, sw_timer_over, sw, 0); timer_add(&sw->trigger_timer, sw_trigger_timer_over, sw, 0); @@ -191,24 +190,24 @@ sw_write(void *priv) sw->poll_data = 1; } - for (uint8_t c = 0; c < 4; c++) { + for (uint8_t js = 0; js < MAX_JOYSTICKS; js++) { uint16_t data = 0x3fff; - if (!JOYSTICK_PRESENT(c)) + if (!JOYSTICK_PRESENT(js)) break; - if (joystick_state[c].axis[1] < -16383) + if (joystick_state[js].axis[1] < -16383) data &= ~1; - if (joystick_state[c].axis[1] > 16383) + if (joystick_state[js].axis[1] > 16383) data &= ~2; - if (joystick_state[c].axis[0] > 16383) + if (joystick_state[js].axis[0] > 16383) data &= ~4; - if (joystick_state[c].axis[0] < -16383) + if (joystick_state[js].axis[0] < -16383) data &= ~8; - for (uint8_t b = 0; b < 10; b++) { - if (joystick_state[c].button[b]) - data &= ~(1 << (b + 4)); + for (uint8_t button_nr = 0; button_nr < 10; button_nr++) { + if (joystick_state[js].button[button_nr]) + data &= ~(1 << (button_nr + 4)); } if (sw_parity(data)) @@ -216,10 +215,10 @@ sw_write(void *priv) if (sw->poll_mode) { sw->poll_left += 5; - sw->poll_data |= (data << (c * 15 + 3)); + sw->poll_data |= (data << (js * 15 + 3)); } else { sw->poll_left += 15; - sw->poll_data |= (data << (c * 15 + 1)); + sw->poll_data |= (data << (js * 15 + 1)); } } } diff --git a/src/qt/qt_settingsinput.cpp b/src/qt/qt_settingsinput.cpp index 05e44c2c0..d7b69442c 100644 --- a/src/qt/qt_settingsinput.cpp +++ b/src/qt/qt_settingsinput.cpp @@ -178,15 +178,17 @@ updateJoystickConfig(int type, int joystick_nr, QWidget *parent) joystick_state[joystick_nr].plat_joystick_nr = jc.selectedDevice(); if (joystick_state[joystick_nr].plat_joystick_nr) { - for (int c = 0; c < joystick_get_axis_count(type); c++) { - joystick_state[joystick_nr].axis_mapping[c] = get_axis(jc, c, joystick_nr); + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(type); axis_nr++) { + joystick_state[joystick_nr].axis_mapping[axis_nr] = get_axis(jc, axis_nr, joystick_nr); } - for (int c = 0; c < joystick_get_button_count(type); c++) { - joystick_state[joystick_nr].button_mapping[c] = jc.selectedButton(c); + + for (int button_nr = 0; button_nr < joystick_get_button_count(type); button_nr++) { + joystick_state[joystick_nr].button_mapping[button_nr] = jc.selectedButton(button_nr); } - for (int c = 0; c < joystick_get_pov_count(type) * 2; c += 2) { - joystick_state[joystick_nr].pov_mapping[c][0] = get_pov(jc, c, joystick_nr); - joystick_state[joystick_nr].pov_mapping[c][1] = get_pov(jc, c + 1, joystick_nr); + + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(type) * 2; pov_nr += 2) { + joystick_state[joystick_nr].pov_mapping[pov_nr][0] = get_pov(jc, pov_nr, joystick_nr); + joystick_state[joystick_nr].pov_mapping[pov_nr][1] = get_pov(jc, pov_nr + 1, joystick_nr); } } } diff --git a/src/qt/sdl_joystick.c b/src/qt/sdl_joystick.c index d56612872..221e12078 100644 --- a/src/qt/sdl_joystick.c +++ b/src/qt/sdl_joystick.c @@ -54,28 +54,26 @@ joystick_init(void) joysticks_present = SDL_NumJoysticks(); memset(sdl_joy, 0, sizeof(sdl_joy)); - for (int c = 0; c < joysticks_present; c++) { - sdl_joy[c] = SDL_JoystickOpen(c); + for (int js = 0; js < joysticks_present; js++) { + sdl_joy[js] = SDL_JoystickOpen(js); - if (sdl_joy[c]) { - int d; + if (sdl_joy[js]) { + strncpy(plat_joystick_state[js].name, SDL_JoystickNameForIndex(js), 64); + plat_joystick_state[js].nr_axes = MIN(SDL_JoystickNumAxes(sdl_joy[js]), MAX_JOY_AXES); + plat_joystick_state[js].nr_buttons = MIN(SDL_JoystickNumButtons(sdl_joy[js]), MAX_JOY_BUTTONS); + plat_joystick_state[js].nr_povs = MIN(SDL_JoystickNumHats(sdl_joy[js]), MAX_JOY_POVS); - strncpy(plat_joystick_state[c].name, SDL_JoystickNameForIndex(c), 64); - plat_joystick_state[c].nr_axes = MIN(SDL_JoystickNumAxes(sdl_joy[c]), MAX_JOY_AXES); - plat_joystick_state[c].nr_buttons = MIN(SDL_JoystickNumButtons(sdl_joy[c]), MAX_JOY_BUTTONS); - plat_joystick_state[c].nr_povs = MIN(SDL_JoystickNumHats(sdl_joy[c]), MAX_JOY_POVS); - - for (d = 0; d < plat_joystick_state[c].nr_axes; d++) { - snprintf(plat_joystick_state[c].axis[d].name, sizeof(plat_joystick_state[c].axis[d].name), "Axis %i", d); - plat_joystick_state[c].axis[d].id = d; + for (int axis_nr = 0; axis_nr < plat_joystick_state[js].nr_axes; axis_nr++) { + snprintf(plat_joystick_state[js].axis[axis_nr].name, sizeof(plat_joystick_state[js].axis[axis_nr].name), "Axis %i", axis_nr); + plat_joystick_state[js].axis[axis_nr].id = axis_nr; } - for (d = 0; d < plat_joystick_state[c].nr_buttons; d++) { - snprintf(plat_joystick_state[c].button[d].name, sizeof(plat_joystick_state[c].button[d].name), "Button %i", d); - plat_joystick_state[c].button[d].id = d; + for (int buttons_nr = 0; buttons_nr < plat_joystick_state[js].nr_buttons; buttons_nr++) { + snprintf(plat_joystick_state[js].button[buttons_nr].name, sizeof(plat_joystick_state[js].button[buttons_nr].name), "Button %i", buttons_nr); + plat_joystick_state[js].button[buttons_nr].id = buttons_nr; } - for (d = 0; d < plat_joystick_state[c].nr_povs; d++) { - snprintf(plat_joystick_state[c].pov[d].name, sizeof(plat_joystick_state[c].pov[d].name), "POV %i", d); - plat_joystick_state[c].pov[d].id = d; + for (int pov_nr = 0; pov_nr < plat_joystick_state[js].nr_povs; pov_nr++) { + snprintf(plat_joystick_state[js].pov[pov_nr].name, sizeof(plat_joystick_state[js].pov[pov_nr].name), "POV %i", pov_nr); + plat_joystick_state[js].pov[pov_nr].id = pov_nr; } } } @@ -84,11 +82,9 @@ joystick_init(void) void joystick_close(void) { - int c; - - for (c = 0; c < joysticks_present; c++) { - if (sdl_joy[c]) - SDL_JoystickClose(sdl_joy[c]); + for (int js = 0; js < joysticks_present; js++) { + if (sdl_joy[js]) + SDL_JoystickClose(sdl_joy[js]); } } @@ -132,57 +128,55 @@ joystick_get_axis(int joystick_nr, int mapping) void joystick_process(void) { - int c; - int d; - if (!joystick_type) return; SDL_JoystickUpdate(); - for (c = 0; c < joysticks_present; c++) { - int b; + for (int js = 0; js < joysticks_present; js++) { + for (int axis_nr = 0; axis_nr < plat_joystick_state[js].nr_axes; axis_nr++) + plat_joystick_state[js].a[axis_nr] = SDL_JoystickGetAxis(sdl_joy[js], axis_nr); - for (b = 0; b < plat_joystick_state[c].nr_axes; b++) - plat_joystick_state[c].a[b] = SDL_JoystickGetAxis(sdl_joy[c], b); + for (int button_nr = 0; button_nr < plat_joystick_state[js].nr_buttons; button_nr++) + plat_joystick_state[js].b[button_nr] = SDL_JoystickGetButton(sdl_joy[js], button_nr); - for (b = 0; b < plat_joystick_state[c].nr_buttons; b++) - plat_joystick_state[c].b[b] = SDL_JoystickGetButton(sdl_joy[c], b); + for (int pov_nr = 0; pov_nr < plat_joystick_state[js].nr_povs; pov_nr++) + plat_joystick_state[js].p[pov_nr] = SDL_JoystickGetHat(sdl_joy[js], pov_nr); - for (b = 0; b < plat_joystick_state[c].nr_povs; b++) - plat_joystick_state[c].p[b] = SDL_JoystickGetHat(sdl_joy[c], b); - // pclog("joystick %i - x=%i y=%i b[0]=%i b[1]=%i %i\n", c, joystick_state[c].x, joystick_state[c].y, joystick_state[c].b[0], joystick_state[c].b[1], joysticks_present); +#if 0 + pclog("joystick %i - x=%i y=%i b[0]=%i b[1]=%i %i\n", js, joystick_state[js].x, joystick_state[js].y, joystick_state[js].b[0], joystick_state[js].b[1], joysticks_present); +#endif } - for (c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { - if (joystick_state[c].plat_joystick_nr) { - int joystick_nr = joystick_state[c].plat_joystick_nr - 1; + for (int js = 0; js < joystick_get_max_joysticks(joystick_type); js++) { + if (joystick_state[js].plat_joystick_nr) { + int joystick_nr = joystick_state[js].plat_joystick_nr - 1; - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) - joystick_state[c].axis[d] = joystick_get_axis(joystick_nr, joystick_state[c].axis_mapping[d]); - for (d = 0; d < joystick_get_button_count(joystick_type); d++) - joystick_state[c].button[d] = plat_joystick_state[joystick_nr].b[joystick_state[c].button_mapping[d]]; - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) { - int x, y; - double angle, magnitude; + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) + joystick_state[js].axis[axis_nr] = joystick_get_axis(joystick_nr, joystick_state[js].axis_mapping[axis_nr]); - x = joystick_get_axis(joystick_nr, joystick_state[c].pov_mapping[d][0]); - y = joystick_get_axis(joystick_nr, joystick_state[c].pov_mapping[d][1]); + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) + joystick_state[js].button[button_nr] = plat_joystick_state[joystick_nr].b[joystick_state[js].button_mapping[button_nr]]; - angle = (atan2((double) y, (double) x) * 360.0) / (2 * M_PI); - magnitude = sqrt((double) x * (double) x + (double) y * (double) y); + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) { + int x = joystick_get_axis(joystick_nr, joystick_state[js].pov_mapping[pov_nr][0]); + int y = joystick_get_axis(joystick_nr, joystick_state[js].pov_mapping[pov_nr][1]); + double angle = (atan2((double) y, (double) x) * 360.0) / (2 * M_PI); + double magnitude = sqrt((double) x * (double) x + (double) y * (double) y); if (magnitude < 16384) - joystick_state[c].pov[d] = -1; + joystick_state[js].pov[pov_nr] = -1; else - joystick_state[c].pov[d] = ((int) angle + 90 + 360) % 360; + joystick_state[js].pov[pov_nr] = ((int) angle + 90 + 360) % 360; } } else { - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) - joystick_state[c].axis[d] = 0; - for (d = 0; d < joystick_get_button_count(joystick_type); d++) - joystick_state[c].button[d] = 0; - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) - joystick_state[c].pov[d] = -1; + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) + joystick_state[js].axis[axis_nr] = 0; + + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) + joystick_state[js].button[button_nr] = 0; + + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) + joystick_state[js].pov[pov_nr] = -1; } } } diff --git a/src/qt/win_joystick_rawinput.c b/src/qt/win_joystick_rawinput.c index bb05c3f5c..811425a98 100644 --- a/src/qt/win_joystick_rawinput.c +++ b/src/qt/win_joystick_rawinput.c @@ -412,7 +412,7 @@ win_joystick_handle(PRAWINPUT raw) /* Read buttons */ USAGE usage_list[128] = { 0 }; ULONG usage_length = plat_joystick_state[j].nr_buttons; - memset(plat_joystick_state[j].b, 0, 32 * sizeof(int)); + memset(plat_joystick_state[j].b, 0, MAX_JOY_BUTTONS * sizeof(int)); r = HidP_GetUsages(HidP_Input, HID_USAGE_PAGE_BUTTON, 0, usage_list, &usage_length, raw_joystick_state[j].data, (PCHAR) raw->data.hid.bRawData, raw->data.hid.dwSizeHid); @@ -425,8 +425,8 @@ win_joystick_handle(PRAWINPUT raw) } /* Read axes */ - for (int a = 0; a < plat_joystick_state[j].nr_axes; a++) { - const struct raw_axis_t *axis = &raw_joystick_state[j].axis[a]; + for (int axis_nr = 0; axis_nr < plat_joystick_state[j].nr_axes; axis_nr++) { + const struct raw_axis_t *axis = &raw_joystick_state[j].axis[axis_nr]; ULONG uvalue = 0; LONG value = 0; LONG center = (axis->max - axis->min + 1) / 2; @@ -453,15 +453,15 @@ win_joystick_handle(PRAWINPUT raw) value = value * 32768 / center; } - plat_joystick_state[j].a[a] = value; + plat_joystick_state[j].a[axis_nr] = value; #if 0 - joystick_log("%s %-06d ", plat_joystick_state[j].axis[a].name, plat_joystick_state[j].a[a]); + joystick_log("%s %-06d ", plat_joystick_state[j].axis[axis_nr].name, plat_joystick_state[j].a[axis_nr]); #endif } /* read povs */ - for (int p = 0; p < plat_joystick_state[j].nr_povs; p++) { - const struct raw_pov_t *pov = &raw_joystick_state[j].pov[p]; + for (int pov_nr = 0; pov_nr < plat_joystick_state[j].nr_povs; pov_nr++) { + const struct raw_pov_t *pov = &raw_joystick_state[j].pov[pov_nr]; ULONG uvalue = 0; LONG value = -1; @@ -474,10 +474,10 @@ win_joystick_handle(PRAWINPUT raw) value %= 36000; } - plat_joystick_state[j].p[p] = value; + plat_joystick_state[j].p[pov_nr] = value; #if 0 - joystick_log("%s %-3d ", plat_joystick_state[j].pov[p].name, plat_joystick_state[j].p[p]); + joystick_log("%s %-3d ", plat_joystick_state[j].pov[pov_nr].name, plat_joystick_state[j].p[pov_nr]); #endif } #if 0 @@ -508,44 +508,45 @@ joystick_get_axis(int joystick_nr, int mapping) void joystick_process(void) { - int d; - if (joystick_type == JS_TYPE_NONE) return; - for (int c = 0; c < joystick_get_max_joysticks(joystick_type); c++) { - if (joystick_state[c].plat_joystick_nr) { - int joystick_nr = joystick_state[c].plat_joystick_nr - 1; + for (int js = 0; js < joystick_get_max_joysticks(joystick_type); js++) { + if (joystick_state[js].plat_joystick_nr) { + int joystick_nr = joystick_state[js].plat_joystick_nr - 1; - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) - joystick_state[c].axis[d] = joystick_get_axis(joystick_nr, joystick_state[c].axis_mapping[d]); - for (d = 0; d < joystick_get_button_count(joystick_type); d++) - joystick_state[c].button[d] = plat_joystick_state[joystick_nr].b[joystick_state[c].button_mapping[d]]; + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) + joystick_state[js].axis[axis_nr] = joystick_get_axis(joystick_nr, joystick_state[js].axis_mapping[axis_nr]); - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) { + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) + joystick_state[js].button[button_nr] = plat_joystick_state[joystick_nr].b[joystick_state[js].button_mapping[button_nr]]; + + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) { int x; int y; double angle; double magnitude; - x = joystick_get_axis(joystick_nr, joystick_state[c].pov_mapping[d][0]); - y = joystick_get_axis(joystick_nr, joystick_state[c].pov_mapping[d][1]); + x = joystick_get_axis(joystick_nr, joystick_state[js].pov_mapping[pov_nr][0]); + y = joystick_get_axis(joystick_nr, joystick_state[js].pov_mapping[pov_nr][1]); angle = (atan2((double) y, (double) x) * 360.0) / (2 * M_PI); magnitude = sqrt((double) x * (double) x + (double) y * (double) y); if (magnitude < 16384) - joystick_state[c].pov[d] = -1; + joystick_state[js].pov[pov_nr] = -1; else - joystick_state[c].pov[d] = ((int) angle + 90 + 360) % 360; + joystick_state[js].pov[pov_nr] = ((int) angle + 90 + 360) % 360; } } else { - for (d = 0; d < joystick_get_axis_count(joystick_type); d++) - joystick_state[c].axis[d] = 0; - for (d = 0; d < joystick_get_button_count(joystick_type); d++) - joystick_state[c].button[d] = 0; - for (d = 0; d < joystick_get_pov_count(joystick_type); d++) - joystick_state[c].pov[d] = -1; + for (int axis_nr = 0; axis_nr < joystick_get_axis_count(joystick_type); axis_nr++) + joystick_state[js].axis[axis_nr] = 0; + + for (int button_nr = 0; button_nr < joystick_get_button_count(joystick_type); button_nr++) + joystick_state[js].button[button_nr] = 0; + + for (int pov_nr = 0; pov_nr < joystick_get_pov_count(joystick_type); pov_nr++) + joystick_state[js].pov[pov_nr] = -1; } } }