[gnome-games/wip/abhinavsingh/gamepad-reassign: 20/27] gamepad: Allow incomplete mappings



commit cf914d4c3008de86fa70c513c987173a7f0e04da
Author: theawless <theawless gmail com>
Date:   Thu Jul 27 01:27:16 2017 +0530

    gamepad: Allow incomplete mappings
    
    Ignore a gamepad input if the mapping doesn't contain it.

 src/gamepad/gamepad-mapping.c |   39 +++++++++++++++++++++++++++++----------
 1 files changed, 29 insertions(+), 10 deletions(-)
---
diff --git a/src/gamepad/gamepad-mapping.c b/src/gamepad/gamepad-mapping.c
index ea4b1c9..9d49655 100644
--- a/src/gamepad/gamepad-mapping.c
+++ b/src/gamepad/gamepad-mapping.c
@@ -266,6 +266,8 @@ games_gamepad_mapping_get_dpad_mapping (GamesGamepadMapping *self,
                                         GamesGamepadInput   *destination)
 {
   GamesGamepadDPad *dpad;
+  GamesGamepadInput *dpad_input;
+
   gint dpad_changed_value;
   gint dpad_position;
 
@@ -274,14 +276,23 @@ games_gamepad_mapping_get_dpad_mapping (GamesGamepadMapping *self,
 
   memset (destination, 0, sizeof (GamesGamepadInput));
 
+  destination->type = EV_MAX;
+  if (dpad_index >= self->dpads->len)
+    return;
   dpad = &g_array_index (self->dpads, GamesGamepadDPad, dpad_index);
+  if (dpad == NULL)
+    return;
   dpad_changed_value = (dpad_value == 0) ?
     dpad->axis_values[dpad_axis] :
     dpad_value;
-  // We add 4 so that the remainder is always positive.
+  // Add 4 so that the remainder is always positive.
   dpad_position = (dpad_changed_value + dpad_axis + 4) % 4;
   dpad->axis_values[dpad_axis] = dpad_value;
-  *destination = dpad->inputs[dpad_position];
+  if (dpad_position >= 4)
+    return;
+  dpad_input = &dpad->inputs[dpad_position];
+  if (dpad_input != NULL)
+    *destination = *dpad_input;
 }
 
 void
@@ -289,15 +300,19 @@ games_gamepad_mapping_get_axis_mapping (GamesGamepadMapping *self,
                                         gint                 axis_number,
                                         GamesGamepadInput   *destination)
 {
+  GamesGamepadInput *axis;
+
   g_return_if_fail (self != NULL);
   g_return_if_fail (destination != NULL);
 
   memset (destination, 0, sizeof (GamesGamepadInput));
 
-  destination->type = (axis_number < self->axes->len) ?
-    g_array_index (self->axes, GamesGamepadInput, axis_number).type :
-    EV_MAX;
-  destination->code = g_array_index (self->axes, GamesGamepadInput, axis_number).code;
+  destination->type = EV_MAX;
+  if (axis_number >= self->axes->len)
+    return;
+  axis = &g_array_index (self->axes, GamesGamepadInput, axis_number);
+  if (axis != NULL)
+    *destination = *axis;
 }
 
 void
@@ -305,15 +320,19 @@ games_gamepad_mapping_get_button_mapping (GamesGamepadMapping *self,
                                           gint                 button_number,
                                           GamesGamepadInput   *destination)
 {
+  GamesGamepadInput *button;
+
   g_return_if_fail (self != NULL);
   g_return_if_fail (destination != NULL);
 
   memset (destination, 0, sizeof (GamesGamepadInput));
 
-  destination->type = (button_number < self->buttons->len) ?
-    g_array_index (self->buttons, GamesGamepadInput, button_number).type :
-    EV_MAX;
-  destination->code = g_array_index (self->buttons, GamesGamepadInput, button_number).code;
+  destination->type = EV_MAX;
+  if (button_number >= self->buttons->len)
+    return;
+  button = &g_array_index (self->buttons, GamesGamepadInput, button_number);
+  if (button != NULL)
+    *destination = *button;
 }
 
 /* Type */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]