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;
31 view::gesture_detector::gesture_detector(maps_view_h v)
35 , _long_press_timer(NULL)
38 if (maps_get_screen_dpi(&dpi) == MAPS_ERROR_NONE)
39 __CLICK_AREA = dpi / 5; /* expected about 0.5cm */
42 view::gesture_detector::~gesture_detector()
45 stop_long_press_timer();
48 void view::gesture_detector::halt_gesture()
52 void view::gesture_detector::reset()
54 MAPS_LOGI("%c[%d;%d;%dm"
57 0x1B, 1, 41, FG_GREEN,
61 /* Store the previous gesture data for possible future analysis */
62 _info_history = _info;
64 /* Save Map View state for complex multi-click gestures */
65 const map_state saved_state = _info._start_view_state;
67 /* Reset current gesture info */
70 /* We would use this state in the complex gestures */
71 _info._start_view_state = saved_state;
73 /* There is definitelly not long enough for "long press" gesture */
74 stop_long_press_timer();
76 /*MAPS_LOGI("Historic timestamps: tap: %d, move: %d, up: %d",
77 _info_history._finger_down[0]._timestamp,
78 _info_history._finger_move[0]._timestamp,
79 _info_history._finger_up[0]._timestamp);*/
82 void view::gesture_detector::tap(int finger_no, const touch_point &tp)
86 void view::gesture_detector::up(int finger_no, const touch_point &tp)
90 void view::gesture_detector::move(int finger_no, const touch_point &tp)
94 void view::gesture_detector::on_tap_timer()
98 void view::gesture_detector::start_tap_timer()
100 log("START TAP TIMER", FG_MAGENTA);
103 _tap_timer = ecore_timer_add(__TAP_DURATION,
108 void view::gesture_detector::stop_tap_timer()
110 log("STOP TAP TIMER", FG_MAGENTA);
112 ecore_timer_del(_tap_timer);
116 Eina_Bool view::gesture_detector::__on_tap_timer(void *data)
118 gesture_detector *d = (gesture_detector *)data;
120 return ECORE_CALLBACK_CANCEL;
122 d->log("------- TAP TIMER EVENT -------", FG_MAGENTA);
123 d->_tap_timer = NULL;
126 return ECORE_CALLBACK_CANCEL;
129 void view::gesture_detector::on_long_press_timer()
133 void view::gesture_detector::start_long_press_timer()
135 log("START LONG PRESS TIMER", FG_MAGENTA);
136 if(_long_press_timer)
137 stop_long_press_timer();
138 _long_press_timer = ecore_timer_add(__LONG_PRESS_DURATION,
139 __on_long_press_timer,
143 void view::gesture_detector::stop_long_press_timer()
145 log("STOP LONG PRESS TIMER", FG_MAGENTA);
146 if(_long_press_timer)
147 ecore_timer_del(_long_press_timer);
148 _long_press_timer = NULL;
151 Eina_Bool view::gesture_detector::__on_long_press_timer(void *data)
153 gesture_detector *d = (gesture_detector *)data;
155 return ECORE_CALLBACK_CANCEL;
157 d->log("------- LONG PRESS TIMER EVENT -------", FG_MAGENTA);
158 d->_long_press_timer = NULL;
159 d->on_long_press_timer();
161 return ECORE_CALLBACK_CANCEL;
164 bool view::gesture_detector::is_gesture_available(maps_view_gesture_e gesture)
166 return _maps_view_is_gesture_available(_view, gesture);
169 bool view::gesture_detector::finger_dragged_enough(int finger_no)
171 const touch_point p1 = _info._finger_down[finger_no];
172 const touch_point p2 = _info._finger_move[finger_no];
173 return (get_trajectory_effective_length(p1, p2) >= __CLICK_AREA);
176 bool view::gesture_detector::finger_pressed_enough(int finger_no,
177 unsigned int duration_min,
178 unsigned int duration_max)
180 unsigned int press_time = _info._finger_down[finger_no]._timestamp;
181 unsigned int up_time = _info._finger_up[finger_no]._timestamp;
183 unsigned int press_duration_tmp = up_time - press_time;
184 MAPS_LOGI("finger_pressed_enough: finger: %d, press: %d, up: %d [%d]",
185 finger_no, press_time, up_time, press_duration_tmp);
187 if(up_time < press_time)
188 return false; /* Not yet unpressed */
189 unsigned int press_duration = up_time - press_time;
190 if ((press_duration >= duration_min) && (press_duration <= duration_max))
195 int view::gesture_detector::get_trajectory_effective_length(
196 const touch_point &p1,
197 const touch_point &p2)
199 int dx = p2._x - p1._x;
200 int dy = p2._y - p1._y;
201 int etl = sqrt(dx * dx + dy * dy);
205 void view::gesture_detector::log(const char *str, log_colors color)
207 MAPS_LOGI("%c[%d;%d;%dm"
216 void view::gesture_detector::log_map_center(int color)
218 maps_coordinates_h central_coords = NULL;
219 maps_view_get_center(_view, ¢ral_coords);
220 double lat = .0, lon = .0;
221 maps_coordinates_get_latitude_longitude(central_coords, &lat, &lon);
222 maps_coordinates_destroy(central_coords);
223 MAPS_LOGI("%c[%d;%d;%dm"
224 "central coordinates: (%f, %f)"