|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #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);
- }
- return;
- }
-
- 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 == IZ_JOYAXIS_DIRECTION_HORIZONTAL1
- || e.jaxis.axis == IZ_JOYAXIS_DIRECTION_HORIZONTAL2
- ) {
- *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);
- return;
- }
- if (e.jaxis.value <= -state->config.axis_threshold) {
- *action |= (0x1 << IZ_ACTION_INDEX_LEFT);
- }
- return;
- }
- if (
- e.jaxis.axis == IZ_JOYAXIS_DIRECTION_VERTICAL1
- || e.jaxis.axis == IZ_JOYAXIS_DIRECTION_VERTICAL2
- ) {
- *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);
- return;
- }
- 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;
- return;
- }
-
- if (e.type == SDL_JOYBUTTONUP) {
- *action &= ~bitflag;
- return;
- }
- }
- }
- }
-
- 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, 128);
-
- 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, 128);
-
- 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);
- }
|