-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidizap.c
1927 lines (1816 loc) · 55.8 KB
/
midizap.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
/*
Copyright 2013 Eric Messick (FixedImagePhoto.com/Contact)
Copyright 2018 Albert Graef <[email protected]>
Based on a version (c) 2006 Trammell Hudson <[email protected]>
which was in turn
Based heavily on code by Arendt David <[email protected]>
*/
#include "midizap.h"
#include "jackdriver.h"
typedef struct input_event EV;
Display *display;
JACK_SEQ seq;
int jack_num_outputs = 0, debug_jack = 0;
int auto_feedback = 1;
int passthrough[2] = {-1, -1}, system_passthrough[2] = {-1, -1};
int shift = 0;
void
initdisplay(void)
{
int event, error, major, minor;
display = XOpenDisplay(0);
if (!display) {
fprintf(stderr, "unable to open X display\n");
exit(1);
}
if (!XTestQueryExtension(display, &event, &error, &major, &minor)) {
fprintf(stderr, "Xtest extensions not supported\n");
XCloseDisplay(display);
exit(1);
}
}
void
send_button(unsigned int button, int press)
{
XTestFakeButtonEvent(display, button, press ? True : False, DELAY);
}
void
send_key(KeySym key, int press)
{
KeyCode keycode;
if (key >= XK_Button_1 && key <= XK_Scroll_Down) {
send_button((unsigned int)key - XK_Button_0, press);
return;
}
keycode = XKeysymToKeycode(display, key);
XTestFakeKeyEvent(display, keycode, press ? True : False, DELAY);
}
// cached controller and pitch bend values
static int16_t notevalue[2][16][128];
static int16_t ccvalue[2][16][128];
static int16_t kpvalue[2][16][128];
static int16_t cpvalue[2][16];
static int16_t pbvalue[2][16] =
{{8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192},
{8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192,
8192, 8192, 8192, 8192, 8192, 8192, 8192, 8192}};
static int dataval(int val, int min, int max)
{
if (!val || val > max)
return max;
else if (val < min)
return min;
else
return val;
}
static int datavals(int val, int step, int *steps, int n_steps)
{
if (val < 0)
return -datavals(-val, step, steps, n_steps);
else if (val < n_steps)
return steps[val];
else if (n_steps)
return steps[n_steps-1];
else if (step)
return step*val;
else
return val;
}
void
handle_event(uint8_t *msg, uint8_t portno, int depth, int recursive);
void
send_midi(uint8_t portno, stroke *s, int index, int dir,
int mod, int mod_step, int mod_n_steps, int *mod_steps,
int val, int depth, uint8_t ret_msg[3])
{
int status = s->status, data = s->data, swap = s->swap,
recursive = s->recursive;
int step = s->step, n_steps = s->n_steps, *steps = s->steps;
if (!recursive && !jack_num_outputs) return; // MIDI output not enabled
uint8_t msg[3];
int chan = status & 0x0f;
msg[0] = status;
msg[1] = data;
switch (status & 0xf0) {
case 0x90:
if (dir) {
// increment (dir==1) or decrement (dir==-1) the current value,
// clamping it to the 0..127 data byte range
if (!step) step = 1;
dir *= step;
if (dir > 0) {
if (notevalue[portno][chan][data] >= 127) return;
notevalue[portno][chan][data] += dir;
if (notevalue[portno][chan][data] > 127) notevalue[portno][chan][data] = 127;
} else {
if (notevalue[portno][chan][data] == 0) return;
notevalue[portno][chan][data] += dir;
if (notevalue[portno][chan][data] < 0) notevalue[portno][chan][data] = 0;
}
msg[2] = notevalue[portno][chan][data];
} else if (mod) {
int q = swap?val%mod:val/mod, r = swap?val/mod:val%mod;
int d = msg[1] + datavals(q, mod_step, mod_steps, mod_n_steps);
int v = datavals(r, step, steps, n_steps);
if (d > 127 || d < 0) return;
if (v > 127 || v < 0) return;
if (s->change) {
if (s->change > 1 && s->d == d && s->v == v) return; // unchanged value
s->d = d; s->v = v; s->change = 2; // >1 => initialized
}
msg[1] = d;
msg[2] = v;
} else if (!index) {
msg[2] = dataval(step, 0, 127);
} else {
msg[2] = 0;
}
break;
case 0xb0:
if (dir) {
if (s->incr) {
// incremental controller, simply spit out a relative sign bit value
if (!step) step = 1;
dir *= step;
if (dir < -63) dir = -63;
if (dir > 63) dir = 63;
msg[2] = dir>0?dir:dir<0?64-dir:0;
} else {
// increment (dir==1) or decrement (dir==-1) the current value,
// clamping it to the 0..127 data byte range
if (!step) step = 1;
dir *= step;
if (dir > 0) {
if (ccvalue[portno][chan][data] >= 127) return;
ccvalue[portno][chan][data] += dir;
if (ccvalue[portno][chan][data] > 127) ccvalue[portno][chan][data] = 127;
} else {
if (ccvalue[portno][chan][data] == 0) return;
ccvalue[portno][chan][data] += dir;
if (ccvalue[portno][chan][data] < 0) ccvalue[portno][chan][data] = 0;
}
msg[2] = ccvalue[portno][chan][data];
}
} else if (mod) {
int m = (data>=128)*128;
int q = swap?val%mod:val/mod, r = swap?val/mod:val%mod;
int d = msg[1] + datavals(q, mod_step, mod_steps, mod_n_steps);
int v = datavals(r, step, steps, n_steps);
if (d-m > 127 || d-m < 0) return;
if (v > 127 || v < 0) return;
if (s->change) {
if (s->change > 1 && s->d == d && s->v == v) return; // unchanged value
s->d = d; s->v = v; s->change = 2; // >1 => initialized
}
msg[1] = d;
msg[2] = v;
} else if (!index) {
msg[2] = dataval(step, 0, 127);
} else {
msg[2] = 0;
}
break;
case 0xa0:
if (dir) {
// increment (dir==1) or decrement (dir==-1) the current value,
// clamping it to the 0..127 data byte range
if (!step) step = 1;
dir *= step;
if (dir > 0) {
if (kpvalue[portno][chan][data] >= 127) return;
kpvalue[portno][chan][data] += dir;
if (kpvalue[portno][chan][data] > 127) kpvalue[portno][chan][data] = 127;
} else {
if (kpvalue[portno][chan][data] == 0) return;
kpvalue[portno][chan][data] += dir;
if (kpvalue[portno][chan][data] < 0) kpvalue[portno][chan][data] = 0;
}
msg[2] = kpvalue[portno][chan][data];
} else if (mod) {
int q = swap?val%mod:val/mod, r = swap?val/mod:val%mod;
int d = msg[1] + datavals(q, mod_step, mod_steps, mod_n_steps);
int v = datavals(r, step, steps, n_steps);
if (d > 127 || d < 0) return;
if (v > 127 || v < 0) return;
if (s->change) {
if (s->change > 1 && s->d == d && s->v == v) return; // unchanged value
s->d = d; s->v = v; s->change = 2; // >1 => initialized
}
msg[1] = d;
msg[2] = v;
} else if (!index) {
msg[2] = dataval(step, 0, 127);
} else {
msg[2] = 0;
}
break;
case 0xd0:
if (dir) {
// increment (dir==1) or decrement (dir==-1) the current value,
// clamping it to the 0..127 data byte range
if (!step) step = 1;
dir *= step;
if (dir > 0) {
if (cpvalue[portno][chan] >= 127) return;
cpvalue[portno][chan] += dir;
if (cpvalue[portno][chan] > 127) cpvalue[portno][chan] = 127;
} else {
if (cpvalue[portno][chan] == 0) return;
cpvalue[portno][chan] += dir;
if (cpvalue[portno][chan] < 0) cpvalue[portno][chan] = 0;
}
msg[1] = cpvalue[portno][chan];
} else if (mod) {
int v = datavals(swap?val/mod:val%mod, step, steps, n_steps);
if (v > 127 || v < 0) return;
if (s->change) {
if (s->change > 1 && s->v == v) return; // unchanged value
s->v = v; s->change = 2; // >1 => initialized
}
msg[1] = v;
} else if (!index) {
msg[1] = dataval(step, 0, 127);
} else {
msg[1] = 0;
}
break;
case 0xe0: {
// pitch bends are treated similarly to a controller, but with a 14 bit
// range (0..16383, with 8192 being the center value)
int pbval = 0;
if (dir) {
if (!step) step = 1;
dir *= step;
if (dir > 0) {
if (pbvalue[portno][chan] >= 16383) return;
pbvalue[portno][chan] += dir;
if (pbvalue[portno][chan] > 16383) pbvalue[portno][chan] = 16383;
} else {
if (pbvalue[portno][chan] == 0) return;
pbvalue[portno][chan] += dir;
if (pbvalue[portno][chan] < 0) pbvalue[portno][chan] = 0;
}
pbval = pbvalue[portno][chan];
} else if (mod) {
int v = datavals(swap?val/mod:val%mod, step, steps, n_steps);
if (v > 16383 || v < 0) return;
if (s->change) {
if (s->change > 1 && s->v == v) return; // unchanged value
s->v = v; s->change = 2; // >1 => initialized
}
pbval = v;
} else if (!index) {
pbval = 8192+dataval(step, -8192, 8191);
} else {
// we use 8192 (center) as the "home" (a.k.a. "off") value, so the pitch
// will only bend up, never down below the center value
pbval = 8192;
}
// the result is a 14 bit value which gets encoded as a combination of two
// 7 bit values which become the data bytes of the message
msg[1] = pbval & 0x7f; // LSB (lower 7 bits)
msg[2] = pbval >> 7; // MSB (upper 7 bits)
break;
}
case 0xc0:
if (mod) {
int d = msg[1] + datavals(swap?val%mod:val/mod, mod_step, mod_steps, mod_n_steps);
if (d > 127 || d < 0) return;
if (s->change) {
if (s->change > 1 && s->d == d) return; // unchanged value
s->d = d; s->change = 2; // >1 => initialized
}
msg[1] = d;
}
// just send the message
break;
default:
return;
}
if (ret_msg) memcpy(ret_msg, msg, 3);
if (recursive) {
// As these values may be mutated, we need to save and restore them, in
// case a macro calls itself recursively.
uint8_t change = s->change;
int d = s->d, v = s->v;
s->change = change>0;
handle_event(msg, portno, depth+1, recursive);
s->change = change;
s->d = d; s->v = v;
} else
queue_midi(&seq, msg, portno);
}
static int stroke_data_cmp(const void *a, const void *b)
{
const stroke_data *ad = (const stroke_data*)a;
const stroke_data *bd = (const stroke_data*)b;
if (ad->chan == bd->chan)
return ad->data - bd->data;
else
return ad->chan - bd->chan;
}
static stroke *find_stroke_data(stroke_data *sd,
int chan, int data, int index,
int *step, int *n_steps, int **steps,
int *incr, int *mod,
uint16_t n)
{
if (n < 16) {
// Linear search is presumably faster for small arrays, and we also avoid
// function calls for doing the comparisons here. Not sure where it breaks
// even with glibc's bsearch(), though (TODO: measure).
uint16_t i;
for (i = 0; i < n; i++) {
if (sd[i].chan == chan && sd[i].data == data) {
if (step) *step = sd[i].step[index];
if (n_steps) *n_steps = sd[i].n_steps[index];
if (steps) *steps = sd[i].steps[index];
if (incr) *incr = sd[i].is_incr;
if (mod) *mod = sd[i].mod;
return sd[i].s[index];
} else if (sd[i].chan > chan ||
(sd[i].chan == chan && sd[i].data > data)) {
return NULL;
}
}
return NULL;
} else {
// binary search from libc
stroke_data *ret, key;
key.chan = chan; key.data = data;
ret = bsearch(&key, sd, n, sizeof(stroke_data), stroke_data_cmp);
if (ret) {
if (step) *step = ret->step[index];
if (n_steps) *n_steps = ret->n_steps[index];
if (steps) *steps = ret->steps[index];
if (incr) *incr = ret->is_incr;
if (mod) *mod = ret->mod;
return ret->s[index];
} else
return NULL;
}
}
static stroke *find_note(translation *tr, int shift,
int chan, int data, int index, int *mod,
int *step, int *n_steps, int **steps)
{
return find_stroke_data(tr->note[shift], chan, data, index,
step, n_steps, steps, 0, mod,
tr->n_note[shift]);
}
static stroke *find_notes(translation *tr, int shift,
int chan, int data, int index, int *step)
{
return find_stroke_data(tr->notes[shift], chan, data, index, step,
0, 0, 0, 0,
tr->n_notes[shift]);
}
static stroke *find_pc(translation *tr, int shift,
int chan, int data, int index)
{
return find_stroke_data(tr->pc[shift], chan, data, index, 0, 0, 0, 0, 0,
tr->n_pc[shift]);
}
static stroke *find_cc(translation *tr, int shift,
int chan, int data, int index, int *mod,
int *step, int *n_steps, int **steps)
{
return find_stroke_data(tr->cc[shift], chan, data, index,
step, n_steps, steps, 0, mod,
tr->n_cc[shift]);
}
static stroke *find_ccs(translation *tr, int shift,
int chan, int data, int index, int *step, int *incr)
{
return find_stroke_data(tr->ccs[shift], chan, data, index, step, 0, 0,
incr, 0,
tr->n_ccs[shift]);
}
static stroke *find_kp(translation *tr, int shift,
int chan, int data, int index, int *mod,
int *step, int *n_steps, int **steps)
{
return find_stroke_data(tr->kp[shift], chan, data, index,
step, n_steps, steps, 0, mod,
tr->n_kp[shift]);
}
static stroke *find_kps(translation *tr, int shift,
int chan, int data, int index, int *step)
{
return find_stroke_data(tr->kps[shift], chan, data, index, step,
0, 0, 0, 0,
tr->n_kps[shift]);
}
static stroke *find_cp(translation *tr, int shift,
int chan, int index, int *mod,
int *step, int *n_steps, int **steps)
{
return find_stroke_data(tr->cp[shift], chan, 0, index,
step, n_steps, steps, 0, mod,
tr->n_cp[shift]);
}
static stroke *find_cps(translation *tr, int shift,
int chan, int index, int *step)
{
return find_stroke_data(tr->cps[shift], chan, 0, index, step,
0, 0, 0, 0,
tr->n_cps[shift]);
}
static stroke *find_pb(translation *tr, int shift,
int chan, int index, int *mod,
int *step, int *n_steps, int **steps)
{
return find_stroke_data(tr->pb[shift], chan, 0, index,
step, n_steps, steps, 0, mod,
tr->n_pb[shift]);
}
static stroke *find_pbs(translation *tr, int shift,
int chan, int index, int *step)
{
return find_stroke_data(tr->pbs[shift], chan, 0, index, step, 0, 0, 0, 0,
tr->n_pbs[shift]);
}
stroke *
fetch_stroke(translation *tr, uint8_t portno, int status, int chan, int data,
int index, int dir, int *step, int *n_steps, int **steps,
int *incr, int *mod)
{
if (tr && tr->portno == portno) {
switch (status) {
case 0x90:
if (dir)
return find_notes(tr, shift, chan, data, dir>0, step);
else
return find_note(tr, shift, chan, data, index, mod, step, n_steps, steps);
case 0xc0:
return find_pc(tr, shift, chan, data, index);
case 0xb0:
if (dir)
return find_ccs(tr, shift, chan, data, dir>0, step, incr);
else
return find_cc(tr, shift, chan, data, index, mod, step, n_steps, steps);
case 0xa0:
if (dir)
return find_kps(tr, shift, chan, data, dir>0, step);
else
return find_kp(tr, shift, chan, data, index, mod, step, n_steps, steps);
case 0xd0:
if (dir)
return find_cps(tr, shift, chan, dir>0, step);
else
return find_cp(tr, shift, chan, index, mod, step, n_steps, steps);
case 0xe0:
if (dir)
return find_pbs(tr, shift, chan, dir>0, step);
else
return find_pb(tr, shift, chan, index, mod, step, n_steps, steps);
default:
return NULL;
}
} else
return NULL;
}
#define MAX_WINNAME_SIZE 1024
static char last_window_name[MAX_WINNAME_SIZE];
static char last_window_class[MAX_WINNAME_SIZE];
static Window last_focused_window = 0;
static translation *last_window_translation = NULL, *last_translation = NULL;
static int last_window = 0;
void reload_callback(void)
{
last_focused_window = 0;
last_window_translation = last_translation = NULL;
last_window = 0;
}
static void debug_section(translation *tr)
{
// we do some caching of the last printed translation here, so that we don't
// print the same message twice
if (debug_regex && (!last_window || tr != last_translation)) {
last_translation = tr;
last_window = 1;
if (tr) {
printf("translation: %s for %s (class %s)\n",
tr->name, last_window_name, last_window_class);
} else {
printf("no translation found for %s (class %s)\n",
last_window_name, last_window_class);
}
}
}
static char *note_name(int n)
{
static char *note_names[] = { "C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B" };
if (n < 0 && n%12)
return note_names[12+n%12];
else
return note_names[n%12];
}
static int note_octave(int n)
{
if (n < 0 && n%12)
return n/12-1 + midi_octave;
else
return n/12 + midi_octave;
}
static char *debug_key(translation *tr, char *name,
int status, int chan, int data, int dir)
{
char prefix[10] = "";
if (shift) sprintf(prefix, "%d^", shift);
char *suffix = "";
strcpy(name, "??");
switch (status) {
case 0x90: {
int mod = 0, step, n_steps, *steps;
if (tr) {
if (dir) {
step = 1;
(void)find_notes(tr, shift, chan, data, dir>0, &step);
} else
(void)find_note(tr, shift, chan, data, 0, &mod, &step, &n_steps, &steps);
}
if (dir)
suffix = (dir<0)?"-":"+";
else
suffix = "";
if (dir && step != 1)
sprintf(name, "%s%s%d[%d]-%d%s", prefix, note_name(data),
note_octave(data), step, chan+1, suffix);
else if (!dir && mod)
if (step != 1)
sprintf(name, "%s%s%d[%d][%d]-%d%s", prefix, note_name(data),
note_octave(data), mod, step, chan+1, suffix);
else if (n_steps) {
sprintf(name, "%s%s%d[%d]{", prefix, note_name(data),
note_octave(data), mod);
int l = strlen(name);
for (int i = 0; i < n_steps; i++, (l = strlen(name)))
sprintf(name+l, "%s%d", i?",":"", steps[i]);
sprintf(name+l, "}-%d%s", chan+1, suffix);
} else
sprintf(name, "%s%s%d[%d]-%d%s", prefix, note_name(data),
note_octave(data), mod, chan+1, suffix);
else
sprintf(name, "%s%s%d-%d%s", prefix, note_name(data),
note_octave(data), chan+1, suffix);
break;
}
case 0xa0: {
int step = 0, n_steps = 0, *steps = 0, mod = 0;
if (tr) {
if (dir) {
step = 1;
(void)find_kps(tr, shift, chan, data, dir>0, &step);
} else
(void)find_kp(tr, shift, chan, data, 0, &mod, &step, &n_steps, &steps);
}
if (dir)
suffix = (dir<0)?"-":"+";
else
suffix = "";
if (dir && step != 1)
sprintf(name, "%sKP:%s%d[%d]-%d%s", prefix, note_name(data),
note_octave(data), step, chan+1, suffix);
else if (!dir && mod)
if (step != 1)
sprintf(name, "%sKP:%s%d[%d][%d]-%d%s", prefix, note_name(data),
note_octave(data), mod, step, chan+1, suffix);
else if (n_steps) {
sprintf(name, "%sKP:%s%d[%d]{", prefix, note_name(data),
note_octave(data), mod);
int l = strlen(name);
for (int i = 0; i < n_steps; i++, (l = strlen(name)))
sprintf(name+l, "%s%d", i?",":"", steps[i]);
sprintf(name+l, "}-%d%s", chan+1, suffix);
} else
sprintf(name, "%sKP:%s%d[%d]-%d%s", prefix, note_name(data),
note_octave(data), mod, chan+1, suffix);
else
sprintf(name, "%sKP:%s%d-%d%s", prefix, note_name(data),
note_octave(data), chan+1, suffix);
break;
}
case 0xb0: {
int step = 0, n_steps = 0, *steps = 0, mod = 0, is_incr = 0;
if (tr) {
if (dir) {
step = 1;
(void)find_ccs(tr, shift, chan, data, dir>0, &step, &is_incr);
} else
(void)find_cc(tr, shift, chan, data, 0, &mod, &step, &n_steps, &steps);
}
if (is_incr)
suffix = (dir<0)?"<":">";
else if (dir)
suffix = (dir<0)?"-":"+";
else
suffix = "";
// check for pseudo CC messages denoting a macro
char *tok = data>=128?"M":"CC";
data %= 128;
if (dir && step != 1)
sprintf(name, "%s%s%d[%d]-%d%s", prefix, tok, data, step, chan+1, suffix);
else if (!dir && mod)
if (step != 1)
sprintf(name, "%s%s%d[%d][%d]-%d%s", prefix, tok, data, mod, step, chan+1, suffix);
else if (n_steps) {
sprintf(name, "%s%s%d[%d]{", prefix, tok, data, mod);
int l = strlen(name);
for (int i = 0; i < n_steps; i++, (l = strlen(name)))
sprintf(name+l, "%s%d", i?",":"", steps[i]);
sprintf(name+l, "}-%d%s", chan+1, suffix);
} else
sprintf(name, "%s%s%d[%d]-%d%s", prefix, tok, data, mod, chan+1, suffix);
else
sprintf(name, "%s%s%d-%d%s", prefix, tok, data, chan+1, suffix);
break;
}
case 0xc0:
sprintf(name, "%sPC%d-%d", prefix, data, chan+1);
break;
case 0xd0: {
int step = 0, n_steps = 0, *steps = 0, mod = 0;
if (tr) {
if (dir) {
step = 1;
(void)find_cps(tr, shift, chan, dir>0, &step);
} else
(void)find_cp(tr, shift, chan, 0, &mod, &step, &n_steps, &steps);
}
if (!dir)
suffix = "";
else
suffix = (dir<0)?"-":"+";
if (dir && step != 1)
sprintf(name, "%sCP[%d]-%d%s", prefix, step, chan+1, suffix);
else if (!dir && mod)
if (step != 1)
sprintf(name, "%sCP[%d][%d]-%d", prefix, mod, step, chan+1);
else if (n_steps) {
sprintf(name, "%sCP[%d]{", prefix, mod);
int l = strlen(name);
for (int i = 0; i < n_steps; i++, (l = strlen(name)))
sprintf(name+l, "%s%d", i?",":"", steps[i]);
sprintf(name+l, "}-%d", chan);
} else
sprintf(name, "%sCP[%d]-%d", prefix, mod, chan+1);
else
sprintf(name, "%sCP-%d%s", prefix, chan+1, suffix);
break;
}
case 0xe0: {
int step = 1;
if (tr) (void)find_pbs(tr, shift, chan, dir>0, &step);
if (!dir)
suffix = "";
else
suffix = (dir<0)?"-":"+";
if (dir && step != 1)
sprintf(name, "%sPB[%d]-%d%s", prefix, step, chan+1, suffix);
else
sprintf(name, "%sPB-%d%s", prefix, chan+1, suffix);
break;
}
default: // this can't happen
break;
}
return name;
}
static void debug_input(int portno,
int status, int chan, int data, int data2)
{
char name[100];
if (status == 0xe0)
// translate LSB,MSB to a pitch bend value in the range -8192..8191
data2 = ((data2 << 7) | data) - 8192;
else if (status == 0xd0)
data2 = data;
if (status == 0xc0)
printf("[%d] %s\n", portno,
debug_key(0, name, status, chan, data, 0));
else if (status != 0xf0) // system messages ignored for now
printf("[%d] %s value = %d\n", portno,
debug_key(0, name, status, chan, data, 0), data2);
}
// Some machinery to handle the debugging of section matches. This is
// necessary since some inputs may generate a lot of calls to send_strokes()
// without ever actually matching any output sequence at all. In such cases we
// want to prevent a cascade of useless debugging messages by handling the
// message printing in a lazy manner.
static int debug_state = 0, debug_count = 0;
static translation *debug_tr = NULL;
static void start_debug()
{
// start a debugging section
debug_state = debug_regex;
debug_tr = NULL;
debug_count = 0;
}
static void end_debug()
{
// end a debugging section; if we still haven't matched an output sequence,
// but processed any input at all, we print the last matched translation
// section now anyway
if (debug_state && debug_count) debug_section(debug_tr);
debug_state = 0;
}
// maximum recursion depth
#define MAX_DEPTH 32
// shift feedback
uint8_t shift_fb[N_SHIFTS][3];
static int toggle_msg(uint8_t msg[3])
{
if (msg[0] < 0x80) return 0;
switch (msg[0]&0xf0) {
case 0xc0:
return 0;
case 0xd0:
msg[1] = 0;
break;
case 0xe0:
msg[1] = 0x40;
msg[2] = 0;
break;
default:
msg[2] = 0;
break;
}
return 1;
}
int
check_strokes(translation *tr, uint8_t portno, int status, int chan, int data)
{
for (int i = 0; i < 2; i++)
if (fetch_stroke(tr, portno, status, chan, data, i, 0, 0,0,0,0,0) ||
fetch_stroke(tr, portno, status, chan, data, 0, i?1:-1, 0,0,0,0,0))
return 1;
if (jack_num_outputs) {
// fall back to default MIDI translation
tr = default_midi_translation[portno];
for (int i = 0; i < 2; i++)
if (fetch_stroke(tr, portno, status, chan, data, i, 0, 0,0,0,0,0) ||
fetch_stroke(tr, portno, status, chan, data, 0, i?1:-1, 0,0,0,0,0))
return 1;
// Ignore all MIDI input on the second port if no translation was found in
// the [MIDI2] section (or the section is missing altogether).
if (portno) return 0;
}
// fall back to the default translation
tr = default_translation;
for (int i = 0; i < 2; i++)
if (fetch_stroke(tr, portno, status, chan, data, i, 0, 0,0,0,0,0) ||
fetch_stroke(tr, portno, status, chan, data, 0, i?1:-1, 0,0,0,0,0))
return 1;
return 0;
}
void
send_strokes(translation *tr, uint8_t portno, int status, int chan,
int data, int data2, int index, int dir, int depth)
{
int nkeys = 0, step = 0, n_steps = 0, *steps = 0, is_incr = 0, mod = 0;
stroke *s = fetch_stroke(tr, portno, status, chan, data, index, dir,
&step, &n_steps, &steps, &is_incr, &mod);
// If there's no press/release translation, check whether we have got at
// least the corresponding release/press translation, in order to prevent
// spurious error messages if either the press or release translation just
// happens to be empty.
int chk = s ||
(!dir && fetch_stroke(tr, portno, status, chan, data, !index, dir, 0, 0, 0, 0, 0));
if (!s && jack_num_outputs) {
// fall back to default MIDI translation
tr = default_midi_translation[portno];
s = fetch_stroke(tr, portno, status, chan, data, index, dir,
&step, &n_steps, &steps, &is_incr, &mod);
chk = chk || s ||
(!dir && fetch_stroke(tr, portno, status, chan, data, !index, dir, 0, 0, 0, 0, 0));
// Ignore all MIDI input on the second port if no translation was found in
// the [MIDI2] section (or the section is missing altogether).
if (portno && !s) return;
}
if (!s) {
// fall back to the default translation
tr = default_translation;
s = fetch_stroke(tr, portno, status, chan, data, index, dir,
&step, &n_steps, &steps, &is_incr, &mod);
chk = chk || s ||
(!dir && fetch_stroke(tr, portno, status, chan, data, !index, dir, 0, 0, 0, 0, 0));
}
if (debug_regex) {
if (s) {
// found a sequence, print the matching section now
debug_section(tr);
debug_state = 0;
} else if (!chk) {
// No matches yet. To prevent a cascade of spurious messages, we defer
// printing the matched section for now and just record it instead; it
// may then be printed later.
debug_tr = tr;
// record that we actually tried to process some input
debug_count = 1;
}
}
if (s && debug_keys) {
char name[100];
print_stroke_sequence(debug_key(tr, name, status, chan, data, dir),
(dir||mod)?"":index?"U":"D", s,
mod, step, n_steps, steps, data2);
}
while (s) {
if (s->keysym) {
send_key(s->keysym, s->press);
nkeys++;
} else if (s->shift) {
// toggle shift status
if (shift != s->shift) {
if (shift) {
// reset current shift feedback
if (toggle_msg(shift_fb[shift-1]))
queue_midi(&seq, shift_fb[shift-1], 1);
memset(shift_fb[shift-1], 0, 3);
}
shift = s->shift;
} else
shift = 0;
} else if (!s->status) {
// do nothing (NOP)
;
} else {
if (s->recursive && depth >= MAX_DEPTH) {
char name[100];
if (tr && tr->name)
fprintf(stderr, "Error: [%s]$%s: recursion too deep\n",
tr->name, debug_key(tr, name, status, chan, data, dir));
else
fprintf(stderr, "Error: $%s: recursion too deep\n",
debug_key(tr, name, status, chan, data, dir));
} else if (s->feedback) {
if (!s->recursive && jack_num_outputs > 1) {
if (s->feedback == 1)
// direct feedback, simply flip the port number
send_midi(!portno, s, index, dir, mod,
step, n_steps, steps, data2, depth, 0);
else if (portno == 0 && !mod && !dir)
// shift feedback, this only works with key translations right
// now, and portno *must* be zero
send_midi(1, s, !shift, 0, 0,
step, n_steps, steps, data2, depth,
shift?shift_fb[shift-1]:0);
}
} else {
send_midi(portno, s, index, dir, mod,
step, n_steps, steps, data2, depth, 0);
}
}
s = s->next;
}
// no need to flush the display if we didn't send any keys
if (nkeys) {
XFlush(display);
}
}
char *
get_window_name(Window win)
{
Atom prop = XInternAtom(display, "WM_NAME", False);
Atom type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(display, win, prop, 0, 1024, False,
AnyPropertyType, &type, &form, &len, &remain,
&list) != Success) {
fprintf(stderr, "XGetWindowProperty failed for window 0x%x\n", (int)win);
return NULL;
}
return (char*)list;
}
char *
get_window_class(Window win)
{
Atom prop = XInternAtom(display, "WM_CLASS", False);
Atom type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(display, win, prop, 0, 1024, False,
AnyPropertyType, &type, &form, &len, &remain,
&list) != Success) {
fprintf(stderr, "XGetWindowProperty failed for window 0x%x\n", (int)win);
return NULL;
}
return (char*)list;
}
char *
walk_window_tree(Window win, char **window_class)
{
char *window_name;
Window root = 0;
Window parent;
Window *children;
unsigned int nchildren;
while (win != root) {
window_name = get_window_name(win);
if (window_name != NULL) {
*window_class = get_window_class(win);
return window_name;
}
if (XQueryTree(display, win, &root, &parent, &children, &nchildren)) {
win = parent;
XFree(children);
} else {
fprintf(stderr, "XQueryTree failed for window 0x%x\n", (int)win);
return NULL;
}
}
return NULL;
}
translation *
get_focused_window_translation()
{
Window focus;
int revert_to;
char *window_name = NULL, *window_class = NULL;