From ae0f0da32410741e7450b7954d21ea60d32ff353 Mon Sep 17 00:00:00 2001 From: Alec Cox Date: Fri, 22 Jan 2021 21:40:59 -0800 Subject: fixed for simpler parenthesis matching --- src/wallpaper.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wallpaper.c b/src/wallpaper.c index 37b5a1d..12a47f4 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -504,10 +504,11 @@ void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, fputc(' ', fp); if (use_filelist) { #ifdef HAVE_LIBXINERAMA - for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) { + for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) #else - for (int i = 0; (i < 1 ) && filelist_pos; i++) { + for (int i = 0; (i < 1 ) && filelist_pos; i++) #endif + { absolute_path = feh_absolute_path(FEH_FILE(filelist_pos->data)->filename); fputs(shell_escape(absolute_path), fp); filelist_pos = filelist_pos->next; -- cgit v1.2.3 From ca6b11f678debc74d61da8cba02f50e837f490de Mon Sep 17 00:00:00 2001 From: Alec Cox Date: Mon, 25 Jan 2021 13:40:24 -0800 Subject: absolute file path for .fehbg, moved script creation into helper function --- src/wallpaper.c | 174 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 82 deletions(-) diff --git a/src/wallpaper.c b/src/wallpaper.c index 12a47f4..b59bdea 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -245,6 +245,96 @@ static void feh_wm_set_bg_maxed(Pixmap pmap, Imlib_Image im, int use_filelist, return; } +/* +** Creates a script that can be used to create the same background +** as the last time the program was called. +*/ +void feh_wm_gen_bg_script(char* fil, int centered, int scaled, int filled, int use_filelist) { + char * home = getenv("HOME"); + + if (home) { + FILE *fp; + int fd; + char *path; + char *exec_absolute_path; + char *absolute_path; + struct stat s; + gib_list *filelist_pos = filelist; + exec_absolute_path = feh_absolute_path(cmdargv[0]); + path = estrjoin("/", home, ".fehbg", NULL); + if ((fp = fopen(path, "w")) == NULL) { + weprintf("Can't write to %s", path); + } else { + fputs("#!/bin/sh\n", fp); + fputs(exec_absolute_path, fp); + fputs(" --no-fehbg --bg-", fp); + if (centered) + fputs("center", fp); + else if (scaled) + fputs("scale", fp); + else if (filled == 1) + fputs("fill", fp); + else if (filled == 2) + fputs("max", fp); + else + fputs("tile", fp); + if (opt.image_bg) { + fputs(" --image-bg ", fp); + fputs(shell_escape(opt.image_bg), fp); + } +#ifdef HAVE_LIBXINERAMA + if (opt.xinerama) { + if (opt.xinerama_index >= 0) { + fprintf(fp, " --xinerama-index %d", opt.xinerama_index); + } + } + else { + fputs(" --no-xinerama", fp); + } +#endif /* HAVE_LIBXINERAMA */ + if (opt.geom_flags & XValue) { + fprintf(fp, " --geometry %c%d", + opt.geom_flags & XNegative ? '-' : '+', + opt.geom_flags & XNegative ? abs(opt.geom_x) : opt.geom_x); + if (opt.geom_flags & YValue) { + fprintf(fp, "%c%d", + opt.geom_flags & YNegative ? '-' : '+', + opt.geom_flags & YNegative ? abs(opt.geom_y) : opt.geom_y); + } + } + if (opt.force_aliasing) { + fputs(" --force-aliasing", fp); + } + fputc(' ', fp); + if (use_filelist) { +#ifdef HAVE_LIBXINERAMA + for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) +#else + for (int i = 0; (i < 1 ) && filelist_pos; i++) +#endif + { + absolute_path = feh_absolute_path(FEH_FILE(filelist_pos->data)->filename); + fputs(shell_escape(absolute_path), fp); + filelist_pos = filelist_pos->next; + free(absolute_path); + fputc(' ', fp); + } + } else if (fil) { + absolute_path = feh_absolute_path(fil); + fputs(shell_escape(absolute_path), fp); + free(absolute_path); + } + fputc('\n', fp); + fd = fileno(fp); + if (fstat(fd, &s) != 0 || fchmod(fd, s.st_mode | S_IXUSR | S_IXGRP) != 0) { + weprintf("Can't set %s as executable", path); + } + fclose(fp); + } + free(path); + } +} + void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, int filled, int desktop, int use_filelist) { @@ -448,88 +538,8 @@ void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, gib_imlib_render_image_on_drawable(pmap_d1, im, 0, 0, 1, 1, 0); } - if (!opt.no_fehbg) { - home = getenv("HOME"); - if (home) { - FILE *fp; - int fd; - char *path; - char *absolute_path; - struct stat s; - gib_list *filelist_pos = filelist; - path = estrjoin("/", home, ".fehbg", NULL); - if ((fp = fopen(path, "w")) == NULL) { - weprintf("Can't write to %s", path); - } else { - fputs("#!/bin/sh\n", fp); - fputs(cmdargv[0], fp); - fputs(" --no-fehbg --bg-", fp); - if (centered) - fputs("center", fp); - else if (scaled) - fputs("scale", fp); - else if (filled == 1) - fputs("fill", fp); - else if (filled == 2) - fputs("max", fp); - else - fputs("tile", fp); - if (opt.image_bg) { - fputs(" --image-bg ", fp); - fputs(shell_escape(opt.image_bg), fp); - } -#ifdef HAVE_LIBXINERAMA - if (opt.xinerama) { - if (opt.xinerama_index >= 0) { - fprintf(fp, " --xinerama-index %d", opt.xinerama_index); - } - } - else { - fputs(" --no-xinerama", fp); - } -#endif /* HAVE_LIBXINERAMA */ - if (opt.geom_flags & XValue) { - fprintf(fp, " --geometry %c%d", - opt.geom_flags & XNegative ? '-' : '+', - opt.geom_flags & XNegative ? abs(opt.geom_x) : opt.geom_x); - if (opt.geom_flags & YValue) { - fprintf(fp, "%c%d", - opt.geom_flags & YNegative ? '-' : '+', - opt.geom_flags & YNegative ? abs(opt.geom_y) : opt.geom_y); - } - } - if (opt.force_aliasing) { - fputs(" --force-aliasing", fp); - } - fputc(' ', fp); - if (use_filelist) { -#ifdef HAVE_LIBXINERAMA - for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) -#else - for (int i = 0; (i < 1 ) && filelist_pos; i++) -#endif - { - absolute_path = feh_absolute_path(FEH_FILE(filelist_pos->data)->filename); - fputs(shell_escape(absolute_path), fp); - filelist_pos = filelist_pos->next; - free(absolute_path); - fputc(' ', fp); - } - } else if (fil) { - absolute_path = feh_absolute_path(fil); - fputs(shell_escape(absolute_path), fp); - free(absolute_path); - } - fputc('\n', fp); - fd = fileno(fp); - if (fstat(fd, &s) != 0 || fchmod(fd, s.st_mode | S_IXUSR | S_IXGRP) != 0) { - weprintf("Can't set %s as executable", path); - } - fclose(fp); - } - free(path); - } - } + if (!opt.no_fehbg) + feh_wm_gen_bg_script(fil, centered, scaled, filled, use_filelist); /* create new display, copy pixmap to new display */ disp2 = XOpenDisplay(NULL); -- cgit v1.2.3 From 392a72cec6bf2cb5ddc89a0615b73ef7338f5444 Mon Sep 17 00:00:00 2001 From: Alec Cox Date: Mon, 25 Jan 2021 15:37:07 -0800 Subject: Removed unused variable. --- src/wallpaper.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wallpaper.c b/src/wallpaper.c index b59bdea..ac9c4de 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -412,8 +412,6 @@ void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, unsigned char *data_root = NULL, *data_esetroot = NULL; Pixmap pmap_d1, pmap_d2; - char *home; - /* local display to set closedownmode on */ Display *disp2; Window root2; -- cgit v1.2.3 From 1c9186fe1b8ea9c946abbd883e060bd3f94776db Mon Sep 17 00:00:00 2001 From: Alec Cox Date: Tue, 26 Jan 2021 08:59:46 -0800 Subject: added early exit condition as per Ferada's suggestion --- src/wallpaper.c | 145 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/src/wallpaper.c b/src/wallpaper.c index ac9c4de..a34f033 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -252,87 +252,88 @@ static void feh_wm_set_bg_maxed(Pixmap pmap, Imlib_Image im, int use_filelist, void feh_wm_gen_bg_script(char* fil, int centered, int scaled, int filled, int use_filelist) { char * home = getenv("HOME"); - if (home) { - FILE *fp; - int fd; - char *path; - char *exec_absolute_path; - char *absolute_path; - struct stat s; - gib_list *filelist_pos = filelist; - exec_absolute_path = feh_absolute_path(cmdargv[0]); - path = estrjoin("/", home, ".fehbg", NULL); - if ((fp = fopen(path, "w")) == NULL) { - weprintf("Can't write to %s", path); - } else { - fputs("#!/bin/sh\n", fp); - fputs(exec_absolute_path, fp); - fputs(" --no-fehbg --bg-", fp); - if (centered) - fputs("center", fp); - else if (scaled) - fputs("scale", fp); - else if (filled == 1) - fputs("fill", fp); - else if (filled == 2) - fputs("max", fp); - else - fputs("tile", fp); - if (opt.image_bg) { - fputs(" --image-bg ", fp); - fputs(shell_escape(opt.image_bg), fp); - } + if (!home) + return; + + FILE *fp; + int fd; + char *path; + char *exec_absolute_path; + char *absolute_path; + struct stat s; + gib_list *filelist_pos = filelist; + exec_absolute_path = feh_absolute_path(cmdargv[0]); + path = estrjoin("/", home, ".fehbg", NULL); + if ((fp = fopen(path, "w")) == NULL) { + weprintf("Can't write to %s", path); + } else { + fputs("#!/bin/sh\n", fp); + fputs(exec_absolute_path, fp); + fputs(" --no-fehbg --bg-", fp); + if (centered) + fputs("center", fp); + else if (scaled) + fputs("scale", fp); + else if (filled == 1) + fputs("fill", fp); + else if (filled == 2) + fputs("max", fp); + else + fputs("tile", fp); + if (opt.image_bg) { + fputs(" --image-bg ", fp); + fputs(shell_escape(opt.image_bg), fp); + } #ifdef HAVE_LIBXINERAMA - if (opt.xinerama) { - if (opt.xinerama_index >= 0) { - fprintf(fp, " --xinerama-index %d", opt.xinerama_index); - } - } - else { - fputs(" --no-xinerama", fp); + if (opt.xinerama) { + if (opt.xinerama_index >= 0) { + fprintf(fp, " --xinerama-index %d", opt.xinerama_index); } + } + else { + fputs(" --no-xinerama", fp); + } #endif /* HAVE_LIBXINERAMA */ - if (opt.geom_flags & XValue) { - fprintf(fp, " --geometry %c%d", - opt.geom_flags & XNegative ? '-' : '+', - opt.geom_flags & XNegative ? abs(opt.geom_x) : opt.geom_x); - if (opt.geom_flags & YValue) { - fprintf(fp, "%c%d", - opt.geom_flags & YNegative ? '-' : '+', - opt.geom_flags & YNegative ? abs(opt.geom_y) : opt.geom_y); - } - } - if (opt.force_aliasing) { - fputs(" --force-aliasing", fp); + if (opt.geom_flags & XValue) { + fprintf(fp, " --geometry %c%d", + opt.geom_flags & XNegative ? '-' : '+', + opt.geom_flags & XNegative ? abs(opt.geom_x) : opt.geom_x); + if (opt.geom_flags & YValue) { + fprintf(fp, "%c%d", + opt.geom_flags & YNegative ? '-' : '+', + opt.geom_flags & YNegative ? abs(opt.geom_y) : opt.geom_y); } - fputc(' ', fp); - if (use_filelist) { + } + if (opt.force_aliasing) { + fputs(" --force-aliasing", fp); + } + fputc(' ', fp); + if (use_filelist) { #ifdef HAVE_LIBXINERAMA - for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) + for (int i = 0; (i < opt.xinerama ? num_xinerama_screens : 1) && filelist_pos; i++) #else - for (int i = 0; (i < 1 ) && filelist_pos; i++) + for (int i = 0; (i < 1 ) && filelist_pos; i++) #endif - { - absolute_path = feh_absolute_path(FEH_FILE(filelist_pos->data)->filename); - fputs(shell_escape(absolute_path), fp); - filelist_pos = filelist_pos->next; - free(absolute_path); - fputc(' ', fp); - } - } else if (fil) { - absolute_path = feh_absolute_path(fil); - fputs(shell_escape(absolute_path), fp); - free(absolute_path); - } - fputc('\n', fp); - fd = fileno(fp); - if (fstat(fd, &s) != 0 || fchmod(fd, s.st_mode | S_IXUSR | S_IXGRP) != 0) { - weprintf("Can't set %s as executable", path); - } - fclose(fp); + { + absolute_path = feh_absolute_path(FEH_FILE(filelist_pos->data)->filename); + fputs(shell_escape(absolute_path), fp); + filelist_pos = filelist_pos->next; + free(absolute_path); + fputc(' ', fp); + } + } else if (fil) { + absolute_path = feh_absolute_path(fil); + fputs(shell_escape(absolute_path), fp); + free(absolute_path); + } + fputc('\n', fp); + fd = fileno(fp); + if (fstat(fd, &s) != 0 || fchmod(fd, s.st_mode | S_IXUSR | S_IXGRP) != 0) { + weprintf("Can't set %s as executable", path); } - free(path); + fclose(fp); } + free(path); } void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, -- cgit v1.2.3 From 43cb03e314513a9a9cecf6d8922146832f0c6c2b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 28 Jan 2021 19:48:51 +0100 Subject: fix memory leak (free exec_absolute_path) --- src/wallpaper.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallpaper.c b/src/wallpaper.c index a34f033..8660cde 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -334,6 +334,7 @@ void feh_wm_gen_bg_script(char* fil, int centered, int scaled, int filled, int u fclose(fp); } free(path); + free(exec_absolute_path); } void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, -- cgit v1.2.3 From 31fd2f6412e3535500fef9da6164320dbbdb6bff Mon Sep 17 00:00:00 2001 From: Alec Cox Date: Thu, 28 Jan 2021 12:46:24 -0800 Subject: added detection for path vs env PATH launch for use in .fehbg script gen. --- src/wallpaper.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wallpaper.c b/src/wallpaper.c index 8660cde..b62d28b 100644 --- a/src/wallpaper.c +++ b/src/wallpaper.c @@ -258,17 +258,23 @@ void feh_wm_gen_bg_script(char* fil, int centered, int scaled, int filled, int u FILE *fp; int fd; char *path; - char *exec_absolute_path; + char *exec_method; char *absolute_path; struct stat s; gib_list *filelist_pos = filelist; - exec_absolute_path = feh_absolute_path(cmdargv[0]); + + if (strchr(cmdargv[0], '/')) + exec_method = feh_absolute_path(cmdargv[0]); + else + exec_method = cmdargv[0]; + path = estrjoin("/", home, ".fehbg", NULL); + if ((fp = fopen(path, "w")) == NULL) { weprintf("Can't write to %s", path); } else { fputs("#!/bin/sh\n", fp); - fputs(exec_absolute_path, fp); + fputs(exec_method, fp); fputs(" --no-fehbg --bg-", fp); if (centered) fputs("center", fp); @@ -334,7 +340,9 @@ void feh_wm_gen_bg_script(char* fil, int centered, int scaled, int filled, int u fclose(fp); } free(path); - free(exec_absolute_path); + + if(exec_method != cmdargv[0]) + free(exec_method); } void feh_wm_set_bg(char *fil, Imlib_Image im, int centered, int scaled, -- cgit v1.2.3