forked from jehervy/node-virtual-gamepads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_custom_virtual_gamepad.py
More file actions
70 lines (50 loc) · 1.77 KB
/
start_custom_virtual_gamepad.py
File metadata and controls
70 lines (50 loc) · 1.77 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 08:28:59 2019
@author: hyamanieu
"""
import os
import sys
import subprocess
DIR_PATH = os.path.dirname(os.path.abspath(__file__))
DEFAULT_SVG = os.path.join(DIR_PATH,'public','images','controler_extracted.svg')
gamepad_html = os.path.join(DIR_PATH,'public','gamepad.html')
gamepad_empty_html = os.path.join(DIR_PATH,'gamepad_empty.html')
def process_inputs(argv):
assert (len(argv)<3),"0 or 1 argument with the gamepad filename is expected."
if len(argv) == 1:
return DEFAULT_SVG
else:
return argv[1]
def set_up_gamepad(fp=DEFAULT_SVG):
print("Setting up following gamepad: {}".format(fp))
with open(fp) as f:
line = f.readline()
while line:
pos = f.tell()
line = f.readline()
if '<svg' in line:
break
f.seek(pos)
svg = f.read()
one_joystick = '"dirCenter"' in svg
second_joystick = '"dirCenter2"' in svg
if one_joystick:
svg = """<div id="dirContainer">
</div>\n"""+svg
if one_joystick and second_joystick:
svg = """<div id="dirContainer2">
</div>\n"""+svg
elif second_joystick and not one_joystick:
raise Exception(('You cannot have the second joystick alone. You need'
' the first joystick as defined by dirCenter in the svg.'))
with open(gamepad_empty_html) as f:
html = f.read()
new_html = html.replace('{{}}',svg)
with open(gamepad_html,'w') as f:
f.write(new_html)
subprocess.run(['sudo','node','main.js'], cwd=DIR_PATH)
if __name__ == '__main__':
fp = process_inputs(sys.argv)
set_up_gamepad(fp)