Skip to content

Commit

Permalink
Added service, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Apfelwurm committed Sep 7, 2021
1 parent 715df93 commit d079563
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
5 changes: 5 additions & 0 deletions rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
82 changes: 57 additions & 25 deletions src/volumehidtoosc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/



//includes
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
Expand All @@ -36,6 +36,7 @@
#include <confuse.h>
#include "lo/lo.h"

//define config
cfg_opt_t opts[] =
{
CFG_STR("VENDOR", "Vendor=2341", CFGF_NONE),
Expand All @@ -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];
Expand All @@ -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));
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -134,22 +139,26 @@ 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;
}


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;
Expand All @@ -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"))
Expand Down Expand Up @@ -255,28 +287,28 @@ int main(void)
{
if (volume != sentvolume)
{
printf("sendvol %f\n", volume);
sendosc(volume);
sentvolume = volume;
}
}

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);

}
14 changes: 7 additions & 7 deletions volumehidtoosc.conf
Original file line number Diff line number Diff line change
@@ -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

12 changes: 12 additions & 0 deletions volumehidtoosc.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d079563

Please sign in to comment.