forked from polychromatic/polychromatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolychromatic-tray-applet
executable file
·1128 lines (940 loc) · 46.6 KB
/
polychromatic-tray-applet
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
# Polychromatic is free software: you can redistribute it and/or modify
# it under the temms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Polychromatic is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Polychromatic. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015-2016 Terry Cain <[email protected]>
# 2015-2017 Luke Horwell <[email protected]>
#
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, Gdk, AppIndicator3 as appindicator
import collections
import os
import sys
import signal
import gettext
import setproctitle
from subprocess import Popen as background_process
from subprocess import call as foreground_process
from shutil import which
try:
import razer.client as rclient
from razer_daemon.keyboard import KeyboardColour
except Exception as e:
print("Failed to import modules for daemon.")
print("Exception: " + str(e))
exit(1)
try:
# Relative copy
import pylib.preferences as pref
import pylib.profiles as prof
import pylib.common as common
except ImportError:
# Installed to system
import polychromatic.preferences as pref
import polychromatic.profiles as prof
import polychromatic.common as common
except Exception as e:
print("One or more of Polychromatic's modules could not be imported!")
print("Try re-installing the application.")
print("\nException:" + str(e))
exit(1)
path = pref.Paths()
path.data_source = path.get_data_source(__file__)
session_storage = os.path.join("/run/user/", str(os.getuid()), "polychromatic-tray-applet")
class Translations():
def __init__(self):
"""
Determine the paths where the locales are stored.
"""
whereami = os.path.abspath(os.path.join(os.path.dirname(__file__)))
if os.path.exists(os.path.join(whereami, 'locale/')):
locale_path = os.path.join(whereami, 'locale/')
stdout.vprint('Using relative path for translations.')
else:
locale_path = '/usr/share/locale/'
stdout.vprint('Using "/usr/share/locale/" or falling back to "en_US" for translations.')
# Initialise i18n translations
global _
t = gettext.translation('polychromatic-tray-applet', localedir=locale_path, fallback=True)
_ = t.gettext
# Functions for populating the indicator menus.
def create_menu_item(label, enabled, function=None, function_params=None, icon_path=None):
"""
Returns a Gtk menu item for use in menus.
label str Text to display to the user.
enabled bool Whether the selection should be highlighted or not.
function obj Callback when button is clicked.
function_params obj Functions to pass the callback function.
icon_path str Path to image file.
"""
if icon_path and os.path.exists(icon_path):
item = Gtk.ImageMenuItem(Gtk.STOCK_NEW, label=label)
item.set_sensitive(enabled)
item.show()
img = Gtk.Image()
img.set_from_file(icon_path)
item.set_image(img)
else:
item = Gtk.MenuItem(label)
item.set_sensitive(enabled)
item.show()
if function and not function_params:
item.connect("activate", function)
elif function and function_params:
item.connect("activate", function, function_params)
return(item)
def create_submenu(label, enabled):
"""
Returns a Gtk menu item for sub-menu options.
label str Text to display to the user.
enabled bool Whether the selection should be highlighted or not.
Returns objects:
item MenuItem (for parent menu)
menu Menu (containing child menu items)
"""
item = Gtk.MenuItem(label)
item.set_sensitive(enabled)
item.show()
menu = Gtk.Menu()
menu.show()
item.set_submenu(menu)
return[item, menu]
def create_seperator():
"""
Returns a Gtk seperator object.
"""
sep = Gtk.SeparatorMenuItem()
sep.show()
return(sep)
class AppIndicator(object):
"""
Indicator applet that provides quick access configuration
options from the system tray.
"""
def __init__(self):
self.devman = None
self.active_device = None
self.active_serial = None
self.indicator = None
self.profiles = None
self.menu_root = None
self.menu_devices = None
self.menu_effects = None
self.menu_brightness = None
self.menu_app_profiles = None
self.menu_colours = None
self.menu_dpi = None
self.menu_poll_rate = None
self.menu_misc = None
self.current_device = _("Unknown")
self.current_effect = _("Unknown")
self.current_brightness = _("Unknown")
self.current_app_profile = None
self.current_colour = _("Green")
self.current_colour_rgb = [0, 255, 0]
self.current_dpi = _("Unknown")
self.current_poll_rate = _("Unknown")
self.current_gamemode = _("Unknown")
self.indicator = appindicator.Indicator.new("polychromatic-tray-applet", self._get_tray_icon(), appindicator.IndicatorCategory.APPLICATION_STATUS)
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
def setup(self):
# Set active device
if not which("razer-service"):
self._setup_failed(_("Daemon Not Installed."), _("Unknown command: razer-service"))
try:
self.devman = rclient.DeviceManager()
except Exception as e:
self._setup_failed(_("Daemon isn't running or crashed."), str(e))
return False
if len(self.devman.devices) == 0:
self._setup_failed(_("No devices found."))
return False
else:
self.active_device = self.devman.devices[0]
self.active_serial = self.active_device.serial
self.current_device = self.active_device.name
# Ensure correct tray icon is loaded
self.indicator.set_icon(self._get_tray_icon())
# Populate root menu and submenus.
self.rebuild_all_submenus()
self.rebuild_root_menu()
# Start watching devicestate index for changes.
common.devicestate_monitor_start(self.device_state_changed, path.devicestate)
def rebuild_all_submenus(self):
"""
Rebuilds the submenus based on the currently active device.
If a device does not require this menu, this returns None.
"""
self.menu_devices = self._build_device_menu()
self.menu_effects = self._build_effect_menu()
self.menu_brightness = self._build_brightness_menu()
self.menu_app_profiles = self._build_app_profile_menu()
self.menu_dpi = self._build_dpi_menu()
self.menu_poll_rate = self._build_poll_rate_menu()
self.menu_gamemode = self._build_gamemode_menu()
self.menu_colours = self._build_colour_menu()
def rebuild_root_menu(self):
"""
Rebuilds the parent "tray" menu.
"""
stdout.vprint("Updating menu layout...")
root = Gtk.Menu()
root.append(create_menu_item(self.current_device, False))
root.append(self.menu_devices)
root.append(create_seperator())
multiple_sources = common.has_multiple_sources(self.active_device)
if self.menu_effects:
root.append(self.menu_effects)
if self.active_device.has("lighting"):
state = common.get_effect_state_string(self._get_device_state("main", "effect"))
if multiple_sources:
root.append(create_menu_item(_("Main") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_backlight"):
state = common.get_effect_state_string(self._get_device_state("backlight", "effect"))
if multiple_sources:
root.append(create_menu_item(_("Backlight") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_logo"):
state = common.get_effect_state_string(self._get_device_state("logo", "effect"))
if multiple_sources:
root.append(create_menu_item(_("Logo") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_scroll"):
state = common.get_effect_state_string(self._get_device_state("scroll", "effect"))
if multiple_sources:
root.append(create_menu_item(_("Scroll Wheel") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
root.append(create_seperator())
if self.menu_brightness:
root.append(self.menu_brightness)
if self.active_device.has("brightness"):
try:
state = str(int(self.active_device.brightness)) + "%"
except Exception:
state = "---%"
if multiple_sources:
root.append(create_menu_item(_("Main") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_backlight_brightness"):
try:
state = str(int(self.active_device.fx.misc.backlight.brightness)) + "%"
except Exception:
state = "---%"
if multiple_sources:
root.append(create_menu_item(_("Backlight") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_logo_brightness"):
try:
state = str(int(self.active_device.fx.misc.logo.brightness)) + "%"
except Exception:
state = "---%"
if multiple_sources:
root.append(create_menu_item(_("Logo") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_scroll_brightness"):
try:
state = str(int(self.active_device.fx.misc.scroll_wheel.brightness)) + "%"
except Exception:
state = "---%"
if multiple_sources:
root.append(create_menu_item(_("Scroll Wheel") + ": " + state, False))
else:
root.append(create_menu_item(state, False))
if self.active_device.has("lighting_backlight_active"):
if self.active_device.fx.misc.logo.active == 1:
root.append(create_menu_item(_("Backlight") + ": " + _("On"), False))
else:
root.append(create_menu_item(_("Backlight") + ": " + _("Off"), False))
if self.active_device.has("lighting_logo_active"):
if self.active_device.fx.misc.logo.active == 1:
root.append(create_menu_item(_("Logo") + ": " + _("On"), False))
else:
root.append(create_menu_item(_("Logo") + ": " + _("Off"), False))
if self.active_device.has("lighting_scroll_active"):
if self.active_device.fx.misc.scroll_wheel.active == 1:
root.append(create_menu_item(_("Scroll Wheel") + ": " + _("On"), False))
else:
root.append(create_menu_item(_("Scroll Wheel") + ": " + _("Off"), False))
root.append(create_seperator())
if self.menu_app_profiles:
root.append(self.menu_app_profiles)
if self.current_app_profile:
root.append(create_menu_item(self.current_app_profile, False))
root.append(create_seperator())
if self.menu_dpi:
root.append(self.menu_dpi)
root.append(create_menu_item(self.current_dpi, False))
root.append(create_seperator())
if self.menu_poll_rate:
root.append(self.menu_poll_rate)
root.append(create_menu_item(self.current_poll_rate, False))
root.append(create_seperator())
if self.menu_gamemode:
root.append(self.menu_gamemode)
root.append(create_menu_item(self.current_gamemode, False))
root.append(create_seperator())
if self.menu_colours:
root.append(self.menu_colours)
root.append(create_menu_item(self.current_colour, True, cb.set_colour_custom, None, self._get_colour_icon(self.current_colour_rgb)))
root.append(create_seperator())
root.append(create_menu_item(_("Open Controller"), True, cb.launch_controller, None, self._get_icon("ui", "controller.svg")))
adv_menu = create_submenu(_("Advanced"), True)
adv_menu[1].append(create_menu_item("Restart Daemon", True, cb.restart_daemon))
root.append(adv_menu[0])
root.append(create_seperator())
root.append(create_menu_item(_("Quit"), True, cb.quit))
self.indicator.set_title("Polychromatic")
self.indicator.set_menu(root)
# Data fetching
def _get_device_state(self, source, state):
value = pref.get_device_state(self.active_serial, source, state)
if value == None:
value = _("Unknown")
return(value)
def _get_tray_icon(self):
"""
Returns path or filename for tray icon.
Icon Sources
"tray_icon": {"type": "?"}
builtin = One provided by Polychromatic. "humanity-light"
custom = One specified by user. "/path/to/file"
gtk = Use icon by GTK name. "keyboard"
"""
# If it's the first time loading, set default icon to desktop environment.
if not pref.exists("tray_icon", "type"):
common.set_default_tray_icon(pref)
icon_type = pref.get("tray_icon", "type", "builtin")
icon_value = pref.get("tray_icon", "value", "0")
icon_fallback = os.path.join(path.data_source, "tray", "humanity-dark.svg")
if icon_type == "builtin":
# icon_value = UUID
icon_index = pref.load_file(os.path.join(path.data_source, "tray/icons.json"))
return(os.path.join(path.data_source, "tray", icon_index[icon_value]["path"]))
elif icon_type == "custom":
# icon_value = Path to icon
if os.path.exists(icon_value):
return(icon_value)
else:
stdout.error("Icon missing: " + icon_value)
stdout.error("Using fallback!")
return(icon_fallback)
elif icon_type == "gtk":
# icon_value = Icon name used by GTK
return(icon_value)
else:
return(icon_fallback)
def _get_icon(self, img_dir, icon):
"""
Returns the path for a Polychromatic icon.
img_dir = Folder inside "data/img", e.g. "effects"
icon = Filename, including extension.
"""
return os.path.join(path.data_source, "img", img_dir, icon)
def _setup_failed(self, error_reason, exception=None):
"""
A simple menu is displayed when something goes wrong.
"""
self.indicator.set_icon(self._get_icon("../tray", "error.svg"))
root = Gtk.Menu()
root.append(create_menu_item(error_reason, False))
root.append(create_seperator())
root.append(create_menu_item(_("Retry"), True, cb.retry_applet))
root.append(create_menu_item(_("Restart Daemon"), True, cb.restart_daemon))
root.append(create_seperator())
root.append(create_menu_item(_("Open Controller"), True, cb.launch_controller, None, self._get_icon("ui", "controller.svg")))
root.append(create_menu_item(_("Quit"), True, cb.quit))
self.indicator.set_menu(root)
stdout.error("ERROR: " + error_reason)
if exception:
stdout.error("Exception: " + exception)
# Builds sub-menus. Returns None if not available for current device.
def _build_device_menu(self):
stdout.vprint("Building device menu...")
submenu = create_submenu(_("Change Device"), True)
for device in self.devman.devices:
name = device.name
serial = device.serial
formfactor = common.get_device_type(device.type)
icon_path = self._get_icon("states", formfactor + ".svg")
if not os.path.exists(icon_path):
icon_path = self._get_icon("states", "unknown.svg")
submenu[1].append(create_menu_item(name, True, cb.set_device, serial, icon_path))
stdout.vprint(" -- Found: " + name)
submenu[1].append(create_seperator())
submenu[1].append(create_menu_item(_("Refresh Device List"), True, cb.reload_devices))
return(submenu[0])
def _build_effect_menu(self):
stdout.vprint("Building effect menu...")
root_submenu = create_submenu("Effects", True)
if self.active_device.has("lighting"):
main_submenu = create_submenu("Main", True)
else:
main_submenu = None
if self.active_device.has("lighting_backlight"):
backlight_submenu = create_submenu("Backlight", True)
else:
backlight_submenu = None
if self.active_device.has("lighting_logo"):
logo_submenu = create_submenu("Logo", True)
else:
logo_submenu = None
if self.active_device.has("lighting_scroll"):
scroll_submenu = create_submenu("Scroll Wheel", True)
else:
scroll_submenu = None
# Disable menu if device has no light sources to set
if not self.active_device.has("lighting") and \
not self.active_device.has("lighting_logo") and \
not self.active_device.has("lighting_scroll") and \
not self.active_device.has("lighting_backlight"):
return None
# Have multiple sub-menus if there are multiple light sources
multiple_menus = common.has_multiple_sources(self.active_device)
fx_list = [
# [submenu_obj, has(), callback [source, effect, effect_params], icon, label]
[main_submenu, "lighting_none", ["main", "none"], "none", _("None")],
[main_submenu, "lighting_spectrum", ["main", "spectrum"], "spectrum", _("Spectrum")],
[main_submenu, "lighting_wave", ["main", "wave"], "wave", _("Wave")],
[main_submenu, "lighting_reactive", ["main", "reactive", 1], "reactive", _("Reactive (Fast)")],
[main_submenu, "lighting_reactive", ["main", "reactive", 2], "reactive", _("Reactive (Medium)")],
[main_submenu, "lighting_reactive", ["main", "reactive", 3], "reactive", _("Reactive (Slow)")],
[main_submenu, "lighting_reactive", ["main", "reactive", 4], "reactive", _("Reactive (Very Slow)")],
[main_submenu, "lighting_breath_random", ["main", "breath", "random"], "breath", _("Breath (Random)")],
[main_submenu, "lighting_breath_single", ["main", "breath", "single"], "breath", _("Breath (Single)")],
[main_submenu, "lighting_breath_dual", ["main", "breath", "dual"], "breath", _("Breath (Dual)")],
[main_submenu, "lighting_pulsate", ["main", "pulsate"], "pulsate", _("Pulsate")],
[main_submenu, "lighting_ripple_random", ["main", "ripple", "random"], "ripple", _("Ripple (Random)")],
[main_submenu, "lighting_ripple", ["main", "ripple", "single"], "ripple", _("Ripple (Single)")],
[main_submenu, "lighting_starlight", ["main", "starlight"], "starlight", _("Starlight")],
[main_submenu, "lighting_static", ["main", "static"], "static", _("Static")],
# Logo Lighting
[logo_submenu, "lighting_logo_none", ["logo", "none"], "none", _("None")],
[logo_submenu, "lighting_logo_spectrum", ["logo", "spectrum"], "spectrum", _("Spectrum")],
[logo_submenu, "lighting_logo_blinking", ["logo", "blinking"], "blinking", _("Blinking")],
[logo_submenu, "lighting_logo_breath_random", ["logo", "breath", "random"], "breath", _("Breath (Random)")],
[logo_submenu, "lighting_logo_breath_single", ["logo", "breath", "single"], "breath", _("Breath (Single)")],
[logo_submenu, "lighting_logo_breath_dual", ["logo", "breath", "dual"], "breath", _("Breath (Dual)")],
[logo_submenu, "lighting_logo_pulsate", ["logo", "pulsate"], "pulsate", _("Pulsate")],
[logo_submenu, "lighting_logo_reactive", ["logo", "reactive", "fast"], "reactive", _("Reactive (Fast)")],
[logo_submenu, "lighting_logo_reactive", ["logo", "reactive", "med"], "reactive", _("Reactive (Medium)")],
[logo_submenu, "lighting_logo_reactive", ["logo", "reactive", "slow"], "reactive", _("Reactive (Slow)")],
[logo_submenu, "lighting_logo_reactive", ["logo", "reactive", "vslow"], "reactive", _("Reactive (Very Slow)")],
[logo_submenu, "lighting_logo_static", ["logo", "static"], "static", _("Static")],
# Scroll Lighting
[scroll_submenu, "lighting_scroll_none", ["scroll", "none"], "none", _("None")],
[scroll_submenu, "lighting_scroll_spectrum", ["scroll", "spectrum"], "spectrum", _("Spectrum")],
[scroll_submenu, "lighting_scroll_blinking", ["scroll", "blinking"], "blinking", _("Blinking")],
[scroll_submenu, "lighting_scroll_breath_random", ["scroll", "breath", "random"], "breath", _("Breath (Random)")],
[scroll_submenu, "lighting_scroll_breath_single", ["scroll", "breath", "single"], "breath", _("Breath (Single)")],
[scroll_submenu, "lighting_scroll_breath_dual", ["scroll", "breath", "dual"], "breath", _("Breath (Dual)")],
[scroll_submenu, "lighting_scroll_pulsate", ["scroll", "pulsate"], "pulsate", _("Pulsate")],
[scroll_submenu, "lighting_scroll_reactive", ["scroll", "reactive", "fast"], "reactive", _("Reactive (Fast)")],
[scroll_submenu, "lighting_scroll_reactive", ["scroll", "reactive", "med"], "reactive", _("Reactive (Medium)")],
[scroll_submenu, "lighting_scroll_reactive", ["scroll", "reactive", "slow"], "reactive", _("Reactive (Slow)")],
[scroll_submenu, "lighting_scroll_reactive", ["scroll", "reactive", "vslow"], "reactive", _("Reactive (Very Slow)")],
[scroll_submenu, "lighting_scroll_static", ["scroll", "static"], "static", _("Static")]
]
has_items = {
"main": False,
"logo": False,
"scroll": False
}
for fx in fx_list:
target_submenu = fx[0]
capability = fx[1]
callback = fx[2]
icon = fx[3]
label = fx[4]
if self.active_device.has(capability):
icon_path = self._get_icon("effects", icon + ".svg")
if multiple_menus:
target_submenu[1].append(create_menu_item(label, True, cb.set_effect, callback, icon_path))
has_items[callback[0]] = True
else:
root_submenu[1].append(create_menu_item(label, True, cb.set_effect, callback, icon_path))
if multiple_menus and not has_items["main"] and not has_items["logo"] and not has_items["scroll"]:
return None
if multiple_menus and main_submenu and has_items["main"]:
root_submenu[1].append(main_submenu[0])
if multiple_menus and backlight_submenu:
root_submenu[1].append(backlight_submenu[0])
if multiple_menus and logo_submenu and has_items["logo"]:
root_submenu[1].append(logo_submenu[0])
if multiple_menus and scroll_submenu and has_items["scroll"]:
root_submenu[1].append(scroll_submenu[0])
return(root_submenu[0])
def _build_brightness_menu(self):
stdout.vprint("Building brightness menu...")
root_submenu = create_submenu("Brightness", True)
if self.active_device.has("brightness"):
main_submenu = create_submenu("Main", True)
else:
main_submenu = None
if self.active_device.has("lighting_backlight_brightness") or self.active_device.has("lighting_backlight_active"):
backlight_submenu = create_submenu("Backlight", True)
else:
backlight_submenu = None
if self.active_device.has("lighting_logo_brightness") or self.active_device.has("lighting_logo_active"):
logo_submenu = create_submenu("Logo", True)
else:
logo_submenu = None
if self.active_device.has("lighting_scroll_brightness") or self.active_device.has("lighting_scroll_active"):
scroll_submenu = create_submenu("Scroll Wheel", True)
else:
scroll_submenu = None
# Disable menu if device has no light sources to set
if not self.active_device.has("brightness") and \
not self.active_device.has("lighting_backlight_brightness") and \
not self.active_device.has("lighting_backlight_active") and \
not self.active_device.has("lighting_logo_brightness") and \
not self.active_device.has("lighting_logo_active") and \
not self.active_device.has("lighting_scroll_brightness") and \
not self.active_device.has("lighting_scroll_active"):
return None
# Have multiple sub-menus if there are multiple light sources
multiple_menus = common.has_multiple_sources(self.active_device)
if self.active_device.has("brightness"):
if multiple_menus:
target_submenu = main_submenu
else:
target_submenu = root_submenu
target_submenu[1].append(create_menu_item(_("Full (100%)"), True, cb.set_brightness, ["main", 100]))
target_submenu[1].append(create_menu_item(_("Bright (75%)"), True, cb.set_brightness, ["main", 75]))
target_submenu[1].append(create_menu_item(_("Medium (50%)"), True, cb.set_brightness, ["main", 50]))
target_submenu[1].append(create_menu_item(_("Dim (25%)"), True, cb.set_brightness, ["main", 25]))
target_submenu[1].append(create_menu_item(_("Off (0%)"), True, cb.set_brightness, ["main", 0]))
if self.active_device.has("lighting_backlight_backlight"):
if multiple_menus:
target_submenu = backlight_submenu
else:
target_submenu = root_submenu
# No known devices use this yet (April 2017)
target_submenu[1].append(create_menu_item(_("Full (100%)"), True, cb.set_brightness, ["backlight", 100]))
target_submenu[1].append(create_menu_item(_("Bright (75%)"), True, cb.set_brightness, ["backlight", 75]))
target_submenu[1].append(create_menu_item(_("Medium (50%)"), True, cb.set_brightness, ["backlight", 50]))
target_submenu[1].append(create_menu_item(_("Dim (25%)"), True, cb.set_brightness, ["backlight", 25]))
target_submenu[1].append(create_menu_item(_("Off (0%)"), True, cb.set_brightness, ["backlight", 0]))
if self.active_device.has("lighting_logo_brightness"):
if multiple_menus:
target_submenu = logo_submenu
else:
target_submenu = root_submenu
target_submenu[1].append(create_menu_item(_("Full (100%)"), True, cb.set_brightness, ["logo", 100]))
target_submenu[1].append(create_menu_item(_("Bright (75%)"), True, cb.set_brightness, ["logo", 75]))
target_submenu[1].append(create_menu_item(_("Medium (50%)"), True, cb.set_brightness, ["logo", 50]))
target_submenu[1].append(create_menu_item(_("Dim (25%)"), True, cb.set_brightness, ["logo", 25]))
target_submenu[1].append(create_menu_item(_("Off (0%)"), True, cb.set_brightness, ["logo", 0]))
if self.active_device.has("lighting_scroll_brightness"):
if multiple_menus:
target_submenu = scroll_submenu
else:
target_submenu = root_submenu
target_submenu[1].append(create_menu_item(_("Full (100%)"), True, cb.set_brightness, ["scroll", 100]))
target_submenu[1].append(create_menu_item(_("Bright (75%)"), True, cb.set_brightness, ["scroll", 75]))
target_submenu[1].append(create_menu_item(_("Medium (50%)"), True, cb.set_brightness, ["scroll", 50]))
target_submenu[1].append(create_menu_item(_("Dim (25%)"), True, cb.set_brightness, ["scroll", 25]))
target_submenu[1].append(create_menu_item(_("Off (0%)"), True, cb.set_brightness, ["scroll", 0]))
# Only show on/off options if the device only supports that.
if self.active_device.has("lighting_backlight_active"):
backlight_submenu[1].append(create_menu_item(_("On"), True, cb.set_brightness_toggle, ["backlight", 1]))
backlight_submenu[1].append(create_menu_item(_("Off"), True, cb.set_brightness_toggle, ["backlight", 0]))
multiple_menus = True
if self.active_device.has("lighting_logo_active"):
logo_submenu[1].append(create_menu_item(_("On"), True, cb.set_brightness_toggle, ["logo", 1]))
logo_submenu[1].append(create_menu_item(_("Off"), True, cb.set_brightness_toggle, ["logo", 0]))
multiple_menus = True
if self.active_device.has("lighting_scroll_active"):
scroll_submenu[1].append(create_menu_item(_("On"), True, cb.set_brightness_toggle, ["scroll", 1]))
scroll_submenu[1].append(create_menu_item(_("Off"), True, cb.set_brightness_toggle, ["scroll", 0]))
multiple_menus = True
if multiple_menus and main_submenu:
root_submenu[1].append(main_submenu[0])
if multiple_menus and backlight_submenu:
root_submenu[1].append(backlight_submenu[0])
if multiple_menus and logo_submenu:
root_submenu[1].append(logo_submenu[0])
if multiple_menus and scroll_submenu:
root_submenu[1].append(scroll_submenu[0])
return root_submenu[0]
def _build_app_profile_menu(self):
stdout.vprint("Building app profiles menu...")
if not self.active_device.has("lighting_led_matrix") or not str(self.active_device.type) == "keyboard":
return None
self.profiles = prof.AppProfiles()
uuids = self.profiles.list_profiles()
submenu = create_submenu(_("Application Profiles"), True)
if len(uuids) > 0:
for uuid in uuids:
try:
index = pref.load_file(os.path.join(path.profile_folder, uuid + ".json"))
name = index["name"]
icon = index["icon"]
submenu[1].append(create_menu_item(name, True, cb.set_profile, uuid, icon))
except Exception as e:
print("Skipping corrupt profile UUID: " + uuid)
print("Exception: " + str(e))
else:
submenu[1].append(create_menu_item(_("No profiles to display."), False))
return(submenu[0])
def _build_colour_menu(self):
stdout.vprint("Building colours menu...")
submenu = create_submenu(_("Change Color"), True)
colour_index = pref.load_file(path.colours)
uuids = list(colour_index)
uuids.sort(key=int)
# Show a range of green shades for Ultimate keyboards that are not RGB.
if self.active_device.name.find("Ultimate") != -1:
colour_index = common.get_green_shades()
uuids = list(colour_index.keys())
uuids.sort()
if len(uuids) > 0:
for uuid in uuids:
name = colour_index[uuid]["name"]
rgb = colour_index[uuid]["col"]
icon = self._get_colour_icon(rgb)
submenu[1].append(create_menu_item(name, True, cb.set_colour, [name, rgb], icon))
else:
submenu[1].append(create_menu_item(_("No colors to list."), False))
submenu[1].append(create_seperator())
submenu[1].append(create_menu_item(_("Where did your colors go?"), False))
stdout.vprint(" -- Loaded {0} colours.".format(str(len(uuids))))
return(submenu[0])
def _build_dpi_menu(self):
stdout.vprint("Building dpi menu...")
submenu = create_submenu(_("DPI"), True)
if not self.active_device.has("dpi"):
return None
try:
self.current_dpi = str(self.active_device.dpi[0])
except Exception:
self.current_dpi = _("Unknown")
# Use hardware values where known.
max_dpi = self.active_device.max_dpi
if max_dpi == 16000:
dpi_speed_1 = 200 # Not H/W
dpi_speed_2 = 800
dpi_speed_3 = 1800
dpi_speed_4 = 4500
dpi_speed_5 = 9000
dpi_speed_6 = 16000
elif max_dpi == 8200:
dpi_speed_1 = 200 # Not H/W
dpi_speed_2 = 800
dpi_speed_3 = 1800
dpi_speed_4 = 4800
dpi_speed_5 = 6400
dpi_speed_6 = 8200
else:
dpi_speed_1 = 200
dpi_speed_2 = int(max_dpi / 10)
dpi_speed_3 = int(max_dpi / 8)
dpi_speed_4 = int(max_dpi / 4)
dpi_speed_5 = int(max_dpi / 2)
dpi_speed_6 = int(max_dpi)
submenu[1].append(create_menu_item(_("Super Slow") + ' (' + str(dpi_speed_1) + ')', True, cb.set_dpi, dpi_speed_1, os.path.join(path.data_source, "img/effects/dpi-slow.svg")))
submenu[1].append(create_menu_item(_("Slow") + ' (' + str(dpi_speed_2) + ')', True, cb.set_dpi, dpi_speed_2, os.path.join(path.data_source, "img/effects/dpi-slow.svg")))
submenu[1].append(create_menu_item(_("Medium") + ' (' + str(dpi_speed_3) + ')', True, cb.set_dpi, dpi_speed_3, os.path.join(path.data_source, "img/effects/dpi-slow.svg")))
submenu[1].append(create_menu_item(_("Fast") + ' (' + str(dpi_speed_4) + ')', True, cb.set_dpi, dpi_speed_4, os.path.join(path.data_source, "img/effects/dpi-fast.svg")))
submenu[1].append(create_menu_item(_("Super Fast") + ' (' + str(dpi_speed_5) + ')', True, cb.set_dpi, dpi_speed_5, os.path.join(path.data_source, "img/effects/dpi-fast.svg")))
submenu[1].append(create_menu_item(_("Blazingly Fast") + ' (' + str(dpi_speed_6) + ')', True, cb.set_dpi, dpi_speed_6, os.path.join(path.data_source, "img/effects/dpi-fast.svg")))
return(submenu[0])
def _build_poll_rate_menu(self):
stdout.vprint("Building poll rate menu...")
submenu = create_submenu(_("Poll Rate"), True)
if not self.active_device.has("poll_rate"):
return None
try:
self.current_poll_rate = str(self.active_device.poll_rate) + " Hz"
except Exception:
self.current_poll_rate = _("Unknown")
submenu[1].append(create_menu_item("125 Hz", True, cb.set_poll_rate, 125))
submenu[1].append(create_menu_item("500 Hz", True, cb.set_poll_rate, 500))
submenu[1].append(create_menu_item("1000 Hz", True, cb.set_poll_rate, 1000))
return(submenu[0])
def _build_gamemode_menu(self):
stdout.vprint("Building game mode menu...")
if not self.active_device.has("game_mode_led"):
return None
state = self.active_device.game_mode_led
if state == True:
self.current_gamemode = _("Enabled")
else:
self.current_gamemode = _("Disabled")
submenu = create_submenu(_("Game Mode"), True)
submenu[1].append(create_menu_item(_("Enable"), True, cb.set_gamemode, 1, os.path.join(path.data_source, "img/ui/game-mode.svg")))
submenu[1].append(create_menu_item(_("Disable"), True, cb.set_gamemode, 0, os.path.join(path.data_source, "img/ui/game-mode-disabled.svg")))
return(submenu[0])
# Creates bitmap files for previewing colours.
def _get_colour_icon(self, colour):
"""
Generates a colour block, and gets the path for use as an icon.
colour = [red, green, blue]
"""
colour_path = os.path.join(session_storage, "{0}-{1}-{2}.png".format(str(colour[0]), str(colour[1]), str(colour[2])))
if not os.path.exists(colour_path):
stdout.vprint("Creating colour bitmap for: " + str(colour))
foreground_process("convert -size 16x16 xc:{0} {1}".format(self._colour_to_hex(colour), colour_path), shell=True)
if not os.path.exists(colour_path):
stdout.error("ERROR: Failed to generate bitmap for: " + str(colour))
return(colour_path)
@staticmethod
def _colour_to_hex(colour):
"""
Converts a tuple input to #RRGGBB format
colour = [red, green, blue]
"""
return "#{0:02X}{1:02X}{2:02X}".format(*colour)
# When there are external changes to devicestate.json
def device_state_changed(self):
stdout.vprint("Device state changed. Refreshing menus.")
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
class Callback():
def launch_controller(cb, item):
stdout.vprint("=> Launch Controller")
possible_paths = [
os.path.join(path.data_source, "../polychromatic-controller"),
"/usr/bin/polychromatic-controller"
]
for bin_path in possible_paths:
if os.path.exists(bin_path):
stdout.vprint("Executing: " + os.path.realpath(bin_path))
background_process(bin_path)
return
def quit(cb, item):
stdout.vprint("=> Quit")
exit(0)
def restart_daemon(cb, item):
stdout.vprint("=> Restart Daemon")
import threading
thread = threading.Thread(target=restart_daemon_service, args=())
thread.daemon = True
thread.start()
def set_device(cb, item, target_serial):
for device in indicator.devman.devices:
if device.serial == target_serial:
indicator.active_device = device
indicator.active_serial = device.serial
indicator.current_device = device.name
stdout.vprint("=> Set Device to: {0} (Serial: {1}".format(device.name, device.serial))
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def reload_devices(cb, item):
stdout.vprint("=> Reload Devices")
indicator.menu_devices = indicator._build_device_menu()
indicator.setup()
def retry_applet(cb, item):
stdout.vprint("=> Reload Tray Applet")
indicator.setup()
def set_effect(cb, item, effect_params):
"""
effect_params list [source, name] = Passed strings to common function.
"""
source = effect_params[0]
effect = effect_params[1]
stdout.vprint("=> Set Effect ID:" + str(effect_params))
try:
params = effect_params[2]
except Exception:
params = None
common.set_lighting_effect(pref, indicator.active_device, source, effect, params)
indicator.current_app_profile = None
indicator.current_effect = common.get_effect_state_string(effect)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_brightness(cb, item, attr):
source = attr[0]
value = attr[1]
common.set_brightness(pref, indicator.active_device, source, value)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_brightness_toggle(cb, item, attr):
source = attr[0]
value = attr[1]
common.set_brightness_toggle(pref, indicator.active_device, source, value)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_colour(cb, item, params):
"""
params = [name (str), rgb (list)]
"""
name = params[0]
rgb = params[1]
indicator.current_colour = name
indicator.current_colour_rgb = rgb
stdout.vprint("Set colour to: {0} (RGB {1})".format(name, rgb))
common.save_colours_to_all_sources(pref, indicator.active_device, "colour_primary", rgb)
common.repeat_last_effect(pref, indicator.active_device)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_colour_custom(cb, item):
color_selection_dlg = Gtk.ColorSelectionDialog(_("Change Tray Color"))
color_selection_result = color_selection_dlg.run()
if color_selection_result == Gtk.ResponseType.OK:
result_rgb = color_selection_dlg.get_color_selection().get_current_color()
# Returns value between 0.0 - 1.0 * 255 = 8-bit RGB Value
rgb = KeyboardColour.gdk_colour_to_rgb(result_rgb)
indicator.current_colour = _("Custom") + " ({0}, {1}, {2})".format(str(rgb[0]), str(rgb[1]), str(rgb[2]))
indicator.current_colour_rgb = [rgb[0], rgb[1], rgb[2]]
stdout.vprint("Set custom colour to: " + str(indicator.current_colour_rgb))
common.save_colours_to_all_sources(pref, indicator.active_device, "colour_primary", indicator.current_colour_rgb)
common.repeat_last_effect(pref, indicator.active_device)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
color_selection_dlg.destroy()
def set_profile(cb, item, uuid):
indicator.profiles.send_profile_from_file(indicator.active_device, uuid)
pref.set_device_state(indicator.active_device.serial, "main", "effect", "profile")
pref.set_device_state(indicator.active_device.serial, "main", "profile", uuid)
metadata = indicator.profiles.get_metadata(uuid)
indicator.current_effect = _("Profile")
indicator.current_app_profile = metadata["name"]
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_dpi(cb, item, value):
indicator.active_device.dpi = (value, value)
indicator.current_dpi = str(value)
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_poll_rate(cb, item, value):
indicator.active_device.poll_rate = value
indicator.current_poll_rate = str(value) + " Hz"
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
def set_gamemode(cb, item, state=False):
indicator.active_device.game_mode_led = state
if state == True:
indicator.current_gamemode = _("Enabled")
else:
indicator.current_gamemode = _("Disabled")
indicator.rebuild_all_submenus()
indicator.rebuild_root_menu()
class Terminal(object):
"""