kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / joy.cpl / main.c
blob32dfad0eb5c7d55650ba1722a4cd1e31ba7f8777
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 LSTATUS status;
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 status = RegEnumValueW(hkey, i, buf_name, &name_len, NULL, NULL, (BYTE*) buf_data, &data_len);
253 if (status == ERROR_SUCCESS && !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 < ARRAY_SIZE(pov_val); 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;
727 DICONDITION cdforce;
729 if (joystick->effects == NULL)
731 joystick->num_effects += 1;
732 return DIENUM_CONTINUE;
735 hr = IDirectInputDevice8_Acquire(joystick->device);
737 if (FAILED(hr)) return DIENUM_CONTINUE;
739 ZeroMemory(&dieffect, sizeof(dieffect));
741 dieffect.dwSize = sizeof(dieffect);
742 dieffect.dwFlags = DIEFF_CARTESIAN;
743 dieffect.dwDuration = FF_PLAY_TIME;
745 dieffect.cAxes = 2;
746 dieffect.rgdwAxes = axes;
747 dieffect.rglDirection = direction;
749 if (IsEqualGUID(&pdei->guid, &GUID_RampForce))
751 rforce.lStart = 0;
752 rforce.lEnd = DI_FFNOMINALMAX;
754 dieffect.cbTypeSpecificParams = sizeof(rforce);
755 dieffect.lpvTypeSpecificParams = &rforce;
756 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
758 else if (IsEqualGUID(&pdei->guid, &GUID_ConstantForce))
760 cforce.lMagnitude = DI_FFNOMINALMAX;
762 dieffect.cbTypeSpecificParams = sizeof(cforce);
763 dieffect.lpvTypeSpecificParams = &cforce;
764 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
766 else if (IsEqualGUID(&pdei->guid, &GUID_Sine) ||
767 IsEqualGUID(&pdei->guid, &GUID_Square) ||
768 IsEqualGUID(&pdei->guid, &GUID_Triangle) ||
769 IsEqualGUID(&pdei->guid, &GUID_SawtoothUp) ||
770 IsEqualGUID(&pdei->guid, &GUID_SawtoothDown))
772 pforce.dwMagnitude = DI_FFNOMINALMAX;
773 pforce.lOffset = 0;
774 pforce.dwPhase = 0;
775 pforce.dwPeriod = FF_PERIOD_TIME;
777 dieffect.cbTypeSpecificParams = sizeof(pforce);
778 dieffect.lpvTypeSpecificParams = &pforce;
779 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
781 else if (IsEqualGUID(&pdei->guid, &GUID_Spring) ||
782 IsEqualGUID(&pdei->guid, &GUID_Damper) ||
783 IsEqualGUID(&pdei->guid, &GUID_Inertia) ||
784 IsEqualGUID(&pdei->guid, &GUID_Friction))
786 cdforce.dwPositiveSaturation = 10000;
787 cdforce.dwNegativeSaturation = 10000;
788 cdforce.lPositiveCoefficient = 10000;
789 cdforce.lNegativeCoefficient = 10000;
790 cdforce.lDeadBand = 0;
791 cdforce.lOffset = 0;
793 dieffect.cbTypeSpecificParams = sizeof(cdforce);
794 dieffect.lpvTypeSpecificParams = &cdforce;
795 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
798 hr = IDirectInputDevice2_CreateEffect(
799 joystick->device, &pdei->guid, &dieffect, &joystick->effects[joystick->cur_effect].effect, NULL);
801 joystick->effects[joystick->cur_effect].params = dieffect;
802 joystick->effects[joystick->cur_effect].info = *pdei;
803 joystick->cur_effect += 1;
805 return DIENUM_CONTINUE;
808 /*********************************************************************
809 * ff_dlgproc [internal]
812 static INT_PTR CALLBACK ff_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
814 static HANDLE thread;
815 static struct JoystickData *data;
816 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
818 switch (msg)
820 case WM_INITDIALOG:
822 int i, cur = 0;
824 data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
826 /* Add joysticks with FF support to the combobox and get the effects */
827 for (i = 0; i < data->num_joysticks; i++)
829 struct Joystick *joy = &data->joysticks[i];
831 if (joy->forcefeedback)
833 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
834 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETITEMDATA, cur, i);
836 cur++;
838 /* Count device effects and then store them */
839 joy->num_effects = 0;
840 joy->effects = NULL;
841 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void *) joy, 0);
842 joy->effects = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Effect) * joy->num_effects);
844 joy->cur_effect = 0;
845 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void*) joy, 0);
846 joy->num_effects = joy->cur_effect;
850 draw_ff_axis(hwnd, data);
852 return TRUE;
855 case WM_COMMAND:
856 switch(wparam)
858 case MAKEWPARAM(IDC_FFSELECTCOMBO, CBN_SELCHANGE):
859 ff_handle_joychange(hwnd, data);
861 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
862 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
863 break;
865 case MAKEWPARAM(IDC_FFEFFECTLIST, LBN_SELCHANGE):
866 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
867 break;
869 return TRUE;
871 case WM_NOTIFY:
872 switch(((LPNMHDR)lparam)->code)
874 case PSN_SETACTIVE:
875 if (data->num_ff > 0)
877 DWORD tid;
879 data->stop = FALSE;
880 /* Set the first joystick as default */
881 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETCURSEL, 0, 0);
882 ff_handle_joychange(hwnd, data);
884 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
885 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
887 thread = CreateThread(NULL, 0, ff_input_thread, (void*) data, 0, &tid);
889 break;
891 case PSN_RESET: /* intentional fall-through */
892 case PSN_KILLACTIVE:
893 /* Stop ff thread */
894 data->stop = TRUE;
895 MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, 0);
896 CloseHandle(thread);
897 break;
899 return TRUE;
901 return FALSE;
904 /******************************************************************************
905 * propsheet_callback [internal]
907 static int CALLBACK propsheet_callback(HWND hwnd, UINT msg, LPARAM lparam)
909 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
910 switch (msg)
912 case PSCB_INITIALIZED:
913 break;
915 return 0;
918 /******************************************************************************
919 * display_cpl_sheets [internal]
921 * Build and display the dialog with all control panel propertysheets
924 static void display_cpl_sheets(HWND parent, struct JoystickData *data)
926 INITCOMMONCONTROLSEX icex;
927 PROPSHEETPAGEW psp[NUM_PROPERTY_PAGES];
928 PROPSHEETHEADERW psh;
929 DWORD id = 0;
931 OleInitialize(NULL);
932 /* Initialize common controls */
933 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
934 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
935 InitCommonControlsEx(&icex);
937 ZeroMemory(&psh, sizeof(psh));
938 ZeroMemory(psp, sizeof(psp));
940 /* Fill out all PROPSHEETPAGE */
941 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
942 psp[id].hInstance = hcpl;
943 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_LIST);
944 psp[id].pfnDlgProc = list_dlgproc;
945 psp[id].lParam = (INT_PTR) data;
946 id++;
948 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
949 psp[id].hInstance = hcpl;
950 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_TEST);
951 psp[id].pfnDlgProc = test_dlgproc;
952 psp[id].lParam = (INT_PTR) data;
953 id++;
955 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
956 psp[id].hInstance = hcpl;
957 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_FORCEFEEDBACK);
958 psp[id].pfnDlgProc = ff_dlgproc;
959 psp[id].lParam = (INT_PTR) data;
960 id++;
962 /* Fill out the PROPSHEETHEADER */
963 psh.dwSize = sizeof (PROPSHEETHEADERW);
964 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
965 psh.hwndParent = parent;
966 psh.hInstance = hcpl;
967 psh.pszCaption = MAKEINTRESOURCEW(IDS_CPL_NAME);
968 psh.nPages = id;
969 psh.u3.ppsp = psp;
970 psh.pfnCallback = propsheet_callback;
972 /* display the dialog */
973 PropertySheetW(&psh);
975 OleUninitialize();
978 /*********************************************************************
979 * CPlApplet (joy.cpl.@)
981 * Control Panel entry point
983 * PARAMS
984 * hWnd [I] Handle for the Control Panel Window
985 * command [I] CPL_* Command
986 * lParam1 [I] first extra Parameter
987 * lParam2 [I] second extra Parameter
989 * RETURNS
990 * Depends on the command
993 LONG CALLBACK CPlApplet(HWND hwnd, UINT command, LPARAM lParam1, LPARAM lParam2)
995 static struct JoystickData data;
996 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd, command, lParam1, lParam2);
998 switch (command)
1000 case CPL_INIT:
1002 HRESULT hr;
1004 /* Initialize dinput */
1005 hr = DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8W, (void**)&data.di, NULL);
1007 if (FAILED(hr))
1009 ERR("Failed to initialize DirectInput: 0x%08x\n", hr);
1010 return FALSE;
1013 /* Then get all the connected joysticks */
1014 initialize_joysticks(&data);
1016 return TRUE;
1018 case CPL_GETCOUNT:
1019 return 1;
1021 case CPL_INQUIRE:
1023 CPLINFO *appletInfo = (CPLINFO *) lParam2;
1025 appletInfo->idIcon = ICO_MAIN;
1026 appletInfo->idName = IDS_CPL_NAME;
1027 appletInfo->idInfo = IDS_CPL_INFO;
1028 appletInfo->lData = 0;
1029 return TRUE;
1032 case CPL_DBLCLK:
1033 display_cpl_sheets(hwnd, &data);
1034 break;
1036 case CPL_STOP:
1037 destroy_joysticks(&data);
1039 /* And destroy dinput too */
1040 IDirectInput8_Release(data.di);
1041 break;
1044 return FALSE;