/* Title:   event.c
 * Purpose: event dispatching
 * Author:  IDJ
 * History: 19-Jun-94: IDJ: created
 *          22nd Mar 1995: J R C: hacked mercilessly to use OSLib
 */

/*From CLib*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*From OSLib*/
#include "wimp.h"

/*From TLib*/
#include "toolbox.h"

/*From Support*/
#include "tbevent.h"
#include "wimpevent.h"
#include "wimpmsg.h"

static toolbox_block *Block = 0L;
static wimp_poll_flags Mask = wimp_MASK_NULL;

/* ------------------------------------- dispatching events ------------------------------------ */
static void Dispatch (wimp_event_no event_code, wimp_block *poll_block, toolbox_block *id_block)

{
   switch (event_code)
   {  case toolbox_EVENT:
         tbevent_dispatch (poll_block, id_block);
      break;

      case wimp_USER_MESSAGE: case wimp_USER_MESSAGE_RECORDED:
         wimpmsg_dispatch (poll_block, id_block);
      break;

      default:
         wimpevent_dispatch (event_code, poll_block, id_block);
      break;
   }
}

void event_poll (wimp_event_no *event_code, wimp_block *poll_block, void *poll_word)
{
    wimp_block    our_poll_block, *pb;
    wimp_event_no our_event_code, *ecode;

    ecode=event_code ? event_code : &our_event_code;
    pb=poll_block ? poll_block : &our_poll_block;

    *ecode = wimp_poll(Mask, pb, poll_word);

    Dispatch(*ecode, pb, Block);
}

extern void event_poll_idle (wimp_event_no *event_code, wimp_block *poll_block, os_t earliest, void *poll_word)
{
    wimp_block    our_poll_block, *pb;
    wimp_event_no our_event_code, *ecode;

    ecode=event_code ? event_code : &our_event_code;
    pb=poll_block ? poll_block : &our_poll_block;

    *ecode = wimp_poll_idle (Mask, pb, earliest, poll_word);

    Dispatch(*ecode, pb, Block);
}

/* ------------------------------------- registering/deregistering handlers ----------------------- */
/* wimp events */
void event_register_wimp_handler (toolbox_o object_id, wimp_event_no event_code, event_wimp_handler handler, void *handle)
      {wimpevent_register_wimp_handler (object_id, event_code, handler, handle);}
void event_deregister_wimp_handler (toolbox_o object_id, wimp_event_no event_code, event_wimp_handler handler, void *handle)
      {wimpevent_deregister_wimp_handler (object_id, event_code, handler, handle);}

/* toolbox events */
void event_register_toolbox_handler (toolbox_o object_id, bits event_code, event_toolbox_handler handler, void *handle)
      {tbevent_register_toolbox_handler (object_id, event_code, handler, handle);}
void event_deregister_toolbox_handler (toolbox_o object_id, bits event_code, event_toolbox_handler handler, void *handle)
      {tbevent_deregister_toolbox_handler (object_id, event_code, handler, handle);}

/* wimp messages */
void event_register_message_handler (bits msg_no, event_message_handler handler, void *handle)
      {wimpmsg_register_message_handler (msg_no, handler, handle);}
void event_deregister_message_handler (bits msg_no, event_message_handler handler, void *handle)
      {wimpmsg_deregister_message_handler (msg_no, handler, handle);}

/* ------------------------------------- getting/setting poll mask -------------------------------- */
extern void event_set_mask (wimp_poll_flags mask) {Mask = mask;}
extern void event_get_mask (wimp_poll_flags *mask) {if (mask != NULL) *mask = Mask;}

/* ---------------------------------------- initialisation -------------------------------------- */
void event_initialise (toolbox_block *b)

{  Block = b;
}
