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
34 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(joycpl
);
39 DECLSPEC_HIDDEN HMODULE hcpl
;
41 /*********************************************************************
44 BOOL WINAPI
DllMain(HINSTANCE hdll
, DWORD reason
, LPVOID reserved
)
46 TRACE("(%p, %d, %p)\n", hdll
, reason
, reserved
);
50 case DLL_PROCESS_ATTACH
:
51 DisableThreadLibraryCalls(hdll
);
57 /***********************************************************************
58 * enum_callback [internal]
59 * Enumerates, creates and sets the common data format for all the joystick devices.
60 * First time it checks if space for the joysticks was already reserved
61 * and if not, just counts how many there are.
63 static BOOL CALLBACK
enum_callback(const DIDEVICEINSTANCEW
*instance
, void *context
)
65 struct JoystickData
*data
= context
;
66 struct Joystick
*joystick
;
67 DIPROPRANGE proprange
;
70 if (data
->joysticks
== NULL
)
72 data
->num_joysticks
+= 1;
73 return DIENUM_CONTINUE
;
76 joystick
= &data
->joysticks
[data
->cur_joystick
];
77 data
->cur_joystick
+= 1;
79 IDirectInput8_CreateDevice(data
->di
, &instance
->guidInstance
, &joystick
->device
, NULL
);
80 IDirectInputDevice8_SetDataFormat(joystick
->device
, &c_dfDIJoystick
);
82 joystick
->instance
= *instance
;
84 caps
.dwSize
= sizeof(caps
);
85 IDirectInputDevice8_GetCapabilities(joystick
->device
, &caps
);
87 joystick
->num_buttons
= caps
.dwButtons
;
88 joystick
->num_axes
= caps
.dwAxes
;
89 joystick
->forcefeedback
= caps
.dwFlags
& DIDC_FORCEFEEDBACK
;
90 joystick
->num_effects
= 0;
92 if (joystick
->forcefeedback
) data
->num_ff
++;
94 /* Set axis range to ease the GUI visualization */
95 proprange
.diph
.dwSize
= sizeof(DIPROPRANGE
);
96 proprange
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
97 proprange
.diph
.dwHow
= DIPH_DEVICE
;
98 proprange
.diph
.dwObj
= 0;
99 proprange
.lMin
= TEST_AXIS_MIN
;
100 proprange
.lMax
= TEST_AXIS_MAX
;
102 IDirectInputDevice_SetProperty(joystick
->device
, DIPROP_RANGE
, &proprange
.diph
);
104 return DIENUM_CONTINUE
;
107 /***********************************************************************
108 * initialize_joysticks [internal]
110 static void initialize_joysticks(struct JoystickData
*data
)
112 data
->num_joysticks
= 0;
113 data
->cur_joystick
= 0;
114 IDirectInput8_EnumDevices(data
->di
, DI8DEVCLASS_GAMECTRL
, enum_callback
, data
, DIEDFL_ATTACHEDONLY
);
115 data
->joysticks
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct Joystick
) * data
->num_joysticks
);
117 /* Get all the joysticks */
118 IDirectInput8_EnumDevices(data
->di
, DI8DEVCLASS_GAMECTRL
, enum_callback
, data
, DIEDFL_ATTACHEDONLY
);
121 /***********************************************************************
122 * destroy_joysticks [internal]
124 static void destroy_joysticks(struct JoystickData
*data
)
128 for (i
= 0; i
< data
->num_joysticks
; i
++)
131 if (data
->joysticks
[i
].forcefeedback
&& data
->joysticks
[i
].num_effects
> 0)
133 for (j
= 0; j
< data
->joysticks
[i
].num_effects
; j
++)
134 if (data
->joysticks
[i
].effects
[j
].effect
)
135 IDirectInputEffect_Release(data
->joysticks
[i
].effects
[j
].effect
);
137 HeapFree(GetProcessHeap(), 0, data
->joysticks
[i
].effects
);
140 IDirectInputDevice8_Unacquire(data
->joysticks
[i
].device
);
141 IDirectInputDevice8_Release(data
->joysticks
[i
].device
);
144 HeapFree(GetProcessHeap(), 0, data
->joysticks
);
147 static void initialize_joysticks_list(HWND hwnd
, struct JoystickData
*data
)
151 SendDlgItemMessageW(hwnd
, IDC_JOYSTICKLIST
, LB_RESETCONTENT
, 0, 0);
153 /* Add enumerated joysticks */
154 for (i
= 0; i
< data
->num_joysticks
; i
++)
156 struct Joystick
*joy
= &data
->joysticks
[i
];
157 SendDlgItemMessageW(hwnd
, IDC_JOYSTICKLIST
, LB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
161 /******************************************************************************
162 * get_app_key [internal]
163 * Get the default DirectInput key and the selected app config key.
165 static BOOL
get_app_key(HKEY
*defkey
, HKEY
*appkey
)
169 /* Registry key can be found in HKCU\Software\Wine\DirectInput */
170 if (RegCreateKeyExW(HKEY_CURRENT_USER
, L
"Software\\Wine\\DirectInput\\Joysticks", 0, NULL
, 0,
171 KEY_SET_VALUE
| KEY_READ
, NULL
, defkey
, NULL
))
174 return *defkey
|| *appkey
;
177 /******************************************************************************
178 * set_config_key [internal]
179 * Writes a string value to a registry key, deletes the key if value == NULL
181 static DWORD
set_config_key(HKEY defkey
, HKEY appkey
, const WCHAR
*name
, const WCHAR
*value
, DWORD size
)
185 if (appkey
&& !RegDeleteValueW(appkey
, name
))
188 if (defkey
&& !RegDeleteValueW(defkey
, name
))
193 if (appkey
&& !RegSetValueExW(appkey
, name
, 0, REG_SZ
, (const BYTE
*) value
, (size
+ 1)*sizeof(WCHAR
)))
196 if (defkey
&& !RegSetValueExW(defkey
, name
, 0, REG_SZ
, (const BYTE
*) value
, (size
+ 1)*sizeof(WCHAR
)))
200 return ERROR_FILE_NOT_FOUND
;
203 /******************************************************************************
204 * enable_joystick [internal]
205 * Writes to the DirectInput registry key that enables/disables a joystick
206 * from being enumerated.
208 static void enable_joystick(WCHAR
*joy_name
, BOOL enable
)
212 get_app_key(&hkey
, &appkey
);
215 set_config_key(hkey
, appkey
, joy_name
, L
"disabled", lstrlenW(L
"disabled"));
217 set_config_key(hkey
, appkey
, joy_name
, NULL
, 0);
219 if (hkey
) RegCloseKey(hkey
);
220 if (appkey
) RegCloseKey(appkey
);
223 static void initialize_disabled_joysticks_list(HWND hwnd
)
230 SendDlgItemMessageW(hwnd
, IDC_DISABLEDLIST
, LB_RESETCONTENT
, 0, 0);
232 /* Search for disabled joysticks */
233 get_app_key(&hkey
, &appkey
);
234 RegQueryInfoKeyW(hkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &values
, NULL
, NULL
, NULL
, NULL
);
236 for (i
=0; i
< values
; i
++)
238 DWORD name_len
= MAX_PATH
, data_len
= MAX_PATH
;
239 WCHAR buf_name
[MAX_PATH
+ 9], buf_data
[MAX_PATH
];
241 status
= RegEnumValueW(hkey
, i
, buf_name
, &name_len
, NULL
, NULL
, (BYTE
*) buf_data
, &data_len
);
243 if (status
== ERROR_SUCCESS
&& !lstrcmpW(L
"disabled", buf_data
))
244 SendDlgItemMessageW(hwnd
, IDC_DISABLEDLIST
, LB_ADDSTRING
, 0, (LPARAM
) buf_name
);
247 if (hkey
) RegCloseKey(hkey
);
248 if (appkey
) RegCloseKey(appkey
);
251 /*********************************************************************
252 * list_dlgproc [internal]
255 static INT_PTR CALLBACK
list_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
257 static struct JoystickData
*data
;
258 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
263 data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
265 initialize_joysticks_list(hwnd
, data
);
266 initialize_disabled_joysticks_list(hwnd
);
268 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONENABLE
), FALSE
);
269 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONDISABLE
), FALSE
);
271 /* Store the hwnd to be used with MapDialogRect for unit conversions */
272 data
->graphics
.hwnd
= hwnd
;
279 switch (LOWORD(wparam
))
281 case IDC_BUTTONDISABLE
:
283 int sel
= SendDlgItemMessageW(hwnd
, IDC_JOYSTICKLIST
, LB_GETCURSEL
, 0, 0);
287 enable_joystick(data
->joysticks
[sel
].instance
.tszInstanceName
, FALSE
);
288 initialize_disabled_joysticks_list(hwnd
);
293 case IDC_BUTTONENABLE
:
295 int sel
= SendDlgItemMessageW(hwnd
, IDC_DISABLEDLIST
, LB_GETCURSEL
, 0, 0);
299 WCHAR text
[MAX_PATH
];
300 SendDlgItemMessageW(hwnd
, IDC_DISABLEDLIST
, LB_GETTEXT
, sel
, (LPARAM
) text
);
301 enable_joystick(text
, TRUE
);
302 initialize_disabled_joysticks_list(hwnd
);
307 case IDC_JOYSTICKLIST
:
308 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONENABLE
), FALSE
);
309 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONDISABLE
), TRUE
);
312 case IDC_DISABLEDLIST
:
313 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONENABLE
), TRUE
);
314 EnableWindow(GetDlgItem(hwnd
, IDC_BUTTONDISABLE
), FALSE
);
329 /*********************************************************************
330 * Joystick testing functions
333 static void dump_joy_state(DIJOYSTATE
* st
, int num_buttons
)
336 TRACE("Ax (% 5d,% 5d,% 5d)\n", st
->lX
,st
->lY
, st
->lZ
);
337 TRACE("RAx (% 5d,% 5d,% 5d)\n", st
->lRx
, st
->lRy
, st
->lRz
);
338 TRACE("Slider (% 5d,% 5d)\n", st
->rglSlider
[0], st
->rglSlider
[1]);
339 TRACE("Pov (% 5d,% 5d,% 5d,% 5d)\n", st
->rgdwPOV
[0], st
->rgdwPOV
[1], st
->rgdwPOV
[2], st
->rgdwPOV
[3]);
342 for(i
=0; i
< num_buttons
; i
++)
343 TRACE(" %c",st
->rgbButtons
[i
] ? 'x' : 'o');
347 static void poll_input(const struct Joystick
*joy
, DIJOYSTATE
*state
)
351 hr
= IDirectInputDevice8_Poll(joy
->device
);
353 /* If it failed, try to acquire the joystick */
356 hr
= IDirectInputDevice8_Acquire(joy
->device
);
357 while (hr
== DIERR_INPUTLOST
) hr
= IDirectInputDevice8_Acquire(joy
->device
);
360 if (hr
== DIERR_OTHERAPPHASPRIO
) return;
362 IDirectInputDevice8_GetDeviceState(joy
->device
, sizeof(DIJOYSTATE
), state
);
365 static DWORD WINAPI
input_thread(void *param
)
367 int axes_pos
[TEST_MAX_AXES
][2];
369 struct JoystickData
*data
= param
;
371 /* Setup POV as clock positions
378 int ma
= TEST_AXIS_MAX
;
379 int pov_val
[9] = {0, 4500, 9000, 13500,
380 18000, 22500, 27000, 31500, -1};
381 int pov_pos
[9][2] = { {0, -ma
}, {ma
/2, -ma
/2}, {ma
, 0}, {ma
/2, ma
/2},
382 {0, ma
}, {-ma
/2, ma
/2}, {-ma
, 0}, {-ma
/2, -ma
/2}, {0, 0} };
384 ZeroMemory(&state
, sizeof(state
));
391 poll_input(&data
->joysticks
[data
->chosen_joystick
], &state
);
393 dump_joy_state(&state
, data
->joysticks
[data
->chosen_joystick
].num_buttons
);
395 /* Indicate pressed buttons */
396 for (i
= 0; i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
; i
++)
397 SendMessageW(data
->graphics
.buttons
[i
], BM_SETSTATE
, !!state
.rgbButtons
[i
], 0);
399 /* Indicate axis positions, axes showing are hardcoded for now */
400 axes_pos
[0][0] = state
.lX
;
401 axes_pos
[0][1] = state
.lY
;
402 axes_pos
[1][0] = state
.lRx
;
403 axes_pos
[1][1] = state
.lRy
;
404 axes_pos
[2][0] = state
.lZ
;
405 axes_pos
[2][1] = state
.lRz
;
408 for (j
= 0; j
< ARRAY_SIZE(pov_val
); j
++)
410 if (state
.rgdwPOV
[0] == pov_val
[j
])
412 axes_pos
[3][0] = pov_pos
[j
][0];
413 axes_pos
[3][1] = pov_pos
[j
][1];
417 for (i
= 0; i
< TEST_MAX_AXES
; i
++)
421 r
.left
= (TEST_AXIS_X
+ TEST_NEXT_AXIS_X
*i
+ axes_pos
[i
][0]);
422 r
.top
= (TEST_AXIS_Y
+ axes_pos
[i
][1]);
423 r
.bottom
= r
.right
= 0; /* unused */
424 MapDialogRect(data
->graphics
.hwnd
, &r
);
426 SetWindowPos(data
->graphics
.axes
[i
], 0, r
.left
, r
.top
, 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
429 Sleep(TEST_POLL_TIME
);
435 static void test_handle_joychange(HWND hwnd
, struct JoystickData
*data
)
439 if (data
->num_joysticks
== 0) return;
441 data
->chosen_joystick
= SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_GETCURSEL
, 0, 0);
443 /* Enable only buttons present in the device */
444 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
445 ShowWindow(data
->graphics
.buttons
[i
], i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
);
448 /*********************************************************************
449 * button_number_to_wchar [internal]
450 * Transforms an integer in the interval [0,99] into a 2 character WCHAR string
452 static void button_number_to_wchar(int n
, WCHAR str
[3])
454 str
[1] = n
% 10 + '0';
456 str
[0] = n
% 10 + '0';
460 static void draw_joystick_buttons(HWND hwnd
, struct JoystickData
* data
)
463 int row
= 0, col
= 0;
464 WCHAR button_label
[3];
465 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
467 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
471 if ((i
% TEST_BUTTON_COL_MAX
) == 0 && i
!= 0)
477 r
.left
= (TEST_BUTTON_X
+ TEST_NEXT_BUTTON_X
*col
);
478 r
.top
= (TEST_BUTTON_Y
+ TEST_NEXT_BUTTON_Y
*row
);
479 r
.right
= r
.left
+ TEST_BUTTON_SIZE_X
;
480 r
.bottom
= r
.top
+ TEST_BUTTON_SIZE_Y
;
481 MapDialogRect(hwnd
, &r
);
483 button_number_to_wchar(i
+ 1, button_label
);
485 data
->graphics
.buttons
[i
] = CreateWindowW(L
"Button", button_label
, WS_CHILD
,
486 r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
,
487 hwnd
, NULL
, NULL
, hinst
);
493 static void draw_joystick_axes(HWND hwnd
, struct JoystickData
* data
)
496 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
497 static const WCHAR axes_names
[TEST_MAX_AXES
][7] = { L
"X,Y", L
"Rx,Ry", L
"Z,Rz", L
"POV"};
498 static const DWORD axes_idc
[TEST_MAX_AXES
] = { IDC_TESTGROUPXY
, IDC_TESTGROUPRXRY
,
499 IDC_TESTGROUPZRZ
, IDC_TESTGROUPPOV
};
501 for (i
= 0; i
< TEST_MAX_AXES
; i
++)
504 /* Set axis box name */
505 SetWindowTextW(GetDlgItem(hwnd
, axes_idc
[i
]), axes_names
[i
]);
507 r
.left
= (TEST_AXIS_X
+ TEST_NEXT_AXIS_X
*i
);
509 r
.right
= r
.left
+ TEST_AXIS_SIZE_X
;
510 r
.bottom
= r
.top
+ TEST_AXIS_SIZE_Y
;
511 MapDialogRect(hwnd
, &r
);
513 data
->graphics
.axes
[i
] = CreateWindowW(L
"Button", NULL
, WS_CHILD
| WS_VISIBLE
,
514 r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
,
515 hwnd
, NULL
, NULL
, hinst
);
519 /*********************************************************************
520 * test_dlgproc [internal]
523 static INT_PTR CALLBACK
test_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
525 static HANDLE thread
;
526 static struct JoystickData
*data
;
527 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
535 data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
537 /* Add enumerated joysticks to the combobox */
538 for (i
= 0; i
< data
->num_joysticks
; i
++)
540 struct Joystick
*joy
= &data
->joysticks
[i
];
541 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
544 draw_joystick_buttons(hwnd
, data
);
545 draw_joystick_axes(hwnd
, data
);
553 case MAKEWPARAM(IDC_TESTSELECTCOMBO
, CBN_SELCHANGE
):
554 test_handle_joychange(hwnd
, data
);
560 switch(((LPNMHDR
)lparam
)->code
)
566 /* Initialize input thread */
567 if (data
->num_joysticks
> 0)
571 /* Set the first joystick as default */
572 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_SETCURSEL
, 0, 0);
573 test_handle_joychange(hwnd
, data
);
575 thread
= CreateThread(NULL
, 0, input_thread
, (void*) data
, 0, &tid
);
580 case PSN_RESET
: /* intentional fall-through */
582 /* Stop input thread */
584 MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, 0);
593 /*********************************************************************
594 * Joystick force feedback testing functions
597 static void draw_ff_axis(HWND hwnd
, struct JoystickData
*data
)
599 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
604 r
.right
= r
.left
+ FF_AXIS_SIZE_X
;
605 r
.bottom
= r
.top
+ FF_AXIS_SIZE_Y
;
606 MapDialogRect(hwnd
, &r
);
608 /* Draw direction axis */
609 data
->graphics
.ff_axis
= CreateWindowW(L
"Button", NULL
, WS_CHILD
| WS_VISIBLE
,
610 r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
,
611 hwnd
, NULL
, NULL
, hinst
);
614 static void initialize_effects_list(HWND hwnd
, struct Joystick
* joy
)
618 SendDlgItemMessageW(hwnd
, IDC_FFEFFECTLIST
, LB_RESETCONTENT
, 0, 0);
620 for (i
=0; i
< joy
->num_effects
; i
++)
622 /* Effect names start with GUID_, so we'll skip this part */
623 WCHAR
*name
= joy
->effects
[i
].info
.tszName
+ 5;
624 SendDlgItemMessageW(hwnd
, IDC_FFEFFECTLIST
, LB_ADDSTRING
, 0, (LPARAM
) name
);
628 static void ff_handle_joychange(HWND hwnd
, struct JoystickData
*data
)
632 if (data
->num_ff
== 0) return;
634 sel
= SendDlgItemMessageW(hwnd
, IDC_FFSELECTCOMBO
, CB_GETCURSEL
, 0, 0);
635 data
->chosen_joystick
= SendDlgItemMessageW(hwnd
, IDC_FFSELECTCOMBO
, CB_GETITEMDATA
, sel
, 0);
636 initialize_effects_list(hwnd
, &data
->joysticks
[data
->chosen_joystick
]);
639 static void ff_handle_effectchange(HWND hwnd
, struct Joystick
*joy
)
641 int sel
= SendDlgItemMessageW(hwnd
, IDC_FFEFFECTLIST
, LB_GETCURSEL
, 0, 0);
645 joy
->chosen_effect
= sel
;
648 static DWORD WINAPI
ff_input_thread(void *param
)
650 struct JoystickData
*data
= param
;
653 ZeroMemory(&state
, sizeof(state
));
658 struct Joystick
*joy
= &data
->joysticks
[data
->chosen_joystick
];
659 int chosen_effect
= joy
->chosen_effect
;
661 DWORD flags
= DIEP_AXES
| DIEP_DIRECTION
| DIEP_NORESTART
;
664 Sleep(TEST_POLL_TIME
);
666 /* Skip this if we have no effects */
667 if (joy
->num_effects
== 0 || chosen_effect
< 0) continue;
669 poll_input(joy
, &state
);
671 /* Set ff parameters and draw the axis */
672 dieffect
= &joy
->effects
[chosen_effect
].params
;
673 dieffect
->rgdwAxes
[0] = state
.lX
;
674 dieffect
->rgdwAxes
[1] = state
.lY
;
676 r
.left
= FF_AXIS_X
+ state
.lX
;
677 r
.top
= FF_AXIS_Y
+ state
.lY
;
678 r
.right
= r
.bottom
= 0; /* unused */
679 MapDialogRect(data
->graphics
.hwnd
, &r
);
681 SetWindowPos(data
->graphics
.ff_axis
, 0, r
.left
, r
.top
, 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
683 for (i
=0; i
< joy
->num_buttons
; i
++)
684 if (state
.rgbButtons
[i
])
686 IDirectInputEffect_SetParameters(joy
->effects
[chosen_effect
].effect
, dieffect
, flags
);
687 IDirectInputEffect_Start(joy
->effects
[chosen_effect
].effect
, 1, 0);
695 /***********************************************************************
696 * ff_effects_callback [internal]
697 * Enumerates, creates, sets the some parameters and stores all ff effects
698 * supported by the joystick. Works like enum_callback, counting the effects
699 * first and then storing them.
701 static BOOL CALLBACK
ff_effects_callback(const DIEFFECTINFOW
*pdei
, void *pvRef
)
705 DWORD axes
[2] = {DIJOFS_X
, DIJOFS_Y
};
706 int direction
[2] = {0, 0};
707 struct Joystick
*joystick
= pvRef
;
709 DICONSTANTFORCE cforce
;
713 if (joystick
->effects
== NULL
)
715 joystick
->num_effects
+= 1;
716 return DIENUM_CONTINUE
;
719 hr
= IDirectInputDevice8_Acquire(joystick
->device
);
721 if (FAILED(hr
)) return DIENUM_CONTINUE
;
723 ZeroMemory(&dieffect
, sizeof(dieffect
));
725 dieffect
.dwSize
= sizeof(dieffect
);
726 dieffect
.dwFlags
= DIEFF_CARTESIAN
;
727 dieffect
.dwDuration
= FF_PLAY_TIME
;
730 dieffect
.rgdwAxes
= axes
;
731 dieffect
.rglDirection
= direction
;
733 if (IsEqualGUID(&pdei
->guid
, &GUID_RampForce
))
736 rforce
.lEnd
= DI_FFNOMINALMAX
;
738 dieffect
.cbTypeSpecificParams
= sizeof(rforce
);
739 dieffect
.lpvTypeSpecificParams
= &rforce
;
740 dieffect
.dwFlags
|= DIEP_TYPESPECIFICPARAMS
;
742 else if (IsEqualGUID(&pdei
->guid
, &GUID_ConstantForce
))
744 cforce
.lMagnitude
= DI_FFNOMINALMAX
;
746 dieffect
.cbTypeSpecificParams
= sizeof(cforce
);
747 dieffect
.lpvTypeSpecificParams
= &cforce
;
748 dieffect
.dwFlags
|= DIEP_TYPESPECIFICPARAMS
;
750 else if (IsEqualGUID(&pdei
->guid
, &GUID_Sine
) ||
751 IsEqualGUID(&pdei
->guid
, &GUID_Square
) ||
752 IsEqualGUID(&pdei
->guid
, &GUID_Triangle
) ||
753 IsEqualGUID(&pdei
->guid
, &GUID_SawtoothUp
) ||
754 IsEqualGUID(&pdei
->guid
, &GUID_SawtoothDown
))
756 pforce
.dwMagnitude
= DI_FFNOMINALMAX
;
759 pforce
.dwPeriod
= FF_PERIOD_TIME
;
761 dieffect
.cbTypeSpecificParams
= sizeof(pforce
);
762 dieffect
.lpvTypeSpecificParams
= &pforce
;
763 dieffect
.dwFlags
|= DIEP_TYPESPECIFICPARAMS
;
765 else if (IsEqualGUID(&pdei
->guid
, &GUID_Spring
) ||
766 IsEqualGUID(&pdei
->guid
, &GUID_Damper
) ||
767 IsEqualGUID(&pdei
->guid
, &GUID_Inertia
) ||
768 IsEqualGUID(&pdei
->guid
, &GUID_Friction
))
770 cdforce
.dwPositiveSaturation
= 10000;
771 cdforce
.dwNegativeSaturation
= 10000;
772 cdforce
.lPositiveCoefficient
= 10000;
773 cdforce
.lNegativeCoefficient
= 10000;
774 cdforce
.lDeadBand
= 0;
777 dieffect
.cbTypeSpecificParams
= sizeof(cdforce
);
778 dieffect
.lpvTypeSpecificParams
= &cdforce
;
779 dieffect
.dwFlags
|= DIEP_TYPESPECIFICPARAMS
;
782 hr
= IDirectInputDevice2_CreateEffect(
783 joystick
->device
, &pdei
->guid
, &dieffect
, &joystick
->effects
[joystick
->cur_effect
].effect
, NULL
);
785 joystick
->effects
[joystick
->cur_effect
].params
= dieffect
;
786 joystick
->effects
[joystick
->cur_effect
].info
= *pdei
;
787 joystick
->cur_effect
+= 1;
789 return DIENUM_CONTINUE
;
792 /*********************************************************************
793 * ff_dlgproc [internal]
796 static INT_PTR CALLBACK
ff_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
798 static HANDLE thread
;
799 static struct JoystickData
*data
;
800 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
808 data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
810 /* Add joysticks with FF support to the combobox and get the effects */
811 for (i
= 0; i
< data
->num_joysticks
; i
++)
813 struct Joystick
*joy
= &data
->joysticks
[i
];
815 if (joy
->forcefeedback
)
817 SendDlgItemMessageW(hwnd
, IDC_FFSELECTCOMBO
, CB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
818 SendDlgItemMessageW(hwnd
, IDC_FFSELECTCOMBO
, CB_SETITEMDATA
, cur
, i
);
822 /* Count device effects and then store them */
823 joy
->num_effects
= 0;
825 IDirectInputDevice8_EnumEffects(joy
->device
, ff_effects_callback
, (void *) joy
, 0);
826 joy
->effects
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct Effect
) * joy
->num_effects
);
829 IDirectInputDevice8_EnumEffects(joy
->device
, ff_effects_callback
, (void*) joy
, 0);
830 joy
->num_effects
= joy
->cur_effect
;
834 draw_ff_axis(hwnd
, data
);
842 case MAKEWPARAM(IDC_FFSELECTCOMBO
, CBN_SELCHANGE
):
843 ff_handle_joychange(hwnd
, data
);
845 SendDlgItemMessageW(hwnd
, IDC_FFEFFECTLIST
, LB_SETCURSEL
, 0, 0);
846 ff_handle_effectchange(hwnd
, &data
->joysticks
[data
->chosen_joystick
]);
849 case MAKEWPARAM(IDC_FFEFFECTLIST
, LBN_SELCHANGE
):
850 ff_handle_effectchange(hwnd
, &data
->joysticks
[data
->chosen_joystick
]);
856 switch(((LPNMHDR
)lparam
)->code
)
859 if (data
->num_ff
> 0)
864 /* Set the first joystick as default */
865 SendDlgItemMessageW(hwnd
, IDC_FFSELECTCOMBO
, CB_SETCURSEL
, 0, 0);
866 ff_handle_joychange(hwnd
, data
);
868 SendDlgItemMessageW(hwnd
, IDC_FFEFFECTLIST
, LB_SETCURSEL
, 0, 0);
869 ff_handle_effectchange(hwnd
, &data
->joysticks
[data
->chosen_joystick
]);
871 thread
= CreateThread(NULL
, 0, ff_input_thread
, (void*) data
, 0, &tid
);
875 case PSN_RESET
: /* intentional fall-through */
879 MsgWaitForMultipleObjects(1, &thread
, FALSE
, INFINITE
, 0);
888 /******************************************************************************
889 * propsheet_callback [internal]
891 static int CALLBACK
propsheet_callback(HWND hwnd
, UINT msg
, LPARAM lparam
)
893 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
896 case PSCB_INITIALIZED
:
902 /******************************************************************************
903 * display_cpl_sheets [internal]
905 * Build and display the dialog with all control panel propertysheets
908 static void display_cpl_sheets(HWND parent
, struct JoystickData
*data
)
910 INITCOMMONCONTROLSEX icex
;
911 PROPSHEETPAGEW psp
[NUM_PROPERTY_PAGES
];
912 PROPSHEETHEADERW psh
;
916 /* Initialize common controls */
917 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
918 icex
.dwICC
= ICC_LISTVIEW_CLASSES
| ICC_BAR_CLASSES
;
919 InitCommonControlsEx(&icex
);
921 ZeroMemory(&psh
, sizeof(psh
));
922 ZeroMemory(psp
, sizeof(psp
));
924 /* Fill out all PROPSHEETPAGE */
925 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
926 psp
[id
].hInstance
= hcpl
;
927 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_LIST
);
928 psp
[id
].pfnDlgProc
= list_dlgproc
;
929 psp
[id
].lParam
= (INT_PTR
) data
;
932 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
933 psp
[id
].hInstance
= hcpl
;
934 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_TEST
);
935 psp
[id
].pfnDlgProc
= test_dlgproc
;
936 psp
[id
].lParam
= (INT_PTR
) data
;
939 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
940 psp
[id
].hInstance
= hcpl
;
941 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_FORCEFEEDBACK
);
942 psp
[id
].pfnDlgProc
= ff_dlgproc
;
943 psp
[id
].lParam
= (INT_PTR
) data
;
946 /* Fill out the PROPSHEETHEADER */
947 psh
.dwSize
= sizeof (PROPSHEETHEADERW
);
948 psh
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_USEICONID
| PSH_USECALLBACK
;
949 psh
.hwndParent
= parent
;
950 psh
.hInstance
= hcpl
;
951 psh
.pszCaption
= MAKEINTRESOURCEW(IDS_CPL_NAME
);
954 psh
.pfnCallback
= propsheet_callback
;
956 /* display the dialog */
957 PropertySheetW(&psh
);
962 /*********************************************************************
963 * CPlApplet (joy.cpl.@)
965 * Control Panel entry point
968 * hWnd [I] Handle for the Control Panel Window
969 * command [I] CPL_* Command
970 * lParam1 [I] first extra Parameter
971 * lParam2 [I] second extra Parameter
974 * Depends on the command
977 LONG CALLBACK
CPlApplet(HWND hwnd
, UINT command
, LPARAM lParam1
, LPARAM lParam2
)
979 static struct JoystickData data
;
980 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd
, command
, lParam1
, lParam2
);
988 /* Initialize dinput */
989 hr
= DirectInput8Create(GetModuleHandleW(NULL
), DIRECTINPUT_VERSION
, &IID_IDirectInput8W
, (void**)&data
.di
, NULL
);
993 ERR("Failed to initialize DirectInput: 0x%08x\n", hr
);
997 /* Then get all the connected joysticks */
998 initialize_joysticks(&data
);
1007 CPLINFO
*appletInfo
= (CPLINFO
*) lParam2
;
1009 appletInfo
->idIcon
= ICO_MAIN
;
1010 appletInfo
->idName
= IDS_CPL_NAME
;
1011 appletInfo
->idInfo
= IDS_CPL_INFO
;
1012 appletInfo
->lData
= 0;
1017 display_cpl_sheets(hwnd
, &data
);
1021 destroy_joysticks(&data
);
1023 /* And destroy dinput too */
1024 IDirectInput8_Release(data
.di
);