sort_controllers.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ('04b4', '2412', 'c529'), # Flydigi Vader 2, Vader 2 Pro, Apex 2, Apex 3
  22. ('16c0', '05e1', '0000'), # Xinmotek Controller
  23. )
  24. def find_element(prefix, bindings):
  25. i=0
  26. for element in bindings:
  27. if element.startswith(prefix):
  28. return i
  29. i=(i + 1)
  30. return -1
  31. def save_controller(line):
  32. global controllers
  33. match = split_pattern.match(line)
  34. entry = [ match.group(1), match.group(2), match.group(3) ]
  35. bindings = sorted(match.group(4).split(","))
  36. if (bindings[0] == ""):
  37. bindings.pop(0)
  38. name = entry[2].rstrip(',')
  39. crc = ""
  40. pos = find_element("crc:", bindings)
  41. if pos >= 0:
  42. crc = bindings[pos] + ","
  43. bindings.pop(pos)
  44. guid_match = standard_guid_pattern.match(entry[1])
  45. if guid_match:
  46. groups = guid_match.groups()
  47. crc_value = groups[2] + groups[1]
  48. vid_value = groups[4] + groups[3]
  49. pid_value = groups[7] + groups[6]
  50. version_value = groups[10] + groups[9]
  51. #print("CRC: %s, VID: %s, PID: %s, VERSION: %s" % (crc_value, vid_value, pid_value, version_value))
  52. if crc_value == "0000":
  53. if crc != "":
  54. crc_value = crc[4:-1]
  55. else:
  56. print("Extracting CRC from GUID of " + name)
  57. entry[1] = groups[0] + "0000" + "".join(groups[3:])
  58. crc = "crc:" + crc_value + ","
  59. if (vid_value, pid_value, crc_value) in invalid_controllers:
  60. print("Controller '%s' not unique, skipping" % name)
  61. return
  62. pos = find_element("type", bindings)
  63. if pos >= 0:
  64. bindings.insert(0, bindings.pop(pos))
  65. pos = find_element("platform", bindings)
  66. if pos >= 0:
  67. bindings.insert(0, bindings.pop(pos))
  68. pos = find_element("sdk", bindings)
  69. if pos >= 0:
  70. bindings.append(bindings.pop(pos))
  71. pos = find_element("hint:", bindings)
  72. if pos >= 0:
  73. bindings.append(bindings.pop(pos))
  74. entry.extend(crc)
  75. entry.extend(",".join(bindings) + ",")
  76. entry.append(match.group(5))
  77. controllers.append(entry)
  78. entry_id = entry[1] + entry[3]
  79. if ',sdk' in line or ',hint:' in line:
  80. conditionals.append(entry_id)
  81. def write_controllers():
  82. global controllers
  83. global controller_guids
  84. # Check for duplicates
  85. for entry in controllers:
  86. entry_id = entry[1] + entry[3]
  87. if (entry_id in controller_guids and entry_id not in conditionals):
  88. current_name = entry[2]
  89. existing_name = controller_guids[entry_id][2]
  90. print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
  91. if (not current_name.startswith("(DUPE)")):
  92. entry[2] = f"(DUPE) {current_name}"
  93. if (not existing_name.startswith("(DUPE)")):
  94. controller_guids[entry_id][2] = f"(DUPE) {existing_name}"
  95. controller_guids[entry_id] = entry
  96. for entry in sorted(controllers, key=lambda entry: f"{entry[2]}-{entry[1]}"):
  97. line = "".join(entry) + "\n"
  98. line = line.replace("\t", " ")
  99. if not line.endswith(",\n") and not line.endswith("*/\n") and not line.endswith(",\r\n") and not line.endswith("*/\r\n"):
  100. print("Warning: '%s' is missing a comma at the end of the line" % (line))
  101. output.write(line)
  102. controllers = []
  103. controller_guids = {}
  104. for line in input:
  105. if parsing_controllers:
  106. if (line.startswith("{")):
  107. output.write(line)
  108. elif (line.startswith(" NULL")):
  109. parsing_controllers = False
  110. write_controllers()
  111. output.write(line)
  112. elif (line.startswith("#if")):
  113. print(f"Parsing {line.strip()}")
  114. output.write(line)
  115. elif (line.startswith("#endif")):
  116. write_controllers()
  117. output.write(line)
  118. else:
  119. save_controller(line)
  120. else:
  121. if (line.startswith("static const char *")):
  122. parsing_controllers = True
  123. output.write(line)
  124. output.close()
  125. print(f"Finished writing {filename}.new")