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