home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / oslib / examples / c / wimpevent < prev    next >
Encoding:
Text File  |  1995-08-30  |  4.6 KB  |  195 lines

  1. /* Title:   wimpevent.c
  2.  * Purpose: functions for wimp event dispatching
  3.  * Author:  IDJ
  4.  * History: 19-Jun-94: IDJ: created
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include "os.h"
  13.  
  14. #include "trace.h"
  15. #include "wimpevent.h"
  16. #include "x.h"
  17.  
  18. #define MAX_WIMP_HANDLER 20
  19.  
  20. static event_wimp_handler_item    *Handlers[MAX_WIMP_HANDLER+1];
  21. static event_wimp_handler_item    *All_Handlers = 0;
  22.  
  23. #define MATCHES(h, object_id, event_code, handler, handle) \
  24.    ((h)->object_id == (object_id) && (h)->event_code == (event_code) && \
  25.          (h)->handler == (handler) && (h)->handle == (handle))
  26.  
  27. static int Matches (int event_code, event_wimp_handler_item *h, toolbox_block *id_block)
  28.  
  29. {
  30.     /*
  31.      * first see if "other" wimp event handler, and interested in event
  32.      * then look to see if interested in all objects,
  33.      * then interest in this object.
  34.      */
  35.  
  36.     if (event_code >= MAX_WIMP_HANDLER && h->event_code != event_code && h->event_code != -1)
  37.         return 0;
  38.  
  39.    return h->object_id == event_ANY || h->object_id == id_block->this_obj;
  40. }
  41.  
  42. static void Call (int event_code, event_wimp_handler_item *h, wimp_block *poll_block, toolbox_block *id_block, int *claimed)
  43.  
  44. {  for (; h != NULL; h = h->next)
  45.       if (Matches (event_code, h, id_block) && h->handler != NULL)
  46.          /* call the handler */
  47.          if (h->handler (event_code, poll_block, id_block, h->handle))
  48.          {  *claimed = TRUE;
  49.             return;
  50.          }
  51.  
  52.     *claimed = FALSE;
  53. }
  54.  
  55. extern void wimpevent_dispatch (int event_code, wimp_block *poll_block, toolbox_block *id_block)
  56.  
  57. { /*
  58.    * if it's not a known wimp event, use dft handler, else we call any wimp handlers
  59.    * on the chain for this event.  If they return non-zero, they have claimed
  60.    * the event.
  61.    */
  62.  
  63.    bool claimed = FALSE;
  64.  
  65.    tracef ("wimpevent_dispatch\n");
  66.    Call (event_code, Handlers [event_code < MAX_WIMP_HANDLER?
  67.          event_code: MAX_WIMP_HANDLER - 1], poll_block, id_block,
  68.          &claimed);
  69.    if (!claimed)
  70.       Call (event_code, All_Handlers, poll_block, id_block, &claimed);
  71. }
  72.  
  73. /* --------------------------- registering handlers for wimp events ----------------------------- */
  74.  
  75. void wimpevent_register_wimp_handler (toolbox_o object_id, int event_code, event_wimp_handler *handler, void *handle)
  76. {
  77.     event_wimp_handler_item *h, *new_h;
  78.     int                   elem;
  79.  
  80.     /*
  81.      * see if there's one there already
  82.      */
  83.  
  84.     /*
  85.      * start looking down the linked list which hangs off the array element for this event
  86.      * Any events > 19 are just handled by a default case.
  87.      */
  88.  
  89.     if (event_code < MAX_WIMP_HANDLER)
  90.         elem = event_code;
  91.     else
  92.         elem = MAX_WIMP_HANDLER;
  93.  
  94.     if (event_code == -1)
  95.         h =  All_Handlers;
  96.     else
  97.         h = Handlers[elem];
  98.  
  99.  
  100.     while (h != NULL)
  101.     {
  102.         /*
  103.          * if there's already a handler, just return.
  104.          */
  105.  
  106.         if (MATCHES (h, object_id, event_code, handler, handle))
  107.             return;
  108.  
  109.         h = h->next;
  110.     }
  111.  
  112.  
  113.     /*
  114.      * make a new entry in the list for this event
  115.      */
  116.  
  117.     new_h = x_ALLOC (sizeof *new_h);
  118.     new_h->object_id  = object_id;
  119.     new_h->event_code = event_code;
  120.     new_h->handler    = handler;
  121.     new_h->handle     = handle;
  122.  
  123.     if (event_code == -1)
  124.     {
  125.         new_h->next               = All_Handlers;
  126.         All_Handlers   = new_h;
  127.     }
  128.     else
  129.     {
  130.         new_h->next               = Handlers[elem];
  131.         Handlers[elem] = new_h;
  132.     }
  133. }
  134.  
  135.  
  136.  
  137. void wimpevent_deregister_wimp_handler (toolbox_o object_id, int event_code, event_wimp_handler *handler, void *handle)
  138. {
  139.     event_wimp_handler_item *h, *prev_h;
  140.     event_wimp_handler_item **listhead;
  141.     int elem;
  142.  
  143.     /*
  144.      * see if handler registered, and if so delete it.
  145.      */
  146.  
  147.     /*
  148.      * start looking down the linked list which hangs off the array element for this event
  149.      */
  150.  
  151.     if (event_code <= MAX_WIMP_HANDLER-1)
  152.         elem = event_code;
  153.     else
  154.         elem = MAX_WIMP_HANDLER;
  155.  
  156.     if (event_code == -1)
  157.     {
  158.         h = prev_h = All_Handlers;
  159.         listhead = &All_Handlers;
  160.     }
  161.     else
  162.     {
  163.         h = prev_h = Handlers[elem];
  164.         listhead = &Handlers[elem];
  165.     }
  166.  
  167.     while (h != NULL)
  168.     {
  169.         /*
  170.          * see if there's a handler, which matches.
  171.          */
  172.  
  173.         if (MATCHES (h, object_id, event_code, handler, handle))
  174.             break;
  175.  
  176.         prev_h = h;
  177.         h = h->next;
  178.     }
  179.  
  180.  
  181.     /*
  182.      * delete the handler from the list
  183.      */
  184.  
  185.     if (h != NULL)
  186.     {
  187.         if (h == *listhead)
  188.             *listhead = h->next;
  189.         else
  190.             prev_h->next = h->next;
  191.  
  192.         x_FREE (h, sizeof *h);
  193.     }
  194. }
  195.