1 | /* $Id: VBoxHlp.cpp 71027 2018-02-15 14:33:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Implementation of OS/2-specific helpers that require to reside in a DLL
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define OS2EMX_PLAIN_CHAR
|
---|
19 |
|
---|
20 | #define INCL_BASE
|
---|
21 | #define INCL_PM
|
---|
22 | #define INCL_DOSINFOSEG
|
---|
23 | #define INCL_DOSDEVIOCTL
|
---|
24 | #include <os2.h>
|
---|
25 |
|
---|
26 | #include "VBoxHlp.h"
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Undocumented PM hook that is called before the pressed key is checked
|
---|
30 | * against the global accelerator table.
|
---|
31 | *
|
---|
32 | * Taken from the xWorkplace source code where it appears to come from the
|
---|
33 | * ProgramCommander/2 source code. Thanks to Ulrich Moeller and Roman Stangl.
|
---|
34 | */
|
---|
35 | #define HK_PREACCEL 17
|
---|
36 |
|
---|
37 | /* NOTE: all global data is per-process (DATA32 is multiple, nonshared). */
|
---|
38 |
|
---|
39 | /* Module handle of this DLL */
|
---|
40 | static HMODULE gThisModule = NULLHANDLE;
|
---|
41 |
|
---|
42 | static PGINFOSEG gGIS = NULL;
|
---|
43 | static PLINFOSEG gLIS = NULL;
|
---|
44 |
|
---|
45 | /* Parameters for the keyboard hook (VBoxHlpInstallKbdHook()) */
|
---|
46 | HAB gKbdHookHab = NULLHANDLE;
|
---|
47 | HWND gKbdHookHwnd = NULLHANDLE;
|
---|
48 | ULONG gKbdHookMsg = 0;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Message Input hook used to monitor the system message queue.
|
---|
52 | *
|
---|
53 | * @param aHab Anchor handle.
|
---|
54 | * @param aHwnd Pointer to the QMSG structure.
|
---|
55 | * @param aFS Flags from WinPeekMsg(), either PM_NOREMOVE or
|
---|
56 | * PM_REMOVE.
|
---|
57 | *
|
---|
58 | * @return @c TRUE to steal the given message or @c FALSE to pass it to the
|
---|
59 | * rest of the hook chain.
|
---|
60 | */
|
---|
61 | static
|
---|
62 | BOOL EXPENTRY vboxInputHook (HAB aHab, PQMSG aMsg, ULONG aFS)
|
---|
63 | {
|
---|
64 | if (aMsg->msg == WM_CHAR)
|
---|
65 | {
|
---|
66 | /* For foreign processes that didn't call VBoxHlpInstallKbdHook(),
|
---|
67 | * gKbdHookHwnd remains NULL. If it's the case while in this input
|
---|
68 | * hook, it means that the given foreign process is in foreground
|
---|
69 | * now. Since forwarding should work only for processes that
|
---|
70 | * called VBoxHlpInstallKbdHook(), we ignore the message. */
|
---|
71 | if (gKbdHookHwnd != NULLHANDLE)
|
---|
72 | {
|
---|
73 | MRESULT reply =
|
---|
74 | WinSendMsg (gKbdHookHwnd, gKbdHookMsg, aMsg->mp1, aMsg->mp2);
|
---|
75 | return (BOOL) reply;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | return FALSE;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Installs a hook that will intercept all keyboard input (WM_CHAR) messages
|
---|
84 | * and forward them to the given window handle using the given message
|
---|
85 | * identifier. Messages are intercepted only when the given top-level window
|
---|
86 | * is the active desktop window (i.e. a window receiving keyboard input).
|
---|
87 | *
|
---|
88 | * When the WM_CHAR message is intercepted, it is forwarded as is (including
|
---|
89 | * all parameters) except that the message ID is changed to the given message
|
---|
90 | * ID. The result of the WinSendMsg() call is then converted to BOOL and if
|
---|
91 | * it results to TRUE then the message is considered to be processed,
|
---|
92 | * otherwise it is passed back to the system for normal processing.
|
---|
93 | *
|
---|
94 | * If the hook is already installed for the same or another window, this
|
---|
95 | * method will return @c false.
|
---|
96 | *
|
---|
97 | * @note This function is not thread-safe and must be called only on the main
|
---|
98 | * thread once per process.
|
---|
99 | *
|
---|
100 | * @param aHab Window anchor block.
|
---|
101 | * @param aHwnd Top-level window handle to forward WM_CHAR messages to.
|
---|
102 | * @param aMsg Message ID to use when forwarding.
|
---|
103 | *
|
---|
104 | * @return @c true on success and @c false otherwise. */
|
---|
105 | VBOXHLPDECL(bool) VBoxHlpInstallKbdHook (HAB aHab, HWND aHwnd,
|
---|
106 | unsigned long aMsg)
|
---|
107 | {
|
---|
108 | if (gKbdHookHwnd != NULLHANDLE ||
|
---|
109 | aHwnd == NULLHANDLE)
|
---|
110 | return false;
|
---|
111 |
|
---|
112 | BOOL ok = WinSetHook (aHab, NULLHANDLE, HK_PREACCEL,
|
---|
113 | (PFN) vboxInputHook, gThisModule);
|
---|
114 |
|
---|
115 | if (ok)
|
---|
116 | {
|
---|
117 | gKbdHookHab = aHab;
|
---|
118 | gKbdHookHwnd = aHwnd;
|
---|
119 | gKbdHookMsg = aMsg;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return (bool) ok;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Uninstalls the keyboard hook installed by VBoxHlpInstallKbdHook().
|
---|
127 | * All arguments must match arguments passed to VBoxHlpInstallKbdHook(),
|
---|
128 | * otherwise this method will do nothing and return @c false.
|
---|
129 | *
|
---|
130 | * @return @c true on success and @c false otherwise.
|
---|
131 | */
|
---|
132 | VBOXHLPDECL(bool) VBoxHlpUninstallKbdHook (HAB aHab, HWND aHwnd,
|
---|
133 | unsigned long aMsg)
|
---|
134 | {
|
---|
135 | if (gKbdHookHab != aHab ||
|
---|
136 | gKbdHookHwnd != aHwnd ||
|
---|
137 | gKbdHookMsg != aMsg)
|
---|
138 | return false;
|
---|
139 |
|
---|
140 | BOOL ok = WinReleaseHook (aHab, NULLHANDLE, HK_PREACCEL,
|
---|
141 | (PFN) vboxInputHook, gThisModule);
|
---|
142 |
|
---|
143 | if (ok)
|
---|
144 | {
|
---|
145 | gKbdHookHab = NULLHANDLE;
|
---|
146 | gKbdHookHwnd = NULLHANDLE;
|
---|
147 | gKbdHookMsg = 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return (bool) ok;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * DLL entry point.
|
---|
155 | *
|
---|
156 | * @param aHandle DLL module handle.
|
---|
157 | * @param aFlag 0 on initialization or 1 on termination.
|
---|
158 | *
|
---|
159 | * @return Non-zero for success or 0 for failure.
|
---|
160 | */
|
---|
161 | ULONG _System _DLL_InitTerm (HMODULE aHandle, ULONG aFlag)
|
---|
162 | {
|
---|
163 | bool ok = true;
|
---|
164 |
|
---|
165 | if (aFlag == 0)
|
---|
166 | {
|
---|
167 | /* DLL initialization */
|
---|
168 |
|
---|
169 | gThisModule = aHandle;
|
---|
170 |
|
---|
171 | gGIS = GETGINFOSEG();
|
---|
172 | gLIS = GETLINFOSEG();
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | /* DLL termination */
|
---|
177 |
|
---|
178 | /* Make sure we release the hook if the user forgets to do so. */
|
---|
179 | if (gKbdHookHwnd != NULLHANDLE)
|
---|
180 | WinReleaseHook (gKbdHookHab, NULLHANDLE, HK_PREACCEL,
|
---|
181 | (PFN) vboxInputHook, gThisModule);
|
---|
182 |
|
---|
183 | gThisModule = NULLHANDLE;
|
---|
184 | }
|
---|
185 |
|
---|
186 | return (unsigned long) ok;
|
---|
187 | }
|
---|
188 |
|
---|