2
* $Xorg: xsetroot.c,v 1.4 2001/02/09 02:05:59 xorgcvs Exp $
4
Copyright 1987, 1998 The Open Group
6
Permission to use, copy, modify, distribute, and sell this software and its
7
documentation for any purpose is hereby granted without fee, provided that
8
the above copyright notice appear in all copies and that both that
9
copyright notice and this permission notice appear in supporting
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
Except as contained in this notice, the name of The Open Group shall not be
23
used in advertising or otherwise to promote the sale, use or other dealings
24
in this Software without prior written authorization from The Open Group.
28
* xsetroot.c MIT Project Athena, X Window System root window
29
* parameter setting utility. This program will set
30
* various parameters of the X root window.
32
* Author: Mark Lillibridge, MIT Project Athena
35
/* $XFree86: xc/programs/xsetroot/xsetroot.c,v 1.7 2001/04/01 14:00:24 tsi Exp $ */
38
#include <X11/Xutil.h>
39
#include <X11/Xatom.h>
40
#include <X11/Xmu/CurUtil.h>
44
#include "X11/bitmaps/gray"
52
char *fore_color = NULL;
53
char *back_color = NULL;
57
Pixmap save_pixmap = (Pixmap)None;
59
static void usage(void);
60
static void FixupState(void);
61
static void SetBackgroundToBitmap(Pixmap bitmap,
62
unsigned int width, unsigned int height);
63
static Cursor CreateCursorFromFiles(char *cursor_file, char *mask_file);
64
static Cursor CreateCursorFromName(char *name);
65
static Pixmap MakeModulaBitmap(int mod_x, int mod_y);
66
static XColor NameToXColor(char *name, unsigned long pixel);
67
static unsigned long NameToPixel(char *name, unsigned long pixel);
68
static Pixmap ReadBitmapFile(char *filename, unsigned int *width, unsigned int *height, int *x_hot, int *y_hot);
73
fprintf(stderr, "usage: %s [options]\n", program_name);
74
fprintf(stderr, " where options are:\n");
75
fprintf(stderr, " -display <display> or -d <display>\n");
76
fprintf(stderr, " -fg <color> or -foreground <color>\n");
77
fprintf(stderr, " -bg <color> or -background <color>\n");
78
fprintf(stderr, " -rv or -reverse\n");
79
fprintf(stderr, " -help\n");
80
fprintf(stderr, " -def or -default\n");
81
fprintf(stderr, " -name <string>\n");
82
fprintf(stderr, " -cursor <cursor file> <mask file>\n");
83
fprintf(stderr, " -cursor_name <cursor-font name>\n");
84
fprintf(stderr, " -solid <color>\n");
85
fprintf(stderr, " -gray or -grey\n");
86
fprintf(stderr, " -bitmap <filename>\n");
87
fprintf(stderr, " -mod <x> <y>\n");
94
main(int argc, char *argv[])
98
int restore_defaults = 0;
99
char *display_name = NULL;
101
char *cursor_file = NULL;
102
char *cursor_mask = NULL;
103
char *cursor_name = NULL;
104
char *solid_color = NULL;
107
char *bitmap_file = NULL;
114
program_name=argv[0];
116
for (i = 1; i < argc; i++) {
117
if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
118
if (++i>=argc) usage ();
119
display_name = argv[i];
122
if (!strcmp("-help", argv[i])) {
125
if (!strcmp("-def", argv[i]) || !strcmp("-default", argv[i])) {
126
restore_defaults = 1;
129
if (!strcmp("-name", argv[i])) {
130
if (++i>=argc) usage();
135
if (!strcmp("-cursor", argv[i])) {
136
if (++i>=argc) usage();
137
cursor_file = argv[i];
138
if (++i>=argc) usage();
139
cursor_mask = argv[i];
143
if (!strcmp("-cursor_name", argv[i])) {
144
if (++i>=argc) usage();
145
cursor_name = argv[i];
149
if (!strcmp("-fg",argv[i]) || !strcmp("-foreground",argv[i])) {
150
if (++i>=argc) usage();
151
fore_color = argv[i];
154
if (!strcmp("-bg",argv[i]) || !strcmp("-background",argv[i])) {
155
if (++i>=argc) usage();
156
back_color = argv[i];
159
if (!strcmp("-solid", argv[i])) {
160
if (++i>=argc) usage();
161
solid_color = argv[i];
165
if (!strcmp("-gray", argv[i]) || !strcmp("-grey", argv[i])) {
170
if (!strcmp("-bitmap", argv[i])) {
171
if (++i>=argc) usage();
172
bitmap_file = argv[i];
176
if (!strcmp("-mod", argv[i])) {
177
if (++i>=argc) usage();
178
mod_x = atoi(argv[i]);
179
if (mod_x <= 0) mod_x = 1;
180
if (++i>=argc) usage();
181
mod_y = atoi(argv[i]);
182
if (mod_y <= 0) mod_y = 1;
186
if (!strcmp("-rv",argv[i]) || !strcmp("-reverse",argv[i])) {
193
/* Check for multiple use of exclusive options */
195
fprintf(stderr, "%s: choose only one of {solid, gray, bitmap, mod}\n",
200
dpy = XOpenDisplay(display_name);
202
fprintf(stderr, "%s: unable to open display '%s'\n",
203
program_name, XDisplayName (display_name));
206
screen = DefaultScreen(dpy);
207
root = RootWindow(dpy, screen);
209
/* If there are no arguments then restore defaults. */
210
if (!excl && !nonexcl)
211
restore_defaults = 1;
213
/* Handle a cursor file */
215
cursor = CreateCursorFromFiles(cursor_file, cursor_mask);
216
XDefineCursor(dpy, root, cursor);
217
XFreeCursor(dpy, cursor);
221
cursor = CreateCursorFromName (cursor_name);
224
XDefineCursor (dpy, root, cursor);
225
XFreeCursor (dpy, cursor);
228
/* Handle -gray and -grey options */
230
bitmap = XCreateBitmapFromData(dpy, root, gray_bits,
231
gray_width, gray_height);
232
SetBackgroundToBitmap(bitmap, gray_width, gray_height);
235
/* Handle -solid option */
237
XSetWindowBackground(dpy, root, NameToPixel(solid_color,
238
BlackPixel(dpy, screen)));
239
XClearWindow(dpy, root);
243
/* Handle -bitmap option */
245
bitmap = ReadBitmapFile(bitmap_file, &ww, &hh, (int *)NULL, (int *)NULL);
246
SetBackgroundToBitmap(bitmap, ww, hh);
249
/* Handle set background to a modula pattern */
251
bitmap = MakeModulaBitmap(mod_x, mod_y);
252
SetBackgroundToBitmap(bitmap, 16, 16);
255
/* Handle set name */
257
XStoreName(dpy, root, name);
259
/* Handle restore defaults */
260
if (restore_defaults) {
262
XUndefineCursor(dpy, root);
264
XSetWindowBackgroundPixmap(dpy, root, (Pixmap) None);
265
XClearWindow(dpy, root);
276
/* Free past incarnation if needed, and retain state if needed. */
282
unsigned long length, after;
285
if (!(DefaultVisual(dpy, screen)->class & Dynamic))
287
if (!unsave_past && !save_colors)
289
prop = XInternAtom(dpy, "_XSETROOT_ID", False);
291
(void)XGetWindowProperty(dpy, root, prop, 0L, 1L, True, AnyPropertyType,
292
&type, &format, &length, &after, &data);
293
if ((type == XA_PIXMAP) && (format == 32) &&
294
(length == 1) && (after == 0))
295
XKillClient(dpy, *((Pixmap *)data));
296
else if (type != None)
297
fprintf(stderr, "%s: warning: _XSETROOT_ID property is garbage\n",
302
save_pixmap = XCreatePixmap(dpy, root, 1, 1, 1);
303
XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, PropModeReplace,
304
(unsigned char *) &save_pixmap, 1);
305
XSetCloseDownMode(dpy, RetainPermanent);
310
* SetBackgroundToBitmap: Set the root window background to a caller supplied
314
SetBackgroundToBitmap(Pixmap bitmap, unsigned int width, unsigned int height)
320
gc_init.foreground = NameToPixel(fore_color, BlackPixel(dpy, screen));
321
gc_init.background = NameToPixel(back_color, WhitePixel(dpy, screen));
323
unsigned long temp=gc_init.foreground;
324
gc_init.foreground=gc_init.background;
325
gc_init.background=temp;
327
gc = XCreateGC(dpy, root, GCForeground|GCBackground, &gc_init);
328
pix = XCreatePixmap(dpy, root, width, height,
329
(unsigned int)DefaultDepth(dpy, screen));
330
XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, (unsigned long)1);
331
XSetWindowBackgroundPixmap(dpy, root, pix);
333
XFreePixmap(dpy, bitmap);
337
XFreePixmap(dpy, pix);
338
XClearWindow(dpy, root);
344
* CreateCursorFromFiles: make a cursor of the right colors from two bitmap
347
#define BITMAP_HOT_DEFAULT 8
350
CreateCursorFromFiles(char *cursor_file, char *mask_file)
352
Pixmap cursor_bitmap, mask_bitmap;
353
unsigned int width, height, ww, hh;
358
fg = NameToXColor(fore_color, BlackPixel(dpy, screen));
359
bg = NameToXColor(back_color, WhitePixel(dpy, screen));
361
temp = fg; fg = bg; bg = temp;
364
cursor_bitmap = ReadBitmapFile(cursor_file, &width, &height, &x_hot, &y_hot);
365
mask_bitmap = ReadBitmapFile(mask_file, &ww, &hh, (int *)NULL, (int *)NULL);
367
if (width != ww || height != hh) {
369
"%s: dimensions of cursor bitmap and cursor mask bitmap are different\n",
375
if ((x_hot == -1) && (y_hot == -1)) {
376
x_hot = BITMAP_HOT_DEFAULT;
377
y_hot = BITMAP_HOT_DEFAULT;
379
if ((x_hot < 0) || (x_hot >= width) ||
380
(y_hot < 0) || (y_hot >= height)) {
381
fprintf(stderr, "%s: hotspot is outside cursor bounds\n", program_name);
386
cursor = XCreatePixmapCursor(dpy, cursor_bitmap, mask_bitmap, &fg, &bg,
387
(unsigned int)x_hot, (unsigned int)y_hot);
388
XFreePixmap(dpy, cursor_bitmap);
389
XFreePixmap(dpy, mask_bitmap);
395
CreateCursorFromName(char *name)
401
fg = NameToXColor(fore_color, BlackPixel(dpy, screen));
402
bg = NameToXColor(back_color, WhitePixel(dpy, screen));
404
temp = fg; fg = bg; bg = temp;
406
i = XmuCursorNameToIndex (name);
409
fid = XLoadFont (dpy, "cursor");
412
return XCreateGlyphCursor (dpy, fid, fid,
417
* MakeModulaBitmap: Returns a modula bitmap based on an x & y mod.
420
MakeModulaBitmap(int mod_x, int mod_y)
423
long pattern_line = 0;
424
char modula_data[16*16/8];
428
if ((i % mod_x) == 0) pattern_line |= 0x0001;
430
for (i=0; i<16; i++) {
431
if ((i % mod_y) == 0) {
432
modula_data[i*2] = (char)0xff;
433
modula_data[i*2+1] = (char)0xff;
435
modula_data[i*2] = pattern_line & 0xff;
436
modula_data[i*2+1] = (pattern_line>>8) & 0xff;
440
return(XCreateBitmapFromData(dpy, root, modula_data, 16, 16));
445
* NameToXColor: Convert the name of a color to its Xcolor value.
448
NameToXColor(char *name, unsigned long pixel)
452
if (!name || !*name) {
454
XQueryColor(dpy, DefaultColormap(dpy, screen), &c);
455
} else if (!XParseColor(dpy, DefaultColormap(dpy, screen), name, &c)) {
456
fprintf(stderr, "%s: unknown color or bad color format: %s\n",
465
NameToPixel(char *name, unsigned long pixel)
471
if (!XParseColor(dpy,DefaultColormap(dpy,screen),name,&ecolor)) {
472
fprintf(stderr,"%s: unknown color \"%s\"\n",program_name,name);
476
if (!XAllocColor(dpy, DefaultColormap(dpy, screen),&ecolor)) {
477
fprintf(stderr, "%s: unable to allocate color for \"%s\"\n",
482
if ((ecolor.pixel != BlackPixel(dpy, screen)) &&
483
(ecolor.pixel != WhitePixel(dpy, screen)) &&
484
(DefaultVisual(dpy, screen)->class & Dynamic))
486
return(ecolor.pixel);
490
ReadBitmapFile(char *filename, unsigned int *width, unsigned int *height,
491
int *x_hot, int *y_hot)
496
status = XReadBitmapFile(dpy, root, filename, width,
497
height, &bitmap, x_hot, y_hot);
498
if (status == BitmapSuccess)
500
else if (status == BitmapOpenFailed)
501
fprintf(stderr, "%s: can't open file: %s\n", program_name, filename);
502
else if (status == BitmapFileInvalid)
503
fprintf(stderr, "%s: bad bitmap format file: %s\n",
504
program_name, filename);
506
fprintf(stderr, "%s: insufficient memory for bitmap: %s",
507
program_name, filename);