507163e9a65f4c4dce59e07a26782e5d10a87b47
[platform/core/uifw/pepper-dali.git] / pepper-dali / internal / input-impl.cpp
1 /*
2  * Copyright (c) 2016 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 <pepper-dali/internal/input-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <pepper-dali/internal/compositor-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <pepper-input-backend.h>
27 #include <Ecore_Input.h>
28 #include <xkbcommon/xkbcommon.h>
29
30 namespace Dali
31 {
32
33 namespace Pepper
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41
42 #if defined(DEBUG_ENABLED)
43 Integration::Log::Filter* gPepperInputLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_INPUT" );
44 #endif
45
46 } // unnamed namespace
47
48 InputPtr Input::New( Pepper::Compositor compositor )
49 {
50   InputPtr impl = new Input();
51
52   // Second-phase init of the implementation
53   impl->Initialize( compositor );
54
55   return impl;
56 }
57
58 Input::Input()
59 : mSeat( NULL ),
60   mDevice( NULL ),
61   mPointer( NULL ),
62   mKeyboard( NULL ),
63   mTouch( NULL ),
64   mContext( NULL ),
65   mEcoreEventHandler()
66 {
67 }
68
69 Input::~Input()
70 {
71   for( std::vector<Ecore_Event_Handler*>::iterator iter = mEcoreEventHandler.begin(), endIter = mEcoreEventHandler.end(); iter != endIter; ++iter )
72   {
73     ecore_event_handler_del( *iter );
74   }
75
76   xkb_context_unref( mContext );
77
78   pepper_seat_remove_input_device( mSeat, mDevice );
79   pepper_input_device_destroy( mDevice );
80
81   pepper_seat_destroy( mSeat );
82 }
83
84 void Input::Initialize( Pepper::Compositor& compositor )
85 {
86   const char *defaultName = "seat0";
87
88   mSeat = pepper_compositor_add_seat( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ), defaultName );
89   if( !mSeat )
90   {
91     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "pepper_compositor_add_seat is failed\n" );
92     return;
93   }
94
95   /* create and add devices. */
96   mDevice = pepper_input_device_create( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ),
97                                         WL_SEAT_CAPABILITY_TOUCH, NULL, NULL );
98 //  mDevice = pepper_input_device_create( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ),
99 //                                        ( WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_TOUCH ), NULL, NULL );
100   if( !mDevice )
101   {
102     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "pepper_input_device_create is failed\n" );
103     pepper_seat_destroy( mSeat );
104     return;
105   }
106
107   pepper_seat_add_input_device( mSeat, mDevice );
108
109   /* get device resource from seat */
110   mPointer = pepper_seat_get_pointer( mSeat );
111   mKeyboard = pepper_seat_get_keyboard( mSeat );
112   mTouch = pepper_seat_get_touch( mSeat );
113
114   mCompositor = compositor;
115
116   SetKeymap();
117
118   mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_DOWN, EcoreEventKeyDown, this ) );
119   mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_UP, EcoreEventKeyUp, this ) );
120
121   DALI_LOG_INFO( gPepperInputLogging, Debug::Verbose, "Input::Initialize: success\n" );
122 }
123
124 void Input::SetKeymap()
125 {
126   struct xkb_keymap* keymap;
127   struct xkb_context* context;
128   struct xkb_rule_names names;
129
130   names.rules = "evdev";
131   names.model = "pc105";
132   names.layout = "us";
133   names.options = NULL;
134
135   context = xkb_context_new( XKB_CONTEXT_NO_FLAGS );
136   if( !context )
137   {
138     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "failed to create xkb_context\n" );
139     return;
140   }
141
142   keymap = xkb_map_new_from_names( context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS );
143   if( !keymap )
144   {
145     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "failed to get keymap\n" );
146     xkb_context_unref( context );
147     return;
148   }
149
150   pepper_keyboard_set_keymap( mKeyboard, keymap );
151   xkb_map_unref( keymap );
152
153   mContext = context;
154 }
155
156 Eina_Bool Input::EcoreEventKeyDown( void* data, int type, void* event )
157 {
158   Input* input = static_cast< Input* >( data );
159   pepper_view_t* view;
160   Ecore_Event_Key *keyEvent = static_cast< Ecore_Event_Key* >( event );
161   uint32_t keycode;
162
163   if( !( view = pepper_keyboard_get_focus( input->mKeyboard ) ) )
164   {
165     return ECORE_CALLBACK_RENEW;
166   }
167
168   keycode = (keyEvent->keycode - 8);
169
170   pepper_keyboard_send_key( input->mKeyboard, view, keyEvent->timestamp, keycode, WL_KEYBOARD_KEY_STATE_PRESSED );
171
172   return ECORE_CALLBACK_RENEW;
173 }
174
175 Eina_Bool Input::EcoreEventKeyUp( void* data, int type, void* event )
176 {
177   Input* input = static_cast< Input* >( data );
178   pepper_view_t* view;
179   Ecore_Event_Key *keyEvent = static_cast< Ecore_Event_Key* >( event );
180   uint32_t keycode;
181
182   if( !( view = pepper_keyboard_get_focus( input->mKeyboard ) ) )
183   {
184     return ECORE_CALLBACK_RENEW;
185   }
186
187   keycode = (keyEvent->keycode - 8);
188
189   pepper_keyboard_send_key( input->mKeyboard, view, keyEvent->timestamp, keycode, WL_KEYBOARD_KEY_STATE_RELEASED );
190
191   return ECORE_CALLBACK_RENEW;
192 }
193
194 pepper_pointer_t* Input::GetPointer()
195 {
196   return mPointer;
197 }
198
199 pepper_keyboard_t* Input::GetKeyboard()
200 {
201   return mKeyboard;
202 }
203
204 pepper_touch_t* Input::GetTouch()
205 {
206   return mTouch;
207 }
208
209 } // namespace Internal
210
211 } // namespace Pepper
212
213 } // namespace Dali