home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / nkcc.lzh / TEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  20.3 KB  |  694 lines

  1. /*TAB=3***CHAR={ATARI}**********************************************************
  2. *
  3. *  Project name : NORMALIZED KEY CODE CONVERTER (NKCC)
  4. *  Module name  : Test utility
  5. *  Symbol prefix: -
  6. *
  7. *  Author       : Harald Siegmund (HS)
  8. *  Co-Authors   : -
  9. *  Write access : HS
  10. *
  11. *  Notes        : -
  12. *-------------------------------------------------------------------------------
  13. *  Things to do : -
  14. *
  15. *-------------------------------------------------------------------------------
  16. *  History:
  17. *
  18. *  1990:
  19. *     May 26: creation of file
  20. *     Oct 03: minor changes
  21. *     Nov 13: NK_LEFT and NK_RIGHT were exchanged
  22. *  1991:
  23. *     Jun 08: bug fixed (hitting space lead to a crash)
  24. *     Aug 22: deadkey flags changed
  25. *     Aug 26: NK_INVALID
  26. *     Sep 14: use NKF_IGNUM
  27. *     Nov 05: use Esc instead of 'C'
  28. *     Nov 16: Control key emulation
  29. *     Dec 29: NK_RVD...
  30. *  1992:
  31. *     Jan 12: don't install 200 Hz timer interrupt
  32. *             reorganizing source
  33. *  1993:
  34. *     Dec 11: new file header
  35. *             nkc_init: parameter added
  36. *             cosmetic changes
  37. *  1994:
  38. *     Jun 27: detect macro keys and display them a different way
  39. *
  40. *******************************************************************************/
  41. /*KEY _NAME="NKCC test utility" */
  42. /*END*/
  43.  
  44.  
  45. /**************************************************************************/
  46. /*                                                                        */
  47. /*                            INCLUDE FILES                               */
  48. /*                                                                        */
  49. /**************************************************************************/
  50.  
  51. /*START*/
  52. #include <stdio.h>
  53. #include <tos.h>
  54. #include "nkcc.h"
  55. /*END*/
  56.  
  57. /*KEY _END */
  58.  
  59.  
  60. /**************************************************************************/
  61. /*                                                                        */
  62. /*                               MACROS                                   */
  63. /*                                                                        */
  64. /**************************************************************************/
  65.  
  66.                                     /* ignore numeric key pad flag and */
  67.                                     /*  CapsLock when comparing key codes */
  68. #define F_IGNORE (NKF_IGNUM | NKF_CAPS)
  69.  
  70.  
  71. #define UNUSED(x) x = x             /* macro for unused formal parameters */
  72.  
  73.  
  74. #define TRUE      1                 /* status codes */
  75. #define FALSE     0
  76. #define NIL       (-1)
  77.  
  78.  
  79.  
  80. /**************************************************************************/
  81. /*                                                                        */
  82. /*                         INITIALIZED STATICS                            */
  83. /*                                                                        */
  84. /**************************************************************************/
  85.  
  86. static char *skeys[] =              /* special function keys */
  87.    {                                /* (index = NK_... defined in NKCC.H) */
  88.    "INVALID",
  89.    "UP",
  90.    "DOWN",
  91.    "RIGHT",
  92.    "LEFT",
  93.    "RESERVED (code 05)",
  94.    "RESERVED (code 06)",
  95.    "RESERVED (code 07)",
  96.    "Backspace",
  97.    "Tab",
  98.    "Enter",
  99.    "Insert",
  100.    "ClrHome",
  101.    "Return",
  102.    "Help",
  103.    "Undo",
  104.    "F1",
  105.    "F2",
  106.    "F3",
  107.    "F4",
  108.    "F5",
  109.    "F6",
  110.    "F7",
  111.    "F8",
  112.    "F9",
  113.    "F10",
  114.    "RESERVED (code 1A)",
  115.    "Esc",
  116.    "RESERVED (code 1C)",
  117.    "RESERVED (code 1D)",
  118.    "RESERVED (code 1E)",
  119.    "Delete"
  120.    };
  121.  
  122. static char *sasc[] =               /* special ASCII characters plus Space */
  123.    {                                /* (cannot be output via GEMDOS) */
  124.    "[ASCII null]",
  125.    "[up arrow]",
  126.    "[down arrow]",
  127.    "[right arrow]",
  128.    "[left arrow]",
  129.    "[window closer]",
  130.    "[window sizer]",
  131.    "[window fuller]",
  132.    "[check mark]",
  133.    "[clock]",
  134.    "[bell]",
  135.    "[note]",
  136.    "[form feed]",
  137.    "[carriage return]",
  138.    "[Atari logo, left part]",
  139.    "[Atari logo, right part]",
  140.    "[decimal 0]",
  141.    "[decimal 1]",
  142.    "[decimal 2]",
  143.    "[decimal 3]",
  144.    "[decimal 4]",
  145.    "[decimal 5]",
  146.    "[decimal 6]",
  147.    "[decimal 7]",
  148.    "[decimal 8]",
  149.    "[decimal 9]",
  150.    "[\"a\"-like character]",
  151.    "[escape]",
  152.    "[face, upper left part]",
  153.    "[face, upper right part]",
  154.    "[face, lower left part]",
  155.    "[face, lower right part]",
  156.    "Space"
  157.    };
  158.  
  159. static unsigned char *dead_tab =    /* deadkey table */
  160.    "^~\'`¨\"°,/";
  161.  
  162. static char *o_on  = "on",          /* option status */
  163.             *o_off = "off";
  164.  
  165.  
  166.  
  167. /**************************************************************************/
  168. /*                                                                        */
  169. /*                          LOCAL PROTOTYPES                              */
  170. /*                                                                        */
  171. /**************************************************************************/
  172.  
  173.    /* display key code */
  174. static void display_key(int key);
  175.  
  176.    /* enter option to enable/disable */
  177. static unsigned long enter_option(unsigned long sflags);
  178.  
  179.    /* handle command: enable option */
  180. static int enable_opt(unsigned long *sflags);
  181.  
  182.    /* handle command: disable option */
  183. static int disable_opt(unsigned long *sflags);
  184.  
  185.    /* handle command: show help text */
  186. static int show_help(unsigned long *sflags);
  187.  
  188.    /* handle command: quit program */
  189. static int quit_program(unsigned long *sflags);
  190.  
  191.    /* handle command: show option status */
  192. static int show_options(unsigned long *sflags);
  193.  
  194.    /* handle command: leave command mode */
  195. static int leave_cmd(unsigned long *sflags);
  196.  
  197.    /* command mode handler */
  198. static int command_mode(unsigned long *sflags);
  199.  
  200.  
  201.  
  202. /**************************************************************************/
  203. /*                                                                        */
  204. /*                          LOCAL FUNCTIONS                               */
  205. /*                                                                        */
  206. /**************************************************************************/
  207.  
  208. /***************************************************************************
  209. *
  210. *  display_key: display key code
  211. *
  212. *  A literal description of the given normalized key code is displayed on
  213. *  the screen.
  214. *
  215. *  Out:
  216. *        -
  217. *
  218. ***************************************************************************/
  219.  
  220. static void display_key(key)
  221.  
  222. int         key;                    /* normalized key code */
  223. {     /* display_key() */
  224.  
  225.    int      ascii;                  /* ASCII part of key code */
  226.  
  227.  
  228.    ascii = key & 0xff;              /* isolate ASCII part */
  229.  
  230.    printf("NKC=$%04x:",key);        /* print complete key code as 4-digit */
  231.                                     /*  hex number */
  232.  
  233.    if (key & NKF_ALT)               /* print Alternate and Control key flags */
  234.       printf(" Alternate");
  235.  
  236.    if (key & NKF_CTRL)
  237.       printf(" Control");
  238.  
  239.    switch (key & NKF_SHIFT)         /* print Shift key flags */
  240.       {
  241.       case NKF_LSH:                 /* only left Shift key pressed */
  242.          printf(" left Shift");
  243.          break;
  244.  
  245.       case NKF_RSH:                 /* only right Shift key pressed */
  246.          printf(" right Shift");
  247.          break;
  248.  
  249.       case NKF_SHIFT:               /* both */
  250.          printf(" left & right Shift");
  251.          break;
  252.       }
  253.  
  254.    if (key & NKF_NUM)               /* print numeric keypad flag */
  255.       printf(" numeric");
  256.  
  257.                                     /* macro key? */
  258.    if ((key & (NKF_FUNC | NKF_ALT | NKF_CTRL)) == NKF_FUNC && ascii >= 32)
  259.       printf(" macro key $%02x (%c)",ascii,ascii);
  260.    else if (ascii > 32)             /* printable character except space? */
  261.       printf(" %c",ascii);          /* show it */
  262.    else if (key < 0 && ascii != 32) /* "function key", not space? */
  263.       printf(" %s",skeys[ascii]);   /* display function key name */
  264.    else                             /* else: printable character with ASCII */
  265.       printf(" %s",sasc[ascii]);    /*  code less than or equal 32: display */
  266.                                     /*  its name */
  267.  
  268.    if (key & NKF_CAPS)              /* print CapsLock flag */
  269.       printf(" (CapsLock)");
  270.  
  271.    printf("\n");                    /* EOL */
  272.  
  273. }     /* display_key() */
  274.  
  275.  
  276.  
  277.  
  278. /***************************************************************************
  279. *
  280. *  enter_option: enter option to enable/disable
  281. *
  282. *  Out:
  283. *        option mask (NKS_...; 0 = aborted)
  284. *
  285. ***************************************************************************/
  286.  
  287. static unsigned long enter_option(sflags)
  288.  
  289. unsigned long sflags;               /* special key flags (NKS_...) */
  290. {     /* enter_option() */
  291.  
  292.    static struct                    /* table of options' key codes and */
  293.       {                             /*  flag masks */
  294.       int   key;                    /* normalized key code */
  295.       unsigned long mask;           /* key flag mask */
  296.       char  *s;                     /* text to print */
  297.       } keytab[] =
  298.       {
  299.          {NKF_FUNC | NK_BS,0,"\033D \033D"}, /* Backspace (abort): cursor */
  300.                                              /*  left, Space, cursor left */
  301.          {F_IGNORE | 'A',NKS_ALTNUM,"A\n"},  /* direct ASCII input */
  302.          {F_IGNORE | 'C',NKS_CTRL,"C\n"},    /* control key emulation */
  303.          {F_IGNORE | 'D',NKS_DEADKEY,"D\n"}, /* all deadkeys */
  304.          {NK_TERM,0}                         /* terminator */
  305.       };
  306.  
  307.    int      key,                    /* normalized key code */
  308.             i,
  309.             a1;
  310.    unsigned long mask;              /* key flag mask */
  311.  
  312.  
  313.    nkc_set(sflags & ~NKS_DEADKEY);  /* disable deadkeys temporary */
  314.  
  315.    do {
  316.       key = nkc_conin();            /* get key code */
  317.  
  318.                                     /* find mask in table */
  319.       for (i = a1 = 0; keytab[i].key != NK_TERM; i++)
  320.          if (a1 = nkc_cmp(keytab[i].key,key))
  321.             {
  322.             mask = keytab[i].mask;  /* found: get flag mask */
  323.             printf(keytab[i].s);    /* print text */
  324.             break;                  /* exit loop */
  325.             }
  326.  
  327.                                     /* not found: search deadkey table */
  328.       if (!a1) for (i = 0; dead_tab[i]; i++)
  329.          if (a1 = nkc_cmp(F_IGNORE | dead_tab[i],key))
  330.             {
  331.             mask = 0x10000UL << i;  /* found: compute mask and print character*/
  332.             printf("%c\n",dead_tab[i]);
  333.             break;                  /* exit loop */
  334.             }
  335.  
  336.       if (!a1) Cconout(BEL);        /* still not found? bing! */
  337.  
  338.       } while (!a1);                /* while not aborted */
  339.  
  340.  
  341.    nkc_set(sflags);                 /* restore NKCC configuration */
  342.  
  343.    return mask;                     /* return flag mask */
  344.  
  345. }     /* enter_option() */
  346.  
  347.  
  348.  
  349.  
  350. /***************************************************************************
  351. *
  352. *  enable_opt: handle command: enable option
  353. *
  354. *  Out:
  355. *        exit status:
  356. *        TRUE        leave command mode
  357. *        FALSE       continue
  358. *        NIL         leave command mode AND program
  359. *
  360. *        updated special key flags in *sflags
  361. *
  362. ***************************************************************************/
  363.  
  364. static int enable_opt(sflags)
  365.  
  366. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  367. {     /* enable_opt() */
  368.  
  369.    printf("+");                        /* enable */
  370.  
  371.    *sflags |= enter_option(*sflags);   /* get option and set its flag */
  372.    nkc_set(*sflags);                   /* reconfigure NKCC */
  373.  
  374.    return FALSE;
  375.  
  376. }     /* enable_opt() */
  377.  
  378.  
  379.  
  380.  
  381. /***************************************************************************
  382. *
  383. *  disable_opt: handle command: disable option
  384. *
  385. *  Out:
  386. *        exit status:
  387. *        TRUE        leave command mode
  388. *        FALSE       continue
  389. *        NIL         leave command mode AND program
  390. *
  391. *        updated special key flags in *sflags
  392. *
  393. ***************************************************************************/
  394.  
  395. static int disable_opt(sflags)
  396.  
  397. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  398. {     /* disable_opt() */
  399.  
  400.    printf("-");                        /* disable */
  401.  
  402.    *sflags &= ~enter_option(*sflags);  /* get option and clear its flag */
  403.    nkc_set(*sflags);                   /* reconfigure NKCC */
  404.  
  405.    return FALSE;
  406.  
  407. }     /* disable_opt() */
  408.  
  409.  
  410.  
  411.  
  412. /***************************************************************************
  413. *
  414. *  show_help: handle command: show help text
  415. *
  416. *  Out:
  417. *        exit status:
  418. *        TRUE        leave command mode
  419. *        FALSE       continue
  420. *        NIL         leave command mode AND program
  421. *
  422. *        updated special key flags in *sflags
  423. *
  424. ***************************************************************************/
  425.  
  426. static int show_help(sflags)
  427.  
  428. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  429. {     /* show_help() */
  430.  
  431.    int      i;
  432.  
  433.  
  434.    UNUSED(sflags);
  435.  
  436.    printf("H\n\n"                   /* just show text */
  437.           "Commands:\n"
  438.           "   Esc   leave command mode\n"
  439.           "   +x    enable option x\n"
  440.           "   -x    disable option x\n"
  441.           "   s     show option status\n"
  442.           "   q     quit program\n"
  443.           "   h     show this help text!\n"
  444.           "\n"
  445.           "Options:\n"
  446.           "   A     direct ASCII input\n"
  447.           "   C     Control key emulation\n"
  448.           "   D     ALL deadkeys\n"
  449.           "   %c     one specific deadkey\n",dead_tab[0]);
  450.  
  451.    for (i = 1; dead_tab[i]; printf("   %c\n",dead_tab[i++]));
  452.  
  453.    printf("\n");                    /* EOL */
  454.  
  455.    return FALSE;
  456.  
  457. }     /* show_help() */
  458.  
  459.  
  460.  
  461.  
  462. /***************************************************************************
  463. *
  464. *  quit_program: handle command: quit program
  465. *
  466. *  Out:
  467. *        exit status:
  468. *        TRUE        leave command mode
  469. *        FALSE       continue
  470. *        NIL         leave command mode AND program
  471. *
  472. *        updated special key flags in *sflags
  473. *
  474. ***************************************************************************/
  475.  
  476. static int quit_program(sflags)
  477.  
  478. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  479. {     /* quit_program() */
  480.  
  481.    UNUSED(sflags);
  482.  
  483.    printf("Q\n\nBye\n");
  484.  
  485.    return NIL;
  486.  
  487. }     /* quit_program() */
  488.  
  489.  
  490.  
  491.  
  492. /***************************************************************************
  493. *
  494. *  show_options: handle command: show option status
  495. *
  496. *  Out:
  497. *        exit status:
  498. *        TRUE        leave command mode
  499. *        FALSE       continue
  500. *        NIL         leave command mode AND program
  501. *
  502. *        updated special key flags in *sflags
  503. *
  504. ***************************************************************************/
  505.  
  506. static int show_options(sflags)
  507.  
  508. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  509. {     /* show_options() */
  510.  
  511.    int      i;
  512.  
  513.                                     /* status of ASCII input */
  514.    printf("S\n\n   ASCII input:  %s\n",
  515.       (*sflags & NKS_ALTNUM) ? o_on : o_off);
  516.  
  517.                                     /* status of control key emulation */
  518.    printf("Ctrl emulation:  %s\n",
  519.       (*sflags & NKS_CTRL) ? o_on : o_off);
  520.  
  521.                                     /* status of deadkeys */
  522.    for (i = 0; dead_tab[i]; i++)
  523.       printf("     %c deadkey:  %s\n",
  524.          dead_tab[i],
  525.          (*sflags & (0x10000UL << i)) ? o_on : o_off);
  526.  
  527.    printf("\n");                    /* EOL */
  528.  
  529.    return FALSE;
  530.  
  531. }     /* show_options() */
  532.  
  533.  
  534.  
  535.  
  536. /***************************************************************************
  537. *
  538. *  leave_cmd: handle command: leave command mode
  539. *
  540. *  Out:
  541. *        exit status:
  542. *        TRUE        leave command mode
  543. *        FALSE       continue
  544. *        NIL         leave command mode AND program
  545. *
  546. *        updated special key flags in *sflags
  547. *
  548. ***************************************************************************/
  549.  
  550. static int leave_cmd(sflags)
  551.  
  552. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  553. {     /* leave_cmd() */
  554.  
  555.    UNUSED(sflags);
  556.  
  557.    printf("Esc\n\n");
  558.  
  559.    return TRUE;
  560.  
  561. }     /* leave_cmd() */
  562.  
  563.  
  564.  
  565.  
  566. /***************************************************************************
  567. *
  568. *  command_mode: command mode handler
  569. *
  570. *  The program enters the command mode. Hitting some specific keys will
  571. *  perform special tasks. See table <keytab> defined below. Unknown
  572. *  keys respectively key combinations are ignored.
  573. *
  574. *  Out:
  575. *        exit status:
  576. *        TRUE        leave program
  577. *        FALSE       continue
  578. *
  579. ***************************************************************************/
  580.  
  581. static int command_mode(sflags)
  582.  
  583. unsigned long *sflags;              /* ^ to special key flags (NKS_...) */
  584. {     /* command_mode() */
  585.  
  586.    static struct                    /* table of supported key codes and */
  587.       {                             /*  their handlers */
  588.       int   key;                    /* normalized key code */
  589.       int   (*pf)(                  /* ^ to handler for that key code */
  590.              unsigned long *sflags);
  591.       } keytab[] =
  592.       {
  593.          {F_IGNORE | '+',enable_opt},        /* enable option */
  594.          {F_IGNORE | '-',disable_opt},       /* disable option */
  595.          {F_IGNORE | 'H',show_help},         /* show help text */
  596.          {F_IGNORE | 'Q',quit_program},      /* quit program */
  597.          {F_IGNORE | 'S',show_options},      /* show option status */
  598.          {NKF_FUNC | NK_ESC,leave_cmd},      /* leave command mode */
  599.          {NK_TERM,NULL}                      /* terminator */
  600.       };
  601.  
  602.    int      key,                    /* normalized key code */
  603.             i,
  604.             a1;
  605.  
  606.  
  607.    printf("\n");                    /* blank line */
  608.  
  609.    do {                             /* receive and evaluate key codes */
  610.       printf("\rEnter command (h=help): ");
  611.  
  612.       key = nkc_conin();            /* get key */
  613.  
  614.                                     /* find handler for that key */
  615.       for (i = a1 = 0; keytab[i].key != NK_TERM; i++)
  616.          if (a1 = nkc_cmp(keytab[i].key,key))
  617.             break;                  /* found: exit loop */
  618.  
  619.       if (a1)                       /* found? */
  620.          a1 = keytab[i].pf(sflags); /* then call handler */
  621.       else                          /* unknown key code: */
  622.          Cconout(BEL);              /* bing! */
  623.  
  624.       } while (!a1);                /* while not aborted */
  625.  
  626.    a1 = (a1 < 0);                   /* a1<0: quit program */
  627.  
  628.    return a1;
  629.  
  630. }     /* command_mode() */
  631.  
  632.  
  633.  
  634. /**************************************************************************/
  635. /*                                                                        */
  636. /*                          GLOBAL FUNCTIONS                              */
  637. /*                                                                        */
  638. /**************************************************************************/
  639.  
  640. /*START*/
  641. /***************************************************************************
  642. *
  643. *  main: program entry point
  644. *
  645. *  Out:
  646. *        -
  647. *
  648. ***************************************************************************/
  649.  
  650. int main()
  651. {     /* main() */
  652. /*END*/
  653.  
  654.    int      key,                    /* normalized key code */
  655.             a1;
  656.    unsigned long sflags;            /* special key flags (NKS_...) */
  657.  
  658.  
  659.  
  660.    a1 = nkc_init(NKI_NO200HZ,0,NULL);  /* init NKCC */
  661.  
  662.                                     /* show header */
  663.    printf("NKCC test utility   (c) 1989-1993 Harald Siegmund\n"
  664.           "Using NKCC version %x.%02x\n"
  665.           "Press Esc to enter command mode\n\n",a1 >> 8,a1 & 0xff);
  666.  
  667.    sflags = 0;                      /* init special flags */
  668.  
  669.  
  670.    do {                             /* receive and evaluate key codes */
  671.       key = nkc_conin();            /* get key */
  672.  
  673.       display_key(key);             /* display it on the screen */
  674.  
  675.                                     /* Escape key? then enter command mode */
  676.       if (a1 = nkc_cmp(NKF_FUNC | NK_ESC,key))
  677.          a1 = command_mode(&sflags);
  678.  
  679.       } while (!a1);                /* while not aborted */
  680.  
  681.  
  682.    a1 = nkc_exit();                 /* exit */
  683.  
  684.    if (a1)                          /* exit error? (strange!) display it */
  685.       printf("\nFATAL EXIT ERROR #%d\n",a1);
  686.  
  687.    return 0;
  688.  
  689. }     /* main() */
  690.  
  691.  
  692. /* EOF */
  693.  
  694.