17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2019 Red Hat
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg#include "macros.h"
247ec681f3Smrg#include <wayland-client.h>
257ec681f3Smrg#include "wayland-drm-client-protocol.h"
267ec681f3Smrg#include "device_select.h"
277ec681f3Smrg#include <string.h>
287ec681f3Smrg#include <stdio.h>
297ec681f3Smrg#include <unistd.h>
307ec681f3Smrg#include <fcntl.h>
317ec681f3Smrg#include <xf86drm.h>
327ec681f3Smrgstruct device_select_wayland_info {
337ec681f3Smrg   struct wl_drm *wl_drm;
347ec681f3Smrg   drmDevicePtr dev_info;
357ec681f3Smrg   bool info_is_set;
367ec681f3Smrg};
377ec681f3Smrg
387ec681f3Smrgstatic void
397ec681f3Smrgdevice_select_drm_handle_device(void *data, struct wl_drm *drm, const char *device)
407ec681f3Smrg{
417ec681f3Smrg   struct device_select_wayland_info *info = data;
427ec681f3Smrg
437ec681f3Smrg   int fd = open(device, O_RDWR | O_CLOEXEC);
447ec681f3Smrg   if (fd == -1)
457ec681f3Smrg      return;
467ec681f3Smrg
477ec681f3Smrg   int ret = drmGetDevice2(fd, 0, &info->dev_info);
487ec681f3Smrg   if (ret >= 0)
497ec681f3Smrg      info->info_is_set = true;
507ec681f3Smrg   close(fd);
517ec681f3Smrg   return;
527ec681f3Smrg}
537ec681f3Smrg
547ec681f3Smrgstatic void
557ec681f3Smrgdevice_select_drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
567ec681f3Smrg{
577ec681f3Smrg
587ec681f3Smrg}
597ec681f3Smrg
607ec681f3Smrgstatic void
617ec681f3Smrgdevice_select_drm_handle_authenticated(void *data, struct wl_drm *drm)
627ec681f3Smrg{
637ec681f3Smrg
647ec681f3Smrg}
657ec681f3Smrg
667ec681f3Smrg
677ec681f3Smrgstatic void
687ec681f3Smrgdevice_select_drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
697ec681f3Smrg{
707ec681f3Smrg
717ec681f3Smrg}
727ec681f3Smrg
737ec681f3Smrg
747ec681f3Smrgstatic const struct wl_drm_listener ds_drm_listener = {
757ec681f3Smrg   .device = device_select_drm_handle_device,
767ec681f3Smrg   .format = device_select_drm_handle_format,
777ec681f3Smrg   .authenticated = device_select_drm_handle_authenticated,
787ec681f3Smrg   .capabilities = device_select_drm_handle_capabilities
797ec681f3Smrg};
807ec681f3Smrg
817ec681f3Smrgstatic void
827ec681f3Smrgdevice_select_registry_global(void *data, struct wl_registry *registry, uint32_t name,
837ec681f3Smrg			      const char *interface, uint32_t version)
847ec681f3Smrg{
857ec681f3Smrg   struct device_select_wayland_info *info = data;
867ec681f3Smrg   if (strcmp(interface, "wl_drm") == 0) {
877ec681f3Smrg      info->wl_drm = wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
887ec681f3Smrg      wl_drm_add_listener(info->wl_drm, &ds_drm_listener, data);
897ec681f3Smrg   }
907ec681f3Smrg}
917ec681f3Smrg
927ec681f3Smrgstatic void
937ec681f3Smrgdevice_select_registry_global_remove_cb(void *data, struct wl_registry *registry,
947ec681f3Smrg					uint32_t name)
957ec681f3Smrg{
967ec681f3Smrg
977ec681f3Smrg}
987ec681f3Smrg
997ec681f3Smrgint device_select_find_wayland_pci_default(struct device_pci_info *devices, uint32_t device_count)
1007ec681f3Smrg{
1017ec681f3Smrg   struct wl_display *display;
1027ec681f3Smrg   struct wl_registry *registry = NULL;
1037ec681f3Smrg   unsigned default_idx = -1;
1047ec681f3Smrg   struct device_select_wayland_info info = {};
1057ec681f3Smrg
1067ec681f3Smrg   display = wl_display_connect(NULL);
1077ec681f3Smrg   if (!display)
1087ec681f3Smrg      goto out;
1097ec681f3Smrg
1107ec681f3Smrg   registry = wl_display_get_registry(display);
1117ec681f3Smrg   if (!registry) {
1127ec681f3Smrg      wl_display_disconnect(display);
1137ec681f3Smrg      goto out;
1147ec681f3Smrg   }
1157ec681f3Smrg
1167ec681f3Smrg   static const struct wl_registry_listener registry_listener =
1177ec681f3Smrg      { device_select_registry_global, device_select_registry_global_remove_cb };
1187ec681f3Smrg
1197ec681f3Smrg   wl_registry_add_listener(registry, &registry_listener, &info);
1207ec681f3Smrg   wl_display_dispatch(display);
1217ec681f3Smrg   wl_display_roundtrip(display);
1227ec681f3Smrg
1237ec681f3Smrg
1247ec681f3Smrg   if (info.info_is_set) {
1257ec681f3Smrg      for (unsigned i = 0; i < device_count; i++) {
1267ec681f3Smrg	 if (devices[i].has_bus_info) {
1277ec681f3Smrg	    if (info.dev_info->businfo.pci->domain == devices[i].bus_info.domain &&
1287ec681f3Smrg		info.dev_info->businfo.pci->bus == devices[i].bus_info.bus &&
1297ec681f3Smrg		info.dev_info->businfo.pci->dev == devices[i].bus_info.dev &&
1307ec681f3Smrg		info.dev_info->businfo.pci->func == devices[i].bus_info.func)
1317ec681f3Smrg	       default_idx = i;
1327ec681f3Smrg	 } else {
1337ec681f3Smrg	    if (info.dev_info->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id &&
1347ec681f3Smrg		info.dev_info->deviceinfo.pci->device_id == devices[i].dev_info.device_id)
1357ec681f3Smrg	       default_idx = i;
1367ec681f3Smrg	 }
1377ec681f3Smrg	 if (default_idx != -1)
1387ec681f3Smrg	    break;
1397ec681f3Smrg      }
1407ec681f3Smrg   }
1417ec681f3Smrg
1427ec681f3Smrg   if (info.wl_drm)
1437ec681f3Smrg      wl_drm_destroy(info.wl_drm);
1447ec681f3Smrg   wl_registry_destroy(registry);
1457ec681f3Smrg   wl_display_disconnect(display);
1467ec681f3Smrg out:
1477ec681f3Smrg   return default_idx;
1487ec681f3Smrg}
149