Ubuntu Pastebin

Paste from slvn_ at Mon, 9 Nov 2015 19:53:59 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
diff -r 7f019df79cfc src/video/mir/SDL_mirevents.c
--- a/src/video/mir/SDL_mirevents.c	Sat Jul 18 00:04:49 2015 -0400
+++ b/src/video/mir/SDL_mirevents.c	Fri Nov 06 19:42:54 2015 +0100
@@ -34,11 +34,12 @@
 
 #include "SDL_mirevents.h"
 #include "SDL_mirwindow.h"
-
+#include "SDL_log.h"
 #include <xkbcommon/xkbcommon.h>
 
 #include "SDL_mirdyn.h"
 
+
 static void
 HandleKeyText(int32_t key_code)
 {
@@ -70,12 +71,13 @@
 static void
 HandleKeyEvent(MirKeyEvent const ev, SDL_Window* window)
 {
+    const uint32_t unsigned_ev_scan_code = ev.scan_code;
     uint32_t scancode = SDL_SCANCODE_UNKNOWN;
     Uint8 key_state = ev.action == mir_key_action_up ? SDL_RELEASED : SDL_PRESSED;
 
     CheckKeyboardFocus(window);
 
-    if (ev.scan_code < SDL_arraysize(xfree86_scancode_table2))
+    if (unsigned_ev_scan_code < SDL_arraysize(xfree86_scancode_table2))
         scancode = xfree86_scancode_table2[ev.scan_code];
 
     if (scancode != SDL_SCANCODE_UNKNOWN)
@@ -85,8 +87,25 @@
         HandleKeyText(ev.key_code);
 }
 
-static void
-HandleMouseButton(SDL_Window* sdl_window, Uint8 state, MirMotionButton button_state)
+#define AMOTION_EVENT_ACTION_MASK                   0xff
+#define AMOTION_EVENT_ACTION_POINTER_INDEX_MASK     0xff00
+#define AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT    8
+
+static int32_t
+getActionMasked(MirMotionEvent const motion)
+{
+    return motion.action & AMOTION_EVENT_ACTION_MASK; 
+}
+
+static int32_t
+getActionIndex(MirMotionEvent const motion) 
+{
+    return (motion.action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
+        >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
+}
+
+static uint32_t
+mir_2_sdl_button_state(MirMotionButton button_state)
 {
     static uint32_t last_sdl_button;
     uint32_t sdl_button;
@@ -113,138 +132,240 @@
     }
 
     last_sdl_button = sdl_button;
-    SDL_SendMouseButton(sdl_window, 0, state, sdl_button);
+    return sdl_button;
 }
 
+
+// Very Similar to "Android_OnTouch" in the file "SDL_androidtouch.c"
 static void
-HandleMouseMotion(SDL_Window* sdl_window, int x, int y)
+Mir_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p,
+        SDL_Window *sdl_window, MirMotionButton mmb)
 {
-    SDL_SendMouseMotion(sdl_window, 0, 0, x, y);
-}
+    SDL_TouchID touchDeviceId = 0;
+    SDL_FingerID fingerId = 0;
+    int window_x, window_y;
+    static SDL_FingerID pointerFingerID = 0;
 
-static void
-HandleTouchPress(int device_id, int source_id, SDL_bool down, float x, float y, float pressure)
-{
-    SDL_SendTouch(device_id, source_id, down, x, y, pressure);
-}
+    int mWidth = sdl_window->w;
+    int mHeight = sdl_window->h;
 
-static void
-HandleTouchMotion(int device_id, int source_id, float x, float y, float pressure)
-{
-    SDL_SendTouchMotion(device_id, source_id, x, y, pressure);
-}
+    touchDeviceId = (SDL_TouchID)touch_device_id_in;
+    if (SDL_AddTouch(touchDeviceId, "") < 0) {
+        SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
+    }
 
-static void
-HandleMouseScroll(SDL_Window* sdl_window, int hscroll, int vscroll)
-{
-    SDL_SendMouseWheel(sdl_window, 0, hscroll, vscroll, SDL_MOUSEWHEEL_NORMAL);
-}
+    fingerId = (SDL_FingerID)pointer_finger_id_in;
 
-static void
-AddTouchDevice(int device_id)
-{
-    if (SDL_AddTouch(device_id, "") < 0)
-        SDL_SetError("Error: can't add touch %s, %d", __FILE__, __LINE__);
-}
+    switch (action) {
 
-static void
-HandleTouchEvent(MirMotionEvent const motion, int cord_index, SDL_Window* sdl_window)
-{
-    int device_id = motion.device_id;
-    int id = motion.pointer_coordinates[cord_index].id;
-
-    int width  = sdl_window->w;
-    int height = sdl_window->h;
-    float x   = motion.pointer_coordinates[cord_index].x;
-    float y   = motion.pointer_coordinates[cord_index].y;
-
-    float n_x = x / width;
-    float n_y = y / height;
-    float pressure = motion.pointer_coordinates[cord_index].pressure;
-
-    AddTouchDevice(motion.device_id);
-
-    switch (motion.action) {
         case mir_motion_action_down:
+            /* Primary pointer down */
+            window_x = x * mWidth;
+            window_y = y * mHeight;
+            /* send moved event */
+            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
+            /* send mouse down event */
+            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, mir_2_sdl_button_state(mmb));
+            pointerFingerID = fingerId;
         case mir_motion_action_pointer_down:
-            HandleTouchPress(device_id, id, SDL_TRUE, n_x, n_y, pressure);
+            /* Non primary pointer down */
+            SDL_SendTouch(touchDeviceId, fingerId, SDL_TRUE, x, y, p);
             break;
+            
+        case mir_motion_action_hover_move: // ? Also this one ?
+        case mir_motion_action_move:
+            if (!pointerFingerID) {
+                window_x = x * mWidth;
+                window_y = y * mHeight;
+                /* send moved event */
+                SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
+            }
+            SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
+            break;
+            
         case mir_motion_action_up:
+            /* Primary pointer up */
+            /* send mouse up */
+            pointerFingerID = (SDL_FingerID) 0;
+            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, mir_2_sdl_button_state(mmb));
         case mir_motion_action_pointer_up:
-            HandleTouchPress(device_id, id, SDL_FALSE, n_x, n_y, pressure);
+            /* Non primary pointer up */
+            SDL_SendTouch(touchDeviceId, fingerId, SDL_FALSE, x, y, p);
             break;
-        case mir_motion_action_hover_move:
-        case mir_motion_action_move:
-            HandleTouchMotion(device_id, id, n_x, n_y, pressure);
-            break;
+            
         default:
+            SDL_Log("Not handled MirEvent action=%08x (Mir_OnTouch)", action);
             break;
     }
 }
 
+
+
+// Very Similar to "public boolean onTouch(View v, MotionEvent event)" in the file "SDLActivity.java"
 static void
-HandleMouseEvent(MirMotionEvent const motion, int cord_index, SDL_Window* sdl_window)
+HandleMotionEvent(MirMotionEvent event, SDL_Window* sdl_window)
 {
-    SDL_SetMouseFocus(sdl_window);
+    /* Ref: http://developer.android.com/training/gestures/multi.html */
+    const int touchDevId = event.device_id;
+    const int pointerCount = event.pointer_count;
+    const int action = getActionMasked(event);
+    int pointerFingerId;
+    int i = -1;
+    float x,y,p;
 
-    switch (motion.action) {
+    const int mWidth = sdl_window->w;
+    const int mHeight = sdl_window->h;
+
+    switch (action) {    
+
+        case mir_motion_action_hover_move: // TODO  Also this one ?
+        case mir_motion_action_move:
+            for (i = 0; i < pointerCount; i++) {
+                pointerFingerId = event.pointer_coordinates[i].id;
+                x = event.pointer_coordinates[i].x / mWidth;
+                y = event.pointer_coordinates[i].y / mHeight;
+                p = event.pointer_coordinates[i].pressure;
+                Mir_OnTouch(touchDevId, pointerFingerId, action, x, y, p, sdl_window, event.button_state);
+            }
+            break;
+
+            /* 
+             * http://developer.android.com/reference/android/view/MotionEvent.html
+             *
+             * The current gesture has been aborted. You will not receive any
+             * more points in it. You should treat this as an up event, but not
+             * perform any action that you normally would.
+             */
+        case mir_motion_action_cancel:
+            for (i = 0; i < pointerCount; i++) {
+                pointerFingerId = event.pointer_coordinates[i].id;
+                x = event.pointer_coordinates[i].x / mWidth;
+                y = event.pointer_coordinates[i].y / mHeight;
+                p = event.pointer_coordinates[i].pressure;
+                Mir_OnTouch(touchDevId, pointerFingerId, mir_motion_action_up, x, y, p, sdl_window, event.button_state);
+            }
+            break;
+
+        case mir_motion_action_up:
         case mir_motion_action_down:
+            // Primary pointer up/down, the index is always zero
+            i = 0;
+            pointerFingerId = event.pointer_coordinates[i].id;
+            x = event.pointer_coordinates[i].x / mWidth;
+            y = event.pointer_coordinates[i].y / mHeight;
+            p = event.pointer_coordinates[i].pressure;
+            Mir_OnTouch(touchDevId, pointerFingerId, action, x, y, p, sdl_window, event.button_state);
+            break;
+
+        case mir_motion_action_pointer_up:
+            // BUG MIR = need to check all FingerId not to miss any FINGER_UP
+            // this brings some duplicate FINGER_UP.
+            for (i = 0; i < pointerCount; i++) {
+                pointerFingerId = event.pointer_coordinates[i].id;
+                x = event.pointer_coordinates[i].x / mWidth;
+                y = event.pointer_coordinates[i].y / mHeight;
+                p = event.pointer_coordinates[i].pressure;
+                Mir_OnTouch(touchDevId, pointerFingerId, action, x, y, p, sdl_window, event.button_state);
+            }
+            break;
+
         case mir_motion_action_pointer_down:
-            HandleMouseButton(sdl_window, SDL_PRESSED, motion.button_state);
+            // Non primary pointer up/down
+            i = getActionIndex(event);
+            pointerFingerId = event.pointer_coordinates[i].id;
+            x = event.pointer_coordinates[i].x / mWidth;
+            y = event.pointer_coordinates[i].y / mHeight;
+            p = event.pointer_coordinates[i].pressure;
+            Mir_OnTouch(touchDevId, pointerFingerId, action, x, y, p, sdl_window, event.button_state);
             break;
-        case mir_motion_action_up:
-        case mir_motion_action_pointer_up:
-            HandleMouseButton(sdl_window, SDL_RELEASED, motion.button_state);
-            break;
-        case mir_motion_action_hover_move:
-        case mir_motion_action_move:
-            HandleMouseMotion(sdl_window,
-                              motion.pointer_coordinates[cord_index].x,
-                              motion.pointer_coordinates[cord_index].y);
-            break;
+
         case mir_motion_action_outside:
             SDL_SetMouseFocus(NULL);
             break;
+
         case mir_motion_action_scroll:
-            HandleMouseScroll(sdl_window,
-                              motion.pointer_coordinates[cord_index].hscroll,
-                              motion.pointer_coordinates[cord_index].vscroll);
+            for (i = 0; i < pointerCount; i++) {
+                SDL_SendMouseWheel(sdl_window, 0,
+                    event.pointer_coordinates[i].hscroll,
+                    event.pointer_coordinates[i].vscroll, 
+                    SDL_MOUSEWHEEL_NORMAL
+                    );
+            } 
+
+        case mir_motion_action_hover_enter:
+            // TODO 
             break;
-        case mir_motion_action_cancel:
-        case mir_motion_action_hover_enter:
+
         case mir_motion_action_hover_exit:
+            // TODO 
+            break;
+
+        default:
+            SDL_Log("Nandled MirMotionEvent action=%08x", action);
+            break;
+    }    
+
+    return;
+}
+
+static void 
+HandleSurfaceEvent(MirSurfaceEvent event, SDL_Window * sdl_window)
+{
+    switch (event.attrib) {
+        case mir_surface_attrib_type:
+            SDL_Log("Not Handled MirSurfaceEvent Type id=%d value=%d", event.id, event.value);
+            break;
+        case mir_surface_attrib_state:
+            SDL_Log("Not Handled MirSurfaceEvent State id=%d value=%d", event.id, event.value);
+            break;
+        case mir_surface_attrib_swapinterval:
+            SDL_Log("Not Handled MirSurfaceEvent SwapInterval id=%d value=%d", event.id, event.value);
+            break;
+        case mir_surface_attrib_focus:
+            if (event.value) {
+                SDL_SetMouseFocus(sdl_window);
+            } else {
+                SDL_SetMouseFocus(NULL);
+            }
+            break;
+        case mir_surface_attribs:
+            SDL_Log("Not Handled MirSurfaceEvent Attribs id=%d value=%d", event.id, event.value);
             break;
         default:
+            SDL_Log("Not Handled MirSurfaceEvent Unkown id=%d value=%d", event.id, event.value);
             break;
     }
+
+    return;
 }
 
 static void
-HandleMotionEvent(MirMotionEvent const motion, SDL_Window* sdl_window)
+HandleResizeEvent(MirResizeEvent event, SDL_Window *sdl_window)
 {
-    int cord_index;
-    for (cord_index = 0; cord_index < motion.pointer_count; cord_index++) {
-        if (motion.pointer_coordinates[cord_index].tool_type == mir_motion_tool_type_finger) {
-            HandleTouchEvent(motion, cord_index, sdl_window);
-        }
-        else {
-            HandleMouseEvent(motion, cord_index, sdl_window);
-        }
-    }
+    SDL_Log("Not Handled MirResizeEvent surface_id=%d width=%d height=%d", event.surface_id, event.width, event.height);
+    return;
 }
+ 
 
 void
 MIR_HandleInput(MirSurface* surface, MirEvent const* ev, void* context)
 {
     SDL_Window* window = (SDL_Window*)context;
     switch (ev->type) {
-        case (mir_event_type_key):
+        case mir_event_type_key:
             HandleKeyEvent(ev->key, window);
             break;
-        case (mir_event_type_motion):
+        case mir_event_type_motion:
             HandleMotionEvent(ev->motion, window);
             break;
+        case mir_event_type_surface:
+            HandleSurfaceEvent(ev->surface, window);
+            break;
+        case mir_event_type_resize:
+            HandleResizeEvent(ev->resize, window);
+            break;
         default:
+            SDL_Log("Not handled MirEvent type=%08x", ev->type);
             break;
     }
 }
Download as text