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