sort_controllers.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. #
  3. # Script to sort the game controller database entries in SDL_gamepad.c
  4. import re
  5. filename = "SDL_gamepad_db.h"
  6. input = open(filename)
  7. output = open(f"{filename}.new", "w")
  8. parsing_controllers = False
  9. controllers = []
  10. controller_guids = {}
  11. conditionals = []
  12. split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
  13. # BUS (1) CRC (3,2) VID (5,4) (6) PID (8,7) (9) VERSION (11,10) MISC (12)
  14. standard_guid_pattern = re.compile(r'^([0-9a-fA-F]{4})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})(0000)([0-9a-fA-F]{2})([0-9a-fA-F]{2})(0000)([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{4},)$')
  15. # These chipsets are used in multiple controllers with different mappings,
  16. # without enough unique information to differentiate them. e.g.
  17. # https://github.com/gabomdq/SDL_GameControllerDB/issues/202
  18. invalid_controllers = (
  19. ('0079', '0006', '0000'), # DragonRise Inc. Generic USB Joystick
  20. ('0079', '0006', '6120'), # DragonRise Inc. Generic USB Joystick
  21. ('16c0', '05e1', '0000'), # Xinmotek Controller
  22. )
  23. def find_element(prefix, bindings):
  24. i=0
  25. for element in bindings:
  26. if element.startswith(prefix):
  27. return i
  28. i=(i + 1)
  29. return -1
  30. def save_controller(line):
  31. global controllers
  32. match = split_pattern.match(line)
  33. entry = [ match.group(1), match.group(2), match.group(3) ]
  34. bindings = sorted(match.group(4).split(","))
  35. if (bindings[0] == ""):
  36. bindings.pop(0)
  37. name = entry[2].rstrip(',')
  38. crc = ""
  39. pos = find_element("crc:", bindings)
  40. if pos >= 0:
  41. crc = bindings[pos] + ","
  42. bindings.pop(pos)
  43. guid_match = standard_guid_pattern.match(entry[1])
  44. if guid_match:
  45. groups = guid_match.groups()
  46. crc_value = groups[2] + groups[1]
  47. vid_value = groups[4] + groups[3]
  48. pid_value = groups[7] + groups[6]
  49. version_value = groups[10] + groups[9]
  50. #print("CRC: %s, VID: %s, PID: %s, VERSION: %s" % (crc_value, vid_value, pid_value, version_value))
  51. if crc_value == "0000":
  52. if crc != "":
  53. crc_value = crc[4:-1]
  54. else:
  55. print("Extracting CRC from GUID of " + name)
  56. entry[1] = groups[0] + "0000" + "".join(groups[3:])
  57. crc = "crc:" + crc_value + ","
  58. if (vid_value, pid_value, crc_value) in invalid_controllers:
  59. print("Controller '%s' not unique, skipping" % name)
  60. return
  61. pos = find_element("sdk", bindings)
  62. if pos >= 0:
  63. bindings.append(bindings.pop(pos))
  64. pos = find_element("hint:", bindings)
  65. if pos >= 0:
  66. bindings.append(bindings.pop(pos))
  67. entry.extend(crc)
  68. entry.extend(",".join(bindings) + ",")
  69. entry.append(match.group(5))
  70. controllers.append(entry)
  71. entry_id = entry[1] + entry[3]
  72. if ',sdk' in line or ',hint:' in line:
  73. conditionals.append(entry_id)
  74. def write_controllers():
  75. global controllers
  76. global controller_guids
  77. # Check for duplicates
  78. for entry in controllers:
  79. entry_id = entry[1] + entry[3]
  80. if (entry_id in controller_guids and entry_id not in conditionals):
  81. current_name = entry[2]
  82. existing_name = controller_guids[entry_id][2]
  83. print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
  84. if (not current_name.startswith("(DUPE)")):
  85. entry[2] = f"(DUPE) {current_name}"
  86. if (not existing_name.startswith("(DUPE)")):
  87. controller_guids[entry_id][2] = f"(DUPE) {existing_name}"
  88. controller_guids[entry_id] = entry
  89. for entry in sorted(controllers, key=lambda entry: f"{entry[2]}-{entry[1]}"):
  90. line = "".join(entry) + "\n"
  91. line = line.replace("\t", " ")
  92. if not line.endswith(",\n") and not line.endswith("*/\n") and not line.endswith(",\r\n") and not line.endswith("*/\r\n"):
  93. print("Warning: '%s' is missing a comma at the end of the line" % (line))
  94. output.write(line)
  95. controllers = []
  96. controller_guids = {}
  97. for line in input:
  98. if parsing_controllers:
  99. if (line.startswith("{")):
  100. output.write(line)
  101. elif (line.startswith(" NULL")):
  102. parsing_controllers = False
  103. write_controllers()
  104. output.write(line)
  105. elif (line.startswith("#if")):
  106. print(f"Parsing {line.strip()}")
  107. output.write(line)
  108. elif (line.startswith("#endif")):
  109. write_controllers()
  110. output.write(line)
  111. else:
  112. save_controller(line)
  113. else:
  114. if (line.startswith("static const char *")):
  115. parsing_controllers = True
  116. output.write(line)
  117. output.close()
  118. print(f"Finished writing {filename}.new")