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