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 const char* KEY_EXTENSION_PLUGIN_SO( "libdali-key-extension.so" );
55 mExtensionKeyLookupTable(NULL),
58 mCreateKeyExtensionPluginPtr(NULL),
59 mDestroyKeyExtensionPluginPtr(NULL),
61 mExtensionLookup( cmpString ),
62 mExtensionLookupCount(0),
63 mIsLookupTableInitialized( false ),
64 mIsExtensionLookupTableInitialized( false )
72 if( mDestroyKeyExtensionPluginPtr != NULL )
74 mDestroyKeyExtensionPluginPtr( mPlugin );
81 int GetDaliKeyEnum( const char* keyName )
83 // If lookup table is not initialized, initialize lookup table
84 if( !mIsLookupTableInitialized )
86 InitializeLookupTable();
89 Lookup::const_iterator i = mLookup.find( keyName );
91 if( i == mLookup.end() )
93 // If cannot find target, find it at the extension
94 // If extension lookup table is not initialized, initialize extension lookup table
95 if( !mIsExtensionLookupTableInitialized )
97 InitializeExtensionLookupTable();
101 i = mExtensionLookup.find( keyName );
103 if( i == mExtensionLookup.end() )
109 return (*i).second.first;
114 return (*i).second.first;
118 const char* GetKeyName( int daliKeyCode )
120 // If lookup table is not initialized, initialize lookup table
121 if( !mIsLookupTableInitialized )
123 InitializeLookupTable();
126 for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
128 if( KeyLookupTable[i].daliKeyCode == daliKeyCode )
130 return KeyLookupTable[i].keyName;
134 // If extension lookup table is not initialized, initialize extension lookup table
135 if( !mIsExtensionLookupTableInitialized )
137 InitializeExtensionLookupTable();
140 for( size_t i = 0; i < mExtensionLookupCount ; ++i )
142 if( mExtensionKeyLookupTable[i].daliKeyCode == daliKeyCode )
144 return mExtensionKeyLookupTable[i].keyName;
151 bool IsDeviceButton( const char* keyName )
153 // If lookup table is not initialized, initialize lookup table
154 if( !mIsLookupTableInitialized )
156 InitializeLookupTable();
159 Lookup::const_iterator i = mLookup.find( keyName );
160 if( i == mLookup.end() )
162 // If cannot find target, find it at the extension.
163 // If extension lookup table is not initialized, initialize extension lookup table
164 if( !mIsExtensionLookupTableInitialized )
166 InitializeExtensionLookupTable();
170 i = mExtensionLookup.find( keyName );
172 if( i == mExtensionLookup.end() )
178 return (*i).second.second;
183 return (*i).second.second;
192 void InitializeLookupTable()
195 for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
197 mLookup[ KeyLookupTable[i].keyName ] = DaliKeyType( KeyLookupTable[i].daliKeyCode, KeyLookupTable[i].deviceButton );
200 mIsLookupTableInitialized = true;
203 void InitializeExtensionLookupTable()
205 // Try to load extension keys
207 mHandle = dlopen( KEY_EXTENSION_PLUGIN_SO, RTLD_NOW );
210 if( mHandle == NULL )
212 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get handle from libdali-key-extension.so\n" );
218 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "dlopen got error: %s \n", error );
222 mCreateKeyExtensionPluginPtr = reinterpret_cast< CreateKeyExtensionPluginFunction >( dlsym( mHandle, "CreateKeyExtensionPlugin" ) );
223 if( mCreateKeyExtensionPluginPtr == NULL )
225 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get CreateKeyExtensionPlugin function\n" );
229 mPlugin = mCreateKeyExtensionPluginPtr();
230 if( mPlugin == NULL )
232 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to create plugin object\n" );
236 mDestroyKeyExtensionPluginPtr = reinterpret_cast< DestroyKeyExtensionPluginFunction >( dlsym( mHandle, "DestroyKeyExtensionPlugin" ) );
237 if( mDestroyKeyExtensionPluginPtr == NULL )
239 DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get DestroyKeyExtensionPlugin function\n" );
243 mExtensionKeyLookupTable = mPlugin->GetKeyLookupTable();
244 mExtensionLookupCount = mPlugin->GetKeyLookupTableCount();
246 // Add extension keys to lookup
247 for( size_t i = 0; i < mExtensionLookupCount ; ++i )
249 mExtensionLookup[ mExtensionKeyLookupTable[i].keyName ] = DaliKeyType( mExtensionKeyLookupTable[i].daliKeyCode, mExtensionKeyLookupTable[i].deviceButton );
252 mIsExtensionLookupTableInitialized = true;
256 * compare function, to compare string by pointer
258 static bool cmpString( const char* a, const char* b)
260 return strcmp(a, b) < 0;
263 KeyExtensionPlugin::KeyLookup* mExtensionKeyLookupTable; ///< Lookup table for extension keys
264 Dali::KeyExtensionPlugin* mPlugin; ///< Key extension plugin handle
265 void* mHandle; ///< Handle for the loaded library
266 typedef Dali::KeyExtensionPlugin* (*CreateKeyExtensionPluginFunction)(); ///< Type of function pointer to get KeyExtensionPlugin object
267 typedef void (*DestroyKeyExtensionPluginFunction)( Dali::KeyExtensionPlugin* plugin ); ///< Type of function pointer to delete KeyExtensionPlugin object
268 CreateKeyExtensionPluginFunction mCreateKeyExtensionPluginPtr; ///< Function pointer to get KeyExtensionPlugin object
269 DestroyKeyExtensionPluginFunction mDestroyKeyExtensionPluginPtr; ///< Function pointer to delete KeyExtensionPlugin object
271 typedef std::pair< int, bool > DaliKeyType;
272 typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
274 Lookup mExtensionLookup;
275 size_t mExtensionLookupCount; ///< count of extension lookup table
276 bool mIsLookupTableInitialized; ///< flag for basic lookup table initialization
277 bool mIsExtensionLookupTableInitialized; ///< flag for extension lookup table initialization
279 KeyMap globalKeyLookup;
281 } // un-named name space
283 bool IsKey( const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
285 int key = globalKeyLookup.GetDaliKeyEnum( keyEvent.keyPressedName.c_str() );
286 return daliKey == key;
289 bool IsDeviceButton( const char* keyName )
291 return globalKeyLookup.IsDeviceButton( keyName );
294 const char* GetKeyName( Dali::KEY daliKey )
296 return globalKeyLookup.GetKeyName( daliKey );
299 int GetDaliKeyCode( const char* keyName )
301 return globalKeyLookup.GetDaliKeyEnum( keyName );
304 } // namespace KeyLookup
306 } // namespace Adaptor
308 } // namespace Internal