2 * Copyright © 2007 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
33 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
36 static int is_master(int fd
)
41 /* Check that we're the only opener and authed. */
43 ret
= ioctl(fd
, DRM_IOCTL_GET_CLIENT
, &client
);
48 ret
= ioctl(fd
, DRM_IOCTL_GET_CLIENT
, &client
);
49 if (ret
!= -1 || errno
!= EINVAL
)
55 /** Open the first DRM device matching the criteria */
56 int drm_open_matching(const char *pci_glob
, int flags
)
59 struct udev_enumerate
*e
;
60 struct udev_device
*device
, *parent
;
61 struct udev_list_entry
*entry
;
62 const char *pci_id
, *path
;
67 fprintf(stderr
, "failed to initialize udev context\n");
72 e
= udev_enumerate_new(udev
);
73 udev_enumerate_add_match_subsystem(e
, "drm");
74 udev_enumerate_scan_devices(e
);
75 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
76 path
= udev_list_entry_get_name(entry
);
77 device
= udev_device_new_from_syspath(udev
, path
);
78 parent
= udev_device_get_parent(device
);
79 /* Filter out KMS output devices. */
80 if (strcmp(udev_device_get_subsystem(parent
), "pci") != 0)
82 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
83 if (fnmatch(pci_glob
, pci_id
, 0) != 0)
85 fd
= open(udev_device_get_devnode(device
), O_RDWR
);
88 if ((flags
& DRM_TEST_MASTER
) && !is_master(fd
)) {
96 udev_enumerate_unref(e
);
102 int drm_open_any(void)
104 int fd
= drm_open_matching("*:*", 0);
107 fprintf(stderr
, "failed to open any drm device\n");
115 * Open the first DRM device we can find where we end up being the master.
117 int drm_open_any_master(void)
119 int fd
= drm_open_matching("*:*", DRM_TEST_MASTER
);
122 fprintf(stderr
, "failed to open any drm device\n");