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