input: use pepper-xkb api to create/set keymap instead of pepper and xkbcommon apis
[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
29 namespace Dali
30 {
31
32 namespace Pepper
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 #if defined(DEBUG_ENABLED)
42 Integration::Log::Filter* gPepperInputLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_PEPPER_INPUT" );
43 #endif
44
45 } // unnamed namespace
46
47 InputPtr Input::New( Pepper::Compositor compositor )
48 {
49   InputPtr impl = new Input();
50
51   // Second-phase init of the implementation
52   impl->Initialize( compositor );
53
54   return impl;
55 }
56
57 Input::Input()
58 : mSeat( NULL ),
59   mDevice( NULL ),
60   mPointer( NULL ),
61   mKeyboard( NULL ),
62   mTouch( NULL ),
63   mEcoreEventHandler()
64 {
65 }
66
67 Input::~Input()
68 {
69   for( std::vector<Ecore_Event_Handler*>::iterator iter = mEcoreEventHandler.begin(), endIter = mEcoreEventHandler.end(); iter != endIter; ++iter )
70   {
71     ecore_event_handler_del( *iter );
72   }
73
74   pepper_xkb_destroy( mXkb );
75
76
77   pepper_seat_remove_input_device( mSeat, mDevice );
78   pepper_input_device_destroy( mDevice );
79
80   pepper_seat_destroy( mSeat );
81 }
82
83 void Input::Initialize( Pepper::Compositor& compositor )
84 {
85   const char *defaultName = "seat0";
86
87   mSeat = pepper_compositor_add_seat( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ), defaultName );
88   if( !mSeat )
89   {
90     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "pepper_compositor_add_seat is failed\n" );
91     return;
92   }
93
94   /* create and add devices. */
95   mDevice = pepper_input_device_create( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ),
96                                         WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_TOUCH, NULL, NULL );
97 //  mDevice = pepper_input_device_create( static_cast< pepper_compositor_t* >( Pepper::GetImplementation( compositor ).GetCompositorHandle() ),
98 //                                        ( WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_TOUCH ), NULL, NULL );
99   if( !mDevice )
100   {
101     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "pepper_input_device_create is failed\n" );
102     pepper_seat_destroy( mSeat );
103     return;
104   }
105
106   pepper_seat_add_input_device( mSeat, mDevice );
107
108   /* get device resource from seat */
109   mPointer = pepper_seat_get_pointer( mSeat );
110   mKeyboard = pepper_seat_get_keyboard( mSeat );
111   mTouch = pepper_seat_get_touch( mSeat );
112
113   mCompositor = compositor;
114   mXkb = pepper_xkb_create( );
115   if( !mXkb )
116   {
117     DALI_LOG_INFO( gPepperInputLogging, Debug::General, "failed to create pepper xkb\n");
118     return;
119   }
120
121   pepper_xkb_keyboard_set_keymap( mXkb, mKeyboard, NULL);
122
123   mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_DOWN, EcoreEventKeyDown, this ) );
124   mEcoreEventHandler.push_back( ecore_event_handler_add( ECORE_EVENT_KEY_UP, EcoreEventKeyUp, this ) );
125
126   DALI_LOG_INFO( gPepperInputLogging, Debug::Verbose, "Input::Initialize: success\n" );
127 }
128
129 Eina_Bool Input::EcoreEventKeyDown( void* data, int type, void* event )
130 {
131   Input* input = static_cast< Input* >( data );
132   pepper_view_t* view;
133   Ecore_Event_Key *keyEvent = static_cast< Ecore_Event_Key* >( event );
134   uint32_t keycode;
135
136   if( !( view = pepper_keyboard_get_focus( input->mKeyboard ) ) )
137   {
138     return ECORE_CALLBACK_RENEW;
139   }
140
141   keycode = (keyEvent->keycode - 8);
142
143   pepper_keyboard_send_key( input->mKeyboard, view, keyEvent->timestamp, keycode, WL_KEYBOARD_KEY_STATE_PRESSED );
144
145   return ECORE_CALLBACK_RENEW;
146 }
147
148 Eina_Bool Input::EcoreEventKeyUp( void* data, int type, void* event )
149 {
150   Input* input = static_cast< Input* >( data );
151   pepper_view_t* view;
152   Ecore_Event_Key *keyEvent = static_cast< Ecore_Event_Key* >( event );
153   uint32_t keycode;
154
155   if( !( view = pepper_keyboard_get_focus( input->mKeyboard ) ) )
156   {
157     return ECORE_CALLBACK_RENEW;
158   }
159
160   keycode = (keyEvent->keycode - 8);
161
162   pepper_keyboard_send_key( input->mKeyboard, view, keyEvent->timestamp, keycode, WL_KEYBOARD_KEY_STATE_RELEASED );
163
164   return ECORE_CALLBACK_RENEW;
165 }
166
167 pepper_pointer_t* Input::GetPointer()
168 {
169   return mPointer;
170 }
171
172 pepper_keyboard_t* Input::GetKeyboard()
173 {
174   return mKeyboard;
175 }
176
177 pepper_touch_t* Input::GetTouch()
178 {
179   return mTouch;
180 }
181
182 } // namespace Internal
183
184 } // namespace Pepper
185
186 } // namespace Dali