input joy sleep wake, revisit string cat code
[vg.git] / vg_input.c
index 74732f4d2c485ba871cbf024224bdc777809cc63..d4e51bc6b77ca3c3c974a4cf9fa7e9156ac2a6c0 100644 (file)
@@ -128,13 +128,33 @@ void vg_input_device_event( SDL_Event *ev )
    }
 }
 
+static void vg_input_set_active_controller( int index, const char *why )
+{
+   if( vg_input.active_controller_index != index )
+   {
+      vg_input.display_input_type = 
+         SDL_GameControllerGetType( vg_input.controllers[index].handle );
+      vg_input.active_controller_index = index;
+      vg_info( "Switching controller index to #%d. (%s)\n", index, why );
+   }
+
+   if( vg_input.display_input_method != k_input_method_controller )
+   {
+      vg_input.display_input_method = k_input_method_controller;
+      vg_info( "Switching input method to controller. (%s)\n", why );
+   }
+}
+
 void vg_input_controller_event( SDL_Event *ev )
 {
-   if( ev->type == SDL_CONTROLLERAXISMOTION ){
-      for( int i=0; i<VG_MAX_CONTROLLERS; i++ ){
+   if( ev->type == SDL_CONTROLLERAXISMOTION )
+   {
+      for( int i=0; i<VG_MAX_CONTROLLERS; i++ )
+      {
          struct vg_controller *esta = &vg_input.controllers[i];
 
-         if( ev->caxis.which == esta->instance_id ){
+         if( ev->caxis.which == esta->instance_id )
+         {
             float value = (float)ev->caxis.value / 32767.0f;
 
             if( ev->caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ||
@@ -146,6 +166,8 @@ void vg_input_controller_event( SDL_Event *ev )
                      high    = vg_maxf( 0.0f, fabsf(value) - deadz );
                
                value = vg_signf(value) * (high / (1.0f-deadz));
+               if( fabsf(value) > 0.5f )
+                  vg_input_set_active_controller( i, "Stick pushed >|0.5|" );
             }
 
             esta->axises[ ev->caxis.axis ] = value;
@@ -153,50 +175,26 @@ void vg_input_controller_event( SDL_Event *ev )
          }
       }
    }
-   else if( ev->type == SDL_CONTROLLERBUTTONDOWN ){
-      struct vg_controller *active = NULL;
-
-      if( vg_input.active_controller_index >= 0 )
-         active = &vg_input.controllers[vg_input.active_controller_index];
-
-      if( !active || (ev->cbutton.which != active->instance_id) ){
-         active = NULL;
-         vg_input.active_controller_index = -1;
-         vg_input.display_input_method = k_input_method_kbm;
-
-         for( int i=0; i<VG_MAX_CONTROLLERS; i++ ){
-            if( vg_input.controllers[i].instance_id == ev->cbutton.which ){
-               active = &vg_input.controllers[i];
-               vg_input.active_controller_index = i;
-               vg_input.display_input_type = 
-                  SDL_GameControllerGetType(active->handle);
-               break;
-            }
-         }
-         
-         if( active ){
-            vg_info( "Switching active controller index to #%d\n", 
-                       vg_input.active_controller_index );
-         }
-         else{
-            vg_error( "Input out of range (SDL_JoystickID#%d)\n", 
-                       ev->cbutton.which );
-         }
-      }
-
-      if( active ){
-         if( vg_input.display_input_method != k_input_method_controller ){
-            vg_input.display_input_method = k_input_method_controller;
-            vg_info( "display_input: k_input_method_controller\n" );
+   else if( ev->type == SDL_CONTROLLERBUTTONDOWN )
+   {
+      for( int i=0; i<VG_MAX_CONTROLLERS; i++ )
+      {
+         struct vg_controller *esta = &vg_input.controllers[i];
+         if( esta->instance_id == ev->cbutton.which )
+         {
+            vg_input_set_active_controller( i, "Button press" );
+            esta->buttons[ ev->cbutton.button ] = 1;
+            break;
          }
-         active->buttons[ ev->cbutton.button ] = 1;
       }
    }
-   else if( ev->type == SDL_CONTROLLERBUTTONUP ){
-      for( int i=0; i<VG_MAX_CONTROLLERS; i++ ){
+   else if( ev->type == SDL_CONTROLLERBUTTONUP )
+   {
+      for( int i=0; i<VG_MAX_CONTROLLERS; i++ )
+      {
          struct vg_controller *esta = &vg_input.controllers[i];
-
-         if( ev->cbutton.which == esta->instance_id ){
+         if( ev->cbutton.which == esta->instance_id )
+         {
             esta->buttons[ ev->cbutton.button ] = 0;
             break;
          }
@@ -210,10 +208,13 @@ void vg_process_inputs(void)
    vg_input.sdl_keys = SDL_GetKeyboardState( &count );
    vg_input.sdl_mouse = SDL_GetMouseState(NULL,NULL);
 
-   if( vg_input.display_input_method != k_input_method_kbm ){
+   if( vg_input.display_input_method != k_input_method_kbm )
+   {
       /* check for giving keyboard priority */
-      for( int i=0; i<count; i++ ){
-         if( vg_input.sdl_keys[i] ){
+      for( int i=0; i<count; i++ )
+      {
+         if( vg_input.sdl_keys[i] )
+         {
             vg_input.display_input_method = k_input_method_kbm;
             vg_info( "display_input: k_input_method_kbm (keyboard %d)\n", i );
             break;
@@ -226,9 +227,19 @@ void vg_process_inputs(void)
              SDL_BUTTON(SDL_BUTTON_MIDDLE)) )
       {
          vg_input.display_input_method = k_input_method_kbm;
-         vg_info( "display_input: k_input_method_kbm (mouse)\n" );
+         vg_info( "display_input: k_input_method_kbm (mouse click)\n" );
+      }
+
+      vg_input.hidden_mouse_travel += v2_length( vg.mouse_delta ); 
+      if( vg_input.hidden_mouse_travel > 64.0f )
+      {
+         vg_input.display_input_method = k_input_method_kbm;
+         vg_input.hidden_mouse_travel = 0.0f;
+         vg_info( "display_input: k_input_method_kbm (mouse move)\n" );
       }
    }
+   else
+      vg_input.hidden_mouse_travel = 0.0f;
 }
 
 void async_vg_input_init( void *payload, u32 size )