1 /* Grok X modifier mappings for shortcuts.
3 Most of this code was taken from src/event-Xt.c in XEmacs 20.3-b17.
4 The copyright(s) from the original XEmacs code are included below.
6 Perpetrator: Sudish Joseph <sj@eng.mindspring.net>, Sept. 1997. */
9 * More changes for WPrefs by Alfredo Kojima, Aug 1998
12 /* The event_stream interface for X11 with Xt, and/or tty frames.
13 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
14 Copyright (C) 1995 Sun Microsystems, Inc.
15 Copyright (C) 1996 Ben Wing.
17 This file is part of XEmacs.
19 XEmacs is free software; you can redistribute it and/or modify it
20 under the terms of the GNU General Public License as published by the
21 Free Software Foundation; either version 2, or (at your option) any
24 XEmacs is distributed in the hope that it will be useful, but WITHOUT
25 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received a copy of the GNU General Public License along
30 with XEmacs; see the file COPYING. if not, write to the
31 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 Boston, MA 02110-1301 USA. */
37 #include <X11/keysym.h>
38 #include <X11/XKBlib.h>
40 #include <WINGs/WUtil.h>
44 /************************************************************************/
46 /************************************************************************/
48 /* X bogusly doesn't define the interpretations of any bits besides
49 ModControl, ModShift, and ModLock; so the Interclient Communication
50 Conventions Manual says that we have to bend over backwards to figure
51 out what the other modifier bits mean. According to ICCCM:
53 - Any keycode which is assigned ModControl is a "control" key.
55 - Any modifier bit which is assigned to a keycode which generates Meta_L
56 or Meta_R is the modifier bit meaning "meta". Likewise for Super, Hyper,
59 - Any keypress event which contains ModControl in its state should be
60 interpreted as a "control" character.
62 - Any keypress event which contains a modifier bit in its state which is
63 generated by a keycode whose corresponding keysym is Meta_L or Meta_R
64 should be interpreted as a "meta" character. Likewise for Super, Hyper,
67 - It is illegal for a keysym to be associated with more than one modifier
70 This means that the only thing that emacs can reasonably interpret as a
71 "meta" key is a key whose keysym is Meta_L or Meta_R, and which generates
72 one of the modifier bits Mod1-Mod5.
74 Unfortunately, many keyboards don't have Meta keys in their default
75 configuration. So, if there are no Meta keys, but there are "Alt" keys,
76 emacs will interpret Alt as Meta. If there are both Meta and Alt keys,
77 then the Meta keys mean "Meta", and the Alt keys mean "Alt" (it used to
78 mean "Symbol," but that just confused the hell out of way too many people).
80 This works with the default configurations of the 19 keyboard-types I've
83 Emacs detects keyboard configurations which violate the above rules, and
84 prints an error message on the standard-error-output. (Perhaps it should
85 use a pop-up-window instead.)
88 static int MetaIndex
, HyperIndex
, SuperIndex
, AltIndex
, ModeIndex
;
90 static const char *index_to_name(int indice
)
114 static void x_reset_modifier_mapping(Display
* display
)
116 int modifier_index
, modifier_key
, column
, mkpm
;
122 XModifierKeymap
*x_modifier_keymap
= XGetModifierMapping(display
);
124 #define modwarn(name,old,other) \
125 wwarning ("%s (0x%x) generates %s, which is generated by %s.", \
126 name, code, index_to_name (old), other)
128 #define modbarf(name,other) \
129 wwarning ("%s (0x%x) generates %s, which is nonsensical.", \
132 #define check_modifier(name,mask) \
133 if ((1<<modifier_index) != mask) \
134 wwarning ("%s (0x%x) generates %s, which is nonsensical.", \
135 name, code, index_to_name (modifier_index))
137 #define store_modifier(name,old) \
138 if (old && old != modifier_index) \
139 wwarning ("%s (0x%x) generates both %s and %s, which is nonsensical.", \
140 name, code, index_to_name (old), \
141 index_to_name (modifier_index)); \
142 if (modifier_index == ShiftMapIndex) modbarf (name,"ModShift"); \
143 else if (modifier_index == LockMapIndex) modbarf (name,"ModLock"); \
144 else if (modifier_index == ControlMapIndex) modbarf (name,"ModControl"); \
145 else if (sym == XK_Mode_switch) \
146 mode_bit = modifier_index; /* Mode_switch is special, see below... */ \
147 else if (modifier_index == meta_bit && old != meta_bit) \
148 modwarn (name, meta_bit, "Meta"); \
149 else if (modifier_index == super_bit && old != super_bit) \
150 modwarn (name, super_bit, "Super"); \
151 else if (modifier_index == hyper_bit && old != hyper_bit) \
152 modwarn (name, hyper_bit, "Hyper"); \
153 else if (modifier_index == alt_bit && old != alt_bit) \
154 modwarn (name, alt_bit, "Alt"); \
156 old = modifier_index;
158 mkpm
= x_modifier_keymap
->max_keypermod
;
159 for (modifier_index
= 0; modifier_index
< 8; modifier_index
++)
160 for (modifier_key
= 0; modifier_key
< mkpm
; modifier_key
++) {
162 for (column
= 0; column
< 4; column
+= 2) {
163 KeyCode code
= x_modifier_keymap
->modifiermap
[modifier_index
* mkpm
165 KeySym sym
= (code
? XkbKeycodeToKeysym(display
, code
, 0, column
) : 0);
171 store_modifier("Mode_switch", mode_bit
);
174 store_modifier("Meta_L", meta_bit
);
177 store_modifier("Meta_R", meta_bit
);
180 store_modifier("Super_L", super_bit
);
183 store_modifier("Super_R", super_bit
);
186 store_modifier("Hyper_L", hyper_bit
);
189 store_modifier("Hyper_R", hyper_bit
);
192 store_modifier("Alt_L", alt_bit
);
195 store_modifier("Alt_R", alt_bit
);
198 check_modifier("Control_L", ControlMask
);
201 check_modifier("Control_R", ControlMask
);
204 check_modifier("Shift_L", ShiftMask
);
207 check_modifier("Shift_R", ShiftMask
);
210 check_modifier("Shift_Lock", LockMask
);
213 check_modifier("Caps_Lock", LockMask
);
216 /* It probably doesn't make any sense for a modifier bit to be
217 assigned to a key that is not one of the above, but OpenWindows
218 assigns modifier bits to a couple of random function keys for
219 no reason that I can discern, so printing a warning here would
224 #undef store_modifier
225 #undef check_modifier
229 /* If there was no Meta key, then try using the Alt key instead.
230 If there is both a Meta key and an Alt key, then the Alt key
231 is not disturbed and remains an Alt key. */
232 if (!meta_bit
&& alt_bit
)
233 meta_bit
= alt_bit
, alt_bit
= 0;
235 /* mode_bit overrides everything, since it's processed down inside of
236 XLookupString() instead of by us. If Meta and Mode_switch both
237 generate the same modifier bit (which is an error), then we don't
238 interpret that bit as Meta, because we can't make XLookupString()
239 not interpret it as Mode_switch; and interpreting it as both would
242 const char *warn
= 0;
243 if (mode_bit
== meta_bit
)
244 warn
= "Meta", meta_bit
= 0;
245 else if (mode_bit
== hyper_bit
)
246 warn
= "Hyper", hyper_bit
= 0;
247 else if (mode_bit
== super_bit
)
248 warn
= "Super", super_bit
= 0;
249 else if (mode_bit
== alt_bit
)
250 warn
= "Alt", alt_bit
= 0;
252 wwarning("%s is being used for both Mode_switch and %s.",
253 index_to_name(mode_bit
), warn
);
257 MetaIndex
= meta_bit
;
258 HyperIndex
= hyper_bit
;
259 SuperIndex
= super_bit
;
261 ModeIndex
= mode_bit
;
263 if (x_modifier_keymap
!= NULL
)
264 XFreeModifiermap(x_modifier_keymap
);
267 int ModifierFromKey(Display
* dpy
, const char *key
)
272 x_reset_modifier_mapping(dpy
);
275 if (strcasecmp(key
, "SHIFT") == 0)
276 return ShiftMapIndex
;
277 else if (strcasecmp(key
, "CONTROL") == 0)
278 return ControlMapIndex
;
279 else if (strcasecmp(key
, "ALT") == 0)
281 else if (strcasecmp(key
, "META") == 0)
283 else if (strcasecmp(key
, "SUPER") == 0)
285 else if (strcasecmp(key
, "HYPER") == 0)
287 else if (strcasecmp(key
, "MOD1") == 0)
289 else if (strcasecmp(key
, "MOD2") == 0)
291 else if (strcasecmp(key
, "MOD3") == 0)
293 else if (strcasecmp(key
, "MOD4") == 0)
295 else if (strcasecmp(key
, "MOD5") == 0)