summaryrefslogtreecommitdiff
path: root/interface/interface.py
blob: d50034462bb8a0d87eae60f12c011f5f7a3c5955 (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
import curses
import os
import datetime as dt
import itertools
import threading
import traceback as tb

import blc2 

from blc2.functions.audio import Audio
from blc2.functions.scene import Scene
from blc2.functions.chaser import Chaser
from blc2.functions.chaserstep import ChaserStep
from blc2.functions.join import Join
from blc2.constants import SCENE, CHASER, AUDIO, MANUAL, INHERIT, INFTY, CHASERSTEP, JOIN, ONESHOT, LOOP, RANDOM

from blc2.topology import Fixture

from blc2.workspace import Workspace

from .globals import CURSES_LOCK 
from .input.tabcomp import Input 
from .channelbank import ChannelBank
from .chaserview import ChaserView
from .pager import Pager
from .dialog import askyesnocancel
from .audioview import AudioView
from .render import Renderer
from .dummy import DummyOutput

def wrap_curses(f):
    def inner(*args, **kwargs):
        return curses.wrapper(lambda stdscr: f(*args, stdscr, **kwargs))
    return inner

__version__ = "v0.1.0"

class Interface:
    def _compute_sizes(self, height, width):
        hb = height // 2
        ht = height - hb

        wr = width // 2
        wl = width - wr - 1

        chasers = []
        if self.chaser_views:
            current = 0
            cw = (wl // len(self.chaser_views)) - 1
            for i in range(len(self.chaser_views)):
                curw = (wl - current) if (i+1) == len(self.chaser_views) else cw
                chasers.append(((hb, curw), (ht, current)))
                current += curw + 1

        return (
            ((ht, width), (0, 0),),
            ((4, wr), (height-4, wl+1),), 
            ((hb - 4, wr), (ht, wl+1),),
            *chasers
        )

    def _resize(self):
        ## FIXME
        try:
            self._actual_resize()
            self._actual_resize()
        except:
            self.stdscr.addstr(0, 0, "Too small")
            self.stdscr.refresh()
            return False
        return True

    def _actual_resize(self):
        for (a1, a2), (f1, f2) in zip(self._compute_sizes(*self.stdscr.getmaxyx()), ((self.channel_bank.set_dim, self.channel_bank.set_pos), (self.input.set_dim, self.input.set_pos), (self.pager.set_dim, self.pager.set_pos), *((c.set_dim, c.set_pos) for c in self.chaser_views))):
            f1(*a1)
            f2(*a2)

    @wrap_curses
    def main(self, stdscr):
        height, width = stdscr.getmaxyx()
        self.stdscr = stdscr
        (cbd, cbp), (ind, inp), (pgd, pgp) = self._compute_sizes(height, width)
        self.channel_bank = ChannelBank(*cbp, *cbd)
        self.input = Input(*inp, *ind)
        self.input.context = self.context_base
        self.pager = Pager(*pgp, *pgd)

        todisp = [
            "Welcome to BLC2!",
            "Currently running library %s, interface %s" % (blc2.__version__, __version__),
            "",
        ]

        if self._w_created:
            todisp.append("Created a new workspace")
        else:
            todisp.append("Loaded workspace \"%s\" from \"%s\"" % (self.w.name, self.path))
            todisp.append("Authored by %s, last modified at %s" % (self.w.author, self.w.modified.strftime("%Y-%m-%d %H:%M:%S")))

        self.pager.display_many(todisp)

        if not self.output.ok:
            self.pager.display_many(("WARNING: Output is not OK!",), True)

        self.verify_audio()

        self.input.main(self._resize)

    def verify_audio(self):
        def gather_audio(func):
            if func.type == AUDIO:
                return (func,)
            elif func.type in (CHASER, JOIN):
                return tuple(itertools.chain(*(gather_audio(i) for i in func.steps)))
            elif func.type == CHASERSTEP and func.function is not None:
                return gather_audio(func.function)

            return ()

        f = None
        bad = []
        with self.w_lock:
            funcs = set()
            if f is None:
                for func in self.w.functions.values():
                    funcs.update(gather_audio(func))
            else:
                funcs = gather_audio(f)

            for f in sorted(funcs, key=lambda i: i.id):
                if not os.path.isfile(f.filename):
                    bad.append("- %3d \"%s\"" % (f.id, f.filename))

            if bad:
                self.pager.display_many(["MISSING AUDIO FILES:"] + bad)
            else:
                self.pager.display_many(("No missing audio files!",))

    def handle_show(self, f):
        with self.w_lock:
            if f is None or f.type == SCENE:
                if not isinstance(self.channel_bank, ChannelBank):
                    (hw, yx), *_ = self._compute_sizes(*self.stdscr.getmaxyx())
                    self.channel_bank = ChannelBank(*yx, *hw)
                if f is None:
                    return
                self.channel_bank.set_scope(f.scope)
                self.channel_bank.set_values(f.values.items())
            elif f.type == AUDIO:
                if not isinstance(self.channel_bank, AudioView):
                    (hw, yx), *_ = self._compute_sizes(*self.stdscr.getmaxyx())
                    self.channel_bank = AudioView(*yx, *hw)
                self.channel_bank.audio = f
            self.channel_bank.title = f.type + ' "%s"' % f.name
            self.renderer.clear_hold()
            if f.type == SCENE:
                self.renderer.hold(f.values)

    def handle_enter(self, fid):
        with self.w_lock:
            if fid is None:
                if not self.chaser_views:
                    return "No chaser loaded"
                f = self.chaser_views[0].chaser
            else:
                if fid not in self.w.functions:
                    return "No such function"
                f = self.w.functions[fid]

            if self.current_cv is not None:
                self.current_cv.highlight = False

            if self.chaser is not None:
                cv = [c for c in self.chaser_views if c.chaser == self.chaser][0].selected
                self.chaser_stack.append((self.chaser, cv))
                self.chaser = None

            if f.type == SCENE:
                self.primitive = f
                self.handle_show(f)
                self.channel_bank.highlight = True
                self.input.context = self.context_scene
            elif f.type in (CHASER, JOIN):
                self.chaser = f
                cv = [c for c in self.chaser_views if c.chaser == f]
                if not cv:
                    if len(self.chaser_views) == 2:
                        self.chaser_views[1].set_chaser(f, None)
                    else:
                        self.base_add(f.id)
                    self.current_cv = self.chaser_views[-1]
                else:
                    cv = cv[0]
                    self.current_cv = cv
                self.current_cv.highlight = True
                if f.type == CHASER:
                    self.input.context = self.context_chaser
                elif f.type == JOIN:
                    self.input.context = self.context_join
            elif f.type == AUDIO:
                self.primitive = f
                self.handle_show(f)
                self.channel_bank.highlight = True
                self.input.context = self.context_audio
            else:
                ## FIXME
                return "No other types allowed yet"

    def base_add(self, fid):
        with self.w_lock:
            if fid not in self.w.functions:
                return "No such function"
            f = self.w.functions[fid]
            if f.type not in (CHASER, JOIN):
                return "Can only add chasers!"
            with CURSES_LOCK:
                if True in (c.chaser.id for c in self.chaser_views):
                    return "Already added!"
                elif len(self.chaser_views) == 2:
                    ## FIXME?
                    return "Can only use two for now"
                self.chaser_views.append(None)
                ld, lp = self._compute_sizes(*self.stdscr.getmaxyx())[-1]
                self.chaser_views[-1] = ChaserView(*lp, *ld)
                self._resize()
                self.chaser_views[-1].set_chaser(f, None)

    def base_remove(self, n, index=True):
        with self.w_lock:
            if not index:
                n = [j for j, i in enumerate(self.chaser_views) if i.chaser.id == n]
                if not n:
                    return "No such chaser loaded"
                n = n[0]
            if n >= len(self.chaser_views) or n < 0:
                return "Index out of range"
            with CURSES_LOCK:
                self.chaser_views[n].win.erase()
                self.chaser_views[n].win.refresh()
            self.chaser_views.pop(n)
            self._resize()

    def base_delete(self, fid):
        with self.w_lock:
            if fid not in self.w.functions:
                return "No such function"
            f = self.w.functions[fid]
            f.delete()

    def base_new(self, name, cls):
        with self.w_lock:
            f = cls(self.w, name=name)
            self.handle_enter(f.id)

    def _gather_channels(self, cr):
        with self.w_lock:
            ## Gather the affected channels
            channels = []
            for f in self.w.fixtures.values():
                for s, e in cr[0]:
                    if (s <= f.id <= e) or (e == -1 and f.id >= s):
                        break
                else:
                    continue

                for n, c in enumerate(f.channels):
                    for s, e in cr[1]:
                        if (s <= n <= e) or (e == -1 and n >= s):
                            channels.append(c)
                            break

            return channels

    def scene_set(self, cr, v):
        with self.w_lock:
            channels = self._gather_channels(cr)

            ## Set the values
            self.primitive.update({c: v for c in channels})

            ## Update the display
            self.channel_bank.set_scope(self.primitive.scope)
            if v is not None:
                self.channel_bank.set_values(((c, v) for c in channels))
            self.renderer.clear_hold()
            self.renderer.hold(self.primitive.values)

    def scene_edit(self, cr, force = False):
        with self.w_lock:

            channels = self._gather_channels(cr)
            if not force:
                channels = [c for c in channels if c in self.primitive.scope]
            else:
                self.primitive.update({c: 0 for c in channels if c not in self.primitive.scope})

            if not channels:
                return "No channels given"

            curses.curs_set(0)
            values = self.primitive.values

            self.channel_bank.set_active(channels, True)

            while True:
                self.channel_bank.set_scope(self.primitive.scope)
                self.channel_bank.set_values(((c, v) for c, v in values.items()))
                self.renderer.clear_hold()
                self.renderer.hold(self.primitive.values)

                l = self.pager.win.getch()
                delta = 0

                if l == curses.KEY_RESIZE:
                    self._resize()
                elif l == ord('q'):
                    break
                elif l in (ord('j'), curses.KEY_DOWN):
                    delta = -5
                elif l in (ord('k'), curses.KEY_UP):
                    delta = 5
                elif l == ord('h'):
                    delta = -1
                elif l == ord('l'):
                    delta = 1
                elif l == curses.KEY_NPAGE:
                    delta = 25
                elif l == curses.KEY_PPAGE:
                    delta = -25
                elif l == curses.KEY_HOME:
                    delta = 255
                elif l == curses.KEY_END:
                    delta = -255

                if delta != 0:
                    for c in channels:
                        values[c] = max(0, min(255, values[c]+delta))
                        self.primitive.update(values)

            self.channel_bank.set_active(channels, False)

            curses.curs_set(1)

    def scene_clear(self, cr):
        self.scene_set(cr, None)

    def scene_exit(self):
        self.primitive = None
        
        self.handle_exit()

    def audio_exit(self):
        self.primitive = None

        self.handle_exit()

    def handle_exit(self):
        for c in self.chaser_views:
            c.highlight = False

        if self.current_cv is not None:
            self.current_cv.selected = None

        self.channel_bank.highlight = False

        if self.chaser_stack:
            ## Load the previous chaser
            c, sel = self.chaser_stack.pop(-1)

            if True not in (True for i in self.chaser_views if i.chaser.id == c.id):
                ## We need to load it
                ## Check if we have one for the current chaser
                try:
                    idx = [i.chaser.id for i in self.chaser_views].index(self.chaser.id)
                except (ValueError, AttributeError):
                    ## We don't have it
                    if len(self.chaser_views) == 2:
                        ## We don't have room for it, just replace the last one
                        self.chaser_views[1].set_chaser(c, sel)
                    else:
                        self.base_add(c.id)
                    self.current_cv = self.chaser_views[-1]
                else:
                    self.chaser_views[idx].set_chaser(c, sel)
                    self.current_cv = self.chaser_views[idx]
            
            self.current_cv.highlight = True
            self.current_cv.selected = sel
            if c.type == CHASER:
                self.input.context = self.context_chaser
            else:
                self.input.context = self.context_join
            self.chaser = c
        else:
            if isinstance(self.channel_bank, AudioView):
                (hw, yx), *_ = self._compute_sizes(*self.stdscr.getmaxyx())
                self.channel_bank = ChannelBank(*yx, *hw)
            self.channel_bank.set_scope(())
            self.channel_bank.title = "Channels"

            self.input.context = self.context_base
            self.chaser = None
            self.current_cv = None
            self.renderer.clear_hold()

    def base_save(self, path=None):
        with self.w_lock:
            if path is not None:
                self.path = path
            if self.path is None:
                return "No path set"
            self.w.modified = dt.datetime.now()
            self.w.save(self.path)

            return "Saved to "+self.path

    def base_quit(self):
        if askyesnocancel(self.stdscr, "Really exit?"):
            quit()
        else:
            self._resize()

    def list_fixtures(self):
        with self.w_lock:
            td = ["FIXTURES:"]
            for f in sorted(self.w.fixtures.values(), key=lambda a: a.id):
                td.append("- %03d: %s" % (f.id, f.name))
                for c in f.channels:
                    td.append("  %03d-%03d: %s" % (f.id, c.id, c.name))
            self.pager.display_many(td, split=True)

    def list_functions(self, typ):
        with self.w_lock:
            td = [typ.upper()+"S:"]
            for f in sorted(self.w.functions.values(), key=lambda a: a.id):
                if f.type == typ:
                    td.append("- %03d: %s" % (f.id, f.name))
            self.pager.display_many(td, split=True)

    def page(self):
        self.pager.user_page()

    def page_clear(self):
        self.pager.clear()

    def help(self, name, commands, help_):
        if name is None:
            dname = "MODE HELP:"
        else:
            dname = name.upper() + " COMMAND:"

        todisp = [dname, ""]
        if commands:
            if name is None:
                todisp.append("Available commands:")
            else:
                todisp.append("Available forms:")
            todisp.extend(("- "+i for i in commands))
            todisp.append("")
        
        todisp.extend(help_.split('\n'))

        self.pager.display_many(todisp, split=True)

    def chaser_select(self, num):
        with self.w_lock:
            if num < 1 or num > len(self.current_cv.chaser.steps):
                return "Out of range"
            
            self.current_cv.selected = num - 1
            s = self.current_cv.chaser.steps[num-1]
            if (s.type == CHASERSTEP and s.function is not None and s.function.type in (SCENE, AUDIO,)) or s.type in (SCENE, AUDIO,):
                self.handle_show(s.function if s.type == CHASERSTEP else s)

    def chaser_edit(self, num=None):
        with self.w_lock:
            if num is not None:
                r = self.chaser_select(num)
                if r is not None:
                    return r
            elif self.current_cv.selected is None:
                return "No step selected"

            c = self.current_cv.chaser.steps[self.current_cv.selected]
            if c.type == CHASERSTEP and c.function is None:
                return "No function on step"
            
            self.handle_enter(c.function.id if c.type == CHASERSTEP else c.id)

    def chaser_delete(self, num=None):
        with self.w_lock:
            if num is not None:
                r = self.chaser_select(num)
                if r is not None:
                    return r
            elif self.current_cv.selected is None:
                return "No step selected"

            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]
            sel = self.current_cv.selected
            with CURSES_LOCK:
                if c.type == CHASER:
                    if askyesnocancel(self.stdscr, "Really delete step %d?" % (s.index+1), resize=self._resize):
                        sel = s.index
                        s.delete()
                    else:
                        return
                else:
                    c.delete_step(s)
                        
                if not c.steps:
                    sel = None
                else:
                    sel = max(0, sel-1)
                self.current_cv.set_chaser(c, sel)
                self._resize()

    def chaser_fade(self, t, out=False):
        with self.w_lock:
            if self.current_cv.selected is None:
                return "No step selected"
            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]
            if out:
                s.fade_out = t
            else:
                s.fade_in = t
            self.current_cv.set_chaser(c, s.index)

    def chaser_duration(self, t):
        with self.w_lock:
            if self.current_cv.selected is None:
                return "No step selected"
            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]
            if s.duration_mode != MANUAL:
                s.duration_mode = MANUAL
            s.duration = t
            self.current_cv.set_chaser(c, s.index)

    def chaser_unset(self):
        with self.w_lock:
            if self.current_cv.selected is None:
                return "No step selected"
            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]
            s.duration_mode = INHERIT
            self.current_cv.set_chaser(c, s.index)

    def chaser_new(self, index, name, fid=None):
        with self.w_lock:
            if fid is not None:
                if fid not in self.w.functions:
                    return "No such function"
                f = self.w.functions[fid]
                if f.type not in (SCENE, AUDIO, CHASER, JOIN):
                    return "Invalid function"
            else:
                f = None
            
            c = self.current_cv.chaser

            if c.type == CHASER:
                if name is None and f is not None:
                    name = "Step \"%s\"" % f.name
                s = ChaserStep(c, index=index, name=name, function=f)
                self.current_cv.set_chaser(c, s.index)
            else:
                if f.id in (i.id for i in c.steps):
                    return "Already added"
                c.add_step(f)
                self.current_cv.set_chaser(c, len(c.steps)-1)
            #self.chaser_edit(s.index+1)

    def chaser_new_new(self, index, name, fname, type_):
        with self.w_lock:
            if name is None and f is not None:
                name = "Step \"%s\"" % f.name

            f = type_(self.w, name=fname)

            return self.chaser_new(index, name, fid=f.id)

    def chaser_rename(self, name):
        with self.w_lock:
            if self.current_cv.selected is None:
                return "No step selected"
            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]
            s.name = name
            
            self.current_cv.set_chaser(c, s.index)

    def chaser_bind(self, fid):
        with self.w_lock:
            if self.current_cv.selected is None:
                return "No step selected"
            c = self.current_cv.chaser
            s = c.steps[self.current_cv.selected]

            if fid not in self.w.functions:
                return "No such function"
            f = self.w.functions[fid]
            if f.type not in (SCENE, AUDIO, CHASER, JOIN):
                return "Invalid function"

            s.function = f

            self.current_cv.set_chaser(c, s.index)
            self.chaser_select(s.index+1)
    
    def chaser_move(self, a, b = None):
        with self.w_lock:
            cursel = self.current_cv.selected 
            if cursel is not None:
                cursel = self.current_cv.chaser.steps[cursel]
            if b is not None:
                sel = a - 1
                if sel < 0 or sel >= len(self.current_cv.chaser.steps):
                    return "Invalid selection"
                to = b - 1
            else:
                to = a - 1
                if self.current_cv.selected is None:
                    return "No step selected"
                sel = self.current_cv.selected
            if to < 0 or to >= len(self.current_cv.chaser.steps):
                return "Invalid destination"
            c = self.current_cv.chaser
            s = c.steps[sel]
            s.index = to
            
            self.current_cv.set_chaser(c, cursel.index if cursel is not None else None)

    def primitive_rename(self, name):
        with self.w_lock:
            self.primitive.name = name
            self.channel_bank.title = self.primitive.type + ' "%s"' % name

    def chaser_rename_self(self, name):
        with self.w_lock:
            c = self.current_cv.chaser
            c.name = name

            self.current_cv.set_chaser(c, self.current_cv.selected)

    def audio_fade(self, t, out=False):
        with self.w_lock:
            if out:
                self.primitive.fade_out = t
            else:
                self.primitive.fade_in = t
            self.channel_bank.audio = self.primitive

    def audio_filename(self, fname):
        with self.w_lock:
            self.primitive.filename = fname
            self.channel_bank.audio = self.primitive

    def _render_callback(self, t, values):
        if not self.rendering:
            return
        with self.w_lock, CURSES_LOCK:
            syx = self.input.win.getyx()

            self.channel_bank.set_values(values)
            self.channel_bank.title = "LIVE: %7.2fs" % t

            for d, cv in zip(self.renderer._data, (i for i in self.chaser_views if i.chaser.type == CHASER)):
                cv.selected = d.steps[-1].index if d.steps else None

            self.input.win.move(*syx)

    def base_run(self):
        if not self.chaser_views:
            return "No chasers loaded"

        self.channel_bank.set_scope(self._channels)
        self.channel_bank.title = "LIVE: %7.2fs" % 0
        self.channel_bank.highlight = True

        self.rendering = True
        self.renderer.set_functions(*((cv.chaser, cv.chaser.get_data(cv.selected)) for cv in self.chaser_views if cv.chaser.type == CHASER))
        self.renderer.start()

        self.input.context = self.context_run

        return "Started running"

    def chaser_run(self):
        self.current_cv.highlight = False
        self.renderer.clear_hold()
        self.chaser_stack.append([c.selected for c in self.chaser_views])
        self.handle_show(None)
        self.base_run()

    def run_exit(self):
        self.rendering = False
        self.renderer.stop()

        with self.w_lock:
            self.channel_bank.title = "Channels"
            self.channel_bank.highlight = False
            self.channel_bank.set_scope(())

            if self.chaser_stack:
                for i, c in zip(self.chaser_stack.pop(-1), self.chaser_views):
                    c.selected = i
                self.current_cv.highlight = True
                if self.current_cv.selected is not None:
                    s = self.current_cv.chaser.steps[self.current_cv.selected]
                    if s.function is not None and s.function.type in (SCENE, AUDIO,):
                        self.handle_show(s.function)
                self.input.context = self.context_chaser
            else:
                self.input.context = self.context_base

                for cv in self.chaser_views:
                    cv.selected = None

    def run_jump(self, n, p, index):
        if not index:
            n = [j for j, i in enumerate(self.chaser_views) if i.chaser.id == n]
            if not n:
                return "No such chaser loaded"
            n = n[0]
        if n >= len(self.chaser_views) or n < 0:
            return "Chaser index out of range"
        if p is not None and (p < 1 or p > len(self.chaser_views[n].chaser.steps)):
            return "Step index out of range"
        self.renderer.advance((n, (p-1) if p is not None else p))

    def run_advance_all(self):
        for n in range(len(self.chaser_views)):
            self.renderer.advance((n, None))

    def current_status(self):
        self.pager.display_many((
            "CURRENT STATUS",
            "Output is %sOK: %s" % ("" if self.output.ok else "NOT ", self.output.status),
        ), True)

    def chaser_info(self, n):
        with self.w_lock:
            if n is None:
                n = self.current_cv.selected 
                if n is None:
                    return "No step selected"
            else:
                n -= 1
                if n < 0 or n >= len(self.current_cv.chaser.steps):
                    return "Invalid step"
            f = self.current_cv.chaser.steps[n]
            self.pager.display_many((
                f.name,
                "-  Fade in: %7.3f" % (f.fade_in/1000),
                "- Duration: " + (("%7.3f" % (f.duration/1000)) if f.duration != INFTY else "infty"),
                "- Fade out: %7.3f" % (f.fade_out/1000),
            ))

    def chaser_mode(self, mode):
        with self.w_lock:
            self.current_cv.chaser.advance_mode = mode
            self.current_cv.set_chaser(self.current_cv.chaser, self.current_cv.selected)

    def base_copy(self, name, num):
        with self.w_lock:
            ## TODO: Implement this in BLC
            if num not in self.w.functions:
                return "No such function"
            f = self.w.functions[num]
            if f.type != SCENE:
                return "Can only close scenes"
            f2 = Scene(self.w, name=name, values=f.values)
            self.handle_enter(f2.id)

    def __init__(self, path, output):
        ## Have to do most of the actual initialization in the main method, as curses isn't
        ## ready yet.
        self.channel_bank = None
        self.input = None
        self.pager = None
        self.stdscr = None

        self.primitive = None
        self.chaser = None
        self.current_cv = None

        self.path = path
        self._w_created = False
        if path is None or not os.path.isfile(path):
            self.w = Workspace("", "", 0, dt.datetime.now())
            self._w_created = True
        else:
            self.w = Workspace.load(path)

        self.w_lock = threading.RLock()

        self.chaser_views = []

        self.chaser_stack = []

        self.context_base = Input.parse_context((
            ("edit $num", self.handle_enter),
            ("edit", lambda: self.handle_enter(None)),
            ("delete $num", self.base_delete),
            ("new scene $quoted_string", lambda n: self.base_new(n, Scene)),
            ("new chaser $quoted_string", lambda n: self.base_new(n, Chaser)),
            ("new audio $quoted_string", lambda n: self.base_new(n, Audio)),
            ("new join $quoted_string", lambda n: self.base_new(n, Join)),

            ("add $num", self.base_add),
            ("subtract $letter", lambda n: self.base_remove(n, True)),
            ("subtract $num", lambda n: self.base_remove(n, False)),

            ("run", self.base_run),

            ("write", self.base_save),
            ("write $quoted_string", self.base_save),

            ("list fixtures", self.list_fixtures),
            ("list scenes", lambda: self.list_functions(SCENE)),
            ("list chasers", lambda: self.list_functions(CHASER)),
            ("list audio", lambda: self.list_functions(AUDIO)),
            ("list joins", lambda: self.list_functions(JOIN)),

            ("new scene $quoted_string from $num", self.base_copy),

            ("currentstatus", self.current_status),

            ("pager page", self.page), 
            ("pager clear", self.page_clear),
            ("verify audio", self.verify_audio),
            ("quit", self.base_quit),
        ), {
            None: "This is the base edit mode for editing functions.",
            "edit": "Edit the specified function.",
            "delete": "Delete the specified function.",
            "new": "Create a new function of the given type.",
            "write": "Save the workspace. The path is implicitly the one loaded from if not given.",
            "list": "List available fixtures or functions.",
            "pager": "Control the pager. In page mode, arrow keys, page up/down, home/end, and j/k can be used to scroll, q exits.",
            "quit": "Exit BLC.",
            "remove": "Remove the specified chaser from the display. If a letter is used, remove the chaser at the given position, where 'a' is the left-most chaser and so forth. If a number is used, treat it as a chaser ID.",
            "add": "Add a chaser to the display. Currently a limit of 2 visible.",
            "currentstatus" : "Display information about the system's current status."
        }, self.help)

        self.context_chaser = Input.parse_context((
            ("choose $num", self.chaser_select),

            ("edit", self.chaser_edit),
            ("edit $num", self.chaser_edit),

            ("delete", self.chaser_delete),
            ("delete $num", self.chaser_delete),

            ("append $quoted_string", lambda n: self.chaser_new(-1, n)),
            ("append $quoted_string from $num", lambda n, s: self.chaser_new(-1, n, s)),
            ("append $quoted_string from new scene $quoted_string", lambda n, s: self.chaser_new_new(-1, n, s, Scene)),
            ("append $quoted_string from new audio $quoted_string", lambda n, s: self.chaser_new_new(-1, n, s, Audio)),

            ("append", lambda: self.chaser_new(-1, "")),
            ("append from $num", lambda s: self.chaser_new(-1, "", s)),
            ("append from new scene $quoted_string", lambda s: self.chaser_new_new(-1, "", s, Scene)),
            ("append from new audio $quoted_string", lambda s: self.chaser_new_new(-1, "", s, Audio)),

            ("new $num $quoted_string", self.chaser_new),
            ("new $num $quoted_string from $num", self.chaser_new),
            ("new $num $quoted_string from new scene $quoted_string", lambda i, n, s: self.chaser_new_new(i, n, s, Scene)),
            ("new $num $quoted_string from new audio $quoted_string", lambda i, n, s: self.chaser_new_new(i, n, s, Audio)),

            ("rename $quoted_string", self.chaser_rename),
            ("rename chaser $quoted_string", self.chaser_rename_self),
            ("move $num to $num", self.chaser_move),
            ("move $num", self.chaser_move),
            ("set fade in $time", self.chaser_fade),
            ("set fade out $time", lambda t: self.chaser_fade(t, True)),
            ("set mode oneshot", lambda: self.chaser_mode(ONESHOT)),
            ("set mode loop", lambda: self.chaser_mode(LOOP)),
            ("set mode random", lambda: self.chaser_mode(RANDOM)),
            ("set length $time", self.chaser_duration),
            ("unbind", self.chaser_unset),
            ("bind $num", self.chaser_bind),

            ("list fixtures", self.list_fixtures),
            ("list scenes", lambda: self.list_functions(SCENE)),
            ("list chasers", lambda: self.list_functions(CHASER)),
            ("list audio", lambda: self.list_functions(AUDIO)),
            ("list joins", lambda: self.list_functions(JOIN)),

            ("info", lambda: self.chaser_info(None)),
            ("info $num", self.chaser_info),

            ("trailer", self.chaser_run),

            ("pager page", self.page), 
            ("pager clear", self.page_clear),
            ("verify audio", self.verify_audio),
            ("quit", self.handle_exit),
        ), {
            None: "This mode is for editing chasers. All functions (excepting select) which take a number as an argument may be called without to act on the currently selected step.",
            "edit": "Edit the given step.",
            "select": "Select a step.",
            "fade": "Change the fade durations for the selected step.",
            "length": "Set the duration for the selected step.",
            "new": "Create a new step at the given position with the given name. Can also create a new function to use for the scene. Immediately enters edit mode for that step.",
            "delete": "Remove the given step.",
            "rename": "Rename the current step.",
            "append": "Create a new step at the end of the chaser. See 'new' for details.",
            "unset": "Unset the duration of the selected step, inheriting the step's duration from its function.",
            "pager": "Control the pager. In page mode, arrow keys, page up/down, home/end, and j/k can be used to scroll, q exits.",
            "move": "Move the given step to the given position",
            "bind": "Bind the step to the given function.",
            "quit": "Return to the previous mode.", 
            "trailer": "Preview the chaser in run mode.",
        }, self.help)

        self.context_scene = Input.parse_context((
            ("set $channel_range to $value", self.scene_set),
            ("clear $channel_range", self.scene_clear),

            ("edit $channel_range", self.scene_edit),
            ("edit $channel_range force", lambda cr: self.scene_edit(cr, True)),

            ("list fixtures", self.list_fixtures),
            ("list scenes", lambda: self.list_functions(SCENE)),
            ("list chasers", lambda: self.list_functions(CHASER)),
            ("list audio", lambda: self.list_functions(AUDIO)),
            ("list joins", lambda: self.list_functions(JOIN)),

            ("rename $quoted_string", self.primitive_rename),

            ("pager page", self.page), 
            ("pager clear", self.page_clear),
            ("quit", self.scene_exit),
        ), {
            None: "This mode is for editing scene primitives for fixed lighting.",
            "set": "Set the given channel range to the given value (0 <= value <= 255).",
            "reset": "Remove the given channel range from the scene",
            "list": "List available fixtures or functions.",
            "pager": "Control the pager. In page mode, arrow keys, page up/down, home/end, and j/k can be used to scroll, q exits.",
            "edit": "Live edit scenes using the arrow keys, page up/down, home/end, and h/j/k/l. If \"force\" is used, all matching channels are edited, instead of just the matching ones already in the scope.",
            "quit": "Return to the previous mode.",
        }, self.help)

        self.context_audio = Input.parse_context((
            ("list fixtures", self.list_fixtures),
            ("list scenes", lambda: self.list_functions(SCENE)),
            ("list chasers", lambda: self.list_functions(CHASER)),

            ("rename $quoted_string", self.primitive_rename),

            ("filename $quoted_string", self.audio_filename),
            ("set fade in $time", self.audio_fade),
            ("set fade out $time", lambda t: self.audio_fade(t, True)),

            ("pager page", self.page), 
            ("pager clear", self.page_clear),
            ("verify audio", self.verify_audio),
            ("quit", self.audio_exit),
        ), {
            None: "This mode is for editing audio primitives for single audio files.",
            "list": "List available fixtures or functions.",
            "rename": "Rename the function.",
            "filename": "Set the filename for the function.",
            "fade": "Set fade times for the function. Note that unlike lighting, fade out is done during the audio's run.",
            "pager": "Control the pager. In page mode, arrow keys, page up/down, home/end, and j/k can be used to scroll, q exits.",
            "quit": "Return to the previous mode.",
        }, self.help)

        self.context_run = Input.parse_context((
            ("quit", self.run_exit),

            ("jump $letter to $num", lambda n, p: self.run_jump(n, p, True)),
            ("jump $num to $num", lambda n, p: self.run_jump(n, p, False)),

            ("advance $letter", lambda n: self.run_jump(n, None, True)),
            ("advance $num", lambda n: self.run_jump(n, None, False)),
            ("advance", lambda: self.run_jump(0, None, True)),
            ("badvance", lambda: self.run_jump(1, None, True)),
            ("", lambda: self.run_jump(0, None, True)),

            ("everythingadvance", self.run_advance_all),

            ("currentstatus", self.current_status),
        ), {
            None: "This mode is for running a show or previewing a chaser.",
            "quit": "Return to the previous mode.",
            "jump": "Jump the given chaser to the given location.",
            "advance": "Advance a chaser a single step. If no letter is given, this is the first from the left.",
            "badvance": "Advance the second chaser from the left a single step.",
            "everythingadvance": "Advance all chasers a single step each.",
            "currentstatus" : "Display information about the system's current status."
        }, self.help)

        self.context_join = Input.parse_context((
            ("choose $num", self.chaser_select),

            ("edit", self.chaser_edit),
            ("edit $num", self.chaser_edit),

            ("delete", self.chaser_delete),
            ("delete $num", self.chaser_delete),

            ("add $num", lambda s: self.chaser_new(-1, "", s)),

            ("rename $quoted_string", self.chaser_rename_self),

            ("list fixtures", self.list_fixtures),
            ("list scenes", lambda: self.list_functions(SCENE)),
            ("list chasers", lambda: self.list_functions(CHASER)),
            ("list audio", lambda: self.list_functions(AUDIO)),
            ("list joins", lambda: self.list_functions(JOIN)),

            ("pager page", self.page), 
            ("pager clear", self.page_clear),
            ("verify audio", self.verify_audio),
            ("quit", self.handle_exit),
        ))

        self.output = output
        self.renderer = Renderer(self.w, self.w_lock, self.output, self._render_callback)
        self.rendering = False

        self._channels = sum((tuple(f.channels) for f in sorted(self.w.fixtures.values(), key=lambda i: i.id)), ())