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