home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_NextEvent.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  10KB  |  369 lines

  1.  
  2. #include "Xlib_private.h"
  3.  
  4. extern long const _Xevent_to_mask[];      
  5.  
  6. /* 
  7.  * I don't think passing the pointers throught the pipe
  8.  * is a good idea, considering the code for peeking and
  9.  * putting back events, I am leaving the passing of
  10.  * pointers in but only as something to set off the fd
  11.  * flag, I am using the EventQueue linked list for all 
  12.  * reads and writes to the event queue.  Brian Smith
  13.  */
  14.  
  15.  
  16. int XNextEvent(Display *display, XEvent *event)
  17. {
  18.     DBUG_ENTER("XNextEvent")
  19.     int rc = 0;
  20.     WinAttribData *attrib;
  21.     XEvent *ev;
  22.     Xlib_EventQueue *tmp;
  23.  
  24. #ifdef DEBUG
  25.     static char debuginfo[256];
  26. #endif
  27.  
  28.     XFlush(display);
  29.  
  30.         DBUG_POINT("WinPeekMsg");
  31.     while (!EventQueue) {
  32.         QMSG qmsg;
  33.         if (WinPeekMsg(mainhab, &qmsg, NULLHANDLE, 0, 0, PM_REMOVE))
  34.             WinDispatchMsg(mainhab, &qmsg);
  35.         else
  36.             _sleep2(1);
  37.     }
  38.  
  39.     /* For performance, assume pipe read/write is atomic */
  40.         DBUG_POINT("read");
  41.     rc = read(display->fd, (char *)&ev, sizeof(void *));
  42.     if (rc<0) {
  43.         DBUG_RETURN(False);    /* broken by signal or alarm */
  44.     }
  45.  
  46.     /* Something really went wrong if EventQueue is NULL */
  47.     if(EventQueue == NULL)
  48.     {
  49.         display->qlen = 0;
  50.         DBUG_RETURN(False);
  51.     }
  52.     pthread_mutex_lock(&evmutex);
  53.     memcpy(event, &(EventQueue->event), sizeof(XEvent));
  54.  
  55.     /* Advance the Queue */
  56.     tmp = EventQueue;
  57.     EventQueue = EventQueue->next;
  58.     free(tmp);
  59.     display->qlen--;
  60.  
  61.         DBUG_POINT("attrib");
  62.     attrib = WinQueryWindowPtr(event->xany.window,QWP_WINATTRIB);
  63.     if (attrib && attrib->lastexpose == ev) attrib->lastexpose = NULL;
  64.     if (attrib && attrib->lastconfigure == ev) attrib->lastconfigure = NULL;
  65.     pthread_mutex_unlock(&evmutex);
  66.  
  67. #ifdef DEBUG
  68.     sprintf(debuginfo, "Event type = %d", event->type);
  69.     DBUG_POINT(debuginfo);
  70. #endif    
  71.  
  72.     DBUG_RETURN(True);
  73. }
  74.  
  75. int XPending(Display *display)
  76. {
  77.     DBUG_ENTER("XPending")
  78.     struct timeval tv = { 0, 0 };
  79.     fd_set readables;
  80.  
  81.     /*FD_ZERO(&readables);
  82.     FD_SET(display->fd, &readables);
  83.     select(display->fd+1, &readables, NULL, NULL, &tv);
  84.     DBUG_RETURN(FD_ISSET(display->fd, &readables));*/
  85.     DBUG_RETURN(EventQueue?1:0);
  86. }
  87.  
  88. int XPeekEvent(Display *display, XEvent *event_return)
  89. {
  90.     DBUG_ENTER("XPeekEvent")
  91.     WinAttribData *attrib;
  92.     fd_set readables;
  93.     int ready = 0;
  94.  
  95.     DBUG_POINT("XFlush()");
  96.     XFlush(display);
  97.  
  98.     DBUG_POINT("spin!");
  99.     /* If we don't have an event waiting block until we have one */
  100.     while (!EventQueue) {
  101.         QMSG qmsg;
  102.         if (WinPeekMsg(mainhab, &qmsg, NULLHANDLE, 0, 0, PM_REMOVE))
  103.             WinDispatchMsg(mainhab, &qmsg);
  104.         else
  105.             _sleep2(1);
  106.     }
  107.  
  108.     /* Return the tail end of the queue */
  109.     DBUG_POINT("pthread_mutex_lock()");
  110.     pthread_mutex_lock(&evmutex);
  111.     DBUG_POINT("memcpy()");
  112.     memcpy(event_return, &(EventQueue->event), sizeof(XEvent));
  113.  
  114.     attrib = WinQueryWindowPtr(event_return->xany.window,QWP_WINATTRIB);
  115.     if (attrib && attrib->lastexpose == &(EventQueue->event)) attrib->lastexpose = NULL;
  116.     if (attrib && attrib->lastconfigure == &(EventQueue->event)) attrib->lastconfigure = NULL;
  117.  
  118.     DBUG_POINT("pthread_mutex_unlock()");
  119.     pthread_mutex_unlock(&evmutex);
  120.     DBUG_RETURN(True);
  121. }
  122.  
  123. int XPutBackEvent(Display *display, XEvent *event)
  124. {
  125.     DBUG_ENTER("XPutBackEvent")
  126.     Xlib_EventQueue *tmp;
  127.  
  128.       pthread_mutex_lock(&evmutex);
  129.  
  130.     /* Add the event to the top of the list */
  131.     tmp = malloc(sizeof(Xlib_EventQueue));
  132.     tmp->next = EventQueue;
  133.     EventQueue = tmp;
  134.  
  135.     memcpy(&EventQueue->event, event, sizeof(XEvent));
  136.     display->qlen++;
  137.     write(pmout[1], (char *) &EventQueue->event, sizeof(void *));
  138.     pthread_mutex_unlock(&evmutex);
  139.     DBUG_RETURN(True);
  140. }
  141.  
  142. Bool XCheckWindowEvent(Display *display, Window w, long event_mask, XEvent *event_return)
  143. {
  144.     DBUG_ENTER("XCheckWindowEvent")
  145.     Xlib_EventQueue *tmp, *prev = NULL;
  146.  
  147.       pthread_mutex_lock(&evmutex);
  148.     tmp = EventQueue;
  149.     while(tmp != NULL)
  150.     {
  151.         if((tmp->event.xany.window == w) && (event_mask & _Xevent_to_mask[tmp->event.type]))
  152.         {
  153.             WinAttribData *attrib;
  154.                         XEvent *ev;
  155.                         int rc = 0;
  156.  
  157.             memcpy(event_return, &tmp->event, sizeof(XEvent));
  158.  
  159.                         if(prev)
  160.                             prev->next = tmp->next;
  161.                         else
  162.                             EventQueue = tmp->next;
  163.  
  164.                         free(tmp);
  165.  
  166.                         /* For performance, assume pipe read/write is atomic */
  167.                         rc = read(display->fd, (char *)&ev, sizeof(void *));
  168.                         if (rc<0) {
  169.                             DBUG_RETURN(False);    /* broken by signal or alarm */
  170.                         }
  171.             attrib = WinQueryWindowPtr(event_return->xany.window,QWP_WINATTRIB);
  172.             if (attrib && attrib->lastexpose == &(tmp->event)) attrib->lastexpose = NULL;
  173.             if (attrib && attrib->lastconfigure == &(tmp->event)) attrib->lastconfigure = NULL;
  174.  
  175.                         pthread_mutex_unlock(&evmutex);
  176.             DBUG_RETURN(True);
  177.                 }
  178.                 prev = tmp;
  179.         tmp = tmp->next;
  180.     }
  181.     pthread_mutex_unlock(&evmutex);
  182.     DBUG_RETURN(False);
  183. }
  184.  
  185. int XWindowEvent(Display *display, Window w, long event_mask, XEvent *event_return)
  186. {
  187.     /* There is probably a better way of doing this but it
  188.      is eluding me at the moment. - Brian */
  189.     DBUG_ENTER("XWindowEvent")
  190.     while(!XCheckWindowEvent(display, w, event_mask, event_return))
  191.         DosSleep(1);
  192.     DBUG_RETURN(True);
  193. }
  194.  
  195.  
  196. Status XSendEvent(Display *display, Window w, Bool propagate, 
  197.                   long event_mask, XEvent *event_send)
  198. {
  199.     DBUG_ENTER("XSendEvent")
  200.     Status result = Xlib_SendEvent(display, w, propagate, event_mask, event_send, TRUE);
  201.     DBUG_RETURN(result);
  202. }
  203.  
  204. int XSelectInput(Display *display, Window w, long event_mask)
  205. {
  206.     DBUG_ENTER("XSelectInput")
  207.     XWindowAttributes *winattrib = GetWinAttrib(w,NULL);
  208.     
  209.     if (winattrib) {
  210.         winattrib->your_event_mask = event_mask;
  211.         DBUG_RETURN(True);
  212.     }
  213.     DBUG_RETURN(False);
  214. }
  215.  
  216. /* Mode parameter appears to have no use on OS/2 since we cannot
  217.  * flush the connection and see if there are more events.
  218.  */
  219. int XEventsQueued(Display *display, int mode)
  220. {
  221.         DBUG_ENTER("XEventsQueued")
  222.         int qlen;
  223.  
  224.         XFlush(display);
  225.         pthread_mutex_lock(&evmutex);
  226.         qlen = display->qlen;
  227.         pthread_mutex_unlock(&evmutex);
  228.         DBUG_RETURN(qlen);
  229. }
  230.  
  231. int XIfEvent (display, event, predicate, arg)
  232.     register Display *display;
  233.     Bool (*predicate)(
  234. #if NeedNestedPrototypes
  235.               Display*            /* display */,
  236.               XEvent*            /* event */,
  237.               char*                /* arg */
  238. #endif                                          
  239.               );        /* function to call */
  240.     register XEvent *event;
  241.     char *arg;
  242. {
  243.     DBUG_ENTER("XIfEvent")
  244.     Xlib_EventQueue *tmp;
  245.     WinAttribData *attrib;
  246.     fd_set readables;
  247.     int ready = 0;
  248.  
  249.         while(1)
  250.         {
  251.         pthread_mutex_lock(&evmutex);
  252.         tmp = EventQueue;
  253.  
  254.             while(tmp != NULL)
  255.             {
  256.                 if((*predicate)(display, &tmp->event, arg))
  257.         {
  258.                     memcpy(event, &tmp->event, sizeof(XEvent));
  259.             attrib = WinQueryWindowPtr(tmp->event.xany.window,QWP_WINATTRIB);
  260.             if (attrib && attrib->lastexpose == &(tmp->event)) attrib->lastexpose = NULL;
  261.             if (attrib && attrib->lastconfigure == &(tmp->event)) attrib->lastconfigure = NULL;
  262.                     Xlib_RemoveEvent(&tmp->event);
  263.                     pthread_mutex_unlock(&evmutex);
  264.             DBUG_RETURN(True);
  265.         }
  266.         tmp = tmp->next;
  267.  
  268.             }
  269.             pthread_mutex_unlock(&evmutex);
  270.  
  271.             /* If we don't have an event waiting block until we have one */
  272.         while (!EventQueue) {
  273.         QMSG qmsg;
  274.         if (WinPeekMsg(mainhab, &qmsg, NULLHANDLE, 0, 0, PM_REMOVE))
  275.             WinDispatchMsg(mainhab, &qmsg);
  276.         else
  277.             _sleep2(1);
  278.         }
  279.         }
  280.     DBUG_RETURN(False);
  281. }
  282.  
  283. int XPeekIfEvent (display, event, predicate, arg)
  284.     register Display *display;
  285.     register XEvent *event;
  286.     Bool (*predicate)(
  287. #if NeedNestedPrototypes
  288.               Display*            /* display */,
  289.               XEvent*            /* event */,
  290.               char*                /* arg */
  291. #endif
  292.               );
  293.     char *arg;
  294. {
  295.     DBUG_ENTER("XPeekIfEvent")
  296.     WinAttribData *attrib;
  297.     Xlib_EventQueue *tmp;
  298.     fd_set readables;
  299.     int ready = 0;
  300.  
  301.         while(1)
  302.         {
  303.         pthread_mutex_lock(&evmutex);
  304.         tmp = EventQueue;
  305.  
  306.             while(tmp != NULL)
  307.             {
  308.                 if((*predicate)(display, &tmp->event, arg))
  309.         {
  310.             memcpy(event, &tmp->event, sizeof(XEvent));
  311.             attrib = WinQueryWindowPtr(tmp->event.xany.window,QWP_WINATTRIB);
  312.             if (attrib && attrib->lastexpose == &(tmp->event)) attrib->lastexpose = NULL;
  313.             if (attrib && attrib->lastconfigure == &(tmp->event)) attrib->lastconfigure = NULL;
  314.                         pthread_mutex_unlock(&evmutex);
  315.             DBUG_RETURN(True);
  316.         }
  317.         tmp = tmp->next;
  318.  
  319.             }
  320.             pthread_mutex_unlock(&evmutex);
  321.  
  322.             /* If we don't have an event waiting block until we have one */
  323.         while (!EventQueue) {
  324.         QMSG qmsg;
  325.         if (WinPeekMsg(mainhab, &qmsg, NULLHANDLE, 0, 0, PM_REMOVE))
  326.             WinDispatchMsg(mainhab, &qmsg);
  327.         else
  328.             _sleep2(1);
  329.         }
  330.         }
  331.     DBUG_RETURN(False);
  332. }
  333.  
  334. int XCheckIfEvent (display, event, predicate, arg)
  335.         register Display *display;
  336.     Bool (*predicate)(
  337. #if NeedNestedPrototypes
  338.               Display*            /* display */,
  339.               XEvent*            /* event */,
  340.               char*                /* arg */
  341. #endif                                                     
  342.               );        /* function to call */
  343.     register XEvent *event;        /* XEvent to be filled in. */
  344.     char *arg;
  345. {
  346.     DBUG_ENTER("XCheckIfEvent")
  347.     WinAttribData *attrib;
  348.     Xlib_EventQueue *tmp = EventQueue;
  349.  
  350.       pthread_mutex_lock(&evmutex);
  351.         while(tmp != NULL)
  352.         {
  353.             if((*predicate)(display, &tmp->event, arg))
  354.         {
  355.                 memcpy(event, &tmp->event, sizeof(XEvent));
  356.         attrib = WinQueryWindowPtr(tmp->event.xany.window,QWP_WINATTRIB);
  357.         if (attrib && attrib->lastexpose == &(tmp->event)) attrib->lastexpose = NULL;
  358.         if (attrib && attrib->lastconfigure == &(tmp->event)) attrib->lastconfigure = NULL;
  359.                 Xlib_RemoveEvent(&tmp->event);
  360.                 pthread_mutex_unlock(&evmutex);
  361.         DBUG_RETURN(True);
  362.             }
  363.         tmp = tmp->next;
  364.  
  365.         }
  366.         pthread_mutex_unlock(&evmutex);
  367.         DBUG_RETURN(False);
  368. }
  369.