5ab65af0ce0d2ae342c42ef224fd2127068644bc
[framework/uifw/libscl-ui.git] / scl / scleventhandler.cpp
1 /*
2  * Copyright 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  *
16  */
17
18 #include "sclresourcecache.h"
19 #include "scleventhandler.h"
20 #include "scldebug.h"
21 #include "sclkeydefines.h"
22 #include "scluiimpl.h"
23
24 using namespace scl;
25
26 CSCLEventHandler* CSCLEventHandler::m_instance = NULL; /* For singleton */
27
28 CSCLEventHandler::CSCLEventHandler()
29 {
30     SCL_DEBUG();
31
32     m_default_event_callback = NULL;
33     m_cur_input_mode_event_callback = NULL;
34 }
35
36 CSCLEventHandler::~CSCLEventHandler()
37 {
38     SCL_DEBUG();
39 }
40
41 CSCLEventHandler*
42 CSCLEventHandler::get_instance()
43 {
44     if (!m_instance) {
45         m_instance = new CSCLEventHandler();
46     }
47     return (CSCLEventHandler*)m_instance;
48 }
49
50 static void handle_shift_button_click_event(SclUIEventDesc ui_event_desc)
51 {
52     CSCLUIImpl *uiimpl = CSCLUIImpl::get_instance();
53     CSCLContext *context = CSCLContext::get_instance();
54
55     if (uiimpl && context) {
56         if (ui_event_desc.key_type == KEY_TYPE_CONTROL && ui_event_desc.key_event == MVK_Shift_L) {
57             switch (uiimpl->get_shift_state()) {
58                 case SCL_SHIFT_STATE_OFF: {
59                     uiimpl->set_shift_state(SCL_SHIFT_STATE_ON);
60                 }
61                 break;
62                 case SCL_SHIFT_STATE_ON: {
63                     /* The default next state should be LOCK state */
64                     SCLShiftState next_state = SCL_SHIFT_STATE_LOCK;
65                     if (context->get_shift_multi_touch_enabled()) {
66                         CSCLContext *context = CSCLContext::get_instance();
67                         if (context) {
68                             if (context->get_shift_multi_touch_state() == SCL_SHIFT_MULTITOUCH_ON_PRESSED) {
69                                 /* If the shift multi touch state is ON_PRESSED, don't leave ON state now */
70                                 next_state = SCL_SHIFT_STATE_ON;
71                             } else if (context->get_shift_multi_touch_state() == SCL_SHIFT_MULTITOUCH_ON_KEY_ENTERED) {
72                                 /* If some keys were already entered while shift key was in pressed state, move to OFF */
73                                 next_state = SCL_SHIFT_STATE_OFF;
74                             }
75                         }
76                     }
77                     uiimpl->set_shift_state(next_state);
78                 }
79                 break;
80                 case SCL_SHIFT_STATE_LOCK: {
81                     uiimpl->set_shift_state(SCL_SHIFT_STATE_OFF);
82                 }
83                 break;
84             }
85         }
86     }
87 }
88
89 static void handle_shift_state_on_button_click_event(SclUIEventDesc ui_event_desc)
90 {
91     CSCLUIImpl *uiimpl = CSCLUIImpl::get_instance();
92     CSCLContext *context = CSCLContext::get_instance();
93
94     if (uiimpl && context) {
95         sclboolean turn_shift_off = TRUE;
96         if (uiimpl->get_caps_mode()) {
97             turn_shift_off = FALSE;
98         } else {
99             if (ui_event_desc.key_type == KEY_TYPE_CONTROL) {
100                 if (ui_event_desc.key_event == MVK_Shift_L || ui_event_desc.key_event == MVK_Caps_Lock) {
101                     turn_shift_off = FALSE;
102                 }
103                 if (ui_event_desc.key_type == KEY_TYPE_MODECHANGE) {
104                     turn_shift_off = FALSE;
105                 }
106             }
107         }
108         /* If we are in ON_PRESSED or ON_KEY_ENTERED mode of shift multi touch state, do not turn it off now */
109         if (context->get_shift_multi_touch_enabled() && turn_shift_off) {
110             CSCLContext *context = CSCLContext::get_instance();
111             if (context) {
112                 if (context->get_shift_multi_touch_state() == SCL_SHIFT_MULTITOUCH_ON_PRESSED) {
113                     context->set_shift_multi_touch_state(SCL_SHIFT_MULTITOUCH_ON_KEY_ENTERED);
114                     turn_shift_off = FALSE;
115                 } else if (context->get_shift_multi_touch_state() == SCL_SHIFT_MULTITOUCH_ON_KEY_ENTERED) {
116                     turn_shift_off = FALSE;
117                 }
118             }
119         }
120         if (turn_shift_off) {
121             if (uiimpl->get_shift_state() == SCL_SHIFT_STATE_ON) {
122                 uiimpl->set_shift_state(SCL_SHIFT_STATE_OFF);
123             }
124         }
125     }
126 }
127
128 static void handle_mode_change_button_click_event(SclUIEventDesc ui_event_desc)
129 {
130     CSCLUIImpl *uiimpl = CSCLUIImpl::get_instance();
131
132     if (uiimpl) {
133         if (ui_event_desc.key_type == KEY_TYPE_MODECHANGE) {
134             uiimpl->set_input_mode(ui_event_desc.key_value);
135         }
136     }
137 }
138
139 SCLEventReturnType
140 CSCLEventHandler::on_event_key_clicked(SclUIEventDesc ui_event_desc)
141 {
142     SCLEventReturnType ret = SCL_EVENT_PASS_ON;
143
144     pre_process_ui_event(ui_event_desc);
145
146     if (m_cur_input_mode_event_callback) {
147         ret = m_cur_input_mode_event_callback->on_event_key_clicked(ui_event_desc);
148     }
149     if (ret == SCL_EVENT_PASS_ON) {
150         if (m_default_event_callback) {
151             ret = m_default_event_callback->on_event_key_clicked(ui_event_desc);
152         }
153     }
154
155     if (ret == SCL_EVENT_PASS_ON) {
156         /* Here we can put the fallback processing of this UIEvent */
157
158         /* General requirement - 1 */
159         /* When the SHIFT button was clicked, we change the shift state to OFF -> ON -> LOCK -> OFF ... */
160         handle_shift_button_click_event(ui_event_desc);
161
162         /* General requirement - 2 */
163         /* If a key was clicked but it is neither a SHIFT nor a CAPSLOCK, we just turn the shift off, if it is on */
164         handle_shift_state_on_button_click_event(ui_event_desc);
165
166         /* General requirement - 3 */
167         /* If the key type is KEY_TYPE_MODECHANGE, change the current input mode to given key_value */
168         handle_mode_change_button_click_event(ui_event_desc);
169     }
170
171     return ret;
172 }
173
174 SCLEventReturnType
175 CSCLEventHandler::on_event_drag_state_changed(SclUIEventDesc ui_event_desc)
176 {
177     SCLEventReturnType ret = SCL_EVENT_PASS_ON;
178
179     pre_process_ui_event(ui_event_desc);
180
181     if (m_cur_input_mode_event_callback) {
182         ret = m_cur_input_mode_event_callback->on_event_drag_state_changed(ui_event_desc);
183     }
184     if (ret == SCL_EVENT_PASS_ON) {
185         if (m_default_event_callback) {
186             ret = m_default_event_callback->on_event_drag_state_changed(ui_event_desc);
187         }
188     }
189
190     if (ret == SCL_EVENT_PASS_ON) {
191         /* Here we can put the fallback processing of this UIEvent */
192         CSCLUIImpl *uiimpl = CSCLUIImpl::get_instance();
193         CSCLContext *context = CSCLContext::get_instance();
194
195         /* General requirement - 1 */
196         /* When the SHIFT button was 'pressed' and shift button multitouch action is enabled,
197            we change the current shift multitouch state to 'ON_PRESSED' */
198         if (uiimpl && context) {
199             if (context->get_shift_multi_touch_enabled()) {
200                 if (ui_event_desc.event_type == EVENT_TYPE_PRESS) {
201                     if (context) {
202                         if (ui_event_desc.key_event == MVK_Shift_L) {
203                             if (context->get_shift_multi_touch_state() == SCL_SHIFT_MULTITOUCH_OFF) {
204                                 uiimpl->set_shift_state(SCL_SHIFT_STATE_ON);
205                                 context->set_shift_multi_touch_state(SCL_SHIFT_MULTITOUCH_ON_PRESSED);
206                             }
207                         }
208                     }
209                 }
210             }
211         }
212     }
213
214     return ret;
215 }
216
217 SCLEventReturnType
218 CSCLEventHandler::on_event_notification(SCLUINotiType noti_type, sclint etc_info)
219 {
220     SCLEventReturnType ret = SCL_EVENT_PASS_ON;
221
222     if (m_cur_input_mode_event_callback) {
223         ret = m_cur_input_mode_event_callback->on_event_notification(noti_type, etc_info);
224     }
225     if (ret == SCL_EVENT_PASS_ON) {
226         if (m_default_event_callback) {
227             ret = m_default_event_callback->on_event_notification(noti_type, etc_info);
228         }
229     }
230
231     if (ret == SCL_EVENT_PASS_ON) {
232         /* Here we can put the fallback processing of this UIEvent */
233     }
234
235     return ret;
236 }
237
238 sclboolean
239 CSCLEventHandler::set_input_mode(const sclchar *input_mode)
240 {
241     SCL_DEBUG();
242
243     sclboolean ret = FALSE;
244     m_cur_input_mode_event_callback = NULL;
245
246     if (input_mode) {
247         std::string id = input_mode;
248         std::map<std::string, ISCLUIEventCallback*>::iterator iter = m_input_mode_event_callbacks.find(input_mode);
249         if (iter != m_input_mode_event_callbacks.end()) {
250             m_cur_input_mode_event_callback = (iter->second);
251         }
252     }
253
254     if (m_cur_input_mode_event_callback) {
255         ret = TRUE;
256     }
257
258     return ret;
259 }
260
261 void
262 CSCLEventHandler::set_event_callback(ISCLUIEventCallback *callback, const sclchar *input_mode)
263 {
264     SCL_DEBUG();
265
266     scl_assert_return(callback);
267
268     if (input_mode) {
269         std::string id = input_mode;
270         m_input_mode_event_callbacks[id] = callback;
271     } else {
272         m_default_event_callback = callback;
273     }
274 }
275
276 void
277 CSCLEventHandler::pre_process_ui_event(SclUIEventDesc &ui_event_desc)
278 {
279     SCL_DEBUG();
280
281     typedef struct {
282         const sclchar *key_value;
283         sclulong key_event;
284     } KEY_VALUE_EVENT_CONVERT_TABLE;
285
286     KEY_VALUE_EVENT_CONVERT_TABLE control_keys[] = {
287         {"Space",       MVK_space       },
288         {"BackSpace",   MVK_BackSpace   },
289         {"Shift",       MVK_Shift_L     },
290         {"CapsLock",    MVK_Caps_Lock   },
291         {"Enter",       MVK_Return      },
292         {"Left",        MVK_Left        },
293         {"Right",       MVK_Right       },
294         {"Up",          MVK_Up          },
295         {"Down",        MVK_Down        },
296     };
297
298     /* Translate key_values only when key_event is 0 and key_value is not NULL */
299     if (ui_event_desc.key_value && ui_event_desc.key_event == 0) {
300         if (ui_event_desc.key_type == KEY_TYPE_CHAR) {
301             /* If the key_value is a string with length 1, and the first character has value between
302                SCL_ISCHAR range, provide the corresponding ASCII code in key_event field */
303             if (ui_event_desc.key_value[0] != '\0' && ui_event_desc.key_value[1] == '\0') {
304                 if (SCL_ISCHAR(ui_event_desc.key_value[0])) {
305                     ui_event_desc.key_event = ui_event_desc.key_value[0];
306                 }
307             }
308         } else if (ui_event_desc.key_type == KEY_TYPE_CONTROL) {
309             const scluint control_keys_size = sizeof(control_keys) / sizeof(KEY_VALUE_EVENT_CONVERT_TABLE);
310
311             for (scluint loop = 0;loop < control_keys_size;loop++) {
312                 if (strncmp(control_keys[loop].key_value, ui_event_desc.key_value, strlen(control_keys[loop].key_value)) == 0) {
313                     ui_event_desc.key_event = control_keys[loop].key_event;
314                 }
315             }
316         } else if (ui_event_desc.key_type == KEY_TYPE_STRING) {
317             CSCLResourceCache *cache = CSCLResourceCache::get_instance();
318             if (cache) {
319                 ui_event_desc.key_value = cache->find_substituted_string(ui_event_desc.key_value);
320             }
321         }
322     }
323 }