home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / eventloop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  6.4 KB  |  195 lines

  1. ;/* eventloop.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 eventloop.c
  3. Blink FROM LIB:c.o,eventloop.o TO eventloop LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. **
  6. ** This example shows how to receive Intuition events.  It reports on a
  7. ** variety of events: close window, keyboard, disk insertion and removal,
  8. ** select button up and down and menu button up and down.  Note that the
  9. ** menu button events will only be received by the program if the
  10. ** WA_RMBTrap attribute is set for the window.
  11. **
  12. ** eventloop.c - standard technique to handle IntuiMessages from an IDCMP.
  13. */
  14. #define INTUI_V36_NAMES_ONLY
  15.  
  16. #include <exec/types.h>
  17. #include <intuition/intuition.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/intuition_protos.h>
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef LATTICE
  25. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  26. int chkabort(void) { return(0); }  /* really */
  27. #endif
  28.  
  29. /* our function prototypes */
  30. BOOL handleIDCMP(struct Window *win, BOOL done);
  31.  
  32. struct Library *IntuitionBase = NULL;
  33.  
  34.  
  35. /*
  36. ** main routine.
  37. ** Open required library and window, then process the events from the
  38. ** window.  Free all resources when done.
  39. */
  40. VOID main(int argc, char **argv)
  41. {
  42. ULONG signals;
  43. UBYTE done;
  44. struct Window *win;
  45.  
  46. IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37);
  47. if (IntuitionBase != NULL)
  48.     {
  49.     if (win = OpenWindowTags(NULL,
  50.                         WA_Title,       "Press Keys and Mouse in this Window",
  51.                         WA_Width,       500,
  52.                         WA_Height,      50,
  53.                         WA_Activate,    TRUE,
  54.                         WA_CloseGadget, TRUE,
  55.                         WA_RMBTrap,     TRUE,
  56.                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY |
  57.                             IDCMP_RAWKEY | IDCMP_DISKINSERTED |
  58.                             IDCMP_DISKREMOVED | IDCMP_MOUSEBUTTONS,
  59.                         TAG_END))
  60.         {
  61.         done = FALSE;
  62.  
  63.         /* perform this loop until the message handling routine signals
  64.         ** that we are done.
  65.         **
  66.         ** When the Wait() returns, check which signal hit and process
  67.         ** the correct port.  There is only one port here, so the test
  68.         ** could be eliminated.  If multiple ports were being watched,
  69.         ** the test would become:
  70.         **
  71.         **    signals = Wait( (1L << win1->UserPort->mp_SigBit) |
  72.         **                    (1L << win2->UserPort->mp_SigBit) |
  73.         **                    (1L << win3->UserPort->mp_SigBit))
  74.         **    if (signals & (1L << win1->UserPort->mp_SigBit))
  75.         **        done = handleWin1IDCMP(win1,done);
  76.         **    else if (signals & (1L << win2->UserPort->mp_SigBit))
  77.         **        done = handleWin2IDCMP(win2,done);
  78.         **    else if (signals & (1L << win3->UserPort->mp_SigBit))
  79.         **        done = handleWin3IDCMP(win3,done);
  80.         **
  81.         ** Note that these could all call the same routine with different
  82.         ** window pointers (if the handling was identical).
  83.         **
  84.         ** handleIDCMP() should remove all of the messages from the port.
  85.         */
  86.         while (!done)
  87.             {
  88.             signals = Wait(1L << win->UserPort->mp_SigBit);
  89.             if (signals & (1L << win->UserPort->mp_SigBit))
  90.                 done = handleIDCMP(win,done);
  91.             };
  92.         CloseWindow(win);
  93.         }
  94.     CloseLibrary(IntuitionBase);
  95.     }
  96. }
  97.  
  98.  
  99. /*
  100. ** handleIDCMP() - handle all of the messages from an IDCMP.
  101. */
  102. BOOL handleIDCMP(struct Window *win, BOOL done)
  103. {
  104. struct IntuiMessage *message;
  105. USHORT code;
  106. SHORT mousex, mousey;
  107. ULONG class;
  108.  
  109. /* Remove all of the messages from the port by calling GetMsg()
  110. ** until it returns NULL.
  111. **
  112. ** The code should be able to handle three cases:
  113. **
  114. ** 1.  No messages waiting at the port, and the first call to GetMsg()
  115. ** returns NULL.  In this case the code should do nothing.
  116. **
  117. ** 2.  A single message waiting.  The code should remove the message,
  118. ** processes it, and finish.
  119. **
  120. ** 3.  Multiple messages waiting.  The code should process each waiting
  121. ** message, and finish.
  122. */
  123. while (NULL != (message = (struct IntuiMessage *)GetMsg(win->UserPort)))
  124.     {
  125.     /* It is often convenient to copy the data out of the message.
  126.     ** In many cases, this lets the application reply to the message
  127.     ** quickly.  Copying the data is not required, if the code does
  128.     ** not reply to the message until the end of the loop, then
  129.     ** it may directly reference the message information anywhere
  130.     ** before the reply.
  131.     */
  132.     class  = message->Class;
  133.     code   = message->Code;
  134.     mousex = message->MouseX;
  135.     mousey = message->MouseY;
  136.  
  137.     /* The loop should reply as soon as possible.  Note that the code
  138.     ** may not reference data in the message after replying to the
  139.     ** message.  Thus, the application should not reply to the message
  140.     ** until it is done referencing information in it.
  141.     **
  142.     ** Be sure to reply to every message received with GetMsg().
  143.     */
  144.     ReplyMsg((struct Message *)message);
  145.  
  146.     /* The class contains the IDCMP type of the message. */
  147.     switch (class)
  148.         {
  149.         case IDCMP_CLOSEWINDOW:
  150.             done = TRUE;
  151.             break;
  152.         case IDCMP_VANILLAKEY:
  153.             printf("IDCMP_VANILLAKEY (%lc)\n",code);
  154.             break;
  155.         case IDCMP_RAWKEY:
  156.             printf("IDCMP_RAWKEY\n");
  157.             break;
  158.         case IDCMP_DISKINSERTED:
  159.             printf("IDCMP_DISKINSERTED\n");
  160.             break;
  161.         case IDCMP_DISKREMOVED:
  162.             printf("IDCMP_DISKREMOVED\n");
  163.             break;
  164.         case IDCMP_MOUSEBUTTONS:
  165.             /* the code often contains useful data, such as the ASCII
  166.             ** value (for IDCMP_VANILLAKEY), or the type of button
  167.             ** event here.
  168.             */
  169.             switch (code)
  170.                 {
  171.                 case SELECTUP:
  172.                     printf("SELECTUP at %d,%d\n",mousex,mousey);
  173.                     break;
  174.                 case SELECTDOWN:
  175.                     printf("SELECTDOWN at %d,%d\n",mousex,mousey);
  176.                     break;
  177.                 case MENUUP:
  178.                     printf("MENUUP\n");
  179.                     break;
  180.                 case MENUDOWN:
  181.                     printf("MENUDOWN\n");
  182.                     break;
  183.                 default:
  184.                     printf("UNKNOWN CODE\n");
  185.                     break;
  186.                 }
  187.             break;
  188.         default:
  189.             printf("Unknown IDCMP message\n");
  190.             break;
  191.         }
  192.     }
  193. return(done);
  194. }
  195.