hidclass.sys: Implement IRP_MN_QUERY_ID for HID devices.
[wine.git] / dlls / joy.cpl / main.c
blobd6ba5d511b995ec6cb8a9dcff84d3ca22984f1c7
1 /*
2 * Joystick testing control panel applet
4 * Copyright 2012 Lucas Fialho Zawacki
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.
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.
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
22 #define NONAMELESSUNION
23 #define COBJMACROS
24 #define CONST_VTABLE
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"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "joy.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(joycpl);
40 DECLSPEC_HIDDEN HMODULE hcpl;
42 static const WCHAR button_class[] = {'B','u','t','t','o','n','\0'};
44 /*********************************************************************
45 * DllMain
47 BOOL WINAPI DllMain(HINSTANCE hdll, DWORD reason, LPVOID reserved)
49 TRACE("(%p, %d, %p)\n", hdll, reason, reserved);
51 switch (reason)
53 case DLL_WINE_PREATTACH:
54 return FALSE; /* prefer native version */
56 case DLL_PROCESS_ATTACH:
57 DisableThreadLibraryCalls(hdll);
58 hcpl = hdll;
60 return TRUE;
63 /***********************************************************************
64 * enum_callback [internal]
65 * Enumerates, creates and sets the common data format for all the joystick devices.
66 * First time it checks if space for the joysticks was already reserved
67 * and if not, just counts how many there are.
69 static BOOL CALLBACK enum_callback(const DIDEVICEINSTANCEW *instance, void *context)
71 struct JoystickData *data = context;
72 struct Joystick *joystick;
73 DIPROPRANGE proprange;
74 DIDEVCAPS caps;
76 if (data->joysticks == NULL)
78 data->num_joysticks += 1;
79 return DIENUM_CONTINUE;
82 joystick = &data->joysticks[data->cur_joystick];
83 data->cur_joystick += 1;
85 IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL);
86 IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick);
88 joystick->instance = *instance;
90 caps.dwSize = sizeof(caps);
91 IDirectInputDevice8_GetCapabilities(joystick->device, &caps);
93 joystick->num_buttons = caps.dwButtons;
94 joystick->num_axes = caps.dwAxes;
95 joystick->forcefeedback = caps.dwFlags & DIDC_FORCEFEEDBACK;
96 joystick->num_effects = 0;
98 if (joystick->forcefeedback) data->num_ff++;
100 /* Set axis range to ease the GUI visualization */
101 proprange.diph.dwSize = sizeof(DIPROPRANGE);
102 proprange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
103 proprange.diph.dwHow = DIPH_DEVICE;
104 proprange.diph.dwObj = 0;
105 proprange.lMin = TEST_AXIS_MIN;
106 proprange.lMax = TEST_AXIS_MAX;
108 IDirectInputDevice_SetProperty(joystick->device, DIPROP_RANGE, &proprange.diph);
110 return DIENUM_CONTINUE;
113 /***********************************************************************
114 * initialize_joysticks [internal]
116 static void initialize_joysticks(struct JoystickData *data)
118 data->num_joysticks = 0;
119 data->cur_joystick = 0;
120 IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
121 data->joysticks = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Joystick) * data->num_joysticks);
123 /* Get all the joysticks */
124 IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
127 /***********************************************************************
128 * destroy_joysticks [internal]
130 static void destroy_joysticks(struct JoystickData *data)
132 int i, j;
134 for (i = 0; i < data->num_joysticks; i++)
137 if (data->joysticks[i].forcefeedback && data->joysticks[i].num_effects > 0)
139 for (j = 0; j < data->joysticks[i].num_effects; j++)
140 IDirectInputEffect_Release(data->joysticks[i].effects[j].effect);
142 HeapFree(GetProcessHeap(), 0, data->joysticks[i].effects);
145 IDirectInputDevice8_Unacquire(data->joysticks[i].device);
146 IDirectInputDevice8_Release(data->joysticks[i].device);
149 HeapFree(GetProcessHeap(), 0, data->joysticks);
152 static void initialize_joysticks_list(HWND hwnd, struct JoystickData *data)
154 int i;
156 SendDlgItemMessageW(hwnd, IDC_JOYSTICKLIST, LB_RESETCONTENT, 0, 0);
158 /* Add enumerated joysticks */
159 for (i = 0; i < data->num_joysticks; i++)
161 struct Joystick *joy = &data->joysticks[i];
162 SendDlgItemMessageW(hwnd, IDC_JOYSTICKLIST, LB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
166 /******************************************************************************
167 * get_app_key [internal]
168 * Get the default DirectInput key and the selected app config key.
170 static BOOL get_app_key(HKEY *defkey, HKEY *appkey)
172 static const WCHAR reg_key[] = { 'S','o','f','t','w','a','r','e','\\',
173 'W','i','n','e','\\',
174 'D','i','r','e','c','t','I','n','p','u','t','\\',
175 'J','o','y','s','t','i','c','k','s','\0' };
176 *appkey = 0;
178 /* Registry key can be found in HKCU\Software\Wine\DirectInput */
179 if (RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0, KEY_SET_VALUE | KEY_READ, NULL, defkey, NULL))
180 *defkey = 0;
182 return *defkey || *appkey;
185 /******************************************************************************
186 * set_config_key [internal]
187 * Writes a string value to a registry key, deletes the key if value == NULL
189 static DWORD set_config_key(HKEY defkey, HKEY appkey, const WCHAR *name, const WCHAR *value, DWORD size)
191 if (value == NULL)
193 if (appkey && !RegDeleteValueW(appkey, name))
194 return 0;
196 if (defkey && !RegDeleteValueW(defkey, name))
197 return 0;
199 else
201 if (appkey && !RegSetValueExW(appkey, name, 0, REG_SZ, (const BYTE*) value, (size + 1)*sizeof(WCHAR)))
202 return 0;
204 if (defkey && !RegSetValueExW(defkey, name, 0, REG_SZ, (const BYTE*) value, (size + 1)*sizeof(WCHAR)))
205 return 0;
208 return ERROR_FILE_NOT_FOUND;
211 /******************************************************************************
212 * enable_joystick [internal]
213 * Writes to the DirectInput registry key that enables/disables a joystick
214 * from being enumerated.
216 static void enable_joystick(WCHAR *joy_name, BOOL enable)
218 static const WCHAR disabled_str[] = {'d','i','s','a','b','l','e','d','\0'};
219 HKEY hkey, appkey;
221 get_app_key(&hkey, &appkey);
223 if (!enable)
224 set_config_key(hkey, appkey, joy_name, disabled_str, lstrlenW(disabled_str));
225 else
226 set_config_key(hkey, appkey, joy_name, NULL, 0);
228 if (hkey) RegCloseKey(hkey);
229 if (appkey) RegCloseKey(appkey);
232 static void initialize_disabled_joysticks_list(HWND hwnd)
234 static const WCHAR disabled_str[] = {'d','i','s','a','b','l','e','d','\0'};
235 HKEY hkey, appkey;
236 DWORD values = 0;
237 HRESULT hr;
238 DWORD i;
240 SendDlgItemMessageW(hwnd, IDC_DISABLEDLIST, LB_RESETCONTENT, 0, 0);
242 /* Search for disabled joysticks */
243 get_app_key(&hkey, &appkey);
244 RegQueryInfoKeyW(hkey, NULL, NULL, NULL, NULL, NULL, NULL, &values, NULL, NULL, NULL, NULL);
246 for (i=0; i < values; i++)
248 DWORD name_len = MAX_PATH, data_len = MAX_PATH;
249 WCHAR buf_name[MAX_PATH + 9], buf_data[MAX_PATH];
251 hr = RegEnumValueW(hkey, i, buf_name, &name_len, NULL, NULL, (BYTE*) buf_data, &data_len);
253 if (SUCCEEDED(hr) && !lstrcmpW(disabled_str, buf_data))
254 SendDlgItemMessageW(hwnd, IDC_DISABLEDLIST, LB_ADDSTRING, 0, (LPARAM) buf_name);
257 if (hkey) RegCloseKey(hkey);
258 if (appkey) RegCloseKey(appkey);
261 /*********************************************************************
262 * list_dlgproc [internal]
265 static INT_PTR CALLBACK list_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
267 static struct JoystickData *data;
268 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
269 switch (msg)
271 case WM_INITDIALOG:
273 data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
275 initialize_joysticks_list(hwnd, data);
276 initialize_disabled_joysticks_list(hwnd);
278 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONENABLE), FALSE);
279 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONDISABLE), FALSE);
281 /* Store the hwnd to be used with MapDialogRect for unit conversions */
282 data->graphics.hwnd = hwnd;
284 return TRUE;
287 case WM_COMMAND:
289 switch (LOWORD(wparam))
291 case IDC_BUTTONDISABLE:
293 int sel = SendDlgItemMessageW(hwnd, IDC_JOYSTICKLIST, LB_GETCURSEL, 0, 0);
295 if (sel >= 0)
297 enable_joystick(data->joysticks[sel].instance.tszInstanceName, FALSE);
298 initialize_disabled_joysticks_list(hwnd);
301 break;
303 case IDC_BUTTONENABLE:
305 int sel = SendDlgItemMessageW(hwnd, IDC_DISABLEDLIST, LB_GETCURSEL, 0, 0);
307 if (sel >= 0)
309 WCHAR text[MAX_PATH];
310 SendDlgItemMessageW(hwnd, IDC_DISABLEDLIST, LB_GETTEXT, sel, (LPARAM) text);
311 enable_joystick(text, TRUE);
312 initialize_disabled_joysticks_list(hwnd);
315 break;
317 case IDC_JOYSTICKLIST:
318 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONENABLE), FALSE);
319 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONDISABLE), TRUE);
320 break;
322 case IDC_DISABLEDLIST:
323 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONENABLE), TRUE);
324 EnableWindow(GetDlgItem(hwnd, IDC_BUTTONDISABLE), FALSE);
325 break;
328 return TRUE;
330 case WM_NOTIFY:
331 return TRUE;
333 default:
334 break;
336 return FALSE;
339 /*********************************************************************
340 * Joystick testing functions
343 static void dump_joy_state(DIJOYSTATE* st, int num_buttons)
345 int i;
346 TRACE("Ax (% 5d,% 5d,% 5d)\n", st->lX,st->lY, st->lZ);
347 TRACE("RAx (% 5d,% 5d,% 5d)\n", st->lRx, st->lRy, st->lRz);
348 TRACE("Slider (% 5d,% 5d)\n", st->rglSlider[0], st->rglSlider[1]);
349 TRACE("Pov (% 5d,% 5d,% 5d,% 5d)\n", st->rgdwPOV[0], st->rgdwPOV[1], st->rgdwPOV[2], st->rgdwPOV[3]);
351 TRACE("Buttons ");
352 for(i=0; i < num_buttons; i++)
353 TRACE(" %c",st->rgbButtons[i] ? 'x' : 'o');
354 TRACE("\n");
357 static void poll_input(const struct Joystick *joy, DIJOYSTATE *state)
359 HRESULT hr;
361 hr = IDirectInputDevice8_Poll(joy->device);
363 /* If it failed, try to acquire the joystick */
364 if (FAILED(hr))
366 hr = IDirectInputDevice8_Acquire(joy->device);
367 while (hr == DIERR_INPUTLOST) hr = IDirectInputDevice8_Acquire(joy->device);
370 if (hr == DIERR_OTHERAPPHASPRIO) return;
372 IDirectInputDevice8_GetDeviceState(joy->device, sizeof(DIJOYSTATE), state);
375 static DWORD WINAPI input_thread(void *param)
377 int axes_pos[TEST_MAX_AXES][2];
378 DIJOYSTATE state;
379 struct JoystickData *data = param;
381 /* Setup POV as clock positions
383 * 31500 4500
384 * 27000 -1 9000
385 * 22500 13500
386 * 18000
388 int ma = TEST_AXIS_MAX;
389 int pov_val[9] = {0, 4500, 9000, 13500,
390 18000, 22500, 27000, 31500, -1};
391 int pov_pos[9][2] = { {0, -ma}, {ma/2, -ma/2}, {ma, 0}, {ma/2, ma/2},
392 {0, ma}, {-ma/2, ma/2}, {-ma, 0}, {-ma/2, -ma/2}, {0, 0} };
394 ZeroMemory(&state, sizeof(state));
396 while (!data->stop)
398 int i;
399 unsigned int j;
401 poll_input(&data->joysticks[data->chosen_joystick], &state);
403 dump_joy_state(&state, data->joysticks[data->chosen_joystick].num_buttons);
405 /* Indicate pressed buttons */
406 for (i = 0; i < data->joysticks[data->chosen_joystick].num_buttons; i++)
407 if (state.rgbButtons[i])
408 SendMessageW(data->graphics.buttons[i], BM_SETSTATE, TRUE, 0);
410 /* Indicate axis positions, axes showing are hardcoded for now */
411 axes_pos[0][0] = state.lX;
412 axes_pos[0][1] = state.lY;
413 axes_pos[1][0] = state.lRx;
414 axes_pos[1][1] = state.lRy;
415 axes_pos[2][0] = state.lZ;
416 axes_pos[2][1] = state.lRz;
418 /* Set pov values */
419 for (j = 0; j < sizeof(pov_val)/sizeof(pov_val[0]); j++)
421 if (state.rgdwPOV[0] == pov_val[j])
423 axes_pos[3][0] = pov_pos[j][0];
424 axes_pos[3][1] = pov_pos[j][1];
428 for (i = 0; i < TEST_MAX_AXES; i++)
430 RECT r;
432 r.left = (TEST_AXIS_X + TEST_NEXT_AXIS_X*i + axes_pos[i][0]);
433 r.top = (TEST_AXIS_Y + axes_pos[i][1]);
434 r.bottom = r.right = 0; /* unused */
435 MapDialogRect(data->graphics.hwnd, &r);
437 SetWindowPos(data->graphics.axes[i], 0, r.left, r.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
440 Sleep(TEST_POLL_TIME);
442 /* Reset button state */
443 for (i = 0; i < data->joysticks[data->chosen_joystick].num_buttons; i++)
444 SendMessageW(data->graphics.buttons[i], BM_SETSTATE, FALSE, 0);
447 return 0;
450 static void test_handle_joychange(HWND hwnd, struct JoystickData *data)
452 int i;
454 if (data->num_joysticks == 0) return;
456 data->chosen_joystick = SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_GETCURSEL, 0, 0);
458 /* Enable only buttons present in the device */
459 for (i = 0; i < TEST_MAX_BUTTONS; i++)
460 ShowWindow(data->graphics.buttons[i], i <= data->joysticks[data->chosen_joystick].num_buttons);
463 /*********************************************************************
464 * button_number_to_wchar [internal]
465 * Transforms an integer in the interval [0,99] into a 2 character WCHAR string
467 static void button_number_to_wchar(int n, WCHAR str[3])
469 str[1] = n % 10 + '0';
470 n /= 10;
471 str[0] = n % 10 + '0';
472 str[2] = '\0';
475 static void draw_joystick_buttons(HWND hwnd, struct JoystickData* data)
477 int i;
478 int row = 0, col = 0;
479 WCHAR button_label[3];
480 HINSTANCE hinst = (HINSTANCE) GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
482 for (i = 0; i < TEST_MAX_BUTTONS; i++)
484 RECT r;
486 if ((i % TEST_BUTTON_COL_MAX) == 0 && i != 0)
488 row += 1;
489 col = 0;
492 r.left = (TEST_BUTTON_X + TEST_NEXT_BUTTON_X*col);
493 r.top = (TEST_BUTTON_Y + TEST_NEXT_BUTTON_Y*row);
494 r.right = r.left + TEST_BUTTON_SIZE_X;
495 r.bottom = r.top + TEST_BUTTON_SIZE_Y;
496 MapDialogRect(hwnd, &r);
498 button_number_to_wchar(i + 1, button_label);
500 data->graphics.buttons[i] = CreateWindowW(button_class, button_label, WS_CHILD,
501 r.left, r.top, r.right - r.left, r.bottom - r.top,
502 hwnd, NULL, NULL, hinst);
504 col += 1;
508 static void draw_joystick_axes(HWND hwnd, struct JoystickData* data)
510 int i;
511 HINSTANCE hinst = (HINSTANCE) GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
512 static const WCHAR axes_names[TEST_MAX_AXES][7] = { {'X',',','Y','\0'}, {'R','x',',','R','y','\0'},
513 {'Z',',','R','z','\0'}, {'P','O','V','\0'} };
514 static const DWORD axes_idc[TEST_MAX_AXES] = { IDC_TESTGROUPXY, IDC_TESTGROUPRXRY,
515 IDC_TESTGROUPZRZ, IDC_TESTGROUPPOV };
517 for (i = 0; i < TEST_MAX_AXES; i++)
519 RECT r;
520 /* Set axis box name */
521 SetWindowTextW(GetDlgItem(hwnd, axes_idc[i]), axes_names[i]);
523 r.left = (TEST_AXIS_X + TEST_NEXT_AXIS_X*i);
524 r.top = TEST_AXIS_Y;
525 r.right = r.left + TEST_AXIS_SIZE_X;
526 r.bottom = r.top + TEST_AXIS_SIZE_Y;
527 MapDialogRect(hwnd, &r);
529 data->graphics.axes[i] = CreateWindowW( button_class, NULL, WS_CHILD | WS_VISIBLE,
530 r.left, r.top, r.right - r.left, r.bottom - r.top,
531 hwnd, NULL, NULL, hinst);
535 /*********************************************************************
536 * test_dlgproc [internal]
539 static INT_PTR CALLBACK test_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
541 static HANDLE thread;
542 static struct JoystickData *data;
543 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
545 switch (msg)
547 case WM_INITDIALOG:
549 int i;
551 data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
553 /* Add enumerated joysticks to the combobox */
554 for (i = 0; i < data->num_joysticks; i++)
556 struct Joystick *joy = &data->joysticks[i];
557 SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
560 draw_joystick_buttons(hwnd, data);
561 draw_joystick_axes(hwnd, data);
563 return TRUE;
566 case WM_COMMAND:
567 switch(wparam)
569 case MAKEWPARAM(IDC_TESTSELECTCOMBO, CBN_SELCHANGE):
570 test_handle_joychange(hwnd, data);
571 break;
573 return TRUE;
575 case WM_NOTIFY:
576 switch(((LPNMHDR)lparam)->code)
578 case PSN_SETACTIVE:
580 DWORD tid;
582 /* Initialize input thread */
583 if (data->num_joysticks > 0)
585 data->stop = FALSE;
587 /* Set the first joystick as default */
588 SendDlgItemMessageW(hwnd, IDC_TESTSELECTCOMBO, CB_SETCURSEL, 0, 0);
589 test_handle_joychange(hwnd, data);
591 thread = CreateThread(NULL, 0, input_thread, (void*) data, 0, &tid);
594 break;
596 case PSN_RESET: /* intentional fall-through */
597 case PSN_KILLACTIVE:
598 /* Stop input thread */
599 data->stop = TRUE;
600 MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, 0);
601 CloseHandle(thread);
602 break;
604 return TRUE;
606 return FALSE;
609 /*********************************************************************
610 * Joystick force feedback testing functions
613 static void draw_ff_axis(HWND hwnd, struct JoystickData *data)
615 HINSTANCE hinst = (HINSTANCE) GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
616 RECT r;
618 r.left = FF_AXIS_X;
619 r.top = FF_AXIS_Y;
620 r.right = r.left + FF_AXIS_SIZE_X;
621 r.bottom = r.top + FF_AXIS_SIZE_Y;
622 MapDialogRect(hwnd, &r);
624 /* Draw direction axis */
625 data->graphics.ff_axis = CreateWindowW( button_class, NULL, WS_CHILD | WS_VISIBLE,
626 r.left, r.top, r.right - r.left, r.bottom - r.top,
627 hwnd, NULL, NULL, hinst);
630 static void initialize_effects_list(HWND hwnd, struct Joystick* joy)
632 int i;
634 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_RESETCONTENT, 0, 0);
636 for (i=0; i < joy->num_effects; i++)
638 /* Effect names start with GUID_, so we'll skip this part */
639 WCHAR *name = joy->effects[i].info.tszName + 5;
640 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_ADDSTRING, 0, (LPARAM) name);
644 static void ff_handle_joychange(HWND hwnd, struct JoystickData *data)
646 int sel;
648 if (data->num_ff == 0) return;
650 sel = SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_GETCURSEL, 0, 0);
651 data->chosen_joystick = SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_GETITEMDATA, sel, 0);
652 initialize_effects_list(hwnd, &data->joysticks[data->chosen_joystick]);
655 static void ff_handle_effectchange(HWND hwnd, struct Joystick *joy)
657 int sel = SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_GETCURSEL, 0, 0);
659 if (sel < 0) return;
661 joy->chosen_effect = sel;
664 static DWORD WINAPI ff_input_thread(void *param)
666 struct JoystickData *data = param;
667 DIJOYSTATE state;
669 ZeroMemory(&state, sizeof(state));
671 while (!data->stop)
673 int i;
674 struct Joystick *joy = &data->joysticks[data->chosen_joystick];
675 int chosen_effect = joy->chosen_effect;
676 DIEFFECT *dieffect;
677 DWORD flags = DIEP_AXES | DIEP_DIRECTION | DIEP_NORESTART;
678 RECT r;
680 /* Skip this if we have no effects */
681 if (joy->num_effects == 0 || chosen_effect < 0) continue;
683 poll_input(joy, &state);
685 /* Set ff parameters and draw the axis */
686 dieffect = &joy->effects[chosen_effect].params;
687 dieffect->rgdwAxes[0] = state.lX;
688 dieffect->rgdwAxes[1] = state.lY;
690 r.left = FF_AXIS_X + state.lX;
691 r.top = FF_AXIS_Y + state.lY;
692 r.right = r.bottom = 0; /* unused */
693 MapDialogRect(data->graphics.hwnd, &r);
695 SetWindowPos(data->graphics.ff_axis, 0, r.left, r.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
697 for (i=0; i < joy->num_buttons; i++)
698 if (state.rgbButtons[i])
700 IDirectInputEffect_SetParameters(joy->effects[chosen_effect].effect, dieffect, flags);
701 IDirectInputEffect_Start(joy->effects[chosen_effect].effect, 1, 0);
702 break;
705 Sleep(TEST_POLL_TIME);
708 return 0;
711 /***********************************************************************
712 * ff_effects_callback [internal]
713 * Enumerates, creates, sets the some parameters and stores all ff effects
714 * supported by the joystick. Works like enum_callback, counting the effects
715 * first and then storing them.
717 static BOOL CALLBACK ff_effects_callback(const DIEFFECTINFOW *pdei, void *pvRef)
719 HRESULT hr;
720 DIEFFECT dieffect;
721 DWORD axes[2] = {DIJOFS_X, DIJOFS_Y};
722 int direction[2] = {0, 0};
723 struct Joystick *joystick = pvRef;
724 DIRAMPFORCE rforce;
725 DICONSTANTFORCE cforce;
726 DIPERIODIC pforce;
728 if (joystick->effects == NULL)
730 joystick->num_effects += 1;
731 return DIENUM_CONTINUE;
734 hr = IDirectInputDevice8_Acquire(joystick->device);
736 if (FAILED(hr)) return DIENUM_CONTINUE;
738 ZeroMemory(&dieffect, sizeof(dieffect));
740 dieffect.dwSize = sizeof(dieffect);
741 dieffect.dwFlags = DIEFF_CARTESIAN;
742 dieffect.dwDuration = FF_PLAY_TIME;
744 dieffect.cAxes = 2;
745 dieffect.rgdwAxes = axes;
746 dieffect.rglDirection = direction;
748 if (IsEqualGUID(&pdei->guid, &GUID_RampForce))
750 rforce.lStart = 0;
751 rforce.lEnd = DI_FFNOMINALMAX;
753 dieffect.cbTypeSpecificParams = sizeof(rforce);
754 dieffect.lpvTypeSpecificParams = &rforce;
755 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
757 else if (IsEqualGUID(&pdei->guid, &GUID_ConstantForce))
759 cforce.lMagnitude = DI_FFNOMINALMAX;
761 dieffect.cbTypeSpecificParams = sizeof(cforce);
762 dieffect.lpvTypeSpecificParams = &cforce;
763 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
765 else if (IsEqualGUID(&pdei->guid, &GUID_Sine) ||
766 IsEqualGUID(&pdei->guid, &GUID_Square) ||
767 IsEqualGUID(&pdei->guid, &GUID_Triangle) ||
768 IsEqualGUID(&pdei->guid, &GUID_SawtoothUp) ||
769 IsEqualGUID(&pdei->guid, &GUID_SawtoothDown))
771 pforce.dwMagnitude = DI_FFNOMINALMAX;
772 pforce.lOffset = 0;
773 pforce.dwPhase = 0;
774 pforce.dwPeriod = FF_PERIOD_TIME;
776 dieffect.cbTypeSpecificParams = sizeof(pforce);
777 dieffect.lpvTypeSpecificParams = &pforce;
778 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
781 hr = IDirectInputDevice2_CreateEffect(
782 joystick->device, &pdei->guid, &dieffect, &joystick->effects[joystick->cur_effect].effect, NULL);
784 joystick->effects[joystick->cur_effect].params = dieffect;
785 joystick->effects[joystick->cur_effect].info = *pdei;
786 joystick->cur_effect += 1;
788 return DIENUM_CONTINUE;
791 /*********************************************************************
792 * ff_dlgproc [internal]
795 static INT_PTR CALLBACK ff_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
797 static HANDLE thread;
798 static struct JoystickData *data;
799 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
801 switch (msg)
803 case WM_INITDIALOG:
805 int i, cur = 0;
807 data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
809 /* Add joysticks with FF support to the combobox and get the effects */
810 for (i = 0; i < data->num_joysticks; i++)
812 struct Joystick *joy = &data->joysticks[i];
814 if (joy->forcefeedback)
816 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
817 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETITEMDATA, cur, i);
819 cur++;
821 /* Count device effects and then store them */
822 joy->num_effects = 0;
823 joy->effects = NULL;
824 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void *) joy, 0);
825 joy->effects = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Effect) * joy->num_effects);
827 joy->cur_effect = 0;
828 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void*) joy, 0);
829 joy->num_effects = joy->cur_effect;
833 draw_ff_axis(hwnd, data);
835 return TRUE;
838 case WM_COMMAND:
839 switch(wparam)
841 case MAKEWPARAM(IDC_FFSELECTCOMBO, CBN_SELCHANGE):
842 ff_handle_joychange(hwnd, data);
844 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
845 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
846 break;
848 case MAKEWPARAM(IDC_FFEFFECTLIST, LBN_SELCHANGE):
849 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
850 break;
852 return TRUE;
854 case WM_NOTIFY:
855 switch(((LPNMHDR)lparam)->code)
857 case PSN_SETACTIVE:
858 if (data->num_ff > 0)
860 DWORD tid;
862 data->stop = FALSE;
863 /* Set the first joystick as default */
864 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETCURSEL, 0, 0);
865 ff_handle_joychange(hwnd, data);
867 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
868 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
870 thread = CreateThread(NULL, 0, ff_input_thread, (void*) data, 0, &tid);
872 break;
874 case PSN_RESET: /* intentional fall-through */
875 case PSN_KILLACTIVE:
876 /* Stop ff thread */
877 data->stop = TRUE;
878 MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, 0);
879 CloseHandle(thread);
880 break;
882 return TRUE;
884 return FALSE;
887 /******************************************************************************
888 * propsheet_callback [internal]
890 static int CALLBACK propsheet_callback(HWND hwnd, UINT msg, LPARAM lparam)
892 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
893 switch (msg)
895 case PSCB_INITIALIZED:
896 break;
898 return 0;
901 /******************************************************************************
902 * display_cpl_sheets [internal]
904 * Build and display the dialog with all control panel propertysheets
907 static void display_cpl_sheets(HWND parent, struct JoystickData *data)
909 INITCOMMONCONTROLSEX icex;
910 PROPSHEETPAGEW psp[NUM_PROPERTY_PAGES];
911 PROPSHEETHEADERW psh;
912 DWORD id = 0;
914 OleInitialize(NULL);
915 /* Initialize common controls */
916 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
917 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
918 InitCommonControlsEx(&icex);
920 ZeroMemory(&psh, sizeof(psh));
921 ZeroMemory(psp, sizeof(psp));
923 /* Fill out all PROPSHEETPAGE */
924 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
925 psp[id].hInstance = hcpl;
926 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_LIST);
927 psp[id].pfnDlgProc = list_dlgproc;
928 psp[id].lParam = (INT_PTR) data;
929 id++;
931 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
932 psp[id].hInstance = hcpl;
933 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_TEST);
934 psp[id].pfnDlgProc = test_dlgproc;
935 psp[id].lParam = (INT_PTR) data;
936 id++;
938 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
939 psp[id].hInstance = hcpl;
940 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_FORCEFEEDBACK);
941 psp[id].pfnDlgProc = ff_dlgproc;
942 psp[id].lParam = (INT_PTR) data;
943 id++;
945 /* Fill out the PROPSHEETHEADER */
946 psh.dwSize = sizeof (PROPSHEETHEADERW);
947 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
948 psh.hwndParent = parent;
949 psh.hInstance = hcpl;
950 psh.pszCaption = MAKEINTRESOURCEW(IDS_CPL_NAME);
951 psh.nPages = id;
952 psh.u3.ppsp = psp;
953 psh.pfnCallback = propsheet_callback;
955 /* display the dialog */
956 PropertySheetW(&psh);
958 OleUninitialize();
961 /*********************************************************************
962 * CPlApplet (joy.cpl.@)
964 * Control Panel entry point
966 * PARAMS
967 * hWnd [I] Handle for the Control Panel Window
968 * command [I] CPL_* Command
969 * lParam1 [I] first extra Parameter
970 * lParam2 [I] second extra Parameter
972 * RETURNS
973 * Depends on the command
976 LONG CALLBACK CPlApplet(HWND hwnd, UINT command, LPARAM lParam1, LPARAM lParam2)
978 static struct JoystickData data;
979 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd, command, lParam1, lParam2);
981 switch (command)
983 case CPL_INIT:
985 HRESULT hr;
987 /* Initialize dinput */
988 hr = DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8W, (void**)&data.di, NULL);
990 if (FAILED(hr))
992 ERR("Failed to initialize DirectInput: 0x%08x\n", hr);
993 return FALSE;
996 /* Then get all the connected joysticks */
997 initialize_joysticks(&data);
999 return TRUE;
1001 case CPL_GETCOUNT:
1002 return 1;
1004 case CPL_INQUIRE:
1006 CPLINFO *appletInfo = (CPLINFO *) lParam2;
1008 appletInfo->idIcon = ICO_MAIN;
1009 appletInfo->idName = IDS_CPL_NAME;
1010 appletInfo->idInfo = IDS_CPL_INFO;
1011 appletInfo->lData = 0;
1012 return TRUE;
1015 case CPL_DBLCLK:
1016 display_cpl_sheets(hwnd, &data);
1017 break;
1019 case CPL_STOP:
1020 destroy_joysticks(&data);
1022 /* And destroy dinput too */
1023 IDirectInput8_Release(data.di);
1024 break;
1027 return FALSE;