gbm-sdl2-example

view src/drmtest.cc @ 0:665119a0580e

example program that uses gbm buffers
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Wed, 04 Jul 2018 22:16:23 +0300
parents
children
line source
1 /*
2 * Copyright © 2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
28 #include <string.h>
30 #include <fcntl.h>
31 #include <fnmatch.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34 #include "drmtest.h"
36 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
37 #include <libudev.h>
39 static int is_master(int fd)
40 {
41 drm_client_t client;
42 int ret;
44 /* Check that we're the only opener and authed. */
45 client.idx = 0;
46 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
47 assert (ret == 0);
48 if (!client.auth)
49 return 0;
50 client.idx = 1;
51 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
52 if (ret != -1 || errno != EINVAL)
53 return 0;
55 return 1;
56 }
58 /** Open the first DRM device matching the criteria */
59 int drm_open_matching(const char *pci_glob, int flags)
60 {
61 struct udev *udev;
62 struct udev_enumerate *e;
63 struct udev_device *device, *parent;
64 struct udev_list_entry *entry;
65 const char *pci_id, *path;
66 int fd;
68 udev = udev_new();
69 if (udev == NULL) {
70 fprintf(stderr, "failed to initialize udev context\n");
71 abort();
72 }
74 fd = -1;
75 e = udev_enumerate_new(udev);
76 udev_enumerate_add_match_subsystem(e, "drm");
77 udev_enumerate_scan_devices(e);
78 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
79 path = udev_list_entry_get_name(entry);
80 device = udev_device_new_from_syspath(udev, path);
81 parent = udev_device_get_parent(device);
82 /* Filter out KMS output devices. */
83 if (strcmp(udev_device_get_subsystem(parent), "pci") != 0)
84 continue;
85 pci_id = udev_device_get_property_value(parent, "PCI_ID");
86 if (fnmatch(pci_glob, pci_id, 0) != 0)
87 continue;
88 fd = open(udev_device_get_devnode(device), O_RDWR);
89 if (fd < 0)
90 continue;
91 if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
92 close(fd);
93 fd = -1;
94 continue;
95 }
97 break;
98 }
99 udev_enumerate_unref(e);
100 udev_unref(udev);
102 return fd;
103 }
105 int drm_open_any(void)
106 {
107 int fd = drm_open_matching("*:*", 0);
109 if (fd < 0) {
110 fprintf(stderr, "failed to open any drm device\n");
111 abort();
112 }
114 return fd;
115 }
117 /**
118 * Open the first DRM device we can find where we end up being the master.
119 */
120 int drm_open_any_master(void)
121 {
122 int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
124 if (fd < 0) {
125 fprintf(stderr, "failed to open any drm device\n");
126 abort();
127 }
129 return fd;
131 }