home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / TMkey.c.orig < prev    next >
Encoding:
Text File  |  1993-07-21  |  22.3 KB  |  671 lines

  1. /* $XConsortium: TMkey.c,v 1.17 92/11/23 15:44:37 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 TRANSLATE(ctx,pd,dpy,key,mod,mod_ret,sym_ret) \
  90. { \
  91.     int _i_ = (((key) - (pd)->min_keycode + modmix[(mod) & 0xff]) & \
  92.            (TMKEYCACHESIZE-1)); \
  93.     if ((key) != 0 && /* Xlib XIM composed input */ \
  94.     (ctx)->keycache.keycode[_i_] == (key) && \
  95.     (ctx)->keycache.modifiers[_i_] == (mod)) { \
  96.     mod_ret = (ctx)->keycache.modifiers_return; \
  97.     sym_ret = (ctx)->keycache.keysym[_i_]; \
  98.     } else { \
  99.     XtTranslateKeycode(dpy, key, mod, &mod_ret, &sym_ret); \
  100.     (ctx)->keycache.keycode[_i_] = key; \
  101.     (ctx)->keycache.modifiers[_i_] = (unsigned short)(mod); \
  102.     (ctx)->keycache.keysym[_i_] = sym_ret; \
  103.     (ctx)->keycache.modifiers_return = mod_ret; \
  104.     } \
  105. }
  106.  
  107. /* usual number of expected keycodes in XtKeysymToKeycodeList */
  108. #define KEYCODE_ARRAY_SIZE 10
  109.  
  110. static void _XtConvertCase();
  111.  
  112. Boolean _XtComputeLateBindings(lateModifiers,eventSeq,computed,computedMask)
  113.     LateBindingsPtr lateModifiers;
  114.     TMEventPtr eventSeq;
  115.     Modifiers *computed,*computedMask;
  116. {
  117.     int i,j,ref;
  118.     ModToKeysymTable* temp;
  119.     XtPerDisplay perDisplay;
  120.     Display *dpy;
  121.     Boolean found;
  122.     KeySym tempKeysym = NoSymbol;
  123.     dpy = eventSeq->xev->xany.display;
  124.     perDisplay = _XtGetPerDisplay(dpy);
  125.     if (perDisplay == NULL) {
  126.         XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
  127.         "displayError","invalidDisplay",XtCXtToolkitError,
  128.             "Can't find display structure",
  129.             (String *)NULL, (Cardinal *)NULL);
  130.          return FALSE;
  131.     }
  132.     _InitializeKeysymTables(dpy, perDisplay);
  133.     for (ref=0; lateModifiers[ref].keysym; ref++) {
  134.         found = FALSE;
  135.         for (i=0;i<8;i++) {
  136.             temp = &(perDisplay->modsToKeysyms[i]);
  137.             for (j=0;j<temp->count;j++){
  138.                 if (perDisplay->modKeysyms[temp->idx+j] ==
  139.             lateModifiers[ref].keysym) {
  140.                     *computedMask = *computedMask | temp->mask;
  141.                     if (!lateModifiers[ref].knot)
  142.               *computed |= temp->mask;
  143.                     tempKeysym = lateModifiers[ref].keysym;
  144.                     found = TRUE; break;
  145.                 }
  146.             }
  147.             if (found) break;
  148.         }
  149.         if (!found  && !lateModifiers[ref].knot)
  150.             if (!lateModifiers[ref].pair && (tempKeysym == NoSymbol))
  151.                 return FALSE;
  152.         /* if you didn't find the modifier and the modifier must be
  153.            asserted then return FALSE. If you didn't find the modifier
  154.            and the modifier must be off, then it is OK . Don't
  155.            return FALSE if this is the first member of a pair or if
  156.            it is the second member of a pair when the first member
  157.            was bound to a modifier */
  158.     if (!lateModifiers[ref].pair) tempKeysym = NoSymbol;
  159.     }
  160.     return TRUE;
  161. }
  162.  
  163. void _XtAllocTMContext(pd)
  164.     XtPerDisplay pd;
  165. {
  166.     TMKeyContext ctx;
  167.     ctx = (TMKeyContext)_XtHeapAlloc(&pd->heap,
  168.                      sizeof(TMKeyContextRec));
  169.     ctx->event = NULL;
  170.     ctx->serial = 0;
  171.     ctx->keysym = NoSymbol;
  172.     ctx->modifiers = 0;
  173.     FLUSHKEYCACHE(ctx);
  174.     pd->tm_context = ctx;
  175. }
  176.  
  177. Boolean _XtMatchUsingDontCareMods(typeMatch, modMatch, eventSeq)
  178.     TMTypeMatch     typeMatch;
  179.     TMModifierMatch     modMatch;
  180.     TMEventPtr         eventSeq;
  181. {
  182.     Modifiers modifiers_return;
  183.     KeySym keysym_return;
  184.     Modifiers useful_mods;
  185.     int i;
  186.     Modifiers computed = 0;
  187.     Modifiers computedMask = 0;
  188.     Boolean resolved = TRUE;
  189.     Display *dpy = eventSeq->xev->xany.display;
  190.     XtPerDisplay pd;
  191.     TMKeyContext tm_context;
  192.     
  193.     if (modMatch->lateModifiers != NULL)
  194.       resolved = _XtComputeLateBindings(modMatch->lateModifiers,
  195.                     eventSeq,&computed,&computedMask);
  196.     if (!resolved) return FALSE;
  197.     computed |= modMatch->modifiers;
  198.     computedMask |= modMatch->modifierMask; /* gives do-care mask */
  199.     
  200.     if ( (computed & computedMask) ==
  201.         (eventSeq->event.modifiers & computedMask) ) {
  202.     Modifiers least_mod;
  203.     
  204.     pd = _XtGetPerDisplay(dpy);
  205.     tm_context = pd->tm_context;
  206.     TRANSLATE(tm_context, pd, dpy, (KeyCode)eventSeq->event.eventCode,
  207.                 (unsigned)0, modifiers_return, keysym_return);
  208.     
  209.         if ((keysym_return & typeMatch->eventCodeMask)  == typeMatch->eventCode ) {
  210.         tm_context->event = eventSeq->xev;
  211.         tm_context->serial = eventSeq->xev->xany.serial;
  212.         tm_context->keysym = keysym_return;
  213.         tm_context->modifiers = (Modifiers)0;
  214.         return TRUE;
  215.     }
  216.         useful_mods = ~computedMask & modifiers_return;
  217.         if (useful_mods == 0) return FALSE;
  218.     for (least_mod = 1; (least_mod & useful_mods)==0; least_mod <<= 1){/*EMPTY*/};
  219.         for (i = modifiers_return; i >= least_mod; i--)
  220.       /* all useful combinations of 8 modifier bits */
  221.       if (useful_mods & i) {
  222.           TRANSLATE(tm_context, pd, dpy, eventSeq->event.eventCode,
  223.             (Modifiers)i, modifiers_return, keysym_return);
  224.           if (keysym_return  ==
  225.           (typeMatch->eventCode & typeMatch->eventCodeMask)) {
  226.           tm_context->event = eventSeq->xev;
  227.           tm_context->serial = eventSeq->xev->xany.serial;
  228.           tm_context->keysym = keysym_return;
  229.           tm_context->modifiers = (Modifiers)i;
  230.           return TRUE;
  231.           }
  232.       }
  233.     }
  234.     return FALSE;
  235. }
  236.  
  237. void XtConvertCase(dpy,keysym,lower_return,upper_return)
  238.     Display *dpy;
  239.     KeySym keysym;
  240.     KeySym *lower_return, *upper_return;
  241. {
  242.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  243.     register CaseConverterPtr ptr;
  244.  
  245.     *lower_return = *upper_return = keysym;
  246.     for (ptr=pd->case_cvt;  ptr; ptr = ptr->next)
  247.     if (ptr->start <= keysym && keysym <= ptr->stop) {
  248.         (*ptr->proc)(dpy, keysym, lower_return, upper_return);
  249.         return;
  250.     }
  251.     if (keysym <= 0x3ff)    /* Latin-1 start = 0, Latin-4 stop = 0x3ff */
  252.     _XtConvertCase(dpy, keysym, lower_return, upper_return);
  253. }
  254.     
  255. Boolean _XtMatchUsingStandardMods (typeMatch, modMatch, eventSeq)
  256.     TMTypeMatch typeMatch;
  257.     TMModifierMatch modMatch;
  258.     TMEventPtr eventSeq;
  259. {
  260.     Modifiers modifiers_return;
  261.     KeySym keysym_return;
  262.     Modifiers computed= 0;
  263.     Modifiers computedMask = 0;
  264.     Boolean resolved = TRUE;
  265.     Display *dpy = eventSeq->xev->xany.display;
  266.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  267.     TMKeyContext tm_context = pd->tm_context;
  268.     Modifiers translateModifiers;
  269.  
  270.     _InitializeKeysymTables(dpy, pd);
  271.     translateModifiers =(Modifiers)
  272.       (eventSeq->event.modifiers & ((ShiftMask|LockMask) | pd->mode_switch));
  273.  
  274.     TRANSLATE(tm_context, pd, dpy, (KeyCode)eventSeq->event.eventCode,
  275.             translateModifiers, modifiers_return, keysym_return);
  276.  
  277.     if ((typeMatch->eventCode & typeMatch->eventCodeMask) ==
  278.              (keysym_return & typeMatch->eventCodeMask)) {
  279.         if (modMatch->lateModifiers != NULL) 
  280.             resolved = _XtComputeLateBindings(modMatch->lateModifiers,
  281.                        eventSeq,&computed,&computedMask);
  282.         if (!resolved) return FALSE;
  283.         computed |= modMatch->modifiers;
  284.         computedMask |= modMatch->modifierMask;
  285.  
  286.         if ((computed & computedMask) ==
  287.         (eventSeq->event.modifiers & ~modifiers_return & computedMask)) {
  288.         tm_context->event = eventSeq->xev;
  289.         tm_context->serial = eventSeq->xev->xany.serial;
  290.         tm_context->keysym = keysym_return;
  291.         tm_context->modifiers = translateModifiers;
  292.         return TRUE;
  293.     }
  294.     }
  295.     return FALSE;
  296. }
  297.  
  298.  
  299. void _XtBuildKeysymTables(dpy,pd)
  300.     Display *dpy;
  301.     register XtPerDisplay pd;
  302. {
  303.     ModToKeysymTable *table;
  304.     int maxCount,i,j,k,tempCount,idx;
  305.     KeySym keysym,tempKeysym;
  306.     XModifierKeymap* modKeymap;
  307.     KeyCode keycode;
  308. #define KeysymTableSize 16
  309.  
  310.     FLUSHKEYCACHE(pd->tm_context);
  311.     if (pd->keysyms)
  312.     XFree( (char *)pd->keysyms );
  313.     pd->keysyms_serial = NextRequest(dpy);
  314.     pd->keysyms = XGetKeyboardMapping(dpy, pd->min_keycode,
  315.                       pd->max_keycode-pd->min_keycode+1,
  316.                       &pd->keysyms_per_keycode);
  317.     if (pd->modKeysyms)
  318.     XtFree((char *)pd->modKeysyms);
  319.     if (pd->modsToKeysyms)
  320.     XtFree((char *)pd->modsToKeysyms);
  321.     pd->modKeysyms = (KeySym*)XtMalloc((Cardinal)KeysymTableSize*sizeof(KeySym));
  322.     maxCount = KeysymTableSize;
  323.     tempCount = 0;
  324.  
  325.     table = (ModToKeysymTable*)XtMalloc((Cardinal)8*sizeof(ModToKeysymTable));
  326.     pd->modsToKeysyms = table;
  327.  
  328.     table[0].mask = ShiftMask;
  329.     table[1].mask = LockMask;
  330.     table[2].mask = ControlMask;
  331.     table[3].mask = Mod1Mask;
  332.     table[4].mask = Mod2Mask;
  333.     table[5].mask = Mod3Mask;
  334.     table[6].mask = Mod4Mask;
  335.     table[7].mask = Mod5Mask;
  336.     tempKeysym = 0;
  337.  
  338.     modKeymap = XGetModifierMapping(dpy);
  339.     for (i=0;i<32;i++)
  340.     pd->isModifier[i] = 0;
  341.     pd->mode_switch = 0;
  342.     for (i=0;i<8;i++) {
  343.         table[i].idx = tempCount;
  344.         table[i].count = 0;
  345.         for (j=0;j<modKeymap->max_keypermod;j++) {
  346.             keycode = modKeymap->modifiermap[i*modKeymap->max_keypermod+j];
  347.             if (keycode != 0) {
  348.         pd->isModifier[keycode>>3] |= 1 << (keycode & 7);
  349.                 for (k=0; k<pd->keysyms_per_keycode;k++) {
  350.                     idx = ((keycode-pd->min_keycode)*
  351.                              pd->keysyms_per_keycode)+k;
  352.                     keysym = pd->keysyms[idx];
  353.             if ((keysym == XK_Mode_switch) && (i > 2))
  354.             pd->mode_switch |= 1 << i;
  355.                     if (keysym != 0 && keysym != tempKeysym ){
  356.                         if (tempCount==maxCount) {
  357.                             maxCount += KeysymTableSize;
  358.                             pd->modKeysyms = (KeySym*)XtRealloc(
  359.                                 (char*)pd->modKeysyms,
  360.                                 (unsigned) (maxCount*sizeof(KeySym)) );
  361.                         }
  362.                         pd->modKeysyms[tempCount++] = keysym;
  363.                         table[i].count++;
  364.                         tempKeysym = keysym;
  365.                     }
  366.                 }
  367.             }
  368.         }
  369.     }
  370.     pd->lock_meaning = NoSymbol;
  371.     for (i = 0; i < table[1].count; i++) {
  372.     keysym = pd->modKeysyms[table[1].idx + i];
  373.     if (keysym == XK_Caps_Lock) {
  374.         pd->lock_meaning = XK_Caps_Lock;
  375.         break;
  376.     } else if (keysym == XK_Shift_Lock) {
  377.         pd->lock_meaning = XK_Shift_Lock;
  378.     }
  379.     }
  380.     XFreeModifiermap(modKeymap);
  381. }
  382.  
  383. #if NeedFunctionPrototypes
  384. void XtTranslateKeycode (
  385.     Display *dpy, 
  386.     _XtKeyCode keycode,
  387.     Modifiers modifiers,
  388.     Modifiers *modifiers_return,
  389.     KeySym *keysym_return
  390.     )
  391. #else
  392. void XtTranslateKeycode (dpy, keycode, modifiers,
  393.                             modifiers_return, keysym_return)
  394.  
  395.     Display *dpy;
  396.     KeyCode keycode;
  397.     Modifiers modifiers;
  398.     Modifiers *modifiers_return;
  399.     KeySym *keysym_return;
  400. #endif
  401. {
  402.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  403.     _InitializeKeysymTables(dpy, pd);
  404.     (*pd->defaultKeycodeTranslator)(
  405.             dpy,keycode,modifiers,modifiers_return,keysym_return);
  406. }
  407.  
  408. /* This code should match XTranslateKey (internal, sigh) in Xlib */
  409. #if NeedFunctionPrototypes
  410. void XtTranslateKey(
  411.     register Display *dpy,
  412.     _XtKeyCode keycode,
  413.     Modifiers modifiers,
  414.     Modifiers *modifiers_return,
  415.     KeySym *keysym_return
  416.     )
  417. #else
  418. void XtTranslateKey(dpy, keycode, modifiers,
  419.                             modifiers_return, keysym_return)
  420.     register Display *dpy;
  421.     KeyCode keycode;
  422.     Modifiers modifiers;
  423.     Modifiers *modifiers_return;
  424.     KeySym *keysym_return;
  425. #endif
  426. {
  427.     register XtPerDisplay pd = _XtGetPerDisplay(dpy);
  428.     int per;
  429.     register KeySym *syms;
  430.     KeySym sym, lsym, usym;
  431.  
  432.     *modifiers_return = (ShiftMask|LockMask) | pd->mode_switch;
  433.     if (((int)keycode < pd->min_keycode) || ((int)keycode > pd->max_keycode)) {
  434.     *keysym_return = NoSymbol;
  435.     return;
  436.     }
  437.     per = pd->keysyms_per_keycode;
  438.     syms = &pd->keysyms[(keycode - pd->min_keycode) * per];
  439.     while ((per > 2) && (syms[per - 1] == NoSymbol))
  440.     per--;
  441.     if ((per > 2) && (modifiers & pd->mode_switch)) {
  442.     syms += 2;
  443.     per -= 2;
  444.     }
  445.     if (!(modifiers & ShiftMask) &&
  446.     (!(modifiers & LockMask) || (pd->lock_meaning == NoSymbol))) {
  447.     if ((per == 1) || (syms[1] == NoSymbol))
  448.         XtConvertCase(dpy, syms[0], keysym_return, &usym);
  449.     else
  450.         *keysym_return = syms[0];
  451.     } else if (!(modifiers & LockMask) ||
  452.            (pd->lock_meaning != XK_Caps_Lock)) {
  453.     if ((per == 1) || ((usym = syms[1]) == NoSymbol))
  454.         XtConvertCase(dpy, syms[0], &lsym, &usym);
  455.     *keysym_return = usym;
  456.     } else {
  457.     if ((per == 1) || ((sym = syms[1]) == NoSymbol))
  458.         sym = syms[0];
  459.     XtConvertCase(dpy, sym, &lsym, &usym);
  460.     if (!(modifiers & ShiftMask) && (sym != syms[0]) &&
  461.         ((sym != usym) || (lsym == usym)))
  462.         XtConvertCase(dpy, syms[0], &lsym, &usym);
  463.     *keysym_return = usym;
  464.     }
  465.  
  466.     if (*keysym_return == XK_VoidSymbol)
  467.     *keysym_return = NoSymbol;
  468. }
  469.  
  470. void XtSetKeyTranslator(dpy, translator)
  471.  
  472.     Display *dpy;
  473.     XtKeyProc translator;
  474.  
  475. {
  476.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  477.  
  478.     pd->defaultKeycodeTranslator = translator;
  479.     FLUSHKEYCACHE(pd->tm_context);
  480.     /* XXX should now redo grabs */
  481. }
  482.  
  483. void XtRegisterCaseConverter(dpy, proc, start, stop)
  484.     Display *dpy;
  485.     XtCaseProc proc;
  486.     KeySym start;
  487.     KeySym stop;
  488. {
  489.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  490.     CaseConverterPtr ptr, prev;
  491.  
  492.     ptr = (CaseConverterPtr) XtMalloc(sizeof(CaseConverterRec));
  493.     ptr->start = start;
  494.     ptr->stop = stop;
  495.     ptr->proc = proc;
  496.     ptr->next = pd->case_cvt;
  497.     pd->case_cvt = ptr;
  498.  
  499.     /* Remove obsolete case converters from the list */
  500.     prev = ptr;
  501.     for (ptr=ptr->next; ptr; ptr=prev->next) {
  502.     if (start <= ptr->start && stop >= ptr->stop) {
  503.         prev->next = ptr->next;
  504.         XtFree((char *)ptr);
  505.     } 
  506.     else prev = ptr;
  507.     }
  508.     FLUSHKEYCACHE(pd->tm_context);
  509.     /* XXX should now redo grabs */
  510. }
  511.  
  512. /* This code should match XConvertCase (internal, sigh) in Xlib */
  513. /* ARGSUSED */
  514. static void _XtConvertCase(dpy, sym, lower, upper)
  515.     Display *dpy;
  516.     KeySym sym;
  517.     KeySym *lower;
  518.     KeySym *upper;
  519. {
  520.     *lower = sym;
  521.     *upper = sym;
  522.     switch(sym >> 8) {
  523.     case 0:
  524.     if ((sym >= XK_A) && (sym <= XK_Z))
  525.         *lower += (XK_a - XK_A);
  526.     else if ((sym >= XK_a) && (sym <= XK_z))
  527.         *upper -= (XK_a - XK_A);
  528.     else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
  529.         *lower += (XK_agrave - XK_Agrave);
  530.     else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
  531.         *upper -= (XK_agrave - XK_Agrave);
  532.     else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
  533.         *lower += (XK_oslash - XK_Ooblique);
  534.     else if ((sym >= XK_oslash) && (sym <= XK_thorn))
  535.         *upper -= (XK_oslash - XK_Ooblique);
  536.     break;
  537. #ifdef XK_LATIN2
  538.       case 1:
  539.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  540.     if (sym == XK_Aogonek)
  541.         *lower = XK_aogonek;
  542.     else if (sym >= XK_Lstroke && sym <= XK_Sacute)
  543.         *lower += (XK_lstroke - XK_Lstroke);
  544.     else if (sym >= XK_Scaron && sym <= XK_Zacute)
  545.         *lower += (XK_scaron - XK_Scaron);
  546.     else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
  547.         *lower += (XK_zcaron - XK_Zcaron);
  548.     else if (sym == XK_aogonek)
  549.         *upper = XK_Aogonek;
  550.     else if (sym >= XK_lstroke && sym <= XK_sacute)
  551.         *upper -= (XK_lstroke - XK_Lstroke);
  552.     else if (sym >= XK_scaron && sym <= XK_zacute)
  553.         *upper -= (XK_scaron - XK_Scaron);
  554.     else if (sym >= XK_zcaron && sym <= XK_zabovedot)
  555.         *upper -= (XK_zcaron - XK_Zcaron);
  556.     else if (sym >= XK_Racute && sym <= XK_Tcedilla)
  557.         *lower += (XK_racute - XK_Racute);
  558.     else if (sym >= XK_racute && sym <= XK_tcedilla)
  559.         *upper -= (XK_racute - XK_Racute);
  560.     break;
  561. #endif
  562. #ifdef XK_LATIN3
  563.       case 2:
  564.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  565.     if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
  566.         *lower += (XK_hstroke - XK_Hstroke);
  567.     else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
  568.         *lower += (XK_gbreve - XK_Gbreve);
  569.     else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
  570.         *upper -= (XK_hstroke - XK_Hstroke);
  571.     else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
  572.         *upper -= (XK_gbreve - XK_Gbreve);
  573.     else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
  574.         *lower += (XK_cabovedot - XK_Cabovedot);
  575.     else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
  576.         *upper -= (XK_cabovedot - XK_Cabovedot);
  577.     break;
  578. #endif
  579. #ifdef XK_LATIN4
  580.       case 3:
  581.     /* Assume the KeySym is a legal value (ignore discontinuities) */
  582.     if (sym >= XK_Rcedilla && sym <= XK_Tslash)
  583.         *lower += (XK_rcedilla - XK_Rcedilla);
  584.     else if (sym >= XK_rcedilla && sym <= XK_tslash)
  585.         *upper -= (XK_rcedilla - XK_Rcedilla);
  586.     else if (sym == XK_ENG)
  587.         *lower = XK_eng;
  588.     else if (sym == XK_eng)
  589.         *upper = XK_ENG;
  590.     else if (sym >= XK_Amacron && sym <= XK_Umacron)
  591.         *lower += (XK_amacron - XK_Amacron);
  592.     else if (sym >= XK_amacron && sym <= XK_umacron)
  593.         *upper -= (XK_amacron - XK_Amacron);
  594.     break;
  595. #endif
  596.     }
  597. }
  598.  
  599.  
  600. KeySym *XtGetKeysymTable(dpy, min_keycode_return, keysyms_per_keycode_return)
  601.     Display *dpy;
  602.     KeyCode *min_keycode_return;
  603.     int *keysyms_per_keycode_return;
  604. {
  605.     XtPerDisplay pd = _XtGetPerDisplay(dpy);
  606.     _InitializeKeysymTables(dpy, pd);
  607.     *min_keycode_return = pd->min_keycode; /* %%% */
  608.     *keysyms_per_keycode_return = pd->keysyms_per_keycode;
  609.     return pd->keysyms;
  610. }
  611.  
  612. void XtKeysymToKeycodeList(dpy, keysym, keycodes_return, keycount_return)
  613.     Display *dpy;
  614.     KeySym keysym;
  615.     KeyCode **keycodes_return;
  616.     Cardinal *keycount_return;
  617. {
  618.     register XtPerDisplay pd = _XtGetPerDisplay(dpy);
  619.     unsigned keycode;
  620.     int per, match;
  621.     register KeySym *syms;
  622.     register int i, j;
  623.     KeySym lsym, usym;
  624.     unsigned maxcodes = 0;
  625.     unsigned ncodes = 0;
  626.     KeyCode *keycodes, *codeP;
  627.  
  628.     _InitializeKeysymTables(dpy, pd);
  629.     keycodes = NULL;
  630.     per = pd->keysyms_per_keycode;
  631.     for (syms = pd->keysyms, keycode = (unsigned) pd->min_keycode;
  632.      (int)keycode <= pd->max_keycode;
  633.      syms += per, keycode++) {
  634.     match = 0;
  635.     for (j = 0; j < per; j++) {
  636.         if (syms[j] == keysym) {
  637.         match = 1;
  638.         break;
  639.         }        
  640.     }
  641.     if (!match)
  642.         for (i = 1; i < 5; i += 2) {
  643.         if ((per == i) || ((per > i) && (syms[i] == NoSymbol))) {
  644.             XtConvertCase(dpy, syms[i-1], &lsym, &usym);
  645.             if ((lsym == keysym) || (usym == keysym)) {
  646.             match = 1;
  647.             break;
  648.             }
  649.         }
  650.         }
  651.     if (match) {
  652.         if (ncodes == maxcodes) {
  653.         KeyCode *old = keycodes;
  654.         maxcodes += KEYCODE_ARRAY_SIZE;
  655.         keycodes = (KeyCode*)XtMalloc(maxcodes*sizeof(KeyCode));
  656.         if (ncodes) {
  657.             bcopy( (char *)old, (char *)keycodes, ncodes*sizeof(KeyCode) );
  658.             XtFree((char *)old);
  659.         }
  660.         codeP = &keycodes[ncodes];
  661.         }
  662.         *codeP++ = (KeyCode) keycode;
  663.         ncodes++;
  664.     }
  665.     }
  666.     *keycodes_return = keycodes;
  667.     *keycount_return = ncodes;
  668. }
  669.  
  670.  
  671.