summaryrefslogtreecommitdiff
path: root/common/src/ivug-mouse-event.c
blob: e58240af275e967e91a74265147b400a13cfb358 (plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <Elementary.h>
#include <stdbool.h>
#include "ivug-debug.h"
#include "ivug-mouse-event.h"


// Click condition.
// Down Up duration under 300ms & Down pos, Up pos under 3 pixel


// Long pressed condition
// Down Up duration upper 1s & Down pos, Up pos under 5 pixel

typedef enum {
	MOUSE_DOWN_UNKNWON,
	MOUSE_DOWN_NONE,
	MOUSE_DOWN_1,
	MOUSE_DOWN_2,
	MOUSE_DOWN_3,
	MOUSE_DOWN_4,		// Invalid state.
} MouseState;



#define INVALID_DEVICE_ID (-1)
#define MULTI_INDEX (9)
#define NUM_MAX_BUTTON (3)

typedef struct _mouse_data_t{
	void *pClientData;
	Evas_Object *obj;

	mouse_callback_t cb;

	MouseState m_state;

	bool bDown[NUM_MAX_BUTTON];		// For Mose down check before mouse move
	bool bHold[NUM_MAX_BUTTON];

	MouseEvent event[NUM_MAX_BUTTON];
	MouseEvent down[NUM_MAX_BUTTON];
	MouseEvent prve_up[NUM_MAX_BUTTON];

	unsigned int prev_dist;

	int multi_min_idx;			// multi min index is different in each target. Temp solution

	bool bSendPinchStart;

	MouseEvent center;
	int pinch_dist;
// For debugging purpose, later will be removed.
	char alias[100];		//
} mouse_data_t;


#ifndef __UNUSED__
#define __UNUSED__ __attribute__((unused))
#endif

static unsigned int _get_distance(int prevX, int prevY, int X, int Y)
{
#include <math.h>
	int dx = prevX - X;
	int dy = prevY - Y;

	return sqrt(dx*dx + dy*dy);
}

static void _on_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	if ( data == NULL )
	{
		MSG_MOUSE_ERROR("Debug me!!!!");
		return;
	}

	mouse_data_t *pData = (mouse_data_t *)data;
	MouseEvent *ev = event_info;

	if ( ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
	{
		MSG_MOUSE_HIGH("[%s] %s On Hold. Dev=%d", pData->alias, __func__, ev->device);
	}

	int pressed = ev->device;

	if(pressed >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("Invalid Pressed(%d)", pressed);
		return;
	}

	if ( pData->m_state == MOUSE_DOWN_UNKNWON )
	{
		MSG_MOUSE_ERROR("[%s] Something wrong", pData->alias);
	}

	if 
( pData->bDown[pressed] == true )
	{
		MSG_MOUSE_WARN("Mouse down is detected. but already pressed button. ignore");
		return;
	}

	pData->m_state++;
	pData->bDown[pressed] = true;		// TODO : probably, can make memory overwritten issues.

	if ( pData->m_state == MOUSE_DOWN_1 )
	{
		MSG_MOUSE_HIGH("[%s] Callback Mouse Down : Dev=%d (%d,%d)", pData->alias, ev->device, ev->x, ev->y);

		if ( pData->cb.onMouseDown )
		{
			(pData->cb.onMouseDown)(pData->obj, ev, pData->pClientData);
		}

		MSG_MOUSE_MED("Func:%s Device=%d XY(%d,%d)", "_on_mouse_down", pressed, ev->x , ev->y );
	}
	else if ( pData->m_state == MOUSE_DOWN_2 )
	{
		MouseEvent *other = NULL;

		if ( pData->bDown[ (pressed + 1) % NUM_MAX_BUTTON] == true )
		{
			other = &pData->event[(pressed + 1) % NUM_MAX_BUTTON];
		}

		if ( pData->bDown[(pressed + 2) % NUM_MAX_BUTTON] == true )
		{
			if ( other != NULL )
			{
				MSG_MOUSE_ERROR("Error!!!");
			}
			other = &pData->event[(pressed + 2) % NUM_MAX_BUTTON];
		}

		if ( other == NULL )
		{
			MSG_MOUSE_ERROR("[%s] Error! Pressed(%d,%d,%d)",  pData->alias,
				pData->bDown[0],
				pData->bDown[1],
				pData->bDown[2]);

			return ;		// Fix for B/S
		}

		int centerX = ( other->x + ev->x ) / 2;
		int centerY = ( other->y + ev->y ) / 2;

		int dist = _get_distance(other->x, other->y, ev->x, ev->y);

		MSG_MOUSE_HIGH("[%s] Callback PinchStart : Dev=%d OtherDev=%d Center(%d,%d) Dist=%d", pData->alias, ev->device, other->device, centerX, centerY, dist);

		if ( pData->cb.onPinchStart )
		{
			pData->center.x = centerX;
			pData->center.y = centerY;
			pData->center.timestamp = ev->timestamp;
			pData->pinch_dist = dist;

			(pData->cb.onPinchStart)(pData->obj, &pData->center, dist, pData->pClientData);
		}

		pData->prev_dist = dist;

		MSG_MOUSE_HIGH("[%s] Two fingure is clicked. Distance=%d", pData->alias, pData->prev_dist);
	}
	else
	{
		MSG_MOUSE_WARN("[%s] Ignore third finger! State=%d Dev=%d bPressed(%d,%d,%d)", pData->alias, pData->m_state, ev->device,
			pData->bDown[0],
			pData->bDown[1],
			pData->bDown[2]);

		return ;
	}

	pData->event[pressed] = *ev;
	pData->down[pressed] = *ev;

	return ;
}


static void _on_mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{

	if ( data == NULL )
	{
		MSG_MOUSE_ERROR("Debug me!!!!");
		return;
	}

	mouse_data_t *pData = (mouse_data_t *)data;
	MouseEvent *ev = event_info;

	int pressed = ev->device;

	if(pressed >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("Invalid Pressed(%d)", pressed);
		return;
	}

	if ( pData->m_state == MOUSE_DOWN_UNKNWON )
	{
		MSG_MOUSE_ERROR("[%s] Something wrong. Dev=%d",  pData->alias, ev->device);
		MSG_MOUSE_ERROR("[%s] Error! State=%d Dev=%d bPressed(%d,%d,%d)", pData->alias, pData->m_state, ev->device,
			pData->bDown[0],
			pData->bDown[1],
			pData->bDown[2]);

	}

	if ( pData->m_state == MOUSE_DOWN_NONE )
	{
		MSG_MOUSE_WARN("[%s] Mouse up is detected. but state is NONE.", pData->alias);
		pData->bDown[0] = pData->bDown[1] = pData->bDown[2] = 0;
		return;
	}

	pData->m_state--;

	if 
( pData->bDown[pressed] == false )
	{
		MSG_MOUSE_ERROR("[%s] Something wrong", pData->alias);
		MSG_MOUSE_ERROR("[%s] Error! State=%d Dev=%d bPressed(%d,%d,%d)", pData->alias, pData->m_state, ev->device,
			pData->bDown[0],
			pData->bDown[1],
			pData->bDown[2]);
	}

	if ( ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
	{
		MSG_MOUSE_HIGH("[%s] %s On Hold. Dev=%d", pData->alias, __func__, ev->device);
		pData->bHold[pressed] = true;
	}

	pData->bDown[pressed] = false;

	if ( pData->m_state == MOUSE_DOWN_1 )
	{
		MSG_MOUSE_HIGH("[%s] Callback Pinch End", pData->alias);

		if ( pData->cb.onPinchEnd )
		{
			pData->bSendPinchStart = false;
			(pData->cb.onPinchEnd)(pData->obj, ev, pData->pClientData);
		}
	}
	else if ( pData->m_state == MOUSE_DOWN_NONE )
	{
		MSG_MOUSE_HIGH("[%s] Callback Mouse Up : Dev=%d (%d,%d)", pData->alias, ev->device, ev->x, ev->y);

		if ( ev->button_flags & EVAS_BUTTON_DOUBLE_CLICK)
		{
			if ( pData->cb.onMouseDlbClick )
			{
				(pData->cb.onMouseDlbClick)(pData->obj, ev, pData->pClientData);
			}
			MSG_MOUSE_HIGH("[%s] %s Double click detected. Dev=%d (%d,%d)", pData->alias, __func__, ev->device, ev->x, ev->y);
		}
		if ( pData->cb.onMouseUp )
		{
			(pData->cb.onMouseUp)(pData->obj, ev, pData->pClientData);
		}

		pData->prve_up[pressed] = *ev;

		MSG_MOUSE_MED("Func:%s State=%d Device=%d XY(%d,%d)", "_on_mouse_up", pData->m_state, pressed, ev->x , ev->y);
	}

	return ;

}

static void _on_mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	if ( data == NULL )
	{
		MSG_MOUSE_ERROR("Debug me!!!!");
		return;
	}

	mouse_data_t *pData = (mouse_data_t *)data;
	MouseEvent *ev = event_info;

	int pressed = ev->device;

	if ( ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
	{
		MSG_MOUSE_HIGH("[%s] %s On Hold. Dev=%d", pData->alias, __func__, ev->device);
	}

	if(pressed >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("Invalid Pressed(%d)", pressed);
		return;
	}

	if ( pData->bDown[pressed] == false)
	{
		// Move event prior down should be ignored
		MSG_MOUSE_MED("Ignore move event before click");
		return ;
	}

	switch ( pData->m_state )
	{
	case MOUSE_DOWN_3:
		// Ignore Mouse Event when all 3 buttons is pressed.
		return ;
	case MOUSE_DOWN_2:
	{
		MouseEvent *other = NULL;

		if ( pData->bDown[ (pressed + 1) % NUM_MAX_BUTTON] == true )
		{
			other = &pData->event[(pressed + 1) % NUM_MAX_BUTTON];
		}

		if ( pData->bDown[(pressed + 2) % NUM_MAX_BUTTON] == true )
		{
			if ( other != NULL )
			{
				MSG_MOUSE_ERROR("Debug me!!!");
			}
			other = &pData->event[(pressed + 2) % NUM_MAX_BUTTON];
		}

		if ( other == NULL )
		{
			MSG_MOUSE_ERROR("[%s] Error! Pressed(%d,%d,%d)", pData->alias,
				pData->bDown[0],
				pData->bDown[1],
				pData->bDown[2]);

			// TODO : Recover.
			return;
		}

		int centerX = ( other->x + ev->x ) / 2;
		int centerY = ( other->y + ev->y ) / 2;

		int dist = _get_distance(other->x, other->y, ev->x, ev->y);

		if ( pData->cb.onPinchMove )
		{
			MSG_MOUSE_MED("Callback Pinch : (%d,%d) dDistance=%d", centerX, centerY, dist - pData->prev_dist);

			pData->center.x = centerX;
			pData->center.y = centerY;
			pData->center.timestamp = ev->timestamp;
			pData->pinch_dist = dist;

			(pData->cb.onPinchMove)(pData->obj, &pData->center, pData->pinch_dist , pData->pClientData);
		}

		pData->prev_dist = dist;

		MSG_MOUSE_LOW("Pinch zoom. Pos1(%d,%d) Pos2(%d,%d) Delta=%d", other->x, other->y, ev->x, ev->y, pData->prev_dist);

		pData->event[pressed] = *ev;

		return ;
	}
	break;
	case MOUSE_DOWN_1:
	{
		MSG_MOUSE_MED("Func:%s. Device=%d Old(%d,%d) Cur(%d,%d)", __FUNCTION__, pressed,
				pData->event[pressed].x, pData->event[pressed].y,
				ev->x , ev->y);

		if ( pData->cb.onMouseMove )
		{
			MSG_MOUSE_MED("[%s] Callback Mouse Move : (%d,%d)",  pData->alias, ev->x, ev->y);
			(pData->cb.onMouseMove)(pData->obj, &pData->event[pressed], ev, pData->pClientData);
		}

		pData->event[pressed] = *ev;

		break;
	}
	default:
		MSG_MOUSE_ERROR("[%s] Something wrong. state=%d", pData->alias, pData->m_state);
		break;

	}

	return ;
}



static void _proxy_on_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Mouse_Down *ev = event_info;

	MouseEvent event;

	event.device = 0;
	event.timestamp = ev->timestamp;
	event.x = ev->canvas.x;
	event.y = ev->canvas.y;
	event.button_flags = ev->flags;
	event.event_flags = ev->event_flags;

	MSG_MOUSE_MED("%s Dev=%d Button=%d Flags=%d Event=%d", __func__, (int)ev->dev, ev->button, ev->flags, ev->event_flags);

	_on_mouse_down(data, NULL, obj, (void *)&event);
}

static void _proxy_on_mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Mouse_Up *ev = event_info;

	MouseEvent event;

	event.device = 0;
	event.timestamp = ev->timestamp;
	event.x = ev->canvas.x;
	event.y = ev->canvas.y;
	event.button_flags = ev->flags;
	event.event_flags = ev->event_flags;


	MSG_MOUSE_MED("%s Dev=%d Button=%d Flags=%d Event=%d", __func__, (int)ev->dev, ev->button, ev->flags, ev->event_flags);


	_on_mouse_up(data, NULL, obj, (void *)&event);
}

static void _proxy_on_mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Mouse_Move *ev = event_info;

	MouseEvent event;

	event.device = 0;
	event.timestamp = ev->timestamp;
	event.x = ev->cur.canvas.x;
	event.y = ev->cur.canvas.y;
	event.button_flags = 0;
	event.event_flags = ev->event_flags;

	MSG_MOUSE_MED("%s Dev=%d Button=%d Event=%d Prev(%d,%d) Cur(%d,%d)", __func__, (int)ev->dev, ev->buttons, ev->event_flags,
				ev->prev.canvas.x, ev->prev.canvas.y, ev->cur.canvas.x, ev->cur.canvas.y);

	_on_mouse_move(data, NULL, obj, (void *)&event);

}

static void _proxy_on_mouse_multi_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Multi_Down *ev = event_info;
	mouse_data_t *pData = data;

	MouseEvent event;

	if ( pData->multi_min_idx == INVALID_DEVICE_ID)
	{
		MSG_MOUSE_HIGH("[%s] Min Device Index=%d", pData->alias, ev->device);
		pData->multi_min_idx = ev->device -1;		// Index start from 0. so -1 is needed
	}

	event.device = ev->device - pData->multi_min_idx;	// ev->device is 10(for second), 11(3rd), 12(4th) finger
	event.timestamp = ev->timestamp;
	event.x = ev->canvas.x;
	event.y = ev->canvas.y;
	event.button_flags = 0;
	event.event_flags = ev->event_flags;


	if(event.device >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("[%s] Invalid Device. Dev=%d Min=%d", pData->alias, event.device, pData->multi_min_idx);
		return;
	}

	MSG_MOUSE_MED("%s Dev=%d", __func__, ev->device);

	_on_mouse_down(data, NULL, obj, (void *)&event);
}

static void _proxy_on_mouse_multi_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Multi_Up *ev = event_info;
	mouse_data_t *pData = data;

	MouseEvent event;

	if ( pData->multi_min_idx == INVALID_DEVICE_ID)
	{
		// Sometimes Multi up came ealry
		MSG_MOUSE_HIGH("[%s] Min Device Index=%d", pData->alias, ev->device);
		pData->multi_min_idx = ev->device -1;		// Index start from 0. so -1 is needed
	}

	event.device = ev->device - pData->multi_min_idx;
	event.timestamp = ev->timestamp;
	event.x = ev->canvas.x;
	event.y = ev->canvas.y;
	event.button_flags = 0;
	event.event_flags = ev->event_flags;

	if(event.device >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("[%s] Invalid Device. Dev=%d Min=%d", pData->alias, event.device, pData->multi_min_idx);
		return;
	}

	MSG_MOUSE_MED("%s Dev=%d", __func__, ev->device);

	_on_mouse_up(data, NULL, obj, (void *)&event);

}

static void _proxy_on_mouse_multi_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info)
{
	Evas_Event_Multi_Move *ev = event_info;
	mouse_data_t *pData = data;

	MouseEvent event;

	if ( pData->multi_min_idx == INVALID_DEVICE_ID)
	{
		// Mouse Move come faster than Mouse down
		MSG_MOUSE_HIGH("[%s] Min Device Index=%d", pData->alias, ev->device);
		pData->multi_min_idx = ev->device -1;
	}

	event.device = ev->device - pData->multi_min_idx;
	event.timestamp = ev->timestamp;
	event.x = ev->cur.canvas.x;
	event.y = ev->cur.canvas.y;
	event.button_flags = 0;
	event.event_flags = ev->event_flags;

	if(event.device >= NUM_MAX_BUTTON)
	{
		MSG_MOUSE_FATAL("[%s] Invalid Device. Dev=%d Min=%d", pData->alias, event.device, pData->multi_min_idx);
		return;
	}

	MSG_MOUSE_MED("%s Dev=%d", __func__, ev->device);

	_on_mouse_move(data, NULL, obj, (void *)&event);
}

Ivug_Event_Handle
ivug_mouse_event_add(Evas_Object *obj, mouse_callback_t *pCallback, void *client_data, const char *alias)
{
	mouse_data_t *pData = NULL;
//TODO : Should check parameters

	pData = calloc(1, sizeof(mouse_data_t));

	pData->obj = obj;
	pData->pClientData = client_data;
	pData->m_state = MOUSE_DOWN_NONE;
	pData->multi_min_idx = INVALID_DEVICE_ID;
	strncpy(pData->alias, alias, sizeof(pData->alias));

	memcpy(&pData->cb, pCallback, sizeof(mouse_callback_t));

	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MOUSE_DOWN, _proxy_on_mouse_down, pData);
	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MOUSE_UP, _proxy_on_mouse_up,  pData);
	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MOUSE_MOVE, _proxy_on_mouse_move, pData);
	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MULTI_DOWN, _proxy_on_mouse_multi_down, pData);
	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MULTI_UP, _proxy_on_mouse_multi_up, pData);
	evas_object_event_callback_add(pData->obj, EVAS_CALLBACK_MULTI_MOVE, _proxy_on_mouse_multi_move, pData);

	MSG_MOUSE_HIGH("Mouse Event Handler Add : Alias(%s) 0x%08x for Object(0x%08x)", alias, pData, obj);

	return (Ivug_Event_Handle *)pData;
}


void ivug_mouse_event_del(Ivug_Event_Handle handle)
{
	mouse_data_t *pData = handle;

	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MOUSE_DOWN, _proxy_on_mouse_down);
	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MOUSE_UP, _proxy_on_mouse_up);
	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MOUSE_MOVE, _proxy_on_mouse_move);
	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MULTI_DOWN, _proxy_on_mouse_multi_down);
	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MULTI_UP, _proxy_on_mouse_multi_up);
	evas_object_event_callback_del(pData->obj, EVAS_CALLBACK_MULTI_MOVE, _proxy_on_mouse_multi_move);

	MSG_MOUSE_HIGH("Mouse Event Handler Remove : 0x%08x", pData);

	free(handle);
}