Annotation of researchv9/X11/src/X.V11R1/lib/X/XIfEvent.c, revision 1.1.1.1

1.1       root        1: #include "copyright.h"
                      2: 
                      3: /* $Header: XIfEvent.c,v 11.8 87/09/11 08:09:02 toddb Exp $ */
                      4: /* Copyright    Massachusetts Institute of Technology    1986  */
                      5: 
                      6: #define NEED_EVENTS
                      7: #include "Xlibint.h"
                      8: 
                      9: extern _XQEvent *_qfree;
                     10: 
                     11: /* 
                     12:  * Flush output and (wait for and) return the next event matching the
                     13:  * predicate in the queue.
                     14:  */
                     15: 
                     16: XIfEvent (dpy, event, predicate, arg)
                     17:        register Display *dpy;
                     18:        Bool (*predicate)();            /* function to call */
                     19:        register XEvent *event;
                     20:        char *arg;
                     21: {
                     22:        register _XQEvent *qelt, *prev;
                     23:        
                     24:        LockDisplay(dpy);
                     25:        _XFlush (dpy);
                     26:        /*
                     27:         * first must search queue to find if any events match.
                     28:         * If so, then remove from queue.
                     29:         */
                     30:        prev = NULL;
                     31:        qelt = dpy->head;
                     32:        while (qelt) {
                     33:            if ((*predicate)(dpy, &qelt->event, arg)) {
                     34:                *event = qelt->event;
                     35:                if (prev) {
                     36:                    if ((prev->next = qelt->next) == NULL)
                     37:                        dpy->tail = prev;
                     38:                } else {
                     39:                    if ((dpy->head = qelt->next) == NULL)
                     40:                     dpy->tail = NULL;
                     41:                 }
                     42:                qelt->next = _qfree;
                     43:                _qfree = qelt;
                     44:                dpy->qlen--;
                     45:                UnlockDisplay(dpy);
                     46:                return;
                     47:                }
                     48:                qelt = (prev = qelt)->next;
                     49:        }
                     50: 
                     51:        
                     52:        /* if the queue is empty, read as many events as possible
                     53:           and enqueue them */
                     54:        while (1) {
                     55:            char buf[BUFSIZE];
                     56:            long pend_not_register; /* because can't "&" a register variable */
                     57:            register long pend;
                     58:            register xEvent *ev;
                     59:            XEvent scratch;
                     60: 
                     61:            /* find out how much data can be read */
                     62:            if (BytesReadable(dpy->fd, (char *)&pend_not_register) < 0)
                     63:                (*_XIOErrorFunction)(dpy);
                     64:            pend = pend_not_register;
                     65: 
                     66:            /* must read at least one xEvent; if none is pending, then
                     67:               we'll just block waiting for it */
                     68:            if (pend < sizeof(xEvent))
                     69:                pend = sizeof (xEvent);
                     70:                
                     71:            /* but we won't read more than the max buffer size */
                     72:            if (pend > BUFSIZE)
                     73:                pend = BUFSIZE;
                     74: 
                     75:            /* round down to an integral number of XReps */
                     76:            pend = (pend / sizeof (xEvent)) * sizeof (xEvent);
                     77: 
                     78:            _XRead (dpy, buf, pend);
                     79:            for (ev = (xEvent *) buf; pend > 0; ev++, pend -= sizeof(xEvent))
                     80:                if (ev->u.u.type == X_Error)
                     81:                    _XError (dpy, (xError *) ev);
                     82:                else  { 
                     83:                    /* it's an event packet; enqueue it if not predicate */
                     84:                    (*dpy->event_vec[ev->u.u.type & 0177])(dpy, &scratch, ev);
                     85:                    if ((*predicate)(dpy, &scratch, arg)) {
                     86:                        ev++;                   /* Enqueue remaining events */
                     87:                        pend -= sizeof(xEvent);
                     88:                        for (; pend > 0; ev++, pend -= sizeof(xEvent))
                     89:                            if (ev->u.u.type == X_Error)
                     90:                                _XError (dpy, (xError *) ev);
                     91:                            else
                     92:                                _XEnq (dpy, ev);
                     93:                        *event = scratch;
                     94:                        UnlockDisplay(dpy);
                     95:                        return;
                     96:                        }
                     97:                    _XEnq (dpy, ev);
                     98:            }
                     99:        }
                    100: }
                    101: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.