From 6e242c197ed1a9e29e7784c5a7394d459e7c033a Mon Sep 17 00:00:00 2001 From: QC Date: Sat, 25 Jan 2014 23:37:37 +0100 Subject: [PATCH] Use XDG basedir for CACHE. --- Makefile | 4 ++-- include/simpleswitcher.h | 3 +++ source/run-dialog.c | 16 ++++++---------- source/simpleswitcher.c | 14 ++++++++++++++ source/ssh-dialog.c | 23 ++++++++--------------- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index cfc1721e..eac610d3 100644 --- a/Makefile +++ b/Makefile @@ -47,8 +47,8 @@ ifeq (${PKG_CONFIG},${EMPTY}) $(error Failed to find pkg-config. Please install pkg-config) endif -CFLAGS+=$(shell ${PKG_CONFIG} --cflags x11 xinerama xft) -LDADD+=$(shell ${PKG_CONFIG} --libs x11 xinerama xft) +CFLAGS+=$(shell ${PKG_CONFIG} --cflags x11 xinerama xft libxdg-basedir) +LDADD+=$(shell ${PKG_CONFIG} --libs x11 xinerama xft libxdg-basedir) ifeq (${LDADD},${EMPTY}) $(error Failed to find the required dependencies: x11, xinerama, xft) diff --git a/include/simpleswitcher.h b/include/simpleswitcher.h index e3b87b84..d3fce432 100644 --- a/include/simpleswitcher.h +++ b/include/simpleswitcher.h @@ -1,12 +1,15 @@ #ifndef __SIMPLESWITCHER_H__ #define __SIMPLESWITCHER_H__ +#include + #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define NEAR(a,o,b) ((b) > (a)-(o) && (b) < (a)+(o)) #define OVERLAP(a,b,c,d) (((a)==(c) && (b)==(d)) || MIN((a)+(b), (c)+(d)) - MAX((a), (c)) > 0) #define INTERSECT(x,y,w,h,x1,y1,w1,h1) (OVERLAP((x),(w),(x1),(w1)) && OVERLAP((y),(h),(y1),(h1))) +extern const char *cache_dir; typedef enum { WINDOW_SWITCHER, diff --git a/source/run-dialog.c b/source/run-dialog.c index 243eccb9..46ee804f 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -44,6 +44,8 @@ #include #endif +#define RUN_CACHE_FILE "simpleswitcher.runcache" + static inline int execsh( const char *cmd ,int run_in_term ) { // use sh for args parsing @@ -69,16 +71,13 @@ static pid_t exec_cmd( const char *cmd, int run_in_term ) int curr = -1; unsigned int index = 0; char **retv = NULL; - const char *hd = getenv( "HOME" ); - - if ( hd == NULL ) return pid; /** * This happens in non-critical time (After launching app) * It is allowed to be a bit slower. */ - char *path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 ); - sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" ); + char *path = allocate( strlen( cache_dir ) + strlen( RUN_CACHE_FILE )+3 ); + sprintf( path, "%s/%s", cache_dir, RUN_CACHE_FILE ); FILE *fd = fopen ( path, "r" ); char buffer[1024]; @@ -148,12 +147,9 @@ static char ** get_apps ( ) if ( getenv( "PATH" ) == NULL ) return NULL; - const char *hd = getenv( "HOME" ); - if ( hd == NULL ) return NULL; - - path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 ); - sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" ); + path = allocate( strlen( cache_dir ) + strlen( RUN_CACHE_FILE )+3 ); + sprintf( path, "%s/%s", cache_dir, RUN_CACHE_FILE ); FILE *fd = fopen ( path, "r" ); char buffer[1024]; diff --git a/source/simpleswitcher.c b/source/simpleswitcher.c index 6e88a814..3d6fad22 100644 --- a/source/simpleswitcher.c +++ b/source/simpleswitcher.c @@ -50,6 +50,7 @@ #include #include + #ifdef I3 #include #include @@ -69,6 +70,9 @@ #define OPACITY "_NET_WM_WINDOW_OPACITY" #define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH" +xdgHandle xdg_handle; +const char *cache_dir = NULL; + Settings config = { // Window settings @@ -1261,6 +1265,15 @@ int main( int argc, char *argv[] ) return EXIT_FAILURE; } + if(xdgInitHandle(&xdg_handle) == NULL) { + fprintf(stderr, "Failed to initialize XDG\n"); + return EXIT_FAILURE; + } + + cache_dir = xdgCacheHome(&xdg_handle); + printf("Cache directory: %s\n", cache_dir); + + signal( SIGCHLD, catch_exit ); screen = DefaultScreenOfDisplay( display ); screen_id = DefaultScreen( display ); @@ -1377,5 +1390,6 @@ int main( int argc, char *argv[] ) if ( i3_socket_path != NULL ) free( i3_socket_path ); #endif + xdgWipeHandle(&xdg_handle); return EXIT_SUCCESS; } diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c index bf8a49bf..fa154fb9 100644 --- a/source/ssh-dialog.c +++ b/source/ssh-dialog.c @@ -45,7 +45,7 @@ #include #endif -#define SSH_CACHE_FILE ".simpleswitcher.sshcache" +#define SSH_CACHE_FILE "simpleswitcher.sshcache" static inline int execshssh( const char *host ) { @@ -68,16 +68,13 @@ static pid_t exec_ssh( const char *cmd ) int curr = -1; unsigned int index = 0; char **retv = NULL; - const char *hd = getenv( "HOME" ); - - if ( hd == NULL ) return pid; /** * This happens in non-critical time (After launching app) * It is allowed to be a bit slower. */ - char *path = allocate( strlen( hd ) + strlen( "/"SSH_CACHE_FILE )+2 ); - sprintf( path, "%s/%s", hd, SSH_CACHE_FILE ); + char *path = allocate( strlen( cache_dir ) + strlen( SSH_CACHE_FILE )+3 ); + sprintf( path, "%s/%s", cache_dir, SSH_CACHE_FILE ); FILE *fd = fopen ( path, "r" ); char buffer[1024]; @@ -145,14 +142,10 @@ static char ** get_ssh ( ) clock_gettime( CLOCK_REALTIME, &start ); #endif - if ( getenv( "PATH" ) == NULL ) return NULL; + if ( getenv( "HOME" ) == NULL ) return NULL; - const char *hd = getenv( "HOME" ); - - if ( hd == NULL ) return NULL; - - path = allocate( strlen( hd ) + strlen( "/"SSH_CACHE_FILE )+2 ); - sprintf( path, "%s/%s", hd, SSH_CACHE_FILE ); + path = allocate( strlen( cache_dir ) + strlen( "/"SSH_CACHE_FILE )+2 ); + sprintf( path, "%s/%s", cache_dir, SSH_CACHE_FILE ); FILE *fd = fopen ( path, "r" ); char buffer[1024]; @@ -170,8 +163,8 @@ static char ** get_ssh ( ) } free( path ); - - path = allocate( strlen( hd ) + strlen( "/.ssh/config" )+2 ); + const char *hd = getenv("HOME"); + path = allocate( strlen( hd ) + strlen( ".ssh/config" )+3 ); sprintf( path, "%s/%s", hd, ".ssh/config" ); fd = fopen ( path, "r" );