Merge "Added devel-API for video player" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / event-handler-ecore-wl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 // CLASS HEADER
19 #include <events/event-handler.h>
20
21 // EXTERNAL INCLUDES
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
25 #include <Ecore.h>
26 #include <Ecore_Input.h>
27 #include <ecore-wl-render-surface.h>
28 #include <cstring>
29
30 #include <sys/time.h>
31
32 #ifndef DALI_PROFILE_UBUNTU
33 #include <vconf.h>
34 #include <vconf-keys.h>
35 #endif // DALI_PROFILE_UBUNTU
36
37 #ifdef DALI_ELDBUS_AVAILABLE
38 #include <Eldbus.h>
39 #endif // DALI_ELDBUS_AVAILABLE
40
41 #include <dali/public-api/common/vector-wrapper.h>
42 #include <dali/public-api/events/touch-point.h>
43 #include <dali/public-api/events/key-event.h>
44 #include <dali/public-api/events/wheel-event.h>
45 #include <dali/integration-api/debug.h>
46 #include <dali/integration-api/events/key-event-integ.h>
47 #include <dali/integration-api/events/touch-event-integ.h>
48 #include <dali/integration-api/events/hover-event-integ.h>
49 #include <dali/integration-api/events/wheel-event-integ.h>
50 #include <dali/devel-api/events/key-event-devel.h>
51
52 // INTERNAL INCLUDES
53 #include <events/gesture-manager.h>
54 #include <window-render-surface.h>
55 #include <clipboard-impl.h>
56 #include <key-impl.h>
57 #include <physical-keyboard-impl.h>
58 #include <style-monitor-impl.h>
59 #include <base/core-event-interface.h>
60 #include <virtual-keyboard.h>
61
62 namespace Dali
63 {
64
65 namespace Internal
66 {
67
68 namespace Adaptor
69 {
70
71 #if defined(DEBUG_ENABLED)
72 namespace
73 {
74 Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
75 Integration::Log::Filter* gDragAndDropLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DND");
76 Integration::Log::Filter* gImfLogging  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_IMF");
77 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
78 } // unnamed namespace
79 #endif
80
81
82 namespace
83 {
84
85 // DBUS accessibility
86 const char* BUS = "org.enlightenment.wm-screen-reader";
87 const char* INTERFACE = "org.tizen.GestureNavigation";
88 const char* PATH = "/org/tizen/GestureNavigation";
89
90 const unsigned int PRIMARY_TOUCH_BUTTON_ID( 1 );
91
92 const unsigned int BYTES_PER_CHARACTER_FOR_ATTRIBUTES = 3;
93
94 /**
95  * Ecore_Event_Modifier enums in Ecore_Input.h do not match Ecore_IMF_Keyboard_Modifiers in Ecore_IMF.h.
96  * This function converts from Ecore_Event_Modifier to Ecore_IMF_Keyboard_Modifiers enums.
97  * @param[in] ecoreModifier the Ecore_Event_Modifier input.
98  * @return the Ecore_IMF_Keyboard_Modifiers output.
99  */
100 Ecore_IMF_Keyboard_Modifiers EcoreInputModifierToEcoreIMFModifier(unsigned int ecoreModifier)
101 {
102    int modifier( ECORE_IMF_KEYBOARD_MODIFIER_NONE );  // If no other matches returns NONE.
103
104
105    if ( ecoreModifier & ECORE_EVENT_MODIFIER_SHIFT )  // enums from ecore_input/Ecore_Input.h
106    {
107      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_SHIFT;  // enums from ecore_imf/ecore_imf.h
108    }
109
110    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALT )
111    {
112      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALT;
113    }
114
115    if ( ecoreModifier & ECORE_EVENT_MODIFIER_CTRL )
116    {
117      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_CTRL;
118    }
119
120    if ( ecoreModifier & ECORE_EVENT_MODIFIER_WIN )
121    {
122      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_WIN;
123    }
124
125    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALTGR )
126    {
127      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALTGR;
128    }
129
130    return static_cast<Ecore_IMF_Keyboard_Modifiers>( modifier );
131 }
132
133
134 // Copied from x server
135 static unsigned int GetCurrentMilliSeconds(void)
136 {
137   struct timeval tv;
138
139   struct timespec tp;
140   static clockid_t clockid;
141
142   if (!clockid)
143   {
144 #ifdef CLOCK_MONOTONIC_COARSE
145     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
146       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
147     {
148       clockid = CLOCK_MONOTONIC_COARSE;
149     }
150     else
151 #endif
152     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
153     {
154       clockid = CLOCK_MONOTONIC;
155     }
156     else
157     {
158       clockid = ~0L;
159     }
160   }
161   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
162   {
163     return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
164   }
165
166   gettimeofday(&tv, NULL);
167   return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
168 }
169
170 #ifndef DALI_PROFILE_UBUNTU
171 const char * DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE = "db/setting/accessibility/font_name";  // It will be update at vconf-key.h and replaced.
172 #endif // DALI_PROFILE_UBUNTU
173
174 /**
175  * Get the device name from the provided ecore key event
176  */
177 void GetDeviceName(  Ecore_Event_Key* keyEvent, std::string& result )
178 {
179   const char* ecoreDeviceName = ecore_device_name_get( keyEvent->dev );
180
181   if ( ecoreDeviceName )
182   {
183     result = ecoreDeviceName;
184   }
185 }
186
187 /**
188  * Get the device class from the provided ecore key event
189  */
190 void GetDeviceClass(  Ecore_Event_Key* keyEvent, DevelKeyEvent::DeviceClass::Type& deviceClass )
191 {
192   Ecore_Device_Class ecoreDeviceClass = ecore_device_class_get( keyEvent->dev );
193
194   switch( ecoreDeviceClass )
195   {
196     case ECORE_DEVICE_CLASS_SEAT:
197     {
198       deviceClass = DevelKeyEvent::DeviceClass::USER;
199       break;
200     }
201     case ECORE_DEVICE_CLASS_KEYBOARD:
202     {
203       deviceClass = DevelKeyEvent::DeviceClass::KEYBOARD;
204       break;
205     }
206     case ECORE_DEVICE_CLASS_MOUSE:
207     {
208       deviceClass = DevelKeyEvent::DeviceClass::MOUSE;
209       break;
210     }
211     case ECORE_DEVICE_CLASS_TOUCH:
212     {
213       deviceClass = DevelKeyEvent::DeviceClass::TOUCH;
214       break;
215     }
216     case ECORE_DEVICE_CLASS_PEN:
217     {
218       deviceClass = DevelKeyEvent::DeviceClass::PEN;
219       break;
220     }
221     case ECORE_DEVICE_CLASS_POINTER:
222     {
223       deviceClass = DevelKeyEvent::DeviceClass::POINTER;
224       break;
225     }
226     case ECORE_DEVICE_CLASS_GAMEPAD:
227     {
228       deviceClass = DevelKeyEvent::DeviceClass::GAMEPAD;
229       break;
230     }
231     default:
232     {
233       deviceClass = DevelKeyEvent::DeviceClass::NONE;
234       break;
235     }
236   }
237 }
238
239 } // unnamed namespace
240
241 // Impl to hide EFL implementation.
242 struct EventHandler::Impl
243 {
244   // Construction & Destruction
245
246   /**
247    * Constructor
248    */
249   Impl( EventHandler* handler, Ecore_Wl_Window* window )
250   : mHandler( handler ),
251     mEcoreEventHandler(),
252     mWindow( window ),
253     mRotationAngle( 0 ),
254     mWindowWidth( 0 ),
255     mWindowHeight( 0 )
256 #ifdef DALI_ELDBUS_AVAILABLE
257   , mSystemConnection( NULL )
258 #endif // DALI_ELDBUS_AVAILABLE
259   {
260     // Only register for touch and key events if we have a window
261     if ( window != 0 )
262     {
263       // Register Touch events
264       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_DOWN,   EcoreEventMouseButtonDown,   handler ) );
265       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_UP,     EcoreEventMouseButtonUp,     handler ) );
266       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_MOVE,          EcoreEventMouseButtonMove,   handler ) );
267       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_OUT,           EcoreEventMouseButtonUp,     handler ) ); // process mouse out event like up event
268       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_CANCEL, EcoreEventMouseButtonCancel, handler ) );
269
270       // Register Mouse wheel events
271       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_WHEEL,        EcoreEventMouseWheel,      handler ) );
272
273       // Register Focus events
274       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_WL_EVENT_FOCUS_IN,  EcoreEventWindowFocusIn,   handler ) );
275       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_WL_EVENT_FOCUS_OUT, EcoreEventWindowFocusOut,  handler ) );
276
277       // Register Key events
278       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_DOWN,           EcoreEventKeyDown,         handler ) );
279       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_UP,             EcoreEventKeyUp,           handler ) );
280
281       // Register Selection event - clipboard selection
282       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_WL_EVENT_DATA_SOURCE_SEND, EcoreEventDataSend, handler ) );
283       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_WL_EVENT_SELECTION_DATA_READY, EcoreEventDataReceive, handler ) );
284
285       // Register Rotate event
286       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ROTATE, EcoreEventRotate, handler) );
287
288       // Register Detent event
289       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_DETENT_ROTATE, EcoreEventDetent, handler) );
290
291 #ifndef DALI_PROFILE_UBUNTU
292       // Register Vconf notify - font name and size
293       vconf_notify_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontNameChanged, handler );
294       vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged, handler );
295 #endif // DALI_PROFILE_UBUNTU
296
297 #ifdef DALI_ELDBUS_AVAILABLE
298       // Initialize ElDBus.
299       DALI_LOG_INFO( gImfLogging, Debug::General, "Starting DBus Initialization\n" );
300
301       // Pass in handler.
302       EcoreElDBusInitialisation( handler );
303
304       DALI_LOG_INFO( gImfLogging, Debug::General, "Finished DBus Initialization\n" );
305 #endif // DALI_ELDBUS_AVAILABLE
306     }
307   }
308
309   /**
310    * Destructor
311    */
312   ~Impl()
313   {
314 #ifndef DALI_PROFILE_UBUNTU
315     vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged );
316     vconf_ignore_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontNameChanged );
317 #endif // DALI_PROFILE_UBUNTU
318
319     for( std::vector<Ecore_Event_Handler*>::iterator iter = mEcoreEventHandler.begin(), endIter = mEcoreEventHandler.end(); iter != endIter; ++iter )
320     {
321       ecore_event_handler_del( *iter );
322     }
323
324 #ifdef DALI_ELDBUS_AVAILABLE
325     // Close down ElDBus connections.
326     if( mSystemConnection )
327     {
328       eldbus_connection_unref( mSystemConnection );
329     }
330 #endif // DALI_ELDBUS_AVAILABLE
331   }
332
333   // Static methods
334
335   /////////////////////////////////////////////////////////////////////////////////////////////////
336   // Touch Callbacks
337   /////////////////////////////////////////////////////////////////////////////////////////////////
338
339   /**
340    * Called when a touch down is received.
341    */
342   static Eina_Bool EcoreEventMouseButtonDown( void* data, int type, void* event )
343   {
344     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
345     EventHandler* handler( (EventHandler*)data );
346
347     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
348     {
349       PointState::Type state ( PointState::DOWN );
350
351       // Check if the buttons field is set and ensure it's the primary touch button.
352       // If this event was triggered by buttons other than the primary button (used for touch), then
353       // just send an interrupted event to Core.
354       if ( touchEvent->buttons && (touchEvent->buttons != PRIMARY_TOUCH_BUTTON_ID ) )
355       {
356         state = PointState::INTERRUPTED;
357       }
358
359       Integration::Point point;
360       point.SetDeviceId( touchEvent->multi.device );
361       point.SetState( state );
362       point.SetScreenPosition( Vector2( touchEvent->x, touchEvent->y ) );
363       point.SetRadius( touchEvent->multi.radius, Vector2( touchEvent->multi.radius_x, touchEvent->multi.radius_y ) );
364       point.SetPressure( touchEvent->multi.pressure );
365       point.SetAngle( Degree( touchEvent->multi.angle ) );
366       handler->SendEvent( point, touchEvent->timestamp );
367     }
368
369     return ECORE_CALLBACK_PASS_ON;
370   }
371
372   /**
373    * Called when a touch up is received.
374    */
375   static Eina_Bool EcoreEventMouseButtonUp( void* data, int type, void* event )
376   {
377     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
378     EventHandler* handler( (EventHandler*)data );
379
380     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
381     {
382       Integration::Point point;
383       point.SetDeviceId( touchEvent->multi.device );
384       point.SetState( PointState::UP );
385       point.SetScreenPosition( Vector2( touchEvent->x, touchEvent->y ) );
386       point.SetRadius( touchEvent->multi.radius, Vector2( touchEvent->multi.radius_x, touchEvent->multi.radius_y ) );
387       point.SetPressure( touchEvent->multi.pressure );
388       point.SetAngle( Degree( touchEvent->multi.angle ) );
389       handler->SendEvent( point, touchEvent->timestamp );
390     }
391
392     return ECORE_CALLBACK_PASS_ON;
393   }
394
395   /**
396    * Called when a touch motion is received.
397    */
398   static Eina_Bool EcoreEventMouseButtonMove( void* data, int type, void* event )
399   {
400     Ecore_Event_Mouse_Move *touchEvent( (Ecore_Event_Mouse_Move*)event );
401     EventHandler* handler( (EventHandler*)data );
402
403     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
404     {
405       Integration::Point point;
406       point.SetDeviceId( touchEvent->multi.device );
407       point.SetState( PointState::MOTION );
408       point.SetScreenPosition( Vector2( touchEvent->x, touchEvent->y ) );
409       point.SetRadius( touchEvent->multi.radius, Vector2( touchEvent->multi.radius_x, touchEvent->multi.radius_y ) );
410       point.SetPressure( touchEvent->multi.pressure );
411       point.SetAngle( Degree( touchEvent->multi.angle ) );
412       handler->SendEvent( point, touchEvent->timestamp );
413     }
414
415     return ECORE_CALLBACK_PASS_ON;
416   }
417
418   /**
419    * Called when a touch is canceled.
420    */
421   static Eina_Bool EcoreEventMouseButtonCancel( void* data, int type, void* event )
422   {
423     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
424     EventHandler* handler( (EventHandler*)data );
425
426     if( touchEvent->window == (unsigned int)ecore_wl_window_id_get( handler->mImpl->mWindow ) )
427     {
428       Integration::Point point;
429       point.SetDeviceId( touchEvent->multi.device );
430       point.SetState( PointState::INTERRUPTED );
431       point.SetScreenPosition( Vector2( 0.0f, 0.0f ) );
432       handler->SendEvent( point, touchEvent->timestamp );
433
434       DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT EcoreEventMouseButtonCancel\n" );
435     }
436
437     return ECORE_CALLBACK_PASS_ON;
438   }
439
440   /**
441    * Called when a mouse wheel is received.
442    */
443   static Eina_Bool EcoreEventMouseWheel( void* data, int type, void* event )
444   {
445     Ecore_Event_Mouse_Wheel *mouseWheelEvent( (Ecore_Event_Mouse_Wheel*)event );
446
447     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT Ecore_Event_Mouse_Wheel: direction: %d, modifiers: %d, x: %d, y: %d, z: %d\n", mouseWheelEvent->direction, mouseWheelEvent->modifiers, mouseWheelEvent->x, mouseWheelEvent->y, mouseWheelEvent->z);
448
449     EventHandler* handler( (EventHandler*)data );
450     if ( mouseWheelEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
451     {
452       WheelEvent wheelEvent( WheelEvent::MOUSE_WHEEL, mouseWheelEvent->direction, mouseWheelEvent->modifiers, Vector2(mouseWheelEvent->x, mouseWheelEvent->y), mouseWheelEvent->z, mouseWheelEvent->timestamp );
453       handler->SendWheelEvent( wheelEvent );
454     }
455     return ECORE_CALLBACK_PASS_ON;
456   }
457
458   /////////////////////////////////////////////////////////////////////////////////////////////////
459   // Key Callbacks
460   /////////////////////////////////////////////////////////////////////////////////////////////////
461
462   /**
463    * Called when a key down is received.
464    */
465   static Eina_Bool EcoreEventKeyDown( void* data, int type, void* event )
466   {
467     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyDown \n" );
468
469     EventHandler* handler( (EventHandler*)data );
470     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
471     bool eventHandled( false );
472
473     // If a device key then skip ecore_imf_context_filter_event.
474     if ( ! KeyLookup::IsDeviceButton( keyEvent->keyname ) )
475     {
476       Ecore_IMF_Context* imfContext = NULL;
477       Dali::ImfManager imfManager( ImfManager::Get() );
478       if ( imfManager )
479       {
480         imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
481       }
482
483       if ( imfContext )
484       {
485         // We're consuming key down event so we have to pass to IMF so that it can parse it as well.
486         Ecore_IMF_Event_Key_Down ecoreKeyDownEvent;
487         ecoreKeyDownEvent.keyname   = keyEvent->keyname;
488         ecoreKeyDownEvent.key       = keyEvent->key;
489         ecoreKeyDownEvent.string    = keyEvent->string;
490         ecoreKeyDownEvent.compose   = keyEvent->compose;
491         ecoreKeyDownEvent.timestamp = keyEvent->timestamp;
492         ecoreKeyDownEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
493         ecoreKeyDownEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
494         ecoreKeyDownEvent.dev_name  = "";
495         ecoreKeyDownEvent.dev_class = ECORE_IMF_DEVICE_CLASS_KEYBOARD;
496         ecoreKeyDownEvent.dev_subclass = ECORE_IMF_DEVICE_SUBCLASS_NONE;
497
498         std::string checkDevice;
499         GetDeviceName( keyEvent, checkDevice );
500
501         // If the device is IME and the focused key is the direction keys, then we should send a key event to move a key cursor.
502         if( ( checkDevice == "ime" ) && ( ( !strncmp( keyEvent->keyname, "Left",  4 ) ) ||
503                                           ( !strncmp( keyEvent->keyname, "Right", 5 ) ) ||
504                                           ( !strncmp( keyEvent->keyname, "Up",    2 ) ) ||
505                                           ( !strncmp( keyEvent->keyname, "Down",  4 ) ) ) )
506         {
507           eventHandled = 0;
508         }
509         else
510         {
511           eventHandled = ecore_imf_context_filter_event( imfContext,
512                                                          ECORE_IMF_EVENT_KEY_DOWN,
513                                                          (Ecore_IMF_Event *) &ecoreKeyDownEvent );
514         }
515
516         // If the event has not been handled by IMF then check if we should reset our IMF context
517         if( !eventHandled )
518         {
519           if ( !strcmp( keyEvent->keyname, "Escape"   ) ||
520                !strcmp( keyEvent->keyname, "Return"   ) ||
521                !strcmp( keyEvent->keyname, "KP_Enter" ) )
522           {
523             ecore_imf_context_reset( imfContext );
524           }
525         }
526       }
527     }
528
529     // If the event wasn't handled then we should send a key event.
530     if ( !eventHandled )
531     {
532       if ( keyEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
533       {
534         std::string keyName( keyEvent->keyname );
535         std::string keyString( "" );
536         int keyCode = KeyLookup::GetDaliKeyCode( keyEvent->keyname);
537         keyCode = (keyCode == -1) ? 0 : keyCode;
538         int modifier( keyEvent->modifiers );
539         unsigned long time = keyEvent->timestamp;
540         if (!strncmp(keyEvent->keyname, "Keycode-", 8))
541           keyCode = atoi(keyEvent->keyname + 8);
542
543         // Ensure key event string is not NULL as keys like SHIFT have a null string.
544         if ( keyEvent->string )
545         {
546           keyString = keyEvent->string;
547         }
548
549         std::string deviceName;
550         DevelKeyEvent::DeviceClass::Type deviceClass;
551
552         GetDeviceName( keyEvent, deviceName );
553         GetDeviceClass( keyEvent, deviceClass );
554
555         DALI_LOG_INFO( gImfLogging, Debug::Verbose, "EVENT EcoreEventKeyDown - >>EcoreEventKeyDown deviceName(%s) deviceClass(%d)\n", deviceName.c_str(), deviceClass );
556
557         Integration::KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, Integration::KeyEvent::Down, deviceName, deviceClass );
558         handler->SendEvent( keyEvent );
559       }
560     }
561
562     return ECORE_CALLBACK_PASS_ON;
563   }
564
565   /**
566    * Called when a key up is received.
567    */
568   static Eina_Bool EcoreEventKeyUp( void* data, int type, void* event )
569   {
570     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyUp\n" );
571
572     EventHandler* handler( (EventHandler*)data );
573     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
574     bool eventHandled( false );
575
576     // Device keys like Menu, home, back button must skip ecore_imf_context_filter_event.
577     if ( ! KeyLookup::IsDeviceButton( keyEvent->keyname ) )
578     {
579       Ecore_IMF_Context* imfContext = NULL;
580       Dali::ImfManager imfManager( ImfManager::Get() );
581       if ( imfManager )
582       {
583         imfContext = ImfManager::GetImplementation( imfManager ).GetContext();
584       }
585
586       if ( imfContext )
587       {
588         // We're consuming key up event so we have to pass to IMF so that it can parse it as well.
589         Ecore_IMF_Event_Key_Up ecoreKeyUpEvent;
590         ecoreKeyUpEvent.keyname   = keyEvent->keyname;
591         ecoreKeyUpEvent.key       = keyEvent->key;
592         ecoreKeyUpEvent.string    = keyEvent->string;
593         ecoreKeyUpEvent.compose   = keyEvent->compose;
594         ecoreKeyUpEvent.timestamp = keyEvent->timestamp;
595         ecoreKeyUpEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
596         ecoreKeyUpEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
597         ecoreKeyUpEvent.dev_name  = "";
598         ecoreKeyUpEvent.dev_class = ECORE_IMF_DEVICE_CLASS_KEYBOARD;
599         ecoreKeyUpEvent.dev_subclass = ECORE_IMF_DEVICE_SUBCLASS_NONE;
600
601         eventHandled = ecore_imf_context_filter_event( imfContext,
602                                                        ECORE_IMF_EVENT_KEY_UP,
603                                                        (Ecore_IMF_Event *) &ecoreKeyUpEvent );
604       }
605     }
606
607     // If the event wasn't handled then we should send a key event.
608     if ( !eventHandled )
609     {
610       if ( keyEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
611       {
612         std::string keyName( keyEvent->keyname );
613         std::string keyString( "" );
614         int keyCode = KeyLookup::GetDaliKeyCode( keyEvent->keyname);
615         keyCode = (keyCode == -1) ? 0 : keyCode;
616         int modifier( keyEvent->modifiers );
617         unsigned long time = keyEvent->timestamp;
618         if (!strncmp(keyEvent->keyname, "Keycode-", 8))
619           keyCode = atoi(keyEvent->keyname + 8);
620
621         // Ensure key event string is not NULL as keys like SHIFT have a null string.
622         if ( keyEvent->string )
623         {
624           keyString = keyEvent->string;
625         }
626
627         std::string deviceName;
628         DevelKeyEvent::DeviceClass::Type deviceClass;
629
630         GetDeviceName( keyEvent, deviceName );
631         GetDeviceClass( keyEvent, deviceClass );
632
633         Integration::KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, Integration::KeyEvent::Up, deviceName, deviceClass );
634         handler->SendEvent( keyEvent );
635       }
636     }
637
638     return ECORE_CALLBACK_PASS_ON;
639   }
640
641   /////////////////////////////////////////////////////////////////////////////////////////////////
642   // Window Callbacks
643   /////////////////////////////////////////////////////////////////////////////////////////////////
644
645   /**
646    * Called when the window gains focus.
647    */
648   static Eina_Bool EcoreEventWindowFocusIn( void* data, int type, void* event )
649   {
650     Ecore_Wl_Event_Focus_In* focusInEvent( (Ecore_Wl_Event_Focus_In*)event );
651     EventHandler* handler( (EventHandler*)data );
652
653     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusIn \n" );
654
655     // If the window gains focus and we hid the keyboard then show it again.
656     if ( focusInEvent->win == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
657     {
658       DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT EcoreEventWindowFocusIn - >>WindowFocusGained \n" );
659
660       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
661       {
662         Dali::ImfManager imfManager( ImfManager::Get() );
663         if ( imfManager )
664         {
665           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
666           if( imfManagerImpl.RestoreAfterFocusLost() )
667           {
668             imfManagerImpl.Activate();
669           }
670         }
671       }
672       Dali::Clipboard clipboard = Clipboard::Get();
673       clipboard.HideClipboard();
674     }
675
676     return ECORE_CALLBACK_PASS_ON;
677   }
678
679   /**
680    * Called when the window loses focus.
681    */
682   static Eina_Bool EcoreEventWindowFocusOut( void* data, int type, void* event )
683   {
684     Ecore_Wl_Event_Focus_Out* focusOutEvent( (Ecore_Wl_Event_Focus_Out*)event );
685     EventHandler* handler( (EventHandler*)data );
686
687     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusOut \n" );
688
689     // If the window loses focus then hide the keyboard.
690     if ( focusOutEvent->win == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
691     {
692       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
693       {
694         Dali::ImfManager imfManager( ImfManager::Get() );
695         if ( imfManager )
696         {
697           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
698           if( imfManagerImpl.RestoreAfterFocusLost() )
699           {
700             imfManagerImpl.Deactivate();
701           }
702         }
703       }
704
705       // Hiding clipboard event will be ignored once because window focus out event is always received on showing clipboard
706       Dali::Clipboard clipboard = Clipboard::Get();
707       if ( clipboard )
708       {
709         Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
710         clipBoardImpl.HideClipboard(true);
711       }
712     }
713
714     return ECORE_CALLBACK_PASS_ON;
715   }
716
717   /**
718    * Called when the window is damaged.
719    */
720   static Eina_Bool EcoreEventWindowDamaged(void *data, int type, void *event)
721   {
722     return ECORE_CALLBACK_PASS_ON;
723   }
724
725   /**
726    * Called when the window properties are changed.
727    * We are only interested in the font change.
728    */
729
730
731   /////////////////////////////////////////////////////////////////////////////////////////////////
732   // Drag & Drop Callbacks
733   /////////////////////////////////////////////////////////////////////////////////////////////////
734
735   /**
736    * Called when a dragged item enters our window's bounds.
737    * This is when items are dragged INTO our window.
738    */
739   static Eina_Bool EcoreEventDndEnter( void* data, int type, void* event )
740   {
741     DALI_LOG_INFO( gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndEnter\n" );
742
743     return ECORE_CALLBACK_PASS_ON;
744   }
745
746   /**
747    * Called when a dragged item is moved within our window.
748    * This is when items are dragged INTO our window.
749    */
750   static Eina_Bool EcoreEventDndPosition( void* data, int type, void* event )
751   {
752     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndPosition\n" );
753
754     return ECORE_CALLBACK_PASS_ON;
755   }
756
757   /**
758    * Called when a dragged item leaves our window's bounds.
759    * This is when items are dragged INTO our window.
760    */
761   static Eina_Bool EcoreEventDndLeave( void* data, int type, void* event )
762   {
763     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndLeave\n" );
764
765     return ECORE_CALLBACK_PASS_ON;
766   }
767
768   /**
769    * Called when the dragged item is dropped within our window's bounds.
770    * This is when items are dragged INTO our window.
771    */
772   static Eina_Bool EcoreEventDndDrop( void* data, int type, void* event )
773   {
774     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndDrop\n" );
775
776     return ECORE_CALLBACK_PASS_ON;
777   }
778
779   /**
780    * Called when a dragged item is moved from our window and the target window has done processing it.
781    * This is when items are dragged FROM our window.
782    */
783   static Eina_Bool EcoreEventDndFinished( void* data, int type, void* event )
784   {
785     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndFinished\n" );
786     return ECORE_CALLBACK_PASS_ON;
787   }
788
789   /**
790    * Called when a dragged item is moved from our window and the target window has sent us a status.
791    * This is when items are dragged FROM our window.
792    */
793   static Eina_Bool EcoreEventDndStatus( void* data, int type, void* event )
794   {
795     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndStatus\n" );
796     return ECORE_CALLBACK_PASS_ON;
797   }
798
799   /**
800    * Called when the client messages (i.e. the accessibility events) are received.
801    */
802   static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
803   {
804     return ECORE_CALLBACK_PASS_ON;
805   }
806
807
808   /////////////////////////////////////////////////////////////////////////////////////////////////
809   // ElDBus Accessibility Callbacks
810   /////////////////////////////////////////////////////////////////////////////////////////////////
811
812 #ifdef DALI_ELDBUS_AVAILABLE
813   // Callback for Ecore ElDBus accessibility events.
814   static void OnEcoreElDBusAccessibilityNotification( void *context EINA_UNUSED, const Eldbus_Message *message )
815   {
816     EventHandler* handler = static_cast< EventHandler* >( context );
817     // Ignore any accessibility events when paused.
818     if( handler->mPaused )
819     {
820       return;
821     }
822
823     if( !handler->mAccessibilityAdaptor )
824     {
825       DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
826       return;
827     }
828
829     AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( handler->mAccessibilityAdaptor ) );
830     if( !accessibilityAdaptor )
831     {
832       DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
833       return;
834     }
835
836     int gestureValue;
837     int xS, yS, xE, yE;
838     int state; // 0 - begin, 1 - ongoing, 2 - ended, 3 - aborted
839     int eventTime;
840
841     // The string defines the arg-list's respective types.
842     if( !eldbus_message_arguments_get( message, "iiiiiiu", &gestureValue, &xS, &yS, &xE, &yE, &state, &eventTime ) )
843     {
844       DALI_LOG_ERROR( "OnEcoreElDBusAccessibilityNotification: Error getting arguments\n" );
845     }
846
847     DALI_LOG_INFO( gImfLogging, Debug::General, "Got gesture: Name: %d  Args: %d,%d,%d,%d  State: %d\n", gestureValue, xS, yS, xE, yE );
848
849     // Create a touch point object.
850     TouchPoint::State touchPointState( TouchPoint::Down );
851     if( state == 0 )
852     {
853       touchPointState = TouchPoint::Down; // Mouse down.
854     }
855     else if( state == 1 )
856     {
857       touchPointState = TouchPoint::Motion; // Mouse move.
858     }
859     else if( state == 2 )
860     {
861       touchPointState = TouchPoint::Up; // Mouse up.
862     }
863     else
864     {
865       touchPointState = TouchPoint::Interrupted; // Error.
866     }
867
868     // Send touch event to accessibility adaptor.
869     TouchPoint point( 0, touchPointState, (float)xS, (float)yS );
870
871     // Perform actions based on received gestures.
872     // Note: This is seperated from the reading so we can have other input readers without changing the below code.
873     switch( gestureValue )
874     {
875       case 0: // OneFingerHover
876       {
877         // Focus, read out.
878         accessibilityAdaptor->HandleActionReadEvent( (unsigned int)xS, (unsigned int)yS, true /* allow read again */ );
879         break;
880       }
881       case 1: // TwoFingersHover
882       {
883         // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
884         accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
885         break;
886       }
887       case 2: // ThreeFingersHover
888       {
889         // Read from top item on screen continuously.
890         accessibilityAdaptor->HandleActionReadFromTopEvent();
891         break;
892       }
893       case 3: // OneFingerFlickLeft
894       {
895         // Move to previous item.
896         accessibilityAdaptor->HandleActionReadPreviousEvent();
897         break;
898       }
899       case 4: // OneFingerFlickRight
900       {
901         // Move to next item.
902         accessibilityAdaptor->HandleActionReadNextEvent();
903         break;
904       }
905       case 5: // OneFingerFlickUp
906       {
907         // Move to previous item.
908         accessibilityAdaptor->HandleActionPreviousEvent();
909         break;
910       }
911       case 6: // OneFingerFlickDown
912       {
913         // Move to next item.
914         accessibilityAdaptor->HandleActionNextEvent();
915         break;
916       }
917       case 7: // TwoFingersFlickUp
918       {
919         // Scroll up the list.
920         accessibilityAdaptor->HandleActionScrollUpEvent();
921         break;
922       }
923       case 8: // TwoFingersFlickDown
924       {
925         // Scroll down the list.
926         accessibilityAdaptor->HandleActionScrollDownEvent();
927         break;
928       }
929       case 9: // TwoFingersFlickLeft
930       {
931         // Scroll left to the previous page
932         accessibilityAdaptor->HandleActionPageLeftEvent();
933         break;
934       }
935       case 10: // TwoFingersFlickRight
936       {
937         // Scroll right to the next page
938         accessibilityAdaptor->HandleActionPageRightEvent();
939         break;
940       }
941       case 11: // ThreeFingersFlickLeft
942       {
943         // Not exist yet
944         break;
945       }
946       case 12: // ThreeFingersFlickRight
947       {
948         // Not exist yet
949         break;
950       }
951       case 13: // ThreeFingersFlickUp
952       {
953         // Not exist yet
954         break;
955       }
956       case 14: // ThreeFingersFlickDown
957       {
958         // Not exist yet
959         break;
960       }
961       case 15: // OneFingerSingleTap
962       {
963         // Focus, read out.
964         accessibilityAdaptor->HandleActionReadEvent( (unsigned int)xS, (unsigned int)yS, true /* allow read again */ );
965         break;
966       }
967       case 16: // OneFingerDoubleTap
968       {
969         // Activate selected item / active edit mode.
970         accessibilityAdaptor->HandleActionActivateEvent();
971         break;
972       }
973       case 17: // OneFingerTripleTap
974       {
975         // Zoom
976         accessibilityAdaptor->HandleActionZoomEvent();
977         break;
978       }
979       case 18: // TwoFingersSingleTap
980       {
981         // Pause/Resume current speech
982         accessibilityAdaptor->HandleActionReadPauseResumeEvent();
983         break;
984       }
985       case 19: // TwoFingersDoubleTap
986       {
987         // Start/Stop current action
988         accessibilityAdaptor->HandleActionStartStopEvent();
989         break;
990       }
991       case 20: // TwoFingersTripleTap
992       {
993         // Read information from indicator
994         accessibilityAdaptor->HandleActionReadIndicatorInformationEvent();
995         break;
996       }
997       case 21: // ThreeFingersSingleTap
998       {
999         // Read from top item on screen continuously.
1000         accessibilityAdaptor->HandleActionReadFromTopEvent();
1001         break;
1002       }
1003       case 22: // ThreeFingersDoubleTap
1004       {
1005         // Read from next item continuously.
1006         accessibilityAdaptor->HandleActionReadFromNextEvent();
1007         break;
1008       }
1009       case 23: // ThreeFingersTripleTap
1010       {
1011         // Not exist yet
1012         break;
1013       }
1014       case 24: // OneFingerFlickLeftReturn
1015       {
1016         // Scroll up to the previous page
1017         accessibilityAdaptor->HandleActionPageUpEvent();
1018         break;
1019       }
1020       case 25: // OneFingerFlickRightReturn
1021       {
1022         // Scroll down to the next page
1023         accessibilityAdaptor->HandleActionPageDownEvent();
1024         break;
1025       }
1026       case 26: // OneFingerFlickUpReturn
1027       {
1028         // Move to the first item on screen
1029         accessibilityAdaptor->HandleActionMoveToFirstEvent();
1030         break;
1031       }
1032       case 27: // OneFingerFlickDownReturn
1033       {
1034         // Move to the last item on screen
1035         accessibilityAdaptor->HandleActionMoveToLastEvent();
1036         break;
1037       }
1038       case 28: // TwoFingersFlickLeftReturn
1039       {
1040         // Not exist yet
1041         break;
1042       }
1043       case 29: // TwoFingersFlickRightReturn
1044       {
1045         // Not exist yet
1046         break;
1047       }
1048       case 30: // TwoFingersFlickUpReturn
1049       {
1050         // Not exist yet
1051         break;
1052       }
1053       case 31: // TwoFingersFlickDownReturn
1054       {
1055         // Not exist yet
1056         break;
1057       }
1058       case 32: // ThreeFingersFlickLeftReturn
1059       {
1060         // Not exist yet
1061         break;
1062       }
1063       case 33: // ThreeFingersFlickRightReturn
1064       {
1065         // Not exist yet
1066         break;
1067       }
1068       case 34: // ThreeFingersFlickUpReturn
1069       {
1070         // Not exist yet
1071         break;
1072       }
1073       case 35: // ThreeFingersFlickDownReturn
1074       {
1075         // Not exist yet
1076         break;
1077       }
1078     }
1079   }
1080
1081   void EcoreElDBusInitialisation( void *handle )
1082   {
1083     Eldbus_Object *object;
1084     Eldbus_Proxy *manager;
1085
1086     if( !( mSystemConnection = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM) ) )
1087     {
1088       DALI_LOG_ERROR( "Unable to get system bus\n" );
1089     }
1090
1091     object = eldbus_object_get( mSystemConnection, BUS, PATH );
1092     if( !object )
1093     {
1094       DALI_LOG_ERROR( "Getting object failed\n" );
1095       return;
1096     }
1097
1098     manager = eldbus_proxy_get( object, INTERFACE );
1099     if( !manager )
1100     {
1101       DALI_LOG_ERROR( "Getting proxy failed\n" );
1102       return;
1103     }
1104
1105     if( !eldbus_proxy_signal_handler_add( manager, "GestureDetected", OnEcoreElDBusAccessibilityNotification, handle ) )
1106     {
1107       DALI_LOG_ERROR( "No signal handler returned\n" );
1108     }
1109   }
1110 #endif // DALI_ELDBUS_AVAILABLE
1111
1112   /**
1113    * Called when the source window notifies us the content in clipboard is selected.
1114    */
1115   static Eina_Bool EcoreEventSelectionClear( void* data, int type, void* event )
1116   {
1117     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionClear\n" );
1118     return ECORE_CALLBACK_PASS_ON;
1119   }
1120
1121   /**
1122    * Called when the source window sends us about the selected content.
1123    * For example, when dragged items are dragged INTO our window or when items are selected in the clipboard.
1124    */
1125   static Eina_Bool EcoreEventSelectionNotify( void* data, int type, void* event )
1126   {
1127     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionNotify\n" );
1128     return ECORE_CALLBACK_PASS_ON;
1129   }
1130
1131   /**
1132   * Called when the source window notifies us the content in clipboard is selected.
1133   */
1134   static Eina_Bool EcoreEventDataSend( void* data, int type, void* event )
1135   {
1136     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventDataSend\n" );
1137
1138     Dali::Clipboard clipboard = Clipboard::Get();
1139     if ( clipboard )
1140     {
1141       Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
1142       clipBoardImpl.ExcuteBuffered( true, event );
1143     }
1144     return ECORE_CALLBACK_PASS_ON;
1145   }
1146
1147    /**
1148     * Called when the source window sends us about the selected content.
1149     * For example, when item is selected in the clipboard.
1150     */
1151    static Eina_Bool EcoreEventDataReceive( void* data, int type, void* event )
1152    {
1153      DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventDataReceive\n" );
1154
1155      EventHandler* handler( (EventHandler*)data );
1156       Dali::Clipboard clipboard = Clipboard::Get();
1157       char *selectionData = NULL;
1158       if ( clipboard )
1159       {
1160         Clipboard& clipBoardImpl( GetImplementation( clipboard ) );
1161         selectionData = clipBoardImpl.ExcuteBuffered( false, event );
1162       }
1163       if ( selectionData && handler->mClipboardEventNotifier )
1164       {
1165         ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( handler->mClipboardEventNotifier ) );
1166         std::string content( selectionData, strlen(selectionData) );
1167
1168         clipboardEventNotifier.SetContent( content );
1169         clipboardEventNotifier.EmitContentSelectedSignal();
1170       }
1171      return ECORE_CALLBACK_PASS_ON;
1172    }
1173
1174   /*
1175   * Called when rotate event is recevied
1176   */
1177   static Eina_Bool EcoreEventRotate( void* data, int type, void* event )
1178   {
1179     DALI_LOG_INFO( gSelectionEventLogFilter, Debug::Concise, "EcoreEventRotate\n" );
1180
1181     EventHandler* handler( (EventHandler*)data );
1182     Ecore_Wl_Event_Window_Rotate* ev( (Ecore_Wl_Event_Window_Rotate*)event );
1183
1184     if( ev->win != (unsigned int)ecore_wl_window_id_get( handler->mImpl->mWindow ) )
1185     {
1186       return ECORE_CALLBACK_PASS_ON;
1187     }
1188
1189     RotationEvent rotationEvent;
1190     rotationEvent.angle = ev->angle;
1191     rotationEvent.winResize = 0;
1192
1193     if( ev->angle == 0 || ev->angle == 180 )
1194     {
1195       rotationEvent.width = ev->w;
1196       rotationEvent.height = ev->h;
1197     }
1198     else
1199     {
1200       rotationEvent.width = ev->h;
1201       rotationEvent.height = ev->w;
1202     }
1203
1204     handler->SendRotationPrepareEvent( rotationEvent );
1205
1206     return ECORE_CALLBACK_PASS_ON;
1207   }
1208
1209   /*
1210   * Called when detent event is recevied
1211   */
1212   static Eina_Bool EcoreEventDetent( void* data, int type, void* event )
1213   {
1214     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventDetent\n" );
1215     EventHandler* handler( (EventHandler*)data );
1216     Ecore_Event_Detent_Rotate *e((Ecore_Event_Detent_Rotate *)event);
1217     int direction = (e->direction == ECORE_DETENT_DIRECTION_CLOCKWISE) ? 1 : -1;
1218     int timeStamp = e->timestamp;
1219
1220     WheelEvent wheelEvent( WheelEvent::CUSTOM_WHEEL, 0, 0, Vector2(0.0f, 0.0f), direction, timeStamp );
1221     handler->SendWheelEvent( wheelEvent );
1222     return ECORE_CALLBACK_PASS_ON;
1223   }
1224
1225   /////////////////////////////////////////////////////////////////////////////////////////////////
1226   // Font Callbacks
1227   /////////////////////////////////////////////////////////////////////////////////////////////////
1228   /**
1229    * Called when a font name is changed.
1230    */
1231   static void VconfNotifyFontNameChanged( keynode_t* node, void* data )
1232   {
1233     EventHandler* handler = static_cast<EventHandler*>( data );
1234     handler->SendEvent( StyleChange::DEFAULT_FONT_CHANGE );
1235   }
1236
1237   /**
1238    * Called when a font size is changed.
1239    */
1240   static void VconfNotifyFontSizeChanged( keynode_t* node, void* data )
1241   {
1242     EventHandler* handler = static_cast<EventHandler*>( data );
1243     handler->SendEvent( StyleChange::DEFAULT_FONT_SIZE_CHANGE );
1244   }
1245
1246   void ConvertTouchPosition( Integration::Point& point )
1247   {
1248     Vector2 position = point.GetScreenPosition();
1249     Vector2 convertedPosition;
1250
1251     switch( mRotationAngle )
1252     {
1253       case 90:
1254       {
1255         convertedPosition.x = mWindowWidth - position.y;
1256         convertedPosition.y = position.x;
1257         break;
1258       }
1259       case 180:
1260       {
1261         convertedPosition.x = mWindowWidth - position.x;
1262         convertedPosition.y = mWindowHeight - position.y;
1263         break;
1264       }
1265       case 270:
1266       {
1267         convertedPosition.x = position.y;
1268         convertedPosition.y = mWindowHeight - position.x;
1269         break;
1270       }
1271       default:
1272       {
1273         convertedPosition = position;
1274         break;
1275       }
1276     }
1277
1278     point.SetScreenPosition( convertedPosition );
1279   }
1280
1281   // Data
1282   EventHandler* mHandler;
1283   std::vector<Ecore_Event_Handler*> mEcoreEventHandler;
1284   Ecore_Wl_Window* mWindow;
1285   int mRotationAngle;
1286   int mWindowWidth;
1287   int mWindowHeight;
1288 #ifdef DALI_ELDBUS_AVAILABLE
1289   Eldbus_Connection* mSystemConnection;
1290 #endif // DALI_ELDBUS_AVAILABLE
1291 };
1292
1293 EventHandler::EventHandler( RenderSurface* surface, CoreEventInterface& coreEventInterface, GestureManager& gestureManager, DamageObserver& damageObserver, DragAndDropDetectorPtr dndDetector )
1294 : mCoreEventInterface( coreEventInterface ),
1295   mGestureManager( gestureManager ),
1296   mStyleMonitor( StyleMonitor::Get() ),
1297   mDamageObserver( damageObserver ),
1298   mRotationObserver( NULL ),
1299   mDragAndDropDetector( dndDetector ),
1300   mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
1301   mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
1302   mClipboard( Clipboard::Get() ),
1303   mImpl( NULL ),
1304   mPaused( false )
1305 {
1306   Ecore_Wl_Window* window = 0;
1307
1308   // this code only works with the Ecore RenderSurface so need to downcast
1309   ECore::WindowRenderSurface* ecoreSurface = dynamic_cast< ECore::WindowRenderSurface* >( surface );
1310   if( ecoreSurface )
1311   {
1312     window = ecoreSurface->GetWlWindow();
1313   }
1314
1315   mImpl = new Impl(this, window);
1316 }
1317
1318 EventHandler::~EventHandler()
1319 {
1320   if(mImpl)
1321   {
1322     delete mImpl;
1323   }
1324
1325   mGestureManager.Stop();
1326 }
1327
1328 void EventHandler::SendEvent(Integration::Point& point, unsigned long timeStamp)
1329 {
1330   if(timeStamp < 1)
1331   {
1332     timeStamp = GetCurrentMilliSeconds();
1333   }
1334
1335   mImpl->ConvertTouchPosition( point );
1336
1337   Integration::TouchEvent touchEvent;
1338   Integration::HoverEvent hoverEvent;
1339   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
1340   if(type != Integration::TouchEventCombiner::DispatchNone )
1341   {
1342     DALI_LOG_INFO(gTouchEventLogFilter, Debug::General, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y);
1343
1344     // First the touch and/or hover event & related gesture events are queued
1345     if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
1346     {
1347       mCoreEventInterface.QueueCoreEvent( touchEvent );
1348       mGestureManager.SendEvent(touchEvent);
1349     }
1350
1351     if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
1352     {
1353       mCoreEventInterface.QueueCoreEvent( hoverEvent );
1354     }
1355
1356     // Next the events are processed with a single call into Core
1357     mCoreEventInterface.ProcessCoreEvents();
1358   }
1359 }
1360
1361 void EventHandler::SendEvent(Integration::KeyEvent& keyEvent)
1362 {
1363   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
1364   if ( physicalKeyboard )
1365   {
1366     if ( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
1367     {
1368       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
1369     }
1370   }
1371
1372   // Create send KeyEvent to Core.
1373   mCoreEventInterface.QueueCoreEvent( keyEvent );
1374   mCoreEventInterface.ProcessCoreEvents();
1375 }
1376
1377 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
1378 {
1379   // Create WheelEvent and send to Core.
1380   Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
1381   mCoreEventInterface.QueueCoreEvent( event );
1382   mCoreEventInterface.ProcessCoreEvents();
1383 }
1384
1385 void EventHandler::SendEvent( StyleChange::Type styleChange )
1386 {
1387   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
1388   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
1389 }
1390
1391 void EventHandler::SendEvent( const DamageArea& area )
1392 {
1393   mDamageObserver.OnDamaged( area );
1394 }
1395
1396 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
1397 {
1398   if( mRotationObserver != NULL )
1399   {
1400     mImpl->mRotationAngle = event.angle;
1401     mImpl->mWindowWidth = event.width;
1402     mImpl->mWindowHeight = event.height;
1403
1404     mRotationObserver->OnRotationPrepare( event );
1405     mRotationObserver->OnRotationRequest();
1406   }
1407 }
1408
1409 void EventHandler::SendRotationRequestEvent( )
1410 {
1411   // No need to separate event into prepare and request in wayland
1412 }
1413
1414 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
1415 {
1416   Integration::Point convertedPoint( point );
1417   SendEvent(convertedPoint, timeStamp);
1418 }
1419
1420 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
1421 {
1422   SendWheelEvent( wheelEvent );
1423 }
1424
1425 void EventHandler::FeedKeyEvent( KeyEvent& event )
1426 {
1427   Integration::KeyEvent convertedEvent( event );
1428   SendEvent( convertedEvent );
1429 }
1430
1431 void EventHandler::FeedEvent( Integration::Event& event )
1432 {
1433   mCoreEventInterface.QueueCoreEvent( event );
1434   mCoreEventInterface.ProcessCoreEvents();
1435 }
1436
1437 void EventHandler::Reset()
1438 {
1439   mCombiner.Reset();
1440
1441   // Any touch listeners should be told of the interruption.
1442   Integration::TouchEvent event;
1443   Integration::Point point;
1444   point.SetState( PointState::INTERRUPTED );
1445   event.AddPoint( point );
1446
1447   // First the touch event & related gesture events are queued
1448   mCoreEventInterface.QueueCoreEvent( event );
1449   mGestureManager.SendEvent( event );
1450
1451   // Next the events are processed with a single call into Core
1452   mCoreEventInterface.ProcessCoreEvents();
1453 }
1454
1455 void EventHandler::Pause()
1456 {
1457   mPaused = true;
1458   Reset();
1459 }
1460
1461 void EventHandler::Resume()
1462 {
1463   mPaused = false;
1464   Reset();
1465 }
1466
1467 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
1468 {
1469   mDragAndDropDetector = detector;
1470 }
1471
1472 void EventHandler::SetRotationObserver( RotationObserver* observer )
1473 {
1474   mRotationObserver = observer;
1475 }
1476
1477 } // namespace Adaptor
1478
1479 } // namespace Internal
1480
1481 } // namespace Dali
1482
1483 #pragma GCC diagnostic pop