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_WINE_PREATTACH
:
51 return FALSE
; /* prefer native version */
53 case DLL_PROCESS_ATTACH
:
54 DisableThreadLibraryCalls(hdll
);
60 /***********************************************************************
61 * enum_callback [internal]
62 * Enumerates, creates and sets the common data format for all the joystick devices.
63 * First time it checks if space for the joysticks was already reserved
64 * and if not, just counts how many there are.
66 static BOOL CALLBACK
enum_callback(const DIDEVICEINSTANCEW
*instance
, void *context
)
68 struct JoystickData
*data
= context
;
69 struct Joystick
*joystick
;
72 if (data
->joysticks
== NULL
)
74 data
->num_joysticks
+= 1;
75 return DIENUM_CONTINUE
;
78 joystick
= &data
->joysticks
[data
->cur_joystick
];
79 data
->cur_joystick
+= 1;
81 IDirectInput8_CreateDevice(data
->di
, &instance
->guidInstance
, &joystick
->device
, NULL
);
82 IDirectInputDevice8_SetDataFormat(joystick
->device
, &c_dfDIJoystick
);
84 joystick
->instance
= *instance
;
86 caps
.dwSize
= sizeof(caps
);
87 IDirectInputDevice8_GetCapabilities(joystick
->device
, &caps
);
89 joystick
->num_buttons
= caps
.dwButtons
;
90 joystick
->num_axes
= caps
.dwAxes
;
92 return DIENUM_CONTINUE
;
95 /***********************************************************************
96 * initialize_joysticks [internal]
98 static void initialize_joysticks(struct JoystickData
*data
)
100 data
->num_joysticks
= 0;
101 data
->cur_joystick
= 0;
102 IDirectInput8_EnumDevices(data
->di
, DI8DEVCLASS_GAMECTRL
, enum_callback
, data
, DIEDFL_ATTACHEDONLY
);
103 data
->joysticks
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct Joystick
) * data
->num_joysticks
);
105 /* Get all the joysticks */
106 IDirectInput8_EnumDevices(data
->di
, DI8DEVCLASS_GAMECTRL
, enum_callback
, data
, DIEDFL_ATTACHEDONLY
);
109 /***********************************************************************
110 * destroy_joysticks [internal]
112 static void destroy_joysticks(struct JoystickData
*data
)
116 for (i
= 0; i
< data
->num_joysticks
; i
++)
118 IDirectInputDevice8_Unacquire(data
->joysticks
[i
].device
);
119 IDirectInputDevice8_Release(data
->joysticks
[i
].device
);
122 HeapFree(GetProcessHeap(), 0, data
->joysticks
);
125 /*********************************************************************
126 * list_dlgproc [internal]
129 static INT_PTR CALLBACK
list_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
131 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
137 struct JoystickData
*data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
139 /* Set dialog information */
140 for (i
= 0; i
< data
->num_joysticks
; i
++)
142 struct Joystick
*joy
= &data
->joysticks
[i
];
143 SendDlgItemMessageW(hwnd
, IDC_JOYSTICKLIST
, LB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
151 switch (LOWORD(wparam
))
153 case IDC_BUTTONDISABLE
:
154 FIXME("Disable selected joystick from being enumerated\n");
157 case IDC_BUTTONENABLE
:
158 FIXME("Re-Enable selected joystick\n");
173 /*********************************************************************
174 * Joystick testing functions
177 static void dump_joy_state(DIJOYSTATE
* st
, int num_buttons
)
180 TRACE("Ax (% 5d,% 5d,% 5d)\n", st
->lX
,st
->lY
, st
->lZ
);
181 TRACE("RAx (% 5d,% 5d,% 5d)\n", st
->lRx
, st
->lRy
, st
->lRz
);
182 TRACE("Slider (% 5d,% 5d)\n", st
->rglSlider
[0], st
->rglSlider
[1]);
183 TRACE("Pov (% 5d,% 5d,% 5d,% 5d)\n", st
->rgdwPOV
[0], st
->rgdwPOV
[1], st
->rgdwPOV
[2], st
->rgdwPOV
[3]);
186 for(i
=0; i
< num_buttons
; i
++)
187 TRACE(" %c",st
->rgbButtons
[i
] ? 'x' : 'o');
191 static void poll_input(const struct Joystick
*joy
, DIJOYSTATE
*state
)
195 hr
= IDirectInputDevice8_Poll(joy
->device
);
197 /* If it failed, try to acquire the joystick */
200 hr
= IDirectInputDevice8_Acquire(joy
->device
);
201 while (hr
== DIERR_INPUTLOST
) hr
= IDirectInputDevice8_Acquire(joy
->device
);
204 if (hr
== DIERR_OTHERAPPHASPRIO
) return;
206 IDirectInputDevice8_GetDeviceState(joy
->device
, sizeof(DIJOYSTATE
), state
);
209 static DWORD WINAPI
input_thread(void *param
)
211 int axes_pos
[TEST_MAX_AXES
][2];
213 struct JoystickData
*data
= param
;
215 ZeroMemory(&state
, sizeof(state
));
220 poll_input(&data
->joysticks
[data
->chosen_joystick
], &state
);
222 dump_joy_state(&state
, data
->joysticks
[data
->chosen_joystick
].num_buttons
);
224 /* Indicate pressed buttons */
225 for (i
= 0; i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
; i
++)
226 if (state
.rgbButtons
[i
])
227 SendMessageW(data
->buttons
[i
], BM_SETSTATE
, TRUE
, 0);
229 /* Indicate axis positions, axes showing are hardcoded for now */
230 axes_pos
[0][0] = state
.lX
;
231 axes_pos
[0][1] = state
.lY
;
232 axes_pos
[1][0] = state
.lRx
;
233 axes_pos
[1][1] = state
.lRy
;
234 axes_pos
[2][0] = state
.lZ
;
235 axes_pos
[2][1] = state
.lRz
;
237 for (i
= 0; i
< TEST_MAX_AXES
; i
++)
238 SetWindowPos(data
->axes
[i
], 0,
239 TEST_AXIS_X
+ TEST_NEXT_AXIS_X
*i
+ axes_pos
[i
][0],
240 TEST_AXIS_Y
+ axes_pos
[i
][1],
241 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
243 Sleep(TEST_POLL_TIME
);
245 /* Reset button state */
246 for (i
= 0; i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
; i
++)
247 SendMessageW(data
->buttons
[i
], BM_SETSTATE
, FALSE
, 0);
253 static void test_handle_joychange(HWND hwnd
, struct JoystickData
*data
)
257 if (data
->num_joysticks
== 0) return;
259 data
->chosen_joystick
= SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_GETCURSEL
, 0, 0);
261 /* Enable only buttons present in the device */
262 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
263 ShowWindow(data
->buttons
[i
], i
<= data
->joysticks
[data
->chosen_joystick
].num_buttons
);
266 /*********************************************************************
267 * button_number_to_wchar [internal]
268 * Transforms an integer in the interval [0,99] into a 2 character WCHAR string
270 static void button_number_to_wchar(int n
, WCHAR str
[3])
272 str
[1] = n
% 10 + '0';
274 str
[0] = n
% 10 + '0';
278 static void draw_joystick_buttons(HWND hwnd
, struct JoystickData
* data
)
281 int row
= 0, col
= 0;
282 WCHAR button_label
[3];
283 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
284 static WCHAR button_class
[] = {'B','u','t','t','o','n','\0'};
286 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
288 if ((i
% TEST_BUTTON_COL_MAX
) == 0 && i
!= 0)
294 button_number_to_wchar(i
+ 1, button_label
);
296 data
->buttons
[i
] = CreateWindowW(button_class
, button_label
, WS_CHILD
,
297 TEST_BUTTON_X
+ TEST_NEXT_BUTTON_X
*col
, TEST_BUTTON_Y
+ TEST_NEXT_BUTTON_Y
*row
,
298 TEST_BUTTON_SIZE_X
, TEST_BUTTON_SIZE_Y
,
299 hwnd
, NULL
, NULL
, hinst
);
305 static void draw_joystick_axes(HWND hwnd
, struct JoystickData
* data
)
308 struct Joystick
*joy
;
309 DIPROPRANGE propRange
;
310 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
311 static const WCHAR button_class
[] = {'B','u','t','t','o','n','\0'};
312 static const WCHAR axes_names
[TEST_MAX_AXES
][7] = { {'X',',','Y','\0'}, {'R','x',',','R','y','\0'}, {'Z',',','R','z','\0'} };
313 static const DWORD axes_idc
[TEST_MAX_AXES
] = { IDC_TESTGROUPXY
, IDC_TESTGROUPRXRY
, IDC_TESTGROUPZRZ
};
315 /* Set axis range to ease the GUI visualization */
316 for (i
= 0; i
< data
->num_joysticks
; i
++)
318 joy
= &data
->joysticks
[i
];
319 propRange
.diph
.dwSize
= sizeof(DIPROPRANGE
);
320 propRange
.diph
.dwHeaderSize
= sizeof(DIPROPHEADER
);
321 propRange
.diph
.dwHow
= DIPH_DEVICE
;
322 propRange
.diph
.dwObj
= 0;
323 propRange
.lMin
= TEST_AXIS_MIN
;
324 propRange
.lMax
= TEST_AXIS_MAX
;
326 IDirectInputDevice_SetProperty(joy
->device
, DIPROP_RANGE
, &propRange
.diph
);
329 for (i
= 0; i
< TEST_MAX_AXES
; i
++)
331 /* Set axis box name */
332 SetWindowTextW(GetDlgItem(hwnd
, axes_idc
[i
]), axes_names
[i
]);
334 data
->axes
[i
] = CreateWindowW( button_class
, NULL
, WS_CHILD
| WS_VISIBLE
,
335 TEST_AXIS_X
+ TEST_NEXT_AXIS_X
*i
, TEST_AXIS_Y
,
336 TEST_AXIS_SIZE_X
, TEST_AXIS_SIZE_Y
,
337 hwnd
, (HMENU
) IDC_JOYSTICKAXES
+ i
, NULL
, hinst
);
341 /*********************************************************************
342 * test_dlgproc [internal]
345 static INT_PTR CALLBACK
test_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
347 static HANDLE thread
;
348 static struct JoystickData
*data
;
349 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
357 data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
359 /* Add enumerated joysticks to the combobox */
360 for (i
= 0; i
< data
->num_joysticks
; i
++)
362 struct Joystick
*joy
= &data
->joysticks
[i
];
363 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
366 draw_joystick_buttons(hwnd
, data
);
367 draw_joystick_axes(hwnd
, data
);
375 case MAKEWPARAM(IDC_TESTSELECTCOMBO
, CBN_SELCHANGE
):
376 test_handle_joychange(hwnd
, data
);
382 switch(((LPNMHDR
)lparam
)->code
)
388 /* Initialize input thread */
389 if (data
->num_joysticks
> 0)
393 /* Set the first joystick as default */
394 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_SETCURSEL
, 0, 0);
395 test_handle_joychange(hwnd
, data
);
397 thread
= CreateThread(NULL
, 0, input_thread
, (void*) data
, 0, &tid
);
403 /* Stop input thread */
413 /******************************************************************************
414 * propsheet_callback [internal]
416 static int CALLBACK
propsheet_callback(HWND hwnd
, UINT msg
, LPARAM lparam
)
418 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
421 case PSCB_INITIALIZED
:
427 /******************************************************************************
428 * display_cpl_sheets [internal]
430 * Build and display the dialog with all control panel propertysheets
433 static void display_cpl_sheets(HWND parent
, struct JoystickData
*data
)
435 INITCOMMONCONTROLSEX icex
;
436 PROPSHEETPAGEW psp
[NUM_PROPERTY_PAGES
];
437 PROPSHEETHEADERW psh
;
441 /* Initialize common controls */
442 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
443 icex
.dwICC
= ICC_LISTVIEW_CLASSES
| ICC_BAR_CLASSES
;
444 InitCommonControlsEx(&icex
);
446 ZeroMemory(&psh
, sizeof(psh
));
447 ZeroMemory(psp
, sizeof(psp
));
449 /* Fill out all PROPSHEETPAGE */
450 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
451 psp
[id
].hInstance
= hcpl
;
452 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_LIST
);
453 psp
[id
].pfnDlgProc
= list_dlgproc
;
454 psp
[id
].lParam
= (INT_PTR
) data
;
457 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
458 psp
[id
].hInstance
= hcpl
;
459 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_TEST
);
460 psp
[id
].pfnDlgProc
= test_dlgproc
;
461 psp
[id
].lParam
= (INT_PTR
) data
;
464 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
465 psp
[id
].hInstance
= hcpl
;
466 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_FORCEFEEDBACK
);
467 psp
[id
].pfnDlgProc
= NULL
;
468 psp
[id
].lParam
= (INT_PTR
) data
;
471 /* Fill out the PROPSHEETHEADER */
472 psh
.dwSize
= sizeof (PROPSHEETHEADERW
);
473 psh
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_USEICONID
| PSH_USECALLBACK
;
474 psh
.hwndParent
= parent
;
475 psh
.hInstance
= hcpl
;
476 psh
.pszCaption
= MAKEINTRESOURCEW(IDS_CPL_NAME
);
479 psh
.pfnCallback
= propsheet_callback
;
481 /* display the dialog */
482 PropertySheetW(&psh
);
487 /*********************************************************************
488 * CPlApplet (joy.cpl.@)
490 * Control Panel entry point
493 * hWnd [I] Handle for the Control Panel Window
494 * command [I] CPL_* Command
495 * lParam1 [I] first extra Parameter
496 * lParam2 [I] second extra Parameter
499 * Depends on the command
502 LONG CALLBACK
CPlApplet(HWND hwnd
, UINT command
, LPARAM lParam1
, LPARAM lParam2
)
504 static struct JoystickData data
;
505 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd
, command
, lParam1
, lParam2
);
513 /* Initialize dinput */
514 hr
= DirectInput8Create(GetModuleHandleW(NULL
), DIRECTINPUT_VERSION
, &IID_IDirectInput8W
, (void**)&data
.di
, NULL
);
518 ERR("Failed to initialize DirectInput: 0x%08x\n", hr
);
522 /* Then get all the connected joysticks */
523 initialize_joysticks(&data
);
532 CPLINFO
*appletInfo
= (CPLINFO
*) lParam2
;
534 appletInfo
->idName
= IDS_CPL_NAME
;
535 appletInfo
->idInfo
= IDS_CPL_INFO
;
536 appletInfo
->lData
= 0;
541 display_cpl_sheets(hwnd
, &data
);
545 destroy_joysticks(&data
);
547 /* And destroy dinput too */
548 IDirectInput8_Release(data
.di
);