/* -------------------------------------------------------------------- * * Module: X.c * * Purpose: X Windows device driver for X workstations. * It allows output to an X win * * ----------------------------------------------------------------- */ #include #include #include #include #include #include #include #include "X.h" #define borderwidth 5 #define TRUE 1 #define FALSE 0 #define grey 1 #define XRGBColour(r,g,b) (long)((b) << 16) | ((g) << 8) | (r) #define BadX(x) ( ((x)<0) || ((x)>userResX) ) #define BadY(y) ( ((y)<0) || ((y)>userResY) ) /* Define variables for win's dpy and screen */ static Display *dpy; static Window win; static GC gc; static Colormap cmap; static Visual *vis; static int screen; static int BW; /* colour or black and white display? */ #define NUMCOLOURS 11 static unsigned long colours[NUMCOLOURS]; static Colour cmap_colours[NUMCOLOURS] = { { 0, 0, 0 }, { 255, 255, 255 }, { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 }, { 255, 255, 0 }, { 0, 255, 255 }, { 255, 0, 255 }, { 250, 130, 80 }, { 30, 230, 250 }, { 230, 30, 250 }, }; /* Variables for height and width of Xwindow */ static unsigned heightm1; static unsigned height; static unsigned width; static unsigned widthreal; static unsigned pixelSize; static unsigned screenSize; static unsigned userResX; static unsigned userResY; /* ------------------------------------------ Local Prototypes ----- */ static void SetCmap(); static unsigned long GetCmapColour(); /* ------------------------------------------ Global Functions ----- */ /* XOpen, open an XWindow or update window when a size change occurs */ void XOpen(int resx, int resy, int gargc, char *gargv[]) { char *dpyname; XSizeHints size_hints; int x=0,y=0,x_hot,y_hot,foo; XSetWindowAttributes xswa; XGCValues gcValue; XVisualInfo vTemplate, *visualList; int visualsMatched; unsigned long valuemask=0; if ( (resx > 0) && (resx <= MAXX) ) userResX = resx; else { fprintf(stderr, "Bad X resolution: %d. Defaulting to %d.\n", resx, MAXX); userResX = DEFAULTX; } if ( (resy > 0) && (resy <= MAXY) ) userResY = resy; else { fprintf(stderr, "Bad Y resolution: %d. Defaulting to %d.\n", resy, MAXY); userResY = DEFAULTY; } height=resy; heightm1=height-1; width=resx; widthreal=((width+7)/8)*8; dpyname=getenv("DISPLAY"); /* printf("opening %d x %d display for %s using display %s\n", width, height, gargv[0], dpyname); */ if (dpyname==NULL) dpy=XOpenDisplay(":0"); else dpy=XOpenDisplay(dpyname); if (dpy==NULL) { fprintf(stderr,"I can't open the dpy.\n\n"); fprintf(stderr,"Is the DISPLAY environment variable set?.\n"); fprintf(stderr,"Do you own the dpy?\n\nExiting\n\n"); exit(-1); } /* Get the default screen */ screen = DefaultScreen(dpy); win = XDefaultRootWindow(dpy); vTemplate.screen = screen; vTemplate.depth = 8; vTemplate.class = PseudoColor; visualList = XGetVisualInfo(dpy, VisualScreenMask|VisualClassMask|VisualDepthMask, &vTemplate, &visualsMatched); if ( visualsMatched == 0 ) { BW = TRUE; /* assume BW display */ vis = DefaultVisual( dpy, screen ); } else { BW = FALSE; /* assume colour */ vis = visualList[0].visual; } XFree((char *)visualList); SetCmap(); /* Set the X Event mask so that an exposure event is generated when the window is "uncovered" */ xswa.event_mask=ExposureMask; /* Set background pixel */ xswa.background_pixel= BlackPixel(dpy,screen); /* Set the colourmap */ /* xswa.colormap=cmap; */ xswa.colormap= cmap; size_hints.flags= PPosition | PSize | PMinSize; size_hints.x=x; size_hints.y=y; size_hints.width=width; size_hints.height=height; size_hints.min_width=8; size_hints.min_height=8; win=XCreateWindow(dpy,RootWindow(dpy,screen), x,y,width,height,borderwidth, CopyFromParent,InputOutput,vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap,&xswa); XSetStandardProperties(dpy,win,gargv[0],gargv[0],None, gargv,gargc,&size_hints); /* Select event types wanted */ XSelectInput(dpy,win,ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask); gcValue.line_style = LineSolid; gcValue.function = GXcopy; gcValue.background = BlackPixel(dpy,screen); gcValue.foreground = WhitePixel(dpy,screen); gc=XCreateGC(dpy,win, GCForeground | GCBackground | GCFunction | GCLineStyle, &gcValue); XMapWindow(dpy,win); /* GetEvent(); */ XSync(dpy,TRUE); } DisplayScreen() { _XFlushGCCache(dpy, gc); XFlush(dpy); } ClearScreen() { XClearWindow(dpy, win); } /* Sets the default colourmap. */ static void SetCmap() { int i; /* Inherit the default colourmap */ cmap= XDefaultColormap(dpy,screen); /* Set up a translation table to go from our colours to X's */ for (i=0;i