home *** CD-ROM | disk | FTP | other *** search
/ Adventures in Heaven 2 / adventuresinheaven2powergamesfordosandwindows.iso / windows / arcade / cbzone / c_gpr.x11 < prev    next >
Text File  |  1992-05-27  |  24KB  |  843 lines

  1. #include "c_includ.h"
  2. /*
  3.  * cbzone_gpr.c
  4.  *  -- Todd W Mummert, December 1990, CMU
  5.  *
  6.  * RCS info
  7.  *  $Header: c_gpr.c,v 1.1 91/01/12 02:03:32 mummert Locked $
  8.  *
  9.  * emulate gpr/ftn on top of X11.
  10.  *
  11.  * I don't know who originally wrote this emulation routine, but
  12.  * I've seriously changed it anyway.  Therefore, it should not
  13.  * be used with any expectation that it will do anything similiar
  14.  * to the original gpr routines.
  15.  * Almost all of the routines have had their arguments shortened.
  16.  * And why bother returning status when it's always the same.  Most
  17.  * importantly I removed the mallocs that were in polyline and
  18.  * multiline.
  19.  *
  20.  * Added support for window managers, iconification, color, etc...
  21.  * Now, it's basically a file full of one line functions...except
  22.  * for the event handling and initialization.
  23.  *
  24.  * let's define how we are going to use the GC's...
  25.  * Erase GC...both foreground and background the color of the game
  26.  *   background...
  27.  * Text GC...foreground is red...as all text is currently
  28.  * Draw GC...will be changed often...usually just the color
  29.  * BitBlt GC...it will handle the printing of the
  30.  *           lander/missile warning boxes after they are initially
  31.  *           placed.
  32.  */
  33.  
  34. #include "bitmaps\moon.bit"
  35. #include "bitmaps\joystick.bit"
  36. #include "bitmaps\hswitch.bit"
  37. #include "bitmaps\lswitch.bit"
  38. #include "bitmaps\tank.bit"
  39. #include "bitmaps\cursor.bit"
  40. #include "bitmaps\cshape.bit"
  41.  
  42. typedef struct {
  43.   char name[14];                /* the color will be stored as  */
  44. } Colorname;                    /*      #RRRRGGGGBBBB           */
  45.  
  46. typedef struct {
  47.   int width;
  48.   int height;
  49.   char* bits;
  50.   Pixmap p;
  51.   unsigned int color;
  52. } Bmap;
  53.  
  54. Display *d;
  55. Window w, root;
  56. GC BitBltGC, DrawGC, TextGC, EraseGC;
  57. XRectangle clipr;                     /* defines the main viewscreen       */
  58. Colormap cmap, dcmap;                 /* private & default colormaps       */
  59. int ufc, unfc;                        /* number of fading/nonfading colors */
  60. int cw, cr;                           /* number of colors wanted/received  */
  61. Screen* screen;
  62. Pixel* pixels = NULL;                 /* the actual pixel values we obtain */
  63. Colorname* bases = NULL;              /* names of the base colors          */
  64. Colorname* fcnames = NULL;            /* names of the fading colors        */
  65. int screen_num;
  66. Position_t mouse_posn;
  67. int wid, hei, depth;
  68.  
  69. #define NUMPIXMAPS 5
  70. Bmap bmaps[NUMPIXMAPS] = {
  71.   moon_width, moon_height, moon_bits, None, COLOR_MOON,
  72.   joystick_width, joystick_height, joystick_bits, None, COLOR_JOYSTICK,
  73.   hswitch_width, hswitch_height, hswitch_bits, None, COLOR_TEXT,
  74.   lswitch_width, lswitch_height, lswitch_bits, None, COLOR_TEXT,
  75.   tank_width, tank_height, tank_bits, None, COLOR_TEXT
  76.   };
  77.  
  78. void grabpointer()
  79. {
  80.   while (XGrabPointer(d, w, False,
  81.                       ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
  82.                       GrabModeAsync,
  83.                       GrabModeAsync,
  84.                       w,
  85.                       None,
  86.                       CurrentTime) != GrabSuccess);
  87. }
  88.  
  89. void ungrabpointer()
  90. {
  91.   XUngrabPointer(d, CurrentTime);
  92. }
  93.  
  94. void setmonomap()
  95. {
  96.   int i;
  97.  
  98.   cmap = dcmap;
  99.   pixels = (Pixel*) malloc(2 * sizeof(Pixel));
  100.   opt->cpi[COLOR_BG] = 0;
  101.   pixels[0] = BlackPixelOfScreen(screen);
  102.   opt->cpi[COLOR_FG] = 1;
  103.   pixels[1] = WhitePixelOfScreen(screen);
  104.   opt->fading_colors = 1;
  105.   for (i=2; i<MAX_COLORS; i++)
  106.     opt->cpi[i] = 1;
  107.   cw = cr = 2;
  108. }
  109.  
  110. void createfadedcolors()
  111. {
  112.   XColor color;
  113.   int i, j;
  114.   float factor;
  115.   int num = 0;
  116.  
  117.   if (fcnames != NULL)
  118.     free(fcnames);
  119.   fcnames =
  120.     (Colorname *) malloc(ufc*opt->fading_colors*sizeof(Colorname));
  121.   for (i=LF_COLOR; i>LN_COLOR; i--)
  122.     if (opt->cpi[i] == i) {
  123.       XParseColor(d, dcmap, bases[i].name, &color);
  124.       sprintf(fcnames[num++].name, "#%04x%04x%04x",
  125.               color.red, color.green, color.blue);
  126.       for (j=1; j<opt->fading_colors; j++) {
  127.         factor = 1.0 - 0.5*j/(opt->fading_colors-1);
  128.         sprintf(fcnames[num++].name, "#%04x%04x%04x",
  129.                 (int) (factor*color.red), (int) (factor*color.green),
  130.                 (int) (factor*color.blue));
  131.       }
  132.     }
  133. }
  134.  
  135. void setcoloroptvalues()
  136. {
  137.   int i;
  138.   int num = 0;
  139.  
  140.   for (i=L_COLOR; i>=0; i--)
  141.     if (opt->cpi[i] == i) {
  142.       opt->cpi[i] = num;
  143.       if (i>LN_COLOR)
  144.         num += opt->fading_colors;
  145.       else
  146.         num++;
  147.     }
  148.     else
  149.       opt->cpi[i] = opt->cpi[opt->cpi[i]];
  150. }
  151.  
  152. void standardizecolors(v)
  153.      Visual* v;
  154. {
  155.   XColor color;
  156.   int i;
  157.   int num = 0;
  158.  
  159.   if (bases != NULL)
  160.     free(bases);
  161.   bases = (Colorname *) malloc(MAX_COLORS*sizeof(Colorname));
  162.   for (i=0; i<MAX_COLORS; i++) {
  163.     XParseColor(d, dcmap, opt->cname[i], &color);
  164.     if (v->class == StaticGray || v->class == GrayScale)
  165.       color.red = color.green = color.blue = color.red > color.green ?
  166.         (color.red > color.blue ? color.red : color.blue) :
  167.           (color.green > color.blue ? color.green : color.blue);
  168.     sprintf(bases[num++].name,
  169.             "#%04x%04x%04x", color.red, color.green, color.blue);
  170.   }
  171. }
  172.  
  173. int writecolormap(status)
  174.      Bool* status;
  175. {
  176.   int i, j;
  177.   XColor color;
  178.   int ind = 0;
  179.   int num = 0;
  180.  
  181.   *status = True;
  182.   if (pixels != NULL)
  183.     free(pixels);
  184.   pixels = (Pixel*) calloc(cw, sizeof(Pixel));
  185.   for (i=L_COLOR; *status && i>=0; i--)
  186.     if (opt->cpi[i] == i)
  187.       if (opt->fading_colors < 2 || i<FF_COLOR) {
  188.         XParseColor(d, cmap, bases[i].name, &color);
  189.         if (XAllocColor(d, cmap, &color))
  190.           pixels[num++] = color.pixel;
  191.         else
  192.           *status = False;
  193.       }
  194.       else
  195.         for (j=0; *status && j<opt->fading_colors; j++) {
  196.           XParseColor(d, cmap, fcnames[ind++].name, &color);
  197.           if (XAllocColor(d, cmap, &color))
  198.             pixels[num++] = color.pixel;
  199.           else
  200.             *status = False;
  201.         }
  202.   return num;
  203. }
  204.  
  205. void countcolors()
  206. {
  207.   int i, j;
  208.  
  209.   ufc = unfc = 0;
  210.   if (opt->mono) {
  211.     cr = cw = unfc = 2;
  212.     return;
  213.   }
  214.   for (i=L_COLOR; i>=0; i--) {
  215.     for (j=L_COLOR; j>i && strcmp(bases[i].name,bases[j].name); j--);
  216.     opt->cpi[i] = j;
  217.     if (i == j)
  218.       if (opt->fading_colors==1 || i<FF_COLOR)
  219.         unfc++;
  220.       else
  221.         ufc++;
  222.   }
  223.   cw = unfc+ufc*opt->fading_colors;
  224. }
  225.  
  226. void reducecolors()
  227. {
  228.   int i, firstcolor, lastcolor;
  229.   static int attempt = 0;
  230.   static int groupings[][2] = {
  231.     F1_COLOR, L1_COLOR, F2_COLOR, L2_COLOR, F3_COLOR, L3_COLOR,
  232.     F4_COLOR, L4_COLOR, F5_COLOR, L5_COLOR, F6_COLOR, L6_COLOR,
  233.     F7_COLOR, L7_COLOR, F8_COLOR, L8_COLOR, F9_COLOR, L9_COLOR };
  234.  
  235.   while (cw > cr) {
  236.     switch(attempt++) {
  237.     case 0:                     /* reduce # of fading colors */
  238.       opt->fading_colors = (cr-unfc)/ufc;
  239.       if (opt->fading_colors < 1)
  240.         opt->fading_colors = 1;
  241.       break;
  242.     case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
  243.       /* now start combining color groups */
  244.       firstcolor = groupings[attempt-1][0];
  245.       lastcolor = groupings[attempt-1][1];
  246.       for (i=lastcolor; i>firstcolor; i--)
  247.         strcpy(bases[i].name, bases[firstcolor].name);
  248.       break;
  249.     case 10:
  250.       opt->mono = True;
  251.       break;
  252.     }
  253.     countcolors();
  254.   }
  255. }
  256.  
  257. void setcolormap()
  258. {
  259.   int dallocated, pallocated;
  260.   Bool success = False;
  261.   Visual* visual;
  262.  
  263.   if (opt->mono || depth == 1) {        /* mono? */
  264.     setmonomap();
  265.     return;
  266.   }
  267.  
  268.   visual = DefaultVisual(d, screen_num);
  269.   standardizecolors(visual);
  270.  
  271.   if (opt->max_colors < 2)
  272.     if (depth<8)
  273.       cr = 1<<depth;
  274.     else
  275.       cr = 256;
  276.   else
  277.     cr = opt->max_colors;
  278.  
  279.   if (opt->fading_colors < 1)
  280.     opt->fading_colors = cr;
  281.  
  282.   do {
  283.     countcolors();
  284.     reducecolors();
  285.  
  286.     if (opt->mono) {
  287.       setmonomap();
  288.       return;
  289.     }
  290.  
  291.     if (opt->fading_colors > 1)
  292.       createfadedcolors();
  293.  
  294.     if (DisplayCells(d, screen_num) >= cw) {
  295.       cmap = dcmap;
  296.       dallocated = writecolormap(&success);
  297.     }
  298.     else
  299.       dallocated = DisplayCells(d, screen_num);
  300.  
  301.     pallocated = 0;
  302.     if (!success) {
  303.       XFreeColors(d, cmap, pixels, dallocated, 0);
  304.       if (!opt->defaultcolormap) {
  305.         cmap = XCreateColormap(d, root, visual, AllocNone);
  306.         pallocated = writecolormap(&success);
  307.         if (!success) {
  308.           XFreeColors(d, cmap, pixels, pallocated, 0);
  309.           XFreeColormap(d, cmap);
  310.         }
  311.       }
  312.     }
  313.  
  314.     cr = dallocated>pallocated ? dallocated : pallocated;
  315.   } while (!success);
  316.  
  317.   setcoloroptvalues();
  318. }
  319.  
  320. void gprinit ()
  321. {
  322.   XEvent ev;
  323.  
  324.   XSelectInput(d, w, ExposureMask|PointerMotionMask|ButtonPressMask
  325.                |ButtonReleaseMask|KeyPressMask|StructureNotifyMask);
  326.   XMapRaised(d, w);
  327.   XWindowEvent(d, w, StructureNotifyMask, &ev);
  328.   grabpointer();
  329. }
  330.  
  331. void buildgcs()
  332. {
  333.   XGCValues xgcv;
  334.   XColor BColor, FColor;
  335.   Font f;
  336.  
  337.   xgcv.foreground = pixels[opt->cpi[COLOR_FG]];
  338.   xgcv.background = pixels[opt->cpi[COLOR_BG]];
  339.   DrawGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
  340.   xgcv.foreground = pixels[opt->cpi[COLOR_TEXT]];
  341.   TextGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
  342.   xgcv.foreground = pixels[opt->cpi[COLOR_BG]];
  343.   EraseGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
  344.   xgcv.function = GXxor;
  345.   xgcv.foreground = pixels[opt->cpi[COLOR_TEXT]] ^ pixels[opt->cpi[COLOR_BG]];
  346.   BitBltGC = XCreateGC(d, w, GCFunction | GCForeground | GCBackground, &xgcv);
  347.   FColor.pixel = pixels[opt->cpi[COLOR_CURSOR]];
  348.   XQueryColor(d, cmap, &FColor);
  349.   BColor.pixel = pixels[opt->cpi[COLOR_BG]];
  350.   XQueryColor(d, cmap, &BColor);
  351.   if (opt->cursor)
  352.     XDefineCursor(d, w,
  353.                   XCreatePixmapCursor(d,
  354.                                       XCreateBitmapFromData(d, w, cursor_bits,
  355.                                                             16, 16),
  356.                                       XCreateBitmapFromData(d, w, cshape_bits,
  357.                                                             16, 16),
  358.                                       &FColor, &BColor, 8, 10 ));
  359.   else {
  360.     f = XLoadFont(d, "fixed");
  361.     XDefineCursor(d, w,
  362.                   XCreateGlyphCursor(d, f, f, ' ', ' ', &FColor, &BColor));
  363.   }
  364. }
  365.  
  366. void buildpixmaps()
  367. {
  368.   int i;
  369.  
  370.   for (i=0; i<NUMPIXMAPS; i++)
  371.     bmaps[i].p =
  372.       XCreatePixmapFromBitmapData(d, w, bmaps[i].bit,
  373.                                   bmaps[i].width, bmaps[i].height,
  374.                                   pixels[opt->cpi[bmaps[i].color]],
  375.                                   pixels[opt->cpi[COLOR_BG]],
  376.                                   depth);
  377. }
  378.  
  379. static XrmOptionDescRec CbzoneOptions[] = {
  380.   {"-delay", "*delay", XrmoptionSepArg, NULL},
  381.   {"-blocks", "*blocks", XrmoptionSepArg, NULL},
  382.   {"-landers", "*landers", XrmoptionSepArg, NULL},
  383.   {"-tanks", "*tanks", XrmoptionSepArg, NULL},
  384.   {"-missiles", "*missiles", XrmoptionSepArg, NULL},
  385.   {"-salvos", "*salvos", XrmoptionSepArg, NULL},
  386.   {"-coptersonly", "*coptersonly", XrmoptionNoArg, "True"},
  387.   {"-quiet", "*quiet", XrmoptionNoArg, "False"},
  388.   {"-scores", "*scores", XrmoptionNoArg,  "True"},
  389.   {"-original", "*original", XrmoptionNoArg, "True"},
  390.   {"-version", "*version", XrmoptionNoArg, "True"},
  391.   {"-help", "*help", XrmoptionNoArg, "True"},
  392.   {"-nooutput", "*output", XrmoptionNoArg, "False"},
  393.   {"-mono", "*mono", XrmoptionNoArg, "True"},
  394.   {"-cursor", "*cursor", XrmoptionNoArg, "True"},
  395.   {"-nofullscreen", "*fullscreen", XrmoptionNoArg, "False"},
  396.   {"-defaultcolormap", "*defaultcolormap", XrmoptionNoArg, "True"}
  397. };
  398.  
  399. static XtResource CbzoneResources[] = {
  400.   {"delay", "Delay", XtRInt, sizeof(int),
  401.      XtOffset(Optionp, delay), XtRImmediate, (caddr_t) DELAY},
  402.   {"blocks", "Blocks", XtRInt, sizeof(int),
  403.      XtOffset(Optionp, mblocks), XtRImmediate, (caddr_t) MBLOCKS},
  404.   {"landers", "Landers", XtRInt, sizeof(int),
  405.      XtOffset(Optionp, mlanders), XtRImmediate, (caddr_t) MLANDERS},
  406.   {"tanks", "Tanks", XtRInt, sizeof(int),
  407.      XtOffset(Optionp, mtanks), XtRImmediate, (caddr_t) MTANKS},
  408.   {"missiles", "Missiles", XtRInt, sizeof(int),
  409.      XtOffset(Optionp, mmissiles), XtRImmediate, (caddr_t) MMISSILES},
  410.   {"salvos", "Salvos", XtRInt, sizeof(int),
  411.      XtOffset(Optionp, msalvos), XtRImmediate, (caddr_t) MSALVOS},
  412.   {"coptersonly", "Coptersonly", XtRBoolean, sizeof(Boolean),
  413.      XtOffset(Optionp, copters), XtRString, "False"},
  414.   {"quiet", "Quiet", XtRBoolean, sizeof(Boolean),
  415.      XtOffset(Optionp, loud), XtRString, "True"},
  416.   {"scores", "Scores", XtRBoolean, sizeof(Boolean),
  417.      XtOffset(Optionp, scores), XtRString, "False"},
  418.   {"original", "Original", XtRBoolean, sizeof(Boolean),
  419.      XtOffset(Optionp, original), XtRString, "False"},
  420.   {"version", "Version", XtRBoolean, sizeof(Boolean),
  421.      XtOffset(Optionp, version), XtRString, "False"},
  422.   {"help", "Help", XtRBoolean, sizeof(Boolean),
  423.      XtOffset(Optionp, help), XtRString, "False"},
  424.   {"output", "Output", XtRBoolean, sizeof(Boolean),
  425.      XtOffset(Optionp, output), XtRString, "True"},
  426.   {"mono", "Mono", XtRBoolean, sizeof(Boolean),
  427.      XtOffset(Optionp, mono), XtRString, "False"},
  428.   {"bgcolor", "BgColor", XtRString, sizeof(String),
  429.      XtOffset(Optionp, cname[COLOR_BG]),
  430.      XtRString, "black"},
  431.   {"fgcolor", "FgColor", XtRString, sizeof(String),
  432.      XtOffset(Optionp, cname[COLOR_FG]),
  433.      XtRString, "skyblue"},
  434.   {"textcolor", "TextColor", XtRString, sizeof(String),
  435.      XtOffset(Optionp, cname[COLOR_TEXT]),
  436.      XtRString, "red"},
  437.   {"esalvocolor", "SalvoColor", XtRString, sizeof(String),
  438.      XtOffset(Optionp, cname[COLOR_ESALVO]),
  439.      XtRString, "red"},
  440.   {"psalvocolor", "SalvoColor", XtRString, sizeof(String),
  441.      XtOffset(Optionp, cname[COLOR_PSALVO]),
  442.      XtRString, "skyblue"},
  443.   {"joystickcolor", "JoystickColor", XtRString, sizeof(String),
  444.      XtOffset(Optionp, cname[COLOR_JOYSTICK]),
  445.      XtRString, "red"},
  446.   {"scannercolor", "ScannerColor", XtRString, sizeof(String),
  447.      XtOffset(Optionp, cname[COLOR_SCANNER]),
  448.      XtRString, "green"},
  449.   {"horizoncolor", "HorizonColor", XtRString, sizeof(String),
  450.      XtOffset(Optionp, cname[COLOR_HORIZON]),
  451.      XtRString, "gray50"},
  452.   {"mooncolor", "MoonColor", XtRString, sizeof(String),
  453.      XtOffset(Optionp, cname[COLOR_MOON]),
  454.      XtRString, "gray50"},
  455.   {"xhaircolor", "XHairColor", XtRString, sizeof(String),
  456.      XtOffset(Optionp, cname[COLOR_XHAIR]),
  457.      XtRString, "gray50"},
  458.   {"cursorcolor", "CursorColor", XtRString, sizeof(String),
  459.      XtOffset(Optionp, cname[COLOR_CURSOR]), XtRString, "skyblue"},
  460.   {"crackcolor", "CrackColor", XtRString, sizeof(String),
  461.      XtOffset(Optionp, cname[COLOR_CRACKS]),
  462.      XtRString, "skyblue"},
  463.   {"tankcolor", "EnemyColor", XtRString, sizeof(String),
  464.      XtOffset(Optionp, cname[COLOR_TANK]),
  465.      XtRString, "green"},
  466.   {"supercolor", "EnemyColor", XtRString, sizeof(String),
  467.      XtOffset(Optionp, cname[COLOR_SUPER]),
  468.      XtRString, "green"},
  469.   {"missilecolor", "EnemyColor", XtRString, sizeof(String),
  470.      XtOffset(Optionp, cname[COLOR_MISSILE]),
  471.      XtRString, "green"},
  472.   {"coptercolor", "EnemyColor", XtRString, sizeof(String),
  473.      XtOffset(Optionp, cname[COLOR_COPTER]),
  474.      XtRString, "green"},
  475.   {"landercolor", "LanderColor", XtRString, sizeof(String),
  476.      XtOffset(Optionp, cname[COLOR_LANDER]),
  477.      XtRString, "green"},
  478.   {"cubecolor", "BlockColor", XtRString, sizeof(String),
  479.      XtOffset(Optionp, cname[COLOR_CUBE]),
  480.      XtRString, "yellow"},
  481.   {"pyramidcolor", "BlockColor", XtRString, sizeof(String),
  482.      XtOffset(Optionp, cname[COLOR_PYRAMID]),
  483.      XtRString, "yellow"},
  484.   {"maxcolors", "MaxColors", XtRInt, sizeof(int),
  485.      XtOffset(Optionp, max_colors), XtRImmediate, (caddr_t) 0},
  486.   {"fadingcolors", "FadingColors", XtRInt, sizeof(int),
  487.      XtOffset(Optionp, fading_colors), XtRImmediate, (caddr_t) 0},
  488.   {"cursor", "Cursor", XtRBoolean, sizeof(Boolean),
  489.      XtOffset(Optionp, cursor), XtRString, "False"},
  490.   {"fullscreen", "Fullscreen", XtRBoolean, sizeof(Boolean),
  491.      XtOffset(Optionp, fullscreen), XtRString, "True"},
  492.   {"defaultcolormap", "DefaultColormap", XtRBoolean, sizeof(Boolean),
  493.      XtOffset(Optionp, defaultcolormap), XtRString, "False"}
  494. };
  495.  
  496. void gprinqconfig(argc, argv)
  497.      int* argc;
  498.      char* argv[];
  499. {
  500.   Widget cbzone;
  501.   XWMHints wmhints;
  502.   XSizeHints shints;
  503.   XtAppContext cbzonecontext;
  504.   int i;
  505.  
  506.   XtToolkitInitialize();
  507.   cbzonecontext = XtCreateApplicationContext();
  508.   if ((d = XtOpenDisplay(cbzonecontext, NULL,
  509.                          "cbzone", "Cbzone",
  510.                          CbzoneOptions, XtNumber(CbzoneOptions),
  511.                          argc, argv)) == NULL) {
  512.     parseopt(*argc, argv, False);
  513.     printf("can't open display! bye.\n");
  514.     exit(0);
  515.   }
  516.   cbzone = XtAppCreateShell("cbzone", "Cbzone",
  517.                             applicationShellWidgetClass, d, NULL, 0);
  518.   XtGetApplicationResources(cbzone, opt, CbzoneResources,
  519.                             XtNumber(CbzoneResources), NULL, 0);
  520.   parseopt(*argc, argv, True);
  521.  
  522.   screen = DefaultScreenOfDisplay(d);
  523.   depth = DisplayPlanes(d, screen_num);
  524.   screen_num = DefaultScreen(d);
  525.   dcmap = DefaultColormap(d, screen_num);
  526.   root = DefaultRootWindow(d);
  527.   setcolormap();
  528.  
  529.   if (opt->fullscreen) {
  530.     shints.width = wid = WidthOfScreen(screen)+10;
  531.     shints.height = hei = HeightOfScreen(screen)+10;
  532.     shints.x = shints.y = 0;
  533.     shints.flags = USPosition | USSize;
  534.   }
  535.   else {
  536.     wid = 1000;
  537.     hei = 710;
  538.     shints.flags = 0;
  539.   }
  540.  
  541.   w = XCreateSimpleWindow (d, root, 0, 0, wid, hei, 0,
  542.                            pixels[opt->cpi[COLOR_BG]],
  543.                            pixels[opt->cpi[COLOR_BG]]);
  544.   XSetWindowColormap(d, w, cmap);
  545.  
  546.   wmhints.input = True;
  547.   wmhints.flags = InputHint;
  548.   XSetWMHints(d, w, &wmhints);
  549.   XSetNormalHints(d, w, &shints);
  550.   XStoreName(d, w,"Cbzone");
  551.   XSetIconName(d, w, "Cbzone");
  552.  
  553.   gprinit();
  554.   buildgcs();
  555.   buildpixmaps();
  556. }
  557.  
  558. void gprsettextfont(font)
  559.      Font font;
  560. {
  561.   XSetFont(d, TextGC, font);
  562. }
  563.  
  564. void printstring(x, y, string, nchars)
  565.      int x, y;
  566.      char* string;
  567.      int nchars;
  568. {
  569.   XDrawImageString (d, w, TextGC, x, y, string, nchars);
  570. }
  571.  
  572. void polyline(points, number)
  573.      XPoint *points;
  574.      int number;
  575. {
  576.   XDrawLines(d, w, DrawGC, points, number, CoordModeOrigin);
  577. }
  578.  
  579. void multiline(segments, number)
  580.      XSegment *segments;
  581.      int number;
  582. {
  583.   XDrawSegments (d, w, DrawGC, segments, number);
  584. }
  585.  
  586. void drawrectangle(x, y, width, height)
  587.      int x, y, width, height;
  588. {
  589.   XDrawRectangle (d, w, DrawGC, x, y, width, height);
  590. }
  591.  
  592. void bitblt(window)
  593.      Window_t *window;
  594. {
  595.   XFillRectangle(d, w, BitBltGC, window->base.x, window->base.y,
  596.                  window->size.x, window->size.y);
  597. }
  598.  
  599. void clearrectangle(window, dsto)
  600.      Window_t *window;
  601.      Position_t *dsto;
  602. {
  603.   XFillRectangle(d,w,EraseGC,dsto->x, dsto->y,
  604.                  window->size.x, window->size.y);
  605. }
  606.  
  607. void gprsetclippingactive(flag)
  608.      Bool flag;
  609. {
  610.   if (flag) {
  611.     XSetClipRectangles (d, DrawGC, 0, 0, &clipr, 1, YXBanded);
  612.     XSetClipRectangles (d, EraseGC, 0, 0, &clipr, 1, YXBanded);
  613.   } else {
  614.     XSetClipMask (d, DrawGC, None);
  615.     XSetClipMask (d, EraseGC, None);
  616.   }
  617. }
  618.  
  619. void tonetime()
  620. {
  621.   if (opt->loud)
  622.     XBell(d, 0);
  623. }
  624.  
  625. void timeclock(tval)
  626.      struct timeval* tval;
  627. {
  628.   XSync(d, False);
  629.   gettimeofday(tval, 0);
  630. }
  631.  
  632. void gprinqcursor(posn)
  633.      Position_t *posn;
  634. {
  635.   *posn = mouse_posn;
  636. }
  637.  
  638. Bool paused = False;
  639. Bool gprcondeventwait(key, posn)
  640.      char* key;
  641.      Position_t *posn;
  642. {
  643.   static unsigned long flag[16]={0};
  644.   XEvent ev;
  645.   char keystr;
  646.   Bool return_val = False;
  647.   Bool motion = False;
  648.  
  649.   while (XPending(d) || paused) {
  650.     XNextEvent(d, &ev);
  651.     switch(ev.type) {
  652.     case Expose:
  653.       if (((XExposeEvent*) &ev)->count) break;
  654.       if (!paused)
  655.         grabpointer();
  656.       mouse_posn.x = ev.xexpose.x;
  657.       mouse_posn.y = ev.xexpose.y;
  658.       *key = 'R';
  659.       return_val = True;
  660.       break;
  661.     case UnmapNotify:
  662.       paused = True;
  663.       break;
  664.     case MapNotify:
  665.       paused = False;
  666.       break;
  667.     case KeyPress:
  668.       if (XLookupString(&ev.xkey,&keystr,1,(KeySym *) 0,
  669.                         (XComposeStatus *) 0) == 1)
  670.         switch (keystr) {
  671.         case 'p': case 'P':
  672.           ungrabpointer();
  673.           paused = True;
  674.           break;
  675.         case 'c': case 'C':
  676.           grabpointer();
  677.           paused = False;
  678.           break;
  679.         case 'i': case 'I': case ' ':
  680.           XIconifyWindow(d, w, screen_num);
  681.           break;
  682.         case 'r': case 'R':
  683.           *key = 'R';
  684.           return_val = True;
  685.           break;
  686.         case '\003': case 'q': case 'Q':
  687.           *key = 'Q';
  688.           return_val = True;
  689.           break;
  690.         }
  691.       if (return_val) {
  692.         mouse_posn.x = ev.xkey.x;
  693.         mouse_posn.y = ev.xkey.y;
  694.       }
  695.       break;
  696.     case MotionNotify:
  697.       motion = True;
  698.       mouse_posn.x = ev.xmotion.x;
  699.       mouse_posn.y = ev.xmotion.y;
  700.       break;
  701.     case ButtonPress:
  702.       mouse_posn.x = ev.xbutton.x;
  703.       mouse_posn.y = ev.xbutton.y;
  704.       *key = ev.xbutton.button + 'a' - 1;
  705.       flag[ev.xbutton.button]=1;
  706.       return_val = True;
  707.       break;
  708.     case ButtonRelease:
  709.       mouse_posn.x = ev.xbutton.x;
  710.       mouse_posn.y = ev.xbutton.y;
  711.       *key = ev.xbutton.button + 'A' - 1;
  712.       flag[ev.xbutton.button]=0;
  713.       return_val = True;
  714.       break;
  715.     }
  716.     if (return_val)
  717.       break;
  718.   }
  719.   if(flag[1]&&flag[3]) {
  720.     *key = 'Q';
  721.     return_val = True;
  722.   }
  723.   if (!paused) {
  724.     *posn = mouse_posn;
  725.   }
  726.   return return_val;
  727. }
  728.  
  729. void gprsetcursorposition(posn)
  730.      Position_t *posn;
  731. {
  732.   XWarpPointer (d, None, w, 0, 0, 0, 0, posn->x, posn->y);
  733. }
  734.  
  735. Font gprloadfontfile(name)
  736.      char* name;
  737. {
  738.   XFontStruct *xfs;
  739.  
  740.   if ((xfs = XLoadQueryFont(d, name)) == NULL)
  741.     return XLoadFont(d, "fixed");
  742.   else
  743.     return xfs->fid;
  744. }
  745.  
  746. void gprsettextvalue(index)
  747.      int index;
  748. {
  749.   XSetForeground(d, TextGC, pixels[index]);
  750. }
  751.  
  752. void gprsettextbackgroundvalue(index)
  753.      int index;
  754. {
  755.   XSetBackground(d, TextGC, pixels[index]);
  756. }
  757.  
  758. void gprsetdrawvalue(index)
  759.      int index;
  760. {
  761.   XSetForeground(d, DrawGC, pixels[index]);
  762. }
  763.  
  764. void gprcircle(center, radius)
  765.      Position_t *center;
  766.      int radius;
  767. {
  768.   XDrawArc (d, w, DrawGC,
  769.             center->x - radius, center->y - radius,
  770.             radius+radius, radius+radius, 0, 360*64);
  771. }
  772.  
  773. void gprcirclefilled(center, radius)
  774.      Position_t *center;
  775.      int radius;
  776. {
  777.   XFillArc (d, w, DrawGC,
  778.             center->x - radius, center->y - radius,
  779.             radius+radius, radius+radius, 0, 360*64);
  780. }
  781.  
  782. void gprsetclipwindow(window)
  783.      Window_t *window;
  784. {
  785.   clipr.x = window->base.x;
  786.   clipr.y = window->base.y;
  787.   clipr.width = window->size.x;
  788.   clipr.height =  window->size.y;
  789. }
  790.  
  791. void clearentirescreen()
  792. {
  793.   XClearWindow(d, w);
  794. }
  795.  
  796. void flushwindow()
  797. {
  798.   XFlush(d);
  799. }
  800.  
  801. void waitforkey(c)
  802.      char c;
  803. {
  804.   char keystr;
  805.   XEvent ev;
  806.  
  807.   XFlush(d);                    /* out with the old */
  808.   while (XPending(d))
  809.     XNextEvent(d, &ev);
  810.                                 /* now wait for the new */
  811.   while (1) {
  812.     XWindowEvent(d, w, KeyPressMask|ButtonPressMask, &ev);
  813.     switch(ev.type) {
  814.     case KeyPress:
  815.       if (!c || XLookupString(&ev.xkey,&keystr,1,(KeySym *) 0,
  816.                         (XComposeStatus *) 0) == 1 &&
  817.           keystr == c)
  818.         return;
  819.       break;
  820.     case ButtonPress:
  821.       if (!c)
  822.         return;
  823.       break;
  824.     }
  825.   }
  826. }
  827.  
  828. void putpixmap(i, p)
  829.      int i;
  830.      int* p;
  831. {
  832.   XCopyArea(d, bmaps[i].p, w, DrawGC, 0, 0,
  833.             bmaps[i].width, bmaps[i].height, p[0], p[1]);
  834. }
  835.  
  836. void removepixmap(i, p)
  837.      int i;
  838.      int* p;
  839. {
  840.   XFillRectangle(d, w, EraseGC, p[0], p[1],
  841.                  bmaps[i].width, bmaps[i].height);
  842. }
  843.