diff --git a/Makefile.in b/Makefile.in index 7914af3..f9b5cdb 100644 --- a/Makefile.in +++ b/Makefile.in @@ -18,7 +18,7 @@ ifeq ($(shell uname -s), Darwin) shared = -dynamiclib else so_major = 0 - so_minor = 1 + so_minor = 2 devlink = lib$(name).so soname = $(devlink).$(so_major) lib_so = $(soname).$(so_minor) @@ -54,8 +54,9 @@ install: $(lib_a) $(lib_so) $(hdr) cp $(lib_so) $(DESTDIR)$(PREFIX)/$(libdir)/$(lib_so) [ -n "$(soname)" ] && \ rm -f $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) $(DESTDIR)$(PREFIX)/$(libdir)/$(devlink) && \ - ln -s $(DESTDIR)$(PREFIX)/$(libdir)/$(lib_so) $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) && \ - ln -s $(DESTDIR)$(PREFIX)/$(libdir)/$(soname) $(DESTDIR)$(PREFIX)/$(libdir)/$(devlink) || \ + cd $(DESTDIR)$(PREFIX)/$(libdir) && \ + ln -s $(lib_so) $(soname) && \ + ln -s $(soname) $(devlink) || \ true for h in $(hdr); do cp -p $(srcdir)/$$h $(DESTDIR)$(PREFIX)/include/; done diff --git a/spnav.c b/spnav.c index f9e10f8..74911c3 100644 --- a/spnav.c +++ b/spnav.c @@ -1,6 +1,6 @@ /* This file is part of libspnav, part of the spacenav project (spacenav.sf.net) -Copyright (C) 2007-2020 John Tsiombikas +Copyright (C) 2007-2022 John Tsiombikas Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -27,6 +27,7 @@ OF SUCH DAMAGE. #include #include #include +#include #include #include #include @@ -70,11 +71,22 @@ static struct event_node *ev_queue, *ev_queue_tail; /* AF_UNIX socket used for alternative communication with daemon */ static int sock = -1; +static int connect_afunix(int s, const char *path) +{ + struct sockaddr_un addr = {0}; + + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, path, sizeof addr.sun_path - 1); + + return connect(s, (struct sockaddr*)&addr, sizeof addr); +} int spnav_open(void) { int s; - struct sockaddr_un addr; + char *path; + FILE *fp; + char buf[256], *ptr; if(IS_OPEN) { return -1; @@ -90,16 +102,38 @@ int spnav_open(void) return -1; } - memset(&addr, 0, sizeof addr); - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path)); + /* heed SPNAV_SOCKET environment variable if it's defined */ + if((path = getenv("SPNAV_SOCKET"))) { + if(connect_afunix(s, path) == 0) goto success; + } + /* hacky config file parser, to look for socket = in /etc/spnavrc */ + if((fp = fopen("/etc/spnavrc", "rb"))) { + path = 0; + while(fgets(buf, sizeof buf, fp)) { + ptr = buf; + while(*ptr && isspace(*ptr)) ptr++; + if(!*ptr || *ptr == '#') continue; /* comment or empty line */ - if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) { + if(memcmp(ptr, "socket", 6) == 0 && (ptr = strchr(ptr, '='))) { + while(*++ptr && isspace(*ptr)); + if(!*ptr) continue; + path = ptr; + ptr += strlen(ptr) - 1; + while(ptr > path && isspace(*ptr)) *ptr-- = 0; + break; + } + } + if(path && connect_afunix(s, path) == 0) goto success; + } + + /* by default use SPNAV_SOCK_PATH (see top of this file) */ + if(connect_afunix(s, SPNAV_SOCK_PATH) == -1) { close(s); return -1; } +success: sock = s; return 0; } diff --git a/spnav.h b/spnav.h index 285d98a..c6974db 100644 --- a/spnav.h +++ b/spnav.h @@ -1,6 +1,6 @@ /* This file is part of libspnav, part of the spacenav project (spacenav.sf.net) -Copyright (C) 2007-2010 John Tsiombikas +Copyright (C) 2007-2022 John Tsiombikas Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: