summaryrefslogtreecommitdiff
path: root/hw/xwin/wmutil/keyboard.c
blob: 78a92a6dce558d27ab86cc1bc14cd275d0fe2295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
 *
 *Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 *"Software"), to deal in the Software without restriction, including
 *without limitation the rights to use, copy, modify, merge, publish,
 *distribute, sublicense, and/or sell copies of the Software, and to
 *permit persons to whom the Software is furnished to do so, subject to
 *the following conditions:
 *
 *The above copyright notice and this permission notice shall be
 *included in all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *Except as contained in this notice, the name of the XFree86 Project
 *shall not be used in advertising or otherwise to promote the sale, use
 *or other dealings in this Software without prior written authorization
 *from the XFree86 Project.
 *
 * Authors:	Dakshinamurthy Karra
 *		Suhaib M Siddiqi
 *		Peter Busch
 *		Harold L Hunt II
 */

#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif

#include <X11/Xwindows.h>
#include <X11/Xmd.h>   // to provide a BYTE type, since the Windows one is eclipsed

#include "winvkmap.h"
#include "keyboard.h"

#include "winmsg.h"

static bool g_winKeyState[NUM_KEYCODES];

/*
 * Translate a Windows WM_[SYS]KEY(UP/DOWN) message
 * into an ASCII scan code.
 *
 * We do this ourselves, rather than letting Windows handle it,
 * because Windows tends to munge the handling of special keys,
 * like AltGr on European keyboards.
 */

int
winTranslateKey(WPARAM wParam, LPARAM lParam)
{
    int iKeyFixup = g_iKeyMap[wParam * WIN_KEYMAP_COLS + 1];
    int iKeyFixupEx = g_iKeyMap[wParam * WIN_KEYMAP_COLS + 2];
    int iParam = HIWORD(lParam);
    int iParamScanCode = LOBYTE(iParam);
    int iScanCode;

    winDebug("winTranslateKey: wParam %08x lParam %08x\n", (int)wParam, (int)lParam);

/* WM_ key messages faked by Vista speech recognition (WSR) don't have a
 * scan code.
 *
 * Vocola 3 (Rick Mohr's supplement to WSR) uses
 * System.Windows.Forms.SendKeys.SendWait(), which appears always to give a
 * scan code of 1
 */
    if (iParamScanCode <= 1) {
        if (VK_PRIOR <= wParam && wParam <= VK_DOWN)
            /* Trigger special case table to translate to extended
             * keycode, otherwise if num_lock is on, we can get keypad
             * numbers instead of navigation keys. */
            iParam |= KF_EXTENDED;
        else
            iParamScanCode = MapVirtualKeyEx(wParam,
                                             /*MAPVK_VK_TO_VSC */ 0,
                                             GetKeyboardLayout(0));
    }

    /* Branch on special extended, special non-extended, or normal key */
    if ((iParam & KF_EXTENDED) && iKeyFixupEx)
        iScanCode = iKeyFixupEx;
    else if (iKeyFixup)
        iScanCode = iKeyFixup;
    else if (wParam == 0 && iParamScanCode == 0x70)
        iScanCode = KEY_HKTG;
    else
        switch (iParamScanCode) {
        case 0x70:
            iScanCode = KEY_HKTG;
            break;
        case 0x73:
            iScanCode = KEY_BSlash2;
            break;
        default:
            iScanCode = iParamScanCode;
            break;
        }

    return iScanCode;
}

/*
 * Look for the lovely fake Control_L press/release generated by Windows
 * when AltGr is pressed/released on a non-U.S. keyboard.
 */

bool
winIsFakeCtrl_L(UINT message, WPARAM wParam, LPARAM lParam)
{
    MSG msgNext;
    LONG lTime;
    bool fReturn;

    static bool lastWasControlL = FALSE;
    static LONG lastTime;

    /*
     * Fake Ctrl_L presses will be followed by an Alt_R press
     * with the same timestamp as the Ctrl_L press.
     */
    if ((message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
        && wParam == VK_CONTROL && (HIWORD(lParam) & KF_EXTENDED) == 0) {
        /* Got a Ctrl_L press */

        /* Get time of current message */
        lTime = GetMessageTime();

        /* Look for next press message */
        fReturn = PeekMessage(&msgNext, NULL,
                              WM_KEYDOWN, WM_SYSKEYDOWN, PM_NOREMOVE);

        if (fReturn && msgNext.message != WM_KEYDOWN &&
            msgNext.message != WM_SYSKEYDOWN)
            fReturn = 0;

        if (!fReturn) {
            lastWasControlL = TRUE;
            lastTime = lTime;
        }
        else {
            lastWasControlL = FALSE;
        }

        /* Is next press an Alt_R with the same timestamp? */
        if (fReturn && msgNext.wParam == VK_MENU
            && msgNext.time == lTime
            && (HIWORD(msgNext.lParam) & KF_EXTENDED)) {
            /*
             * Next key press is Alt_R with same timestamp as current
             * Ctrl_L message.  Therefore, this Ctrl_L press is a fake
             * event, so discard it.
             */
            return TRUE;
        }
    }
    /*
     * Sometimes, the Alt_R press message is not yet posted when the
     * fake Ctrl_L press message arrives (even though it has the
     * same timestamp), so check for an Alt_R press message that has
     * arrived since the last Ctrl_L message.
     */
    else if ((message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
             && wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
        /* Got a Alt_R press */

        if (lastWasControlL) {
            lTime = GetMessageTime();

            if (lastTime == lTime) {
                /* Undo the fake Ctrl_L press by sending a fake Ctrl_L release */
                winSendKeyEvent(KEY_LCtrl, FALSE);
            }
            lastWasControlL = FALSE;
        }
    }
    /*
     * Fake Ctrl_L releases will be followed by an Alt_R release
     * with the same timestamp as the Ctrl_L release.
     */
    else if ((message == WM_KEYUP || message == WM_SYSKEYUP)
             && wParam == VK_CONTROL && (HIWORD(lParam) & KF_EXTENDED) == 0) {
        /* Got a Ctrl_L release */

        /* Get time of current message */
        lTime = GetMessageTime();

        /* Look for next release message */
        fReturn = PeekMessage(&msgNext, NULL,
                              WM_KEYUP, WM_SYSKEYUP, PM_NOREMOVE);

        if (fReturn && msgNext.message != WM_KEYUP &&
            msgNext.message != WM_SYSKEYUP)
            fReturn = 0;

        lastWasControlL = FALSE;

        /* Is next press an Alt_R with the same timestamp? */
        if (fReturn
            && (msgNext.message == WM_KEYUP || msgNext.message == WM_SYSKEYUP)
            && msgNext.wParam == VK_MENU
            && msgNext.time == lTime
            && (HIWORD(msgNext.lParam) & KF_EXTENDED)) {
            /*
             * Next key release is Alt_R with same timestamp as current
             * Ctrl_L message. Therefore, this Ctrl_L release is a fake
             * event, so discard it.
             */
            return TRUE;
        }
    }
    else {
        /* On any other press or release message, we don't have a
           potentially fake Ctrl_L to worry about anymore... */
        lastWasControlL = FALSE;
    }

    /* Not a fake control left press/release */
    return FALSE;
}

/*
 * Lift any modifier keys that are pressed
 */

void
winKeybdReleaseKeys(void)
{
    int i;

    /* Loop through all keys */
    for (i = 0; i < NUM_KEYCODES; ++i) {
        /* Pop key if pressed */
        if (g_winKeyState[i])
            winSendKeyEvent(i, FALSE);

        /* Reset pressed flag for keys */
        g_winKeyState[i] = FALSE;
    }
}

/*
 * Take a raw X key code and send an up or down event for it.
 *
 * Thanks to VNC for inspiration, though it is a simple function.
 */

void
winSendKeyEvent(DWORD dwKey, bool fDown)
{
    /*
     * When alt-tabing between screens we can get phantom key up messages
     * Here we only pass them through it we think we should!
     */
    if (g_winKeyState[dwKey] == FALSE && fDown == FALSE)
        return;

    /* Update the keyState map */
    g_winKeyState[dwKey] = fDown;

    winSendKeyEventCallback(dwKey + MIN_KEYCODE, fDown);

    winDebug("winSendKeyEvent: dwKey: %u, fDown: %u\n", (unsigned int)dwKey, fDown);
}

bool
winCheckKeyPressed(WPARAM wParam, LPARAM lParam)
{
    switch (wParam) {
    case VK_CONTROL:
        if ((lParam & 0x1ff0000) == 0x11d0000 && g_winKeyState[KEY_RCtrl])
            return TRUE;
        if ((lParam & 0x1ff0000) == 0x01d0000 && g_winKeyState[KEY_LCtrl])
            return TRUE;
        break;
    case VK_SHIFT:
        if ((lParam & 0x1ff0000) == 0x0360000 && g_winKeyState[KEY_ShiftR])
            return TRUE;
        if ((lParam & 0x1ff0000) == 0x02a0000 && g_winKeyState[KEY_ShiftL])
            return TRUE;
        break;
    default:
        return TRUE;
    }
    return FALSE;
}

/* Only one shift release message is sent even if both are pressed.
 * Fix this here
 */
void
winFixShiftKeys(int iScanCode)
{
    if (GetKeyState(VK_SHIFT) & 0x8000)
        return;

    if (iScanCode == KEY_ShiftL && g_winKeyState[KEY_ShiftR])
        winSendKeyEvent(KEY_ShiftR, FALSE);
    if (iScanCode == KEY_ShiftR && g_winKeyState[KEY_ShiftL])
        winSendKeyEvent(KEY_ShiftL, FALSE);
}