Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / libraries / libspnav / configure-socket-path.patch
blob9a8ef0d4981151c36eed72dbdc14b88b0a50ec3a
1 diff --git a/spnav.c b/spnav.c
2 index f9e10f8..27149f7 100644
3 --- a/spnav.c
4 +++ b/spnav.c
5 @@ -36,7 +36,7 @@ OF SUCH DAMAGE.
6 #include <sys/select.h>
7 #include "spnav.h"
9 -#define SPNAV_SOCK_PATH "/var/run/spnav.sock"
10 +#define DEFAULT_SPNAV_SOCK_PATH "/run/spnav.sock"
12 #ifdef USE_X11
13 #include <X11/Xlib.h>
14 @@ -70,6 +70,24 @@ static struct event_node *ev_queue, *ev_queue_tail;
15 /* AF_UNIX socket used for alternative communication with daemon */
16 static int sock = -1;
18 +static char *spath = NULL;
20 +static char *socket_path()
22 + char *xdg_runtime_dir;
23 + if((xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"))) {
24 + if ( spath == NULL ) {
25 + spath = malloc(strlen(xdg_runtime_dir) + strlen("/spnav.sock") + 1);
26 + if ( spath != NULL ) {
27 + sprintf(spath, "%s/spnav.sock", xdg_runtime_dir);
28 + }
29 + }
30 + if(access(spath, F_OK) != -1){
31 + return spath;
32 + }
33 + }
34 + return DEFAULT_SPNAV_SOCK_PATH;
37 int spnav_open(void)
39 @@ -92,7 +110,7 @@ int spnav_open(void)
41 memset(&addr, 0, sizeof addr);
42 addr.sun_family = AF_UNIX;
43 - strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path));
44 + strncpy(addr.sun_path, socket_path(), sizeof(addr.sun_path));
47 if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {