INT21_GetFreeDiskSpace(): The drive parameter is found in the DL
[wine] / dlls / user / 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., 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 #include "winbase.h"
54 #include "winuser.h"
55
56 #include <assert.h>
57
58 /* globals */
59 HWND hWndTest;
60 long timetag = 0x10000000;
61
62 #define MAXKEYEVENTS 6
63 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
64                                        and only one message */
65
66 /* keyboard message names, sorted as their value */
67 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
68     "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
69
70 /* keyevents, add more as needed */
71 typedef enum KEVtag
72 {  ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
73 /* matching VK's */
74 int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
75 /* matching scan codes */
76 int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
77 /* matching updown events */
78 int GETUPDOWN[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
79 /* matching descripts */
80 char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
81
82 #define ADDTOINPUTS(kev) \
83 inputs[evtctr].type = INPUT_KEYBOARD; \
84     inputs[evtctr].u.ki.wVk = GETVKEY[ kev]; \
85     inputs[evtctr].u.ki.wScan = GETSCAN[ kev]; \
86     inputs[evtctr].u.ki.dwFlags = GETUPDOWN[ kev]; \
87     inputs[evtctr].u.ki.dwExtraInfo = 0; \
88     inputs[evtctr].u.ki.time = ++timetag; \
89     if( kev) evtctr++;
90
91 typedef struct {
92     UINT    message;
93     WPARAM  wParam;
94     LPARAM  lParam;
95 } KMSG;
96
97 /*******************************************
98  * add new test sets here
99  * the software will make all combinations of the
100  * keyevent defined here
101  */
102 struct { int nrkev;
103     KEV keydwn[MAXKEYEVENTS];
104     KEV keyup[MAXKEYEVENTS];
105 } testkeyset[]= {
106     { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
107     { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
108     { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
109     { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
110     { 0 } /* mark the end */
111 };
112
113 /**********************adapted from input.c **********************************/
114
115 BYTE InputKeyStateTable[256];
116 BYTE AsyncKeyStateTable[256];
117 BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
118                          or a WM_KEYUP message */
119 typedef union
120 {
121     struct
122     {
123         unsigned long count : 16;
124         unsigned long code : 8;
125         unsigned long extended : 1;
126         unsigned long unused : 2;
127         unsigned long win_internal : 2;
128         unsigned long context : 1;
129         unsigned long previous : 1;
130         unsigned long transition : 1;
131     } lp1;
132     unsigned long lp2;
133 } KEYLP;
134
135 int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
136 {
137     UINT message;
138     int VKey = GETVKEY[kev];
139     KEYLP keylp;
140
141     keylp.lp2 = 0;
142
143     keylp.lp1.count = 1;
144     keylp.lp1.code = GETSCAN[kev];
145     keylp.lp1.extended = 0 ;/*  FIXME (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0; */
146     keylp.lp1.win_internal = 0;
147
148     if (GETUPDOWN[kev] & KEYEVENTF_KEYUP )
149     {
150         message = WM_KEYUP;
151         if( (InputKeyStateTable[VK_MENU] & 0x80) && (
152                 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
153                  !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
154             if(  TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
155                     (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
156                 message = WM_SYSKEYUP;
157                 TrackSysKey = 0;
158         }
159         InputKeyStateTable[VKey] &= ~0x80;
160         keylp.lp1.previous = 1;
161         keylp.lp1.transition = 1;
162     }
163     else
164     {
165         keylp.lp1.previous = (InputKeyStateTable[VKey] & 0x80) != 0;
166         keylp.lp1.transition = 0;
167         if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
168         InputKeyStateTable[VKey] |= 0x80;
169         AsyncKeyStateTable[VKey] |= 0x80;
170
171         message = WM_KEYDOWN;
172         if( (InputKeyStateTable[VK_MENU] & 0x80) &&
173                 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
174             message = WM_SYSKEYDOWN;
175             TrackSysKey = VKey;
176         }
177     }
178
179     keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
180
181     if( plParam) *plParam = keylp.lp2;
182     if( pwParam) *pwParam = VKey;
183     return message;
184 }
185
186 /****************************** end copy input.c ****************************/
187
188 /*
189  * . prepare the keyevents for SendInputs
190  * . calculate the "expected" messages
191  * . Send the events to our window
192  * . retrieve the messages from the input queue
193  * . verify
194  */
195 void do_test( HWND hwnd, int seqnr, KEV td[] )
196 {
197     INPUT inputs[MAXKEYEVENTS];
198     KMSG expmsg[MAXKEYEVENTS];
199     MSG msg;
200     char buf[100];
201     int evtctr=0, kmctr, i;
202     buf[0]='\0';
203     TrackSysKey=0; /* see input.c */
204     for( i = 0; i < MAXKEYEVENTS; i++) {
205         ADDTOINPUTS(td[i])
206         strcat(buf, getdesc[td[i]]);
207         if(td[i])
208             expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
209         else
210             expmsg[i].message = 0;
211     }
212     for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
213         ;
214     assert( evtctr <= MAXKEYEVENTS );
215     assert( evtctr == SendInput(evtctr, &inputs[0], sizeof(INPUT)));
216     i = 0;
217     trace("======== key stroke sequence #%d: %s =============\n",
218             seqnr + 1, buf);
219     while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
220         trace("message[%d] %-15s wParam %04x lParam %08lx time %lx\n", i,
221                 MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
222         if( i < kmctr ) {
223             ok( msg.message == expmsg[i].message &&
224                     msg.wParam == expmsg[i].wParam &&
225                     msg.lParam == expmsg[i].lParam,
226                     "wrong message! expected:\n"
227                     "message[%d] %-15s wParam %04x lParam %08lx",i,
228                     MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
229                     expmsg[i].wParam, expmsg[i].lParam );
230         }
231         i++;
232     }
233     trace("%d messages retrieved\n", i);
234     ok( i == kmctr, "message count is wrong: got %d expected: %d", i, kmctr);
235 }
236
237 /* test all combinations of the specified key events */
238 void TestASet( HWND hWnd, int nrkev, KEV kevdwn[], KEV kevup[] )
239 {
240     int i,j,k,l,m,n;
241     static int count=0;
242     KEV kbuf[MAXKEYEVENTS];
243     assert( nrkev==2 || nrkev==3);
244     for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
245     /* two keys involved gives 4 test cases */
246     if(nrkev==2) {
247         for(i=0;i<nrkev;i++) {
248             for(j=0;j<nrkev;j++) {
249                 kbuf[0] = kevdwn[i];
250                 kbuf[1] = kevdwn[1-i];
251                 kbuf[2] = kevup[j];
252                 kbuf[3] = kevup[1-j];
253                 do_test( hWnd, count++, kbuf);
254             }
255         }
256     }
257     /* three keys involved gives 36 test cases */
258     if(nrkev==3){
259         for(i=0;i<nrkev;i++){
260             for(j=0;j<nrkev;j++){
261                 if(j==i) continue;
262                 for(k=0;k<nrkev;k++){
263                     if(k==i || k==j) continue;
264                     for(l=0;l<nrkev;l++){
265                         for(m=0;m<nrkev;m++){
266                             if(m==l) continue;
267                             for(n=0;n<nrkev;n++){
268                                 if(n==l ||n==m) continue;
269                                 kbuf[0] = kevdwn[i];
270                                 kbuf[1] = kevdwn[j];
271                                 kbuf[2] = kevdwn[k];
272                                 kbuf[3] = kevup[l];
273                                 kbuf[4] = kevup[m];
274                                 kbuf[5] = kevup[n];
275                                 do_test( hWnd, count++, kbuf);
276                             }
277                         }
278                     }
279                 }
280             }
281         }
282     }
283 }
284
285 /* test each set specified in the global testkeyset array */
286 void TestSysKeys( HWND hWnd)
287 {
288     int i;
289     for(i=0; testkeyset[i].nrkev;i++)
290         TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
291                 testkeyset[i].keyup);
292
293 }
294
295 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
296         LPARAM lParam )
297 {
298     switch (msg) {
299         case WM_SETFOCUS:
300             /* window has focus, now do the test */
301             if( hWnd == hWndTest) TestSysKeys( hWnd);
302             /* finished :-) */
303             DestroyWindow(hWnd);
304             break;
305
306         case WM_DESTROY:
307             PostQuitMessage( 0 );
308             break;
309
310         default:
311             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
312     }
313
314     return 0;
315 }
316
317 START_TEST(input)
318 {
319     MSG msg;
320     WNDCLASSA  wclass;
321     HANDLE hInstance = GetModuleHandleA( NULL );
322
323     wclass.lpszClassName = "InputSysKeyTestClass";
324     wclass.style         = CS_HREDRAW | CS_VREDRAW;
325     wclass.lpfnWndProc   = (WNDPROC)WndProc;
326     wclass.hInstance     = hInstance;
327     wclass.hIcon         = LoadIconA( 0, (LPSTR)IDI_APPLICATION );
328     wclass.hCursor       = LoadCursorA( NULL, IDC_ARROW);
329     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1);
330     wclass.lpszMenuName = 0;
331     wclass.cbClsExtra    = 0;
332     wclass.cbWndExtra    = 0;
333     assert (RegisterClassA( &wclass ));
334     /* create the test window that will receive the keystrokes */
335     assert ( hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
336                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
337                 NULL, NULL, hInstance, NULL) );
338     ShowWindow( hWndTest, SW_SHOW);
339     UpdateWindow( hWndTest);
340     /* message loop */
341     while( GetMessageA( &msg, 0, 0, 0 )) {
342         TranslateMessage( &msg );
343         DispatchMessageA( &msg );
344     }
345 }