From 8e524b6f48f798f8c1784ad8ec5ca6f2aa7a34a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20K=C3=BCpper?= Date: Sun, 25 Aug 2013 14:57:00 +0200 Subject: [PATCH] Some improvements for wiimote-support (Darkice). --- Vocaluxe/Lib/Input/CHIDAPI.cs | 28 ++++++++-------- Vocaluxe/Lib/Input/WiiMote/CWiiMote.cs | 1 + Vocaluxe/Lib/Input/WiiMote/WiiMoteLib.cs | 41 +++++++++++++----------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Vocaluxe/Lib/Input/CHIDAPI.cs b/Vocaluxe/Lib/Input/CHIDAPI.cs index d90e50d7..214ea172 100644 --- a/Vocaluxe/Lib/Input/CHIDAPI.cs +++ b/Vocaluxe/Lib/Input/CHIDAPI.cs @@ -23,20 +23,20 @@ using Vocaluxe.Base; namespace Vocaluxe.Lib.Input { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SHIDDeviceInfo { // ReSharper disable MemberCanBePrivate.Global [MarshalAs(UnmanagedType.LPTStr)] public readonly String Path; - public readonly ushort VendorString; - public readonly ushort ProductID; - public readonly String SerialNumber; - public readonly ushort ReleaseNumber; - public readonly String ManufacturerString; - public readonly String ProductString; - public readonly ushort UsagePage; - public readonly ushort Usage; - public readonly int InterfaceNumber; + [MarshalAs(UnmanagedType.LPWStr)] public readonly ushort VendorString; + [MarshalAs(UnmanagedType.LPWStr)] public readonly ushort ProductID; + [MarshalAs(UnmanagedType.LPWStr)] public readonly String SerialNumber; + [MarshalAs(UnmanagedType.LPWStr)] public readonly ushort ReleaseNumber; + [MarshalAs(UnmanagedType.LPWStr)] public readonly String ManufacturerString; + [MarshalAs(UnmanagedType.LPWStr)] public readonly String ProductString; + [MarshalAs(UnmanagedType.LPWStr)] public readonly ushort UsagePage; + [MarshalAs(UnmanagedType.LPWStr)] public readonly ushort Usage; + [MarshalAs(UnmanagedType.LPWStr)] public readonly int InterfaceNumber; internal IntPtr Next; // ReSharper restore MemberCanBePrivate.Global } @@ -62,12 +62,14 @@ namespace Vocaluxe.Lib.Input private const string _HIDApiDll = "libhidapi.so"; #endif #endif + private static IntPtr dataPtr; [DllImport(_HIDApiDll, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl, EntryPoint = "hid_init", CharSet = CharSet.Unicode)] private static extern int hid_init(); public static bool Init() { + dataPtr = Marshal.AllocCoTaskMem(22); int result; try { @@ -160,11 +162,8 @@ namespace Vocaluxe.Lib.Input [DllImport(_HIDApiDll, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl, EntryPoint = "hid_read_timeout", CharSet = CharSet.Unicode)] private static extern int hid_read_timeout(IntPtr device, IntPtr data, int length, int milliseconds); - public static int ReadTimeout(IntPtr device, out byte[] data, int length, int milliseconds) + public static int ReadTimeout(IntPtr device, ref byte[] data, int length, int milliseconds) { - data = new byte[length]; - IntPtr dataPtr = Marshal.AllocHGlobal(length); - int result; try { @@ -181,7 +180,6 @@ namespace Vocaluxe.Lib.Input else data = null; - Marshal.FreeHGlobal(dataPtr); return result; } diff --git a/Vocaluxe/Lib/Input/WiiMote/CWiiMote.cs b/Vocaluxe/Lib/Input/WiiMote/CWiiMote.cs index 6fffb49c..c57354ba 100644 --- a/Vocaluxe/Lib/Input/WiiMote/CWiiMote.cs +++ b/Vocaluxe/Lib/Input/WiiMote/CWiiMote.cs @@ -71,6 +71,7 @@ namespace Vocaluxe.Lib.Input.WiiMote public void Close() { _Active = false; + _WiiMote.Disconnect(); } public void Connect() diff --git a/Vocaluxe/Lib/Input/WiiMote/WiiMoteLib.cs b/Vocaluxe/Lib/Input/WiiMote/WiiMoteLib.cs index d23258ab..bc6db789 100644 --- a/Vocaluxe/Lib/Input/WiiMote/WiiMoteLib.cs +++ b/Vocaluxe/Lib/Input/WiiMote/WiiMoteLib.cs @@ -198,6 +198,16 @@ namespace Vocaluxe.Lib.Input.WiiMote _Error = false; _Reader = new Thread(_ReaderLoop); _Reader.Start(); + + if (!CHIDApi.Init()) + { + CLog.LogError("WiiMoteLib: Can't initialize HID API"); + const string msg = "Please install the Visual C++ Redistributable Packages 2008!"; + CLog.LogError(msg); + + _Active = false; + _Error = true; + } } ~CWiiMoteLib() @@ -224,35 +234,30 @@ namespace Vocaluxe.Lib.Input.WiiMote if (_Error) return false; - CHIDApi.Exit(); - - if (!CHIDApi.Init()) - { - CLog.LogError("WiiMoteLib: Can't initialize HID API"); - const string msg = "Please install the Visual C++ Redistributable Packages 2008!"; - CLog.LogError(msg); - - _Active = false; - _Error = true; - return false; - } - //Try WiiMotion - if (Connected = CHIDApi.Open(_VID, _PID, out _Handle)) + Connected = CHIDApi.Open(_VID, _PID, out _Handle); + if (Connected) { - if (!(Connected = _ReadCalibration())) + Connected = _ReadCalibration(); + if (!Connected) CHIDApi.Close(_Handle); } + else + CHIDApi.Close(_Handle); if (Connected) return true; //Try WiiMotion Plus - if (Connected = CHIDApi.Open(_VID, _PIDPlus, out _Handle)) + Connected = CHIDApi.Open(_VID, _PIDPlus, out _Handle); + if (Connected) { - if (!(Connected = _ReadCalibration())) + Connected = _ReadCalibration(); + if (!Connected) CHIDApi.Close(_Handle); } + else + CHIDApi.Close(_Handle); return Connected; @@ -335,7 +340,7 @@ namespace Vocaluxe.Lib.Input.WiiMote try { - CHIDApi.ReadTimeout(_Handle, out buff, _ReportLength, 100); + CHIDApi.ReadTimeout(_Handle, ref buff, _ReportLength, 200); } catch (Exception e) {