1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
diff --git a/config.mk b/config.mk
index b469a2b..bdd8c9f 100644
--- a/config.mk
+++ b/config.mk
@@ -23,7 +23,7 @@ FREETYPEINC = /usr/include/freetype2
# includes and libs
INCS = -I${X11INC} -I${FREETYPEINC}
-LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
+LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lImlib2 -lXrender -lm
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
diff --git a/dwm.c b/dwm.c
index 4f345ee..7b238ed 100644
--- a/dwm.c
+++ b/dwm.c
@@ -43,6 +43,12 @@
#include "drw.h"
#include "util.h"
+/* Wallpaper stuff */
+#include <X11/extensions/Xrender.h>
+#include <Imlib2.h>
+#include <time.h>
+#include <math.h>
+
/* macros */
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
@@ -232,6 +238,8 @@ static int xerror(Display *dpy, XErrorEvent *ee);
static int xerrordummy(Display *dpy, XErrorEvent *ee);
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void zoom(const Arg *arg);
+static void nw_key(const Arg *arg);
+
/* variables */
static const char broken[] = "broken";
@@ -1378,6 +1386,90 @@ restack(Monitor *m)
while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
}
+static Pixmap current_pm = 0;
+
+static void
+load_wallpaper(Display *dpy, Window root, Pixmap *pm, const char *path)
+{
+ Screen *screen = DefaultScreenOfDisplay(dpy);
+ int sw = screen->width;
+ int sh = screen->height;
+ Visual *vis = DefaultVisual(dpy, DefaultScreen(dpy));
+ Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
+ int depth = DefaultDepth(dpy, DefaultScreen(dpy));
+
+ Imlib_Image img = imlib_load_image(path);
+ if (!img)
+ return;
+
+ imlib_context_set_display(dpy);
+ imlib_context_set_visual(vis);
+ imlib_context_set_colormap(cmap);
+ imlib_context_set_image(img);
+
+ int iw = imlib_image_get_width();
+ int ih = imlib_image_get_height();
+
+#ifdef ENABLE_ASPECT_SCALE
+ double scale = fmin((double)sw / iw, (double)sh / ih);
+ int dw = iw * scale;
+ int dh = ih * scale;
+ int xoff = (sw - dw) / 2;
+ int yoff = (sh - dh) / 2;
+#else
+ int dw = sw;
+ int dh = sh;
+ int xoff = 0;
+ int yoff = 0;
+#endif
+
+ *pm = XCreatePixmap(dpy, root, sw, sh, depth);
+ Pixmap img_pm = XCreatePixmap(dpy, root, dw, dh, depth);
+ imlib_context_set_drawable(img_pm);
+ imlib_render_image_on_drawable_at_size(0, 0, dw, dh);
+
+ XSetWindowBackgroundPixmap(dpy, root, img_pm);
+ XClearWindow(dpy, root);
+ XFlush(dpy);
+ if (current_pm)
+ XFreePixmap(dpy, current_pm);
+ current_pm = img_pm;
+ imlib_free_image();
+}
+
+static void
+next_wall(Display *dpy, Window root, const char **wals, int len)
+{
+ static int idx = -1;
+ if (len <= 0)
+ return;
+
+ idx = (idx + 1) % len;
+ load_wallpaper(dpy, root, ¤t_pm, wals[idx]);
+}
+
+static void
+set_wall(void)
+{
+ int len = sizeof(wals) / sizeof(wals[0]);
+ if (len <= 0)
+ return;
+
+ const char *s = (len > 1) ? wals[rand() % len] : wals[0];
+ load_wallpaper(dpy, root, ¤t_pm, s);
+}
+
+
+static
+void
+nw_key(const Arg *arg)
+{
+ (void) arg;
+ int len = sizeof(wals)/sizeof(wals[0]);
+ next_wall(dpy, root, wals, len);
+}
+
+
void
run(void)
{
@@ -1586,6 +1678,7 @@ setup(void)
scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
for (i = 0; i < LENGTH(colors); i++)
scheme[i] = drw_scm_create(drw, colors[i], 3);
+ set_wall();
/* init bars */
updatebars();
updatestatus();
|