1: #include <X/mit-copyright.h> 2: 3: /* Copyright 1985 Massachusetts Institute of Technology */ 4: 5: /* 6: * xsetroot.c MIT Project Athena, X Window system root window 7: * parameter setting utility. This program will set 8: * various parameters of the X root window. 9: * 10: * Author: Tony Della Fera, DEC 11: * 28-Nov-84 12: */ 13: #ifndef lint 14: static char *rcsid_xsetroot_c = "$Header: xsetroot.c,v 10.8 86/02/01 16:16:52 tony Rel $"; 15: #endif 16: 17: #include <X/Xlib.h> 18: #include <strings.h> 19: #define NULL 0 20: 21: #define MAX(a, b) (a) > (b) ? (a) : (b) 22: #define MIN(a, b) (a) < (b) ? (a) : (b) 23: #define ABS(a) (a) < 0 ? -(a) : (a) 24: 25: #define DEF_MSE_COLOR BlackPixel 26: #define DEF_SOLID_COLOR WhitePixel 27: #define DEF_FG_COLOR BlackPixel 28: #define DEF_BG_COLOR WhitePixel 29: #define DEF_ROOT_NAME " X Root Window " 30: 31: #define BITMAP_SIZE 16 32: #define BITMAP_HOT 8 33: 34: #define FAILURE 0 35: 36: typedef enum bool {FALSE, TRUE} bool; 37: 38: static unsigned short solid_line = 0xffff; 39: 40: static short gray_bits[BITMAP_SIZE] = { 41: 0xaaaa, 0x5555, 0xaaaa, 0x5555, 42: 0xaaaa, 0x5555, 0xaaaa, 0x5555, 43: 0xaaaa, 0x5555, 0xaaaa, 0x5555, 44: 0xaaaa, 0x5555, 0xaaaa, 0x5555 45: }; 46: 47: main(argc, argv) 48: int argc; 49: char **argv; 50: { 51: register int i; 52: register int x; 53: register int y; 54: register unsigned short pattern_line = 0; 55: int mse = DEF_MSE_COLOR; 56: int solid = DEF_SOLID_COLOR; 57: int fg = DEF_FG_COLOR; 58: int bg = DEF_BG_COLOR; 59: int width; 60: int height; 61: int x_hot; 62: int y_hot; 63: int xmod; 64: int ymod; 65: char *def_val; 66: char *strind; 67: char *root_name; 68: char *cursor_fname; 69: char *mask_fname; 70: char *bitmap_fname; 71: char *mse_name = NULL; 72: char *solid_name = NULL; 73: char *fg_name = NULL; 74: char *bg_name = NULL; 75: char display[40]; 76: bool def_sw = FALSE; 77: bool name_sw = FALSE; 78: bool cursor_sw = FALSE; 79: bool solid_sw = FALSE; 80: bool invert_sw = FALSE; 81: bool gray_sw = FALSE; 82: bool bitmap_sw = FALSE; 83: bool mod_sw = FALSE; 84: 85: short *cursor_bits; 86: short *mask_bits; 87: short *bkgnd_bits; 88: 89: Color color_def; 90: Cursor cursor; 91: Bitmap bkgnd_bitmap; 92: Pixmap bkgnd_pixmap; 93: 94: display[0] = '\0'; 95: 96: /* 97: * Get X defaults. 98: */ 99: def_val = XGetDefault(argv[0], "Foreground"); 100: fg_name = def_val; 101: 102: def_val = XGetDefault(argv[0], "Background"); 103: bg_name = def_val; 104: 105: def_val = XGetDefault(argv[0], "Mouse"); 106: mse_name = def_val; 107: 108: /* 109: * Parse argument list. 110: */ 111: for (i = 1; i < argc; i++) { 112: strind = index(argv[i], ':'); 113: if(strind != NULL) { 114: (void) strncpy(display, argv[i], sizeof(display)); 115: if (argc == 2) def_sw = TRUE; 116: continue; 117: } 118: strind = index (argv[i], '-'); 119: if (strind == NULL) Syntax(argv[0]); 120: if (strncmp(argv[i], "-help", 5) == 0) { 121: Syntax(argv[0]); 122: } 123: if (strncmp(argv[i], "-def", 4) == 0) { 124: def_sw = TRUE; 125: continue; 126: } 127: if (strncmp(argv[i], "-fg", 3) == 0) { 128: if (++i >= argc) Syntax(argv[0]); 129: fg_name = argv[i]; 130: continue; 131: } 132: if (strncmp(argv[i], "-bg", 3) == 0) { 133: if (++i >= argc) Syntax(argv[0]); 134: bg_name = argv[i]; 135: continue; 136: } 137: if (strncmp(argv[i], "-invert", 7) == 0) { 138: invert_sw = TRUE; 139: continue; 140: } 141: if (strncmp(argv[i], "-name", 5) == 0) { 142: if (++i >= argc) Syntax(argv[0]); 143: root_name = argv[i]; 144: name_sw = TRUE; 145: continue; 146: } 147: if (strncmp(argv[i], "-cursor", 7) == 0) { 148: if (++i >= argc) Syntax(argv[0]); 149: cursor_fname = argv[i]; 150: if (++i >= argc) Syntax(argv[0]); 151: mask_fname = argv[i]; 152: cursor_sw = TRUE; 153: continue; 154: } 155: if (strncmp(argv[i], "-solid", 6) == 0) { 156: if ( 157: gray_sw == TRUE || 158: bitmap_sw == TRUE || 159: mod_sw == TRUE 160: ) Syntax(argv[0]); 161: if (++i >= argc) Syntax(argv[0]); 162: solid_name = argv[i]; 163: solid_sw = TRUE; 164: continue; 165: } 166: if ( 167: (strncmp(argv[i], "-gray", 5) == 0) || 168: (strncmp(argv[i], "-grey", 5) == 0) 169: ){ 170: if ( 171: solid_sw == TRUE || 172: bitmap_sw == TRUE || 173: mod_sw == TRUE 174: ) Syntax(argv[0]); 175: gray_sw = TRUE; 176: continue; 177: } 178: if (strncmp(argv[i], "-bitmap", 7) == 0) { 179: if ( 180: gray_sw == TRUE || 181: solid_sw == TRUE || 182: mod_sw == TRUE 183: ) Syntax(argv[0]); 184: if (++i >= argc) Syntax(argv[0]); 185: bitmap_fname = argv[i]; 186: bitmap_sw = TRUE; 187: continue; 188: } 189: if (strncmp(argv[i], "-mod", 5) == 0) { 190: if ( 191: gray_sw == TRUE || 192: bitmap_sw == TRUE || 193: solid_sw == TRUE 194: ) Syntax(argv[0]); 195: if (++i >= argc) Syntax(argv[0]); 196: xmod = atoi(argv[i]); 197: if (++i >= argc) Syntax(argv[0]); 198: ymod = atoi(argv[i]); 199: mod_sw = TRUE; 200: continue; 201: } 202: Syntax(argv[0]); 203: } 204: 205: /* 206: * If there are no arguments then restore defaults. 207: */ 208: if (argc == 1) def_sw = TRUE; 209: 210: /* 211: * Open the display. 212: */ 213: if (XOpenDisplay(display) == NULL) Error("Unable to open display"); 214: 215: /* 216: * Parse color definintions. 217: */ 218: if ((DisplayCells() > 2) && (solid_name != NULL)) { 219: if ( 220: XParseColor(solid_name, &color_def) && 221: XGetHardwareColor(&color_def) 222: ) solid = color_def.pixel; 223: } 224: else if (solid_name && strcmp(solid_name, "black") == 0) solid = BlackPixel; 225: else if (solid_name && strcmp(solid_name, "white") == 0) solid = WhitePixel; 226: 227: if ((DisplayCells() > 2) && (fg_name != NULL)) { 228: if ( 229: XParseColor(fg_name, &color_def) && 230: XGetHardwareColor(&color_def) 231: ) fg = color_def.pixel; 232: } 233: else if (solid_name && strcmp(fg_name, "black") == 0) fg = BlackPixel; 234: else if (solid_name && strcmp(fg_name, "white") == 0) fg = WhitePixel; 235: 236: if ((DisplayCells() > 2) && (bg_name != NULL)) { 237: if ( 238: XParseColor(bg_name, &color_def) && 239: XGetHardwareColor(&color_def) 240: ) bg = color_def.pixel; 241: } 242: else if (bg_name && strcmp(bg_name, "black") == 0) bg = BlackPixel; 243: else if (bg_name && strcmp(bg_name, "white") == 0) bg = WhitePixel; 244: 245: if ((DisplayCells() > 2) && (mse_name != NULL)) { 246: if ( 247: XParseColor(mse_name, &color_def) && 248: XGetHardwareColor(&color_def) 249: ) mse = color_def.pixel; 250: } 251: else if (mse_name && strcmp(mse_name, "black") == 0) mse = BlackPixel; 252: else if (mse_name && strcmp(mse_name, "white") == 0) mse = WhitePixel; 253: 254: /* 255: * Set the root window name if a new root name supplied. 256: */ 257: if (name_sw == TRUE) XStoreName(RootWindow, root_name); 258: 259: /* 260: * Set cursor if a cursor is supplied. 261: */ 262: if (cursor_sw == TRUE) { 263: int status; 264: /* 265: * Open and read the mask file. 266: */ 267: status = XReadBitmapFile( 268: mask_fname, 269: &width, &height, &mask_bits, 270: NULL, NULL 271: ); 272: if (status == 0) Error ("Unable to open mask file"); 273: else if (status < 0) Error ("Unable to parse mask file"); 274: else if ((width != BITMAP_SIZE) || (height != BITMAP_SIZE)) 275: Error("Invaild mask Bitmap size"); 276: /* 277: * Open and read the cursor file. 278: */ 279: status = XReadBitmapFile( 280: cursor_fname, 281: &width, &height, &cursor_bits, 282: &x_hot, &y_hot 283: ); 284: if (status == 0) Error ("Unable to open cursor file"); 285: else if (status < 0) Error("Unable to parse cursor file"); 286: else if ((width != BITMAP_SIZE) || (height != BITMAP_SIZE)) 287: Error("Invaild cursor Bitmap size"); 288: 289: 290: /* 291: * If there is no hot spot defined or if the one defined is 292: * invalid, place the hot spot at BITMAP_HOT. 293: */ 294: if ( 295: (x_hot >= BITMAP_SIZE) || (x_hot < 0) 296: ){ 297: x_hot = BITMAP_HOT; 298: } 299: if ( 300: (y_hot >= BITMAP_SIZE) || (y_hot < 0) 301: ){ 302: y_hot = BITMAP_HOT; 303: } 304: 305: /* 306: * Create the cursor. 307: */ 308: cursor = XCreateCursor( 309: width, height, 310: cursor_bits, mask_bits, 311: x_hot, y_hot, 312: mse, bg, 313: GXcopy 314: ); 315: if (cursor == FAILURE) Error("Unable to store cursor"); 316: 317: /* 318: * Define the root window's cursor to be the new 319: * cursor. 320: */ 321: XDefineCursor(RootWindow, cursor); 322: } 323: 324: /* 325: * Set background to a solid color if requested. 326: */ 327: if (solid_sw == TRUE) { 328: /* 329: * Create the tile Pixmap. 330: */ 331: bkgnd_pixmap = XMakeTile(solid); 332: if (bkgnd_pixmap == FAILURE) Error("Unable to store solid Pixmap"); 333: /* 334: * Change the window. 335: */ 336: XChangeBackground(RootWindow, bkgnd_pixmap); 337: Done(); 338: } 339: 340: /* 341: * Set background to a gray pattern if requested. 342: */ 343: if (gray_sw == TRUE) { 344: /* 345: * Create the Bitmap. 346: */ 347: bkgnd_bitmap = XStoreBitmap(BITMAP_SIZE, BITMAP_SIZE, gray_bits); 348: if (bkgnd_bitmap == FAILURE) Error("Unable to store gray Bitmap"); 349: /* 350: * Create the tile Pixmap. 351: */ 352: if (invert_sw == TRUE) { 353: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, bg, fg); 354: } 355: else { 356: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, fg, bg); 357: } 358: if (bkgnd_pixmap == FAILURE) Error("Unable to store gray Pixmap"); 359: /* 360: * Change the window. 361: */ 362: XChangeBackground(RootWindow, bkgnd_pixmap); 363: Done(); 364: } 365: 366: /* 367: * Set background to a bitmap pattern if requested. 368: */ 369: if (bitmap_sw == TRUE) { 370: int status; 371: /* 372: * Open and read the bitmap file. 373: */ 374: status = XReadBitmapFile( 375: bitmap_fname, 376: &width, &height, &bkgnd_bits, 377: &x_hot, &y_hot 378: ); 379: if (status == 0) Error ("Unable to open Bitmap file"); 380: else if (status < 0) Error("Unable to parse Bitmap file"); 381: else if ((width != BITMAP_SIZE) || (height != BITMAP_SIZE)) 382: Error("Invaild Bitmap size"); 383: bkgnd_bitmap = XStoreBitmap (width, height, bkgnd_bits); 384: if (bkgnd_bitmap == FAILURE) 385: Error("Unable to store Bitmap"); 386: 387: /* 388: * Create the tile pixmap. 389: */ 390: if (invert_sw == TRUE) { 391: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, bg, fg); 392: } 393: else { 394: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, fg, bg); 395: } 396: if (bkgnd_pixmap == FAILURE) Error("Unable to store Pixmap"); 397: /* 398: * Change the window. 399: */ 400: XChangeBackground(RootWindow, bkgnd_pixmap); 401: Done(); 402: } 403: 404: /* 405: * Set background to a modula pattern if requested. 406: */ 407: if (mod_sw == TRUE) { 408: bkgnd_bits = (short *) malloc (BITMAP_SIZE*sizeof(short)); 409: /* 410: * Compute modula bits. 411: */ 412: for (x = 0; x < BITMAP_SIZE; x++) { 413: pattern_line <<= 1; 414: if (x % xmod == 0) pattern_line |= 0x0001; 415: } 416: for (y = 0; y < BITMAP_SIZE; y++) { 417: if (y % ymod == 0) { 418: bkgnd_bits[y] = solid_line; 419: } 420: else { 421: bkgnd_bits[y] = pattern_line; 422: } 423: } 424: /* 425: * Create and store the pattern pixmap. 426: */ 427: bkgnd_bitmap = XStoreBitmap(BITMAP_SIZE, BITMAP_SIZE, bkgnd_bits); 428: if (bkgnd_bitmap == FAILURE) Error("Error storing bitmap"); 429: if (invert_sw == TRUE) { 430: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, bg, fg); 431: } 432: else { 433: bkgnd_pixmap = XMakePixmap(bkgnd_bitmap, fg, bg); 434: } 435: if (bkgnd_pixmap == FAILURE) Error("Error storing pixmap"); 436: XChangeBackground(RootWindow, bkgnd_pixmap); 437: Done(); 438: } 439: 440: /* 441: * If we got here then check to see if the default switch is on 442: * OR if there were no arguments. 443: */ 444: if (def_sw == TRUE) { 445: if (name_sw == FALSE) XStoreName(RootWindow, DEF_ROOT_NAME); 446: if (cursor_sw == FALSE) XUndefineCursor(RootWindow); 447: XChangeBackground(RootWindow, (Pixmap)0); 448: Done(); 449: } 450: else XFlush(); 451: exit(0); 452: } 453: 454: 455: 456: /* 457: * Clear the root window, flush all output and exit. 458: */ 459: Done() 460: { 461: XClear(RootWindow); 462: XFlush(); 463: exit(0); 464: } 465: 466: 467: 468: /* 469: * Report an internal error. 470: */ 471: Error(description) 472: char *description; 473: { 474: printf ("\nxsetroot: %s.\n\n", description); 475: exit(1); 476: } 477: 478: 479: 480: /* 481: * Report the syntax for calling xsetroot. 482: */ 483: Syntax(call) 484: char *call; 485: { 486: printf("\nUsage: %s [-help] [-def] [-fg <color>] [-bg <color>] [-invert]\n", call); 487: printf(" [-name <string>] [-cursor <cursor file> <mask file>]\n"); 488: printf(" [-solid <color>] [-gray, -grey] [-bitmap <file>]\n"); 489: printf(" [-mod <x> <y>] [[<host>]:<vs>]\n"); 490: printf("\nNOTE: *** Use only one of [-solid] [-gray, -grey] [-bitmap] [-mod] ***\n\n"); 491: exit(1); 492: } 493: 494: /* End of xsetroot.c */