d3drm: Handle texture associated with the material when loading a mesh.
[wine] / dlls / joy.cpl / main.c
1 /*
2  * Joystick testing control panel applet
3  *
4  * Copyright 2012 Lucas Fialho Zawacki
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #define NONAMELESSUNION
23 #define COBJMACROS
24 #define CONST_VTABLE
25
26 #include <stdarg.h>
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winuser.h>
30 #include <commctrl.h>
31 #include <cpl.h>
32 #include "ole2.h"
33
34 #include "wine/debug.h"
35 #include "joy.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(joycpl);
38
39 DECLSPEC_HIDDEN HMODULE hcpl;
40
41 /*********************************************************************
42  *  DllMain
43  */
44 BOOL WINAPI DllMain(HINSTANCE hdll, DWORD reason, LPVOID reserved)
45 {
46     TRACE("(%p, %d, %p)\n", hdll, reason, reserved);
47
48     switch (reason)
49     {
50         case DLL_WINE_PREATTACH:
51             return FALSE;  /* prefer native version */
52
53         case DLL_PROCESS_ATTACH:
54             DisableThreadLibraryCalls(hdll);
55             hcpl = hdll;
56     }
57     return TRUE;
58 }
59
60 /***********************************************************************
61  *  enum_callback [internal]
62  *   Enumerates, creates and sets the common data format for all the joystick devices.
63  *   First time it checks if space for the joysticks was already reserved
64  *   and if not, just counts how many there are.
65  */
66 static BOOL CALLBACK enum_callback(const DIDEVICEINSTANCEW *instance, void *context)
67 {
68     struct JoystickData *data = context;
69     struct Joystick *joystick;
70     DIDEVCAPS caps;
71
72     if (data->joysticks == NULL)
73     {
74         data->num_joysticks += 1;
75         return DIENUM_CONTINUE;
76     }
77
78     joystick = &data->joysticks[data->cur_joystick];
79     data->cur_joystick += 1;
80
81     IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL);
82     IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick);
83
84     joystick->instance = *instance;
85
86     caps.dwSize = sizeof(caps);
87     IDirectInputDevice8_GetCapabilities(joystick->device, &caps);
88
89     joystick->num_buttons = caps.dwButtons;
90     joystick->num_axes = caps.dwAxes;
91
92     return DIENUM_CONTINUE;
93 }
94
95 /***********************************************************************
96  *  initialize_joysticks [internal]
97  */
98 static void initialize_joysticks(struct JoystickData *data)
99 {
100     data->num_joysticks = 0;
101     data->cur_joystick = 0;
102     IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
103     data->joysticks = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Joystick) * data->num_joysticks);
104
105     /* Get all the joysticks */
106     IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
107 }
108
109 /***********************************************************************
110  *  destroy_joysticks [internal]
111  */
112 static void destroy_joysticks(struct JoystickData *data)
113 {
114     int i;
115
116     for (i = 0; i < data->num_joysticks; i++)
117     {
118         IDirectInputDevice8_Unacquire(data->joysticks[i].device);
119         IDirectInputDevice8_Release(data->joysticks[i].device);
120     }
121
122     HeapFree(GetProcessHeap(), 0, data->joysticks);
123 }
124
125 /*********************************************************************
126  * list_dlgproc [internal]
127  *
128  */
129 INT_PTR CALLBACK list_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
130 {
131     TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
132     switch (msg)
133     {
134         case WM_INITDIALOG:
135         {
136             int i;
137             struct JoystickData *data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
138
139             /* Set dialog information */
140             for (i = 0; i < data->num_joysticks; i++)
141             {
142                 struct Joystick *joy = &data->joysticks[i];
143                 SendDlgItemMessageW(hwnd, IDC_JOYSTICKLIST, LB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
144             }
145
146             return TRUE;
147         }
148
149         case WM_COMMAND:
150
151             switch (LOWORD(wparam))
152             {
153                 case IDC_BUTTONDISABLE:
154                     FIXME("Disable selected joystick from being enumerated\n");
155                     break;
156
157                 case IDC_BUTTONENABLE:
158                     FIXME("Re-Enable selected joystick\n");
159                     break;
160             }
161
162             return TRUE;
163
164         case WM_NOTIFY:
165             return TRUE;
166
167         default:
168             break;
169     }
170     return FALSE;
171 }
172
173 /*********************************************************************
174  * Joystick testing functions
175  *
176  */
177 static void dump_joy_state(DIJOYSTATE* st, int num_buttons)
178 {
179     int i;
180     TRACE("Ax (% 5d,% 5d,% 5d)\n", st->lX,st->lY, st->lZ);
181     TRACE("RAx (% 5d,% 5d,% 5d)\n", st->lRx, st->lRy, st->lRz);
182     TRACE("Slider (% 5d,% 5d)\n", st->rglSlider[0], st->rglSlider[1]);
183     TRACE("Pov (% 5d,% 5d,% 5d,% 5d)\n", st->rgdwPOV[0], st->rgdwPOV[1], st->rgdwPOV[2], st->rgdwPOV[3]);
184
185     TRACE("Buttons ");
186     for(i=0; i < num_buttons; i++)
187         TRACE("  %c",st->rgbButtons[i] ? 'x' : 'o');
188     TRACE("\n");
189 }
190
191 static void poll_input(const struct Joystick *joy, DIJOYSTATE *state)
192 {
193     HRESULT  hr;
194
195     hr = IDirectInputDevice8_Poll(joy->device);
196
197     /* If it failed, try to acquire the joystick */
198     if (FAILED(hr))
199     {
200         hr = IDirectInputDevice8_Acquire(joy->device);
201         while (hr == DIERR_INPUTLOST) hr = IDirectInputDevice8_Acquire(joy->device);
202     }
203
204     if (hr == DIERR_OTHERAPPHASPRIO) return;
205
206     IDirectInputDevice8_GetDeviceState(joy->device, sizeof(DIJOYSTATE), state);
207 }
208
209 static DWORD WINAPI input_thread(void *param)
210 {
211     int axes_pos[TEST_MAX_AXES][2];
212     DIJOYSTATE state;
213     struct JoystickData *data = param;
214
215     ZeroMemory(&state, sizeof(state));
216
217     while (!data->stop)
218     {
219         int i;
220         poll_input(&data->joysticks[data->chosen_joystick], &state);
221
222         dump_joy_state(&state, data->joysticks[data->chosen_joystick].num_buttons);
223
224         /* Indicate pressed buttons */
225         for (i = 0; i < data->joysticks[data->chosen_joystick].num_buttons; i++)
226             if (state.rgbButtons[i])
227                 SendMessageW(data->buttons[i], BM_SETSTATE, TRUE, 0);
228
229         /* Indicate axis positions, axes showing are hardcoded for now */
230         axes_pos[0][0] = state.lX;
231         axes_pos[0][1] = state.lY;
232         axes_pos[1][0] = state.lRx;
233         axes_pos[1][1] = state.lRy;
234         axes_pos[2][0] = state.lZ;
235         axes_pos[2][1] = state.lRz;
236
237         for (i = 0; i < TEST_MAX_AXES; i++)
238             SetWindowPos(data->axes[i], 0,
239                         TEST_AXIS_X + TEST_NEXT_AXIS_X*i + axes_pos[i][0],
240                         TEST_AXIS_Y + axes_pos[i][1],
241                         0, 0, SWP_NOZORDER | SWP_NOSIZE);
242
243         Sleep(TEST_POLL_TIME);
244
245         /* Reset button state */
246         for (i = 0; i < data->joysticks[data->chosen_joystick].num_buttons; i++)
247             SendMessageW(data->buttons[i], BM_SETSTATE, FALSE, 0);
248     }
249
250     return 0;
251 }
252
253 static void test_handle_joychange(HWND hwnd, struct JoystickData *data)
254 {
255     int i;
256
257     if (data->num_joysticks == 0) return;
258
259     data->chosen_joystick = SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_GETCURSEL, 0, 0);
260
261     /* Enable only  buttons present in the device */
262     for (i = 0; i < TEST_MAX_BUTTONS; i++)
263         ShowWindow(data->buttons[i], i <= data->joysticks[data->chosen_joystick].num_buttons);
264 }
265
266 /*********************************************************************
267  * button_number_to_wchar [internal]
268  *  Transforms an integer in the interval [0,99] into a 2 character WCHAR string
269  */
270 static void button_number_to_wchar(int n, WCHAR str[3])
271 {
272     str[1] = n % 10 + '0';
273     n /= 10;
274     str[0] = n % 10 + '0';
275     str[2] = '\0';
276 }
277
278 static void draw_joystick_buttons(HWND hwnd, struct JoystickData* data)
279 {
280     int i;
281     int row = 0, col = 0;
282     WCHAR button_label[3];
283     HINSTANCE hinst = (HINSTANCE) GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
284     static WCHAR button_class[] = {'B','u','t','t','o','n','\0'};
285
286     for (i = 0; i < TEST_MAX_BUTTONS; i++)
287     {
288         if ((i % TEST_BUTTON_COL_MAX) == 0 && i != 0)
289         {
290             row += 1;
291             col = 0;
292         }
293
294         button_number_to_wchar(i + 1, button_label);
295
296         data->buttons[i] = CreateWindowW(button_class, button_label, WS_CHILD,
297             TEST_BUTTON_X + TEST_NEXT_BUTTON_X*col, TEST_BUTTON_Y + TEST_NEXT_BUTTON_Y*row,
298             TEST_BUTTON_SIZE_X, TEST_BUTTON_SIZE_Y,
299             hwnd, NULL, NULL, hinst);
300
301         col += 1;
302     }
303 }
304
305 static void draw_joystick_axes(HWND hwnd, struct JoystickData* data)
306 {
307     int i;
308     struct Joystick *joy;
309     DIPROPRANGE propRange;
310     HINSTANCE hinst = (HINSTANCE) GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
311     static WCHAR button_class[] = {'B','u','t','t','o','n','\0'};
312
313     /* Set axis range to ease the GUI visualization */
314     for (i = 0; i < data->num_joysticks; i++)
315     {
316         joy = &data->joysticks[i];
317         propRange.diph.dwSize = sizeof(DIPROPRANGE);
318         propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
319         propRange.diph.dwHow = DIPH_DEVICE;
320         propRange.diph.dwObj = 0;
321         propRange.lMin = TEST_AXIS_MIN;
322         propRange.lMax = TEST_AXIS_MAX;
323
324         IDirectInputDevice_SetProperty(joy->device, DIPROP_RANGE, &propRange.diph);
325     }
326
327     for (i = 0; i < TEST_MAX_AXES; i++)
328     {
329         data->axes[i] = CreateWindowW( button_class, NULL, WS_CHILD | WS_VISIBLE,
330             TEST_AXIS_X + TEST_NEXT_AXIS_X*i, TEST_AXIS_Y,
331             TEST_AXIS_SIZE_X, TEST_AXIS_SIZE_Y,
332             hwnd, (HMENU) IDC_JOYSTICKAXES + i, NULL, hinst);
333     }
334 }
335
336 /*********************************************************************
337  * test_dlgproc [internal]
338  *
339  */
340 static INT_PTR CALLBACK test_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
341 {
342     static HANDLE thread;
343     static struct JoystickData *data;
344     TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
345
346     switch (msg)
347     {
348         case WM_INITDIALOG:
349         {
350             int i;
351
352             data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
353
354             /* Add enumerated joysticks to the combobox */
355             for (i = 0; i < data->num_joysticks; i++)
356             {
357                 struct Joystick *joy = &data->joysticks[i];
358                 SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
359             }
360
361             draw_joystick_buttons(hwnd, data);
362             draw_joystick_axes(hwnd, data);
363
364             return TRUE;
365         }
366
367         case WM_COMMAND:
368             switch(wparam)
369             {
370                 case MAKEWPARAM(IDC_TESTSELECTCOMBO, CBN_SELCHANGE):
371                     test_handle_joychange(hwnd, data);
372                 break;
373             }
374             return TRUE;
375
376         case WM_NOTIFY:
377             switch(((LPNMHDR)lparam)->code)
378             {
379                 case PSN_SETACTIVE:
380                 {
381                     DWORD tid;
382
383                     /* Initialize input thread */
384                     if (data->num_joysticks > 0)
385                     {
386                         data->stop = FALSE;
387
388                         /* Set the first joystick as default */
389                         SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_SETCURSEL, 0, 0);
390                         test_handle_joychange(hwnd, data);
391
392                         thread = CreateThread(NULL, 0, input_thread, (void*) data, 0, &tid);
393                     }
394                 }
395                 break;
396
397                 case PSN_RESET:
398                     /* Stop input thread */
399                     data->stop = TRUE;
400                     CloseHandle(thread);
401                 break;
402             }
403             return TRUE;
404     }
405     return FALSE;
406 }
407
408 /******************************************************************************
409  * propsheet_callback [internal]
410  */
411 static int CALLBACK propsheet_callback(HWND hwnd, UINT msg, LPARAM lparam)
412 {
413     TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
414     switch (msg)
415     {
416         case PSCB_INITIALIZED:
417             break;
418     }
419     return 0;
420 }
421
422 /******************************************************************************
423  * display_cpl_sheets [internal]
424  *
425  * Build and display the dialog with all control panel propertysheets
426  *
427  */
428 static void display_cpl_sheets(HWND parent, struct JoystickData *data)
429 {
430     INITCOMMONCONTROLSEX icex;
431     PROPSHEETPAGEW psp[NUM_PROPERTY_PAGES];
432     PROPSHEETHEADERW psh;
433     DWORD id = 0;
434
435     OleInitialize(NULL);
436     /* Initialize common controls */
437     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
438     icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
439     InitCommonControlsEx(&icex);
440
441     ZeroMemory(&psh, sizeof(psh));
442     ZeroMemory(psp, sizeof(psp));
443
444     /* Fill out all PROPSHEETPAGE */
445     psp[id].dwSize = sizeof (PROPSHEETPAGEW);
446     psp[id].hInstance = hcpl;
447     psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_LIST);
448     psp[id].pfnDlgProc = list_dlgproc;
449     psp[id].lParam = (INT_PTR) data;
450     id++;
451
452     psp[id].dwSize = sizeof (PROPSHEETPAGEW);
453     psp[id].hInstance = hcpl;
454     psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_TEST);
455     psp[id].pfnDlgProc = test_dlgproc;
456     psp[id].lParam = (INT_PTR) data;
457     id++;
458
459     psp[id].dwSize = sizeof (PROPSHEETPAGEW);
460     psp[id].hInstance = hcpl;
461     psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_FORCEFEEDBACK);
462     psp[id].pfnDlgProc = NULL;
463     psp[id].lParam = (INT_PTR) data;
464     id++;
465
466     /* Fill out the PROPSHEETHEADER */
467     psh.dwSize = sizeof (PROPSHEETHEADERW);
468     psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
469     psh.hwndParent = parent;
470     psh.hInstance = hcpl;
471     psh.pszCaption = MAKEINTRESOURCEW(IDS_CPL_NAME);
472     psh.nPages = id;
473     psh.u3.ppsp = psp;
474     psh.pfnCallback = propsheet_callback;
475
476     /* display the dialog */
477     PropertySheetW(&psh);
478
479     OleUninitialize();
480 }
481
482 /*********************************************************************
483  * CPlApplet (joy.cpl.@)
484  *
485  * Control Panel entry point
486  *
487  * PARAMS
488  *  hWnd    [I] Handle for the Control Panel Window
489  *  command [I] CPL_* Command
490  *  lParam1 [I] first extra Parameter
491  *  lParam2 [I] second extra Parameter
492  *
493  * RETURNS
494  *  Depends on the command
495  *
496  */
497 LONG CALLBACK CPlApplet(HWND hwnd, UINT command, LPARAM lParam1, LPARAM lParam2)
498 {
499     static struct JoystickData data;
500     TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd, command, lParam1, lParam2);
501
502     switch (command)
503     {
504         case CPL_INIT:
505         {
506             HRESULT hr;
507
508             /* Initialize dinput */
509             hr = DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8W, (void**)&data.di, NULL);
510
511             if (FAILED(hr))
512             {
513                 ERR("Failed to initialize DirectInput: 0x%08x\n", hr);
514                 return FALSE;
515             }
516
517             /* Then get all the connected joysticks */
518             initialize_joysticks(&data);
519
520             return TRUE;
521         }
522         case CPL_GETCOUNT:
523             return 1;
524
525         case CPL_INQUIRE:
526         {
527             CPLINFO *appletInfo = (CPLINFO *) lParam2;
528
529             appletInfo->idName = IDS_CPL_NAME;
530             appletInfo->idInfo = IDS_CPL_INFO;
531             appletInfo->lData = 0;
532             return TRUE;
533         }
534
535         case CPL_DBLCLK:
536             display_cpl_sheets(hwnd, &data);
537             break;
538
539         case CPL_STOP:
540             destroy_joysticks(&data);
541
542             /* And destroy dinput too */
543             IDirectInput8_Release(data.di);
544             break;
545     }
546
547     return FALSE;
548 }