00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 namespace cmil {
00024 class DeviceInputs : public Logger {
00025 public:
00026 typedef std::map<const std::string, InputData* const> Inputs;
00027 DeviceInputs() {};
00028
00029 void cleanup();
00030
00031 void add(const std::string& set_device_name, InputData* input);
00032
00033 DeviceInputs* clone() const;
00034
00035 void dumpInputs() const;
00036
00037 const Inputs& getInputs() const;
00038 const Inputs& getCustomInputs() const;
00039
00041 static const bool use_base = true;
00042 static const bool use_custom = false;
00043
00044 InputData* const getData(bool which, const std::string& name) const;
00045
00046 std::string getName() const;
00047
00048 bool isEqual(bool match_custom, const DeviceInputs& inputs) const;
00049
00050 bool operator==(const DeviceInputs& inputs) const;
00051
00052 void removeInput(const std::string& name);
00053
00054 unsigned long int resetIDs( unsigned long int count );
00055
00056 void resetWhichDevice( unsigned long int which_device);
00057
00058 bool setInput(const std::string& old_name, const std::string& new_name);
00059 bool setInputs(const DeviceInputs& dev_inputs);
00060
00061 private:
00062 DeviceInputs(const Inputs& cloned_base, const Inputs& cloned_custom);
00063
00064 InputData* const getData(const Inputs& inputs, const std::string& name) const;
00065
00067 bool match_inputs(const Inputs& first, const Inputs& second) const;
00068
00070 bool erase(const std::string& name, Inputs& inputs);
00071 bool remove(const std::string& name, Inputs& inputs);
00072
00073 bool renameData(Inputs& inputs, const std::string& old_name, const std::string& new_name);
00074
00076 Inputs m_base;
00077
00079 Inputs m_custom;
00080 };
00081
00082 template <typename T> class tInputIndex : public Logger {
00083 public:
00084 tInputIndex(unsigned int size) : m_index(size, NULL) {};
00085 tInputIndex() {};
00086
00087 bool addData(InputData* input);
00088
00089 void fill_indexes(const std::string& device_name, const std::string& input_name, unsigned int num, DeviceInputs& inputs);
00090
00091 unsigned int getSize() const;
00092
00094
00095 bool setData(unsigned int i, InputData* input) {
00096 T* tmp_input_type;
00097
00098 if(m_index.size() >= i)
00099 if ( (tmp_input_type = dynamic_cast<T*>(input)) != 0 ) {
00100 if(m_index[i]) {
00101 log_error("setData() : InputData already present at key index value; memory will be leaked.");
00102 delete input;
00103
00104 return false;
00105 }
00106 else
00107 m_index[i] = tmp_input_type;
00108 }
00109 else
00110 return false;
00111
00112 return true;
00113 }
00114
00115 T* getData(unsigned int i) const;
00116
00117 std::string getName() const;
00118
00119 bool operator==(const tInputIndex<T>& other) const;
00120
00121 private:
00122 std::vector<T* > m_index;
00123 };
00124
00126
00127 class InputDevice : public Logger {
00128 public:
00129 ~InputDevice() {}
00130
00133 virtual InputDevice* clone() const;
00134
00135 void cleanup();
00136
00138
00139 void dumpInputs() const {
00140 m_inputs.dumpInputs();
00141 }
00142
00144
00145 std::string getName() const {
00146 return m_name;
00147 };
00148
00149 InputData* const getData(const std::string& name) const;
00150
00151 InputData* const getData(bool base_data, const std::string& name) const;
00152
00156
00157 virtual bool operator==(const InputDevice& dev) const;
00158
00159 const DeviceInputs& getInputs() const;
00160
00162
00163 bool setInputs(const InputDevice& dev);
00164
00165 protected:
00166 friend class DeviceRegistry;
00167
00168 InputDevice();
00169
00171 InputDevice(const std::string& name, const DeviceInputs& cloned_inputs );
00172
00174
00175 InputDevice( const std::string& name );
00176
00177 unsigned long int resetIDs( const unsigned long int count );
00178 void resetWhichDevice( const unsigned long int which_device );
00179 void add( InputData* const input );
00180
00182
00183 void removeInput(const std::string& name);
00184
00185 bool setInput(const std::string& old_name, const std::string& new_name);
00186
00188 DeviceInputs m_inputs;
00189
00190 private:
00191 std::string m_name;
00192 };
00193
00196
00197 class DeviceRegistry : public Logger {
00198 public:
00200 typedef std::vector<InputDevice > DeviceMultiple;
00202 typedef std::map<const std::string, DeviceMultiple> DeviceIndex;
00203
00204 DeviceRegistry() {}
00205
00206 void cleanup();
00207 void clear();
00208
00209 virtual ~DeviceRegistry() {};
00210
00211 virtual std::string getName() const {
00212 return "DeviceRegistry";
00213 };
00214
00215 void registerDevice(InputDevice& dev);
00216 void mergeRegistry(DeviceRegistry& reg);
00217 void SetInputIDs(InputDevice& dev);
00218
00219 int getSize() const;
00220
00221 std::string getDefaultDevice() const;
00222
00223 InputData* const getData(unsigned int id) const;
00224 InputData* const getData(const Ev& event) const;
00225
00228
00229 int getID(const Ev& event) const;
00230 void integrityCheck() const;
00231
00232 private:
00233 void copyDevice(InputDevice& dev);
00234
00235 DeviceIndex m_index;
00236
00237 std::string m_default_device;
00238
00240 std::vector<InputData* > m_inputs;
00241 };
00242
00243 class GameController : public InputDevice {
00244 public:
00245 GameController(const std::string& name, unsigned int axis, unsigned int buttons, unsigned int balls,
00246 unsigned int hats);
00247
00248
00249 virtual InputDevice* clone() const;
00250
00251 bool operator==(const GameController& controller) const;
00252
00254 tInputIndex<Axis> m_axes;
00255 tInputIndex<Press> m_buttons;
00256 tInputIndex<Motion> m_balls;
00257 tInputIndex<Hat> m_hats;
00258 };
00259
00261
00262 class Keyboard : public InputDevice {
00263 public:
00264 std::string getName() const {
00265 return "Keyboard";
00266 };
00267
00269 Keyboard(const std::string& name);
00270 Keyboard();
00271
00272 virtual InputDevice* clone() const;
00273
00274 bool operator==(const Keyboard& keyboard) const;
00275
00276 private:
00278 void init_inputs();
00279 };
00280
00282
00283 class Mouse : public InputDevice {
00284 public:
00286 Mouse(const std::string& name,
00287 unsigned int buttons,
00288 unsigned int coords);
00289
00290
00291 virtual InputDevice* clone() const;
00292
00293 virtual bool operator==(const Mouse& mouse) const;
00294
00295 tInputIndex<Press> m_buttons;
00296 tInputIndex<Coord> m_coords;
00297
00298 private:
00300 void init_inputs(const std::string& name, unsigned int buttons, unsigned int coords);
00301 };
00302 }