1
/* $Id: VBoxHlp.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
3
* VBox frontends: Qt GUI ("VirtualBox"):
4
* Implementation of OS/2-specific helpers that require to reside in a DLL
8
* Copyright (C) 2008 Oracle Corporation
10
* This file is part of VirtualBox Open Source Edition (OSE), as
11
* available from http://www.virtualbox.org. This file is free software;
12
* you can redistribute it and/or modify it under the terms of the GNU
13
* General Public License (GPL) as published by the Free Software
14
* Foundation, in version 2 as it comes in the "COPYING" file of the
15
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
#define OS2EMX_PLAIN_CHAR
23
#define INCL_DOSINFOSEG
24
#define INCL_DOSDEVIOCTL
30
* Undocumented PM hook that is called before the pressed key is checked
31
* against the global accelerator table.
33
* Taken from the xWorkplace source code where it appears to come from the
34
* ProgramCommander/2 source code. Thanks to Ulrich Moeller and Roman Stangl.
36
#define HK_PREACCEL 17
38
/* NOTE: all global data is per-process (DATA32 is multiple, nonshared). */
40
/* Module handle of this DLL */
41
static HMODULE gThisModule = NULLHANDLE;
43
static PGINFOSEG gGIS = NULL;
44
static PLINFOSEG gLIS = NULL;
46
/* Parameters for the keyboard hook (VBoxHlpInstallKbdHook()) */
47
HAB gKbdHookHab = NULLHANDLE;
48
HWND gKbdHookHwnd = NULLHANDLE;
49
ULONG gKbdHookMsg = 0;
52
* Message Input hook used to monitor the system message queue.
54
* @param aHab Anchor handle.
55
* @param aHwnd Pointer to the QMSG structure.
56
* @param aFS Flags from WinPeekMsg(), either PM_NOREMOVE or
59
* @return @c TRUE to steal the given message or @c FALSE to pass it to the
60
* rest of the hook chain.
63
BOOL EXPENTRY vboxInputHook (HAB aHab, PQMSG aMsg, ULONG aFS)
65
if (aMsg->msg == WM_CHAR)
67
/* For foreign processes that didn't call VBoxHlpInstallKbdHook(),
68
* gKbdHookHwnd remains NULL. If it's the case while in this input
69
* hook, it means that the given foreign process is in foreground
70
* now. Since forwarding should work only for processes that
71
* called VBoxHlpInstallKbdHook(), we ignore the message. */
72
if (gKbdHookHwnd != NULLHANDLE)
75
WinSendMsg (gKbdHookHwnd, gKbdHookMsg, aMsg->mp1, aMsg->mp2);
84
* Installs a hook that will intercept all keyboard input (WM_CHAR) messages
85
* and forward them to the given window handle using the given message
86
* identifier. Messages are intercepted only when the given top-level window
87
* is the active desktop window (i.e. a window receiving keyboard input).
89
* When the WM_CHAR message is intercepted, it is forwarded as is (including
90
* all parameters) except that the message ID is changed to the given message
91
* ID. The result of the WinSendMsg() call is then converted to BOOL and if
92
* it results to TRUE then the message is considered to be processed,
93
* otherwise it is passed back to the system for normal processing.
95
* If the hook is already installed for the same or another window, this
96
* method will return @c false.
98
* @note This function is not thread-safe and must be called only on the main
99
* thread once per process.
101
* @param aHab Window anchor block.
102
* @param aHwnd Top-level window handle to forward WM_CHAR messages to.
103
* @param aMsg Message ID to use when forwarding.
105
* @return @c true on success and @c false otherwise. */
106
VBOXHLPDECL(bool) VBoxHlpInstallKbdHook (HAB aHab, HWND aHwnd,
109
if (gKbdHookHwnd != NULLHANDLE ||
113
BOOL ok = WinSetHook (aHab, NULLHANDLE, HK_PREACCEL,
114
(PFN) vboxInputHook, gThisModule);
119
gKbdHookHwnd = aHwnd;
127
* Uninstalls the keyboard hook installed by VBoxHlpInstallKbdHook().
128
* All arguments must match arguments passed to VBoxHlpInstallKbdHook(),
129
* otherwise this method will do nothing and return @c false.
131
* @return @c true on success and @c false otherwise.
133
VBOXHLPDECL(bool) VBoxHlpUninstallKbdHook (HAB aHab, HWND aHwnd,
136
if (gKbdHookHab != aHab ||
137
gKbdHookHwnd != aHwnd ||
141
BOOL ok = WinReleaseHook (aHab, NULLHANDLE, HK_PREACCEL,
142
(PFN) vboxInputHook, gThisModule);
146
gKbdHookHab = NULLHANDLE;
147
gKbdHookHwnd = NULLHANDLE;
157
* @param aHandle DLL module handle.
158
* @param aFlag 0 on initialization or 1 on termination.
160
* @return Non-zero for success or 0 for failure.
162
ULONG _System _DLL_InitTerm (HMODULE aHandle, ULONG aFlag)
168
/* DLL initialization */
170
gThisModule = aHandle;
172
gGIS = GETGINFOSEG();
173
gLIS = GETLINFOSEG();
177
/* DLL termination */
179
/* Make sure we release the hook if the user forgets to do so. */
180
if (gKbdHookHwnd != NULLHANDLE)
181
WinReleaseHook (gKbdHookHab, NULLHANDLE, HK_PREACCEL,
182
(PFN) vboxInputHook, gThisModule);
184
gThisModule = NULLHANDLE;
187
return (unsigned long) ok;