2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <dali/integration-api/debug.h>
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gKeyExtensionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEY_EXTENSION");
47 // Path for loading extension keys
48 #if _GLIBCXX_USE_CXX11_ABI
49 const char* KEY_EXTENSION_PLUGIN_SO( "libdali-key-extension.so" );
51 const char* KEY_EXTENSION_PLUGIN_SO( "libdali-key-extension-cxx03.so" );
59 mExtensionKeyLookupTable(NULL),
62 mCreateKeyExtensionPluginPtr(NULL),
63 mDestroyKeyExtensionPluginPtr(NULL),
65 mExtensionLookup( cmpString ),
66 mExtensionLookupCount(0),
67 mIsLookupTableInitialized( false ),
68 mIsExtensionLookupTableInitialized( false )
76 if( mDestroyKeyExtensionPluginPtr != NULL )
78 mDestroyKeyExtensionPluginPtr( mPlugin );
85 int GetDaliKeyEnum( const char* keyName )
87 // If lookup table is not initialized, initialize lookup table
88 if( !mIsLookupTableInitialized )
90 InitializeLookupTable();
93 Lookup::const_iterator i = mLookup.find( keyName );
95 if( i == mLookup.end() )
97 // If cannot find target, find it at the extension
98 // If extension lookup table is not initialized, initialize extension lookup table
99 if( !mIsExtensionLookupTableInitialized )
101 InitializeExtensionLookupTable();
105 i = mExtensionLookup.find( keyName );
107 if( i == mExtensionLookup.end() )
113 return (*i).second.first;
118 return (*i).second.first;
122 const char* GetKeyName( int daliKeyCode )
124 // If lookup table is not initialized, initialize lookup table
125 if( !mIsLookupTableInitialized )
127 InitializeLookupTable();
130 for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
132 if( KeyLookupTable[i].daliKeyCode == daliKeyCode )
134 return KeyLookupTable[i].keyName;
138 // If extension lookup table is not initialized, initialize extension lookup table
139 if( !mIsExtensionLookupTableInitialized )
141 InitializeExtensionLookupTable();
144 for( size_t i = 0; i < mExtensionLookupCount ; ++i )
146 if( mExtensionKeyLookupTable[i].daliKeyCode == daliKeyCode )
148 return mExtensionKeyLookupTable[i].keyName;
155 bool IsDeviceButton( const char* keyName )
157 // If lookup table is not initialized, initialize lookup table
158 if( !mIsLookupTableInitialized )
160 InitializeLookupTable();
163 Lookup::const_iterator i = mLookup.find( keyName );
164 if( i == mLookup.end() )
166 // If cannot find target, find it at the extension.
167 // If extension lookup table is not initialized, initialize extension lookup table
168 if( !mIsExtensionLookupTableInitialized )
170 InitializeExtensionLookupTable();
174 i = mExtensionLookup.find( keyName );
176 if( i == mExtensionLookup.end() )
182 return (*i).second.second;
187 return (*i).second.second;
196 void InitializeLookupTable()
199 for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
201 mLookup[ KeyLookupTable[i].keyName ] = DaliKeyType( KeyLookupTable[i].daliKeyCode, KeyLookupTable[i].deviceButton );
204 mIsLookupTableInitialized = true;
207 void InitializeExtensionLookupTable()
209 // Try to load extension keys
211 mHandle = dlopen( KEY_EXTENSION_PLUGIN_SO, RTLD_NOW );
214 if( mHandle == NULL )
216 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get handle from libdali-key-extension.so\n" );
222 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "dlopen got error: %s \n", error );
226 mCreateKeyExtensionPluginPtr = reinterpret_cast< CreateKeyExtensionPluginFunction >( dlsym( mHandle, "CreateKeyExtensionPlugin" ) );
227 if( mCreateKeyExtensionPluginPtr == NULL )
229 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get CreateKeyExtensionPlugin function\n" );
233 mPlugin = mCreateKeyExtensionPluginPtr();
234 if( mPlugin == NULL )
236 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to create plugin object\n" );
240 mDestroyKeyExtensionPluginPtr = reinterpret_cast< DestroyKeyExtensionPluginFunction >( dlsym( mHandle, "DestroyKeyExtensionPlugin" ) );
241 if( mDestroyKeyExtensionPluginPtr == NULL )
243 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get DestroyKeyExtensionPlugin function\n" );
247 mExtensionKeyLookupTable = mPlugin->GetKeyLookupTable();
248 mExtensionLookupCount = mPlugin->GetKeyLookupTableCount();
250 // Add extension keys to lookup
251 for( size_t i = 0; i < mExtensionLookupCount ; ++i )
253 mExtensionLookup[ mExtensionKeyLookupTable[i].keyName ] = DaliKeyType( mExtensionKeyLookupTable[i].daliKeyCode, mExtensionKeyLookupTable[i].deviceButton );
256 mIsExtensionLookupTableInitialized = true;
260 * compare function, to compare string by pointer
262 static bool cmpString( const char* a, const char* b)
264 return strcmp(a, b) < 0;
267 KeyExtensionPlugin::KeyLookup* mExtensionKeyLookupTable; ///< Lookup table for extension keys
268 Dali::KeyExtensionPlugin* mPlugin; ///< Key extension plugin handle
269 void* mHandle; ///< Handle for the loaded library
270 typedef Dali::KeyExtensionPlugin* (*CreateKeyExtensionPluginFunction)(); ///< Type of function pointer to get KeyExtensionPlugin object
271 typedef void (*DestroyKeyExtensionPluginFunction)( Dali::KeyExtensionPlugin* plugin ); ///< Type of function pointer to delete KeyExtensionPlugin object
272 CreateKeyExtensionPluginFunction mCreateKeyExtensionPluginPtr; ///< Function pointer to get KeyExtensionPlugin object
273 DestroyKeyExtensionPluginFunction mDestroyKeyExtensionPluginPtr; ///< Function pointer to delete KeyExtensionPlugin object
275 typedef std::pair< int, bool > DaliKeyType;
276 typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
278 Lookup mExtensionLookup;
279 size_t mExtensionLookupCount; ///< count of extension lookup table
280 bool mIsLookupTableInitialized; ///< flag for basic lookup table initialization
281 bool mIsExtensionLookupTableInitialized; ///< flag for extension lookup table initialization
283 KeyMap globalKeyLookup;
285 } // un-named name space
287 bool IsKey( const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
289 int key = globalKeyLookup.GetDaliKeyEnum( keyEvent.keyPressedName.c_str() );
290 return daliKey == key;
293 bool IsDeviceButton( const char* keyName )
295 return globalKeyLookup.IsDeviceButton( keyName );
298 const char* GetKeyName( Dali::KEY daliKey )
300 return globalKeyLookup.GetKeyName( daliKey );
303 int GetDaliKeyCode( const char* keyName )
305 return globalKeyLookup.GetDaliKeyEnum( keyName );
308 } // namespace KeyLookup
310 } // namespace Adaptor
312 } // namespace Internal