|
4 | 4 |
|
5 | 5 | esInputs = batoceraFiles.esInputs |
6 | 6 |
|
| 7 | +"""Default mapping of Batocera keys to SDL_GAMECONTROLLERCONFIG keys.""" |
| 8 | +_DEFAULT_SDL_MAPPING = { |
| 9 | + 'b': 'a', 'a': 'b', |
| 10 | + 'x': 'y', 'y': 'x', |
| 11 | + 'l2': 'lefttrigger', 'r2': 'righttrigger', |
| 12 | + 'l3': 'leftstick', 'r3': 'rightstick', |
| 13 | + 'pageup': 'leftshoulder', 'pagedown': 'rightshoulder', |
| 14 | + 'start': 'start', 'select': 'back', |
| 15 | + 'up': 'dpup', 'down': 'dpdown', 'left': 'dpleft', 'right': 'dpright', |
| 16 | + 'joystick1up': 'lefty', 'joystick1left': 'leftx', |
| 17 | + 'joystick2up': 'righty', 'joystick2left': 'rightx', 'hotkey': 'guide' |
| 18 | +} |
7 | 19 |
|
8 | 20 | class Input: |
9 | 21 | def __init__(self, name, type, id, value, code): |
@@ -32,50 +44,7 @@ def __init__(self, configName, type, guid, player, index="-1", realName="", inpu |
32 | 44 | self.inputs = inputs |
33 | 45 |
|
34 | 46 | def generateSDLGameDBLine(self): |
35 | | - # Making a dirty assumption here : if a dpad is an axis, then it shouldn't have any analog joystick |
36 | | - nameMapping = { |
37 | | - 'a' : { 'button' : 'b' }, |
38 | | - 'b' : { 'button' : 'a' }, |
39 | | - 'x' : { 'button' : 'y' }, |
40 | | - 'y' : { 'button' : 'x' }, |
41 | | - 'start' : { 'button' : 'start' }, |
42 | | - 'select' : { 'button' : 'back' }, |
43 | | - 'pageup' : { 'button' : 'leftshoulder' }, |
44 | | - 'pagedown' : { 'button' : 'rightshoulder' }, |
45 | | - 'l2' : { 'button' : 'lefttrigger', 'axis' : 'lefttrigger' }, |
46 | | - 'r2' : { 'button' : 'righttrigger', 'axis' : 'righttrigger' }, |
47 | | - 'l3' : { 'button' : 'leftstick' }, |
48 | | - 'r3' : { 'button' : 'rightstick' }, |
49 | | - 'up' : { 'button' : 'dpup', 'hat' : 'dpup', 'axis' : 'lefty' }, |
50 | | - 'down' : { 'button' : 'dpdown', 'hat' : 'dpdown' }, |
51 | | - 'left' : { 'button' : 'dpleft', 'hat' : 'dpleft', 'axis' : 'leftx' }, |
52 | | - 'right' : { 'button' : 'dpright', 'hat' : 'dpright' }, |
53 | | - 'joystick1up' : { 'axis' : 'lefty' }, |
54 | | - 'joystick1left' : { 'axis' : 'leftx' }, |
55 | | - 'joystick2up' : { 'axis' : 'righty' }, |
56 | | - 'joystick2left' : { 'axis' : 'rightx' }, |
57 | | - 'hotkey' : { 'button' : 'guide' } |
58 | | - } |
59 | | - typePrefix = { |
60 | | - 'axis' : 'a', |
61 | | - 'button' : 'b', |
62 | | - 'hat' : 'h0.' # Force dpad 0 until ES handles others |
63 | | - } |
64 | | - |
65 | | - if not self.inputs: |
66 | | - return None |
67 | | - # TODO: python3 - force to use unicode |
68 | | - strOut = u"{},{},platform:Linux,".format(self.guid, self.configName) |
69 | | - |
70 | | - for idx, input in self.inputs.iteritems(): |
71 | | - if input.name in nameMapping and input.type in typePrefix and input.type in nameMapping[input.name] : |
72 | | - if input.type == 'hat': |
73 | | - # TODO: python3 - force to use unicode |
74 | | - strOut += u"{}:{}{},".format(nameMapping[input.name][input.type], typePrefix[input.type], input.value) |
75 | | - else: |
76 | | - # TODO: python3 - force to use unicode |
77 | | - strOut += u"{}:{}{},".format(nameMapping[input.name][input.type], typePrefix[input.type], input.id) |
78 | | - return strOut |
| 47 | + return _generateSdlGameControllerConfig(self) |
79 | 48 |
|
80 | 49 |
|
81 | 50 | # Load all controllers from the es_input.cfg |
@@ -143,11 +112,65 @@ def findBestControllerConfig(controllers, x, pxguid, pxindex, pxname, pxdev, pxn |
143 | 112 | controller.inputs, pxdev, pxnbbuttons, pxnbhats, pxnbaxes) |
144 | 113 | return None |
145 | 114 |
|
146 | | -def generateSDLGameDBAllControllers(controllers, outputFile = "/tmp/gamecontrollerdb.txt"): |
147 | | - finalData = [] |
| 115 | + |
| 116 | +def _generateSdlGameControllerConfig(controller, sdlMapping=_DEFAULT_SDL_MAPPING): |
| 117 | + """Returns an SDL_GAMECONTROLLERCONFIG-formatted string for the given configuration.""" |
| 118 | + config = [] |
| 119 | + config.append(controller.guid) |
| 120 | + config.append(controller.configName) |
| 121 | + for k in controller.inputs: |
| 122 | + input = controller.inputs[k] |
| 123 | + keyname = sdlMapping.get(input.name, None) |
| 124 | + if input.name is not None: |
| 125 | + sdlConf = _keyToSdlGameControllerConfig( |
| 126 | + keyname, input.type, input.id, input.value) |
| 127 | + if sdlConf is not None: |
| 128 | + config.append(sdlConf) |
| 129 | + config.append('') |
| 130 | + return ','.join(config) |
| 131 | + |
| 132 | + |
| 133 | +def _keyToSdlGameControllerConfig(keyname, type, id, value=None): |
| 134 | + """ |
| 135 | + Converts a key mapping to the SDL_GAMECONTROLLER format. |
| 136 | +
|
| 137 | + Arguments: |
| 138 | + keyname: (str) One of the SDL_GAMECONTROLLERCONFIG keys. |
| 139 | + type: (str) 'button', 'hat', or 'axis' |
| 140 | + id: (int) Numeric key id. |
| 141 | + value: (int) Hat value. Only used if type == 'hat'. |
| 142 | + Returns: |
| 143 | + (str) SDL_GAMECONTROLLERCONFIG-formatted key mapping string. |
| 144 | + Examples: |
| 145 | + keyToSdlGameControllerConfig('button', 'leftshoulder', 6) |
| 146 | + 'leftshoulder:b6' |
| 147 | +
|
| 148 | + keyToSdlGameControllerConfig('hat', 'dpleft', 0, 8) |
| 149 | + 'dpleft:h0.9' |
| 150 | +
|
| 151 | + keyToSdlGameControllerConfig('axis', 'lefty', 1) |
| 152 | + 'lefty:a1' |
| 153 | + """ |
| 154 | + if type == 'button': |
| 155 | + return '{}:b{}'.format(keyname, id) |
| 156 | + elif type == 'hat': |
| 157 | + return '{}:h{}.{}'.format(keyname, id, value) |
| 158 | + elif type == 'axis': |
| 159 | + return '{}:a{}{}'.format(keyname, id, '~' if int(value) > 0 else '') |
| 160 | + elif type == 'key': |
| 161 | + return None |
| 162 | + else: |
| 163 | + raise ValueError, 'unknown key type: {!r}'.format(type) |
| 164 | + |
| 165 | + |
| 166 | +def generateSdlGameControllerConfig(controllers): |
| 167 | + configs = [] |
148 | 168 | for idx, controller in controllers.iteritems(): |
149 | | - finalData.append(controller.generateSDLGameDBLine()) |
150 | | - sdlData = "\n".join(finalData).encode("utf-8") |
| 169 | + configs.append(controller.generateSDLGameDBLine()) |
| 170 | + return "\n".join(configs) |
| 171 | + |
| 172 | + |
| 173 | +def writeSDLGameDBAllControllers(controllers, outputFile = "/tmp/gamecontrollerdb.txt"): |
151 | 174 | with open(outputFile, "w") as text_file: |
152 | | - text_file.write(sdlData) |
| 175 | + text_file.write(generateSdlGameControllerConfig(controllers).encode("utf-8")) |
153 | 176 | return outputFile |
0 commit comments