Skip to content

Commit 3a706e0

Browse files
committed
port patch v2.1 to dwm 6.3
1 parent 2323be1 commit 3a706e0

8 files changed

+231
-3
lines changed

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
winicon
2+
========
3+
4+
Description
5+
-----------
6+
**dwm-winicon** is a patch that enables dwm to show window icons.
7+
8+
![](https://raw.githubusercontent.com/AdamYuan/dwm-winicon/master/screenshots.png)
9+
10+
It is recommended to enable the compiler optimization flags: **-O3** and **-march=native** to enable auto loop vectorize, which leads to better performance.
11+
12+
If you discover any bugs or have any idea to optimize it, feel free to create an issue there.
13+
14+
Dependency
15+
----------
16+
The patch depends on Imlib2 for icon scaling, which can be easily installed in most distros.
17+
18+
Arch Linux:
19+
```sh
20+
sudo pacman -S imlib2
21+
```
22+
Debian:
23+
```sh
24+
sudo apt install libimlib2-dev
25+
```
26+
27+
Configuration
28+
-------------
29+
```c
30+
#define ICONSIZE 20 /* icon size in pixels */
31+
#define ICONSPACING 5 /* space (pixels) between icon and title */
32+
```
33+
34+
Alpha Patch
35+
-----------
36+
If you also use [alpha patch](https://dwm.suckless.org/patches/alpha/), some modifications are needed to make dwm work correctly.
37+
* Replace (in drw.c, drw_create function)
38+
```c
39+
drw->picture = XRenderCreatePicture(dpy, drw->drawable, XRenderFindVisualFormat(dpy, DefaultVisual(dpy, screen)), 0, NULL);
40+
```
41+
with
42+
```c
43+
drw->picture = XRenderCreatePicture(dpy, drw->drawable, XRenderFindVisualFormat(dpy, visual), 0, NULL);
44+
```
45+
46+
* Replace (in drw.c, drw_resize function)
47+
```c
48+
drw->picture = XRenderCreatePicture(drw->dpy, drw->drawable, XRenderFindVisualFormat(drw->dpy, DefaultVisual(drw->dpy, drw->screen)), 0, NULL);
49+
```
50+
with
51+
```c
52+
drw->picture = XRenderCreatePicture(drw->dpy, drw->drawable, XRenderFindVisualFormat(drw->dpy, drw->visual), 0, NULL);
53+
```

config.def.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ static const unsigned int borderpx = 1; /* border pixel of windows */
55
static const unsigned int snap = 32; /* snap pixel */
66
static const int showbar = 1; /* 0 means no bar */
77
static const int topbar = 1; /* 0 means bottom bar */
8+
#define ICONSIZE 16 /* icon size */
9+
#define ICONSPACING 5 /* space between icon and title */
810
static const char *fonts[] = { "monospace:size=10" };
911
static const char dmenufont[] = "monospace:size=10";
1012
static const char col_gray1[] = "#222222";

config.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ FREETYPEINC = /usr/include/freetype2
2222

2323
# includes and libs
2424
INCS = -I${X11INC} -I${FREETYPEINC}
25-
LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
25+
LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lXrender -lImlib2
2626

2727
# flags
2828
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}

diff.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
git diff 6.3-unpatched 6.3 ':!README.md' ':!screenshots.png' ':!diff.sh'

drw.c

+75
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include <X11/Xlib.h>
66
#include <X11/Xft/Xft.h>
7+
#include <Imlib2.h>
78

89
#include "drw.h"
910
#include "util.h"
@@ -71,6 +72,7 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
7172
drw->w = w;
7273
drw->h = h;
7374
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
75+
drw->picture = XRenderCreatePicture(dpy, drw->drawable, XRenderFindVisualFormat(dpy, DefaultVisual(dpy, screen)), 0, NULL);
7476
drw->gc = XCreateGC(dpy, root, 0, NULL);
7577
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
7678

@@ -85,14 +87,18 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h)
8587

8688
drw->w = w;
8789
drw->h = h;
90+
if (drw->picture)
91+
XRenderFreePicture(drw->dpy, drw->picture);
8892
if (drw->drawable)
8993
XFreePixmap(drw->dpy, drw->drawable);
9094
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
95+
drw->picture = XRenderCreatePicture(drw->dpy, drw->drawable, XRenderFindVisualFormat(drw->dpy, DefaultVisual(drw->dpy, drw->screen)), 0, NULL);
9196
}
9297

9398
void
9499
drw_free(Drw *drw)
95100
{
101+
XRenderFreePicture(drw->dpy, drw->picture);
96102
XFreePixmap(drw->dpy, drw->drawable);
97103
XFreeGC(drw->dpy, drw->gc);
98104
drw_fontset_free(drw->fonts);
@@ -236,6 +242,67 @@ drw_setscheme(Drw *drw, Clr *scm)
236242
drw->scheme = scm;
237243
}
238244

245+
Picture
246+
drw_picture_create_resized(Drw *drw, char *src, unsigned int srcw, unsigned int srch, unsigned int dstw, unsigned int dsth) {
247+
Pixmap pm;
248+
Picture pic;
249+
GC gc;
250+
251+
if (srcw <= (dstw << 1u) && srch <= (dsth << 1u)) {
252+
XImage img = {
253+
srcw, srch, 0, ZPixmap, src,
254+
ImageByteOrder(drw->dpy), BitmapUnit(drw->dpy), BitmapBitOrder(drw->dpy), 32,
255+
32, 0, 32,
256+
0, 0, 0
257+
};
258+
XInitImage(&img);
259+
260+
pm = XCreatePixmap(drw->dpy, drw->root, srcw, srch, 32);
261+
gc = XCreateGC(drw->dpy, pm, 0, NULL);
262+
XPutImage(drw->dpy, pm, gc, &img, 0, 0, 0, 0, srcw, srch);
263+
XFreeGC(drw->dpy, gc);
264+
265+
pic = XRenderCreatePicture(drw->dpy, pm, XRenderFindStandardFormat(drw->dpy, PictStandardARGB32), 0, NULL);
266+
XFreePixmap(drw->dpy, pm);
267+
268+
XRenderSetPictureFilter(drw->dpy, pic, FilterBilinear, NULL, 0);
269+
XTransform xf;
270+
xf.matrix[0][0] = (srcw << 16u) / dstw; xf.matrix[0][1] = 0; xf.matrix[0][2] = 0;
271+
xf.matrix[1][0] = 0; xf.matrix[1][1] = (srch << 16u) / dsth; xf.matrix[1][2] = 0;
272+
xf.matrix[2][0] = 0; xf.matrix[2][1] = 0; xf.matrix[2][2] = 65536;
273+
XRenderSetPictureTransform(drw->dpy, pic, &xf);
274+
} else {
275+
Imlib_Image origin = imlib_create_image_using_data(srcw, srch, (DATA32 *)src);
276+
if (!origin) return None;
277+
imlib_context_set_image(origin);
278+
imlib_image_set_has_alpha(1);
279+
Imlib_Image scaled = imlib_create_cropped_scaled_image(0, 0, srcw, srch, dstw, dsth);
280+
imlib_free_image_and_decache();
281+
if (!scaled) return None;
282+
imlib_context_set_image(scaled);
283+
imlib_image_set_has_alpha(1);
284+
285+
XImage img = {
286+
dstw, dsth, 0, ZPixmap, (char *)imlib_image_get_data_for_reading_only(),
287+
ImageByteOrder(drw->dpy), BitmapUnit(drw->dpy), BitmapBitOrder(drw->dpy), 32,
288+
32, 0, 32,
289+
0, 0, 0
290+
};
291+
XInitImage(&img);
292+
293+
pm = XCreatePixmap(drw->dpy, drw->root, dstw, dsth, 32);
294+
gc = XCreateGC(drw->dpy, pm, 0, NULL);
295+
XPutImage(drw->dpy, pm, gc, &img, 0, 0, 0, 0, dstw, dsth);
296+
imlib_free_image_and_decache();
297+
XFreeGC(drw->dpy, gc);
298+
299+
pic = XRenderCreatePicture(drw->dpy, pm, XRenderFindStandardFormat(drw->dpy, PictStandardARGB32), 0, NULL);
300+
XFreePixmap(drw->dpy, pm);
301+
}
302+
303+
return pic;
304+
}
305+
239306
void
240307
drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
241308
{
@@ -379,6 +446,14 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
379446
return x + (render ? w : 0);
380447
}
381448

449+
void
450+
drw_pic(Drw *drw, int x, int y, unsigned int w, unsigned int h, Picture pic)
451+
{
452+
if (!drw)
453+
return;
454+
XRenderComposite(drw->dpy, PictOpOver, pic, None, drw->picture, 0, 0, 0, 0, x, y, w, h);
455+
}
456+
382457
void
383458
drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
384459
{

drw.h

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef struct {
2121
int screen;
2222
Window root;
2323
Drawable drawable;
24+
Picture picture;
2425
GC gc;
2526
Clr *scheme;
2627
Fnt *fonts;
@@ -49,9 +50,12 @@ void drw_cur_free(Drw *drw, Cur *cursor);
4950
void drw_setfontset(Drw *drw, Fnt *set);
5051
void drw_setscheme(Drw *drw, Clr *scm);
5152

53+
Picture drw_picture_create_resized(Drw *drw, char *src, unsigned int src_w, unsigned int src_h, unsigned int dst_w, unsigned int dst_h);
54+
5255
/* Drawing functions */
5356
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert);
5457
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert);
58+
void drw_pic(Drw *drw, int x, int y, unsigned int w, unsigned int h, Picture pic);
5559

5660
/* Map functions */
5761
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);

0 commit comments

Comments
 (0)