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 <memory>
26 #include <cstdint>
27 #include <vector>
28 #include <queue>
29 #include <linux/input.h>
30 #ifdef HAVE_LINUX_UINPUT_H
31 #include <linux/uinput.h>
32 #endif
33 
34 #include "GnashKey.h"
35 
36 namespace gnash {
37 
38 // Define if you want to support multiple input devices of the same type.
39 // The default is to support the devices we prefer for mouse, keyboard,
40 // and touchscreen.
41 // #define MULTIPLE_DEVICES 1
42 
43 // If we have a mouse, but the size isn't specified, then this is the
44 // default size.
45 static const int DEFAULT_BUFFER_SIZE = 256;
46 
47 
48 // The Uinput device is write only, and is used to control the mouse movements.
49 // It's not really an input device, but uses the same subsystem.
51 {
52 public:
53  UinputDevice();
54  ~UinputDevice();
55  const char *id() { return "Uinput"; };
56  bool init();
57 
58  bool scanForDevice();
59 
60  // Move the mouse cursor to a specified location
61  bool moveTo(int x, int y);
62 private:
63  int _fd;
64  std::string _filespec;
65 };
66 
67 // This is an InputDevice class to cover the various touchscreens, Mice, or
68 // keyboards supported.
70 {
71 public:
72  typedef struct {
73  bool pressed;
75  int modifier;
76  int x;
77  int y;
78  int z;
79  int button;
80  int position;
81  int pressure;
82  int volumne;
83  int distance;
84  int rx;
85  int ry;
86  int rz;
87  int throttle;
88  int rudder;
89  int gas;
90  int brake;
91  int tiltX;
92  int tiltY;
93  } input_data_t;
94  typedef enum {
107  TSLIB
108  } devicetype_e;
109  InputDevice();
110  // Instantiate with the screen size
111  InputDevice(int x, int y);
112  virtual ~InputDevice();
113 
114  virtual const char *id() = 0;
115 
116  virtual bool init();
117  bool init(devicetype_e type);
118  bool init(devicetype_e type, size_t size);
119  bool init(devicetype_e type, const std::string &filespec);
120  bool init(devicetype_e type, const std::string &filespec, size_t size);
121  virtual bool init(const std::string &filespec, size_t size) = 0;
122  virtual bool check() = 0;
123 
124  static DSOEXPORT std::vector<std::shared_ptr<InputDevice> > scanForDevices();
125 
126  InputDevice::devicetype_e getType() { return _type; };
127  void setType(InputDevice::devicetype_e x) { _type = x; };
128 
129  // Read data into the Device input buffer.
130  std::unique_ptr<std::uint8_t[]> readData(size_t size);
131  std::shared_ptr<input_data_t> popData()
132  {
133  std::shared_ptr<InputDevice::input_data_t> input;
134  if (_data.size()) {
135  // std::cerr << "FIXME: " <<_data.size() << std::endl;
136  input = _data.front();
137  _data.pop();
138  }
139  return input;
140  }
141 
142  static DSOEXPORT std::unique_ptr<int[]> convertAbsCoords(int x, int y,
143  int width, int height);
144 
145  void setScreenSize(int x, int y)
146  {
147  _screen_width = x;
148  _screen_height = y;
149  }
150  void dump() const;
151 
152 protected:
153  void addData(bool pressed, key::code key, int modifier, int x, int y);
154 
155  devicetype_e _type;
156  std::string _filespec;
157  int _fd;
159  // These hold the data queue
160  std::unique_ptr<std::uint8_t[]> _buffer;
161  std::queue<std::shared_ptr<input_data_t> > _data;
164 };
165 
166 class MouseDevice : public InputDevice
167 {
168 public:
169  MouseDevice();
170  ~MouseDevice();
171  const char *id() { return "Mouse"; };
172  bool init();
173  bool init(const std::string &filespec, size_t size);
174  bool check();
175 
176  static std::vector<std::shared_ptr<InputDevice> > scanForDevices();
177 
179  bool command(unsigned char cmd, unsigned char *buf, int count);
180 
181 private:
182  int _previous_x;
183  int _previous_y;
184 };
185 
186 class TouchDevice : public InputDevice
187 {
188 public:
189  const char *id() { return "TouchScreen"; };
190  TouchDevice();
191  virtual ~TouchDevice();
192  bool init();
193  bool init(const std::string &filespec, size_t size);
194  bool check();
195 
196  void apply_ts_calibration(float* cx, float* cy, int rawx, int rawy);
197 
198  static std::vector<std::shared_ptr<InputDevice> > scanForDevices();
199 private:
200  // Although the value is only set when using a touchscreen, it takes up little
201  // memory to initialize a pointer to avoid lots of messy ifdefs.
202  struct tsdev *_tsDev;
203 };
204 
205 class EventDevice : public InputDevice
206 {
207 public:
208  EventDevice();
209  const char *id() { return "InputEvent"; };
210  virtual bool init();
211  virtual bool init(const std::string &filespec, size_t size);
212  virtual bool check();
213 
214  gnash::key::code scancode_to_gnash_key(int code, bool shift);
215 
216  // This looks for all the input event devices.
217  static std::vector<std::shared_ptr<InputDevice> > scanForDevices();
218 
219 private:
220  // Keyboard SHIFT/CTRL/ALT states (left + right)
221  bool keyb_lshift, keyb_rshift, keyb_lctrl, keyb_rctrl, keyb_lalt, keyb_ralt;
222  struct input_id _device_info;
223 };
224 
225 } // end of gnash namespace
226 
227 // end of GNASH_INPUTDEVICE_H
228 #endif
229 
230 // Local Variables:
231 // mode: C++
232 // indent-tabs-mode: nil
233 // End:
std::shared_ptr< input_data_t > popData()
Definition: InputDevice.h:131
const char * id()
Definition: InputDevice.h:171
Definition: InputDevice.h:105
Definition: InputDevice.h:100
std::unique_ptr< std::uint8_t[]> _buffer
Definition: InputDevice.h:160
const char * id()
Definition: InputDevice.h:189
modifier
Definition: GnashKey.h:33
int y
Definition: InputDevice.h:77
Definition: InputDevice.h:72
Definition: InputDevice.h:95
void setType(InputDevice::devicetype_e x)
Definition: InputDevice.h:127
Definition: InputDevice.h:50
int tiltY
Definition: InputDevice.h:92
Definition: InputDevice.h:166
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
int _screen_height
Definition: InputDevice.h:163
devicetype_e _type
Definition: InputDevice.h:155
Definition: InputDevice.h:96
type
Definition: GnashKey.h:329
int tiltX
Definition: InputDevice.h:91
gnash::key::code key
Definition: InputDevice.h:74
int volumne
Definition: InputDevice.h:82
int position
Definition: InputDevice.h:80
Definition: klash_part.cpp:329
int distance
Definition: InputDevice.h:83
Definition: InputDevice.h:103
int _screen_width
Definition: InputDevice.h:162
void setScreenSize(int x, int y)
Definition: InputDevice.h:145
Definition: InputDevice.h:102
Definition: InputDevice.h:205
input_data_t _input_data
Definition: InputDevice.h:158
code
Definition: GnashKey.h:43
int x
Definition: InputDevice.h:76
Definition: InputDevice.h:98
int rz
Definition: InputDevice.h:86
int z
Definition: InputDevice.h:78
Definition: klash_part.cpp:329
Definition: InputDevice.h:97
Definition: InputDevice.h:104
int pressure
Definition: InputDevice.h:81
std::int32_t x
Definition: BitmapData_as.cpp:434
#define DSOEXPORT
Definition: dsodefs.h:55
Definition: InputDevice.h:69
int throttle
Definition: InputDevice.h:87
int rudder
Definition: InputDevice.h:88
Definition: InputDevice.h:186
int gas
Definition: InputDevice.h:89
int ry
Definition: InputDevice.h:85
Definition: InputDevice.h:101
InputDevice::devicetype_e getType()
Definition: InputDevice.h:126
bool pressed
Definition: InputDevice.h:73
const char * id()
Definition: InputDevice.h:209
Definition: InputDevice.h:106
std::int32_t y
Definition: BitmapData_as.cpp:435
std::queue< std::shared_ptr< input_data_t > > _data
Definition: InputDevice.h:161
std::string _filespec
Definition: InputDevice.h:156
Definition: InputDevice.h:99
int modifier
Definition: InputDevice.h:75
int _fd
Definition: InputDevice.h:157
devicetype_e
Definition: InputDevice.h:94
int brake
Definition: InputDevice.h:90
const char * id()
Definition: InputDevice.h:55
int rx
Definition: InputDevice.h:84
int button
Definition: InputDevice.h:79