2 * Copyright © 2019 Red Hat
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 /* connect to an X server and work out the default device. */
33 #include "device_select.h"
35 ds_dri3_open(xcb_connection_t
*conn
,
39 xcb_dri3_open_cookie_t cookie
;
40 xcb_dri3_open_reply_t
*reply
;
43 cookie
= xcb_dri3_open(conn
,
47 reply
= xcb_dri3_open_reply(conn
, cookie
, NULL
);
51 if (reply
->nfd
!= 1) {
56 fd
= xcb_dri3_open_reply_fds(conn
, reply
)[0];
58 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
63 int device_select_find_xcb_pci_default(struct device_pci_info
*devices
, uint32_t device_count
)
65 const xcb_setup_t
*setup
;
66 xcb_screen_iterator_t iter
;
68 xcb_connection_t
*conn
;
70 conn
= xcb_connect(NULL
, &scrn
);
74 xcb_query_extension_cookie_t dri3_cookie
;
75 xcb_query_extension_reply_t
*dri3_reply
;
77 dri3_cookie
= xcb_query_extension(conn
, 4, "DRI3");
78 dri3_reply
= xcb_query_extension_reply(conn
, dri3_cookie
, NULL
);
83 if (dri3_reply
->present
== 0)
85 setup
= xcb_get_setup(conn
);
86 iter
= xcb_setup_roots_iterator(setup
);
88 xcb_screen_t
*screen
= iter
.data
;
90 int dri3_fd
= ds_dri3_open(conn
, screen
->root
, 0);
95 int ret
= drmGetDevice2(dri3_fd
, 0, &xdev
);
99 for (unsigned i
= 0; i
< device_count
; i
++) {
100 if (devices
[i
].has_bus_info
) {
101 if (xdev
->businfo
.pci
->domain
== devices
[i
].bus_info
.domain
&&
102 xdev
->businfo
.pci
->bus
== devices
[i
].bus_info
.bus
&&
103 xdev
->businfo
.pci
->dev
== devices
[i
].bus_info
.dev
&&
104 xdev
->businfo
.pci
->func
== devices
[i
].bus_info
.func
) {
108 if (xdev
->deviceinfo
.pci
->vendor_id
== devices
[i
].dev_info
.vendor_id
&&
109 xdev
->deviceinfo
.pci
->device_id
== devices
[i
].dev_info
.device_id
)
112 if (default_idx
!= -1)
116 xcb_disconnect(conn
);