From df54448b20d488c0d1630497d31204128f593746 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Fri, 14 Feb 2014 11:06:15 -0500 Subject: [PATCH] allow for joystick hotplug --- include/joy.h | 1 + src/osapi/osapi.cpp | 48 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/joy.h b/include/joy.h index 15cd220..cba7d13 100644 --- a/include/joy.h +++ b/include/joy.h @@ -104,6 +104,7 @@ extern int Joy_sensitivity; extern int Dead_zone_size; // percentage of range that is dead zone int joy_init(); +void joy_close(); void joy_flush(); int joy_get_pos(int * x, int * y, int *z, int *r); int joy_down_count(int btn, int reset_count = 1); diff --git a/src/osapi/osapi.cpp b/src/osapi/osapi.cpp index bfdb1f8..be91f09 100644 --- a/src/osapi/osapi.cpp +++ b/src/osapi/osapi.cpp @@ -295,23 +295,29 @@ void os_poll() while (SDL_PollEvent (&e)) { switch (e.type) { case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONUP: { if (e.motion.windowID > 0) { mouse_mark_button(e.button.button, e.button.state); } + break; + } - case SDL_MOUSEMOTION: + case SDL_MOUSEMOTION: { if (e.motion.windowID > 0) { mouse_update_pos(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel); } + break; + } - case SDL_TEXTINPUT: + case SDL_TEXTINPUT: { key_set_text_input((int)e.text.text[0]); + break; + } - case SDL_KEYDOWN: + case SDL_KEYDOWN: { if (e.key.keysym.mod & KMOD_GUI) { if (e.key.keysym.sym == SDLK_f ) { gr_opengl_force_fullscreen(); @@ -323,16 +329,20 @@ void os_poll() } else { key_mark(e.key.keysym.scancode, 1, e.key.keysym.mod, 0); } + break; + } - case SDL_KEYUP: + case SDL_KEYUP: { if (e.key.keysym.mod & KMOD_GUI) { // blank, just don't want to process up keys we skipped // the down for } else { key_mark(e.key.keysym.scancode, 0, e.key.keysym.mod, 0); } + break; + } /* case SDL_ACTIVEEVENT: if (e.active.state & SDL_APPACTIVE) { @@ -344,21 +354,41 @@ void os_poll() } break; */ - case SDL_JOYAXISMOTION: + case SDL_JOYDEVICEADDED: { + if ( !Is_standalone ) { + joy_init(); + } + + break; + } + + case SDL_JOYDEVICEREMOVED: { + if (e.jdevice.which == joystick_get_id()) { + joy_close(); + } + + break; + } + + case SDL_JOYAXISMOTION: { if (e.jaxis.which == joystick_get_id()) { joystick_update_axis(e.jaxis.axis, e.jaxis.value); } + break; + } case SDL_JOYBUTTONDOWN: - case SDL_JOYBUTTONUP: + case SDL_JOYBUTTONUP: { if (e.jbutton.which == joystick_get_id()) { state = (e.jbutton.state == SDL_PRESSED) ? 1 : 0; joy_mark_button((int)e.jbutton.button, state); } + break; + } - case SDL_JOYHATMOTION: + case SDL_JOYHATMOTION: { if (e.jhat.which == joystick_get_id()) { // can only handle one hat if (e.jhat.hat == 0) { @@ -389,7 +419,9 @@ void os_poll() joy_mark_button(button, state); } } + break; + } case SDL_WINDOWEVENT: { switch (e.window.event) { -- 2.39.2