mcicda: Exclude unused headers.
[wine] / dlls / user32 / tests / 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 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 #define _WIN32_WINNT 0x401
48
49 #include <stdarg.h>
50 #include <assert.h>
51
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winuser.h"
55
56 #include "wine/test.h"
57
58 /* globals */
59 static HWND hWndTest;
60 static long timetag = 0x10000000;
61
62 static UINT (WINAPI *ptr_SendInput) (UINT, INPUT*, size_t);
63
64 #define MAXKEYEVENTS 6
65 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
66                                        and only one message */
67
68 /* keyboard message names, sorted as their value */
69 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
70     "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
71
72 /* keyevents, add more as needed */
73 typedef enum KEVtag
74 {  ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
75 /* matching VK's */
76 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
77 /* matching scan codes */
78 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
79 /* matching updown events */
80 static const int GETUPDOWN[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
81 /* matching descripts */
82 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
83
84 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
85 typedef struct
86 {
87     DWORD type;
88     union
89     {
90         MOUSEINPUT      mi;
91         KEYBDINPUT      ki;
92         HARDWAREINPUT   hi;
93     } u;
94 } TEST_INPUT;
95
96 #define ADDTOINPUTS(kev) \
97 inputs[evtctr].type = INPUT_KEYBOARD; \
98     ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
99     ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
100     ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETUPDOWN[ kev]; \
101     ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
102     ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
103     if( kev) evtctr++;
104
105 typedef struct {
106     UINT    message;
107     WPARAM  wParam;
108     LPARAM  lParam;
109 } KMSG;
110
111 /*******************************************
112  * add new test sets here
113  * the software will make all combinations of the
114  * keyevent defined here
115  */
116 static const struct {
117     int nrkev;
118     KEV keydwn[MAXKEYEVENTS];
119     KEV keyup[MAXKEYEVENTS];
120 } testkeyset[]= {
121     { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
122     { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
123     { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
124     { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
125     { 0 } /* mark the end */
126 };
127
128 /**********************adapted from input.c **********************************/
129
130 static BYTE InputKeyStateTable[256];
131 static BYTE AsyncKeyStateTable[256];
132 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
133                          or a WM_KEYUP message */
134 typedef union
135 {
136     struct
137     {
138         unsigned long count : 16;
139         unsigned long code : 8;
140         unsigned long extended : 1;
141         unsigned long unused : 2;
142         unsigned long win_internal : 2;
143         unsigned long context : 1;
144         unsigned long previous : 1;
145         unsigned long transition : 1;
146     } lp1;
147     unsigned long lp2;
148 } KEYLP;
149
150 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
151 {
152     UINT message;
153     int VKey = GETVKEY[kev];
154     KEYLP keylp;
155
156     keylp.lp2 = 0;
157
158     keylp.lp1.count = 1;
159     keylp.lp1.code = GETSCAN[kev];
160     keylp.lp1.extended = 0 ;/*  FIXME (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0; */
161     keylp.lp1.win_internal = 0;
162
163     if (GETUPDOWN[kev] & KEYEVENTF_KEYUP )
164     {
165         message = WM_KEYUP;
166         if( (InputKeyStateTable[VK_MENU] & 0x80) && (
167                 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
168                  !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
169             if(  TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
170                     (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
171                 message = WM_SYSKEYUP;
172                 TrackSysKey = 0;
173         }
174         InputKeyStateTable[VKey] &= ~0x80;
175         keylp.lp1.previous = 1;
176         keylp.lp1.transition = 1;
177     }
178     else
179     {
180         keylp.lp1.previous = (InputKeyStateTable[VKey] & 0x80) != 0;
181         keylp.lp1.transition = 0;
182         if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
183         InputKeyStateTable[VKey] |= 0x80;
184         AsyncKeyStateTable[VKey] |= 0x80;
185
186         message = WM_KEYDOWN;
187         if( (InputKeyStateTable[VK_MENU] & 0x80) &&
188                 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
189             message = WM_SYSKEYDOWN;
190             TrackSysKey = VKey;
191         }
192     }
193
194     keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
195
196     if( plParam) *plParam = keylp.lp2;
197     if( pwParam) *pwParam = VKey;
198     return message;
199 }
200
201 /****************************** end copy input.c ****************************/
202
203 /*
204  * . prepare the keyevents for SendInputs
205  * . calculate the "expected" messages
206  * . Send the events to our window
207  * . retrieve the messages from the input queue
208  * . verify
209  */
210 static void do_test( HWND hwnd, int seqnr, const KEV td[] )
211 {
212     HMODULE module;
213     INPUT inputs[MAXKEYEVENTS];
214     KMSG expmsg[MAXKEYEVENTS];
215     MSG msg;
216     char buf[100];
217     UINT evtctr=0;
218     int kmctr, i;
219
220     module = GetModuleHandleA("user32");
221     ptr_SendInput = (void *)GetProcAddress(module, "SendInput");
222     if (!ptr_SendInput)
223     {
224         skip("skipping SendInput tests\n");
225         return;
226     }
227
228     buf[0]='\0';
229     TrackSysKey=0; /* see input.c */
230     for( i = 0; i < MAXKEYEVENTS; i++) {
231         ADDTOINPUTS(td[i])
232         strcat(buf, getdesc[td[i]]);
233         if(td[i])
234             expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
235         else
236             expmsg[i].message = 0;
237     }
238     for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
239         ;
240     assert( evtctr <= MAXKEYEVENTS );
241     assert( evtctr == ptr_SendInput(evtctr, &inputs[0], sizeof(INPUT)));
242     i = 0;
243     if (winetest_debug > 1)
244         trace("======== key stroke sequence #%d: %s =============\n",
245             seqnr + 1, buf);
246     while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
247         if (winetest_debug > 1)
248             trace("message[%d] %-15s wParam %04x lParam %08lx time %x\n", i,
249                   MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
250         if( i < kmctr ) {
251             ok( msg.message == expmsg[i].message &&
252                     msg.wParam == expmsg[i].wParam &&
253                     msg.lParam == expmsg[i].lParam,
254                     "wrong message! expected:\n"
255                     "message[%d] %-15s wParam %04x lParam %08lx\n",i,
256                     MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
257                     expmsg[i].wParam, expmsg[i].lParam );
258         }
259         i++;
260     }
261     if (winetest_debug > 1)
262         trace("%d messages retrieved\n", i);
263     ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
264 }
265
266 /* test all combinations of the specified key events */
267 static void TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
268 {
269     int i,j,k,l,m,n;
270     static int count=0;
271     KEV kbuf[MAXKEYEVENTS];
272     assert( nrkev==2 || nrkev==3);
273     for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
274     /* two keys involved gives 4 test cases */
275     if(nrkev==2) {
276         for(i=0;i<nrkev;i++) {
277             for(j=0;j<nrkev;j++) {
278                 kbuf[0] = kevdwn[i];
279                 kbuf[1] = kevdwn[1-i];
280                 kbuf[2] = kevup[j];
281                 kbuf[3] = kevup[1-j];
282                 do_test( hWnd, count++, kbuf);
283             }
284         }
285     }
286     /* three keys involved gives 36 test cases */
287     if(nrkev==3){
288         for(i=0;i<nrkev;i++){
289             for(j=0;j<nrkev;j++){
290                 if(j==i) continue;
291                 for(k=0;k<nrkev;k++){
292                     if(k==i || k==j) continue;
293                     for(l=0;l<nrkev;l++){
294                         for(m=0;m<nrkev;m++){
295                             if(m==l) continue;
296                             for(n=0;n<nrkev;n++){
297                                 if(n==l ||n==m) continue;
298                                 kbuf[0] = kevdwn[i];
299                                 kbuf[1] = kevdwn[j];
300                                 kbuf[2] = kevdwn[k];
301                                 kbuf[3] = kevup[l];
302                                 kbuf[4] = kevup[m];
303                                 kbuf[5] = kevup[n];
304                                 do_test( hWnd, count++, kbuf);
305                             }
306                         }
307                     }
308                 }
309             }
310         }
311     }
312 }
313
314 /* test each set specified in the global testkeyset array */
315 static void TestSysKeys( HWND hWnd)
316 {
317     int i;
318     for(i=0; testkeyset[i].nrkev;i++)
319         TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
320                 testkeyset[i].keyup);
321 }
322
323 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
324         LPARAM lParam )
325 {
326     switch (msg) {
327         case WM_USER:
328             SetFocus(hWnd);
329             /* window has focus, now do the test */
330             if( hWnd == hWndTest) TestSysKeys( hWnd);
331             /* finished :-) */
332             break;
333
334         case WM_DESTROY:
335             PostQuitMessage( 0 );
336             break;
337
338         default:
339             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
340     }
341
342     return 0;
343 }
344
345 static void test_Input_whitebox(void)
346 {
347     MSG msg;
348     WNDCLASSA  wclass;
349     HANDLE hInstance = GetModuleHandleA( NULL );
350
351     wclass.lpszClassName = "InputSysKeyTestClass";
352     wclass.style         = CS_HREDRAW | CS_VREDRAW;
353     wclass.lpfnWndProc   = WndProc;
354     wclass.hInstance     = hInstance;
355     wclass.hIcon         = LoadIconA( 0, (LPSTR)IDI_APPLICATION );
356     wclass.hCursor       = LoadCursorA( NULL, IDC_ARROW);
357     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1);
358     wclass.lpszMenuName = 0;
359     wclass.cbClsExtra    = 0;
360     wclass.cbWndExtra    = 0;
361     assert (RegisterClassA( &wclass ));
362     /* create the test window that will receive the keystrokes */
363     assert ( hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
364                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
365                 NULL, NULL, hInstance, NULL) );
366     ShowWindow( hWndTest, SW_SHOW);
367     UpdateWindow( hWndTest);
368
369     /* flush pending messages */
370     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
371
372     SendMessageA(hWndTest, WM_USER, 0, 0);
373     DestroyWindow(hWndTest);
374 }
375
376 static void empty_message_queue(void) {
377     MSG msg;
378     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
379         TranslateMessage(&msg);
380         DispatchMessage(&msg);
381     }
382 }
383
384 struct transition_s {
385     WORD wVk;
386     BYTE before_state;
387     BOOL _todo_wine;
388 };
389
390 struct sendinput_test_s {
391     WORD wVk;
392     DWORD dwFlags;
393     struct transition_s expected_transitions[MAXKEYEVENTS+1];
394     UINT expected_messages[MAXKEYMESSAGES+1];
395 } sendinput_test[] = {
396     /* test ALT+F */
397     {VK_LMENU, 0, {{VK_MENU, 0x00, 0}, {VK_LMENU, 0x00, 0}, {0}},
398         {WM_SYSKEYDOWN, 0}},
399     {'F', 0, {{'F', 0x00, 0}, {0}},
400         {WM_SYSKEYDOWN, WM_SYSCHAR, WM_SYSCOMMAND, 0}},
401     {'F', KEYEVENTF_KEYUP, {{'F', 0x80, 0}, {0}}, {WM_SYSKEYUP, 0}},
402     {VK_LMENU, KEYEVENTF_KEYUP, {{VK_MENU, 0x80, 0}, {VK_LMENU, 0x80, 0}, {0}},
403         {WM_KEYUP, 0}},
404
405     /* test CTRL+O */
406     {VK_LCONTROL, 0, {{VK_CONTROL, 0x00, 0}, {VK_LCONTROL, 0x00, 0}, {0}},
407         {WM_KEYDOWN, 0}},
408     {'O', 0, {{'O', 0x00, 0}, {0}}, {WM_KEYDOWN, WM_CHAR, 0}},
409     {'O', KEYEVENTF_KEYUP, {{'O', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
410     {VK_LCONTROL, KEYEVENTF_KEYUP,
411         {{VK_CONTROL, 0x80, 0}, {VK_LCONTROL, 0x80, 0}, {0}}, {WM_KEYUP, 0}},
412
413     /* test ALT+CTRL+X */
414     {VK_LMENU, 0, {{VK_MENU, 0x00, 0}, {VK_LMENU, 0x00, 0}, {0}},
415         {WM_SYSKEYDOWN, 0}},
416     {VK_LCONTROL, 0, {{VK_CONTROL, 0x00, 0}, {VK_LCONTROL, 0x00, 0}, {0}},
417         {WM_KEYDOWN, 0}},
418     {'X', 0, {{'X', 0x00, 0}, {0}}, {WM_KEYDOWN, 0}},
419     {'X', KEYEVENTF_KEYUP, {{'X', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
420     {VK_LCONTROL, KEYEVENTF_KEYUP,
421         {{VK_CONTROL, 0x80, 0}, {VK_LCONTROL, 0x80, 0}, {0}},
422         {WM_SYSKEYUP, 0}},
423     {VK_LMENU, KEYEVENTF_KEYUP, {{VK_MENU, 0x80, 0}, {VK_LMENU, 0x80, 0}, {0}},
424         {WM_KEYUP, 0}},
425
426     /* test SHIFT+A */
427     {VK_LSHIFT, 0,
428         {{VK_SHIFT, 0x00, 0}, {VK_LSHIFT, 0x00, 0}, {0}}, {WM_KEYDOWN, 0}},
429     {'A', 0, {{'A', 0x00, 0}, {0}}, {WM_KEYDOWN, WM_CHAR, 0}},
430     {'A', KEYEVENTF_KEYUP, {{'A', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
431     {VK_LSHIFT, KEYEVENTF_KEYUP,
432         {{VK_SHIFT, 0x80, 0}, {VK_LSHIFT, 0x80, 0}, {0}}, {WM_KEYUP, 0}},
433
434     {0, 0, {{0}}, {0}} /* end */
435 };
436
437 static struct sendinput_test_s *pTest = sendinput_test;
438 static UINT *pMsg = sendinput_test[0].expected_messages;
439
440 /* Verify that only specified key state transitions occur */
441 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, struct transition_s *t) {
442     int i;
443     while (t->wVk) {
444         int matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
445                        && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
446         if (t->_todo_wine) {
447             todo_wine {
448                 ok(matched, "%02d: %02x from %02x -> %02x "
449                    "instead of %02x -> %02x\n", id, t->wVk,
450                    ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
451                    ~t->before_state&0x80);
452             }
453         } else {
454             ok(matched, "%02d: %02x from %02x -> %02x "
455                "instead of %02x -> %02x\n", id, t->wVk,
456                ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
457                ~t->before_state&0x80);
458         }
459         ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
460         t++;
461     }
462     for (i = 0; i < 256; i++)
463         ok(ks2[i] == ks1[i], "%02d: %02x from %02x -> %02x unexpected\n",
464            id, i, ks1[i], ks2[i]);
465 }
466
467 /* WndProc2 checks that we get at least the messages specified */
468 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
469                                    LPARAM lParam)
470 {
471     if (pTest->wVk != 0) { /* not end */
472         while(pTest->wVk != 0 && *pMsg == 0) {
473             pTest++;
474             pMsg = pTest->expected_messages;
475         }
476         if (Msg == *pMsg)
477             pMsg++;
478     }
479     return DefWindowProc(hWnd, Msg, wParam, lParam);
480 }
481
482 static void test_Input_blackbox(void)
483 {
484     TEST_INPUT i;
485     int ii;
486     BYTE ks1[256], ks2[256];
487     LONG_PTR prevWndProc;
488
489     HWND window;
490     window = CreateWindow("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
491         |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
492         NULL, NULL);
493     ok(window != NULL, "error: %d\n", (int) GetLastError());
494
495     /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
496      * key state set by SendInput(). */
497     empty_message_queue();
498
499     prevWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
500     ok(prevWndProc != 0 || (prevWndProc == 0 && GetLastError() == 0),
501        "error: %d\n", (int) GetLastError());
502
503     i.type = INPUT_KEYBOARD;
504     i.u.ki.wScan = 0;
505     i.u.ki.time = 0;
506     i.u.ki.dwExtraInfo = 0;
507
508     for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
509          ii++) {
510         GetKeyboardState(ks1);
511         i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
512         i.u.ki.wVk = sendinput_test[ii].wVk;
513         SendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
514         empty_message_queue();
515         GetKeyboardState(ks2);
516         compare_and_check(ii, ks1, ks2,
517                           sendinput_test[ii].expected_transitions);
518     }
519
520     /* *pMsg should be 0 and (++pTest)->wVk should be 0 */
521     if (pTest->wVk && *pMsg == 0) pTest++;
522     while(pTest->wVk && pTest->expected_messages[0] == 0) {
523         ++pTest;
524     }
525     ok(*pMsg == 0 && pTest->wVk == 0,
526        "not enough messages found; looking for %x\n", *pMsg);
527     DestroyWindow(window);
528 }
529
530 static void test_keynames(void)
531 {
532     int i, len;
533     char buff[256];
534
535     for (i = 0; i < 512; i++)
536     {
537         strcpy(buff, "----");
538         len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
539         ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
540     }
541 }
542
543 static POINT pt_old, pt_new;
544 static BOOL clipped;
545 #define STEP 20
546
547 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
548 {
549     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
550     POINT pt, pt1;
551
552     if (code == HC_ACTION)
553     {
554         /* This is our new cursor position */
555         pt_new = hook->pt;
556         /* Should return previous position */
557         GetCursorPos(&pt);
558         ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
559
560         /* Should set new position until hook chain is finished. */
561         pt.x = pt_old.x + STEP;
562         pt.y = pt_old.y + STEP;
563         SetCursorPos(pt.x, pt.y);
564         GetCursorPos(&pt1);
565         if (clipped)
566             ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
567         else
568             ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
569     }
570     return CallNextHookEx( 0, code, wparam, lparam );
571 }
572
573 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
574 {
575     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
576     POINT pt;
577
578     if (code == HC_ACTION)
579     {
580         ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
581            "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
582
583         /* Should match position set above */
584         GetCursorPos(&pt);
585         if (clipped)
586             ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
587         else
588             ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
589     }
590     return CallNextHookEx( 0, code, wparam, lparam );
591 }
592
593 static void test_mouse_ll_hook(void)
594 {
595     HWND hwnd;
596     HHOOK hook1, hook2;
597     POINT pt_org, pt;
598     RECT rc;
599
600     GetCursorPos(&pt_org);
601     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
602                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
603     SetCursorPos(100, 100);
604
605     hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0);
606     hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
607
608     GetCursorPos(&pt_old);
609     mouse_event(MOUSEEVENTF_MOVE, -STEP,  0, 0, 0);
610     GetCursorPos(&pt_old);
611     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);
612     mouse_event(MOUSEEVENTF_MOVE, +STEP,  0, 0, 0);
613     GetCursorPos(&pt_old);
614     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);
615     mouse_event(MOUSEEVENTF_MOVE,  0, -STEP, 0, 0);
616     GetCursorPos(&pt_old);
617     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);
618     mouse_event(MOUSEEVENTF_MOVE,  0, +STEP, 0, 0);
619     GetCursorPos(&pt_old);
620     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);
621
622     SetRect(&rc, 50, 50, 151, 151);
623     ClipCursor(&rc);
624     clipped = TRUE;
625
626     SetCursorPos(40, 40);
627     GetCursorPos(&pt_old);
628     ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
629     SetCursorPos(160, 160);
630     GetCursorPos(&pt_old);
631     ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
632     mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
633     GetCursorPos(&pt_old);
634     ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
635
636     clipped = FALSE;
637     pt_new.x = pt_new.y = 150;
638     ClipCursor(NULL);
639     UnhookWindowsHookEx(hook1);
640
641     /* Now check that mouse buttons do not change mouse position
642        if we don't have MOUSEEVENTF_MOVE flag specified. */
643
644     /* We reusing the same hook callback, so make it happy */
645     pt_old.x = pt_new.x - STEP;
646     pt_old.y = pt_new.y - STEP;
647     mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
648     GetCursorPos(&pt);
649     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
650     mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
651     GetCursorPos(&pt);
652     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
653
654     mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
655     GetCursorPos(&pt);
656     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
657     mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
658     GetCursorPos(&pt);
659     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
660
661     UnhookWindowsHookEx(hook2);
662     DestroyWindow(hwnd);
663     SetCursorPos(pt_org.x, pt_org.y);
664 }
665
666 START_TEST(input)
667 {
668     test_Input_whitebox();
669     test_Input_blackbox();
670     test_keynames();
671     test_mouse_ll_hook();
672 }