/**************************************************************************/ /* * CSE 142 Graphics Package * Port to X windows. * Implementation Status: Incomplete * This file contains all of the X specific calls. * * Author: Ben Dugan * Modified: Jim Fix */ /**************************************************************************/ #include #include #include #include #include #include #include #include #include "GPKernel.h" using namespace std; /**************************************************************************/ /* * Global Vars. */ struct timeval time0; static GC gc; static Visual *visual; static Window window; static Display *dpy; static int screen; static Pixmap pix; static int window_x; static int window_y; /* * colorTable maps GP142 color indeces -> X color names, which are * entries in the X color database */ static char *colorTable[MAX_COLORS] = { "black", "white", "red", "green", "blue", "yellow", "magenta", "cyan", "purple", "navy", "LightSlateBlue", "DarkSlateBlue", "turquoise", "orange", "brown", "pink", "gray80", "gold", "peach puff", "ForestGreen", "SeaGreen", "DarkOliveGreen", "gray40", "gray90" }; /* * pixTable maps GP142 color indeces -> X colormap pixel values */ unsigned long pixTable[MAX_COLORS]; /* * fontTable maps GP142 point values -> X font names */ static char *fontTable[25] = { "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x10", "6x12", "6x13", "7x14", "9x15", "9x15", "9x15", "9x15", "9x15", "10x20", "10x20", "10x20", "12x24", "12x24" }; /**************************************************************************/ /* * Random helper routines. */ /* * This code is due to: Mark Lillibridge * * Resolve_Color: This routine takes a color name and returns the pixel # * that when used in the window w will be of color name. * (WARNING: The colormap of w MAY be modified! ) * If colors are run out of, only the first n colors will be * as correct as the hardware can make them where n depends * on the display. This routine does not require wind to * be defined. */ unsigned long Resolve_Color(Window w, char *name) { XColor c_rgb, c_actual, c; Colormap colormap; XWindowAttributes wind_info; if (!strcmp(name, "white")) name="#ffffffffffff"; if (!strcmp(name, "black")) name="#000000000000"; XGetWindowAttributes(dpy, w, &wind_info); colormap = wind_info.colormap; if (!XParseColor(dpy, colormap, name, &c)) fprintf(stderr, "%s: Bad color name\n", name); else if (!XAllocColor(dpy, colormap, &c)) fprintf(stderr, "%s: Couldn't allocate color\n", name); return(c.pixel); } /* * setUpcolors initializes the pixTable with X color map pixel values * corresponding to GP142 color indeces */ int setUpColors () { int i; for (i=0; ifid); height = f->ascent + f->descent; } len = strlen(str); first = 0; for (i=0; itype = KEYPRESS; event->ch = buffer[0]; return KEYPRESS; } else { /* fprintf(stderr, "*** Unprintable %x ***\n", keysym); */ break; } case ButtonPress: /* fprintf(stderr, ".Button\n"); */ event->type = MOUSE; event->x = report.xbutton.x; event->y = report.xbutton.y; /* windowToCartesian(&(event->x), &(event->y));*/ /* fprintf(stderr, "%d %d\n", event->x, event->y); */ return MOUSE; default: /* fprintf(stderr, ".Strange\n"); fprintf(stderr, "Strange Event\n"); */ event->type = PERIODIC; return PERIODIC; } } } event->type = PERIODIC; return PERIODIC; } void initWindow (Window *win, GC *gc, Visual **v, char *title, int w, int h) { XGCValues values; window_x = w; window_y = h; *win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), 0, 0, w, h, 5, BlackPixel(dpy, screen), WhitePixel(dpy, screen)); /* new */ pix = XCreatePixmap(dpy, *win, w, h, DefaultDepth(dpy,screen)); *gc = XCreateGC(dpy, pix /* used to be *win */, 0, &values); XStoreName(dpy, *win, title); *v = DefaultVisual(dpy, screen); XSetClipMask(dpy, *gc, None); clear(); XGetGCValues(dpy, *gc, GCFunction|GCPlaneMask|GCForeground|GCBackground|GCLineWidth| GCLineStyle|GCClipXOrigin|GCClipYOrigin, &values); } int openPackage (int width, int height) { struct timezone tz; dpy = XOpenDisplay(NULL); if (dpy == NULL) { fprintf(stderr, "Unable to open display\n"); exit(0); } screen = DefaultScreen(dpy); initWindow(&window, &gc, &visual, "Test", width,height); XSelectInput(dpy, window, ExposureMask|ButtonPressMask|KeyPressMask); XMapWindow(dpy, window); XFlush(dpy); setUpColors(); gettimeofday(&time0, &tz); clear(); return 1; } /* * close: this should do some more cleanup... */ int closePackage (void) { return 1; }