[PATCH 1/5] programs: First version of the joystick testing tool. Just lists connected joysticks.

Lucas Fialho Zawacki lfzawacki at gmail.com
Fri May 18 08:50:46 CDT 2012


From: Lucas Fialho Zawacki <lfzawacki at gmail.com>

---
 configure.ac                  |    1 +
 programs/joystick/Makefile.in |   17 +++++++
 programs/joystick/main.c      |  102 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)
 create mode 100644 programs/joystick/Makefile.in
 create mode 100644 programs/joystick/main.c

diff --git a/configure.ac b/configure.ac
index f7701e2..85c8939 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2994,6 +2994,7 @@ WINE_CONFIG_PROGRAM(hostname,,[install,po])
 WINE_CONFIG_PROGRAM(icinfo,,[install])
 WINE_CONFIG_PROGRAM(iexplore,,[install])
 WINE_CONFIG_PROGRAM(ipconfig,,[install,po])
+WINE_CONFIG_PROGRAM(joystick,,[install])
 WINE_CONFIG_PROGRAM(lodctr,,[install])
 WINE_CONFIG_PROGRAM(mofcomp,,[install])
 WINE_CONFIG_PROGRAM(mshta,,[install])
diff --git a/programs/joystick/Makefile.in b/programs/joystick/Makefile.in
new file mode 100644
index 0000000..460f125
--- /dev/null
+++ b/programs/joystick/Makefile.in
@@ -0,0 +1,17 @@
+MODULE    = joystick.exe
+APPMODE   = -mconsole
+IMPORTS   = user32 advapi32 dinput dinput8 dxguid
+
+C_SRCS = \
+	main.c \
+
+#RC_SRCS = winecfg.rc
+#PO_SRCS = winecfg.rc
+
+#MANPAGE = winecfg.man
+
+#SVG_SRCS = \
+#	logo.svg \
+#	winecfg.svg
+
+ at MAKE_PROG_RULES@
diff --git a/programs/joystick/main.c b/programs/joystick/main.c
new file mode 100644
index 0000000..108d7f0
--- /dev/null
+++ b/programs/joystick/main.c
@@ -0,0 +1,102 @@
+/*
+ * Joystick Testing utility
+ *
+ * Copyright 2012 Lucas Fialho Zawacki
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ */
+
+#define WIN32_LEAN_AND_MEAN
+
+#define NONAMELESSUNION
+#define NONAMELESSSTRUCT
+
+#include <windows.h>
+#include <stdio.h>
+#include <dinput.h>
+
+struct Joystick {
+    IDirectInputDevice8A *device;
+    DIDEVICEINSTANCEA instance;
+    int num_buttons;
+    int num_axes;
+};
+
+struct JoystickData {
+    IDirectInput8A *di;
+    struct Joystick *joysticks;
+    int num_joysticks;
+    int cur_joystick;
+};
+
+/*
+    EnumCallback
+    Enumerates, creates and sets the common data format for all the joystick devices.
+    First time it checks if space for the joysticks was already reserved
+    and if not, just counts how many there are.
+*/
+static BOOL CALLBACK EnumCallback(const DIDEVICEINSTANCEA *instance, void *context)
+{
+    struct JoystickData *data = context;
+    struct Joystick *joystick;
+
+    if (data->joysticks == NULL) {
+        data->num_joysticks += 1;
+        return DIENUM_CONTINUE;
+    }
+
+    joystick = &data->joysticks[data->cur_joystick];
+    data->cur_joystick += 1;
+
+    IDirectInput8_CreateDevice(data->di, &instance->guidInstance, &joystick->device, NULL);
+    IDirectInputDevice8_SetDataFormat(joystick->device, &c_dfDIJoystick);
+    joystick->instance = *instance;
+    joystick->num_buttons = 0;
+    joystick->num_axes = 0;
+
+    return DIENUM_CONTINUE;
+}
+
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR szCmdLine, int nShow)
+{
+    /* Structure with the data and settings for the application */
+    /* data is: lpdi, joy[], num_joy, cur_joy */
+    struct JoystickData data = { NULL, NULL, 0, 0 };
+    HRESULT hr;
+    int i;
+
+    hr = DirectInput8Create(GetModuleHandleA(NULL), DIRECTINPUT_VERSION, &IID_IDirectInput8A, (void**) &data.di, NULL);
+
+    if (FAILED(hr)) {
+        printf("Failed to initialize DirectInput: 0x%08x\n", hr);
+        return 1;
+    }
+
+    /* First count how many joysticks are connected */
+    hr = IDirectInput8_EnumDevices(data.di, DI8DEVCLASS_GAMECTRL, EnumCallback, &data, DIEDFL_ATTACHEDONLY);
+    data.joysticks = malloc(sizeof(struct Joystick) * data.num_joysticks);
+
+    /* Get all the joysticks */
+    hr = IDirectInput8_EnumDevices(data.di, DI8DEVCLASS_GAMECTRL, EnumCallback, &data, DIEDFL_ATTACHEDONLY);
+
+    printf("Found %d joysticks.\n", data.num_joysticks);
+
+    /* Default case just lists the joysticks */
+    for (i=0; i < data.num_joysticks; i++)
+        printf("%d: %s\n", i, data.joysticks[i].instance.tszInstanceName);
+
+    return 0;
+}
-- 
1.7.9.5




More information about the wine-patches mailing list