home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / TMkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  23.3 KB  |  695 lines

  1. /* $XConsortium: TMkey.c,v 1.19 92/12/22 17:20:52 converse Exp $ */
  2. /*LINTLIBRARY*/
  3.  
  4. /***********************************************************
  5. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  6. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  7.  
  8.                         All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the names of Digital or MIT not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25.  
  26. ******************************************************************/
  27.  
  28. #define XK_MISCELLANY
  29. #define XK_LATIN1
  30. #define XK_LATIN2
  31. #define XK_LATIN3
  32. #define XK_LATIN4
  33.  
  34. #include "IntrinsicI.h"
  35. #include <X11/keysymdef.h>
  36.  
  37. #ifdef __STDC__
  38. #define Const const
  39. #else
  40. #define Const /**/
  41. #endif
  42.  
  43. #define FLUSHKEYCACHE(ctx) bzero((char *)&ctx->keycache, sizeof(TMKeyCache))
  44.  
  45. /*
  46.  * The following array reorders the modifier bits so that the most common ones
  47.  * (used by a translator) are in the top-most bits with respect to the size of
  48.  * the keycache.  The array currently just reverses the bits as a good guess.
  49.  * This might be more trouble than it is worth, but it seems to help.
  50.  */
  51.  
  52. #define FM(i) i >> (8 - TMKEYCACHELOG2)
  53. static Const unsigned char modmix[256] = {
  54. FM(0x00), FM(0x80), FM(0x40), FM(0xc0), FM(0x20), FM(0xa0), FM(0x60), FM(0xe0),
  55. FM(0x10), FM(0x90), FM(0x50), FM(0xd0), FM(0x30), FM(0xb0), FM(0x70), FM(0xf0),
  56. FM(0x08), FM(0x88), FM(0x48), FM(0xc8), FM(0x28), FM(0xa8), FM(0x68), FM(0xe8),
  57. FM(0x18), FM(0x98), FM(0x58), FM(0xd8), FM(0x38), FM(0xb8), FM(0x78), FM(0xf8),
  58. FM(0x04), FM(0x84), FM(0x44), FM(0xc4), FM(0x24), FM(0xa4), FM(0x64), FM(0xe4),
  59. FM(0x14), FM(0x94), FM(0x54), FM(0xd4), FM(0x34), FM(0xb4), FM(0x74), FM(0xf4),
  60. FM(0x0c), FM(0x8c), FM(0x4c), FM(0xcc), FM(0x2c), FM(0xac), FM(0x6c), FM(0xec),
  61. FM(0x1c), FM(0x9c), FM(0x5c), FM(0xdc), FM(0x3c), FM(0xbc), FM(0x7c), FM(0xfc),
  62. FM(0x02), FM(0x82), FM(0x42), FM(0xc2), FM(0x22), FM(0xa2), FM(0x62), FM(0xe2),
  63. FM(0x12), FM(0x92), FM(0x52), FM(0xd2), FM(0x32), FM(0xb2), FM(0x72), FM(0xf2),
  64. FM(0x0a), FM(0x8a), FM(0x4a), FM(0xca), FM(0x2a), FM(0xaa), FM(0x6a), FM(0xea),
  65. FM(0x1a), FM(0x9a), FM(0x5a), FM(0xda), FM(0x3a), FM(0xba), FM(0x7a), FM(0xfa),
  66. FM(0x06), FM(0x86), FM(0x46), FM(0xc6), FM(0x26), FM(0xa6), FM(0x66), FM(0xe6),
  67. FM(0x16), FM(0x96), FM(0x56), FM(0xd6), FM(0x36), FM(0xb6), FM(0x76), FM(0xf6),
  68. FM(0x0e), FM(0x8e), FM(0x4e), FM(0xce), FM(0x2e), FM(0xae), FM(0x6e), FM(0xee),
  69. FM(0x1e), FM(0x9e), FM(0x5e), FM(0xde), FM(0x3e), FM(0xbe), FM(0x7e), FM(0xfe),
  70. FM(0x01), FM(0x81), FM(0x41), FM(0xc1), FM(0x21), FM(0xa1), FM(0x61), FM(0xe1),
  71. FM(0x11), FM(0x91), FM(0x51), FM(0xd1), FM(0x31), FM(0xb1), FM(0x71), FM(0xf1),
  72. FM(0x09), FM(0x89), FM(0x49), FM(0xc9), FM(0x29), FM(0xa9), FM(0x69), FM(0xe9),
  73. FM(0x19), FM(0x99), FM(0x59), FM(0xd9), FM(0x39), FM(0xb9), FM(0x79), FM(0xf9),
  74. FM(0x05), FM(0x85), FM(0x45), FM(0xc5), FM(0x25), FM(0xa5), FM(0x65), FM(0xe5),
  75. FM(0x15), FM(0x95), FM(0x55), FM(0xd5), FM(0x35), FM(0xb5), FM(0x75), FM(0xf5),
  76. FM(0x0d), FM(0x8d), FM(0x4d), FM(0xcd), FM(0x2d), FM(0xad), FM(0x6d), FM(0xed),
  77. FM(0x1d), FM(0x9d), FM(0x5d), FM(0xdd), FM(0x3d), FM(0xbd), FM(0x7d), FM(0xfd),
  78. FM(0x03), FM(0x83), FM(0x43), FM(0xc3), FM(0x23), FM(0xa3), FM(0x63), FM(0xe3),
  79. FM(0x13), FM(0x93), FM(0x53), FM(0xd3), FM(0x33), FM(0xb3), FM(0x73), FM(0xf3),
  80. FM(0x0b), FM(0x8b), FM(0x4b), FM(0xcb), FM(0x2b), FM(0xab), FM(0x6b), FM(0xeb),
  81. FM(0x1b), FM(0x9b), FM(0x5b), FM(0xdb), FM(0x3b), FM(0xbb), FM(0x7b), FM(0xfb),
  82. FM(0x07), FM(0x87), FM(0x47), FM(0xc7), FM(0x27), FM(0xa7), FM(0x67), FM(0xe7),
  83. FM(0x17), FM(0x97), FM(0x57), FM(0xd7), FM(0x37), FM(0xb7), FM(0x77), FM(0xf7),
  84. FM(0x0f), FM(0x8f), FM(0x4f), FM(0xcf), FM(0x2f), FM(0xaf), FM(0x6f), FM(0xef),
  85. FM(0x1f), FM(0x9f), FM(0x5f), FM(0xdf), FM(0x3f), FM(0xbf), FM(0x7f), FM(0xff)
  86. };
  87. #undef FM
  88.  
  89. #define MOD_RETURN(ctx, key) (ctx)->keycache.modifiers_return[key]
  90.  
  91. #define TRANSLATE(ctx,pd,dpy,key,mod,mod_ret,sym_ret) \
  92. { \
  93.     int _i_ = (((key) - (pd)->min_keycode + modmix[(mod) & 0xff]) & \
  94.            (TMKEYCACHESIZE-1)); \
  95.     if ((key) != 0 && /* Xlib XIM composed input */ \
  96.     (ctx)->keycache.keycode[_i_] == (key) && \
  97.     (ctx)->keycache.modifiers[_i_] == (mod)) { \
  98.     mod_ret = MOD_RETURN(ctx, key); \
  99.     sym_ret = (ctx)->keycache.keysym[_i_]; \
  100.     } else { \
  101.     XtTranslateKeycode(dpy, key, mod, &mod_ret, &sym_ret); \
  102.     (ctx)->keycache.keycode[_i_] = key; \
  103.     (ctx)->keycache.modifiers[_i_] = (unsigned char)(mod); \
  104.     (ctx)->keycache.keysym[_i_] = sym_ret; \
  105.     MOD_RETURN(ctx, key) = (unsigned char)mod_ret; \
  106.     } \
  107. }
  108.  
  109. #define UPDATE_CACHE(ctx, pd, key, mod, mod_ret, sym_ret) \
  110. { \
  111.     int _i_ = (((key) - (pd)->min_keycode + modmix[(mod) & 0xff]) & \
  112.            (TMKEYCACHESIZE-1)); \
  113.     (ctx)->keycache.keycode[_i_] = key; \
  114.     (ctx)->keycache.modifiers[_i_] = (unsigned char)(mod); \
  115.     (ctx)->keycache.keysym[_i_] = sym_ret; \
  116.     MOD_RETURN(ctx, key) = (unsigned char)mod_ret; \
  117. }
  118.  
  119. /* usual number of expected keycodes in XtKeysymToKeycodeList */
  120. #define KEYCODE_ARRAY_SIZE 10
  121.  
  122. static void _XtConvertCase();
  123.  
  124. Boolean _XtComputeLateBindings(dpy, lateModifiers, computed, computedMask)
  125.     Display *dpy;
  126.     LateBindingsPtr lateModifiers;
  127.     Modifiers *computed,*computedMask;
  128. {
  129.     int i,j,ref;
  130.     ModToKeysymTable* temp;
  131.     XtPerDisplay perDisplay;
  132.     Boolean found;
  133.     KeySym tempKeysym = NoSymbol;
  134.  
  135.     perDisplay = _XtGetPerDisplay(dpy);
  136.     if (perDisplay == NULL) {
  137.         XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
  138.         "displayError","invalidDisplay",XtCXtToolkitError,
  139.             "Can't find display structure",
  140.             (String *)NULL, (Cardinal *)NULL);
  141.          return FALSE;
  142.     }
  143.     _InitializeKeysymTables(dpy, perDisplay);
  144.     for (ref=0; lateModifiers[ref].keysym; ref++) {
  145.         found = FALSE;
  146.         for (i=0;i<8;i++) {
  147.             temp = &(perDisplay->modsToKeysyms[i]);
  148.             for (j=0;j<temp->count;j++){
  149.                 if (perDisplay->modKeysyms[temp->idx+j] ==
  150.             lateModifiers[ref].keysym) {
  151.                     *computedMask = *computedMask | temp->mask;
  152.                     if (!lateModifiers[ref].knot)
  153.               *computed |= temp->mask;
  154.                     tempKeysym = lateModifiers[ref].keysym;
  155.                     found = TRUE; break;
  156.                 }
  157.             }
  158.             if (found) break;
  159.         }
  160.         if (!found  && !lateModifiers[ref].knot)
  161.             if (!lateModifiers[ref].pair && (tempKeysym == NoSymbol))
  162.                 return FALSE;
  163.         /* if you didn't find the modifier and the modifier must be
  164.            asserted then return FALSE. If you didn't find the modifier
  165.            and the modifier must be off, then it is OK . Don't
  166.            return FALSE if this is the first member of a pair or if
  167.            it is the second member of a pair when the first member
  168.            was bound to a modifier */
  169.     if (!lateModifiers[ref].pair) tempKeysym = NoSymbol;
  170.     }
  171.     return TRUE;
  172. }
  173.  
  174. void _XtAllocTMContext(pd)
  175.     XtPerDisplay pd;
  176. {
  177.     TMKeyContext ctx;
  178.     ctx = (TMKeyContext)_XtHeapAlloc(&pd->heap,
  179.                      sizeof(TMKeyContextRec));
  180.     ctx->event = NULL;
  181.     ctx->serial = 0;
  182.     ctx->keysym = NoSymbol;
  183.     ctx->modifiers = 0;
  184.     FLUSHKEYCACHE(ctx);
  185.     pd->tm_context = ctx;
  186. }
  187.  
  188. Boolean _XtMatchUsingDontCareMods(typeMatch, modMatch, eventSeq)
  189.     TMTypeMatch     typeMatch;
  190.     TMModifierMatch     modMatch;
  191.     TMEventPtr         eventSeq;
  192. {
  193.     Modifiers modifiers_return;
  194.     KeySym keysym_return;
  195.     Modifiers useful_mods;
  196.     int i;
  197.     Modifiers computed = 0;
  198.     Modifiers computedMask = 0;
  199.     Boolean resolved = TRUE;
  200.     Display *dpy = eventSeq->xev->xany.display;
  201.     XtPerDisplay pd;
  202.     TMKeyContext tm_context;
  203.     
  204.     if (modMatch->lateModifiers != NULL)
  205.     resolved = _XtComputeLateBindings(dpy, modMatch->lateModifiers,
  206.                       &computed, &computedMask);
  207.     if (!resolved) return FALSE;
  208.     computed |= modMatch->modifiers;
  209.     computedMask |= modMatch->modifierMask; /* gives do-care mask */
  210.     
  211.     if ( (computed & computedMask) ==
  212.         (eventSeq->event.modifiers & computedMask) ) {
  213.     Modifiers least_mod;
  214.     
  215.     pd = _XtGetPerDisplay(dpy);
  216.     tm_context = pd->tm_context;
  217.     TRANSLATE(tm_context, pd, dpy, (KeyCode)eventSeq->event.eventCode,
  218.                 (unsigned)0, modifiers_return, keysym_return);
  219.     
  220.         if ((keysym_return & typeMatch->eventCodeMask)  == typeMatch->eventCode ) {
  221.         tm_context->event = eventSeq->xev;
  222.         tm_context->serial = eventSeq->xev->xany.serial;
  223.         tm_context->keysym = keysym_return;
  224.         tm_context->modifiers = (Modifiers)0;
  225.         return TRUE;
  226.     }
  227.         useful_mods = ~computedMask & modifiers_return;
  228.         if (useful_mods == 0) return FALSE;
  229.     for (least_mod = 1; (least_mod & useful_mods)==0; least_mod <<= 1){/*EMPTY*/};
  230.         for (i = modifiers_return; i >= least_mod; i--)
  231.       /* all useful combinations of 8 modifier bits */
  232.       if (useful_mods & i) {
  233.           TRANSLATE(tm_context, pd, dpy, eventSeq->event.eventCode,
  234.             (Modifiers)i, modifiers_return, keysym_return);
  235.           if (keysym_return  ==
  236.           (typeMatch->eventCode & typeMatch->eventCodeMask)) {
  237.           tm_context->event = eventSeq->xev;
  238.           tm_context->serial = eventSeq->xev->xany.serial;
  239.           tm_context->keysym = keysym_return;
  240.           tm_context->modifiers = (Modifiers)i;
  241.           return TRUE;
  242.           }
  243.       }
  244.     }
  245.     return FALSE;
  246. }
  247.  
  248. void XtConvertCase(dpy,keysym,lower_return,upper_return)
  249.     Display *dpy;
  250.     KeySym keysym;
  251.     KeySym *lower_return, *upper_return;
  252. {
  253.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  254.     register CaseConverterPtr ptr;
  255.  
  256.     *lower_return = *upper_return = keysym;
  257.     for (ptr=pd->case_cvt;  ptr; ptr = ptr->next)
  258.     if (ptr->start <= keysym && keysym <= ptr->stop) {
  259.         (*ptr->proc)(dpy, keysym, lower_return, upper_return);
  260.         return;
  261.     }
  262.     if (keysym <= 0x3ff)    /* Latin-1 start = 0, Latin-4 stop = 0x3ff */
  263.     _XtConvertCase(dpy, keysym, lower_return, upper_return);
  264. }
  265.     
  266. Boolean _XtMatchUsingStandardMods (typeMatch, modMatch, eventSeq)
  267.     TMTypeMatch typeMatch;
  268.     TMModifierMatch modMatch;
  269.     TMEventPtr eventSeq;
  270. {
  271.     Modifiers modifiers_return;
  272.     KeySym keysym_return;
  273.     Modifiers computed= 0;
  274.     Modifiers computedMask = 0;
  275.     Boolean resolved = TRUE;
  276.     Display *dpy = eventSeq->xev->xany.display;
  277.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  278.     TMKeyContext tm_context = pd->tm_context;
  279.     Modifiers translateModifiers;
  280.  
  281.     /* To maximize cache utilization, we mask off nonstandard modifiers
  282.        before cache lookup.  For a given key translator, standard modifiers
  283.        are constant per KeyCode.  If a key translator uses no standard
  284.        modifiers this implementation will never reference the cache.
  285.      */
  286.  
  287.     modifiers_return = MOD_RETURN(tm_context, eventSeq->event.eventCode);
  288.     if (!modifiers_return) {
  289.     XtTranslateKeycode(dpy, (KeyCode)eventSeq->event.eventCode, 
  290.                eventSeq->event.modifiers, &modifiers_return,
  291.                &keysym_return);
  292.     translateModifiers = eventSeq->event.modifiers & modifiers_return;
  293.     UPDATE_CACHE(tm_context, pd, eventSeq->event.eventCode, 
  294.              translateModifiers, modifiers_return, keysym_return);
  295.     } else {
  296.     translateModifiers = eventSeq->event.modifiers & modifiers_return;
  297.     TRANSLATE(tm_context, pd, dpy, (KeyCode)eventSeq->event.eventCode,
  298.           translateModifiers, modifiers_return, keysym_return);
  299.     }
  300.  
  301.     if ((typeMatch->eventCode & typeMatch->eventCodeMask) ==
  302.              (keysym_return & typeMatch->eventCodeMask)) {
  303.         if (modMatch->lateModifiers != NULL) 
  304.             resolved = _XtComputeLateBindings(dpy, modMatch->lateModifiers,
  305.                           &computed, &computedMask);
  306.         if (!resolved) return FALSE;
  307.         computed |= modMatch->modifiers;
  308.         computedMask |= modMatch->modifierMask;
  309.  
  310.         if ((computed & computedMask) ==
  311.         (eventSeq->event.modifiers & ~modifiers_return & computedMask)) {
  312.         tm_context->event = eventSeq->xev;
  313.         tm_context->serial = eventSeq->xev->xany.serial;
  314.         tm_context->keysym = keysym_return;
  315.         tm_context->modifiers = translateModifiers;
  316.         return TRUE;
  317.     }
  318.     }
  319.     return FALSE;
  320. }
  321.  
  322.  
  323. void _XtBuildKeysymTables(dpy,pd)
  324.     Display *dpy;
  325.     register XtPerDisplay pd;
  326. {
  327.     ModToKeysymTable *table;
  328.     int maxCount,i,j,k,tempCount,idx;
  329.     KeySym keysym,tempKeysym;
  330.     XModifierKeymap* modKeymap;
  331.     KeyCode keycode;
  332. #define KeysymTableSize 16
  333.  
  334.     FLUSHKEYCACHE(pd->tm_context);
  335.     if (pd->keysyms)
  336.     XFree( (char *)pd->keysyms );
  337.     pd->keysyms_serial = NextRequest(dpy);
  338.     pd->keysyms = XGetKeyboardMapping(dpy, pd->min_keycode,
  339.                       pd->max_keycode-pd->min_keycode+1,
  340.                       &pd->keysyms_per_keycode);
  341.     if (pd->modKeysyms)
  342.     XtFree((char *)pd->modKeysyms);
  343.     if (pd->modsToKeysyms)
  344.     XtFree((char *)pd->modsToKeysyms);
  345.     pd->modKeysyms = (KeySym*)XtMalloc((Cardinal)KeysymTableSize*sizeof(KeySym));
  346.     maxCount = KeysymTableSize;
  347.     tempCount = 0;
  348.  
  349.     table = (ModToKeysymTable*)XtMalloc((Cardinal)8*sizeof(ModToKeysymTable));
  350.     pd->modsToKeysyms = table;
  351.  
  352.     table[0].mask = ShiftMask;
  353.     table[1].mask = LockMask;
  354.     table[2].mask = ControlMask;
  355.     table[3].mask = Mod1Mask;
  356.     table[4].mask = Mod2Mask;
  357.     table[5].mask = Mod3Mask;
  358.     table[6].mask = Mod4Mask;
  359.     table[7].mask = Mod5Mask;
  360.     tempKeysym = 0;
  361.  
  362.     modKeymap = XGetModifierMapping(dpy);
  363.     for (i=0;i<32;i++)
  364.     pd->isModifier[i] = 0;
  365.     pd->mode_switch = 0;
  366.     for (i=0;i<8;i++) {
  367.         table[i].idx = tempCount;
  368.         table[i].count = 0;
  369.         for (j=0;j<modKeymap->max_keypermod;j++) {
  370.             keycode = modKeymap->modifiermap[i*modKeymap->max_keypermod+j];
  371.             if (keycode != 0) {
  372.         pd->isModifier[keycode>>3] |= 1 << (keycode & 7);
  373.                 for (k=0; k<pd->keysyms_per_keycode;k++) {
  374.                     idx = ((keycode-pd->min_keycode)*
  375.                              pd->keysyms_per_keycode)+k;
  376.                     keysym = pd->keysyms[idx];
  377.             if ((keysym == XK_Mode_switch) && (i > 2))
  378.             pd->mode_switch |= 1 << i;
  379.                     if (keysym != 0 && keysym != tempKeysym ){
  380.                         if (tempCount==maxCount) {
  381.                             maxCount += KeysymTableSize;
  382.                             pd->modKeysyms = (KeySym*)XtRealloc(
  383.                                 (char*)pd->modKeysyms,
  384.                                 (unsigned) (maxCount*sizeof(KeySym)) );
  385.                         }
  386.                         pd->modKeysyms[tempCount++] = keysym;
  387.                         table[i].count++;
  388.                         tempKeysym = keysym;
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.     }
  394.     pd->lock_meaning = NoSymbol;
  395.     for (i = 0; i < table[1].count; i++) {
  396.     keysym = pd->modKeysyms[table[1].idx + i];
  397.     if (keysym == XK_Caps_Lock) {
  398.         pd->lock_meaning = XK_Caps_Lock;
  399.         break;
  400.     } else if (keysym == XK_Shift_Lock) {
  401.         pd->lock_meaning = XK_Shift_Lock;
  402.     }
  403.     }
  404.     XFreeModifiermap(modKeymap);
  405. }
  406.  
  407. #if NeedFunctionPrototypes
  408. void XtTranslateKeycode (
  409.     Display *dpy, 
  410.     _XtKeyCode keycode,
  411.     Modifiers modifiers,
  412.     Modifiers *modifiers_return,
  413.     KeySym *keysym_return
  414.     )
  415. #else
  416. void XtTranslateKeycode (dpy, keycode, modifiers,
  417.                             modifiers_return, keysym_return)
  418.  
  419.     Display *dpy;
  420.     KeyCode keycode;
  421.     Modifiers modifiers;
  422.     Modifiers *modifiers_return;
  423.     KeySym *keysym_return;
  424. #endif
  425. {
  426.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  427.     _InitializeKeysymTables(dpy, pd);
  428.     (*pd->defaultKeycodeTranslator)(
  429.             dpy,keycode,modifiers,modifiers_return,keysym_return);
  430. }
  431.  
  432. /* This code should match XTranslateKey (internal, sigh) in Xlib */
  433. #if NeedFunctionPrototypes
  434. void XtTranslateKey(
  435.     register Display *dpy,
  436.     _XtKeyCode keycode,
  437.     Modifiers modifiers,
  438.     Modifiers *modifiers_return,
  439.     KeySym *keysym_return
  440.     )
  441. #else
  442. void XtTranslateKey(dpy, keycode, modifiers,
  443.                             modifiers_return, keysym_return)
  444.     register Display *dpy;
  445.     KeyCode keycode;
  446.     Modifiers modifiers;
  447.     Modifiers *modifiers_return;
  448.     KeySym *keysym_return;
  449. #endif
  450. {
  451.     register XtPerDisplay pd = _XtGetPerDisplay(dpy);
  452.     int per;
  453.     register KeySym *syms;
  454.     KeySym sym, lsym, usym;
  455.  
  456.     *modifiers_return = (ShiftMask|LockMask) | pd->mode_switch;
  457.     if (((int)keycode < pd->min_keycode) || ((int)keycode > pd->max_keycode)) {
  458.     *keysym_return = NoSymbol;
  459.     return;
  460.     }
  461.     per = pd->keysyms_per_keycode;
  462.     syms = &pd->keysyms[(keycode - pd->min_keycode) * per];
  463.     while ((per > 2) && (syms[per - 1] == NoSymbol))
  464.     per--;
  465.     if ((per > 2) && (modifiers & pd->mode_switch)) {
  466.     syms += 2;
  467.     per -= 2;
  468.     }
  469.     if (!(modifiers & ShiftMask) &&
  470.     (!(modifiers & LockMask) || (pd->lock_meaning == NoSymbol))) {
  471.     if ((per == 1) || (syms[1] == NoSymbol))
  472.         XtConvertCase(dpy, syms[0], keysym_return, &usym);
  473.     else
  474.         *keysym_return = syms[0];
  475.     } else if (!(modifiers & LockMask) ||
  476.            (pd->lock_meaning != XK_Caps_Lock)) {
  477.     if ((per == 1) || ((usym = syms[1]) == NoSymbol))
  478.         XtConvertCase(dpy, syms[0], &lsym, &usym);
  479.     *keysym_return = usym;
  480.     } else {
  481.     if ((per == 1) || ((sym = syms[1]) == NoSymbol))
  482.         sym = syms[0];
  483.     XtConvertCase(dpy, sym, &lsym, &usym);
  484.     if (!(modifiers & ShiftMask) && (sym != syms[0]) &&
  485.         ((sym != usym) || (lsym == usym)))
  486.         XtConvertCase(dpy, syms[0], &lsym, &usym);
  487.     *keysym_return = usym;
  488.     }
  489.  
  490.     if (*keysym_return == XK_VoidSymbol)
  491.     *keysym_return = NoSymbol;
  492. }
  493.  
  494. void XtSetKeyTranslator(dpy, translator)
  495.  
  496.     Display *dpy;
  497.     XtKeyProc translator;
  498.  
  499. {
  500.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  501.  
  502.     pd->defaultKeycodeTranslator = translator;
  503.     FLUSHKEYCACHE(pd->tm_context);
  504.     /* XXX should now redo grabs */
  505. }
  506.  
  507. void XtRegisterCaseConverter(dpy, proc, start, stop)
  508.     Display *dpy;
  509.     XtCaseProc proc;
  510.     KeySym start;
  511.     KeySym stop;
  512. {
  513.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  514.     CaseConverterPtr ptr, prev;
  515.  
  516.     ptr = (CaseConverterPtr) XtMalloc(sizeof(CaseConverterRec));
  517.     ptr->start = start;
  518.     ptr->stop = stop;
  519.     ptr->proc = proc;
  520.     ptr->next = pd->case_cvt;
  521.     pd->case_cvt = ptr;
  522.  
  523.     /* Remove obsolete case converters from the list */
  524.     prev = ptr;
  525.     for (ptr=ptr->next; ptr; ptr=prev->next) {
  526.     if (start <= ptr->start && stop >= ptr->stop) {
  527.         prev->next = ptr->next;
  528.         XtFree((char *)ptr);
  529.     } 
  530.     else prev = ptr;
  531.     }
  532.     FLUSHKEYCACHE(pd->tm_context);
  533.     /* XXX should now redo grabs */
  534. }
  535.  
  536. /* This code should match XConvertCase (internal, sigh) in Xlib */
  537. /* ARGSUSED */
  538. static void _XtConvertCase(dpy, sym, lower, upper)
  539.     Display *dpy;
  540.     KeySym sym;
  541.     KeySym *lower;
  542.     KeySym *upper;
  543. {
  544.     *lower = sym;
  545.     *upper = sym;
  546.     switch(sym >> 8) {
  547.     case 0:
  548.     if ((sym >= XK_A) && (sym <= XK_Z))
  549.         *lower += (XK_a - XK_A);
  550.     else if ((sym >= XK_a) && (sym <= XK_z))
  551.         *upper -= (XK_a - XK_A);
  552.     else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
  553.         *lower += (XK_agrave - XK_Agrave);
  554.     else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
  555.         *upper -= (XK_agrave - XK_Agrave);
  556.     else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
  557.         *lower += (XK_oslash - XK_Ooblique);
  558.     else if ((sym >= XK_oslash) && (sym <= XK_thorn))
  559.         *upper -= (XK_oslash - XK_Ooblique);
  560.     break;
  561. #ifdef XK_LATIN2
  562.       case 1:
  563.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  564.     if (sym == XK_Aogonek)
  565.         *lower = XK_aogonek;
  566.     else if (sym >= XK_Lstroke && sym <= XK_Sacute)
  567.         *lower += (XK_lstroke - XK_Lstroke);
  568.     else if (sym >= XK_Scaron && sym <= XK_Zacute)
  569.         *lower += (XK_scaron - XK_Scaron);
  570.     else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
  571.         *lower += (XK_zcaron - XK_Zcaron);
  572.     else if (sym == XK_aogonek)
  573.         *upper = XK_Aogonek;
  574.     else if (sym >= XK_lstroke && sym <= XK_sacute)
  575.         *upper -= (XK_lstroke - XK_Lstroke);
  576.     else if (sym >= XK_scaron && sym <= XK_zacute)
  577.         *upper -= (XK_scaron - XK_Scaron);
  578.     else if (sym >= XK_zcaron && sym <= XK_zabovedot)
  579.         *upper -= (XK_zcaron - XK_Zcaron);
  580.     else if (sym >= XK_Racute && sym <= XK_Tcedilla)
  581.         *lower += (XK_racute - XK_Racute);
  582.     else if (sym >= XK_racute && sym <= XK_tcedilla)
  583.         *upper -= (XK_racute - XK_Racute);
  584.     break;
  585. #endif
  586. #ifdef XK_LATIN3
  587.       case 2:
  588.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  589.     if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
  590.         *lower += (XK_hstroke - XK_Hstroke);
  591.     else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
  592.         *lower += (XK_gbreve - XK_Gbreve);
  593.     else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
  594.         *upper -= (XK_hstroke - XK_Hstroke);
  595.     else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
  596.         *upper -= (XK_gbreve - XK_Gbreve);
  597.     else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
  598.         *lower += (XK_cabovedot - XK_Cabovedot);
  599.     else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
  600.         *upper -= (XK_cabovedot - XK_Cabovedot);
  601.     break;
  602. #endif
  603. #ifdef XK_LATIN4
  604.       case 3:
  605.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  606.     if (sym >= XK_Rcedilla && sym <= XK_Tslash)
  607.         *lower += (XK_rcedilla - XK_Rcedilla);
  608.     else if (sym >= XK_rcedilla && sym <= XK_tslash)
  609.         *upper -= (XK_rcedilla - XK_Rcedilla);
  610.     else if (sym == XK_ENG)
  611.         *lower = XK_eng;
  612.     else if (sym == XK_eng)
  613.         *upper = XK_ENG;
  614.     else if (sym >= XK_Amacron && sym <= XK_Umacron)
  615.         *lower += (XK_amacron - XK_Amacron);
  616.     else if (sym >= XK_amacron && sym <= XK_umacron)
  617.         *upper -= (XK_amacron - XK_Amacron);
  618.     break;
  619. #endif
  620.     }
  621. }
  622.  
  623.  
  624. KeySym *XtGetKeysymTable(dpy, min_keycode_return, keysyms_per_keycode_return)
  625.     Display *dpy;
  626.     KeyCode *min_keycode_return;
  627.     int *keysyms_per_keycode_return;
  628. {
  629.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  630.     _InitializeKeysymTables(dpy, pd);
  631.     *min_keycode_return = pd->min_keycode; /* %%% */
  632.     *keysyms_per_keycode_return = pd->keysyms_per_keycode;
  633.     return pd->keysyms;
  634. }
  635.  
  636. void XtKeysymToKeycodeList(dpy, keysym, keycodes_return, keycount_return)
  637.     Display *dpy;
  638.     KeySym keysym;
  639.     KeyCode **keycodes_return;
  640.     Cardinal *keycount_return;
  641. {
  642.     register XtPerDisplay pd = _XtGetPerDisplay(dpy);
  643.     unsigned keycode;
  644.     int per, match;
  645.     register KeySym *syms;
  646.     register int i, j;
  647.     KeySym lsym, usym;
  648.     unsigned maxcodes = 0;
  649.     unsigned ncodes = 0;
  650.     KeyCode *keycodes, *codeP;
  651.  
  652.     _InitializeKeysymTables(dpy, pd);
  653.     keycodes = NULL;
  654.     per = pd->keysyms_per_keycode;
  655.     for (syms = pd->keysyms, keycode = (unsigned) pd->min_keycode;
  656.      (int)keycode <= pd->max_keycode;
  657.      syms += per, keycode++) {
  658.     match = 0;
  659.     for (j = 0; j < per; j++) {
  660.         if (syms[j] == keysym) {
  661.         match = 1;
  662.         break;
  663.         }        
  664.     }
  665.     if (!match)
  666.         for (i = 1; i < 5; i += 2) {
  667.         if ((per == i) || ((per > i) && (syms[i] == NoSymbol))) {
  668.             XtConvertCase(dpy, syms[i-1], &lsym, &usym);
  669.             if ((lsym == keysym) || (usym == keysym)) {
  670.             match = 1;
  671.             break;
  672.             }
  673.         }
  674.         }
  675.     if (match) {
  676.         if (ncodes == maxcodes) {
  677.         KeyCode *old = keycodes;
  678.         maxcodes += KEYCODE_ARRAY_SIZE;
  679.         keycodes = (KeyCode*)XtMalloc(maxcodes*sizeof(KeyCode));
  680.         if (ncodes) {
  681.             bcopy( (char *)old, (char *)keycodes, ncodes*sizeof(KeyCode) );
  682.             XtFree((char *)old);
  683.         }
  684.         codeP = &keycodes[ncodes];
  685.         }
  686.         *codeP++ = (KeyCode) keycode;
  687.         ncodes++;
  688.     }
  689.     }
  690.     *keycodes_return = keycodes;
  691.     *keycount_return = ncodes;
  692. }
  693.  
  694.  
  695.