[comp.sources.x] v11i091: Another Star Trek Game, Part05/14

pfuetz@agd.fhg.de (02/26/91)

Submitted-by: pfuetz@agd.fhg.de
Posting-number: Volume 11, Issue 91
Archive-name: xstrek/part05

#!/bin/sh
# To unshare, sh or unshar this file
echo xstrek/original_code/c_gpr.c 1>&2
sed -e 's/^X//' > xstrek/original_code/c_gpr.c <<'E!O!F! xstrek/original_code/c_gpr.c'
X/*
X * cbzone_gpr.c
X *  -- Todd W Mummert, December 1990, CMU
X * 
X * emulate gpr/ftn on top of X11.
X *
X * I don't know who originally wrote this emulation routine, but
X * I've seriously changed it anyway.  Therefore, it should not
X * be used with any expectation that it will do anything similiar
X * to the original gpr routines which I never saw.    (twm)
X * Almost all of the routines have had their arguments shortened.
X * And why bother returning status when it's always the same.  Most
X * importantly I removed the mallocs that were in polyline and
X * multiline.
X *
X * Added support for window managers, iconification, etc...
X * This code could be a lot neater.  If anyone can tell me how
X * to place a window at (0,0) regardless of the what the window
X * manager wants to do to it, I'd be appreciative.  As it is, I
X * now let the wm do what it wants, then attempt to adjust that.
X * No, I don't think I want a transient window, as I want the wm
X * to handle the iconification....
X * See placewindow() for the gory details.
X *
X * Now, it's basically a file full of one line functions...except
X * for the event handling and initialization.
X */
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/time.h>
X#include <X11/Xlib.h>
X
X#include <X11/Xatom.h>
X#include <X11/Xutil.h>
Xtypedef long int logical;
X
Xunsigned int colortrans[2];
X
Xtypedef unsigned long raster_op_t;
Xtypedef long coord_t;
X
Xtypedef struct {
X  coord_t x, y;
X} position_t;
X
Xtypedef struct {
X  coord_t x, y;
X} offset_t;
X
Xtypedef struct {
X  position_t base;
X  offset_t size;
X} window_t;
X
XDisplay *d;
XWindow w;
XGC FillGC, DrawGC, TextGC, EraseGC;
X
Xvoid gprinit ()
X{
X  /* (twm) I assume that the window will be mapped after this
X   * routine is called.  I should then be free to move it to
X   * (0,0).  However, this is not always the case...as I have
X   * occasionally seen problems with the initial window
X   * placement.  However, 'r' will fix them.
X   */
X  XEvent ev;
X
X  XSelectInput(d, w, ExposureMask);
X  XMapRaised(d, w);
X    do 
X      XWindowEvent(d, w, ExposureMask, &ev);
X    while (((XExposeEvent*) &ev)->count);
X}
X
Xint wid, hei;
XBool was_placed = False;
Xvoid placewindow()
X{
X  /*  the plan is to move the window to (0,0).  However, the window
X   *  manager may not place it there due to borders, titles, etc.
X   *  So we look at it again after movement and readjust.  This
X   *  routine depends on the window already being mapped.
X   *
X   *  Now the major problem is that I really want to interract w/
X   *  the window manager to handle iconification w/ use of icon
X   *  managers and other niceties.  However, the wm intercepting
X   *  events causes lots of problems.
X   *  XSync can flush the xserver, but the wm may have some event it
X   *  has picked up and is working on.  I would like someway to
X   *  GUARANTEE that before the 2nd XGetWindowAttributes below,
X   *  that the XMoveResizeWindow above it has completed. 
X   *  or not).
X   *
X   * why the was_placed?  because when an expose event occurs this
X   * routine gets called, which can generate yet more expose
X   * events...taking time for things to settle down to a steady
X   * state.  The raise window at the bottom may or may not generate
X   * an expose event, so I can't wait for an expose event on it
X   * or I hang if it is already at the top of the stack.
X   *
X   *  If anyone really knows how this should be done, please send
X   *  me mail at mummert+@sam.cs.cmu.edu
X   *
X   * btw, I know I'm breaking the cardinal rule of window manager
X   * interaction...namely let the wm control size and placement.
X   * tough...
X   *
X   * okay, i took out the raisewindow at the bottom...now there may be
X   * cases when moving the window that you expose another.
X   */
X  XEvent ev;
X  XWindowAttributes wattr;
X  Window dummywin;
X  int rx, ry;
X
X  XSelectInput(d, w, StructureNotifyMask);
X  XSync(d, True);               /*  get rid of anything outstanding */
X
X  /* i cheat and change width and height by 1....this almost guarantees
X     i'll generate a ConfigureRequest event.
X   */
X  XMoveResizeWindow(d, w, 0, 0, wid+1, hei+1);  
X  XWindowEvent(d, w, StructureNotifyMask, &ev);
X
X  XGetWindowAttributes(d, w, &wattr);           /* check to see where the */
X  XTranslateCoordinates(d, w, wattr.root, 0, 0, /* wm actually placed it */
X			&rx, &ry, &dummywin);
X    
X  XMoveResizeWindow(d, w, -rx, -ry, wid, hei);  
X  XWindowEvent(d, w, StructureNotifyMask, &ev);
X
X  XSelectInput(d, w, NoEventMask);
X  XSync(d, False);
X  was_placed = True;
X}
X
Xvoid gprinqconfig(config)
X     long *config;
X{
X  XColor color;
X  Cursor c;
X  Font f;
X  XGCValues xgcv;
X  unsigned long fcolor, bcolor;
X  XWMHints wmhints;
X  
X  d = XOpenDisplay (0);
X  if (d == 0) {
X    printf("can't open display! bye.\n");
X    exit(1);
X  }
X  
X  colortrans[1] = fcolor = WhitePixelOfScreen(DefaultScreenOfDisplay(d));
X  colortrans[0] = bcolor = BlackPixelOfScreen(DefaultScreenOfDisplay(d));
X
X  wid = WidthOfScreen(DefaultScreenOfDisplay(d));
X  hei = HeightOfScreen(DefaultScreenOfDisplay(d));
X
X  /* full screen, no border, I'd like it at (0,0)  */
X  w = XCreateSimpleWindow (d, DefaultRootWindow(d),
X                           0, 0,
X                           wid, hei,
X                           0, bcolor, bcolor);
X  
X  /* Tell the WM that we want input... */
X  wmhints.input = True;
X  wmhints.flags = InputHint;
X  XSetWMHints(d, w, &wmhints);
X
X  /* Name and raise the window */
X  XStoreName(d, w,"Cbzone");
X  XSetIconName(d, w, "Cbzone");
X
X  xgcv.background = bcolor;
X  xgcv.foreground = fcolor;
X  FillGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X  xgcv.background = bcolor;
X  xgcv.foreground = bcolor;
X  EraseGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X  xgcv.background = bcolor;
X  xgcv.foreground = fcolor;
X  DrawGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X  TextGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X  f = XLoadFont (d, "fixed");
X  c = XCreateGlyphCursor (d, f, f, ' ', ' ',
X                          &color, &color);
X  XDefineCursor(d, w, c);
X  XSetGraphicsExposures (d, FillGC, False);
X  XSetGraphicsExposures (d, DrawGC, False);
X  XSetGraphicsExposures (d, TextGC, False);
X  
X  *config = 1;		/* XXX should kludge depending on number
X                         * of planes */
X  gprinit();
X  placewindow();
X}
X
Xvoid gprterminate()
X{
X  XCloseDisplay(d);
X}
X
Xvoid gprsettextfont(font)
X     long font;
X{
X  XSetFont(d, TextGC, font);
X}
X
XXRectangle clipr;
X
Xvoid printstring(x, y, string, nchars)
X     int x, y;
X     char *string;
X     long nchars;
X{
X  XDrawImageString (d, w, TextGC, x, y, string, nchars);
X}
X
Xvoid polyline(points, number)
X     XPoint *points;
X     int number;
X{
X  XDrawLines(d, w, DrawGC, points, number, CoordModeOrigin);
X}
X
Xvoid multiline(segments, number)
X     XSegment *segments;
X     int number;
X{
X  XDrawSegments (d, w, DrawGC, segments, number);
X}
X
Xvoid drawrectangle(x, y, width, height)
X     int x, y, width, height;
X{
X  XDrawRectangle (d, w, DrawGC, x, y, width, height);
X}
X
Xvoid fillrectangle(x, y, width, height)
X     int x, y, width, height;
X{
X  XFillRectangle (d, w, FillGC, x, y, width, height);
X}
X
Xstatic long lop;
Xvoid gprbitblt(window, dsto)
X     window_t *window;
X     position_t *dsto;
X{
X  if(lop==6)
X    XFillRectangle(d,w,EraseGC,dsto->x, dsto->y,
X                   window->size.x, window->size.y);
X  else 
X    XCopyArea (d, w, w, FillGC, window->base.x, window->base.y,
X               window->size.x, window->size.y, dsto->x, dsto->y);
X}
X
Xvoid gprsetrasterop(op)
X     raster_op_t op;
X{
X  lop = op;
X  XSetFunction(d, FillGC, lop);
X  XSetFunction(d, TextGC, lop);
X  XSetFunction(d, DrawGC, lop);
X}
X
Xvoid gprsetclippingactive(flag)
X     long flag;
X{
X  if (flag) {
X    XSetClipRectangles (d, FillGC, 0, 0, &clipr, 1, YXBanded);
X    XSetClipRectangles (d, DrawGC, 0, 0, &clipr, 1, YXBanded);
X    XSetClipRectangles (d, TextGC, 0, 0, &clipr, 1, YXBanded);
X  } else {
X    XSetClipMask (d, FillGC, None);
X    XSetClipMask (d, DrawGC, None);
X    XSetClipMask (d, TextGC, None);		
X  }
X}
X
Xvoid tonetime()
X{
X  XBell(d, 0);
X}
X
Xvoid timeclock(tval)
X     struct timeval* tval;
X{
X  XSync(d, 0);
X  gettimeofday(tval, 0);
X}
X
Xposition_t mouse_posn;
X
Xvoid gprinqcursor(posn)
X     position_t *posn;
X{
X  *posn = mouse_posn;
X}
X
Xint gprcondeventwait(key, posn)
X     char *key;
X     position_t *posn;
X{
X  static unsigned long flag[16]={0,0,0,0};
X  XEvent ev;
X  int paused = 0;
X  char keystr;
X  int early_return;
X
X  if (was_placed) {
X    XSync(d, False);
X    XSelectInput(d, w, ExposureMask|PointerMotionMask|
X                 ButtonPressMask|ButtonReleaseMask
X                 |KeyPressMask
X                 |StructureNotifyMask
X                 );
X    was_placed = False;
X  }
X
X  XFlush(d);
X  while (XPending(d) || paused) {
X    if (!paused)
X      XNextEvent(d, &ev);
X    else
X      XWindowEvent(d,w,KeyPressMask|StructureNotifyMask,&ev);
X    switch(ev.type) {
X    case Expose:
X      if (((XExposeEvent*) &ev)->count) break;
X      placewindow();
X      mouse_posn.x = ev.xexpose.x;
X      mouse_posn.y = ev.xexpose.y;
X      *key = 'R';
X      *posn = mouse_posn;
X      return 1;
X    case UnmapNotify:
X      paused = 1;
X      break;
X    case MapNotify:
X      paused = 0;
X      break;
X    case KeyPress:
X      early_return = False;
X      if (XLookupString(&ev.xkey,&keystr,1,(KeySym *) 0,
X                        (XComposeStatus *) 0) == 1)
X        switch (keystr) {
X        case 'p': case 'P':
X          paused = 1;
X          break;
X        case 'c': case 'C':
X          paused = 0;
X          break;
X        case 'i': case 'I': case ' ':
X          XIconifyWindow(d, w, d->default_screen);
X          break;
X        case 'r': case 'R':
X          placewindow();
X          *key = 'R';
X          early_return = True;
X          break;
X        case '\003': case 'q': case 'Q':
X          *key = 'Q';
X          early_return = True;
X          break;
X        default:
X          break;
X        }
X      if (early_return) {
X        mouse_posn.x = ev.xkey.x;
X        mouse_posn.y = ev.xkey.y;
X        *posn = mouse_posn;
X        return 1;
X      }
X      break;
X    case MotionNotify:
X      mouse_posn.x = ev.xmotion.x;
X      mouse_posn.y = ev.xmotion.y;
X      break;
X    case ButtonPress:
X      mouse_posn.x = ev.xbutton.x;
X      mouse_posn.y = ev.xbutton.y;	    
X      *key = ev.xbutton.button + 'a' - 1;
X      flag[ev.xbutton.button]=1;
X      *posn = mouse_posn;
X      return 1;
X    case ButtonRelease:
X      mouse_posn.x = ev.xbutton.x;
X      mouse_posn.y = ev.xbutton.y;
X      *key = ev.xbutton.button + 'A' - 1;
X      flag[ev.xbutton.button]=0;
X      *posn = mouse_posn;
X      return 1;
X    }
X  }
X  *posn = mouse_posn;
X  if(flag[1]&&flag[3]) {
X    mouse_posn.x = ev.xkey.x;
X    mouse_posn.y = ev.xkey.y;
X    *key = 'Q';
X    *posn = mouse_posn;
X    return 1;
X  }
X  return 0;
X}
X
Xvoid gprsetcursorposition(posn)
X     position_t *posn;
X{
X  XWarpPointer (d, None, w, 0, 0, 0, 0,
X                posn->x, posn->y);
X}
X
Xvoid gprsetcolormap()
X{
X  printf("setcolormap\n");
X}
X
Xvoid gprloadfontfile(path, fontid)
X     char *path;
X     long *fontid;
X{
X  int i;
X  
X  static struct {
X    char *gprname;
X    char *xname;
X  } fontmap[] = { {"f5x9", "6x10" },
X                    {"i.12", "-*-*-medium-i-*-*-14-*-*-*-*-*-*-*"},
X                    {"f7x13", "8x13" },
X                    {"bzonefont", "-*-*-medium-r-*-*-24-*-*-*-*-*-*-*"},
X                    {0, 0 }};
X  
X  for (i=0; fontmap[i].gprname; i++) 
X    if (strcmp(path, fontmap[i].gprname) == 0) {
X      *fontid = XLoadFont (d, fontmap[i].xname);
X      return;
X    }
X  *fontid = XLoadFont(d, "fixed");
X}
X
Xvoid gprsettextvalue(pixel)
X     long pixel;
X{
X  XSetForeground(d, TextGC, colortrans[pixel]);
X}
X
Xvoid gprsettextbackgroundvalue(pixel)
X     long pixel;
X{
X  XSetBackground(d, TextGC, colortrans[pixel]);
X}
X
Xvoid gprsetfillvalue(pixel)
X     long pixel;
X{
X  XSetForeground(d, FillGC, colortrans[pixel]);
X}
X
Xvoid gprsetdrawvalue(pixel)
X     long pixel;
X{
X  XSetForeground(d, DrawGC, colortrans[pixel]);
X}
X
Xvoid gprcircle(center, radius)
X     position_t *center;
X     long radius;
X{
X  XDrawArc (d, w, DrawGC,
X            center->x - radius, center->y - radius,
X            radius+radius, radius+radius, 0, 360*64);
X}
X
Xvoid gprcirclefilled(center, radius)
X     position_t *center;
X     long radius;
X{
X  XFillArc (d, w, DrawGC,
X            center->x - radius, center->y - radius,
X            radius+radius, radius+radius, 0, 360*64);
X}
X
Xvoid gprsetclipwindow(window)
X     window_t *window;
X{
X  clipr.x = window->base.x;
X  clipr.y = window->base.y;
X  clipr.width = window->size.x;
X  clipr.height =  window->size.y;
X}
X
Xvoid clearentirescreen()
X{
X  XClearWindow(d, w);
X}
E!O!F! xstrek/original_code/c_gpr.c
echo xstrek/original_code/gpr.c 1>&2
sed -e 's/^X//' > xstrek/original_code/gpr.c <<'E!O!F! xstrek/original_code/gpr.c'
X
X/*
X * emulate gpr/ftn on top of X11.
X *
X * this is a gross crock, and I make no apologies for it..
X */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/time.h>
X#include <X11/Xlib.h>
X#include <X11/Xutil.h>
X
Xunsigned long colortrans[15];
X
Xtypedef long bitmap_desc_t;
Xtypedef unsigned long plane_t;
Xtypedef unsigned long raster_op_t;
Xtypedef long coord_t;
Xtypedef long pixel_t;
X
Xtypedef struct {
X    coord_t x, y;
X} position_t;
X
Xtypedef struct {
X    coord_t x, y;
X} offset_t;
X
Xtypedef struct {
X    coord_t x_coord_l, x_coord_r, y_coord;
X} horiz_seg_t;
X
Xtypedef struct {
X    horiz_seg_t top, bot;
X} trap_t;
X
Xtypedef struct {
X    position_t base;
X    offset_t size;
X} window_t;
X
XDisplay *d;
XWindow w;
XGC FillGC, DrawGC, TextGC, EraseGC;
X
X#define MAX_COLORS 16
X
Xvoid gprinqconfig_ (config, status)
X    long *config;
X    long *status;
X{
X    XSetWindowAttributes attr;
X    XColor color;
X    Cursor c;
X    Font f;
X    XGCValues xgcv;
X    unsigned long fcolor, bcolor;
X    Colormap cmap;
X    XColor exact, closest;
X    int i, ncolors = MAX_COLORS;
X
X    /* These colors chosen at random */
X    static char *ColorName[] = {"Black", "White", "Red", "Blue","Yellow",
X	"Green", "Orange", "Sea Green", "Coral", "Maroon",
X	"Orange Red", "Sky Blue", "Violet Red", "Brown", "Lime Green", "Gold"};
X    
X    d = XOpenDisplay (0);
X    if (d == 0) {
X	printf("can't open display! bye.\n");
X	exit(1);
X    }
X
X    colortrans[1] = fcolor = WhitePixelOfScreen(DefaultScreenOfDisplay(d));
X    colortrans[0] = bcolor = BlackPixelOfScreen(DefaultScreenOfDisplay(d));
X    
X    *config = DisplayPlanes(d,0);
X    if (*config != 1) {
X	cmap = XDefaultColormap(d,0);
X	for (i = 0; i < MAX_COLORS; i++) {
X		if (!XParseColor(d, cmap, ColorName[i], &exact)) {
X			fprintf(stderr,"XParseColor: color name %s \
X			is not in database\n", ColorName[i]);
X			exit(1);
X		}
X		if (!XAllocColor(d,cmap, &exact)) {
X			fprintf(stderr, "XAllocColor: Couldn't allocate %s\n",
X				ColorName[i]);
X			exit(1);
X		}
X		colortrans[i] = exact.pixel;
X	}
X    }
X
X    
X    w = XCreateSimpleWindow (d, DefaultRootWindow(d),
X			     0, 0,
X			     WidthOfScreen(DefaultScreenOfDisplay(d))-4,
X			     HeightOfScreen(DefaultScreenOfDisplay(d))-4,
X			     2,
X			     fcolor,
X			     bcolor);
X    attr.override_redirect = True;
X    XChangeWindowAttributes (d, w, CWOverrideRedirect, &attr);
X    xgcv.background = bcolor;
X    xgcv.foreground = fcolor;
X    FillGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X    xgcv.background = bcolor;
X    xgcv.foreground = bcolor;
X    EraseGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X    xgcv.background = bcolor;
X    xgcv.foreground = fcolor;
X    DrawGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X    TextGC = XCreateGC(d, w, GCForeground | GCBackground, &xgcv);
X    f = XLoadFont (d, "fixed");
X    c = XCreateGlyphCursor (d, f, f, ' ', ' ',
X			    &color, &color);
X    XDefineCursor(d, w, c);
X    XSetGraphicsExposures (d, FillGC, False);
X    XSetGraphicsExposures (d, DrawGC, False);
X    XSetGraphicsExposures (d, TextGC, False);    
X    *status = 0;
X}
X
Xvoid gprinit_ (flag, xx, size, yy, bitmapdesc, status)
X    long *status;
X{
X    XEvent ev;
X    XSelectInput(d, w, ExposureMask|PointerMotionMask|
X		 ButtonPressMask|ButtonReleaseMask|KeyPressMask);
X    XMapWindow(d, w);
X    XWindowEvent(d, w, ExposureMask, &ev);
X#if 0
X    while (XGrabPointer (d, w, False,
X			 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
X			 GrabModeAsync,
X			 GrabModeAsync,
X			 w, None, ev.xmotion.time) != GrabSuccess) {
X	printf("couldn't grab cursor.. waiting 1 second..\n");
X	sleep(1);
X    }
X    printf("cursor grabbed..\n");
X#endif
X    XGrabServer(d);
X    *status = 0;		/* XXX most args */
X}
X
Xvoid gprterminate_ (flag, status)
X    long *status;
X{
X    XUngrabServer(d);
X    XUngrabPointer(d, CurrentTime);
X    XCloseDisplay(d);
X    *status = 0;
X}
X
Xvoid caldecodelocaltime_ (array)
X    long *array;
X{
X    time_t clock;
X    struct tm *lt;
X
X    (void) time(&clock);
X    lt = localtime(&clock);
X    array[0] = lt->tm_year + 1900;
X    array[1] = lt->tm_mon;
X    array[2] = lt->tm_mday;
X}
X
Xvoid gprsettextfont_ (font, status)
X    long *font;
X    long *status;
X{
X    /* XXX fill in font here */
X    XSetFont(d, TextGC, *font);
X    *status = 0;
X}
X    
XXPoint curpoint;
XXRectangle clipr;
X
Xvoid gprmove_ (x, y, status)
X    long *x;
X    long *y;
X    long *status;
X{
X    curpoint.x = *x;
X    if (curpoint.x == 0)
X	curpoint.x = *(long *)x;
X    curpoint.y = *y;
X    if (curpoint.y == 0)
X	curpoint.y = *(long *)y;
X    *status = 0;
X}
X
Xvoid gprtext_ (string, nchars, status)
X    char *string;
X    long *nchars;
X    long *status;
X{
X    int count = *nchars;
X    if (count == 0)
X	count = *(long *)nchars;
X#if 0
X    printf("%d: (%.*s) show at %d, %d\n", count, count, string,
X	   curpoint.x, curpoint.y);
X#endif
X    XDrawImageString (d, w, TextGC, curpoint.x, curpoint.y, string, count);
X    *status = 0;
X}
X
X/* polyline does a series of lines, starting with the cp */
X
Xvoid gprpolyline_(x, y, number, status)
X    long *x;
X    long *y;
X    long *number;
X    long *status;
X{
X    int i, count;
X    XPoint *points;
X    *status = 0;
X    count = *number;
X
X    /*
X     * gross hack.  we may be passed an integer*2 or an integer*4;
X     * neither is likely to be greater than 2<<16
X     */
X    if (count == 0)
X	count = *((long *)number);
X	
X    points = (XPoint *) malloc (sizeof(XPoint) * (count + 1));
X    points[0] = curpoint;
X    for (i=0; i<count; i++) {
X	points[i+1].x = x[i];
X	points[i+1].y = y[i];
X    }
X    curpoint = points[count];
X    XDrawLines(d, w, DrawGC, points, count+1, CoordModeOrigin);
X    free(points);
X}
X
X/* "multiline draws a series of alternate moves and lines" */
X
Xvoid gprmultiline_(x, y, number, status)
X    long *x;
X    long *y;
X    long *number;
X    long *status;
X{
X    int i, count;
X    XSegment *segments;
X    *status = 0;
X    count = *number;
X    /*
X     * gross hack.  we may be passed an integer*2 or an integer*4;
X     * neither is likely to be greater than 2<<16
X     */
X    if (count == 0)
X	count = *((long *)number);
X
X    if (count & 1)
X	printf("odd multiline??\n");
X    /* not exactly what you want.. */
X    segments = (XSegment *)malloc (sizeof(XSegment) * (count/2));
X    for (i=0; i<count; i+= 2) {
X	segments[i/2].x1 = x[i];
X	segments[i/2].y1 = y[i];
X	segments[i/2].x2 = x[i+1];
X	segments[i/2].y2 = y[i+1];
X    }
X    curpoint.x = segments[count/2].x2;
X    curpoint.y = segments[count/2].y2;    
X    XDrawSegments (d, w, DrawGC, segments, count/2);
X    free(segments);
X}
X
Xstatic long lop;
Xvoid gprbitblt_ (bitmap, window, plane, dsto, dstp, status)
Xbitmap_desc_t *bitmap;
Xwindow_t *window;
Xplane_t *plane;
Xposition_t *dsto;
Xplane_t *dstp;
Xlong *status;
X{
X  
X    if(lop==6) XFillRectangle(d,w,EraseGC,dsto->x, dsto->y,window->size.x, window->size.y);
X    else {
X	XCopyArea (d, w, w, FillGC,
X		window->base.x, window->base.y,
X		window->size.x, window->size.y,
X		dsto->x, dsto->y);
X    }
X    *status = 0;
X}
X
X
Xvoid gprsetrasterop_ (plane, op, status)
X    plane_t *plane;
X    raster_op_t *op;
X    long *status;
X{
X    lop = *op;
X#if 0
X    
X#endif
X    /*
X     * When we invert, invert only one bit.
X     */
X    if (lop == 10) {
X	XSetPlaneMask(d, FillGC, 0x00000001);
X	XSetPlaneMask(d, TextGC, 0x00000001);
X    }
X    else {
X	XSetPlaneMask(d, FillGC, 0xffffffff);
X	XSetPlaneMask(d, TextGC, 0xffffffff);
X    }
X	
X    XSetFunction(d, FillGC, lop);
X    XSetFunction(d, TextGC, lop);
X#if 0
X    XSetFunction(d, DrawGC, *op);
X#endif
X}
X
Xvoid gprtrapezoid_ (trap, status)
X    trap_t *trap;
X    long *status;
X{
X    XPoint tr[4];
X    tr[0].x = trap->top.x_coord_l;
X    tr[0].y = trap->top.y_coord;
X    tr[1].x = trap->top.x_coord_r;
X    tr[1].y = trap->top.y_coord;    
X    tr[2].x = trap->bot.x_coord_r;
X    tr[2].y = trap->bot.y_coord;    
X    tr[3].x = trap->bot.x_coord_l;
X    tr[3].y = trap->bot.y_coord;    
X    XFillPolygon (d, w, FillGC, tr, 4, Convex, CoordModeOrigin);
X}
X
Xvoid gprsetclippingactive_ (flag, status)
X    long *flag;
X    long *status;
X{
X    if (*flag) {
X	XSetClipRectangles (d, FillGC, 0, 0, &clipr, 1, YXBanded);
X	XSetClipRectangles (d, DrawGC, 0, 0, &clipr, 1, YXBanded);
X	XSetClipRectangles (d, TextGC, 0, 0, &clipr, 1, YXBanded);
X    } else {
X	XSetClipMask (d, FillGC, None);
X	XSetClipMask (d, DrawGC, None);
X	XSetClipMask (d, TextGC, None);		
X    }
X}
X
Xvoid tonetime_ (time)
X    long *time;
X{
X    XBell(d, 0);
X}
X
Xvoid timeclock_ (clock)
X    long *clock;
X{
X    struct timeval tv;
X
X    XSync(d,0);
X    gettimeofday(&tv, 0);
X
X    clock[0] = tv.tv_sec >> 16;
X    clock[1] = tv.tv_sec & 0xffff;
X    clock[2] = tv.tv_usec/4;
X}
X
Xvoid timewait_ (delta, clock, status)
X    long *clock;
X    long *status;
X{
X    struct timeval tv;
X    XSync(d, 0);
X    tv.tv_sec = ((clock[0] <<16) + clock[1])>>1;
X    tv.tv_usec = clock[2] * 4;
X    select (0, 0, 0, 0, &tv);
X}
X
Xvoid calgetlocaltime_(clock)
X    long *clock;
X{
X    struct timeval tv;
X    gettimeofday(&tv, 0);
X
X    clock[0] = tv.tv_sec >> 16;
X    clock[1] = tv.tv_sec & 0xffff;
X    clock[2] = tv.tv_usec/4;
X}
X
Xposition_t mouse_posn;
X
Xvoid gprinqcursor_ (cursor, ops, active, posn, origin, status)
Xlong *active;
Xposition_t *posn;
Xposition_t *origin;
Xlong *status;
X{
X    *posn = mouse_posn;
X    *status = 0;
X}
X
Xint gprcondeventwait_ (event, key, posn, status)
Xchar *key;
Xposition_t *posn;
Xlong *status;
X{
X    static unsigned long flag[16]={0,0,0,0};
X    XEvent ev;
X    XFlush(d);
X    while (XPending(d)) {
X	XNextEvent(d, &ev);
X	switch(ev.type) {
X	    case MotionNotify:
X		mouse_posn.x = ev.xmotion.x;
X		mouse_posn.y = ev.xmotion.y;
X		break;
X
X	    case ButtonPress:
X		mouse_posn.x = ev.xbutton.x;
X		mouse_posn.y = ev.xbutton.y;	    
X		*key = ev.xbutton.button + 'a' - 1;
X		flag[ev.xbutton.button]=1;
X		*posn = mouse_posn;
X		return 1;
X
X	    case ButtonRelease:
X		mouse_posn.x = ev.xbutton.x;
X		mouse_posn.y = ev.xbutton.y;
X		*key = ev.xbutton.button + 'A' - 1;
X		flag[ev.xbutton.button]=0;
X		*posn = mouse_posn;
X		return 1;
X	    case KeyPress:
X		{
X		    XKeyEvent *keyEvent = (XKeyEvent *) &ev;
X		    char buf[128];
X		    KeySym ks;
X		    XComposeStatus status;
X		    buf[0] = '\0';
X
X		    XLookupString(keyEvent,buf,128,&ks,&status);
X		    switch (buf[0]) {
X		    case 'q':
X		    case 'Q':
X			exit(0);
X			break;
X		    default:
X			return 0;
X		    }
X		}
X		return 0;
X	    }
X    }
X    *posn = mouse_posn;
X    if(flag[1]&&flag[3]) exit(0);
X    return 0;
X}
X
Xvoid calfloatclock_ (clock, floatclock)
X    long *clock;
X    double *floatclock;
X{
X    *floatclock = (double) (clock[0] * 65536) +
X	(double) (clock[1]) +
X	    ((double) (clock [2]) / (1000000/4));
X}
X
Xvoid gprsetcursorposition_ (posn, status)
X    position_t *posn;
X    long *status;
X{
X    XWarpPointer (d, None, w, 0, 0, 0, 0,
X		  posn->x, posn->y);
X}
X
Xvoid gprsetcolormap_ (start, nentries, values, status)
X    pixel_t *start;
X    long *nentries;
X    long *values;
X    long *status;
X{
X	Colormap cmap;
X	XColor exact, closest;
X}
X
Xvoid gprloadfontfile_ (path, pnlen, fontid, status)
X    char *path;
X    long *pnlen;
X    long *fontid;
X    long *status;
X{
X    int i;
X   
X    static struct {
X	char *gprname;
X	char *xname;
X    } fontmap[] = { {	"/sys/dm/fonts/f5x9", "6x10" },
X      {     "/sys/dm/fonts/i.12", "-*-*-medium-i-*-*-14-*-*-*-*-*-*-*"},
X      {	    "/sys/dm/fonts/f7x13", "8x13" },
X      {     "bzonefont", "-*-times-medium-r-*-*-24-*-*-*-*-*-*-*"},
X	  {     0, 0 }};
X
X    for (i=0; fontmap[i].gprname; i++) {
X	if (strcmp(path, fontmap[i].gprname) == 0) {
X	    *fontid = XLoadFont (d, fontmap[i].xname);
X	    return;
X	}
X    }
X    *fontid = XLoadFont(d, "fixed");
X}
X
Xvoid gprsettextpath_ (direction, status)
X    long *status;
X{
X    /*XXX noop */
X    *status = 0;
X}
X
X
Xvoid gprsettextvalue_ (pixel, status)
X    long *pixel;
X    long *status;
X{
X    XSetForeground(d, TextGC, colortrans[*pixel]);
X    *status = 0;
X}
X
Xvoid gprsettextbackgroundvalue_ (pixel, status)
X    long *pixel;
X    long *status;
X{
X    XSetBackground(d, TextGC, colortrans[*pixel]);
X    *status = 0;
X}
X
Xvoid gprsetfillvalue_ (pixel, status)
Xlong *pixel;
Xlong *status;
X{
X    XSetForeground(d, FillGC, colortrans[*pixel]);
X    *status = 0;
X}
X
X
Xvoid gprsetdrawvalue_ (pixel, status)
X    long *pixel;
X    long *status;
X{
X    XSetForeground(d, DrawGC, colortrans[*pixel]);
X    *status = 0;
X}
X
Xvoid gprcircle_ (center, radius, status)
X    position_t *center;
X    long *radius;
X    long *status;
X{
X    long size = *radius;
X    long moon = 1;
X    if (moon) {
X	if ((strncmp(ServerVendor(d), "MIT", 3) == 0) &&
X	    VendorRelease(d) < 4)
X	    return;
X    }
X    XDrawArc (d, w, DrawGC,
X	      center->x - size, center->y - size,
X	      size+size, size+size, 0, 360*64);
X}
X
Xvoid gprcirclefilled_ (center, radius, status)
X    position_t *center;
X    long *radius;
X    long *status;
X{
X    long size = *radius;
X
X    XFillArc (d, w, DrawGC,
X	      center->x - size, center->y - size,
X	      size+size, size+size, 0, 360*64);
X}
X
Xvoid gprenableinput_ (event_type, key_set, status)
X    long *status;
X{
X    /* XXX noop */
X}
X
Xvoid gprsetclipwindow_ (window, status)
X    window_t *window;
X    long *status;
X{
X    clipr.x = window->base.x;
X    clipr.y = window->base.y;
X    clipr.width = window->size.x;
X    clipr.height =  window->size.y;
X}
X
Xvoid rand_ (x)
X    float *x;
X{
X    unsigned long r = random();
X    double dr = r % 566927;
X    
X    *x = dr / 566927;
X}
X
X#include <pwd.h>
Xvoid
Xgetusername_ (name, namlen)
Xchar *name;
Xlong *namlen;
X{
X    struct passwd *pwd;
X    char *tname;
X    int i;
X    extern char *getenv();
X    extern struct passwd *getpwuid();
X
X    if ((pwd = getpwuid(getuid())) != NULL) {
X	tname = pwd->pw_name;
X	(void) strncpy(name, tname, sizeof(pwd->pw_name));
X	name[sizeof(pwd->pw_name)] == '\0';
X    }
X    else if ((tname = getenv("USER")) == NULL && 
X	     (tname = getenv("LOGNAME")) == NULL)
X	tname = "intruder";
X    (void) strncpy(name, tname, 9);
X    name[9] = '\0';
X}
E!O!F! xstrek/original_code/gpr.c
exit
=====
            @work:            | Matthias Pfuetzner  |         @home:
  ZGDV, Wilhelminenstrasse 7  | 6100 Darmstadt, FRG |  Lichtenbergstrasse 73
    +49 6151 155-164 or -101  \    <- Tel.nr. ->    /     +49 6151 75717
   pfuetzner@agd.fhg.de    pfuetzner@zgdvda.UUCP    XBR1YD3U@DDATHD21.BITNET

--
Dan Heller
------------------------------------------------
O'Reilly && Associates		ZipCode Software
Senior Writer			       President
argv@ora.com			argv@zipcode.com