From b8ca69675f9fad12682c34ae65291ef940eb3eac Mon Sep 17 00:00:00 2001
From: Andrew Fenn <andrewfenn@gmail.com>
Date: Sat, 25 Oct 2008 19:46:35 +0100
Subject: Added the function XInputGetState with test cases

---
 dlls/xinput1_1/tests/Makefile.in   |   13 ++++++
 dlls/xinput1_1/tests/xinput.c      |   76 ++++++++++++++++++++++++++++++++++++
 dlls/xinput1_2/tests/Makefile.in   |   13 ++++++
 dlls/xinput1_2/tests/xinput.c      |   76 ++++++++++++++++++++++++++++++++++++
 dlls/xinput1_3/tests/Makefile.in   |   13 ++++++
 dlls/xinput1_3/tests/xinput.c      |   76 ++++++++++++++++++++++++++++++++++++
 dlls/xinput1_3/xinput1_3.spec      |    2 +-
 dlls/xinput1_3/xinput1_3_main.c    |   15 ++++++-
 dlls/xinput9_1_0/tests/Makefile.in |   13 ++++++
 dlls/xinput9_1_0/tests/xinput.c    |   76 ++++++++++++++++++++++++++++++++++++
 include/xinput.h                   |   55 ++++++++++++++++++++++++++
 11 files changed, 425 insertions(+), 3 deletions(-)
 create mode 100644 dlls/xinput1_1/tests/Makefile.in
 create mode 100644 dlls/xinput1_1/tests/xinput.c
 create mode 100644 dlls/xinput1_2/tests/Makefile.in
 create mode 100644 dlls/xinput1_2/tests/xinput.c
 create mode 100644 dlls/xinput1_3/tests/Makefile.in
 create mode 100644 dlls/xinput1_3/tests/xinput.c
 create mode 100644 dlls/xinput9_1_0/tests/Makefile.in
 create mode 100644 dlls/xinput9_1_0/tests/xinput.c
 create mode 100644 include/xinput.h

diff --git a/dlls/xinput1_1/tests/Makefile.in b/dlls/xinput1_1/tests/Makefile.in
new file mode 100644
index 0000000..004cf29
--- /dev/null
+++ b/dlls/xinput1_1/tests/Makefile.in
@@ -0,0 +1,13 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = xinput1_1.dll
+IMPORTS   = user32 kernel32
+
+CTESTS = \
+	xinput.c \
+
+@MAKE_TEST_RULES@
+
+@DEPENDENCIES@  # everything below this line is overwritten by make depend
diff --git a/dlls/xinput1_1/tests/xinput.c b/dlls/xinput1_1/tests/xinput.c
new file mode 100644
index 0000000..bf4a169
--- /dev/null
+++ b/dlls/xinput1_1/tests/xinput.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2008 Andrew Fenn
+ *
+ * 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
+ */
+
+#include <windows.h>
+#include <stdio.h>
+
+#include "xinput.h"
+#include "wine/test.h"
+
+static DWORD (WINAPI *XInputGetState)(DWORD, XINPUT_STATE*)=NULL;
+
+static void test_no_controller(void) {
+
+    XINPUT_STATE controllerState;
+    DWORD controllerNum;
+    DWORD result;
+
+    for(controllerNum=0; controllerNum < 4; controllerNum++) {
+        ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+        // Get the state
+        result = XInputGetState(controllerNum, &controllerState);
+        ok(result != ERROR_BAD_ARGUMENTS, "XInputGetState failed with (%d)\n", result);
+
+        if (ERROR_DEVICE_NOT_CONNECTED == result) {
+            skip("Controller %d is not connected\n", controllerNum);
+        } else {
+            printf("-- Results for controller %d --\n", controllerNum);
+            printf("XInputGetState: %d\n", result);
+            printf("State->dwPacketNumber: %d\n", controllerState.dwPacketNumber);
+            printf("Gamepad Variables --\n");
+            printf("Gamepad.wButtons: %#x\n", controllerState.Gamepad.wButtons);
+            printf("Gamepad.bLeftTrigger: 0x%08x\n", controllerState.Gamepad.bLeftTrigger);
+            printf("Gamepad.bRightTrigger: 0x%08x\n", controllerState.Gamepad.bRightTrigger);
+            printf("Gamepad.sThumbLX: %d\n", controllerState.Gamepad.sThumbLX);
+            printf("Gamepad.sThumbLY: %d\n", controllerState.Gamepad.sThumbLY);
+            printf("Gamepad.sThumbRX: %d\n", controllerState.Gamepad.sThumbRX);
+            printf("Gamepad.sThumbRY: %d\n", controllerState.Gamepad.sThumbRY);
+        }
+    }
+
+    ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+    result = XInputGetState(5, &controllerState);
+    ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result);
+}
+
+START_TEST(xinput)
+{
+    HMODULE hXinput;
+
+    hXinput = LoadLibraryA( "xinput1_1.dll" );
+
+    if (hXinput)
+    {
+        XInputGetState = (void*)GetProcAddress(hXinput, "XInputGetState");
+
+
+        test_no_controller();
+    } else {
+        skip("Could not load xinput1_1.dll\n");
+    }
+}
diff --git a/dlls/xinput1_2/tests/Makefile.in b/dlls/xinput1_2/tests/Makefile.in
new file mode 100644
index 0000000..6c1047f
--- /dev/null
+++ b/dlls/xinput1_2/tests/Makefile.in
@@ -0,0 +1,13 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = xinput1_2.dll
+IMPORTS   = user32 kernel32
+
+CTESTS = \
+	xinput.c \
+
+@MAKE_TEST_RULES@
+
+@DEPENDENCIES@  # everything below this line is overwritten by make depend
diff --git a/dlls/xinput1_2/tests/xinput.c b/dlls/xinput1_2/tests/xinput.c
new file mode 100644
index 0000000..f1c2460
--- /dev/null
+++ b/dlls/xinput1_2/tests/xinput.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2008 Andrew Fenn
+ *
+ * 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
+ */
+
+#include <windows.h>
+#include <stdio.h>
+
+#include "xinput.h"
+#include "wine/test.h"
+
+static DWORD (WINAPI *XInputGetState)(DWORD, XINPUT_STATE*)=NULL;
+
+static void test_no_controller(void) {
+
+    XINPUT_STATE controllerState;
+    DWORD controllerNum;
+    DWORD result;
+
+    for(controllerNum=0; controllerNum < 4; controllerNum++) {
+        ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+        // Get the state
+        result = XInputGetState(controllerNum, &controllerState);
+        ok(result != ERROR_BAD_ARGUMENTS, "XInputGetState failed with (%d)\n", result);
+
+        if (ERROR_DEVICE_NOT_CONNECTED == result) {
+            skip("Controller %d is not connected\n", controllerNum);
+        } else {
+            printf("-- Results for controller %d --\n", controllerNum);
+            printf("XInputGetState: %d\n", result);
+            printf("State->dwPacketNumber: %d\n", controllerState.dwPacketNumber);
+            printf("Gamepad Variables --\n");
+            printf("Gamepad.wButtons: %#x\n", controllerState.Gamepad.wButtons);
+            printf("Gamepad.bLeftTrigger: 0x%08x\n", controllerState.Gamepad.bLeftTrigger);
+            printf("Gamepad.bRightTrigger: 0x%08x\n", controllerState.Gamepad.bRightTrigger);
+            printf("Gamepad.sThumbLX: %d\n", controllerState.Gamepad.sThumbLX);
+            printf("Gamepad.sThumbLY: %d\n", controllerState.Gamepad.sThumbLY);
+            printf("Gamepad.sThumbRX: %d\n", controllerState.Gamepad.sThumbRX);
+            printf("Gamepad.sThumbRY: %d\n", controllerState.Gamepad.sThumbRY);
+        }
+    }
+
+    ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+    result = XInputGetState(5, &controllerState);
+    ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result);
+}
+
+START_TEST(xinput)
+{
+    HMODULE hXinput;
+
+    hXinput = LoadLibraryA( "xinput1_2.dll" );
+
+    if (hXinput)
+    {
+        XInputGetState = (void*)GetProcAddress(hXinput, "XInputGetState");
+
+
+        test_no_controller();
+    } else {
+        skip("Could not load xinput1_2.dll\n");
+    }
+}
diff --git a/dlls/xinput1_3/tests/Makefile.in b/dlls/xinput1_3/tests/Makefile.in
new file mode 100644
index 0000000..e855dc8
--- /dev/null
+++ b/dlls/xinput1_3/tests/Makefile.in
@@ -0,0 +1,13 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = xinput1_3.dll
+IMPORTS   = user32 kernel32
+
+CTESTS = \
+	xinput.c \
+
+@MAKE_TEST_RULES@
+
+@DEPENDENCIES@  # everything below this line is overwritten by make depend
diff --git a/dlls/xinput1_3/tests/xinput.c b/dlls/xinput1_3/tests/xinput.c
new file mode 100644
index 0000000..ded20b8
--- /dev/null
+++ b/dlls/xinput1_3/tests/xinput.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2008 Andrew Fenn
+ *
+ * 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
+ */
+
+#include <windows.h>
+#include <stdio.h>
+
+#include "xinput.h"
+#include "wine/test.h"
+
+static DWORD (WINAPI *XInputGetState)(DWORD, XINPUT_STATE*)=NULL;
+
+static void test_no_controller(void) {
+
+    XINPUT_STATE controllerState;
+    DWORD controllerNum;
+    DWORD result;
+
+    for(controllerNum=0; controllerNum < 4; controllerNum++) {
+        ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+        // Get the state
+        result = XInputGetState(controllerNum, &controllerState);
+        ok(result != ERROR_BAD_ARGUMENTS, "XInputGetState failed with (%d)\n", result);
+
+        if (ERROR_DEVICE_NOT_CONNECTED == result) {
+            skip("Controller %d is not connected\n", controllerNum);
+        } else {
+            printf("-- Results for controller %d --\n", controllerNum);
+            printf("XInputGetState: %d\n", result);
+            printf("State->dwPacketNumber: %d\n", controllerState.dwPacketNumber);
+            printf("Gamepad Variables --\n");
+            printf("Gamepad.wButtons: %#x\n", controllerState.Gamepad.wButtons);
+            printf("Gamepad.bLeftTrigger: 0x%08x\n", controllerState.Gamepad.bLeftTrigger);
+            printf("Gamepad.bRightTrigger: 0x%08x\n", controllerState.Gamepad.bRightTrigger);
+            printf("Gamepad.sThumbLX: %d\n", controllerState.Gamepad.sThumbLX);
+            printf("Gamepad.sThumbLY: %d\n", controllerState.Gamepad.sThumbLY);
+            printf("Gamepad.sThumbRX: %d\n", controllerState.Gamepad.sThumbRX);
+            printf("Gamepad.sThumbRY: %d\n", controllerState.Gamepad.sThumbRY);
+        }
+    }
+
+    ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+    result = XInputGetState(5, &controllerState);
+    ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result);
+}
+
+START_TEST(xinput)
+{
+    HMODULE hXinput;
+
+    hXinput = LoadLibraryA( "xinput1_3.dll" );
+
+    if (hXinput)
+    {
+        XInputGetState = (void*)GetProcAddress(hXinput, "XInputGetState");
+
+
+        test_no_controller();
+    } else {
+        skip("Could not load xinput1_3.dll\n");
+    }
+}
diff --git a/dlls/xinput1_3/xinput1_3.spec b/dlls/xinput1_3/xinput1_3.spec
index 5caaa37..8fb6353 100644
--- a/dlls/xinput1_3/xinput1_3.spec
+++ b/dlls/xinput1_3/xinput1_3.spec
@@ -1,6 +1,6 @@
 @ stub XInputEnable #(long)
 @ stub XInputSetState #(long ptr)
-@ stub XInputGetState #(long ptr)
+@ stdcall XInputGetState(long ptr)
 @ stub XInputGetKeystroke #(long long ptr)
 @ stub XInputGetCapabilities #(long long ptr)
 @ stub XInputGetDSoundAudioDeviceGuids #(long ptr ptr)
diff --git a/dlls/xinput1_3/xinput1_3_main.c b/dlls/xinput1_3/xinput1_3_main.c
index 1a41256..b7249ea 100644
--- a/dlls/xinput1_3/xinput1_3_main.c
+++ b/dlls/xinput1_3/xinput1_3_main.c
@@ -22,13 +22,13 @@
 #include <stdarg.h>
 #include <string.h>
 
-#define COBJMACROS
-
 #include "wine/debug.h"
 #include "windef.h"
 #include "winbase.h"
 #include "winerror.h"
 
+#include "xinput.h"
+
 WINE_DEFAULT_DEBUG_CHANNEL(xinput);
 
 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
@@ -45,3 +45,14 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
     }
     return TRUE;
 }
+
+DWORD XInputGetState(DWORD dwUserIndex, XINPUT_STATE* pState)
+{
+    FIXME("Stub \n");
+
+    if (dwUserIndex < 4) {
+        return ERROR_DEVICE_NOT_CONNECTED;
+        /* If controller exists then return ERROR_SUCCESS */
+    }
+    return ERROR_BAD_ARGUMENTS;
+}
diff --git a/dlls/xinput9_1_0/tests/Makefile.in b/dlls/xinput9_1_0/tests/Makefile.in
new file mode 100644
index 0000000..8b595d6
--- /dev/null
+++ b/dlls/xinput9_1_0/tests/Makefile.in
@@ -0,0 +1,13 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = xinput9_1_0.dll
+IMPORTS   = user32 kernel32
+
+CTESTS = \
+	xinput.c \
+
+@MAKE_TEST_RULES@
+
+@DEPENDENCIES@  # everything below this line is overwritten by make depend
diff --git a/dlls/xinput9_1_0/tests/xinput.c b/dlls/xinput9_1_0/tests/xinput.c
new file mode 100644
index 0000000..731474f
--- /dev/null
+++ b/dlls/xinput9_1_0/tests/xinput.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2008 Andrew Fenn
+ *
+ * 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
+ */
+
+#include <windows.h>
+#include <stdio.h>
+
+#include "xinput.h"
+#include "wine/test.h"
+
+static DWORD (WINAPI *XInputGetState)(DWORD, XINPUT_STATE*)=NULL;
+
+static void test_no_controller(void) {
+
+    XINPUT_STATE controllerState;
+    DWORD controllerNum;
+    DWORD result;
+
+    for(controllerNum=0; controllerNum < 4; controllerNum++) {
+        ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+        // Get the state
+        result = XInputGetState(controllerNum, &controllerState);
+        ok(result != ERROR_BAD_ARGUMENTS, "XInputGetState failed with (%d)\n", result);
+
+        if (ERROR_DEVICE_NOT_CONNECTED == result) {
+            skip("Controller %d is not connected\n", controllerNum);
+        } else {
+            printf("-- Results for controller %d --\n", controllerNum);
+            printf("XInputGetState: %d\n", result);
+            printf("State->dwPacketNumber: %d\n", controllerState.dwPacketNumber);
+            printf("Gamepad Variables --\n");
+            printf("Gamepad.wButtons: %#x\n", controllerState.Gamepad.wButtons);
+            printf("Gamepad.bLeftTrigger: 0x%08x\n", controllerState.Gamepad.bLeftTrigger);
+            printf("Gamepad.bRightTrigger: 0x%08x\n", controllerState.Gamepad.bRightTrigger);
+            printf("Gamepad.sThumbLX: %d\n", controllerState.Gamepad.sThumbLX);
+            printf("Gamepad.sThumbLY: %d\n", controllerState.Gamepad.sThumbLY);
+            printf("Gamepad.sThumbRX: %d\n", controllerState.Gamepad.sThumbRX);
+            printf("Gamepad.sThumbRY: %d\n", controllerState.Gamepad.sThumbRY);
+        }
+    }
+
+    ZeroMemory(&controllerState, sizeof(XINPUT_STATE));
+    result = XInputGetState(5, &controllerState);
+    ok(result == ERROR_BAD_ARGUMENTS, "XInputGetState returned (%d)\n", result);
+}
+
+START_TEST(xinput)
+{
+    HMODULE hXinput;
+
+    hXinput = LoadLibraryA( "xinput9_1_0.dll" );
+
+    if (hXinput)
+    {
+        XInputGetState = (void*)GetProcAddress(hXinput, "XInputGetState");
+
+
+        test_no_controller();
+    } else {
+        skip("Could not load xinput9_1_0.dll\n");
+    }
+}
diff --git a/include/xinput.h b/include/xinput.h
new file mode 100644
index 0000000..da3ffbe
--- /dev/null
+++ b/include/xinput.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) the Wine project
+ *
+ * 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
+ */
+
+#ifndef __WINE_XINPUT_H
+#define __WINE_XINPUT_H
+
+#define COM_NO_WINDOWS_H
+#include <objbase.h>
+
+#define XINPUT_GAMEPAD_DPAD_UP          0x00000001
+#define XINPUT_GAMEPAD_DPAD_DOWN        0x00000002
+#define XINPUT_GAMEPAD_DPAD_LEFT        0x00000004
+#define XINPUT_GAMEPAD_DPAD_RIGHT       0x00000008
+#define XINPUT_GAMEPAD_START            0x00000010
+#define XINPUT_GAMEPAD_BACK             0x00000020
+#define XINPUT_GAMEPAD_LEFT_THUMB       0x00000040
+#define XINPUT_GAMEPAD_RIGHT_THUMB      0x00000080
+#define XINPUT_GAMEPAD_LEFT_SHOULDER    0x0100
+#define XINPUT_GAMEPAD_RIGHT_SHOULDER   0x0200
+#define XINPUT_GAMEPAD_A                0x1000
+#define XINPUT_GAMEPAD_B                0x2000
+#define XINPUT_GAMEPAD_X                0x4000
+#define XINPUT_GAMEPAD_Y                0x8000
+
+typedef struct _XINPUT_GAMEPAD {
+    WORD wButtons;
+    BYTE bLeftTrigger;
+    BYTE bRightTrigger;
+    SHORT sThumbLX;
+    SHORT sThumbLY;
+    SHORT sThumbRX;
+    SHORT sThumbRY;
+} XINPUT_GAMEPAD, *PXINPUT_GAMEPAD;
+
+typedef struct _XINPUT_STATE {
+    DWORD dwPacketNumber;
+    XINPUT_GAMEPAD Gamepad;
+} XINPUT_STATE, *PXINPUT_STATE;
+
+#endif /* __WINE_XINPUT_H */
-- 
1.5.4.3

