usrlib.de / CVS

Diff for revision 1.9 of bgset/bgset.c

--- //cvs/lukas/bgset/bgset.c	2016/08/14 20:56:46	1.8
+++ //cvs/lukas/bgset/bgset.c	2016/08/15 12:03:21	1.9
@@ -35,8 +35,10 @@
 
 
 Imlib_Image prepare_image(char *, enum bgmode, int, int);
-int draw_image(char *, enum bgmode, int, int, int, int, Display *, int);
+int draw_image(Display *, int, Window, char *, enum bgmode, int, int, int, int);
 void usage(char *);
+void x_init(Display **, int *, Window *, XineramaScreenInfo **, int *);
+void x_teardown(Display *, XineramaScreenInfo *);
 int main(int, char **);
 
 
@@ -81,18 +83,17 @@
 		    scr_scl_w, scr_scl_h, scr_w, scr_h);
 		break;
 	}
+
 	imlib_free_image();
 	return prep;
 }
 
 int
-draw_image(char *filename, enum bgmode mode, int scr_x, int scr_y, int scr_w,
-    int scr_h, Display *dpy, int scr) {
-	int format;
+draw_image(Display *dpy, int scr, Window win, char *filename, enum bgmode mode,
+    int scr_x, int scr_y, int scr_w, int scr_h) {
+	int format = 32;
 	unsigned long nitems, bytes_after;
 	unsigned char *data;
-	Screen *scr_o;
-	Window root_win;
 	Atom pxm_prop, type;
 	Pixmap pxm;
 	Imlib_Image image;
@@ -101,13 +102,9 @@
 		return 0;
 	}
 
-	root_win = DefaultRootWindow(dpy);
-	scr_o = ScreenOfDisplay(dpy, scr);
-	XSetCloseDownMode(dpy, RetainPermanent);
-
-	/* init pixmap */
+	/* init pixmap, set window property for compositors */
 	pxm_prop = XInternAtom(dpy, "_XROOTPMAP_ID", False);
-	XGetWindowProperty(dpy, root_win, pxm_prop, 0L, 1L, False,
+	XGetWindowProperty(dpy, win, pxm_prop, 0L, 1L, False,
 	    AnyPropertyType, &type, &format, &nitems, &bytes_after, &data);
 	if (data) {
 		if (type == XA_PIXMAP) {
@@ -115,27 +112,23 @@
 		}
 		XFree(data);
 	} else {
-		pxm = XCreatePixmap(dpy, root_win, DisplayWidth(dpy, scr),
-		    DisplayHeight(dpy, scr), DefaultDepthOfScreen(scr_o));
-		XChangeProperty(dpy, root_win, pxm_prop, XA_PIXMAP, 32, PropModeReplace,
-		    (unsigned char *) &pxm, 1);
+		pxm = XCreatePixmap(dpy, win, DisplayWidth(dpy, scr),
+		    DisplayHeight(dpy, scr), DefaultDepth(dpy, scr));
+		XChangeProperty(dpy, win, pxm_prop, XA_PIXMAP, format,
+		    PropModeReplace, (unsigned char *) &pxm, 1);
 	}
 
 	/* init imlib */
 	imlib_context_set_image(image);
 	imlib_context_set_display(dpy);
 	imlib_context_set_drawable(pxm);
-	imlib_context_set_visual(DefaultVisualOfScreen(scr_o));
-	imlib_context_set_colormap(DefaultColormapOfScreen(scr_o));
+	imlib_context_set_visual(DefaultVisual(dpy, scr));
+	imlib_context_set_colormap(DefaultColormap(dpy, scr));
 
 	/* draw image */
 	imlib_render_image_on_drawable(scr_x, scr_y);
-	XSetWindowBackgroundPixmap(dpy, root_win, pxm);
-	XClearWindow(dpy, root_win);
-	while (XPending(dpy)) {
-		XEvent ev;
-		XNextEvent(dpy, &ev);
-	}
+	XSetWindowBackgroundPixmap(dpy, win, pxm);
+	XClearWindow(dpy, win);
 
 	/* clean up */
 	imlib_free_image();
@@ -149,6 +142,26 @@
 	    myname);
 }
 
+void
+x_init(Display **dpy, int *scr, Window *root, XineramaScreenInfo **info,
+    int *scrnum) {
+	if (!(*dpy = XOpenDisplay(NULL))) {
+		errx(1, "error opening display");
+	}
+	*scr = DefaultScreen(*dpy);
+	*root = DefaultRootWindow(*dpy);
+	*info = XineramaQueryScreens(*dpy, scrnum);
+	XSetCloseDownMode(*dpy, RetainPermanent);
+}
+
+void
+x_teardown(Display *dpy, XineramaScreenInfo *info) {
+	if (info) {
+		XFree(info);
+	}
+	XCloseDisplay(dpy);
+}
+
 int
 main(int argc, char *argv[]) {
 
@@ -157,6 +170,7 @@
 	int scr;
 	enum bgmode mode;
 	Display *dpy;
+	Window root;
 	XineramaScreenInfo *info = NULL;
 
 	if (argc == 1) {
@@ -164,16 +178,10 @@
 		return 1;
 	}
 
-	/* initialize X11 stuff */
-	if (!(dpy = XOpenDisplay(NULL))) {
-		errx(1, "error opening display");
-	}
-	scr = DefaultScreen(dpy);
-	info = XineramaQueryScreens(dpy, &scrnum);
+	x_init(&dpy, &scr, &root, &info, &scrnum);
 
 	/* default screen dimensions */
-	x = 0;
-	y = 0;
+	x = y = 0;
 	w = DisplayWidth(dpy, scr);
 	h = DisplayHeight(dpy, scr);
 
@@ -207,21 +215,15 @@
 			continue;
 		default:
 			usage(argv[0]);
-			if (info) {
-				XFree(info);
-			}
+			x_teardown(dpy, info);
 			return 1;
-
 		}
 
-		if (!(draw_image(optarg, mode, x, y, w, h, dpy, scr))) {
-			warnx("%s: unable to open file", optarg);
+		if (!(draw_image(dpy, scr, root, optarg, mode, x, y, w, h))) {
+			warn("%s on screen %d", optarg, scrind);
 		}
 	}
 
-	if (info) {
-		XFree(info);
-	}
-
+	x_teardown(dpy, info);
 	return 0;
 }