d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / joy.cpl / main.c
blob53c0eaebc1d86feb3e4a1d9c577103fde2c3fd43
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;
725 if (joystick->effects == NULL)
727 joystick->num_effects += 1;
728 return DIENUM_CONTINUE;
731 hr = IDirectInputDevice8_Acquire(joystick->device);
733 if (FAILED(hr)) return DIENUM_CONTINUE;
735 ZeroMemory(&dieffect, sizeof(dieffect));
737 dieffect.dwSize = sizeof(dieffect);
738 dieffect.dwFlags = DIEFF_CARTESIAN;
739 dieffect.dwDuration = FF_PLAY_TIME;
741 dieffect.cAxes = 2;
742 dieffect.rgdwAxes = axes;
743 dieffect.rglDirection = direction;
745 if (IsEqualGUID(&pdei->guid, &GUID_RampForce))
747 DIRAMPFORCE rforce;
749 rforce.lStart = 0;
750 rforce.lEnd = DI_FFNOMINALMAX;
752 dieffect.cbTypeSpecificParams = sizeof(rforce);
753 dieffect.lpvTypeSpecificParams = &rforce;
754 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
756 else if (IsEqualGUID(&pdei->guid, &GUID_ConstantForce))
758 DICONSTANTFORCE cforce;
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 DIPERIODIC pforce;
774 pforce.dwMagnitude = DI_FFNOMINALMAX;
775 pforce.lOffset = 0;
776 pforce.dwPhase = 0;
777 pforce.dwPeriod = FF_PERIOD_TIME;
779 dieffect.cbTypeSpecificParams = sizeof(pforce);
780 dieffect.lpvTypeSpecificParams = &pforce;
781 dieffect.dwFlags |= DIEP_TYPESPECIFICPARAMS;
784 hr = IDirectInputDevice2_CreateEffect(
785 joystick->device, &pdei->guid, &dieffect, &joystick->effects[joystick->cur_effect].effect, NULL);
787 joystick->effects[joystick->cur_effect].params = dieffect;
788 joystick->effects[joystick->cur_effect].info = *pdei;
789 joystick->cur_effect += 1;
791 return DIENUM_CONTINUE;
794 /*********************************************************************
795 * ff_dlgproc [internal]
798 static INT_PTR CALLBACK ff_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
800 static HANDLE thread;
801 static struct JoystickData *data;
802 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
804 switch (msg)
806 case WM_INITDIALOG:
808 int i, cur = 0;
810 data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
812 /* Add joysticks with FF support to the combobox and get the effects */
813 for (i = 0; i < data->num_joysticks; i++)
815 struct Joystick *joy = &data->joysticks[i];
817 if (joy->forcefeedback)
819 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
820 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETITEMDATA, cur, i);
822 cur++;
824 /* Count device effects and then store them */
825 joy->num_effects = 0;
826 joy->effects = NULL;
827 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void *) joy, 0);
828 joy->effects = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Effect) * joy->num_effects);
830 joy->cur_effect = 0;
831 IDirectInputDevice8_EnumEffects(joy->device, ff_effects_callback, (void*) joy, 0);
832 joy->num_effects = joy->cur_effect;
836 draw_ff_axis(hwnd, data);
838 return TRUE;
841 case WM_COMMAND:
842 switch(wparam)
844 case MAKEWPARAM(IDC_FFSELECTCOMBO, CBN_SELCHANGE):
845 ff_handle_joychange(hwnd, data);
847 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
848 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
849 break;
851 case MAKEWPARAM(IDC_FFEFFECTLIST, LBN_SELCHANGE):
852 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
853 break;
855 return TRUE;
857 case WM_NOTIFY:
858 switch(((LPNMHDR)lparam)->code)
860 case PSN_SETACTIVE:
861 if (data->num_ff > 0)
863 DWORD tid;
865 data->stop = FALSE;
866 /* Set the first joystick as default */
867 SendDlgItemMessageW(hwnd, IDC_FFSELECTCOMBO, CB_SETCURSEL, 0, 0);
868 ff_handle_joychange(hwnd, data);
870 SendDlgItemMessageW(hwnd, IDC_FFEFFECTLIST, LB_SETCURSEL, 0, 0);
871 ff_handle_effectchange(hwnd, &data->joysticks[data->chosen_joystick]);
873 thread = CreateThread(NULL, 0, ff_input_thread, (void*) data, 0, &tid);
875 break;
877 case PSN_RESET: /* intentional fall-through */
878 case PSN_KILLACTIVE:
879 /* Stop ff thread */
880 data->stop = TRUE;
881 MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, 0);
882 CloseHandle(thread);
883 break;
885 return TRUE;
887 return FALSE;
890 /******************************************************************************
891 * propsheet_callback [internal]
893 static int CALLBACK propsheet_callback(HWND hwnd, UINT msg, LPARAM lparam)
895 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
896 switch (msg)
898 case PSCB_INITIALIZED:
899 break;
901 return 0;
904 /******************************************************************************
905 * display_cpl_sheets [internal]
907 * Build and display the dialog with all control panel propertysheets
910 static void display_cpl_sheets(HWND parent, struct JoystickData *data)
912 INITCOMMONCONTROLSEX icex;
913 PROPSHEETPAGEW psp[NUM_PROPERTY_PAGES];
914 PROPSHEETHEADERW psh;
915 DWORD id = 0;
917 OleInitialize(NULL);
918 /* Initialize common controls */
919 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
920 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
921 InitCommonControlsEx(&icex);
923 ZeroMemory(&psh, sizeof(psh));
924 ZeroMemory(psp, sizeof(psp));
926 /* Fill out all PROPSHEETPAGE */
927 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
928 psp[id].hInstance = hcpl;
929 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_LIST);
930 psp[id].pfnDlgProc = list_dlgproc;
931 psp[id].lParam = (INT_PTR) data;
932 id++;
934 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
935 psp[id].hInstance = hcpl;
936 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_TEST);
937 psp[id].pfnDlgProc = test_dlgproc;
938 psp[id].lParam = (INT_PTR) data;
939 id++;
941 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
942 psp[id].hInstance = hcpl;
943 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_FORCEFEEDBACK);
944 psp[id].pfnDlgProc = ff_dlgproc;
945 psp[id].lParam = (INT_PTR) data;
946 id++;
948 /* Fill out the PROPSHEETHEADER */
949 psh.dwSize = sizeof (PROPSHEETHEADERW);
950 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
951 psh.hwndParent = parent;
952 psh.hInstance = hcpl;
953 psh.pszCaption = MAKEINTRESOURCEW(IDS_CPL_NAME);
954 psh.nPages = id;
955 psh.u3.ppsp = psp;
956 psh.pfnCallback = propsheet_callback;
958 /* display the dialog */
959 PropertySheetW(&psh);
961 OleUninitialize();
964 /*********************************************************************
965 * CPlApplet (joy.cpl.@)
967 * Control Panel entry point
969 * PARAMS
970 * hWnd [I] Handle for the Control Panel Window
971 * command [I] CPL_* Command
972 * lParam1 [I] first extra Parameter
973 * lParam2 [I] second extra Parameter
975 * RETURNS
976 * Depends on the command
979 LONG CALLBACK CPlApplet(HWND hwnd, UINT command, LPARAM lParam1, LPARAM lParam2)
981 static struct JoystickData data;
982 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd, command, lParam1, lParam2);
984 switch (command)
986 case CPL_INIT:
988 HRESULT hr;
990 /* Initialize dinput */
991 hr = DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8W, (void**)&data.di, NULL);
993 if (FAILED(hr))
995 ERR("Failed to initialize DirectInput: 0x%08x\n", hr);
996 return FALSE;
999 /* Then get all the connected joysticks */
1000 initialize_joysticks(&data);
1002 return TRUE;
1004 case CPL_GETCOUNT:
1005 return 1;
1007 case CPL_INQUIRE:
1009 CPLINFO *appletInfo = (CPLINFO *) lParam2;
1011 appletInfo->idIcon = ICO_MAIN;
1012 appletInfo->idName = IDS_CPL_NAME;
1013 appletInfo->idInfo = IDS_CPL_INFO;
1014 appletInfo->lData = 0;
1015 return TRUE;
1018 case CPL_DBLCLK:
1019 display_cpl_sheets(hwnd, &data);
1020 break;
1022 case CPL_STOP:
1023 destroy_joysticks(&data);
1025 /* And destroy dinput too */
1026 IDirectInput8_Release(data.di);
1027 break;
1030 return FALSE;