From d07956339ebbbaa8c0323cfa9f70b266beba7ed5 Mon Sep 17 00:00:00 2001 From: Alexander Volz Date: Tue, 7 Sep 2021 20:58:14 +0200 Subject: [PATCH] Added service, added comments --- CMakeLists.txt | 4 +-- rebuild.sh | 5 +++ src/volumehidtoosc.c | 82 +++++++++++++++++++++++++++++------------- volumehidtoosc.conf | 14 ++++---- volumehidtoosc.service | 12 +++++++ 5 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 volumehidtoosc.service diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f25f80..6174c17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ add_subdirectory (src) set (VOLUMEHIDTOOSC_CONF_DIR "/etc/volumehidtoosc") # Directory with systemd unit files -set (SYSTEMD_UNIT_DIR "/usr/lib/systemd/user/") +set (SYSTEMD_UNIT_DIR "/usr/lib/systemd/system/") # Macro for installing configuration files function(install_conf src dest) @@ -70,7 +70,7 @@ endfunction(install_conf) target_link_libraries(volumehidtoosc confuse lo) # Install systemd unit files -#install_conf (./volumehidtoosc.service ${SYSTEMD_UNIT_DIR}) +install_conf (./volumehidtoosc.service ${SYSTEMD_UNIT_DIR}) # Install config install_conf (./volumehidtoosc.conf ${VOLUMEHIDTOOSC_CONF_DIR}) diff --git a/rebuild.sh b/rebuild.sh index 86f2ac6..c7df2f3 100755 --- a/rebuild.sh +++ b/rebuild.sh @@ -8,8 +8,13 @@ mkdir packageroot cp -rf ../DEBIAN packageroot/ mkdir -p packageroot/usr/bin/ mkdir -p packageroot/etc/volumehidtoosc/ +mkdir -p packageroot/usr/lib/systemd/system/ +mkdir -p packageroot/var/log/volumehidtoosc/ + cp bin/volumehidtoosc packageroot/usr/bin/ cp ../volumehidtoosc.conf packageroot/etc/volumehidtoosc/ +cp ../volumehidtoosc.service packageroot/usr/lib/systemd/system/ +touch packageroot/var/log/volumehidtoosc/volumehidtoosc.log dpkg-deb -b packageroot volumehidtoosc_1.0.0_amd64.deb diff --git a/src/volumehidtoosc.c b/src/volumehidtoosc.c index bce0b91..4ef40f0 100644 --- a/src/volumehidtoosc.c +++ b/src/volumehidtoosc.c @@ -23,7 +23,7 @@ */ - +//includes #include #include #include @@ -36,6 +36,7 @@ #include #include "lo/lo.h" +//define config cfg_opt_t opts[] = { CFG_STR("VENDOR", "Vendor=2341", CFGF_NONE), @@ -56,13 +57,15 @@ cfg_opt_t opts[] = CFG_FLOAT("VOL_START",0.2,CFGF_NONE), CFG_END() }; -cfg_t *cfg; +//create objects +cfg_t *cfg; lo_address t; +FILE *logfile= NULL; - -char *extract_keyboard_eventname() +//get hid eventname based on configured filters +char *extract_hid_eventname() { FILE *fp = NULL; char buffer[1024]; @@ -75,7 +78,8 @@ char *extract_keyboard_eventname() fp = fopen("/proc/bus/input/devices", "r"); if (!fp) { int err = errno; - fprintf(stderr, "Unable to open file. %s\n", strerror(err)); + fprintf(logfile, "Unable to open file. %s\n", strerror(err)); + fflush(logfile); return NULL; } memset(buffer, 0, sizeof(buffer)); @@ -90,7 +94,8 @@ char *extract_keyboard_eventname() *ptr2 = '\0'; eventname = strdup(ptr); if (!eventname) { - fprintf(stderr, "Out of memory.\n"); + fprintf(logfile, "Out of memory.\n"); + fflush(logfile); break; } } @@ -134,13 +139,13 @@ char *extract_keyboard_eventname() } } +//send float value via osc int sendosc(float currvol) { - - - + printf("sendvol %f\n", currvol); if (lo_send(t, cfg_getstr(cfg, "OSC_PATH"), "ff", currvol) == -1) { - printf("OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); + fprintf(logfile, "OSC error %d: %s\n", lo_address_errno(t), lo_address_errstr(t)); + fflush(logfile); } return 0; } @@ -148,8 +153,12 @@ int sendosc(float currvol) int main(void) { + //create objects + char *evdev; int fd; struct input_event evt; + + //nessecary running variables int volpluscount = 0; int volminuscount = 0; int mutetogglecount = 0; @@ -158,42 +167,65 @@ int main(void) bool mutetrigger = false; bool unmutetrigger = false; bool mute = false; - char *evdev; + //initialize logging + logfile = fopen("/var/log/volumehidtoosc/volumehidtoosc.log", "w"); + + if (!logfile) { + int err = errno; + printf("Unable to open logfile. %s\n", strerror(err)); + sleep(15); + exit(1); + } + + fprintf(logfile, "volumehidtoosc started!\n"); + fflush(logfile); + + //initialize config cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, "/etc/volumehidtoosc/volumehidtoosc.conf") == CFG_PARSE_ERROR) { - printf("Config error"); + fprintf(logfile, "config error, exit!\n"); + fflush(logfile); + sleep(15); exit(1); } - + //create osc address object t = lo_address_new(cfg_getstr(cfg, "IP"), cfg_getstr(cfg, "PORT")); - volume = cfg_getfloat(cfg, "VOL_START"); - sendosc(volume); - sentvolume = volume; - if (extract_keyboard_eventname() == "not found") + + //get hid device and exif if not available + if (strcmp(extract_hid_eventname(), "not found") == 0 || extract_hid_eventname() == NULL) { - printf("Controller not found"); + fprintf(logfile, "controller not found or error when opening file! exit!\n"); + fflush(logfile); + sleep(15); exit(1); } else { - fprintf(stderr, "selected /dev/input/%s\n", extract_keyboard_eventname()); - asprintf(&evdev, "%s%s", "/dev/input/", extract_keyboard_eventname()); + fprintf(logfile, "selected /dev/input/%s\n", extract_hid_eventname()); + fflush(logfile); + asprintf(&evdev, "%s%s", "/dev/input/", extract_hid_eventname()); } + //send initial float value + volume = cfg_getfloat(cfg, "VOL_START"); + sendosc(volume); + sentvolume = volume; + + //open hid device fd = open(evdev, O_RDWR); ioctl(fd, EVIOCGRAB, 1); while(read(fd, &evt, sizeof(struct input_event)) > 0) { - + //count and mute logic if (!mute) { if (evt.code == cfg_getint(cfg, "VOL_PLUS")) @@ -255,7 +287,6 @@ int main(void) { if (volume != sentvolume) { - printf("sendvol %f\n", volume); sendosc(volume); sentvolume = volume; } @@ -263,20 +294,21 @@ int main(void) if(mutetrigger) { - printf("sendvol %f\n", cfg_getfloat(cfg, "VOL_MIN")); + sendosc(cfg_getfloat(cfg, "VOL_MIN")); mutetrigger = false; mute = true; } if(unmutetrigger) { - printf("sendvol %f\n", volume); sendosc(volume); unmutetrigger = false; mute = false; } } - + fprintf(logfile, "hid removed! exit!\n"); + fflush(logfile); + fclose(logfile); } diff --git a/volumehidtoosc.conf b/volumehidtoosc.conf index e7e3f2a..1b731a6 100644 --- a/volumehidtoosc.conf +++ b/volumehidtoosc.conf @@ -1,17 +1,17 @@ VENDOR='Vendor=2341' PRODUCT='Product=484d' EV='EV=1f' -IP='10.10.10.227' -PORT='8000' -OSC_PATH='/mastervolume' +IP='10.10.10.220' +PORT='7001' +OSC_PATH='/1/mastervolume' VOL_PLUS=115 VOL_PLUS_TIMES=2 VOL_MINUS=114 VOL_MINUS_TIMES=2 MUTE_TOGGLE=113 MUTE_TOGGLE_TIMES=2 -VOL_MIN=0 -VOL_MAX=100 -VOL_STEP=2 -VOL_START=30 +VOL_MIN=0.0 +VOL_MAX=1.0 +VOL_STEP=0.002 +VOL_START=0.2 diff --git a/volumehidtoosc.service b/volumehidtoosc.service new file mode 100644 index 0000000..6c249b2 --- /dev/null +++ b/volumehidtoosc.service @@ -0,0 +1,12 @@ +[Unit] +Description=volumehidtoosc + +[Service] +Type=simple +ExecStart=/usr/bin/volumehidtoosc +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +RestartSec=30 + +[Install] +WantedBy=default.target \ No newline at end of file