Debian Codegraph @

daemon/openrazer_daemon/dbus_services/dbus_methods/chroma_keyboard.py

raw
1 # SPDX-License-Identifier: GPL-2.0-or-later
2
3 """
4 BlackWidow Chroma Effects
5 """
6 import os
7 from openrazer_daemon.dbus_services import endpoint
8
9
10 @endpoint('razer.device.lighting.brightness', 'getBrightness', out_sig='d')
11 def get_brightness(self):
12 """
13 Get the device's brightness
14
15 :return: Brightness
16 :rtype: float
17 """
18 self.logger.debug("DBus call get_brightness")
19
20 return self.zone["backlight"]["brightness"]
21
22
23 @endpoint('razer.device.lighting.brightness', 'setBrightness', in_sig='d')
24 def set_brightness(self, brightness):
25 """
26 Set the device's brightness
27
28 :param brightness: Brightness
29 :type brightness: int
30 """
31 self.logger.debug("DBus call set_brightness")
32
33 driver_path = self.get_driver_path('matrix_brightness')
34
35 self.method_args['brightness'] = brightness
36
37 if brightness > 100:
38 brightness = 100
39 elif brightness < 0:
40 brightness = 0
41
42 self.set_persistence("backlight", "brightness", int(brightness))
43
44 brightness = int(round(brightness * (255.0 / 100.0)))
45
46 with open(driver_path, 'w') as driver_file:
47 driver_file.write(str(brightness))
48
49 # Notify others
50 self.send_effect_event('setBrightness', brightness)
51
52
53 @endpoint('razer.device.led.gamemode', 'getGameMode', out_sig='b')
54 def get_game_mode(self):
55 """
56 Get game mode LED state
57
58 :return: Game mode LED state
59 :rtype: bool
60 """
61 self.logger.debug("DBus call get_game_mode")
62
63 driver_path = self.get_driver_path('game_led_state')
64
65 with open(driver_path, 'r') as driver_file:
66 return driver_file.read().strip() == '1'
67
68
69 @endpoint('razer.device.led.gamemode', 'setGameMode', in_sig='b')
70 def set_game_mode(self, enable):
71 """
72 Set game mode LED state
73
74 :param enable: Status of game mode
75 :type enable: bool
76 """
77 self.logger.debug("DBus call set_game_mode")
78
79 driver_path = self.get_driver_path('game_led_state')
80
81 # Checks the usual MOUSE PROTOCOL Interface first
82 # before checking additional interfaces such as
83 # the KEYBOARD PROTOCOL.
84 super_file = self.get_driver_path('key_super')
85 alt_tab = self.get_driver_path('key_alt_tab')
86 alt_f4 = self.get_driver_path('key_alt_f4')
87
88 if os.path.exists(super_file):
89 if enable:
90 open(super_file, 'wb').write(b'\x01')
91 open(alt_tab, 'wb').write(b'\x01')
92 open(alt_f4, 'wb').write(b'\x01')
93 else:
94 open(super_file, 'wb').write(b'\x00')
95 open(alt_tab, 'wb').write(b'\x00')
96 open(alt_f4, 'wb').write(b'\x00')
97 else:
98 for kb_int in self.additional_interfaces:
99 super_file = os.path.join(kb_int, 'key_super')
100 alt_tab = os.path.join(kb_int, 'key_alt_tab')
101 alt_f4 = os.path.join(kb_int, 'key_alt_f4')
102
103 # Some keyboards such as BlackWidow V4 provide additional interfaces
104 # without key_super, key_alt_tab and key_alt_f4 files. We have to go
105 # through all interfaces and check if these files are actually available
106 if os.path.exists(super_file):
107 if enable:
108 open(super_file, 'wb').write(b'\x01')
109 open(alt_tab, 'wb').write(b'\x01')
110 open(alt_f4, 'wb').write(b'\x01')
111 else:
112 open(super_file, 'wb').write(b'\x00')
113 open(alt_tab, 'wb').write(b'\x00')
114 open(alt_f4, 'wb').write(b'\x00')
115
116 with open(driver_path, 'w') as driver_file:
117 if enable:
118 driver_file.write('1')
119 else:
120 driver_file.write('0')
121
122
123 @endpoint('razer.device.led.macromode', 'getMacroMode', out_sig='b')
124 def get_macro_mode(self):
125 """
126 Get macro mode LED state
127
128 :return: Status of macro mode
129 :rtype: bool
130 """
131 self.logger.debug("DBus call get_macro_mode")
132
133 driver_path = self.get_driver_path('macro_led_state')
134
135 with open(driver_path, 'r') as driver_file:
136 return driver_file.read().strip() == '1'
137
138
139 @endpoint('razer.device.led.macromode', 'setMacroMode', in_sig='b')
140 def set_macro_mode(self, enable):
141 """
142 Set macro mode LED state
143
144 :param enable: Status of macro mode
145 :type enable: bool
146 """
147 self.logger.debug("DBus call set_macro_mode")
148
149 driver_path = self.get_driver_path('macro_led_state')
150
151 with open(driver_path, 'w') as driver_file:
152 if enable:
153 driver_file.write('1')
154 else:
155 driver_file.write('0')
156
157
158 @endpoint('razer.device.misc.keyswitchoptimization', 'getKeyswitchOptimization', out_sig='b')
159 def get_keyswitch_optimization(self):
160 """
161 Get Keyswitch optimization state
162
163 :return: Status of keyswitch optimization
164 :rtype: bool
165 """
166 self.logger.debug("DBus call get_keyswitch_optimization")
167
168 driver_path = self.get_driver_path('keyswitch_optimization')
169
170 with open(driver_path, 'r') as driver_file:
171 return driver_file.read().strip() == '1'
172
173
174 @endpoint('razer.device.misc.keyswitchoptimization', 'setKeyswitchOptimization', in_sig='b')
175 def set_keyswitch_optimization(self, enable):
176 """
177 Set Keyswitch optimization state
178
179 :param enable: Status of keyswitch optimization
180 :type enable: bool
181 """
182 self.logger.debug("DBus call set_keyswitch_optimization")
183
184 driver_path = self.get_driver_path('keyswitch_optimization')
185
186 with open(driver_path, 'w') as driver_file:
187 if enable:
188 driver_file.write('1')
189 else:
190 driver_file.write('0')
191
192
193 @endpoint('razer.device.led.macromode', 'getMacroEffect', out_sig='i')
194 def get_macro_effect(self):
195 """
196 Get the effect on the macro LED
197
198 :return: Macro LED effect ID
199 :rtype: int
200 """
201 self.logger.debug("DBus call get_macro_effect")
202
203 driver_path = self.get_driver_path('macro_led_effect')
204
205 with open(driver_path, 'r') as driver_file:
206 return int(driver_file.read().strip())
207
208
209 @endpoint('razer.device.led.macromode', 'setMacroEffect', in_sig='y')
210 def set_macro_effect(self, effect):
211 """
212 Set the effect on the macro LED
213
214 :param effect: Macro LED effect ID
215 :type effect: int
216 """
217 self.logger.debug("DBus call set_macro_effect")
218
219 driver_path = self.get_driver_path('macro_led_effect')
220
221 with open(driver_path, 'w') as driver_file:
222 driver_file.write(str(int(effect)))
223
224
225 @endpoint('razer.device.lighting.chroma', 'setWave', in_sig='i')
226 def set_wave_effect(self, direction):
227 """
228 Set the wave effect on the device
229
230 :param direction: 1 - left to right, 2 right to left
231 :type direction: int
232 """
233 self.logger.debug("DBus call set_wave_effect")
234
235 # Notify others
236 self.send_effect_event('setWave', direction)
237
238 # remember effect
239 self.set_persistence("backlight", "effect", 'wave')
240 self.set_persistence("backlight", "wave_dir", int(direction))
241
242 driver_path = self.get_driver_path('matrix_effect_wave')
243
244 if direction not in self.WAVE_DIRS:
245 direction = self.WAVE_DIRS[0]
246
247 with open(driver_path, 'w') as driver_file:
248 driver_file.write(str(direction))
249
250
251 @endpoint('razer.device.lighting.chroma', 'setWheel', in_sig='i')
252 def set_wheel_effect(self, direction):
253 """
254 Set the wheel effect on the device
255
256 :param direction: 1 - right, 2 - left
257 :type direction: int
258 """
259 self.logger.debug("DBus call set_wheel_effect")
260
261 # Notify others
262 self.send_effect_event('setWheel', direction)
263
264 # Note: wheel direction is saved in wave_dir!
265 # TODO: Add wheel_dir field handling instead!
266 self.set_persistence("backlight", "effect", 'wheel')
267 self.set_persistence("backlight", "wave_dir", int(direction))
268
269 driver_path = self.get_driver_path('matrix_effect_wheel')
270
271 # If this needs to be configurable, add WHEEL_DIRS like WAVE_DIRS
272 if direction not in (1, 2):
273 direction = 1
274
275 with open(driver_path, 'w') as driver_file:
276 driver_file.write(str(direction))
277
278
279 @endpoint('razer.device.lighting.chroma', 'setStatic', in_sig='yyy')
280 def set_static_effect(self, red, green, blue):
281 """
282 Set the device to static colour
283
284 :param red: Red component
285 :type red: int
286
287 :param green: Green component
288 :type green: int
289
290 :param blue: Blue component
291 :type blue: int
292 """
293 self.logger.debug("DBus call set_static_effect")
294
295 # Notify others
296 self.send_effect_event('setStatic', red, green, blue)
297
298 # remember effect
299 self.set_persistence("backlight", "effect", 'static')
300 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
301
302 driver_path = self.get_driver_path('matrix_effect_static')
303
304 payload = bytes([red, green, blue])
305
306 with open(driver_path, 'wb') as driver_file:
307 driver_file.write(payload)
308
309
310 @endpoint('razer.device.lighting.chroma', 'setBlinking', in_sig='yyy')
311 def set_blinking_effect(self, red, green, blue):
312 """
313 Set the device to static colour
314
315 :param red: Red component
316 :type red: int
317
318 :param green: Green component
319 :type green: int
320
321 :param blue: Blue component
322 :type blue: int
323 """
324 self.logger.debug("DBus call set_blinking_effect")
325
326 # Notify others
327 self.send_effect_event('setBlinking', red, green, blue)
328
329 # remember effect
330 self.set_persistence("backlight", "effect", 'blinking')
331 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
332
333 driver_path = self.get_driver_path('matrix_effect_blinking')
334
335 payload = bytes([red, green, blue])
336
337 with open(driver_path, 'wb') as driver_file:
338 driver_file.write(payload)
339
340
341 @endpoint('razer.device.lighting.chroma', 'setSpectrum')
342 def set_spectrum_effect(self):
343 """
344 Set the device to spectrum mode
345 """
346 self.logger.debug("DBus call set_spectrum_effect")
347
348 # Notify others
349 self.send_effect_event('setSpectrum')
350
351 # remember effect
352 self.set_persistence("backlight", "effect", 'spectrum')
353
354 driver_path = self.get_driver_path('matrix_effect_spectrum')
355
356 with open(driver_path, 'w') as driver_file:
357 driver_file.write('1')
358
359
360 @endpoint('razer.device.lighting.chroma', 'setNone')
361 def set_none_effect(self):
362 """
363 Set the device to spectrum mode
364 """
365 self.logger.debug("DBus call set_none_effect")
366
367 # Notify others
368 self.send_effect_event('setNone')
369
370 # remember effect
371 self.set_persistence("backlight", "effect", 'none')
372
373 driver_path = self.get_driver_path('matrix_effect_none')
374
375 with open(driver_path, 'w') as driver_file:
376 driver_file.write('1')
377
378
379 @endpoint('razer.device.misc', 'triggerReactive')
380 def trigger_reactive_effect(self):
381 """
382 Trigger reactive on Firefly
383 """
384 self.logger.debug("DBus call trigger_reactive_effect")
385
386 # Notify others
387 self.send_effect_event('triggerReactive')
388
389 driver_path = self.get_driver_path('matrix_reactive_trigger')
390
391 with open(driver_path, 'w') as driver_file:
392 driver_file.write('1')
393
394
395 @endpoint('razer.device.lighting.chroma', 'setReactive', in_sig='yyyy')
396 def set_reactive_effect(self, red, green, blue, speed):
397 """
398 Set the device to reactive effect
399
400 :param red: Red component
401 :type red: int
402
403 :param green: Green component
404 :type green: int
405
406 :param blue: Blue component
407 :type blue: int
408
409 :param speed: Speed
410 :type speed: int
411 """
412 self.logger.debug("DBus call set_reactive_effect")
413
414 driver_path = self.get_driver_path('matrix_effect_reactive')
415
416 # Notify others
417 self.send_effect_event('setReactive', red, green, blue, speed)
418
419 # remember effect
420 self.set_persistence("backlight", "effect", 'reactive')
421 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
422
423 if speed not in (1, 2, 3, 4):
424 speed = 4
425
426 self.set_persistence("backlight", "speed", int(speed))
427
428 payload = bytes([speed, red, green, blue])
429
430 with open(driver_path, 'wb') as driver_file:
431 driver_file.write(payload)
432
433
434 @endpoint('razer.device.lighting.chroma', 'setBreathRandom')
435 def set_breath_random_effect(self):
436 """
437 Set the device to random colour breathing effect
438 """
439 self.logger.debug("DBus call set_breath_random_effect")
440
441 # Notify others
442 self.send_effect_event('setBreathRandom')
443
444 # remember effect
445 self.set_persistence("backlight", "effect", 'breathRandom')
446
447 driver_path = self.get_driver_path('matrix_effect_breath')
448
449 payload = b'1'
450
451 with open(driver_path, 'wb') as driver_file:
452 driver_file.write(payload)
453
454
455 @endpoint('razer.device.lighting.chroma', 'setBreathSingle', in_sig='yyy')
456 def set_breath_single_effect(self, red, green, blue):
457 """
458 Set the device to single colour breathing effect
459
460 :param red: Red component
461 :type red: int
462
463 :param green: Green component
464 :type green: int
465
466 :param blue: Blue component
467 :type blue: int
468 """
469 self.logger.debug("DBus call set_breath_single_effect")
470
471 # Notify others
472 self.send_effect_event('setBreathSingle', red, green, blue)
473
474 # remember effect
475 self.set_persistence("backlight", "effect", 'breathSingle')
476 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
477
478 driver_path = self.get_driver_path('matrix_effect_breath')
479
480 payload = bytes([red, green, blue])
481
482 with open(driver_path, 'wb') as driver_file:
483 driver_file.write(payload)
484
485
486 @endpoint('razer.device.lighting.chroma', 'setBreathDual', in_sig='yyyyyy')
487 def set_breath_dual_effect(self, red1, green1, blue1, red2, green2, blue2):
488 """
489 Set the device to dual colour breathing effect
490
491 :param red1: Red component
492 :type red1: int
493
494 :param green1: Green component
495 :type green1: int
496
497 :param blue1: Blue component
498 :type blue1: int
499
500 :param red2: Red component
501 :type red2: int
502
503 :param green2: Green component
504 :type green2: int
505
506 :param blue2: Blue component
507 :type blue2: int
508 """
509 self.logger.debug("DBus call set_breath_dual_effect")
510
511 # Notify others
512 self.send_effect_event('setBreathDual', red1, green1, blue1, red2, green2, blue2)
513
514 # remember effect
515 self.set_persistence("backlight", "effect", 'breathDual')
516 self.zone["backlight"]["colors"][0:6] = int(red1), int(green1), int(blue1), int(red2), int(green2), int(blue2)
517
518 driver_path = self.get_driver_path('matrix_effect_breath')
519
520 payload = bytes([red1, green1, blue1, red2, green2, blue2])
521
522 with open(driver_path, 'wb') as driver_file:
523 driver_file.write(payload)
524
525
526 @endpoint('razer.device.lighting.chroma', 'setBreathTriple', in_sig='yyyyyyyyy')
527 def set_breath_triple_effect(self, red1, green1, blue1, red2, green2, blue2, red3, green3, blue3):
528 """
529 Set the device to triple colour breathing effect
530
531 :param red1: Red component
532 :type red1: int
533
534 :param green1: Green component
535 :type green1: int
536
537 :param blue1: Blue component
538 :type blue1: int
539
540 :param red2: Red component
541 :type red2: int
542
543 :param green2: Green component
544 :type green2: int
545
546 :param blue2: Blue component
547 :type blue2: int
548
549 :param red3: Red component
550 :type red3: int
551
552 :param green3: Green component
553 :type green3: int
554
555 :param blue3: Blue component
556 :type blue3: int
557 """
558 self.logger.debug("DBus call set_breath_triple_effect")
559
560 # Notify others
561 self.send_effect_event('setBreathTriple', red1, green1, blue1, red2, green2, blue2, red3, green3, blue3)
562
563 # remember effect
564 self.set_persistence("backlight", "effect", 'breathTriple')
565 self.zone["backlight"]["colors"][0:9] = int(red1), int(green1), int(blue1), int(red2), int(green2), int(blue2), int(red3), int(green3), int(blue3)
566
567 driver_path = self.get_driver_path('matrix_effect_breath')
568
569 payload = bytes([red1, green1, blue1, red2, green2, blue2, red3, green3, blue3])
570
571 with open(driver_path, 'wb') as driver_file:
572 driver_file.write(payload)
573
574
575 @endpoint('razer.device.lighting.chroma', 'setCustom')
576 def set_custom_effect(self):
577 """
578 Set the device to use custom LED matrix
579 """
580 self.send_effect_event('setCustom')
581
582 self._set_custom_effect()
583
584
585 @endpoint('razer.device.lighting.chroma', 'setKeyRow', in_sig='ay', byte_arrays=True)
586 def set_key_row(self, payload):
587 """
588 Set the RGB matrix on the device
589
590 Byte array like
591 [1, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00,
592 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 255, 00, 255, 00, 00]
593
594 First byte is row, on firefly its always 1, on keyboard its 0-5
595 Then its 3byte groups of RGB
596 :param payload: Binary payload
597 :type payload: bytes
598 """
599 self.send_effect_event('setCustom')
600
601 self._set_key_row(payload)
602
603
604 @endpoint('razer.device.lighting.custom', 'setRipple', in_sig='yyyd')
605 def set_ripple_effect(self, red, green, blue, refresh_rate):
606 """
607 Set the daemon to serve a ripple effect of the specified colour
608
609 :param red: Red component
610 :type red: int
611
612 :param green: Green component
613 :type green: int
614
615 :param blue: Blue component
616 :type blue: int
617
618 :param refresh_rate: Refresh rate
619 :type refresh_rate: int
620 """
621 self.logger.debug("DBus call set_ripple_effect")
622
623 # Notify others
624 self.send_effect_event('setRipple', red, green, blue, refresh_rate)
625
626 # remember effect
627 self.set_persistence("backlight", "effect", 'ripple')
628 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
629
630
631 @endpoint('razer.device.lighting.custom', 'setRippleRandomColour', in_sig='d')
632 def set_ripple_effect_random_colour(self, refresh_rate):
633 """
634 Set the daemon to serve a ripple effect of random colours
635
636 :param refresh_rate: Refresh rate
637 :type refresh_rate: int
638 """
639 self.logger.debug("DBus call set_ripple_effect")
640
641 # Notify others
642 self.send_effect_event('setRipple', None, None, None, refresh_rate)
643
644 # remember effect
645 self.set_persistence("backlight", "effect", 'rippleRandomColour')
646
647
648 @endpoint('razer.device.lighting.chroma', 'setStarlightRandom', in_sig='y')
649 def set_starlight_random_effect(self, speed):
650 """
651 Set startlight random mode
652 """
653 self.logger.debug("DBus call set_starlight_random")
654
655 driver_path = self.get_driver_path('matrix_effect_starlight')
656
657 with open(driver_path, 'wb') as driver_file:
658 driver_file.write(bytes([speed]))
659
660 # Notify others
661 self.send_effect_event('setStarlightRandom')
662
663 # remember effect
664 self.set_persistence("backlight", "effect", 'starlightRandom')
665 self.set_persistence("backlight", "speed", int(speed))
666
667
668 @endpoint('razer.device.lighting.chroma', 'setStarlightSingle', in_sig='yyyy')
669 def set_starlight_single_effect(self, red, green, blue, speed):
670 """
671 Set starlight mode
672 """
673 self.logger.debug("DBus call set_starlight_single")
674
675 driver_path = self.get_driver_path('matrix_effect_starlight')
676
677 with open(driver_path, 'wb') as driver_file:
678 driver_file.write(bytes([speed, red, green, blue]))
679
680 # Notify others
681 self.send_effect_event('setStarlightSingle', red, green, blue, speed)
682
683 # remember effect
684 self.set_persistence("backlight", "effect", 'starlightSingle')
685 self.set_persistence("backlight", "speed", int(speed))
686 self.zone["backlight"]["colors"][0:3] = int(red), int(green), int(blue)
687
688
689 @endpoint('razer.device.lighting.chroma', 'setStarlightDual', in_sig='yyyyyyy')
690 def set_starlight_dual_effect(self, red1, green1, blue1, red2, green2, blue2, speed):
691 """
692 Set starlight dual mode
693 """
694 self.logger.debug("DBus call set_starlight_dual")
695
696 driver_path = self.get_driver_path('matrix_effect_starlight')
697
698 with open(driver_path, 'wb') as driver_file:
699 driver_file.write(bytes([speed, red1, green1, blue1, red2, green2, blue2]))
700
701 # Notify others
702 self.send_effect_event('setStarlightDual', red1, green1, blue1, red2, green2, blue2, speed)
703
704 # remember effect
705 self.set_persistence("backlight", "effect", 'starlightDual')
706 self.set_persistence("backlight", "speed", int(speed))
707 self.zone["backlight"]["colors"][0:6] = int(red1), int(green1), int(blue1), int(red2), int(green2), int(blue2)