From 49567866c314ea7267fc8cec961f80a60840bdc6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 13 Oct 2015 02:17:00 +0200 Subject: renamed files to prepare merge of code dirs --- client/Makefile | 4 +- client/dolmetschctl-client.c | 4 +- client/midi.c | 242 ------------------------------------------- client/midi.h | 54 ---------- client/midi_client.c | 242 +++++++++++++++++++++++++++++++++++++++++++ client/midi_client.h | 54 ++++++++++ client/osc.c | 123 ---------------------- client/osc.h | 41 -------- client/osc_client.c | 123 ++++++++++++++++++++++ client/osc_client.h | 41 ++++++++ 10 files changed, 464 insertions(+), 464 deletions(-) delete mode 100644 client/midi.c delete mode 100644 client/midi.h create mode 100644 client/midi_client.c create mode 100644 client/midi_client.h delete mode 100644 client/osc.c delete mode 100644 client/osc.h create mode 100644 client/osc_client.c create mode 100644 client/osc_client.h (limited to 'client') diff --git a/client/Makefile b/client/Makefile index bb767d9..0d47f83 100644 --- a/client/Makefile +++ b/client/Makefile @@ -27,8 +27,8 @@ endif EXECUTABLE := dolmetschctl-client C_OBJS := slist.o \ - midi.o \ - osc.o \ + midi_client.o \ + osc_client.o \ dolmetschctl-client.o C_SRCS := $(C_OBJS:%.o=%.c) diff --git a/client/dolmetschctl-client.c b/client/dolmetschctl-client.c index b67f238..9815924 100644 --- a/client/dolmetschctl-client.c +++ b/client/dolmetschctl-client.c @@ -30,8 +30,8 @@ #include #include -#include "midi.h" -#include "osc.h" +#include "midi_client.h" +#include "osc_client.h" void print_version() diff --git a/client/midi.c b/client/midi.c deleted file mode 100644 index 7075432..0000000 --- a/client/midi.c +++ /dev/null @@ -1,242 +0,0 @@ -/* - * dolmetschctl - * - * - * Copyright (C) 2015 Christian Pointner - * - * This file is part of dolmetschctl. - * - * dolmetschctl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * dolmetschctl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with dolmetschctl. If not, see . - */ - -#include "config.h" - -#include -#include -#include -#include - -#include "midi.h" - -#define NOTE_EN 0x00 -#define NOTE_DE 0x01 -u_int8_t led_cmd_en[] = { 0xB0, NOTE_EN, 0x01, 0xB0, NOTE_DE, 0x00 }; -u_int8_t led_cmd_de[] = { 0xB0, NOTE_EN, 0x00, 0xB0, NOTE_DE, 0x01 }; - -static void free_cmd_entry(void* ptr) -{ - cmd_t* c = ptr; - assert(c); - free(c->buf_); - free(c); -} - -int midi_init(midi_t* m, const char* device) -{ - assert(m != NULL); - - m->input_ = NULL; - m->output_ = NULL; - memset(m->buf_, 0, sizeof(m->buf_)); - m->read_idx_ = 0; - slist_init(&(m->cmds_), free_cmd_entry); - - int ret = snd_rawmidi_open(&(m->input_), &(m->output_), device, SND_RAWMIDI_NONBLOCK); - if(ret < 0) { - error(0, 0, "MIDI: cannot open port '%s': %s", device, snd_strerror(ret)); - return ret; - } - - return 0; -} - -int midi_enqueue_cmd(midi_t* m, const char* lang) -{ - assert(m && lang); - - int len; - const u_int8_t* src; - if(!strcmp(lang, "en")) { - len = sizeof(led_cmd_en); - src = led_cmd_en; - } - else if(!strcmp(lang, "de")) { - len = sizeof(led_cmd_de); - src = led_cmd_de; - } - else - return 0; - - cmd_t* cmd = malloc(sizeof(cmd_t)); - assert(cmd); - cmd->len_ = len; - assert((cmd->buf_ = malloc(cmd->len_))); - memcpy(cmd->buf_, src, cmd->len_); - cmd->write_idx_ = 0; - - slist_add(&(m->cmds_), cmd); - - return 0; -} - -int midi_get_poll_fd_count(midi_t* m) -{ - assert(m); - - m->in_pfds_cnt_ = snd_rawmidi_poll_descriptors_count(m->input_); - assert(m->in_pfds_cnt_ > 0); - m->out_pfds_cnt_ = snd_rawmidi_poll_descriptors_count(m->output_); - assert(m->out_pfds_cnt_ > 0); - - return (m->in_pfds_cnt_ + m->out_pfds_cnt_); -} - -int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds) -{ - assert(m && pfds && npfds); - - snd_rawmidi_poll_descriptors(m->input_, pfds, m->in_pfds_cnt_); - snd_rawmidi_poll_descriptors(m->output_, &(pfds[m->in_pfds_cnt_]), npfds - m->in_pfds_cnt_); - - if(!slist_length(&(m->cmds_))) { - int i; - for(i = m->in_pfds_cnt_; i < npfds; ++i) - pfds[i].events = 0; - } - return (m->in_pfds_cnt_ + m->out_pfds_cnt_); -} - -static int midi_enqueue_lang_switch(midi_t* m, osc_t* o, const char* lang) -{ - return osc_switch_lang(o, lang); -} - -static int midi_handle_note_on(midi_t* m, osc_t* o) -{ - int ret = 0; - switch(m->buf_[1]) { - case NOTE_EN: ret = midi_enqueue_lang_switch(m, o,"en"); break; - case NOTE_DE: ret = midi_enqueue_lang_switch(m, o, "de"); break; - default: printf("ignoring unknown note\n"); break; - } - return ret; -} - -static int midi_handle_note_off(midi_t* m, osc_t* o) -{ - return 0; -} - -static int midi_handle_message(midi_t* m, osc_t* o) -{ - /* int i; */ - /* printf("MIDI: "); */ - /* for (i = 0; i < sizeof(m->buf_); ++i) */ - /* printf("%02X%c", m->buf_[i], (i >= (sizeof(m->buf_)-1)) ? '\n' : ' '); */ - - int ret = 0; - switch(m->buf_[0]) { - case 0x90: ret = midi_handle_note_on(m, o); break; - case 0x80: ret = midi_handle_note_off(m, o); break; - default: printf("ignoring unknown midi command\n"); break; - } - - return ret; -} - -static int midi_handle_in_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o) -{ - int err; - unsigned short revents; - if((err = snd_rawmidi_poll_descriptors_revents(m->input_, pfds, npfds, &revents)) < 0) { - error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); - return -1; - } - if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - error(0, 0, "MIDI: got POLLERR, POLLHUP or POLLNVAL"); - return -1; - } - if(!(revents & POLLIN)) - return 0; - - int ret = snd_rawmidi_read(m->input_, &(m->buf_[m->read_idx_]), sizeof(m->buf_) - m->read_idx_); - if(ret == -EAGAIN) - return 0; - if(ret < 0) { - error(0, 0, "MIDI: cannot read from midi port: %s", snd_strerror(ret)); - return -1; - } - m->read_idx_ += ret; - if(m->read_idx_ >= sizeof(m->buf_)) { - ret = midi_handle_message(m, o); - memset(m->buf_, 0, sizeof(m->buf_)); - m->read_idx_ = 0; - return ret; - } - - return ret; -} - -static int midi_handle_out_revents(midi_t* m, struct pollfd *pfds, int npfds) -{ - int err; - unsigned short revents; - if((err = snd_rawmidi_poll_descriptors_revents(m->output_, pfds, npfds, &revents)) < 0) { - error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); - return -1; - } - if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - error(0, 0, "MIDI: got POLLERR, POLLHUP or POLLNVAL"); - return -1; - } - if(!(revents & POLLOUT)) - return 0; - - assert(m->cmds_.first_); - cmd_t* cmd = (cmd_t*)(m->cmds_.first_->data_); - assert(cmd); - assert(cmd->buf_); - - int ret = snd_rawmidi_write(m->output_, &(cmd->buf_[cmd->write_idx_]), cmd->len_ - cmd->write_idx_); - if(ret == -EAGAIN) - return 0; - if(ret < 0) { - error(0, 0, "MIDI: cannot write to port: %s", snd_strerror(ret)); - return -1; - } - cmd->write_idx_ += ret; - if(cmd->write_idx_ >= cmd->len_) { - if((err = snd_rawmidi_drain(m->output_)) < 0) { - error(0, 0, "MIDI: cannot drain output: %s", snd_strerror(err)); - return -1; - } - slist_remove(&(m->cmds_), cmd); - } - - return 0; -} - -int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o) -{ - assert(m); - - if(!m->input_ || !m->output_) - return 0; - - int ret = midi_handle_in_revents(m, pfds, m->in_pfds_cnt_, o); - if(ret) - return ret; - - return midi_handle_out_revents(m, &(pfds[m->in_pfds_cnt_]), m->out_pfds_cnt_); -} diff --git a/client/midi.h b/client/midi.h deleted file mode 100644 index 1a051ae..0000000 --- a/client/midi.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * dolmetschctl - * - * - * Copyright (C) 2015 Christian Pointner - * - * This file is part of dolmetschctl. - * - * dolmetschctl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * dolmetschctl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with dolmetschctl. If not, see . - */ - -#ifndef DOLMETSCHCTL_midi_h_INCLUDED -#define DOLMETSCHCTL_midi_h_INCLUDED - -#include -#include - -#include "slist.h" -#include "osc.h" - -typedef struct { - u_int8_t* buf_; - int len_; - int write_idx_; -} cmd_t; - -typedef struct { - snd_rawmidi_t* input_; - int in_pfds_cnt_; - snd_rawmidi_t* output_; - int out_pfds_cnt_; - u_int8_t buf_[3]; - int read_idx_; - slist_t cmds_; -} midi_t; - -int midi_init(midi_t* m, const char* device); -int midi_enqueue_cmd(midi_t* m, const char* lang); -int midi_get_poll_fd_count(midi_t* m); -int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds); -int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o); - -#endif diff --git a/client/midi_client.c b/client/midi_client.c new file mode 100644 index 0000000..456ec7d --- /dev/null +++ b/client/midi_client.c @@ -0,0 +1,242 @@ +/* + * dolmetschctl + * + * + * Copyright (C) 2015 Christian Pointner + * + * This file is part of dolmetschctl. + * + * dolmetschctl is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * dolmetschctl is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dolmetschctl. If not, see . + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "midi_client.h" + +#define NOTE_EN 0x00 +#define NOTE_DE 0x01 +u_int8_t led_cmd_en[] = { 0xB0, NOTE_EN, 0x01, 0xB0, NOTE_DE, 0x00 }; +u_int8_t led_cmd_de[] = { 0xB0, NOTE_EN, 0x00, 0xB0, NOTE_DE, 0x01 }; + +static void free_cmd_entry(void* ptr) +{ + cmd_t* c = ptr; + assert(c); + free(c->buf_); + free(c); +} + +int midi_init(midi_t* m, const char* device) +{ + assert(m != NULL); + + m->input_ = NULL; + m->output_ = NULL; + memset(m->buf_, 0, sizeof(m->buf_)); + m->read_idx_ = 0; + slist_init(&(m->cmds_), free_cmd_entry); + + int ret = snd_rawmidi_open(&(m->input_), &(m->output_), device, SND_RAWMIDI_NONBLOCK); + if(ret < 0) { + error(0, 0, "MIDI: cannot open port '%s': %s", device, snd_strerror(ret)); + return ret; + } + + return 0; +} + +int midi_enqueue_cmd(midi_t* m, const char* lang) +{ + assert(m && lang); + + int len; + const u_int8_t* src; + if(!strcmp(lang, "en")) { + len = sizeof(led_cmd_en); + src = led_cmd_en; + } + else if(!strcmp(lang, "de")) { + len = sizeof(led_cmd_de); + src = led_cmd_de; + } + else + return 0; + + cmd_t* cmd = malloc(sizeof(cmd_t)); + assert(cmd); + cmd->len_ = len; + assert((cmd->buf_ = malloc(cmd->len_))); + memcpy(cmd->buf_, src, cmd->len_); + cmd->write_idx_ = 0; + + slist_add(&(m->cmds_), cmd); + + return 0; +} + +int midi_get_poll_fd_count(midi_t* m) +{ + assert(m); + + m->in_pfds_cnt_ = snd_rawmidi_poll_descriptors_count(m->input_); + assert(m->in_pfds_cnt_ > 0); + m->out_pfds_cnt_ = snd_rawmidi_poll_descriptors_count(m->output_); + assert(m->out_pfds_cnt_ > 0); + + return (m->in_pfds_cnt_ + m->out_pfds_cnt_); +} + +int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds) +{ + assert(m && pfds && npfds); + + snd_rawmidi_poll_descriptors(m->input_, pfds, m->in_pfds_cnt_); + snd_rawmidi_poll_descriptors(m->output_, &(pfds[m->in_pfds_cnt_]), npfds - m->in_pfds_cnt_); + + if(!slist_length(&(m->cmds_))) { + int i; + for(i = m->in_pfds_cnt_; i < npfds; ++i) + pfds[i].events = 0; + } + return (m->in_pfds_cnt_ + m->out_pfds_cnt_); +} + +static int midi_enqueue_lang_switch(midi_t* m, osc_t* o, const char* lang) +{ + return osc_switch_lang(o, lang); +} + +static int midi_handle_note_on(midi_t* m, osc_t* o) +{ + int ret = 0; + switch(m->buf_[1]) { + case NOTE_EN: ret = midi_enqueue_lang_switch(m, o,"en"); break; + case NOTE_DE: ret = midi_enqueue_lang_switch(m, o, "de"); break; + default: printf("ignoring unknown note\n"); break; + } + return ret; +} + +static int midi_handle_note_off(midi_t* m, osc_t* o) +{ + return 0; +} + +static int midi_handle_message(midi_t* m, osc_t* o) +{ + /* int i; */ + /* printf("MIDI: "); */ + /* for (i = 0; i < sizeof(m->buf_); ++i) */ + /* printf("%02X%c", m->buf_[i], (i >= (sizeof(m->buf_)-1)) ? '\n' : ' '); */ + + int ret = 0; + switch(m->buf_[0]) { + case 0x90: ret = midi_handle_note_on(m, o); break; + case 0x80: ret = midi_handle_note_off(m, o); break; + default: printf("ignoring unknown midi command\n"); break; + } + + return ret; +} + +static int midi_handle_in_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o) +{ + int err; + unsigned short revents; + if((err = snd_rawmidi_poll_descriptors_revents(m->input_, pfds, npfds, &revents)) < 0) { + error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); + return -1; + } + if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { + error(0, 0, "MIDI: got POLLERR, POLLHUP or POLLNVAL"); + return -1; + } + if(!(revents & POLLIN)) + return 0; + + int ret = snd_rawmidi_read(m->input_, &(m->buf_[m->read_idx_]), sizeof(m->buf_) - m->read_idx_); + if(ret == -EAGAIN) + return 0; + if(ret < 0) { + error(0, 0, "MIDI: cannot read from midi port: %s", snd_strerror(ret)); + return -1; + } + m->read_idx_ += ret; + if(m->read_idx_ >= sizeof(m->buf_)) { + ret = midi_handle_message(m, o); + memset(m->buf_, 0, sizeof(m->buf_)); + m->read_idx_ = 0; + return ret; + } + + return ret; +} + +static int midi_handle_out_revents(midi_t* m, struct pollfd *pfds, int npfds) +{ + int err; + unsigned short revents; + if((err = snd_rawmidi_poll_descriptors_revents(m->output_, pfds, npfds, &revents)) < 0) { + error(0, 0, "MIDI: cannot get poll events: %s", snd_strerror(errno)); + return -1; + } + if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { + error(0, 0, "MIDI: got POLLERR, POLLHUP or POLLNVAL"); + return -1; + } + if(!(revents & POLLOUT)) + return 0; + + assert(m->cmds_.first_); + cmd_t* cmd = (cmd_t*)(m->cmds_.first_->data_); + assert(cmd); + assert(cmd->buf_); + + int ret = snd_rawmidi_write(m->output_, &(cmd->buf_[cmd->write_idx_]), cmd->len_ - cmd->write_idx_); + if(ret == -EAGAIN) + return 0; + if(ret < 0) { + error(0, 0, "MIDI: cannot write to port: %s", snd_strerror(ret)); + return -1; + } + cmd->write_idx_ += ret; + if(cmd->write_idx_ >= cmd->len_) { + if((err = snd_rawmidi_drain(m->output_)) < 0) { + error(0, 0, "MIDI: cannot drain output: %s", snd_strerror(err)); + return -1; + } + slist_remove(&(m->cmds_), cmd); + } + + return 0; +} + +int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o) +{ + assert(m); + + if(!m->input_ || !m->output_) + return 0; + + int ret = midi_handle_in_revents(m, pfds, m->in_pfds_cnt_, o); + if(ret) + return ret; + + return midi_handle_out_revents(m, &(pfds[m->in_pfds_cnt_]), m->out_pfds_cnt_); +} diff --git a/client/midi_client.h b/client/midi_client.h new file mode 100644 index 0000000..7ae65bd --- /dev/null +++ b/client/midi_client.h @@ -0,0 +1,54 @@ +/* + * dolmetschctl + * + * + * Copyright (C) 2015 Christian Pointner + * + * This file is part of dolmetschctl. + * + * dolmetschctl is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * dolmetschctl is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dolmetschctl. If not, see . + */ + +#ifndef DOLMETSCHCTL_midi_h_INCLUDED +#define DOLMETSCHCTL_midi_h_INCLUDED + +#include +#include + +#include "slist.h" +#include "osc_client.h" + +typedef struct { + u_int8_t* buf_; + int len_; + int write_idx_; +} cmd_t; + +typedef struct { + snd_rawmidi_t* input_; + int in_pfds_cnt_; + snd_rawmidi_t* output_; + int out_pfds_cnt_; + u_int8_t buf_[3]; + int read_idx_; + slist_t cmds_; +} midi_t; + +int midi_init(midi_t* m, const char* device); +int midi_enqueue_cmd(midi_t* m, const char* lang); +int midi_get_poll_fd_count(midi_t* m); +int midi_get_poll_fds(midi_t* m, struct pollfd *pfds, int npfds); +int midi_handle_revents(midi_t* m, struct pollfd *pfds, int npfds, osc_t* o); + +#endif diff --git a/client/osc.c b/client/osc.c deleted file mode 100644 index 79ee76e..0000000 --- a/client/osc.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * dolmetschctl - * - * - * Copyright (C) 2015 Christian Pointner - * - * This file is part of dolmetschctl. - * - * dolmetschctl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * dolmetschctl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with dolmetschctl. If not, see . - */ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include "osc.h" -#include "midi.h" - - -static void print_error(int num, const char *msg, const char *path) -{ - error(0, 0, "liblo server error %d in path %s: %s", num, path, msg); -} - -static int lang_handler(const char *path, const char *types, lo_arg ** argv, - int argc, lo_message msg, void *user_data) -{ - osc_t* o = (osc_t*)user_data; - assert(o); - - if(argc != 1 || !lo_is_string_type((lo_type)types[0])) - return 1; - -// printf("got ack for: lang '%s'\n", &(argv[0]->s)); - midi_enqueue_cmd((midi_t*)o->m_, &(argv[0]->s)); - - return 0; -} - -int osc_init(osc_t* o, void* m, const char* host, const char* port) -{ - assert(o != NULL); - - o->server_ = NULL; - o->target_ = lo_address_new(host, port); - o->m_ = m; - - if(!port) - return 0; - - o->server_ = lo_server_new(NULL, print_error); - if(!o->server_) - return -1; - - if(!lo_server_add_method(o->server_, "/lang/ack", "s", lang_handler, (void*)o)) - return -1; - - return 0; -} - -int osc_switch_lang(osc_t* o, const char* lang) -{ - lo_send_from(o->target_, o->server_, LO_TT_IMMEDIATE, "/lang/switch", "s", lang); - return 0; -} - -int osc_get_poll_fd_count(osc_t* o) -{ - assert(o); - - if(!o->server_) - return 0; - - return 1; -} - -int osc_get_poll_fds(osc_t* o, struct pollfd *pfds, int npfds) -{ - assert(o); - - if(!o->server_) - return 0; - - pfds[0].fd = lo_server_get_socket_fd(o->server_); - pfds[0].events = POLLIN; - pfds[0].revents = 0; - - return 0; -} - -int osc_handle_revents(osc_t* o, struct pollfd *pfds, int npfds) -{ - assert(o); - - if(!o->server_) - return 0; - - if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - error(0, 0, "OSC: got POLLERR, POLLHUP or POLLNVAL"); - return -1; - } - if(!(pfds[0].revents & POLLIN)) - return 0; - - lo_server_recv_noblock(o->server_, 0); - - return 0; -} diff --git a/client/osc.h b/client/osc.h deleted file mode 100644 index 9d1ad94..0000000 --- a/client/osc.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * dolmetschctl - * - * - * Copyright (C) 2015 Christian Pointner - * - * This file is part of dolmetschctl. - * - * dolmetschctl is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * dolmetschctl is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with dolmetschctl. If not, see . - */ - -#ifndef DOLMETSCHCTL_osc_h_INCLUDED -#define DOLMETSCHCTL_osc_h_INCLUDED - -#include "lo/lo.h" -#include - -typedef struct { - lo_server server_; - lo_address target_; - void* m_; // forward declarations of typedes are not possible -- this is a HACK! FIXME!!! -} osc_t; - -int osc_init(osc_t* o, void* m, const char* host, const char* port); -int osc_switch_lang(osc_t* o, const char* lang); -int osc_get_poll_fd_count(osc_t* o); -int osc_get_poll_fds(osc_t* o, struct pollfd *pfds, int npfds); -int osc_handle_revents(osc_t* o, struct pollfd *pfds, int npfds); - -#endif diff --git a/client/osc_client.c b/client/osc_client.c new file mode 100644 index 0000000..a70eb67 --- /dev/null +++ b/client/osc_client.c @@ -0,0 +1,123 @@ +/* + * dolmetschctl + * + * + * Copyright (C) 2015 Christian Pointner + * + * This file is part of dolmetschctl. + * + * dolmetschctl is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * dolmetschctl is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dolmetschctl. If not, see . + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "osc_client.h" +#include "midi_client.h" + + +static void print_error(int num, const char *msg, const char *path) +{ + error(0, 0, "liblo server error %d in path %s: %s", num, path, msg); +} + +static int lang_handler(const char *path, const char *types, lo_arg ** argv, + int argc, lo_message msg, void *user_data) +{ + osc_t* o = (osc_t*)user_data; + assert(o); + + if(argc != 1 || !lo_is_string_type((lo_type)types[0])) + return 1; + +// printf("got ack for: lang '%s'\n", &(argv[0]->s)); + midi_enqueue_cmd((midi_t*)o->m_, &(argv[0]->s)); + + return 0; +} + +int osc_init(osc_t* o, void* m, const char* host, const char* port) +{ + assert(o != NULL); + + o->server_ = NULL; + o->target_ = lo_address_new(host, port); + o->m_ = m; + + if(!port) + return 0; + + o->server_ = lo_server_new(NULL, print_error); + if(!o->server_) + return -1; + + if(!lo_server_add_method(o->server_, "/lang/ack", "s", lang_handler, (void*)o)) + return -1; + + return 0; +} + +int osc_switch_lang(osc_t* o, const char* lang) +{ + lo_send_from(o->target_, o->server_, LO_TT_IMMEDIATE, "/lang/switch", "s", lang); + return 0; +} + +int osc_get_poll_fd_count(osc_t* o) +{ + assert(o); + + if(!o->server_) + return 0; + + return 1; +} + +int osc_get_poll_fds(osc_t* o, struct pollfd *pfds, int npfds) +{ + assert(o); + + if(!o->server_) + return 0; + + pfds[0].fd = lo_server_get_socket_fd(o->server_); + pfds[0].events = POLLIN; + pfds[0].revents = 0; + + return 0; +} + +int osc_handle_revents(osc_t* o, struct pollfd *pfds, int npfds) +{ + assert(o); + + if(!o->server_) + return 0; + + if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { + error(0, 0, "OSC: got POLLERR, POLLHUP or POLLNVAL"); + return -1; + } + if(!(pfds[0].revents & POLLIN)) + return 0; + + lo_server_recv_noblock(o->server_, 0); + + return 0; +} diff --git a/client/osc_client.h b/client/osc_client.h new file mode 100644 index 0000000..9d1ad94 --- /dev/null +++ b/client/osc_client.h @@ -0,0 +1,41 @@ +/* + * dolmetschctl + * + * + * Copyright (C) 2015 Christian Pointner + * + * This file is part of dolmetschctl. + * + * dolmetschctl is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * dolmetschctl is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dolmetschctl. If not, see . + */ + +#ifndef DOLMETSCHCTL_osc_h_INCLUDED +#define DOLMETSCHCTL_osc_h_INCLUDED + +#include "lo/lo.h" +#include + +typedef struct { + lo_server server_; + lo_address target_; + void* m_; // forward declarations of typedes are not possible -- this is a HACK! FIXME!!! +} osc_t; + +int osc_init(osc_t* o, void* m, const char* host, const char* port); +int osc_switch_lang(osc_t* o, const char* lang); +int osc_get_poll_fd_count(osc_t* o); +int osc_get_poll_fds(osc_t* o, struct pollfd *pfds, int npfds); +int osc_handle_revents(osc_t* o, struct pollfd *pfds, int npfds); + +#endif -- cgit v1.2.3