st-visualbell3-0.9.2.diff - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
st-visualbell3-0.9.2.diff (3817B)
---
1 diff --git a/config.def.h b/config.def.h
2 index 2cd740a..ccf3ab8 100644
3 --- a/config.def.h
4 +++ b/config.def.h
5 @@ -73,6 +73,20 @@ static unsigned int cursorthickness = 2;
6 */
7 static int bellvolume = 0;
8
9 +/* visual bell duration (in milliseconds) */
10 +static unsigned int bellduration = 150;
11 +
12 +/*
13 + * visual bell colors
14 + *
15 + * Formulas from normal colors to bell colors.
16 + * Bell colors are clipped between 0x0000 and 0xffff.
17 + */
18 +#define BELLR(color) (color.red * 0.9 + 0xffff * 0.1)
19 +#define BELLG(color) (color.green * 0.9 + 0xffff * 0.1)
20 +#define BELLB(color) (color.blue * 0.9 + 0xffff * 0.1)
21 +#define BELLA(color) (color.alpha - 0x0500)
22 +
23 /* default TERM value */
24 char *termname = "st-256color";
25
26 diff --git a/x.c b/x.c
27 index bd23686..30db597 100644
28 --- a/x.c
29 +++ b/x.c
30 @@ -135,6 +135,8 @@ typedef struct {
31 /* Drawing Context */
32 typedef struct {
33 Color *col;
34 + Color *normalcol;
35 + Color *bellcol;
36 size_t collen;
37 Font font, bfont, ifont, ibfont;
38 GC gc;
39 @@ -155,6 +157,8 @@ static void cresize(int, int);
40 static void xresize(int, int);
41 static void xhints(void);
42 static int xloadcolor(int, const char *, Color *);
43 +static void xnormalcols(void);
44 +static void xbellcols(void);
45 static int xloadfont(Font *, FcPattern *);
46 static void xloadfonts(const char *, double);
47 static void xunloadfont(Font *);
48 @@ -220,6 +224,7 @@ static DC dc;
49 static XWindow xw;
50 static XSelection xsel;
51 static TermWindow win;
52 +struct timespec lastbell;
53
54 /* Font Ring Cache */
55 enum {
56 @@ -795,18 +800,33 @@ xloadcols(void)
57 {
58 int i;
59 static int loaded;
60 - Color *cp;
61
62 if (loaded) {
63 - for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
64 - XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
65 + for (i = 0; i < dc.collen; i++) {
66 + XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.normalcol[i]);
67 + XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.bellcol[i]);
68 + }
69 } else {
70 dc.collen = MAX(LEN(colorname), 256);
71 - dc.col = xmalloc(dc.collen * sizeof(Color));
72 + dc.normalcol = xmalloc(dc.collen * sizeof(Color));
73 + dc.bellcol = xmalloc(dc.collen * sizeof(Color));
74 + dc.col = dc.normalcol;
75 }
76
77 + xnormalcols();
78 + xbellcols();
79 +
80 + loaded = 1;
81 +}
82 +
83 +void
84 +xnormalcols(void)
85 +{
86 + int i;
87 + static int loaded;
88 +
89 for (i = 0; i < dc.collen; i++)
90 - if (!xloadcolor(i, NULL, &dc.col[i])) {
91 + if (!xloadcolor(i, NULL, &dc.normalcol[i])) {
92 if (colorname[i])
93 die("could not allocate color '%s'\n", colorname[i]);
94 else
95 @@ -815,6 +835,22 @@ xloadcols(void)
96 loaded = 1;
97 }
98
99 +void
100 +xbellcols(void)
101 +{
102 + int i;
103 + XRenderColor bc;
104 +
105 + for (i = 0; i < dc.collen; i++) {
106 + bc.red = MAX(0, MIN(0xffff, BELLR(dc.normalcol[i].color)));
107 + bc.green = MAX(0, MIN(0xffff, BELLG(dc.normalcol[i].color)));
108 + bc.blue = MAX(0, MIN(0xffff, BELLB(dc.normalcol[i].color)));
109 + bc.alpha = MAX(0, MIN(0xffff, BELLA(dc.normalcol[i].color)));
110 + XftColorAllocValue(xw.dpy, xw.vis,
111 + xw.cmap, &bc, &dc.bellcol[i]);
112 + }
113 +}
114 +
115 int
116 xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b)
117 {
118 @@ -1766,6 +1802,10 @@ xbell(void)
119 xseturgency(1);
120 if (bellvolume)
121 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
122 +
123 + clock_gettime(CLOCK_MONOTONIC, &lastbell);
124 + dc.col = dc.bellcol;
125 + redraw();
126 }
127
128 void
129 @@ -1925,7 +1965,7 @@ run(void)
130 fd_set rfd;
131 int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
132 struct timespec seltv, *tv, now, lastblink, trigger;
133 - double timeout;
134 + double timeout, bellremain;
135
136 /* Waiting for window mapping */
137 do {
138 @@ -2014,6 +2054,17 @@ run(void)
139 }
140 }
141
142 + /* bell */
143 + if (dc.col == dc.bellcol) {
144 + bellremain = bellduration - TIMEDIFF(now, lastbell);
145 + if (bellremain < 0) {
146 + dc.col = dc.normalcol;
147 + redraw();
148 + } else if (timeout < 0 || bellremain < timeout) {
149 + timeout = bellremain;
150 + }
151 + }
152 +
153 draw();
154 XFlush(xw.dpy);
155 drawing = 0;