-
Notifications
You must be signed in to change notification settings - Fork 6
/
driver_frontend.py
307 lines (253 loc) · 13 KB
/
driver_frontend.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
import driver_backend
import gi
import configparser
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gdk
driver_api = driver_backend.Driver_API
class driver_frontend(Gtk.Window, driver_api):
def __init__(self):
super(driver_frontend, self).__init__()
super(driver_api, self).__init__()
self.set_title("FantechX9Thor")
self.set_default_size(500, 600)
########################################################################
# Default Configs
self.profile_dpi_configs = [200, 600, 1000, 1600, 2400, 4000]
self.profile_states = [1, 1, 1, 1, 1, 1]
self.profile_color_configs = ["rgb(255,255,0)", "rgb(0,0,255)", "rgb(255,0,255)", "rgb(0,255,0)", "rgb(255,0,0)", "rgb(0,255,255)"]
self.current_active_profile = 3
self.rgb_color_change_scheme = "Cyclic"
self.current_scheme_timer = 1
self.cyclic_colors = {"Yellow": 1, "Blue": 1, "Violet": 1, "Green": 1, "Red": 1, "Cyan": 1, "White": 1}
########################################################################
self.config_location = "driver.conf"
self.startup()
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(self.vbox)
self.profiles = [Gtk.VBox(spacing=6) for i in range(6)]
self.profile_color_container = [Gdk.RGBA() for i in range(6)]
self.set_default_colors()
self.colors = [Gtk.ColorButton() for i in range(6)]
for i in range(6):
self.colors[i].connect("color-set", self.on_color_changed, i)
self.colors[i].set_rgba(self.profile_color_container[i])
self.dpis = [Gtk.Scale.new_with_range(Gtk.Orientation.VERTICAL, 200, 4800, 200) for i in range(6)]
for i in range(6):
self.dpis[i].set_inverted(True)
self.dpis[i].set_value(self.profile_dpi_configs[i])
self.dpis[i].connect("value-changed", self.on_dpi_changed, i)
for j in self.supported_dpis:
self.dpis[i].add_mark(j, Gtk.PositionType.LEFT)
self.active_profiles = [Gtk.RadioButton() for i in range(6)]
for i in range(6):
self.active_profiles[i].join_group(self.active_profiles[0])
self.active_profiles[i].set_halign(Gtk.Align.CENTER)
self.active_profiles[i].connect("toggled", self.on_active_profile_toggled, i)
self.active_profiles[self.current_active_profile - 1].set_active(True)
self.profile_name = [Gtk.Label() for i in range(6)]
for i in range(6):
label = "Profile " + str(i + 1)
self.profile_name[i].set_label(label)
self.profile_state = [Gtk.CheckButton() for i in range(6)]
for i in range(6):
self.profile_state[i].set_active(True if self.profile_states[i] == 1 else False)
self.profile_state[i].connect("toggled", self.on_state_toggled, i)
self.profile_state[i].set_halign(Gtk.Align.CENTER)
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.vbox.pack_start(self.hbox, True, True, 0)
for i in range(6):
self.hbox.pack_start(self.profiles[i], True, True, 0)
self.profiles[i].pack_start(self.dpis[i], True, True, 10)
self.profiles[i].pack_start(self.profile_state[i], False, True, 0)
self.profiles[i].pack_start(self.profile_name[i], False, True, 0)
self.profiles[i].pack_start(self.active_profiles[i], False, True, 0)
self.profiles[i].pack_start(self.colors[i], False, False, 0)
self.schemes = ["Fixed", "Cyclic", "Static", "Off"]
self.color_schemes = Gtk.ListStore(str)
for scheme in self.schemes:
self.color_schemes.append([scheme])
self.scheme_timers = [1, 2, 3, 4, 5, 6]
self.color_scheme_timers = Gtk.ListStore(int)
for timer in self.scheme_timers:
self.color_scheme_timers.append([timer])
self.hbox_color_scheme = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.vbox.pack_start(self.hbox_color_scheme, False, False, 20)
self.scheme_combo_label = Gtk.Label()
self.scheme_combo_label.set_label("Color Scheme:")
self.hbox_color_scheme.pack_start(self.scheme_combo_label, False, False, 0)
self.scheme_combo = Gtk.ComboBox()
self.scheme_combo.set_model(self.color_schemes)
self.scheme_combo.set_active(self.schemes.index(self.rgb_color_change_scheme))
self.scheme_combo.connect("changed", self.on_scheme_changed)
self.hbox_color_scheme.pack_start(self.scheme_combo, True, True, 0)
self.renderer_text = Gtk.CellRendererText()
self.scheme_combo.pack_start(self.renderer_text, True)
self.scheme_combo.add_attribute(self.renderer_text, "text", 0)
self.scheme_timer_label = Gtk.Label()
self.scheme_timer_label.set_label("Scheme Timer:")
self.hbox_color_scheme.pack_start(self.scheme_timer_label, False, False, 0)
self.scheme_timer_combo = Gtk.ComboBox()
self.scheme_timer_combo.set_model(self.color_scheme_timers)
self.scheme_timer_combo.connect("changed", self.on_current_scheme_timer_changed)
self.scheme_timer_combo.set_active(self.scheme_timers.index(self.current_scheme_timer))
self.hbox_color_scheme.pack_start(self.scheme_timer_combo, True, True, 0)
self.renderer_text = Gtk.CellRendererText()
self.scheme_timer_combo.pack_start(self.renderer_text, True)
self.scheme_timer_combo.add_attribute(self.renderer_text, "text", 0)
self.cyclic_colors_label = Gtk.Label()
self.cyclic_colors_label.set_label("Colors active during cyclic color change:")
self.cyclic_colors_label.set_halign(Gtk.Align.START)
self.vbox.pack_start(self.cyclic_colors_label, False, True, 0)
self.hbox_cyclic_color_state = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.vbox.pack_start(self.hbox_cyclic_color_state, False, True, 0)
self.cyclic_colors_state = [Gtk.CheckButton() for i in range(len(self.cyclic_colors))]
prof = 0
for colorname in self.cyclic_colors.keys():
self.cyclic_colors_state[prof].set_label(colorname)
self.cyclic_colors_state[prof].set_active(True if self.cyclic_colors[colorname] == 1 else False)
self.cyclic_colors_state[prof].connect("toggled", self.on_cyclic_color_changed)
self.hbox_cyclic_color_state.pack_start(self.cyclic_colors_state[prof], True, True, 0)
prof += 1
self.action_bar = Gtk.ActionBar()
self.vbox.pack_end(self.action_bar, False, False, 0)
self.apply_button = Gtk.Button()
self.apply_button.set_label("Apply")
self.apply_button.connect("clicked", self.on_apply_button_clicked)
self.action_bar.pack_end(self.apply_button)
self.reconfigure_button = Gtk.Button()
self.reconfigure_button.set_label("Reconfigure Device")
self.reconfigure_button.connect("clicked", self.reconfigure_button_clicked)
self.action_bar.pack_start(self.reconfigure_button)
self.save_button = Gtk.Button()
self.save_button.set_label("Save Config")
self.save_button.connect("clicked", self.on_save_button_clicked)
self.action_bar.pack_end(self.save_button)
def startup(self):
self.find_device()
state = self.device_state()
alert = Gtk.MessageDialog()
alert.add_button("Close", Gtk.ResponseType.CLOSE)
alert.props.use_markup = True
if state == -1:
alert.set_markup('''Insufficient Permissions! Try adding a udev rule for your mouse, follow the guide <a href="https://wiki.archlinux.org/index.php/udev#Accessing_firmware_programmers_and_USB_virtual_comm_devicesrunning" title="Arch Wiki Guide">here</a>. Running as root will probably work too but not recommended''')
alert.run()
alert.destroy()
elif state == -2:
alert.set_markup("Device not found! Try Replugging.")
alert.run()
alert.destroy()
try:
file = open(self.config_location, "r")
file.close()
self.retrieve_configs()
except FileNotFoundError:
print("Config file not found. Creating...")
self.save_configs()
def save_configs(self):
config = configparser.ConfigParser()
profiles = ["profile_1", "profile_2", "profile_3", "profile_4", "profile_5", "profile_6"]
config["Active_Profile"] = {"Profile": self.current_active_profile}
config["Profile_DPIs"] = dict(zip(profiles, self.profile_dpi_configs))
config["Profile_States"] = dict(zip(profiles, self.profile_states))
config["Profile_Colors"] = dict(zip(profiles, self.profile_color_configs))
config["Color_Scheme"] = {"type": self.rgb_color_change_scheme, "duration": self.current_scheme_timer}
config["Cyclic_Colors"] = self.cyclic_colors
with open(self.config_location, "w") as configfile:
config.write(configfile)
def retrieve_configs(self):
config = configparser.ConfigParser()
config.read(self.config_location)
temp_dict = config["Active_Profile"]
for key, value in temp_dict.items():
self.current_active_profile = int(value)
prof = 0
temp_dict = config["Profile_DPIs"]
for key, value in temp_dict.items():
self.profile_dpi_configs[prof] = int(value)
prof += 1
prof = 0
temp_dict = config["Profile_States"]
for key, value in temp_dict.items():
self.profile_states[prof] = int(value)
prof += 1
prof = 0
temp_dict = config["Profile_Colors"]
for key, value in temp_dict.items():
self.profile_color_configs[prof] = value
prof += 1
temp_dict = config["Color_Scheme"]
self.rgb_color_change_scheme = temp_dict["type"]
self.current_scheme_timer = int(temp_dict["duration"])
temp_dict = dict(config["Cyclic_Colors"])
temp_values = list(temp_dict.values())
temp_keys = list(temp_dict.keys())
for prof in range(len(temp_dict)):
temp_keys[prof] = temp_keys[prof].capitalize()
self.cyclic_colors[temp_keys[prof]] = int(temp_values[prof])
def reconfigure_button_clicked(self, button):
self.startup()
def on_state_toggled(self, button, profile):
state = button.get_active()
if state:
self.profile_states[profile] = 1
else:
self.profile_states[profile] = 0
def on_active_profile_toggled(self, button, profile):
if button.get_active():
self.current_active_profile = profile + 1
def on_current_scheme_timer_changed(self, radio):
timer = radio.get_active()
self.current_scheme_timer = timer + 1
def on_apply_button_clicked(self, button):
self.conquer()
profile = 1
for dpi in self.profile_dpi_configs:
config = self.create_dpi_profile_config(dpi, profile)
self.send_payload(config)
profile += 1
profile = 1
for color in self.profile_color_container:
red = int(color.red * 255)
green = int(color.green * 255)
blue = int(color.blue * 255)
config = self.create_color_profile_config(profile, red, green, blue)
self.send_payload(config)
profile += 1
config = self.create_rgb_lights_config(self.rgb_color_change_scheme, self.current_scheme_timer)
self.send_payload(config)
self.liberate()
def on_dpi_changed(self, scale, profile):
value = int(scale.get_value())
value = self.find_closest_dpi(value)
scale.set_value(value)
self.profile_dpi_configs[profile] = value
def on_color_changed(self, color, profile):
self.profile_color_container[profile] = color.get_rgba()
self.profile_color_configs[profile] = color.get_rgba().to_string()
def on_cyclic_color_changed(self, button):
label = button.get_label()
state = button.get_active()
if state:
self.cyclic_colors[label] = 1
else:
self.cyclic_colors[label] = 0
def on_save_button_clicked(self, button):
self.save_configs()
def set_default_colors(self):
for i in range(6):
self.profile_color_container[i].parse(self.profile_color_configs[i])
def on_scheme_changed(self, combo):
tree_iter = combo.get_active_iter()
model = combo.get_model()
self.rgb_color_change_scheme = model[tree_iter][0]
time_not_req = ["Static", "Off"]
if self.rgb_color_change_scheme in time_not_req:
self.scheme_timer_combo.set_model()
else:
self.scheme_timer_combo.set_model(self.color_scheme_timers)
self.scheme_timer_combo.set_active(self.scheme_timers.index(self.current_scheme_timer))
ui = driver_frontend()
ui.show_all()
ui.connect("destroy", Gtk.main_quit)
Gtk.main()