mscoree: Correct possible uninitialized variable (clang).
[wine/multimedia.git] / dlls / joy.cpl / main.c
blobdb72281b193d3a399777262aa9f4e79eaba21a84
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 "joy.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(joycpl);
39 DECLSPEC_HIDDEN HMODULE hcpl;
41 /*********************************************************************
42 * DllMain
44 BOOL WINAPI DllMain(HINSTANCE hdll, DWORD reason, LPVOID reserved)
46 TRACE("(%p, %d, %p)\n", hdll, reason, reserved);
48 switch (reason)
50 case DLL_WINE_PREATTACH:
51 return FALSE; /* prefer native version */
53 case DLL_PROCESS_ATTACH:
54 DisableThreadLibraryCalls(hdll);
55 hcpl = hdll;
57 return TRUE;
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;
71 if (data->joysticks == NULL)
73 data->num_joysticks += 1;
74 return DIENUM_CONTINUE;
77 joystick = &data->joysticks[data->cur_joystick];
78 data->cur_joystick += 1;
80 IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL);
81 IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick);
83 joystick->instance = *instance;
85 return DIENUM_CONTINUE;
88 /***********************************************************************
89 * initialize_joysticks [internal]
91 static void initialize_joysticks(struct JoystickData *data)
93 data->num_joysticks = 0;
94 data->cur_joystick = 0;
95 IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
96 data->joysticks = HeapAlloc(GetProcessHeap(), 0, sizeof(struct Joystick) * data->num_joysticks);
98 /* Get all the joysticks */
99 IDirectInput8_EnumDevices(data->di, DI8DEVCLASS_GAMECTRL, enum_callback, data, DIEDFL_ATTACHEDONLY);
102 /***********************************************************************
103 * destroy_joysticks [internal]
105 static void destroy_joysticks(struct JoystickData *data)
107 int i;
109 for (i = 0; i < data->num_joysticks; i++)
111 IDirectInputDevice8_Unacquire(data->joysticks[i].device);
112 IDirectInputDevice8_Release(data->joysticks[i].device);
115 HeapFree(GetProcessHeap(), 0, data->joysticks);
118 /*********************************************************************
119 * list_dlgproc [internal]
122 INT_PTR CALLBACK list_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
124 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
125 switch (msg)
127 case WM_INITDIALOG:
129 int i;
130 struct JoystickData *data = (struct JoystickData*) ((PROPSHEETPAGEW*)lparam)->lParam;
132 /* Set dialog information */
133 for (i = 0; i < data->num_joysticks; i++)
135 struct Joystick *joy = &data->joysticks[i];
136 SendDlgItemMessageW(hwnd, IDC_JOYSTICKLIST, LB_ADDSTRING, 0, (LPARAM) joy->instance.tszInstanceName);
139 return TRUE;
142 case WM_COMMAND:
144 switch (LOWORD(wparam))
146 case IDC_BUTTONDISABLE:
147 FIXME("Disable selected joystick from being enumerated\n");
148 break;
150 case IDC_BUTTONENABLE:
151 FIXME("Re-Enable selected joystick\n");
152 break;
155 return TRUE;
157 case WM_NOTIFY:
158 return TRUE;
160 default:
161 break;
163 return FALSE;
166 /******************************************************************************
167 * propsheet_callback [internal]
169 static int CALLBACK propsheet_callback(HWND hwnd, UINT msg, LPARAM lparam)
171 TRACE("(%p, 0x%08x/%d, 0x%lx)\n", hwnd, msg, msg, lparam);
172 switch (msg)
174 case PSCB_INITIALIZED:
175 break;
177 return 0;
180 /******************************************************************************
181 * display_cpl_sheets [internal]
183 * Build and display the dialog with all control panel propertysheets
186 static void display_cpl_sheets(HWND parent, struct JoystickData *data)
188 INITCOMMONCONTROLSEX icex;
189 PROPSHEETPAGEW psp[NUM_PROPERTY_PAGES];
190 PROPSHEETHEADERW psh;
191 DWORD id = 0;
193 OleInitialize(NULL);
194 /* Initialize common controls */
195 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
196 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
197 InitCommonControlsEx(&icex);
199 ZeroMemory(&psh, sizeof(psh));
200 ZeroMemory(psp, sizeof(psp));
202 /* Fill out all PROPSHEETPAGE */
203 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
204 psp[id].hInstance = hcpl;
205 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_LIST);
206 psp[id].pfnDlgProc = list_dlgproc;
207 psp[id].lParam = (INT_PTR) data;
208 id++;
210 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
211 psp[id].hInstance = hcpl;
212 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_TEST);
213 psp[id].pfnDlgProc = NULL;
214 psp[id].lParam = (INT_PTR) data;
215 id++;
217 psp[id].dwSize = sizeof (PROPSHEETPAGEW);
218 psp[id].hInstance = hcpl;
219 psp[id].u.pszTemplate = MAKEINTRESOURCEW(IDD_FORCEFEEDBACK);
220 psp[id].pfnDlgProc = NULL;
221 psp[id].lParam = (INT_PTR) data;
222 id++;
224 /* Fill out the PROPSHEETHEADER */
225 psh.dwSize = sizeof (PROPSHEETHEADERW);
226 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
227 psh.hwndParent = parent;
228 psh.hInstance = hcpl;
229 psh.pszCaption = MAKEINTRESOURCEW(IDS_CPL_NAME);
230 psh.nPages = id;
231 psh.u3.ppsp = psp;
232 psh.pfnCallback = propsheet_callback;
234 /* display the dialog */
235 PropertySheetW(&psh);
237 OleUninitialize();
240 /*********************************************************************
241 * CPlApplet (joy.cpl.@)
243 * Control Panel entry point
245 * PARAMS
246 * hWnd [I] Handle for the Control Panel Window
247 * command [I] CPL_* Command
248 * lParam1 [I] first extra Parameter
249 * lParam2 [I] second extra Parameter
251 * RETURNS
252 * Depends on the command
255 LONG CALLBACK CPlApplet(HWND hwnd, UINT command, LPARAM lParam1, LPARAM lParam2)
257 static struct JoystickData data;
258 TRACE("(%p, %u, 0x%lx, 0x%lx)\n", hwnd, command, lParam1, lParam2);
260 switch (command)
262 case CPL_INIT:
264 HRESULT hr;
266 /* Initialize dinput */
267 hr = DirectInput8Create(GetModuleHandleW(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8W, (void**)&data.di, NULL);
269 if (FAILED(hr))
271 ERR("Failed to initialize DirectInput: 0x%08x\n", hr);
272 return FALSE;
275 /* Then get all the connected joysticks */
276 initialize_joysticks(&data);
278 return TRUE;
280 case CPL_GETCOUNT:
281 return 1;
283 case CPL_INQUIRE:
285 CPLINFO *appletInfo = (CPLINFO *) lParam2;
287 appletInfo->idName = IDS_CPL_NAME;
288 appletInfo->idInfo = IDS_CPL_INFO;
289 appletInfo->lData = 0;
290 return TRUE;
293 case CPL_DBLCLK:
294 display_cpl_sheets(hwnd, &data);
295 break;
297 case CPL_STOP:
298 destroy_joysticks(&data);
300 /* And destroy dinput too */
301 IDirectInput8_Release(data.di);
302 break;
305 return FALSE;