-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurator.py
160 lines (123 loc) · 4.08 KB
/
configurator.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
import hid
import configparser
import os
import sys
def resource_path(relative_path):
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
return os.path.join(application_path, relative_path)
config = configparser.ConfigParser()
try:
config.read(resource_path("config.ini"))
except:
with open(resource_path("config.ini"), "w") as f:
f.write("""
[device]
vendor_id = 9025
product_id = 32823
[1]
name = Spotify
process = Spotify.exe
[2]
name = Discord
process = Discord.exe
[3]
name = Game
process = fornite.exe,SuperHexagon.exe
[4]
name = Chrome
process = chrome.exe
""")
print("Created default config file")
config.read(resource_path("config.ini"))
def finddevice():
devices = hid.enumerate()
for n, device in enumerate(devices):
print(
f"{n}) 0x{device['vendor_id']:04x}:0x{device['product_id']:04x} {device['product_string']} {device['serial_number']}")
device = devices[int(input())]
try:
gamepad = hid.device()
try:
gamepad.open(device['vendor_id'],
device['product_id'], device['serial_number'])
device_array = [device['vendor_id'],
device['product_id'], device['serial_number']]
except:
gamepad.open(device['vendor_id'], device['product_id'])
device_array = [device['vendor_id'], device['product_id']]
print("No serial number used")
gamepad.set_nonblocking(True)
except:
print("Failed to open Device")
while True:
report = gamepad.read(64)
if report:
print(report)
print(device_array)
break
if input("Store device? (y): ") == "y":
config['device']['vendor_id'] = str(device_array[0])
config['device']['product_id'] = str(device_array[1])
if len(device_array) == 3:
config['device']['serial_number'] = str(device_array[2])
with open(resource_path("config.ini"), "w") as cf:
config.write(cf)
def listAssignment():
for sec in config:
if sec.isdigit():
if "," in config.get(sec, "process"):
print(sec, config.get(sec, "name"), config.get(sec, "process"))
else:
print(sec, config.get(sec, "name"))
def swapAssignment():
listAssignment()
print()
swap = input("First to swap: "), input("Second to swap: ")
first = dict(config.items(swap[0]))
second = dict(config.items(swap[1]))
config[swap[0]] = second
config[swap[1]] = first
config.write(open(resource_path("config.ini"), "w"))
print()
listAssignment()
print("Saved")
def editAissignment():
listAssignment()
print()
item = input("Choose item to edit: ")
new = {
'name': input("Name: "),
'process': input("Process: ")
}
config[item] = new
config.write(open(resource_path("config.ini"), "w"))
print("\nSaved")
def addGame():
for sec in config:
if sec.isdigit():
if config.get(sec, "name") == "Game":
games = config.get(sec, "process")
games += "," + input("Process of game: ")
config.set(sec, "process", games)
config.write(open(resource_path("config.ini"), "w"))
print("\nSaved")
return
print("No game item found")
def main():
print("1) List assignment\n2) Swap assignment\n3) Edit item\n4) Add game\n5) Find device")
try:
inp = int(input("\nChoose number (0 to exit): "))
except: exit()
if inp == 1: listAssignment()
elif inp == 2: swapAssignment()
elif inp == 3: editAissignment()
elif inp == 4: addGame()
elif inp == 5: finddevice()
else: exit()
print("\n\n")
main()
if __name__ == "__main__":
main()