1*4882a593Smuzhiyun /************************************************************
2*4882a593Smuzhiyun Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this
5*4882a593Smuzhiyun software and its documentation for any purpose and without
6*4882a593Smuzhiyun fee is hereby granted, provided that the above copyright
7*4882a593Smuzhiyun notice appear in all copies and that both that copyright
8*4882a593Smuzhiyun notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation, and that the name of Silicon Graphics not be
10*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution
11*4882a593Smuzhiyun of the software without specific prior written permission.
12*4882a593Smuzhiyun Silicon Graphics makes no representation about the suitability
13*4882a593Smuzhiyun of this software for any purpose. It is provided "as is"
14*4882a593Smuzhiyun without any express or implied warranty.
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17*4882a593Smuzhiyun SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18*4882a593Smuzhiyun AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19*4882a593Smuzhiyun GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20*4882a593Smuzhiyun DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21*4882a593Smuzhiyun DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22*4882a593Smuzhiyun OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23*4882a593Smuzhiyun THE USE OR PERFORMANCE OF THIS SOFTWARE.
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun ********************************************************/
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
28*4882a593Smuzhiyun #include <dix-config.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <stdio.h>
32*4882a593Smuzhiyun #include <math.h>
33*4882a593Smuzhiyun #include <X11/X.h>
34*4882a593Smuzhiyun #include <X11/Xproto.h>
35*4882a593Smuzhiyun #include <X11/keysym.h>
36*4882a593Smuzhiyun #include "misc.h"
37*4882a593Smuzhiyun #include "inputstr.h"
38*4882a593Smuzhiyun #include "exevents.h"
39*4882a593Smuzhiyun #include "eventstr.h"
40*4882a593Smuzhiyun #include <xkbsrv.h>
41*4882a593Smuzhiyun #include <ctype.h>
42*4882a593Smuzhiyun #include "events.h"
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /***====================================================================***/
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun void
XkbProcessKeyboardEvent(DeviceEvent * event,DeviceIntPtr keybd)47*4882a593Smuzhiyun XkbProcessKeyboardEvent(DeviceEvent *event, DeviceIntPtr keybd)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun KeyClassPtr keyc = keybd->key;
50*4882a593Smuzhiyun XkbSrvInfoPtr xkbi;
51*4882a593Smuzhiyun int key;
52*4882a593Smuzhiyun XkbBehavior behavior;
53*4882a593Smuzhiyun unsigned ndx;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun xkbi = keyc->xkbInfo;
56*4882a593Smuzhiyun key = event->detail.key;
57*4882a593Smuzhiyun if (xkbDebugFlags & 0x8)
58*4882a593Smuzhiyun DebugF("[xkb] XkbPKE: Key %d %s\n", key,
59*4882a593Smuzhiyun (event->type == ET_KeyPress ? "down" : "up"));
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun if (xkbi->repeatKey == key && event->type == ET_KeyRelease &&
62*4882a593Smuzhiyun !(xkbi->desc->ctrls->enabled_ctrls & XkbRepeatKeysMask))
63*4882a593Smuzhiyun AccessXCancelRepeatKey(xkbi, key);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun behavior = xkbi->desc->server->behaviors[key];
66*4882a593Smuzhiyun /* The "permanent" flag indicates a hard-wired behavior that occurs */
67*4882a593Smuzhiyun /* below XKB, such as a key that physically locks. XKB does not */
68*4882a593Smuzhiyun /* do anything to implement the behavior, but it *does* report that */
69*4882a593Smuzhiyun /* key is hardwired */
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (!(behavior.type & XkbKB_Permanent)) {
72*4882a593Smuzhiyun switch (behavior.type) {
73*4882a593Smuzhiyun case XkbKB_Default:
74*4882a593Smuzhiyun /* Neither of these should happen in practice, but ignore them
75*4882a593Smuzhiyun anyway. */
76*4882a593Smuzhiyun if (event->type == ET_KeyPress && !event->key_repeat &&
77*4882a593Smuzhiyun key_is_down(keybd, key, KEY_PROCESSED))
78*4882a593Smuzhiyun return;
79*4882a593Smuzhiyun else if (event->type == ET_KeyRelease &&
80*4882a593Smuzhiyun !key_is_down(keybd, key, KEY_PROCESSED))
81*4882a593Smuzhiyun return;
82*4882a593Smuzhiyun break;
83*4882a593Smuzhiyun case XkbKB_Lock:
84*4882a593Smuzhiyun if (event->type == ET_KeyRelease)
85*4882a593Smuzhiyun return;
86*4882a593Smuzhiyun else if (key_is_down(keybd, key, KEY_PROCESSED))
87*4882a593Smuzhiyun event->type = ET_KeyRelease;
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun case XkbKB_RadioGroup:
90*4882a593Smuzhiyun ndx = (behavior.data & (~XkbKB_RGAllowNone));
91*4882a593Smuzhiyun if (ndx < xkbi->nRadioGroups) {
92*4882a593Smuzhiyun XkbRadioGroupPtr rg;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (event->type == ET_KeyRelease)
95*4882a593Smuzhiyun return;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun rg = &xkbi->radioGroups[ndx];
98*4882a593Smuzhiyun if (rg->currentDown == event->detail.key) {
99*4882a593Smuzhiyun if (behavior.data & XkbKB_RGAllowNone) {
100*4882a593Smuzhiyun event->type = ET_KeyRelease;
101*4882a593Smuzhiyun XkbHandleActions(keybd, keybd, event);
102*4882a593Smuzhiyun rg->currentDown = 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun return;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun if (rg->currentDown != 0) {
107*4882a593Smuzhiyun int tmpkey = event->detail.key;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun event->type = ET_KeyRelease;
110*4882a593Smuzhiyun event->detail.key = rg->currentDown;
111*4882a593Smuzhiyun XkbHandleActions(keybd, keybd, event);
112*4882a593Smuzhiyun event->type = ET_KeyPress;
113*4882a593Smuzhiyun event->detail.key = tmpkey;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun rg->currentDown = key;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun else
118*4882a593Smuzhiyun ErrorF("[xkb] InternalError! Illegal radio group %d\n", ndx);
119*4882a593Smuzhiyun break;
120*4882a593Smuzhiyun case XkbKB_Overlay1:
121*4882a593Smuzhiyun case XkbKB_Overlay2:
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun unsigned which;
124*4882a593Smuzhiyun unsigned overlay_active_now;
125*4882a593Smuzhiyun unsigned is_keyrelease = (event->type == ET_KeyRelease) ? 1 : 0;
126*4882a593Smuzhiyun /* Remembers whether the key was pressed while overlay was down,
127*4882a593Smuzhiyun * for when overlay is already released, but the key is not. */
128*4882a593Smuzhiyun unsigned key_was_overlaid = 0;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (behavior.type == XkbKB_Overlay1)
131*4882a593Smuzhiyun which = XkbOverlay1Mask;
132*4882a593Smuzhiyun else
133*4882a593Smuzhiyun which = XkbOverlay2Mask;
134*4882a593Smuzhiyun overlay_active_now = (xkbi->desc->ctrls->enabled_ctrls & which) ? 1 : 0;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if ((unsigned char)key == key) {
137*4882a593Smuzhiyun key_was_overlaid = BitIsOn(xkbi->overlay_perkey_state, key);
138*4882a593Smuzhiyun if (!is_keyrelease) {
139*4882a593Smuzhiyun if (overlay_active_now)
140*4882a593Smuzhiyun SetBit(xkbi->overlay_perkey_state, key);
141*4882a593Smuzhiyun } else {
142*4882a593Smuzhiyun if (key_was_overlaid)
143*4882a593Smuzhiyun ClearBit(xkbi->overlay_perkey_state, key);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if ((overlay_active_now || key_was_overlaid) &&
148*4882a593Smuzhiyun (behavior.data >= xkbi->desc->min_key_code) &&
149*4882a593Smuzhiyun (behavior.data <= xkbi->desc->max_key_code)) {
150*4882a593Smuzhiyun event->detail.key = behavior.data;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun break;
154*4882a593Smuzhiyun default:
155*4882a593Smuzhiyun ErrorF("[xkb] unknown key behavior 0x%04x\n", behavior.type);
156*4882a593Smuzhiyun break;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun XkbHandleActions(keybd, keybd, event);
160*4882a593Smuzhiyun return;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun void
ProcessKeyboardEvent(InternalEvent * ev,DeviceIntPtr keybd)164*4882a593Smuzhiyun ProcessKeyboardEvent(InternalEvent *ev, DeviceIntPtr keybd)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun KeyClassPtr keyc = keybd->key;
168*4882a593Smuzhiyun XkbSrvInfoPtr xkbi = NULL;
169*4882a593Smuzhiyun ProcessInputProc backup_proc;
170*4882a593Smuzhiyun xkbDeviceInfoPtr xkb_priv = XKBDEVICEINFO(keybd);
171*4882a593Smuzhiyun DeviceEvent *event = &ev->device_event;
172*4882a593Smuzhiyun int is_press = (event->type == ET_KeyPress);
173*4882a593Smuzhiyun int is_release = (event->type == ET_KeyRelease);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* We're only interested in key events. */
176*4882a593Smuzhiyun if (!is_press && !is_release) {
177*4882a593Smuzhiyun UNWRAP_PROCESS_INPUT_PROC(keybd, xkb_priv, backup_proc);
178*4882a593Smuzhiyun keybd->public.processInputProc(ev, keybd);
179*4882a593Smuzhiyun COND_WRAP_PROCESS_INPUT_PROC(keybd, xkb_priv, backup_proc,
180*4882a593Smuzhiyun xkbUnwrapProc);
181*4882a593Smuzhiyun return;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun xkbi = keyc->xkbInfo;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* If AccessX filters are active, then pass it through to
187*4882a593Smuzhiyun * AccessXFilter{Press,Release}Event; else, punt to
188*4882a593Smuzhiyun * XkbProcessKeyboardEvent.
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * If AXF[PK]E don't intercept anything (which they probably won't),
191*4882a593Smuzhiyun * they'll punt through XPKE anyway. */
192*4882a593Smuzhiyun if ((xkbi->desc->ctrls->enabled_ctrls & XkbAllFilteredEventsMask)) {
193*4882a593Smuzhiyun if (is_press)
194*4882a593Smuzhiyun AccessXFilterPressEvent(event, keybd);
195*4882a593Smuzhiyun else if (is_release)
196*4882a593Smuzhiyun AccessXFilterReleaseEvent(event, keybd);
197*4882a593Smuzhiyun return;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun else {
200*4882a593Smuzhiyun XkbProcessKeyboardEvent(event, keybd);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun return;
204*4882a593Smuzhiyun }
205