xsetroot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. *
  3. Copyright 1987, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. */
  21. /*
  22. * xsetroot.c MIT Project Athena, X Window System root window
  23. * parameter setting utility. This program will set
  24. * various parameters of the X root window.
  25. *
  26. * Author: Mark Lillibridge, MIT Project Athena
  27. * 11-Jun-87
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif
  32. #include <X11/Xlib.h>
  33. #include <X11/Xutil.h>
  34. #include <X11/Xatom.h>
  35. #include <X11/Xmu/CurUtil.h>
  36. #include <X11/Xcursor/Xcursor.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "X11/bitmaps/gray"
  41. #define Dynamic 1
  42. static char *program_name;
  43. static Display *dpy;
  44. static int screen;
  45. static Window root;
  46. static char *fore_color = NULL;
  47. static char *back_color = NULL;
  48. static int reverse = 0;
  49. static int save_colors = 0;
  50. static int unsave_past = 0;
  51. static Pixmap save_pixmap = (Pixmap)None;
  52. static void usage(void) _X_NORETURN;
  53. static void FixupState(void);
  54. static void SetBackgroundToBitmap(Pixmap bitmap,
  55. unsigned int width, unsigned int height);
  56. static Cursor CreateCursorFromFiles(char *cursor_file, char *mask_file);
  57. static Cursor CreateCursorFromName(char *name);
  58. static Pixmap MakeModulaBitmap(int mod_x, int mod_y);
  59. static XColor NameToXColor(char *name, unsigned long pixel);
  60. static unsigned long NameToPixel(char *name, unsigned long pixel);
  61. static Pixmap ReadBitmapFile(char *filename, unsigned int *width, unsigned int *height, int *x_hot, int *y_hot);
  62. static void
  63. usage(void)
  64. {
  65. fprintf(stderr, "usage: %s [options]\n%s\n", program_name,
  66. " where options are:\n"
  67. " -display <display> or -d <display>\n"
  68. " -fg <color> or -foreground <color>\n"
  69. " -bg <color> or -background <color>\n"
  70. " -rv or -reverse\n"
  71. " -def or -default\n"
  72. " -name <string>\n"
  73. " -cursor <cursor file> <mask file>\n"
  74. " -cursor_name <cursor-font name>\n"
  75. " -xcf <ARGB cursor file> <cursor size>\n"
  76. " -solid <color>\n"
  77. " -gray or -grey\n"
  78. " -bitmap <filename>\n"
  79. " -mod <x> <y>\n"
  80. " -help\n"
  81. " -version\n"
  82. );
  83. exit(1);
  84. /*NOTREACHED*/
  85. }
  86. int
  87. main(int argc, char *argv[])
  88. {
  89. int excl = 0;
  90. int nonexcl = 0;
  91. int restore_defaults = 0;
  92. char *display_name = NULL;
  93. char *name = NULL;
  94. char *cursor_file = NULL;
  95. char *cursor_mask = NULL;
  96. char *cursor_name = NULL;
  97. char *solid_color = NULL;
  98. char *xcf = NULL;
  99. int xcf_size = 32;
  100. Cursor cursor;
  101. int gray = 0;
  102. char *bitmap_file = NULL;
  103. int mod_x = 0;
  104. int mod_y = 0;
  105. register int i;
  106. unsigned int ww, hh;
  107. Pixmap bitmap;
  108. program_name=argv[0];
  109. for (i = 1; i < argc; i++) {
  110. if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
  111. if (++i>=argc) usage ();
  112. display_name = argv[i];
  113. continue;
  114. }
  115. if (!strcmp("-help", argv[i])) {
  116. usage();
  117. }
  118. if (!strcmp("-version", argv[i])) {
  119. printf("%s\n", PACKAGE_STRING);
  120. exit(0);
  121. }
  122. if (!strcmp("-def", argv[i]) || !strcmp("-default", argv[i])) {
  123. restore_defaults = 1;
  124. continue;
  125. }
  126. if (!strcmp("-name", argv[i])) {
  127. if (++i>=argc) usage();
  128. name = argv[i];
  129. nonexcl++;
  130. continue;
  131. }
  132. if (!strcmp("-cursor", argv[i])) {
  133. if (++i>=argc) usage();
  134. cursor_file = argv[i];
  135. if (++i>=argc) usage();
  136. cursor_mask = argv[i];
  137. nonexcl++;
  138. continue;
  139. }
  140. if (!strcmp("-cursor_name", argv[i])) {
  141. if (++i>=argc) usage();
  142. cursor_name = argv[i];
  143. nonexcl++;
  144. continue;
  145. }
  146. if (!strcmp("-xcf", argv[i])) {
  147. if (++i>=argc) usage();
  148. xcf = argv[i];
  149. if (++i>=argc) usage();
  150. xcf_size = atoi(argv[i]);
  151. if (xcf_size <= 0)
  152. xcf_size = 32;
  153. nonexcl++;
  154. continue;
  155. }
  156. if (!strcmp("-fg",argv[i]) || !strcmp("-foreground",argv[i])) {
  157. if (++i>=argc) usage();
  158. fore_color = argv[i];
  159. continue;
  160. }
  161. if (!strcmp("-bg",argv[i]) || !strcmp("-background",argv[i])) {
  162. if (++i>=argc) usage();
  163. back_color = argv[i];
  164. continue;
  165. }
  166. if (!strcmp("-solid", argv[i])) {
  167. if (++i>=argc) usage();
  168. solid_color = argv[i];
  169. excl++;
  170. continue;
  171. }
  172. if (!strcmp("-gray", argv[i]) || !strcmp("-grey", argv[i])) {
  173. gray = 1;
  174. excl++;
  175. continue;
  176. }
  177. if (!strcmp("-bitmap", argv[i])) {
  178. if (++i>=argc) usage();
  179. bitmap_file = argv[i];
  180. excl++;
  181. continue;
  182. }
  183. if (!strcmp("-mod", argv[i])) {
  184. if (++i>=argc) usage();
  185. mod_x = atoi(argv[i]);
  186. if (mod_x <= 0) mod_x = 1;
  187. if (++i>=argc) usage();
  188. mod_y = atoi(argv[i]);
  189. if (mod_y <= 0) mod_y = 1;
  190. excl++;
  191. continue;
  192. }
  193. if (!strcmp("-rv",argv[i]) || !strcmp("-reverse",argv[i])) {
  194. reverse = 1;
  195. continue;
  196. }
  197. usage();
  198. }
  199. /* Check for multiple use of exclusive options */
  200. if (excl > 1) {
  201. fprintf(stderr, "%s: choose only one of {solid, gray, bitmap, mod}\n",
  202. program_name);
  203. usage();
  204. }
  205. dpy = XOpenDisplay(display_name);
  206. if (!dpy) {
  207. fprintf(stderr, "%s: unable to open display '%s'\n",
  208. program_name, XDisplayName (display_name));
  209. exit (2);
  210. }
  211. screen = DefaultScreen(dpy);
  212. root = RootWindow(dpy, screen);
  213. /* If there are no arguments then restore defaults. */
  214. if (!excl && !nonexcl)
  215. restore_defaults = 1;
  216. /* Handle a cursor file */
  217. if (cursor_file) {
  218. cursor = CreateCursorFromFiles(cursor_file, cursor_mask);
  219. XDefineCursor(dpy, root, cursor);
  220. XFreeCursor(dpy, cursor);
  221. }
  222. if (cursor_name) {
  223. cursor = CreateCursorFromName (cursor_name);
  224. if (cursor)
  225. {
  226. XDefineCursor (dpy, root, cursor);
  227. XFreeCursor (dpy, cursor);
  228. }
  229. }
  230. if (xcf) {
  231. XcursorImages *images = XcursorFilenameLoadImages(xcf, xcf_size);
  232. if (!images) {
  233. fprintf(stderr, "Invalid cursor file \"%s\"\n", xcf);
  234. } else {
  235. cursor = XcursorImagesLoadCursor(dpy, images);
  236. if (cursor)
  237. {
  238. XDefineCursor (dpy, root, cursor);
  239. XFreeCursor (dpy, cursor);
  240. }
  241. }
  242. }
  243. /* Handle -gray and -grey options */
  244. if (gray) {
  245. bitmap = XCreateBitmapFromData(dpy, root, gray_bits,
  246. gray_width, gray_height);
  247. SetBackgroundToBitmap(bitmap, gray_width, gray_height);
  248. }
  249. /* Handle -solid option */
  250. if (solid_color) {
  251. XSetWindowBackground(dpy, root, NameToPixel(solid_color,
  252. BlackPixel(dpy, screen)));
  253. XClearWindow(dpy, root);
  254. unsave_past = 1;
  255. }
  256. /* Handle -bitmap option */
  257. if (bitmap_file) {
  258. bitmap = ReadBitmapFile(bitmap_file, &ww, &hh, (int *)NULL, (int *)NULL);
  259. SetBackgroundToBitmap(bitmap, ww, hh);
  260. }
  261. /* Handle set background to a modula pattern */
  262. if (mod_x) {
  263. bitmap = MakeModulaBitmap(mod_x, mod_y);
  264. SetBackgroundToBitmap(bitmap, 16, 16);
  265. }
  266. /* Handle set name */
  267. if (name)
  268. XStoreName(dpy, root, name);
  269. /* Handle restore defaults */
  270. if (restore_defaults) {
  271. if (!cursor_file)
  272. XUndefineCursor(dpy, root);
  273. if (!excl) {
  274. XSetWindowBackgroundPixmap(dpy, root, (Pixmap) None);
  275. XClearWindow(dpy, root);
  276. unsave_past = 1;
  277. }
  278. }
  279. FixupState();
  280. XCloseDisplay(dpy);
  281. exit (0);
  282. }
  283. /* Free past incarnation if needed, and retain state if needed. */
  284. static void
  285. FixupState(void)
  286. {
  287. Atom prop, type;
  288. int format;
  289. unsigned long length, after;
  290. unsigned char *data;
  291. if (!(DefaultVisual(dpy, screen)->class & Dynamic))
  292. unsave_past = 0;
  293. if (!unsave_past && !save_colors)
  294. return;
  295. prop = XInternAtom(dpy, "_XSETROOT_ID", False);
  296. if (unsave_past) {
  297. (void)XGetWindowProperty(dpy, root, prop, 0L, 1L, True, AnyPropertyType,
  298. &type, &format, &length, &after, &data);
  299. if ((type == XA_PIXMAP) && (format == 32) &&
  300. (length == 1) && (after == 0))
  301. XKillClient(dpy, *((Pixmap *)data));
  302. else if (type != None)
  303. fprintf(stderr, "%s: warning: _XSETROOT_ID property is garbage\n",
  304. program_name);
  305. }
  306. if (save_colors) {
  307. if (!save_pixmap)
  308. save_pixmap = XCreatePixmap(dpy, root, 1, 1, 1);
  309. XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, PropModeReplace,
  310. (unsigned char *) &save_pixmap, 1);
  311. XSetCloseDownMode(dpy, RetainPermanent);
  312. }
  313. }
  314. /*
  315. * SetBackgroundToBitmap: Set the root window background to a caller supplied
  316. * bitmap.
  317. */
  318. static void
  319. SetBackgroundToBitmap(Pixmap bitmap, unsigned int width, unsigned int height)
  320. {
  321. Pixmap pix;
  322. GC gc;
  323. XGCValues gc_init;
  324. gc_init.foreground = NameToPixel(fore_color, BlackPixel(dpy, screen));
  325. gc_init.background = NameToPixel(back_color, WhitePixel(dpy, screen));
  326. if (reverse) {
  327. unsigned long temp=gc_init.foreground;
  328. gc_init.foreground=gc_init.background;
  329. gc_init.background=temp;
  330. }
  331. gc = XCreateGC(dpy, root, GCForeground|GCBackground, &gc_init);
  332. pix = XCreatePixmap(dpy, root, width, height,
  333. (unsigned int)DefaultDepth(dpy, screen));
  334. XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, (unsigned long)1);
  335. XSetWindowBackgroundPixmap(dpy, root, pix);
  336. XFreeGC(dpy, gc);
  337. XFreePixmap(dpy, bitmap);
  338. if (save_colors)
  339. save_pixmap = pix;
  340. else
  341. XFreePixmap(dpy, pix);
  342. XClearWindow(dpy, root);
  343. unsave_past = 1;
  344. }
  345. /*
  346. * CreateCursorFromFiles: make a cursor of the right colors from two bitmap
  347. * files.
  348. */
  349. #define BITMAP_HOT_DEFAULT 8
  350. static Cursor
  351. CreateCursorFromFiles(char *cursor_file, char *mask_file)
  352. {
  353. Pixmap cursor_bitmap, mask_bitmap;
  354. unsigned int width, height, ww, hh;
  355. int x_hot, y_hot;
  356. Cursor cursor;
  357. XColor fg, bg, temp;
  358. fg = NameToXColor(fore_color, BlackPixel(dpy, screen));
  359. bg = NameToXColor(back_color, WhitePixel(dpy, screen));
  360. if (reverse) {
  361. temp = fg; fg = bg; bg = temp;
  362. }
  363. cursor_bitmap = ReadBitmapFile(cursor_file, &width, &height, &x_hot, &y_hot);
  364. mask_bitmap = ReadBitmapFile(mask_file, &ww, &hh, (int *)NULL, (int *)NULL);
  365. if (width != ww || height != hh) {
  366. fprintf(stderr,
  367. "%s: dimensions of cursor bitmap and cursor mask bitmap are different\n",
  368. program_name);
  369. exit(1);
  370. /*NOTREACHED*/
  371. }
  372. if ((x_hot == -1) && (y_hot == -1)) {
  373. x_hot = BITMAP_HOT_DEFAULT;
  374. y_hot = BITMAP_HOT_DEFAULT;
  375. }
  376. if ((x_hot < 0) || (x_hot >= width) ||
  377. (y_hot < 0) || (y_hot >= height)) {
  378. fprintf(stderr, "%s: hotspot is outside cursor bounds\n", program_name);
  379. exit(1);
  380. /*NOTREACHED*/
  381. }
  382. cursor = XCreatePixmapCursor(dpy, cursor_bitmap, mask_bitmap, &fg, &bg,
  383. (unsigned int)x_hot, (unsigned int)y_hot);
  384. XFreePixmap(dpy, cursor_bitmap);
  385. XFreePixmap(dpy, mask_bitmap);
  386. return(cursor);
  387. }
  388. static Cursor
  389. CreateCursorFromName(char *name)
  390. {
  391. XColor fg, bg, temp;
  392. int i;
  393. Font fid;
  394. fg = NameToXColor(fore_color, BlackPixel(dpy, screen));
  395. bg = NameToXColor(back_color, WhitePixel(dpy, screen));
  396. if (reverse) {
  397. temp = fg; fg = bg; bg = temp;
  398. }
  399. i = XmuCursorNameToIndex (name);
  400. if (i == -1)
  401. return (Cursor) 0;
  402. fid = XLoadFont (dpy, "cursor");
  403. if (!fid)
  404. return (Cursor) 0;
  405. return XCreateGlyphCursor (dpy, fid, fid,
  406. i, i+1, &fg, &bg);
  407. }
  408. /*
  409. * MakeModulaBitmap: Returns a modula bitmap based on an x & y mod.
  410. */
  411. static Pixmap
  412. MakeModulaBitmap(int mod_x, int mod_y)
  413. {
  414. int i;
  415. long pattern_line = 0;
  416. char modula_data[16*16/8];
  417. for (i=16; i--; ) {
  418. pattern_line <<=1;
  419. if ((i % mod_x) == 0) pattern_line |= 0x0001;
  420. }
  421. for (i=0; i<16; i++) {
  422. if ((i % mod_y) == 0) {
  423. modula_data[i*2] = (char)0xff;
  424. modula_data[i*2+1] = (char)0xff;
  425. } else {
  426. modula_data[i*2] = pattern_line & 0xff;
  427. modula_data[i*2+1] = (pattern_line>>8) & 0xff;
  428. }
  429. }
  430. return(XCreateBitmapFromData(dpy, root, modula_data, 16, 16));
  431. }
  432. /*
  433. * NameToXColor: Convert the name of a color to its Xcolor value.
  434. */
  435. static XColor
  436. NameToXColor(char *name, unsigned long pixel)
  437. {
  438. XColor c;
  439. if (!name || !*name) {
  440. c.pixel = pixel;
  441. XQueryColor(dpy, DefaultColormap(dpy, screen), &c);
  442. } else if (!XParseColor(dpy, DefaultColormap(dpy, screen), name, &c)) {
  443. fprintf(stderr, "%s: unknown color or bad color format: %s\n",
  444. program_name, name);
  445. exit(1);
  446. /*NOTREACHED*/
  447. }
  448. return(c);
  449. }
  450. static unsigned long
  451. NameToPixel(char *name, unsigned long pixel)
  452. {
  453. XColor ecolor;
  454. if (!name || !*name)
  455. return pixel;
  456. if (!XParseColor(dpy,DefaultColormap(dpy,screen),name,&ecolor)) {
  457. fprintf(stderr,"%s: unknown color \"%s\"\n",program_name,name);
  458. exit(1);
  459. /*NOTREACHED*/
  460. }
  461. if (!XAllocColor(dpy, DefaultColormap(dpy, screen),&ecolor)) {
  462. fprintf(stderr, "%s: unable to allocate color for \"%s\"\n",
  463. program_name, name);
  464. exit(1);
  465. /*NOTREACHED*/
  466. }
  467. if ((ecolor.pixel != BlackPixel(dpy, screen)) &&
  468. (ecolor.pixel != WhitePixel(dpy, screen)) &&
  469. (DefaultVisual(dpy, screen)->class & Dynamic))
  470. save_colors = 1;
  471. return(ecolor.pixel);
  472. }
  473. static Pixmap
  474. ReadBitmapFile(char *filename, unsigned int *width, unsigned int *height,
  475. int *x_hot, int *y_hot)
  476. {
  477. Pixmap bitmap;
  478. int status;
  479. status = XReadBitmapFile(dpy, root, filename, width,
  480. height, &bitmap, x_hot, y_hot);
  481. if (status == BitmapSuccess)
  482. return(bitmap);
  483. else if (status == BitmapOpenFailed)
  484. fprintf(stderr, "%s: can't open file: %s\n", program_name, filename);
  485. else if (status == BitmapFileInvalid)
  486. fprintf(stderr, "%s: bad bitmap format file: %s\n",
  487. program_name, filename);
  488. else
  489. fprintf(stderr, "%s: insufficient memory for bitmap: %s",
  490. program_name, filename);
  491. exit(1);
  492. /*NOTREACHED*/
  493. }