e_keyrouter: Remove deeply nested code and apply early returns 07/318207/1
authorTaeHyeon Jeong <thyeon.jeong@samsung.com>
Fri, 10 Jan 2025 02:43:10 +0000 (11:43 +0900)
committerTizen Window System <tizen.windowsystem@gmail.com>
Tue, 14 Jan 2025 07:14:43 +0000 (16:14 +0900)
- Refactored the code to remove deeply nested conditions by breaking down complex conditions and using early returns.
- Improved code readability and maintainability by reducing the overall nesting level.
- Ensured that the refactored code maintains the same functionality as the original implementation.

Change-Id: I754cd39f27716961c61089fb06ba04ff121a1a36

src/bin/inputmgr/e_keyrouter_list.c

index 29438774b077c9885b206fdd24bb846ffa9d68a7..cafe673e90ab75f79a33d7f7ce10cded835198d7 100644 (file)
@@ -6,41 +6,34 @@
 
 #include <tizen-extension-server-protocol.h>
 
-static int _e_keyrouter_find_duplicated_client(struct wl_resource *surface, struct wl_client *wc, uint32_t key, uint32_t mode);
+static int _e_keyrouter_find_duplicated_grabbed_key(struct wl_resource *surface, struct wl_client *wc, uint32_t key, uint32_t mode);
 
 /* Function for checking whether the key has been grabbed already by the same wl_surface or not */
 static int
-_e_keyrouter_find_duplicated_client(struct wl_resource *surface, struct wl_client *wc, uint32_t key, uint32_t mode)
+_e_keyrouter_find_duplicated_grabbed_key(struct wl_resource *surface, struct wl_client *wc, uint32_t key, uint32_t mode)
 {
    if (mode == TIZEN_KEYROUTER_MODE_EXCLUSIVE)
-      return TIZEN_KEYROUTER_ERROR_NONE;
+     return TIZEN_KEYROUTER_ERROR_NONE;
 
    g_rec_mutex_lock(&krt->grab_key_mutex);
 
-   if (surface)
-     {
-        if (e_keyrouter_hardkeys_is_key_grabbed_by_surface(krt->HardKeys, key, surface, mode))
-          {
-             KLDBG("The key(%d) is already grabbed same mode(%s) on the same wl_surface %p",
-                   key, e_keyrouter_util_mode_to_string(mode), surface);
-             g_rec_mutex_unlock(&krt->grab_key_mutex);
-             return TIZEN_KEYROUTER_ERROR_GRABBED_ALREADY;
-          }
-     }
-   else
-     {
-        if (e_keyrouter_hardkeys_is_key_grabbed_by_client(krt->HardKeys, key, wc, mode))
-          {
-             KLDBG("The key(%d) is already grabbed same mode(%s) on the same wl_client %p",
-                   key, e_keyrouter_util_mode_to_string(mode), wc);
-             g_rec_mutex_unlock(&krt->grab_key_mutex);
-             return TIZEN_KEYROUTER_ERROR_GRABBED_ALREADY;
-          }
-     }
+   Eina_Bool is_grabbed;
+   is_grabbed = surface ? e_keyrouter_hardkeys_is_key_grabbed_by_surface(krt->HardKeys, key, surface, mode)
+                        : e_keyrouter_hardkeys_is_key_grabbed_by_client(krt->HardKeys, key, wc, mode);
 
    g_rec_mutex_unlock(&krt->grab_key_mutex);
 
-   return TIZEN_KEYROUTER_ERROR_NONE;
+   if (!is_grabbed)
+     return TIZEN_KEYROUTER_ERROR_NONE;
+
+   if (surface)
+     KLDBG("The key(%d) is already grabbed same mode(%s) on the same wl_surface %p",
+           key, e_keyrouter_util_mode_to_string(mode), surface);
+   else
+     KLDBG("The key(%d) is already grabbed same mode(%s) on the same wl_client %p",
+           key, e_keyrouter_util_mode_to_string(mode), wc);
+
+   return TIZEN_KEYROUTER_ERROR_GRABBED_ALREADY;
 }
 
 /* Function for prepending a new key grab information in the keyrouting list */
@@ -49,7 +42,7 @@ e_keyrouter_prepend_to_keylist(struct wl_resource *surface, struct wl_client *wc
 {
    int res = TIZEN_KEYROUTER_ERROR_NONE;
 
-   res = _e_keyrouter_find_duplicated_client(surface, wc, key, mode);
+   res = _e_keyrouter_find_duplicated_grabbed_key(surface, wc, key, mode);
    CHECK_ERR_VAL(res);
 
    E_Keyrouter_Key_List_NodePtr new_keyptr = E_NEW(E_Keyrouter_Key_List_Node, 1);