home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tk8.0.5 / os2 / tkOS2Key.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  9KB  |  401 lines

  1. /* 
  2.  * tkOS2Key.c --
  3.  *
  4.  *    This file contains X emulation routines for keyboard related
  5.  *    functions.
  6.  *
  7.  * Copyright (c) 1996-2000 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.  
  84.     /*
  85.      * The following support the new keys in the Microsoft keyboard.
  86.      * Win_L and Win_R have the windows logo.  App has the menu.
  87.     VK_LWIN, XK_Win_L,
  88.     VK_RWIN, XK_Win_R,
  89.     VK_APPS, XK_App,
  90.      */
  91.  
  92.     {0, NoSymbol}
  93. };
  94.  
  95.  
  96. /*
  97.  *----------------------------------------------------------------------
  98.  *
  99.  * XLookupString --
  100.  *
  101.  *    Retrieve the string equivalent for the given keyboard event.
  102.  *
  103.  * Results:
  104.  *    Returns the number of characters stored in buffer_return.
  105.  *
  106.  * Side effects:
  107.  *    Retrieves the characters stored in the event and inserts them
  108.  *    into buffer_return.
  109.  *
  110.  *----------------------------------------------------------------------
  111.  */
  112.  
  113. int
  114. XLookupString(event_struct, buffer_return, bytes_buffer, keysym_return,
  115.     status_in_out)
  116.     XKeyEvent* event_struct;
  117.     char* buffer_return;
  118.     int bytes_buffer;
  119.     KeySym* keysym_return;
  120.     XComposeStatus* status_in_out;
  121. {
  122.     int i, limit;
  123.  
  124. #ifdef VERBOSE
  125.     printf("XLookupString\n");
  126. #endif
  127.  
  128.     if (event_struct->send_event != -1) {
  129.         /*
  130.          * This is an event generated from generic code.  It has no
  131.          * nchars or trans_chars members.
  132.          */
  133.  
  134.         int index;
  135.         KeySym keysym;
  136. #ifdef VERBOSE
  137.         printf("generated from generic code\n");
  138. #endif
  139.  
  140.         index = 0;
  141.         if (event_struct->state & ShiftMask) {
  142.             index |= 1;
  143.         }
  144.         if (event_struct->state & Mod1Mask) {
  145.             index |= 2;
  146.         }
  147.         keysym = XKeycodeToKeysym(event_struct->display,
  148.                 event_struct->keycode, index);
  149.         if (((keysym != NoSymbol) && (keysym > 0) && (keysym < 256))
  150.                 || (keysym == XK_Return)
  151.                 || (keysym == XK_Tab)) {
  152.             buffer_return[0] = (char) keysym;
  153.             return 1;
  154.         }
  155.         return 0;
  156.     }
  157.  
  158.     if ((event_struct->nchars <= 0) || (buffer_return == NULL)) {
  159.     return 0;
  160.     }
  161.     limit = (event_struct->nchars < bytes_buffer) ? event_struct->nchars :
  162.     bytes_buffer;
  163.  
  164.     for (i = 0; i < limit; i++) {
  165.     buffer_return[i] = event_struct->trans_chars[i];
  166.     }
  167.  
  168.     if (keysym_return != NULL) {
  169.     *keysym_return = NoSymbol;
  170.     }
  171.     return i;
  172. }
  173.  
  174. /*
  175.  *----------------------------------------------------------------------
  176.  *
  177.  * XKeycodeToKeysym --
  178.  *
  179.  *    Translate from a system-dependent keycode to a
  180.  *    system-independent keysym.
  181.  *
  182.  * Results:
  183.  *    Returns the translated keysym, or NoSymbol on failure.
  184.  *
  185.  * Side effects:
  186.  *    None.
  187.  *
  188.  *----------------------------------------------------------------------
  189.  */
  190.  
  191. KeySym
  192. XKeycodeToKeysym(display, keycode, index)
  193.     Display* display;
  194.     unsigned int keycode;
  195.     int index;
  196. {
  197.     int result;
  198.     Keys* key;
  199.     /* Move to VK_USER range */
  200.     result = keycode > 0x120 && keycode < 0x200;
  201.  
  202. #ifdef VERBOSE
  203.     printf("XKeycodeToKeysym keycode %x (%c), index %d\n", keycode, keycode,
  204.            index);
  205. #endif
  206.  
  207.  
  208.     /*
  209.      * Keycode mapped to a valid Latin-1 character.  Since the keysyms
  210.      * for alphanumeric characters map onto Latin-1, we just return it.
  211.      */
  212.  
  213.     if (result) {
  214.     keycode -= 0x100;
  215. #ifdef VERBOSE
  216.         printf("XKeycodeToKeysym returning char %x (%c)\n", keycode, keycode);
  217. #endif
  218.     return (KeySym) keycode;
  219.     }
  220.  
  221.     /*
  222.      * Keycode is a non-alphanumeric key, so we have to do the lookup.
  223.      */
  224.  
  225.     for (key = keymap; key->keycode != 0; key++) {
  226.     if (key->keycode == keycode) {
  227. #ifdef VERBOSE
  228.             printf("XKeycodeToKeysym keycode %x -> keysym %x\n", keycode,
  229.                    key->keysym);
  230. #endif
  231.         return key->keysym;
  232.     }
  233.     }
  234.  
  235.     return NoSymbol;
  236. }
  237.  
  238. /*
  239.  *----------------------------------------------------------------------
  240.  *
  241.  * XKeysymToKeycode --
  242.  *
  243.  *    Translate a keysym back into a keycode.
  244.  *
  245.  * Results:
  246.  *    Returns the keycode that would generate the specified keysym.
  247.  *
  248.  * Side effects:
  249.  *    None.
  250.  *
  251.  *----------------------------------------------------------------------
  252.  */
  253.  
  254. KeyCode
  255. XKeysymToKeycode(display, keysym)
  256.     Display* display;
  257.     KeySym keysym;
  258. {
  259.     Keys* key;
  260.  
  261. #ifdef VERBOSE
  262.     printf("XKeysymToKeycode\n");
  263. #endif
  264.  
  265.     if (keysym >= 0x20 && keysym <= 0xFF) {
  266. #ifdef VERBOSE
  267.         printf("  returning keysym + 0x100: %d\n", keysym + 0x100);
  268. #endif
  269.         return (KeyCode) (keysym + 0x100);
  270.     }
  271.  
  272.     /*
  273.      * Couldn't map the character to a virtual keycode, so do a
  274.      * table lookup.
  275.      */
  276.  
  277.     for (key = keymap; key->keycode != 0; key++) {
  278.     if (key->keysym == keysym) {
  279. #ifdef VERBOSE
  280.             printf("  returning key->keycode %d\n", key->keycode);
  281. #endif
  282.         return key->keycode;
  283.     }
  284.     }
  285. #ifdef VERBOSE
  286.     printf("  returning 0\n");
  287. #endif
  288.     return 0;
  289. }
  290.  
  291. /*
  292.  *----------------------------------------------------------------------
  293.  *
  294.  * XGetModifierMapping --
  295.  *
  296.  *    Fetch the current keycodes used as modifiers.
  297.  *
  298.  * Results:
  299.  *    Returns a new modifier map.
  300.  *
  301.  * Side effects:
  302.  *    Allocates a new modifier map data structure.
  303.  *
  304.  *----------------------------------------------------------------------
  305.  */
  306.  
  307. XModifierKeymap    *
  308. XGetModifierMapping(display)
  309.     Display* display;
  310. {
  311.     XModifierKeymap *map = (XModifierKeymap *)ckalloc(sizeof(XModifierKeymap));
  312.  
  313. #ifdef VERBOSE
  314.     printf("XGetModifierMapping\n");
  315. #endif
  316.  
  317.     map->max_keypermod = 1;
  318.     map->modifiermap = (KeyCode *) ckalloc(sizeof(KeyCode)*8);
  319.     map->modifiermap[ShiftMapIndex] = VK_SHIFT;
  320.     map->modifiermap[LockMapIndex] = VK_CAPSLOCK;
  321.     map->modifiermap[ControlMapIndex] = VK_CTRL;
  322.     map->modifiermap[Mod1MapIndex] = VK_NUMLOCK;
  323.     map->modifiermap[Mod2MapIndex] = VK_MENU;
  324.     map->modifiermap[Mod3MapIndex] = VK_SCRLLOCK;
  325.     map->modifiermap[Mod4MapIndex] = VK_ALT;
  326.     map->modifiermap[Mod5MapIndex] = 0;
  327.     return map;
  328. }
  329.  
  330. /*
  331.  *----------------------------------------------------------------------
  332.  *
  333.  * XFreeModifiermap --
  334.  *
  335.  *    Deallocate a modifier map that was created by
  336.  *    XGetModifierMapping.
  337.  *
  338.  * Results:
  339.  *    None.
  340.  *
  341.  * Side effects:
  342.  *    Frees the datastructure referenced by modmap.
  343.  *
  344.  *----------------------------------------------------------------------
  345.  */
  346.  
  347. void
  348. XFreeModifiermap(modmap)
  349.     XModifierKeymap* modmap;
  350. {
  351.     ckfree((char *) modmap->modifiermap);
  352.     ckfree((char *) modmap);
  353. }
  354.  
  355. /*
  356.  *----------------------------------------------------------------------
  357.  *
  358.  * XStringToKeysym --
  359.  *
  360.  *    Translate a keysym name to the matching keysym. 
  361.  *
  362.  * Results:
  363.  *    Returns the keysym.  Since this is already handled by
  364.  *    Tk's StringToKeysym function, we just return NoSymbol.
  365.  *
  366.  * Side effects:
  367.  *    None.
  368.  *
  369.  *----------------------------------------------------------------------
  370.  */
  371.  
  372. KeySym
  373. XStringToKeysym(string)
  374.     _Xconst char *string;
  375. {
  376.     return NoSymbol;
  377. }
  378.  
  379. /*
  380.  *----------------------------------------------------------------------
  381.  *
  382.  * XKeysymToString --
  383.  *
  384.  *    Convert a keysym to character form.
  385.  *
  386.  * Results:
  387.  *    Returns NULL, since Tk will have handled this already.
  388.  *
  389.  * Side effects:
  390.  *    None.
  391.  *
  392.  *----------------------------------------------------------------------
  393.  */
  394.  
  395. char *
  396. XKeysymToString(keysym)
  397.     KeySym keysym;
  398. {
  399.     return NULL;
  400. }
  401.