1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "gesture_detector.h"
23 extern bool _maps_view_is_gesture_available(maps_view_h view,
24 maps_view_gesture_e gesture);
27 /*----------------------------------------------------------------------------*/
29 int view::gesture_detector::__CLICK_AREA = 50;
32 view::gesture_detector::gesture_detector(maps_view_h v)
36 , _long_press_timer(NULL)
39 if (maps_get_screen_dpi(&dpi) == MAPS_ERROR_NONE)
40 __CLICK_AREA = dpi / 5; /* expected about 0.5cm */
43 view::gesture_detector::~gesture_detector()
46 stop_long_press_timer();
49 void view::gesture_detector::halt_gesture()
53 void view::gesture_detector::reset()
55 MAPS_LOGI("%c[%d;%d;%dm"
58 0x1B, 1, 41, FG_GREEN,
62 /* Store the previous gesture data for possible future analysis */
63 _info_history = _info;
65 /* Save Map View state for complex multi-click gestures */
66 const map_state saved_state = _info._start_view_state;
68 /* Reset current gesture info */
71 /* We would use this state in the complex gestures */
72 _info._start_view_state = saved_state;
74 /* There is definitelly not long enough for "long press" gesture */
75 stop_long_press_timer();
77 /*MAPS_LOGI("Historic timestamps: tap: %d, move: %d, up: %d",
78 _info_history._finger_down[0]._timestamp,
79 _info_history._finger_move[0]._timestamp,
80 _info_history._finger_up[0]._timestamp);*/
83 void view::gesture_detector::tap(int finger_no, const touch_point &tp)
87 void view::gesture_detector::up(int finger_no, const touch_point &tp)
91 void view::gesture_detector::move(int finger_no, const touch_point &tp)
95 void view::gesture_detector::on_tap_timer()
99 void view::gesture_detector::start_tap_timer()
101 log("START TAP TIMER", FG_MAGENTA);
104 _tap_timer = ecore_timer_add(__TAP_DURATION,
109 void view::gesture_detector::stop_tap_timer()
111 log("STOP TAP TIMER", FG_MAGENTA);
113 ecore_timer_del(_tap_timer);
117 Eina_Bool view::gesture_detector::__on_tap_timer(void *data)
119 gesture_detector *d = (gesture_detector *)data;
121 return ECORE_CALLBACK_CANCEL;
123 d->log("------- TAP TIMER EVENT -------", FG_MAGENTA);
124 d->_tap_timer = NULL;
127 return ECORE_CALLBACK_CANCEL;
130 void view::gesture_detector::on_long_press_timer()
134 void view::gesture_detector::start_long_press_timer()
136 log("START LONG PRESS TIMER", FG_MAGENTA);
137 if(_long_press_timer)
138 stop_long_press_timer();
139 _long_press_timer = ecore_timer_add(__LONG_PRESS_DURATION,
140 __on_long_press_timer,
144 void view::gesture_detector::stop_long_press_timer()
146 log("STOP LONG PRESS TIMER", FG_MAGENTA);
147 if(_long_press_timer)
148 ecore_timer_del(_long_press_timer);
149 _long_press_timer = NULL;
152 Eina_Bool view::gesture_detector::__on_long_press_timer(void *data)
154 gesture_detector *d = (gesture_detector *)data;
156 return ECORE_CALLBACK_CANCEL;
158 d->log("------- LONG PRESS TIMER EVENT -------", FG_MAGENTA);
159 d->_long_press_timer = NULL;
160 d->on_long_press_timer();
162 return ECORE_CALLBACK_CANCEL;
165 bool view::gesture_detector::is_gesture_available(maps_view_gesture_e gesture)
167 return _maps_view_is_gesture_available(_view, gesture);
170 bool view::gesture_detector::finger_dragged_enough(int finger_no)
172 const touch_point p1 = _info._finger_down[finger_no];
173 const touch_point p2 = _info._finger_move[finger_no];
174 return (get_trajectory_effective_length(p1, p2) >= __CLICK_AREA);
177 bool view::gesture_detector::finger_pressed_enough(int finger_no,
178 unsigned int duration_min,
179 unsigned int duration_max)
181 unsigned int press_time = _info._finger_down[finger_no]._timestamp;
182 unsigned int up_time = _info._finger_up[finger_no]._timestamp;
184 unsigned int press_duration_tmp = up_time - press_time;
185 MAPS_LOGI("finger_pressed_enough: finger: %d, press: %d, up: %d [%d]",
186 finger_no, press_time, up_time, press_duration_tmp);
188 if(up_time < press_time)
189 return false; /* Not yet unpressed */
190 unsigned int press_duration = up_time - press_time;
191 if ((press_duration >= duration_min) && (press_duration <= duration_max))
196 int view::gesture_detector::get_trajectory_effective_length(
197 const touch_point &p1,
198 const touch_point &p2)
200 int dx = p2._x - p1._x;
201 int dy = p2._y - p1._y;
202 int etl = sqrt(dx * dx + dy * dy);
206 void view::gesture_detector::log(const char *str, log_colors color)
208 MAPS_LOGI("%c[%d;%d;%dm"
217 void view::gesture_detector::log_map_center(int color)
219 maps_coordinates_h central_coords = NULL;
220 maps_view_get_center(_view, ¢ral_coords);
221 double lat = .0, lon = .0;
222 maps_coordinates_get_latitude_longitude(central_coords, &lat, &lon);
223 maps_coordinates_destroy(central_coords);
224 MAPS_LOGI("%c[%d;%d;%dm"
225 "central coordinates: (%f, %f)"