28a065897ff980ac21822fd042fbc84344782802
[apps/core/preloaded/ug-camera-efl.git] / src / cam_long_press.c
1 /*\r
2  * Copyright 2012  Samsung Electronics Co., Ltd\r
3  *\r
4  * Licensed under the Flora License, Version 1.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *        http://floralicense.org/license/\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 \r
18 \r
19 #include <stdio.h>\r
20 #include <stdlib.h>\r
21 #include <assert.h>\r
22 #include <Elementary.h>\r
23 #include "cam.h"\r
24 #include "cam_long_press.h"\r
25 \r
26 typedef struct cb_item {\r
27         LONG_PRESS_START_CALLBACK start_func;\r
28         LONG_PRESS_CANCEL_CALLBACK cancel_func;\r
29         void *data;\r
30 \r
31         Evas_Coord x;\r
32         Evas_Coord y;\r
33 \r
34         Ecore_Timer *hold_timer;\r
35         double time;\r
36 \r
37         bool runing;\r
38 }long_press_timer_t;\r
39 \r
40 static long_press_timer_t g_timer[LONG_PRESS_ID_MAX_NUMS];\r
41 \r
42 bool cam_get_long_press_running_state(int index)\r
43 {\r
44         if (index >=LONG_PRESS_ID_MAX_NUMS || index < 0) {\r
45                 return FALSE;\r
46         }\r
47         return g_timer[index].runing;\r
48 }\r
49 static Eina_Bool _timer_cb(void *data)\r
50 {\r
51         if(NULL == data) {\r
52                 return FALSE;\r
53         }\r
54 \r
55         long_press_timer_t *item = (long_press_timer_t*)data;\r
56 \r
57         item->hold_timer = NULL;\r
58         if (item->start_func) {\r
59                 item->start_func(item->data, item->x, item->y);\r
60                 item->runing = TRUE;\r
61         }\r
62         return ECORE_CALLBACK_CANCEL;\r
63 }\r
64 \r
65 int cam_long_press_register(int index, double time,\r
66                 LONG_PRESS_START_CALLBACK start_func,\r
67                 LONG_PRESS_CANCEL_CALLBACK end_func,\r
68                 void *data)\r
69 {\r
70         if(index >= LONG_PRESS_ID_MAX_NUMS)\r
71                 return EXIT_FAILURE;\r
72 \r
73         if(g_timer[index].hold_timer != NULL) {\r
74                 DEBUG_TRACE("Already register! \n");\r
75                 return EXIT_SUCCESS;\r
76         }\r
77 \r
78         g_timer[index].start_func = start_func;\r
79         g_timer[index].cancel_func = end_func;\r
80         g_timer[index].data = data;\r
81         g_timer[index].runing = FALSE;\r
82         g_timer[index].time = time;\r
83 \r
84         return EXIT_SUCCESS;\r
85 }\r
86 \r
87 int cam_long_press_unregister(int index)\r
88 {\r
89         if(index >= LONG_PRESS_ID_MAX_NUMS)\r
90                 return EXIT_FAILURE;\r
91 \r
92         if (g_timer[index].hold_timer) {\r
93                 ecore_timer_del(g_timer[index].hold_timer);\r
94                 g_timer[index].hold_timer = NULL;\r
95         }\r
96 \r
97         return EXIT_SUCCESS;\r
98 }\r
99 \r
100 void cam_long_press_trigger(int index, Evas_Coord x, Evas_Coord y)\r
101 {\r
102         if(index >= LONG_PRESS_ID_MAX_NUMS)\r
103                 return;\r
104 \r
105         if (g_timer[index].hold_timer) {\r
106                 return;\r
107         }\r
108 \r
109         g_timer[index].hold_timer = ecore_timer_add(g_timer[index].time, _timer_cb, &g_timer[index]);\r
110         if (!g_timer[index].hold_timer) {\r
111                 DEBUG_TRACE("Failed to trigger the hold timer\n");\r
112         }\r
113 \r
114         /*Update valid region*/\r
115         g_timer[index].x = x;\r
116         g_timer[index].y = y;\r
117         g_timer[index].runing = FALSE;\r
118 }\r
119 \r
120 void cam_long_press_validate(int index, Evas_Coord x, Evas_Coord y)\r
121 {\r
122         if(index >= LONG_PRESS_ID_MAX_NUMS)\r
123                 return;\r
124 \r
125         int dx;\r
126         int dy;\r
127 \r
128         if (!g_timer[index].hold_timer) {\r
129                 return;\r
130         }\r
131 \r
132         dx = (g_timer[index].x - x);\r
133         dx *= dx;\r
134 \r
135         dy = (g_timer[index].y - y);\r
136         dy *= dy;\r
137 \r
138         /*TODO:this condision is too strict*/\r
139         if ((dx + dy) > ((elm_config_finger_size_get() / 2) * (elm_config_finger_size_get() / 2))) {\r
140                 DEBUG_TRACE("validate failed, del timer\n");\r
141                 ecore_timer_del(g_timer[index].hold_timer);\r
142                 g_timer[index].hold_timer = NULL;\r
143         }\r
144 \r
145 }\r
146 \r
147 void cam_long_press_cancel(int index)\r
148 {\r
149         if(index >= LONG_PRESS_ID_MAX_NUMS)\r
150                 return;\r
151 \r
152         if (g_timer[index].hold_timer) {\r
153                 ecore_timer_del(g_timer[index].hold_timer);\r
154                 g_timer[index].hold_timer = NULL;\r
155         }\r
156         g_timer[index].runing = FALSE;\r
157         if (g_timer[index].cancel_func) {\r
158                 g_timer[index].cancel_func(g_timer[index].data, g_timer[index].x, g_timer[index].y);\r
159         }\r
160 }\r
161 \r
162 \r