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