sort_controllers.py 4.8 KB

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