]> icculus.org git repositories - mikachu/openbox.git/blob - util/bsetroot.cc
nls from bb-cvs
[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 I18n i18n("openbox.cat");
48
49 bsetroot::bsetroot(int argc, char **argv, char *dpy_name)
50   : BaseDisplay(argv[0], dpy_name)
51 {
52   grad = fore = back = (char *) 0;
53
54   Bool mod = False, sol = False, grd = False;
55   int mod_x = 0, mod_y = 0;
56
57   for (int i = 1; i < argc; i++) {
58     if (! strcmp("-help", argv[i])) {
59       usage();
60     } else if ((! strcmp("-fg", argv[i])) ||
61                (! strcmp("-foreground", argv[i])) ||
62                (! strcmp("-from", argv[i]))) {
63       if ((++i) >= argc) usage(1);
64
65       fore = argv[i];
66     } else if ((! strcmp("-bg", argv[i])) ||
67                (! strcmp("-background", argv[i])) ||
68                (! strcmp("-to", argv[i]))) {
69       if ((++i) >= argc) usage(1);
70
71       back = argv[i];
72     } else if (! strcmp("-solid", argv[i])) {
73       if ((++i) >= argc) usage(1);
74
75       fore = argv[i];
76       sol = True;
77     } else if (! strcmp("-mod", argv[i])) {
78       if ((++i) >= argc) usage();
79
80       mod_x = atoi(argv[i]);
81
82       if ((++i) >= argc) usage();
83
84       mod_y = atoi(argv[i]);
85
86       if (mod_x < 1) mod_x = 1;
87       if (mod_y < 1) mod_y = 1;
88
89       mod = True;
90     } else if (! strcmp("-gradient", argv[i])) {
91       if ((++i) >= argc) usage();
92
93       grad = argv[i];
94       grd = True;
95     } else if (! strcmp("-display", argv[i])) {
96       // -display passed through tests ealier... we just skip it now
97       i++;
98     } else
99       usage();
100   }
101
102   if ((mod + sol + grd) != True) {
103     fprintf(stderr,
104       i18n(bsetrootSet, bsetrootMustSpecify,
105            "%s: error: must specify one of: -solid, -mod, -gradient\n"),
106       getApplicationName());
107
108     usage(2);
109   }
110
111   img_ctrl = new BImageControl*[numberOfScreens()];
112   for (unsigned int s = 0, n = numberOfScreens(); s < n; ++s)
113     img_ctrl[s] = new BImageControl(*this, *getScreenInfo(s), true);
114
115   if (sol && fore) solid();
116   else if (mod && mod_x && mod_y && fore && back) modula(mod_x, mod_y);
117   else if (grd && grad && fore && back) gradient();
118   else usage();
119 }
120
121
122 bsetroot::~bsetroot(void) {
123   XSetCloseDownMode(getXDisplay(), RetainPermanent);
124
125   XKillClient(getXDisplay(), AllTemporary);
126
127   std::for_each(img_ctrl, img_ctrl + numberOfScreens(), PointerAssassin());
128
129   delete [] img_ctrl;
130 }
131
132
133 // adapted from wmsetbg
134 void bsetroot::setPixmapProperty(int screen, Pixmap pixmap) {
135   static Atom rootpmap_id = None, esetroot_id = None;
136   Atom type;
137   int format;
138   unsigned long length, after;
139   unsigned char *data;
140   const ScreenInfo *screen_info = getScreenInfo(screen);
141
142   if (rootpmap_id == None) {
143     rootpmap_id = XInternAtom(getXDisplay(), "_XROOTPMAP_ID", False);
144     esetroot_id = XInternAtom(getXDisplay(), "ESETROOT_PMAP_ID", False);
145   }
146
147   XGrabServer(getXDisplay());
148
149   // Clear out the old pixmap
150   XGetWindowProperty(getXDisplay(), screen_info->getRootWindow(),
151                      rootpmap_id, 0L, 1L, False, XA_PIXMAP,
152                      &type, &format, &length, &after, &data);
153   if (type == XA_PIXMAP && format == 32) {
154     XKillClient(getXDisplay(), *((Pixmap *) data));
155     XSync(getXDisplay(), False);
156     XFree(data);
157   }
158
159   if (pixmap) {
160     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
161         rootpmap_id, XA_PIXMAP, 32, PropModeReplace,
162         (unsigned char *) &pixmap, 1);
163     XChangeProperty(getXDisplay(), screen_info->getRootWindow(),
164         esetroot_id, XA_PIXMAP, 32, PropModeReplace,
165         (unsigned char *) &pixmap, 1);
166   } else {
167     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
168         rootpmap_id);
169     XDeleteProperty(getXDisplay(), screen_info->getRootWindow(),
170         esetroot_id);
171   }
172
173   XUngrabServer(getXDisplay());
174   XFlush(getXDisplay());
175 }
176
177
178 // adapted from wmsetbg
179 Pixmap bsetroot::duplicatePixmap(int screen, Pixmap pixmap,
180          int width, int height) {
181   XSync(getXDisplay(), False);
182
183   Pixmap copyP = XCreatePixmap(getXDisplay(),
184              getScreenInfo(screen)->getRootWindow(),
185              width, height,
186              DefaultDepth(getXDisplay(), screen));
187   XCopyArea(getXDisplay(), pixmap, copyP, DefaultGC(getXDisplay(), screen),
188       0, 0, width, height, 0, 0);
189   XSync(getXDisplay(), False);
190
191   return copyP;
192 }
193
194
195 void bsetroot::solid(void) {
196   for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
197     BColor c;
198
199     img_ctrl[screen]->parseColor(&c, fore);
200     if (! c.isAllocated()) c.setPixel(BlackPixel(getXDisplay(), screen));
201
202     const ScreenInfo *screen_info = getScreenInfo(screen);
203
204     XSetWindowBackground(getXDisplay(), screen_info->getRootWindow(),
205                          c.getPixel());
206     XClearWindow(getXDisplay(), screen_info->getRootWindow());
207
208     Pixmap pixmap = XCreatePixmap(getXDisplay(),
209           screen_info->getRootWindow(),
210           8, 8, DefaultDepth(getXDisplay(), screen));
211     
212     XSetForeground(getXDisplay(), DefaultGC(getXDisplay(), screen),
213                    c.getPixel());
214     XFillRectangle(getXDisplay(), pixmap, DefaultGC(getXDisplay(), screen),
215                    0, 0, 8, 8);
216
217     setPixmapProperty(screen, duplicatePixmap(screen, pixmap, 8, 8));
218
219     XFreePixmap(getXDisplay(), pixmap);
220   }
221 }
222
223
224 void bsetroot::modula(int x, int y) {
225   char data[32];
226   long pattern;
227
228   unsigned int screen, i;
229   unsigned int n = numberOfScreens();
230
231   for (pattern = 0, screen = 0; screen < n; screen++) {
232     for (i = 0; i < 16; i++) {
233       pattern <<= 1;
234       if ((i % x) == 0)
235         pattern |= 0x0001;
236     }
237
238     for (i = 0; i < 16; i++) {
239       if ((i %  y) == 0) {
240         data[(i * 2)] = (char) 0xff;
241         data[(i * 2) + 1] = (char) 0xff;
242       } else {
243         data[(i * 2)] = pattern & 0xff;
244         data[(i * 2) + 1] = (pattern >> 8) & 0xff;
245       }
246     }
247
248     BColor f, b;
249     GC gc;
250     Pixmap bitmap;
251     
252     const ScreenInfo *screen_info = getScreenInfo(screen);
253
254     bitmap =
255       XCreateBitmapFromData(getXDisplay(),
256                             screen_info->getRootWindow(), data,
257                             16, 16);
258     
259     img_ctrl[screen]->parseColor(&f, fore);
260     img_ctrl[screen]->parseColor(&b, back);
261
262     if (! f.isAllocated()) f.setPixel(WhitePixel(getXDisplay(), screen));
263     if (! b.isAllocated()) b.setPixel(BlackPixel(getXDisplay(), screen));
264
265     XGCValues gcv;
266     gcv.foreground = f.getPixel();
267     gcv.background = b.getPixel();
268
269     gc = XCreateGC(getXDisplay(), screen_info->getRootWindow(),
270                    GCForeground | GCBackground, &gcv);
271
272     Pixmap pixmap = XCreatePixmap(getXDisplay(),
273           screen_info->getRootWindow(),
274           16, 16, screen_info->getDepth());
275
276     XCopyPlane(getXDisplay(), bitmap, pixmap, gc,
277                0, 0, 16, 16, 0, 0, 1l);
278     XSetWindowBackgroundPixmap(getXDisplay(),
279                                screen_info->getRootWindow(),
280                                pixmap);
281     XClearWindow(getXDisplay(), screen_info->getRootWindow());
282
283     setPixmapProperty(screen,
284           duplicatePixmap(screen, pixmap, 16, 16));
285
286     XFreeGC(getXDisplay(), gc);
287     XFreePixmap(getXDisplay(), bitmap);
288
289     if (! (screen_info->getVisual()->c_class & 1))
290       XFreePixmap(getXDisplay(), pixmap);
291   }
292 }
293
294
295 void bsetroot::gradient(void) {
296   for (unsigned int screen = 0, n = numberOfScreens(); screen < n; screen++) {
297     BTexture texture;
298     img_ctrl[screen]->parseTexture(&texture, grad);
299     img_ctrl[screen]->parseColor(texture.getColor(), fore);
300     img_ctrl[screen]->parseColor(texture.getColorTo(), back);
301     const ScreenInfo *screen_info = getScreenInfo(screen);
302
303     if (! texture.getColor()->isAllocated())
304       texture.getColor()->setPixel(WhitePixel(getXDisplay(), screen));
305     if (! texture.getColorTo()->isAllocated())
306       texture.getColorTo()->setPixel(BlackPixel(getXDisplay(), screen));
307
308     Pixmap pixmap =
309       img_ctrl[screen]->renderImage(screen_info->size().w(),
310                                     screen_info->size().h(),
311                                     &texture);
312
313     XSetWindowBackgroundPixmap(getXDisplay(),
314                                screen_info->getRootWindow(),
315                                pixmap);
316     XClearWindow(getXDisplay(), screen_info->getRootWindow());
317
318     setPixmapProperty(screen,
319           duplicatePixmap(screen, pixmap,
320               screen_info->size().w(),
321               screen_info->size().h()));
322
323     if (! (screen_info->getVisual()->c_class & 1)) {
324       img_ctrl[screen]->removeImage(pixmap);
325     }
326   }
327 }
328
329
330 void bsetroot::usage(int exit_code) {
331     fprintf(stderr,
332       i18n(bsetrootSet, bsetrootUsage,
333      "%s 2.0\n\n"
334      "Copyright (c) 1997-2000, 2002 Bradley T Hughes\n"
335      "Copyright (c) 2001-2002 Sean 'Shaleh' Perry\n\n"
336      "  -display <string>        display connection\n"
337      "  -mod <x> <y>             modula pattern\n"
338      "  -foreground, -fg <color> modula foreground color\n"
339      "  -background, -bg <color> modula background color\n\n"
340      "  -gradient <texture>      gradient texture\n"
341      "  -from <color>            gradient start color\n"
342      "  -to <color>              gradient end color\n\n"
343      "  -solid <color>           solid color\n\n"
344      "  -help                    print this help text and exit\n"),
345       getApplicationName());
346
347     exit(exit_code);
348 }
349
350 int main(int argc, char **argv) {
351   char *display_name = (char *) 0;
352
353   for (int i = 1; i < argc; i++) {
354     if (! strcmp(argv[i], "-display")) {
355       // check for -display option
356
357       if ((++i) >= argc) {
358         fprintf(stderr, i18n(mainSet, mainDISPLAYRequiresArg,
359                              "error: '-display' requires an argument\n"));
360
361         ::exit(1);
362       }
363
364       display_name = argv[i];
365     }
366   }
367
368   bsetroot app(argc, argv, display_name);
369
370   return 0;
371 }
372