-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautoconfig.py
357 lines (284 loc) · 9.13 KB
/
autoconfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import xml.etree.ElementTree as ET
import os
import sys
import tools
MODE = 0
OVERLAY_STYLE = ""
SNES_MINI_THEME = 0
MODE_HDMI = "HDMI"
MODE_LCD = "LCD"
def getWidth():
return int(subprocess.check_output(["fbset"]).splitlines()[2].strip().split(' ')[1])
def getHeight():
return int(subprocess.check_output(["fbset"]).splitlines()[2].strip().split(' ')[2])
def compatibleResolution(width, height):
resolution = ""
if (width == 640 and height == 480) or (width == 800 and height == 600) or (width == 1024 and height == 768) or (width == 1280 and height == 720) or (width == 1280 and height == 960) or (width == 1366 and height == 768) or (width == 1440 and height == 1080) or (width == 1920 and height == 1080):
resolution = str(width) + "x" + str(height)
else:
resolution = "640x480"
return str(resolution)
def makeEmulationStationCfg(mode):
# set Theme
if (mode == MODE_LCD):
lcd_theme = tools.getOptionFromPiboyconfFile('theme_lcd')
if len(lcd_theme) > 0:
p2 = subprocess.Popen(" python " + tools.READERS_FOLDER + "es_config_file.py ThemeSet set " + lcd_theme, stdout=subprocess.PIPE, shell=True)
out, err = p2.communicate()
result = out.split('\n')
elif (mode == MODE_HDMI):
tv_theme = tools.getOptionFromPiboyconfFile('theme_tv')
if len(tv_theme) > 0:
p2 = subprocess.Popen(" python " + tools.READERS_FOLDER + "es_config_file.py ThemeSet set " + tv_theme, stdout=subprocess.PIPE, shell=True)
out, err = p2.communicate()
result = out.split('\n')
def makeRetroarchCfg(system, mode, overlayStyle):
uid = 1000
gid = 1000
if (mode == MODE_HDMI):
suffix = "tv"
elif (mode == MODE_LCD):
suffix = "lcd"
path = tools.CONFIGS_ROOT_FOLDER + system + "/" + "retroarch.cfg." + suffix
# check if config file mode exits
if (not os.path.exists(path)):
return
# removes existing retroarch.cfg in configs folder
if (os.path.exists(tools.CONFIGS_ROOT_FOLDER + system + "/" + "retroarch.cfg")):
os.remove(tools.CONFIGS_ROOT_FOLDER + system + "/" + "retroarch.cfg")
# copy the appropriate file according to the actual mode
fin = open(path, "rt")
fout = open(tools.CONFIGS_ROOT_FOLDER + system + "/" + "retroarch.cfg", "wt")
for line in fin:
# replace necessary entries
line = line.replace('[overlay_style]', overlayStyle.lower())
fout.write(line)
fin.close()
fout.close()
# set the owner to "pi"
os.chown(tools.CONFIGS_ROOT_FOLDER + system + "/" + "retroarch.cfg", uid, gid)
return
def changeEmulatorsCfgSGB(mode):
path = tools.CONFIGS_ROOT_FOLDER + "sgb/emulators.cfg"
# check if file exits
if (not os.path.exists(path)):
return
file = open(path, "rt")
data = file.read()
# change default emulator according to MODE
if (mode == MODE_LCD):
data = data.replace('default = "lr-mgba-sgb"', 'default = "lr-gambatte"')
else:
data = data.replace('default = "lr-gambatte"', 'default = "lr-mgba-sgb"')
file.close()
file = open(path, "wt")
file.write(data)
file.close()
return
def enableOnboardPiboyController():
enableOnboardPiboyControllerInESInput()
setOnboardPiboyControllerRetroarchJoypads(True)
def setOnboardPiboyControllerRetroarchJoypads(enable):
uid = 1000
gid = 1000
if (enable==True):
path = tools.RETROARCHJOYPADS_CONFIG_FOLDER + 'PiBoy DMG Controller.cfg.disabled'
else:
path = tools.RETROARCHJOYPADS_CONFIG_FOLDER + 'PiBoy DMG Controller.cfg'
# check if config file exits
if (not os.path.exists(path)):
return -1
if (enable==True):
os.rename(path, tools.RETROARCHJOYPADS_CONFIG_FOLDER + 'PiBoy DMG Controller.cfg')
else:
os.rename(path, tools.RETROARCHJOYPADS_CONFIG_FOLDER + 'PiBoy DMG Controller.cfg.disabled')
def enableOnboardPiboyControllerInESInput():
uid = 1000
gid = 1000
# in ES input
pathEsInput = tools.EMULATIONSTATION_CONFIG_FOLDER + 'es_input.cfg'
# check if config file exits
if (not os.path.exists(pathEsInput)):
return -1
pathPiboyEsInput = tools.EMULATIONSTATION_CONFIG_FOLDER + 'piboy_es_input.cfg'
# check if config file exits
if (not os.path.exists(pathPiboyEsInput)):
#print(pathPiboyEsInput + " is not exists")
return -1
treeOptions = ET.parse(pathPiboyEsInput)
inputConfigPiboy = treeOptions.getroot()
esInput = ET.parse(pathEsInput)
inputList = esInput.getroot()
already_here = False
for inputConfig in inputList.iter('inputConfig'):
if inputConfig.get("deviceName") == tools.PIBOY_CONTROLLER_NAME:
already_here = True
if not already_here:
inputList.insert(1, inputConfigPiboy)
tools.indent(inputList)
esInput.write(pathEsInput, encoding="utf-8", xml_declaration=True)
def disableOnboardPiboyControllerInESInput():
uid = 1000
gid = 1000
path = tools.EMULATIONSTATION_CONFIG_FOLDER + 'es_input.cfg'
# check if config file exits
if (not os.path.exists(path)):
return -1
treeOptions = ET.parse(path)
inputList = treeOptions.getroot()
for inputConfig in inputList.iter('inputConfig'):
if inputConfig.get("deviceName") == tools.PIBOY_CONTROLLER_NAME:
# save the xml
newPath = tools.EMULATIONSTATION_CONFIG_FOLDER + 'piboy_es_input.cfg'
tree = ET.ElementTree(inputConfig)
tree.write(newPath, encoding="utf-8", xml_declaration=True)
# set the owner to "pi"
os.chown(newPath, uid, gid)
# delete in es_input
inputList.remove(inputConfig)
treeOptions.write(path, encoding="utf-8", xml_declaration=True)
def disableOnboardPiboyController():
disableOnboardPiboyControllerInESInput()
setOnboardPiboyControllerRetroarchJoypads(False)
def manageSystems(systems, actual_path):
for system in systems[1]:
newPath = r"" + actual_path + system + '/'
if os.path.isdir(newPath):
subfolders = next(os.walk(newPath))
if len(subfolders[1]) > 0 :
manageSystems(subfolders, newPath)
makeRetroarchCfg(system, MODE, OVERLAY_STYLE)
#####################
# BEGIN
width = getWidth()
height = getHeight()
resolution = compatibleResolution(width, height)
# Determine MODE
if (resolution == "640x480"):
MODE = MODE_LCD
else:
MODE = MODE_HDMI
# Read options
res = tools.getOptionFromPiboyconfFile('overlay_style')
if (res <> -1):
OVERLAY_STYLE = res.upper()
else:
exit(-1)
# Disable PiBoy onboard controller if needed
res = tools.getOptionFromPiboyconfFile('onboard_controller_hdmi')
if (res <> -1):
ONBOARD_CONTROLLER_HDMI = res
else:
exit(-1)
if (MODE == MODE_HDMI):
if (ONBOARD_CONTROLLER_HDMI == 'no'):
disableOnboardPiboyController()
else:
enableOnboardPiboyController()
else:
#print('Try to enable onBoardController if necessary')
enableOnboardPiboyController()
# WiFi
if (MODE == MODE_HDMI):
res = tools.getOptionFromPiboyconfFile('wifi_always_enable_hdmi')
if (res <> -1):
WIFI_ALWAYS_ENABLE_HDMI = res
else:
exit(-1)
if (WIFI_ALWAYS_ENABLE_HDMI != 'undefined'):
if (WIFI_ALWAYS_ENABLE_HDMI == 'yes'):
tools.setWiFi(True)
elif (WIFI_ALWAYS_ENABLE_HDMI == 'no'):
tools.setWiFi(False)
else:
res = tools.getOptionFromPiboyconfFile('wifi_enable')
if (res <> -1):
if (res == 'yes'):
tools.setWiFi(True)
elif (res == 'no'):
tools.setWiFi(False)
else:
tools.setWiFi(True)
elif (MODE == MODE_LCD):
res = tools.getOptionFromPiboyconfFile('wifi_always_enable_lcd')
if (res <> -1):
WIFI_ALWAYS_ENABLE_LCD = res
else:
exit(-1)
if (WIFI_ALWAYS_ENABLE_LCD != 'undefined'):
if (WIFI_ALWAYS_ENABLE_LCD == 'yes'):
tools.setWiFi(True)
elif (WIFI_ALWAYS_ENABLE_LCD == 'no'):
tools.setWiFi(False)
else:
res = tools.getOptionFromPiboyconfFile('wifi_enable')
if (res <> -1):
if (res == 'yes'):
tools.setWiFi(True)
elif (res == 'no'):
tools.setWiFi(False)
else:
tools.setWiFi(True)
# Bkuetooth
if (MODE == MODE_HDMI):
res = tools.getOptionFromPiboyconfFile('bt_always_enable_hdmi')
if (res <> -1):
BT_ALWAYS_ENABLE_HDMI = res
else:
exit(-1)
if (BT_ALWAYS_ENABLE_HDMI != 'undefined'):
if (BT_ALWAYS_ENABLE_HDMI == 'yes'):
tools.setBluetooth(True)
elif (BT_ALWAYS_ENABLE_HDMI == 'no'):
tools.setBluetooth(False)
else:
res = tools.getOptionFromPiboyconfFile('bt_enable')
if (res <> -1):
if (res == 'yes'):
tools.setBluetooth(True)
elif (res == 'no'):
tools.setBluetooth(False)
else:
tools.setBluetooth(True)
elif (MODE == MODE_LCD):
res = tools.getOptionFromPiboyconfFile('bt_always_enable_lcd')
if (res <> -1):
BT_ALWAYS_ENABLE_LCD = res
else:
exit(-1)
if (BT_ALWAYS_ENABLE_LCD != 'undefined'):
if (BT_ALWAYS_ENABLE_LCD == 'yes'):
tools.setBluetooth(True)
elif (BT_ALWAYS_ENABLE_LCD == 'no'):
tools.setBluetooth(False)
else:
res = tools.getOptionFromPiboyconfFile('bt_enable')
if (res <> -1):
if (res == 'yes'):
tools.setBluetooth(True)
elif (res == 'no'):
tools.setBluetooth(False)
else:
tools.setBluetooth(True)
# Overscan
res = tools.getOptionFromPiboyconfFile('overscan_enable')
if (res <> -1):
OVERSCAN_ENABLE = res
else:
exit(-1)
if (OVERSCAN_ENABLE == 'yes'):
tools.addOverscan()
else:
tools.removeOverscan()
# Super Game Boy case
changeEmulatorsCfgSGB(MODE)
# EmulationStation configuration
makeEmulationStationCfg(MODE)
# Retroarch configurations
# Get all systems folders
systems = next(os.walk(tools.CONFIGS_ROOT_FOLDER))
manageSystems(systems, tools.CONFIGS_ROOT_FOLDER)
exit()