Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / applications / misc / spnavcfg / configure-cfgfile-path.patch
blob22db2ee66ce3fc12637bd720704fe91af3aa879e
1 diff --git a/back.c b/back.c
2 index c1810dc..75416fb 100644
3 --- a/back.c
4 +++ b/back.c
5 @@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
6 #include "cfgfile.h"
7 #include "cmd.h"
9 -#define CFGFILE "/etc/spnavrc"
11 int get_daemon_pid(void);
12 static int update_cfg(void);
13 @@ -127,7 +126,7 @@ int get_daemon_pid(void)
15 static int update_cfg(void)
17 - if(write_cfg(CFGFILE, &cfg) == -1) {
18 + if(write_cfg(cfg_path(), &cfg) == -1) {
19 fprintf(stderr, "failed to update config file\n");
20 return -1;
22 diff --git a/cfgfile.c b/cfgfile.c
23 index 5a9c502..2ea323d 100644
24 --- a/cfgfile.c
25 +++ b/cfgfile.c
26 @@ -22,12 +22,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 +#include <unistd.h>
31 +#include <sys/types.h>
32 +#include <pwd.h>
33 #include "cfgfile.h"
35 enum {TX, TY, TZ, RX, RY, RZ};
37 static const int def_axmap[] = {0, 2, 1, 3, 5, 4};
38 static const int def_axinv[] = {0, 1, 1, 0, 1, 1};
39 +static char* config_path;
41 +char* cfg_path()
43 + char* buf;
44 + if((buf = getenv("XDG_CONFIG_HOME"))) {
45 + if(config_path == NULL) {
46 + config_path = malloc(strlen(buf) + strlen("/spnavrc") + 1);
47 + if ( config_path != NULL) {
48 + sprintf(config_path, "%s/spnavrc", buf);
49 + }
50 + };
51 + return config_path;
52 + } else {
53 + if (!(buf = getenv("HOME"))) {
54 + struct passwd *pw = getpwuid(getuid());
55 + buf = pw->pw_dir;
56 + }
57 + config_path = malloc(strlen(buf) + strlen("/.config/spnavrc") + 1);
58 + if ( config_path != NULL) {
59 + sprintf(config_path, "%s/.config/spnavrc", buf);
60 + }
61 + return config_path;
62 + }
65 void default_cfg(struct cfg *cfg)
67 diff --git a/cfgfile.h b/cfgfile.h
68 index dfed8c9..5bb1b2c 100644
69 --- a/cfgfile.h
70 +++ b/cfgfile.h
71 @@ -47,6 +47,7 @@ struct cfg {
72 int devid[MAX_CUSTOM][2]; /* custom USB vendor/product id list */
75 +char* cfg_path(void);
76 void default_cfg(struct cfg *cfg);
77 int read_cfg(const char *fname, struct cfg *cfg);
78 int write_cfg(const char *fname, struct cfg *cfg);
79 diff --git a/front_gtk.c b/front_gtk.c
80 index e4c2cd7..6a800a0 100644
81 --- a/front_gtk.c
82 +++ b/front_gtk.c
83 @@ -28,8 +28,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
84 #include "cmd.h"
85 #include "ui.h"
87 -#define CFGFILE "/etc/spnavrc"
89 #define CHK_AXINV_TRANS_X "axinv_trans_x"
90 #define CHK_AXINV_TRANS_Y "axinv_trans_y"
91 #define CHK_AXINV_TRANS_Z "axinv_trans_z"
92 @@ -121,7 +119,7 @@ void frontend(int pfd)
94 gtk_init(&argc, 0);
96 - read_cfg(CFGFILE, &cfg);
97 + read_cfg(cfg_path(), &cfg);
99 create_ui();