-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.py
executable file
·1167 lines (1034 loc) · 46.8 KB
/
screen.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
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/python
import argparse
import json
import logging
import os
import subprocess
import platform
import pathlib
import traceback # noqa
import locale
import sys
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Pango
from importlib import import_module
from jinja2 import Environment
from signal import SIGTERM
from datetime import datetime
from sv_includes import functions
from sv_includes.error import KnownError
from sv_includes.error import known_errors
from sv_includes.KlippyWebsocket import KlippyWebsocket
from sv_includes.KlippyRest import KlippyRest
from sv_includes.files import KlippyFiles
from sv_includes.KlippyGtk import KlippyGtk
from sv_includes.printer import Printer
from sv_includes.widgets.keyboard import Keyboard
from sv_includes.config import SwierVisionConfig
from panels.base_panel import BasePanel
logging.getLogger("urllib3").setLevel(logging.WARNING)
PRINTER_BASE_STATUS_OBJECTS = [
'bed_mesh',
'configfile',
'display_status',
'extruder',
'fan',
'gcode_move',
'heater_bed',
'idle_timeout',
'pause_resume',
'print_stats',
'toolhead',
'virtual_sdcard',
'webhooks',
'motion_report',
'firmware_retraction',
'exclude_object',
'manual_probe',
]
swiervisiondir = pathlib.Path(__file__).parent.resolve()
def set_text_direction(lang=None):
rtl_languages = ['he']
if lang is None:
for lng in rtl_languages:
if locale.getlocale()[0].startswith(lng):
lang = lng
break
if lang in rtl_languages:
Gtk.Widget.set_default_direction(Gtk.TextDirection.RTL)
logging.debug("Enabling RTL mode")
return False
Gtk.Widget.set_default_direction(Gtk.TextDirection.LTR)
return True
def state_execute(callback):
callback()
return False
class SwierVision(Gtk.Window):
""" Class for creating a screen for Klipper via HDMI """
_cur_panels = []
connecting = False
connecting_to_printer = None
connected_printer = None
files = None
keyboard = None
panels = {}
popup_message = None
screensaver = None
printers = printer = None
updating = False
_ws = None
screensaver_timeout = None
reinit_count = 0
max_retries = 4
initialized = initializing = False
popup_timeout = None
wayland = False
windowed = False
hepa_conscious = False
notification_log = []
def __init__(self, args):
try:
super().__init__(title="Syncraft")
except Exception as e:
logging.exception(f"{e}\n\n{traceback.format_exc()}")
raise RuntimeError from e
self.blanking_time = 600
self.use_dpms = True
self.apiclient = None
self.dialogs = []
self.confirm = None
self.panels_reinit = []
configfile = os.path.normpath(os.path.expanduser(args.configfile))
self._config = SwierVisionConfig(configfile, self)
self.lang_ltr = set_text_direction(self._config.get_main_config().get("language", None))
self.env = Environment(extensions=["jinja2.ext.i18n"], autoescape=True)
self.env.install_gettext_translations(self._config.get_lang())
self.connect("key-press-event", self._key_press_event)
self.connect("configure_event", self.update_size)
monitor = Gdk.Display.get_default().get_primary_monitor()
if monitor is None:
self.wayland = True
monitor = Gdk.Display.get_default().get_monitor(0)
if monitor is None:
raise RuntimeError("Couldn't get default monitor")
self.width = self._config.get_main_config().getint("width", None)
self.height = self._config.get_main_config().getint("height", None)
if 'XDG_CURRENT_DESKTOP' in os.environ \
or platform.system() in ["Darwin", "Windows"]:
logging.warning("Desktop environment detected")
if not self.width:
self.width = max(int(monitor.get_geometry().width * .5), 480)
if not self.height:
self.height = max(int(monitor.get_geometry().height * .5), 320)
if self.width or self.height:
logging.info("Setting windowed mode")
self.set_resizable(True)
self.windowed = True
else:
self.width = monitor.get_geometry().width
self.height = monitor.get_geometry().height
self.fullscreen()
self.set_default_size(self.width, self.height)
self.aspect_ratio = self.width / self.height
self.vertical_mode = self.aspect_ratio < 1.0
logging.info(f"Screen resolution: {self.width}x{self.height}")
self.theme = self._config.get_main_config().get('theme')
self.show_cursor = self._config.get_main_config().getboolean("show_cursor", fallback=False)
self.gtk = KlippyGtk(self)
self.init_style()
self.set_icon_from_file(os.path.join(swiervisiondir, "styles", "icon.svg"))
self.base_panel = BasePanel(self, title="Base Panel")
self.add(self.base_panel.main_grid)
self.show_all()
if self.show_cursor:
self.get_window().set_cursor(
Gdk.Cursor.new_for_display(Gdk.Display.get_default(), Gdk.CursorType.ARROW))
os.system("xsetroot -cursor_name arrow")
else:
self.get_window().set_cursor(
Gdk.Cursor.new_for_display(Gdk.Display.get_default(), Gdk.CursorType.BLANK_CURSOR))
os.system("xsetroot -cursor sv_includes/emptyCursor.xbm sv_includes/emptyCursor.xbm")
self.base_panel.activate()
if self._config.errors:
self.show_error_modal("Invalid config file", self._config.get_errors())
# Prevent this dialog from being destroyed
self.dialogs = []
self.set_screenblanking_timeout(self._config.get_main_config().get('screen_blanking'))
self.log_notification("SwierVision Started", 1)
self.initial_connection()
def initial_connection(self):
self.printers = self._config.get_printers()
state_callbacks = {
"disconnected": self.state_disconnected,
"error": self.state_error,
"paused": self.state_paused,
"printing": self.state_printing,
"ready": self.state_ready,
"startup": self.state_startup,
"shutdown": self.state_shutdown
}
for printer in self.printers:
printer["data"] = Printer(state_execute, state_callbacks, self.process_busy_state)
default_printer = self._config.get_main_config().get('default_printer')
logging.debug(f"Default printer: {default_printer}")
if [True for p in self.printers if default_printer in p]:
self.connect_printer(default_printer)
elif len(self.printers) == 1:
pname = list(self.printers[0])[0]
self.connect_printer(pname)
else:
self.base_panel.show_printer_select(True)
self.show_printer_select()
def connect_printer(self, name):
self.connecting_to_printer = name
if self._ws is not None and self._ws.connected:
self._ws.close()
self.connected_printer = None
self.printer.state = "disconnected"
if self.files:
self.files.reset()
self.files = None
self.connecting = True
self.initialized = False
ind = 0
logging.info(f"Connecting to printer: {name}")
for printer in self.printers:
if name == list(printer)[0]:
ind = self.printers.index(printer)
break
self.printer = self.printers[ind]["data"]
self.apiclient = KlippyRest(
self.printers[ind][name]["moonraker_host"],
self.printers[ind][name]["moonraker_port"],
self.printers[ind][name]["moonraker_api_key"],
)
self.printer_initializing(_("Starting %s") % name, remove=True)
self._ws = KlippyWebsocket(self,
{
"on_connect": self.init_printer,
"on_message": self._websocket_callback,
"on_close": self.websocket_disconnected
},
self.printers[ind][name]["moonraker_host"],
self.printers[ind][name]["moonraker_port"],
)
self.files = KlippyFiles(self)
self._ws.initial_connect()
def ws_subscribe(self):
requested_updates = {
"objects": {
"bed_mesh": ["profile_name", "mesh_max", "mesh_min", "probed_matrix", "profiles"],
"configfile": ["config"],
"display_status": ["progress", "message"],
"fan": ["speed"],
"gcode_move": ["extrude_factor", "gcode_position", "homing_origin", "speed_factor", "speed"],
"idle_timeout": ["state"],
"pause_resume": ["is_paused"],
"print_stats": ["print_duration", "total_duration", "filament_used", "filename", "state", "message",
"info"],
"toolhead": ["homed_axes", "estimated_print_time", "print_time", "position", "extruder",
"max_accel", "max_accel_to_decel", "max_velocity", "square_corner_velocity"],
"virtual_sdcard": ["file_position", "is_active", "progress"],
"webhooks": ["state", "state_message"],
"firmware_retraction": ["retract_length", "retract_speed", "unretract_extra_length", "unretract_speed"],
"motion_report": ["live_position", "live_velocity", "live_extruder_velocity"],
"exclude_object": ["current_object", "objects", "excluded_objects"],
"manual_probe": ['is_active'],
}
}
for extruder in self.printer.get_tools():
requested_updates['objects'][extruder] = [
"target", "temperature", "pressure_advance", "smooth_time", "power"]
for h in self.printer.get_heaters():
requested_updates['objects'][h] = ["target", "temperature", "power"]
for f in self.printer.get_fans():
requested_updates['objects'][f] = ["speed"]
for f in self.printer.get_filament_sensors():
requested_updates['objects'][f] = ["enabled", "filament_detected"]
for p in self.printer.get_output_pins():
requested_updates['objects'][p] = ["value"]
for led in self.printer.get_leds():
requested_updates['objects'][led] = ["color_data"]
self._ws.klippy.object_subscription(requested_updates)
@staticmethod
def _load_panel(panel):
logging.debug(f"Loading panel: {panel}")
panel_path = os.path.join(os.path.dirname(__file__), 'panels', f"{panel}.py")
if not os.path.exists(panel_path):
logging.error(f"Panel {panel} does not exist")
raise FileNotFoundError(os.strerror(2), "\n" + panel_path)
return import_module(f"panels.{panel}")
def show_panel(self, panel, title, remove_all=False, panel_name=None, **kwargs):
if panel_name is None:
panel_name = panel
try:
if remove_all:
self._remove_all_panels()
self.panels_reinit = list(self.panels)
else:
self._remove_current_panel()
if panel_name not in self.panels:
try:
self.panels[panel_name] = self._load_panel(panel).Panel(self, title, **kwargs)
except Exception as e:
self.show_error_modal(f"Unable to load panel {panel}", f"{e}\n\n{traceback.format_exc()}")
return
elif panel_name in self.panels_reinit:
logging.info("Reinitializing panel")
self.panels[panel_name].__init__(self, title, **kwargs)
self.panels_reinit.remove(panel_name)
self._cur_panels.append(panel_name)
self.attach_panel(panel_name)
except Exception as e:
logging.exception(f"Error attaching panel:\n{e}\n\n{traceback.format_exc()}")
def attach_panel(self, panel):
self.base_panel.add_content(self.panels[panel])
logging.debug(f"Current panel hierarchy: {' > '.join(self._cur_panels)}")
if hasattr(self.panels[panel], "process_update"):
self.process_update("notify_status_update", self.printer.data)
self.process_update("notify_busy", self.printer.busy)
if hasattr(self.panels[panel], "activate"):
self.panels[panel].activate()
self.show_all()
def log_notification(self, message, level=0):
time = datetime.now().strftime("%H:%M:%S")
log_entry = {"message": message, "level": level, "time": time}
if len(self.notification_log) > 999:
del self.notification_log[0]
self.notification_log.append(log_entry)
self.process_update("notify_log", log_entry)
def show_popup_message(self, message, level=3):
self.close_screensaver()
if self.popup_message is not None:
self.close_popup_message()
ignore_list = ['timeout', 'inotify', 'moonraker.asvc']
for keyword in ignore_list:
if keyword in message.lower():
return
known_error = None
for er in known_errors:
if message in er.error:
known_error = er
break
if 'unknown command:' in message.lower():
msg_texts = [
message,
_('This may have been caused by a recent update'),
_('Restart the machine for the updates to take effect')
]
message = f"{msg_texts[0]}.\n{msg_texts[1]}.\n{msg_texts[2]}."
msg = Gtk.Button(label=message if known_error is None else _(known_error.message))
msg.set_hexpand(True)
msg.set_vexpand(True)
msg.get_child().set_line_wrap(True)
msg.get_child().set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
msg.get_child().set_max_width_chars(40)
msg.connect("clicked", self.close_popup_message)
msg.get_style_context().add_class("message_popup")
if level == 1:
msg.get_style_context().add_class("message_popup_echo")
elif level == 2:
msg.get_style_context().add_class("message_popup_warning")
elif level == 3:
msg.get_style_context().add_class("message_popup_error")
else:
msg.get_style_context().add_class("message_popup_alert")
if level in [1, 2]:
popup_screentime = 12
else:
popup_screentime = 25
if level in [1, 2]:
popup_length = (self.width * .9) * 0.15
else:
popup_length = (self.width * .9) * 0.2
popup_width = self.width * 0.9
self.pop_grid = self.gtk.HomogeneousGrid()
self.pop_grid.attach(msg, 0, 0, 1, 1)
try:
if known_error.code:
event_box = Gtk.EventBox()
self.image = self.gtk.Image(f"help-{known_error.code}", self.gtk.content_width * 4, self.gtk.content_height * .4, universal=True)
event_box.add(self.image)
event_box.connect("button-press-event", self.close_popup_message)
self.pop_grid.attach(event_box, 0, 1, 1, 3)
except:
pass
popup = Gtk.Popover.new(self.base_panel.titlebar)
popup.get_style_context().add_class("message_popup_popover")
popup.set_size_request(popup_width, popup_length)
popup.set_halign(Gtk.Align.CENTER)
popup.add(self.pop_grid)
popup.popup()
self.popup_message = popup
self.popup_message.show_all()
if self._config.get_main_config().getboolean('autoclose_popups', True):
GLib.timeout_add_seconds(popup_screentime, self.close_popup_message)
return False
def delete_temporary_panels(self, exclude=None):
temp = ["material_load", "material_popup", "nozzle_popup", "material_set", "steps"]
temp_remove = []
for panel in temp:
if panel == exclude:
continue
else:
temp_remove.append(panel)
for panel in temp_remove:
try:
del self.panels[panel]
except:
pass
def close_popup_message(self, widget=None, *args):
if self.popup_message is None:
return
self.popup_message.popdown()
if self.popup_timeout is not None:
GLib.source_remove(self.popup_timeout)
self.popup_message = self.popup_timeout = None
return False
def show_error_modal(self, err, e=""):
logging.error(f"Showing error modal: {err} {e}")
title = Gtk.Label()
title.set_markup(f"<b>{err}</b>\n")
title.set_line_wrap(True)
title.set_line_wrap_mode(Pango.WrapMode.CHAR)
title.set_halign(Gtk.Align.START)
title.set_hexpand(True)
version = Gtk.Label(label=f"{functions.get_software_version()}")
version.set_halign(Gtk.Align.END)
help_msg = _("Provide SwierVision.log when asking for help.\n")
message = Gtk.Label(label=f"{help_msg}\n\n{e}")
message.set_line_wrap(True)
scroll = self.gtk.ScrolledWindow(steppers=False)
scroll.set_vexpand(True)
if self.vertical_mode:
scroll.set_size_request(self.gtk.width - 30, self.gtk.height * .6)
else:
scroll.set_size_request(self.gtk.width - 30, self.gtk.height * .45)
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scroll.add(message)
grid = Gtk.Grid()
grid.attach(title, 0, 0, 1, 1)
grid.attach(version, 1, 0, 1, 1)
grid.attach(Gtk.Separator(), 0, 1, 2, 1)
grid.attach(scroll, 0, 2, 2, 1)
buttons = [
{"name": _("Go Back"), "response": Gtk.ResponseType.CANCEL}
]
self.gtk.Dialog(_("Error"), buttons, grid, self.error_modal_response)
def error_modal_response(self, dialog, response_id):
self.gtk.remove_dialog(dialog)
self.restart_sv()
def restart_sv(self, *args):
logging.debug(f"Restarting {sys.executable} {' '.join(sys.argv)}")
os.execv(sys.executable, ['python'] + sys.argv)
self._ws.send_method("machine.services.restart", {"service": "SwierVision"}) # Fallback
def init_style(self):
settings = Gtk.Settings.get_default()
settings.set_property("gtk-theme-name", "Adwaita")
settings.set_property("gtk-application-prefer-dark-theme", False)
css_data = pathlib.Path(os.path.join(swiervisiondir, "styles", "base.css")).read_text()
with open(os.path.join(swiervisiondir, "styles", "base.conf")) as f:
style_options = json.load(f)
# Load custom theme
theme = os.path.join(swiervisiondir, "styles", self.theme)
theme_style = os.path.join(theme, "style.css")
theme_style_conf = os.path.join(theme, "style.conf")
if os.path.exists(theme_style):
with open(theme_style) as css:
css_data += css.read()
if os.path.exists(theme_style_conf):
try:
with open(theme_style_conf) as f:
style_options.update(json.load(f))
except Exception as e:
logging.error(f"Unable to parse custom template conf file:\n{e}\n\n{traceback.format_exc()}")
self.gtk.color_list = style_options['graph_colors']
for i in range(len(style_options['graph_colors']['extruder']['colors'])):
num = "" if i == 0 else i
css_data += "\n.graph_label_extruder%s {border-left-color: #%s}" % (
num,
style_options['graph_colors']['extruder']['colors'][i]
)
for i in range(len(style_options['graph_colors']['bed']['colors'])):
css_data += "\n.graph_label_heater_bed%s {border-left-color: #%s}" % (
"" if i == 0 else i + 1,
style_options['graph_colors']['bed']['colors'][i]
)
for i in range(len(style_options['graph_colors']['fan']['colors'])):
css_data += "\n.graph_label_fan_%s {border-left-color: #%s}" % (
i + 1,
style_options['graph_colors']['fan']['colors'][i]
)
for i in range(len(style_options['graph_colors']['sensor']['colors'])):
css_data += "\n.graph_label_sensor_%s {border-left-color: #%s}" % (
i + 1,
style_options['graph_colors']['sensor']['colors'][i]
)
css_data = css_data.replace("SV_FONT_SIZE", f"{self.gtk.font_size}")
style_provider = Gtk.CssProvider()
style_provider.load_from_data(css_data.encode())
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
def _go_to_submenu(self, widget, name):
logging.info(f"#### Go to submenu {name}")
# Find current menu item
if "main_menu" in self._cur_panels:
menu = "__main"
elif "splash_screen" in self._cur_panels:
menu = "__splashscreen"
else:
menu = "__print"
logging.info(f"#### Menu {menu}")
disname = self._config.get_menu_name(menu, name)
menuitems = self._config.get_menu_items(menu, name)
if len(menuitems) != 0:
self.show_panel("menu", disname, panel_name=name, items=menuitems)
else:
logging.info("No items in menu")
def _remove_all_panels(self):
for _ in self.base_panel.content.get_children():
self.base_panel.content.remove(_)
for dialog in self.dialogs:
self.gtk.remove_dialog(dialog)
for panel in list(self.panels):
if hasattr(self.panels[panel], "deactivate"):
self.panels[panel].deactivate()
self._cur_panels.clear()
self.close_screensaver()
def _remove_current_panel(self):
self.base_panel.remove(self.panels[self._cur_panels[-1]].content)
if hasattr(self.panels[self._cur_panels[-1]], "deactivate"):
self.panels[self._cur_panels[-1]].deactivate()
def _menu_go_back(self, widget=None, home=False):
logging.info(f"#### Menu go {'home' if home else 'back'}")
self.remove_keyboard()
while len(self._cur_panels) > 1:
self._remove_current_panel()
del self._cur_panels[-1]
if not home:
break
if len(self._cur_panels) < 1:
self.reload_panels()
return
self.attach_panel(self._cur_panels[-1])
def reset_screensaver_timeout(self, *args):
if self.screensaver_timeout is not None:
GLib.source_remove(self.screensaver_timeout)
self.screensaver_timeout = None
if not self.use_dpms and self._config.get_main_config().get('screen_blanking') != "off":
self.screensaver_timeout = GLib.timeout_add_seconds(self.blanking_time, self.show_screensaver)
def show_screensaver(self):
logging.debug("Showing Screensaver")
if self.screensaver is not None:
self.close_screensaver()
self.remove_keyboard()
self.close_popup_message()
for dialog in self.dialogs:
logging.debug("Hiding dialog")
dialog.hide()
close = Gtk.Button()
close.connect("clicked", self.close_screensaver)
box = Gtk.Box()
box.set_size_request(self.width, self.height)
box.pack_start(close, True, True, 0)
box.set_halign(Gtk.Align.CENTER)
box.get_style_context().add_class("screensaver")
self.remove(self.base_panel.main_grid)
self.add(box)
# Avoid leaving a cursor-handle
close.grab_focus()
self.screensaver = box
self.screensaver.show_all()
self.power_devices(None, self._config.get_main_config().get("screen_off_devices", ""), on=False)
if self.screensaver_timeout is not None:
GLib.source_remove(self.screensaver_timeout)
self.screensaver_timeout = None
return False
def close_screensaver(self, widget=None):
if self.screensaver is None:
return False
logging.debug("Closing Screensaver")
self.remove(self.screensaver)
self.screensaver = None
self.add(self.base_panel.main_grid)
if self.use_dpms:
self.wake_screen()
else:
self.reset_screensaver_timeout()
for dialog in self.dialogs:
logging.info(f"Restoring Dialog {dialog}")
dialog.show()
self.show_all()
self.power_devices(None, self._config.get_main_config().get("screen_on_devices", ""), on=True)
def check_dpms_state(self):
if not self.use_dpms:
return False
state = functions.get_DPMS_state()
if state == functions.DPMS_State.Fail:
logging.info("DPMS State FAIL: Stopping DPMS Check")
self.set_dpms(False)
return False
elif state != functions.DPMS_State.On:
if self.screensaver is None:
self.show_screensaver()
return True
def wake_screen(self):
# Wake the screen (it will go to standby as configured)
if self._config.get_main_config().get('screen_blanking') != "off":
logging.debug("Screen wake up")
if not self.wayland:
os.system("xset -display :0 dpms force on")
def set_dpms(self, use_dpms):
self.use_dpms = use_dpms
logging.info(f"DPMS set to: {self.use_dpms}")
self.set_screenblanking_timeout(self._config.get_main_config().get('screen_blanking'))
def set_screenblanking_timeout(self, time):
if not self.wayland:
os.system("xset -display :0 s off")
self.use_dpms = self._config.get_main_config().getboolean("use_dpms", fallback=True)
if time == "off":
logging.debug(f"Screen blanking: {time}")
if self.screensaver_timeout is not None:
GLib.source_remove(self.screensaver_timeout)
self.screensaver_timeout = None
if not self.wayland:
os.system("xset -display :0 dpms 0 0 0")
return
self.blanking_time = abs(int(time))
logging.debug(f"Changing screen blanking to: {self.blanking_time}")
if self.use_dpms and functions.dpms_loaded is True:
if not self.wayland:
os.system("xset -display :0 +dpms")
if functions.get_DPMS_state() == functions.DPMS_State.Fail:
logging.info("DPMS State FAIL")
self.show_popup_message(_("DPMS has failed to load and has been disabled"))
self._config.set("main", "use_dpms", "False")
self._config.save_user_config_options()
else:
logging.debug("Using DPMS")
if not self.wayland:
os.system(f"xset -display :0 dpms 0 {self.blanking_time} 0")
GLib.timeout_add_seconds(1, self.check_dpms_state)
return
# Without dpms just blank the screen
logging.debug("Not using DPMS")
if not self.wayland:
os.system("xset -display :0 dpms 0 0 0")
self.reset_screensaver_timeout()
return
def show_printer_select(self, widget=None):
self.base_panel.show_heaters(False)
self.show_panel("printer_select", _("Printer Select"), remove_all=True)
def process_busy_state(self, busy):
self.process_update("notify_busy", busy)
return False
def websocket_disconnected(self, msg):
self.printer_initializing(msg, remove=True)
self.printer.state = "disconnected"
self.connecting = True
self.connected_printer = None
self.files.reset()
self.files = None
self.initialized = False
self.connect_printer(self.connecting_to_printer)
def state_disconnected(self):
logging.debug("### Going to disconnected")
self.close_screensaver()
self.initialized = False
self.reinit_count = 0
self._init_printer(_("Klipper has disconnected"), remove=True)
def state_error(self):
self.close_screensaver()
msg = _("Klipper has encountered an error.") + "\n"
state = self.printer.get_stat("webhooks", "state_message")
if "FIRMWARE_RESTART" in state:
msg += _("A FIRMWARE_RESTART may fix the issue.") + "\n"
elif "micro-controller" in state:
msg += _("Please recompile and flash the micro-controller.") + "\n"
self.printer_initializing(msg + "\n" + state, remove=True)
def state_paused(self):
self.state_printing()
def state_printing(self):
self.close_screensaver()
for dialog in self.dialogs:
self.gtk.remove_dialog(dialog)
self.show_panel("job_status", _("Printing"), remove_all=True)
def state_ready(self, wait=True):
# Do not return to main menu if completing a job, timeouts/user input will return
if "job_status" in self._cur_panels and wait:
return
if not self.initialized:
logging.debug("Printer not initialized yet")
self.printer.state = "not ready"
return
self._config.set_ready_timestamp()
if self._config.get_hidden_config().getboolean('welcome', False):
self.first_panel = "welcome"
elif not functions.valid_hepa() and not self.hepa_conscious:
self.first_panel = "hepa_warning"
else:
self.first_panel = "main_menu"
if self.first_panel != "main_menu":
self.hepa_conscious = True
self.show_panel(self.first_panel, None, remove_all=True)
else:
self.show_panel(self.first_panel, None, remove_all=True, items=self._config.get_menu_items("__main"))
def state_startup(self):
self.printer_initializing(_("Klipper is attempting to start"))
def state_shutdown(self):
self.close_screensaver()
msg = self.printer.get_stat("webhooks", "state_message")
msg = msg if "ready" not in msg else ""
self.printer_initializing(_("Klipper has shutdown") + "\n\n" + msg, remove=True)
def toggle_brightness_shortcut(self, value):
self.base_panel.show_shortcut()
def change_language(self, widget, lang):
self._config.install_language(lang)
self.lang_ltr = set_text_direction(lang)
self.env.install_gettext_translations(self._config.get_lang())
self._config._create_configurable_options(self)
self._config.set('main', 'language', lang)
self._config.save_user_config_options()
self.reload_panels()
def reload_panels(self, *args):
if "printer_select" in self._cur_panels:
self.show_printer_select()
return
self._remove_all_panels()
if self.printer is not None:
self.printer.change_state(self.printer.state)
def _websocket_callback(self, action, data):
if self.connecting:
return
if action == "notify_klippy_disconnected":
self.printer.process_update({'webhooks': {'state': "disconnected"}})
return
elif action == "notify_klippy_shutdown":
self.printer.process_update({'webhooks': {'state': "shutdown"}})
elif action == "notify_klippy_ready":
if not self.initialized:
logging.debug("Still not initialized")
return
self.printer.process_update({'webhooks': {'state': "ready"}})
elif action == "notify_status_update" and self.printer.state != "shutdown":
self.printer.process_update(data)
if 'manual_probe' in data and data['manual_probe']['is_active'] and 'zcalibrate' not in self._cur_panels:
self.show_panel("zcalibrate", _('Z Calibrate'))
elif action == "notify_filelist_changed":
if self.files is not None:
self.files.process_update(data)
elif action == "notify_metadata_update":
self.files.request_metadata(data['filename'])
elif action == "notify_update_response":
if 'message' in data and 'Error' in data['message']:
logging.error(f"{action}:{data['message']}")
self.show_popup_message(data['message'], 3)
if "SwierVision" in data['message']:
self.restart_sv()
elif action == "notify_power_changed":
logging.debug("Power status changed: %s", data)
self.printer.process_power_update(data)
self.panels['splash_screen'].check_power_status()
elif action == "notify_gcode_response" and self.printer.state not in ["error", "shutdown"]:
if not (data.startswith("B:") or data.startswith("T:")):
if data.startswith("echo: "):
self.show_popup_message(data[6:], 1)
elif data.startswith("!! "):
self.show_popup_message(data[3:], 3)
elif "unknown" in data.lower() and \
not ("TESTZ" in data or "MEASURE_AXES_NOISE" in data or "ACCELEROMETER_QUERY" in data):
self.show_popup_message(data)
elif "SAVE_CONFIG" in data and self.printer.state == "ready":
script = {"script": "SAVE_CONFIG"}
self._confirm_send_action(
None,
_("Save changes?"),
"printer.gcode.script",
script
)
self.process_update(action, data)
def process_update(self, *args):
self.base_panel.process_update(*args)
if self._cur_panels and hasattr(self.panels[self._cur_panels[-1]], "process_update"):
self.panels[self._cur_panels[-1]].process_update(*args)
def _confirm_send_action(self, widget, text, method, params=None):
buttons = [
{"name": _("Accept"), "response": Gtk.ResponseType.OK},
{"name": _("Cancel"), "response": Gtk.ResponseType.CANCEL}
]
try:
j2_temp = self.env.from_string(text)
text = j2_temp.render()
except Exception as e:
logging.debug(f"Error parsing jinja for confirm_send_action\n{e}\n\n{traceback.format_exc()}")
label = Gtk.Label()
label.set_markup(text)
label.set_hexpand(True)
label.set_halign(Gtk.Align.CENTER)
label.set_vexpand(True)
label.set_valign(Gtk.Align.CENTER)
label.set_line_wrap(True)
label.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
if self.confirm is not None:
self.gtk.remove_dialog(self.confirm)
self.confirm = self.gtk.Dialog(
"SwierVision", buttons, label, self._confirm_send_action_response, method, params
)
def _confirm_send_action_response(self, dialog, response_id, method, params):
self.gtk.remove_dialog(dialog)
if response_id == Gtk.ResponseType.OK:
self._send_action(None, method, params)
if method == "server.files.delete_directory":
GLib.timeout_add_seconds(2, self.files.refresh_files)
def _send_action(self, widget, method, params):
logging.info(f"{method}: {params}")
if isinstance(widget, Gtk.Button):
self.gtk.Button_busy(widget, True)
self._ws.send_method(method, params, self.enable_widget, widget)
else:
self._ws.send_method(method, params)
def enable_widget(self, *args):
for x in args:
if isinstance(x, Gtk.Button):
GLib.timeout_add(150, self.gtk.Button_busy, x, False)
def printer_initializing(self, msg, remove=False):
if 'splash_screen' not in self.panels or remove:
self.show_panel("splash_screen", None, remove_all=True)
self.panels['splash_screen'].update_text(msg)
self.log_notification(msg, 0)
def search_power_devices(self, devices):
found_devices = []
if self.connected_printer is None or not devices:
return found_devices
devices = [str(i.strip()) for i in devices.split(',')]
power_devices = self.printer.get_power_devices()
if power_devices:
found_devices = [dev for dev in devices if dev in power_devices]
logging.info(f"Found {found_devices}", )
return found_devices
def power_devices(self, widget=None, devices=None, on=False):
devs = self.search_power_devices(devices)
for dev in devs:
if on:
self._ws.klippy.power_device_on(dev)
else:
self._ws.klippy.power_device_off(dev)
def _init_printer(self, msg, remove=False):
self.printer_initializing(msg, remove)
self.initializing = False
GLib.timeout_add_seconds(3, self.init_printer)
return False
def init_printer(self):
if self.initializing:
return False
self.initializing = True
if self.reinit_count > self.max_retries or 'printer_select' in self._cur_panels:
self.initializing = False
return False
state = self.apiclient.get_server_info()
if state is False:
logging.info("Moonraker not connected")
self.initializing = False
return False
self.connecting = not self._ws.connected
self.connected_printer = self.connecting_to_printer
self.base_panel.set_sv_printer_cfg(self.connected_printer)
# Moonraker is ready, set a loop to init the printer
self.reinit_count += 1
server_info = self.apiclient.get_server_info()["result"]
logging.info(f"Moonraker info {server_info}")
popup = ''
level = 2
if server_info["warnings"]:
popup += '\nMoonraker warnings:\n'
for warning in server_info["warnings"]:
warning = warning.replace('<br>', '').replace('<br/>', '\n').replace('</br>', '\n').replace(':', ':\n')
popup += f"{warning}\n"
if server_info["failed_components"]:
popup += '\nMoonraker failed components:\n'
for failed in server_info["failed_components"]:
popup += f'[{failed}]\n'
if server_info["missing_klippy_requirements"]:
popup += '\nMissing Klipper configuration:\n'
for missing in server_info["missing_klippy_requirements"]:
popup += f'[{missing}]\n'
level = 3
if popup:
self.show_popup_message(popup, level)
if "power" in server_info["components"]:
powerdevs = self.apiclient.send_request("machine/device_power/devices")
if powerdevs is not False:
self.printer.configure_power_devices(powerdevs['result'])
if "webcam" in server_info["components"]:
cameras = self.apiclient.send_request("server/webcams/list")
if cameras is not False:
self.printer.configure_cameras(cameras['result']['webcams'])
if "spoolman" in server_info["components"]:
self.printer.enable_spoolman()
if state['result']['klippy_connected'] is False:
logging.info("Klipper not connected")
msg = _("Moonraker: connected") + "\n\n"
msg += f"Klipper: {state['result']['klippy_state']}" + "\n\n"
if self.reinit_count <= self.max_retries:
msg += _("Retrying") + f' #{self.reinit_count}'
return self._init_printer(msg)
printer_info = self.apiclient.get_printer_info()
if printer_info is False:
return self._init_printer("Unable to get printer info from moonraker")
config = self.apiclient.send_request("printer/objects/query?configfile")
if config is False:
return self._init_printer("Error getting printer configuration")
logging.debug(config['result']['status'])
# Reinitialize printer, in case the printer was shut down and anything has changed.
self.printer.reinit(printer_info['result'], config['result']['status'])
self.printer.available_commands = self.apiclient.get_gcode_help()['result']