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 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 poll_input(const struct Joystick
*joy
, DIJOYSTATE
*state
)
181 hr
= IDirectInputDevice8_Poll(joy
->device
);
183 /* If it failed, try to acquire the joystick */
186 hr
= IDirectInputDevice8_Acquire(joy
->device
);
187 while (hr
== DIERR_INPUTLOST
) hr
= IDirectInputDevice8_Acquire(joy
->device
);
190 if (hr
== DIERR_OTHERAPPHASPRIO
) return;
192 IDirectInputDevice8_GetDeviceState(joy
->device
, sizeof(DIJOYSTATE
), state
);
195 static DWORD WINAPI
input_thread(void *param
)
198 struct JoystickData
*data
= param
;
200 ZeroMemory(&state
, sizeof(state
));
205 poll_input(&data
->joysticks
[data
->chosen_joystick
], &state
);
207 /* Indicate pressed buttons */
208 for (i
= 0; i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
; i
++)
209 if (state
.rgbButtons
[i
])
210 SendMessageW(data
->buttons
[i
], BM_SETSTATE
, TRUE
, 0);
212 Sleep(TEST_POLL_TIME
);
214 /* Reset button state */
215 for (i
= 0; i
< data
->joysticks
[data
->chosen_joystick
].num_buttons
; i
++)
216 SendMessageW(data
->buttons
[i
], BM_SETSTATE
, FALSE
, 0);
222 static void test_handle_joychange(HWND hwnd
, struct JoystickData
*data
)
226 if (data
->num_joysticks
== 0) return;
228 data
->chosen_joystick
= SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_GETCURSEL
, 0, 0);
230 /* Enable only buttons present in the device */
231 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
232 ShowWindow(data
->buttons
[i
], i
<= data
->joysticks
[data
->chosen_joystick
].num_buttons
);
235 /*********************************************************************
236 * button_number_to_wchar [internal]
237 * Transforms an integer in the interval [0,99] into a 2 character WCHAR string
239 static void button_number_to_wchar(int n
, WCHAR str
[3])
241 str
[1] = n
% 10 + '0';
243 str
[0] = n
% 10 + '0';
247 static void draw_joystick_buttons(HWND hwnd
, struct JoystickData
* data
)
250 int row
= 0, col
= 0;
251 WCHAR button_label
[3];
252 HINSTANCE hinst
= (HINSTANCE
) GetWindowLongPtrW(hwnd
, GWLP_HINSTANCE
);
253 static WCHAR button_class
[] = {'B','u','t','t','o','n','\0'};
255 for (i
= 0; i
< TEST_MAX_BUTTONS
; i
++)
257 if ((i
% TEST_BUTTON_COL_MAX
) == 0 && i
!= 0)
263 button_number_to_wchar(i
+ 1, button_label
);
265 data
->buttons
[i
] = CreateWindowW(button_class
, button_label
, WS_CHILD
,
266 TEST_BUTTON_X
+ TEST_NEXT_BUTTON_X
*col
, TEST_BUTTON_Y
+ TEST_NEXT_BUTTON_Y
*row
,
267 TEST_BUTTON_SIZE_X
, TEST_BUTTON_SIZE_Y
,
268 hwnd
, NULL
, NULL
, hinst
);
274 /*********************************************************************
275 * test_dlgproc [internal]
278 static INT_PTR CALLBACK
test_dlgproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
280 static HANDLE thread
;
281 static struct JoystickData
*data
;
282 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
290 data
= (struct JoystickData
*) ((PROPSHEETPAGEW
*)lparam
)->lParam
;
292 /* Add enumerated joysticks to the combobox */
293 for (i
= 0; i
< data
->num_joysticks
; i
++)
295 struct Joystick
*joy
= &data
->joysticks
[i
];
296 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_ADDSTRING
, 0, (LPARAM
) joy
->instance
.tszInstanceName
);
299 draw_joystick_buttons(hwnd
, data
);
307 case MAKEWPARAM(IDC_TESTSELECTCOMBO
, CBN_SELCHANGE
):
308 test_handle_joychange(hwnd
, data
);
314 switch(((LPNMHDR
)lparam
)->code
)
320 /* Initialize input thread */
321 if (data
->num_joysticks
> 0)
325 /* Set the first joystick as default */
326 SendDlgItemMessageW(hwnd
, IDC_TESTSELECTCOMBO
, CB_SETCURSEL
, 0, 0);
327 test_handle_joychange(hwnd
, data
);
329 thread
= CreateThread(NULL
, 0, input_thread
, (void*) data
, 0, &tid
);
335 /* Stop input thread */
345 /******************************************************************************
346 * propsheet_callback [internal]
348 static int CALLBACK
propsheet_callback(HWND hwnd
, UINT msg
, LPARAM lparam
)
350 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd
, msg
, msg
, lparam
);
353 case PSCB_INITIALIZED
:
359 /******************************************************************************
360 * display_cpl_sheets [internal]
362 * Build and display the dialog with all control panel propertysheets
365 static void display_cpl_sheets(HWND parent
, struct JoystickData
*data
)
367 INITCOMMONCONTROLSEX icex
;
368 PROPSHEETPAGEW psp
[NUM_PROPERTY_PAGES
];
369 PROPSHEETHEADERW psh
;
373 /* Initialize common controls */
374 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
375 icex
.dwICC
= ICC_LISTVIEW_CLASSES
| ICC_BAR_CLASSES
;
376 InitCommonControlsEx(&icex
);
378 ZeroMemory(&psh
, sizeof(psh
));
379 ZeroMemory(psp
, sizeof(psp
));
381 /* Fill out all PROPSHEETPAGE */
382 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
383 psp
[id
].hInstance
= hcpl
;
384 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_LIST
);
385 psp
[id
].pfnDlgProc
= list_dlgproc
;
386 psp
[id
].lParam
= (INT_PTR
) data
;
389 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
390 psp
[id
].hInstance
= hcpl
;
391 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_TEST
);
392 psp
[id
].pfnDlgProc
= test_dlgproc
;
393 psp
[id
].lParam
= (INT_PTR
) data
;
396 psp
[id
].dwSize
= sizeof (PROPSHEETPAGEW
);
397 psp
[id
].hInstance
= hcpl
;
398 psp
[id
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_FORCEFEEDBACK
);
399 psp
[id
].pfnDlgProc
= NULL
;
400 psp
[id
].lParam
= (INT_PTR
) data
;
403 /* Fill out the PROPSHEETHEADER */
404 psh
.dwSize
= sizeof (PROPSHEETHEADERW
);
405 psh
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_USEICONID
| PSH_USECALLBACK
;
406 psh
.hwndParent
= parent
;
407 psh
.hInstance
= hcpl
;
408 psh
.pszCaption
= MAKEINTRESOURCEW(IDS_CPL_NAME
);
411 psh
.pfnCallback
= propsheet_callback
;
413 /* display the dialog */
414 PropertySheetW(&psh
);
419 /*********************************************************************
420 * CPlApplet (joy.cpl.@)
422 * Control Panel entry point
425 * hWnd [I] Handle for the Control Panel Window
426 * command [I] CPL_* Command
427 * lParam1 [I] first extra Parameter
428 * lParam2 [I] second extra Parameter
431 * Depends on the command
434 LONG CALLBACK
CPlApplet(HWND hwnd
, UINT command
, LPARAM lParam1
, LPARAM lParam2
)
436 static struct JoystickData data
;
437 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd
, command
, lParam1
, lParam2
);
445 /* Initialize dinput */
446 hr
= DirectInput8Create(GetModuleHandleW(NULL
), DIRECTINPUT_VERSION
, &IID_IDirectInput8W
, (void**)&data
.di
, NULL
);
450 ERR("Failed to initialize DirectInput: 0x%08x\n", hr
);
454 /* Then get all the connected joysticks */
455 initialize_joysticks(&data
);
464 CPLINFO
*appletInfo
= (CPLINFO
*) lParam2
;
466 appletInfo
->idName
= IDS_CPL_NAME
;
467 appletInfo
->idInfo
= IDS_CPL_INFO
;
468 appletInfo
->lData
= 0;
473 display_cpl_sheets(hwnd
, &data
);
477 destroy_joysticks(&data
);
479 /* And destroy dinput too */
480 IDirectInput8_Release(data
.di
);