Merge "(ImfManager) Initialise ImfManager only when required rather than at startup...
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / event-handler-wl.cpp
1 /*
2  * Copyright (c) 2014 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 #include <dali/public-api/common/vector-wrapper.h>
35 #include <dali/public-api/events/touch-point.h>
36 #include <dali/public-api/events/key-event.h>
37 #include <dali/public-api/events/mouse-wheel-event.h>
38 #include <dali/integration-api/debug.h>
39 #include <dali/integration-api/events/key-event-integ.h>
40 #include <dali/integration-api/events/touch-event-integ.h>
41 #include <dali/integration-api/events/hover-event-integ.h>
42 #include <dali/integration-api/events/mouse-wheel-event-integ.h>
43
44 // INTERNAL INCLUDES
45 #include <events/gesture-manager.h>
46 #include <window-render-surface.h>
47 #include <clipboard-impl.h>
48 #include <key-impl.h>
49 #include <physical-keyboard-impl.h>
50 #include <style-monitor-impl.h>
51 #include <base/core-event-interface.h>
52
53 using namespace std;
54
55 namespace Dali
56 {
57
58 namespace Internal
59 {
60
61 namespace Adaptor
62 {
63
64 #if defined(DEBUG_ENABLED)
65 namespace
66 {
67 Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
68 Integration::Log::Filter* gClientMessageLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_CLIENT_MESSAGE");
69 Integration::Log::Filter* gDragAndDropLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_DND");
70 Integration::Log::Filter* gImfLogging  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_IMF");
71 Integration::Log::Filter* gSelectionEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_SELECTION");
72 } // unnamed namespace
73 #endif
74
75
76 namespace
77 {
78 const unsigned int PRIMARY_TOUCH_BUTTON_ID( 1 );
79
80 const unsigned int BYTES_PER_CHARACTER_FOR_ATTRIBUTES = 3;
81
82 /**
83  * Ecore_Event_Modifier enums in Ecore_Input.h do not match Ecore_IMF_Keyboard_Modifiers in Ecore_IMF.h.
84  * This function converts from Ecore_Event_Modifier to Ecore_IMF_Keyboard_Modifiers enums.
85  * @param[in] ecoreModifier the Ecore_Event_Modifier input.
86  * @return the Ecore_IMF_Keyboard_Modifiers output.
87  */
88 Ecore_IMF_Keyboard_Modifiers EcoreInputModifierToEcoreIMFModifier(unsigned int ecoreModifier)
89 {
90    int modifier( ECORE_IMF_KEYBOARD_MODIFIER_NONE );  // If no other matches returns NONE.
91
92
93    if ( ecoreModifier & ECORE_EVENT_MODIFIER_SHIFT )  // enums from ecore_input/Ecore_Input.h
94    {
95      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_SHIFT;  // enums from ecore_imf/ecore_imf.h
96    }
97
98    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALT )
99    {
100      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALT;
101    }
102
103    if ( ecoreModifier & ECORE_EVENT_MODIFIER_CTRL )
104    {
105      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_CTRL;
106    }
107
108    if ( ecoreModifier & ECORE_EVENT_MODIFIER_WIN )
109    {
110      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_WIN;
111    }
112
113    if ( ecoreModifier & ECORE_EVENT_MODIFIER_ALTGR )
114    {
115      modifier |= ECORE_IMF_KEYBOARD_MODIFIER_ALTGR;
116    }
117
118    return static_cast<Ecore_IMF_Keyboard_Modifiers>( modifier );
119 }
120
121
122 // Copied from x server
123 static unsigned int GetCurrentMilliSeconds(void)
124 {
125   struct timeval tv;
126
127   struct timespec tp;
128   static clockid_t clockid;
129
130   if (!clockid)
131   {
132 #ifdef CLOCK_MONOTONIC_COARSE
133     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
134       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
135     {
136       clockid = CLOCK_MONOTONIC_COARSE;
137     }
138     else
139 #endif
140     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
141     {
142       clockid = CLOCK_MONOTONIC;
143     }
144     else
145     {
146       clockid = ~0L;
147     }
148   }
149   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
150   {
151     return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
152   }
153
154   gettimeofday(&tv, NULL);
155   return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
156 }
157
158 #ifndef DALI_PROFILE_UBUNTU
159 const char * DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE = "db/setting/accessibility/font_name";  // It will be update at vconf-key.h and replaced.
160 #endif // DALI_PROFILE_UBUNTU
161
162 } // unnamed namespace
163
164 // Impl to hide EFL implementation.
165 struct EventHandler::Impl
166 {
167   // Construction & Destruction
168
169   /**
170    * Constructor
171    */
172   Impl( EventHandler* handler, Ecore_Wl_Window* window )
173   : mHandler( handler ),
174     mEcoreEventHandler(),
175     mWindow( window )
176   {
177     // Only register for touch and key events if we have a window
178     if ( window != 0 )
179     {
180       // Register Touch events
181       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_DOWN,  EcoreEventMouseButtonDown, handler ) );
182       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_BUTTON_UP,    EcoreEventMouseButtonUp,   handler ) );
183       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_MOVE,         EcoreEventMouseButtonMove, handler ) );
184       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_OUT,          EcoreEventMouseButtonUp,   handler ) ); // process mouse out event like up event
185
186       // Register Mouse wheel events
187       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_MOUSE_WHEEL,        EcoreEventMouseWheel,      handler ) );
188
189       // Register Key events
190       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_DOWN,           EcoreEventKeyDown,         handler ) );
191       mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_UP,             EcoreEventKeyUp,           handler ) );
192
193 #ifndef DALI_PROFILE_UBUNTU
194       // Register Vconf notify - font name and size
195       vconf_notify_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontNameChanged, handler );
196       vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged, handler );
197 #endif // DALI_PROFILE_UBUNTU
198     }
199   }
200
201   /**
202    * Destructor
203    */
204   ~Impl()
205   {
206 #ifndef DALI_PROFILE_UBUNTU
207     vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontSizeChanged );
208     vconf_ignore_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, VconfNotifyFontNameChanged );
209 #endif // DALI_PROFILE_UBUNTU
210
211     for( std::vector<Ecore_Event_Handler*>::iterator iter = mEcoreEventHandler.begin(), endIter = mEcoreEventHandler.end(); iter != endIter; ++iter )
212     {
213       ecore_event_handler_del( *iter );
214     }
215   }
216
217   // Static methods
218
219   /////////////////////////////////////////////////////////////////////////////////////////////////
220   // Touch Callbacks
221   /////////////////////////////////////////////////////////////////////////////////////////////////
222
223   /**
224    * Called when a touch down is received.
225    */
226   static Eina_Bool EcoreEventMouseButtonDown( void* data, int type, void* event )
227   {
228     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
229     EventHandler* handler( (EventHandler*)data );
230
231     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
232     {
233       TouchPoint::State state ( TouchPoint::Down );
234
235       // Check if the buttons field is set and ensure it's the primary touch button.
236       // If this event was triggered by buttons other than the primary button (used for touch), then
237       // just send an interrupted event to Core.
238       if ( touchEvent->buttons && (touchEvent->buttons != PRIMARY_TOUCH_BUTTON_ID ) )
239       {
240         state = TouchPoint::Interrupted;
241       }
242
243       TouchPoint point( touchEvent->multi.device, state, touchEvent->x, touchEvent->y );
244       handler->SendEvent( point, touchEvent->timestamp );
245     }
246
247     return ECORE_CALLBACK_PASS_ON;
248   }
249
250   /**
251    * Called when a touch up is received.
252    */
253   static Eina_Bool EcoreEventMouseButtonUp( void* data, int type, void* event )
254   {
255     Ecore_Event_Mouse_Button *touchEvent( (Ecore_Event_Mouse_Button*)event );
256     EventHandler* handler( (EventHandler*)data );
257
258     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
259     {
260       TouchPoint point( touchEvent->multi.device, TouchPoint::Up, touchEvent->x, touchEvent->y );
261       handler->SendEvent( point, touchEvent->timestamp );
262     }
263
264     return ECORE_CALLBACK_PASS_ON;
265   }
266
267   /**
268    * Called when a touch up is received.
269    */
270   static Eina_Bool EcoreEventMouseWheel( void* data, int type, void* event )
271   {
272     Ecore_Event_Mouse_Wheel *mouseWheelEvent( (Ecore_Event_Mouse_Wheel*)event );
273
274     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);
275
276     EventHandler* handler( (EventHandler*)data );
277     if ( mouseWheelEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
278     {
279       MouseWheelEvent wheelEvent(mouseWheelEvent->direction, mouseWheelEvent->modifiers, Vector2(mouseWheelEvent->x, mouseWheelEvent->y), mouseWheelEvent->z, mouseWheelEvent->timestamp);
280       handler->SendMouseWheelEvent( wheelEvent );
281     }
282     return ECORE_CALLBACK_PASS_ON;
283   }
284
285   /**
286    * Called when a touch motion is received.
287    */
288   static Eina_Bool EcoreEventMouseButtonMove( void* data, int type, void* event )
289   {
290     Ecore_Event_Mouse_Move *touchEvent( (Ecore_Event_Mouse_Move*)event );
291     EventHandler* handler( (EventHandler*)data );
292
293     if ( touchEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
294     {
295       TouchPoint point( touchEvent->multi.device, TouchPoint::Motion, touchEvent->x, touchEvent->y );
296       handler->SendEvent( point, touchEvent->timestamp );
297     }
298
299     return ECORE_CALLBACK_PASS_ON;
300   }
301
302   /////////////////////////////////////////////////////////////////////////////////////////////////
303   // Key Callbacks
304   /////////////////////////////////////////////////////////////////////////////////////////////////
305
306   /**
307    * Called when a key down is received.
308    */
309   static Eina_Bool EcoreEventKeyDown( void* data, int type, void* event )
310   {
311     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyDown \n" );
312
313     EventHandler* handler( (EventHandler*)data );
314     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
315     bool eventHandled( false );
316
317     // If a device key then skip ecore_imf_context_filter_event.
318     if ( ! KeyLookup::IsDeviceButton( keyEvent->keyname ) )
319     {
320       Ecore_IMF_Context* imfContext = NULL;
321       Dali::ImfManager imfManager( ImfManager::Get() );
322       if ( imfManager )
323       {
324         imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
325       }
326
327       if ( imfContext )
328       {
329         // We're consuming key down event so we have to pass to IMF so that it can parse it as well.
330         Ecore_IMF_Event_Key_Down ecoreKeyDownEvent;
331         ecoreKeyDownEvent.keyname   = keyEvent->keyname;
332         ecoreKeyDownEvent.key       = keyEvent->key;
333         ecoreKeyDownEvent.string    = keyEvent->string;
334         ecoreKeyDownEvent.compose   = keyEvent->compose;
335         ecoreKeyDownEvent.timestamp = keyEvent->timestamp;
336         ecoreKeyDownEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
337         ecoreKeyDownEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
338
339         eventHandled = ecore_imf_context_filter_event( imfContext,
340                                                        ECORE_IMF_EVENT_KEY_DOWN,
341                                                        (Ecore_IMF_Event *) &ecoreKeyDownEvent );
342
343         // If the event has not been handled by IMF then check if we should reset our IMF context
344         if( !eventHandled )
345         {
346           if ( !strcmp( keyEvent->keyname, "Escape"   ) ||
347                !strcmp( keyEvent->keyname, "Return"   ) ||
348                !strcmp( keyEvent->keyname, "KP_Enter" ) )
349           {
350             ecore_imf_context_reset( imfContext );
351           }
352         }
353       }
354     }
355
356     // If the event wasn't handled then we should send a key event.
357     if ( !eventHandled )
358     {
359       if ( keyEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
360       {
361         std::string keyName( keyEvent->keyname );
362         std::string keyString( "" );
363         int keyCode = 0/*ecore_x_keysym_keycode_get(keyEvent->keyname)*/;
364         int modifier( keyEvent->modifiers );
365         unsigned long time = keyEvent->timestamp;
366
367         if (!strncmp(keyEvent->keyname, "Keycode-", 8))
368           keyCode = atoi(keyEvent->keyname + 8);
369
370         // Ensure key event string is not NULL as keys like SHIFT have a null string.
371         if ( keyEvent->string )
372         {
373           keyString = keyEvent->string;
374         }
375
376         KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, KeyEvent::Down);
377         handler->SendEvent( keyEvent );
378       }
379     }
380
381     return ECORE_CALLBACK_PASS_ON;
382   }
383
384   /**
385    * Called when a key up is received.
386    */
387   static Eina_Bool EcoreEventKeyUp( void* data, int type, void* event )
388   {
389     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventKeyUp \n" );
390
391     EventHandler* handler( (EventHandler*)data );
392     Ecore_Event_Key *keyEvent( (Ecore_Event_Key*)event );
393     bool eventHandled( false );
394
395     // XF86Stop and XF86Send must skip ecore_imf_context_filter_event.
396     if ( strcmp( keyEvent->keyname, "XF86Send" )  &&
397          strcmp( keyEvent->keyname, "XF86Phone" ) &&
398          strcmp( keyEvent->keyname, "XF86Stop" ) )
399     {
400       Ecore_IMF_Context* imfContext = NULL;
401       Dali::ImfManager imfManager( ImfManager::Get() );
402       if ( imfManager )
403       {
404         imfContext = reinterpret_cast<Ecore_IMF_Context*>( imfManager.GetContext() );
405       }
406
407       if ( imfContext )
408       {
409         // We're consuming key up event so we have to pass to IMF so that it can parse it as well.
410         Ecore_IMF_Event_Key_Up ecoreKeyUpEvent;
411         ecoreKeyUpEvent.keyname   = keyEvent->keyname;
412         ecoreKeyUpEvent.key       = keyEvent->key;
413         ecoreKeyUpEvent.string    = keyEvent->string;
414         ecoreKeyUpEvent.compose   = keyEvent->compose;
415         ecoreKeyUpEvent.timestamp = keyEvent->timestamp;
416         ecoreKeyUpEvent.modifiers = EcoreInputModifierToEcoreIMFModifier ( keyEvent->modifiers );
417         ecoreKeyUpEvent.locks     = (Ecore_IMF_Keyboard_Locks) ECORE_IMF_KEYBOARD_LOCK_NONE;
418
419         eventHandled = ecore_imf_context_filter_event( imfContext,
420                                                        ECORE_IMF_EVENT_KEY_UP,
421                                                        (Ecore_IMF_Event *) &ecoreKeyUpEvent );
422       }
423     }
424
425     // If the event wasn't handled then we should send a key event.
426     if ( !eventHandled )
427     {
428       if ( keyEvent->window == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
429       {
430         std::string keyName( keyEvent->keyname );
431         std::string keyString( "" );
432         int keyCode = 0/*ecore_x_keysym_keycode_get(keyEvent->keyname)*/;
433         int modifier( keyEvent->modifiers );
434         unsigned long time( keyEvent->timestamp );
435
436         if (!strncmp(keyEvent->keyname, "Keycode-", 8))
437           keyCode = atoi(keyEvent->keyname + 8);
438
439         // Ensure key event string is not NULL as keys like SHIFT have a null string.
440         if ( keyEvent->string )
441         {
442           keyString = keyEvent->string;
443         }
444
445         KeyEvent keyEvent(keyName, keyString, keyCode, modifier, time, KeyEvent::Up);
446         handler->SendEvent( keyEvent );
447       }
448     }
449
450     return ECORE_CALLBACK_PASS_ON;
451   }
452
453   /////////////////////////////////////////////////////////////////////////////////////////////////
454   // Window Callbacks
455   /////////////////////////////////////////////////////////////////////////////////////////////////
456
457   /**
458    * Called when the window gains focus.
459    */
460   static Eina_Bool EcoreEventWindowFocusIn( void* data, int type, void* event )
461   {
462     Ecore_Wl_Event_Focus_In* focusInEvent( (Ecore_Wl_Event_Focus_In*)event );
463     EventHandler* handler( (EventHandler*)data );
464
465     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusIn \n" );
466
467     // If the window gains focus and we hid the keyboard then show it again.
468     if ( focusInEvent->win == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
469     {
470       DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT EcoreEventWindowFocusIn - >>WindowFocusGained \n" );
471
472       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
473       {
474         Dali::ImfManager imfManager( ImfManager::Get() );
475         if ( imfManager )
476         {
477           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
478           if( imfManagerImpl.RestoreAfterFocusLost() )
479           {
480             imfManagerImpl.Activate();
481           }
482         }
483       }
484       // No need to connect callbacks as KeyboardStatusChanged will be called.
485     }
486
487     return ECORE_CALLBACK_PASS_ON;
488   }
489
490   /**
491    * Called when the window loses focus.
492    */
493   static Eina_Bool EcoreEventWindowFocusOut( void* data, int type, void* event )
494   {
495     Ecore_Wl_Event_Focus_Out* focusOutEvent( (Ecore_Wl_Event_Focus_Out*)event );
496     EventHandler* handler( (EventHandler*)data );
497
498     DALI_LOG_INFO( gImfLogging, Debug::General, "EVENT >>EcoreEventWindowFocusOut \n" );
499
500     // If the window loses focus then hide the keyboard.
501     if ( focusOutEvent->win == (unsigned int)ecore_wl_window_id_get(handler->mImpl->mWindow) )
502     {
503       if ( ImfManager::IsAvailable() /* Only get the ImfManager if it's available as we do not want to create it */ )
504       {
505         Dali::ImfManager imfManager( ImfManager::Get() );
506         if ( imfManager )
507         {
508           ImfManager& imfManagerImpl( ImfManager::GetImplementation( imfManager ) );
509           if( imfManagerImpl.RestoreAfterFocusLost() )
510           {
511             imfManagerImpl.Deactivate();
512           }
513         }
514       }
515
516       // Clipboard don't support that whether clipboard is shown or not. Hide clipboard.
517       Dali::Clipboard clipboard = Clipboard::Get();
518       clipboard.HideClipboard();
519     }
520
521     return ECORE_CALLBACK_PASS_ON;
522   }
523
524   /**
525    * Called when the window is damaged.
526    */
527   static Eina_Bool EcoreEventWindowDamaged(void *data, int type, void *event)
528   {
529     return ECORE_CALLBACK_PASS_ON;
530   }
531
532   /**
533    * Called when the window properties are changed.
534    * We are only interested in the font change.
535    */
536
537
538   /////////////////////////////////////////////////////////////////////////////////////////////////
539   // Drag & Drop Callbacks
540   /////////////////////////////////////////////////////////////////////////////////////////////////
541
542   /**
543    * Called when a dragged item enters our window's bounds.
544    * This is when items are dragged INTO our window.
545    */
546   static Eina_Bool EcoreEventDndEnter( void* data, int type, void* event )
547   {
548     DALI_LOG_INFO( gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndEnter\n" );
549
550     return ECORE_CALLBACK_PASS_ON;
551   }
552
553   /**
554    * Called when a dragged item is moved within our window.
555    * This is when items are dragged INTO our window.
556    */
557   static Eina_Bool EcoreEventDndPosition( void* data, int type, void* event )
558   {
559     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndPosition\n" );
560
561     return ECORE_CALLBACK_PASS_ON;
562   }
563
564   /**
565    * Called when a dragged item leaves our window's bounds.
566    * This is when items are dragged INTO our window.
567    */
568   static Eina_Bool EcoreEventDndLeave( void* data, int type, void* event )
569   {
570     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndLeave\n" );
571
572     return ECORE_CALLBACK_PASS_ON;
573   }
574
575   /**
576    * Called when the dragged item is dropped within our window's bounds.
577    * This is when items are dragged INTO our window.
578    */
579   static Eina_Bool EcoreEventDndDrop( void* data, int type, void* event )
580   {
581     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndDrop\n" );
582
583     return ECORE_CALLBACK_PASS_ON;
584   }
585
586   /**
587    * Called when a dragged item is moved from our window and the target window has done processing it.
588    * This is when items are dragged FROM our window.
589    */
590   static Eina_Bool EcoreEventDndFinished( void* data, int type, void* event )
591   {
592     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndFinished\n" );
593     return ECORE_CALLBACK_PASS_ON;
594   }
595
596   /**
597    * Called when a dragged item is moved from our window and the target window has sent us a status.
598    * This is when items are dragged FROM our window.
599    */
600   static Eina_Bool EcoreEventDndStatus( void* data, int type, void* event )
601   {
602     DALI_LOG_INFO(gDragAndDropLogFilter, Debug::Concise, "EcoreEventDndStatus\n" );
603     return ECORE_CALLBACK_PASS_ON;
604   }
605
606   /**
607    * Called when the client messages (i.e. the accessibility events) are received.
608    */
609   static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
610   {
611     return ECORE_CALLBACK_PASS_ON;
612   }
613
614   /**
615    * Called when the source window notifies us the content in clipboard is selected.
616    */
617   static Eina_Bool EcoreEventSelectionClear( void* data, int type, void* event )
618   {
619     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionClear\n" );
620     return ECORE_CALLBACK_PASS_ON;
621   }
622
623   /**
624    * Called when the source window sends us about the selected content.
625    * For example, when dragged items are dragged INTO our window or when items are selected in the clipboard.
626    */
627   static Eina_Bool EcoreEventSelectionNotify( void* data, int type, void* event )
628   {
629     DALI_LOG_INFO(gSelectionEventLogFilter, Debug::Concise, "EcoreEventSelectionNotify\n" );
630     return ECORE_CALLBACK_PASS_ON;
631   }
632
633   /////////////////////////////////////////////////////////////////////////////////////////////////
634   // Font Callbacks
635   /////////////////////////////////////////////////////////////////////////////////////////////////
636   /**
637    * Called when a font name is changed.
638    */
639   static void VconfNotifyFontNameChanged( keynode_t* node, void* data )
640   {
641     EventHandler* handler = static_cast<EventHandler*>( data );
642
643     StyleChange fontChange;
644     fontChange.defaultFontChange = true;
645
646     handler->SendEvent( fontChange );
647   }
648
649   /**
650    * Called when a font size is changed.
651    */
652   static void VconfNotifyFontSizeChanged( keynode_t* node, void* data )
653   {
654     EventHandler* handler = static_cast<EventHandler*>( data );
655
656     StyleChange fontChange;
657     fontChange.defaultFontSizeChange = true;
658
659     handler->SendEvent( fontChange );
660   }
661
662   // Data
663   EventHandler* mHandler;
664   std::vector<Ecore_Event_Handler*> mEcoreEventHandler;
665   Ecore_Wl_Window* mWindow;
666 };
667
668 EventHandler::EventHandler( RenderSurface* surface, CoreEventInterface& coreEventInterface, GestureManager& gestureManager, DamageObserver& damageObserver, DragAndDropDetectorPtr dndDetector )
669 : mCoreEventInterface(coreEventInterface),
670   mGestureManager( gestureManager ),
671   mStyleMonitor( StyleMonitor::Get() ),
672   mDamageObserver( damageObserver ),
673   mRotationObserver( NULL ),
674   mDragAndDropDetector( dndDetector ),
675   mAccessibilityManager( AccessibilityManager::Get() ),
676   mClipboardEventNotifier( ClipboardEventNotifier::Get() ),
677   mClipboard(Clipboard::Get()),
678   mImpl( NULL )
679 {
680   Ecore_Wl_Window* window = 0;
681
682   if( surface->GetType() == Dali::RenderSurface::WINDOW )
683   {
684     // this code only works with the Ecore RenderSurface so need to downcast
685     ECore::WindowRenderSurface* ecoreSurface = dynamic_cast< ECore::WindowRenderSurface* >( surface );
686     if( ecoreSurface )
687     {
688       window = ecoreSurface->GetWlWindow();
689     }
690   }
691
692   mImpl = new Impl(this, window);
693 }
694
695 EventHandler::~EventHandler()
696 {
697   if(mImpl)
698   {
699     delete mImpl;
700   }
701
702   mGestureManager.Stop();
703 }
704
705 void EventHandler::SendEvent(TouchPoint& point, unsigned long timeStamp)
706 {
707   if(timeStamp < 1)
708   {
709     timeStamp = GetCurrentMilliSeconds();
710   }
711
712   Integration::TouchEvent touchEvent;
713   Integration::HoverEvent hoverEvent;
714   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
715   if(type != Integration::TouchEventCombiner::DispatchNone )
716   {
717     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);
718
719     // First the touch and/or hover event & related gesture events are queued
720     if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth)
721     {
722       mCoreEventInterface.QueueCoreEvent( touchEvent );
723       mGestureManager.SendEvent(touchEvent);
724     }
725
726     if(type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth)
727     {
728       mCoreEventInterface.QueueCoreEvent( hoverEvent );
729     }
730
731     // Next the events are processed with a single call into Core
732     mCoreEventInterface.ProcessCoreEvents();
733   }
734 }
735
736 void EventHandler::SendEvent(KeyEvent& keyEvent)
737 {
738   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
739   if ( physicalKeyboard )
740   {
741     if ( ! KeyLookup::IsDeviceButton( keyEvent.keyPressedName.c_str() ) )
742     {
743       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
744     }
745   }
746
747   // Create KeyEvent and send to Core.
748   Integration::KeyEvent event(keyEvent.keyPressedName, keyEvent.keyPressed, keyEvent.keyCode,
749   keyEvent.keyModifier, keyEvent.time, static_cast<Integration::KeyEvent::State>(keyEvent.state));
750   mCoreEventInterface.QueueCoreEvent( event );
751   mCoreEventInterface.ProcessCoreEvents();
752 }
753
754 void EventHandler::SendMouseWheelEvent( MouseWheelEvent& wheelEvent )
755 {
756   // Create MouseWheelEvent and send to Core.
757   Integration::MouseWheelEvent event(wheelEvent.direction, wheelEvent.modifiers, wheelEvent.point, wheelEvent.z, wheelEvent.timeStamp);
758   mCoreEventInterface.QueueCoreEvent( event );
759   mCoreEventInterface.ProcessCoreEvents();
760 }
761
762 void EventHandler::SendEvent(StyleChange styleChange)
763 {
764   DALI_ASSERT_DEBUG( mStyleMonitor && "StyleMonitor Not Available" );
765   GetImplementation( mStyleMonitor ).StyleChanged(styleChange);
766 }
767
768 void EventHandler::SendEvent( const DamageArea& area )
769 {
770   mDamageObserver.OnDamaged( area );
771 }
772
773 void EventHandler::SendRotationPrepareEvent( const RotationEvent& event )
774 {
775   if( mRotationObserver != NULL )
776   {
777     mRotationObserver->OnRotationPrepare( event );
778   }
779 }
780
781 void EventHandler::SendRotationRequestEvent( )
782 {
783   if( mRotationObserver != NULL )
784   {
785     mRotationObserver->OnRotationRequest( );
786   }
787 }
788
789 void EventHandler::FeedTouchPoint( TouchPoint& point, int timeStamp)
790 {
791   SendEvent(point, timeStamp);
792 }
793
794 void EventHandler::FeedWheelEvent( MouseWheelEvent& wheelEvent )
795 {
796   SendMouseWheelEvent( wheelEvent );
797 }
798
799 void EventHandler::FeedKeyEvent( KeyEvent& event )
800 {
801   SendEvent( event );
802 }
803
804 void EventHandler::FeedEvent( Integration::Event& event )
805 {
806   mCoreEventInterface.QueueCoreEvent( event );
807   mCoreEventInterface.ProcessCoreEvents();
808 }
809
810 void EventHandler::Reset()
811 {
812   mCombiner.Reset();
813
814   // Any touch listeners should be told of the interruption.
815   Integration::TouchEvent event;
816   TouchPoint point(0, TouchPoint::Interrupted, 0, 0);
817   event.AddPoint( point );
818
819   // First the touch event & related gesture events are queued
820   mCoreEventInterface.QueueCoreEvent( event );
821   mGestureManager.SendEvent( event );
822
823   // Next the events are processed with a single call into Core
824   mCoreEventInterface.ProcessCoreEvents();
825 }
826
827 void EventHandler::SetDragAndDropDetector( DragAndDropDetectorPtr detector )
828 {
829   mDragAndDropDetector = detector;
830 }
831
832 void EventHandler::SetRotationObserver( RotationObserver* observer )
833 {
834   mRotationObserver = observer;
835 }
836
837 } // namespace Adaptor
838
839 } // namespace Internal
840
841 } // namespace Dali