aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/display/menu.py
blob: e7723a7e36b0b519b940d93402c27dedc8159350 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# -*- coding: utf-8 -*-
# Basic LCD menu support
#
# Copyright (C) 2020  Janar Sööt <janar.soot@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, logging, ast, re
from string import Template
from . import menu_keys


class sentinel:
    pass


class error(Exception):
    pass


# Scriptable menu element abstract baseclass
class MenuElement(object):
    def __init__(self, manager, config, **kwargs):
        if type(self) is MenuElement:
            raise error(
                'Abstract MenuElement cannot be instantiated directly')
        self._manager = manager
        self._cursor = '>'
        # set class defaults and attributes from arguments
        self._index = kwargs.get('index', None)
        self._enable = kwargs.get('enable', True)
        self._name = kwargs.get('name', None)
        self._enable_tpl = self._name_tpl = None
        if config is not None:
            # overwrite class attributes from config
            self._index = config.getint('index', self._index)
            self._name_tpl = manager.gcode_macro.load_template(
                config, 'name', self._name)
            try:
                self._enable = config.getboolean('enable', self._enable)
            except config.error:
                self._enable_tpl = manager.gcode_macro.load_template(
                    config, 'enable')
            # item namespace - used in relative paths
            self._ns = str(" ".join(config.get_name().split(' ')[1:])).strip()
        else:
            # ns - item namespace key, used in item relative paths
            # $__id - generated id text variable
            __id = '__menu_' + hex(id(self)).lstrip("0x").rstrip("L")
            self._ns = Template(
                'menu ' + kwargs.get('ns', __id)).safe_substitute(__id=__id)
        self._last_heartbeat = None
        self.__scroll_pos = None
        self.__scroll_request_pending = False
        self.__scroll_next = 0
        # menu scripts
        self._scripts = {}
        # init
        self.init()

    # override
    def init(self):
        pass

    def _render_name(self):
        if self._name_tpl is not None:
            context = self.get_context()
            return self.manager.asflat(self._name_tpl.render(context))
        return self.manager.asflat(self._name)

    def _load_script(self, config, name, option=None):
        """Load script template from config or callback from dict"""
        if name in self._scripts:
            logging.info(
                "Declaration of '%s' hides "
                "previous script declaration" % (name,))
        option = option or name
        if isinstance(config, dict):
            self._scripts[name] = config.get(option, None)
        else:
            self._scripts[name] = self.manager.gcode_macro.load_template(
                config, option, '')

    # override
    def is_editing(self):
        return False

    # override
    def is_scrollable(self):
        return True

    # override
    def is_enabled(self):
        context = self.get_context()
        return self.eval_enable(context)

    # override
    def start_editing(self):
        pass

    # override
    def stop_editing(self):
        pass

    # override
    def get_context(self, cxt=None):
        # get default menu context
        context = self.manager.get_context(cxt)
        context['menu'].update({
            'ns': self.get_ns()
        })
        return context

    def eval_enable(self, context):
        if self._enable_tpl is not None:
            return bool(ast.literal_eval(self._enable_tpl.render(context)))
        return bool(self._enable)

    # Called when a item is selected
    def select(self):
        self.__reset_scroller()

    def heartbeat(self, eventtime):
        self._last_heartbeat = eventtime
        if eventtime >= self.__scroll_next:
            self.__scroll_next = eventtime + 0.5
            if not self.is_editing():
                self.__update_scroller()

    def __update_scroller(self):
        if self.__scroll_pos is None and self.__scroll_request_pending is True:
            self.__scroll_pos = 0
        elif self.__scroll_request_pending is True:
            self.__scroll_pos += 1
            self.__scroll_request_pending = False
        elif self.__scroll_request_pending is False:
            pass  # hold scroll position
        elif self.__scroll_request_pending is None:
            self.__reset_scroller()

    def __reset_scroller(self):
        self.__scroll_pos = None
        self.__scroll_request_pending = False

    def need_scroller(self, value):
        """
        Allows to control the scroller
        Parameters:
            value (bool, None): True  - inc. scroll pos. on next update
                                False - hold scroll pos.
                                None  - reset the scroller
        """
        self.__scroll_request_pending = value

    def __slice_name(self, name, index):
        chunks = []
        for i, text in enumerate(re.split(r'(\~.*?\~)', name)):
            if i & 1 == 0:  # text
                chunks += text
            else:  # glyph placeholder
                chunks.append(text)
        return "".join(chunks[index:])

    def render_name(self, selected=False):
        name = str(self._render_name())
        if selected and self.__scroll_pos is not None:
            name = self.__slice_name(name, self.__scroll_pos)
        else:
            self.__reset_scroller()
        return name

    def get_ns(self, name='.'):
        name = str(name).strip()
        if name.startswith('..'):
            name = ' '.join(
                [(' '.join(str(self._ns).split(' ')[:-1])), name[2:]])
        elif name.startswith('.'):
            name = ' '.join([str(self._ns), name[1:]])
        return name.strip()

    def send_event(self, event, *args):
        return self.manager.send_event(
            "%s:%s" % (self.get_ns(), str(event)), *args)

    def get_script(self, name):
        if name in self._scripts:
            return self._scripts[name]
        return None

    def _run_script(self, name, context):
        _render = getattr(self._scripts[name], 'render', None)
        # check template
        if _render is not None and callable(_render):
            return _render(context)
        # check callback
        elif callable(self._scripts[name]):
            return self._scripts[name](self, context)
        # check static string
        elif isinstance(self._scripts[name], str):
            return self._scripts[name]

    def run_script(self, name, **kwargs):
        event = kwargs.get('event', None)
        context = kwargs.get('context', None)
        render_only = kwargs.get('render_only', False)
        result = ""
        # init context
        if name in self._scripts:
            context = self.get_context(context)
            context['menu'].update({
                'event': event or name
            })
            result = self._run_script(name, context)
        if not render_only:
            # run result as gcode
            self.manager.queue_gcode(result)
            # default behaviour
            _handle = getattr(self, "handle_script_" + name, None)
            if callable(_handle):
                _handle()
        return result

    @property
    def cursor(self):
        return str(self._cursor)[:1]

    @property
    def manager(self):
        return self._manager

    @property
    def index(self):
        return self._index


class MenuContainer(MenuElement):
    """Menu container abstract class"""
    def __init__(self, manager, config, **kwargs):
        if type(self) is MenuContainer:
            raise error(
                'Abstract MenuContainer cannot be instantiated directly')
        super(MenuContainer, self).__init__(manager, config, **kwargs)
        self._populate_cb = kwargs.get('populate', None)
        self._cursor = '>'
        self.__selected = None
        self._allitems = []
        self._names = []
        self._items = []

    def init(self):
        super(MenuContainer, self).init()
        # recursive guard
        self._parents = []

    # overload
    def _names_aslist(self):
        return []

    # overload
    def is_accepted(self, item):
        return isinstance(item, MenuElement)

    def is_editing(self):
        return any([item.is_editing() for item in self._items])

    def stop_editing(self):
        for item in self._items:
            if item.is_editing():
                item.stop_editing()

    def lookup_item(self, item):
        if isinstance(item, str):
            name = item.strip()
            ns = self.get_ns(name)
            return (self.manager.lookup_menuitem(ns), name)
        elif isinstance(item, MenuElement):
            return (item, item.get_ns())
        return (None, item)

    # overload
    def _lookup_item(self, item):
        return self.lookup_item(item)

    def _index_of(self, item):
        try:
            index = None
            if isinstance(item, str):
                s = item.strip()
                index = self._names.index(s)
            elif isinstance(item, MenuElement):
                index = self._items.index(item)
            return index
        except ValueError:
            return None

    def index_of(self, item, look_inside=False):
        index = self._index_of(item)
        if index is None and look_inside is True:
            for con in self:
                if isinstance(con, MenuContainer) and con._index_of(item):
                    index = self._index_of(con)
        return index

    def add_parents(self, parents):
        if isinstance(parents, list):
            self._parents.extend(parents)
        else:
            self._parents.append(parents)

    def assert_recursive_relation(self, parents=None):
        assert self not in (parents or self._parents), \
            "Recursive relation of '%s' container" % (self.get_ns(),)

    def insert_item(self, s, index=None):
        self._insert_item(s, index)

    def _insert_item(self, s, index=None):
        item, name = self._lookup_item(s)
        if item is not None:
            if not self.is_accepted(item):
                raise error("Menu item '%s'is not accepted!" % str(type(item)))
            if isinstance(item, (MenuElement)):
                item.init()
            if isinstance(item, (MenuContainer)):
                item.add_parents(self._parents)
                item.add_parents(self)
                item.assert_recursive_relation()
            if index is None:
                self._allitems.append((item, name))
            else:
                self._allitems.insert(index, (item, name))

    # overload
    def _populate(self):
        pass

    def populate(self):
        self._allitems = []  # empty list
        for name in self._names_aslist():
            self._insert_item(name)
        # populate successor items
        self._populate()
        # run populate callback
        if self._populate_cb is not None and callable(self._populate_cb):
            self._populate_cb(self)
        # send populate event
        self.send_event('populate', self)

    def update_items(self):
        _a = [(item, name) for item, name in self._allitems
              if item.is_enabled()]
        self._items, self._names = zip(*_a) or ([], [])

    # select methods
    def init_selection(self):
        self.select_at(0)

    def select_at(self, index):
        self.__selected = index
        # select element
        item = self.selected_item()
        if isinstance(item, MenuElement):
            item.select()
        return item

    def select_item(self, needle):
        if isinstance(needle, MenuElement):
            if self.selected_item() is not needle:
                index = self.index_of(needle)
                if index is not None:
                    self.select_at(index)
        else:
            logging.error("Cannot select non menuitem")
        return self.selected

    def selected_item(self):
        if isinstance(self.selected, int) and 0 <= self.selected < len(self):
            return self[self.selected]
        else:
            return None

    def select_next(self):
        if not isinstance(self.selected, int):
            index = 0 if len(self) else None
        elif 0 <= self.selected < len(self) - 1:
            index = self.selected + 1
        else:
            index = self.selected
        return self.select_at(index)

    def select_prev(self):
        if not isinstance(self.selected, int):
            index = 0 if len(self) else None
        elif 0 < self.selected < len(self):
            index = self.selected - 1
        else:
            index = self.selected
        return self.select_at(index)

    # override
    def draw_container(self, nrows, eventtime):
        pass

    def __iter__(self):
        return iter(self._items)

    def __len__(self):
        return len(self._items)

    def __getitem__(self, key):
        return self._items[key]

    @property
    def selected(self):
        return self.__selected


class MenuDisabled(MenuElement):
    def __init__(self, manager, config, **kwargs):
        super(MenuDisabled, self).__init__(manager, config, name='')

    def is_enabled(self):
        return False


class MenuCommand(MenuElement):
    def __init__(self, manager, config, **kwargs):
        super(MenuCommand, self).__init__(manager, config, **kwargs)
        self._load_script(config or kwargs, 'gcode')


class MenuInput(MenuCommand):
    def __init__(self, manager, config, **kwargs):
        super(MenuInput, self).__init__(manager, config, **kwargs)
        # set class defaults and attributes from arguments
        self._input = kwargs.get('input', None)
        self._input_min = kwargs.get('input_min', -999999.0)
        self._input_max = kwargs.get('input_max', 999999.0)
        self._input_step = kwargs.get('input_step', 1.0)
        self._realtime = kwargs.get('realtime', False)
        self._input_tpl = self._input_min_tpl = self._input_max_tpl = None
        if config is not None:
            # overwrite class attributes from config
            self._realtime = config.getboolean('realtime', self._realtime)
            self._input_tpl = manager.gcode_macro.load_template(
                config, 'input')
            self._input_min_tpl = manager.gcode_macro.load_template(
                config, 'input_min', str(self._input_min))
            self._input_max_tpl = manager.gcode_macro.load_template(
                config, 'input_max', str(self._input_max))
            self._input_step = config.getfloat(
                'input_step', self._input_step, above=0.)

    def init(self):
        super(MenuInput, self).init()
        self._is_dirty = False
        self.__last_change = None
        self._input_value = None

    def is_scrollable(self):
        return False

    def is_editing(self):
        return self._input_value is not None

    def stop_editing(self):
        if not self.is_editing():
            return
        self._reset_value()

    def start_editing(self):
        if self.is_editing():
            return
        self._init_value()

    def heartbeat(self, eventtime):
        super(MenuInput, self).heartbeat(eventtime)
        if (self._is_dirty is True
                and self.__last_change is not None
                and self._input_value is not None
                and (eventtime - self.__last_change) > 0.250):
            if self._realtime is True:
                self.run_script('gcode', event='change')
                self.run_script('change')
            self._is_dirty = False

    def get_context(self, cxt=None):
        context = super(MenuInput, self).get_context(cxt)
        value = (self._eval_value(context) if self._input_value is None
                 else self._input_value)
        context['menu'].update({
            'input': value
        })
        return context

    def is_enabled(self):
        context = super(MenuInput, self).get_context()
        return self.eval_enable(context)

    def _eval_min(self, context):
        try:
            if self._input_min_tpl is not None:
                return float(ast.literal_eval(
                    self._input_min_tpl.render(context)))
            return float(self._input_min)
        except ValueError:
            logging.exception("Input min value evaluation error")

    def _eval_max(self, context):
        try:
            if self._input_max_tpl is not None:
                return float(ast.literal_eval(
                    self._input_max_tpl.render(context)))
            return float(self._input_max)
        except ValueError:
            logging.exception("Input max value evaluation error")

    def _eval_value(self, context):
        try:
            if self._input_tpl is not None:
                return float(ast.literal_eval(
                    self._input_tpl.render(context)))
            return float(self._input)
        except ValueError:
            logging.exception("Input value evaluation error")

    def _value_changed(self):
        self.__last_change = self._last_heartbeat
        self._is_dirty = True

    def _init_value(self):
        context = super(MenuInput, self).get_context()
        self._input_value = None
        self._input_min = self._eval_min(context)
        self._input_max = self._eval_max(context)
        self._input_value = min(self._input_max, max(
            self._input_min, self._eval_value(context)))
        self._value_changed()

    def _reset_value(self):
        self._input_value = None

    def _get_input_step(self, fast_rate=False):
        return ((10.0 * self._input_step) if fast_rate and (
                (self._input_max - self._input_min) / self._input_step > 100.0)
                else self._input_step)

    def inc_value(self, fast_rate=False):
        last_value = self._input_value
        if self._input_value is None:
            return

        input_step = self._get_input_step(fast_rate)
        self._input_value += abs(input_step)
        self._input_value = min(self._input_max, max(
            self._input_min, self._input_value))

        if last_value != self._input_value:
            self._value_changed()

    def dec_value(self, fast_rate=False):
        last_value = self._input_value
        if self._input_value is None:
            return

        input_step = self._get_input_step(fast_rate)
        self._input_value -= abs(input_step)
        self._input_value = min(self._input_max, max(
            self._input_min, self._input_value))

        if last_value != self._input_value:
            self._value_changed()

    # default behaviour on click
    def handle_script_click(self):
        if not self.is_editing():
            self.start_editing()
        elif self.is_editing():
            self.stop_editing()


class MenuList(MenuContainer):
    def __init__(self, manager, config, **kwargs):
        super(MenuList, self).__init__(manager, config, **kwargs)
        self._viewport_top = 0

        def _cb(el, context):
            el.manager.back()
        # create back item
        self._itemBack = self.manager.menuitem_from(
            'command', name='..', gcode=_cb)

    def _names_aslist(self):
        return self.manager.lookup_children(self.get_ns())

    def _populate(self):
        super(MenuList, self)._populate()
        self._viewport_top = 0
        #  add back as first item
        self.insert_item(self._itemBack, 0)

    def draw_container(self, nrows, eventtime):
        display = self.manager.display
        selected_row = self.selected
        # adjust viewport
        if selected_row is not None:
            if selected_row >= (self._viewport_top + nrows):
                self._viewport_top = (selected_row - nrows) + 1
            if selected_row < self._viewport_top:
                self._viewport_top = selected_row
        else:
            self._viewport_top = 0
        # clamps viewport
        self._viewport_top = max(0, min(self._viewport_top, len(self) - nrows))
        try:
            y = 0
            for row in range(self._viewport_top, self._viewport_top + nrows):
                text = ""
                prefix = ""
                suffix = ""
                if row < len(self):
                    current = self[row]
                    selected = (row == selected_row)
                    if selected:
                        current.heartbeat(eventtime)
                    text = current.render_name(selected)
                    # add prefix (selection indicator)
                    if selected and not current.is_editing():
                        prefix = current.cursor
                    elif selected and current.is_editing():
                        prefix = '*'
                    else:
                        prefix = ' '
                    # add suffix (folder indicator)
                    if isinstance(current, MenuList):
                        suffix += '>'
                # draw to display
                plen = len(prefix)
                slen = len(suffix)
                width = self.manager.cols - plen - slen
                # draw item prefix (cursor)
                ppos = display.draw_text(y, 0, prefix, eventtime)
                # draw item name
                tpos = display.draw_text(y, ppos, text.ljust(width), eventtime)
                # check scroller
                if (selected and tpos > self.manager.cols
                        and current.is_scrollable()):
                    # scroll next
                    current.need_scroller(True)
                else:
                    # reset scroller
                    current.need_scroller(None)
                # draw item suffix
                if suffix:
                    display.draw_text(
                        y, self.manager.cols - slen, suffix, eventtime)
                # next display row
                y += 1
        except Exception:
            logging.exception('List drawing error')


class MenuVSDList(MenuList):
    def __init__(self, manager, config, **kwargs):
        super(MenuVSDList, self).__init__(manager, config, **kwargs)

    def _populate(self):
        super(MenuVSDList, self)._populate()
        sdcard = self.manager.printer.lookup_object('virtual_sdcard', None)
        if sdcard is not None:
            files = sdcard.get_file_list()
            for fname, fsize in files:
                self.insert_item(self.manager.menuitem_from(
                    'command', name=repr(fname), gcode='M23 /%s' % str(fname)))


menu_items = {
    'disabled': MenuDisabled,
    'command': MenuCommand,
    'input': MenuInput,
    'list': MenuList,
    'vsdlist': MenuVSDList
}


TIMER_DELAY = 1.0


class MenuManager:
    def __init__(self, config, display):
        self.running = False
        self.menuitems = {}
        self.menustack = []
        self.children = {}
        self.display = display
        self.printer = config.get_printer()
        self.pconfig = self.printer.lookup_object('configfile')
        self.gcode = self.printer.lookup_object('gcode')
        self.gcode_queue = []
        self.context = {}
        self.root = None
        self._root = config.get('menu_root', '__main')
        self.cols, self.rows = self.display.get_dimensions()
        self.timeout = config.getint('menu_timeout', 0)
        self.timer = 0
        # reverse container navigation
        self._reverse_navigation = config.getboolean(
            'menu_reverse_navigation', False)
        # load printer objects
        self.gcode_macro = self.printer.load_object(config, 'gcode_macro')
        # register itself for printer callbacks
        self.printer.add_object('menu', self)
        self.printer.register_event_handler("klippy:ready", self.handle_ready)
        # register for key events
        menu_keys.MenuKeys(config, self.key_event)
        # Load local config file in same directory as current module
        self.load_config(os.path.dirname(__file__), 'menu.cfg')
        # Load items from main config
        self.load_menuitems(config)
        # Load menu root
        self.root = self.lookup_menuitem(self._root)
        # send init event
        self.send_event('init', self)

    def handle_ready(self):
        # start timer
        reactor = self.printer.get_reactor()
        reactor.register_timer(self.timer_event, reactor.NOW)

    def timer_event(self, eventtime):
        self.timeout_check(eventtime)
        return eventtime + TIMER_DELAY

    def timeout_check(self, eventtime):
        if (self.is_running() and self.timeout > 0
                and isinstance(self.root, MenuContainer)):
            if self.timer >= self.timeout:
                self.exit()
            else:
                self.timer += 1
        else:
            self.timer = 0

    def send_event(self, event, *args):
        return self.printer.send_event("menu:" + str(event), *args)

    def is_running(self):
        return self.running

    def begin(self, eventtime):
        self.menustack = []
        self.timer = 0
        if isinstance(self.root, MenuContainer):
            # send begin event
            self.send_event('begin', self)
            self.update_context(eventtime)
            if isinstance(self.root, MenuContainer):
                self.root.init_selection()
            self.stack_push(self.root)
            self.running = True
            return
        elif self.root is not None:
            logging.error("Invalid root, menu stopped!")
        self.running = False

    def get_status(self, eventtime):
        return {
            'timeout': self.timeout,
            'running': self.running,
            'rows': self.rows,
            'cols': self.cols
        }

    def _action_back(self, force=False, update=True):
        self.back(force, update)
        return ""

    def _action_exit(self, force=False):
        self.exit(force)
        return ""

    def get_context(self, cxt=None):
        context = dict(self.context)
        if isinstance(cxt, dict):
            context.update(cxt)
        return context

    def update_context(self, eventtime):
        # menu default jinja2 context
        self.context = self.gcode_macro.create_template_context(eventtime)
        self.context['menu'] = {
            'eventtime': eventtime,
            'back': self._action_back,
            'exit': self._action_exit
        }

    def stack_push(self, container):
        if not isinstance(container, MenuContainer):
            raise error("Wrong type, expected MenuContainer")
        container.populate()
        top = self.stack_peek()
        if top is not None:
            if isinstance(top, MenuList):
                top.run_script('leave')
        if isinstance(container, MenuList):
            container.run_script('enter')
        if not container.is_editing():
            container.update_items()
            container.init_selection()
        self.menustack.append(container)

    def stack_pop(self, update=True):
        container = None
        if self.stack_size() > 0:
            container = self.menustack.pop()
            if not isinstance(container, MenuContainer):
                raise error("Wrong type, expected MenuContainer")
            top = self.stack_peek()
            if top is not None:
                if not isinstance(container, MenuContainer):
                    raise error("Wrong type, expected MenuContainer")
                if not top.is_editing() and update is True:
                    top.update_items()
                    top.init_selection()
                if isinstance(container, MenuList):
                    container.run_script('leave')
                if isinstance(top, MenuList):
                    top.run_script('enter')
            else:
                if isinstance(container, MenuList):
                    container.run_script('leave')
        return container

    def stack_size(self):
        return len(self.menustack)

    def stack_peek(self, lvl=0):
        container = None
        if self.stack_size() > lvl:
            container = self.menustack[self.stack_size() - lvl - 1]
        return container

    def screen_update_event(self, eventtime):
        # screen update
        if not self.is_running():
            return False
        # draw menu
        self.update_context(eventtime)
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            container.heartbeat(eventtime)
            container.draw_container(self.rows, eventtime)
        return True

    def up(self, fast_rate=False):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            self.timer = 0
            current = container.selected_item()
            if isinstance(current, MenuInput) and current.is_editing():
                current.dec_value(fast_rate)
            else:
                if self._reverse_navigation is True:
                    container.select_next()  # reverse
                else:
                    container.select_prev()  # normal

    def down(self, fast_rate=False):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            self.timer = 0
            current = container.selected_item()
            if isinstance(current, MenuInput) and current.is_editing():
                current.inc_value(fast_rate)
            else:
                if self._reverse_navigation is True:
                    container.select_prev()  # reverse
                else:
                    container.select_next()  # normal

    def back(self, force=False, update=True):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            self.timer = 0
            current = container.selected_item()
            if isinstance(current, MenuInput) and current.is_editing():
                if force is True:
                    current.stop_editing()
                else:
                    return
            parent = self.stack_peek(1)
            if isinstance(parent, MenuContainer):
                self.stack_pop(update)
                index = parent.index_of(container, True)
                if index is not None:
                    parent.select_at(index)
                elif parent.selected_item() is None:
                    parent.init_selection()

            else:
                self.stack_pop()
                self.running = False

    def exit(self, force=False):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            self.timer = 0
            current = container.selected_item()
            if (not force and isinstance(current, MenuInput)
                    and current.is_editing()):
                return
            if isinstance(container, MenuList):
                container.run_script('leave')
            self.send_event('exit', self)
            self.running = False

    def push_container(self, menu):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            if (isinstance(menu, MenuContainer)
                    and not container.is_editing()
                    and menu is not container):
                self.stack_push(menu)
                return True
        return False

    def press(self, event='click'):
        container = self.stack_peek()
        if self.running and isinstance(container, MenuContainer):
            self.timer = 0
            current = container.selected_item()
            if isinstance(current, MenuContainer):
                self.stack_push(current)
            elif isinstance(current, MenuInput):
                if current.is_editing():
                    current.run_script('gcode', event=event)
                current.run_script(event)
            elif isinstance(current, MenuCommand):
                current.run_script('gcode', event=event)
                current.run_script(event)
            else:
                # current is None, no selection. passthru to container
                container.run_script(event)

    def queue_gcode(self, script):
        if not script:
            return
        if not self.gcode_queue:
            reactor = self.printer.get_reactor()
            reactor.register_callback(self.dispatch_gcode)
        self.gcode_queue.append(script)

    def dispatch_gcode(self, eventtime):
        while self.gcode_queue:
            script = self.gcode_queue[0]
            try:
                self.gcode.run_script(script)
            except Exception:
                logging.exception("Script running error")
            self.gcode_queue.pop(0)

    def menuitem_from(self, type, **kwargs):
        if type not in menu_items:
            raise error("Choice '%s' for option '%s'"
                        " is not a valid choice" % (type, menu_items))
        return menu_items[type](self, None, **kwargs)

    def add_menuitem(self, name, item):
        existing_item = False
        if name in self.menuitems:
            existing_item = True
            logging.info(
                "Declaration of '%s' hides "
                "previous menuitem declaration" % (name,))
        self.menuitems[name] = item
        if isinstance(item, MenuElement):
            parent = item.get_ns('..')
            if parent and not existing_item:
                if item.index is not None:
                    self.children.setdefault(parent, []).insert(
                        item.index, item.get_ns())
                else:
                    self.children.setdefault(parent, []).append(
                        item.get_ns())

    def lookup_menuitem(self, name, default=sentinel):
        if name is None:
            return None
        if name in self.menuitems:
            return self.menuitems[name]
        if default is sentinel:
            raise self.printer.config_error(
                "Unknown menuitem '%s'" % (name,))
        return default

    def lookup_children(self, ns):
        if ns in self.children:
            return list(self.children[ns])
        return list()

    def load_config(self, *args):
        cfg = None
        filename = os.path.join(*args)
        try:
            cfg = self.pconfig.read_config(filename)
        except Exception:
            raise self.printer.config_error(
                "Cannot load config '%s'" % (filename,))
        if cfg:
            self.load_menuitems(cfg)
        return cfg

    def load_menuitems(self, config):
        for cfg in config.get_prefix_sections('menu '):
            type = cfg.get('type')
            if type not in menu_items:
                raise error("Choice '%s' for option '%s'"
                            " is not a valid choice" % (type, menu_items))
            item = menu_items[type](self, cfg)
            self.add_menuitem(item.get_ns(), item)

    def _click_callback(self, eventtime, event):
        if self.is_running():
            self.press(event)
        else:
            # lets start and populate the menu items
            self.begin(eventtime)

    def key_event(self, key, eventtime):
        if key == 'click':
            self._click_callback(eventtime, key)
        elif key == 'long_click':
            self._click_callback(eventtime, key)
        elif key == 'up':
            self.up(False)
        elif key == 'fast_up':
            self.up(True)
        elif key == 'down':
            self.down(False)
        elif key == 'fast_down':
            self.down(True)
        elif key == 'back':
            self.back()
        self.display.request_redraw()

    # Collection of manager class helper methods

    @classmethod
    def stripliterals(cls, s):
        """Literals are beginning or ending by the double or single quotes"""
        s = str(s)
        if (s.startswith('"') and s.endswith('"')) or \
                (s.startswith("'") and s.endswith("'")):
            s = s[1:-1]
        return s

    @classmethod
    def aslatin(cls, s):
        if isinstance(s, str):
            return s
        elif isinstance(s, unicode):
            return unicode(s).encode('latin-1', 'ignore')
        else:
            return str(s)

    @classmethod
    def asflat(cls, s):
        return cls.stripliterals(''.join(cls.aslatin(s).splitlines()))