From ff74e83997de9c11096c6c54074f0e64bfaede0c Mon Sep 17 00:00:00 2001 From: Boram Park Date: Mon, 21 Mar 2016 09:28:20 +0900 Subject: [PATCH] add tdm_helper_get_fd & tdm_helper_set_fd Change-Id: I07027116cf37fd312eda56c49b271b2f36575c8f --- include/tdm_helper.h | 27 ++++++++++++++++++++++++++ src/tdm.c | 3 +++ src/tdm_helper.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/tdm_helper.h b/include/tdm_helper.h index 86b6620..c329669 100644 --- a/include/tdm_helper.h +++ b/include/tdm_helper.h @@ -68,6 +68,33 @@ extern "C" { void tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file); +/** + * @brief Get a fd from the given enviroment variable. + * @details + * This function will dup the fd of the given enviroment variable. The Caller + * @b SHOULD close the fd. + * \n + * In DRM system, a drm-master-fd @b SHOULD be shared between TDM backend and + * TBM backend in display server side by using "TDM_DRM_MASTER_FD" + * and "TBM_DRM_MASTER_FD". + * @param[in] env The given enviroment variable + * @return fd if success. Otherwise, -1. + * @see #tdm_helper_set_fd() + */ +int tdm_helper_get_fd(const char *env); + +/** + * @brief Set the given fd to the give enviroment variable. + * @details + * In DRM system, a drm-master-fd @b SHOULD be shared between TDM backend and + * TBM backend in display server side by using "TDM_DRM_MASTER_FD" + * and "TBM_DRM_MASTER_FD". + * @param[in] env The given enviroment variable + * @param[in] fd The given fd + * @see #tdm_helper_get_fd() + */ +void tdm_helper_set_fd(const char *env, int fd); + #ifdef __cplusplus } #endif diff --git a/src/tdm.c b/src/tdm.c index a7451f1..1a02ef1 100644 --- a/src/tdm.c +++ b/src/tdm.c @@ -40,6 +40,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "tdm.h" #include "tdm_backend.h" #include "tdm_private.h" +#include "tdm_helper.h" static tdm_private_layer * _tdm_display_find_private_layer(tdm_private_output *private_output, @@ -784,6 +785,8 @@ tdm_display_deinit(tdm_display *dpy) _tdm_display_destroy_private_display(private_display); _tdm_display_unload_module(private_display); + tdm_helper_set_fd("TDM_DRM_MASTER_FD", -1); + pthread_mutex_unlock(&private_display->lock); pthread_mutex_destroy(&private_display->lock); diff --git a/src/tdm_helper.c b/src/tdm_helper.c index 7c47b6e..311ea6e 100644 --- a/src/tdm_helper.c +++ b/src/tdm_helper.c @@ -173,3 +173,56 @@ tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file) TDM_INFO("dump %s", file); } + +EXTERN int +tdm_helper_get_fd(const char *env) +{ + const char *value; + int fd, newfd, flags, ret; + + value = (const char*)getenv(env); + if (!value) + return -1; + + ret = sscanf(value, "%d", &fd); + if (ret < 0) { + TDM_ERR("sscanf failed: %m"); + return -1; + } + + flags = fcntl(fd, F_GETFD); + if (flags == -1) { + TDM_ERR("fcntl failed: %m"); + return -1; + } + + newfd = dup(fd); + if (newfd < 0) { + TDM_ERR("dup failed: %m"); + return -1; + } + + TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd); + + fcntl(newfd, F_SETFD, flags | FD_CLOEXEC); + + return newfd; +} + +EXTERN void +tdm_helper_set_fd(const char *env, int fd) +{ + char buf[32]; + int ret; + + snprintf(buf, sizeof(buf), "%d", fd); + + ret = setenv(env, (const char*)buf, 1); + if (ret) { + TDM_ERR("setenv failed: %m"); + return; + } + + if (fd >= 0) + TDM_INFO("%s: fd(%d)", env, fd); +} -- 2.7.4