forked from hundredrabbits/Orca-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui_main.c
3898 lines (3758 loc) · 128 KB
/
tui_main.c
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
#include "base.h"
#include "field.h"
#include "gbuffer.h"
#include "osc_out.h"
#include "oso.h"
#include "sim.h"
#include "sysmisc.h"
#include "term_util.h"
#include "vmio.h"
#include <getopt.h>
#include <locale.h>
#define SOKOL_IMPL
#include "sokol_time.h"
#undef SOKOL_IMPL
#ifdef FEAT_PORTMIDI
#include <portmidi.h>
#endif
#define TIME_DEBUG 0
#if TIME_DEBUG
static int spin_track_timeout = 0;
#endif
#define staticni ORCA_NOINLINE static
staticni void usage(void) { // clang-format off
fprintf(stderr,
"Usage: orca [options] [file]\n\n"
"General options:\n"
" --undo-limit <number> Set the maximum number of undo steps.\n"
" If you plan to work with large files,\n"
" set this to a low number.\n"
" Default: 100\n"
" --initial-size <nxn> When creating a new grid file, use these\n"
" starting dimensions.\n"
" --bpm <number> Set the tempo (beats per minute).\n"
" Default: 120\n"
" --seed <number> Set the seed for the random function.\n"
" Default: 1\n"
" -h or --help Print this message and exit.\n"
"\n"
"OSC/MIDI options:\n"
" --strict-timing\n"
" Attempt to reduce timing jitter of outgoing MIDI and OSC\n"
" messages. Uses more CPU time. May have no effect.\n"
"\n"
" --osc-midi-bidule <path>\n"
" Set MIDI to be sent via OSC formatted for Plogue Bidule.\n"
" The path argument is the path of the Plogue OSC MIDI device.\n"
" Example: /OSC_MIDI_0/MIDI\n"
);} // clang-format on
typedef enum {
Glyph_class_unknown,
Glyph_class_grid,
Glyph_class_comment,
Glyph_class_uppercase,
Glyph_class_lowercase,
Glyph_class_movement,
Glyph_class_numeric,
Glyph_class_bang,
} Glyph_class;
static Glyph_class glyph_class_of(Glyph glyph) {
if (glyph == '.')
return Glyph_class_grid;
if (glyph >= '0' && glyph <= '9')
return Glyph_class_numeric;
switch (glyph) {
case 'N':
case 'n':
case 'E':
case 'e':
case 'S':
case 's':
case 'W':
case 'w':
case 'Z':
case 'z':
return Glyph_class_movement;
case '!':
case ':':
case ';':
case '=':
case '%':
case '?':
return Glyph_class_lowercase;
case '*':
return Glyph_class_bang;
case '#':
return Glyph_class_comment;
}
if (glyph >= 'A' && glyph <= 'Z')
return Glyph_class_uppercase;
if (glyph >= 'a' && glyph <= 'z')
return Glyph_class_lowercase;
return Glyph_class_unknown;
}
static attr_t term_attrs_of_cell(Glyph g, Mark m) {
Glyph_class gclass = glyph_class_of(g);
attr_t attr = A_normal;
switch (gclass) {
case Glyph_class_unknown:
attr = A_bold | fg_bg(C_red, C_natural);
break;
case Glyph_class_grid:
attr = A_bold | fg_bg(C_black, C_natural);
break;
case Glyph_class_comment:
attr = A_dim | Cdef_normal;
break;
case Glyph_class_uppercase:
attr = A_normal | fg_bg(C_black, C_cyan);
break;
case Glyph_class_lowercase:
case Glyph_class_movement:
case Glyph_class_numeric:
attr = A_bold | Cdef_normal;
break;
case Glyph_class_bang:
attr = A_bold | Cdef_normal;
break;
}
if (gclass != Glyph_class_comment) {
if ((m & (Mark_flag_lock | Mark_flag_input)) ==
(Mark_flag_lock | Mark_flag_input)) {
// Standard locking input
attr = A_normal | Cdef_normal;
} else if ((m & Mark_flag_input) == Mark_flag_input) {
// Non-locking input
attr = A_normal | Cdef_normal;
} else if (m & Mark_flag_lock) {
// Locked only
attr = A_dim | Cdef_normal;
}
}
if (m & Mark_flag_output) {
attr = A_reverse;
}
if (m & Mark_flag_haste_input) {
attr = A_bold | fg_bg(C_cyan, C_natural);
}
return attr;
}
typedef enum {
Ged_input_mode_normal = 0,
Ged_input_mode_append,
Ged_input_mode_selresize,
Ged_input_mode_slide,
} Ged_input_mode;
typedef struct {
Usz y, x, h, w;
} Ged_cursor;
void ged_cursor_init(Ged_cursor *tc) {
tc->x = tc->y = 0;
tc->w = tc->h = 1;
}
static void ged_cursor_move_relative(Ged_cursor *tc, Usz field_h, Usz field_w,
Isz delta_y, Isz delta_x) {
Isz y0 = (Isz)tc->y + delta_y;
Isz x0 = (Isz)tc->x + delta_x;
if (y0 >= (Isz)field_h)
y0 = (Isz)field_h - 1;
if (y0 < 0)
y0 = 0;
if (x0 >= (Isz)field_w)
x0 = (Isz)field_w - 1;
if (x0 < 0)
x0 = 0;
tc->y = (Usz)y0;
tc->x = (Usz)x0;
}
staticni void draw_grid_cursor(WINDOW *win, int draw_y, int draw_x, int draw_h,
int draw_w, Glyph const *gbuffer, Usz field_h,
Usz field_w, int scroll_y, int scroll_x,
Usz cursor_y, Usz cursor_x, Usz cursor_h,
Usz cursor_w, Ged_input_mode input_mode,
bool is_playing) {
(void)input_mode;
if (cursor_y >= field_h || cursor_x >= field_w)
return;
if (scroll_y < 0) {
draw_y += -scroll_y;
scroll_y = 0;
}
if (scroll_x < 0) {
draw_x += -scroll_x;
scroll_x = 0;
}
Usz offset_y = (Usz)scroll_y;
Usz offset_x = (Usz)scroll_x;
if (offset_y >= field_h || offset_x >= field_w)
return;
if (draw_y >= draw_h || draw_x >= draw_w)
return;
attr_t const curs_attr = A_reverse | A_bold | fg_bg(C_yellow, C_natural);
if (offset_y <= cursor_y && offset_x <= cursor_x) {
Usz cdraw_y = cursor_y - offset_y + (Usz)draw_y;
Usz cdraw_x = cursor_x - offset_x + (Usz)draw_x;
if (cdraw_y < (Usz)draw_h && cdraw_x < (Usz)draw_w) {
Glyph beneath = gbuffer[cursor_y * field_w + cursor_x];
char displayed;
if (beneath == '.') {
displayed = is_playing ? '@' : '~';
} else {
displayed = beneath;
}
chtype ch = (chtype)displayed | curs_attr;
wmove(win, (int)cdraw_y, (int)cdraw_x);
waddchnstr(win, &ch, 1);
}
}
// Early out for selection area that won't have any visual effect
if (cursor_h <= 1 && cursor_w <= 1)
return;
// Now mutate visually selected area under grid to have the selection color
// attributes. (This will rewrite the attributes on the cursor character we
// wrote above, but if it was the only character that would have been
// changed, we already early-outed.)
//
// We'll do this by reading back the characters on the grid from the curses
// window buffer, changing the attributes, then writing it back. This is
// easier than pulling the glyphs from the gbuffer, since we already did the
// ruler calculations to turn . into +, and we don't need special behavior
// for any other attributes (e.g. we don't show a special state for selected
// uppercase characters.)
//
// First, confine cursor selection to the grid field/gbuffer that actually
// exists, in case the cursor selection exceeds the area of the field.
Usz sel_rows = field_h - cursor_y;
if (cursor_h < sel_rows)
sel_rows = cursor_h;
Usz sel_cols = field_w - cursor_x;
if (cursor_w < sel_cols)
sel_cols = cursor_w;
// Now, confine the selection area to what's visible on screen. Kind of
// tricky since we have to handle it being partially visible from any edge on
// any axis, and we have to be mindful overflow.
Usz vis_sel_y;
Usz vis_sel_x;
if (offset_y > cursor_y) {
vis_sel_y = 0;
Usz sub_y = offset_y - cursor_y;
if (sub_y > sel_rows)
sel_rows = 0;
else
sel_rows -= sub_y;
} else {
vis_sel_y = cursor_y - offset_y;
}
if (offset_x > cursor_x) {
vis_sel_x = 0;
Usz sub_x = offset_x - cursor_x;
if (sub_x > sel_cols)
sel_cols = 0;
else
sel_cols -= sub_x;
} else {
vis_sel_x = cursor_x - offset_x;
}
vis_sel_y += (Usz)draw_y;
vis_sel_x += (Usz)draw_x;
if (vis_sel_y >= (Usz)draw_h || vis_sel_x >= (Usz)draw_w)
return;
Usz vis_sel_h = (Usz)draw_h - vis_sel_y;
Usz vis_sel_w = (Usz)draw_w - vis_sel_x;
if (sel_rows < vis_sel_h)
vis_sel_h = sel_rows;
if (sel_cols < vis_sel_w)
vis_sel_w = sel_cols;
if (vis_sel_w == 0 || vis_sel_h == 0)
return;
enum { Bufcount = 4096 };
chtype chbuffer[Bufcount];
if (Bufcount < vis_sel_w)
vis_sel_w = Bufcount;
for (Usz iy = 0; iy < vis_sel_h; ++iy) {
int at_y = (int)(vis_sel_y + iy);
int num = mvwinchnstr(win, at_y, (int)vis_sel_x, chbuffer, (int)vis_sel_w);
for (int ix = 0; ix < num; ++ix) {
chbuffer[ix] = (chtype)((chbuffer[ix] & (A_CHARTEXT | A_ALTCHARSET)) |
(chtype)curs_attr);
}
waddchnstr(win, chbuffer, (int)num);
}
}
typedef struct Undo_node {
Field field;
Usz tick_num;
struct Undo_node *prev, *next;
} Undo_node;
typedef struct {
Undo_node *first, *last;
Usz count, limit;
} Undo_history;
static void undo_history_init(Undo_history *hist, Usz limit) {
*hist = (Undo_history){0};
hist->limit = limit;
}
static void undo_history_deinit(Undo_history *hist) {
Undo_node *a = hist->first;
while (a) {
Undo_node *b = a->next;
field_deinit(&a->field);
free(a);
a = b;
}
}
staticni bool undo_history_push(Undo_history *hist, Field *field,
Usz tick_num) {
if (hist->limit == 0)
return false;
Undo_node *new_node;
if (hist->count == hist->limit) {
new_node = hist->first;
if (new_node == hist->last) {
hist->first = NULL;
hist->last = NULL;
} else {
hist->first = new_node->next;
hist->first->prev = NULL;
}
} else {
new_node = malloc(sizeof(Undo_node));
if (!new_node)
return false;
++hist->count;
field_init(&new_node->field);
}
field_copy(field, &new_node->field);
new_node->tick_num = tick_num;
if (hist->last) {
hist->last->next = new_node;
new_node->prev = hist->last;
} else {
hist->first = new_node;
hist->last = new_node;
new_node->prev = NULL;
}
new_node->next = NULL;
hist->last = new_node;
return true;
}
staticni void undo_history_pop(Undo_history *hist, Field *out_field,
Usz *out_tick_num) {
Undo_node *last = hist->last;
if (!last)
return;
field_copy(&last->field, out_field);
*out_tick_num = last->tick_num;
if (hist->first == last) {
hist->first = NULL;
hist->last = NULL;
} else {
Undo_node *new_last = last->prev;
new_last->next = NULL;
hist->last = new_last;
}
field_deinit(&last->field);
free(last);
--hist->count;
}
staticni void undo_history_apply(Undo_history *hist, Field *out_field,
Usz *out_tick_num) {
Undo_node *last = hist->last;
if (!last)
return;
field_copy(&last->field, out_field);
*out_tick_num = last->tick_num;
}
static Usz undo_history_count(Undo_history *hist) { return hist->count; }
staticni void print_activity_indicator(WINDOW *win, Usz activity_counter) {
// 7 segments that can each light up as Colors different colors.
// This gives us Colors^Segments total configurations.
enum { Segments = 7, Colors = 4 };
Usz states = 1; // calculate Colors^Segments
for (Usz i = 0; i < Segments; ++i)
states *= Colors;
// Wrap the counter to the range of displayable configurations.
Usz val = activity_counter % states;
chtype lamps[Colors];
#if 1 // Appearance where segments are always lit
lamps[0] = ACS_HLINE | fg_bg(C_black, C_natural) | A_bold;
lamps[1] = ACS_HLINE | fg_bg(C_white, C_natural) | A_normal;
lamps[2] = ACS_HLINE | A_bold;
lamps[3] = lamps[1];
#elif 0 // Brighter appearance where segments are always lit
lamps[0] = ACS_HLINE | fg_bg(C_black, C_natural) | A_bold;
lamps[1] = ACS_HLINE | A_normal;
lamps[2] = ACS_HLINE | A_bold;
lamps[3] = lamps[1];
#else // Appearance where segments can turn off completely
lamps[0] = ' ';
lamps[1] = ACS_HLINE | fg_bg(C_black, C_natural) | A_bold;
lamps[2] = ACS_HLINE | A_normal;
lamps[3] = lamps[1];
#endif
chtype buffer[Segments];
for (Usz i = 0; i < Segments; ++i) {
// Instead of a left-to-right, straightforward ascending least-to-most
// significant digits display, we'll display it as a spiral.
Usz j = i % 2 ? (6 - i / 2) : (i / 2);
buffer[j] = lamps[val % Colors];
val = val / Colors;
}
waddchnstr(win, buffer, Segments);
// If you want to see what various combinations of colors and attributes look
// like in different terminals.
#if 0
waddch(win, 'a' | fg_bg(C_black, C_natural) | A_dim);
waddch(win, 'b' | fg_bg(C_black, C_natural) | A_normal);
waddch(win, 'c' | fg_bg(C_black, C_natural) | A_bold);
waddch(win, 'd' | A_dim);
waddch(win, 'e' | A_normal);
waddch(win, 'f' | A_bold);
waddch(win, 'g' | fg_bg(C_white, C_natural) | A_dim);
waddch(win, 'h' | fg_bg(C_white, C_natural) | A_normal);
waddch(win, 'i' | fg_bg(C_white, C_natural) | A_bold);
#endif
}
staticni void advance_faketab(WINDOW *win, int offset_x, int tabstop) {
if (tabstop < 1)
return;
int y, x, h, w;
getyx(win, y, x);
getmaxyx(win, h, w);
(void)h;
x = ((x + tabstop - 1) / tabstop) * tabstop + offset_x % tabstop;
if (w < 1)
w = 1;
if (x >= w)
x = w - 1;
wmove(win, y, x);
}
staticni void draw_hud(WINDOW *win, int win_y, int win_x, int height, int width,
char const *filename, Usz field_h, Usz field_w,
Usz ruler_spacing_y, Usz ruler_spacing_x, Usz tick_num,
Usz bpm, Ged_cursor const *ged_cursor,
Ged_input_mode input_mode, Usz activity_counter) {
(void)height;
(void)width;
enum { Tabstop = 8 };
wmove(win, win_y, win_x);
wprintw(win, "%zux%zu", field_w, field_h);
advance_faketab(win, win_x, Tabstop);
wprintw(win, "%zu/%zu", ruler_spacing_x, ruler_spacing_y);
advance_faketab(win, win_x, Tabstop);
wprintw(win, "%zuf", tick_num);
advance_faketab(win, win_x, Tabstop);
wprintw(win, "%zu", bpm);
advance_faketab(win, win_x, Tabstop);
print_activity_indicator(win, activity_counter);
wmove(win, win_y + 1, win_x);
wprintw(win, "%zu,%zu", ged_cursor->x, ged_cursor->y);
advance_faketab(win, win_x, Tabstop);
wprintw(win, "%zu:%zu", ged_cursor->w, ged_cursor->h);
advance_faketab(win, win_x, Tabstop);
switch (input_mode) {
case Ged_input_mode_normal:
wattrset(win, A_normal);
waddstr(win, "insert");
break;
case Ged_input_mode_append:
wattrset(win, A_bold);
waddstr(win, "append");
break;
case Ged_input_mode_selresize:
wattrset(win, A_bold);
waddstr(win, "select");
break;
case Ged_input_mode_slide:
wattrset(win, A_reverse);
waddstr(win, "slide");
break;
}
advance_faketab(win, win_x, Tabstop);
wattrset(win, A_normal);
waddstr(win, filename);
}
staticni void draw_glyphs_grid(WINDOW *win, int draw_y, int draw_x, int draw_h,
int draw_w, Glyph const *restrict gbuffer,
Mark const *restrict mbuffer, Usz field_h,
Usz field_w, Usz offset_y, Usz offset_x,
Usz ruler_spacing_y, Usz ruler_spacing_x,
bool use_fancy_dots, bool use_fancy_rulers) {
assert(draw_y >= 0 && draw_x >= 0);
assert(draw_h >= 0 && draw_w >= 0);
enum { Bufcount = 4096 };
chtype chbuffer[Bufcount];
// todo buffer limit
if (offset_y >= field_h || offset_x >= field_w)
return;
if (draw_y >= draw_h || draw_x >= draw_w)
return;
Usz rows = (Usz)(draw_h - draw_y);
if (field_h - offset_y < rows)
rows = field_h - offset_y;
Usz cols = (Usz)(draw_w - draw_x);
if (field_w - offset_x < cols)
cols = field_w - offset_x;
if (Bufcount < cols)
cols = Bufcount;
if (rows == 0 || cols == 0)
return;
bool use_rulers = ruler_spacing_y != 0 && ruler_spacing_x != 0;
chtype bullet = use_fancy_dots ? ACS_BULLET : '.';
enum { T = 1 << 0, B = 1 << 1, L = 1 << 2, R = 1 << 3 };
chtype rs[(T | B | L | R) + 1];
if (use_rulers) {
for (Usz i = 0; i < sizeof rs / sizeof(chtype); ++i)
rs[i] = '+';
if (use_fancy_rulers) {
rs[T | L] = ACS_ULCORNER;
rs[T | R] = ACS_URCORNER;
rs[B | L] = ACS_LLCORNER;
rs[B | R] = ACS_LRCORNER;
rs[T] = ACS_TTEE;
rs[B] = ACS_BTEE;
rs[L] = ACS_LTEE;
rs[R] = ACS_RTEE;
}
}
for (Usz iy = 0; iy < rows; ++iy) {
Usz line_offset = (offset_y + iy) * field_w + offset_x;
Glyph const *g_row = gbuffer + line_offset;
Mark const *m_row = mbuffer + line_offset;
bool use_y_ruler = use_rulers && (iy + offset_y) % ruler_spacing_y == 0;
for (Usz ix = 0; ix < cols; ++ix) {
Glyph g = g_row[ix];
Mark m = m_row[ix];
chtype ch;
if (g == '.') {
if (use_y_ruler && (ix + offset_x) % ruler_spacing_x == 0) {
int p = 0; // clang-format off
if (iy + offset_y == 0 ) p |= T;
if (iy + offset_y + 1 == field_h) p |= B;
if (ix + offset_x == 0 ) p |= L;
if (ix + offset_x + 1 == field_w) p |= R;
ch = rs[p]; // clang-format on
} else {
ch = bullet;
}
} else {
ch = (chtype)g;
}
attr_t attrs = term_attrs_of_cell(g, m);
chbuffer[ix] = ch | attrs;
}
wmove(win, draw_y + (int)iy, draw_x);
waddchnstr(win, chbuffer, (int)cols);
}
}
staticni void draw_glyphs_grid_scrolled(
WINDOW *win, int draw_y, int draw_x, int draw_h, int draw_w,
Glyph const *restrict gbuffer, Mark const *restrict mbuffer, Usz field_h,
Usz field_w, int scroll_y, int scroll_x, Usz ruler_spacing_y,
Usz ruler_spacing_x, bool use_fancy_dots, bool use_fancy_rulers) {
if (scroll_y < 0) {
draw_y += -scroll_y;
scroll_y = 0;
}
if (scroll_x < 0) {
draw_x += -scroll_x;
scroll_x = 0;
}
draw_glyphs_grid(win, draw_y, draw_x, draw_h, draw_w, gbuffer, mbuffer,
field_h, field_w, (Usz)scroll_y, (Usz)scroll_x,
ruler_spacing_y, ruler_spacing_x, use_fancy_dots,
use_fancy_rulers);
}
static void ged_cursor_confine(Ged_cursor *tc, Usz height, Usz width) {
if (height == 0 || width == 0)
return;
if (tc->y >= height)
tc->y = height - 1;
if (tc->x >= width)
tc->x = width - 1;
}
staticni void draw_oevent_list(WINDOW *win, Oevent_list const *oevent_list) {
wmove(win, 0, 0);
int win_h = getmaxy(win);
wprintw(win, "Count: %d", (int)oevent_list->count);
for (Usz i = 0, num_events = oevent_list->count; i < num_events; ++i) {
int cury = getcury(win);
if (cury + 1 >= win_h)
return;
wmove(win, cury + 1, 0);
Oevent const *ev = oevent_list->buffer + i;
Oevent_types evt = ev->any.oevent_type;
switch (evt) {
case Oevent_type_midi_note: {
Oevent_midi_note const *em = &ev->midi_note;
wprintw(
win,
"MIDI Note\tchannel %d\toctave %d\tnote %d\tvelocity %d\tlength %d",
(int)em->channel, (int)em->octave, (int)em->note, (int)em->velocity,
(int)em->duration);
break;
}
case Oevent_type_midi_cc: {
Oevent_midi_cc const *ec = &ev->midi_cc;
wprintw(win, "MIDI CC\tchannel %d\tcontrol %d\tvalue %d",
(int)ec->channel, (int)ec->control, (int)ec->value);
break;
}
case Oevent_type_midi_pb: {
Oevent_midi_pb const *ep = &ev->midi_pb;
wprintw(win, "MIDI PB\tchannel %d\tmsb %d\tlsb %d", (int)ep->channel,
(int)ep->msb, (int)ep->lsb);
break;
}
case Oevent_type_osc_ints: {
Oevent_osc_ints const *eo = &ev->osc_ints;
wprintw(win, "OSC\t%c\tcount: %d ", eo->glyph, eo->count, eo->count);
waddch(win, ACS_VLINE);
for (Usz j = 0; j < eo->count; ++j) {
wprintw(win, " %d", eo->numbers[j]);
}
break;
}
case Oevent_type_udp_string: {
Oevent_udp_string const *eo = &ev->udp_string;
wprintw(win, "UDP\tcount %d\t", (int)eo->count);
for (Usz j = 0; j < (Usz)eo->count; ++j) {
waddch(win, (chtype)eo->chars[j]);
}
break;
}
}
}
}
staticni void ged_resize_grid(Field *field, Mbuf_reusable *mbr, Usz new_height,
Usz new_width, Usz tick_num, Field *scratch_field,
Undo_history *undo_hist, Ged_cursor *ged_cursor) {
assert(new_height > 0 && new_width > 0);
undo_history_push(undo_hist, field, tick_num);
field_copy(field, scratch_field);
field_resize_raw(field, new_height, new_width);
// junky copies until i write a smarter thing
memset(field->buffer, '.', new_height * new_width * sizeof(Glyph));
gbuffer_copy_subrect(scratch_field->buffer, field->buffer,
scratch_field->height, scratch_field->width,
field->height, field->width, 0, 0, 0, 0,
scratch_field->height, scratch_field->width);
ged_cursor_confine(ged_cursor, new_height, new_width);
mbuf_reusable_ensure_size(mbr, new_height, new_width);
}
staticni Usz adjust_rulers_humanized(Usz ruler, Usz in, Isz delta_rulers) {
// slightly more confusing because desired grid sizes are +1 (e.g. ruler of
// length 8 wants to snap to 25 and 33, not 24 and 32). also this math is
// sloppy.
assert(ruler > 0);
if (in == 0)
return delta_rulers > 0 ? ruler * (Usz)delta_rulers : 1;
// could overflow if inputs are big
if (delta_rulers < 0)
in += ruler - 1;
Isz n = ((Isz)in - 1) / (Isz)ruler + delta_rulers;
if (n < 0)
n = 0;
return ruler * (Usz)n + 1;
}
// Resizes by number of ruler divisions, and snaps size to closest division in
// a way a human would expect. Adds +1 to the output, so grid resulting size is
// 1 unit longer than the actual ruler length.
staticni bool ged_resize_grid_snap_ruler(Field *field, Mbuf_reusable *mbr,
Usz ruler_y, Usz ruler_x, Isz delta_h,
Isz delta_w, Usz tick_num,
Field *scratch_field,
Undo_history *undo_hist,
Ged_cursor *ged_cursor) {
assert(ruler_y > 0);
assert(ruler_x > 0);
Usz field_h = field->height;
Usz field_w = field->width;
assert(field_h > 0);
assert(field_w > 0);
if (ruler_y == 0 || ruler_x == 0 || field_h == 0 || field_w == 0)
return false;
Usz new_field_h = field_h;
Usz new_field_w = field_w;
if (delta_h != 0)
new_field_h = adjust_rulers_humanized(ruler_y, field_h, delta_h);
if (delta_w != 0)
new_field_w = adjust_rulers_humanized(ruler_x, field_w, delta_w);
if (new_field_h > ORCA_Y_MAX)
new_field_h = ORCA_Y_MAX;
if (new_field_w > ORCA_X_MAX)
new_field_w = ORCA_X_MAX;
if (new_field_h == field_h && new_field_w == field_w)
return false;
ged_resize_grid(field, mbr, new_field_h, new_field_w, tick_num, scratch_field,
undo_hist, ged_cursor);
return true;
}
typedef enum {
Midi_mode_type_null,
Midi_mode_type_osc_bidule,
#ifdef FEAT_PORTMIDI
Midi_mode_type_portmidi,
#endif
} Midi_mode_type;
typedef struct {
Midi_mode_type type;
} Midi_mode_any;
typedef struct {
Midi_mode_type type;
char const *path;
} Midi_mode_osc_bidule;
#ifdef FEAT_PORTMIDI
typedef struct {
Midi_mode_type type;
PmDeviceID device_id;
PortMidiStream *stream;
} Midi_mode_portmidi;
// Not sure whether it's OK to call Pm_Terminate() without having a successful
// call to Pm_Initialize() -- let's just treat it with tweezers.
static bool portmidi_is_initialized = false;
#endif
typedef union {
Midi_mode_any any;
Midi_mode_osc_bidule osc_bidule;
#ifdef FEAT_PORTMIDI
Midi_mode_portmidi portmidi;
#endif
} Midi_mode;
void midi_mode_init_null(Midi_mode *mm) { mm->any.type = Midi_mode_type_null; }
void midi_mode_init_osc_bidule(Midi_mode *mm, char const *path) {
mm->osc_bidule.type = Midi_mode_type_osc_bidule;
mm->osc_bidule.path = path;
}
#ifdef FEAT_PORTMIDI
enum {
Portmidi_artificial_latency = 1,
};
struct {
U64 clock_base;
bool did_init;
} portmidi_global_data;
static PmTimestamp portmidi_timestamp_now(void) {
if (!portmidi_global_data.did_init) {
portmidi_global_data.did_init = true;
portmidi_global_data.clock_base = stm_now();
}
return (PmTimestamp)(stm_ms(stm_since(portmidi_global_data.clock_base)));
}
static PmTimestamp portmidi_timeproc(void *time_info) {
(void)time_info;
return portmidi_timestamp_now();
}
static PmError portmidi_init_if_necessary(void) {
if (portmidi_is_initialized)
return 0;
PmError e = Pm_Initialize();
if (e)
return e;
portmidi_is_initialized = true;
return 0;
}
staticni PmError midi_mode_init_portmidi(Midi_mode *mm, PmDeviceID dev_id) {
PmError e = portmidi_init_if_necessary();
if (e)
goto fail;
e = Pm_OpenOutput(&mm->portmidi.stream, dev_id, NULL, 128, portmidi_timeproc,
NULL, Portmidi_artificial_latency);
if (e)
goto fail;
mm->portmidi.type = Midi_mode_type_portmidi;
mm->portmidi.device_id = dev_id;
return pmNoError;
fail:
midi_mode_init_null(mm);
return e;
}
// Returns true on success. todo currently output only
staticni bool portmidi_find_device_id_by_name(char const *name, Usz namelen,
PmError *out_pmerror,
PmDeviceID *out_id) {
*out_pmerror = portmidi_init_if_necessary();
if (*out_pmerror)
return false;
int num = Pm_CountDevices();
for (int i = 0; i < num; ++i) {
PmDeviceInfo const *info = Pm_GetDeviceInfo(i);
if (!info || !info->output)
continue;
Usz len = strlen(info->name);
if (len != namelen)
continue;
if (strncmp(name, info->name, namelen) == 0) {
*out_id = i;
return true;
}
}
return false;
}
static bool portmidi_find_name_of_device_id(PmDeviceID id, PmError *out_pmerror,
oso **out_name) {
*out_pmerror = portmidi_init_if_necessary();
if (*out_pmerror)
return false;
int num = Pm_CountDevices();
if (id < 0 || id >= num)
return false;
PmDeviceInfo const *info = Pm_GetDeviceInfo(id);
if (!info || !info->output)
return false;
osoput(out_name, info->name);
return true;
}
#endif
staticni void midi_mode_deinit(Midi_mode *mm) {
switch (mm->any.type) {
case Midi_mode_type_null:
case Midi_mode_type_osc_bidule:
break;
#ifdef FEAT_PORTMIDI
case Midi_mode_type_portmidi:
// Because PortMidi seems to work correctly ony more platforms when using
// its timing stuff, we are using it. And because we are using it, and
// because it may be buffering events for sending 'later', we might have
// pending outgoing MIDI events. We'll need to wait until they finish being
// before calling Pm_Close, otherwise users could have problems like MIDI
// notes being stuck on. This is slow and blocking, but not much we can do
// about it right now.
//
// TODO use nansleep on platforms that support it.
for (U64 start = stm_now();
stm_ms(stm_since(start)) <= (double)Portmidi_artificial_latency;)
sleep(0);
Pm_Close(mm->portmidi.stream);
break;
#endif
}
}
typedef struct {
Field field;
Field scratch_field;
Field clipboard_field;
Mbuf_reusable mbuf_r;
Undo_history undo_hist;
Oevent_list oevent_list;
Oevent_list scratch_oevent_list;
Susnote_list susnote_list;
Ged_cursor ged_cursor;
Usz tick_num;
Usz ruler_spacing_y, ruler_spacing_x;
Ged_input_mode input_mode;
Usz bpm;
U64 clock;
double accum_secs;
double time_to_next_note_off;
Oosc_dev *oosc_dev;
Midi_mode midi_mode;
Usz activity_counter;
Usz random_seed;
Usz drag_start_y, drag_start_x;
int win_h, win_w;
int softmargin_y, softmargin_x;
int grid_h;
int grid_scroll_y, grid_scroll_x; // not sure if i like this being int
U8 midi_bclock_sixths; // 0..5, holds 6th of the quarter note step
bool needs_remarking : 1;
bool is_draw_dirty : 1;
bool is_playing : 1;
bool midi_bclock : 1;
bool draw_event_list : 1;
bool is_mouse_down : 1;
bool is_mouse_dragging : 1;
bool is_hud_visible : 1;
} Ged;
static void ged_init(Ged *a, Usz undo_limit, Usz init_bpm, Usz init_seed) {
field_init(&a->field);
field_init(&a->scratch_field);
field_init(&a->clipboard_field);
mbuf_reusable_init(&a->mbuf_r);
undo_history_init(&a->undo_hist, undo_limit);
oevent_list_init(&a->oevent_list);
oevent_list_init(&a->scratch_oevent_list);
susnote_list_init(&a->susnote_list);
ged_cursor_init(&a->ged_cursor);
a->tick_num = 0;
a->ruler_spacing_y = a->ruler_spacing_x = 8;
a->input_mode = Ged_input_mode_normal;
a->bpm = init_bpm;
a->clock = 0;
a->accum_secs = 0.0;
a->time_to_next_note_off = 1.0;
a->oosc_dev = NULL;
midi_mode_init_null(&a->midi_mode);
a->activity_counter = 0;
a->random_seed = init_seed;
a->drag_start_y = a->drag_start_x = 0;
a->win_h = a->win_w = 0;
a->softmargin_y = a->softmargin_x = 0;
a->grid_h = 0;
a->grid_scroll_y = a->grid_scroll_x = 0;
a->midi_bclock_sixths = 0;
a->needs_remarking = true;
a->is_draw_dirty = false;
a->is_playing = false;
a->midi_bclock = false;
a->draw_event_list = false;
a->is_mouse_down = false;
a->is_mouse_dragging = false;
a->is_hud_visible = false;
}
static void ged_deinit(Ged *a) {
field_deinit(&a->field);
field_deinit(&a->scratch_field);
field_deinit(&a->clipboard_field);
mbuf_reusable_deinit(&a->mbuf_r);
undo_history_deinit(&a->undo_hist);
oevent_list_deinit(&a->oevent_list);
oevent_list_deinit(&a->scratch_oevent_list);
susnote_list_deinit(&a->susnote_list);
if (a->oosc_dev)
oosc_dev_destroy(a->oosc_dev);
midi_mode_deinit(&a->midi_mode);
}
static bool ged_is_draw_dirty(Ged *a) {
return a->is_draw_dirty || a->needs_remarking;
}
staticni void send_midi_3bytes(Oosc_dev *oosc_dev, Midi_mode const *midi_mode,
int status, int byte1, int byte2) {
switch (midi_mode->any.type) {
case Midi_mode_type_null:
break;
case Midi_mode_type_osc_bidule: {
if (!oosc_dev)
break;
oosc_send_int32s(oosc_dev, midi_mode->osc_bidule.path,
(int[]){status, byte1, byte2}, 3);
break;
}
#ifdef FEAT_PORTMIDI
case Midi_mode_type_portmidi: {
// timestamp is totally fake, to prevent problems with some MIDI systems
// getting angry if there's no timestamping info.
//
// Eventually, we will want to create real timestamps based on a real orca
// clock, instead of ad-hoc at the last moment like this. When we do that,
// we'll need to thread the timestamping/timing info through the function
// calls, instead of creating it at the last moment here. (This timestamp
// is actually 'useless', because it doesn't convey any additional
// information. But if we don't provide it, at least to PortMidi, some
// people's MIDI setups may malfunction and have terrible timing problems.)
PmTimestamp pm_timestamp = portmidi_timestamp_now();
PmError pme = Pm_WriteShort(midi_mode->portmidi.stream, pm_timestamp,
Pm_Message(status, byte1, byte2));
(void)pme;
break;
}
#endif
}
}
static void send_midi_chan_msg(Oosc_dev *oosc_dev, Midi_mode const *midi_mode,
int type /*0..15*/, int chan /*0.. 15*/,
int byte1 /*0..127*/, int byte2 /*0..127*/) {
send_midi_3bytes(oosc_dev, midi_mode, type << 4 | chan, byte1, byte2);
}