OSDN Git Service

#32713 初コミット。SVNrev567時点での、ファイルはbranch/140707(ReBuild XGVersion)から移行したもの。
[dtxmaniaxg-verk/dtxmaniaxg-verk-git.git] / SlimDXc_Jun2010(VC++2008) / source / directinput / Keyboard.cpp
1 /*
2 * Copyright (c) 2007-2010 SlimDX Group
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22 #include "stdafx.h"
23
24 #include <windows.h>
25 #include <dinput.h>
26
27 #include "../stack_array.h"
28 #include "../ComObject.h"
29 #include "../Utilities.h"
30
31 #include "DirectInput.h"
32 #include "DirectInputException.h"
33
34 #include "Keyboard.h"
35
36 using namespace System;
37 using namespace System::Collections::Generic;
38 using namespace System::Windows::Forms;
39 using namespace System::Runtime::InteropServices;
40
41 namespace SlimDX
42 {
43 namespace DirectInput
44 {
45         Keyboard::Keyboard( DirectInput^ directInput ) : Device( directInput, Utilities::ConvertNativeGuid( GUID_SysKeyboard ) )
46         {
47                 HRESULT hr = InternalPointer->SetDataFormat( &c_dfDIKeyboard );
48                 if( RECORD_DINPUT( hr ).IsFailure )
49                         throw gcnew DirectInputException( Result::Last );
50         }
51
52         IList<KeyboardState^>^ Keyboard::GetBufferedData()
53         {
54                 DWORD size = INFINITE;
55                 HRESULT hr = InternalPointer->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), NULL, &size, DIGDD_PEEK );
56                 if( RecordError( hr ).IsFailure )
57                         return nullptr;
58
59                 List<KeyboardState^>^ list = gcnew List<KeyboardState^>( size );
60                 if( size == 0 )
61                         return list;
62
63                 stack_array<DIDEVICEOBJECTDATA> data = stackalloc( DIDEVICEOBJECTDATA, size );
64                 hr = InternalPointer->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ), &data[0], &size, 0 );
65                 if( RecordError( hr ).IsFailure )
66                         return nullptr;
67
68                 if( size == 0 )
69                         return list;
70
71                 for( unsigned int i = 0; i < size; i++ )
72                 {
73                         KeyboardState^ result = gcnew KeyboardState();
74                         result->UpdateKey( data[i].dwOfs, data[i].dwData > 0 );
75                         result->TimeStamp = data[i].dwTimeStamp;
76
77                         list->Add( result );
78                 }
79
80                 return list;
81         }
82
83         Result Keyboard::GetCurrentState( KeyboardState^% data )
84         {
85                 BYTE keys[256];
86                 HRESULT hr = InternalPointer->GetDeviceState( 256, keys );
87                 if( RecordError( hr ).IsFailure )
88                         return Result::Last;
89
90                 data->UpdateKeys( keys, 256 );
91
92                 return Result::Last;
93         }
94
95         KeyboardState^ Keyboard::GetCurrentState()
96         {
97                 KeyboardState^ result = gcnew KeyboardState();
98                 GetCurrentState( result );
99                 return result;
100         }
101
102         ObjectProperties^ Keyboard::GetObjectPropertiesByName( String^ name )
103         {
104                 return gcnew ObjectProperties( InternalPointer, name, KeyboardState::typeid );
105         }
106
107         DeviceObjectInstance Keyboard::GetObjectInfoByName( String^ name )
108         {
109                 DIDEVICEOBJECTINSTANCE di;
110                 di.dwSize = sizeof( DIDEVICEOBJECTINSTANCE );
111
112                 HRESULT hr = InternalPointer->GetObjectInfo( &di, Marshal::OffsetOf( KeyboardState::typeid, name ).ToInt32(), DIPH_BYUSAGE );
113                 if( RECORD_DINPUT( hr ).IsFailure )
114                         return DeviceObjectInstance();
115
116                 return DeviceObjectInstance( di );
117         }
118 }
119 }