improved customizability of game controller configuration screen
[rocksndiamonds.git] / src / libgame / joystick.c
index a6a065cddc318a4322279fb8dd20b85d3328b973..dfe1cca4ab1e520be35cf402393fd373135555d6 100644 (file)
@@ -119,6 +119,39 @@ char *getDeviceNameFromJoystickNr(int joystick_nr)
          joystick_device_name[joystick_nr] : "");
 }
 
+char *getFormattedJoystickName(const char *name_raw)
+{
+  static char name[MAX_JOYSTICK_NAME_LEN + 1];
+  boolean name_skip_space = TRUE;
+  int i, j;
+
+  if (name_raw == NULL)
+    name_raw = "(unknown joystick)";
+
+  // copy joystick name, cutting leading and multiple spaces
+  for (i = 0, j = 0; i < strlen(name_raw) && i < MAX_JOYSTICK_NAME_LEN; i++)
+  {
+    if (name_raw[i] != ' ')
+    {
+      name[j++] = name_raw[i];
+      name_skip_space = FALSE;
+    }
+    else if (!name_skip_space)
+    {
+      name[j++] = name_raw[i];
+      name_skip_space = TRUE;
+    }
+  }
+
+  // cut trailing space
+  if (j > 0 && name[j - 1] == ' ')
+    j--;
+
+  name[j] = '\0';
+
+  return name;
+}
+
 static int JoystickPositionPercent(int center, int border, int actual)
 {
   int range, position;
@@ -161,22 +194,22 @@ void CheckJoystickData()
 
 int JoystickExt(int player_nr, boolean use_as_joystick_nr)
 {
-  int joystick_fd = joystick.fd[player_nr];
+  int joystick_nr = joystick.nr[player_nr];
   int js_x, js_y;
   boolean js_b1, js_b2;
   int left, right, up, down;
   int result = JOY_NO_ACTION;
 
   if (use_as_joystick_nr)
-    joystick_fd = player_nr;
+    joystick_nr = player_nr;
 
   if (joystick.status != JOYSTICK_ACTIVATED)
     return JOY_NO_ACTION;
 
-  if (joystick_fd < 0)
+  if (joystick_nr < 0)
     return JOY_NO_ACTION;
 
-  if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2))
+  if (!ReadJoystick(joystick_nr, &js_x, &js_y, &js_b1, &js_b2))
   {
     Error(ERR_WARN, "cannot read joystick device '%s'",
          setup.input[player_nr].joy.device_name);