Merge "Build and package Layer Management service binaries." into tizen
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / include / WindowSystems / WaylandEvdevInputEvent.h
1 /***************************************************************************
2  *
3  * Copyright 2010, 2011 BMW Car IT GmbH
4  * Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *
20  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
21  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
24  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  *
28  ****************************************************************************/
29
30 #ifndef _WAYLANDEVDEVINPUTEVENT_H_
31 #define _WAYLANDEVDEVINPUTEVENT_H_
32
33 #include "config.h"
34 #include <wayland-server.h>
35 #include "Log.h"
36 #include "WindowSystems/WaylandInputEvent.h"
37 #include "WindowSystems/WaylandBaseWindowSystem.h"
38
39 //////////////////////////////////////////////////////////////////////////////
40 #define MAX_SLOTS 16
41
42 enum evdev_event_type
43 {
44     EVDEV_ABSOLUTE_MOTION    = (1 << 0),
45     EVDEV_ABSOLUTE_MT_DOWN   = (1 << 1),
46     EVDEV_ABSOLUTE_MT_MOTION = (1 << 2),
47     EVDEV_ABSOLUTE_MT_UP     = (1 << 3),
48     EVDEV_RELATIVE_MOTION    = (1 << 4)
49 };
50
51 enum evdev_device_capability
52 {
53     EVDEV_KEYBOARD   = (1 << 0),
54     EVDEV_BUTTON     = (1 << 1),
55     EVDEV_MOTION_ABS = (1 << 2),
56     EVDEV_MOTION_REL = (1 << 3),
57     EVDEV_TOUCH      = (1 << 4)
58 };
59
60 enum key_state_update
61 {
62     STATE_UPDATE_AUTOMATIC,
63     STATE_UPDATE_NONE
64 };
65
66 class WaylandEvdevInputEvent;
67 struct evdev_dispatch;
68
69 struct evdev_input_device
70 {
71     WaylandEvdevInputEvent *master;
72     struct wl_list          link;
73     struct wl_event_source *source;
74     struct evdev_dispatch  *dispatch;
75     char *devnode;
76     int   fd;
77     struct
78     {
79         int min_x;
80         int max_x;
81         int min_y;
82         int max_y;
83
84         int32_t x;
85         int32_t y;
86     } abs;
87
88     struct
89     {
90         int slot;
91         int32_t x[MAX_SLOTS];
92         int32_t y[MAX_SLOTS];
93     } mt;
94     struct mtdev* mtdev;
95
96     struct
97     {
98         wl_fixed_t dx;
99         wl_fixed_t dy;
100     } rel;
101
102     unsigned int pending_events;
103     unsigned int caps;
104     int isMt;
105 };
106
107 struct evdev_dispatch_interface
108 {
109     // Process an evdev input event
110     void (*process)(struct evdev_dispatch *dispatch,
111                     struct evdev_input_device *device,
112                     struct input_event *event,
113                     uint32_t time);
114
115     // Destroy an event dispatch handler and free all its resources
116     void (*destroy)(struct evdev_dispatch *dispatch);
117 };
118
119 struct evdev_dispatch
120 {
121     struct evdev_dispatch_interface *interface;
122 };
123
124 //////////////////////////////////////////////////////////////////////////////
125
126 // Default values
127 #define DEFAULT_CONSTANT_ACCEL_NUMERATOR 50
128 #define DEFAULT_MIN_ACCEL_FACTOR 0.16
129 #define DEFAULT_MAX_ACCEL_FACTOR 1.0
130 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
131
132 enum touchpad_model
133 {
134     TOUCHPAD_MODEL_UNKNOWN = 0,
135     TOUCHPAD_MODEL_SYNAPTICS,
136     TOUCHPAD_MODEL_ALPS,
137     TOUCHPAD_MODEL_APPLETOUCH,
138     TOUCHPAD_MODEL_ELANTECH
139 };
140
141 #define TOUCHPAD_EVENT_NONE           0
142 #define TOUCHPAD_EVENT_ABSOLUTE_ANY  (1 << 0)
143 #define TOUCHPAD_EVENT_ABSOLUTE_X    (1 << 1)
144 #define TOUCHPAD_EVENT_ABSOLUTE_Y    (1 << 2)
145 #define TOUCHPAD_EVENT_REPORT        (1 << 3)
146
147 struct touchpad_model_spec
148 {
149     short vendor;
150     short product;
151     enum touchpad_model model;
152 };
153
154 enum touchpad_state
155 {
156     TOUCHPAD_STATE_NONE = 0,
157     TOUCHPAD_STATE_TOUCH,
158     TOUCHPAD_STATE_PRESS
159 };
160
161 #define TOUCHPAD_HISTORY_LENGTH 4
162
163 struct touchpad_motion
164 {
165     int32_t x;
166     int32_t y;
167 };
168
169 enum touchpad_fingers_state
170 {
171     TOUCHPAD_FINGERS_ONE   = (1 << 0),
172     TOUCHPAD_FINGERS_TWO   = (1 << 1),
173     TOUCHPAD_FINGERS_THREE = (1 << 2)
174 };
175
176 struct touchpad_dispatch
177 {
178     struct evdev_dispatch base;
179     struct evdev_input_device *device;
180
181     enum touchpad_model model;
182     enum touchpad_state state;
183     int finger_state;
184     int last_finger_state;
185
186     double constant_accel_factor;
187     double min_accel_factor;
188     double max_accel_factor;
189
190     unsigned int event_mask;
191     unsigned int event_mask_filter;
192
193     int reset;
194
195     struct
196     {
197         int32_t x;
198         int32_t y;
199     } hw_abs;
200
201     int has_pressure;
202     struct
203     {
204         int32_t touch_low;
205         int32_t touch_high;
206         int32_t press;
207     } pressure;
208
209     struct
210     {
211         int32_t margin_x;
212         int32_t margin_y;
213         int32_t center_x;
214         int32_t center_y;
215     } hysteresis;
216
217     struct touchpad_motion motion_history[TOUCHPAD_HISTORY_LENGTH];
218     int motion_index;
219     unsigned int motion_count;
220
221     struct wl_list motion_filters;
222 };
223
224 struct motion_params
225 {
226     double dx;
227     double dy;
228 };
229
230 struct motion_filter;
231
232 struct motion_filter_interface
233 {
234     void (*filter)(struct motion_filter *filter,
235                     struct motion_params *motion,
236                     void *data, uint32_t time);
237     void (*destroy)(struct motion_filter *filter);
238 };
239
240 struct motion_filter
241 {
242     struct motion_filter_interface *interface;
243     struct wl_list link;
244 };
245
246 typedef double (*accel_profile_func_t)(struct motion_filter *filter,
247                                         void *data,
248                                         double velocity,
249                                         uint32_t time);
250
251 struct pointer_tracker
252 {
253     double   dx;
254     double   dy;
255     uint32_t time;
256     int      dir;
257 };
258
259 struct pointer_accelerator
260 {
261     struct motion_filter base;
262     accel_profile_func_t profile;
263
264     double velocity;
265     double last_velocity;
266     int    last_dx;
267     int    last_dy;
268
269     struct pointer_tracker *trackers;
270     int cur_tracker;
271 };
272
273 enum directions
274 {
275     N = 1 << 0,
276     NE = 1 << 1,
277     E = 1 << 2,
278     SE = 1 << 3,
279     S = 1 << 4,
280     SW = 1 << 5,
281     W = 1 << 6,
282     NW = 1 << 7,
283     UNDEFINED_DIRECTION = 0xff
284 };
285
286 //////////////////////////////////////////////////////////////////////////////
287
288 class WaylandEvdevInputEvent : public WaylandInputEvent
289 {
290 // Properties
291 private:
292     struct udev*   m_udev;
293     struct wl_list m_deviceList;
294     int m_screenWidth;
295     int m_screenHeight;
296
297 // Methods
298 public:
299     WaylandEvdevInputEvent(WaylandBaseWindowSystem *windowSystem);
300     virtual ~WaylandEvdevInputEvent();
301
302     virtual void setupInputEvent();
303
304     // Default event handler
305     static void fallbackProcess(struct evdev_dispatch *dispatch,
306                                 struct evdev_input_device *device,
307                                 struct input_event *e,
308                                 uint32_t time);
309     static void fallbackDestroy(struct evdev_dispatch *dispatch);
310
311     // Multi-touch event handler
312     static void touchpadProcess(struct evdev_dispatch *dispatch,
313                                 struct evdev_input_device *device,
314                                 struct input_event *e,
315                                 uint32_t time);
316     static void touchpadDestroy(struct evdev_dispatch *dispatch);
317
318     static void flushMotion(struct evdev_input_device *device, uint32_t time);
319
320 private:
321     static int  handleInputEvent(int fd, uint32_t mask, void *data);
322     static void processEvents(struct evdev_input_device *device,
323                                 struct input_event *ev, int count);
324
325     // Default event handler
326     static void evdevProcessRelative(struct evdev_input_device *device,
327                                         uint32_t time, struct input_event *e);
328     static void evdevProcessAbsolute(struct evdev_input_device *device,
329                                         struct input_event *e);
330     static void evdevProcessKey(struct evdev_input_device *device,
331                                 uint32_t time, struct input_event *e);
332
333     // Multi-touch event handler
334     static void touchpadProcessAbsolute(struct touchpad_dispatch *touchpad,
335                                         struct evdev_input_device *device,
336                                         struct input_event *e);
337     static void touchpadProcessKey(struct touchpad_dispatch *touchpad,
338                                     struct evdev_input_device *device,
339                                     struct input_event *e,
340                                     uint32_t time);
341     static void touchpadUpdateState(struct touchpad_dispatch *touchpad,
342                                     uint32_t time);
343
344     // Notifier
345     static void notifyButton(struct evdev_input_device *device, uint32_t time,
346                                 int32_t button, enum wl_pointer_button_state state);
347     static void notifyMotion(struct evdev_input_device *device, uint32_t time,
348                                 wl_fixed_t fx, wl_fixed_t fy);
349     static void notifyKey(struct evdev_input_device *device, uint32_t time,
350                             uint32_t key, enum wl_keyboard_key_state state,
351                             bool bUpdateAutomatic);
352     static void notifyTouch(struct evdev_input_device *device);
353
354     bool addDevices();
355     void addDevice(struct udev_device *udevDevice);
356     void removeDevice(struct evdev_input_device *device);
357     void createInputDevice(struct wl_display *display, const char *path);
358     int  configureDevice(struct evdev_input_device *device);
359     void dispatchProcess(struct evdev_input_device *device,
360                             struct input_event *ev,
361                             uint32_t time);
362     void notifyKeyboardFocus();
363     void notifyKeyboardFocusIn(struct wl_array *keys, enum key_state_update updateState);
364     void updateModifierState(struct wl_seat *wlSeat, uint32_t serial, uint32_t key,
365                                 enum wl_keyboard_key_state state);
366     void notifyModifiers(struct wl_seat *wlSeat, uint32_t serial);
367
368     struct evdev_dispatch* createTouchpad(struct evdev_input_device *device);
369     void configureTouchpad(struct touchpad_dispatch *touchpad,
370                             struct evdev_input_device *device);
371 };
372
373 #endif /* _WAYLANDEVDEVINPUTEVENT_H_ */