Support extension keys 28/129928/13
authorminho.sun <minho.sun@samsung.com>
Thu, 18 May 2017 10:59:08 +0000 (19:59 +0900)
committerminho.sun <minho.sun@samsung.com>
Mon, 5 Jun 2017 04:16:33 +0000 (13:16 +0900)
Implement key-extension as Plugin to support extension keys.

For this, create new repository named "dali-extension"

Change-Id: If8177597658d99fda336f8d7bcf885bcd88018db
Signed-off-by: minho.sun <minho.sun@samsung.com>
adaptors/common/key-impl.cpp
adaptors/common/key-impl.h
adaptors/devel-api/adaptor-framework/key-extension-plugin.h [new file with mode: 0644]
adaptors/devel-api/file.list

index 75259ad..d50b52e 100644 (file)
 #include "key-impl.h"
 
 // EXTERNAL INCLUDES
+#include <dlfcn.h>
 #include <map>
 #include <string.h>
 #include <iostream>
 
 #include <dali/integration-api/debug.h>
 
-
 namespace Dali
 {
 
@@ -40,28 +40,74 @@ namespace KeyLookup
 
 namespace
 {
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gKeyExtensionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEY_EXTENSION");
+#endif
+
+// Path for loading extension keys
+const char* KEY_EXTENSION_PLUGIN_SO( "libdali-key-extension.so" );
 
 class KeyMap
 {
   public:
 
   KeyMap():
-  mLookup( cmpString )
+  mExtensionKeyLookupTable(NULL),
+  mPlugin(NULL),
+  mHandle(NULL),
+  mCreateKeyExtensionPluginPtr(NULL),
+  mDestroyKeyExtensionPluginPtr(NULL),
+  mLookup( cmpString ),
+  mExtensionLookup( cmpString ),
+  mExtensionLookupCount(0),
+  mIsLookupTableInitialized( false ),
+  mIsExtensionLookupTableInitialized( false )
   {
-    // create the lookup
-    for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
+  }
+
+  ~KeyMap()
+  {
+    if( mHandle != NULL )
     {
-      const KeyLookup&  keyLookup( KeyLookupTable[i] );
-      mLookup[ keyLookup.keyName  ] = DaliKeyType( keyLookup.daliKeyCode, keyLookup.deviceButton );
+      if( mDestroyKeyExtensionPluginPtr != NULL )
+      {
+        mDestroyKeyExtensionPluginPtr( mPlugin );
+      }
+
+      dlclose( mHandle );
     }
   }
 
-  int GetDaliKeyEnum( const char* keyName ) const
+  int GetDaliKeyEnum( const char* keyName )
   {
+    // If lookup table is not initialized, initialize lookup table
+    if( !mIsLookupTableInitialized )
+    {
+      InitializeLookupTable();
+    }
+
     Lookup::const_iterator i = mLookup.find( keyName );
+
     if( i == mLookup.end() )
     {
-      return -1;
+      // If cannot find target, find it at the extension
+      // If extension lookup table is not initialized, initialize extension lookup table
+      if( !mIsExtensionLookupTableInitialized )
+      {
+        InitializeExtensionLookupTable();
+      }
+
+      // Find at extension
+      i = mExtensionLookup.find( keyName );
+
+      if( i == mExtensionLookup.end() )
+      {
+        return -1;
+      }
+      else
+      {
+        return (*i).second.first;
+      }
     }
     else
     {
@@ -69,31 +115,143 @@ class KeyMap
     }
   }
 
-  const char* GetKeyName( int daliKeyCode ) const
+  const char* GetKeyName( int daliKeyCode )
   {
+    // If lookup table is not initialized, initialize lookup table
+    if( !mIsLookupTableInitialized )
+    {
+      InitializeLookupTable();
+    }
+
     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
     {
-      const KeyLookup& keyLookup( KeyLookupTable[i] );
-      if( keyLookup.daliKeyCode == daliKeyCode )
+      if( KeyLookupTable[i].daliKeyCode == daliKeyCode )
       {
-        return keyLookup.keyName;
+        return KeyLookupTable[i].keyName;
       }
     }
+
+    // If extension lookup table is not initialized, initialize extension lookup table
+    if( !mIsExtensionLookupTableInitialized )
+    {
+      InitializeExtensionLookupTable();
+    }
+
+    for( size_t i = 0; i < mExtensionLookupCount ; ++i )
+    {
+      if( mExtensionKeyLookupTable[i].daliKeyCode == daliKeyCode )
+      {
+        return mExtensionKeyLookupTable[i].keyName;
+      }
+    }
+
     return NULL;
   }
 
-  bool IsDeviceButton( const char* keyName ) const
+  bool IsDeviceButton( const char* keyName )
   {
+    // If lookup table is not initialized, initialize lookup table
+    if( !mIsLookupTableInitialized )
+    {
+      InitializeLookupTable();
+    }
+
     Lookup::const_iterator i = mLookup.find( keyName );
-    if ( i != mLookup.end() )
+    if( i == mLookup.end() )
+    {
+      // If cannot find target, find it at the extension.
+      // If extension lookup table is not initialized, initialize extension lookup table
+      if( !mIsExtensionLookupTableInitialized )
+      {
+        InitializeExtensionLookupTable();
+      }
+
+      // Find at extension
+      i = mExtensionLookup.find( keyName );
+
+      if( i == mExtensionLookup.end() )
+      {
+        return false;
+      }
+      else
+      {
+        return (*i).second.second;
+      }
+    }
+    else
     {
       return (*i).second.second;
     }
+
     return false;
   }
 
+
   private:
 
+  void InitializeLookupTable()
+  {
+    // create the lookup
+    for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
+    {
+      mLookup[ KeyLookupTable[i].keyName ] = DaliKeyType( KeyLookupTable[i].daliKeyCode, KeyLookupTable[i].deviceButton );
+    }
+
+    mIsLookupTableInitialized = true;
+  }
+
+  void InitializeExtensionLookupTable()
+  {
+    // Try to load extension keys
+    char* error = NULL;
+    mHandle = dlopen( KEY_EXTENSION_PLUGIN_SO, RTLD_NOW );
+    error = dlerror();
+
+    if( mHandle == NULL )
+    {
+      DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get handle from libdali-key-extension.so\n" );
+      return;
+    }
+
+    if( error != NULL )
+    {
+      DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "dlopen got error: %s  \n", error );
+      return;
+    }
+
+    mCreateKeyExtensionPluginPtr = reinterpret_cast< CreateKeyExtensionPluginFunction >( dlsym( mHandle, "CreateKeyExtensionPlugin" ) );
+    if( mCreateKeyExtensionPluginPtr == NULL )
+    {
+      DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get CreateKeyExtensionPlugin function\n" );
+      return;
+    }
+
+    mPlugin = mCreateKeyExtensionPluginPtr();
+    if( mPlugin == NULL )
+    {
+      DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to create plugin object\n" );
+      return;
+    }
+
+    mDestroyKeyExtensionPluginPtr = reinterpret_cast< DestroyKeyExtensionPluginFunction >( dlsym( mHandle, "DestroyKeyExtensionPlugin" ) );
+    if( mDestroyKeyExtensionPluginPtr == NULL )
+    {
+      DALI_LOG_INFO( gKeyExtensionLogFilter, Debug::General, "Failed to get DestroyKeyExtensionPlugin function\n" );
+      return;
+    }
+
+    mExtensionKeyLookupTable = mPlugin->GetKeyLookupTable();
+    mExtensionLookupCount = mPlugin->GetKeyLookupTableCount();
+
+    // Add extension keys to lookup
+    for( size_t i = 0; i < mExtensionLookupCount ; ++i )
+    {
+      mExtensionLookup[ mExtensionKeyLookupTable[i].keyName  ] = DaliKeyType( mExtensionKeyLookupTable[i].daliKeyCode, mExtensionKeyLookupTable[i].deviceButton );
+    }
+
+    mIsExtensionLookupTableInitialized = true;
+  }
+
   /**
    * compare function, to compare string by pointer
    */
@@ -102,12 +260,23 @@ class KeyMap
     return strcmp(a, b) < 0;
   }
 
+  KeyExtensionPlugin::KeyLookup* mExtensionKeyLookupTable;                               ///< Lookup table for extension keys
+  Dali::KeyExtensionPlugin* mPlugin;                                                     ///< Key extension plugin handle
+  void* mHandle;                                                                         ///< Handle for the loaded library
+  typedef Dali::KeyExtensionPlugin* (*CreateKeyExtensionPluginFunction)();               ///< Type of function pointer to get KeyExtensionPlugin object
+  typedef void (*DestroyKeyExtensionPluginFunction)( Dali::KeyExtensionPlugin* plugin ); ///< Type of function pointer to delete KeyExtensionPlugin object
+  CreateKeyExtensionPluginFunction mCreateKeyExtensionPluginPtr;                         ///< Function pointer to get KeyExtensionPlugin object
+  DestroyKeyExtensionPluginFunction mDestroyKeyExtensionPluginPtr;                       ///< Function pointer to delete KeyExtensionPlugin object
+
   typedef std::pair< int, bool > DaliKeyType;
   typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
   Lookup mLookup;
-
+  Lookup mExtensionLookup;
+  size_t mExtensionLookupCount;                                                          ///< count of extension lookup table
+  bool mIsLookupTableInitialized;                                                        ///< flag for basic lookup table initialization
+  bool mIsExtensionLookupTableInitialized;                                               ///< flag for extension lookup table initialization
 };
-const KeyMap globalKeyLookup;
+KeyMap globalKeyLookup;
 
 } // un-named name space
 
index 39a4174..25400f3 100644 (file)
@@ -20,6 +20,7 @@
 
 // INTERNAL INCLUDES
 #include <key.h>
+#include <key-extension-plugin.h>
 
 namespace Dali
 {
diff --git a/adaptors/devel-api/adaptor-framework/key-extension-plugin.h b/adaptors/devel-api/adaptor-framework/key-extension-plugin.h
new file mode 100644 (file)
index 0000000..b21456e
--- /dev/null
@@ -0,0 +1,74 @@
+#ifndef __DALI_KEY_EXTENSION_PLUGIN_H__
+#define __DALI_KEY_EXTENSION_PLUGIN_H__
+
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <cstdlib>
+
+namespace Dali
+{
+
+/**
+ * @brief KeyExtensionPlugin is an abstract interface, used by dali-adaptor to access key extension plugin.
+ * A concrete implementation must be created for each platform and provided as dynamic library.
+ * @SINCE_1_2.41
+ */
+class KeyExtensionPlugin
+{
+public:
+
+  struct KeyLookup
+  {
+    const char* keyName;          ///< XF86 key name
+    const int   daliKeyCode;      ///< Dali key code
+    const bool  deviceButton;     ///< Whether the key is from a button on the device
+  };
+
+  /**
+   * @brief Constructor.
+   * @SINCE_1_2.41
+   */
+  KeyExtensionPlugin(){}
+
+  /**
+   * @brief Destructor.
+   * @SINCE_1_2.41
+   */
+  virtual ~KeyExtensionPlugin(){}
+
+  /**
+   * @brief Get extension key lookup table.
+   *
+   * @SINCE_1_2.41
+   * @return Pointer of extension Key lookup table.
+   */
+  virtual KeyLookup* GetKeyLookupTable() = 0;
+
+  /**
+   * @brief Get count of extension key lookup table.
+   *
+   * @SINCE_1_2.41
+   * @return count of extension Key lookup table.
+   */
+  virtual std::size_t GetKeyLookupTableCount() = 0;
+};
+
+} // namespace Dali;
+
+#endif
index 17bc1bd..894b0a5 100644 (file)
@@ -61,6 +61,7 @@ devel_api_adaptor_framework_header_files = \
   $(adaptor_devel_api_dir)/adaptor-framework/tilt-sensor.h \
   $(adaptor_devel_api_dir)/adaptor-framework/video-player.h \
   $(adaptor_devel_api_dir)/adaptor-framework/video-player-plugin.h \
+  $(adaptor_devel_api_dir)/adaptor-framework/key-extension-plugin.h \
   $(adaptor_devel_api_dir)/adaptor-framework/virtual-keyboard.h \
   $(adaptor_devel_api_dir)/adaptor-framework/physical-keyboard.h \
   $(adaptor_devel_api_dir)/adaptor-framework/window-devel.h