~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/platform/os2/VBoxHlp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: VBoxHlp.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
 
2
/** @file
 
3
 * VBox frontends: Qt GUI ("VirtualBox"):
 
4
 * Implementation of OS/2-specific helpers that require to reside in a DLL
 
5
 */
 
6
 
 
7
/*
 
8
 * Copyright (C) 2008 Oracle Corporation
 
9
 *
 
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.
 
17
 */
 
18
 
 
19
#define OS2EMX_PLAIN_CHAR
 
20
 
 
21
#define INCL_BASE
 
22
#define INCL_PM
 
23
#define INCL_DOSINFOSEG
 
24
#define INCL_DOSDEVIOCTL
 
25
#include <os2.h>
 
26
 
 
27
#include "VBoxHlp.h"
 
28
 
 
29
/**
 
30
 *  Undocumented PM hook that is called before the pressed key is checked
 
31
 *  against the global accelerator table.
 
32
 *
 
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.
 
35
 */
 
36
#define HK_PREACCEL 17
 
37
 
 
38
/* NOTE: all global data is per-process (DATA32 is multiple, nonshared). */
 
39
 
 
40
/* Module handle of this DLL */
 
41
static HMODULE gThisModule = NULLHANDLE;
 
42
 
 
43
static PGINFOSEG gGIS = NULL;
 
44
static PLINFOSEG gLIS = NULL;
 
45
 
 
46
/* Parameters for the keyboard hook (VBoxHlpInstallKbdHook()) */
 
47
HAB gKbdHookHab = NULLHANDLE;
 
48
HWND gKbdHookHwnd = NULLHANDLE;
 
49
ULONG gKbdHookMsg = 0;
 
50
 
 
51
/**
 
52
 *  Message Input hook used to monitor the system message queue.
 
53
 *
 
54
 *  @param aHab     Anchor handle.
 
55
 *  @param aHwnd    Pointer to the QMSG structure.
 
56
 *  @param aFS      Flags from WinPeekMsg(), either PM_NOREMOVE or
 
57
 *                  PM_REMOVE.
 
58
 *
 
59
 *  @return @c TRUE to steal the given message or @c FALSE to pass it to the
 
60
 *  rest of the hook chain.
 
61
 */
 
62
static
 
63
BOOL EXPENTRY vboxInputHook (HAB aHab, PQMSG aMsg, ULONG aFS)
 
64
{
 
65
    if (aMsg->msg == WM_CHAR)
 
66
    {
 
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)
 
73
        {
 
74
            MRESULT reply =
 
75
                WinSendMsg (gKbdHookHwnd, gKbdHookMsg, aMsg->mp1, aMsg->mp2);
 
76
            return (BOOL) reply;
 
77
        }
 
78
    }
 
79
 
 
80
    return FALSE;
 
81
}
 
82
 
 
83
/**
 
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).
 
88
 *
 
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.
 
94
 *
 
95
 *  If the hook is already installed for the same or another window, this
 
96
 *  method will return @c false.
 
97
 *
 
98
 *  @note This function is not thread-safe and must be called only on the main
 
99
 *  thread once per process.
 
100
 *
 
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.
 
104
 *
 
105
 *  @return @c true on success and @c false otherwise.  */
 
106
VBOXHLPDECL(bool) VBoxHlpInstallKbdHook (HAB aHab, HWND aHwnd,
 
107
                                         unsigned long aMsg)
 
108
{
 
109
    if (gKbdHookHwnd != NULLHANDLE ||
 
110
        aHwnd == NULLHANDLE)
 
111
        return false;
 
112
 
 
113
    BOOL ok = WinSetHook (aHab, NULLHANDLE, HK_PREACCEL,
 
114
                          (PFN) vboxInputHook, gThisModule);
 
115
 
 
116
    if (ok)
 
117
    {
 
118
        gKbdHookHab = aHab;
 
119
        gKbdHookHwnd = aHwnd;
 
120
        gKbdHookMsg = aMsg;
 
121
    }
 
122
 
 
123
    return (bool) ok;
 
124
}
 
125
 
 
126
/**
 
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.
 
130
 *
 
131
 *  @return @c true on success and @c false otherwise.
 
132
 */
 
133
VBOXHLPDECL(bool) VBoxHlpUninstallKbdHook (HAB aHab, HWND aHwnd,
 
134
                                           unsigned long aMsg)
 
135
{
 
136
    if (gKbdHookHab != aHab ||
 
137
        gKbdHookHwnd != aHwnd ||
 
138
        gKbdHookMsg != aMsg)
 
139
        return false;
 
140
 
 
141
    BOOL ok = WinReleaseHook (aHab, NULLHANDLE, HK_PREACCEL,
 
142
                              (PFN) vboxInputHook, gThisModule);
 
143
 
 
144
    if (ok)
 
145
    {
 
146
        gKbdHookHab = NULLHANDLE;
 
147
        gKbdHookHwnd = NULLHANDLE;
 
148
        gKbdHookMsg = 0;
 
149
    }
 
150
 
 
151
    return (bool) ok;
 
152
}
 
153
 
 
154
/**
 
155
 *  DLL entry point.
 
156
 *
 
157
 *  @param aHandle  DLL module handle.
 
158
 *  @param aFlag    0 on initialization or 1 on termination.
 
159
 *
 
160
 *  @return Non-zero for success or 0 for failure.
 
161
 */
 
162
ULONG _System _DLL_InitTerm (HMODULE aHandle, ULONG aFlag)
 
163
{
 
164
    bool ok = true;
 
165
 
 
166
    if (aFlag == 0)
 
167
    {
 
168
        /* DLL initialization */
 
169
 
 
170
        gThisModule = aHandle;
 
171
 
 
172
        gGIS = GETGINFOSEG();
 
173
        gLIS = GETLINFOSEG();
 
174
    }
 
175
    else
 
176
    {
 
177
        /* DLL termination */
 
178
 
 
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);
 
183
 
 
184
        gThisModule = NULLHANDLE;
 
185
    }
 
186
 
 
187
    return (unsigned long) ok;
 
188
}
 
189