home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / keytoc.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  9KB  |  329 lines

  1. /* Keyboard support routines.
  2.     for OS/2 system.
  3.  
  4.    20. April 97: Alexander Dong (ado)
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.    
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  19.  
  20. */
  21.  
  22. #include <config.h>
  23.  
  24.  
  25. #define INCL_BASE
  26. #define INCL_NOPM
  27. #define INCL_VIO
  28. #define INCL_KBD
  29. #define INCL_DOS
  30. #define INCL_DOSERRORS
  31. #define INCL_WININPUT
  32. #include <os2.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sys/select.h>
  36. #include <sys/time.h>
  37. #include <errno.h>
  38. #include "src/mouse.h"
  39. #include "src/global.h"
  40. #include "src/main.h"
  41. #include "src/key.h"
  42. #include "src/main.h"
  43. #include "src/tty.h"
  44.  
  45.  
  46. /* Code to read keystrokes in a separate thread */
  47.  
  48. typedef struct kbdcodes {
  49.   UCHAR ascii;
  50.   UCHAR scan;
  51.   USHORT shift;  /* .ado: change for mc */
  52. } KBDCODES;
  53.  
  54.  
  55. int VKtoCurses (int vkcode);
  56.  
  57. /* -------------------------------------------------------------- */
  58. /* DEFINITIONS:
  59.       Return from SLANG: KeyCode:  0xaaaabbcc
  60.  
  61.       where: aaaa = Flags
  62.              bb   = Scan code
  63.              cc   = ASCII-code (if available)
  64.  
  65.    if no flags (CTRL and ALT) is set, cc will be returned.
  66.    If CTRL is pressed, cc is already the XCTRL(code).
  67.    case cc is:
  68.        0xE0: The scan code will be used for the following keys:
  69.               Insert:  0x52,      DEL:       0x53,
  70.               Page_Up: 0x49,      Page_Down: 0x51,
  71.               Pos1:    0x47,      Ende:      0x4F,
  72.               Up:      0x48,      Down:      0x50,
  73.               Left:    0x4B,      Right:     0x4D,
  74.  
  75.        0x00: The function keys are defined as:
  76.                 F1: 3b00, F2: 3c00 ... F10: 4400, F11: 8500, F12: 8600.
  77.            With ALT-bit set:
  78.            ALT(F1): 6800, 6900,... ALT(F10): 7100, ALT(F11): 8b00
  79.                                                    ALT(F12): 8c00
  80.  
  81.            Mapping for ALT(key_code):
  82.                 For Mapping with normal keys, only the scan code can be 
  83.                 used. (see struct ALT_table)
  84.         
  85.    Special keys:
  86.         ENTER (number block): 0xaaaaE00D
  87.         + (number block):     0xaaaa4E2B        Normal: 1B2B
  88.         - (number block):     0xaaaa4A2D        Normal: 352D
  89.         * (number block):     0xaaaa372A        Normal: 1B2A
  90.         / (number block):     0xaaaaE02F
  91. */
  92. /* -------------------------------------------------------------- */
  93. #define RIGHT_SHIFT_PRESSED             1
  94. #define LEFT_SHIFT_PRESSED              2
  95. #define CTRL_PRESSED                    4
  96. #define ALT_PRESSED                     8
  97. #define SCROLL_LOCK_MODE                16
  98. #define NUM_LOCK_MODE                   32
  99. #define CAPS_LOCK_MODE                  64
  100. #define INSERT_MODE                     128
  101. #define LEFT_CTRL_PRESSED               256
  102. #define LEFT_ALT_PRESSED                512
  103. #define RIGHT_CTRL_PRESSED              1024
  104. #define RIGHT_ALT_PRESSED               2048
  105. #define SCROLL_LOCK_PRESSED             4096
  106. #define NUM_LOCK_PRESSED                8192
  107. #define CAPS_LOCK_PRESSED               16384
  108. #define SYSREQ                          32768
  109. /* -------------------------------------------------------------- */
  110.  
  111. /* Static Tables */
  112. static
  113. struct {
  114.     int key_code;
  115.     int vkcode;
  116. } fkt_table [] = {
  117.     { KEY_F(1),  0x3B },
  118.     { KEY_F(2),  0x3C },
  119.     { KEY_F(3),  0x3D },
  120.     { KEY_F(4),  0x3E },
  121.     { KEY_F(5),  0x3F },
  122.     { KEY_F(6),  0x40 },
  123.     { KEY_F(7),  0x41 },
  124.     { KEY_F(8),  0x42 },
  125.     { KEY_F(9),  0x43 },
  126.     { KEY_F(10), 0x44 },
  127.     { KEY_F(11), 0x85 },
  128.     { KEY_F(12), 0x86 },
  129.     { KEY_F(11), 0x54 },  /* Shift-F */
  130.     { KEY_F(12), 0x55 },
  131.     { KEY_F(13), 0x56 },
  132.     { KEY_F(11), 0x57 },
  133.     { KEY_F(12), 0x58 },
  134.     { KEY_F(13), 0x59 },
  135.     { KEY_F(11), 0x5A },
  136.     { KEY_F(12), 0x5B },
  137.     { KEY_F(13), 0x5C },
  138.     { KEY_F(11), 0x5D },
  139.     { 0, 0}
  140. };        
  141.  
  142. static
  143. struct {
  144.     int key_code;
  145.     int vkcode;
  146. } ALT_table [] = {
  147.     { ALT('1'),  0x78 },
  148.     { ALT('2'),  0x79 },
  149.     { ALT('3'),  0x7A },
  150.     { ALT('4'),  0x7B },
  151.     { ALT('5'),  0x7C },
  152.     { ALT('6'),  0x7D },
  153.     { ALT('7'),  0x7E },
  154.     { ALT('8'),  0x7F },
  155.     { ALT('9'),  0x80 },
  156.     { ALT('0'),  0x81 },
  157. /*  { ALT('-'),  0x82 },  only numpad '+' '-' '*' */
  158. /*  { ALT('\\'), 0x2B },  only numpad '+' '-' '*' */
  159.     { ALT('='),  0x83 },
  160.     { ALT('*'),  0x37 }, /* numpad */
  161.     { ALT('-'),  0x4A }, /* numpad */
  162.     { ALT('+'),  0x4E }, /* numpad */
  163.     { ALT('/'),  0xA4 }, /* numpad */
  164.     { ALT('a'),  0x1E },
  165.     { ALT('b'),  0x30 },
  166.     { ALT('c'),  0x2E },
  167.     { ALT('d'),  0x20 },
  168.     { ALT('e'),  0x12 },
  169.     { ALT('f'),  0x21 },
  170.     { ALT('g'),  0x22 },
  171.     { ALT('h'),  0x23 },
  172.     { ALT('i'),  0x17 },
  173.     { ALT('j'),  0x24 },
  174.     { ALT('k'),  0x25 },
  175.     { ALT('l'),  0x26 },
  176.     { ALT('m'),  0x32 },
  177.     { ALT('n'),  0x31 },
  178.     { ALT('o'),  0x18 },
  179.     { ALT('p'),  0x19 },
  180.     { ALT('q'),  0x10 },
  181.     { ALT('r'),  0x13 },
  182.     { ALT('s'),  0x1F },
  183.     { ALT('t'),  0x14 },
  184.     { ALT('u'),  0x16 },
  185.     { ALT('v'),  0x2F },
  186.     { ALT('w'),  0x11 },
  187.     { ALT('x'),  0x2D },
  188.     { ALT('y'),  0x15 },
  189.     { ALT('z'),  0x2C },
  190.     { ALT('\n'),  0x1C },    
  191.     { ALT('\n'),  0xA6 },  /* numpad */
  192.     { ALT('\b'),  0x0E },
  193.     { ALT(ESC_CHAR),  0x01 },
  194.     { ALT('['),  0x1A },
  195.     { ALT(']'),  0x1B },
  196.     { ALT(';'),  0x27 },
  197.     { ALT('\''),  0x28 },
  198.     { ALT('`'),  0x29 },
  199.     { ALT(','),  0x33 },
  200.     { ALT('.'),  0x34 },
  201.     { ALT('/'),  0x35 },
  202.     { ALT(' '),  0x39 },
  203.     { ALT(KEY_F(1)),  0x68 },
  204.     { ALT(KEY_F(2)),  0x69 },
  205.     { ALT(KEY_F(3)),  0x6A },
  206.     { ALT(KEY_F(4)),  0x6B },
  207.     { ALT(KEY_F(5)),  0x6C },
  208.     { ALT(KEY_F(6)),  0x6D },
  209.     { ALT(KEY_F(7)),  0x6E },
  210.     { ALT(KEY_F(8)),  0x6F },
  211.     { ALT(KEY_F(9)),  0x70 },
  212.     { ALT(KEY_F(10)),  0x71 },
  213.     { ALT(KEY_F(11)),  0x8B },
  214.     { ALT(KEY_F(12)),  0x8C },
  215.     { 0, 0}
  216. };        
  217.  
  218. static
  219. struct {
  220.     int key_code;
  221.     int vkcode;
  222. } CTR_table [] = {
  223.     { 0, 0}
  224. };        
  225.  
  226.  
  227. static
  228. struct {
  229.     int key_code;
  230.     int vkcode;
  231. } movement [] = {
  232.     { KEY_BTAB,  0x0f },  /* Shift-Tab */
  233.     { KEY_IC,    0x52 },
  234.     { KEY_IC,    0x92 },  /* Ctrl-Ins */
  235.     { KEY_IC,    0x04 },  /* Shift-Ins */
  236.     { KEY_DC,    0x53 },
  237.     { KEY_DC,    0x93 },  /* Ctrl-Del */
  238.     { KEY_DC,    0x05 },  /* Shift-Del */
  239.     { KEY_PPAGE, 0x49 },
  240.     { KEY_PPAGE, 0x84 },  /* Ctrl-PgUp */
  241.     { KEY_NPAGE, 0x51 },
  242.     { KEY_NPAGE, 0x76 },  /* Ctrl-PgDn */
  243.     { KEY_LEFT,  0x4B },
  244.     { KEY_LEFT,  0x73 },  /* Ctrl-Left */
  245.     { KEY_RIGHT, 0x4D },
  246.     { KEY_RIGHT, 0x74 },  /* Ctrl-Right */
  247.     { KEY_UP,    0x48 },
  248.     { KEY_UP,    0x8D },  /* Ctrl-Up */
  249.     { KEY_DOWN,  0x50 },
  250.     { KEY_DOWN,  0x91 },  /* Ctrl-Dw */
  251.     { KEY_HOME,  0x47 },
  252.     { KEY_HOME,  0x77 },  /* Ctrl-Home */ 
  253.     { KEY_END,     0x4F },
  254.     { KEY_END,     0x75 },  /* Ctrl-End */
  255.     { ALT(KEY_HOME), 0x97 },
  256.     { ALT(KEY_UP), 0x98 },
  257.     { ALT(KEY_PPAGE), 0x99 },
  258.     { ALT(KEY_LEFT), 0x9B },
  259.     { ALT(KEY_RIGHT), 0x9D },
  260.     { ALT(KEY_END), 0x9F },
  261.     { ALT(KEY_HOME), 0x97 },
  262.     { ALT(KEY_DOWN), 0xA0 },
  263.     { ALT(KEY_NPAGE), 0xA1 },
  264.     { ALT(KEY_IC), 0xA2 },
  265.     { ALT(KEY_DC), 0xA3 },
  266.     { ALT('\t'), 0xA5 },
  267.     { 0, 0}
  268. };        
  269.  
  270.  
  271. static
  272. struct {
  273.     int key_code;
  274.     int vkcode;
  275. } special [] = {
  276.     { KEY_KP_ADD, 0x4e2b },
  277.     { KEY_KP_SUBTRACT, 0x4a2d },
  278.     { KEY_KP_MULTIPLY, 0x372a },
  279.     { 0, 0}
  280. };
  281.  
  282. int spVKtoCurses( int a_vkc)
  283. { int i;
  284.   for (i=0;  special[i].vkcode != 0 || special[i].key_code != 0; i++) 
  285.     if (a_vkc&0xFFFF == special[i].vkcode) 
  286.            return (special[i].key_code);
  287.   return 0;
  288. }
  289.  
  290.  
  291. int VKtoCurses (int a_vkc)
  292. {
  293.    unsigned char scanCode;
  294.    unsigned char asciiCode;
  295.    register int i;
  296.    int rtnCode = 0;
  297.  
  298.    scanCode = (unsigned char) ((a_vkc & 0x0000FFFF) >> 8);
  299.    asciiCode = (unsigned char) (a_vkc & 0x000000FF);
  300.  /* stdlog("%04x %02x\n",a_vkc,scanCode);  */ 
  301.  
  302.    rtnCode = asciiCode;
  303.  
  304.    /* Scan Movement codes */
  305.    if (asciiCode == 0) {
  306.       /* Replace key code with that in table */
  307.       for (i=0;  movement[i].vkcode != 0 || movement[i].key_code != 0; i++) 
  308.     if (scanCode == movement[i].vkcode) 
  309.            return (movement[i].key_code);
  310.       /* Function-key detected */
  311.       for (i=0;  fkt_table[i].vkcode != 0 || fkt_table[i].key_code != 0; i++) 
  312.     if (scanCode == fkt_table[i].vkcode) 
  313.            return (fkt_table[i].key_code);
  314.       /* ALT - KEY */
  315.       for (i=0;  ALT_table[i].vkcode != 0 || ALT_table[i].key_code != 0; i++) 
  316.                 if (scanCode == ALT_table[i].vkcode) 
  317.                           return (ALT_table[i].key_code);
  318.    }
  319.  
  320.    if (asciiCode == 0x0d) {
  321.       return '\n';
  322.    }
  323.  
  324.    return rtnCode;
  325. }
  326.  
  327.  
  328.  
  329.