summaryrefslogtreecommitdiff
path: root/xc/programs/xsetroot/xsetroot.c
blob: bfb7b6fcae272b73ad56e39e91522147c64211a6 (plain)
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
/* Copyright 1987, Massachusetts Institute of Technology */

#include <X11/Xlib.h>
#include <stdio.h>

/*
 * xsetroot.c 	MIT Project Athena, X Window system root window 
 *		parameter setting utility.  This program will set 
 *		various parameters of the X root window.
 *
 *  Author:	Mark Lillibridge, MIT Project Athena
 *		11-Jun-87
 */

/* Include routines to handle parsing defaults */
#include "jowindow.h"
#include "ostuff.h"

#include "bitmaps/gray"
#include "bitmaps/root_weave"

usage()
{
	fprintf(stderr, "%s: usage: %s [-fg <color>] [-bg <color>] [-rv] [-help] [-def] [-name <string>] [-cursor <cursor file> <mask file>] [-solid <color>] [-gray] [-grey] [-bitmap <filename>] [-mod <x> <y>] [<host>:<display>]\n\nNOTE: *** Use only one of -solid, -gray, -grey, -bitmap, and -mod ***\n\n", program_name, program_name);
	exit(1);
}

extern Pixmap MakeModulaBitmap();

main(argc, argv) 
     int argc;
     char **argv;
{
  int excl = 0;
  int restore_defaults = 0;
  char *name = 0;
  char *cursor_file = 0;
  char *cursor_mask = 0;
  Cursor cursor;
  char *solid_color = 0;
  int gray = 0;
  char *bitmap_file = 0;
  int mod_x = -1;
  int mod_y = -1;
  register int i;
  Pixmap bitmap;
  int ww, hh;

  INIT_NAME;

  /* Handle command line arguments, open the display */
  Get_X_Options(&argc, argv);

  for (i = 1; i < argc; i++) {
    if (!strcmp("-", argv[i]))
      continue;
    if (!strcmp("-help", argv[i]))
      usage();
    if (!strcmp("-def", argv[i])) {
      restore_defaults = 1;
      continue;
    }
    if (!strcmp("-name", argv[i])) {
      if (++i>=argc) usage();
      name = argv[i];
      continue;
    }
    if (!strcmp("-cursor", argv[i])) {
      if (i++>=argc) usage();
      cursor_file = argv[i];
      if (i++>=argc) usage();
      cursor_mask = argv[i];
      continue;
    }
    if (!strcmp("-solid", argv[i])) {
      if (i++>=argc) usage();
      solid_color = argv[i];
      excl++;
      continue;
    }
    if (!strcmp("-gray", argv[i]) || !strcmp("-grey", argv[i])) {
      gray = 1;
      excl++;
      continue;
    }
    if (!strcmp("-bitmap", argv[i])) {
      if (++i>=argc) usage();
      bitmap_file = argv[i];
      excl++;
      continue;
    }
    if (!strcmp("-mod", argv[i])) {
      if (++i>=argc) usage();
      outl("setting mod_x!");
      mod_x = atoi(argv[i]);
      if (++i>=argc) usage();
      mod_y = atoi(argv[i]);
      excl++;
      continue;
    }
    usage();
  } 

  /* Check for multiple use of exclusive options */
  if (excl > 1)
    usage();

  /* If there are no arguments then restore defaults. */
  if (argc == 1 || (argc == 2 && !strcmp(argv[1],"-")))
    restore_defaults = 1;

  /* Resolve the X options */
  Resolve_X_Options();

  /* Handle a cursor file */
  if (cursor_file) {
	  cursor = CreateCursorFromFiles(cursor_file, cursor_mask, fore_color,
					 back_color);

	  XDefineCursor(dpy, RootWindow(dpy, screen), cursor);

	  XFreeCursor(dpy, cursor);
  }

  /* Handle -gray and -grey options */
  if (gray)
    SetBackgroundToData(gray_bits, gray_width, gray_height);

  /* Handle -solid option */
  if (solid_color)
    XSetWindowBackground(dpy, RootWindow(dpy, screen),
			 Resolve_Color(RootWindow(dpy, screen),
				       solid_color));

  /* Handle -bitmap option */
  if (bitmap_file) {
	  bitmap = ReadBitmapFile(RootWindow(dpy, screen), bitmap_file,
				  &ww, &hh, 0, 0);
	  SetBackgroundToBitmap(bitmap);
  }

  /* Handle set background to a modula pattern */
  if (mod_x != -1)
    SetBackgroundToBitmap(MakeModulaBitmap(mod_x, mod_y));

  /* Handle set name */
  if (name)
    XStoreName(dpy, RootWindow(dpy, screen), name);

  /* Handle restore defaults */
  if (restore_defaults) {
    if (!cursor_file) {
      printf("Reseting cursor not done because it crashes the server...\n");
/*      XUndefineCursor(dpy, RootWindow(dpy, screen)); */
    }
    if (!excl) {
      /* because of Xlib bug.... */
	    SetBackgroundToData(root_weave_bits, root_weave_width,
				root_weave_height);
/*      XSetBackgroundPixmap(dpy, RootWindow(dpy, screen), (Pixmap) 0);*/
    }
  }

  /* Clear the root window, flush all output and exit. */
  XClearWindow(dpy, RootWindow(dpy, screen));
  XFlush(dpy);
}


/*
 * SetBackgroundToBitmap: Set the root window background to a caller supplied 
 *                        bitmap.
 */
SetBackgroundToBitmap(bitmap)
Pixmap bitmap;
{
	Pixmap pix;
	GC gc;
	XGCValues gc_init;
	long temp;

	gc_init.foreground = Resolve_Color(RootWindow(dpy,screen),fore_color);
	gc_init.background = Resolve_Color(RootWindow(dpy,screen),back_color);
	if (reverse) {
		temp=gc_init.foreground;
		gc_init.foreground=gc_init.background;
		gc_init.background=temp;
	}
	gc = XCreateGC(dpy, RootWindow(dpy, screen),GCForeground|GCBackground,
		       &gc_init);

	pix = Bitmap_To_Pixmap(dpy, RootWindow(dpy, screen), gc, bitmap);

	XSetWindowBackgroundPixmap(dpy, RootWindow(dpy, screen), pix);
}


/*
 * SetBackgroundToData: As SetBackgroundToBitmap but uses the data for
 *                      a bitmap instead of an actual bitmap.  Data format
 *                      is that used by XCreateBitmapFromData.
 */
SetBackgroundToData(data, width, height)
char *data;
int width, height;
{
	Pixmap bitmap;

	bitmap = XCreateBitmapFromData(dpy, RootWindow(dpy, screen),
				       data, width, height);

	SetBackgroundToBitmap(bitmap);
}


/*
 * CreateCursorFromFiles: make a cursor of the right colors from two bitmap
 *                        files.
 */
#define BITMAP_HOT_DEFAULT 8

CreateCursorFromFiles(cursor_file, mask_file, fore_color, back_color)
char *cursor_file, *mask_file;
char *fore_color, *back_color;
{
	Pixmap cursor_bitmap, mask_bitmap;
	int width, height, ww, hh, x_hot, y_hot;
	Cursor cursor;
	XColor fg, bg, temp, NameToXColor();

	fg = NameToXColor(fore_color);
	bg = NameToXColor(back_color);
	if (reverse) {
		temp = fg; fg = bg; bg = temp;
	}

	cursor_bitmap = ReadBitmapFile(RootWindow(dpy, screen),
				       cursor_file, &width, &height,
				       &x_hot, &y_hot);
	mask_bitmap = ReadBitmapFile(RootWindow(dpy, screen), mask_file,
				     &ww, &hh, 0, 0);

	if (width != ww || height != hh)
	  Fatal_Error("Dimensions of cursor bitmap and cursor mask bitmap are different!");

	if (x_hot == -1) {
		x_hot = BITMAP_HOT_DEFAULT;
		y_hot = BITMAP_HOT_DEFAULT;
	}

	cursor = XCreatePixmapCursor(dpy, cursor_bitmap, mask_bitmap, &fg, &bg,
				     x_hot, y_hot);

	return(cursor);
}


/*
 * MakeModulaBitmap: Returns a modula bitmap based on an x & y mod.
 */
Pixmap MakeModulaBitmap(mod_x, mod_y)
int mod_x, mod_y;
{
	int i;
	long pattern_line = 0;
	char modula_data[16*16/8];

	if (mod_x<1)
	  mod_x=1;
	if (mod_y<1)
	  mod_y=1;

	for (i=0; i<16; i++) {
		pattern_line <<=1;
		if (i % mod_x == 0) pattern_line |= 0x0001;
	}
	for (i=0; i<16; i++) {
		if (i % mod_y) {
			modula_data[i*2] = 0xff;
			modula_data[i*2+1] = 0xff;
		} else {
			modula_data[i*2] = pattern_line & 0xff;
			modula_data[i*2+1] = (pattern_line>>8) & 0xff;
		}
	}


	return(XCreateBitmapFromData(dpy, RootWindow(dpy, screen), modula_data,
				     16, 16));
}


/*
 * NameToXColor: Convert the name of a color to its Xcolor value.
 */
XColor NameToXColor(name)
char *name;
{
	XColor c;
	Colormap cmap;
	
	cmap = DefaultColormap(dpy, screen);

	if (!XParseColor(dpy, cmap, name, &c))
	  Fatal_Error("unknown color or bad color format: %s", name);

	return(c);
}


/*
 * Tempary replace for not implemented Xlib routine.
 */
char *XGetDefault(dpy, name, option_name)
Display *dpy;
char *name;
char *option_name;
{
	return(NULL);
}