fluxbox/util/fbsetroot.cc

413 lines
12 KiB
C++
Raw Normal View History

2006-02-16 06:53:05 +00:00
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
2002-08-18 09:47:03 +00:00
// 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
#include "fbsetroot.hh"
2002-08-15 11:19:21 +00:00
2004-06-08 13:15:30 +00:00
#include "../src/FbTk/I18n.hh"
2003-02-17 13:33:36 +00:00
#include "../src/FbTk/ImageControl.hh"
#include "../src/FbTk/Texture.hh"
#include "../src/FbTk/GContext.hh"
2003-05-11 00:00:32 +00:00
#include "../src/FbRootWindow.hh"
2002-08-15 11:19:21 +00:00
#include <X11/Xatom.h>
2001-12-11 20:47:02 +00:00
2004-08-31 15:26:40 +00:00
#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>
2002-08-15 11:19:21 +00:00
2006-10-30 19:31:15 +00:00
using std::cout;
using std::cerr;
using std::endl;
using std::string;
2001-12-11 20:47:02 +00:00
inline int getRootDepth(const FbTk::FbWindow& w) {
return (w.depth() == 32 ? 24 : w.depth());
}
fbsetroot::fbsetroot(int argc, char **argv, char *dpy_name)
2003-05-10 15:44:15 +00:00
: FbTk::App(dpy_name), m_app_name(argv[0]) {
pixmap = (Pixmap *) 0;
screen = DefaultScreen(FbTk::App::instance()->display());
2003-05-10 15:44:15 +00:00
grad = fore = back = (char *) 0;
bool mod = false, sol = false, grd = false;
int mod_x = 0, mod_y = 0, i = 1;
2003-05-10 15:44:15 +00:00
img_ctrl = new FbTk::ImageControl(screen);
2003-05-10 15:44:15 +00:00
for (; i < argc; i++) {
string arg = argv[i];
if (arg == "-help" || arg == "--help" || arg == "-h") {
2003-05-10 15:44:15 +00:00
usage();
} else if (arg == "-fg" || arg == "-foreground" ||
arg == "--foreground" || arg == "-from" || arg == "--from") {
2003-05-10 15:44:15 +00:00
if ((++i) >= argc)
usage(1);
fore = argv[i];
} else if (arg == "-bg" || arg == "-background" ||
arg == "--background" || arg == "-to" || arg == "--to") {
2003-05-10 15:44:15 +00:00
if ((++i) >= argc)
usage(1);
back = argv[i];
} else if (arg == "-solid" || arg == "--solid") {
2003-05-10 15:44:15 +00:00
if ((++i) >= argc)
usage(1);
fore = argv[i];
sol = true;
} else if (arg == "-mod" || arg == "--mod") {
2003-05-10 15:44:15 +00:00
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 (arg == "-gradient" || arg == "--gradient") {
2003-05-10 15:44:15 +00:00
if ((++i) >= argc)
usage();
grad = argv[i];
grd = true;
} else if (arg == "-display" || arg == "--display") {
2003-05-10 15:44:15 +00:00
// -display passed through tests earlier... we just skip it now
i++;
} else
usage();
}
if ((mod + sol + grd) != true) {
2004-06-08 13:15:30 +00:00
_FB_USES_NLS;
2006-10-30 19:31:15 +00:00
cerr << _FB_CONSOLETEXT(fbsetroot, MustSpecify,
2004-06-08 13:15:30 +00:00
"Error: must specify one of: -solid, -mod, -gradient\n",
2006-02-01 21:03:46 +00:00
"user didn't give one of the required options") << endl;
2003-05-10 15:44:15 +00:00
2006-02-01 21:03:46 +00:00
exit(2);
2003-05-10 15:44:15 +00:00
}
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();
2001-12-11 20:47:02 +00:00
}
fbsetroot::~fbsetroot() {
2003-05-10 15:44:15 +00:00
XKillClient(display(), AllTemporary);
2001-12-11 20:47:02 +00:00
if (pixmap) { // should always be true
2003-05-10 15:44:15 +00:00
XSetCloseDownMode(display(), RetainTemporary);
2001-12-11 20:47:02 +00:00
delete pixmap;
2003-05-10 15:44:15 +00:00
}
delete img_ctrl;
}
2001-12-11 20:47:02 +00:00
2002-11-26 17:48:58 +00:00
/**
2003-05-10 15:44:15 +00:00
set root pixmap atoms so that apps like
Eterm and xchat will be able to use
transparent background
2002-11-26 17:48:58 +00:00
*/
void fbsetroot::setRootAtoms(Pixmap pixmap, int screen) {
2003-05-10 15:44:15 +00:00
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);
2003-05-11 00:00:32 +00:00
FbRootWindow root(screen);
2003-05-10 15:44:15 +00:00
// 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);
2003-05-10 15:44:15 +00:00
if (type == XA_PIXMAP) {
root.property(atom_eroot, 0L, 1L, False, AnyPropertyType,
&type, &format, &length, &after, &data_eroot);
2006-10-30 19:31:15 +00:00
2003-05-10 15:44:15 +00:00
if (data_root && data_eroot && type == XA_PIXMAP &&
*((Pixmap *) data_root) == *((Pixmap *) data_eroot)) {
2006-10-30 19:31:15 +00:00
2003-05-10 15:44:15 +00:00
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) {
2004-06-08 13:15:30 +00:00
_FB_USES_NLS;
2006-06-21 17:29:35 +00:00
cerr<<_FB_CONSOLETEXT(fbsetroot, NoPixmapAtoms, "Couldn't create pixmap atoms, giving up!", "Couldn't create atoms to point at root pixmap")<<endl;
2003-05-10 15:44:15 +00:00
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);
}
2003-05-10 15:44:15 +00:00
/**
2006-10-30 19:31:15 +00:00
Draws pixmaps with a single color
2003-05-10 15:44:15 +00:00
*/
void fbsetroot::solid() {
FbTk::Color c(fore, screen);
if (! c.isAllocated())
c.setPixel(BlackPixel(display(), screen));
FbRootWindow root(screen);
FbTk::GContext gc(root);
gc.setForeground(c);
2006-10-30 19:31:15 +00:00
pixmap = new Pixmap(XCreatePixmap(display(),
root.window(),
root.width(), root.height(),
getRootDepth(root)));
XFillRectangle(display(), *pixmap, gc.gc(), 0, 0,
root.width(), root.height());
setRootAtoms(*pixmap, screen);
2001-12-11 20:47:02 +00:00
root.setBackgroundPixmap(*pixmap);
root.clear();
2001-12-11 20:47:02 +00:00
}
/**
Draws pixmaps with an 16x16 pattern with
fg and bg colors.
*/
void fbsetroot::modula(int x, int y) {
2011-10-21 06:34:53 +00:00
const int s = 16;
2003-05-10 15:44:15 +00:00
char data[32];
long pattern = 0;
2001-12-11 20:47:02 +00:00
register int i;
2001-12-11 20:47:02 +00:00
FbRootWindow root(screen);
2001-12-11 20:47:02 +00:00
2011-10-21 06:34:53 +00:00
for (i = 0; i < s; i++) {
pattern <<= 1;
if ((i % x) == 0)
pattern |= 0x0001;
}
2001-12-11 20:47:02 +00:00
2011-10-21 06:34:53 +00:00
for (i = 0; i < s; 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;
2003-05-10 15:44:15 +00:00
}
}
2001-12-11 20:47:02 +00:00
Pixmap bitmap, r_bitmap;
2001-12-11 20:47:02 +00:00
bitmap = XCreateBitmapFromData(display(),
2011-10-21 06:34:53 +00:00
root.window(), data, s, s);
2001-12-11 20:47:02 +00:00
// bitmap used as tile, needs to have the same depth as background pixmap
r_bitmap = XCreatePixmap(display(),
2011-10-21 06:34:53 +00:00
root.window(), s, s,
(root.depth() == 32 ? 24 : root.depth()));
2001-12-11 20:47:02 +00:00
FbTk::Color f(fore, screen), b(back, screen);
2001-12-11 20:47:02 +00:00
if (! f.isAllocated())
f.setPixel(WhitePixel(display(), screen));
if (! b.isAllocated())
b.setPixel(BlackPixel(display(), screen));
2001-12-11 20:47:02 +00:00
FbTk::GContext gc(root);
2001-12-11 20:47:02 +00:00
gc.setForeground(f);
gc.setBackground(b);
2001-12-11 20:47:02 +00:00
// copying bitmap to the one going to be used as tile
XCopyPlane(display(), bitmap, r_bitmap, gc.gc(),
2011-10-21 06:34:53 +00:00
0, 0, s, s, 0, 0, 1l);
2001-12-11 20:47:02 +00:00
gc.setTile(r_bitmap);
gc.setFillStyle(FillTiled);
2006-10-30 19:31:15 +00:00
pixmap = new Pixmap(XCreatePixmap(display(),
root.window(),
root.width(), root.height(),
getRootDepth(root)));
XFillRectangle(display(), *pixmap, gc.gc(), 0, 0,
root.width(), root.height());
2001-12-11 20:47:02 +00:00
setRootAtoms(*pixmap, screen);
root.setBackgroundPixmap(*pixmap);
root.clear();
2001-12-11 20:47:02 +00:00
XFreePixmap(display(), bitmap);
XFreePixmap(display(), r_bitmap);
}
2003-05-10 15:44:15 +00:00
/**
draws pixmaps with a fluxbox texure
*/
void fbsetroot::gradient() {
2003-05-10 15:44:15 +00:00
// 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;
// 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());
2003-05-11 00:00:32 +00:00
FbRootWindow root(screen);
2003-05-10 15:44:15 +00:00
FbTk::GContext gc(root);
texture.color().setFromString(fore, screen);
texture.colorTo().setFromString(back, screen);
if (! texture.color().isAllocated())
texture.color().setPixel(WhitePixel(display(), screen));
2001-12-11 20:47:02 +00:00
if (! texture.colorTo().isAllocated())
texture.colorTo().setPixel(BlackPixel(display(), screen));
2001-12-11 20:47:02 +00:00
tmp = img_ctrl->renderImage(root.width(), root.height(), texture);
2001-12-11 20:47:02 +00:00
2006-10-30 19:31:15 +00:00
pixmap = new Pixmap(XCreatePixmap(display(),
root.window(),
root.width(), root.height(),
getRootDepth(root)));
2001-12-11 20:47:02 +00:00
XCopyArea(display(), tmp, *pixmap, gc.gc(), 0, 0,
root.width(), root.height(),
0, 0);
2001-12-11 20:47:02 +00:00
setRootAtoms(*pixmap, screen);
2001-12-11 20:47:02 +00:00
root.setBackgroundPixmap(*pixmap);
root.clear();
if (! (root.visual()->c_class & 1)) {
img_ctrl->removeImage(tmp);
img_ctrl->cleanCache();
2003-05-10 15:44:15 +00:00
}
}
2003-05-10 15:44:15 +00:00
/**
Shows information about usage
*/
void fbsetroot::usage(int exit_code) {
2004-06-08 13:15:30 +00:00
_FB_USES_NLS;
2011-10-21 06:34:53 +00:00
cout << m_app_name << " 2.3 : (c) 2003-2011 Fluxbox Development Team" << endl;
2006-02-01 21:02:35 +00:00
cout << m_app_name << " 2.1 : (c) 2002 Claes Nasten" << endl;
cout << m_app_name << " 2.0 : (c) 1997-2000 Brad Hughes\n" << endl;
2006-06-21 17:29:35 +00:00
cout << _FB_CONSOLETEXT(fbsetroot, Usage,
2004-06-08 13:15:30 +00:00
" -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",
2006-02-01 21:02:35 +00:00
"fbsetroot usage options") << endl;
2003-05-10 15:44:15 +00:00
exit(exit_code);
2001-12-11 20:47:02 +00:00
}
int main(int argc, char **argv) {
2003-05-10 15:44:15 +00:00
char *display_name = (char *) 0;
int i = 1;
2006-10-30 19:31:15 +00:00
2004-06-08 13:15:30 +00:00
FbTk::NLSInit("fluxbox.cat");
2006-10-30 19:31:15 +00:00
2003-05-10 15:44:15 +00:00
for (; i < argc; i++) {
if (!strcmp(argv[i], "-display") || !strcmp(argv[i], "--display")) {
2003-05-10 15:44:15 +00:00
// check for -display option
if ((++i) >= argc) {
2004-06-08 13:15:30 +00:00
_FB_USES_NLS;
2006-06-21 17:29:35 +00:00
cerr<<_FB_CONSOLETEXT(main, DISPLAYRequiresArg,
2004-06-08 13:15:30 +00:00
"error: '-display' requires an argument",
"option requires an argument")<<endl;
2003-05-10 15:44:15 +00:00
::exit(1);
}
2003-05-10 15:44:15 +00:00
display_name = argv[i];
}
}
try {
fbsetroot app(argc, argv, display_name);
} catch (string & error_str) {
2006-06-29 05:46:11 +00:00
_FB_USES_NLS;
cerr<<_FB_CONSOLETEXT(Common, Error, "Error", "Error message header")<<": "<<error_str<<endl;
}
2003-05-10 15:44:15 +00:00
return (0);
2001-12-11 20:47:02 +00:00
}