Merge "Fix for deadlock in PixmapRenderSurface" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / ecore-x-event-handler.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_X.h>
25
26 #include <X11/Xlib.h>
27 #include <X11/extensions/XInput2.h>
28 #include <X11/extensions/XI2.h>
29
30 #include <cstring>
31
32 #include <sys/time.h>
33
34 #ifndef DALI_PROFILE_UBUNTU
35 #include <vconf.h>
36 #include <vconf-keys.h>
37 #endif // DALI_PROFILE_UBUNTU
38
39 #ifdef DALI_ELDBUS_AVAILABLE
40 #include <Eldbus.h>
41 #endif // DALI_ELDBUS_AVAILABLE
42
43 #include <dali/public-api/common/vector-wrapper.h>
44 #include <dali/public-api/events/touch-point.h>
45 #include <dali/public-api/events/key-event.h>
46 #include <dali/public-api/events/wheel-event.h>
47 #include <dali/integration-api/debug.h>
48 #include <dali/integration-api/events/key-event-integ.h>
49 #include <dali/integration-api/events/touch-event-integ.h>
50 #include <dali/integration-api/events/hover-event-integ.h>
51 #include <dali/integration-api/events/wheel-event-integ.h>
52
53 // INTERNAL INCLUDES
54 #include <events/gesture-manager.h>
55 #include <window-render-surface.h>
56 #include <clipboard-impl.h>
57 #include <key-impl.h>
58 #include <physical-keyboard-impl.h>
59 #include <style-monitor-impl.h>
60 #include <base/core-event-interface.h>
61
62 namespace Dali
63 {
64
65 namespace Internal
66 {
67
68 namespace Adaptor
69 {
70
71 #if defined(DEBUG_ENABLED)
72 namespace
73 {
74 Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
75 Integration::Log::Filter* gClientMessageLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_CLIENT_MESSAGE");
76 Integration::Log::Filter* gDragAndDropLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DND");
77 Integration::Log::Filter* gImfLogging  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_IMF");
78 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
79 } // unnamed namespace
80 #endif
81
82
83 namespace
84 {
85
86 const char * DETENT_DEVICE_NAME = "tizen_detent";
87
88 // DBUS accessibility
89 #define A11Y_BUS "org.a11y.Bus"
90 #define A11Y_INTERFACE "org.a11y.Bus"
91 #define A11Y_PATH "/org/a11y/bus"
92 #define A11Y_GET_ADDRESS "GetAddress"
93 #define BUS "com.samsung.EModule"
94 #define INTERFACE "com.samsung.GestureNavigation"
95 #define PATH "/com/samsung/GestureNavigation"
96 #define SIGNAL "GestureDetected"
97
98 #ifndef DALI_PROFILE_UBUNTU
99 const char * DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME = "db/setting/accessibility/font_name"; // It will be update at vconf-key.h and replaced.
100 #endif // DALI_PROFILE_UBUNTU
101
102 const unsigned int PRIMARY_TOUCH_BUTTON_ID( 1 );
103
104 #ifndef DALI_PROFILE_UBUNTU
105 const char * CLIPBOARD_ATOM                = "CBHM_MSG";
106 const char * CLIPBOARD_SET_OWNER_MESSAGE   = "SET_OWNER";
107 #endif // DALI_PROFILE_UBUNTU
108
109 /// The atoms required by Ecore for Drag & Drop behaviour.
110 Ecore_X_Atom DRAG_AND_DROP_ATOMS[] =
111 {
112   ECORE_X_ATOM_XDND_ACTION_COPY,
113 };
114
115 /// The types that we support.
116 const char * DRAG_AND_DROP_TYPES[] =
117 {
118   ECORE_X_SELECTION_TARGET_UTF8_STRING,
119 };
120
121 const unsigned int DRAG_AND_DROP_ATOMS_NUMBER = sizeof( DRAG_AND_DROP_ATOMS ) / sizeof( Ecore_X_Atom );
122 const unsigned int DRAG_AND_DROP_TYPES_NUMBER = sizeof( DRAG_AND_DROP_TYPES ) / sizeof( const char * );
123
124 const unsigned int BYTES_PER_CHARACTER_FOR_ATTRIBUTES = 3;
125
126 #ifdef DALI_ELDBUS_AVAILABLE
127 // DBus gesture string matching lists.
128 // TODO: This needs moving to its own module.
129 const char * ElDBusAccessibilityFingerCountStrings[] =
130 {
131   "OneFinger",
132   "TwoFingers",
133   "ThreeFingers"
134 };
135 const unsigned int FingerCountStringsTotal = sizeof( ElDBusAccessibilityFingerCountStrings ) / sizeof( ElDBusAccessibilityFingerCountStrings[0] );
136 enum GestureType
137 {
138   GESTURE_TYPE_NONE,
139   GESTURE_TYPE_HOVER,
140   GESTURE_TYPE_SINGLE_TAP,
141   GESTURE_TYPE_DOUBLE_TAP,
142   GESTURE_TYPE_TRIPLE_TAP
143 };
144 struct GestureTypeTable
145 {
146   const char* name;
147   const GestureType type;
148 };
149 GestureTypeTable ElDBusAccessibilityFullEventTypeStrings[] =
150 {
151   { "Hover",     GESTURE_TYPE_HOVER      },
152   { "SingleTap", GESTURE_TYPE_SINGLE_TAP },
153   { "DoubleTap", GESTURE_TYPE_DOUBLE_TAP },
154   { "TripleTap", GESTURE_TYPE_TRIPLE_TAP }
155 };
156 const unsigned int FullEventTypeStringsTotal = sizeof( ElDBusAccessibilityFullEventTypeStrings ) / sizeof( ElDBusAccessibilityFullEventTypeStrings[0] );
157 enum SubGestureType
158 {
159   SUB_GESTURE_TYPE_NONE,
160   SUB_GESTURE_TYPE_FLICK
161 };
162 struct SubGestureTypeTable
163 {
164   const char* name;
165   const SubGestureType type;
166 };
167 SubGestureTypeTable ElDBusAccessibilityDirectionalEventTypeStrings[] =
168 {
169   { "Flick", SUB_GESTURE_TYPE_FLICK }
170 };
171 const unsigned int DirectionalEventTypeStringsTotal = sizeof( ElDBusAccessibilityDirectionalEventTypeStrings ) / sizeof( ElDBusAccessibilityDirectionalEventTypeStrings[0] );
172 enum GestureDirection
173 {
174   GESTURE_DIRECTION_NONE,
175   GESTURE_DIRECTION_UP,
176   GESTURE_DIRECTION_DOWN,
177   GESTURE_DIRECTION_LEFT,
178   GESTURE_DIRECTION_RIGHT,
179   GESTURE_DIRECTION_UP_RETURN,
180   GESTURE_DIRECTION_DOWN_RETURN,
181   GESTURE_DIRECTION_LEFT_RETURN,
182   GESTURE_DIRECTION_RIGHT_RETURN
183 };
184 struct GestureDirectionTable
185 {
186         const char* name;
187         const GestureDirection direction;
188 };
189 GestureDirectionTable ElDBusAccessibilityDirectionStrings[] =
190 {
191   { "Up",           GESTURE_DIRECTION_UP           },
192   { "Down",         GESTURE_DIRECTION_DOWN         },
193   { "Left",         GESTURE_DIRECTION_LEFT         },
194   { "Right",        GESTURE_DIRECTION_RIGHT        },
195   { "UpReturn",     GESTURE_DIRECTION_UP_RETURN    },
196   { "DownReturn",   GESTURE_DIRECTION_DOWN_RETURN  },
197   { "LeftReturn",   GESTURE_DIRECTION_LEFT_RETURN  },
198   { "RightReturn",  GESTURE_DIRECTION_RIGHT_RETURN }
199 };
200 const unsigned int DirectionStringsTotal = sizeof( ElDBusAccessibilityDirectionStrings ) / sizeof( ElDBusAccessibilityDirectionStrings[0] );
201 #endif // DALI_ELDBUS_AVAILABLE
202
203 /**
204  * Ecore_Event_Modifier enums in Ecore_Input.h do not match Ecore_IMF_Keyboard_Modifiers in Ecore_IMF.h.
205  * This function converts from Ecore_Event_Modifier to Ecore_IMF_Keyboard_Modifiers enums.
206  * @param[in] ecoreModifier the Ecore_Event_Modifier input.
207  * @return the Ecore_IMF_Keyboard_Modifiers output.
208  */
209 Ecore_IMF_Keyboard_Modifiers EcoreInputModifierToEcoreIMFModifier(unsigned int ecoreModifier)
210 {
211    int modifier( ECORE_IMF_KEYBOARD_MODIFIER_NONE );  // If no other matches returns NONE.
212
213
214    if ( ecoreModifier & ECORE_EVENT_MODIFIER_SHIFT )  // enums from ecore_input/Ecore_Input.h
215    {
216      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_SHIFT;  // enums from ecore_imf/ecore_imf.h
217    }
218
219    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALT )
220    {
221      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALT;
222    }
223
224    if ( ecoreModifier & ECORE_EVENT_MODIFIER_CTRL )
225    {
226      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_CTRL;
227    }
228
229    if ( ecoreModifier & ECORE_EVENT_MODIFIER_WIN )
230    {
231      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_WIN;
232    }
233
234    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALTGR )
235    {
236      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALTGR;
237    }
238
239    return static_cast<Ecore_IMF_Keyboard_Modifiers>( modifier );
240 }
241
242
243 // Copied from x server
244 static unsigned int GetCurrentMilliSeconds(void)
245 {
246   struct timeval tv;
247
248   struct timespec tp;
249   static clockid_t clockid;
250
251   if (!clockid)
252   {
253 #ifdef CLOCK_MONOTONIC_COARSE
254     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
255       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
256     {
257       clockid = CLOCK_MONOTONIC_COARSE;
258     }
259     else
260 #endif
261     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
262     {
263       clockid = CLOCK_MONOTONIC;
264     }
265     else
266     {
267       clockid = ~0L;
268     }
269   }
270   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
271   {
272     return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
273   }
274
275   gettimeofday(&tv, NULL);
276   return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
277 }
278
279 } // unnamed namespace
280
281 // Impl to hide EFL implementation.
282 struct EventHandler::Impl
283 {
284   // Construction & Destruction
285
286   /**
287    * Constructor
288    */
289   Impl( EventHandler* handler, Ecore_X_Window window )
290   : mHandler( handler ),
291     mEcoreEventHandler(),
292     mWindow( window ),
293     mXiDeviceId( 0 )
294   {
295     // Only register for touch and key events if we have a window
296     if ( window != 0 )
297     {
298       // Register Touch events
299       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_DOWN,  EcoreEventMouseButtonDown, handler ) );
300       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_UP,    EcoreEventMouseButtonUp,   handler ) );
301       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_MOVE,         EcoreEventMouseButtonMove, handler ) );
302       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_OUT,          EcoreEventMouseButtonUp,   handler ) ); // process mouse out event like up event
303
304       // Register Mouse wheel events
305       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_WHEEL,        EcoreEventMouseWheel,      handler ) );
306
307       // Register Key events
308       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_DOWN,           EcoreEventKeyDown,         handler ) );
309       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_UP,             EcoreEventKeyUp,           handler ) );
310
311       // Register Focus events
312       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_WINDOW_FOCUS_IN,  EcoreEventWindowFocusIn,   handler ) );
313       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_WINDOW_FOCUS_OUT, EcoreEventWindowFocusOut,  handler ) );
314
315       // Register Window damage events
316       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_WINDOW_DAMAGE,    EcoreEventWindowDamaged, handler ) );
317
318       // Enable Drag & Drop and register DnD events
319       ecore_x_dnd_aware_set( window, EINA_TRUE );
320       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_ENTER,       EcoreEventDndEnter,            handler) );
321       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_POSITION,    EcoreEventDndPosition,         handler) );
322       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_LEAVE,       EcoreEventDndLeave,            handler) );
323       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_DROP,        EcoreEventDndDrop,             handler) );
324       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_FINISHED,    EcoreEventDndFinished,         handler) );
325       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_XDND_STATUS,      EcoreEventDndStatus,           handler) );
326
327       // Register Client message events - accessibility etc.
328       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_CLIENT_MESSAGE,  EcoreEventClientMessage, handler ) );
329
330       // Register Selection event - clipboard selection, Drag & Drop selection etc.
331       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_SELECTION_CLEAR, EcoreEventSelectionClear, handler ) );
332       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_SELECTION_NOTIFY, EcoreEventSelectionNotify, handler ) );
333
334       // Initialize Xi2 system
335       Display* display = static_cast< Display* >(ecore_x_display_get());
336       Ecore_X_Window rootWindow = ecore_x_window_root_first_get();
337       int opcode = 0, event = 0, error = 0;
338       int major = XI_2_Major;
339       int minor = XI_2_Minor;
340       int deviceCount = 0;
341       XIEventMask xiEventMask;
342
343       // Check if X input extension available
344       if( XQueryExtension( display, "XInputExtension", &opcode, &event, &error ) )
345       {
346         // We support version 2.0
347         if( XIQueryVersion( display, &major, &minor ) != BadRequest )
348         {
349           xiEventMask.deviceid = XIAllDevices;
350
351           // Check device id
352           bool match = false;
353           XIDeviceInfo* deviceInfo = NULL;
354           deviceInfo = XIQueryDevice( display, XIAllDevices, &deviceCount );
355
356           for( int i = 0; i < deviceCount; i++ )
357           {
358             if( !strncmp( deviceInfo[i].name, DETENT_DEVICE_NAME, strlen( DETENT_DEVICE_NAME ) ) )
359             {
360               xiEventMask.deviceid = deviceInfo[i].deviceid;
361               match = true;
362               break;
363             }
364           }
365
366           if( match )
367           {
368             mXiDeviceId = xiEventMask.deviceid;
369
370             // SelectXi2Event
371             xiEventMask.mask = (unsigned char*)(calloc( 1, XIMaskLen( XI_LASTEVENT ) ) );
372             XISetMask( xiEventMask.mask, XI_RawMotion );
373
374             xiEventMask.mask_len = sizeof( xiEventMask.mask );
375
376             int ret = XISelectEvents( display, rootWindow, &xiEventMask, 1 );
377             if( ret == 0 )
378             {
379               // Register custom wheel events
380               mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_X_EVENT_GENERIC, EcoreEventCustomWheel, handler ) );
381             }
382             else
383             {
384               DALI_LOG_INFO( gImfLogging, Debug::General, "Failed to Select Events\n" );
385             }
386
387             free( xiEventMask.mask );
388
389             if( deviceInfo != NULL )
390             {
391               XIFreeDeviceInfo( deviceInfo );
392             }
393           }
394         }
395         else
396         {
397           DALI_LOG_INFO( gImfLogging, Debug::General, "Failed to query XI Version\n" );
398         }
399       }
400       else
401       {
402         DALI_LOG_INFO( gImfLogging, Debug::General, "Failed to query XInputExtension\n" );
403       }
404
405 #ifndef DALI_PROFILE_UBUNTU
406       // Register Vconf notify - font name, font size and style
407       vconf_notify_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, VconfNotifyFontNameChanged, handler );
408       vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged, handler );
409 #endif // DALI_PROFILE_UBUNTU
410
411 #ifdef DALI_ELDBUS_AVAILABLE
412
413       // Initialize ElDBus.
414       DALI_LOG_INFO( gImfLogging, Debug::General, "Starting DBus Initialization" );
415       eldbus_init();
416
417       Eldbus_Connection *sessionConnection;
418       sessionConnection = eldbus_connection_get( ELDBUS_CONNECTION_TYPE_SESSION );
419
420       Eldbus_Object *a11yObject = eldbus_object_get( sessionConnection, A11Y_BUS, A11Y_PATH );
421       Eldbus_Proxy *elDBusManager = eldbus_proxy_get( a11yObject, A11Y_INTERFACE );
422
423       // Pass in handler in the cb_data field so we can access the accessibility adaptor within the callback.
424       eldbus_proxy_call( elDBusManager, A11Y_GET_ADDRESS, EcoreElDBusInitialisation, handler, -1, "" );
425
426       DALI_LOG_INFO( gImfLogging, Debug::General, "Finished DBus Initialization" );
427
428 #endif // DALI_ELDBUS_AVAILABLE
429     }
430   }
431
432   /**
433    * Destructor
434    */
435   ~Impl()
436   {
437 #ifndef DALI_PROFILE_UBUNTU
438     vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged );
439     vconf_ignore_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, VconfNotifyFontNameChanged );
440 #endif // DALI_PROFILE_UBUNTU
441
442     for( std::vector<Ecore_Event_Handler*>::iterator iter = mEcoreEventHandler.begin(), endIter = mEcoreEventHandler.end(); iter != endIter; ++iter )
443     {
444       ecore_event_handler_del( *iter );
445     }
446
447 #ifdef DALI_ELDBUS_AVAILABLE
448     // Close down ElDBus
449     eldbus_shutdown();
450 #endif // DALI_ELDBUS_AVAILABLE
451   }
452
453   // Static methods
454
455   /////////////////////////////////////////////////////////////////////////////////////////////////
456   // Touch Callbacks
457   /////////////////////////////////////////////////////////////////////////////////////////////////
458
459   /**
460    * Called when a touch down is received.
461    */
462   static Eina_Bool EcoreEventMouseButtonDown( void* data, int type, void* event )
463   {
464     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
465     EventHandler* handler( (EventHandler*)data );
466
467     if ( touchEvent->window == handler->mImpl->mWindow )
468     {
469       TouchPoint::State state ( TouchPoint::Down );
470
471       // Check if the buttons field is set and ensure it's the primary touch button.
472       // If this event was triggered by buttons other than the primary button (used for touch), then
473       // just send an interrupted event to Core.
474       if ( touchEvent->buttons && (touchEvent->buttons != PRIMARY_TOUCH_BUTTON_ID ) )
475       {
476         state = TouchPoint::Interrupted;
477       }
478
479       TouchPoint point( touchEvent->multi.device, state, touchEvent->x, touchEvent->y );
480       handler->SendEvent( point, touchEvent->timestamp );
481     }
482
483     return ECORE_CALLBACK_PASS_ON;
484   }
485
486   /**
487    * Called when a touch up is received.
488    */
489   static Eina_Bool EcoreEventMouseButtonUp( void* data, int type, void* event )
490   {
491     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
492     EventHandler* handler( (EventHandler*)data );
493
494     if ( touchEvent->window == handler->mImpl->mWindow )
495     {
496       TouchPoint point( touchEvent->multi.device, TouchPoint::Up, touchEvent->x, touchEvent->y );
497       handler->SendEvent( point, touchEvent->timestamp );
498     }
499
500     return ECORE_CALLBACK_PASS_ON;
501   }
502
503   /**
504    * Called when a touch motion is received.
505    */
506   static Eina_Bool EcoreEventMouseButtonMove( void* data, int type, void* event )
507   {
508     Ecore_Event_Mouse_Move *touchEvent( (Ecore_Event_Mouse_Move*)event );
509     EventHandler* handler( (EventHandler*)data );
510
511     if ( touchEvent->window == handler->mImpl->mWindow )
512     {
513       TouchPoint point( touchEvent->multi.device, TouchPoint::Motion, touchEvent->x, touchEvent->y );
514       handler->SendEvent( point, touchEvent->timestamp );
515     }
516
517     return ECORE_CALLBACK_PASS_ON;
518   }
519
520   /////////////////////////////////////////////////////////////////////////////////////////////////
521   // Wheel Callbacks
522   /////////////////////////////////////////////////////////////////////////////////////////////////
523
524   /**
525    * Called when a mouse wheel is received.
526    */
527   static Eina_Bool EcoreEventMouseWheel( void* data, int type, void* event )
528   {
529     Ecore_Event_Mouse_Wheel *mouseWheelEvent( (Ecore_Event_Mouse_Wheel*)event );
530
531     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 );
532
533     EventHandler* handler( (EventHandler*)data );
534     if ( mouseWheelEvent->window == handler->mImpl->mWindow )
535     {
536       WheelEvent wheelEvent( WheelEvent::MOUSE_WHEEL, mouseWheelEvent->direction, mouseWheelEvent->modifiers, Vector2(mouseWheelEvent->x, mouseWheelEvent->y), mouseWheelEvent->z, mouseWheelEvent->timestamp );
537       handler->SendWheelEvent( wheelEvent );
538     }
539     return ECORE_CALLBACK_PASS_ON;
540   }
541
542   /**
543    * Called when a custom wheel is received.
544    */
545   static Eina_Bool EcoreEventCustomWheel( void* data, int type, void* event )
546   {
547     Ecore_X_Event_Generic *genericEvent( (Ecore_X_Event_Generic*)event );
548     EventHandler* handler( (EventHandler*)data );
549
550     switch( genericEvent->evtype )
551     {
552       case XI_RawMotion:
553       {
554         XIRawEvent* xiRawEvent = static_cast< XIRawEvent* >( genericEvent->data );
555         unsigned int timeStamp = 0;
556
557         if( xiRawEvent->deviceid != handler->mImpl->mXiDeviceId )
558         {
559           return ECORE_CALLBACK_PASS_ON;
560         }
561
562         // X(0): rotate: NOT USED
563         // Y(1): timestamp
564         // Z(2): direction
565
566         double* value = xiRawEvent->raw_values;
567
568         if( XIMaskIsSet( xiRawEvent->valuators.mask, 1) )
569         {
570           timeStamp = static_cast< unsigned int >( *(value + 1) );
571         }
572
573         if( XIMaskIsSet( xiRawEvent->valuators.mask, 2) )
574         {
575           // if z == 1, clockwise
576           // otherwise counter-clockwise
577           int z = static_cast< int >( *(value + 2) );
578
579           // In DALi, positive value means clockwise, and negative value means counter-clockwise
580           if( z == 0 )
581           {
582             z = -1;
583           }
584
585           DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT EcoreEventCustomWheel: z: %d\n", z );
586
587           WheelEvent wheelEvent( WheelEvent::CUSTOM_WHEEL, 0, 0, Vector2(0.0f, 0.0f), z, timeStamp );
588           handler->SendWheelEvent( wheelEvent );
589         }
590         break;
591       }
592       default:
593       {
594         break;
595       }
596     }
597
598     return ECORE_CALLBACK_PASS_ON;
599   }
600
601   /////////////////////////////////////////////////////////////////////////////////////////////////
602   // Key Callbacks
603   /////////////////////////////////////////////////////////////////////////////////////////////////
604
605   /**
606    * Called when a key down is received.
607    */
608   static Eina_Bool EcoreEventKeyDown( void* data, int type, void* event )
609   {
610     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyDown \n" );
611
612     EventHandler* handler( (EventHandler*)data );
613     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
614     bool eventHandled( false );
615
616     // If a device key then skip ecore_imf_context_filter_event.
617     if ( ! KeyLookup::IsDeviceButton( keyEvent->keyname ) )
618     {
619       Ecore_IMF_Context* imfContext = NULL;
620       Dali::ImfManager imfManager( ImfManager::Get() );
621       if ( imfManager )
622       {
623         imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
624       }
625
626       if ( imfContext )
627       {
628         // We're consuming key down event so we have to pass to IMF so that it can parse it as well.
629         Ecore_IMF_Event_Key_Down ecoreKeyDownEvent;
630         ecoreKeyDownEvent.keyname   = keyEvent->keyname;
631         ecoreKeyDownEvent.key       = keyEvent->key;
632         ecoreKeyDownEvent.string    = keyEvent->string;
633         ecoreKeyDownEvent.compose   = keyEvent->compose;
634         ecoreKeyDownEvent.timestamp = keyEvent->timestamp;
635         ecoreKeyDownEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
636         ecoreKeyDownEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
637
638         eventHandled = ecore_imf_context_filter_event( imfContext,
639                                                        ECORE_IMF_EVENT_KEY_DOWN,
640                                                        (Ecore_IMF_Event *) &ecoreKeyDownEvent );
641
642         // If the event has not been handled by IMF then check if we should reset our IMF context
643         if( !eventHandled )
644         {
645           if ( !strcmp( keyEvent->keyname, "Escape"   ) ||
646                !strcmp( keyEvent->keyname, "Return"   ) ||
647                !strcmp( keyEvent->keyname, "KP_Enter" ) )
648           {
649             ecore_imf_context_reset( imfContext );
650           }
651         }
652       }
653     }
654
655     // If the event wasn't handled then we should send a key event.
656     if ( !eventHandled )
657     {
658       if ( keyEvent->window == handler->mImpl->mWindow )
659       {
660         std::string keyName( keyEvent->keyname );
661         std::string keyString( "" );
662         int keyCode = ecore_x_keysym_keycode_get(keyEvent->keyname);
663         int modifier( keyEvent->modifiers );
664         unsigned long time = keyEvent->timestamp;
665
666         // Ensure key event string is not NULL as keys like SHIFT have a null string.
667         if ( keyEvent->string )
668         {
669           keyString = keyEvent->string;
670         }
671
672         KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, KeyEvent::Down);
673         handler->SendEvent( keyEvent );
674       }
675     }
676
677     return ECORE_CALLBACK_PASS_ON;
678   }
679
680   /**
681    * Called when a key up is received.
682    */
683   static Eina_Bool EcoreEventKeyUp( void* data, int type, void* event )
684   {
685     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyUp \n" );
686
687     EventHandler* handler( (EventHandler*)data );
688     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
689     bool eventHandled( false );
690
691     // Menu, home, back button must skip ecore_imf_context_filter_event.
692     static const char* menuKeyName = KeyLookup::GetKeyName( DALI_KEY_MENU );
693     static const char* homeKeyName = KeyLookup::GetKeyName( DALI_KEY_HOME );
694     static const char* backKeyName = KeyLookup::GetKeyName( DALI_KEY_BACK );
695     if ( ( menuKeyName && strcmp( keyEvent->keyname, menuKeyName ) != 0 ) &&
696          ( homeKeyName && strcmp( keyEvent->keyname, homeKeyName ) != 0 ) &&
697          ( backKeyName && strcmp( keyEvent->keyname, backKeyName ) != 0 ) )
698     {
699       Ecore_IMF_Context* imfContext = NULL;
700       Dali::ImfManager imfManager( ImfManager::Get() );
701       if ( imfManager )
702       {
703         imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
704       }
705
706       if ( imfContext )
707       {
708         // We're consuming key up event so we have to pass to IMF so that it can parse it as well.
709         Ecore_IMF_Event_Key_Up ecoreKeyUpEvent;
710         ecoreKeyUpEvent.keyname   = keyEvent->keyname;
711         ecoreKeyUpEvent.key       = keyEvent->key;
712         ecoreKeyUpEvent.string    = keyEvent->string;
713         ecoreKeyUpEvent.compose   = keyEvent->compose;
714         ecoreKeyUpEvent.timestamp = keyEvent->timestamp;
715         ecoreKeyUpEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
716         ecoreKeyUpEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
717
718         eventHandled = ecore_imf_context_filter_event( imfContext,
719                                                        ECORE_IMF_EVENT_KEY_UP,
720                                                        (Ecore_IMF_Event *) &ecoreKeyUpEvent );
721       }
722     }
723
724     // If the event wasn't handled then we should send a key event.
725     if ( !eventHandled )
726     {
727       if ( keyEvent->window == handler->mImpl->mWindow )
728       {
729         std::string keyName( keyEvent->keyname );
730         std::string keyString( "" );
731         int keyCode = ecore_x_keysym_keycode_get(keyEvent->keyname);
732         int modifier( keyEvent->modifiers );
733         unsigned long time( keyEvent->timestamp );
734
735         // Ensure key event string is not NULL as keys like SHIFT have a null string.
736         if ( keyEvent->string )
737         {
738           keyString = keyEvent->string;
739         }
740
741         KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, KeyEvent::Up);
742         handler->SendEvent( keyEvent );
743
744       }
745     }
746
747     return ECORE_CALLBACK_PASS_ON;
748   }
749
750   /////////////////////////////////////////////////////////////////////////////////////////////////
751   // Window Callbacks
752   /////////////////////////////////////////////////////////////////////////////////////////////////
753
754   /**
755    * Called when the window gains focus.
756    */
757   static Eina_Bool EcoreEventWindowFocusIn( void* data, int type, void* event )
758   {
759     Ecore_X_Event_Window_Focus_In* focusInEvent( (Ecore_X_Event_Window_Focus_In*)event );
760     EventHandler* handler( (EventHandler*)data );
761
762     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusIn \n" );
763
764     // If the window gains focus and we hid the keyboard then show it again.
765     if ( focusInEvent->win == handler->mImpl->mWindow )
766     {
767       DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT EcoreEventWindowFocusIn - >>WindowFocusGained \n" );
768
769       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
770       {
771         Dali::ImfManager imfManager( ImfManager::Get() );
772         if ( imfManager )
773         {
774           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
775           if( imfManagerImpl.RestoreAfterFocusLost() )
776           {
777             imfManagerImpl.Activate();
778           }
779         }
780       }
781       // No need to connect callbacks as KeyboardStatusChanged will be called.
782     }
783
784     return ECORE_CALLBACK_PASS_ON;
785   }
786
787   /**
788    * Called when the window loses focus.
789    */
790   static Eina_Bool EcoreEventWindowFocusOut( void* data, int type, void* event )
791   {
792     Ecore_X_Event_Window_Focus_Out* focusOutEvent( (Ecore_X_Event_Window_Focus_Out*)event );
793     EventHandler* handler( (EventHandler*)data );
794
795     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusOut \n" );
796
797     // If the window loses focus then hide the keyboard.
798     if ( focusOutEvent->win == handler->mImpl->mWindow )
799     {
800       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
801       {
802         Dali::ImfManager imfManager( ImfManager::Get() );
803         if ( imfManager )
804         {
805           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
806           if( imfManagerImpl.RestoreAfterFocusLost() )
807           {
808             imfManagerImpl.Deactivate();
809           }
810         }
811       }
812
813       // Clipboard don't support that whether clipboard is shown or not. Hide clipboard.
814       Dali::Clipboard clipboard = Clipboard::Get();
815       clipboard.HideClipboard();
816     }
817
818     return ECORE_CALLBACK_PASS_ON;
819   }
820
821   /**
822    * Called when the window is damaged.
823    */
824   static Eina_Bool EcoreEventWindowDamaged(void *data, int type, void *event)
825   {
826     Ecore_X_Event_Window_Damage* windowDamagedEvent( (Ecore_X_Event_Window_Damage*)event );
827     EventHandler* handler( (EventHandler*)data );
828
829     if( windowDamagedEvent->win == handler->mImpl->mWindow )
830     {
831       DamageArea area;
832       area.x = windowDamagedEvent->x;
833       area.y = windowDamagedEvent->y;
834       area.width = windowDamagedEvent->w;
835       area.height = windowDamagedEvent->h;
836
837       handler->SendEvent( area );
838     }
839
840     return ECORE_CALLBACK_PASS_ON;
841   }
842
843   /**
844    * Called when the window properties are changed.
845    * We are only interested in the font change.
846    */
847
848
849   /////////////////////////////////////////////////////////////////////////////////////////////////
850   // Drag & Drop Callbacks
851   /////////////////////////////////////////////////////////////////////////////////////////////////
852
853   /**
854    * Called when a dragged item enters our window's bounds.
855    * This is when items are dragged INTO our window.
856    */
857   static Eina_Bool EcoreEventDndEnter( void* data, int type, void* event )
858   {
859     DALI_LOG_INFO( gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndEnter\n" );
860
861     Ecore_X_Event_Xdnd_Enter* enterEvent( (Ecore_X_Event_Xdnd_Enter*) event );
862     EventHandler* handler( (EventHandler*)data );
863     Ecore_X_Window window ( handler->mImpl->mWindow );
864
865     if ( enterEvent->win == window )
866     {
867       DragAndDropDetectorPtr dndDetector( handler->mDragAndDropDetector );
868
869       // Check whether the Drag & Drop detector has Drag & Drop behaviour enabled before we accept.
870       if ( dndDetector && dndDetector->IsEnabled() )
871       {
872         // Tell Ecore that we want to enable drop in the entire window.
873         Ecore_X_Rectangle rect;
874         rect.x = rect.y = 0;
875         ecore_x_window_geometry_get( window, NULL, NULL, (int*)&rect.width, (int*)&rect.height );
876
877         // Tell Ecore that we are able to process a drop.
878         ecore_x_dnd_send_status( EINA_TRUE, EINA_FALSE, rect, ECORE_X_ATOM_XDND_DROP );
879
880         // Register the required atoms and types.
881         ecore_x_dnd_actions_set( window, DRAG_AND_DROP_ATOMS, DRAG_AND_DROP_ATOMS_NUMBER );
882         ecore_x_dnd_types_set(   window, DRAG_AND_DROP_TYPES, DRAG_AND_DROP_TYPES_NUMBER );
883
884         // Request to get the content from Ecore.
885         ecore_x_selection_xdnd_request( window, ECORE_X_SELECTION_TARGET_UTF8_STRING );
886
887         DALI_LOG_INFO( gDragAndDropLogFilter, Debug::General, "EcoreEventDndEnter: Requesting Drag & Drop\n" );
888
889         // Clear the previous content
890         dndDetector->ClearContent();
891
892         // Emit the entered signal
893         dndDetector->EmitEnteredSignal();
894       }
895     }
896
897     return ECORE_CALLBACK_PASS_ON;
898   }
899
900   /**
901    * Called when a dragged item is moved within our window.
902    * This is when items are dragged INTO our window.
903    */
904   static Eina_Bool EcoreEventDndPosition( void* data, int type, void* event )
905   {
906     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndPosition\n" );
907
908     Ecore_X_Event_Xdnd_Position* positionEvent( (Ecore_X_Event_Xdnd_Position*) event );
909     EventHandler* handler( (EventHandler*)data );
910
911     if ( positionEvent->win == handler->mImpl->mWindow )
912     {
913       DragAndDropDetectorPtr dndDetector( handler->mDragAndDropDetector );
914
915       // If we have a detector then update its latest position.
916       if ( dndDetector )
917       {
918         DALI_LOG_INFO(gDragAndDropLogFilter, Debug::General, "EcoreEventDndPosition: position ( %d x %d )\n", positionEvent->position.x, positionEvent->position.y );
919         dndDetector->SetPosition( Vector2( positionEvent->position.x, positionEvent->position.y ));
920         dndDetector->EmitMovedSignal();
921       }
922     }
923
924     return ECORE_CALLBACK_PASS_ON;
925   }
926
927   /**
928    * Called when a dragged item leaves our window's bounds.
929    * This is when items are dragged INTO our window.
930    */
931   static Eina_Bool EcoreEventDndLeave( void* data, int type, void* event )
932   {
933     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndLeave\n" );
934
935     Ecore_X_Event_Xdnd_Leave* leaveEvent( (Ecore_X_Event_Xdnd_Leave*) event );
936     EventHandler* handler( (EventHandler*)data );
937
938     if ( leaveEvent->win == handler->mImpl->mWindow )
939     {
940       DragAndDropDetectorPtr dndDetector( handler->mDragAndDropDetector );
941
942       // If we have a detector then clear its content and emit the exited-signal. Also tell Ecore that we have finished.
943       if ( dndDetector )
944       {
945         dndDetector->ClearContent();
946         dndDetector->EmitExitedSignal();
947
948         ecore_x_dnd_send_finished();
949
950         DALI_LOG_INFO( gDragAndDropLogFilter, Debug::General, "EcoreEventDndLeave: Finished\n" );
951       }
952     }
953
954     return ECORE_CALLBACK_PASS_ON;
955   }
956
957   /**
958    * Called when the dragged item is dropped within our window's bounds.
959    * This is when items are dragged INTO our window.
960    */
961   static Eina_Bool EcoreEventDndDrop( void* data, int type, void* event )
962   {
963     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndDrop\n" );
964
965     Ecore_X_Event_Xdnd_Drop* dropEvent ( (Ecore_X_Event_Xdnd_Drop*) event);
966     EventHandler* handler( (EventHandler*)data );
967
968     if ( dropEvent->win == handler->mImpl->mWindow )
969     {
970       DragAndDropDetectorPtr dndDetector( handler->mDragAndDropDetector );
971
972       // Something has been dropped, inform the detector (if we have one) and tell Ecore that we have finished.
973       if ( dndDetector )
974       {
975         DALI_LOG_INFO(gDragAndDropLogFilter, Debug::General, "EcoreEventDndDrop: position ( %d x %d )\n", dropEvent->position.x, dropEvent->position.y );
976
977         dndDetector->SetPosition( Vector2( dropEvent->position.x, dropEvent->position.y ) );
978         dndDetector->EmitDroppedSignal();
979         ecore_x_dnd_send_finished();
980
981         DALI_LOG_INFO( gDragAndDropLogFilter, Debug::General, "EcoreEventDndDrop: Finished\n" );
982       }
983     }
984
985     return ECORE_CALLBACK_PASS_ON;
986   }
987
988   /**
989    * Called when a dragged item is moved from our window and the target window has done processing it.
990    * This is when items are dragged FROM our window.
991    */
992   static Eina_Bool EcoreEventDndFinished( void* data, int type, void* event )
993   {
994     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndFinished\n" );
995     return ECORE_CALLBACK_PASS_ON;
996   }
997
998   /**
999    * Called when a dragged item is moved from our window and the target window has sent us a status.
1000    * This is when items are dragged FROM our window.
1001    */
1002   static Eina_Bool EcoreEventDndStatus( void* data, int type, void* event )
1003   {
1004     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndStatus\n" );
1005     return ECORE_CALLBACK_PASS_ON;
1006   }
1007
1008   /**
1009    * Called when the client messages (i.e. the accessibility events) are received.
1010    */
1011   static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
1012   {
1013 #ifndef DALI_PROFILE_UBUNTU
1014     Ecore_X_Event_Client_Message* clientMessageEvent( (Ecore_X_Event_Client_Message*)event );
1015     EventHandler* handler( (EventHandler*)data );
1016
1017     if (clientMessageEvent->message_type == ECORE_X_ATOM_E_ILLUME_ACCESS_CONTROL)
1018     {
1019       if ( ( (unsigned int)clientMessageEvent->data.l[0] == handler->mImpl->mWindow ) && handler->mAccessibilityAdaptor )
1020       {
1021         AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( handler->mAccessibilityAdaptor ) );
1022
1023         if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_SCROLL)
1024         {
1025           // 2 finger touch & move, 2 finger flick
1026
1027           // mouse state : e->data.l[2] (0: mouse down, 1: mouse move, 2: mouse up)
1028           // x : e->data.l[3]
1029           // y : e->data.l[4]
1030           TouchPoint::State state(TouchPoint::Down);
1031
1032           if ((unsigned int)clientMessageEvent->data.l[2] == 0)
1033           {
1034             state = TouchPoint::Down; // mouse down
1035           }
1036           else if ((unsigned int)clientMessageEvent->data.l[2] == 1)
1037           {
1038             state = TouchPoint::Motion; // mouse move
1039           }
1040           else if ((unsigned int)clientMessageEvent->data.l[2] == 2)
1041           {
1042             state = TouchPoint::Up; // mouse up
1043           }
1044           else
1045           {
1046             state = TouchPoint::Interrupted; // error
1047           }
1048
1049           DALI_LOG_INFO(gClientMessageLogFilter, Debug::General,
1050             "[%s:%d] [%d] %d, %d\n", __FUNCTION__, __LINE__,
1051             (unsigned int)clientMessageEvent->data.l[2],
1052             (unsigned int)clientMessageEvent->data.l[3], (unsigned int)clientMessageEvent->data.l[4]);
1053
1054           // Send touch event to accessibility adaptor.
1055           TouchPoint point( 0, state, (float)clientMessageEvent->data.l[3], (float)clientMessageEvent->data.l[4] );
1056
1057           // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
1058           accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
1059         }
1060         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_MOUSE)
1061         {
1062           // 1 finger double tap and hold
1063
1064           // mouse state : e->data.l[2] (0: mouse down, 1: mouse move, 2: mouse up)
1065           // x : e->data.l[3]
1066           // y : e->data.l[4]
1067           TouchPoint::State state(TouchPoint::Down);
1068
1069           if ((unsigned int)clientMessageEvent->data.l[2] == 0)
1070           {
1071             state = TouchPoint::Down; // mouse down
1072           }
1073           else if ((unsigned int)clientMessageEvent->data.l[2] == 1)
1074           {
1075             state = TouchPoint::Motion; // mouse move
1076           }
1077           else if ((unsigned int)clientMessageEvent->data.l[2] == 2)
1078           {
1079             state = TouchPoint::Up; // mouse up
1080           }
1081           else
1082           {
1083             state = TouchPoint::Interrupted; // error
1084           }
1085
1086           DALI_LOG_INFO(gClientMessageLogFilter, Debug::General,
1087             "[%s:%d] [%d] %d, %d\n", __FUNCTION__, __LINE__,
1088             (unsigned int)clientMessageEvent->data.l[2],
1089             (unsigned int)clientMessageEvent->data.l[3], (unsigned int)clientMessageEvent->data.l[4]);
1090
1091           // Send touch event to accessibility adaptor.
1092           TouchPoint point( 0, state, (float)clientMessageEvent->data.l[3], (float)clientMessageEvent->data.l[4] );
1093
1094           // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
1095           accessibilityAdaptor->HandleActionTouchEvent( point, GetCurrentMilliSeconds() );
1096         }
1097         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_BACK)
1098         {
1099           // 2 finger circle draw, do back
1100           accessibilityAdaptor->HandleActionBackEvent();
1101         }
1102         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_NEXT)
1103         {
1104           // one finger flick down
1105           // focus next object
1106           if(accessibilityAdaptor)
1107           {
1108             accessibilityAdaptor->HandleActionNextEvent();
1109           }
1110         }
1111         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_PREV)
1112         {
1113           // one finger flick up
1114           // focus previous object
1115           if(accessibilityAdaptor)
1116           {
1117             accessibilityAdaptor->HandleActionPreviousEvent();
1118           }
1119         }
1120         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_ACTIVATE)
1121         {
1122           // one finger double tap
1123           // same as one finger tap in normal mode (i.e. execute focused actor)
1124           if(accessibilityAdaptor)
1125           {
1126             accessibilityAdaptor->HandleActionActivateEvent();
1127           }
1128         }
1129         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_READ)
1130         {
1131           // one finger tap
1132           // focus & read an actor at ( e->data.l[2], e->data.l[3] ) position according to finger
1133           if(accessibilityAdaptor)
1134           {
1135             accessibilityAdaptor->HandleActionReadEvent((unsigned int)clientMessageEvent->data.l[2], (unsigned int)clientMessageEvent->data.l[3], true /* allow read again*/);
1136           }
1137         }
1138 #if defined(DALI_PROFILE_MOBILE)
1139         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_OVER)
1140         {
1141           // one finger tap & move
1142           // mouse state : e->data.l[2] (0: mouse down, 1: mouse move, 2: mouse up)
1143           // x : e->data.l[3]
1144           // y : e->data.l[4]
1145           // focus & read an actor at (x, y) position according to finger
1146           if(accessibilityAdaptor && (unsigned int)clientMessageEvent->data.l[2] == 1 /*only work for move event*/)
1147           {
1148             accessibilityAdaptor->HandleActionReadEvent((unsigned int)clientMessageEvent->data.l[3], (unsigned int)clientMessageEvent->data.l[4], false /* not allow read again*/);
1149           }
1150         }
1151 #endif
1152         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_READ_NEXT)
1153         {
1154           // one finger flick right
1155           // focus next object
1156            if(accessibilityAdaptor)
1157           {
1158             accessibilityAdaptor->HandleActionReadNextEvent();
1159           }
1160         }
1161         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_READ_PREV)
1162         {
1163           // one finger flick left
1164           // focus previous object
1165           if(accessibilityAdaptor)
1166           {
1167             accessibilityAdaptor->HandleActionReadPreviousEvent();
1168           }
1169         }
1170         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_UP)
1171         {
1172           // double down and move (right, up)
1173           // change slider value
1174           if(accessibilityAdaptor)
1175           {
1176             accessibilityAdaptor->HandleActionUpEvent();
1177           }
1178         }
1179         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_DOWN)
1180         {
1181           // double down and move (left, down)
1182           // change slider value
1183           if(accessibilityAdaptor)
1184           {
1185             accessibilityAdaptor->HandleActionDownEvent();
1186           }
1187         }
1188         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_ENABLE)
1189         {
1190            if(accessibilityAdaptor)
1191           {
1192             accessibilityAdaptor->HandleActionEnableEvent();
1193           }
1194         }
1195         else if((unsigned int)clientMessageEvent->data.l[1] == ECORE_X_ATOM_E_ILLUME_ACCESS_ACTION_DISABLE)
1196         {
1197           if(accessibilityAdaptor)
1198           {
1199             accessibilityAdaptor->HandleActionDisableEvent();
1200           }
1201         }
1202         // TODO: some more actions could be added later
1203       }
1204     }
1205     else if(clientMessageEvent->message_type == ecore_x_atom_get(CLIPBOARD_ATOM))
1206     {
1207       std::string message(clientMessageEvent->data.b);
1208       if( message == CLIPBOARD_SET_OWNER_MESSAGE)
1209       {
1210         // Claim the ownership of the SECONDARY selection.
1211         ecore_x_selection_secondary_set(handler->mImpl->mWindow, "", 1);
1212
1213         // Show the clipboard window
1214         Dali::Clipboard clipboard = Dali::Clipboard::Get();
1215         clipboard.ShowClipboard();
1216       }
1217     }
1218     else if( clientMessageEvent->message_type == ECORE_X_ATOM_E_WINDOW_ROTATION_CHANGE_PREPARE )
1219     {
1220       RotationEvent rotationEvent;
1221       rotationEvent.angle      = static_cast<int>(clientMessageEvent->data.l[1]);
1222       rotationEvent.winResize  = static_cast<int>(clientMessageEvent->data.l[2]);
1223       rotationEvent.width      = static_cast<int>(clientMessageEvent->data.l[3]);
1224       rotationEvent.height     = static_cast<int>(clientMessageEvent->data.l[4]);
1225       handler->SendRotationPrepareEvent( rotationEvent );
1226     }
1227     else if( clientMessageEvent->message_type == ECORE_X_ATOM_E_WINDOW_ROTATION_CHANGE_REQUEST )
1228     {
1229       handler->SendRotationRequestEvent();
1230     }
1231
1232 #endif // DALI_PROFILE_UBUNTU
1233     return ECORE_CALLBACK_PASS_ON;
1234   }
1235
1236
1237   /////////////////////////////////////////////////////////////////////////////////////////////////
1238   // ElDBus Accessibility Callbacks
1239   /////////////////////////////////////////////////////////////////////////////////////////////////
1240
1241 #ifdef DALI_ELDBUS_AVAILABLE
1242   // Callback for Ecore ElDBus accessibility events.
1243   static void OnEcoreElDBusAccessibilityNotification( void *context EINA_UNUSED, const Eldbus_Message *message )
1244   {
1245     EventHandler* handler( (EventHandler*)context );
1246
1247     if ( !handler->mAccessibilityAdaptor )
1248     {
1249       DALI_LOG_ERROR( "Invalid accessibility adaptor\n" );
1250       return;
1251     }
1252
1253     AccessibilityAdaptor* accessibilityAdaptor( &AccessibilityAdaptor::GetImplementation( handler->mAccessibilityAdaptor ) );
1254     if ( !accessibilityAdaptor )
1255     {
1256       DALI_LOG_ERROR( "Cannot access accessibility adaptor\n" );
1257       return;
1258     }
1259
1260     const char *gestureName;
1261     int xS, yS, xE, yE;
1262     unsigned int state;
1263
1264     // The string defines the arg-list's respective types.
1265     if( !eldbus_message_arguments_get( message, "siiiiu", &gestureName, &xS, &yS, &xE, &yE, &state ) )
1266     {
1267       DALI_LOG_ERROR( "OnEcoreElDBusAccessibilityNotification: Error getting arguments\n" );
1268     }
1269
1270     DALI_LOG_INFO( gImfLogging, Debug::General, "Got gesture: Name: %s  Args: %d,%d,%d,%d  State: %d\n", gestureName, xS, yS, xE, yE );
1271
1272     unsigned int fingers = 0;
1273     char* stringPosition = ( char* )gestureName;
1274
1275     // Check how many fingers the gesture uses.
1276     for( unsigned int i = 0; i < FingerCountStringsTotal; ++i )
1277     {
1278       unsigned int matchLength = strlen( ElDBusAccessibilityFingerCountStrings[ i ] );
1279       if( strncmp( gestureName, ElDBusAccessibilityFingerCountStrings[ i ], matchLength ) == 0 )
1280       {
1281         fingers = i + 1;
1282         stringPosition += matchLength;
1283         break;
1284       }
1285     }
1286
1287     if( fingers == 0 )
1288     {
1289       // Error: invalid gesture.
1290       return;
1291     }
1292
1293     GestureType gestureType = GESTURE_TYPE_NONE;
1294     SubGestureType subGestureType = SUB_GESTURE_TYPE_NONE;
1295     GestureDirection direction = GESTURE_DIRECTION_NONE;
1296
1297     // Check for full gesture type names first.
1298     for( unsigned int i = 0; i < FullEventTypeStringsTotal; ++i )
1299     {
1300       unsigned int matchLength = strlen( ElDBusAccessibilityFullEventTypeStrings[ i ].name );
1301       if( strncmp( stringPosition, ElDBusAccessibilityFullEventTypeStrings[ i ].name, matchLength ) == 0 )
1302       {
1303         gestureType = ElDBusAccessibilityFullEventTypeStrings[ i ].type;
1304         break;
1305       }
1306     }
1307
1308     // If we didn't find a full gesture, check for sub gesture type names.
1309     if( gestureType == GESTURE_TYPE_NONE )
1310     {
1311       // No full gesture name found, look for partial types.
1312       for( unsigned int i = 0; i < DirectionalEventTypeStringsTotal; ++i )
1313       {
1314         unsigned int matchLength = strlen( ElDBusAccessibilityDirectionalEventTypeStrings[ i ].name );
1315         if( strncmp( stringPosition, ElDBusAccessibilityDirectionalEventTypeStrings[ i ].name, matchLength ) == 0 )
1316         {
1317           subGestureType = ElDBusAccessibilityDirectionalEventTypeStrings[ i ].type;
1318           stringPosition += matchLength;
1319         break;
1320         }
1321       }
1322
1323       if( subGestureType == SUB_GESTURE_TYPE_NONE )
1324       {
1325         // ERROR: Gesture not recognised.
1326         return;
1327       }
1328
1329       // If the gesture was a sub type, get it's respective direction.
1330       for( unsigned int i = 0; i < DirectionStringsTotal; ++i )
1331       {
1332         unsigned int matchLength = strlen( ElDBusAccessibilityDirectionStrings[ i ].name );
1333         if( strncmp( stringPosition, ElDBusAccessibilityDirectionStrings[ i ].name, matchLength ) == 0 )
1334         {
1335           direction = ElDBusAccessibilityDirectionStrings[ i ].direction;
1336           stringPosition += matchLength;
1337           break;
1338         }
1339       }
1340
1341       if( direction == GESTURE_DIRECTION_NONE )
1342       {
1343         // ERROR: Gesture not recognised.
1344         return;
1345       }
1346     }
1347
1348     // Action the detected gesture here.
1349     if( gestureType != GESTURE_TYPE_NONE )
1350     {
1351       DALI_LOG_INFO( gImfLogging, Debug::General, "Got gesture: Fingers: %d  Gesture type: %d\n", fingers, gestureType );
1352     }
1353     else
1354     {
1355       DALI_LOG_INFO( gImfLogging, Debug::General, "Got gesture: Fingers: %d  Gesture sub type: %d Gesture direction: %d\n",
1356         fingers, subGestureType, direction );
1357     }
1358
1359     // Create a touch point object.
1360     TouchPoint::State touchPointState( TouchPoint::Down );
1361     if ( state == 0 )
1362     {
1363       touchPointState = TouchPoint::Down; // Mouse down.
1364     }
1365     else if ( state == 1 )
1366     {
1367       touchPointState = TouchPoint::Motion; // Mouse move.
1368     }
1369     else if ( state == 2 )
1370     {
1371       touchPointState = TouchPoint::Up; // Mouse up.
1372     }
1373     else
1374     {
1375       touchPointState = TouchPoint::Interrupted; // Error.
1376     }
1377
1378     // Send touch event to accessibility adaptor.
1379     TouchPoint point( 0, touchPointState, (float)xS, (float)yS );
1380
1381     // Perform actions based on received gestures.
1382     // Note: This is seperated from the reading so we can (in future)
1383     // have other input readers without changing the below code.
1384     switch( fingers )
1385     {
1386       case 1:
1387       {
1388         if( gestureType == GESTURE_TYPE_SINGLE_TAP || ( gestureType == GESTURE_TYPE_HOVER && touchPointState == TouchPoint::Motion ) )
1389         {
1390           // Focus, read out.
1391           accessibilityAdaptor->HandleActionReadEvent( (unsigned int)xS, (unsigned int)yS, true /* allow read again */ );
1392         }
1393         else if( gestureType == GESTURE_TYPE_DOUBLE_TAP )
1394         {
1395           if( false ) // TODO: how to detect double tap + hold?
1396           {
1397             // Move or drag icon / view more options for selected items.
1398             // accessibilityAdaptor->HandleActionTouchEvent( point, GetCurrentMilliSeconds() );
1399           }
1400           else
1401           {
1402             // Activate selected item / active edit mode.
1403             accessibilityAdaptor->HandleActionActivateEvent();
1404           }
1405         }
1406         else if( gestureType == GESTURE_TYPE_TRIPLE_TAP )
1407         {
1408           // Zoom
1409           accessibilityAdaptor->HandleActionZoomEvent();
1410         }
1411         else if( subGestureType == SUB_GESTURE_TYPE_FLICK )
1412         {
1413           if( direction == GESTURE_DIRECTION_LEFT )
1414           {
1415             // Move to previous item.
1416             accessibilityAdaptor->HandleActionReadPreviousEvent();
1417           }
1418           else if( direction == GESTURE_DIRECTION_RIGHT )
1419           {
1420             // Move to next item.
1421             accessibilityAdaptor->HandleActionReadNextEvent();
1422           }
1423           else if( direction == GESTURE_DIRECTION_UP )
1424           {
1425             // Move to next item.
1426             accessibilityAdaptor->HandleActionPreviousEvent();
1427           }
1428           else if( direction == GESTURE_DIRECTION_DOWN )
1429           {
1430             // Move to next item.
1431             accessibilityAdaptor->HandleActionNextEvent();
1432           }
1433           else if( direction == GESTURE_DIRECTION_LEFT_RETURN )
1434           {
1435             // Scroll up to the previous page
1436             accessibilityAdaptor->HandleActionPageUpEvent();
1437           }
1438           else if( direction == GESTURE_DIRECTION_RIGHT_RETURN )
1439           {
1440             // Scroll down to the next page
1441             accessibilityAdaptor->HandleActionPageDownEvent();
1442           }
1443           else if( direction == GESTURE_DIRECTION_UP_RETURN )
1444           {
1445             // Move to the first item on screen
1446             accessibilityAdaptor->HandleActionMoveToFirstEvent();
1447           }
1448           else if( direction == GESTURE_DIRECTION_DOWN_RETURN )
1449           {
1450             // Move to the last item on screen
1451             accessibilityAdaptor->HandleActionMoveToLastEvent();
1452           }
1453         }
1454         break;
1455       }
1456
1457       case 2:
1458       {
1459         if( gestureType == GESTURE_TYPE_HOVER )
1460         {
1461           // In accessibility mode, scroll action should be handled when the currently focused actor is contained in scrollable control
1462           accessibilityAdaptor->HandleActionScrollEvent( point, GetCurrentMilliSeconds() );
1463         }
1464         else if( gestureType == GESTURE_TYPE_SINGLE_TAP )
1465         {
1466           // Pause/Resume current speech
1467           accessibilityAdaptor->HandleActionReadPauseResumeEvent();
1468         }
1469         else if( gestureType == GESTURE_TYPE_DOUBLE_TAP )
1470         {
1471           // Start/Stop current action
1472           accessibilityAdaptor->HandleActionStartStopEvent();
1473         }
1474         else if( gestureType == GESTURE_TYPE_TRIPLE_TAP )
1475         {
1476           // Read information from indicator
1477           accessibilityAdaptor->HandleActionReadIndicatorInformationEvent();
1478         }
1479         else if( subGestureType == SUB_GESTURE_TYPE_FLICK )
1480         {
1481           if( direction == GESTURE_DIRECTION_LEFT )
1482           {
1483             // Scroll left to the previous page
1484             accessibilityAdaptor->HandleActionPageLeftEvent();
1485           }
1486           else if( direction == GESTURE_DIRECTION_RIGHT )
1487           {
1488             // Scroll right to the next page
1489             accessibilityAdaptor->HandleActionPageRightEvent();
1490           }
1491           else if( direction == GESTURE_DIRECTION_UP )
1492           {
1493             // Scroll up the list.
1494             accessibilityAdaptor->HandleActionScrollUpEvent();
1495           }
1496           else if( direction == GESTURE_DIRECTION_DOWN )
1497           {
1498             // Scroll down the list.
1499             accessibilityAdaptor->HandleActionScrollDownEvent();
1500           }
1501         }
1502         break;
1503       }
1504
1505       case 3:
1506       {
1507         if( gestureType == GESTURE_TYPE_SINGLE_TAP )
1508         {
1509           // Read from top item on screen continuously.
1510           accessibilityAdaptor->HandleActionReadFromTopEvent();
1511         }
1512         else if( gestureType == GESTURE_TYPE_DOUBLE_TAP )
1513         {
1514           // Read from next item continuously.
1515           accessibilityAdaptor->HandleActionReadFromNextEvent();
1516         }
1517         break;
1518       }
1519     }
1520   }
1521
1522   // Callback for to set up Ecore ElDBus for accessibility callbacks.
1523   static void EcoreElDBusInitialisation( void *handle, const Eldbus_Message *message, Eldbus_Pending *pending EINA_UNUSED )
1524   {
1525     Eldbus_Object *object;
1526     Eldbus_Proxy *manager;
1527     const char *a11yBusAddress = NULL;
1528
1529     // The string defines the arg-list's respective types.
1530     if( !eldbus_message_arguments_get( message, "s", &a11yBusAddress ) )
1531     {
1532       DALI_LOG_ERROR( "EcoreElDBusInitialisation: Error getting arguments\n" );
1533     }
1534
1535     DALI_LOG_INFO( gImfLogging, Debug::General, "Ecore ElDBus Accessibility address: %s\n", a11yBusAddress );
1536
1537     Eldbus_Connection *a11yConnection = eldbus_address_connection_get( a11yBusAddress );
1538
1539     object = eldbus_object_get( a11yConnection, BUS, PATH );
1540     manager = eldbus_proxy_get( object, INTERFACE );
1541
1542     // Pass the callback data through to the signal handler.
1543     eldbus_proxy_signal_handler_add( manager, SIGNAL, OnEcoreElDBusAccessibilityNotification, handle );
1544   }
1545 #endif // DALI_ELDBUS_AVAILABLE
1546
1547   /**
1548    * Called when the source window notifies us the content in clipboard is selected.
1549    */
1550   static Eina_Bool EcoreEventSelectionClear( void* data, int type, void* event )
1551   {
1552     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionClear\n" );
1553     Ecore_X_Event_Selection_Clear* selectionClearEvent( (Ecore_X_Event_Selection_Clear*) event );
1554     EventHandler* handler( (EventHandler*)data );
1555
1556     if ( selectionClearEvent->win == handler->mImpl->mWindow )
1557     {
1558       if ( selectionClearEvent->selection == ECORE_X_SELECTION_SECONDARY )
1559       {
1560         // Request to get the content from Ecore.
1561         ecore_x_selection_secondary_request(selectionClearEvent->win, ECORE_X_SELECTION_TARGET_TEXT);
1562       }
1563     }
1564     return ECORE_CALLBACK_PASS_ON;
1565   }
1566
1567   /**
1568    * Called when the source window sends us about the selected content.
1569    * For example, when dragged items are dragged INTO our window or when items are selected in the clipboard.
1570    */
1571   static Eina_Bool EcoreEventSelectionNotify( void* data, int type, void* event )
1572   {
1573     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionNotify\n" );
1574
1575     Ecore_X_Event_Selection_Notify* selectionNotifyEvent( (Ecore_X_Event_Selection_Notify*) event );
1576     EventHandler* handler( (EventHandler*)data );
1577
1578     if ( selectionNotifyEvent->win == handler->mImpl->mWindow )
1579     {
1580       Ecore_X_Selection_Data* selectionData( (Ecore_X_Selection_Data*) selectionNotifyEvent->data );
1581       if ( selectionData->data )
1582       {
1583         if ( selectionNotifyEvent->selection == ECORE_X_SELECTION_XDND )
1584         {
1585           DragAndDropDetectorPtr dndDetector( handler->mDragAndDropDetector );
1586
1587           // We have got the content that is to be dropped, inform the DndListener (if we have one).
1588           if ( dndDetector )
1589           {
1590             std::string content( (char*) selectionData->data, selectionData->length );
1591             dndDetector->SetContent( content );
1592
1593             DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d):\n" , selectionData->length );
1594             DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "======================================\n" );
1595             DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "%s\n", selectionData->data );
1596             DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "======================================\n" );
1597           }
1598         }
1599         else if ( selectionNotifyEvent->selection == ECORE_X_SELECTION_SECONDARY )
1600         {
1601           // We have got the selected content, inform the clipboard event listener (if we have one).
1602           if ( handler->mClipboardEventNotifier )
1603           {
1604             ClipboardEventNotifier& clipboardEventNotifier( ClipboardEventNotifier::GetImplementation( handler->mClipboardEventNotifier ) );
1605             std::string content( (char*) selectionData->data, selectionData->length );
1606             clipboardEventNotifier.SetContent( content );
1607             clipboardEventNotifier.EmitContentSelectedSignal();
1608           }
1609
1610           // Claim the ownership of the SECONDARY selection.
1611           ecore_x_selection_secondary_set(handler->mImpl->mWindow, "", 1);
1612
1613           DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "EcoreEventSelectionNotify: Content(%d):\n" , selectionData->length );
1614           DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "======================================\n" );
1615           DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "%s\n", selectionData->data );
1616           DALI_LOG_INFO( gSelectionEventLogFilter, Debug::General, "======================================\n" );
1617         }
1618       }
1619     }
1620     return ECORE_CALLBACK_PASS_ON;
1621   }
1622
1623
1624 #ifndef DALI_PROFILE_UBUNTU
1625   /////////////////////////////////////////////////////////////////////////////////////////////////
1626   // Font Callbacks
1627   /////////////////////////////////////////////////////////////////////////////////////////////////
1628   /**
1629    * Called when a font name is changed.
1630    */
1631   static void VconfNotifyFontNameChanged( keynode_t* node, void* data )
1632   {
1633     EventHandler* handler = static_cast<EventHandler*>( data );
1634     handler->SendEvent( StyleChange::DEFAULT_FONT_CHANGE );
1635   }
1636
1637   /**
1638    * Called when a font size is changed.
1639    */
1640   static void VconfNotifyFontSizeChanged( keynode_t* node, void* data )
1641   {
1642     EventHandler* handler = static_cast<EventHandler*>( data );
1643     handler->SendEvent( StyleChange::DEFAULT_FONT_SIZE_CHANGE );
1644   }
1645 #endif // DALI_PROFILE_UBUNTU
1646
1647   // Data
1648   EventHandler* mHandler;
1649   std::vector<Ecore_Event_Handler*> mEcoreEventHandler;
1650   Ecore_X_Window mWindow;
1651   int mXiDeviceId;
1652 };
1653
1654 EventHandler::EventHandler( RenderSurface* surface, CoreEventInterface& coreEventInterface, GestureManager& gestureManager, DamageObserver& damageObserver, DragAndDropDetectorPtr dndDetector )
1655 : mCoreEventInterface(coreEventInterface),
1656   mGestureManager( gestureManager ),
1657   mStyleMonitor( StyleMonitor::Get() ),
1658   mDamageObserver( damageObserver ),
1659   mRotationObserver( NULL ),
1660   mDragAndDropDetector( dndDetector ),
1661   mAccessibilityAdaptor( AccessibilityAdaptor::Get() ),
1662   mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
1663   mClipboard(Clipboard::Get()),
1664   mImpl( NULL )
1665 {
1666   Ecore_X_Window window = 0;
1667
1668   // this code only works with the EcoreX11 RenderSurface so need to downcast
1669   ECore::WindowRenderSurface* ecoreSurface = dynamic_cast< ECore::WindowRenderSurface* >( surface );
1670   if( ecoreSurface )
1671   {
1672     // enable multi touch
1673     window = ecoreSurface->GetXWindow();
1674   }
1675
1676   mImpl = new Impl(this, window);
1677 }
1678
1679 EventHandler::~EventHandler()
1680 {
1681   if(mImpl)
1682   {
1683     delete mImpl;
1684   }
1685
1686   mGestureManager.Stop();
1687 }
1688
1689 void EventHandler::SendEvent(TouchPoint& point, unsigned long timeStamp)
1690 {
1691   if(timeStamp < 1)
1692   {
1693     timeStamp = GetCurrentMilliSeconds();
1694   }
1695
1696   Integration::TouchEvent touchEvent;
1697   Integration::HoverEvent hoverEvent;
1698   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
1699   if(type != Integration::TouchEventCombiner::DispatchNone )
1700   {
1701     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);
1702
1703     // First the touch and/or hover event & related gesture events are queued
1704     if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
1705     {
1706       mCoreEventInterface.QueueCoreEvent( touchEvent );
1707       mGestureManager.SendEvent(touchEvent);
1708     }
1709
1710     if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
1711     {
1712       mCoreEventInterface.QueueCoreEvent( hoverEvent );
1713     }
1714
1715     // Next the events are processed with a single call into Core
1716     mCoreEventInterface.ProcessCoreEvents();
1717   }
1718 }
1719
1720 void EventHandler::SendEvent(KeyEvent& keyEvent)
1721 {
1722   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
1723   if ( physicalKeyboard )
1724   {
1725     if ( ! KeyLookup::IsDeviceButton( keyEvent.keyPressedName.c_str() ) )
1726     {
1727       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
1728     }
1729   }
1730
1731   // Create KeyEvent and send to Core.
1732   Integration::KeyEvent event(keyEvent.keyPressedName, keyEvent.keyPressed, keyEvent.keyCode,
1733   keyEvent.keyModifier, keyEvent.time, static_cast<Integration::KeyEvent::State>(keyEvent.state));
1734   mCoreEventInterface.QueueCoreEvent( event );
1735   mCoreEventInterface.ProcessCoreEvents();
1736 }
1737
1738 void EventHandler::SendWheelEvent( WheelEvent& wheelEvent )
1739 {
1740   // Create WheelEvent and send to Core.
1741   Integration::WheelEvent event( static_cast< Integration::WheelEvent::Type >(wheelEvent.type), wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp );
1742   mCoreEventInterface.QueueCoreEvent( event );
1743   mCoreEventInterface.ProcessCoreEvents();
1744 }
1745
1746 void EventHandler::SendEvent( StyleChange::Type styleChange )
1747 {
1748   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
1749   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
1750 }
1751
1752 void EventHandler::SendEvent( const DamageArea& area )
1753 {
1754   mDamageObserver.OnDamaged( area );
1755 }
1756
1757 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
1758 {
1759   if( mRotationObserver != NULL )
1760   {
1761     mRotationObserver->OnRotationPrepare( event );
1762   }
1763 }
1764
1765 void EventHandler::SendRotationRequestEvent( )
1766 {
1767   if( mRotationObserver != NULL )
1768   {
1769     mRotationObserver->OnRotationRequest( );
1770   }
1771 }
1772
1773 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
1774 {
1775   SendEvent(point, timeStamp);
1776 }
1777
1778 void EventHandler::FeedWheelEvent( WheelEvent& wheelEvent )
1779 {
1780   SendWheelEvent( wheelEvent );
1781 }
1782
1783 void EventHandler::FeedKeyEvent( KeyEvent& event )
1784 {
1785   SendEvent( event );
1786 }
1787
1788 void EventHandler::FeedEvent( Integration::Event& event )
1789 {
1790   mCoreEventInterface.QueueCoreEvent( event );
1791   mCoreEventInterface.ProcessCoreEvents();
1792 }
1793
1794 void EventHandler::Reset()
1795 {
1796   mCombiner.Reset();
1797
1798   // Any touch listeners should be told of the interruption.
1799   Integration::TouchEvent event;
1800   TouchPoint point(0, TouchPoint::Interrupted, 0, 0);
1801   event.AddPoint( point );
1802
1803   // First the touch event & related gesture events are queued
1804   mCoreEventInterface.QueueCoreEvent( event );
1805   mGestureManager.SendEvent( event );
1806
1807   // Next the events are processed with a single call into Core
1808   mCoreEventInterface.ProcessCoreEvents();
1809 }
1810
1811 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
1812 {
1813   mDragAndDropDetector = detector;
1814 }
1815
1816 void EventHandler::SetRotationObserver( RotationObserver* observer )
1817 {
1818   mRotationObserver = observer;
1819 }
1820
1821 } // namespace Adaptor
1822
1823 } // namespace Internal
1824
1825 } // namespace Dali