handle clock rollback... we need to adjust the start time of all
[blackbox.git] / util / bsetroot.cc
blob9fe3c5d512d159eb0484e72cdcb6a83f57054b19
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // bsetroot - a background setting utility
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 #include "bsetroot.hh"
27 #include <Pen.hh>
28 #include <Texture.hh>
30 #include <cctype>
32 #include <X11/Xatom.h>
33 #include <stdio.h>
36 // ignore all X errors
37 static int x11_error(::Display *, XErrorEvent *)
38 { return 0; }
41 bsetroot::bsetroot(int argc, char **argv, char *dpy_name,
42 bool multi_head): display(dpy_name, multi_head) {
43 XSetErrorHandler(x11_error); // silently handle all errors
45 bool mod = False, sol = False, grd = False;
46 int mod_x = 0, mod_y = 0;
48 for (int i = 1; i < argc; i++) {
49 if (! strcmp("-help", argv[i])) {
50 usage();
51 } else if ((! strcmp("-fg", argv[i])) ||
52 (! strcmp("-foreground", argv[i])) ||
53 (! strcmp("-from", argv[i]))) {
54 if ((++i) >= argc) usage(1);
56 fore = argv[i];
57 } else if ((! strcmp("-bg", argv[i])) ||
58 (! strcmp("-background", argv[i])) ||
59 (! strcmp("-to", argv[i]))) {
60 if ((++i) >= argc) usage(1);
62 back = argv[i];
63 } else if (! strcmp("-solid", argv[i])) {
64 if ((++i) >= argc) usage(1);
66 fore = argv[i];
67 sol = True;
68 } else if (! strcmp("-mod", argv[i])) {
69 if ((++i) >= argc) usage();
71 mod_x = atoi(argv[i]);
73 if ((++i) >= argc) usage();
75 mod_y = atoi(argv[i]);
77 if (mod_x < 1) mod_x = 1;
78 if (mod_y < 1) mod_y = 1;
80 mod = True;
81 } else if (! strcmp("-gradient", argv[i])) {
82 if ((++i) >= argc) usage();
84 grad = argv[i];
85 grd = True;
86 } else if (! strcmp("-display", argv[i])) {
87 // -display passed through tests ealier... we
88 i++;
89 } else {
90 usage();
94 if ((mod + sol + grd) != True) {
95 fprintf(stderr,
96 "bsetroot: error: must specify one of: -solid, -mod, -gradient\n");
98 usage(2);
101 // keep the server grabbed while rendering all screens
102 XGrabServer(display.XDisplay());
104 if (sol && ! fore.empty())
105 solid();
106 else if (mod && mod_x && mod_y && ! (fore.empty() || back.empty()))
107 modula(mod_x, mod_y);
108 else if (grd && ! (grad.empty() || fore.empty() || back.empty()))
109 gradient();
110 else
111 usage();
113 // ungrab the server and discard any events
114 XUngrabServer(display.XDisplay());
115 XSync(display.XDisplay(), True);
119 bsetroot::~bsetroot(void) {
120 XSetCloseDownMode(display.XDisplay(), RetainPermanent);
121 XKillClient(display.XDisplay(), AllTemporary);
125 // adapted from wmsetbg
126 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
127 Atom rootpmap_id = XInternAtom(display.XDisplay(), "_XROOTPMAP_ID", False),
128 esetroot_id = XInternAtom(display.XDisplay(), "ESETROOT_PMAP_ID", False);
130 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
132 Atom type;
133 int format;
134 unsigned long length, after;
135 unsigned char *data = 0;
136 Pixmap xrootpmap = None;
137 Pixmap esetrootpmap = None;
139 // Clear out the old _XROOTPMAP_ID property
140 XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
141 rootpmap_id, 0L, 1L, True, AnyPropertyType,
142 &type, &format, &length, &after, &data);
144 if (data && type == XA_PIXMAP && format == 32 && length == 1)
145 xrootpmap = *(reinterpret_cast<Pixmap *>(data));
147 if (data) XFree(data);
149 // Clear out the old ESETROOT_PMAP_ID property
150 XGetWindowProperty(display.XDisplay(), screen_info.rootWindow(),
151 esetroot_id, 0L, 1L, True, AnyPropertyType,
152 &type, &format, &length, &after, &data);
154 if (data && type == XA_PIXMAP && format == 32 && length == 1)
155 esetrootpmap = *(reinterpret_cast<Pixmap *>(data));
157 if (data) XFree(data);
159 // Destroy the old pixmaps
160 if (xrootpmap)
161 XKillClient(display.XDisplay(), xrootpmap);
162 if (esetrootpmap && esetrootpmap != xrootpmap)
163 XKillClient(display.XDisplay(), esetrootpmap);
165 if (pixmap) {
166 XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
167 rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
168 reinterpret_cast<unsigned char *>(&pixmap), 1);
169 XChangeProperty(display.XDisplay(), screen_info.rootWindow(),
170 esetroot_id, XA_PIXMAP, 32, PropModeReplace,
171 reinterpret_cast<unsigned char *>(&pixmap), 1);
176 unsigned long bsetroot::duplicateColor(unsigned int screen,
177 const bt::Color &color) {
179 When using a colormap that is not read-only, we need to
180 reallocate the color we are using. The application's color cache
181 will be freed on exit, and another client can allocate a new
182 color at the same pixel value, which will immediately change the
183 color of the root window.
185 this is not what we want, so we need to make sure that the pixel
186 we are using is doubly allocated, so that it stays around with the
187 pixmap. It will be released when we use
188 XKillClient(..., AllTemporary);
190 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
191 unsigned long pixel = color.pixel(screen);
192 XColor xcolor;
193 xcolor.pixel = pixel;
194 XQueryColor(display.XDisplay(), screen_info.colormap(), &xcolor);
195 if (! XAllocColor(display.XDisplay(), screen_info.colormap(),
196 &xcolor)) {
197 fprintf(stderr, "warning: couldn't duplicate color %02x/%02x/%02x\n",
198 color.red(), color.green(), color.blue());
200 return pixel;
204 void bsetroot::solid(void) {
205 bt::Color c = bt::Color::namedColor(display, 0, fore);
207 for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
208 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
209 unsigned long pixel = duplicateColor(screen, c);
211 XSetWindowBackground(display.XDisplay(), screen_info.rootWindow(), pixel);
212 XClearWindow(display.XDisplay(), screen_info.rootWindow());
214 Pixmap pixmap =
215 XCreatePixmap(display.XDisplay(), screen_info.rootWindow(),
216 8, 8, DefaultDepth(display.XDisplay(), screen));
218 bt::Pen pen(screen, c);
219 XFillRectangle(display.XDisplay(), pixmap, pen.gc(), 0, 0, 8, 8);
221 setPixmapProperty(screen, pixmap);
226 void bsetroot::modula(int x, int y) {
227 char data[32];
228 long pattern;
230 unsigned int screen, i;
232 bt::Color f = bt::Color::namedColor(display, 0, fore),
233 b = bt::Color::namedColor(display, 0, back);
235 for (pattern = 0, screen = 0; screen < display.screenCount(); screen++) {
236 for (i = 0; i < 16; i++) {
237 pattern <<= 1;
238 if ((i % x) == 0)
239 pattern |= 0x0001;
242 for (i = 0; i < 16; i++) {
243 if ((i % y) == 0) {
244 data[(i * 2)] = static_cast<char>(0xff);
245 data[(i * 2) + 1] = static_cast<char>(0xff);
246 } else {
247 data[(i * 2)] = pattern & 0xff;
248 data[(i * 2) + 1] = (pattern >> 8) & 0xff;
252 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
254 XGCValues gcv;
255 gcv.foreground = duplicateColor(screen, f);
256 gcv.background = duplicateColor(screen, b);
258 GC gc = XCreateGC(display.XDisplay(), screen_info.rootWindow(),
259 GCForeground | GCBackground, &gcv);
261 Pixmap pixmap = XCreatePixmap(display.XDisplay(),
262 screen_info.rootWindow(),
263 16, 16, screen_info.depth());
265 Pixmap bitmap =
266 XCreateBitmapFromData(display.XDisplay(), screen_info.rootWindow(),
267 data, 16, 16);
268 XCopyPlane(display.XDisplay(), bitmap, pixmap, gc,
269 0, 0, 16, 16, 0, 0, 1l);
270 XFreeGC(display.XDisplay(), gc);
271 XFreePixmap(display.XDisplay(), bitmap);
273 XSetWindowBackgroundPixmap(display.XDisplay(),
274 screen_info.rootWindow(),
275 pixmap);
276 XClearWindow(display.XDisplay(), screen_info.rootWindow());
278 setPixmapProperty(screen, pixmap);
283 void bsetroot::gradient(void) {
285 we have to be sure that neither raised nor sunken is specified otherwise
286 odd looking borders appear. So we convert to lowercase then look for
287 'raised' or 'sunken' in the description and erase them. To be paranoid
288 the search is done in a loop.
290 std::string descr = bt::tolower(grad);
291 std::string::size_type pos;
292 while ((pos = descr.find("raised")) != std::string::npos)
293 descr.erase(pos, 6); // 6 is strlen raised
294 while ((pos = descr.find("sunken")) != std::string::npos)
295 descr.erase(pos, 6);
297 // now add on 'flat' to prevent the bevels from being added
298 descr += "flat";
300 bt::Color f = bt::Color::namedColor(display, 0, fore);
301 bt::Color b = bt::Color::namedColor(display, 0, back);
303 bt::Texture texture;
304 texture.setDescription(descr);
305 texture.setColor1(f);
306 texture.setColor2(b);
308 for (unsigned int screen = 0; screen < display.screenCount(); screen++) {
309 const bt::ScreenInfo &screen_info = display.screenInfo(screen);
311 bt::Image image(screen_info.width(), screen_info.height());
312 Pixmap pixmap = image.render(display, screen, texture);
314 XSetWindowBackgroundPixmap(display.XDisplay(),
315 screen_info.rootWindow(),
316 pixmap);
317 XClearWindow(display.XDisplay(), screen_info.rootWindow());
319 setPixmapProperty(screen, pixmap);
324 void bsetroot::usage(int exit_code) {
325 fprintf(stderr,
326 "bsetroot 3.1\n\n"
327 "Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry\n"
328 "Copyright (c) 1997 - 2000, 2002 - 2005 Bradley T Hughes\n");
329 fprintf(stderr,
330 " -display <string> use display connection\n"
331 " -mod <x> <y> modula pattern\n"
332 " -foreground, -fg <color> modula foreground color\n"
333 " -background, -bg <color> modula background color\n\n"
334 " -gradient <texture> gradient texture\n"
335 " -from <color> gradient start color\n"
336 " -to <color> gradient end color\n\n"
337 " -solid <color> solid color\n\n"
338 " -help print this help text and exit\n");
339 exit(exit_code);
342 int main(int argc, char **argv) {
343 char *display_name = 0;
344 bool multi_head = False;
346 for (int i = 1; i < argc; i++) {
347 if (! strcmp(argv[i], "-display")) {
348 // check for -display option
350 if ((++i) >= argc) {
351 fprintf(stderr, "error: '-display' requires an argument\n");
353 ::exit(1);
356 display_name = argv[i];
357 } else if (! strcmp(argv[i], "-multi")) {
358 multi_head = True;
362 bsetroot app(argc, argv, display_name, multi_head);
363 return 0;