minor corrections by M.Taguchi
[reactos.git] / reactos / apps / tests / user32 / input.c
1 /* Test Key event to Key message translation
2 *
3 * Copyright 2003 Rein Klazes
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /* test whether the right type of messages:
21 * WM_KEYUP/DOWN vs WM_SYSKEYUP/DOWN are sent in case of combined
22 * keystrokes.
23 *
24 * For instance <ALT>-X can be accompished by
25 * the sequence ALT-KEY-DOWN, X-KEY-DOWN, ALT-KEY-UP, X-KEY-UP
26 * but also X-KEY-DOWN, ALT-KEY-DOWN, X-KEY-UP, ALT-KEY-UP
27 * Whether a KEY or a SYSKEY message is sent is not always clear, it is
28 * also not the same in WINNT as in WIN9X */
29
30 /* NOTE that there will be test failures under WIN9X
31 * No applications are known to me that rely on this
32 * so I don't fix it */
33
34 /* TODO:
35 * 1. extend it to the wm_command and wm_syscommand notifications
36 * 2. add some more tests with special cases like dead keys or right (alt) key
37 * 3. there is some adapted code from input.c in here. Should really
38 * make that code exactly the same.
39 * 4. resolve the win9x case when there is a need or the testing frame work
40 * offers a nice way.
41 * 5. The test app creates a window, the user should not take the focus
42 * away during its short existence. I could do something to prevent that
43 * if it is a problem.
44 *
45 */
46
47 /* for definitions of INPUT */
48 #define _WIN32_WINNT 0x401
49
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52 #include "wine/test.h"
53
54 #include "winbase.h"
55 #include "winuser.h"
56
57 typedef struct tagINPUT
58 {
59 DWORD type;
60 union
61 {
62 MOUSEINPUT mi;
63 KEYBDINPUT ki;
64 HARDWAREINPUT hi;
65 } DUMMYUNIONNAME;
66 } INPUT, *PINPUT, *LPINPUT;
67
68 #include <assert.h>
69
70 /* globals */
71 HWND hWndTest;
72 long timetag = 0x10000000;
73
74 #define MAXKEYEVENTS 6
75 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
76 and only one message */
77
78 /* keyboard message names, sorted as their value */
79 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
80 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
81
82 /* keyevents, add more as needed */
83 typedef enum KEVtag
84 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
85 /* matching VK's */
86 int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
87 /* matching scan codes */
88 int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
89 /* matching updown events */
90 int GETUPDOWN[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
91 /* matching descripts */
92 char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
93
94 #define ADDTOINPUTS(kev) \
95 inputs[evtctr].type = INPUT_KEYBOARD; \
96 inputs[evtctr].u.ki.wVk = GETVKEY[ kev]; \
97 inputs[evtctr].u.ki.wScan = GETSCAN[ kev]; \
98 inputs[evtctr].u.ki.dwFlags = GETUPDOWN[ kev]; \
99 inputs[evtctr].u.ki.dwExtraInfo = 0; \
100 inputs[evtctr].u.ki.time = ++timetag; \
101 if( kev) evtctr++;
102
103 typedef struct {
104 UINT message;
105 WPARAM wParam;
106 LPARAM lParam;
107 } KMSG;
108
109 /*******************************************
110 * add new test sets here
111 * the software will make all combinations of the
112 * keyevent defined here
113 */
114 struct { int nrkev;
115 KEV keydwn[MAXKEYEVENTS];
116 KEV keyup[MAXKEYEVENTS];
117 } testkeyset[]= {
118 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
119 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
120 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
121 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
122 { 0 } /* mark the end */
123 };
124
125 /**********************adapted from input.c **********************************/
126
127 BYTE InputKeyStateTable[256];
128 BYTE AsyncKeyStateTable[256];
129 BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
130 or a WM_KEYUP message */
131 typedef union
132 {
133 struct
134 {
135 unsigned long count : 16;
136 unsigned long code : 8;
137 unsigned long extended : 1;
138 unsigned long unused : 2;
139 unsigned long win_internal : 2;
140 unsigned long context : 1;
141 unsigned long previous : 1;
142 unsigned long transition : 1;
143 } lp1;
144 unsigned long lp2;
145 } KEYLP;
146
147 int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
148 {
149 UINT message;
150 int VKey = GETVKEY[kev];
151 KEYLP keylp;
152
153 keylp.lp2 = 0;
154
155 keylp.lp1.count = 1;
156 keylp.lp1.code = GETSCAN[kev];
157 keylp.lp1.extended = 0 ;/* FIXME (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0; */
158 keylp.lp1.win_internal = 0;
159
160 if (GETUPDOWN[kev] & KEYEVENTF_KEYUP )
161 {
162 message = WM_KEYUP;
163 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
164 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
165 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
166 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
167 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
168 message = WM_SYSKEYUP;
169 TrackSysKey = 0;
170 }
171 InputKeyStateTable[VKey] &= ~0x80;
172 keylp.lp1.previous = 1;
173 keylp.lp1.transition = 1;
174 }
175 else
176 {
177 keylp.lp1.previous = (InputKeyStateTable[VKey] & 0x80) != 0;
178 keylp.lp1.transition = 0;
179 if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
180 InputKeyStateTable[VKey] |= 0x80;
181 AsyncKeyStateTable[VKey] |= 0x80;
182
183 message = WM_KEYDOWN;
184 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
185 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
186 message = WM_SYSKEYDOWN;
187 TrackSysKey = VKey;
188 }
189 }
190
191 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
192
193 if( plParam) *plParam = keylp.lp2;
194 if( pwParam) *pwParam = VKey;
195 return message;
196 }
197
198 /****************************** end copy input.c ****************************/
199
200 /*
201 * . prepare the keyevents for SendInputs
202 * . calculate the "expected" messages
203 * . Send the events to our window
204 * . retrieve the messages from the input queue
205 * . verify
206 */
207 void do_test( HWND hwnd, int seqnr, KEV td[] )
208 {
209 INPUT inputs[MAXKEYEVENTS];
210 KMSG expmsg[MAXKEYEVENTS];
211 MSG msg;
212 char buf[100];
213 int evtctr=0, kmctr, i;
214 buf[0]='\0';
215 TrackSysKey=0; /* see input.c */
216 for( i = 0; i < MAXKEYEVENTS; i++) {
217 ADDTOINPUTS(td[i])
218 strcat(buf, getdesc[td[i]]);
219 if(td[i])
220 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
221 else
222 expmsg[i].message = 0;
223 }
224 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
225 ;
226 assert( evtctr <= MAXKEYEVENTS );
227 assert( evtctr == SendInput(evtctr, &inputs[0], sizeof(INPUT)));
228 i = 0;
229 trace("======== key stroke sequence #%d: %s =============\n",
230 seqnr + 1, buf);
231 while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
232 trace("message[%d] %-15s wParam %04x lParam %08lx time %lx\n", i,
233 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
234 if( i < kmctr ) {
235 ok( msg.message == expmsg[i].message &&
236 msg.wParam == expmsg[i].wParam &&
237 msg.lParam == expmsg[i].lParam,
238 "wrong message! expected:\n"
239 "message[%d] %-15s wParam %04x lParam %08lx",i,
240 MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
241 expmsg[i].wParam, expmsg[i].lParam );
242 }
243 i++;
244 }
245 trace("%d messages retrieved\n", i);
246 ok( i == kmctr, "message count is wrong: got %d expected: %d", i, kmctr);
247 }
248
249 /* test all combinations of the specified key events */
250 void TestASet( HWND hWnd, int nrkev, KEV kevdwn[], KEV kevup[] )
251 {
252 int i,j,k,l,m,n;
253 static int count=0;
254 KEV kbuf[MAXKEYEVENTS];
255 assert( nrkev==2 || nrkev==3);
256 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
257 /* two keys involved gives 4 test cases */
258 if(nrkev==2) {
259 for(i=0;i<nrkev;i++) {
260 for(j=0;j<nrkev;j++) {
261 kbuf[0] = kevdwn[i];
262 kbuf[1] = kevdwn[1-i];
263 kbuf[2] = kevup[j];
264 kbuf[3] = kevup[1-j];
265 do_test( hWnd, count++, kbuf);
266 }
267 }
268 }
269 /* three keys involved gives 36 test cases */
270 if(nrkev==3){
271 for(i=0;i<nrkev;i++){
272 for(j=0;j<nrkev;j++){
273 if(j==i) continue;
274 for(k=0;k<nrkev;k++){
275 if(k==i || k==j) continue;
276 for(l=0;l<nrkev;l++){
277 for(m=0;m<nrkev;m++){
278 if(m==l) continue;
279 for(n=0;n<nrkev;n++){
280 if(n==l ||n==m) continue;
281 kbuf[0] = kevdwn[i];
282 kbuf[1] = kevdwn[j];
283 kbuf[2] = kevdwn[k];
284 kbuf[3] = kevup[l];
285 kbuf[4] = kevup[m];
286 kbuf[5] = kevup[n];
287 do_test( hWnd, count++, kbuf);
288 }
289 }
290 }
291 }
292 }
293 }
294 }
295 }
296
297 /* test each set specified in the global testkeyset array */
298 void TestSysKeys( HWND hWnd)
299 {
300 int i;
301 for(i=0; testkeyset[i].nrkev;i++)
302 TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
303 testkeyset[i].keyup);
304
305 }
306
307 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
308 LPARAM lParam )
309 {
310 switch (msg) {
311 case WM_SETFOCUS:
312 /* window has focus, now do the test */
313 if( hWnd == hWndTest) TestSysKeys( hWnd);
314 /* finished :-) */
315 DestroyWindow(hWnd);
316 break;
317
318 case WM_DESTROY:
319 PostQuitMessage( 0 );
320 break;
321
322 default:
323 return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
324 }
325
326 return 0;
327 }
328
329 START_TEST(input)
330 {
331 MSG msg;
332 WNDCLASSA wclass;
333 HANDLE hInstance = GetModuleHandleA( NULL );
334
335 wclass.lpszClassName = "InputSysKeyTestClass";
336 wclass.style = CS_HREDRAW | CS_VREDRAW;
337 wclass.lpfnWndProc = (WNDPROC)WndProc;
338 wclass.hInstance = hInstance;
339 wclass.hIcon = LoadIconA( 0, (LPSTR)IDI_APPLICATION );
340 wclass.hCursor = LoadCursorA( NULL, IDC_ARROW);
341 wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1);
342 wclass.lpszMenuName = 0;
343 wclass.cbClsExtra = 0;
344 wclass.cbWndExtra = 0;
345 assert (RegisterClassA( &wclass ));
346 /* create the test window that will receive the keystrokes */
347 assert ( hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
348 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
349 NULL, NULL, hInstance, NULL) );
350 ShowWindow( hWndTest, SW_SHOW);
351 UpdateWindow( hWndTest);
352 /* message loop */
353 while( GetMessageA( &msg, 0, 0, 0 )) {
354 TranslateMessage( &msg );
355 DispatchMessageA( &msg );
356 }
357 }