summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/platform/win/WinKeyboard.cpp
blob: 60d3e38a42ee860af1b4876c61e14ea4c58204c2 (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
/* $Id$ */
/** @file
 * VBox Qt GUI - Declarations of utility functions for handling Windows Keyboard specific tasks.
 */

/*
 * Copyright (C) 2014-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/* Defines: */
#define LOG_GROUP LOG_GROUP_GUI

/* GUI includes: */
#include "WinKeyboard.h"

/* Other VBox includes: */
#include <iprt/assert.h>
#include <VBox/log.h>

/* External includes: */
#include <stdio.h>


/* Beautification of log output */
#define VBOX_BOOL_TO_STR_STATE(x)   (x) ? "ON" : "OFF"
#define VBOX_CONTROL_TO_STR_NAME(x) ((x == VK_CAPITAL) ? "CAPS" : (x == VK_SCROLL ? "SCROLL" : ((x == VK_NUMLOCK) ? "NUM" : "UNKNOWN")))

/* A structure that contains internal control state representation */
typedef struct VBoxModifiersState_t {
    bool fNumLockOn;                        /** A state of NUM LOCK */
    bool fCapsLockOn;                       /** A state of CAPS LOCK */
    bool fScrollLockOn;                     /** A state of SCROLL LOCK */
} VBoxModifiersState_t;

/**
 * Get current state of a keyboard modifier.
 *
 * @param   idModifier        Modifier to examine (VK_CAPITAL, VK_SCROLL or VK_NUMLOCK)
 *
 * @returns modifier state or asserts if wrong modifier is specified.
 */
static bool winGetModifierState(int idModifier)
{
    AssertReturn((idModifier == VK_CAPITAL) || (idModifier == VK_SCROLL) || (idModifier == VK_NUMLOCK), false);
    return (GetKeyState(idModifier) & 0x0001) != 0;
}

/**
 * Set current state of a keyboard modifier.
 *
 * @param   idModifier        Modifier to set (VK_CAPITAL, VK_SCROLL or VK_NUMLOCK)
 * @param   fState            State to set
 */
static void winSetModifierState(int idModifier, bool fState)
{
    AssertReturnVoid((idModifier == VK_CAPITAL) || (idModifier == VK_SCROLL) || (idModifier == VK_NUMLOCK));

    /* If the modifier is already in desired state, just do nothing. Otherwise, toggle it. */
    if (winGetModifierState(idModifier) != fState)
    {
        /* Simulate KeyUp+KeyDown keystroke */
        keybd_event(idModifier, 0, KEYEVENTF_EXTENDEDKEY, 0);
        keybd_event(idModifier, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

        /* Process posted above keyboard events immediately: */
        MSG msg;
        while (::PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
            ::DispatchMessage(&msg);

        LogRel2(("HID LEDs sync: setting %s state to %s (0x%X).\n",
                 VBOX_CONTROL_TO_STR_NAME(idModifier), VBOX_BOOL_TO_STR_STATE(fState), MapVirtualKey(idModifier, MAPVK_VK_TO_VSC)));
    }
    else
    {
        LogRel2(("HID LEDs sync: setting %s state: skipped: state is already %s (0x%X).\n",
                 VBOX_CONTROL_TO_STR_NAME(idModifier), VBOX_BOOL_TO_STR_STATE(fState), MapVirtualKey(idModifier, MAPVK_VK_TO_VSC)));
    }
}

/** Set all HID LEDs at once. */
static bool winSetHidLeds(bool fNumLockOn, bool fCapsLockOn, bool fScrollLockOn)
{
    winSetModifierState(VK_NUMLOCK, fNumLockOn);
    winSetModifierState(VK_CAPITAL, fCapsLockOn);
    winSetModifierState(VK_SCROLL,  fScrollLockOn);
    return true;
}

/** Check if specified LED states correspond to the system modifier states. */
bool winHidLedsInSync(bool fNumLockOn, bool fCapsLockOn, bool fScrollLockOn)
{
    if (winGetModifierState(VK_NUMLOCK) != fNumLockOn)
        return false;

    if (winGetModifierState(VK_CAPITAL) != fCapsLockOn)
        return false;

    if (winGetModifierState(VK_SCROLL) != fScrollLockOn)
        return false;

    return true;
}

/**
 * Allocate memory and store modifier states there.
 *
 * @returns allocated memory witch contains modifier states or NULL.
 */
void * WinHidDevicesKeepLedsState(void)
{
    VBoxModifiersState_t *pState;

    pState = (VBoxModifiersState_t *)malloc(sizeof(VBoxModifiersState_t));
    if (pState)
    {
        pState->fNumLockOn    = winGetModifierState(VK_NUMLOCK);
        pState->fCapsLockOn   = winGetModifierState(VK_CAPITAL);
        pState->fScrollLockOn = winGetModifierState(VK_SCROLL);

        LogRel2(("HID LEDs sync: host state captured: NUM(%s) CAPS(%s) SCROLL(%s)\n",
                 VBOX_BOOL_TO_STR_STATE(pState->fNumLockOn),
                 VBOX_BOOL_TO_STR_STATE(pState->fCapsLockOn),
                 VBOX_BOOL_TO_STR_STATE(pState->fScrollLockOn)));

        return (void *)pState;
    }

    LogRel2(("HID LEDs sync: unable to allocate memory for HID LEDs synchronization data. LEDs sync will be disabled.\n"));

    return NULL;
}

/**
 * Restore host modifier states and free memory.
 */
void WinHidDevicesApplyAndReleaseLedsState(void *pData)
{
    VBoxModifiersState_t *pState = (VBoxModifiersState_t *)pData;

    if (pState)
    {
        LogRel2(("HID LEDs sync: attempt to restore host state: NUM(%s) CAPS(%s) SCROLL(%s)\n",
                 VBOX_BOOL_TO_STR_STATE(pState->fNumLockOn),
                 VBOX_BOOL_TO_STR_STATE(pState->fCapsLockOn),
                 VBOX_BOOL_TO_STR_STATE(pState->fScrollLockOn)));

        if (winSetHidLeds(pState->fNumLockOn, pState->fCapsLockOn, pState->fScrollLockOn))
            LogRel2(("HID LEDs sync: host state restored\n"));
        else
            LogRel2(("HID LEDs sync: host state restore failed\n"));

        free(pState);
    }
}

/**
 * Broadcast HID modifier states.
 *
 * @param   fNumLockOn        NUM LOCK state
 * @param   fCapsLockOn       CAPS LOCK state
 * @param   fScrollLockOn     SCROLL LOCK state
 */
void WinHidDevicesBroadcastLeds(bool fNumLockOn, bool fCapsLockOn, bool fScrollLockOn)
{
    LogRel2(("HID LEDs sync: start broadcast guest modifier states: NUM(%s) CAPS(%s) SCROLL(%s)\n",
             VBOX_BOOL_TO_STR_STATE(fNumLockOn),
             VBOX_BOOL_TO_STR_STATE(fCapsLockOn),
             VBOX_BOOL_TO_STR_STATE(fScrollLockOn)));

    if (winSetHidLeds(fNumLockOn, fCapsLockOn, fScrollLockOn))
        LogRel2(("HID LEDs sync: broadcast completed\n"));
    else
        LogRel2(("HID LEDs sync: broadcast failed\n"));
}

/** @brief doesCurrentLayoutHaveAltGr
  *
  * @return true if this keyboard layout has an AltGr key, false otherwise
  * Check to see whether the current keyboard layout actually has an AltGr key
  * by checking whether any of the keys which might do produce a symbol when
  * AltGr (Control + Alt) is depressed. Generally this loop will exit pretty
  * early (it exits on the first iteration for my German layout). If there is
  * no AltGr key in the layout then it will run right through, but that should
  * hopefully not happen very often.
  *
  * In theory we could do this once and cache the result, but that involves
  * tracking layout switches to invalidate the cache, and I don't think that the
  * added complexity is worth the price. */
static bool doesCurrentLayoutHaveAltGr()
{
    /* Keyboard state array with VK_CONTROL and VK_MENU depressed. */
    static const BYTE s_auKeyStates[256] =
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x80 };
    WORD ach;
    unsigned i;

    for (i = '0'; i <= VK_OEM_102; ++i)
    {
        if (ToAscii(i, 0, s_auKeyStates, &ach, 0))
            break;
        /* Skip ranges of virtual keys which are undefined or not relevant. */
        if (i == '9')
            i = 'A' - 1;
        if (i == 'Z')
            i = VK_OEM_1 - 1;
        if (i == VK_OEM_3)
            i = VK_OEM_4 - 1;
        if (i == VK_OEM_8)
            i = VK_OEM_102 - 1;
    }
    if (i > VK_OEM_102)
        return false;
    return true;
}

void WinAltGrMonitor::updateStateFromKeyEvent(unsigned iDownScanCode,
                                              bool fKeyDown, bool fExtendedKey)
{
    LONG messageTime = GetMessageTime();
    /* We do not want the make/break: */
    AssertRelease(~iDownScanCode & 0x80);
    /* Depending on m_enmFakeControlDetectionState: */
    switch (m_enmFakeControlDetectionState)
    {
        case NONE:
        case FAKE_CONTROL_DOWN:
            if (   iDownScanCode == 0x1D /* left control */
                && fKeyDown
                && !fExtendedKey)
                m_enmFakeControlDetectionState = LAST_EVENT_WAS_LEFT_CONTROL_DOWN;
            else
                m_enmFakeControlDetectionState = NONE;
            break;
        case LAST_EVENT_WAS_LEFT_CONTROL_DOWN:
            if (   iDownScanCode == 0x38 /* Alt */
                && fKeyDown
                && fExtendedKey
                && m_timeOfLastKeyEvent == messageTime
                && doesCurrentLayoutHaveAltGr())
            {
                m_enmFakeControlDetectionState = FAKE_CONTROL_DOWN;
                break;
            }
            else
                m_enmFakeControlDetectionState = LEFT_CONTROL_DOWN;
            RT_FALL_THRU();
        case LEFT_CONTROL_DOWN:
            if (   iDownScanCode == 0x1D /* left control */
                && !fKeyDown
                && !fExtendedKey)
                m_enmFakeControlDetectionState = NONE;
            break;
        default:
            AssertReleaseMsgFailed(("Unknown AltGr detection state.\n"));
    }
    m_timeOfLastKeyEvent = messageTime;
}

bool WinAltGrMonitor::isLeftControlReleaseNeeded() const
{
    return m_enmFakeControlDetectionState == FAKE_CONTROL_DOWN;
}

bool WinAltGrMonitor::isCurrentEventDefinitelyFake(unsigned iDownScanCode,
                                                   bool fKeyDown,
                                                   bool fExtendedKey) const
{
    if (iDownScanCode != 0x1d /* scan code: Control */ || fExtendedKey)
        return false;

    LONG messageTime = GetMessageTime();
    MSG peekMsg;
    if (!PeekMessage(&peekMsg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_NOREMOVE))
        return false;
    if (messageTime != (LONG)peekMsg.time)
        return false;

    if (   fKeyDown
        && peekMsg.message != WM_KEYDOWN
        && peekMsg.message != WM_SYSKEYDOWN)
        return false;
    if (   !fKeyDown
        && peekMsg.message != WM_KEYUP
        && peekMsg.message != WM_SYSKEYUP)
        return false;
    if (   (RT_HIWORD(peekMsg.lParam) & 0xFF) != 0x38 /* scan code: Alt */
        || !(RT_HIWORD(peekMsg.lParam) & KF_EXTENDED))
        return false;
    if (!doesCurrentLayoutHaveAltGr())
        return false;
    return true;
}