screenshot: refactoring screenshot init 33/280333/2
authorJunkyeong Kim <jk0430.kim@samsung.com>
Sat, 27 Aug 2022 04:03:13 +0000 (13:03 +0900)
committerJunkyeong Kim <jk0430.kim@samsung.com>
Sat, 27 Aug 2022 04:08:06 +0000 (13:08 +0900)
make a function for tizen_screenshooter binding and destroy.

Change-Id: I25ca2e6100b6de676a8c9761e690a0b5b4536636
Signed-off-by: Junkyeong Kim <jk0430.kim@samsung.com>
src/efl_util.c

index 520e97e..4a837c5 100644 (file)
@@ -2026,73 +2026,98 @@ _screenshot_mutex_unlock(void)
    pthread_mutex_unlock(&shot_lock);
 }
 
-API efl_util_screenshot_h
-efl_util_screenshot_initialize(int width, int height)
+static Eina_Bool
+_efl_util_wl_screenshooter_init()
 {
-   efl_util_screenshot_h screenshot = NULL;
    struct wl_display *display_wrapper = NULL;
-   struct wl_registry *reg = NULL;
+   struct wl_registry *registry = NULL;
    int ret = 0;
 
-   if (width <= 0 || height <= 0)
-     {
-        set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
-        return NULL;
-     }
+   if (_wl_init() == EINA_FALSE)
+     return EINA_FALSE;
 
-   _screenshot_mutex_lock();
+   wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
 
-   if (!g_screenshot)
-     {
-        screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
-        EINA_SAFETY_ON_NULL_GOTO(screenshot, fail_memory);
-     }
+   display_wrapper = wl_proxy_create_wrapper(_eflutil.wl.dpy);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(display_wrapper, EINA_FALSE);
 
-   if (!_eflutil.wl.shot.tz_screenshooter)
-     {
-        ret = _wl_init();
-        if (ret == (int)EINA_FALSE)
-          {
-             set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
-             _screenshot_mutex_unlock();
-             if (screenshot)
-                free(screenshot);
-             return NULL;
-          }
-        wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
+   _eflutil.wl.shot.queue = wl_display_create_queue(_eflutil.wl.dpy);
+   EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.queue, fail_create_queue);
+
+   wl_proxy_set_queue((struct wl_proxy *)display_wrapper, _eflutil.wl.shot.queue);
 
-        display_wrapper = wl_proxy_create_wrapper(_eflutil.wl.dpy);
-        EINA_SAFETY_ON_NULL_GOTO(display_wrapper, fail_memory);
+   registry = wl_display_get_registry(display_wrapper);
+   EINA_SAFETY_ON_NULL_GOTO(registry, fail_get_registry);
 
-        _eflutil.wl.shot.queue = wl_display_create_queue(_eflutil.wl.dpy);
-        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.queue, fail_memory);
+   wl_registry_add_listener(registry, &_wl_reg_screenshooter_listener, NULL);
 
-        wl_proxy_set_queue((struct wl_proxy *)display_wrapper, _eflutil.wl.shot.queue);
+   ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.shot.queue);
+   EINA_SAFETY_ON_TRUE_GOTO(ret == -1, fail_roundtrip);
+   EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tz_screenshooter, fail_roundtrip);
+
+   _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
+   EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_tbm_client_init);
+
+   wl_registry_destroy(registry);
+   wl_proxy_wrapper_destroy(display_wrapper);
+
+   if (_eflutil.wl.shot.noti == 0)
+     {
+        fprintf(stderr, "[screenshot] fail: privilege error\n"); /* LCOV_EXCL_LINE */
+        return EINA_FALSE;
+     }
 
-        reg = wl_display_get_registry(display_wrapper);
-        wl_proxy_wrapper_destroy(display_wrapper);
-        display_wrapper = NULL;
-        EINA_SAFETY_ON_NULL_GOTO(reg, fail_init);
+   return EINA_TRUE;
 
-        wl_registry_add_listener(reg, &_wl_reg_screenshooter_listener, NULL);
+fail_tbm_client_init:
+   tizen_screenshooter_destroy(_eflutil.wl.shot.tz_screenshooter);
+   _eflutil.wl.shot.tz_screenshooter = NULL;
+fail_roundtrip:
+   wl_registry_destroy(registry);
+fail_get_registry:
+   wl_event_queue_destroy(_eflutil.wl.shot.queue);
+   _eflutil.wl.shot.queue = NULL;
+fail_create_queue:
+   wl_proxy_wrapper_destroy(display_wrapper);
 
-        ret = wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.shot.queue);
-        EINA_SAFETY_ON_TRUE_GOTO(ret == -1, fail_init);
-        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tz_screenshooter, fail_init);
+   return EINA_FALSE;
+}
 
-        _eflutil.wl.shot.tbm_client = wayland_tbm_client_init(_eflutil.wl.dpy);
-        EINA_SAFETY_ON_NULL_GOTO(_eflutil.wl.shot.tbm_client, fail_init);
+static void
+_efl_util_wl_screenshooter_deinit()
+{
+   if (_eflutil.wl.shot.tbm_client)
+     {
+        wayland_tbm_client_deinit(_eflutil.wl.shot.tbm_client);
+        _eflutil.wl.shot.tbm_client = NULL;
+     }
 
-        wl_registry_destroy(reg);
-        reg = NULL;
+   if (_eflutil.wl.shot.tz_screenshooter)
+     {
+        tizen_screenshooter_destroy(_eflutil.wl.shot.tz_screenshooter);
+        _eflutil.wl.shot.tz_screenshooter = NULL;
      }
 
-   if (_eflutil.wl.shot.noti == 0)
+   if (_eflutil.wl.shot.queue)
      {
-        fprintf(stderr, "[screenshot] fail: privilege error\n"); /* LCOV_EXCL_LINE */
-        goto fail_init;
+        wl_event_queue_destroy(_eflutil.wl.shot.queue);
+        _eflutil.wl.shot.queue = NULL;
+     }
+}
+
+API efl_util_screenshot_h
+efl_util_screenshot_initialize(int width, int height)
+{
+   efl_util_screenshot_h screenshot = NULL;
+
+   if (width <= 0 || height <= 0)
+     {
+        set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
+        return NULL;
      }
 
+   _screenshot_mutex_lock();
+
    if (g_screenshot)
      {
         if (g_screenshot->width != width || g_screenshot->height != height)
@@ -2107,12 +2132,39 @@ efl_util_screenshot_initialize(int width, int height)
         return g_screenshot;
      }
 
+   screenshot = calloc(1, sizeof(struct _efl_util_screenshot_h));
+   if (screenshot == NULL)
+     {
+        set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
+
+        _screenshot_mutex_unlock();
+
+        return NULL;
+     }
+
+   if (!_eflutil.wl.shot.tz_screenshooter)
+     {
+        if (_efl_util_wl_screenshooter_init() == EINA_FALSE)
+          {
+             if (_eflutil.wl.shot.noti == 0)
+               set_last_result(EFL_UTIL_ERROR_PERMISSION_DENIED);
+             else
+               set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
+
+             free(screenshot);
+
+             _screenshot_mutex_unlock();
+
+             return NULL;
+          }
+     }
+
    screenshot->width = width;
    screenshot->height = height;
    screenshot->auto_rotation = EINA_TRUE;
 
    screenshot->bufmgr = wayland_tbm_client_get_bufmgr(_eflutil.wl.shot.tbm_client);
-   EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_init);
+   EINA_SAFETY_ON_NULL_GOTO(screenshot->bufmgr, fail_get_bufmgr);
 
    g_screenshot = screenshot;
    set_last_result(EFL_UTIL_ERROR_NONE);
@@ -2124,27 +2176,11 @@ efl_util_screenshot_initialize(int width, int height)
    return g_screenshot;
 
 /* LCOV_EXCL_START */
-fail_memory:
-   if (display_wrapper)
-     wl_proxy_wrapper_destroy(display_wrapper);
-   set_last_result(EFL_UTIL_ERROR_OUT_OF_MEMORY);
-   if (screenshot)
-     free(screenshot);
+fail_get_bufmgr:
+   set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
    _screenshot_mutex_unlock();
-   return NULL;
+   efl_util_screenshot_deinitialize(screenshot);
 
-fail_init:
-   if (reg)
-     wl_registry_destroy(reg);
-   if (screenshot)
-     {
-        _screenshot_mutex_unlock();
-        efl_util_screenshot_deinitialize(screenshot);
-     }
-   if (_eflutil.wl.shot.noti == 0)
-     set_last_result(EFL_UTIL_ERROR_PERMISSION_DENIED);
-   else
-     set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
    return NULL;
 /* LCOV_EXCL_STOP */
 }
@@ -2164,23 +2200,7 @@ efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
    free(screenshot);
    g_screenshot = NULL;
 
-   if (_eflutil.wl.shot.tbm_client)
-     {
-        wayland_tbm_client_deinit(_eflutil.wl.shot.tbm_client);
-        _eflutil.wl.shot.tbm_client = NULL;
-     }
-
-   if (_eflutil.wl.shot.tz_screenshooter)
-     {
-        tizen_screenshooter_destroy(_eflutil.wl.shot.tz_screenshooter);
-        _eflutil.wl.shot.tz_screenshooter = NULL;
-     }
-
-   if (_eflutil.wl.shot.queue)
-     {
-        wl_event_queue_destroy(_eflutil.wl.shot.queue);
-        _eflutil.wl.shot.queue = NULL;
-     }
+   _efl_util_wl_screenshooter_deinit();
 
    _screenshot_mutex_unlock();
    _screenshot_mutex_destory();