-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgtk-razer.pyw
More file actions
258 lines (223 loc) · 7.73 KB
/
gtk-razer.pyw
File metadata and controls
258 lines (223 loc) · 7.73 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
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
#!/usr/bin/env python
# file: gtk-razer.pyw
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2020 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2020-03-26T20:44:32+0100
# Last modified: 2023-03-13T23:30:26+0100
"""Set the LEDs on a Razer keyboard to a static RGB color.
Uses the GTK+ GUI toolkit.
Tested on an Ornata Chroma and a BlackWidow Elite.
The USB control transfer messages were compiled by tracing what the
Openrazer (https://github.com/openrazer/openrazer) driver code does.
"""
from types import SimpleNamespace
from functools import partial
import array
import base64
import os
import sys
import zlib
import usb.core
import gi
gi.require_version("Gtk", "3.0") # noqa
from gi.repository import Gtk, Gdk
__version__ = "2023.03.13"
def create_widgets():
# Namespace to save widgets that we need to reference later.
w = SimpleNamespace()
# Load the UI definition.
builder = Gtk.Builder()
builder.add_from_string(ui)
handlers = {
"on_key": on_key,
"on_red": partial(set_preview, red=255),
"on_green": partial(set_preview, green=255),
"on_blue": partial(set_preview, blue=255),
"on_slider_change": on_slider_change,
"on_draw": on_draw,
"set_color": set_color,
"on_quit": on_quit,
}
builder.connect_signals(handlers)
# Root window
root = builder.get_object("root")
# Type label
tp = builder.get_object("type")
tp.props.label = f"{state.model} (0x1532:0x{state.id:04x})"
# Red
sRed = builder.get_object("sRed")
w.red = sRed
# Green
sGreen = builder.get_object("sGreen")
w.green = sGreen
# Blue
sBlue = builder.get_object("sBlue")
w.blue = sBlue
# Preview
da = builder.get_object("show")
# da.connect('draw', on_draw)
w.show = da
return root, w
def set_preview(w, red=0, green=0, blue=0):
"""Callback for sliders."""
widgets.red.set_value(red)
widgets.green.set_value(green)
widgets.blue.set_value(blue)
widgets.show.queue_draw()
def on_key(w, event):
"""Callback for keypresses."""
key = Gdk.keyval_name(event.keyval)
if key in ["q", "Q"]:
Gtk.main_quit()
def on_draw(da, ctx):
"""Fill the drawingarea with a color."""
w, h = da.get_allocated_width(), da.get_allocated_height()
ctx.rectangle(0, 0, w, h)
ctx.set_source_rgb(
widgets.red.get_value() / 255,
widgets.green.get_value() / 255,
widgets.blue.get_value() / 255,
)
ctx.fill()
return True
def on_slider_change(rng, scroll, value):
"""Change the slider value"""
widgets.show.queue_draw()
def on_quit(arg=None):
R = int(widgets.red.get_value())
G = int(widgets.green.get_value())
B = int(widgets.blue.get_value())
write_rc(state.rcpath, R, G, B)
Gtk.main_quit()
def static_color_msg(red, green, blue):
"""
Create a message to set the Razer Ornata Chroma lights to a static color.
All arguments should be an number in the range 0-255.
Returns an array object containing the message ready to feed into a ctrl_transfer.
"""
msg = array.array("B", b'\x00'*90)
# msg[0] = status = 0
msg[1] = 0x3F # transaction id
# msg[2:4] = remaining packets = 0, 0
# msg[4] = protocol type = 0
msg[5] = 0x09 # data_size
msg[6] = 0x0F # command class
msg[7] = 0x02 # command id
msg[8] = 0x01 # VARSTORE
msg[9] = 0x05 # BACKLIGHT_LED
msg[10] = 0x01 # effect id
# msg[11:13] are 0
msg[13] = 0x01 # unknown
msg[14] = int(red) # color: red
msg[15] = int(green) # color: green
msg[16] = int(blue) # color: blue
chksum = 0
for j in msg[2:-2]:
chksum ^= j
msg[88] = chksum
return msg
def set_color(w):
"""Callback to set the color."""
msg = static_color_msg(
int(widgets.red.get_value()),
int(widgets.green.get_value()),
int(widgets.blue.get_value()),
)
# 0x21: request_type USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT
# 0x09: request HID_REQ_SET_REPORT
# 0x300: value
# 0x01: report index HID_REQ_GET_REPORT
state.dev.ctrl_transfer(0x21, 0x09, 0x300, 0x01, msg)
def create_state():
"""Create the global state."""
state = SimpleNamespace()
state.dev = None
state.rcpath = (
os.environ["HOME"]
+ os.sep
+ '.'
+ os.path.splitext(os.path.basename(sys.argv[0]))[0]
+ "rc"
)
state.model = "No Razer keyboard found!"
state.id = 0
# Find devices
devs = list(usb.core.find(find_all=True, idVendor=0x1532))
if devs:
state.dev = devs[0]
state.model = devs[0].product
state.id = devs[0].idProduct
return state
def read_rc(rcpath):
"""Retrieve color values from the rc file.
Arguments:
rcpath (str): path to the rc file.
Returns:
3-tuple of integers representing R,G,B.
"""
if not os.path.exists(rcpath):
return None
with open(rcpath) as rc:
colors = [int(ln) for ln in rc.readlines() if ln.strip()[0] in "1234567890"]
if len(colors) != 3:
return None
return colors
def write_rc(rcpath, R, G, B):
with open(rcpath, "w") as rc:
rc.write(f"{R}\n")
rc.write(f"{G}\n")
rc.write(f"{B}\n")
# The definition of the user interface was made with Glade.
# It was encapsulated by the py-include script as follows:
# python3 py-include.py -c -t gtk-razer.glade
ui = zlib.decompress(
base64.b85decode(
"c-rlnO>f&c5Qgvl6<l5mf2IL;0oz5|BDw6L=m+*FP~uo(O_N%Zvg3c>k>u1?Oj(v4rw*E1V$Dl"
"&W*&}*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z?yHhDm1&>AYh02~+$#Nb$!<RT|9b5%<=od!3O<+9"
"oBG8Lj@uJ`Zw;UDCQ(-^o^_C8;rKbuS8#ve6bHR7W;iogg_;>oLQIOx6Sp2RGE4F=7)6BI9zh;"
"G?7<WT#meVz%Z3*??kP)G0c&3KE~!ClWaSe$bO!hamfVGIs|L`!aPg!r?!T`??>{LDyMJ@P5M?"
"#QTMsu6M!l_nvH1yWMXp`"
)
).decode("utf-8")
if __name__ == "__main__":
# Detach from the command line on UNIX systems.
if os.name == "posix":
if os.fork():
sys.exit()
# Create the GUI and run it.
state = create_state()
colors = read_rc(state.rcpath)
root, widgets = create_widgets()
if colors:
widgets.red.set_value(colors[0])
widgets.green.set_value(colors[1])
widgets.blue.set_value(colors[2])
root.connect("destroy", Gtk.main_quit)
root.set_modal(True) # Makes a floating window.
if state.dev is None:
msg = Gtk.MessageDialog(
parent=root,
message_type=Gtk.MessageType.ERROR,
title="Warning",
text="No Razer keyboard detected!",
buttons=Gtk.ButtonsType.CLOSE,
)
msg.run()
else:
root.show_all()
Gtk.main()