2 * Copyright (c) 2021 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.
19 #include <dali/internal/input/common/key-impl.h>
27 #include <dali/integration-api/debug.h>
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gKeyExtensionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEY_EXTENSION");
43 // Path for loading extension keys
44 const char* KEY_EXTENSION_PLUGIN_SO("libdali2-key-extension.so");
50 : mExtensionKeyLookupTable(NULL),
53 mCreateKeyExtensionPluginPtr(NULL),
54 mDestroyKeyExtensionPluginPtr(NULL),
56 mExtensionLookup(cmpString),
57 mExtensionLookupCount(0),
58 mIsLookupTableInitialized(false),
59 mIsExtensionLookupTableInitialized(false)
67 if(mDestroyKeyExtensionPluginPtr != NULL)
69 mDestroyKeyExtensionPluginPtr(mPlugin);
76 int GetDaliKeyEnum(const char* keyName)
78 // If lookup table is not initialized, initialize lookup table
79 if(!mIsLookupTableInitialized)
81 InitializeLookupTable();
84 Lookup::const_iterator i = mLookup.find(keyName);
86 if(i == mLookup.end())
88 // If cannot find target, find it at the extension
89 // If extension lookup table is not initialized, initialize extension lookup table
90 if(!mIsExtensionLookupTableInitialized)
92 InitializeExtensionLookupTable();
96 i = mExtensionLookup.find(keyName);
98 if(i == mExtensionLookup.end())
104 return (*i).second.first;
109 return (*i).second.first;
113 const char* GetKeyName(int daliKeyCode)
115 // If lookup table is not initialized, initialize lookup table
116 if(!mIsLookupTableInitialized)
118 InitializeLookupTable();
121 for(size_t i = 0; i < KEY_LOOKUP_COUNT; ++i)
123 if(KeyLookupTable[i].daliKeyCode == daliKeyCode)
125 return KeyLookupTable[i].keyName;
129 // If extension lookup table is not initialized, initialize extension lookup table
130 if(!mIsExtensionLookupTableInitialized)
132 InitializeExtensionLookupTable();
135 for(size_t i = 0; i < mExtensionLookupCount; ++i)
137 if(mExtensionKeyLookupTable[i].daliKeyCode == daliKeyCode)
139 return mExtensionKeyLookupTable[i].keyName;
146 bool IsDeviceButton(const char* keyName)
148 // If lookup table is not initialized, initialize lookup table
149 if(!mIsLookupTableInitialized)
151 InitializeLookupTable();
154 Lookup::const_iterator i = mLookup.find(keyName);
155 if(i == mLookup.end())
157 // If cannot find target, find it at the extension.
158 // If extension lookup table is not initialized, initialize extension lookup table
159 if(!mIsExtensionLookupTableInitialized)
161 InitializeExtensionLookupTable();
165 i = mExtensionLookup.find(keyName);
167 if(i == mExtensionLookup.end())
173 return (*i).second.second;
178 return (*i).second.second;
185 void InitializeLookupTable()
188 for(size_t i = 0; i < KEY_LOOKUP_COUNT; ++i)
190 mLookup[KeyLookupTable[i].keyName] = DaliKeyType(KeyLookupTable[i].daliKeyCode, KeyLookupTable[i].deviceButton);
193 mIsLookupTableInitialized = true;
196 void InitializeExtensionLookupTable()
198 // Try to load extension keys
200 mHandle = dlopen(KEY_EXTENSION_PLUGIN_SO, RTLD_NOW);
205 DALI_LOG_INFO(gKeyExtensionLogFilter, Debug::General, "Failed to get handle from libdali2-key-extension.so\n");
211 DALI_LOG_INFO(gKeyExtensionLogFilter, Debug::General, "dlopen got error: %s \n", error);
215 mCreateKeyExtensionPluginPtr = reinterpret_cast<CreateKeyExtensionPluginFunction>(dlsym(mHandle, "CreateKeyExtensionPlugin"));
216 if(mCreateKeyExtensionPluginPtr == NULL)
218 DALI_LOG_INFO(gKeyExtensionLogFilter, Debug::General, "Failed to get CreateKeyExtensionPlugin function\n");
222 mPlugin = mCreateKeyExtensionPluginPtr();
225 DALI_LOG_INFO(gKeyExtensionLogFilter, Debug::General, "Failed to create plugin object\n");
229 mDestroyKeyExtensionPluginPtr = reinterpret_cast<DestroyKeyExtensionPluginFunction>(dlsym(mHandle, "DestroyKeyExtensionPlugin"));
230 if(mDestroyKeyExtensionPluginPtr == NULL)
232 DALI_LOG_INFO(gKeyExtensionLogFilter, Debug::General, "Failed to get DestroyKeyExtensionPlugin function\n");
236 mExtensionKeyLookupTable = mPlugin->GetKeyLookupTable();
237 mExtensionLookupCount = mPlugin->GetKeyLookupTableCount();
239 // Add extension keys to lookup
240 for(size_t i = 0; i < mExtensionLookupCount; ++i)
242 mExtensionLookup[mExtensionKeyLookupTable[i].keyName] = DaliKeyType(mExtensionKeyLookupTable[i].daliKeyCode, mExtensionKeyLookupTable[i].deviceButton);
245 mIsExtensionLookupTableInitialized = true;
249 * compare function, to compare string by pointer
251 static bool cmpString(const char* a, const char* b)
253 return strcmp(a, b) < 0;
256 KeyExtensionPlugin::KeyLookup* mExtensionKeyLookupTable; ///< Lookup table for extension keys
257 Dali::KeyExtensionPlugin* mPlugin; ///< Key extension plugin handle
258 void* mHandle; ///< Handle for the loaded library
259 typedef Dali::KeyExtensionPlugin* (*CreateKeyExtensionPluginFunction)(); ///< Type of function pointer to get KeyExtensionPlugin object
260 typedef void (*DestroyKeyExtensionPluginFunction)(Dali::KeyExtensionPlugin* plugin); ///< Type of function pointer to delete KeyExtensionPlugin object
261 CreateKeyExtensionPluginFunction mCreateKeyExtensionPluginPtr; ///< Function pointer to get KeyExtensionPlugin object
262 DestroyKeyExtensionPluginFunction mDestroyKeyExtensionPluginPtr; ///< Function pointer to delete KeyExtensionPlugin object
264 typedef std::pair<int, bool> DaliKeyType;
266 typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool (*)(char const* a, char const* b)> Lookup;
269 Lookup mExtensionLookup;
270 size_t mExtensionLookupCount; ///< count of extension lookup table
271 bool mIsLookupTableInitialized; ///< flag for basic lookup table initialization
272 bool mIsExtensionLookupTableInitialized; ///< flag for extension lookup table initialization
274 KeyMap globalKeyLookup;
278 bool IsKey(const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
280 int key = globalKeyLookup.GetDaliKeyEnum(keyEvent.GetKeyName().c_str());
281 return daliKey == key;
284 bool IsDeviceButton(const char* keyName)
286 return globalKeyLookup.IsDeviceButton(keyName);
289 const char* GetKeyName(Dali::KEY daliKey)
291 return globalKeyLookup.GetKeyName(daliKey);
294 int GetDaliKeyCode(const char* keyName)
296 return globalKeyLookup.GetDaliKeyEnum(keyName);
299 } // namespace KeyLookup
301 } // namespace Adaptor
303 } // namespace Internal