Debian Codegraph @

nx-X11/lib/src/xkb/XKBBind.c

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