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