home *** CD-ROM | disk | FTP | other *** search
- #ifdef INCLUDE_X_WINDOWS
- #include "Kernel/h/system.h"
- #include "Kernel/h/macros.h"
- #include "Kernel/h/assert.h"
-
- #include <sys/file.h>
- #include <sys/time.h>
- #include <errno.h>
-
- #include "Kernel/h/stdTypes.h"
- #include "Kernel/h/ecTypes.h"
- #include "Kernel/h/emTypes.h"
- #include "Kernel/h/builtins.h"
- #include "Kernel/h/kmdTypes.h"
- #include "Kernel/h/emeraldTypes.h"
- #include "Kernel/h/kEvents.h"
- #include "Kernel/h/sigio.h"
- #undef True
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- static Display *display;
- static EmBitChunk *waitingEvent;
- static SSPtr waitingProcess;
- extern SSPtr preemptRunning();
- XWMHints xwmh={
- (InputHint|StateHint),
- False,
- NormalState,
- 0,
- 0,
- 0,0,
- 0,
- 0
- };
-
- /* shumei -s */
- static unsigned long black,white;
- /* shumei -e */
-
- int XSIGIOOccurred(), XSockDead(), XEventHandler();
-
- void *XCreateDisplay(es)
- EmString *es;
- {
- char *s;
-
- s = malloc(es->sizeInBytes + 1);
- (void) strncpy(s, es->data, es->sizeInBytes);
- display = XOpenDisplay(s);
- if (display == NULL) {
- KMDTrace("X", 1, "Cannot initialize display\n");
- return;
- }
- if (fcntl(display->fd, F_SETFL, FASYNC ) < 0) {
- perror("XCreateDisplay: first fcntl");
- };
- if (fcntl(display->fd, F_SETOWN, getpid()) < 0) {
- perror("XCreateDisplay: second fcntl");
- };
-
- SISetSockHandler(display->fd, SIREAD, (SIHandlerPtr) XSIGIOOccurred);
- SISetSockHandler(display->fd, SIEXCEPT, (SIHandlerPtr) XSockDead);
- SIEnsureSIGIO();
-
- black = BlackPixel(display,DefaultScreen(display));
- white = WhitePixel(display,DefaultScreen(display));
- }
-
- int XSIGIOOccurred(fKind, fSock)
- int fKind, fSock;
- {
- SIRemoveSockHandler(fSock, SIREAD);
- HoldSigs();
- QueueTask(XEventHandler, (char *) fSock);
- ReleaseSigs();
- return 0; /* procedure ought to be void */
- }
-
-
- /**********************************************************************/
- int XSockDead(fSock)
- int fSock;
- {
- KMDTrace("X", 3, "Socket #%d died.\n", fSock);
- display = NULL;
- }
-
- /**********************************************************************/
- /* XEventHandler
- /**********************************************************************/
- int XEventHandler(fSock)
- int fSock;
- /* The socket has become available for input, read the event */
- {
- SSPtr theProcess;
-
- KMDTrace("X", 3, "Events on socket %d\n", fSock);
-
- /* Attempt to read */
- if (XPending(display) <= 0) {
- /* Must have been XError events */
- KMDTrace("X", 5, "XNextEvent found no events\n");
- SISetSockHandler(display->fd, SIREAD, (SIHandlerPtr) XSIGIOOccurred);
- return;
- }
- XNextEvent(display, (XEvent *)waitingEvent->data);
- KMDTrace("X", 5, "XNextEvent returned\n");
- KMDTrace("X", 5, "event = (%d %d %d %d %d)\n",
- *(0 + (int *)waitingEvent->data),
- *(1 + (int *)waitingEvent->data),
- *(2 + (int *)waitingEvent->data),
- *(3 + (int *)waitingEvent->data),
- *(4 + (int *)waitingEvent->data));
- theProcess = waitingProcess;
- theProcess->resultBrand = DataBrand;
- theProcess->regs.arg1 = 1;
- waitingProcess = (SSPtr) NULL;
- waitingEvent = NULL;
- schedule(theProcess);
- }
-
- /**********************************************************************/
- /* EMXReadEvent */
- /**********************************************************************/
-
- /*Kernel Call */
- void EMXReadEvent(fEvent)
- GODP fEvent;
- {
- int sock, i, count, length;
- EmBitChunk *event;
- SSPtr theProcess;
- AbConPtr eventAbCon, OIDOIDOIDToAbCon();
-
- theProcess = preemptRunning();
-
- KMDTrace("X", 4, "EMXReadEvent in %s\n", PPSSPlace(theProcess));
-
- if (fEvent == (GODP) EMNIL) {
- KMDTrace("X", 2, "Event is NIL in EMXReadEvent.\n");
- fail(theProcess);
- return;
- }
-
- if (!fEvent->tag.isResident) {
- KMDTrace("X", 2, "Event is non-resident.\n");
- eventAbCon = OIDOIDOIDToAbCon(
- fEvent->dataPtr->myCodePtr->ownAbstractType,
- OIDOfBuiltin(B_INSTAT, ANYINDEX),
- fEvent->dataPtr->myCodePtr->ownOID);
- unavail(theProcess, fEvent, eventAbCon);
- return;
- }
-
- if (fEvent->tag.global) {
- event = (EmBitChunk *) fEvent->dataPtr;
- } else event = (EmBitChunk *) fEvent;
-
- /* Check for other readers */
- if (NonNULL(waitingProcess)) {
- KMDTrace("X", 2, "Read socket busy.\n");
- fail(theProcess);
- return;
- }
-
- /* Check the size of the bitchunk */
- if (event->sizeInBytes < sizeof(XEvent)) {
- KMDTrace("X", 2, "Read attempt with too small BitChunk %d should be %d.\n",
- event->sizeInBytes, sizeof(XEvent));
- fail(theProcess);
- return;
- }
-
- /* Check for a display */
- if (!display) {
- KMDTrace("X", 1, "Read attempt with no display (= nil).\n");
- fail(theProcess);
- return;
- }
-
- /* Attempt to read */
- if (XPending(display) <= 0) {
- KMDTrace("X", 3, "EMXReadEvent blocking %s at %s\n",
- PPPOID(theProcess->processOID), PPSSPlace(theProcess));
- waitingEvent = event;
- waitingProcess = theProcess;
- theProcess->status.rs = SSReadIOWait;
- KMDTrace("LineNumber", 3, "%s blocking on X read in %s\n",
- PPPOID(theProcess->processOID), PPSSPlace(theProcess));
- SISetSockHandler(display->fd, SIREAD, (SIHandlerPtr) XSIGIOOccurred);
- return;
- }
- XNextEvent(display, (XEvent *)event->data);
- theProcess->resultBrand = DataBrand;
- theProcess->regs.arg1 = 0;
- schedule(theProcess);
- }
-
- /* Change from here on */
-
- int EMXCreateWindow(pos, size, name)
- int pos, size;
- EmString *name;
- {
- Window window;
- XSizeHints xsh;
- char *myname;
- myname = calloc(1, name->sizeInBytes+1);
- strncpy(myname, name->data, name->sizeInBytes);
-
- if (display == NULL) {
- KMDTrace("X", 1, "Create window called with no display\n");
- return(-1);
- }
- xsh.x = pos >> 16;
- xsh.y = pos & 0xffff;
- xsh.width = size >> 16;
- xsh.height = size & 0xffff;
- xsh.flags = (USPosition|USSize);
-
- KMDTrace("X", 3, "Create window at (%d, %d) size (%d, %d)\n",
- xsh.x, xsh.y, xsh.width, xsh.height);
- window = XCreateSimpleWindow(display, DefaultRootWindow(display), xsh.x, xsh.y, xsh.width, xsh.height, 1,black,white);
- KMDTrace("X", 3, "XCreateWindow returns %d\n", window);
- /* norm, i am not sure the parameter: None should be argv, 0 should be argc
- but it works for right now */
- XSetStandardProperties(display,window,myname,myname,None,NULL,0,&xsh);
- /*
- XSetWHints(display,window,&xwhm);
- */
- XMapWindow(display,window);
- return(window);
- }
-
-
- /*-----------------------------------------------------------------------
- * FUCTION : EMXSelectInput
- * To requests X server report the events associated with the event
- * Mask
- * Jyhlin Chang Jul 15,1988
- *-----------------------------------------------------------------------*/
-
- void EMXSelectInput(w, event_mask)
- Window w;
- unsigned long event_mask;
- {
- XSelectInput(display, w, event_mask);
-
- }/*EMXSelectInput*/
-
-
-
- GC EMXInitGc(win)
- Window win;
- {
- XGCValues gcvalues;
- GC gc;
- char *font;
- XFontStruct *fontstruct;
-
- gcvalues.foreground = black;
- gcvalues.background = white;
- gcvalues.line_width = 0;
- font = "9x15";
- if ((fontstruct = XLoadQueryFont(display , font)) == NULL) {
- fprintf(stderr, "%s: display %s doesn't know font %s\n",
- "ha,wrong", DisplayString(display), font);
- exit(1);
- }
- gcvalues.font = fontstruct->fid;
- gc = XCreateGC(display,win,(GCLineWidth|GCFont|GCForeground|GCBackground),
- &gcvalues);
- return (gc);
- }
-
- EMXSetWidth(gc, win,width )
- GC gc;
- Window win;
- int width;
- {
- XGCValues gcvalues;
-
- gcvalues.line_width = width;
- XChangeGC(display,gc,GCLineWidth,&gcvalues);
- }
-
- EMXSetFont(gc,win,font)
- GC gc;
- Window win;
- EmString *font;
- {
- XGCValues gcv;
- XFontStruct *fontstruct;
- char *blotto = calloc(1, font->sizeInBytes + 1);
- strncpy(blotto, font->data, font->sizeInBytes);
- if ((fontstruct = XLoadQueryFont(display , blotto)) == NULL) {
- fprintf(stderr, "%s: display %s doesn't know font %s\n",
- "ha,wrong", DisplayString(display), blotto);
- exit(1);
- }
- gcv.font = fontstruct->fid;
- /*
- gcv.foreground = black;
- gcv.background = white;
- gc = XCreateGC(display,win,(GCFont|GCForeground|GCBackground), &gcv);
- */
- XChangeGC(display,gc,GCFont, &gcv);
- }
-
- void EMXLine(win, p1, p2,gc)
- Window win;
- int p1, p2;
- GC gc;
- {
- int x1, y1, x2, y2;
- x1 = p1 >> 16;
- y1 = p1 & 0xffff;
- x2 = p2 >> 16;
- y2 = p2 & 0xffff;
- KMDTrace("X", 3, "X Line on window %d from (%d, %d) to (%d, %d)\n",
- win, x1, y1, x2, y2);
- XDrawLine(display,win,gc,x1, y1, x2, y2);
- KMDTrace("X", 3, "XLine done\n");
- }
-
- void EMXString(win, string, x, y, gc )
- Window win;
- EmString *string;
- int x,y;
- GC gc;
- {
- KMDTrace("X", 3, "XString begin %s\n",string);
- XDrawString(display, win, gc, x, y, string->data, string->sizeInBytes);
- XFlush(display);
- KMDTrace("X", 3, "XString done\n");
- }
- void EMXConfigureWindow(win, p, s)
- int win, p, s;
- {
- int x, y, w, h;
- x = p >> 16;
- y = p & 0xffff;
- w = s >> 16;
- h = s & 0xffff;
- KMDTrace("X", 3, "X ConfigureWindow on window %d (%d, %d) (%d, %d)\n",
- win, x, y, w, h);
- XConfigureWindow(win, x, y, w, h);
- KMDTrace("X", 3, "XConfigureWindow done\n");
- }
-
- void EMXFlush()
- {
- KMDTrace("X", 3, "X Flush called\n");
- XFlush(display);
- KMDTrace("X", 3, "X Flush done\n");
- }
-
- int sync_write(fd, b, l)
- int fd, l;
- char *b;
- {
- int write_stat, nTimes = 0;
- while ((write_stat = write(fd, b, l)) < 0 && errno == EWOULDBLOCK) {
- nTimes ++;
- /* wait a while */
- }
- if (nTimes > 0) KMDTrace("X", 1, "Sync_write had to wait %d times\n");
- return(write_stat);
- }
-
- int sync_writev(fd, iov, l)
- int fd, l;
- struct iovec *iov;
- {
- int write_stat, nTimes = 0;
- while ((write_stat = write(fd, iov, l)) < 0 && errno == EWOULDBLOCK) {
- nTimes ++;
- /* wait a while */
- }
- if (nTimes > 0) KMDTrace("X", 1, "Sync_writev had to wait %d times\n");
- return(write_stat);
- }
-
- EMXRaiseWindow(w)
- Window w;
- {
- XRaiseWindow(display,w);
- }
-
-
- EMXLowerWindow(w)
- Window w;
- {
- XLowerWindow(display,w);
- }
-
-
- EMXUnmapWindow(w)
- Window w;
- {
- XUnmapWindow(display,w);
- }
-
- EMXResizeWindow(w,x,y)
- Window w;
- int x,y;
- {
- XResizeWindow(display,w,x,y);
- }
-
- EMXMoveWindow(w,x,y)
- Window w;
- int x,y;
- {
- XMoveWindow(display,w,x,y);
- }
-
- EMXCloseWindow(w)
- Window w;
- {
- XDestroyWindow(display,w);
- }
-
- void XInit()
- {
- EmString es;
- DebugMsg(1, "XInit\n");
- es.sizeInBytes = 0;
- XCreateDisplay(&es);
- KMDSetTrace(X);
- KMDSetProcedure(EMXInitGc);
- KMDSetProcedure(EMXSetFont);
- KMDSetProcedure(EMXSetWidth);
- KMDSetProcedure(EMXLine);
- KMDSetProcedure(EMXString);
- KMDSetProcedure(EMXCreateWindow);
- KMDSetProcedure(EMXUnmapWindow);
- KMDSetProcedure(EMXCloseWindow);
- KMDSetProcedure(EMXMoveWindow);
- KMDSetProcedure(EMXResizeWindow);
- KMDSetProcedure(EMXConfigureWindow);
- KMDSetProcedure(EMXRaiseWindow);
- KMDSetProcedure(EMXLowerWindow);
- KMDSetProcedure(EMXFlush);
- KMDSetProcedure(EMXReadEvent);
-
- KMDSetProcedure(EMXSelectInput);
- }
-
- #else
- void XInit()
- {}
- #endif
-