OSDN Git Service

Tidy up compile warnings by cleaning up types.
[android-x86/external-libdrm.git] / tests / drmtest.c
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  */
27
28 #include <string.h>
29 #include <fcntl.h>
30 #include <fnmatch.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include "drmtest.h"
34
35 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
36 #include <libudev.h>
37
38 static int is_master(int fd)
39 {
40         drm_client_t client;
41         int ret;
42
43         /* Check that we're the only opener and authed. */
44         client.idx = 0;
45         ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
46         assert (ret == 0);
47         if (!client.auth)
48                 return 0;
49         client.idx = 1;
50         ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
51         if (ret != -1 || errno != EINVAL)
52                 return 0;
53
54         return 1;
55 }
56
57 /** Open the first DRM device matching the criteria */
58 int drm_open_matching(const char *pci_glob, int flags)
59 {
60         struct udev *udev;
61         struct udev_enumerate *e;
62         struct udev_device *device, *parent;
63         struct udev_list_entry *entry;
64         const char *pci_id, *path;
65         int fd;
66
67         udev = udev_new();
68         if (udev == NULL) {
69                 fprintf(stderr, "failed to initialize udev context\n");
70                 abort();
71         }
72
73         fd = -1;
74         e = udev_enumerate_new(udev);
75         udev_enumerate_add_match_subsystem(e, "drm");
76         udev_enumerate_scan_devices(e);
77         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
78                 path = udev_list_entry_get_name(entry);
79                 device = udev_device_new_from_syspath(udev, path);
80                 parent = udev_device_get_parent(device);
81                 /* Filter out KMS output devices. */
82                 if (strcmp(udev_device_get_subsystem(parent), "pci") != 0)
83                         continue;
84                 pci_id = udev_device_get_property_value(parent, "PCI_ID");
85                 if (fnmatch(pci_glob, pci_id, 0) != 0)
86                         continue;
87                 fd = open(udev_device_get_devnode(device), O_RDWR);
88                 if (fd < 0)
89                         continue;
90                 if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
91                         close(fd);
92                         fd = -1;
93                         continue;
94                 }
95
96                 break;
97         }
98         udev_enumerate_unref(e);
99         udev_unref(udev);
100
101         return fd;
102 }
103
104 int drm_open_any(void)
105 {
106         int fd = drm_open_matching("*:*", 0);
107
108         if (fd < 0) {
109                 fprintf(stderr, "failed to open any drm device\n");
110                 abort();
111         }
112
113         return fd;
114 }
115
116 /**
117  * Open the first DRM device we can find where we end up being the master.
118  */
119 int drm_open_any_master(void)
120 {
121         int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
122
123         if (fd < 0) {
124                 fprintf(stderr, "failed to open any drm device\n");
125                 abort();
126         }
127
128         return fd;
129
130 }