OSDN Git Service

xf86drm: add drmOpenByFB
authorChih-Wei Huang <cwhuang@linux.org.tw>
Sun, 13 Jan 2019 16:06:59 +0000 (00:06 +0800)
committerMauro Rossi <issor.oruam@gmail.com>
Thu, 17 Oct 2019 18:11:23 +0000 (20:11 +0200)
The new function looks up the DRM device with specified type
associated with the specified framebuffer and opens it.

Signed-off-by: Chih-Wei Huang <cwhuang@linux.org.tw>
xf86drm.c
xf86drm.h

index 693ff4d..c6187d3 100644 (file)
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -773,6 +773,48 @@ drm_public int drmOpenRender(int minor)
 }
 
 /**
+ * Open the DRM device with specified type of specified framebuffer.
+ *
+ * Looks up the associated DRM device with specified type of the
+ * specified framebuffer and opens it.
+ *
+ * \param fb the index of framebuffer.
+ * \param type the device node type to open, PRIMARY, CONTROL or RENDER
+ *
+ * \return a file descriptor on success, or a negative value on error.
+ *
+ */
+drm_public int drmOpenByFB(int fb, int type)
+{
+#ifdef __linux__
+    DIR *sysdir;
+    struct dirent *ent;
+    char buf[64];
+    const char *name = drmGetMinorName(type);
+    int fd = -1, len = strlen(name);
+
+    snprintf(buf, sizeof(buf), "/sys/class/graphics/fb%d/device/drm", fb);
+    sysdir = opendir(buf);
+    if (!sysdir)
+        return -errno;
+
+    while ((ent = readdir(sysdir))) {
+        if (!strncmp(ent->d_name, name, len)) {
+            snprintf(buf, sizeof(buf), "%s/%s", DRM_DIR_NAME, ent->d_name);
+            fd = open(buf, O_RDWR | O_CLOEXEC, 0);
+            break;
+        }
+    }
+
+    closedir(sysdir);
+    return fd;
+#else
+#warning "Missing implementation of drmOpenByFB"
+    return -EINVAL;
+#endif
+}
+
+/**
  * Free the version information returned by drmGetVersion().
  *
  * \param v pointer to the version information.
index 3f52cd8..e94960f 100644 (file)
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -582,6 +582,7 @@ extern int           drmOpenWithType(const char *name, const char *busid,
 
 extern int           drmOpenControl(int minor);
 extern int           drmOpenRender(int minor);
+extern int           drmOpenByFB(int fb, int type);
 extern int           drmClose(int fd);
 extern drmVersionPtr drmGetVersion(int fd);
 extern drmVersionPtr drmGetLibVersion(int fd);