Path: blob/21.2-virgl/src/vulkan/device-select-layer/device_select_wayland.c
7191 views
/*1* Copyright © 2019 Red Hat2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/22#include "macros.h"23#include <wayland-client.h>24#include "wayland-drm-client-protocol.h"25#include "device_select.h"26#include <string.h>27#include <stdio.h>28#include <unistd.h>29#include <fcntl.h>30#include <xf86drm.h>31struct device_select_wayland_info {32struct wl_drm *wl_drm;33drmDevicePtr dev_info;34bool info_is_set;35};3637static void38device_select_drm_handle_device(void *data, struct wl_drm *drm, const char *device)39{40struct device_select_wayland_info *info = data;4142int fd = open(device, O_RDWR | O_CLOEXEC);43if (fd == -1)44return;4546int ret = drmGetDevice2(fd, 0, &info->dev_info);47if (ret >= 0)48info->info_is_set = true;49close(fd);50return;51}5253static void54device_select_drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)55{5657}5859static void60device_select_drm_handle_authenticated(void *data, struct wl_drm *drm)61{6263}646566static void67device_select_drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)68{6970}717273static const struct wl_drm_listener ds_drm_listener = {74.device = device_select_drm_handle_device,75.format = device_select_drm_handle_format,76.authenticated = device_select_drm_handle_authenticated,77.capabilities = device_select_drm_handle_capabilities78};7980static void81device_select_registry_global(void *data, struct wl_registry *registry, uint32_t name,82const char *interface, uint32_t version)83{84struct device_select_wayland_info *info = data;85if (strcmp(interface, "wl_drm") == 0) {86info->wl_drm = wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));87wl_drm_add_listener(info->wl_drm, &ds_drm_listener, data);88}89}9091static void92device_select_registry_global_remove_cb(void *data, struct wl_registry *registry,93uint32_t name)94{9596}9798int device_select_find_wayland_pci_default(struct device_pci_info *devices, uint32_t device_count)99{100struct wl_display *display;101struct wl_registry *registry = NULL;102unsigned default_idx = -1;103struct device_select_wayland_info info = {};104105display = wl_display_connect(NULL);106if (!display)107goto out;108109registry = wl_display_get_registry(display);110if (!registry) {111wl_display_disconnect(display);112goto out;113}114115static const struct wl_registry_listener registry_listener =116{ device_select_registry_global, device_select_registry_global_remove_cb };117118wl_registry_add_listener(registry, ®istry_listener, &info);119wl_display_dispatch(display);120wl_display_roundtrip(display);121122123if (info.info_is_set) {124for (unsigned i = 0; i < device_count; i++) {125if (devices[i].has_bus_info) {126if (info.dev_info->businfo.pci->domain == devices[i].bus_info.domain &&127info.dev_info->businfo.pci->bus == devices[i].bus_info.bus &&128info.dev_info->businfo.pci->dev == devices[i].bus_info.dev &&129info.dev_info->businfo.pci->func == devices[i].bus_info.func)130default_idx = i;131} else {132if (info.dev_info->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id &&133info.dev_info->deviceinfo.pci->device_id == devices[i].dev_info.device_id)134default_idx = i;135}136if (default_idx != -1)137break;138}139}140141if (info.wl_drm)142wl_drm_destroy(info.wl_drm);143wl_registry_destroy(registry);144wl_display_disconnect(display);145out:146return default_idx;147}148149150