-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbsetroot.cc
345 lines (265 loc) · 10.2 KB
/
bsetroot.cc
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// TODO - Documentation
#include "bsetroot.hh"
#include <Pen.hh>
#include <Texture.hh>
#include <cctype>
#include <cstdlib>
#include <X11/Xatom.h>
#include <stdio.h>
// ignore all X errors
static int x11_error(::Display *, XErrorEvent *)
{ return 0; }
bsetroot::bsetroot(int argc, char **argv, char *dpy_name,
bool multi_head): display(dpy_name, multi_head) {
XSetErrorHandler(x11_error); // silently handle all errors
bool mod = False, sol = False, grd = False;
int mod_x = 0, mod_y = 0;
for (int i = 1; i < argc; i++) {
if (! strcmp("-help", argv[i])) {
usage();
} else if ((! strcmp("-fg", argv[i])) ||
(! strcmp("-foreground", argv[i])) ||
(! strcmp("-from", argv[i]))) {
if ((++i) >= argc) usage(1);
fore = argv[i];
} else if ((! strcmp("-bg", argv[i])) ||
(! strcmp("-background", argv[i])) ||
(! strcmp("-to", argv[i]))) {
if ((++i) >= argc) usage(1);
back = argv[i];
} else if (! strcmp("-solid", argv[i])) {
if ((++i) >= argc) usage(1);
fore = argv[i];
sol = True;
} else if (! strcmp("-mod", argv[i])) {
if ((++i) >= argc) usage();
mod_x = atoi(argv[i]);
if ((++i) >= argc) usage();
mod_y = atoi(argv[i]);
if (mod_x < 1) mod_x = 1;
if (mod_y < 1) mod_y = 1;
mod = True;
} else if (! strcmp("-gradient", argv[i])) {
if ((++i) >= argc) usage();
grad = argv[i];
grd = True;
} else if (! strcmp("-display", argv[i])) {
// -display passed through tests ealier... we
i++;
} else {
usage();
}
}
if ((mod + sol + grd) != True) {
fprintf(stderr,
"bsetroot: error: must specify one of: -solid, -mod, -gradient\n");
usage(2);
}
// keep the server grabbed while rendering all screens
XGrabServer(display.XDisplay());
if (sol && ! fore.empty())
solid();
else if (mod && mod_x && mod_y && ! (fore.empty() || back.empty()))
modula(mod_x, mod_y);
else if (grd && ! (grad.empty() || fore.empty() || back.empty()))
gradient();
else
usage();
// ungrab the server and discard any events
XUngrabServer(display.XDisplay());
XSync(display.XDisplay(), True);
}
bsetroot::~bsetroot(void) {
XSetCloseDownMode(display.XDisplay(), RetainPermanent);
XKillClient(display.XDisplay(), AllTemporary);
}
// adapted from wmsetbg
void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
Atom rootpmap_id = XInternAtom(display.XDisplay(), "_XROOTPMAP_ID", False),
esetroot_id = XInternAtom(display.XDisplay(), "ESETROOT_PMAP_ID", False);
const bt::ScreenInfo &screen_info = display.screenInfo(screen);
Atom type;
int format;
unsigned long length, after;
unsigned char *data = 0;
Pixmap xrootpmap = None;
Pixmap esetrootpmap = None;
// Clear out the old _XROOTPMAP_ID property
XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
rootpmap_id, 0L, 1L, True, AnyPropertyType,
&type, &format, &length, &after, &data);
if (data && type == XA_PIXMAP && format == 32 && length == 1)
xrootpmap = *(reinterpret_cast<Pixmap *>(data));
if (data) XFree(data);
// Clear out the old ESETROOT_PMAP_ID property
XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
esetroot_id, 0L, 1L, True, AnyPropertyType,
&type, &format, &length, &after, &data);
if (data && type == XA_PIXMAP && format == 32 && length == 1)
esetrootpmap = *(reinterpret_cast<Pixmap *>(data));
if (data) XFree(data);
// Destroy the old pixmaps
if (xrootpmap)
XKillClient(display.XDisplay(), xrootpmap);
if (esetrootpmap && esetrootpmap != xrootpmap)
XKillClient(display.XDisplay(), esetrootpmap);
if (pixmap) {
XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
reinterpret_cast<unsigned char *>(&pixmap), 1);
XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
esetroot_id, XA_PIXMAP, 32, PropModeReplace,
reinterpret_cast<unsigned char *>(&pixmap), 1);
}
}
unsigned long bsetroot::duplicateColor(unsigned int screen,
const bt::Color &color) {
/*
When using a colormap that is not read-only, we need to
reallocate the color we are using. The application's color cache
will be freed on exit, and another client can allocate a new
color at the same pixel value, which will immediately change the
color of the root window.
this is not what we want, so we need to make sure that the pixel
we are using is doubly allocated, so that it stays around with the
pixmap. It will be released when we use
XKillClient(..., AllTemporary);
*/
const bt::ScreenInfo &screen_info = display.screenInfo(screen);
unsigned long pixel = color.pixel(screen);
XColor xcolor;
xcolor.pixel = pixel;
XQueryColor(display.XDisplay(), screen_info.colormap(), &xcolor);
if (! XAllocColor(display.XDisplay(), screen_info.colormap(),
&xcolor)) {
fprintf(stderr, "warning: couldn't duplicate color %02x/%02x/%02x\n",
color.red(), color.green(), color.blue());
}
return pixel;
}
void bsetroot::solid(void) {
bt::Color c = bt::Color::namedColor(display, 0, fore);
for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
const bt::ScreenInfo &screen_info = display.screenInfo(screen);
unsigned long pixel = duplicateColor(screen, c);
XSetWindowBackground(display.XDisplay(), screen_info.rootWindow(), pixel);
XClearWindow(display.XDisplay(), screen_info.rootWindow());
Pixmap pixmap =
XCreatePixmap(display.XDisplay(), screen_info.rootWindow(),
8, 8, DefaultDepth(display.XDisplay(), screen));
bt::Pen pen(screen, c);
XFillRectangle(display.XDisplay(), pixmap, pen.gc(), 0, 0, 8, 8);
setPixmapProperty(screen, pixmap);
}
}
void bsetroot::modula(int x, int y) {
char data[32];
long pattern;
unsigned int screen, i;
bt::Color f = bt::Color::namedColor(display, 0, fore),
b = bt::Color::namedColor(display, 0, back);
for (pattern = 0, screen = 0; screen < display.screenCount(); screen++) {
for (i = 0; i < 16; i++) {
pattern <<= 1;
if ((i % x) == 0)
pattern |= 0x0001;
}
for (i = 0; i < 16; i++) {
if ((i % y) == 0) {
data[(i * 2)] = static_cast<char>(0xff);
data[(i * 2) + 1] = static_cast<char>(0xff);
} else {
data[(i * 2)] = pattern & 0xff;
data[(i * 2) + 1] = (pattern >> 8) & 0xff;
}
}
const bt::ScreenInfo &screen_info = display.screenInfo(screen);
XGCValues gcv;
gcv.foreground = duplicateColor(screen, f);
gcv.background = duplicateColor(screen, b);
GC gc = XCreateGC(display.XDisplay(), screen_info.rootWindow(),
GCForeground | GCBackground, &gcv);
Pixmap pixmap = XCreatePixmap(display.XDisplay(),
screen_info.rootWindow(),
16, 16, screen_info.depth());
Pixmap bitmap =
XCreateBitmapFromData(display.XDisplay(), screen_info.rootWindow(),
data, 16, 16);
XCopyPlane(display.XDisplay(), bitmap, pixmap, gc,
0, 0, 16, 16, 0, 0, 1l);
XFreeGC(display.XDisplay(), gc);
XFreePixmap(display.XDisplay(), bitmap);
XSetWindowBackgroundPixmap(display.XDisplay(),
screen_info.rootWindow(),
pixmap);
XClearWindow(display.XDisplay(), screen_info.rootWindow());
setPixmapProperty(screen, pixmap);
}
}
void bsetroot::gradient(void) {
/*
we have to be sure that neither raised nor sunken is specified otherwise
odd looking borders appear. So we convert to lowercase then look for
'raised' or 'sunken' in the description and erase them. To be paranoid
the search is done in a loop.
*/
std::string descr = bt::tolower(grad);
std::string::size_type pos;
while ((pos = descr.find("raised")) != std::string::npos)
descr.erase(pos, 6); // 6 is strlen raised
while ((pos = descr.find("sunken")) != std::string::npos)
descr.erase(pos, 6);
// now add on 'flat' to prevent the bevels from being added
descr += "flat";
bt::Color f = bt::Color::namedColor(display, 0, fore);
bt::Color b = bt::Color::namedColor(display, 0, back);
bt::Texture texture;
texture.setDescription(descr);
texture.setColor1(f);
texture.setColor2(b);
for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
const bt::ScreenInfo &screen_info = display.screenInfo(screen);
bt::Image image(screen_info.width(), screen_info.height());
Pixmap pixmap = image.render(display, screen, texture);
XSetWindowBackgroundPixmap(display.XDisplay(),
screen_info.rootWindow(),
pixmap);
XClearWindow(display.XDisplay(), screen_info.rootWindow());
setPixmapProperty(screen, pixmap);
}
}
void bsetroot::usage(int exit_code) {
fprintf(stderr,
"bsetroot 3.1\n\n"
"Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
"Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n");
fprintf(stderr,
" -display <string> use display connection\n"
" -mod <x> <y> modula pattern\n"
" -foreground, -fg <color> modula foreground color\n"
" -background, -bg <color> modula background color\n\n"
" -gradient <texture> gradient texture\n"
" -from <color> gradient start color\n"
" -to <color> gradient end color\n\n"
" -solid <color> solid color\n\n"
" -help print this help text and exit\n");
exit(exit_code);
}
int main(int argc, char **argv) {
char *display_name = 0;
bool multi_head = False;
for (int i = 1; i < argc; i++) {
if (! strcmp(argv[i], "-display")) {
// check for -display option
if ((++i) >= argc) {
fprintf(stderr, "error: '-display' requires an argument\n");
::exit(1);
}
display_name = argv[i];
} else if (! strcmp(argv[i], "-multi")) {
multi_head = True;
}
}
bsetroot app(argc, argv, display_name, multi_head);
return 0;
}