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