add utilx_get_available_key_names() and utilx_get_key_symbol() 76/34276/3 tizen_3.0.2014.q4_common tizen_3.0.2015.q1_common accepted/tizen/common/20150127.100930 accepted/tizen/mobile/20150127.110945 accepted/tizen/tv/20150128.103619 accepted/tizen/wearable/20150127.105955 submit/tizen/20150123.091027 submit/tizen/20150127.011226
authorBoram Park <boram1288.park@samsung.com>
Thu, 22 Jan 2015 12:51:21 +0000 (21:51 +0900)
committerBoram Park <boram1288.park@samsung.com>
Fri, 23 Jan 2015 08:07:53 +0000 (17:07 +0900)
   The key-symbol can be different between each profiles and targets.
   So we will read /etc/X11/xkeysymbol.map and find a proper key-symbol
   for each profile's or target's key-name(ex. KEY-HOME, KEY-MENU, etc)

Change-Id: I9cbdff86fa8e24bd78b50a773158bdb35a055085

CMakeLists.txt
TC/testcase/utc_utilx_key_grab_test.c
utilX.h
x11.c

index 310f2de..fd8cc3c 100644 (file)
@@ -6,6 +6,7 @@ SET(HEADERS utilX.h utilX_ext.h)
 
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 SET(EXEC_PREFIX "\${prefix}")
+SET(SYSCONFDIR ${SYSCONF_INSTALL_DIR})
 SET(LIBDIR "\${prefix}/lib")
 SET(INCLUDEDIR "\${prefix}/include")
 SET(VERSION_MAJOR 1)
@@ -28,6 +29,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 
 ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"")
 ADD_DEFINITIONS("-DFACTORYFS=\"${PREFIX}\"")
+ADD_DEFINITIONS("-DSYSCONFDIR=\"${SYSCONFDIR}\"")
 #ADD_DEFINITIONS("-DDEBUG")
 
 ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS})
index 7f7bbb4..1dd8ff3 100644 (file)
@@ -99,12 +99,13 @@ static void utc_utilx_grab_key_excl_p(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_MEDIA");
 
-       grab_result = utilx_grab_key(disp, win, KEY_MEDIA, EXCLUSIVE_GRAB);
+       grab_result = utilx_grab_key(disp, win, symbol, EXCLUSIVE_GRAB);
        if( grab_result == 0 )
        {
                dts_pass("utilx_grab_key", "passed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_MEDIA);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
        }
@@ -118,12 +119,13 @@ static void utc_utilx_grab_key_top_p(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(disp, win, KEY_STOPCD, TOP_POSITION_GRAB);
+       grab_result = utilx_grab_key(disp, win, symbol, TOP_POSITION_GRAB);
        if( grab_result == 0 )
        {
                dts_pass("utilx_grab_key", "passed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
 
@@ -138,12 +140,13 @@ static void utc_utilx_grab_key_share_p(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(disp, win, KEY_STOPCD, SHARED_GRAB);
+       grab_result = utilx_grab_key(disp, win, symbol, SHARED_GRAB);
        if( grab_result == 0 )
        {
                dts_pass("utilx_grab_key", "passed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
        }
@@ -157,8 +160,9 @@ static void utc_utilx_grab_key_excl_n(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(NULL, win, KEY_STOPCD, EXCLUSIVE_GRAB);
+       grab_result = utilx_grab_key(NULL, win, symbol, EXCLUSIVE_GRAB);
        if( grab_result != 0 )
        {
                dts_pass("utilx_grab_key", "passed");
@@ -166,7 +170,7 @@ static void utc_utilx_grab_key_excl_n(void)
        else
        {
                dts_fail("utilx_grab_key", "failed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
 
@@ -177,8 +181,9 @@ static void utc_utilx_grab_key_top_n(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(NULL, win, KEY_STOPCD, TOP_POSITION_GRAB);
+       grab_result = utilx_grab_key(NULL, win, symbol, TOP_POSITION_GRAB);
        if( grab_result != 0 )
        {
                dts_pass("utilx_grab_key", "passed");
@@ -186,7 +191,7 @@ static void utc_utilx_grab_key_top_n(void)
        else
        {
                dts_fail("utilx_grab_key", "failed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
 
@@ -197,8 +202,9 @@ static void utc_utilx_grab_key_share_n(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(NULL, win, KEY_STOPCD, SHARED_GRAB);
+       grab_result = utilx_grab_key(NULL, win, symbol, SHARED_GRAB);
        if( grab_result != 0 )
        {
                dts_pass("utilx_grab_key", "passed");
@@ -206,7 +212,7 @@ static void utc_utilx_grab_key_share_n(void)
        else
        {
                dts_fail("utilx_grab_key", "failed");
-               ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+               ungrab_result = utilx_ungrab_key(disp, win, symbol);
                if( ungrab_result != 0)
                        dts_fail("utilx_grab_key", "ungrab failed");
 
@@ -217,13 +223,14 @@ static void utc_utilx_ungrab_key_p(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       grab_result = utilx_grab_key(disp, win, KEY_STOPCD, SHARED_GRAB);
+       grab_result = utilx_grab_key(disp, win, symbol, SHARED_GRAB);
        if( grab_result != 0 ){
                dts_fail("utilx_ungrab_key", "failed to grab the key");
        }
 
-       ungrab_result = utilx_ungrab_key(disp, win, KEY_STOPCD);
+       ungrab_result = utilx_ungrab_key(disp, win, symbol);
        if(ungrab_result == 0)
        {
                dts_pass("utilx_ungrab_key", "passed");
@@ -238,8 +245,9 @@ static void utc_utilx_ungrab_key_n(void)
 {
        int grab_result;
        int ungrab_result;
+       const char *symbol = utilx_get_key_symbol("KEY_STOPCD");
 
-       ungrab_result = utilx_ungrab_key(NULL, win, KEY_STOPCD);
+       ungrab_result = utilx_ungrab_key(NULL, win, symbol);
        if(ungrab_result != 0)
        {
                dts_pass("utilx_ungrab_key", "passed");
diff --git a/utilX.h b/utilX.h
index c956520..3919aa7 100755 (executable)
--- a/utilX.h
+++ b/utilX.h
@@ -513,7 +513,90 @@ void utilx_set_system_notification_level (Display* dpy, Window win, Utilx_Notifi
 Utilx_Notification_Level utilx_get_system_notification_level (Display* dpy, Window win);
 
 /**
- * @brief Grabs the key specfied by @a key_name for a window in the @a grab_mode.
+ * @brief Gets the string list and count of available key-names to grab.
+ * @details This function returns the newly-allocated list and count of available key-names to grab for a target device.
+ *          The key-name is unchangeable whatever you develope in different target.
+ *          However, key-symbol got by utilx_get_key_symbol() is changeable in different target.
+ * @since_tizen 3.0
+ * @remarks You should free the pointer of name list by free() to avoid memory-leak
+ * @param[out] count The number of avaiable keys for a target device
+ * @return newly-allocated list of available key strings, otherwise NULL.
+ * @see utilx_get_key_symbol()
+ * @par Example
+   @code
+
+  #include <utilX.h>
+
+  int
+  main (int argc, char* argv[])
+  {
+       const char **names;
+       int count = 0;
+       int i;
+
+       names = utilx_get_available_key_names (&count);
+       if (!names)
+               exit (-1);
+
+       for (i = 0; i < count; i++)
+       {
+               const char *symbol = utilx_get_key_symbol (names[i]);
+               printf ("name: %s, symbol: %s\n", names[i], symbol);
+       }
+
+       //should free names
+       free (names);
+
+       return 0;
+  }
+
+  @endcode
+ */
+const char** utilx_get_available_key_names (int *count);
+
+/**
+ * @brief Gets a key-symbol string for a available key-name string to grab.
+ * @details This function returns a key-symbol string for a available key-name to grab for a target device.
+ *          The key-name got by utilx_get_available_key_names() is unchangeable whatever you develope in different target.
+ *          However, key-symbol is changeable in different target.
+ * @since_tizen 3.0
+ * @param[in] key_name The name of a key got by utilx_get_available_key_names()
+ * @return key-symbol string if success, otherwise NULL.
+ * @see utilx_get_available_key_names(), utilx_grab_key(), utilx_ungrab_key()
+ * @par Example
+   @code
+
+  #include <utilX.h>
+
+  int
+  main (int argc, char* argv[])
+  {
+       const char **names;
+       int count = 0;
+       int i;
+
+       names = utilx_get_available_key_names (&count);
+       if (!names)
+               exit (-1);
+
+       for (i = 0; i < count; i++)
+       {
+               const char *symbol = utilx_get_key_symbol (names[i]);
+               printf ("name: %s, symbol: %s\n", names[i], symbol);
+       }
+
+       //should free names
+       free (names);
+
+       return 0;
+  }
+
+  @endcode
+ */
+const char* utilx_get_key_symbol (const char *key_name);
+
+/**
+ * @brief Grabs the key specfied by @a key_symbol for a window in the @a grab_mode.
  *
  * @details This function establishes a grab of the specified key for the specified window.\n
  *          Once a key is grabbed, all events originating from the key will only be reported to the specfied window.\n
@@ -528,183 +611,143 @@ Utilx_Notification_Level utilx_get_system_notification_level (Display* dpy, Wind
  *          A trial for choosing a proper grab mode will be needed.
  * @param[in] dpy The connection to the X server
  * @param[in] win The window to grab a key
- * @param[in] key_name The name of a key in the string (ex> #KEY_VOLUMEUP, #KEY_VOLUMEDOWN, #KEY_SEND, and so on)
+ * @param[in] key_symbol The symbol of a key in the string
  * @param[in] grab_mode The grab mode (such as #EXCLUSIVE_GRAB, #TOP_POSITION_GRAB, and #SHARED_GRAB)
  * @return @c 0 on success,
  *         otherwise failure
  * @pre This API must be called after the window 'win' has been mapped.
  * @post This API adds/changes the window property related to the grabbed key.
- * @see utilx_ungrab_key()
+ * @see utilx_ungrab_key(), utilx_get_available_key_names(), utilx_get_key_symbol()
  * @par Example (using X11 APIs)
   @code
-
-  // EXCLUSIVE_GRAB //
-
   #include <X11/Xlib.h>
   #include <utilX.h>
 
-  int main()
-  {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       int grab_result;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
-
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     grab_result = utilx_grab_key(disp, w, KEY_POWER, EXCLUSIVE_GRAB);
-                     if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
-                           return -1;
-                     break;
-             }
-             ...
-       }
-       ...
+  static const char* key_symbol;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
+  static int
+  get_key_symbol (void)
+  {
+      const char **names;
+      int i, count = 0;
+
+      names = utilx_get_available_key_names (&count);
+      if (!names)
+          return 0;
+
+      for (i = 0; i < count; i++)
+          if (!strcmp (names[i], "KEY_UP"))
+          {
+              key_symbol = utilx_get_key_symbol ("KEY_UP");
+              printf ("found: key(%s,%s)\n", "KEY_UP", key_symbol);
+              free (names);  // should free
+              return 1;
+          }
+
+      free (names); // should free
+      return 0;
   }
 
-  // TOP_POSITION_GRAB //
-
-  #include <X11/Xlib.h>
-  #include <utilX.h>
-
   int main()
   {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
+      Display *disp = XOpenDisplay(NULL);
+      XEvent e;
+      int grab_result;
+      Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(disp,0), WhitePixel(disp,0));
+      XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask | ExposureMask);
+      XMapWindow(disp, w);
 
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     utilx_grab_key(disp, w, KEY_POWER, TOP_POSITION_GRAB);
-                     break;
-             }
-             ...
-       }
-       ...
+      if (!get_key_symbol ())
+          return -1;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
-  }
+      while(1)
+      {
+          XNextEvent(disp, &e);
 
-  // SHARED_GRAB //
+          switch(e.type)
+          {
+          case MapNotify:
+              //EXCLUSIVE_GRAB
+              grab_result = utilx_grab_key(disp, w, key_symbol, EXCLUSIVE_GRAB);
+              if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
+                  return -1;
 
-  #include <X11/Xlib.h>
-  #include <utilX.h>
+              //utilx_grab_key(disp, w, key_symbol, TOP_POSITION_GRAB);
+              //utilx_grab_key(disp, w, key_symbol, SHARED_GRAB);
 
-  int main()
-  {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
+              break;
+          }
+          ...
+      }
 
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     utilx_grab_key(disp, w, KEY_POWER, SHARED_GRAB);
-                     break;
-             }
-             ...
-       }
-       ...
+      ...
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
-  }
+      utilx_ungrab_key(disp, w, key_symbol);
 
+      return 0;
+  }
   @endcode
    * @par Example (using EFL APIs)
   @code
-
-  // EXCLUSIVE_GRAB //
-
   #include <utilX.h>
   #include <Ecore_Evas.h>
   #include <Ecore_X.h>
 
-  int main()
-  {
-       ...
-
-       Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
-
-       int grab_result = utilx_grab_key(disp, win, KEY_POWER, EXCLUSIVE_GRAB);
-       if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
-               return -1;
-
-       ...
+  static const char* key_symbol;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
-       return 0;
+  static int
+  get_key_symbol (void)
+  {
+      const char **names;
+      int i, count = 0;
+
+      names = utilx_get_available_key_names (&count);
+      if (!names)
+          return 0;
+
+      for (i = 0; i < count; i++)
+          if (!strcmp (names[i], "KEY_UP"))
+          {
+              key_symbol = utilx_get_key_symbol ("KEY_UP");
+              printf ("found: key(%s,%s)\n", "KEY_UP", key_symbol);
+              free (names);  // should free
+              return 1;
+          }
+
+      free (names); // should free
+      return 0;
   }
 
-  // TOP_POSITION_GRAB //
-
-  #include <utilX.h>
-  #include <Ecore_Evas.h>
-  #include <Ecore_X.h>
-
   int main()
   {
        ...
 
        Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
-
-       utilx_grab_key(disp, win, KEY_POWER, TOP_POSITION_GRAB);
-
-       ...
+       Ecore_X_Window w = ecore_evas_software_x11_window_get(ee);
+       int grab_result;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
-       return 0;
-  }
+       get_key_symbol ();
 
-  // SHARED_GRAB //
+      //EXCLUSIVE_GRAB
+      grab_result = utilx_grab_key(disp, w, key_symbol, EXCLUSIVE_GRAB);
+      if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
+          return -1;
 
-  #include <utilX.h>
-  #include <Ecore_Evas.h>
-  #include <Ecore_X.h>
+      //utilx_grab_key(disp, w, key_symbol, TOP_POSITION_GRAB);
+      //utilx_grab_key(disp, w, key_symbol, SHARED_GRAB);
 
-  int main()
-  {
        ...
 
-       Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
-
-       utilx_grab_key(disp, win, KEY_POWER, SHARED_GRAB);
-
-       ...
+      utilx_ungrab_key(disp, w, key_symbol); //Ungrab whenever a user wants to
 
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
        return 0;
   }
   @endcode
  */
-int utilx_grab_key (Display* dpy, Window win, const char* key_name, int grab_mode);
+int utilx_grab_key (Display* dpy, Window win, const char* key_symbol, int grab_mode);
 
 /**
- * @brief Ungrabs the key specfied by @a key_name for a window.
+ * @brief Ungrabs the key specfied by @a key_symbol for a window.
  *
  * @details This function releases the already established grab of the specfied key for the specified window.\n
  *          Once the grab of the key is released, delivery of the key events for the specfied window is going to be stopped.
@@ -714,179 +757,139 @@ int utilx_grab_key (Display* dpy, Window win, const char* key_name, int grab_mod
  * @since_tizen 2.3
  * @param[in] dpy The connection to the X server
  * @param[in] win The window to grab a key
- * @param[in] key_name The name of a key in the string (ex> #KEY_VOLUMEUP, #KEY_VOLUMEDOWN, #KEY_SEND, and so on)
+ * @param[in] key_symbol The symbol of a key in the string got by utilx_get_key_symbol()
  * @return @c 0 on success,
  *         otherwise failure
  * @pre This API must be called after the window 'win' is mapped.
  * @post This API changes/removes the window property related to the grabbed key.
- * @see utilx_grab_key()
+ * @see utilx_grab_key(), utilx_get_available_key_names(), utilx_get_key_symbol()
  * @par Example (using X11 APIs)
   @code
-
-  // EXCLUSIVE_GRAB //
-
   #include <X11/Xlib.h>
   #include <utilX.h>
 
-  int main()
-  {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       int grab_result;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
-
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     grab_result = utilx_grab_key(disp, w, KEY_POWER, EXCLUSIVE_GRAB);
-                     if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
-                           return -1;
-                     break;
-             }
-             ...
-       }
-       ...
+  static const char* key_symbol;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
+  static int
+  get_key_symbol (void)
+  {
+      const char **names;
+      int i, count = 0;
+
+      names = utilx_get_available_key_names (&count);
+      if (!names)
+          return 0;
+
+      for (i = 0; i < count; i++)
+          if (!strcmp (names[i], "KEY_UP"))
+          {
+              key_symbol = utilx_get_key_symbol ("KEY_UP");
+              printf ("found: key(%s,%s)\n", "KEY_UP", key_symbol);
+              free (names);  // should free
+              return 1;
+          }
+
+      free (names); // should free
+      return 0;
   }
 
-  // TOP_POSITION_GRAB //
-
-  #include <X11/Xlib.h>
-  #include <utilX.h>
-
   int main()
   {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
+      Display *disp = XOpenDisplay(NULL);
+      XEvent e;
+      int grab_result;
+      Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(disp,0), WhitePixel(disp,0));
+      XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask | ExposureMask);
+      XMapWindow(disp, w);
 
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     utilx_grab_key(disp, w, KEY_POWER, TOP_POSITION_GRAB);
-                     break;
-             }
-             ...
-       }
-       ...
+      if (!get_key_symbol ())
+          return -1;
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
-  }
+      while(1)
+      {
+          XNextEvent(disp, &e);
 
-  // SHARED_GRAB //
+          switch(e.type)
+          {
+          case MapNotify:
+              //EXCLUSIVE_GRAB
+              grab_result = utilx_grab_key(disp, w, key_symbol, EXCLUSIVE_GRAB);
+              if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
+                  return -1;
 
-  #include <X11/Xlib.h>
-  #include <utilX.h>
+              //utilx_grab_key(disp, w, key_symbol, TOP_POSITION_GRAB);
+              //utilx_grab_key(disp, w, key_symbol, SHARED_GRAB);
 
-  int main()
-  {
-       Display *disp = XOpenDisplay(NULL);
-       XEvent e;
-       Window w = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 5,5, 470, 780, 2, BlackPixel(d,0), WhitePixel(d,0));
-       XSelectInput(disp, w, StructureNotifyMask | KeyPressMask | KeyReleaseMask);
-       XMapWindow(disp, w);
+              break;
+          }
+          ...
+      }
 
-       while(1)
-       {
-             XNextEvent(disp, &e);
-             switch(e.type)
-             {
-                    case MapNotify:
-                     utilx_grab_key(disp, w, KEY_POWER, SHARED_GRAB);
-                     break;
-             }
-             ...
-       }
-       ...
+      ...
 
-       utilx_ungrab_key(disp, win, KEY_POWER);
-       return 0;
-  }
+      utilx_ungrab_key(disp, w, key_symbol);
 
+      return 0;
+  }
   @endcode
    * @par Example (using EFL APIs)
   @code
-
-  // EXCLUSIVE_GRAB //
-
   #include <utilX.h>
   #include <Ecore_Evas.h>
   #include <Ecore_X.h>
 
-  int main()
-  {
-       ...
-
-       Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
+  static const char* key_symbol;
 
-       int grab_result = utilx_grab_key(disp, win, KEY_POWER, EXCLUSIVE_GRAB);
-       if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
-               return -1;
-       ...
-
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
-       return 0;
+  static int
+  get_key_symbol (void)
+  {
+      const char **names;
+      int i, count = 0;
+
+      names = utilx_get_available_key_names (&count);
+      if (!names)
+          return 0;
+
+      for (i = 0; i < count; i++)
+          if (!strcmp (names[i], "KEY_UP"))
+          {
+              key_symbol = utilx_get_key_symbol ("KEY_UP");
+              printf ("found: key(%s,%s)\n", "KEY_UP", key_symbol);
+              free (names);  // should free
+              return 1;
+          }
+
+      free (names); // should free
+      return 0;
   }
 
-  // TOP_POSITION_GRAB //
-
-  #include <utilX.h>
-  #include <Ecore_Evas.h>
-  #include <Ecore_X.h>
-
   int main()
   {
        ...
 
        Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
-
-       utilx_grab_key(disp, win, KEY_POWER, TOP_POSITION_GRAB);
-
-       ...
-
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
+       Ecore_X_Window w = ecore_evas_software_x11_window_get(ee);
+       int grab_result;
 
-       return 0;
-  }
+       get_key_symbol ();
 
-  // SHARED_GRAB //
+      //EXCLUSIVE_GRAB
+      grab_result = utilx_grab_key(disp, w, key_symbol, EXCLUSIVE_GRAB);
+      if( EXCLUSIVE_GRABBED_ALREADY == grab_result )
+          return -1;
 
-  #include <utilX.h>
-  #include <Ecore_Evas.h>
-  #include <Ecore_X.h>
+      //utilx_grab_key(disp, w, key_symbol, TOP_POSITION_GRAB);
+      //utilx_grab_key(disp, w, key_symbol, SHARED_GRAB);
 
-  int main()
-  {
        ...
 
-       Ecore_X_Display* disp = ecore_x_display_get();
-       Ecore_X_Window win = ecore_evas_software_x11_window_get(ee);
-
-       utilx_grab_key(disp, win, KEY_POWER, SHARED_GRAB);
-
-       ...
+      utilx_ungrab_key(disp, w, key_symbol); //Ungrab whenever a user wants to
 
-       utilx_ungrab_key(disp, win, KEY_POWER);//Ungrab whenever a user wants to
        return 0;
   }
   @endcode
  */
-int utilx_ungrab_key (Display* dpy, Window win, const char* key_name);
+int utilx_ungrab_key (Display* dpy, Window win, const char* key_symbol);
 
 /**
  * @brief Sets the window's opaque state.
diff --git a/x11.c b/x11.c
index 4b3cb17..2dfc23a 100644 (file)
--- a/x11.c
+++ b/x11.c
 #include <X11/extensions/Xvproto.h>
 #include <X11/extensions/Xdamage.h>
 #include <X11/extensions/XShm.h>
+#include <X11/Xresource.h>
 #include <xf86drm.h>
 #include <tbm_bufmgr.h>
 #include <dri2.h>
 
-
 #define UTILX_DEBUG 0
 #if UTILX_DEBUG
 #define UTILX_TRACE  printf
@@ -500,6 +500,174 @@ out:
        return result;
 }
 
+#ifndef SYSCONFDIR
+#define SYSCONFDIR "/etc/"
+#endif
+#define KEYMAP_FILE    SYSCONFDIR"/X11/xkeysymbol.map"
+#define is_space(c) ((c)==' '||(c)=='\t'||(c) =='\n')
+
+typedef struct _Symbol_Entry
+{
+       char name[64];
+       char symbol[64];
+       XrmQuark quark;
+       struct _Symbol_Entry *next;
+} Symbol_Entry;
+
+static Symbol_Entry *symbol_list;
+static int symbol_list_len;
+
+static char*
+_utilx_strip_whitespace(char *s)
+{
+    int i;
+
+    while (s[0] && is_space(s[0]))
+        s++;
+
+    i = strlen(s) - 1;
+    while (i >= 0 && is_space(s[i]))
+        s[i--] = 0;
+
+    return s;
+}
+
+static int
+_utilx_check_key_symbol_list (char *path)
+{
+       FILE *f;
+    char *stripped, *comma, *key, *value;
+    char buf[256];
+       int line;
+       Symbol_Entry *cursor;
+
+       if (symbol_list)
+               return 1;
+
+       f = fopen(path, "r");
+       if (!f) {
+        fprintf (stderr, "[utilx] '%s' open failed: %s\n", path, strerror(errno));
+               return 0;
+       }
+
+       line = 0;
+       symbol_list_len = 0;
+
+       cursor = NULL;
+    while (fgets(buf, sizeof(buf), f)) {
+               Symbol_Entry *entry;
+
+        line++;
+
+        stripped = _utilx_strip_whitespace (buf);
+        if (stripped[0] == '#' || stripped[0] == 0)
+            continue;
+
+        comma = strchr(stripped, ',');
+        if (!comma)
+               {
+            fprintf(stderr, "[utilx] Syntax error at %s line %d\n", path, line);
+            continue;
+        }
+
+        *comma = 0;
+        key   = _utilx_strip_whitespace (stripped);
+        value = _utilx_strip_whitespace (comma + 1);
+        if (!key[0])
+               {
+            fprintf (stderr, "[utilx] Missing key at %s line %d\n", path, line);
+            continue;
+        }
+        if (!value[0])
+               {
+            fprintf (stderr, "[utilx] Missing value at %s line %d\n", path, line);
+            continue;
+        }
+
+               entry = calloc (1, sizeof (Symbol_Entry));
+               if (!entry)
+                       continue;
+
+               symbol_list_len++;
+
+               if (!symbol_list)
+                       cursor = symbol_list = entry;
+               else if (cursor)
+               {
+                       cursor->next = entry;
+                       cursor = entry;
+               }
+
+               snprintf (entry->name, sizeof (entry->name), "%s", key);
+               snprintf (entry->symbol, sizeof (entry->symbol), "%s", value);
+
+               entry->quark = XrmStringToQuark (key);
+    }
+
+       fclose(f);
+
+       return 1;
+}
+
+API const char** utilx_get_available_key_names (int *count)
+{
+       char **names;
+       Symbol_Entry *entry;
+       int i;
+
+       if (!count)
+       {
+               fprintf(stderr, "[%s] 'count' param is NULL.\n", __FUNCTION__);
+               return NULL;
+       }
+
+       *count = 0;
+
+       if (!_utilx_check_key_symbol_list (KEYMAP_FILE))
+               return NULL;
+
+       names = calloc (symbol_list_len, sizeof (char*));
+       if (!names)
+       {
+               fprintf(stderr, "[%s] calloc failed.\n", __FUNCTION__);
+               return NULL;
+       }
+
+       *count = symbol_list_len;
+
+       i = 0;
+       entry = symbol_list;
+       while (entry && i < symbol_list_len)
+       {
+               names[i++] = entry->name;
+               entry = entry->next;
+       }
+
+       return (const char**)names;
+}
+
+API const char* utilx_get_key_symbol (const char *key_name)
+{
+       Symbol_Entry *entry;
+       XrmQuark quark;
+
+       if (!_utilx_check_key_symbol_list (KEYMAP_FILE))
+               return NULL;
+
+       quark = XrmStringToQuark (key_name);
+       entry = symbol_list;
+
+       while (entry)
+       {
+               if (entry->quark == quark)
+                       return (const char*)entry->symbol;
+
+               entry = entry->next;
+       }
+
+       return NULL;
+}
+
 API int utilx_grab_key (Display* disp, Window win, const char* key, int grab_mode)
 {
        unsigned long cnt;
@@ -515,6 +683,12 @@ API int utilx_grab_key (Display* disp, Window win, const char* key, int grab_mod
                return -1;
        }
 
+       if( NULL == key )
+       {
+               fprintf(stderr, "[%s] key is NULL\n", __FUNCTION__);
+               return -1;
+       }
+
        if (_atom_grab_key == None) {
                _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
        }
@@ -593,6 +767,12 @@ API int utilx_ungrab_key (Display* disp, Window win, const char* key)
                return -1;
        }
 
+       if( NULL == key )
+       {
+               fprintf(stderr, "[%s] key is NULL\n", __FUNCTION__);
+               return -1;
+       }
+
        if (_atom_grab_key == None) {
                _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
        }