From 2c0714f1499fe4ccd110b236943a41aced4f7ed3 Mon Sep 17 00:00:00 2001 From: David Schneider Date: Mon, 21 Sep 2009 22:51:06 +0000 Subject: [PATCH] Added code. It should work. --- Makefile | 5 +++ mid2key.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test | 15 ++++++++ 3 files changed, 131 insertions(+) create mode 100644 Makefile create mode 100644 mid2key.c create mode 100644 test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..24859b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +LDFLAGS=-lasound -lX11 -lXtst -Wall + +mid2key: mid2key.c +clean: + rm -f mid2key diff --git a/mid2key.c b/mid2key.c new file mode 100644 index 0000000..e516d81 --- /dev/null +++ b/mid2key.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + /*defines*/ + Display* xdp; + FILE* midi; + FILE* config; + const char* file; + KeyCode map[128]; + 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 ((map[temp] = XStringToKeysym(keysym))) { + if (!(map[temp] = XKeysymToKeycode(xdp,map[temp]))) + 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; +} diff --git a/test b/test new file mode 100644 index 0000000..2c73198 --- /dev/null +++ b/test @@ -0,0 +1,15 @@ +31=v +37=v +34=v +48=b +47=n +43=m +33=c +46=b +44= +42=b +49=m +91=m +51=n +89=n +53=n