00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef INPUT_DEVICE_H_
00026 #define INPUT_DEVICE_H_
00027
00028 namespace Lamp{
00029
00030
00031
00032
00033
00034 class InputDevice{
00035 public:
00036
00037
00038
00039
00040
00041 InputDevice(bool exclusive) : inputDevice_(NULL), windowHandle_(NULL),
00042 exclusive_(exclusive), foreground_(true), initialized_(false){}
00043
00044
00045
00046
00047 virtual ~InputDevice(){
00048 SafeRelease(inputDevice_);
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 virtual bool initialize(
00058 DirectInputDevice* inputDevice, HWND windowHandle){
00059 Assert(inputDevice != NULL);
00060 inputDevice_ = inputDevice;
00061 windowHandle_ = windowHandle;
00062
00063 instance_.dwSize = sizeof(DIDEVICEINSTANCE);
00064 if(DirectXFailed(inputDevice->GetDeviceInfo(&instance_))){
00065 ErrorOut("InputDevice::initialize() "
00066 "GetDeviceInfo()に失敗しました。");
00067 return false;
00068 }
00069
00070 capabilities_.dwSize = sizeof(DIDEVCAPS);
00071 if(DirectXFailed(inputDevice->GetCapabilities(&capabilities_))){
00072 ErrorOut("InputDevice::initialize() "
00073 "GetCapabilities()に失敗しました。");
00074 return false;
00075 }
00076 initialized_ = true;
00077 return true;
00078 }
00079
00080
00081
00082
00083
00084
00085 virtual bool acquire(){
00086 Assert(initialized_);
00087 HRESULT result = inputDevice_->Acquire();
00088 if(DirectXSucceeded(result)){ return true; }
00089 if(result != DIERR_OTHERAPPHASPRIO){
00090 ErrorOut("InputDevice::acquire() Acquire()に失敗しました。");
00091 }
00092 return false;
00093 }
00094
00095
00096
00097
00098 virtual void unacquire(){
00099 Assert(initialized_);
00100 inputDevice_->Unacquire();
00101 }
00102
00103
00104
00105
00106
00107
00108 virtual bool polling(){
00109 if(isPolled()){ inputDevice_->Poll(); }
00110 return true;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 virtual bool setCooperativeLevel(bool exclusive, bool foreground){
00121 if(inputDevice_ != NULL){
00122
00123 u_int flag = DISCL_NOWINKEY;
00124 if(exclusive){ flag |= DISCL_EXCLUSIVE; }
00125 else{ flag |= DISCL_NONEXCLUSIVE; }
00126 if(foreground){ flag |= DISCL_FOREGROUND; }
00127 else{ flag |= DISCL_BACKGROUND; }
00128 unacquire();
00129 if(DirectXFailed(inputDevice_->SetCooperativeLevel(
00130 windowHandle_,flag))){
00131 ErrorOut("InputDevice::setCooperativeLevel() "
00132 "SetCooperativeLevel()に失敗しました。");
00133 return false;
00134 }
00135 acquire();
00136 }
00137 exclusive_ = exclusive;
00138 foreground_ = foreground;
00139 return true;
00140 }
00141
00142
00143
00144
00145
00146 virtual bool isExclusive() const{ return exclusive_; }
00147
00148
00149
00150
00151
00152 virtual bool isForeground() const{ return foreground_; }
00153
00154
00155
00156
00157
00158
00159 virtual String getProductName() const{
00160 return String(instance_.tszProductName);
00161 }
00162
00163
00164
00165
00166
00167 virtual String getInstanceName() const{
00168 return String(instance_.tszInstanceName);
00169 }
00170
00171
00172
00173
00174
00175 virtual int getAxisCount() const{ return capabilities_.dwAxes; }
00176
00177
00178
00179
00180
00181 virtual int getButtonCount() const{ return capabilities_.dwButtons; }
00182
00183
00184
00185
00186
00187 virtual int getPOVCount() const{ return capabilities_.dwPOVs; }
00188
00189
00190
00191
00192
00193 virtual bool isAttached() const{
00194 return ((capabilities_.dwFlags & DIDC_ATTACHED) != 0);
00195 }
00196
00197
00198
00199
00200
00201 virtual bool isPolled() const{
00202 return ((capabilities_.dwFlags & DIDC_POLLEDDATAFORMAT) != 0);
00203 }
00204
00205
00206
00207
00208
00209
00210 virtual String toString() const{ return getInputDeviceString(); }
00211
00212
00213
00214
00215
00216 virtual String getInputDeviceString() const{
00217 Assert(initialized_);
00218 String result;
00219 result.format("ProductName = %s\nInstanceName = %s\n"
00220 "軸数 = %d ボタン数 = %d 視点コントローラ = %d\n"
00221 "アタッチ = %d ポーリング = %d\n",
00222 getProductName().getBytes(), getInstanceName().getBytes(),
00223 getAxisCount(), getButtonCount(), getPOVCount(),
00224 isAttached(), isPolled());
00225 return result;
00226 }
00227
00228
00229 protected:
00230
00231
00232
00233
00234 virtual void setExclusive(bool exclusive){ exclusive_ = exclusive; }
00235
00236
00237
00238
00239
00240
00241 virtual void setForeground(bool foreground){ foreground_ = foreground; }
00242
00243
00244 DirectInputDevice* inputDevice_;
00245
00246 HWND windowHandle_;
00247
00248
00249 private:
00250
00251 InputDevice(const InputDevice& copy);
00252
00253
00254 void operator =(const InputDevice& copy);
00255
00256
00257 DIDEVICEINSTANCE instance_;
00258
00259 DIDEVCAPS capabilities_;
00260
00261 bool exclusive_;
00262
00263 bool foreground_;
00264
00265 bool initialized_;
00266
00267 };
00268
00269
00270 }
00271 #endif // End of INPUT_DEVICE_H_
00272