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