|
|
1.1 ! root 1: /* $Header: wsimple.c,v 1.2 87/09/11 22:38:15 rws Exp $ */ ! 2: #include <X11/Xlib.h> ! 3: #include <X11/Xutil.h> ! 4: #include <X11/cursorfont.h> ! 5: #include <stdio.h> ! 6: /* ! 7: * Other_stuff.h: Definitions of routines in other_stuff. ! 8: * ! 9: * Written by Mark Lillibridge. Last updated 7/1/87 ! 10: * ! 11: * Send bugs, etc. to [email protected]. ! 12: */ ! 13: ! 14: unsigned long Resolve_Color(); ! 15: Pixmap Bitmap_To_Pixmap(); ! 16: Window Select_Window(); ! 17: void out(); ! 18: void blip(); ! 19: Window Window_With_Name(); ! 20: /* ! 21: * Just_display: A group of routines designed to make the writting of simple ! 22: * X11 applications which open a display but do not open ! 23: * any windows much faster and easier. Unless a routine says ! 24: * otherwise, it may be assumed to require program_name, dpy, ! 25: * and screen already defined on entry. ! 26: * ! 27: * Written by Mark Lillibridge. Last updated 7/1/87 ! 28: * ! 29: * Send bugs, etc. to [email protected]. ! 30: */ ! 31: ! 32: #include <strings.h> ! 33: ! 34: #define NULL 0 ! 35: ! 36: /* This stuff is defined in the calling program by just_display.h */ ! 37: extern char *program_name; ! 38: extern Display *dpy; ! 39: extern int screen; ! 40: ! 41: ! 42: /* ! 43: * Standard fatal error routine - call like printf but maximum of 7 arguments. ! 44: * Does not require dpy or screen defined. ! 45: */ ! 46: void Fatal_Error(msg, arg0,arg1,arg2,arg3,arg4,arg5,arg6) ! 47: char *msg; ! 48: char *arg0, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6; ! 49: { ! 50: fflush(stdout); ! 51: fflush(stderr); ! 52: fprintf(stderr, "%s: error: ", program_name); ! 53: fprintf(stderr, msg, arg0, arg1, arg2, arg3, arg4, arg5, arg6); ! 54: fprintf(stderr, "\n"); ! 55: exit(1); ! 56: } ! 57: ! 58: ! 59: /* ! 60: * Malloc: like malloc but handles out of memory using Fatal_Error. ! 61: */ ! 62: char *Malloc(size) ! 63: unsigned size; ! 64: { ! 65: char *data, *malloc(); ! 66: ! 67: if (!(data = malloc(size))) ! 68: Fatal_Error("Out of memory!"); ! 69: ! 70: return(data); ! 71: } ! 72: ! 73: ! 74: /* ! 75: * Realloc: like Malloc except for realloc, handles NULL using Malloc. ! 76: */ ! 77: char *Realloc(ptr, size) ! 78: char *ptr; ! 79: int size; ! 80: { ! 81: char *new_ptr, *realloc(); ! 82: ! 83: if (!ptr) ! 84: return(Malloc(size)); ! 85: ! 86: if (!(new_ptr = realloc(ptr, size))) ! 87: Fatal_Error("Out of memory!"); ! 88: ! 89: return(new_ptr); ! 90: } ! 91: ! 92: ! 93: /* ! 94: * Get_Display_Name: Routine which returns the name of the display we ! 95: * are supposed to use. This is either the display name ! 96: * given in the list of command arguments or if no name ! 97: * is given, the value of the DISPLAY environmental ! 98: * variable. If DISPLAY is unset, NULL is returned. ! 99: * The main program should pass its command arguments ! 100: * to this routine. The display argument if any is found ! 101: * will be removed from the list of command arguments. ! 102: * Any command argument containing a ':' which occurs ! 103: * before a '-' is considered to be a display. If ! 104: * two or more of these occur, only the first will be used. ! 105: * Does not require dpy or screen defined on entry. ! 106: */ ! 107: char *Get_Display_Name(argc, argv) ! 108: int *argc; /* MODIFIED */ ! 109: char **argv; /* MODIFIED */ ! 110: { ! 111: char *display; ! 112: char *getenv(); ! 113: int nargc=1; ! 114: int count; ! 115: char **nargv; ! 116: ! 117: /* Get default display name from environmental variable DISPLAY */ ! 118: display = getenv("DISPLAY"); ! 119: ! 120: /* ! 121: * Search for a user supplied overide command argument of the ! 122: * form host:display and remove it from command arguments if found. ! 123: */ ! 124: count = *argc; ! 125: nargv = argv + 1; ! 126: for (count--, argv++; count>0; count--, argv++) { ! 127: if (!strcmp("-", *argv)) { /* Don't search past a "-" */ ! 128: break; ! 129: } ! 130: if (index(*argv, ':')) { ! 131: display = *(argv++); ! 132: count--; ! 133: break; /* Only use first display name */ ! 134: } ! 135: *(nargv++) = *argv; ! 136: nargc++; ! 137: } ! 138: while (count>0) { ! 139: *(nargv++) = *(argv++); ! 140: nargc++; count--; ! 141: } ! 142: *argc = nargc; ! 143: ! 144: return(display); ! 145: } ! 146: ! 147: ! 148: /* ! 149: * Open_Display: Routine to open a display with correct error handling. ! 150: * Does not require dpy or screen defined on entry. ! 151: */ ! 152: Display *Open_Display(display_name) ! 153: char *display_name; ! 154: { ! 155: Display *d; ! 156: ! 157: d = XOpenDisplay(display_name); ! 158: if (d == NULL) { ! 159: if (display_name == NULL) ! 160: Fatal_Error("Could not open default display!"); ! 161: else ! 162: Fatal_Error("Could not open display %s!", display_name); ! 163: } ! 164: ! 165: return(d); ! 166: } ! 167: ! 168: ! 169: /* ! 170: * Setup_Display_And_Screen: This routine opens up the correct display (i.e., ! 171: * it calls Get_Display_Name) and then stores a ! 172: * pointer to it in dpy. The default screen ! 173: * for this display is then stored in screen. ! 174: * Does not require dpy or screen defined. ! 175: */ ! 176: void Setup_Display_And_Screen(argc, argv) ! 177: int *argc; /* MODIFIED */ ! 178: char **argv; /* MODIFIED */ ! 179: { ! 180: dpy = Open_Display(Get_Display_Name(argc, argv)); ! 181: screen = DefaultScreen(dpy); ! 182: } ! 183: ! 184: ! 185: /* ! 186: * Open_Font: This routine opens a font with error handling. ! 187: */ ! 188: XFontStruct *Open_Font(name) ! 189: char *name; ! 190: { ! 191: XFontStruct *font; ! 192: ! 193: if (!(font=XLoadQueryFont(dpy, name))) ! 194: Fatal_Error("Unable to open font %s!", name); ! 195: ! 196: return(font); ! 197: } ! 198: ! 199: ! 200: /* ! 201: * Beep: Routine to beep the display. ! 202: */ ! 203: void Beep() ! 204: { ! 205: XBell(dpy, 50); ! 206: } ! 207: ! 208: ! 209: /* ! 210: * ReadBitmapFile: same as XReadBitmapFile except it returns the bitmap ! 211: * directly and handles errors using Fatal_Error. ! 212: */ ! 213: static void _bitmap_error(status, filename) ! 214: int status; ! 215: char *filename; ! 216: { ! 217: if (status == BitmapOpenFailed) ! 218: Fatal_Error("Can't open file %s!", filename); ! 219: else if (status == BitmapFileInvalid) ! 220: Fatal_Error("file %s: Bad bitmap format.", filename); ! 221: else ! 222: Fatal_Error("Out of memory!"); ! 223: } ! 224: ! 225: Pixmap ReadBitmapFile(d, filename, width, height, x_hot, y_hot) ! 226: Drawable d; ! 227: char *filename; ! 228: int *width, *height, *x_hot, *y_hot; ! 229: { ! 230: Pixmap bitmap; ! 231: int status; ! 232: ! 233: status = XReadBitmapFile(dpy, RootWindow(dpy, screen), filename, width, ! 234: height, &bitmap, x_hot, y_hot); ! 235: if (status != BitmapSuccess) ! 236: _bitmap_error(status, filename); ! 237: ! 238: return(bitmap); ! 239: } ! 240: ! 241: ! 242: /* ! 243: * WriteBitmapFile: same as XWriteBitmapFile except it handles errors ! 244: * using Fatal_Error. ! 245: */ ! 246: void WriteBitmapFile(filename, bitmap, width, height, x_hot, y_hot) ! 247: char *filename; ! 248: Pixmap bitmap; ! 249: int width, height, x_hot, y_hot; ! 250: { ! 251: int status; ! 252: ! 253: status= XWriteBitmapFile(dpy, filename, bitmap, width, height, x_hot, ! 254: y_hot); ! 255: if (status != BitmapSuccess) ! 256: _bitmap_error(status, filename); ! 257: } ! 258: ! 259: ! 260: /* ! 261: * Select_Window_Args: a rountine to provide a common interface for ! 262: * applications that need to allow the user to select one ! 263: * window on the screen for special consideration. ! 264: * This routine implements the following command line ! 265: * arguments: ! 266: * ! 267: * -root Selects the root window. ! 268: * -id <id> Selects window with id <id>. <id> may ! 269: * be either in decimal or hex. ! 270: * -name <name> Selects the window with name <name>. ! 271: * ! 272: * Call as Select_Window_Args(&argc, argv) in main before ! 273: * parsing any of your program's command line arguments. ! 274: * Select_Window_Args will remove its arguments so that ! 275: * your program does not have to worry about them. ! 276: * The window returned is the window selected or 0 if ! 277: * none of the above arguments was present. If 0 is ! 278: * returned, Select_Window should probably be called after ! 279: * all command line arguments, and other setup is done. ! 280: * For examples of usage, see xwininfo, xwd, or xprop. ! 281: */ ! 282: Window Select_Window_Args(rargc, argv) ! 283: int *rargc; ! 284: char **argv; ! 285: #define ARGC (*rargc) ! 286: { ! 287: int nargc=1; ! 288: int argc; ! 289: char **nargv; ! 290: Window w=0; ! 291: ! 292: nargv = argv+1; argc = ARGC; ! 293: #define OPTION argv[0] ! 294: #define NXTOPTP ++argv, --argc>0 ! 295: #define NXTOPT if (++argv, --argc==0) usage() ! 296: #define COPYOPT nargv++[0]=OPTION; nargc++ ! 297: ! 298: while (NXTOPTP) { ! 299: if (!strcmp(OPTION, "-")) { ! 300: COPYOPT; ! 301: while (NXTOPTP) ! 302: COPYOPT; ! 303: break; ! 304: } ! 305: if (!strcmp(OPTION, "-root")) { ! 306: w=RootWindow(dpy, screen); ! 307: continue; ! 308: } ! 309: if (!strcmp(OPTION, "-name")) { ! 310: NXTOPT; ! 311: w = Window_With_Name(dpy, RootWindow(dpy, screen), ! 312: OPTION); ! 313: if (!w) ! 314: Fatal_Error("No window with name %s exists!",OPTION); ! 315: continue; ! 316: } ! 317: if (!strcmp(OPTION, "-id")) { ! 318: NXTOPT; ! 319: w=0; ! 320: sscanf(OPTION, "0x%lx", &w); ! 321: if (!w) ! 322: sscanf(OPTION, "%ld", &w); ! 323: if (!w) ! 324: Fatal_Error("Invalid window id format: %s.", OPTION); ! 325: continue; ! 326: } ! 327: COPYOPT; ! 328: } ! 329: ARGC = nargc; ! 330: ! 331: return(w); ! 332: } ! 333: ! 334: /* ! 335: * Just_one_window: A group of routines designed to make the writting of simple ! 336: * X11 applications which open a display and exactly one real ! 337: * window much faster and easier. Unless a routine says ! 338: * otherwise, it requires program_name, dpy, screen, and ! 339: * wind to be defined on entry. ! 340: * ! 341: * Written by Mark Lillibridge. Last updated 7/1/87 ! 342: * ! 343: * Send bugs, etc. to [email protected]. ! 344: */ ! 345: ! 346: ! 347: #define NULL 0 ! 348: ! 349: /* This stuff is defined in program by just_display.h */ ! 350: extern char *program_name; ! 351: extern Display *dpy; ! 352: extern int screen; ! 353: ! 354: /* This stuff is defined in the calling program by just_one_window.h */ ! 355: extern Window wind; ! 356: extern char **_commands; ! 357: extern int _number_of_commands; ! 358: extern char *title, *icon_name, *icon_bitmap_file; ! 359: extern char *geometry,*border_color, *back_color, *fore_color, *body_font_name; ! 360: extern int border_width, reverse; ! 361: extern unsigned long border, background, foreground; ! 362: extern XFontStruct *body_font; ! 363: extern XSizeHints size_hints; ! 364: extern Pixmap icon_pixmap; ! 365: ! 366: /* ! 367: * Get_X_Defaults: This routine reads in the user's .Xdefaults file and ! 368: * uses it to update the X Options to the user's personal ! 369: * defaults. Note: this routines does not require wind ! 370: * defined on entry. ! 371: */ ! 372: void Get_X_Defaults() ! 373: { ! 374: char *option; ! 375: ! 376: if (option = XGetDefault(dpy, program_name, "ReverseVideo")) { ! 377: if (!strcmp("on", option)) ! 378: reverse=1; ! 379: if (!strcmp("off", option)) ! 380: reverse=0; ! 381: } ! 382: if (option = XGetDefault(dpy, program_name, "BorderWidth")) ! 383: border_width = atoi(option); ! 384: if (option = XGetDefault(dpy, program_name, "BorderColor")) ! 385: border_color = option; ! 386: if (option = XGetDefault(dpy, program_name, "Border")) ! 387: border_color = option; ! 388: if (option = XGetDefault(dpy, program_name, "Background")) ! 389: back_color = option; ! 390: if (option = XGetDefault(dpy, program_name, "Foreground")) ! 391: fore_color = option; ! 392: if (option = XGetDefault(dpy, program_name, "BodyFont")) ! 393: body_font_name = option; ! 394: if (option = XGetDefault(dpy, program_name, "Title")) ! 395: title = option; ! 396: if (option = XGetDefault(dpy, program_name, "IconName")) ! 397: icon_name = option; ! 398: if (option = XGetDefault(dpy, program_name, "IconBitmap")) ! 399: icon_bitmap_file = option; ! 400: } ! 401: ! 402: ! 403: /* ! 404: * Get_X_Arguments: This routine takes a program's command argument list and ! 405: * extracts all standard X arguments and uses these to ! 406: * set the X Options with the user's overides. For Options ! 407: * requiring a lookup to get the real value (e.g., a color) ! 408: * the lookup is not done and only the name is stored. ! 409: * The X arguments are removed from the command argument ! 410: * list by compressing the list. *argc is changed ! 411: * appropiatly. All arguments looking like a geometry ! 412: * are also removed unless they follow a '-'. In no case, ! 413: * are any arguments beyond a singe '-' examined or ! 414: * changed. This is to allow a file name with a ':' ! 415: * for instance. The '-' will be left in the argument list. ! 416: * This routine does not require wind be defined. ! 417: * ! 418: */ ! 419: void Get_X_Arguments(argc, argv) ! 420: int *argc; /* Modified */ ! 421: char **argv; /* Modified */ ! 422: { ! 423: int i; ! 424: int nargc; ! 425: char **nargv; ! 426: ! 427: nargv = argv+1; ! 428: nargc = 1; ! 429: ! 430: for (i=1; i<*argc; i++) { ! 431: if (argv[i][0] == '=') { ! 432: geometry = argv[i]; ! 433: continue; ! 434: } ! 435: if (!strcmp(argv[i],"-rv") || !strcmp(argv[i],"-reverse")) { ! 436: reverse = 1; ! 437: continue; ! 438: } ! 439: if (!strcmp(argv[i],"-nm") || !strcmp(argv[i],"-normal")) { ! 440: reverse = 0; ! 441: continue; ! 442: } ! 443: if (!strcmp(argv[i],"-fw") || !strcmp(argv[i],"-forward")) { ! 444: reverse = 0; ! 445: continue; ! 446: } ! 447: if (!strcmp(argv[i],"-bw") || !strcmp(argv[i],"-borderwidth")){ ! 448: if (++i >= *argc) ! 449: usage(); ! 450: border_width = atoi(argv[i]); ! 451: continue; ! 452: } ! 453: if (!strcmp(argv[i],"-bd") || !strcmp(argv[i],"-bordercolor")){ ! 454: if (++i >= *argc) ! 455: usage(); ! 456: border_color = argv[i]; ! 457: continue; ! 458: } ! 459: if (!strcmp(argv[i],"-fg") || !strcmp(argv[i],"-foreground")) { ! 460: if (++i >= *argc) ! 461: usage(); ! 462: fore_color = argv[i]; ! 463: continue; ! 464: } ! 465: if (!strcmp(argv[i],"-bg") || !strcmp(argv[i],"-background")) { ! 466: if (++i >= *argc) ! 467: usage(); ! 468: back_color = argv[i]; ! 469: continue; ! 470: } ! 471: if (!strcmp(argv[i],"-bf") || !strcmp(argv[i],"-bodyfont")) { ! 472: if (++i >= *argc) ! 473: usage(); ! 474: body_font_name = argv[i]; ! 475: continue; ! 476: } ! 477: if (!strcmp(argv[i],"-tl") || !strcmp(argv[i],"-title")) { ! 478: if (++i >= *argc) ! 479: usage(); ! 480: title = argv[i]; ! 481: continue; ! 482: } ! 483: if (!strcmp(argv[i],"-in") || !strcmp(argv[i],"-iconname")) { ! 484: if (++i >= *argc) ! 485: usage(); ! 486: icon_name = argv[i]; ! 487: continue; ! 488: } ! 489: if (!strcmp(argv[i],"-ib") || !strcmp(argv[i],"-icon")) { ! 490: if (++i >= *argc) ! 491: usage(); ! 492: icon_bitmap_file = argv[i]; ! 493: continue; ! 494: } ! 495: ! 496: if (!strcmp(argv[i],"-")) { ! 497: while (i<*argc) { ! 498: nargv[0] = argv[i++]; ! 499: nargc++; ! 500: } ! 501: continue; ! 502: } ! 503: ! 504: *(nargv++) = argv[i]; ! 505: nargc++; ! 506: } ! 507: *argc = nargc; ! 508: } ! 509: ! 510: /* ! 511: * Get_X_Options: The routine does the "right" thing to get the X Options. ! 512: * It is provided basically as a binding procedure. ! 513: * This routine requires only that program_name is defined on ! 514: * entry. As a side effect, both dpy & screen are setup & the ! 515: * "right" display is opened. ! 516: */ ! 517: void Get_X_Options(argc, argv) ! 518: int *argc; /* Modified */ ! 519: char **argv; /* Modified */ ! 520: { ! 521: Setup_Display_And_Screen(argc, argv); ! 522: Get_X_Defaults(); ! 523: ! 524: Get_X_Arguments(argc, argv); ! 525: } ! 526: ! 527: /* ! 528: * Resolve_X_Options: This routine takes off where Get_X_Options left off. ! 529: * It resolves each of the X Options left as names by ! 530: * Get_X_Options to their real value. Wind need not ! 531: * be set on entry. ! 532: * Colors are not resolved. They must be resolved with ! 533: * Resolve_X_Colors AFTER the window wind is created. ! 534: */ ! 535: void Resolve_X_Options() ! 536: { ! 537: XFontStruct *Open_Font(); ! 538: int status, width, height; ! 539: Pixmap bitmap; ! 540: GC gc; ! 541: XGCValues gc_init; ! 542: unsigned long temp; ! 543: ! 544: /* Handle Geometry */ ! 545: if (geometry) { ! 546: status = XParseGeometry(geometry, &(size_hints.x), ! 547: &(size_hints.y), ! 548: &(size_hints.width), ! 549: &(size_hints.height)); ! 550: if (status & (XValue|YValue)) { ! 551: size_hints.flags |= USPosition; ! 552: size_hints.flags &= ~PPosition; ! 553: } ! 554: if (status & (WidthValue|HeightValue)) { ! 555: size_hints.flags |= USSize; ! 556: size_hints.flags &= ~PSize; ! 557: } ! 558: } ! 559: ! 560: /* Handle body font */ ! 561: body_font = Open_Font(body_font_name); ! 562: ! 563: /* Handle no icon name specified, default = use title */ ! 564: if (!icon_name) ! 565: icon_name = title; ! 566: ! 567: /* Handle icon bitmap if any */ ! 568: if (icon_bitmap_file) { ! 569: bitmap = ReadBitmapFile(RootWindow(dpy, screen), ! 570: icon_bitmap_file, ! 571: &width, &height, NULL, NULL); ! 572: gc_init.foreground = Resolve_Color(RootWindow(dpy, screen), ! 573: fore_color); ! 574: gc_init.background = Resolve_Color(RootWindow(dpy, screen), ! 575: back_color); ! 576: if (reverse) { ! 577: temp=gc_init.foreground; ! 578: gc_init.foreground=gc_init.background; ! 579: gc_init.background=temp; ! 580: } ! 581: gc = XCreateGC(dpy, RootWindow(dpy, screen), ! 582: GCForeground|GCBackground, &gc_init); ! 583: icon_pixmap = Bitmap_To_Pixmap(dpy, RootWindow(dpy, screen), ! 584: gc, bitmap, width, height); ! 585: } ! 586: } ! 587: ! 588: /* ! 589: * Resolve_X_Colors: This routine resolves the X Options involving colors. ! 590: * This must be called AFTER the default window has ! 591: * been created and stored in wind. ! 592: */ ! 593: void Resolve_X_Colors() ! 594: { ! 595: unsigned long temp; ! 596: ! 597: foreground = Resolve_Color(wind, fore_color); ! 598: background = Resolve_Color(wind, back_color); ! 599: border = Resolve_Color(wind, border_color); ! 600: if (reverse) { ! 601: temp = foreground; ! 602: foreground = background; ! 603: background = temp; ! 604: } ! 605: } ! 606: ! 607: /* ! 608: * Create_Default_Window: This routine is used once the X Options have been ! 609: * gotten and resolved (except for resolving the colors) ! 610: * It opens up the default window with the placement, ! 611: * size, colors, etc. specified by the X Options. ! 612: * The default window is stored in wind. Wind need ! 613: * not be defined on entry but the X Options must be ! 614: * set before entry. ! 615: */ ! 616: void Create_Default_Window() ! 617: { ! 618: wind = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), ! 619: size_hints.x, size_hints.y, ! 620: size_hints.width, size_hints.height, ! 621: border_width, 0, 0); ! 622: if (!wind) ! 623: Fatal_Error("Unable to create a window!"); ! 624: ! 625: Resolve_X_Colors(); ! 626: ! 627: XSetWindowBackground(dpy, wind, background); ! 628: XSetWindowBorder(dpy, wind, border); ! 629: ! 630: XSetStandardProperties(dpy, wind, title, icon_name, None, _commands, ! 631: _number_of_commands, &size_hints); ! 632: } ! 633: ! 634: /* ! 635: * Get_Default: This routine returns a graphics context suitable for use in ! 636: * drawing into the default window. I.e., it has the right ! 637: * colors, font, etc. Note: This routine always returns a ! 638: * new graphics context. X Options must be set on entry. ! 639: */ ! 640: GC Get_Default_GC() ! 641: { ! 642: XGCValues gc_init; ! 643: ! 644: gc_init.foreground = foreground; ! 645: gc_init.background = background; ! 646: gc_init.font = body_font->fid; ! 647: ! 648: return(XCreateGC(dpy, wind, GCFont|GCForeground|GCBackground, ! 649: &gc_init)); ! 650: } ! 651: ! 652: ! 653: /* ! 654: * _Save_Commands: internal procedure to save away list of commands to program ! 655: * and set command name of program. ! 656: */ ! 657: _Save_Commands(argc, argv) ! 658: int argc; ! 659: char **argv; ! 660: { ! 661: program_name = argv[0]; ! 662: _number_of_commands = argc; ! 663: ! 664: _commands=(char **)Malloc(argc*sizeof(char *)); ! 665: ! 666: bcopy(argv, _commands, argc*sizeof(char *)); ! 667: } ! 668: /* ! 669: * Other_stuff: A group of routines which do common X11 tasks. ! 670: * ! 671: * Written by Mark Lillibridge. Last updated 7/1/87 ! 672: * ! 673: * Send bugs, etc. to [email protected]. ! 674: */ ! 675: ! 676: ! 677: #define NULL 0 ! 678: ! 679: extern Display *dpy; ! 680: extern int screen; ! 681: ! 682: /* ! 683: * Resolve_Color: This routine takes a color name and returns the pixel # ! 684: * that when used in the window w will be of color name. ! 685: * (WARNING: The colormap of w MAY be modified! ) ! 686: * If colors are run out of, only the first n colors will be ! 687: * as correct as the hardware can make them where n depends ! 688: * on the display. This routine does not require wind to ! 689: * be defined. ! 690: */ ! 691: unsigned long Resolve_Color(w, name) ! 692: Window w; ! 693: char *name; ! 694: { ! 695: XColor c; ! 696: Colormap colormap; ! 697: XWindowAttributes wind_info; ! 698: ! 699: /* ! 700: * The following is a hack to insure machines without a rgb table ! 701: * handle at least white & black right. ! 702: */ ! 703: if (!strcmp(name, "white")) ! 704: name="#ffffffffffff"; ! 705: if (!strcmp(name, "black")) ! 706: name="#000000000000"; ! 707: ! 708: XGetWindowAttributes(dpy, w, &wind_info); ! 709: colormap = wind_info.colormap; ! 710: ! 711: if (!XParseColor(dpy, colormap, name, &c)) ! 712: Fatal_Error("Bad color format '%s'.", name); ! 713: ! 714: if (!XAllocColor(dpy, colormap, &c)) ! 715: Fatal_Error("XAllocColor failed!"); ! 716: ! 717: return(c.pixel); ! 718: } ! 719: ! 720: ! 721: /* ! 722: * Bitmap_To_Pixmap: Convert a bitmap to a 2 colored pixmap. The colors come ! 723: * from the foreground and background colors of the gc. ! 724: * Width and height are required solely for efficiency. ! 725: * If needed, they can be obtained via. XGetGeometry. ! 726: */ ! 727: Pixmap Bitmap_To_Pixmap(dpy, d, gc, bitmap, width, height) ! 728: Display *dpy; ! 729: Drawable d; ! 730: GC gc; ! 731: Pixmap bitmap; ! 732: int width, height; ! 733: { ! 734: Pixmap pix; ! 735: int x, depth; ! 736: Drawable root; ! 737: ! 738: if (!XGetGeometry(dpy, d, &root, &x, &x, &x, &x, &x, &depth)) ! 739: return(0); ! 740: ! 741: pix = XCreatePixmap(dpy, d, width, height, depth); ! 742: ! 743: XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, 1); ! 744: ! 745: return(pix); ! 746: } ! 747: ! 748: ! 749: /* ! 750: * outl: a debugging routine. Flushes stdout then prints a message on stderr ! 751: * and flushes stderr. Used to print messages when past certain points ! 752: * in code so we can tell where we are. Outl may be invoked like ! 753: * printf with up to 7 arguments. ! 754: */ ! 755: outl(msg, arg0,arg1,arg2,arg3,arg4,arg5,arg6) ! 756: char *msg; ! 757: char *arg0, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6; ! 758: { ! 759: fflush(stdout); ! 760: fprintf(stderr, msg, arg0, arg1, arg2, arg3, arg4, arg5, arg6); ! 761: fprintf(stderr, "\n"); ! 762: fflush(stderr); ! 763: } ! 764: ! 765: ! 766: /* ! 767: * blip: a debugging routine. Prints Blip! on stderr with flushing. ! 768: */ ! 769: void blip() ! 770: { ! 771: outl("blip!"); ! 772: } ! 773: ! 774: ! 775: /* ! 776: * Routine to let user select a window using the mouse ! 777: */ ! 778: ! 779: Window Select_Window(dpy) ! 780: Display *dpy; ! 781: { ! 782: int status; ! 783: Cursor cursor; ! 784: XEvent event; ! 785: Window target_win; ! 786: ! 787: /* Make the target cursor */ ! 788: cursor = XCreateFontCursor(dpy, XC_crosshair); ! 789: ! 790: /* Grab the pointer using target cursor, letting it room all over */ ! 791: status = XGrabPointer(dpy, RootWindow(dpy, screen), True, ! 792: ButtonPressMask, GrabModeAsync, ! 793: GrabModeAsync, None, cursor, CurrentTime); ! 794: if (status != GrabSuccess) Fatal_Error("Can't grab the mouse."); ! 795: ! 796: /* Let the user select a window... */ ! 797: XNextEvent(dpy, &event); ! 798: target_win = event.xbutton.subwindow; /* Get which window selected */ ! 799: ! 800: XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ ! 801: ! 802: /* 0 for subwindow means root window */ ! 803: if (target_win == 0) ! 804: target_win = RootWindow(dpy, screen); ! 805: ! 806: return(target_win); ! 807: } ! 808: ! 809: ! 810: /* ! 811: * Window_With_Name: routine to locate a window with a given name on a display. ! 812: * If no window with the given name is found, 0 is returned. ! 813: * If more than one window has the given name, the first ! 814: * one found will be returned. Only top and its subwindows ! 815: * are looked at. Normally, top should be the RootWindow. ! 816: */ ! 817: Window Window_With_Name(dpy, top, name) ! 818: Display *dpy; ! 819: Window top; ! 820: char *name; ! 821: { ! 822: Window *children, dummy; ! 823: int nchildren, i; ! 824: Window w=0; ! 825: char *window_name; ! 826: ! 827: if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name)) ! 828: return(top); ! 829: ! 830: if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren)) ! 831: return(0); ! 832: ! 833: for (i=0; i<nchildren; i++) { ! 834: w = Window_With_Name(dpy, children[i], name); ! 835: if (w) ! 836: break; ! 837: } ! 838: XFree(children); ! 839: return(w); ! 840: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.