home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNU_C++ / LIB / NKCC-293.LZH / DEMO / DEMO.C next >
Encoding:
C/C++ Source or Header  |  1995-11-09  |  20.4 KB  |  698 lines

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