wsi: Move common objects to the wayland wsi::surface
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wayland / wl_helpers.cpp
1 /*
2  * Copyright (c) 2017-2019, 2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24
25 #include "wl_helpers.hpp"
26
27 #include <cstring>
28 #include <memory>
29 #include <poll.h>
30 #include <errno.h>
31 #include <cassert>
32
33 #include "wl_object_owner.hpp"
34
35 #include "util/log.hpp"
36
37 extern "C" {
38
39    void registry_handler(void *data, struct wl_registry *wl_registry, uint32_t name, const char *interface,
40                          uint32_t version)
41    {
42       auto dmabuf_interface = reinterpret_cast<wsi::wayland::zwp_linux_dmabuf_v1_owner* >(data);
43
44       if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
45       {
46          version = ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION;
47          zwp_linux_dmabuf_v1 *dmabuf_interface_obj =
48             reinterpret_cast<zwp_linux_dmabuf_v1 *>(wl_registry_bind(
49                wl_registry, name, &zwp_linux_dmabuf_v1_interface, version));
50
51          if (dmabuf_interface_obj == nullptr)
52          {
53             WSI_LOG_ERROR("Failed to get zwp_linux_dmabuf_v1 interface.");
54             return;
55          }
56
57          dmabuf_interface->reset(dmabuf_interface_obj);
58       }
59    }
60
61    int dispatch_queue(struct wl_display *display, struct wl_event_queue *queue, int timeout)
62    {
63       int err;
64       struct pollfd pfd = {};
65       int retval;
66
67       /* Before we sleep, dispatch any pending events. prepare_read_queue will return 0 whilst there are pending
68        * events to dispatch on the queue. */
69       while (0 != wl_display_prepare_read_queue(display, queue))
70       {
71          /* dispatch_queue_pending returns -1 on error, or the number of events dispatched otherwise. If we
72           * already dispatched some events, then we might not need to sleep, as we might have just dispatched
73           * the event we want, so return immediately. */
74          err = wl_display_dispatch_queue_pending(display, queue);
75          if (err)
76          {
77             return (0 > err) ? -1 : 1;
78          }
79       }
80
81       /* wl_display_read_events performs a non-blocking read. */
82       pfd.fd = wl_display_get_fd(display);
83       pfd.events = POLLIN;
84       while (true)
85       {
86          /* Timeout is given in milliseconds. A return value of 0, or -1 with errno set to EINTR means that we
87           * should retry as the timeout was exceeded or we were interrupted by a signal, respectively. A
88           * return value of 1 means that something happened, and we should inspect the pollfd structure to see
89           * just what that was.
90           */
91          err = poll(&pfd, 1, timeout);
92          if (0 == err)
93          {
94             /* Timeout. */
95             wl_display_cancel_read(display);
96             return 0;
97          }
98          else if (-1 == err)
99          {
100             if (EINTR == errno)
101             {
102                /* Interrupted by a signal; restart. This resets the timeout. */
103                continue;
104             }
105             else
106             {
107                /* Something else bad happened; abort. */
108                wl_display_cancel_read(display);
109                return -1;
110             }
111          }
112          else
113          {
114             if (POLLIN == pfd.revents)
115             {
116                /* We have data to read, and no errors; proceed to read_events. */
117                break;
118             }
119             else
120             {
121                /* An error occurred, e.g. file descriptor was closed from underneath us. */
122                wl_display_cancel_read(display);
123                return -1;
124             }
125          }
126       }
127
128       /* Actually read the events from the display. A failure in read_events calls cancel_read internally for us,
129        * so we don't need to do that here. */
130       err = wl_display_read_events(display);
131       if (0 != err)
132       {
133          return -1;
134       }
135
136       /* Finally, if we read any events relevant to our queue, we can dispatch them. */
137       err = wl_display_dispatch_queue_pending(display, queue);
138       retval = err < 0 ? -1 : 1;
139
140       return retval;
141    }
142 }