|
- #include "IZ_joystick.h"
-
- void IZ_HandleJoystickDeviceEvents(SDL_Event e, IZ_JoystickState* state) {
- if (e.type == SDL_JOYDEVICEADDED) {
- if (SDL_NumJoysticks() <= PLAYERS && !state->joystick_instance) {
- state->joystick_instance = SDL_JoystickOpen(e.jdevice.which);
- }
- }
-
- if (e.type == SDL_JOYDEVICEREMOVED) {
- if (
- state->joystick_instance
- && SDL_JoystickInstanceID(state->joystick_instance) == e.jdevice.which
- ) {
- state->joystick_instance = NULL;
- }
- }
- }
-
- void IZ_HandleJoystickAxisEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
- if (e.type == SDL_JOYAXISMOTION) {
- if (e.jaxis.axis == 0 || e.jaxis.axis == 3) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jaxis.value > state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_RIGHT);
- } else if (e.jaxis.value <= -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_LEFT);
- }
- }
- if (e.jaxis.axis == 1 || e.jaxis.axis == 4) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
- *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- if (e.jaxis.value > -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_DOWN);
- } else if (e.jaxis.value <= -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_UP);
- }
- }
- }
- }
-
- void IZ_HandleJoystickHatEvents(SDL_Event e, IZ_Action* action) {
- if (e.type == SDL_JOYHATMOTION) {
- *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
- *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jhat.value != 0) {
- *action |= e.jhat.value;
- }
- }
- }
-
- void IZ_HandleJoystickButtonEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
- for (uint8_t i = 4; i < CONTROLS; i += 1) {
- if (e.jbutton.button == state->config.control_mapping[i]) {
- const uint16_t bitflag = (0x1 << i);
- if (e.type == SDL_JOYBUTTONDOWN) {
- *action |= bitflag;
- } else if (e.type == SDL_JOYBUTTONUP) {
- *action &= ~bitflag;
- }
- }
- }
- }
-
- void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
- IZ_HandleJoystickDeviceEvents(e, state);
- IZ_HandleJoystickAxisEvents(e, state, action);
- IZ_HandleJoystickHatEvents(e, action);
- IZ_HandleJoystickButtonEvents(e, state, action);
- }
-
- void IZ_LoadJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
- char config_path[128];
- IZ_GetConfigPath(config_path);
-
- char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
- joystick_control_mapping_section_name[9] = (char) (48 + p);
- for (uint8_t i = 4; i < CONTROLS; i += 1) {
- config->control_mapping[i] = ini_getl(joystick_control_mapping_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[p][i], config_path);
- }
-
- char joystick_section_name[] = "Joystick.0";
- joystick_section_name[9] = (char) (48 + p);
- config->axis_threshold = ini_getl(joystick_section_name, "AxisThreshold", 8000, config_path);
- }
-
- void IZ_SaveJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
- char config_path[128];
- IZ_GetConfigPath(config_path);
-
- char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
- joystick_control_mapping_section_name[9] = (char) (48 + p);
- for (uint8_t i = 4; i < CONTROLS; i += 1) {
- ini_putl(joystick_control_mapping_section_name, ACTION_NAMES[i], config->control_mapping[i], config_path);
- }
-
- char joystick_section_name[] = "Joystick.0";
- joystick_section_name[9] = (char) (48 + p);
- ini_putl(joystick_section_name, "AxisThreshold", config->axis_threshold, config_path);
- }
|