]> icculus.org git repositories - mikachu/openbox.git/blob - util/bsetroot.cc
better behavior with root pixmap atoms. create them if they do not exist.
[mikachu/openbox.git] / util / bsetroot.cc
1 // bsetroot.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Janens (ben@orodu.net)
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Brad Hughes <bhughes at trolltech.com>
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 #ifdef    HAVE_STRING_H
29 #  include <string.h>
30 #endif // HAVE_STRING_H
31
32 #ifdef    HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif // HAVE_STDLIB_H
35
36 #ifdef    HAVE_STDIO_H
37 #  include <stdio.h>
38 #endif // HAVE_STDIO_H
39
40 #include "../src/i18n.h"
41 #include "../src/BaseDisplay.h"
42 #include "../src/Image.h"
43 #include "bsetroot.h"
44
45 #include <algorithm>
46
47 bsetroot::bsetroot(int argc, char **argv, char *dpy_name)
48   : BaseDisplay(argv[0], dpy_name)
49 {
50   grad = fore = back = (char *) 0;
51
52   Bool mod = False, sol = False, grd = False;
53   int mod_x = 0, mod_y = 0;
54
55   for (int i = 1; i < argc; i++) {
56     if (! strcmp("-help", argv[i])) {
57       usage();
58     } else if ((! strcmp("-fg", argv[i])) ||
59                (! strcmp("-foreground", argv[i])) ||
60                (! strcmp("-from", argv[i]))) {
61       if ((++i) >= argc) usage(1);
62
63       fore = argv[i];
64     } else if ((! strcmp("-bg", argv[i])) ||
65                (! strcmp("-background", argv[i])) ||
66                (! strcmp("-to", argv[i]))) {
67       if ((++i) >= argc) usage(1);
68
69       back = argv[i];
70     } else if (! strcmp("-solid", argv[i])) {
71       if ((++i) >= argc) usage(1);
72
73       fore = argv[i];
74       sol = True;
75     } else if (! strcmp("-mod", argv[i])) {
76       if ((++i) >= argc) usage();
77
78       mod_x = atoi(argv[i]);
79
80       if ((++i) >= argc) usage();
81
82       mod_y = atoi(argv[i]);
83
84       if (mod_x < 1) mod_x = 1;
85       if (mod_y < 1) mod_y = 1;
86
87       mod = True;
88     } else if (! strcmp("-gradient", argv[i])) {
89       if ((++i) >= argc) usage();
90
91       grad = argv[i];
92       grd = True;
93     } else if (! strcmp("-display", argv[i])) {
94       // -display passed through tests ealier... we just skip it now
95       i++;
96     } else
97       usage();
98   }
99
100   if ((mod + sol + grd) != True) {
101     fprintf(stderr,
102       i18n->getMessage(bsetrootSet, bsetrootMustSpecify,
103                        "%s: error: must specify one of: "
104                        "-solid, -mod, -gradient\n"),
105       getApplicationName());
106
107     usage(2);
108   }
109
110   img_ctrl = new BImageControl*[numberOfScreens()];
111   for (unsigned int s = 0, n = numberOfScreens(); s < n; ++s)
112     img_ctrl[s] = new BImageControl(*this, *getScreenInfo(s), true);
113
114   if (sol && fore) solid();
115   else if (mod && mod_x && mod_y && fore && back) modula(mod_x, mod_y);
116   else if (grd && grad && fore && back) gradient();
117   else usage();
118 }
119
120
121 bsetroot::~bsetroot(void) {
122   XSetCloseDownMode(getXDisplay(), RetainPermanent);
123
124   XKillClient(getXDisplay(), AllTemporary);
125
126   std::for_each(img_ctrl, img_ctrl + numberOfScreens(), PointerAssassin());
127
128   delete [] img_ctrl;
129 }
130
131
132 // adapted from wmsetbg
133 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
134   static Atom rootpmap_id = None, esetroot_id = None;
135   Atom type;
136   int format;
137   unsigned long length, after;
138   unsigned char *data;
139   int mode = PropModeAppend;
140   int emode = PropModeAppend;
141   const ScreenInfo *screen_info = getScreenInfo(screen);
142
143   if (rootpmap_id == None) {
144     rootpmap_id = XInternAtom(getXDisplay(), "_XROOTPMAP_ID", False);
145     esetroot_id = XInternAtom(getXDisplay(), "ESETROOT_PMAP_ID", False);
146   }
147
148   XGrabServer(getXDisplay());
149
150   // Clear out the old pixmap?
151   XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
152                      rootpmap_id, 0L, 1L, False, AnyPropertyType,
153                      &type, &format, &length, &after, &data);
154   if ((type == XA_PIXMAP) && (format == 32) && (length == 1)) {
155     XKillClient(getXDisplay(), *((Pixmap *) data));
156     XSync(getXDisplay(), False);
157     mode = PropModeReplace;
158   }
159
160   // Clear out the old esetroot pixmap?
161   XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
162                      esetroot_id, 0L, 1L, False, AnyPropertyType,
163                      &type, &format, &length, &after, &data);
164   if ((type == XA_PIXMAP) && (format == 32) && (length == 1))
165     emode = PropModeReplace;
166
167   if (pixmap) {
168     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
169         rootpmap_id, XA_PIXMAP, 32, mode,
170         (unsigned char *) &pixmap, 1);
171     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
172         esetroot_id, XA_PIXMAP, 32, emode,
173         (unsigned char *) &pixmap, 1);
174   } else {
175     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
176         rootpmap_id);
177     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
178         esetroot_id);
179   }
180
181   XUngrabServer(getXDisplay());
182   XFlush(getXDisplay());
183 }
184
185
186 // adapted from wmsetbg
187 Pixmap bsetroot::duplicatePixmap(int screen, Pixmap pixmap,
188          int width, int height) {
189   XSync(getXDisplay(), False);
190
191   Pixmap copyP = XCreatePixmap(getXDisplay(),
192              getScreenInfo(screen)->getRootWindow(),
193              width, height,
194              DefaultDepth(getXDisplay(), screen));
195   XCopyArea(getXDisplay(), pixmap, copyP, DefaultGC(getXDisplay(), screen),
196       0, 0, width, height, 0, 0);
197   XSync(getXDisplay(), False);
198
199   return copyP;
200 }
201
202
203 void bsetroot::solid(void) {
204   for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
205     BColor c;
206
207     img_ctrl[screen]->parseColor(&c, fore);
208     if (! c.isAllocated()) c.setPixel(BlackPixel(getXDisplay(), screen));
209
210     const ScreenInfo *screen_info = getScreenInfo(screen);
211
212     XSetWindowBackground(getXDisplay(), screen_info->getRootWindow(),
213                          c.getPixel());
214     XClearWindow(getXDisplay(), screen_info->getRootWindow());
215
216     Pixmap pixmap = XCreatePixmap(getXDisplay(),
217           screen_info->getRootWindow(),
218           8, 8, DefaultDepth(getXDisplay(), screen));
219     
220     XSetForeground(getXDisplay(), DefaultGC(getXDisplay(), screen),
221                    c.getPixel());
222     XFillRectangle(getXDisplay(), pixmap, DefaultGC(getXDisplay(), screen),
223                    0, 0, 8, 8);
224
225     setPixmapProperty(screen, duplicatePixmap(screen, pixmap, 8, 8));
226
227     XFreePixmap(getXDisplay(), pixmap);
228   }
229 }
230
231
232 void bsetroot::modula(int x, int y) {
233   char data[32];
234   long pattern;
235
236   unsigned int screen, i;
237   unsigned int n = numberOfScreens();
238
239   for (pattern = 0, screen = 0; screen < n; screen++) {
240     for (i = 0; i < 16; i++) {
241       pattern <<= 1;
242       if ((i % x) == 0)
243         pattern |= 0x0001;
244     }
245
246     for (i = 0; i < 16; i++) {
247       if ((i %  y) == 0) {
248         data[(i * 2)] = (char) 0xff;
249         data[(i * 2) + 1] = (char) 0xff;
250       } else {
251         data[(i * 2)] = pattern & 0xff;
252         data[(i * 2) + 1] = (pattern >> 8) & 0xff;
253       }
254     }
255
256     BColor f, b;
257     GC gc;
258     Pixmap bitmap;
259     
260     const ScreenInfo *screen_info = getScreenInfo(screen);
261
262     bitmap =
263       XCreateBitmapFromData(getXDisplay(),
264                             screen_info->getRootWindow(), data,
265                             16, 16);
266     
267     img_ctrl[screen]->parseColor(&f, fore);
268     img_ctrl[screen]->parseColor(&b, back);
269
270     if (! f.isAllocated()) f.setPixel(WhitePixel(getXDisplay(), screen));
271     if (! b.isAllocated()) b.setPixel(BlackPixel(getXDisplay(), screen));
272
273     XGCValues gcv;
274     gcv.foreground = f.getPixel();
275     gcv.background = b.getPixel();
276
277     gc = XCreateGC(getXDisplay(), screen_info->getRootWindow(),
278                    GCForeground | GCBackground, &gcv);
279
280     Pixmap pixmap = XCreatePixmap(getXDisplay(),
281           screen_info->getRootWindow(),
282           16, 16, screen_info->getDepth());
283
284     XCopyPlane(getXDisplay(), bitmap, pixmap, gc,
285                0, 0, 16, 16, 0, 0, 1l);
286     XSetWindowBackgroundPixmap(getXDisplay(),
287                                screen_info->getRootWindow(),
288                                pixmap);
289     XClearWindow(getXDisplay(), screen_info->getRootWindow());
290
291     setPixmapProperty(screen,
292           duplicatePixmap(screen, pixmap, 16, 16));
293
294     XFreeGC(getXDisplay(), gc);
295     XFreePixmap(getXDisplay(), bitmap);
296
297     if (! (screen_info->getVisual()->c_class & 1))
298       XFreePixmap(getXDisplay(), pixmap);
299   }
300 }
301
302
303 void bsetroot::gradient(void) {
304   for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
305     BTexture texture;
306     img_ctrl[screen]->parseTexture(&texture, grad);
307     img_ctrl[screen]->parseColor(texture.getColor(), fore);
308     img_ctrl[screen]->parseColor(texture.getColorTo(), back);
309     const ScreenInfo *screen_info = getScreenInfo(screen);
310
311     if (! texture.getColor()->isAllocated())
312       texture.getColor()->setPixel(WhitePixel(getXDisplay(), screen));
313     if (! texture.getColorTo()->isAllocated())
314       texture.getColorTo()->setPixel(BlackPixel(getXDisplay(), screen));
315
316     Pixmap pixmap =
317       img_ctrl[screen]->renderImage(screen_info->size().w(),
318                                     screen_info->size().h(),
319                                     &texture);
320
321     XSetWindowBackgroundPixmap(getXDisplay(),
322                                screen_info->getRootWindow(),
323                                pixmap);
324     XClearWindow(getXDisplay(), screen_info->getRootWindow());
325
326     setPixmapProperty(screen,
327           duplicatePixmap(screen, pixmap,
328               screen_info->size().w(),
329               screen_info->size().h()));
330
331     if (! (screen_info->getVisual()->c_class & 1)) {
332       img_ctrl[screen]->removeImage(pixmap);
333     }
334   }
335 }
336
337
338 void bsetroot::usage(int exit_code) {
339     fprintf(stderr,
340       i18n->getMessage(bsetrootSet, bsetrootUsage,
341      "%s 2.0\n\n"
342      "Copyright (c) 1997-2000, 2002 Bradley T Hughes\n"
343      "Copyright (c) 2001-2002 Sean 'Shaleh' Perry\n\n"
344      "  -display <string>        display connection\n"
345      "  -mod <x> <y>             modula pattern\n"
346      "  -foreground, -fg <color> modula foreground color\n"
347      "  -background, -bg <color> modula background color\n\n"
348      "  -gradient <texture>      gradient texture\n"
349      "  -from <color>            gradient start color\n"
350      "  -to <color>              gradient end color\n\n"
351      "  -solid <color>           solid color\n\n"
352      "  -help                    print this help text and exit\n"),
353       getApplicationName());
354
355     exit(exit_code);
356 }
357
358 int main(int argc, char **argv) {
359   char *display_name = (char *) 0;
360
361   NLSInit("openbox.cat");
362   
363   for (int i = 1; i < argc; i++) {
364     if (! strcmp(argv[i], "-display")) {
365       // check for -display option
366
367       if ((++i) >= argc) {
368         fprintf(stderr, i18n->getMessage(mainSet, mainDISPLAYRequiresArg,
369                  "error: '-display' requires an argument\n"));
370
371         ::exit(1);
372       }
373
374       display_name = argv[i];
375     }
376   }
377
378   bsetroot app(argc, argv, display_name);
379
380   return 0;
381 }
382