]> git.tdb.fi Git - libs/gui.git/blob - source/input/android/touchscreen.cpp
Check for event type and source in Android touchscreen driver
[libs/gui.git] / source / input / android / touchscreen.cpp
1 #include <msp/graphics/window_private.h>
2 #include "touchscreen.h"
3
4 namespace Msp {
5 namespace Input {
6
7 bool Touchscreen::is_available()
8 {
9         return true;
10 }
11
12 void Touchscreen::input_event(const Graphics::Window::Event &event)
13 {
14         int type = AInputEvent_getType(event.aevent);
15         if(type!=AINPUT_EVENT_TYPE_MOTION)
16                 return;
17
18         int source = AInputEvent_getSource(event.aevent);
19         if(source!=AINPUT_SOURCE_TOUCHSCREEN && source!=AINPUT_SOURCE_STYLUS)
20                 return;
21
22         int action = AMotionEvent_getAction(event.aevent);
23         int action_pointer = (action&AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)>>AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
24         action &= AMOTION_EVENT_ACTION_MASK;
25
26         int pointer_count = AMotionEvent_getPointerCount(event.aevent);
27         for(int i=0; i<pointer_count; ++i)
28         {
29                 int pointer = AMotionEvent_getPointerId(event.aevent, i);
30                 float x = AMotionEvent_getX(event.aevent, i);
31                 float y = AMotionEvent_getY(event.aevent, i);
32                 touch_move(pointer, x*2/window.get_width()-1, 1-y*2/window.get_height());
33         }
34
35         switch(action)
36         {
37         case AMOTION_EVENT_ACTION_DOWN:
38         case AMOTION_EVENT_ACTION_POINTER_DOWN:
39                 touch_down(AMotionEvent_getPointerId(event.aevent, action_pointer));
40                 break;
41         case AMOTION_EVENT_ACTION_UP:
42         case AMOTION_EVENT_ACTION_POINTER_UP:
43                 touch_up(AMotionEvent_getPointerId(event.aevent, action_pointer));
44                 break;
45         default:;
46         }
47 }
48
49 } // namespace Input
50 } // namespace Msp