util/bsetroot.cc (raw)
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) // Copyright (c) 1997 - 2000 Brad Hughes <bhughes at trolltech.com> // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // $Id$ #include "bsetroot.hh" #include "../src/FbTk/I18n.hh" #include "../src/FbTk/ImageControl.hh" #include "../src/FbTk/GContext.hh" #include "../src/FbRootWindow.hh" #ifdef HAVE_CONFIG_H #include "config.h" #endif // HAVE_CONFIG_H #include <X11/Xatom.h> #ifdef HAVE_CSTRING #include <cstring> #else #include <string.h> #endif #ifdef HAVE_CSTDLIB #include <cstdlib> #else #include <stdlib.h> #endif #ifdef HAVE_CSTDIO #include <cstdio> #else #include <stdio.h> #endif #include <iostream> using namespace std; bsetroot::bsetroot(int argc, char **argv, char *dpy_name) : FbTk::App(dpy_name), m_app_name(argv[0]) { pixmaps = (Pixmap *) 0; grad = fore = back = (char *) 0; bool mod = false, sol = false, grd = false; int mod_x = 0, mod_y = 0, i = 0; img_ctrl = new FbTk::ImageControl*[ScreenCount(display())]; for (; i < ScreenCount(display()); i++) { img_ctrl[i] = new FbTk::ImageControl(i, true); } for (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 earlier... we just skip it now i++; } else usage(); } if ((mod + sol + grd) != true) { _FB_USES_NLS; cerr<<m_app_name<< _FBTEXT(bsetroot, MustSpecify, "Error: must specify one of: -solid, -mod, -gradient\n", "user didn't give one of the required options")<<endl; usage(2); } num_screens = ScreenCount(display()); if (sol && fore) solid(); else if (mod && mod_x && mod_y && fore && back) modula(mod_x, mod_y); else if (grd && grad && fore && back) gradient(); else usage(); } bsetroot::~bsetroot() { XKillClient(display(), AllTemporary); if (pixmaps) { // should always be true XSetCloseDownMode(display(), RetainTemporary); delete [] pixmaps; } if (img_ctrl != 0) { for (int i=0; i < num_screens; i++) delete img_ctrl[i]; delete [] img_ctrl; } } /** set root pixmap atoms so that apps like Eterm and xchat will be able to use transparent background */ void bsetroot::setRootAtoms(Pixmap pixmap, int screen) { Atom atom_root, atom_eroot, type; unsigned char *data_root, *data_eroot; int format; unsigned long length, after; atom_root = XInternAtom(display(), "_XROOTMAP_ID", true); atom_eroot = XInternAtom(display(), "ESETROOT_PMAP_ID", true); FbRootWindow root(screen); // doing this to clean up after old background if (atom_root != None && atom_eroot != None) { root.property(atom_root, 0L, 1L, false, AnyPropertyType, &type, &format, &length, &after, &data_root); if (type == XA_PIXMAP) { root.property(atom_eroot, 0L, 1L, False, AnyPropertyType, &type, &format, &length, &after, &data_eroot); if (data_root && data_eroot && type == XA_PIXMAP && *((Pixmap *) data_root) == *((Pixmap *) data_eroot)) { XKillClient(display(), *((Pixmap *) data_root)); } } } atom_root = XInternAtom(display(), "_XROOTPMAP_ID", false); atom_eroot = XInternAtom(display(), "ESETROOT_PMAP_ID", false); if (atom_root == None || atom_eroot == None) { _FB_USES_NLS; cerr<<_FBTEXT(bsetroot, NoPixmapAtoms, "Couldn't create pixmap atoms, giving up!", "Couldn't create atoms to point at root pixmap")<<endl; exit(1); } // setting new background atoms root.changeProperty(atom_root, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &pixmap, 1); root.changeProperty(atom_eroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &pixmap, 1); } /** Draws pixmaps with a single color */ void bsetroot::solid() { register int screen = 0; pixmaps = new Pixmap[ScreenCount(display())]; for (; screen < ScreenCount(display()); screen++) { FbTk::Color c(fore, screen); if (! c.isAllocated()) c.setPixel(BlackPixel(display(), screen)); FbRootWindow root(screen); FbTk::GContext gc(root); gc.setForeground(c); pixmaps[screen] = XCreatePixmap(display(), root.window(), root.width(), root.height(), root.depth()); XFillRectangle(display(), pixmaps[screen], gc.gc(), 0, 0, root.width(), root.height()); setRootAtoms(pixmaps[screen], screen); root.setBackgroundPixmap(pixmaps[screen]); root.clear(); } } /** Draws pixmaps with an 16x16 pattern with fg and bg colors. */ void bsetroot::modula(int x, int y) { char data[32]; long pattern; register int screen, i; pixmaps = new Pixmap[ScreenCount(display())]; for (pattern = 0, screen = 0; screen < ScreenCount(display()); screen++) { FbRootWindow root(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)] = (char) 0xff; data[(i * 2) + 1] = (char) 0xff; } else { data[(i * 2)] = pattern & 0xff; data[(i * 2) + 1] = (pattern >> 8) & 0xff; } } Pixmap bitmap, r_bitmap; bitmap = XCreateBitmapFromData(display(), root.window(), data, 16, 16); // bitmap used as tile, needs to have the same depth as background pixmap r_bitmap = XCreatePixmap(display(), root.window(), 16, 16, root.depth()); FbTk::Color f(fore, screen), b(back, screen); if (! f.isAllocated()) f.setPixel(WhitePixel(display(), screen)); if (! b.isAllocated()) b.setPixel(BlackPixel(display(), screen)); FbTk::GContext gc(root); gc.setForeground(f); gc.setBackground(b); // copying bitmap to the one going to be used as tile XCopyPlane(display(), bitmap, r_bitmap, gc.gc(), 0, 0, 16, 16, 0, 0, 1l); gc.setTile(r_bitmap); gc.setFillStyle(FillTiled); pixmaps[screen] = XCreatePixmap(display(), root.window(), root.width(), root.height(), root.depth()); XFillRectangle(display(), pixmaps[screen], gc.gc(), 0, 0, root.width(), root.height()); setRootAtoms(pixmaps[screen], screen); root.setBackgroundPixmap(pixmaps[screen]); root.clear(); XFreePixmap(display(), bitmap); XFreePixmap(display(), r_bitmap); } } /** draws pixmaps with a fluxbox texure */ void bsetroot::gradient() { // using temporaray pixmap and then copying it to background pixmap, as it'll // get crashed somewhere on the way causing apps like XChat chrashing // as the pixmap has been destroyed Pixmap tmp; pixmaps = new Pixmap[ScreenCount(display())]; // we must insert gradient text string texture_value = grad ? grad : "solid"; texture_value.insert(0, "gradient "); FbTk::Texture texture; texture.setFromString(texture_value.c_str()); for (int screen = 0; screen < ScreenCount(display()); screen++) { FbRootWindow root(screen); FbTk::GContext gc(root); texture.color().setFromString(fore, screen); texture.colorTo().setFromString(back, screen); if (! texture.color().isAllocated()) texture.color().setPixel(WhitePixel(display(), screen)); if (! texture.colorTo().isAllocated()) texture.colorTo().setPixel(BlackPixel(display(), screen)); tmp = img_ctrl[screen]->renderImage(root.width(), root.height(), texture); pixmaps[screen] = XCreatePixmap(display(), root.window(), root.width(), root.height(), root.depth()); XCopyArea(display(), tmp, pixmaps[screen], gc.gc(), 0, 0, root.width(), root.height(), 0, 0); setRootAtoms(pixmaps[screen], screen); root.setBackgroundPixmap(pixmaps[screen]); root.clear(); if (! (root.visual()->c_class & 1)) { img_ctrl[screen]->removeImage(tmp); img_ctrl[screen]->cleanCache(); } } } /** Shows information about usage */ void bsetroot::usage(int exit_code) { _FB_USES_NLS; cerr<<m_app_name<<" 2.3 : (c) 2003-2004 Fluxbox Development Team"<<endl; cerr<<m_app_name<<" 2.1 : (c) 2002 Claes Nasten"<<endl; cerr<<m_app_name<<" 2.0 : (c) 1997-2000 Brad Hughes\n"<<endl; cerr<<_FBTEXT(bsetroot, Usage, " -display <string> 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", "bsetroot usage options")<<endl; exit(exit_code); } int main(int argc, char **argv) { char *display_name = (char *) 0; int i = 1; FbTk::NLSInit("fluxbox.cat"); for (; i < argc; i++) { if (! strcmp(argv[i], "-display")) { // check for -display option if ((++i) >= argc) { _FB_USES_NLS; cerr<<_FBTEXT(main, DISPLAYRequiresArg, "error: '-display' requires an argument", "option requires an argument")<<endl; ::exit(1); } display_name = argv[i]; } } bsetroot app(argc, argv, display_name); return (0); } |