home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / xi-25 / appmap_demo.c < prev   
C/C++ Source or Header  |  1996-01-30  |  9KB  |  286 lines

  1. ;/* appmap_demo.c - Execute me to compile me with SAS/C 6.56
  2. sc data=far nominc strmer streq nostkchk saveds ign=73 appmap_demo.c
  3. slink FROM LIB:c.o,appmap_demo.o,appkeymap.o TO appmap_demo LIB LIB:SC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /* Appmap_demo uses the routines in appkeymap.o to clone the current  */
  8. /* task's console keymap.  Appmap_demo then modifies the keymap clone */
  9. /* by redefining some of the keypad keys.  It installs a string key,  */
  10. /* a "normal" key, and a NO OP key, which are acceptable replacements */
  11. /* for keypad keys on a console-based application.                    */
  12. /*                                                                    */
  13. /* This example also installs a dead/deadable key pair and a double   */
  14. /* dead/deadable key pair.  These are provided only as a means for    */
  15. /* understanding how dead-class keys work.  Do not use them as part   */
  16. /* of anything for public consumption.                                */
  17.  
  18. #include       <exec/types.h>
  19. #include       <exec/memory.h>
  20. #include       <exec/io.h>
  21. #include       <dos/dos.h>
  22. #include       <devices/console.h>
  23. #include       <devices/keymap.h>
  24.  
  25. #include       <clib/exec_protos.h>
  26. #include       <clib/dos_protos.h>
  27.  
  28. #include       <stdio.h>
  29. #include       <stdlib.h>
  30.  
  31. #ifdef  __SASC
  32. void __regargs __chkabort(void);
  33. void __regargs __chkabort(void){}
  34. #endif
  35.  
  36. #include "appkeymap.h"
  37.  
  38. #define MYKEYCOUNT 7
  39.  
  40. far UBYTE deadmap[] =           /* The data for the dead key (the introducer) */
  41. {
  42.   0, '4',                       /* maps to '4' when keypad 4 is pressed alone */
  43.   DPF_DEAD, 1                   /* maps to a dead key when shift is down      */
  44. };
  45.  
  46. far UBYTE deadablemap[] =
  47. {
  48.   0, '5',                       /* maps to '5' when keypad 5 is pressed alone */
  49.   DPF_MOD, 4,                   /* maps to a deadable key when shift is down  */
  50.   '5', 0xC2, '5', '5', '5'      /* Entry 1 in this table maps to 0xC2, the    */
  51. };                              /* others map to '5'.                         */
  52.  
  53. far UBYTE doubledeadmap[] =
  54. {
  55.   0, '6',                              /* maps to '6' when keypad 6 is pressed alone */
  56.   DPF_DEAD, 1 | (2 << DP_2DFACSHIFT)   /* maps to a dead key when shift is down      */
  57. };
  58.  
  59. far UBYTE doubledeadablemap[] =
  60. {
  61.   0, '7',                       /* maps to '7' when keypad 7 is pressed alone */
  62.   DPF_MOD, 4,                   /* maps to a deadable key when shift is down  */
  63.   '7', '7', 0xA5, 0xA9, '7'     /* Entry 2 in this table maps to $A5, entry 3 */
  64. };                              /* to $A9, the others to '7'.                 */
  65.  
  66.  
  67. UBYTE strings[] =
  68. {
  69.         5,16,
  70.         5,21,
  71.         3,26,
  72.         9,29,
  73.         4,38,
  74.         10,42,
  75.         8,52,
  76.         14,60,
  77.         'p','l','a','i','n',
  78.         's','h','i','f','t',
  79.         'a','l','t',
  80.         's','h','i','f','t','+','a','l','t',
  81.         'c','t','r','l',
  82.         'c','t','r','l','+','s','h','i','f','t',
  83.         'c','t','r','l','+','a','l','t',
  84.         'c','t','r','l','+','a','l','t','+','s','h','i','f','t'
  85. };
  86.  
  87.  
  88. struct MyKey mykeys[MYKEYCOUNT] =
  89. {
  90.   { N1_KEY,                    /* The '1' from the numeric key pad                 */
  91.     KCF_STRING | KC_VANILLA,   /* This is a string key that accepts all qualifiers */
  92.     1, 1,                      /* Capsable and Repeatable                          */
  93.     (ULONG) strings            /* This points to the key's string data table       */
  94.   },
  95.  
  96.  
  97.   { N2_KEY,     /* The '2' from the numeric key pad                           */
  98.     KC_VANILLA, /* VANILLA key generating shift-alted, alted, shifted, plain  */
  99.     0, 0,       /* Non-Capsable, Non-Repeatable                               */
  100.     0x26252423  /* This long word is four ANSI codes: $26=& $25=% $24=$ $23=# */
  101.   },
  102.  
  103.  
  104.   { N3_KEY,     /* The '3' from the numeric key pad */
  105.     KCF_NOP,    /* This key is a NO OPeration key   */
  106.     0, 0,       /* Non-Capsable, Non-Repeatable     */
  107.     0L
  108.   },
  109.  
  110.  
  111.   { N4_KEY,                /* The '4' from the numeric key pad */
  112.     KCF_DEAD | KCF_SHIFT,  /* A shiftable Dead-class key       */
  113.     0, 0,                  /* Non-Capsable, Non-Repeatable     */
  114.     (ULONG) deadmap        /* pointer to dead-class key data   */
  115.   },
  116.  
  117.  
  118.   { N5_KEY,                /* The '5' from the numeric key pad */
  119.     KCF_DEAD | KCF_SHIFT,  /* A shiftable Dead-class key       */
  120.     0, 0,                  /* Non-Capsable, Non-Repeatable     */
  121.     (ULONG) deadablemap    /* pointer to dead-class key data   */
  122.   },
  123.  
  124.  
  125.   { N6_KEY,                /* The '6' from the numeric key pad */
  126.     KCF_DEAD | KCF_SHIFT,  /* A shiftable Dead-class key       */
  127.     0, 0,                  /* Non-Capsable, Non-Repeatable     */
  128.     (ULONG) doubledeadmap  /* pointer to dead-class key data   */
  129.   },
  130.  
  131.  
  132.   { N7_KEY,                   /* The '7' from the numeric key pad */
  133.     KCF_DEAD | KCF_SHIFT,     /* A shiftable Dead-class key       */
  134.     0, 0,                     /* Non-Capsable, Non-Repeatable     */
  135.     (ULONG) doubledeadablemap /* pointer to dead-class key data   */
  136.   }
  137. };
  138.  
  139.  
  140. extern struct Library *SysBase;
  141.  
  142. struct KeyMap *appkeymap, defkeymap;
  143.  
  144. struct IOStdReq *conio = NULL;
  145. struct MsgPort *replyport = NULL;
  146.  
  147. /* prototypes for our program functions */
  148.  
  149. void closeall (void);
  150. void closeout (UBYTE * errstring, LONG rc);
  151. struct IOStdReq *makeio (void);
  152. void freeio (struct IOStdReq *ior);
  153.  
  154.  
  155. void
  156. main (int argc, char **argv)
  157. {
  158.   LONG rc = 0;
  159.  
  160. /* This example is shell-only. It modifies a shell's keymap while running. */
  161. /* Alternately, you could modify this demo code to change the keymap       */
  162. /* of the console.device of another CON: window OR a console.device        */
  163. /* unit you've attached to your own Intuition window.  You need the        */
  164. /* device and unit pointers for the console unit you wish to affect.       */
  165.  
  166.   if (!argc)
  167.     exit (RETURN_FAIL);
  168.  
  169.   if (SysBase->lib_Version < 37)
  170.     closeout ("Kickstart 2.0 required", RETURN_FAIL);
  171.  
  172.   conio = makeio ();
  173.   if (conio == NULL)
  174.     closeout ("Can't create IORequest", RETURN_FAIL);
  175.  
  176.  
  177.   conio->io_Command = CD_ASKKEYMAP;          /* Obtain a copy of the    */
  178.   conio->io_Data = (APTR) & defkeymap;       /* shell console's KeyMap. */
  179.   conio->io_Length = sizeof (struct KeyMap);
  180.   DoIO (conio);
  181.  
  182.   appkeymap = CreateAppKeyMap (&defkeymap);
  183.  
  184.   if (appkeymap == NULL)
  185.     closeout ("Can't create keymap", RETURN_FAIL);
  186.  
  187. /* If we get here, all went OK.  We now have appkeymap and defkeymap, and  */
  188. /* conio IOStdRequest is init'd to talk to our shell's console device unit */
  189.  
  190.   AlterAppKeyMap (appkeymap, mykeys, MYKEYCOUNT);
  191.  
  192.   conio->io_Command = CD_SETKEYMAP;      /* Set the keymap for the console */
  193.   conio->io_Data = (APTR) appkeymap;     /* to be the modified clone.      */
  194.   conio->io_Length = sizeof (struct KeyMap);
  195.   DoIO (conio);
  196.  
  197.   printf ("Appkeymap installed for this console.\n");
  198.   printf ("Changes for our keymap were specified in an array in appmap_demo.c\n\n");
  199.   printf ("Numeric pad 1 is now a repeatable and capsable string key.  It\n");
  200.   printf ("    also prints a different string for all qualifier combos.\n");
  201.   printf ("Numeric pad 2 is different chars if normal, shifted, alted, shift-alted.\n");
  202.   printf ("Numeric pad 3 is disabled.\n");
  203.   printf ("Shift-Numeric pad 4 is a dead-key.  Shift-Numeric pad 5 is a deadable key.\n");
  204.   printf ("    When you hit Shift-Numeric pad 4 then Shift-Numeric pad 5, this\n");
  205.   printf ("    example prints '\302'\n");
  206.   printf ("Shift-Numeric pad 6 is a double dead-key.  Shift-Numeric pad 7 is a deadable\n");
  207.   printf ("    key.  When you hit Shift-Numeric pad 6 once then Shift-Numeric pad 7,\n");
  208.   printf ("    this example prints '\245'.  When you hit Shift-Numeric pad 6 twice then\n");
  209.   printf ("    Shift-Numeric pad 7, this example prints '\251'.\n");
  210.   printf ("\nHit return to exit when done\n");
  211.   getchar ();
  212.   printf ("Setting this console back to default keymap\n");
  213.  
  214.   conio->io_Command = CD_SETKEYMAP;          /* Restore the shell's original keymap */
  215.   conio->io_Data = (APTR) & defkeymap;
  216.   conio->io_Length = sizeof (struct KeyMap);
  217.   DoIO (conio);
  218.  
  219.   closeall ();
  220.   exit (rc);
  221. }
  222.  
  223.  
  224.  
  225. struct IOStdReq *
  226. makeio (void)
  227. {
  228.   struct MsgPort *conport;
  229.   struct IOStdReq *ior = NULL;
  230.   struct InfoData *id;
  231.   struct Process *proc;
  232.  
  233.   proc = (struct Process *) FindTask (NULL);
  234.   conport = (struct MsgPort *) proc->pr_ConsoleTask;
  235.   if (!conport)
  236.     return (NULL);
  237.  
  238.   if (id = (struct InfoData *)
  239.       AllocMem (sizeof (struct InfoData), MEMF_PUBLIC | MEMF_CLEAR))
  240.   {
  241.     if (DoPkt (conport, ACTION_DISK_INFO, ((ULONG) id) >> 2, 0L, 0L, 0L, 0L))
  242.     {
  243.       if (replyport = CreateMsgPort ())
  244.       {
  245.         if (ior = CreateIORequest (replyport, sizeof (struct IOStdReq)))
  246.         {
  247.           ior->io_Device = ((struct IOStdReq *) id->id_InUse)->io_Device;
  248.           ior->io_Unit = ((struct IOStdReq *) id->id_InUse)->io_Unit;
  249.         }
  250.       }
  251.     }
  252.     FreeMem (id, sizeof (struct InfoData));
  253.   }
  254.   return (ior);
  255. }
  256.  
  257. void
  258. freeio (struct IOStdReq *ior)
  259. {
  260.   if (ior)
  261.   {
  262.     if (ior->io_Message.mn_ReplyPort)
  263.       DeleteMsgPort (ior->io_Message.mn_ReplyPort);
  264.     DeleteIORequest (ior);
  265.   }
  266. }
  267.  
  268. void
  269. closeall ()
  270. {
  271.   if (conio)
  272.     freeio (conio);
  273.   if (appkeymap)
  274.     DeleteAppKeyMap (appkeymap);
  275. }
  276.  
  277.  
  278. void
  279. closeout (UBYTE * errstring, LONG rc)
  280. {
  281.   if (*errstring)
  282.     printf ("%s\n", errstring);
  283.   closeall ();
  284.   exit (rc);
  285. }
  286.