tbm_bufmgr: add checking of a map_count in the unmap function
[platform/core/uifw/libtbm.git] / src / tbm_drm_helper_client.c
index ac86115..ecd5653 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,8 @@ struct wayland_tbm_drm_auth_client {
        uint32_t capabilities;
 };
 
+static int tbm_drm_fd = -1;
+
 /* 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)
@@ -117,7 +121,13 @@ 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
+       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_E("Failed to get wl_tbm_drm_auth interface\n");
@@ -129,7 +139,15 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
        }
 
        wl_tbm_drm_auth_get_authentication_info(tbm_drm_client->wl_tbm_drm_auth);
-       wl_display_roundtrip(display);
+       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_E("Failed to get auth info\n");
@@ -170,5 +188,69 @@ tbm_drm_helper_get_auth_info(int *auth_fd, char **device, uint32_t *capabilities
 
        return 1;
 }
-/* LCOV_EXCL_STOP */
 
+
+void
+tbm_drm_helper_set_fd(int fd)
+{
+       int fd_max = tbm_bufmgr_get_fd_limit();
+
+       if (tbm_drm_fd == fd)
+               return;
+
+       if (fd < 0 || fd > fd_max) {
+               TBM_LOG_E("%d out of fd range\n", fd);
+               return;
+       }
+
+       if (tbm_drm_fd != -1)
+               TBM_LOG_W("already has TBM_DRM_FD: %d\n", tbm_drm_fd);
+
+       tbm_drm_fd = fd;
+
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
+}
+
+void
+tbm_drm_helper_unset_fd(void)
+{
+       tbm_drm_fd = -1;
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
+}
+
+int
+tbm_drm_helper_get_fd(void)
+{
+       int new_fd, flags;
+
+       if (tbm_drm_fd == -1) {
+               TBM_LOG_E("no drm fd");
+               return -1;
+       }
+
+       TBM_LOG_I("TBM_DRM_FD: %d\n", tbm_drm_fd);
+
+       flags = fcntl(tbm_drm_fd, F_GETFD);
+       if (flags == -1) {
+               TBM_LOG_E("fcntl failed: %m");
+               return -1;
+       }
+
+       new_fd = dup(tbm_drm_fd);
+       if (new_fd < 0) {
+               TBM_LOG_E("dup failed: %m");
+               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 */