-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadconfig.c
2143 lines (2015 loc) · 58 KB
/
readconfig.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]>
Read and process the configuration file ~/.midizaprc
Lines starting with # are comments.
The file is a sequence of sections defining translation classes. Each
section takes the following form:
[name] regex
CC<0..127> output # control change
PC<0..127> output # program change
PB output # pitch bend
CP output # channel pressure
KP:<A-G>[#b]<-11..11> output # key pressure (aftertouch)
<A-G>[#b]<-11..11> output # note
When focus is on a window whose class or title matches regex, the
following translation class is in effect. An empty regex for the last
class will always match, allowing default translations. Any output
sequences not bound in a matched section will be loaded from the
default section if they are bound there.
Each "[name] regex" line introduces the list of MIDI message
translations for the named translation class. The name is only used
for debugging output, and needn't be unique. The following lines
indicate what output should be produced for the given MIDI messages.
MIDI messages are on channel 1 by default; a suffix of the form
-<1..16> can be used to specify a different MIDI channel. E.g., C3-10
denotes note C3 on MIDI channel 10.
Note messages are specified using the cutomary notation (note name
A..G, optionally followed by an accidental, # or b, followed by a
(zero-based) MIDI octave number. Note that all MIDI octaves start at
the note C, so B0 comes before C1. By default, C5 denotes middle C, A5
is the chamber pitch (usually at 440 Hz). Enharmonic spellings are
equivalent, so, e.g., D# and Eb denote exactly the same MIDI note.
More details on the syntax of MIDI messages can be found in the
comments preceding the parse_midi() routine below.
*/
#include "midizap.h"
int default_debug_regex = 0;
int default_debug_strokes = 0;
int default_debug_keys = 0;
int default_debug_midi = 0;
int debug_regex = 0;
int debug_strokes = 0;
int debug_keys = 0;
int debug_midi = 0;
int midi_octave = 0;
char *jack_client_name, *jack_in_regex[2], *jack_out_regex[2];
char *
allocate(size_t len)
{
char *ret = (char *)calloc(1, len);
if (ret == NULL) {
fprintf(stderr, "out of memory!\n");
exit(1);
}
return ret;
}
char *
alloc_strcat(char *a, char *b)
{
size_t len = 0;
char *result;
if (a != NULL) {
len += strlen(a);
}
if (b != NULL) {
len += strlen(b);
}
result = allocate(len+1);
result[0] = '\0';
if (a != NULL) {
strcpy(result, a);
}
if (b != NULL) {
strcat(result, b);
}
return result;
}
static char *read_line_buffer = NULL;
static int read_line_buffer_length = 0;
#define BUF_GROWTH_STEP 1024
// read a line of text from the given file into a managed buffer.
// returns a partial line at EOF if the file does not end with \n.
// exits with error message on read error.
char *
read_line(FILE *f, char *name)
{
int pos = 0;
char *new_buffer;
int new_buffer_length;
if (read_line_buffer == NULL) {
read_line_buffer_length = BUF_GROWTH_STEP;
read_line_buffer = allocate(read_line_buffer_length);
read_line_buffer[0] = '\0';
}
while (1) {
read_line_buffer[read_line_buffer_length-1] = '\377';
if (fgets(read_line_buffer+pos, read_line_buffer_length-pos, f) == NULL) {
if (feof(f)) {
if (pos > 0) {
// partial line at EOF
return read_line_buffer;
} else {
return NULL;
}
}
perror(name);
exit(1);
}
if (read_line_buffer[read_line_buffer_length-1] != '\0') {
return read_line_buffer;
}
if (read_line_buffer[read_line_buffer_length-2] == '\n') {
return read_line_buffer;
}
new_buffer_length = read_line_buffer_length + BUF_GROWTH_STEP;
new_buffer = allocate(new_buffer_length);
memcpy(new_buffer, read_line_buffer, read_line_buffer_length);
free(read_line_buffer);
pos = read_line_buffer_length-1;
read_line_buffer = new_buffer;
read_line_buffer_length = new_buffer_length;
}
}
static translation *first_translation_section = NULL;
static translation *last_translation_section = NULL;
translation *default_translation, *default_midi_translation[2];
translation *
new_translation_section(char *name, int mode, char *regex)
{
translation *ret = (translation *)allocate(sizeof(translation));
int err;
memset(ret, 0, sizeof(translation));
if (debug_strokes) {
printf("------------------------\n[%s] %s%s\n\n", name,
mode==1?"TITLE ":mode==2?"CLASS ":"",
regex);
}
ret->next = NULL;
ret->name = alloc_strcat(name, NULL);
ret->mode = mode;
if (regex == NULL || *regex == '\0') {
ret->is_default = 1;
if (!strcmp(name, "MIDI"))
default_midi_translation[0] = ret;
else if (!strcmp(name, "MIDI2")) {
default_midi_translation[1] = ret;
ret->portno = 1;
} else
default_translation = ret;
} else {
ret->is_default = 0;
err = regcomp(&ret->regex, regex, REG_EXTENDED|REG_NOSUB);
if (err != 0) {
regerror(err, &ret->regex, read_line_buffer, read_line_buffer_length);
fprintf(stderr, "error compiling regex for [%s]: %s\n", name, read_line_buffer);
regfree(&ret->regex);
free(ret->name);
free(ret);
return NULL;
}
}
if (first_translation_section == NULL) {
first_translation_section = ret;
last_translation_section = ret;
} else {
last_translation_section->next = ret;
last_translation_section = ret;
}
return ret;
}
void
free_strokes(stroke *s)
{
stroke *next;
while (s != NULL) {
next = s->next;
if (s->steps) free(s->steps);
free(s);
s = next;
}
}
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 void finish_stroke_data(stroke_data **sd,
uint16_t *n, uint16_t *a)
{
if (*a && *a > *n) {
// realloc to needed size
*sd = realloc(*sd, (*n)*sizeof(stroke_data));
*a = *n;
}
// sort by chan/data for faster access
qsort(*sd, *n, sizeof(stroke_data), stroke_data_cmp);
}
static void free_stroke_data(stroke_data *sd, uint16_t n)
{
uint16_t i;
for (i = 0; i < n; i++) {
free_strokes(sd[i].s[0]);
free_strokes(sd[i].s[1]);
if (sd[i].steps[0]) free(sd[i].steps[0]);
if (sd[i].steps[1]) free(sd[i].steps[1]);
}
free(sd);
}
static int *stepsdup(int n_steps, int *steps)
{
if (n_steps) {
int *ret = malloc(n_steps*sizeof(int));
memcpy(ret, steps, n_steps*sizeof(int));
return ret;
} else
return 0;
}
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, int anyshift,
uint16_t *n, uint16_t *a)
{
uint16_t i;
for (i = 0; i < *n; i++) {
if ((*sd)[i].chan == chan && (*sd)[i].data == data) {
// existing entry
if ((*sd)[i].s[index]) return 0;
(*sd)[i].step[index] = step;
(*sd)[i].n_steps[index] = n_steps;
(*sd)[i].steps[index] = stepsdup(n_steps, steps);
(*sd)[i].is_incr = incr;
(*sd)[i].mod = mod;
(*sd)[i].anyshift = anyshift;
return &(*sd)[i].s[index];
}
}
// add a new entry
if (*n >= *a) {
// make some room
*a = (*a)?2*(*a):8;
*sd = realloc(*sd, (*a)*sizeof(stroke_data));
}
memset(&(*sd)[*n], 0, sizeof(stroke_data));
(*sd)[*n].chan = chan;
(*sd)[*n].data = data;
(*sd)[*n].step[index] = step;
(*sd)[*n].n_steps[index] = n_steps;
(*sd)[*n].steps[index] = stepsdup(n_steps, steps);
(*sd)[*n].is_incr = incr;
(*sd)[*n].mod = mod;
(*sd)[*n].anyshift = anyshift;
return &(*sd)[(*n)++].s[index];
}
static int check_stroke_data(stroke_data *sd,
int chan, int data,
uint16_t n)
{
uint16_t i;
for (i = 0; i < n; i++) {
if (sd[i].chan == chan && sd[i].data == data)
return 1;
}
return 0;
}
static stroke **find_note(translation *tr, int shift,
int chan, int data, int index, int mod,
int step, int n_steps, int *steps,
int anyshift)
{
if (check_stroke_data(tr->notes[shift], chan, data, tr->n_notes[shift]))
return 0;
else
return find_stroke_data(&tr->note[shift], chan, data, index,
step, n_steps, steps, 0, mod, anyshift,
&tr->n_note[shift], &tr->a_note[shift]);
}
static stroke **find_notes(translation *tr, int shift,
int chan, int data, int index, int step,
int anyshift)
{
if (check_stroke_data(tr->note[shift], chan, data, tr->n_note[shift]))
return 0;
else
return find_stroke_data(&tr->notes[shift], chan, data, index,
step, 0, 0, 0, 0, anyshift,
&tr->n_notes[shift], &tr->a_notes[shift]);
}
static stroke **find_pc(translation *tr, int shift,
int chan, int data, int index,
int anyshift)
{
return find_stroke_data(&tr->pc[shift], chan, data, index,
0, 0, 0, 0, 0, anyshift,
&tr->n_pc[shift], &tr->a_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,
int anyshift)
{
if (check_stroke_data(tr->ccs[shift], chan, data, tr->n_ccs[shift]))
return 0;
else
return find_stroke_data(&tr->cc[shift], chan, data, index,
step, n_steps, steps, 0, mod, anyshift,
&tr->n_cc[shift], &tr->a_cc[shift]);
}
static stroke **find_ccs(translation *tr, int shift,
int chan, int data, int index, int step, int incr,
int anyshift)
{
if (check_stroke_data(tr->cc[shift], chan, data, tr->n_cc[shift]))
return 0;
else
return find_stroke_data(&tr->ccs[shift], chan, data, index,
step, 0, 0, incr, 0, anyshift,
&tr->n_ccs[shift], &tr->a_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,
int anyshift)
{
if (check_stroke_data(tr->kps[shift], chan, data, tr->n_kps[shift]))
return 0;
else
return find_stroke_data(&tr->kp[shift], chan, data, index,
step, n_steps, steps, 0, mod, anyshift,
&tr->n_kp[shift], &tr->a_kp[shift]);
}
static stroke **find_kps(translation *tr, int shift,
int chan, int data, int index, int step,
int anyshift)
{
if (check_stroke_data(tr->kp[shift], chan, data, tr->n_kp[shift]))
return 0;
else
return find_stroke_data(&tr->kps[shift], chan, data, index, step,
0, 0, 0, 0, anyshift,
&tr->n_kps[shift], &tr->a_kps[shift]);
}
static stroke **find_cp(translation *tr, int shift,
int chan, int index, int mod,
int step, int n_steps, int *steps,
int anyshift)
{
if (check_stroke_data(tr->cps[shift], chan, 0, tr->n_cps[shift]))
return 0;
else
return find_stroke_data(&tr->cp[shift], chan, 0, index,
step, n_steps, steps, 0, mod, anyshift,
&tr->n_cp[shift], &tr->a_cp[shift]);
}
static stroke **find_cps(translation *tr, int shift,
int chan, int index, int step,
int anyshift)
{
if (check_stroke_data(tr->cp[shift], chan, 0, tr->n_cp[shift]))
return 0;
else
return find_stroke_data(&tr->cps[shift], chan, 0, index, step,
0, 0, 0, 0, anyshift,
&tr->n_cps[shift], &tr->a_cps[shift]);
}
static stroke **find_pb(translation *tr, int shift,
int chan, int index, int mod,
int step, int n_steps, int *steps,
int anyshift)
{
if (check_stroke_data(tr->pbs[shift], chan, 0, tr->n_pbs[shift]))
return 0;
else
return find_stroke_data(&tr->pb[shift], chan, 0, index,
step, n_steps, steps, 0, mod, anyshift,
&tr->n_pb[shift], &tr->a_pb[shift]);
}
static stroke **find_pbs(translation *tr, int shift,
int chan, int index, int step,
int anyshift)
{
if (check_stroke_data(tr->pb[shift], chan, 0, tr->n_pb[shift]))
return 0;
else
return find_stroke_data(&tr->pbs[shift], chan, 0, index, step,
0, 0, 0, 0, anyshift,
&tr->n_pbs[shift], &tr->a_pbs[shift]);
}
static void dup_stroke_data(stroke_data **sd, uint16_t *n, uint16_t *a,
stroke_data *sd0, uint16_t n0,
stroke_data *sd1, uint16_t n1);
void
finish_translation_section(translation *tr)
{
int k;
if (tr) {
for (k=1; k<N_SHIFTS+1; k++) {
dup_stroke_data(&tr->pc[k], &tr->n_pc[k], &tr->a_pc[k],
0, 0,
tr->pc[0], tr->n_pc[0]);
dup_stroke_data(&tr->note[k], &tr->n_note[k], &tr->a_note[k],
tr->notes[k], tr->n_notes[k],
tr->note[0], tr->n_note[0]);
dup_stroke_data(&tr->notes[k], &tr->n_notes[k], &tr->a_notes[k],
tr->note[k], tr->n_note[k],
tr->notes[0], tr->n_notes[0]);
dup_stroke_data(&tr->cc[k], &tr->n_cc[k], &tr->a_cc[k],
tr->ccs[k], tr->n_ccs[k],
tr->cc[0], tr->n_cc[0]);
dup_stroke_data(&tr->ccs[k], &tr->n_ccs[k], &tr->a_ccs[k],
tr->cc[k], tr->n_cc[k],
tr->ccs[0], tr->n_ccs[0]);
dup_stroke_data(&tr->pb[k], &tr->n_pb[k], &tr->a_pb[k],
tr->pbs[k], tr->n_pbs[k],
tr->pb[0], tr->n_pb[0]);
dup_stroke_data(&tr->pbs[k], &tr->n_pbs[k], &tr->a_pbs[k],
tr->pb[k], tr->n_pb[k],
tr->pbs[0], tr->n_pbs[0]);
dup_stroke_data(&tr->kp[k], &tr->n_kp[k], &tr->a_kp[k],
tr->kps[k], tr->n_kps[k],
tr->kp[0], tr->n_kp[0]);
dup_stroke_data(&tr->kps[k], &tr->n_kps[k], &tr->a_kps[k],
tr->kp[k], tr->n_kp[k],
tr->kps[0], tr->n_kps[0]);
dup_stroke_data(&tr->cp[k], &tr->n_cp[k], &tr->a_cp[k],
tr->cps[k], tr->n_cps[k],
tr->cp[0], tr->n_cp[0]);
dup_stroke_data(&tr->cps[k], &tr->n_cps[k], &tr->a_cps[k],
tr->cp[k], tr->n_cp[k],
tr->cps[0], tr->n_cps[0]);
}
for (k=0; k<N_SHIFTS+1; k++) {
finish_stroke_data(&tr->pc[k], &tr->n_pc[k], &tr->a_pc[k]);
finish_stroke_data(&tr->note[k], &tr->n_note[k], &tr->a_note[k]);
finish_stroke_data(&tr->notes[k], &tr->n_notes[k], &tr->a_notes[k]);
finish_stroke_data(&tr->cc[k], &tr->n_cc[k], &tr->a_cc[k]);
finish_stroke_data(&tr->ccs[k], &tr->n_ccs[k], &tr->a_ccs[k]);
finish_stroke_data(&tr->pb[k], &tr->n_pb[k], &tr->a_pb[k]);
finish_stroke_data(&tr->pbs[k], &tr->n_pbs[k], &tr->a_pbs[k]);
finish_stroke_data(&tr->kp[k], &tr->n_kp[k], &tr->a_kp[k]);
finish_stroke_data(&tr->kps[k], &tr->n_kps[k], &tr->a_kps[k]);
finish_stroke_data(&tr->cp[k], &tr->n_cp[k], &tr->a_cp[k]);
finish_stroke_data(&tr->cps[k], &tr->n_cps[k], &tr->a_cps[k]);
}
}
}
void
free_translation_section(translation *tr)
{
int k;
if (tr != NULL) {
free(tr->name);
if (!tr->is_default) {
regfree(&tr->regex);
}
for (k=0; k<N_SHIFTS+1; k++) {
free_stroke_data(tr->pc[k], tr->n_pc[k]);
free_stroke_data(tr->note[k], tr->n_note[k]);
free_stroke_data(tr->cc[k], tr->n_cc[k]);
free_stroke_data(tr->ccs[k], tr->n_ccs[k]);
free_stroke_data(tr->pb[k], tr->n_pb[k]);
free_stroke_data(tr->pbs[k], tr->n_pbs[k]);
}
free(tr);
}
}
void
free_all_translations(void)
{
translation *tr = first_translation_section;
translation *next;
while (tr != NULL) {
next = tr->next;
free_translation_section(tr);
tr = next;
}
first_translation_section = NULL;
last_translation_section = NULL;
default_translation = default_midi_translation[0] =
default_midi_translation[1] = NULL;
}
char *config_file_name = NULL;
static time_t config_file_modification_time;
static char *token_src = NULL;
// similar to strtok, but it tells us what delimiter was found at the
// end of the token, handles double quoted strings specially, and
// hardcodes the delimiter set.
char *
token(char *src, char *delim_found)
{
char *delims = " \t\n/\"";
char *d;
char *token_start;
if (src == NULL) {
src = token_src;
}
if (src == NULL) {
*delim_found = '\0';
return NULL;
}
token_start = src;
while (*src) {
d = delims;
while (*d && *src != *d) {
d++;
}
if (*d) {
if (src == token_start) {
src++;
token_start = src;
if (*d == '"') {
while (*src && *src != '"' && *src != '\n') {
src++;
}
} else {
continue;
}
}
*delim_found = *d;
if (*src) {
*src = '\0';
token_src = src+1;
} else {
token_src = NULL;
}
return token_start;
}
src++;
}
token_src = NULL;
*delim_found = '\0';
if (src == token_start) {
return NULL;
}
return token_start;
}
typedef struct _keysymmapping {
char *str;
KeySym sym;
} keysymmapping;
static keysymmapping key_sym_mapping[] = {
#include "keys.h"
{ "XK_Button_1", XK_Button_1 },
{ "XK_Button_2", XK_Button_2 },
{ "XK_Button_3", XK_Button_3 },
{ "XK_Scroll_Up", XK_Scroll_Up },
{ "XK_Scroll_Down", XK_Scroll_Down },
{ NULL, 0 }
};
KeySym
string_to_KeySym(char *str)
{
size_t len = strlen(str) + 1;
int i = 0;
while (key_sym_mapping[i].str != NULL) {
if (!strncmp(str, key_sym_mapping[i].str, len)) {
return key_sym_mapping[i].sym;
}
i++;
}
return 0;
}
char *
KeySym_to_string(KeySym ks)
{
int i = 0;
while (key_sym_mapping[i].sym != 0) {
if (key_sym_mapping[i].sym == ks) {
return key_sym_mapping[i].str;
}
i++;
}
return NULL;
}
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 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
print_stroke(stroke *s, int mod, int step, int n_steps, int *steps, int val)
{
char *str;
if (s != NULL) {
if (s->keysym) {
str = KeySym_to_string(s->keysym);
if (str == NULL) {
printf("0x%x", (int)s->keysym);
str = "???";
}
printf("%s/%c ", str, s->press ? 'D' : 'U');
} else if (s->shift) {
printf("SHIFT%d ", s->shift);
} else if (!s->status) {
printf("NOP ");
} else {
int status = s->status & 0xf0;
int channel = (s->status & 0x0f) + 1;
char suffix[3] = "";
if (s->incr) strcpy(suffix, "~");
if (s->swap) strcat(suffix, "'");
if (s->change) strcat(suffix, "?");
if (s->recursive) printf("$");
if (s->feedback) printf(s->feedback==2?"^":"!");
switch (status) {
case 0x90:
if (mod) {
int q = s->swap?val%mod:val/mod, r = s->swap?val/mod:val%mod;
int d = s->data + datavals(q, step, steps, n_steps);
int v = datavals(r, s->step, s->steps, s->n_steps);
printf("%s%d[%d]-%d%s ", note_name(d),
note_octave(d), v, channel, suffix);
} else if (s->steps) {
printf("%s%d{", note_name(s->data),
note_octave(s->data));
for (int i = 0; i < s->n_steps; i++)
printf("%s%d", i?",":"", s->steps[i]);
printf("}-%d%s ", channel, suffix);
} else if (s->step)
printf("%s%d[%d]-%d%s ", note_name(s->data),
note_octave(s->data), s->step, channel, suffix);
else
printf("%s%d-%d%s ", note_name(s->data),
note_octave(s->data), channel, suffix);
break;
case 0xa0:
if (mod) {
int q = s->swap?val%mod:val/mod, r = s->swap?val/mod:val%mod;
int d = s->data + datavals(q, step, steps, n_steps);
int v = datavals(r, s->step, s->steps, s->n_steps);
printf("KP:%s%d[%d]-%d%s ", note_name(d),
note_octave(d), v, channel, suffix);
} else if (s->steps) {
printf("KP:%s%d{", note_name(s->data),
note_octave(s->data));
for (int i = 0; i < s->n_steps; i++)
printf("%s%d", i?",":"", s->steps[i]);
printf("}-%d%s ", channel, suffix);
} else if (s->step)
printf("KP:%s%d[%d]-%d%s ", note_name(s->data),
note_octave(s->data), s->step, channel, suffix);
else
printf("KP:%s%d-%d%s ", note_name(s->data),
note_octave(s->data), channel, suffix);
break;
case 0xb0: {
// check for pseudo CC messages denoting a macro
int data = s->data;
char *tok = data>=128?"M":"CC";
data %= 128;
if (mod) {
int q = s->swap?val%mod:val/mod, r = s->swap?val/mod:val%mod;
int d = data + datavals(q, step, steps, n_steps);
int v = datavals(r, s->step, s->steps, s->n_steps);
printf("%s%d[%d]-%d%s ", tok, d, v, channel, suffix);
} else if (s->steps) {
printf("%s%d{", tok, data);
for (int i = 0; i < s->n_steps; i++)
printf("%s%d", i?",":"", s->steps[i]);
printf("}-%d%s ", channel, suffix);
} else if (s->step)
printf("%s%d[%d]-%d%s ", tok, data, s->step, channel, suffix);
else
printf("%s%d-%d%s ", tok, data, channel, suffix);
break;
}
case 0xc0:
if (mod) {
int v = datavals(s->swap?val%mod:val/mod, s->step, s->steps, s->n_steps);
printf("PC%d-%d%s ", v, channel, suffix);
} else
printf("PC%d-%d%s ", s->data, channel, suffix);
break;
case 0xd0:
if (mod) {
int v = datavals(s->swap?val/mod:val%mod, s->step, s->steps, s->n_steps);
printf("CP[%d]-%d%s ", v, channel, suffix);
} else if (s->steps) {
printf("CP{");
for (int i = 0; i < s->n_steps; i++)
printf("%s%d", i?",":"", s->steps[i]);
printf("}-%d%s ", channel, suffix);
} else if (s->step)
printf("CP[%d]-%d%s ", s->step, channel, suffix);
else
printf("CP-%d%s ", channel, suffix);
break;
case 0xe0:
if (mod) {
int v = datavals(s->swap?val/mod:val%mod, s->step, s->steps, s->n_steps);
printf("PB[%d]-%d%s ", v-8192, channel, suffix);
} else if (s->steps) {
printf("PB{");
for (int i = 0; i < s->n_steps; i++)
printf("%s%d", i?",":"", s->steps[i]);
printf("}-%d%s ", channel, suffix);
} else if (s->step)
printf("PB[%d]-%d%s ", s->step, channel, suffix);
else
printf("PB-%d%s ", channel, suffix);
break;
default: // this can't happen
break;
}
}
}
}
void
print_stroke_sequence(char *name, char *up_or_down, stroke *s,
int mod, int step, int n_steps, int *steps,
int val)
{
if (up_or_down && *up_or_down)
printf("%s[%s]: ", name, up_or_down);
else
printf("%s: ", name);
while (s) {
print_stroke(s, mod, step, n_steps, steps, val);
s = s->next;
}
printf("\n");
}
stroke **first_stroke;
stroke *last_stroke;
stroke **press_first_stroke;
stroke **release_first_stroke;
int is_keystroke, is_bidirectional, is_nop, midi_release, explicit_release;
int mode;
char *current_translation;
char *key_name;
int first_release_stroke; // is this the first stroke of a release?
KeySym regular_key_down;
#define NUM_MODIFIERS 64
stroke modifiers_down[NUM_MODIFIERS];
int modifier_count;
int midi_channel;
void
append_stroke(KeySym sym, int press)
{
stroke *s = (stroke *)allocate(sizeof(stroke));
memset(s, 0, sizeof(stroke));
s->keysym = sym;
s->press = press;
if (*first_stroke) {
last_stroke->next = s;
} else {
*first_stroke = s;
}
last_stroke = s;
}
void
append_shift(int shift)
{
stroke *s = (stroke *)allocate(sizeof(stroke));
memset(s, 0, sizeof(stroke));
s->shift = shift;
if (*first_stroke) {
last_stroke->next = s;
} else {
*first_stroke = s;
}
last_stroke = s;
}
void
append_nop(void)
{
stroke *s = (stroke *)allocate(sizeof(stroke));
memset(s, 0, sizeof(stroke));
if (*first_stroke) {
last_stroke->next = s;
} else {
*first_stroke = s;
}
last_stroke = s;
is_nop = is_keystroke;
}
void
append_midi(int status, int data, int step, int n_steps, int *steps,
int swap, int change, int incr, int recursive, int feedback)
{
stroke *s = (stroke *)allocate(sizeof(stroke));
memset(s, 0, sizeof(stroke));
s->status = status;
s->data = data;
s->swap = swap;
s->change = change;
s->step = step;
s->n_steps = n_steps;
s->steps = stepsdup(n_steps, steps);
s->incr = incr;
s->recursive = recursive;
s->feedback = feedback;
// if this is a keystroke event, for all messages but program change (which
// has no "on" and "off" states), mark the event as "dirty" so that the
// corresponding "off" event gets added later to the "release" strokes
s->dirty = is_keystroke && ((status&0xf0) != 0xc0);
if (*first_stroke) {
last_stroke->next = s;
} else {
*first_stroke = s;
}
last_stroke = s;
midi_release = 1;
}
// s->press values in modifiers_down:
// PRESS -> down
// HOLD -> held
// PRESS_RELEASE -> released, but to be re-pressed if necessary
// RELEASE -> up
void
mark_as_down(KeySym sym, int hold)
{
int i;
for (i=0; i<modifier_count; i++) {
if (modifiers_down[i].keysym == sym) {
modifiers_down[i].press = hold ? HOLD : PRESS;
return;
}
}
if (modifier_count > NUM_MODIFIERS) {
fprintf(stderr, "too many modifiers down in [%s]%s\n", current_translation, key_name);
return;
}
modifiers_down[modifier_count].keysym = sym;
modifiers_down[modifier_count].press = hold ? HOLD : PRESS;
modifier_count++;
}
void
mark_as_up(KeySym sym)
{
int i;
for (i=0; i<modifier_count; i++) {
if (modifiers_down[i].keysym == sym) {
modifiers_down[i].press = RELEASE;
return;
}
}
}
void
release_modifiers(int allkeys)
{
int i;
for (i=0; i<modifier_count; i++) {
if (modifiers_down[i].press == PRESS) {
append_stroke(modifiers_down[i].keysym, 0);
modifiers_down[i].press = PRESS_RELEASE;
} else if (allkeys && modifiers_down[i].press == HOLD) {
append_stroke(modifiers_down[i].keysym, 0);
modifiers_down[i].press = RELEASE;
}
}
}
void
re_press_temp_modifiers(void)
{
int i;
for (i=0; i<modifier_count; i++) {
if (modifiers_down[i].press == PRESS_RELEASE) {
append_stroke(modifiers_down[i].keysym, 1);
modifiers_down[i].press = PRESS;
}
}
}
/* Parser for the MIDI message syntax. The same parser is used for both
the left-hand side (lhs) and the right-hand side (rhs) of a translation.
The syntax we actually parse here is the following:
tok ::= msg [ number ] [ steps ] [ "-" number] [ flag ]
msg ::= note | other
note ::= ( "a" | ... | "g" ) [ "#" | "b" ]
other ::= "ch" | "pb" | "pc" | "cc" | "cp" | "kp:" note
steps ::= [ "[" [ number ] "]" ] [ "{" list "}" ]
list ::= number { "," number | ":" number | "-" number }
flag ::= "-" | "+" | "=" | "<" | ">" | "~" | "'"
Case is insignificant. Numbers are always in decimal. The meaning of
the first number depends on the context (octave number for notes and
key pressure, the actual data byte for other messages). This can
optionally be followed by a number in brackets, denoting a step size,
or (in some translations) a list of values in curly braces. Also