tbm_bufmgr_debug: change signature of tbm_bufmgr_debug_tbm_info_get
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_client.c
index c266e77..95814be 100644 (file)
@@ -31,6 +31,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 #define WL_HIDE_DEPRECATED
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -50,6 +52,7 @@ struct wayland_tbm_drm_auth_client {
        uint32_t capabilities;
 };
 
+/* LCOV_EXCL_START */
 static void
 handle_tbm_drm_authentication_info(void *data, struct wl_tbm_drm_auth *wl_tbm_drm_auth, const char *device_name, uint32_t capabilities, int32_t auth_fd)
 {
@@ -63,7 +66,7 @@ handle_tbm_drm_authentication_info(void *data, struct wl_tbm_drm_auth *wl_tbm_dr
 }
 
 static const struct wl_tbm_drm_auth_listener wl_tbm_drm_auth_client_listener = {
-    handle_tbm_drm_authentication_info
+       handle_tbm_drm_authentication_info
 };
 
 static void
@@ -80,8 +83,8 @@ _wayland_tbm_drm_auth_client_registry_handle_global(void *data, struct wl_regist
 }
 
 static const struct wl_registry_listener registry_listener = {
-    _wayland_tbm_drm_auth_client_registry_handle_global,
-    NULL
+       _wayland_tbm_drm_auth_client_registry_handle_global,
+       NULL
 };
 
 int
@@ -98,7 +101,7 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
 
        display = wl_display_connect("tbm-drm-auth");
        if (!display) {
-               TBM_LOG("Failed to connect display\n");
+               TBM_LOG_E("Failed to connect display\n");
                free(tbm_drm_client);
 
                return 0;
@@ -108,7 +111,7 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
 
        wl_registry = wl_display_get_registry(display);
        if (!wl_registry) {
-               TBM_LOG("Failed to get registry\n");
+               TBM_LOG_E("Failed to get registry\n");
                wl_display_disconnect(display);
                free(tbm_drm_client);
 
@@ -116,12 +119,16 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
        }
 
        wl_registry_add_listener(wl_registry, &registry_listener, tbm_drm_client);
-       wl_display_roundtrip(display); //For Gloabl registry
-        wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
-       wl_display_roundtrip(display);
+       if (wl_display_roundtrip(display) < 0) { //For Gloabl registry
+               TBM_LOG_E("Failed to wl_display_roundtrip for global registry\n");
+               wl_registry_destroy(wl_registry);
+               wl_display_disconnect(display);
+               free(tbm_drm_client);
+               return 0;
+       }
 
        if (!tbm_drm_client->wl_tbm_drm_auth) {
-               TBM_LOG("Failed to get wl_tbm_drm_auth interface\n");
+               TBM_LOG_E("Failed to get wl_tbm_drm_auth interface\n");
                wl_registry_destroy(wl_registry);
                wl_display_disconnect(display);
                free(tbm_drm_client);
@@ -129,8 +136,19 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
                return 0;
        }
 
+       wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
+       if (wl_display_roundtrip(display) < 0) {
+               TBM_LOG_E("Failed to wl_display_roundtrip get auth info\n");
+               wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
+               wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
+               wl_registry_destroy(wl_registry);
+               wl_display_disconnect(display);
+               free(tbm_drm_client);
+               return 0;
+       }
+
        if (tbm_drm_client->auth_fd < 0) {
-               TBM_LOG("Failed to get auth info\n");
+               TBM_LOG_E("Failed to get auth info\n");
                wl_tbm_drm_auth_set_user_data(tbm_drm_client->wl_tbm_drm_auth, NULL);
                wl_tbm_drm_auth_destroy(tbm_drm_client->wl_tbm_drm_auth);
                wl_registry_destroy(wl_registry);
@@ -143,7 +161,7 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
        if (auth_fd)
                *auth_fd = tbm_drm_client->auth_fd;
        else
-               close(*auth_fd);
+               close(tbm_drm_client->auth_fd);
 
        if (capabilities)
                *capabilities = tbm_drm_client->capabilities;
@@ -168,3 +186,98 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
 
        return 1;
 }
+
+
+void
+tbm_drm_helper_set_fd(int fd)
+{
+       char buf[32];
+       int ret;
+
+       snprintf(buf, sizeof(buf), "%d", fd);
+
+       ret = setenv("TBM_DRM_FD", (const char*)buf, 1);
+       if (ret) {
+               TBM_LOG_E("failed to set TBM_DRM_FD to %d\n", fd);
+               return;
+       }
+
+       TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
+}
+
+void
+tbm_drm_helper_unset_fd(void)
+{
+       int ret;
+
+       ret = unsetenv("TBM_DRM_FD");
+       if (ret) {
+               TBM_LOG_E("failed to unset TBM_DRM_FD\n");
+               return;
+       }
+}
+
+int
+tbm_drm_helper_get_fd(void)
+{
+       const char *value;
+       int flags, fd = -1;
+       int new_fd = -1;
+       char *end;
+       errno = 0;
+
+       value = (const char*)getenv("TBM_DRM_FD");
+       if (!value)
+               return -1;
+
+       const long int sl = strtol(value, &end, 10);
+       if (end == value) {
+               TBM_LOG_E("%s: not a decimal number\n", value);
+               return -1;
+       } else if (*end != '\0') {
+               TBM_LOG_E("%s: extra characters at end of input: %s\n", value, end);
+               return -1;
+       } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
+               TBM_LOG_E("%s out of range of type long\n", value);
+               return -1;
+       } else if (sl >= INT_MAX) {
+               TBM_LOG_E("%ld greater than INT_MAX\n", sl);
+               return -1;
+       } else if (sl <= INT_MIN) {
+               TBM_LOG_E("%ld less than INT_MIN\n", sl);
+               return -1;
+       } else {
+               int fd_max = tbm_bufmgr_get_fd_limit();
+               fd = (int)sl;
+               if (fd < 0 || fd > fd_max) {
+                       TBM_LOG_E("%d out of fd range\n", fd);
+                       return -1;
+               }
+       }
+
+       TBM_LOG_I("TBM_DRM_FD: %d\n", fd);
+
+       flags = fcntl(fd, F_GETFD);
+       if (flags == -1) {
+               TBM_LOG_E("fcntl failed: %m\n");
+               return -1;
+       }
+
+       new_fd = dup(fd);
+       if (new_fd < 0) {
+               TBM_LOG_E("dup failed: %m\n");
+               return -1;
+       }
+
+       if (fcntl(new_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
+               TBM_LOG_E("failed to set fd\n");
+               close(new_fd);
+               return -1;
+       }
+
+       TBM_LOG_I("Return TBM_FD: %d\n", new_fd);
+
+       return new_fd;
+}
+
+/* LCOV_EXCL_STOP */