1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Red Hat
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include "macros.h"
24bf215546Sopenharmony_ci#include <wayland-client.h>
25bf215546Sopenharmony_ci#include "wayland-drm-client-protocol.h"
26bf215546Sopenharmony_ci#include "device_select.h"
27bf215546Sopenharmony_ci#include <string.h>
28bf215546Sopenharmony_ci#include <stdio.h>
29bf215546Sopenharmony_ci#include <unistd.h>
30bf215546Sopenharmony_ci#include <fcntl.h>
31bf215546Sopenharmony_ci#include <xf86drm.h>
32bf215546Sopenharmony_cistruct device_select_wayland_info {
33bf215546Sopenharmony_ci   struct wl_drm *wl_drm;
34bf215546Sopenharmony_ci   drmDevicePtr dev_info;
35bf215546Sopenharmony_ci   bool info_is_set;
36bf215546Sopenharmony_ci};
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic void
39bf215546Sopenharmony_cidevice_select_drm_handle_device(void *data, struct wl_drm *drm, const char *device)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   struct device_select_wayland_info *info = data;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   int fd = open(device, O_RDWR | O_CLOEXEC);
44bf215546Sopenharmony_ci   if (fd == -1)
45bf215546Sopenharmony_ci      return;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   int ret = drmGetDevice2(fd, 0, &info->dev_info);
48bf215546Sopenharmony_ci   if (ret >= 0)
49bf215546Sopenharmony_ci      info->info_is_set = true;
50bf215546Sopenharmony_ci   close(fd);
51bf215546Sopenharmony_ci   return;
52bf215546Sopenharmony_ci}
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistatic void
55bf215546Sopenharmony_cidevice_select_drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci}
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistatic void
61bf215546Sopenharmony_cidevice_select_drm_handle_authenticated(void *data, struct wl_drm *drm)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci}
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic void
68bf215546Sopenharmony_cidevice_select_drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci}
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_cistatic const struct wl_drm_listener ds_drm_listener = {
75bf215546Sopenharmony_ci   .device = device_select_drm_handle_device,
76bf215546Sopenharmony_ci   .format = device_select_drm_handle_format,
77bf215546Sopenharmony_ci   .authenticated = device_select_drm_handle_authenticated,
78bf215546Sopenharmony_ci   .capabilities = device_select_drm_handle_capabilities
79bf215546Sopenharmony_ci};
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_cistatic void
82bf215546Sopenharmony_cidevice_select_registry_global(void *data, struct wl_registry *registry, uint32_t name,
83bf215546Sopenharmony_ci			      const char *interface, uint32_t version)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci   struct device_select_wayland_info *info = data;
86bf215546Sopenharmony_ci   if (strcmp(interface, "wl_drm") == 0) {
87bf215546Sopenharmony_ci      info->wl_drm = wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
88bf215546Sopenharmony_ci      wl_drm_add_listener(info->wl_drm, &ds_drm_listener, data);
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistatic void
93bf215546Sopenharmony_cidevice_select_registry_global_remove_cb(void *data, struct wl_registry *registry,
94bf215546Sopenharmony_ci					uint32_t name)
95bf215546Sopenharmony_ci{
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ciint device_select_find_wayland_pci_default(struct device_pci_info *devices, uint32_t device_count)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   struct wl_display *display;
102bf215546Sopenharmony_ci   struct wl_registry *registry = NULL;
103bf215546Sopenharmony_ci   unsigned default_idx = -1;
104bf215546Sopenharmony_ci   struct device_select_wayland_info info = {};
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   display = wl_display_connect(NULL);
107bf215546Sopenharmony_ci   if (!display)
108bf215546Sopenharmony_ci      goto out;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   registry = wl_display_get_registry(display);
111bf215546Sopenharmony_ci   if (!registry) {
112bf215546Sopenharmony_ci      wl_display_disconnect(display);
113bf215546Sopenharmony_ci      goto out;
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   static const struct wl_registry_listener registry_listener =
117bf215546Sopenharmony_ci      { device_select_registry_global, device_select_registry_global_remove_cb };
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   wl_registry_add_listener(registry, &registry_listener, &info);
120bf215546Sopenharmony_ci   wl_display_dispatch(display);
121bf215546Sopenharmony_ci   wl_display_roundtrip(display);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   if (info.info_is_set) {
125bf215546Sopenharmony_ci      for (unsigned i = 0; i < device_count; i++) {
126bf215546Sopenharmony_ci	 if (devices[i].has_bus_info) {
127bf215546Sopenharmony_ci	    if (info.dev_info->businfo.pci->domain == devices[i].bus_info.domain &&
128bf215546Sopenharmony_ci		info.dev_info->businfo.pci->bus == devices[i].bus_info.bus &&
129bf215546Sopenharmony_ci		info.dev_info->businfo.pci->dev == devices[i].bus_info.dev &&
130bf215546Sopenharmony_ci		info.dev_info->businfo.pci->func == devices[i].bus_info.func)
131bf215546Sopenharmony_ci	       default_idx = i;
132bf215546Sopenharmony_ci	 } else {
133bf215546Sopenharmony_ci	    if (info.dev_info->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id &&
134bf215546Sopenharmony_ci		info.dev_info->deviceinfo.pci->device_id == devices[i].dev_info.device_id)
135bf215546Sopenharmony_ci	       default_idx = i;
136bf215546Sopenharmony_ci	 }
137bf215546Sopenharmony_ci	 if (default_idx != -1)
138bf215546Sopenharmony_ci	    break;
139bf215546Sopenharmony_ci      }
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci      drmFreeDevice(&info.dev_info);
142bf215546Sopenharmony_ci   }
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   if (info.wl_drm)
145bf215546Sopenharmony_ci      wl_drm_destroy(info.wl_drm);
146bf215546Sopenharmony_ci   wl_registry_destroy(registry);
147bf215546Sopenharmony_ci   wl_display_disconnect(display);
148bf215546Sopenharmony_ci out:
149bf215546Sopenharmony_ci   return default_idx;
150bf215546Sopenharmony_ci}
151