Gnash  0.8.11dev
InputDevice.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 #ifndef GNASH_INPUTDEVICE_H
19 #define GNASH_INPUTDEVICE_H
20 
21 #ifdef HAVE_CONFIG_H
22 #include "gnashconfig.h"
23 #endif
24 
25 #include <boost/scoped_array.hpp>
26 #include <boost/shared_array.hpp>
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/cstdint.hpp>
30 #include <vector>
31 #include <queue>
32 #include <linux/input.h>
33 #include <linux/uinput.h>
34 
35 #include "GnashKey.h"
36 
37 namespace gnash {
38 
39 // Define if you want to support multiple input devices of the same type.
40 // The default is to support the devices we prefer for mouse, keyboard,
41 // and touchscreen.
42 // #define MULTIPLE_DEVICES 1
43 
44 // If we have a mouse, but the size isn't specified, then this is the
45 // default size.
46 static const int DEFAULT_BUFFER_SIZE = 256;
47 
48 
49 // The Uinput device is write only, and is used to control the mouse movements.
50 // It's not really an input device, but uses the same subsystem.
52 {
53 public:
54  UinputDevice();
55  ~UinputDevice();
56  const char *id() { return "Uinput"; };
57  bool init();
58 
59  bool scanForDevice();
60 
61  // Move the mouse cursor to a specified location
62  bool moveTo(int x, int y);
63 private:
64  int _fd;
65  std::string _filespec;
66 };
67 
68 // This is an InputDevice class to cover the various touchscreens, Mice, or
69 // keyboards supported.
71 {
72 public:
73  typedef struct {
74  bool pressed;
76  int modifier;
77  int x;
78  int y;
79  int z;
80  int button;
81  int position;
82  int pressure;
83  int volumne;
84  int distance;
85  int rx;
86  int ry;
87  int rz;
88  int throttle;
89  int rudder;
90  int gas;
91  int brake;
92  int tiltX;
93  int tiltY;
94  } input_data_t;
95  typedef enum {
109  } devicetype_e;
110  InputDevice();
111  // Instantiate with the screen size
112  InputDevice(int x, int y);
113  virtual ~InputDevice();
114 
115  virtual const char *id() = 0;
116 
117  virtual bool init();
118  bool init(devicetype_e type);
119  bool init(devicetype_e type, size_t size);
120  bool init(devicetype_e type, const std::string &filespec);
121  bool init(devicetype_e type, const std::string &filespec, size_t size);
122  virtual bool init(const std::string &filespec, size_t size) = 0;
123  virtual bool check() = 0;
124 
125  static DSOEXPORT std::vector<boost::shared_ptr<InputDevice> > scanForDevices();
126 
129 
130  // Read data into the Device input buffer.
131  boost::shared_array<boost::uint8_t> readData(size_t size);
132  boost::shared_ptr<input_data_t> popData()
133  {
134  boost::shared_ptr<InputDevice::input_data_t> input;
135  if (_data.size()) {
136  // std::cerr << "FIXME: " <<_data.size() << std::endl;
137  input = _data.front();
138  _data.pop();
139  }
140  return input;
141  }
142 
143  static DSOEXPORT boost::shared_array<int> convertAbsCoords(int x, int y,
144  int width, int height);
145 
146  void setScreenSize(int x, int y)
147  {
148  _screen_width = x;
149  _screen_height = y;
150  }
151  void dump() const;
152 
153 protected:
154  void addData(bool pressed, key::code key, int modifier, int x, int y);
155 
157  std::string _filespec;
158  int _fd;
160  // These hold the data queue
161  boost::scoped_array<boost::uint8_t> _buffer;
162  std::queue<boost::shared_ptr<input_data_t> > _data;
165 };
166 
167 class MouseDevice : public InputDevice
168 {
169 public:
170  MouseDevice();
171  ~MouseDevice();
172  const char *id() { return "Mouse"; };
173  bool init();
174  bool init(const std::string &filespec, size_t size);
175  bool check();
176 
177  static std::vector<boost::shared_ptr<InputDevice> > scanForDevices();
178 
180  bool command(unsigned char cmd, unsigned char *buf, int count);
181 
182 private:
183  int _previous_x;
184  int _previous_y;
185 };
186 
187 class TouchDevice : public InputDevice
188 {
189 public:
190  const char *id() { return "TouchScreen"; };
191  TouchDevice();
192  virtual ~TouchDevice();
193  bool init();
194  bool init(const std::string &filespec, size_t size);
195  bool check();
196 
197  void apply_ts_calibration(float* cx, float* cy, int rawx, int rawy);
198 
199  static std::vector<boost::shared_ptr<InputDevice> > scanForDevices();
200 private:
201  // Although the value is only set when using a touchscreen, it takes up little
202  // memory to initialize a pointer to avoid lots of messy ifdefs.
203  struct tsdev *_tsDev;
204 };
205 
206 class EventDevice : public InputDevice
207 {
208 public:
209  EventDevice();
210  const char *id() { return "InputEvent"; };
211  virtual bool init();
212  virtual bool init(const std::string &filespec, size_t size);
213  virtual bool check();
214 
216 
217  // This looks for all the input event devices.
218  static std::vector<boost::shared_ptr<InputDevice> > scanForDevices();
219 
220 private:
221  // Keyboard SHIFT/CTRL/ALT states (left + right)
222  bool keyb_lshift, keyb_rshift, keyb_lctrl, keyb_rctrl, keyb_lalt, keyb_ralt;
223  struct input_id _device_info;
224 };
225 
226 } // end of gnash namespace
227 
228 // end of GNASH_INPUTDEVICE_H
229 #endif
230 
231 // Local Variables:
232 // mode: C++
233 // indent-tabs-mode: nil
234 // End: