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