PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libX11-1.5.0/src/xkb/XKBBind.c

#
C | 848 lines | 716 code | 81 blank | 51 comment | 161 complexity | cbfad56b75ab1a63640ca412b0063ed9 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. Copyright 1985, 1987, 1994, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /* the new monsters ate the old ones */
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include "XKBlib.h"
  27. #include <X11/Xlibint.h>
  28. #include <X11/Xutil.h>
  29. #include <X11/keysym.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <X11/extensions/XKBproto.h>
  33. #include "XKBlibint.h"
  34. #ifdef USE_OWN_COMPOSE
  35. #define COMPOSE_NO_CONST_MEMBERS
  36. #include "imComp.h"
  37. #endif
  38. #define AllMods (ShiftMask|LockMask|ControlMask| \
  39. Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)
  40. static int _XkbLoadDpy(
  41. Display *dpy
  42. );
  43. struct _XKeytrans {
  44. struct _XKeytrans *next;/* next on list */
  45. char *string; /* string to return when the time comes */
  46. int len; /* length of string (since NULL is legit)*/
  47. KeySym key; /* keysym rebound */
  48. unsigned int state; /* modifier state */
  49. KeySym *modifiers; /* modifier keysyms you want */
  50. int mlen; /* length of modifier list */
  51. };
  52. KeySym
  53. XkbKeycodeToKeysym(Display *dpy,
  54. #if NeedWidePrototypes
  55. unsigned int kc,
  56. #else
  57. KeyCode kc,
  58. #endif
  59. int group,
  60. int level)
  61. {
  62. XkbDescRec *xkb;
  63. if (_XkbUnavailable(dpy))
  64. return NoSymbol;
  65. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  66. xkb = dpy->xkb_info->desc;
  67. if ((kc<xkb->min_key_code)||(kc>xkb->max_key_code))
  68. return NoSymbol;
  69. if ((group<0)||(level<0)||(group>=XkbKeyNumGroups(xkb,kc)))
  70. return NoSymbol;
  71. if (level>=XkbKeyGroupWidth(xkb,kc,group)) {
  72. /* for compatibility with the core protocol, _always_ allow */
  73. /* two symbols in the first two groups. If either of the */
  74. /* two is of type ONE_LEVEL, just replicate the first symbol */
  75. if ((group>XkbGroup2Index)||(XkbKeyGroupWidth(xkb,kc,group)!=1)||
  76. (level!=1)) {
  77. return NoSymbol;
  78. }
  79. level= 0;
  80. }
  81. return XkbKeySymEntry(xkb,kc,level,group);
  82. }
  83. KeySym
  84. XKeycodeToKeysym(Display *dpy,
  85. #if NeedWidePrototypes
  86. unsigned int kc,
  87. #else
  88. KeyCode kc,
  89. #endif
  90. int col)
  91. {
  92. XkbDescRec *xkb;
  93. if (_XkbUnavailable(dpy))
  94. return _XKeycodeToKeysym(dpy, kc, col);
  95. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  96. xkb = dpy->xkb_info->desc;
  97. if ((kc<xkb->min_key_code)||(kc>xkb->max_key_code))
  98. return NoSymbol;
  99. if (col>3) {
  100. int lastSym,tmp,nGrp;
  101. lastSym= 3;
  102. nGrp= XkbKeyNumGroups(xkb,kc);
  103. if ((nGrp>0)&&((tmp=XkbKeyGroupWidth(xkb,kc,XkbGroup1Index))>2)) {
  104. if (col<=(lastSym+tmp-2))
  105. return XkbKeycodeToKeysym(dpy,kc,XkbGroup1Index,col-lastSym+2);
  106. lastSym+= tmp-2;
  107. }
  108. if ((nGrp>1)&&((tmp=XkbKeyGroupWidth(xkb,kc,XkbGroup2Index))>2)) {
  109. if (col<=(lastSym+tmp-2))
  110. return XkbKeycodeToKeysym(dpy,kc,XkbGroup2Index,col-lastSym+2);
  111. lastSym+= tmp-2;
  112. }
  113. if (nGrp>2) {
  114. tmp= XkbKeyGroupWidth(xkb,kc,XkbGroup3Index);
  115. if (col<=lastSym+tmp)
  116. return XkbKeycodeToKeysym(dpy,kc,XkbGroup3Index,col-lastSym);
  117. lastSym+= tmp;
  118. }
  119. if (nGrp>3) {
  120. tmp= XkbKeyGroupWidth(xkb,kc,XkbGroup4Index);
  121. if (col<=lastSym+tmp)
  122. return XkbKeycodeToKeysym(dpy,kc,XkbGroup4Index,col-lastSym);
  123. }
  124. return NoSymbol;
  125. }
  126. return XkbKeycodeToKeysym(dpy,kc,(col>>1),(col&1));
  127. }
  128. KeyCode
  129. XKeysymToKeycode(Display *dpy, KeySym ks)
  130. {
  131. register int i, j, gotOne;
  132. if (_XkbUnavailable(dpy))
  133. return _XKeysymToKeycode(dpy,ks);
  134. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  135. j= 0;
  136. do {
  137. register XkbDescRec *xkb = dpy->xkb_info->desc;
  138. gotOne= 0;
  139. for (i = dpy->min_keycode; i <= dpy->max_keycode; i++) {
  140. if ( j<(int)XkbKeyNumSyms(xkb,i) ) {
  141. gotOne = 1;
  142. if ((XkbKeySym(xkb,i,j)==ks))
  143. return i;
  144. }
  145. }
  146. j++;
  147. } while (gotOne);
  148. return 0;
  149. }
  150. static int
  151. _XkbComputeModmap(Display *dpy)
  152. {
  153. register XkbDescPtr xkb;
  154. xkb= dpy->xkb_info->desc;
  155. if (XkbGetUpdatedMap(dpy,XkbModifierMapMask,xkb)==Success)
  156. return 1;
  157. return 0;
  158. }
  159. unsigned
  160. XkbKeysymToModifiers(Display *dpy,KeySym ks)
  161. {
  162. XkbDescRec *xkb;
  163. register int i,j;
  164. register KeySym *pSyms;
  165. CARD8 mods;
  166. if (_XkbUnavailable(dpy))
  167. return _XKeysymToModifiers(dpy,ks);
  168. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  169. if (_XkbNeedModmap(dpy->xkb_info)&&(!_XkbComputeModmap(dpy)))
  170. return _XKeysymToModifiers(dpy,ks);
  171. xkb= dpy->xkb_info->desc;
  172. mods= 0;
  173. for (i = xkb->min_key_code; i <= (int)xkb->max_key_code; i++) {
  174. pSyms= XkbKeySymsPtr(xkb,i);
  175. for (j=XkbKeyNumSyms(xkb,i)-1;j>=0;j--) {
  176. if (pSyms[j]==ks) {
  177. mods|= xkb->map->modmap[i];
  178. break;
  179. }
  180. }
  181. }
  182. return mods;
  183. }
  184. KeySym
  185. XLookupKeysym(register XKeyEvent *event, int col)
  186. {
  187. Display *dpy = event->display;
  188. if (_XkbUnavailable(dpy))
  189. return _XLookupKeysym(event, col);
  190. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  191. return XKeycodeToKeysym(dpy, event->keycode, col);
  192. }
  193. /*
  194. * Not a public entry point -- XkbTranslateKey is an obsolete name
  195. * that is preserved here so that functions linked against the old
  196. * version will continue to work in a shared library environment.
  197. */
  198. int
  199. XkbTranslateKey( register Display * dpy,
  200. KeyCode key,
  201. register unsigned int mods,
  202. unsigned int * mods_rtrn,
  203. KeySym * keysym_rtrn);
  204. int
  205. XkbTranslateKey( register Display * dpy,
  206. KeyCode key,
  207. register unsigned int mods,
  208. unsigned int * mods_rtrn,
  209. KeySym * keysym_rtrn)
  210. {
  211. return XkbLookupKeySym(dpy,key,mods,mods_rtrn,keysym_rtrn);
  212. }
  213. Bool
  214. XkbLookupKeySym( register Display * dpy,
  215. KeyCode key,
  216. register unsigned int mods,
  217. unsigned int * mods_rtrn,
  218. KeySym * keysym_rtrn)
  219. {
  220. if (_XkbUnavailable(dpy))
  221. return _XTranslateKey(dpy, key, mods, mods_rtrn, keysym_rtrn);
  222. _XkbCheckPendingRefresh(dpy,dpy->xkb_info);
  223. return XkbTranslateKeyCode(dpy->xkb_info->desc,key,mods,mods_rtrn,
  224. keysym_rtrn);
  225. }
  226. Bool
  227. XkbTranslateKeyCode( register XkbDescPtr xkb,
  228. KeyCode key,
  229. register unsigned int mods,
  230. unsigned int * mods_rtrn,
  231. KeySym * keysym_rtrn)
  232. {
  233. XkbKeyTypeRec *type;
  234. int col,nKeyGroups;
  235. unsigned preserve,effectiveGroup;
  236. KeySym *syms;
  237. if (mods_rtrn!=NULL)
  238. *mods_rtrn = 0;
  239. nKeyGroups= XkbKeyNumGroups(xkb,key);
  240. if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
  241. if (keysym_rtrn!=NULL)
  242. *keysym_rtrn = NoSymbol;
  243. return False;
  244. }
  245. syms = XkbKeySymsPtr(xkb,key);
  246. /* find the offset of the effective group */
  247. col = 0;
  248. effectiveGroup= XkbGroupForCoreState(mods);
  249. if ( effectiveGroup>=nKeyGroups ) {
  250. unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
  251. switch (XkbOutOfRangeGroupAction(groupInfo)) {
  252. default:
  253. effectiveGroup %= nKeyGroups;
  254. break;
  255. case XkbClampIntoRange:
  256. effectiveGroup = nKeyGroups-1;
  257. break;
  258. case XkbRedirectIntoRange:
  259. effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
  260. if (effectiveGroup>=nKeyGroups)
  261. effectiveGroup= 0;
  262. break;
  263. }
  264. }
  265. col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
  266. type = XkbKeyKeyType(xkb,key,effectiveGroup);
  267. preserve= 0;
  268. if (type->map) { /* find the column (shift level) within the group */
  269. register int i;
  270. register XkbKTMapEntryPtr entry;
  271. for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
  272. if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
  273. col+= entry->level;
  274. if (type->preserve)
  275. preserve= type->preserve[i].mask;
  276. break;
  277. }
  278. }
  279. }
  280. if (keysym_rtrn!=NULL)
  281. *keysym_rtrn= syms[col];
  282. if (mods_rtrn) {
  283. *mods_rtrn= type->mods.mask&(~preserve);
  284. /* The Motif VTS doesn't get the help callback called if help
  285. * is bound to Shift+<whatever>, and it appears as though it
  286. * is XkbTranslateKeyCode that is causing the problem. The
  287. * core X version of XTranslateKey always OR's in ShiftMask
  288. * and LockMask for mods_rtrn, so this "fix" keeps this behavior
  289. * and solves the VTS problem.
  290. */
  291. if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
  292. (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) {
  293. *mods_rtrn|= (ShiftMask|LockMask);
  294. }
  295. }
  296. return (syms[col]!=NoSymbol);
  297. }
  298. Status
  299. XkbRefreshKeyboardMapping(register XkbMapNotifyEvent *event)
  300. {
  301. Display *dpy = event->display;
  302. XkbInfoPtr xkbi;
  303. if (_XkbUnavailable(dpy)) {
  304. _XRefreshKeyboardMapping((XMappingEvent *)event);
  305. return Success;
  306. }
  307. xkbi= dpy->xkb_info;
  308. if (((event->type&0x7f)-xkbi->codes->first_event)!=XkbEventCode)
  309. return BadMatch;
  310. if (event->xkb_type==XkbNewKeyboardNotify) {
  311. _XkbReloadDpy(dpy);
  312. return Success;
  313. }
  314. if (event->xkb_type==XkbMapNotify) {
  315. XkbMapChangesRec changes;
  316. Status rtrn;
  317. if (xkbi->flags&XkbMapPending)
  318. changes= xkbi->changes;
  319. else bzero(&changes,sizeof(changes));
  320. XkbNoteMapChanges(&changes,event,XKB_XLIB_MAP_MASK);
  321. if ((rtrn=XkbGetMapChanges(dpy,xkbi->desc,&changes))!=Success) {
  322. #ifdef DEBUG
  323. fprintf(stderr,"Internal Error! XkbGetMapChanges failed:\n");
  324. #endif
  325. xkbi->changes= changes;
  326. }
  327. else if (xkbi->flags&XkbMapPending) {
  328. xkbi->flags&= ~XkbMapPending;
  329. bzero(&xkbi->changes,sizeof(XkbMapChangesRec));
  330. }
  331. return rtrn;
  332. }
  333. return BadMatch;
  334. }
  335. int
  336. XRefreshKeyboardMapping(register XMappingEvent *event)
  337. {
  338. XkbEvent *xkbevent = (XkbEvent *)event;
  339. Display *dpy = event->display;
  340. XkbMapChangesRec changes;
  341. XkbInfoPtr xkbi;
  342. /* always do this for input methods, which still use the old keymap */
  343. (void) _XRefreshKeyboardMapping(event);
  344. if (_XkbUnavailable(dpy))
  345. return 1;
  346. xkbi = dpy->xkb_info;
  347. if (((event->type&0x7f)-xkbi->codes->first_event)==XkbEventCode)
  348. return XkbRefreshKeyboardMapping(&xkbevent->map);
  349. if (xkbi->flags&XkbXlibNewKeyboard) {
  350. _XkbReloadDpy(dpy);
  351. return 1;
  352. }
  353. if ((xkbi->flags&XkbMapPending)||(event->request==MappingKeyboard)) {
  354. if (xkbi->flags&XkbMapPending) {
  355. changes= xkbi->changes;
  356. _XkbNoteCoreMapChanges(&changes,event,XKB_XLIB_MAP_MASK);
  357. }
  358. else {
  359. bzero(&changes,sizeof(changes));
  360. changes.changed= XkbKeySymsMask;
  361. if (xkbi->desc->min_key_code<xkbi->desc->max_key_code) {
  362. changes.first_key_sym= xkbi->desc->min_key_code;
  363. changes.num_key_syms= xkbi->desc->max_key_code-
  364. xkbi->desc->min_key_code+1;
  365. }
  366. else {
  367. changes.first_key_sym= event->first_keycode;
  368. changes.num_key_syms= event->count;
  369. }
  370. }
  371. if (XkbGetMapChanges(dpy,xkbi->desc, &changes)!=Success) {
  372. #ifdef DEBUG
  373. fprintf(stderr,"Internal Error! XkbGetMapChanges failed:\n");
  374. if (changes.changed&XkbKeyTypesMask) {
  375. int first= changes.first_type;
  376. int last= changes.first_type+changes.num_types-1;
  377. fprintf(stderr," types: %d..%d\n",first,last);
  378. }
  379. if (changes.changed&XkbKeySymsMask) {
  380. int first= changes.first_key_sym;
  381. int last= changes.first_key_sym+changes.num_key_syms-1;
  382. fprintf(stderr," symbols: %d..%d\n",first,last);
  383. }
  384. if (changes.changed&XkbKeyActionsMask) {
  385. int last,first= changes.first_key_act;
  386. last= changes.first_key_act+changes.num_key_acts-1;
  387. fprintf(stderr," acts: %d..%d\n",first,last);
  388. }
  389. if (changes.changed&XkbKeyBehaviorsMask) {
  390. int last,first= changes.first_key_behavior;
  391. last= first+changes.num_key_behaviors-1;
  392. fprintf(stderr," behaviors: %d..%d\n",first,last);
  393. }
  394. if (changes.changed&XkbVirtualModsMask) {
  395. fprintf(stderr,"virtual mods: 0x%04x\n",
  396. changes.vmods);
  397. }
  398. if (changes.changed&XkbExplicitComponentsMask) {
  399. int last,first= changes.first_key_explicit;
  400. last= first+changes.num_key_explicit-1;
  401. fprintf(stderr," explicit: %d..%d\n",first,last);
  402. }
  403. #endif
  404. }
  405. LockDisplay(dpy);
  406. if (xkbi->flags&XkbMapPending) {
  407. xkbi->flags&= ~XkbMapPending;
  408. bzero(&xkbi->changes,sizeof(XkbMapChangesRec));
  409. }
  410. UnlockDisplay(dpy);
  411. }
  412. if (event->request==MappingModifier) {
  413. LockDisplay(dpy);
  414. if (xkbi->desc->map->modmap) {
  415. _XkbFree(xkbi->desc->map->modmap);
  416. xkbi->desc->map->modmap= NULL;
  417. }
  418. if (dpy->key_bindings) {
  419. register struct _XKeytrans *p;
  420. for (p = dpy->key_bindings; p; p = p->next) {
  421. register int i;
  422. p->state= 0;
  423. if (p->mlen>0) {
  424. for (i = 0; i < p->mlen; i++) {
  425. p->state|= XkbKeysymToModifiers(dpy,p->modifiers[i]);
  426. }
  427. if (p->state) p->state &= AllMods;
  428. else p->state = AnyModifier;
  429. }
  430. }
  431. }
  432. UnlockDisplay(dpy);
  433. }
  434. return 1;
  435. }
  436. static int
  437. _XkbLoadDpy(Display *dpy)
  438. {
  439. XkbInfoPtr xkbi;
  440. unsigned query,oldEvents;
  441. XkbDescRec *desc;
  442. if (!XkbUseExtension(dpy,NULL,NULL))
  443. return 0;
  444. xkbi = dpy->xkb_info;
  445. query = XkbAllClientInfoMask;
  446. desc = XkbGetMap(dpy,query,XkbUseCoreKbd);
  447. if (!desc) {
  448. #ifdef DEBUG
  449. fprintf(stderr,"Warning! XkbGetMap failed!\n");
  450. #endif
  451. return 0;
  452. }
  453. LockDisplay(dpy);
  454. xkbi->desc = desc;
  455. UnlockDisplay(dpy);
  456. oldEvents= xkbi->selected_events;
  457. if (!(xkbi->xlib_ctrls&XkbLC_IgnoreNewKeyboards)) {
  458. XkbSelectEventDetails(dpy,xkbi->desc->device_spec,XkbNewKeyboardNotify,
  459. XkbNKN_KeycodesMask|XkbNKN_DeviceIDMask,
  460. XkbNKN_KeycodesMask|XkbNKN_DeviceIDMask);
  461. }
  462. XkbSelectEventDetails(dpy,xkbi->desc->device_spec,XkbMapNotify,
  463. XkbAllClientInfoMask,XkbAllClientInfoMask);
  464. LockDisplay(dpy);
  465. xkbi->selected_events= oldEvents;
  466. UnlockDisplay(dpy);
  467. return 1;
  468. }
  469. void
  470. _XkbReloadDpy(Display *dpy)
  471. {
  472. XkbInfoPtr xkbi;
  473. XkbDescRec *desc;
  474. unsigned oldDeviceID;
  475. if (_XkbUnavailable(dpy))
  476. return;
  477. xkbi = dpy->xkb_info;
  478. LockDisplay(dpy);
  479. if (xkbi->desc) {
  480. oldDeviceID= xkbi->desc->device_spec;
  481. XkbFreeKeyboard(xkbi->desc,XkbAllComponentsMask,True);
  482. xkbi->desc= NULL;
  483. xkbi->flags&= ~(XkbMapPending|XkbXlibNewKeyboard);
  484. xkbi->changes.changed= 0;
  485. }
  486. else oldDeviceID= XkbUseCoreKbd;
  487. UnlockDisplay(dpy);
  488. desc = XkbGetMap(dpy,XkbAllClientInfoMask,XkbUseCoreKbd);
  489. if (!desc)
  490. return;
  491. LockDisplay(dpy);
  492. xkbi->desc = desc;
  493. UnlockDisplay(dpy);
  494. if (desc->device_spec!=oldDeviceID) {
  495. /* transfer(?) event masks here */
  496. #ifdef NOTYET
  497. unsigned oldEvents;
  498. oldEvents= xkbi->selected_events;
  499. XkbSelectEventDetails(dpy,xkbi->desc->device_spec,XkbMapNotify,
  500. XkbAllMapComponentsMask,XkbAllClientInfoMask);
  501. LockDisplay(dpy);
  502. xkbi->selected_events= oldEvents;
  503. UnlockDisplay(dpy);
  504. #endif
  505. }
  506. return;
  507. }
  508. int
  509. XkbTranslateKeySym( register Display * dpy,
  510. register KeySym * sym_rtrn,
  511. unsigned int mods,
  512. char * buffer,
  513. int nbytes,
  514. int * extra_rtrn)
  515. {
  516. register XkbInfoPtr xkb;
  517. XkbKSToMBFunc cvtr;
  518. XPointer priv;
  519. char tmp[4];
  520. int n;
  521. xkb= dpy->xkb_info;
  522. if (!xkb->cvt.KSToMB) {
  523. _XkbGetConverters(_XkbGetCharset(),&xkb->cvt);
  524. _XkbGetConverters("ISO8859-1",&xkb->latin1cvt);
  525. }
  526. if (extra_rtrn)
  527. *extra_rtrn= 0;
  528. if ((buffer==NULL)||(nbytes==0)) {
  529. buffer= tmp;
  530. nbytes= 4;
  531. }
  532. /* see if symbol rebound, if so, return that string. */
  533. n = XkbLookupKeyBinding(dpy,*sym_rtrn,mods,buffer,nbytes,extra_rtrn);
  534. if (n)
  535. return n;
  536. if ( nbytes>0 )
  537. buffer[0]= '\0';
  538. if ( xkb->cvt.KSToUpper && (mods&LockMask) ) {
  539. *sym_rtrn = (*xkb->cvt.KSToUpper)(*sym_rtrn);
  540. }
  541. if (xkb->xlib_ctrls & XkbLC_ForceLatin1Lookup) {
  542. cvtr = xkb->latin1cvt.KSToMB;
  543. priv = xkb->latin1cvt.KSToMBPriv;
  544. } else {
  545. cvtr = xkb->cvt.KSToMB;
  546. priv = xkb->cvt.KSToMBPriv;
  547. }
  548. n = (*cvtr)(priv,*sym_rtrn,buffer,nbytes,extra_rtrn);
  549. if ((!xkb->cvt.KSToUpper)&&( mods&LockMask )) {
  550. register int i;
  551. int change;
  552. char ch;
  553. for (i=change=0;i<n;i++) {
  554. ch= toupper(buffer[i]);
  555. change= (change||(buffer[i]!=ch));
  556. buffer[i] = ch;
  557. }
  558. if (change) {
  559. if (n==1)
  560. *sym_rtrn=(*xkb->cvt.MBToKS)(xkb->cvt.MBToKSPriv,buffer,n,NULL);
  561. else *sym_rtrn= NoSymbol;
  562. }
  563. }
  564. if ( mods&ControlMask ) {
  565. if ( n==1 ) {
  566. buffer[0]= XkbToControl(buffer[0]);
  567. if ( nbytes>1 )
  568. buffer[1]= '\0';
  569. return 1;
  570. }
  571. if ( nbytes > 0 )
  572. buffer[0]= '\0';
  573. return 0;
  574. }
  575. return n;
  576. }
  577. int
  578. XLookupString ( register XKeyEvent * event,
  579. char * buffer,
  580. int nbytes,
  581. KeySym * keysym,
  582. XComposeStatus * status)
  583. {
  584. KeySym dummy;
  585. int rtrnLen;
  586. unsigned int new_mods;
  587. Display *dpy = event->display;
  588. if (keysym==NULL)
  589. keysym= &dummy;
  590. if (!XkbLookupKeySym(dpy,event->keycode,event->state, &new_mods,keysym))
  591. return 0;
  592. new_mods= (event->state&(~new_mods));
  593. /* find the group where a symbol can be converted to control one */
  594. if (new_mods&ControlMask && *keysym > 0x7F &&
  595. (dpy->xkb_info->xlib_ctrls & XkbLC_ControlFallback)) {
  596. XKeyEvent tmp_ev = *event;
  597. KeySym tmp_keysym;
  598. unsigned int tmp_new_mods;
  599. if (_XkbUnavailable(dpy)) {
  600. tmp_ev.state= event->state ^ dpy->mode_switch;
  601. if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
  602. &tmp_new_mods, &tmp_keysym) &&
  603. tmp_keysym != NoSymbol && tmp_keysym < 0x80 ) {
  604. *keysym = tmp_keysym;
  605. }
  606. } else {
  607. int n = XkbKeyNumGroups(dpy->xkb_info->desc, tmp_ev.keycode);
  608. int i;
  609. for (i = 0; i < n; i++) {
  610. if (XkbGroupForCoreState(event->state) == i)
  611. continue;
  612. tmp_ev.state= XkbBuildCoreState(tmp_ev.state, i);
  613. if (XkbLookupKeySym(dpy, tmp_ev.keycode, tmp_ev.state,
  614. &tmp_new_mods, &tmp_keysym) &&
  615. tmp_keysym != NoSymbol && tmp_keysym < 0x80 ) {
  616. *keysym = tmp_keysym;
  617. new_mods= (event->state&(~tmp_new_mods));
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. #ifdef USE_OWN_COMPOSE
  624. if ( status ) {
  625. static int been_here= 0;
  626. if ( !been_here ) {
  627. XimCompInitTables();
  628. been_here = 1;
  629. }
  630. if ( !XimCompLegalStatus(status) ) {
  631. status->compose_ptr = NULL;
  632. status->chars_matched = 0;
  633. }
  634. if ( ((status->chars_matched>0)&&(status->compose_ptr!=NULL)) ||
  635. XimCompIsComposeKey(*keysym,event->keycode,status) ) {
  636. XimCompRtrn rtrn;
  637. switch (XimCompProcessSym(status,*keysym,&rtrn)) {
  638. case XIM_COMP_IGNORE:
  639. break;
  640. case XIM_COMP_IN_PROGRESS:
  641. if ( keysym!=NULL )
  642. *keysym = NoSymbol;
  643. #ifndef NO_COMPOSE_LED
  644. if ( dpy->xkb_info->xlib_ctrls&XkbLC_ComposeLED ) {
  645. XkbSetNamedIndicator(dpy,dpy->xkb_info->composeLED,
  646. True,True,False,NULL);
  647. }
  648. #endif
  649. return 0;
  650. case XIM_COMP_FAIL:
  651. {
  652. static Atom _ComposeFail= None;
  653. int n = 0, len= 0;
  654. #ifndef NO_COMPOSE_LED
  655. if ( dpy->xkb_info->xlib_ctrls&XkbLC_ComposeLED ) {
  656. XkbSetNamedIndicator(dpy,dpy->xkb_info->composeLED,
  657. True,False,False,NULL);
  658. }
  659. #endif
  660. #ifndef NO_BELL_ON_COMPOSE_FAIL
  661. if (dpy->xkb_info->xlib_ctrls&XkbLC_BeepOnComposeFail) {
  662. if (_ComposeFail==None)
  663. _ComposeFail= XInternAtom(dpy,"ComposeFail",0);
  664. XkbBell(dpy,event->window,0,_ComposeFail);
  665. }
  666. #endif
  667. for (n=len=0;rtrn.sym[n]!=XK_VoidSymbol;n++) {
  668. if ( nbytes-len > 0 ) {
  669. len+= XkbTranslateKeySym(dpy,&rtrn.sym[n],new_mods,
  670. buffer+len,nbytes-len,
  671. NULL);
  672. }
  673. }
  674. if ( keysym!=NULL ) {
  675. if ( n==1 ) *keysym = rtrn.sym[0];
  676. else *keysym = NoSymbol;
  677. }
  678. return len;
  679. }
  680. case XIM_COMP_SUCCEED:
  681. {
  682. int len,n = 0;
  683. #ifndef NO_COMPOSE_LED
  684. if ( dpy->xkb_info->xlib_ctrls&XkbLC_ComposeLED ) {
  685. XkbSetNamedIndicator(dpy,dpy->xkb_info->composeLED,
  686. True,False,False,NULL);
  687. }
  688. #endif
  689. *keysym = rtrn.matchSym;
  690. if ( rtrn.str[0]!='\0' ) {
  691. strncpy(buffer,rtrn.str,nbytes-1);
  692. buffer[nbytes-1]= '\0';
  693. len = (int)strlen(buffer);
  694. }
  695. else {
  696. len = XkbTranslateKeySym(dpy,keysym,new_mods,
  697. buffer,nbytes,
  698. NULL);
  699. }
  700. for (n=0;rtrn.sym[n]!=XK_VoidSymbol;n++) {
  701. if ( nbytes-len > 0 ) {
  702. len+= XkbTranslateKeySym(dpy,&rtrn.sym[n],
  703. event->state,
  704. buffer+len,nbytes-len,
  705. NULL);
  706. }
  707. }
  708. return len;
  709. }
  710. }
  711. }
  712. }
  713. #endif
  714. /* We *should* use the new_mods (which does not contain any modifiers */
  715. /* that were used to compute the symbol here, but pre-XKB XLookupString */
  716. /* did not and we have to remain compatible. Sigh. */
  717. if (_XkbUnavailable(dpy) ||
  718. (dpy->xkb_info->xlib_ctrls&XkbLC_ConsumeLookupMods)==0)
  719. new_mods= event->state;
  720. rtrnLen= XkbLookupKeyBinding(dpy,*keysym,new_mods,buffer,nbytes,NULL);
  721. if (rtrnLen>0)
  722. return rtrnLen;
  723. return XkbTranslateKeySym(dpy,keysym,new_mods,buffer,nbytes,NULL);
  724. }
  725. int
  726. XkbLookupKeyBinding( Display * dpy,
  727. register KeySym sym,
  728. unsigned int mods,
  729. char * buffer,
  730. int nbytes,
  731. int * extra_rtrn)
  732. {
  733. register struct _XKeytrans *p;
  734. if (extra_rtrn)
  735. *extra_rtrn= 0;
  736. for (p = dpy->key_bindings; p; p = p->next) {
  737. if (((mods & AllMods) == p->state) && (sym == p->key)) {
  738. int tmp = p->len;
  739. if (tmp > nbytes) {
  740. if (extra_rtrn)
  741. *extra_rtrn= (tmp-nbytes);
  742. tmp = nbytes;
  743. }
  744. memcpy (buffer, p->string, tmp);
  745. if (tmp < nbytes) buffer[tmp]= '\0';
  746. return tmp;
  747. }
  748. }
  749. return 0;
  750. }
  751. char
  752. XkbToControl( char ch )
  753. {
  754. register char c = ch;
  755. if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
  756. else if (c == '2') c = '\000';
  757. else if (c >= '3' && c <= '7') c -= ('3' - '\033');
  758. else if (c == '8') c = '\177';
  759. else if (c == '/') c = '_' & 0x1F;
  760. return c;
  761. }