Implemented GetKeyState, GetKeyboardState & SetKeyboardState.
[reactos.git] / reactos / subsys / win32k / ntuser / keyboard.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: keyboard.c,v 1.5 2003/07/20 05:32:19 jimtabor Exp $
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * PURPOSE: Messages
24 * FILE: subsys/win32k/ntuser/keyboard.c
25 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * REVISION HISTORY:
27 * 06-06-2001 CSH Created
28 */
29
30 /* INCLUDES ******************************************************************/
31
32 #include <ddk/ntddk.h>
33 #include <win32k/win32k.h>
34 #include <internal/safe.h>
35 #include <include/guicheck.h>
36 #include <include/msgqueue.h>
37 #include <include/window.h>
38 #include <include/class.h>
39 #include <include/error.h>
40 #include <include/object.h>
41 #include <include/winsta.h>
42
43 #define NDEBUG
44 #include <debug.h>
45
46 /* FUNCTIONS *****************************************************************/
47
48 BYTE QueueKeyStateTable[256];
49
50 struct accent_char
51 {
52 BYTE ac_accent;
53 BYTE ac_char;
54 BYTE ac_result;
55 };
56
57 static const struct accent_char accent_chars[] =
58 {
59 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
60 {'`', 'A', '\300'}, {'`', 'a', '\340'},
61 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
62 {'^', 'A', '\302'}, {'^', 'a', '\342'},
63 {'~', 'A', '\303'}, {'~', 'a', '\343'},
64 {'"', 'A', '\304'}, {'"', 'a', '\344'},
65 {'O', 'A', '\305'}, {'o', 'a', '\345'},
66 {'0', 'A', '\305'}, {'0', 'a', '\345'},
67 {'A', 'A', '\305'}, {'a', 'a', '\345'},
68 {'A', 'E', '\306'}, {'a', 'e', '\346'},
69 {',', 'C', '\307'}, {',', 'c', '\347'},
70 {'`', 'E', '\310'}, {'`', 'e', '\350'},
71 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
72 {'^', 'E', '\312'}, {'^', 'e', '\352'},
73 {'"', 'E', '\313'}, {'"', 'e', '\353'},
74 {'`', 'I', '\314'}, {'`', 'i', '\354'},
75 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
76 {'^', 'I', '\316'}, {'^', 'i', '\356'},
77 {'"', 'I', '\317'}, {'"', 'i', '\357'},
78 {'-', 'D', '\320'}, {'-', 'd', '\360'},
79 {'~', 'N', '\321'}, {'~', 'n', '\361'},
80 {'`', 'O', '\322'}, {'`', 'o', '\362'},
81 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
82 {'^', 'O', '\324'}, {'^', 'o', '\364'},
83 {'~', 'O', '\325'}, {'~', 'o', '\365'},
84 {'"', 'O', '\326'}, {'"', 'o', '\366'},
85 {'/', 'O', '\330'}, {'/', 'o', '\370'},
86 {'`', 'U', '\331'}, {'`', 'u', '\371'},
87 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
88 {'^', 'U', '\333'}, {'^', 'u', '\373'},
89 {'"', 'U', '\334'}, {'"', 'u', '\374'},
90 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
91 {'T', 'H', '\336'}, {'t', 'h', '\376'},
92 {'s', 's', '\337'}, {'"', 'y', '\377'},
93 {'s', 'z', '\337'}, {'i', 'j', '\377'},
94 /* iso-8859-2 uses this */
95 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
96 {'<', 'S', '\251'}, {'<', 's', '\271'},
97 {'<', 'T', '\253'}, {'<', 't', '\273'},
98 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
99 {'<', 'C', '\310'}, {'<', 'c', '\350'},
100 {'<', 'E', '\314'}, {'<', 'e', '\354'},
101 {'<', 'D', '\317'}, {'<', 'd', '\357'},
102 {'<', 'N', '\322'}, {'<', 'n', '\362'},
103 {'<', 'R', '\330'}, {'<', 'r', '\370'},
104 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
105 {';', 'E', '\312'}, {';', 'e', '\332'},
106 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
107 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
108 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
109 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
110 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
111 /* collision whith S, from iso-8859-9 !!! */
112 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
113 {',', 'T', '\336'}, {',', 't', '\376'},
114 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
115 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
116 {'/', 'D', '\320'}, {'/', 'd', '\360'},
117 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
118 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
119 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
120 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
121 /* iso-8859-3 uses this */
122 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
123 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
124 {'>', 'J', '\254'}, {'>', 'j', '\274'},
125 {'>', 'C', '\306'}, {'>', 'c', '\346'},
126 {'>', 'G', '\330'}, {'>', 'g', '\370'},
127 {'>', 'S', '\336'}, {'>', 's', '\376'},
128 /* collision whith G( from iso-8859-9 !!! */
129 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
130 {'(', 'U', '\335'}, {'(', 'u', '\375'},
131 /* collision whith I. from iso-8859-3 !!! */
132 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
133 {'.', 'C', '\305'}, {'.', 'c', '\345'},
134 {'.', 'G', '\325'}, {'.', 'g', '\365'},
135 /* iso-8859-4 uses this */
136 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
137 {',', 'L', '\246'}, {',', 'l', '\266'},
138 {',', 'G', '\253'}, {',', 'g', '\273'},
139 {',', 'N', '\321'}, {',', 'n', '\361'},
140 {',', 'K', '\323'}, {',', 'k', '\363'},
141 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
142 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
143 {'-', 'A', '\300'}, {'-', 'a', '\340'},
144 {'-', 'I', '\317'}, {'-', 'i', '\357'},
145 {'-', 'O', '\322'}, {'-', 'o', '\362'},
146 {'-', 'U', '\336'}, {'-', 'u', '\376'},
147 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
148 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
149 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
150 {';', 'U', '\331'}, {';', 'u', '\371'},
151 /* iso-8859-9 uses this */
152 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
153 * whith the same letters on other iso-8859-x (that is they are on
154 * different places :-( ), if you use turkish uncomment these and
155 * comment out the lines in iso-8859-2 and iso-8859-3 sections
156 * FIXME: should be dynamic according to chosen language
157 * if/when Wine has turkish support.
158 */
159 /* collision whith G( from iso-8859-3 !!! */
160 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
161 /* collision whith S, from iso-8859-2 !!! */
162 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
163 /* collision whith I. from iso-8859-3 !!! */
164 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
165 };
166
167 int STDCALL
168 ToUnicode(UINT wVirtKey,
169 UINT wScanCode,
170 PBYTE lpKeyState,
171 LPWSTR pwszBuff,
172 int cchBuff,
173 UINT wFlags)
174 {
175 return(0);
176 }
177
178 BOOL STDCALL
179 NtUserTranslateMessage(LPMSG lpMsg,
180 DWORD Unknown1)
181 {
182 LONG UState;
183 WCHAR wp[2];
184 MSG NewMsg;
185 static INT dead_char;
186 PUSER_MESSAGE UMsg;
187
188 if (lpMsg->message != WM_KEYDOWN && lpMsg->message != WM_SYSKEYDOWN)
189 {
190 return(FALSE);
191 }
192
193 /* FIXME: Should pass current keyboard layout for this thread. */
194 UState = ToUnicode(lpMsg->wParam, HIWORD(lpMsg->lParam),
195 QueueKeyStateTable, wp, 2, 0);
196 if (UState == 1)
197 {
198 NewMsg.message = (lpMsg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
199 if (dead_char)
200 {
201 int i;
202
203 if (wp[0] == ' ') wp[0] = dead_char;
204 if (dead_char == 0xa2) dead_char = '(';
205 else if (dead_char == 0xa8) dead_char = '"';
206 else if (dead_char == 0xb2) dead_char = ';';
207 else if (dead_char == 0xb4) dead_char = '\'';
208 else if (dead_char == 0xb7) dead_char = '<';
209 else if (dead_char == 0xb8) dead_char = ',';
210 else if (dead_char == 0xff) dead_char = '.';
211 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
212 {
213 if ((accent_chars[i].ac_accent == dead_char) &&
214 (accent_chars[i].ac_char == wp[0]))
215 {
216 wp[0] = accent_chars[i].ac_result;
217 break;
218 }
219 dead_char = 0;
220 }
221 NewMsg.hwnd = lpMsg->hwnd;
222 NewMsg.wParam = wp[0];
223 NewMsg.lParam = lpMsg->lParam;
224 UMsg = MsqCreateMessage(&NewMsg);
225 MsqPostMessage(PsGetWin32Thread()->MessageQueue, UMsg);
226 return(TRUE);
227 }
228 }
229 else if (UState == -1)
230 {
231 NewMsg.message =
232 (lpMsg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
233 NewMsg.hwnd = lpMsg->hwnd;
234 NewMsg.wParam = wp[0];
235 NewMsg.lParam = lpMsg->lParam;
236 dead_char = wp[0];
237 UMsg = MsqCreateMessage(&NewMsg);
238 MsqPostMessage(PsGetWin32Thread()->MessageQueue, UMsg);
239 return(TRUE);
240 }
241 return(FALSE);
242 }
243
244 HWND STDCALL
245 NtUserSetFocus(HWND hWnd)
246 {
247 return W32kSetFocusWindow(hWnd);
248 }
249
250
251 DWORD
252 STDCALL
253 NtUserGetKeyState(
254 DWORD key)
255 {
256 DWORD ret;
257
258 if (key >= 'a' && key <= 'z') key += 'A' - 'a';
259 ret = ((DWORD)(QueueKeyStateTable[key] & 0x80) << 8 ) |
260 (QueueKeyStateTable[key] & 0x80) |
261 (QueueKeyStateTable[key] & 0x01);
262 return ret;
263 }
264
265 DWORD
266 STDCALL
267 NtUserGetKeyboardState(
268 LPBYTE lpKeyState)
269 {
270 if (lpKeyState) {
271 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState, QueueKeyStateTable, 256)))
272 return FALSE;
273 }
274 return TRUE;
275 }
276
277 DWORD
278 STDCALL
279 NtUserSetKeyboardState(
280 LPBYTE lpKeyState)
281 {
282 if (lpKeyState) {
283 if(! NT_SUCCESS(MmCopyFromCaller(QueueKeyStateTable, lpKeyState, 256)))
284 return FALSE;
285 }
286 return TRUE;
287 }
288
289
290 /* EOF */