1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *Copyright (C) 2004 Harold L Hunt II All Rights Reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun *Permission is hereby granted, free of charge, to any person obtaining
5*4882a593Smuzhiyun * a copy of this software and associated documentation files (the
6*4882a593Smuzhiyun *"Software"), to deal in the Software without restriction, including
7*4882a593Smuzhiyun *without limitation the rights to use, copy, modify, merge, publish,
8*4882a593Smuzhiyun *distribute, sublicense, and/or sell copies of the Software, and to
9*4882a593Smuzhiyun *permit persons to whom the Software is furnished to do so, subject to
10*4882a593Smuzhiyun *the following conditions:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun *The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun *included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19*4882a593Smuzhiyun *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20*4882a593Smuzhiyun *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21*4882a593Smuzhiyun *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun *Except as contained in this notice, the name of Harold L Hunt II
24*4882a593Smuzhiyun *shall not be used in advertising or otherwise to promote the sale, use
25*4882a593Smuzhiyun *or other dealings in this Software without prior written authorization
26*4882a593Smuzhiyun *from Harold L Hunt II.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Authors: Harold L Hunt II
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
32*4882a593Smuzhiyun #include <xwin-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun #include "win.h"
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static HHOOK g_hhookKeyboardLL = NULL;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * Function prototypes
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static LRESULT CALLBACK
43*4882a593Smuzhiyun winKeyboardMessageHookLL(int iCode, WPARAM wParam, LPARAM lParam);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #ifndef LLKHF_EXTENDED
46*4882a593Smuzhiyun #define LLKHF_EXTENDED 0x00000001
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #ifndef LLKHF_UP
49*4882a593Smuzhiyun #define LLKHF_UP 0x00000080
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * KeyboardMessageHook
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static LRESULT CALLBACK
winKeyboardMessageHookLL(int iCode,WPARAM wParam,LPARAM lParam)57*4882a593Smuzhiyun winKeyboardMessageHookLL(int iCode, WPARAM wParam, LPARAM lParam)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun BOOL fPassKeystroke = FALSE;
60*4882a593Smuzhiyun BOOL fPassAltTab = TRUE;
61*4882a593Smuzhiyun PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
62*4882a593Smuzhiyun HWND hwnd = GetActiveWindow();
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun WindowPtr pWin = NULL;
65*4882a593Smuzhiyun winPrivWinPtr pWinPriv = NULL;
66*4882a593Smuzhiyun winPrivScreenPtr pScreenPriv = NULL;
67*4882a593Smuzhiyun winScreenInfo *pScreenInfo = NULL;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Check if the Windows window property for our X window pointer is valid */
70*4882a593Smuzhiyun if ((pWin = GetProp(hwnd, WIN_WINDOW_PROP)) != NULL) {
71*4882a593Smuzhiyun /* Get a pointer to our window privates */
72*4882a593Smuzhiyun pWinPriv = winGetWindowPriv(pWin);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Get pointers to our screen privates and screen info */
75*4882a593Smuzhiyun pScreenPriv = pWinPriv->pScreenPriv;
76*4882a593Smuzhiyun pScreenInfo = pScreenPriv->pScreenInfo;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun if (pScreenInfo->fMultiWindow)
79*4882a593Smuzhiyun fPassAltTab = FALSE;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Pass keystrokes on to our main message loop */
83*4882a593Smuzhiyun if (iCode == HC_ACTION) {
84*4882a593Smuzhiyun winDebug("winKeyboardMessageHook: vkCode: %08x scanCode: %08x\n",
85*4882a593Smuzhiyun (unsigned int)p->vkCode, (unsigned int)p->scanCode);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun switch (wParam) {
88*4882a593Smuzhiyun case WM_KEYDOWN:
89*4882a593Smuzhiyun case WM_SYSKEYDOWN:
90*4882a593Smuzhiyun case WM_KEYUP:
91*4882a593Smuzhiyun case WM_SYSKEYUP:
92*4882a593Smuzhiyun fPassKeystroke =
93*4882a593Smuzhiyun (fPassAltTab &&
94*4882a593Smuzhiyun (p->vkCode == VK_TAB) && ((p->flags & LLKHF_ALTDOWN) != 0))
95*4882a593Smuzhiyun || (p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN);
96*4882a593Smuzhiyun break;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Pass message on to our main message loop.
102*4882a593Smuzhiyun * We process this immediately with SendMessage so that the keystroke
103*4882a593Smuzhiyun * appears in, hopefully, the correct order.
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun if (fPassKeystroke) {
106*4882a593Smuzhiyun LPARAM lParamKey = 0x0;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* Construct the lParam from KBDLLHOOKSTRUCT */
109*4882a593Smuzhiyun lParamKey = lParamKey | (0x0000FFFF & 0x00000001); /* Repeat count */
110*4882a593Smuzhiyun lParamKey = lParamKey | (0x00FF0000 & (p->scanCode << 16));
111*4882a593Smuzhiyun lParamKey = lParamKey
112*4882a593Smuzhiyun | (0x01000000 & ((p->flags & LLKHF_EXTENDED) << 23));
113*4882a593Smuzhiyun lParamKey = lParamKey
114*4882a593Smuzhiyun | (0x20000000 & ((p->flags & LLKHF_ALTDOWN) << 24));
115*4882a593Smuzhiyun lParamKey = lParamKey | (0x80000000 & ((p->flags & LLKHF_UP) << 24));
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Send message to our main window that has the keyboard focus */
118*4882a593Smuzhiyun PostMessage(hwnd, (UINT) wParam, (WPARAM) p->vkCode, lParamKey);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return 1;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* Call next hook */
124*4882a593Smuzhiyun return CallNextHookEx(NULL, iCode, wParam, lParam);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Attempt to install the keyboard hook, return FALSE if it was not installed
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun Bool
winInstallKeyboardHookLL(void)132*4882a593Smuzhiyun winInstallKeyboardHookLL(void)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun /* Install the hook only once */
135*4882a593Smuzhiyun if (!g_hhookKeyboardLL)
136*4882a593Smuzhiyun g_hhookKeyboardLL = SetWindowsHookEx(WH_KEYBOARD_LL,
137*4882a593Smuzhiyun winKeyboardMessageHookLL,
138*4882a593Smuzhiyun g_hInstance, 0);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return TRUE;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * Remove the keyboard hook if it is installed
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun void
winRemoveKeyboardHookLL(void)148*4882a593Smuzhiyun winRemoveKeyboardHookLL(void)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun if (g_hhookKeyboardLL)
151*4882a593Smuzhiyun UnhookWindowsHookEx(g_hhookKeyboardLL);
152*4882a593Smuzhiyun g_hhookKeyboardLL = NULL;
153*4882a593Smuzhiyun }
154