home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tk42r2s.zip / tk4.2 / os2 / tkOS2Key.c < prev    next >
C/C++ Source or Header  |  1999-07-26  |  9KB  |  377 lines

  1. /* 
  2.  * tkOS2Key.c --
  3.  *
  4.  *    This file contains X emulation routines for keyboard related
  5.  *    functions.
  6.  *
  7.  * Copyright (c) 1996-1998 Illya Vaes
  8.  * Copyright (c) 1995 Sun Microsystems, Inc.
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  */
  13.  
  14.  
  15. #include "tkOS2Int.h"
  16.  
  17. typedef struct {
  18.     unsigned int keycode;
  19.     KeySym keysym;
  20. } Keys;
  21.  
  22. static Keys keymap[] = {
  23.     {VK_BREAK, XK_Cancel},
  24.     {VK_BACKSPACE, XK_BackSpace},
  25.     {VK_TAB, XK_Tab},
  26.     {VK_CLEAR, XK_Clear},
  27.     {VK_NEWLINE, XK_Return},
  28.     {VK_ENTER, XK_Return},
  29.     {VK_SHIFT, XK_Shift_L},
  30.     {VK_CTRL, XK_Control_L},
  31.     /*
  32.     {VK_MENU, XK_Alt_L},
  33.     */
  34.     {VK_ALT, XK_Alt_L},
  35.     /*
  36.     {VK_MENU, XK_Alt_R},
  37.     */
  38.     {VK_ALTGRAF, XK_Alt_R},
  39.     {VK_PAUSE, XK_Pause},
  40.     {VK_CAPSLOCK, XK_Caps_Lock},
  41.     {VK_ESC, XK_Escape},
  42.     {VK_SPACE, XK_space},
  43.     {VK_PAGEUP, XK_Prior},
  44.     {VK_PAGEDOWN, XK_Next},
  45.     {VK_END, XK_End},
  46.     {VK_HOME, XK_Home},
  47.     {VK_LEFT, XK_Left},
  48.     {VK_UP, XK_Up},
  49.     {VK_RIGHT, XK_Right},
  50.     {VK_DOWN, XK_Down},
  51. /*    {0, XK_Select}, */
  52.     {VK_PRINTSCRN, XK_Print},
  53. /*    {0, XK_Execute}, */
  54.     {VK_INSERT, XK_Insert},
  55.     {VK_DELETE, XK_Delete},
  56. /*    {0, XK_Help}, */
  57.     {VK_F1, XK_F1},
  58.     {VK_F2, XK_F2},
  59.     {VK_F3, XK_F3},
  60.     {VK_F4, XK_F4},
  61.     {VK_F5, XK_F5},
  62.     {VK_F6, XK_F6},
  63.     {VK_F7, XK_F7},
  64.     {VK_F8, XK_F8},
  65.     {VK_F9, XK_F9},
  66.     {VK_F10, XK_F10},
  67.     {VK_F11, XK_F11},
  68.     {VK_F12, XK_F12},
  69.     {VK_F13, XK_F13},
  70.     {VK_F14, XK_F14},
  71.     {VK_F15, XK_F15},
  72.     {VK_F16, XK_F16},
  73.     {VK_F17, XK_F17},
  74.     {VK_F18, XK_F18},
  75.     {VK_F19, XK_F19},
  76.     {VK_F20, XK_F20},
  77.     {VK_F21, XK_F21},
  78.     {VK_F22, XK_F22},
  79.     {VK_F23, XK_F23},
  80.     {VK_F24, XK_F24},
  81.     {VK_NUMLOCK, XK_Num_Lock}, 
  82.     {VK_SCRLLOCK, XK_Scroll_Lock},
  83.     {0, NoSymbol}
  84. };
  85.  
  86.  
  87. /*
  88.  *----------------------------------------------------------------------
  89.  *
  90.  * XLookupString --
  91.  *
  92.  *    Retrieve the string equivalent for the given keyboard event.
  93.  *
  94.  * Results:
  95.  *    Returns the number of characters stored in buffer_return.
  96.  *
  97.  * Side effects:
  98.  *    Retrieves the characters stored in the event and inserts them
  99.  *    into buffer_return.
  100.  *
  101.  *----------------------------------------------------------------------
  102.  */
  103.  
  104. int
  105. XLookupString(event_struct, buffer_return, bytes_buffer, keysym_return,
  106.     status_in_out)
  107.     XKeyEvent* event_struct;
  108.     char* buffer_return;
  109.     int bytes_buffer;
  110.     KeySym* keysym_return;
  111.     XComposeStatus* status_in_out;
  112. {
  113.     int i, limit;
  114.  
  115.     if (event_struct->send_event != -1) {
  116.         /*
  117.          * This is an event generated from generic code.  It has no
  118.          * nchars or trans_chars members.
  119.          */
  120.  
  121.         int index;
  122.         KeySym keysym;
  123.  
  124.         index = 0;
  125.         if (event_struct->state & ShiftMask) {
  126.             index |= 1;
  127.         }
  128.         if (event_struct->state & Mod1Mask) {
  129.             index |= 2;
  130.         }
  131.         keysym = XKeycodeToKeysym(event_struct->display,
  132.                 event_struct->keycode, index);
  133.         if ((keysym != NoSymbol) && (keysym > 0) && (keysym < 256)) {
  134.             buffer_return[0] = (char) keysym;
  135.             return 1;
  136.         }
  137.         return 0;
  138.     }
  139.  
  140.     if ((event_struct->nchars <= 0) || (buffer_return == NULL)) {
  141.     return 0;
  142.     }
  143.     limit = (event_struct->nchars < bytes_buffer) ? event_struct->nchars :
  144.     bytes_buffer;
  145.  
  146.     for (i = 0; i < limit; i++) {
  147.     buffer_return[i] = event_struct->trans_chars[i];
  148.     }
  149.  
  150.     if (keysym_return != NULL) {
  151.     *keysym_return = NoSymbol;
  152.     }
  153.     return i;
  154. }
  155.  
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * XKeycodeToKeysym --
  160.  *
  161.  *    Translate from a system-dependent keycode to a
  162.  *    system-independent keysym.
  163.  *
  164.  * Results:
  165.  *    Returns the translated keysym, or NoSymbol on failure.
  166.  *
  167.  * Side effects:
  168.  *    None.
  169.  *
  170.  *----------------------------------------------------------------------
  171.  */
  172.  
  173. KeySym
  174. XKeycodeToKeysym(display, keycode, index)
  175.     Display* display;
  176.     unsigned int keycode;
  177.     int index;
  178. {
  179.     int result;
  180.     Keys* key;
  181. #ifdef KEYPATCHES
  182.     /* Move to VK_USER range */
  183.     result = keycode > 0x120 && keycode < 0x200;
  184. #else /* KEYPATCHES */
  185.     BYTE keys[256];
  186.  
  187.     memset(keys, 0, 256);
  188.     if (index & 0x02) {
  189.         keys[VK_NUMLOCK] = 1;
  190.     }
  191.     if (index & 0x01) {
  192.         keys[VK_SHIFT] = 0x80;
  193.     }
  194.     result = keycode >= 32 && keycode <= 255;
  195. #endif /* KEYPATCHES */
  196.  
  197.     /*
  198.      * Keycode mapped to a valid Latin-1 character.  Since the keysyms
  199.      * for alphanumeric characters map onto Latin-1, we just return it.
  200.      */
  201.  
  202. #ifdef KEYPATCHES
  203.     if (result) {
  204.     keycode -= 0x100;
  205. #else /* KEYPATCHES */
  206.     if (result == 1 && keycode >= 0x20) {
  207. #endif /* KEYPATCHES */
  208.     return (KeySym) keycode;
  209.     }
  210.  
  211.     /*
  212.      * Keycode is a non-alphanumeric key, so we have to do the lookup.
  213.      */
  214.  
  215.     for (key = keymap; key->keycode != 0; key++) {
  216.     if (key->keycode == keycode) {
  217.         return key->keysym;
  218.     }
  219.     }
  220.  
  221.     return NoSymbol;
  222. }
  223.  
  224. /*
  225.  *----------------------------------------------------------------------
  226.  *
  227.  * XKeysymToKeycode --
  228.  *
  229.  *    Translate a keysym back into a keycode.
  230.  *
  231.  * Results:
  232.  *    Returns the keycode that would generate the specified keysym.
  233.  *
  234.  * Side effects:
  235.  *    None.
  236.  *
  237.  *----------------------------------------------------------------------
  238.  */
  239.  
  240. KeyCode
  241. XKeysymToKeycode(display, keysym)
  242.     Display* display;
  243.     KeySym keysym;
  244. {
  245.     Keys* key;
  246. #ifndef KEYPATCHES
  247.     SHORT result = (SHORT)keysym;
  248. #endif
  249.  
  250.     if (keysym >= 0x20 && keysym <= 0xFF) {
  251. #ifdef KEYPATCHES
  252.         return (KeyCode) (keysym + 0x100);
  253. #else /* KEYPATCHES */
  254.         return (KeyCode) (result & 0xFF);
  255. #endif /* KEYPATCHES */
  256.     }
  257.  
  258.     /*
  259.      * Couldn't map the character to a virtual keycode, so do a
  260.      * table lookup.
  261.      */
  262.  
  263.     for (key = keymap; key->keycode != 0; key++) {
  264.     if (key->keysym == keysym) {
  265.         return key->keycode;
  266.     }
  267.     }
  268.     return 0;
  269. }
  270.  
  271. /*
  272.  *----------------------------------------------------------------------
  273.  *
  274.  * XGetModifierMapping --
  275.  *
  276.  *    Fetch the current keycodes used as modifiers.
  277.  *
  278.  * Results:
  279.  *    Returns a new modifier map.
  280.  *
  281.  * Side effects:
  282.  *    Allocates a new modifier map data structure.
  283.  *
  284.  *----------------------------------------------------------------------
  285.  */
  286.  
  287. XModifierKeymap    *
  288. XGetModifierMapping(display)
  289.     Display* display;
  290. {
  291.     XModifierKeymap *map = (XModifierKeymap *)ckalloc(sizeof(XModifierKeymap));
  292.  
  293.     map->max_keypermod = 1;
  294.     map->modifiermap = (KeyCode *) ckalloc(sizeof(KeyCode)*8);
  295.     map->modifiermap[ShiftMapIndex] = VK_SHIFT;
  296.     map->modifiermap[LockMapIndex] = VK_CAPSLOCK;
  297.     map->modifiermap[ControlMapIndex] = VK_CTRL;
  298.     map->modifiermap[Mod1MapIndex] = VK_NUMLOCK;
  299.     map->modifiermap[Mod2MapIndex] = VK_MENU;
  300.     map->modifiermap[Mod3MapIndex] = VK_SCRLLOCK;
  301.     map->modifiermap[Mod4MapIndex] = VK_ALT;
  302.     map->modifiermap[Mod5MapIndex] = 0;
  303.     return map;
  304. }
  305.  
  306. /*
  307.  *----------------------------------------------------------------------
  308.  *
  309.  * XFreeModifiermap --
  310.  *
  311.  *    Deallocate a modifier map that was created by
  312.  *    XGetModifierMapping.
  313.  *
  314.  * Results:
  315.  *    None.
  316.  *
  317.  * Side effects:
  318.  *    Frees the datastructure referenced by modmap.
  319.  *
  320.  *----------------------------------------------------------------------
  321.  */
  322.  
  323. void
  324. XFreeModifiermap(modmap)
  325.     XModifierKeymap* modmap;
  326. {
  327.     ckfree((char *) modmap->modifiermap);
  328.     ckfree((char *) modmap);
  329. }
  330.  
  331. /*
  332.  *----------------------------------------------------------------------
  333.  *
  334.  * XStringToKeysym --
  335.  *
  336.  *    Translate a keysym name to the matching keysym. 
  337.  *
  338.  * Results:
  339.  *    Returns the keysym.  Since this is already handled by
  340.  *    Tk's StringToKeysym function, we just return NoSymbol.
  341.  *
  342.  * Side effects:
  343.  *    None.
  344.  *
  345.  *----------------------------------------------------------------------
  346.  */
  347.  
  348. KeySym
  349. XStringToKeysym(string)
  350.     _Xconst char *string;
  351. {
  352.     return NoSymbol;
  353. }
  354.  
  355. /*
  356.  *----------------------------------------------------------------------
  357.  *
  358.  * XKeysymToString --
  359.  *
  360.  *    Convert a keysym to character form.
  361.  *
  362.  * Results:
  363.  *    Returns NULL, since Tk will have handled this already.
  364.  *
  365.  * Side effects:
  366.  *    None.
  367.  *
  368.  *----------------------------------------------------------------------
  369.  */
  370.  
  371. char *
  372. XKeysymToString(keysym)
  373.     KeySym keysym;
  374. {
  375.     return NULL;
  376. }
  377.