1706f2543Smrg/*
2706f2543Smrg *Copyright (C) 2004 Harold L Hunt II All Rights Reserved.
3706f2543Smrg *
4706f2543Smrg *Permission is hereby granted, free of charge, to any person obtaining
5706f2543Smrg * a copy of this software and associated documentation files (the
6706f2543Smrg *"Software"), to deal in the Software without restriction, including
7706f2543Smrg *without limitation the rights to use, copy, modify, merge, publish,
8706f2543Smrg *distribute, sublicense, and/or sell copies of the Software, and to
9706f2543Smrg *permit persons to whom the Software is furnished to do so, subject to
10706f2543Smrg *the following conditions:
11706f2543Smrg *
12706f2543Smrg *The above copyright notice and this permission notice shall be
13706f2543Smrg *included in all copies or substantial portions of the Software.
14706f2543Smrg *
15706f2543Smrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16706f2543Smrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17706f2543Smrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18706f2543Smrg *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19706f2543Smrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20706f2543Smrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21706f2543Smrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22706f2543Smrg *
23706f2543Smrg *Except as contained in this notice, the name of Harold L Hunt II
24706f2543Smrg *shall not be used in advertising or otherwise to promote the sale, use
25706f2543Smrg *or other dealings in this Software without prior written authorization
26706f2543Smrg *from Harold L Hunt II.
27706f2543Smrg *
28706f2543Smrg * Authors:	Harold L Hunt II
29706f2543Smrg */
30706f2543Smrg
31706f2543Smrg#ifdef HAVE_XWIN_CONFIG_H
32706f2543Smrg#include <xwin-config.h>
33706f2543Smrg#endif
34706f2543Smrg#include "win.h"
35706f2543Smrg
36706f2543Smrg
37706f2543Smrgstatic HHOOK g_hhookKeyboardLL = NULL;
38706f2543Smrg
39706f2543Smrg
40706f2543Smrg/*
41706f2543Smrg * Function prototypes
42706f2543Smrg */
43706f2543Smrg
44706f2543Smrgstatic LRESULT CALLBACK
45706f2543SmrgwinKeyboardMessageHookLL (int iCode, WPARAM wParam, LPARAM lParam);
46706f2543Smrg
47706f2543Smrg
48706f2543Smrg#ifndef LLKHF_EXTENDED
49706f2543Smrg# define LLKHF_EXTENDED  0x00000001
50706f2543Smrg#endif
51706f2543Smrg#ifndef LLKHF_UP
52706f2543Smrg# define LLKHF_UP  0x00000080
53706f2543Smrg#endif
54706f2543Smrg
55706f2543Smrg
56706f2543Smrg/*
57706f2543Smrg * KeyboardMessageHook
58706f2543Smrg */
59706f2543Smrg
60706f2543Smrgstatic LRESULT CALLBACK
61706f2543SmrgwinKeyboardMessageHookLL (int iCode, WPARAM wParam, LPARAM lParam)
62706f2543Smrg{
63706f2543Smrg  BOOL			fPassKeystroke = FALSE;
64706f2543Smrg  BOOL			fPassAltTab = TRUE;
65706f2543Smrg  PKBDLLHOOKSTRUCT	p = (PKBDLLHOOKSTRUCT) lParam;
66706f2543Smrg  HWND			hwnd = GetActiveWindow();
67706f2543Smrg#ifdef XWIN_MULTIWINDOW
68706f2543Smrg  WindowPtr		pWin = NULL;
69706f2543Smrg  winPrivWinPtr	        pWinPriv = NULL;
70706f2543Smrg  winPrivScreenPtr	pScreenPriv = NULL;
71706f2543Smrg  winScreenInfo		*pScreenInfo = NULL;
72706f2543Smrg
73706f2543Smrg  /* Check if the Windows window property for our X window pointer is valid */
74706f2543Smrg  if ((pWin = GetProp (hwnd, WIN_WINDOW_PROP)) != NULL)
75706f2543Smrg    {
76706f2543Smrg      /* Get a pointer to our window privates */
77706f2543Smrg      pWinPriv		= winGetWindowPriv(pWin);
78706f2543Smrg
79706f2543Smrg      /* Get pointers to our screen privates and screen info */
80706f2543Smrg      pScreenPriv	= pWinPriv->pScreenPriv;
81706f2543Smrg      pScreenInfo	= pScreenPriv->pScreenInfo;
82706f2543Smrg
83706f2543Smrg      if (pScreenInfo->fMultiWindow)
84706f2543Smrg          fPassAltTab = FALSE;
85706f2543Smrg    }
86706f2543Smrg#endif
87706f2543Smrg
88706f2543Smrg  /* Pass keystrokes on to our main message loop */
89706f2543Smrg  if (iCode == HC_ACTION)
90706f2543Smrg    {
91706f2543Smrg      winDebug("winKeyboardMessageHook: vkCode: %08x scanCode: %08x\n", p->vkCode, p->scanCode);
92706f2543Smrg
93706f2543Smrg      switch (wParam)
94706f2543Smrg	{
95706f2543Smrg	case WM_KEYDOWN:  case WM_SYSKEYDOWN:
96706f2543Smrg	case WM_KEYUP:    case WM_SYSKEYUP:
97706f2543Smrg	  fPassKeystroke =
98706f2543Smrg	    (fPassAltTab &&
99706f2543Smrg                (p->vkCode == VK_TAB) && ((p->flags & LLKHF_ALTDOWN) != 0))
100706f2543Smrg	    || (p->vkCode == VK_LWIN) || (p->vkCode == VK_RWIN)
101706f2543Smrg	    ;
102706f2543Smrg	  break;
103706f2543Smrg	}
104706f2543Smrg    }
105706f2543Smrg
106706f2543Smrg  /*
107706f2543Smrg   * Pass message on to our main message loop.
108706f2543Smrg   * We process this immediately with SendMessage so that the keystroke
109706f2543Smrg   * appears in, hopefully, the correct order.
110706f2543Smrg   */
111706f2543Smrg  if (fPassKeystroke)
112706f2543Smrg    {
113706f2543Smrg      LPARAM		lParamKey = 0x0;
114706f2543Smrg
115706f2543Smrg      /* Construct the lParam from KBDLLHOOKSTRUCT */
116706f2543Smrg      lParamKey = lParamKey | (0x0000FFFF & 0x00000001); /* Repeat count */
117706f2543Smrg      lParamKey = lParamKey | (0x00FF0000 & (p->scanCode << 16));
118706f2543Smrg      lParamKey = lParamKey
119706f2543Smrg	| (0x01000000 & ((p->flags & LLKHF_EXTENDED) << 23));
120706f2543Smrg      lParamKey = lParamKey
121706f2543Smrg	| (0x20000000
122706f2543Smrg	   & ((p->flags & LLKHF_ALTDOWN) << 24));
123706f2543Smrg      lParamKey = lParamKey | (0x80000000 & ((p->flags & LLKHF_UP) << 24));
124706f2543Smrg
125706f2543Smrg      /* Send message to our main window that has the keyboard focus */
126706f2543Smrg      PostMessage (hwnd,
127706f2543Smrg		   (UINT) wParam,
128706f2543Smrg		   (WPARAM) p->vkCode,
129706f2543Smrg		   lParamKey);
130706f2543Smrg
131706f2543Smrg      return 1;
132706f2543Smrg    }
133706f2543Smrg
134706f2543Smrg  /* Call next hook */
135706f2543Smrg  return CallNextHookEx (NULL, iCode, wParam, lParam);
136706f2543Smrg}
137706f2543Smrg
138706f2543Smrg
139706f2543Smrg/*
140706f2543Smrg * Attempt to install the keyboard hook, return FALSE if it was not installed
141706f2543Smrg */
142706f2543Smrg
143706f2543SmrgBool
144706f2543SmrgwinInstallKeyboardHookLL (void)
145706f2543Smrg{
146706f2543Smrg  OSVERSIONINFO		osvi = {0};
147706f2543Smrg
148706f2543Smrg  /* Get operating system version information */
149706f2543Smrg  osvi.dwOSVersionInfoSize = sizeof (osvi);
150706f2543Smrg  GetVersionEx (&osvi);
151706f2543Smrg
152706f2543Smrg  /* Branch on platform ID */
153706f2543Smrg  switch (osvi.dwPlatformId)
154706f2543Smrg    {
155706f2543Smrg    case VER_PLATFORM_WIN32_NT:
156706f2543Smrg      /* Low-level is supported on NT 4.0 SP3+ only */
157706f2543Smrg      /* TODO: Return FALSE on NT 4.0 with no SP, SP1, or SP2 */
158706f2543Smrg      break;
159706f2543Smrg
160706f2543Smrg    case VER_PLATFORM_WIN32_WINDOWS:
161706f2543Smrg      /* Low-level hook is not supported on non-NT */
162706f2543Smrg      return FALSE;
163706f2543Smrg    }
164706f2543Smrg
165706f2543Smrg  /* Install the hook only once */
166706f2543Smrg  if (!g_hhookKeyboardLL)
167706f2543Smrg    g_hhookKeyboardLL = SetWindowsHookEx (WH_KEYBOARD_LL,
168706f2543Smrg					  winKeyboardMessageHookLL,
169706f2543Smrg					  g_hInstance,
170706f2543Smrg					  0);
171706f2543Smrg
172706f2543Smrg  return TRUE;
173706f2543Smrg}
174706f2543Smrg
175706f2543Smrg
176706f2543Smrg/*
177706f2543Smrg * Remove the keyboard hook if it is installed
178706f2543Smrg */
179706f2543Smrg
180706f2543Smrgvoid
181706f2543SmrgwinRemoveKeyboardHookLL (void)
182706f2543Smrg{
183706f2543Smrg  if (g_hhookKeyboardLL)
184706f2543Smrg    UnhookWindowsHookEx (g_hhookKeyboardLL);
185706f2543Smrg  g_hhookKeyboardLL = NULL;
186706f2543Smrg}
187