diff mbox

[2/5] drm: generate the path of a different device type for the same drm device

Message ID 1355709775-15674-2-git-send-email-martin.peres@free.fr (mailing list archive)
State New, archived
Headers show

Commit Message

Martin Peres Dec. 17, 2012, 2:02 a.m. UTC
From: Martin Peres <martin.peres@labri.fr>

Signed-off-by: Martin Peres <martin.peres@labri.fr>
---
 .gitignore                   |  1 +
 tests/Makefile.am            |  1 +
 tests/same_device_but_type.c | 52 ++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.c                    | 44 +++++++++++++++++++++++++++++++++++++
 xf86drm.h                    |  1 +
 5 files changed, 99 insertions(+)
 create mode 100644 tests/same_device_but_type.c
diff mbox

Patch

diff --git a/.gitignore b/.gitignore
index 28c77c5..318e129 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@  tests/gem_readwrite
 tests/openclose
 tests/setversion
 tests/updatedraw
+tests/same_device_but_type
 tests/modeprint/modeprint
 tests/modetest/modetest
 tests/kmstest/kmstest
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a3a59bd..ddc73ca 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -46,6 +46,7 @@  TESTS =						\
 	setversion				\
 	updatedraw				\
 	name_from_fd				\
+	same_device_but_type			\
 	$(NULL)
 
 SUBDIRS += vbltest $(NULL)
diff --git a/tests/same_device_but_type.c b/tests/same_device_but_type.c
new file mode 100644
index 0000000..f35951f
--- /dev/null
+++ b/tests/same_device_but_type.c
@@ -0,0 +1,52 @@ 
+/*
+ * Copyright © 2012
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Martin Peres <martin.peres@labri.fr>
+ *
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks drmGetSameDeviceButType
+ */
+int main(int argc, char **argv)
+{
+	int fd, ret;
+	drm_set_version_t sv, version;
+	const char *name = "/dev/dri/card0";
+	char *render;
+	int i;
+
+	render = drmGetSameDeviceButType(name, DRM_NODE_RENDER);
+	assert(strcmp(render, name) == 0);
+	drmFree(render);
+	
+	render = drmGetSameDeviceButType("/no_device", DRM_NODE_RENDER);
+	assert(render == NULL);
+
+	return 0;
+}
diff --git a/xf86drm.c b/xf86drm.c
index eb0549d..cca8d8a 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2602,6 +2602,50 @@  char *drmGetDeviceNameFromFd(int fd)
 	return NULL;
 }
 
+/**
+ * Generate the path to a different type (render node, control node, etc...)
+ * but for the same actual drm device.
+ *
+ * \param device a path to an existing drm device type.
+ * \param type the new type that should be returned.
+ * 
+ * \return the path to the same drm device but to a different type if success.
+ * NULL otherwise.
+ */
+char *drmGetSameDeviceButType(const char *device, int type)
+{
+	char base_path[64], name[64], *path, *device_name;
+	struct stat sbuf;
+	int i;
+	
+	/* get the device name (ie /card0) */
+	device_name=strrchr(device, '/');
+	if (!device_name)
+		return NULL;
+	
+	/* get the device's drm folder (ie /sys/class/drm/card0/device/drm */
+	snprintf(base_path, sizeof(base_path), "/sys/class/drm/%s/device/drm",
+		 device_name + 1);
+	
+	/* search for the type in the base path */
+	device_name = NULL;
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		drmDevicePath(name, sizeof(name), base_path, type, i);
+		if (stat(name, &sbuf) == 0) {
+			device_name = strrchr(name, '/');
+			break;
+		}
+	}
+	if (!device_name)
+		return NULL;
+	
+	/* create a more appropriate path for the device (/dev/dri/card0) */
+	path = malloc(64);
+	snprintf(path, 64, "%s/%s", DRM_DIR_NAME, device_name + 1);
+
+	return path;
+}
+
 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
 {
 	struct drm_prime_handle args;
diff --git a/xf86drm.h b/xf86drm.h
index d727ce1..658dfd7 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -733,6 +733,7 @@  typedef struct _drmEventContext {
 extern int drmHandleEvent(int fd, drmEventContextPtr evctx);
 
 extern char *drmGetDeviceNameFromFd(int fd);
+extern char *drmGetSameDeviceButType(const char *device, int type);
 
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);