/* xrootbg.c - sets desktop wallpaper with xlib (root background) * PUBLIC DOMAIN - CC0 http://creativecommons.org/publicdomain/zero/1.0/ * J.Mayo 2013 * * gcc -Wall -W -g3 -o xrootbg xrootbg.c -lX11 -lImlib2 * */ #include #include #include int main(int argc, char **argv) { Imlib_Image img; Display *dpy; Pixmap pix; Window root; Screen *scn; int width, height; const char *filename = NULL; if (argc < 2) goto usage; filename = argv[1]; img = imlib_load_image(filename); if (!img) { fprintf(stderr, "%s:Unable to load image\n", filename); goto usage; } imlib_context_set_image(img); dpy = XOpenDisplay(NULL); if (!dpy) return 0; scn = DefaultScreenOfDisplay(dpy); root = DefaultRootWindow(dpy); if (1) { /* tile */ width = imlib_image_get_width(); height = imlib_image_get_height(); } else { /* scale to screen */ width = WidthOfScreen(scn); height = HeightOfScreen(scn); } pix = XCreatePixmap(dpy, root, width, height, DefaultDepthOfScreen(scn)); XFillRectangle(dpy, pix, DefaultGCOfScreen(scn), 0, 0, width, height); imlib_context_set_display(dpy); imlib_context_set_visual(DefaultVisualOfScreen(scn)); imlib_context_set_colormap(DefaultColormapOfScreen(scn)); imlib_context_set_drawable(pix); imlib_render_image_on_drawable_at_size(0, 0, width, height); XSetWindowBackgroundPixmap(dpy, root, pix); XClearWindow(dpy, root); while (XPending(dpy)) { XEvent ev; XNextEvent(dpy, &ev); } XFreePixmap(dpy, pix); imlib_free_image(); XCloseDisplay(dpy); return 0; usage: fprintf(stderr, "usage: %s \n", argv[0]); return 1; }