summaryrefslogtreecommitdiff
path: root/tools/dri3info.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/dri3info.c')
-rw-r--r--tools/dri3info.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/tools/dri3info.c b/tools/dri3info.c
new file mode 100644
index 00000000..0dcb495f
--- /dev/null
+++ b/tools/dri3info.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2015 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#include <X11/Xlib.h>
+#include <X11/Xlib-xcb.h>
+#include <xcb/xcb.h>
+#include <xcb/dri3.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+static int dri3_open(Display *dpy)
+{
+ xcb_connection_t *c = XGetXCBConnection(dpy);
+ xcb_dri3_open_cookie_t cookie;
+ xcb_dri3_open_reply_t *reply;
+
+ cookie = xcb_dri3_open(c, RootWindow(dpy, DefaultScreen(dpy)), None);
+ reply = xcb_dri3_open_reply(c, cookie, NULL);
+
+ if (!reply)
+ return -1;
+
+ if (reply->nfd != 1)
+ return -1;
+
+ return xcb_dri3_open_reply_fds(c, reply)[0];
+}
+
+static void match_device(int fd, char *buf, int len)
+{
+ struct stat remote, local;
+ int i;
+
+ if (fstat(fd, &remote))
+ goto out;
+
+ for (i = 0; i < 64; i++) {
+ snprintf(buf, len, "/dev/dri/card%d", i);
+ if (stat(buf, &local))
+ continue;
+
+ if (local.st_mode == remote.st_mode &&
+ local.st_rdev == remote.st_rdev)
+ return;
+ }
+
+out:
+ strncpy(buf, "unknown card", len);
+}
+
+static void info(const char *dpyname)
+{
+ Display *dpy;
+ int device;
+ char device_name[1024];
+
+ dpy = XOpenDisplay(dpyname);
+ if (dpy == NULL) {
+ printf("Unable to connect to display '%s'\n",
+ dpyname ?: getenv("DISPLAY") ?: "unset");
+ return;
+ }
+
+ device = dri3_open(dpy);
+ if (device < 0) {
+ printf("Unable to connect to DRI3 on display '%s'\n",
+ DisplayString(dpy));
+ return;
+ }
+
+ match_device(device, device_name, sizeof(device_name));
+
+ printf("Connected to DRI3 on display '%s', using fd %d: matches %s\n",
+ DisplayString(dpy), device, device_name);
+
+ XCloseDisplay(dpy);
+ close(device);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+
+ if (argc > 1) {
+ for (i = 1; i < argc; i++)
+ info(argv[i]);
+ } else
+ info(NULL);
+
+ return 0;
+}