home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / intuition / mouse_keybd / mousekeys.c next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  7.8 KB  |  278 lines

  1. /* MouseKeys.c */
  2. /* Compiled with Lattice C v5.02                                     */
  3. /* Compiler invoked with: lc -b1 -cfist -L -v -w                     */
  4.  
  5. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  6.  *
  7.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  8.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  9.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  10.  * information on the correct usage of the techniques and operating system
  11.  * functions presented in this example.  The source and executable code of
  12.  * this example may only be distributed in free electronic form, via bulletin
  13.  * board or as part of a fully non-commercial and freely redistributable
  14.  * diskette.  Both the source and executable code (including comments) must
  15.  * be included, without modification, in any copy.  This example may not be
  16.  * published in printed form or distributed with any commercial product.
  17.  * However, the programming techniques and support routines set forth in
  18.  * this example may be used in the development of original executable
  19.  * software products for Commodore Amiga computers.
  20.  * All other rights reserved.
  21.  * This example is provided "as-is" and is subject to change; no warranties
  22.  * are made.  All use is at your own risk.  No liability or responsibility
  23.  * is assumed.
  24.  */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/memory.h>
  28. #include <intuition/intuition.h>
  29. #include <graphics/gfxbase.h>
  30. #include <devices/inputevent.h>
  31. #include <libraries/dos.h>
  32. #ifdef LATTICE
  33. #include <proto/all.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. int CXBRK(void) { return(0); } /* Disable Lattice CTRL/C handling */
  38. #endif
  39.  
  40. VOID   cleanExit(int);
  41. VOID   OpenAll(VOID);
  42. VOID   DoKeys(struct IntuiMessage *);
  43. LONG   DeadKeyConvert(struct IntuiMessage *, UBYTE *, LONG, struct KeyMap *);
  44. VOID   DoMouseMove(struct RastPort *, struct IntuiMessage *);
  45. VOID   DoButtons(struct IntuiMessage *);
  46.  
  47. #define    BUFSIZE 15
  48. #define RP window->RPort
  49. #define WIDTH 320
  50. #define HEIGHT 50
  51. #define SHIFTED (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)
  52.  
  53. struct    IntuitionBase    *IntuitionBase=NULL;
  54. struct  GfxBase         *GfxBase=NULL;
  55. struct    Window        *window=NULL;
  56. struct    ConsoleDevice    *ConsoleDevice=NULL;
  57. struct    IOStdReq    ioreq;
  58.  
  59. struct    NewWindow    NewWindow = {
  60.     0,0,WIDTH,HEIGHT,
  61.     -1,-1,
  62.     CLOSEWINDOW|RAWKEY|MOUSEMOVE|MOUSEBUTTONS,
  63.     WINDOWDRAG|WINDOWCLOSE|ACTIVATE|REPORTMOUSE|RMBTRAP,
  64.     NULL,NULL,
  65.     "Mouse & KeyBoard",
  66.     NULL,NULL,
  67.     0,0,0,0,
  68.     WBENCHSCREEN
  69. };
  70.  
  71.  
  72. /*
  73.  The Main Loop
  74.  */
  75.  
  76. VOID    main(int argc, char *argv[])
  77. {
  78. USHORT    keep=TRUE;           /* loop control */
  79. struct  IntuiMessage  *msg;  /* for the Intuition message */
  80. ULONG    class;               /* event class */
  81.  
  82.     OpenAll();
  83.     printf("Monitors the Mouse AND Keyboard\n");
  84.     printf("Try DoubleClicking and Special Keys\n");
  85.     while(keep)
  86.     {
  87.         Wait((1L<<window->UserPort->mp_SigBit));
  88.         while(msg=(struct IntuiMessage *)GetMsg(window->UserPort))
  89.         {
  90.             class=msg->Class;  /* Get the event class */
  91.             switch(class)      /* handle our events */
  92.             {
  93.                 case CLOSEWINDOW:  keep=FALSE; break;
  94.                 case RAWKEY:       DoKeys(msg); break;
  95.                 case MOUSEMOVE:    DoMouseMove(RP,msg); break;
  96.                 case MOUSEBUTTONS: DoButtons(msg); break;
  97.             }
  98.             ReplyMsg((struct Message *)msg);
  99.         }
  100.     }
  101.     cleanExit(RETURN_OK);
  102. }
  103.  
  104.  
  105. /*
  106.  Show what mouse buttons where pushed
  107.  */
  108.  
  109. VOID   DoButtons(struct IntuiMessage *msg)
  110. {
  111. static ULONG  lsecs = 0L, lmics = 0L;   /* For detecting DoubleClick */
  112. USHORT code;
  113. USHORT qual;
  114. ULONG  secs, mics;
  115.  
  116.     code = msg->Code;
  117.     qual = msg->Qualifier;
  118.     secs = msg->Seconds;          /* get the current time for */
  119.     mics = msg->Micros;           /* DoubleClick() */
  120.  
  121.     /* Yes, qualifiers can apply to the mouse also.  That is how
  122.      * we get the shift select on the WorkBench.  This shows how
  123.      * to see if a specific bit is set within the qualifier
  124.      */
  125.  
  126.     if(qual&SHIFTED)  printf("Shift ");
  127.     switch(code)
  128.     {
  129.        case SELECTDOWN:
  130.           printf("Left Button Down");
  131.           if(DoubleClick(lsecs, lmics, secs, mics)) {
  132.              printf(" DoubleClick!");
  133.           } else {
  134.              lsecs = secs;
  135.              lmics = mics;
  136.           }
  137.           break;
  138.        case SELECTUP:
  139.           printf("Left Button up");
  140.           break;
  141.        case MENUDOWN:
  142.           printf("Right Button down");
  143.           break;
  144.        case MENUUP:
  145.           printf("Right Button up");
  146.           break;
  147.     }
  148.     printf("\n");
  149. }
  150.  
  151.  
  152.  
  153. /* Show the current position of the mouse relative to the
  154.  * upper left-hand corner of our window
  155.  */
  156.  
  157. VOID   DoMouseMove(
  158.    struct RastPort *rp,         /* RastPort to write coordinates into */
  159.    struct IntuiMessage *msg)    /* IntuiMessage containing mouse coords */
  160. {
  161. UBYTE  coords[12];
  162.  
  163.     sprintf(coords,"X%4d Y%4d", msg->MouseX, msg->MouseY);
  164.     Move(rp,((WIDTH-88)/2),((HEIGHT-8)/2)+10);  /* ASSUMES 8 Pixel-High Font */
  165.     Text(rp,&coords[0],11);
  166. }
  167.  
  168.  
  169. /*
  170.  Show what keys where pressed...
  171.  */
  172.  
  173. VOID   DoKeys(struct IntuiMessage *msg)
  174. {
  175. register USHORT i;
  176. USHORT  numchars;
  177. UBYTE   buffer[BUFSIZE];
  178.  
  179.     strcpy(buffer,"               ");
  180.     numchars=(int)DeadKeyConvert(msg, &buffer[0], BUFSIZE, 0L);
  181.  
  182.     /* numchars now contains the number of characters placed 
  183.      * within the buffer.  If this was a message notifying us of a
  184.      * key release, then it shows zero.  Special keys (like HELP,
  185.      * the cursor keys, FKeys, etc) return multiple characters that
  186.      * have to then be parsed.
  187.      */
  188.  
  189.     if(numchars>0)
  190.     {
  191.         printf("key %d maps to %d character(s)\n", msg->Code, numchars);
  192.         for(i=0; i<numchars; i++)
  193.         {
  194.            printf("  %3d = %c\n", buffer[i], buffer[i]);
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. /*
  201.  Open up all the resources that we need
  202.  */
  203.  
  204. VOID    OpenAll(VOID)
  205. {
  206.     if(!(IntuitionBase=(struct IntuitionBase *)
  207.       OpenLibrary("intuition.library",33)))
  208.         cleanExit(ERROR_INVALID_RESIDENT_LIBRARY);
  209.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",33)))
  210.         cleanExit(ERROR_INVALID_RESIDENT_LIBRARY);
  211.  
  212.     /* must have the console.device opened to use RawKeyConvert()
  213.      */
  214.  
  215.     if(OpenDevice("console.device",-1L,(struct IORequest *)&ioreq,0L))
  216.         cleanExit(ERROR_DEVICE_NOT_MOUNTED);
  217.     ConsoleDevice=(struct ConsoleDevice *)ioreq.io_Device;
  218.  
  219.     /* center the window that we are going to use to display
  220.      * the current mouse position in.
  221.      */
  222.  
  223.     NewWindow.LeftEdge = (GfxBase->NormalDisplayColumns - WIDTH) / 2;
  224.     NewWindow.TopEdge = (GfxBase->NormalDisplayRows - HEIGHT) / 2;
  225.  
  226.     if(!(window=(struct Window *)OpenWindow(&NewWindow)))
  227.         cleanExit(ERROR_NO_FREE_STORE);
  228.  
  229.     /* initialize the drawing variables used for rendering the
  230.      * current mouse position 
  231.      */
  232.  
  233.     SetAPen(RP,1);
  234.     SetBPen(RP,0);
  235.     SetDrMd(RP,JAM2);
  236. }
  237.  
  238.  
  239. /*
  240.  Free up all the resources that we used
  241.  */
  242.  
  243. VOID    cleanExit(int retval)
  244. {
  245.     if(window)         CloseWindow(window);
  246.     if(ConsoleDevice)  CloseDevice((struct IORequest *)&ioreq);
  247.     if(GfxBase)        CloseLibrary((struct Library *)GfxBase);
  248.     if(IntuitionBase)  CloseLibrary((struct Library *)IntuitionBase);
  249.     exit(retval);
  250. }
  251.  
  252.  
  253. /*
  254.  Convert RAWKEYS into VANILLAKEYS, also shows
  255.  special keys like HELP, Cursor Keys, FKeys, etc.
  256.  
  257.  Returns:
  258.    -2 if not a RAWKEY event
  259.    -1 if not enough room in the buffer
  260.    the number of characters placed in the buffer
  261.  */
  262.  
  263. LONG   DeadKeyConvert(
  264. struct IntuiMessage *msg,
  265. UBYTE               *kbuffer,
  266. LONG                kbsize,
  267. struct KeyMap        *kmap
  268. )
  269. {
  270.     static struct InputEvent ievent = {NULL, IECLASS_RAWKEY,0,0,0};
  271.  
  272.     if(msg->Class != RAWKEY) return(-2);
  273.     ievent.ie_Code = msg->Code;
  274.     ievent.ie_Qualifier = msg->Qualifier;
  275.     ievent.ie_position.ie_addr = *((APTR*)msg->IAddress);
  276.     return(RawKeyConvert(&ievent,kbuffer,kbsize,kmap));
  277. }
  278.