OSDN Git Service

フォーラム[#56172] SlimDX(改)のフォルダ内容が不完全だったので再UP。
[dtxmania/dtxmania.git] / SlimDXc_Jun2010(VC++2008) / source / directinput / DirectInput.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
6 * of this software and associated documentation files (the "Software"), to deal\r
7 * in the Software without restriction, including without limitation the rights\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
9 * copies of the Software, and to permit persons to whom the Software is\r
10 * furnished to do so, subject to the following conditions:\r
11\r
12 * The above copyright notice and this permission notice shall be included in\r
13 * all copies or substantial portions of the Software.\r
14\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
21 * THE SOFTWARE.\r
22 */ \r
23 #include <windows.h>\r
24 #include <dinput.h>\r
25 #include <vcclr.h>\r
26 \r
27 #include "../ComObject.h"\r
28 #include "../SlimDXException.h"\r
29 #include "../InternalHelpers.h"\r
30 #include "../CollectionShim.h"\r
31 \r
32 #include "DirectInput.h"\r
33 #include "DirectInputException.h"\r
34 #include "DirectInputNotFoundException.h"\r
35 \r
36 #include "CallbacksDI.h"\r
37 #include "DeviceDI.h"\r
38 \r
39 using namespace System;\r
40 using namespace System::Collections::Generic;\r
41 using namespace System::Runtime::InteropServices;\r
42 \r
43 namespace SlimDX\r
44 {\r
45 namespace DirectInput\r
46 {\r
47     DirectInput::DirectInput()\r
48         {\r
49                 IDirectInput8W* dinput;\r
50                 IntPtr hInstance = Marshal::GetHINSTANCE( DirectInput::typeid->Module );\r
51 \r
52         try\r
53         {\r
54                     HRESULT hr = DirectInput8Create( static_cast<HINSTANCE>( hInstance.ToPointer() ), DIRECTINPUT_VERSION, \r
55                             IID_IDirectInput8, reinterpret_cast<void**>( &dinput ), NULL );\r
56 \r
57                         RECORD_DINPUT( hr );\r
58         }\r
59         catch( SEHException^ ex )\r
60         {\r
61             throw gcnew DirectInputNotFoundException( "DirectInput was not found. Reinstalling DirectX may fix the problem.", ex );\r
62         }\r
63 \r
64                 if( dinput == NULL )\r
65                         throw gcnew DirectInputException( "Could not create DirectInput instance." );\r
66 \r
67                 Construct( dinput );\r
68         }\r
69 \r
70         Result DirectInput::RunControlPanel()\r
71         {\r
72                 HRESULT hr = InternalPointer->RunControlPanel( NULL, 0 );\r
73                 return RECORD_DINPUT( hr );\r
74         }\r
75 \r
76         Result DirectInput::RunControlPanel( IntPtr parent )\r
77         {\r
78                 HRESULT hr = InternalPointer->RunControlPanel( static_cast<HWND>( parent.ToPointer() ), 0 );\r
79                 return RECORD_DINPUT( hr );\r
80         }\r
81 \r
82         bool DirectInput::IsDeviceAttached( Guid device )\r
83         {\r
84                 HRESULT hr = InternalPointer->GetDeviceStatus( Utilities::ConvertManagedGuid( device ) );\r
85                 RECORD_DINPUT( hr );\r
86 \r
87                 return hr == DI_OK;\r
88         }\r
89 \r
90         Guid DirectInput::FindDevice( Guid deviceClass, String^ name )\r
91         {\r
92                 GUID result;\r
93                 pin_ptr<const wchar_t> pinnedName = PtrToStringChars( name );\r
94 \r
95                 HRESULT hr = InternalPointer->FindDevice( Utilities::ConvertManagedGuid( deviceClass ),\r
96                         reinterpret_cast<LPCTSTR>( pinnedName ), &result );\r
97                 RECORD_DINPUT( hr );\r
98 \r
99                 if( FAILED( hr ) )\r
100                         return Guid::Empty;\r
101 \r
102                 return Utilities::ConvertNativeGuid( result );\r
103         }\r
104 \r
105         IList<DeviceInstance^>^ DirectInput::GetDevices()\r
106         {\r
107                 return GetDevices( DeviceClass::All, DeviceEnumerationFlags::AllDevices );\r
108         }\r
109 \r
110         IList<DeviceInstance^>^ DirectInput::GetDevices( DeviceClass deviceClass, DeviceEnumerationFlags enumerationFlags )\r
111         {\r
112                 List<DeviceInstance^>^ results = gcnew List<DeviceInstance^>();\r
113                 CollectionShim<DeviceInstance^> shim( results );\r
114 \r
115                 HRESULT hr = InternalPointer->EnumDevices( static_cast<DWORD>( deviceClass ), static_cast<LPDIENUMDEVICESCALLBACK>( EnumerateDevices ), &shim, static_cast<DWORD>( enumerationFlags ) );\r
116                 if( RECORD_DINPUT( hr ).IsFailure )\r
117                         return nullptr;\r
118 \r
119                 return results;\r
120         }\r
121 \r
122         IList<DeviceInstance^>^ DirectInput::GetDevices( DeviceType deviceType, DeviceEnumerationFlags enumerationFlags )\r
123         {\r
124                 List<DeviceInstance^>^ results = gcnew List<DeviceInstance^>();\r
125                 CollectionShim<DeviceInstance^> shim( results );\r
126 \r
127                 HRESULT hr = InternalPointer->EnumDevices( static_cast<DWORD>( deviceType ), static_cast<LPDIENUMDEVICESCALLBACK>( EnumerateDevices ), &shim, static_cast<DWORD>( enumerationFlags ) );\r
128                 if( RECORD_DINPUT( hr ).IsFailure )\r
129                         return nullptr;\r
130 \r
131                 return results;\r
132         }\r
133 }\r
134 }