/********************************************************************
 *                                                                  *
 * test1.c: a simple test program                                   *
 *                                                                  *
 * Author: Dave Harris <dah31@cam.ac.uk>                            *
 *                                                                  *
 * Date: $Date$
 *                                                                  *
 * $Log$
 *                                                                  *
 ********************************************************************/

#include <proto/exec.h>
#include <proto/dos.h>
#include <signal.h>
#include <stdio.h>

#include "include/mousewheel.h"

struct mousewheel_message the_message, *the_reply;

int main(int, char **);
#ifdef __SASC
void exit(int);
#endif

int main(int argc, char *argv[])
{
  struct MsgPort *mouse_port;
  ULONG signals,
        wheel_down_sigbit, wheel_down_signal,
        wheel_up_sigbit,   wheel_up_signal,
        quit_sigbit,       quit_signal;

  struct MsgPort *our_port = CreatePort(0, 0);
  if (our_port == NULL) {
    printf("Couldn't create port!\n");
    exit(1);
  }
  printf("Looking for port...");
  mouse_port = FindPort(MOUSEWHEEL_PORTNAME);
  if (mouse_port == NULL)
  {
    printf("Couldn't open mouse_port!\n");
    if (our_port != NULL) {
      DeletePort(our_port);
      exit(1);
    }
  }

  wheel_down_sigbit = AllocSignal(-1);
  if (wheel_down_sigbit != -1)
    {
      wheel_up_sigbit = AllocSignal(-1);
      if (wheel_up_sigbit != -1)
        {
          quit_sigbit = AllocSignal(-1);
          if (quit_sigbit != -1)
            {
              wheel_down_signal = 1 << wheel_down_sigbit;
              wheel_up_signal = 1 << wheel_up_sigbit;
              quit_signal = 1 << quit_sigbit;

              printf(" done.\nCreating message...");
              the_message.message.mn_Node.ln_Type = NT_MESSAGE;
              the_message.message.mn_Length = sizeof(struct mousewheel_message);
              the_message.message.mn_ReplyPort = our_port;
              the_message.type = MOUSEWHEEL_SIGNALS;
              the_message.content.signals.wheel_up_sigbit   = wheel_up_sigbit;
              the_message.content.signals.wheel_down_sigbit = wheel_down_sigbit;
              the_message.content.signals.quit_sigbit       = quit_sigbit;

              printf(" done.\nPutMsg()ing...");
              PutMsg(mouse_port, (struct Message *)&the_message);
              WaitPort(our_port);  /* wait for the reply */

              printf(" done.\nWaiting for ^C.\n");
#ifdef __GNUC__
              /* disable ^C handling with GCC / libnix */
              signal(SIGINT, SIG_IGN);
#endif  /* __GNUC__ */
#ifdef __SASC
              /* this is handled in compiler options */
#endif  /* __SASC */
              for (;;)
                {
                  signals = Wait(wheel_down_signal | wheel_up_signal | quit_signal | SIGBREAKF_CTRL_C);
                  printf("client: signals:\n");
                  if (signals & wheel_down_signal) printf("  wheel_down_signal\n");
                  if (signals & wheel_up_signal) printf("  wheel_up_signal\n");
                  if (signals & quit_signal) { printf("  quit_signal\n"); break; }
                  if (signals & SIGBREAKF_CTRL_C) { printf("  SIGBREAKF_CTRL_C\n"); break; }
                }

              /* ahh, time to quit */
              the_message.type = MOUSEWHEEL_CLIENT_QUITTING;
              the_message.content.task = FindTask(NULL);
              PutMsg(mouse_port, (struct Message *)&the_message);
              WaitPort(our_port);

              /* we should really while() over GetMsg(), but it seems
               * to get stuck just at the moment */
              the_reply = (struct mousewheel_message *)GetMsg(our_port);
              printf("client: reply received (type %d)\n", the_reply->type);
            }  /* quit_sigbit != -1 */
          FreeSignal(wheel_up_sigbit);
        }  /* wheel_up_sigbit != -1 */
      FreeSignal(wheel_down_sigbit);
    }  /* wheel_down_sigbit != -1 */
  DeletePort(our_port);
}
