-
Notifications
You must be signed in to change notification settings - Fork 6
/
mid2key.c
112 lines (104 loc) · 3.44 KB
/
mid2key.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wordexp.h>
#include <alsa/seq_event.h>
#include <alsa/seq_midi_event.h>
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
int main(int argc, char** argv)
{
/*defines*/
Display* xdp;
FILE* midi;
FILE* config;
const char* file;
KeyCode map[128];
KeySym sym;
char buffer[128];
char* keysym;
int temp;
snd_midi_event_t* parser;
snd_seq_event_t event;
wordexp_t file_expanded;
/*usage*/
if ((argc != 2) && (argc != 3)) {
fprintf(stderr,"Usage: %s device [config]\n"
"Defaults to ~/.mid2key if config is unspecified.\n"
"Configuration example:\n"
"#set note #32 to spacebar\n"
"32=space\n"
"#set note #33 to keycode 67\n"
"33=67\n"
"#leave note #34 unset\n"
"34=\n"
"#also set note 35 to space\n"
"35=space\n"
,*argv);
return 1;
}
/*display*/
if (!(xdp = XOpenDisplay(getenv("DISPLAY")))) {
fputs("Unable to open X display.",stderr);
return 2;
}
/*config*/
if (argc == 3) {
file = argv[2];
} else {
file = "~/.mid2key";
}
wordexp(file, &file_expanded, 0);
memset(map,0,sizeof(map));
if (!(config = fopen(file_expanded.we_wordv[0],"r"))) {
fprintf(stderr,"Unable to open configuration file '%s'\nUsing null mapping.\n",file);
} else {
while (fgets(buffer,128,config)) {
if (*buffer != '#') {
if ((keysym = strchr(buffer,'='))) {
temp = atoi(buffer);
if ((temp >= 0) && (temp <= 127)) {
++keysym;
if (strchr(keysym,'\n')) *strchr(keysym,'\n') = '\0';
if ((sym = XStringToKeysym(keysym))) {
if (!(map[temp] = XKeysymToKeycode(xdp,sym)))
fprintf(stderr,"No keycode for keysym '%s'\n",keysym);
} else if (*keysym) {
fprintf(stderr,"Undefined keysym '%s'\n",keysym);
}
}
}
}
}
fclose(config);
}
/*midi*/
if (!(midi = fopen(argv[1],"rb"))) {
fprintf(stderr,"Unable to open MIDI device '%s'\n",argv[1]);
XCloseDisplay(xdp);
return 3;
}
snd_midi_event_new(32,&parser);
/*feed implied note on message*/
snd_midi_event_encode_byte(parser,0x9A,&event);
/*main loop*/
fputs("Ready for midi input\n",stderr);
while ((temp = fgetc(midi)) != EOF) {
if (snd_midi_event_encode_byte(parser, temp, &event) == 1) {
if (event.type == SND_SEQ_EVENT_NOTEON) {
if ((event.data.note.note <= 127) && (event.data.note.velocity)) {
if ((temp = map[event.data.note.note])) {
XTestFakeKeyEvent(xdp,temp,1,CurrentTime);
XTestFakeKeyEvent(xdp,temp,0,CurrentTime);
XFlush(xdp);
}
printf("%i->%i\n",event.data.note.note,(int)map[event.data.note.note]);
}
}
}
}
/*cleanup*/
snd_midi_event_free(parser);
fclose(midi);
return 0;
}