Debian Codegraph @

pTk/mTk/unix/tkUnixKey.c

raw symbols SCIP
1 /*
2 * tkUnixKey.c --
3 *
4 * This file contains routines for dealing with international keyboard
5 * input.
6 *
7 * Copyright (c) 1997 by Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkUnixKey.c,v 1.10 2002/06/17 20:09:01 hobbs Exp $
13 */
14
15 #include "tkInt.h"
16
17 /*
18 * Prototypes for local procedures defined in this file:
19 */
20
21
22 /*
23 *----------------------------------------------------------------------
24 *
25 * Tk_SetCaretPos --
26 *
27 * This enables correct placement of the XIM caret. This is called
28 * by widgets to indicate their cursor placement, and the caret
29 * location is used by TkpGetString to place the XIM caret.
30 * This is currently only used for over-the-spot XIM.
31 *
32 * Results:
33 * None
34 *
35 * Side effects:
36 * None
37 *
38 *----------------------------------------------------------------------
39 */
40
41 void
42 Tk_SetCaretPos(tkwin, x, y, height)
43 Tk_Window tkwin;
44 int x;
45 int y;
46 int height;
47 {
48 TkCaret *caretPtr = &(((TkWindow *) tkwin)->dispPtr->caret);
49
50 /*
51 * Use height for best placement of the XIM over-the-spot box.
52 */
53
54 caretPtr->winPtr = ((TkWindow *) tkwin);
55 caretPtr->x = x;
56 caretPtr->y = y;
57 caretPtr->height = height;
58 }
59
60 /*
61 *----------------------------------------------------------------------
62 *
63 * TkpGetString --
64 *
65 * Retrieve the UTF string associated with a keyboard event.
66 *
67 * Results:
68 * Returns the UTF string.
69 *
70 * Side effects:
71 * Stores the input string in the specified Tcl_DString. Modifies
72 * the internal input state. This routine can only be called
73 * once for a given event.
74 *
75 *----------------------------------------------------------------------
76 */
77
78 char *
79 TkpGetString(winPtr, eventPtr, dsPtr)
80 TkWindow *winPtr; /* Window where event occurred: needed to
81 * get input context. */
82 XEvent *eventPtr; /* X keyboard event. */
83 Tcl_DString *dsPtr; /* Uninitialized or empty string to hold
84 * result. */
85 {
86 int len;
87 Tcl_DString buf;
88 Status status;
89 #ifdef TK_USE_INPUT_METHODS
90 TkDisplay *dispPtr = winPtr->dispPtr;
91 #endif
92
93 /*
94 * Overallocate the dstring to the maximum stack amount.
95 */
96
97 #ifdef TK_USE_INPUT_METHODS
98 if ((dispPtr->flags & TK_DISPLAY_USE_IM)
99 && (winPtr->inputContext != NULL)
100 && (eventPtr->type == KeyPress)) {
101 #if TK_XIM_SPOT
102 XVaNestedList preedit_attr;
103 XPoint spot;
104 #endif
105
106 #ifdef X_HAVE_UTF8_STRING
107 Tcl_DStringInit(dsPtr);
108 Tcl_DStringSetLength(dsPtr, TCL_DSTRING_STATIC_SIZE-1);
109 len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey,
110 Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr),
111 (KeySym *) NULL, &status);
112 #else
113 Tcl_DStringInit(&buf);
114 Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
115 len = XmbLookupString(winPtr->inputContext, &eventPtr->xkey,
116 Tcl_DStringValue(&buf), Tcl_DStringLength(&buf),
117 (KeySym *) NULL, &status);
118 #endif
119 /*
120 * If the buffer wasn't big enough, grow the buffer and try again.
121 */
122
123 if (status == XBufferOverflow) {
124 #ifdef X_HAVE_UTF8_STRING
125 Tcl_DStringSetLength(dsPtr, len);
126 len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey,
127 Tcl_DStringValue(dsPtr), len, (KeySym *) NULL, &status);
128 #else
129 Tcl_DStringSetLength(&buf, len);
130 len = XmbLookupString(winPtr->inputContext, &eventPtr->xkey,
131 Tcl_DStringValue(&buf), len, (KeySym *) NULL, &status);
132 #endif
133 }
134 if ((status != XLookupChars) && (status != XLookupBoth)) {
135 len = 0;
136 }
137
138 #if TK_XIM_SPOT
139 /*
140 * Adjust the XIM caret position. We might want to check that
141 * this is the right caret.winPtr as well.
142 */
143 if (dispPtr->flags & TK_DISPLAY_XIM_SPOT) {
144 spot.x = dispPtr->caret.x;
145 spot.y = dispPtr->caret.y + dispPtr->caret.height;
146 preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
147 XSetICValues(winPtr->inputContext,
148 XNPreeditAttributes, preedit_attr, NULL);
149 XFree(preedit_attr);
150 }
151 #endif
152 #ifdef X_HAVE_UTF8_STRING
153 Tcl_DStringSetLength(dsPtr, len);
154 return Tcl_DStringValue(dsPtr);
155 #endif
156 } else {
157 Tcl_DStringInit(&buf);
158 Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
159 len = XLookupString(&eventPtr->xkey, Tcl_DStringValue(&buf),
160 Tcl_DStringLength(&buf), (KeySym *) NULL,
161 (XComposeStatus *) NULL);
162 }
163 #else /* TK_USE_INPUT_METHODS */
164 Tcl_DStringInit(&buf);
165 Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
166 len = XLookupString(&eventPtr->xkey, Tcl_DStringValue(&buf),
167 Tcl_DStringLength(&buf), (KeySym *) NULL,
168 (XComposeStatus *) NULL);
169 #endif /* TK_USE_INPUT_METHODS */
170 Tcl_DStringSetLength(&buf, len);
171
172 Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr);
173 Tcl_DStringFree(&buf);
174
175 return Tcl_DStringValue(dsPtr);
176 }
177
178 /*
179 * When mapping from a keysym to a keycode, need
180 * information about the modifier state that should be used
181 * so that when they call XKeycodeToKeysym taking into
182 * account the xkey.state, they will get back the original
183 * keysym.
184 */
185
186 void
187 TkpSetKeycodeAndState(tkwin, keySym, eventPtr)
188 Tk_Window tkwin;
189 KeySym keySym;
190 XEvent *eventPtr;
191 {
192 Display *display;
193 int state;
194 KeyCode keycode;
195
196 display = Tk_Display(tkwin);
197
198 if (keySym == NoSymbol) {
199 keycode = 0;
200 } else {
201 keycode = XKeysymToKeycode(display, keySym);
202 }
203 if (keycode != 0) {
204 for (state = 0; state < 4; state++) {
205 if (XKeycodeToKeysym(display, keycode, state) == keySym) {
206 if (state & 1) {
207 eventPtr->xkey.state |= ShiftMask;
208 }
209 if (state & 2) {
210 TkDisplay *dispPtr;
211
212 dispPtr = ((TkWindow *) tkwin)->dispPtr;
213 eventPtr->xkey.state |= dispPtr->modeModMask;
214 }
215 break;
216 }
217 }
218 }
219 eventPtr->xkey.keycode = keycode;
220 }
221
222 /*
223 *----------------------------------------------------------------------
224 *
225 * TkpGetKeySym --
226 *
227 * Given an X KeyPress or KeyRelease event, map the
228 * keycode in the event into a KeySym.
229 *
230 * Results:
231 * The return value is the KeySym corresponding to
232 * eventPtr, or NoSymbol if no matching Keysym could be
233 * found.
234 *
235 * Side effects:
236 * In the first call for a given display, keycode-to-
237 * KeySym maps get loaded.
238 *
239 *----------------------------------------------------------------------
240 */
241
242 KeySym
243 TkpGetKeySym(dispPtr, eventPtr)
244 TkDisplay *dispPtr; /* Display in which to
245 * map keycode. */
246 XEvent *eventPtr; /* Description of X event. */
247 {
248 KeySym sym;
249 int index;
250
251 /*
252 * Refresh the mapping information if it's stale
253 */
254
255 if (dispPtr->bindInfoStale) {
256 TkpInitKeymapInfo(dispPtr);
257 }
258
259 /*
260 * Figure out which of the four slots in the keymap vector to
261 * use for this key. Refer to Xlib documentation for more info
262 * on how this computation works.
263 */
264
265 index = 0;
266 if (eventPtr->xkey.state & dispPtr->modeModMask) {
267 index = 2;
268 }
269 if ((eventPtr->xkey.state & ShiftMask)
270 || ((dispPtr->lockUsage != LU_IGNORE)
271 && (eventPtr->xkey.state & LockMask))) {
272 index += 1;
273 }
274 sym = XKeycodeToKeysym(dispPtr->display, eventPtr->xkey.keycode, index);
275
276 /*
277 * Special handling: if the key was shifted because of Lock, but
278 * lock is only caps lock, not shift lock, and the shifted keysym
279 * isn't upper-case alphabetic, then switch back to the unshifted
280 * keysym.
281 */
282
283 if ((index & 1) && !(eventPtr->xkey.state & ShiftMask)
284 && (dispPtr->lockUsage == LU_CAPS)) {
285 if (!(((sym >= XK_A) && (sym <= XK_Z))
286 || ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
287 || ((sym >= XK_Ooblique) && (sym <= XK_Thorn)))) {
288 index &= ~1;
289 sym = XKeycodeToKeysym(dispPtr->display, eventPtr->xkey.keycode,
290 index);
291 }
292 }
293
294 /*
295 * Another bit of special handling: if this is a shifted key and there
296 * is no keysym defined, then use the keysym for the unshifted key.
297 */
298
299 if ((index & 1) && (sym == NoSymbol)) {
300 sym = XKeycodeToKeysym(dispPtr->display, eventPtr->xkey.keycode,
301 index & ~1);
302 }
303 return sym;
304 }
305
306 /*
307 *--------------------------------------------------------------
308 *
309 * TkpInitKeymapInfo --
310 *
311 * This procedure is invoked to scan keymap information
312 * to recompute stuff that's important for binding, such
313 * as the modifier key (if any) that corresponds to "mode
314 * switch".
315 *
316 * Results:
317 * None.
318 *
319 * Side effects:
320 * Keymap-related information in dispPtr is updated.
321 *
322 *--------------------------------------------------------------
323 */
324
325 void
326 TkpInitKeymapInfo(dispPtr)
327 TkDisplay *dispPtr; /* Display for which to recompute keymap
328 * information. */
329 {
330 XModifierKeymap *modMapPtr;
331 KeyCode *codePtr;
332 KeySym keysym;
333 int count, i, j, max, arraySize;
334 #define KEYCODE_ARRAY_SIZE 20
335
336 dispPtr->bindInfoStale = 0;
337 modMapPtr = XGetModifierMapping(dispPtr->display);
338
339 /*
340 * Check the keycodes associated with the Lock modifier. If
341 * any of them is associated with the XK_Shift_Lock modifier,
342 * then Lock has to be interpreted as Shift Lock, not Caps Lock.
343 */
344
345 dispPtr->lockUsage = LU_IGNORE;
346 codePtr = modMapPtr->modifiermap + modMapPtr->max_keypermod*LockMapIndex;
347 for (count = modMapPtr->max_keypermod; count > 0; count--, codePtr++) {
348 if (*codePtr == 0) {
349 continue;
350 }
351 keysym = XKeycodeToKeysym(dispPtr->display, *codePtr, 0);
352 if (keysym == XK_Shift_Lock) {
353 dispPtr->lockUsage = LU_SHIFT;
354 break;
355 }
356 if (keysym == XK_Caps_Lock) {
357 dispPtr->lockUsage = LU_CAPS;
358 break;
359 }
360 }
361
362 /*
363 * Look through the keycodes associated with modifiers to see if
364 * the the "mode switch", "meta", or "alt" keysyms are associated
365 * with any modifiers. If so, remember their modifier mask bits.
366 */
367
368 dispPtr->modeModMask = 0;
369 dispPtr->metaModMask = 0;
370 dispPtr->altModMask = 0;
371 codePtr = modMapPtr->modifiermap;
372 max = 8*modMapPtr->max_keypermod;
373 for (i = 0; i < max; i++, codePtr++) {
374 if (*codePtr == 0) {
375 continue;
376 }
377 keysym = XKeycodeToKeysym(dispPtr->display, *codePtr, 0);
378 if (keysym == XK_Mode_switch) {
379 dispPtr->modeModMask |= ShiftMask << (i/modMapPtr->max_keypermod);
380 }
381 if ((keysym == XK_Meta_L) || (keysym == XK_Meta_R)) {
382 dispPtr->metaModMask |= ShiftMask << (i/modMapPtr->max_keypermod);
383 }
384 if ((keysym == XK_Alt_L) || (keysym == XK_Alt_R)) {
385 dispPtr->altModMask |= ShiftMask << (i/modMapPtr->max_keypermod);
386 }
387 }
388
389 /*
390 * Create an array of the keycodes for all modifier keys.
391 */
392
393 if (dispPtr->modKeyCodes != NULL) {
394 ckfree((char *) dispPtr->modKeyCodes);
395 }
396 dispPtr->numModKeyCodes = 0;
397 arraySize = KEYCODE_ARRAY_SIZE;
398 dispPtr->modKeyCodes = (KeyCode *) ckalloc((unsigned)
399 (KEYCODE_ARRAY_SIZE * sizeof(KeyCode)));
400 for (i = 0, codePtr = modMapPtr->modifiermap; i < max; i++, codePtr++) {
401 if (*codePtr == 0) {
402 continue;
403 }
404
405 /*
406 * Make sure that the keycode isn't already in the array.
407 */
408
409 for (j = 0; j < dispPtr->numModKeyCodes; j++) {
410 if (dispPtr->modKeyCodes[j] == *codePtr) {
411 goto nextModCode;
412 }
413 }
414 if (dispPtr->numModKeyCodes >= arraySize) {
415 KeyCode *new;
416
417 /*
418 * Ran out of space in the array; grow it.
419 */
420
421 arraySize *= 2;
422 new = (KeyCode *) ckalloc((unsigned)
423 (arraySize * sizeof(KeyCode)));
424 memcpy((VOID *) new, (VOID *) dispPtr->modKeyCodes,
425 (dispPtr->numModKeyCodes * sizeof(KeyCode)));
426 ckfree((char *) dispPtr->modKeyCodes);
427 dispPtr->modKeyCodes = new;
428 }
429 dispPtr->modKeyCodes[dispPtr->numModKeyCodes] = *codePtr;
430 dispPtr->numModKeyCodes++;
431 nextModCode: continue;
432 }
433 XFreeModifiermap(modMapPtr);
434 }