sync to trunk head (37853) (except rbuild changes)
[reactos.git] / rostests / winetests / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 accomplished 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 #include <stdarg.h>
48 #include <assert.h>
49
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wine/winuser.h"
53
54 #include "wine/test.h"
55
56 /* globals */
57 static HWND hWndTest;
58 static long timetag = 0x10000000;
59
60 static UINT (WINAPI *pSendInput) (UINT, INPUT*, size_t);
61 static int (WINAPI *pGetMouseMovePointsEx) (UINT, LPMOUSEMOVEPOINT, LPMOUSEMOVEPOINT, int, DWORD);
62
63 #define MAXKEYEVENTS 6
64 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
65 and only one message */
66
67 /* keyboard message names, sorted as their value */
68 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
69 "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
70
71 /* keyevents, add more as needed */
72 typedef enum KEVtag
73 { ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
74 /* matching VK's */
75 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
76 /* matching scan codes */
77 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
78 /* matching updown events */
79 static const int GETFLAGS[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
80 /* matching descriptions */
81 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
82
83 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
84 typedef struct
85 {
86 DWORD type;
87 union
88 {
89 MOUSEINPUT mi;
90 KEYBDINPUT ki;
91 HARDWAREINPUT hi;
92 } u;
93 } TEST_INPUT;
94
95 #define ADDTOINPUTS(kev) \
96 inputs[evtctr].type = INPUT_KEYBOARD; \
97 ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
98 ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
99 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETFLAGS[ kev]; \
100 ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
101 ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
102 if( kev) evtctr++;
103
104 typedef struct {
105 UINT message;
106 WPARAM wParam;
107 LPARAM lParam;
108 } KMSG;
109
110 /*******************************************
111 * add new test sets here
112 * the software will make all combinations of the
113 * keyevent defined here
114 */
115 static const struct {
116 int nrkev;
117 KEV keydwn[MAXKEYEVENTS];
118 KEV keyup[MAXKEYEVENTS];
119 } testkeyset[]= {
120 { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
121 { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
122 { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
123 { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
124 { 0 } /* mark the end */
125 };
126
127 /**********************adapted from input.c **********************************/
128
129 static BYTE InputKeyStateTable[256];
130 static BYTE AsyncKeyStateTable[256];
131 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
132 or a WM_KEYUP message */
133
134 static void init_function_pointers(void)
135 {
136 HMODULE hdll = GetModuleHandleA("user32");
137
138 #define GET_PROC(func) \
139 p ## func = (void*)GetProcAddress(hdll, #func); \
140 if(!p ## func) \
141 trace("GetProcAddress(%s) failed\n", #func);
142
143 GET_PROC(SendInput)
144 GET_PROC(GetMouseMovePointsEx)
145
146 #undef GET_PROC
147 }
148
149 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
150 {
151 UINT message;
152 int VKey = GETVKEY[kev];
153 WORD flags;
154
155 flags = LOBYTE(GETSCAN[kev]);
156 if (GETFLAGS[kev] & KEYEVENTF_EXTENDEDKEY) flags |= KF_EXTENDED;
157
158 if (GETFLAGS[kev] & KEYEVENTF_KEYUP )
159 {
160 message = WM_KEYUP;
161 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
162 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
163 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
164 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
165 (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
166 message = WM_SYSKEYUP;
167 TrackSysKey = 0;
168 }
169 InputKeyStateTable[VKey] &= ~0x80;
170 flags |= KF_REPEAT | KF_UP;
171 }
172 else
173 {
174 if (InputKeyStateTable[VKey] & 0x80) flags |= KF_REPEAT;
175 if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
176 InputKeyStateTable[VKey] |= 0x80;
177 AsyncKeyStateTable[VKey] |= 0x80;
178
179 message = WM_KEYDOWN;
180 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
181 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
182 message = WM_SYSKEYDOWN;
183 TrackSysKey = VKey;
184 }
185 }
186
187 if (InputKeyStateTable[VK_MENU] & 0x80) flags |= KF_ALTDOWN;
188
189 if( plParam) *plParam = MAKELPARAM( 1, flags );
190 if( pwParam) *pwParam = VKey;
191 return message;
192 }
193
194 /****************************** end copy input.c ****************************/
195
196 /*
197 * . prepare the keyevents for SendInputs
198 * . calculate the "expected" messages
199 * . Send the events to our window
200 * . retrieve the messages from the input queue
201 * . verify
202 */
203 static void do_test( HWND hwnd, int seqnr, const KEV td[] )
204 {
205 INPUT inputs[MAXKEYEVENTS];
206 KMSG expmsg[MAXKEYEVENTS];
207 MSG msg;
208 char buf[100];
209 UINT evtctr=0;
210 int kmctr, i;
211
212 buf[0]='\0';
213 TrackSysKey=0; /* see input.c */
214 for( i = 0; i < MAXKEYEVENTS; i++) {
215 ADDTOINPUTS(td[i])
216 strcat(buf, getdesc[td[i]]);
217 if(td[i])
218 expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
219 else
220 expmsg[i].message = 0;
221 }
222 for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
223 ;
224 assert( evtctr <= MAXKEYEVENTS );
225 assert( evtctr == pSendInput(evtctr, &inputs[0], sizeof(INPUT)));
226 i = 0;
227 if (winetest_debug > 1)
228 trace("======== key stroke sequence #%d: %s =============\n",
229 seqnr + 1, buf);
230 while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
231 if (winetest_debug > 1)
232 trace("message[%d] %-15s wParam %04lx lParam %08lx time %x\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 %04lx lParam %08lx\n",i,
240 MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
241 expmsg[i].wParam, expmsg[i].lParam );
242 }
243 i++;
244 }
245 if (winetest_debug > 1)
246 trace("%d messages retrieved\n", i);
247 ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
248 }
249
250 /* test all combinations of the specified key events */
251 static void TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
252 {
253 int i,j,k,l,m,n;
254 static int count=0;
255 KEV kbuf[MAXKEYEVENTS];
256 assert( nrkev==2 || nrkev==3);
257 for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
258 /* two keys involved gives 4 test cases */
259 if(nrkev==2) {
260 for(i=0;i<nrkev;i++) {
261 for(j=0;j<nrkev;j++) {
262 kbuf[0] = kevdwn[i];
263 kbuf[1] = kevdwn[1-i];
264 kbuf[2] = kevup[j];
265 kbuf[3] = kevup[1-j];
266 do_test( hWnd, count++, kbuf);
267 }
268 }
269 }
270 /* three keys involved gives 36 test cases */
271 if(nrkev==3){
272 for(i=0;i<nrkev;i++){
273 for(j=0;j<nrkev;j++){
274 if(j==i) continue;
275 for(k=0;k<nrkev;k++){
276 if(k==i || k==j) continue;
277 for(l=0;l<nrkev;l++){
278 for(m=0;m<nrkev;m++){
279 if(m==l) continue;
280 for(n=0;n<nrkev;n++){
281 if(n==l ||n==m) continue;
282 kbuf[0] = kevdwn[i];
283 kbuf[1] = kevdwn[j];
284 kbuf[2] = kevdwn[k];
285 kbuf[3] = kevup[l];
286 kbuf[4] = kevup[m];
287 kbuf[5] = kevup[n];
288 do_test( hWnd, count++, kbuf);
289 }
290 }
291 }
292 }
293 }
294 }
295 }
296 }
297
298 /* test each set specified in the global testkeyset array */
299 static void TestSysKeys( HWND hWnd)
300 {
301 int i;
302 for(i=0; testkeyset[i].nrkev;i++)
303 TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
304 testkeyset[i].keyup);
305 }
306
307 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
308 LPARAM lParam )
309 {
310 switch (msg) {
311 case WM_USER:
312 SetFocus(hWnd);
313 /* window has focus, now do the test */
314 if( hWnd == hWndTest) TestSysKeys( hWnd);
315 /* finished :-) */
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 static void test_Input_whitebox(void)
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;
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
353 /* flush pending messages */
354 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
355
356 SendMessageA(hWndTest, WM_USER, 0, 0);
357 DestroyWindow(hWndTest);
358 }
359
360 static void empty_message_queue(void) {
361 MSG msg;
362 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
363 TranslateMessage(&msg);
364 DispatchMessage(&msg);
365 }
366 }
367
368 struct transition_s {
369 WORD wVk;
370 BYTE before_state;
371 };
372
373 typedef enum {
374 sent=0x1,
375 posted=0x2,
376 parent=0x4,
377 wparam=0x8,
378 lparam=0x10,
379 defwinproc=0x20,
380 beginpaint=0x40,
381 optional=0x80,
382 hook=0x100,
383 winevent_hook=0x200
384 } msg_flags_t;
385
386 struct message {
387 UINT message; /* the WM_* code */
388 msg_flags_t flags; /* message props */
389 WPARAM wParam; /* expected value of wParam */
390 LPARAM lParam; /* expected value of lParam */
391 };
392
393 struct sendinput_test_s {
394 WORD wVk;
395 DWORD dwFlags;
396 BOOL _todo_wine;
397 struct transition_s expected_transitions[MAXKEYEVENTS+1];
398 struct message expected_messages[MAXKEYMESSAGES+1];
399 } sendinput_test[] = {
400 /* test ALT+F */
401 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
402 {{WM_SYSKEYDOWN, hook|wparam, VK_LMENU}, {WM_SYSKEYDOWN}, {0}}},
403 {'F', 0, 0, {{'F', 0x00}, {0}},
404 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN},
405 {WM_SYSCHAR},
406 {WM_SYSCOMMAND}, {0}}},
407 {'F', KEYEVENTF_KEYUP, 0, {{'F', 0x80}, {0}},
408 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
409 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
410 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
411
412 /* test CTRL+O */
413 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
414 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
415 {'O', 0, 0, {{'O', 0x00}, {0}},
416 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
417 {'O', KEYEVENTF_KEYUP, 0, {{'O', 0x80}, {0}},
418 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
419 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
420 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
421
422 /* test ALT+CTRL+X */
423 {VK_LMENU, 0, 0, {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
424 {{WM_SYSKEYDOWN, hook}, {WM_SYSKEYDOWN}, {0}}},
425 {VK_LCONTROL, 0, 0, {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
426 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
427 {'X', 0, 0, {{'X', 0x00}, {0}},
428 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
429 {'X', KEYEVENTF_KEYUP, 0, {{'X', 0x80}, {0}},
430 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
431 {VK_LCONTROL, KEYEVENTF_KEYUP, 0, {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
432 {{WM_SYSKEYUP, hook}, {WM_SYSKEYUP}, {0}}},
433 {VK_LMENU, KEYEVENTF_KEYUP, 0, {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
434 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
435
436 /* test SHIFT+A */
437 {VK_LSHIFT, 0, 0, {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
438 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {0}}},
439 {'A', 0, 0, {{'A', 0x00}, {0}},
440 {{WM_KEYDOWN, hook}, {WM_KEYDOWN}, {WM_CHAR}, {0}}},
441 {'A', KEYEVENTF_KEYUP, 0, {{'A', 0x80}, {0}},
442 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
443 {VK_LSHIFT, KEYEVENTF_KEYUP, 0, {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
444 {{WM_KEYUP, hook}, {WM_KEYUP}, {0}}},
445 /* test L-SHIFT & R-SHIFT: */
446 /* RSHIFT == LSHIFT */
447 {VK_RSHIFT, 0, 0,
448 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
449 {{WM_KEYDOWN, hook|wparam, VK_RSHIFT},
450 {WM_KEYDOWN}, {0}}},
451 {VK_RSHIFT, KEYEVENTF_KEYUP, 0,
452 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
453 {{WM_KEYUP, hook, hook|wparam, VK_RSHIFT},
454 {WM_KEYUP}, {0}}},
455
456 /* LSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
457 {VK_LSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
458 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
459 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, LLKHF_EXTENDED},
460 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
461 {VK_LSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
462 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
463 {{WM_KEYUP, hook|wparam|lparam, VK_LSHIFT, LLKHF_UP|LLKHF_EXTENDED},
464 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
465 /* RSHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
466 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
467 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
468 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
469 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
470 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
471 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
472 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
473 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
474 /* SHIFT == LSHIFT */
475 {VK_SHIFT, 0, 0,
476 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
477 {{WM_KEYDOWN, hook|wparam|lparam, VK_SHIFT, 0},
478 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
479 {VK_SHIFT, KEYEVENTF_KEYUP, 0,
480 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
481 {{WM_KEYUP, hook|wparam|lparam, VK_SHIFT, LLKHF_UP},
482 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
483 /* SHIFT | KEYEVENTF_EXTENDEDKEY == RSHIFT */
484 {VK_SHIFT, KEYEVENTF_EXTENDEDKEY, 0,
485 {{VK_SHIFT, 0x00}, {VK_RSHIFT, 0x00}, {0}},
486 {{WM_KEYDOWN, hook|wparam|lparam, VK_SHIFT, LLKHF_EXTENDED},
487 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
488 {VK_SHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
489 {{VK_SHIFT, 0x80}, {VK_RSHIFT, 0x80}, {0}},
490 {{WM_KEYUP, hook|wparam|lparam, VK_SHIFT, LLKHF_UP|LLKHF_EXTENDED},
491 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
492
493 /* test L-CONTROL & R-CONTROL: */
494 /* RCONTROL == LCONTROL */
495 {VK_RCONTROL, 0, 0,
496 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
497 {{WM_KEYDOWN, hook|wparam, VK_RCONTROL},
498 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
499 {VK_RCONTROL, KEYEVENTF_KEYUP, 0,
500 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
501 {{WM_KEYUP, hook|wparam, VK_RCONTROL},
502 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
503 /* LCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
504 {VK_LCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
505 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
506 {{WM_KEYDOWN, hook|wparam|lparam, VK_LCONTROL, LLKHF_EXTENDED},
507 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
508 {VK_LCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
509 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
510 {{WM_KEYUP, hook|wparam|lparam, VK_LCONTROL, LLKHF_UP|LLKHF_EXTENDED},
511 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
512 /* RCONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
513 {VK_RCONTROL, KEYEVENTF_EXTENDEDKEY, 0,
514 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
515 {{WM_KEYDOWN, hook|wparam|lparam, VK_RCONTROL, LLKHF_EXTENDED},
516 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
517 {VK_RCONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
518 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
519 {{WM_KEYUP, hook|wparam|lparam, VK_RCONTROL, LLKHF_UP|LLKHF_EXTENDED},
520 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
521 /* CONTROL == LCONTROL */
522 {VK_CONTROL, 0, 0,
523 {{VK_CONTROL, 0x00}, {VK_LCONTROL, 0x00}, {0}},
524 {{WM_KEYDOWN, hook|wparam, VK_CONTROL},
525 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, 0}, {0}}},
526 {VK_CONTROL, KEYEVENTF_KEYUP, 0,
527 {{VK_CONTROL, 0x80}, {VK_LCONTROL, 0x80}, {0}},
528 {{WM_KEYUP, hook|wparam, VK_CONTROL},
529 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP}, {0}}},
530 /* CONTROL | KEYEVENTF_EXTENDEDKEY == RCONTROL */
531 {VK_CONTROL, KEYEVENTF_EXTENDEDKEY, 0,
532 {{VK_CONTROL, 0x00}, {VK_RCONTROL, 0x00}, {0}},
533 {{WM_KEYDOWN, hook|wparam|lparam, VK_CONTROL, LLKHF_EXTENDED},
534 {WM_KEYDOWN, wparam|lparam, VK_CONTROL, KF_EXTENDED}, {0}}},
535 {VK_CONTROL, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
536 {{VK_CONTROL, 0x80}, {VK_RCONTROL, 0x80}, {0}},
537 {{WM_KEYUP, hook|wparam|lparam, VK_CONTROL, LLKHF_UP|LLKHF_EXTENDED},
538 {WM_KEYUP, wparam|lparam, VK_CONTROL, KF_UP|KF_EXTENDED}, {0}}},
539
540 /* test L-MENU & R-MENU: */
541 /* RMENU == LMENU */
542 {VK_RMENU, 0, 0,
543 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
544 {{WM_SYSKEYDOWN, hook|wparam, VK_RMENU},
545 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
546 {VK_RMENU, KEYEVENTF_KEYUP, 1,
547 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
548 {{WM_KEYUP, hook|wparam, VK_RMENU},
549 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
550 {WM_SYSCOMMAND}, {0}}},
551 /* LMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
552 {VK_LMENU, KEYEVENTF_EXTENDEDKEY, 0,
553 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
554 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_LMENU, LLKHF_EXTENDED},
555 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
556 {VK_LMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
557 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
558 {{WM_KEYUP, hook|wparam|lparam, VK_LMENU, LLKHF_UP|LLKHF_EXTENDED},
559 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
560 {WM_SYSCOMMAND}, {0}}},
561 /* RMENU | KEYEVENTF_EXTENDEDKEY == RMENU */
562 {VK_RMENU, KEYEVENTF_EXTENDEDKEY, 0,
563 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
564 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_RMENU, LLKHF_EXTENDED},
565 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
566 {VK_RMENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
567 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
568 {{WM_KEYUP, hook|wparam|lparam, VK_RMENU, LLKHF_UP|LLKHF_EXTENDED},
569 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
570 {WM_SYSCOMMAND}, {0}}},
571 /* MENU == LMENU */
572 {VK_MENU, 0, 0,
573 {{VK_MENU, 0x00}, {VK_LMENU, 0x00}, {0}},
574 {{WM_SYSKEYDOWN, hook|wparam, VK_MENU},
575 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0}, {0}}},
576 {VK_MENU, KEYEVENTF_KEYUP, 1,
577 {{VK_MENU, 0x80}, {VK_LMENU, 0x80}, {0}},
578 {{WM_KEYUP, hook|wparam, VK_MENU},
579 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP},
580 {WM_SYSCOMMAND}, {0}}},
581 /* MENU | KEYEVENTF_EXTENDEDKEY == RMENU */
582 {VK_MENU, KEYEVENTF_EXTENDEDKEY, 0,
583 {{VK_MENU, 0x00}, {VK_RMENU, 0x00}, {0}},
584 {{WM_SYSKEYDOWN, hook|wparam|lparam, VK_MENU, LLKHF_EXTENDED},
585 {WM_SYSKEYDOWN, wparam|lparam, VK_MENU, KF_EXTENDED}, {0}}},
586 {VK_MENU, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 1,
587 {{VK_MENU, 0x80}, {VK_RMENU, 0x80}, {0}},
588 {{WM_KEYUP, hook|wparam|lparam, VK_MENU, LLKHF_UP|LLKHF_EXTENDED},
589 {WM_SYSKEYUP, wparam|lparam, VK_MENU, KF_UP|KF_EXTENDED},
590 {WM_SYSCOMMAND}, {0}}},
591
592 /* test LSHIFT & RSHIFT */
593 {VK_LSHIFT, 0, 0,
594 {{VK_SHIFT, 0x00}, {VK_LSHIFT, 0x00}, {0}},
595 {{WM_KEYDOWN, hook|wparam|lparam, VK_LSHIFT, 0},
596 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
597 {VK_RSHIFT, KEYEVENTF_EXTENDEDKEY, 0,
598 {{VK_RSHIFT, 0x00}, {0}},
599 {{WM_KEYDOWN, hook|wparam|lparam, VK_RSHIFT, LLKHF_EXTENDED},
600 {WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0}, {0}}},
601 {VK_RSHIFT, KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY, 0,
602 {{VK_RSHIFT, 0x80}, {0}},
603 {{WM_KEYUP, hook|wparam|lparam, VK_RSHIFT, LLKHF_UP|LLKHF_EXTENDED},
604 {WM_KEYUP, optional}, {0}}},
605 {VK_LSHIFT, KEYEVENTF_KEYUP, 0,
606 {{VK_SHIFT, 0x80}, {VK_LSHIFT, 0x80}, {0}},
607 {{WM_KEYUP, hook|wparam, VK_LSHIFT},
608 {WM_KEYUP, wparam|lparam, VK_SHIFT, KF_UP}, {0}}},
609
610 {0, 0, 0, {{0}}, {{0}}} /* end */
611 };
612
613 static struct message sent_messages[MAXKEYMESSAGES];
614 static UINT sent_messages_cnt;
615
616 /* Verify that only specified key state transitions occur */
617 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, struct sendinput_test_s *test)
618 {
619 int i, failcount = 0;
620 struct transition_s *t = test->expected_transitions;
621 UINT actual_cnt = 0;
622 const struct message *expected = test->expected_messages;
623
624 while (t->wVk) {
625 int matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
626 && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
627
628 if (!matched && test->_todo_wine)
629 {
630 failcount++;
631 todo_wine {
632 ok(matched, "%02d: %02x from %02x -> %02x "
633 "instead of %02x -> %02x\n", id, t->wVk,
634 ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
635 ~t->before_state&0x80);
636 }
637 } else {
638 ok(matched, "%02d: %02x from %02x -> %02x "
639 "instead of %02x -> %02x\n", id, t->wVk,
640 ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
641 ~t->before_state&0x80);
642 }
643 ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
644 t++;
645 }
646 for (i = 0; i < 256; i++)
647 if (ks2[i] != ks1[i] && test->_todo_wine)
648 {
649 failcount++;
650 todo_wine
651 ok(FALSE, "%02d: %02x from %02x -> %02x unexpected\n", id, i, ks1[i], ks2[i]);
652 }
653 else
654 ok(ks2[i] == ks1[i], "%02d: %02x from %02x -> %02x unexpected\n",
655 id, i, ks1[i], ks2[i]);
656
657 while (expected->message && actual_cnt < sent_messages_cnt)
658 {
659 const struct message *actual = &sent_messages[actual_cnt];
660
661 if (expected->message == actual->message)
662 {
663 ok((expected->flags & hook) == (actual->flags & hook),
664 "%x/%x: the msg 0x%04x should have been sent by a hook\n",
665 test->wVk, test->dwFlags, expected->message);
666
667 if (expected->flags & wparam)
668 {
669 if (expected->wParam != actual->wParam && test->_todo_wine)
670 {
671 failcount++;
672 todo_wine
673 ok(FALSE, "%x/%x: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
674 test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
675 }
676 else
677 ok(expected->wParam == actual->wParam,
678 "%x/%x: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
679 test->wVk, test->dwFlags, expected->message, expected->wParam, actual->wParam);
680 }
681 if (expected->flags & lparam)
682 {
683 if (expected->lParam != actual->lParam && test->_todo_wine)
684 {
685 failcount++;
686 todo_wine
687 ok(FALSE, "%x/%x: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
688 test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
689 }
690 else
691 ok(expected->lParam == actual->lParam,
692 "%x/%x: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
693 test->wVk, test->dwFlags, expected->message, expected->lParam, actual->lParam);
694 }
695 }
696 else if (expected->flags & optional)
697 {
698 expected++;
699 continue;
700 }
701 else if (test->_todo_wine)
702 {
703 failcount++;
704 todo_wine
705 ok(FALSE,
706 "%x/%x: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
707 test->wVk, test->dwFlags, expected->message, actual->message);
708 }
709 else
710 ok(FALSE,
711 "%x/%x: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
712 test->wVk, test->dwFlags, expected->message, actual->message);
713
714 actual_cnt++;
715 expected++;
716 }
717 /* skip all optional trailing messages */
718 while (expected->message && (expected->flags & optional))
719 expected++;
720
721
722 if (expected->message || actual_cnt < sent_messages_cnt)
723 {
724 if (test->_todo_wine)
725 {
726 failcount++;
727 todo_wine
728 ok(FALSE, "%x/%x: the msg sequence is not complete: expected %04x - actual %04x\n",
729 test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
730 }
731 else
732 ok(FALSE, "%x/%x: the msg sequence is not complete: expected %04x - actual %04x\n",
733 test->wVk, test->dwFlags, expected->message, sent_messages[actual_cnt].message);
734 }
735
736 if( test->_todo_wine && !failcount) /* succeeded yet marked todo */
737 todo_wine
738 ok(TRUE, "%x/%x: marked \"todo_wine\" but succeeds\n", test->wVk, test->dwFlags);
739
740 sent_messages_cnt = 0;
741 }
742
743 /* WndProc2 checks that we get at least the messages specified */
744 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
745 LPARAM lParam)
746 {
747 if (winetest_debug > 1) trace("MSG: %8x W:%8lx L:%8lx\n", Msg, wParam, lParam);
748
749 if (Msg != WM_PAINT &&
750 Msg != WM_NCPAINT &&
751 Msg != WM_SYNCPAINT &&
752 Msg != WM_ERASEBKGND &&
753 Msg != WM_NCHITTEST &&
754 Msg != WM_GETTEXT &&
755 Msg != WM_GETICON &&
756 Msg != WM_DEVICECHANGE)
757 {
758 sent_messages[sent_messages_cnt].message = Msg;
759 sent_messages[sent_messages_cnt].flags = 0;
760 sent_messages[sent_messages_cnt].wParam = wParam;
761 sent_messages[sent_messages_cnt++].lParam = HIWORD(lParam) & (KF_UP|KF_EXTENDED);
762 }
763 return DefWindowProc(hWnd, Msg, wParam, lParam);
764 }
765
766 static LRESULT CALLBACK hook_proc(int code, WPARAM wparam, LPARAM lparam)
767 {
768 KBDLLHOOKSTRUCT *hook_info = (KBDLLHOOKSTRUCT *)lparam;
769
770 if (code == HC_ACTION)
771 {
772 sent_messages[sent_messages_cnt].message = wparam;
773 sent_messages[sent_messages_cnt].flags = hook;
774 sent_messages[sent_messages_cnt].wParam = hook_info->vkCode;
775 sent_messages[sent_messages_cnt++].lParam = hook_info->flags & (LLKHF_UP|LLKHF_EXTENDED);
776
777 if(0) /* For some reason not stable on Wine */
778 {
779 if (wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN)
780 ok(!(GetAsyncKeyState(hook_info->vkCode) & 0x8000), "key %x should be up\n", hook_info->vkCode);
781 else if (wparam == WM_KEYUP || wparam == WM_SYSKEYUP)
782 ok(GetAsyncKeyState(hook_info->vkCode) & 0x8000, "key %x should be down\n", hook_info->vkCode);
783 }
784
785 if (winetest_debug > 1)
786 trace("Hook: w=%lx vk:%8x sc:%8x fl:%8x %lx\n", wparam,
787 hook_info->vkCode, hook_info->scanCode, hook_info->flags, hook_info->dwExtraInfo);
788 }
789 return CallNextHookEx( 0, code, wparam, lparam );
790 }
791 static void test_Input_blackbox(void)
792 {
793 TEST_INPUT i;
794 int ii;
795 BYTE ks1[256], ks2[256];
796 LONG_PTR prevWndProc;
797 HWND window;
798 HHOOK hook;
799
800 if (!pSendInput)
801 {
802 skip("SendInput is not available\n");
803 return;
804 }
805
806 window = CreateWindow("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
807 |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
808 NULL, NULL);
809 ok(window != NULL, "error: %d\n", (int) GetLastError());
810
811 hook = SetWindowsHookExA(WH_KEYBOARD_LL, hook_proc, GetModuleHandleA( NULL ), 0);
812
813 /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
814 * key state set by SendInput(). */
815 empty_message_queue();
816
817 prevWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
818 ok(prevWndProc != 0 || (prevWndProc == 0 && GetLastError() == 0),
819 "error: %d\n", (int) GetLastError());
820
821 i.type = INPUT_KEYBOARD;
822 i.u.ki.time = 0;
823 i.u.ki.dwExtraInfo = 0;
824
825 for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
826 ii++) {
827 GetKeyboardState(ks1);
828 i.u.ki.wScan = ii+1 /* useful for debugging */;
829 i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
830 i.u.ki.wVk = sendinput_test[ii].wVk;
831 pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
832 empty_message_queue();
833 GetKeyboardState(ks2);
834 compare_and_check(ii, ks1, ks2, &sendinput_test[ii]);
835 }
836
837 empty_message_queue();
838 DestroyWindow(window);
839 UnhookWindowsHookEx(hook);
840 }
841
842 static void test_keynames(void)
843 {
844 int i, len;
845 char buff[256];
846
847 for (i = 0; i < 512; i++)
848 {
849 strcpy(buff, "----");
850 len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
851 ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
852 }
853 }
854
855 static POINT pt_old, pt_new;
856 static BOOL clipped;
857 #define STEP 20
858
859 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
860 {
861 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
862 POINT pt, pt1;
863
864 if (code == HC_ACTION)
865 {
866 /* This is our new cursor position */
867 pt_new = hook->pt;
868 /* Should return previous position */
869 GetCursorPos(&pt);
870 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
871
872 /* Should set new position until hook chain is finished. */
873 pt.x = pt_old.x + STEP;
874 pt.y = pt_old.y + STEP;
875 SetCursorPos(pt.x, pt.y);
876 GetCursorPos(&pt1);
877 if (clipped)
878 ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
879 else
880 ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
881 }
882 return CallNextHookEx( 0, code, wparam, lparam );
883 }
884
885 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
886 {
887 MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
888 POINT pt;
889
890 if (code == HC_ACTION)
891 {
892 ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
893 "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
894
895 /* Should match position set above */
896 GetCursorPos(&pt);
897 if (clipped)
898 ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
899 else
900 ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
901 }
902 return CallNextHookEx( 0, code, wparam, lparam );
903 }
904
905 static void test_mouse_ll_hook(void)
906 {
907 HWND hwnd;
908 HHOOK hook1, hook2;
909 POINT pt_org, pt;
910 RECT rc;
911
912 GetCursorPos(&pt_org);
913 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
914 10, 10, 200, 200, NULL, NULL, NULL, NULL);
915 SetCursorPos(100, 100);
916
917 hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0);
918 hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
919
920 GetCursorPos(&pt_old);
921 mouse_event(MOUSEEVENTF_MOVE, -STEP, 0, 0, 0);
922 GetCursorPos(&pt_old);
923 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
924 mouse_event(MOUSEEVENTF_MOVE, +STEP, 0, 0, 0);
925 GetCursorPos(&pt_old);
926 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
927 mouse_event(MOUSEEVENTF_MOVE, 0, -STEP, 0, 0);
928 GetCursorPos(&pt_old);
929 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
930 mouse_event(MOUSEEVENTF_MOVE, 0, +STEP, 0, 0);
931 GetCursorPos(&pt_old);
932 ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
933
934 SetRect(&rc, 50, 50, 151, 151);
935 ClipCursor(&rc);
936 clipped = TRUE;
937
938 SetCursorPos(40, 40);
939 GetCursorPos(&pt_old);
940 ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
941 SetCursorPos(160, 160);
942 GetCursorPos(&pt_old);
943 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
944 mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
945 GetCursorPos(&pt_old);
946 ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
947
948 clipped = FALSE;
949 pt_new.x = pt_new.y = 150;
950 ClipCursor(NULL);
951 UnhookWindowsHookEx(hook1);
952
953 /* Now check that mouse buttons do not change mouse position
954 if we don't have MOUSEEVENTF_MOVE flag specified. */
955
956 /* We reusing the same hook callback, so make it happy */
957 pt_old.x = pt_new.x - STEP;
958 pt_old.y = pt_new.y - STEP;
959 mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
960 GetCursorPos(&pt);
961 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
962 mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
963 GetCursorPos(&pt);
964 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
965
966 mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
967 GetCursorPos(&pt);
968 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
969 mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
970 GetCursorPos(&pt);
971 ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
972
973 UnhookWindowsHookEx(hook2);
974 DestroyWindow(hwnd);
975 SetCursorPos(pt_org.x, pt_org.y);
976 }
977
978 static void test_GetMouseMovePointsEx(void)
979 {
980 #define BUFLIM 64
981 #define MYERROR 0xdeadbeef
982 int count, retval;
983 MOUSEMOVEPOINT in;
984 MOUSEMOVEPOINT out[200];
985 POINT point;
986
987 /* Get a valid content for the input struct */
988 if(!GetCursorPos(&point)) {
989 skip("GetCursorPos() failed with error %u\n", GetLastError());
990 return;
991 }
992 memset(&in, 0, sizeof(MOUSEMOVEPOINT));
993 in.x = point.x;
994 in.y = point.y;
995
996 /* test first parameter
997 * everything different than sizeof(MOUSEMOVEPOINT)
998 * is expected to fail with ERROR_INVALID_PARAMETER
999 */
1000 SetLastError(MYERROR);
1001 retval = pGetMouseMovePointsEx(0, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1002 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1003 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1004 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1005
1006 SetLastError(MYERROR);
1007 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1008 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1009 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1010 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1011
1012 SetLastError(MYERROR);
1013 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)+1, &in, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1014 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1015 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1016 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1017
1018 /* test second and third parameter
1019 */
1020 SetLastError(MYERROR);
1021 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1022 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1023 ok(ERROR_NOACCESS == GetLastError(),
1024 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1025
1026 SetLastError(MYERROR);
1027 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1028 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1029 ok(ERROR_NOACCESS == GetLastError(),
1030 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1031
1032 SetLastError(MYERROR);
1033 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1034 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1035 ok(ERROR_NOACCESS == GetLastError(),
1036 "expected error ERROR_NOACCESS, got %u\n", GetLastError());
1037
1038 SetLastError(MYERROR);
1039 count = 0;
1040 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, count, GMMP_USE_DISPLAY_POINTS);
1041 todo_wine {
1042 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1043 ok(MYERROR == GetLastError(),
1044 "expected error %d, got %u\n", MYERROR, GetLastError());
1045 }
1046
1047 /* test fourth parameter
1048 * a value higher than 64 is expected to fail with ERROR_INVALID_PARAMETER
1049 */
1050 SetLastError(MYERROR);
1051 count = -1;
1052 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1053 ok(retval == count, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1054 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1055 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1056
1057 SetLastError(MYERROR);
1058 count = 0;
1059 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1060 todo_wine {
1061 ok(retval == count, "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1062 ok(MYERROR == GetLastError(),
1063 "expected error %d, got %u\n", MYERROR, GetLastError());
1064 }
1065
1066 SetLastError(MYERROR);
1067 count = BUFLIM;
1068 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, count, GMMP_USE_DISPLAY_POINTS);
1069 todo_wine {
1070 ok((0 <= retval) && (retval <= count), "expected GetMouseMovePointsEx to succeed, got %d\n", retval);
1071 ok(MYERROR == GetLastError(),
1072 "expected error %d, got %u\n", MYERROR, GetLastError());
1073 }
1074
1075 SetLastError(MYERROR);
1076 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1077 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1078 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1079 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1080
1081 /* it was not possible to force an error with the fifth parameter on win2k */
1082
1083 /* test combinations of wrong parameters to see which error wins */
1084 SetLastError(MYERROR);
1085 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, NULL, out, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1086 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1087 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1088 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1089
1090 SetLastError(MYERROR);
1091 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT)-1, &in, NULL, BUFLIM, GMMP_USE_DISPLAY_POINTS);
1092 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1093 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1094 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1095
1096 SetLastError(MYERROR);
1097 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), NULL, out, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1098 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1099 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1100 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1101
1102 SetLastError(MYERROR);
1103 retval = pGetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &in, NULL, BUFLIM+1, GMMP_USE_DISPLAY_POINTS);
1104 ok(retval == -1, "expected GetMouseMovePointsEx to fail, got %d\n", retval);
1105 ok(ERROR_INVALID_PARAMETER == GetLastError(),
1106 "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1107
1108 #undef BUFLIM
1109 #undef MYERROR
1110 }
1111
1112 static void test_key_map(void)
1113 {
1114 HKL kl = GetKeyboardLayout(0);
1115 UINT kL, kR, s, sL;
1116
1117 s = MapVirtualKeyEx(VK_SHIFT, MAPVK_VK_TO_VSC, kl);
1118 ok(s != 0, "MapVirtualKeyEx(VK_SHIFT) should return non-zero\n");
1119 sL = MapVirtualKeyEx(VK_LSHIFT, MAPVK_VK_TO_VSC, kl);
1120 ok(s == sL, "%x != %x\n", s, sL);
1121
1122 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK, kl);
1123 ok(kL == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kL);
1124 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK, kl);
1125 ok(kR == VK_SHIFT, "Scan code -> vKey = %x (not VK_SHIFT)\n", kR);
1126
1127 kL = MapVirtualKeyEx(0x2a, MAPVK_VSC_TO_VK_EX, kl);
1128 ok(kL == VK_LSHIFT, "Scan code -> vKey = %x (not VK_LSHIFT)\n", kL);
1129 kR = MapVirtualKeyEx(0x36, MAPVK_VSC_TO_VK_EX, kl);
1130 ok(kR == VK_RSHIFT, "Scan code -> vKey = %x (not VK_RSHIFT)\n", kR);
1131 }
1132
1133 START_TEST(input)
1134 {
1135 init_function_pointers();
1136
1137 if (!pSendInput)
1138 skip("SendInput is not available\n");
1139 else
1140 test_Input_whitebox();
1141
1142 test_Input_blackbox();
1143 test_keynames();
1144 test_mouse_ll_hook();
1145 test_key_map();
1146
1147 if(pGetMouseMovePointsEx)
1148 test_GetMouseMovePointsEx();
1149 else
1150 skip("GetMouseMovePointsEx is not available\n");
1151 }