From ca6b11f678debc74d61da8cba02f50e837f490de Mon Sep 17 00:00:00 2001
From: Alec Cox <avlecxk@gmail.com>
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