xref: /aosp_15_r20/external/angle/util/linux/wayland/WaylandWindow.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker // WaylandWindow.cpp: Implementation of OSWindow for Wayland
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #include "util/linux/wayland/WaylandWindow.h"
10*8975f5c5SAndroid Build Coastguard Worker #include <cerrno>
11*8975f5c5SAndroid Build Coastguard Worker #include <cstring>
12*8975f5c5SAndroid Build Coastguard Worker 
WaylandWindow()13*8975f5c5SAndroid Build Coastguard Worker WaylandWindow::WaylandWindow()
14*8975f5c5SAndroid Build Coastguard Worker     : mDisplay{nullptr}, mCompositor{nullptr}, mSurface{nullptr}, mWindow{nullptr}
15*8975f5c5SAndroid Build Coastguard Worker {}
16*8975f5c5SAndroid Build Coastguard Worker 
~WaylandWindow()17*8975f5c5SAndroid Build Coastguard Worker WaylandWindow::~WaylandWindow()
18*8975f5c5SAndroid Build Coastguard Worker {
19*8975f5c5SAndroid Build Coastguard Worker     destroy();
20*8975f5c5SAndroid Build Coastguard Worker }
21*8975f5c5SAndroid Build Coastguard Worker 
RegistryHandleGlobal(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)22*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::RegistryHandleGlobal(void *data,
23*8975f5c5SAndroid Build Coastguard Worker                                          struct wl_registry *registry,
24*8975f5c5SAndroid Build Coastguard Worker                                          uint32_t name,
25*8975f5c5SAndroid Build Coastguard Worker                                          const char *interface,
26*8975f5c5SAndroid Build Coastguard Worker                                          uint32_t version)
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker     WaylandWindow *vc = reinterpret_cast<WaylandWindow *>(data);
29*8975f5c5SAndroid Build Coastguard Worker 
30*8975f5c5SAndroid Build Coastguard Worker     if (strcmp(interface, "wl_compositor") == 0)
31*8975f5c5SAndroid Build Coastguard Worker     {
32*8975f5c5SAndroid Build Coastguard Worker         void *compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
33*8975f5c5SAndroid Build Coastguard Worker         vc->mCompositor  = reinterpret_cast<wl_compositor *>(compositor);
34*8975f5c5SAndroid Build Coastguard Worker     }
35*8975f5c5SAndroid Build Coastguard Worker }
36*8975f5c5SAndroid Build Coastguard Worker 
RegistryHandleGlobalRemove(void * data,struct wl_registry * registry,uint32_t name)37*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::RegistryHandleGlobalRemove(void *data,
38*8975f5c5SAndroid Build Coastguard Worker                                                struct wl_registry *registry,
39*8975f5c5SAndroid Build Coastguard Worker                                                uint32_t name)
40*8975f5c5SAndroid Build Coastguard Worker {}
41*8975f5c5SAndroid Build Coastguard Worker 
42*8975f5c5SAndroid Build Coastguard Worker const struct wl_registry_listener WaylandWindow::registryListener = {
43*8975f5c5SAndroid Build Coastguard Worker     WaylandWindow::RegistryHandleGlobal, WaylandWindow::RegistryHandleGlobalRemove};
44*8975f5c5SAndroid Build Coastguard Worker 
initializeImpl(const std::string & name,int width,int height)45*8975f5c5SAndroid Build Coastguard Worker bool WaylandWindow::initializeImpl(const std::string &name, int width, int height)
46*8975f5c5SAndroid Build Coastguard Worker {
47*8975f5c5SAndroid Build Coastguard Worker     destroy();
48*8975f5c5SAndroid Build Coastguard Worker 
49*8975f5c5SAndroid Build Coastguard Worker     if (!mDisplay)
50*8975f5c5SAndroid Build Coastguard Worker     {
51*8975f5c5SAndroid Build Coastguard Worker         mDisplay = wl_display_connect(nullptr);
52*8975f5c5SAndroid Build Coastguard Worker         if (!mDisplay)
53*8975f5c5SAndroid Build Coastguard Worker         {
54*8975f5c5SAndroid Build Coastguard Worker             return false;
55*8975f5c5SAndroid Build Coastguard Worker         }
56*8975f5c5SAndroid Build Coastguard Worker     }
57*8975f5c5SAndroid Build Coastguard Worker 
58*8975f5c5SAndroid Build Coastguard Worker     // Not get a window
59*8975f5c5SAndroid Build Coastguard Worker     struct wl_registry *registry = wl_display_get_registry(mDisplay);
60*8975f5c5SAndroid Build Coastguard Worker     wl_registry_add_listener(registry, &registryListener, this);
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker     // Round-trip to get globals
63*8975f5c5SAndroid Build Coastguard Worker     wl_display_roundtrip(mDisplay);
64*8975f5c5SAndroid Build Coastguard Worker     if (!mCompositor)
65*8975f5c5SAndroid Build Coastguard Worker     {
66*8975f5c5SAndroid Build Coastguard Worker         return false;
67*8975f5c5SAndroid Build Coastguard Worker     }
68*8975f5c5SAndroid Build Coastguard Worker 
69*8975f5c5SAndroid Build Coastguard Worker     // We don't need this anymore
70*8975f5c5SAndroid Build Coastguard Worker     wl_registry_destroy(registry);
71*8975f5c5SAndroid Build Coastguard Worker 
72*8975f5c5SAndroid Build Coastguard Worker     mSurface = wl_compositor_create_surface(mCompositor);
73*8975f5c5SAndroid Build Coastguard Worker     if (!mSurface)
74*8975f5c5SAndroid Build Coastguard Worker     {
75*8975f5c5SAndroid Build Coastguard Worker         return false;
76*8975f5c5SAndroid Build Coastguard Worker     }
77*8975f5c5SAndroid Build Coastguard Worker 
78*8975f5c5SAndroid Build Coastguard Worker     mWindow = wl_egl_window_create(mSurface, width, height);
79*8975f5c5SAndroid Build Coastguard Worker     if (!mWindow)
80*8975f5c5SAndroid Build Coastguard Worker     {
81*8975f5c5SAndroid Build Coastguard Worker         return false;
82*8975f5c5SAndroid Build Coastguard Worker     }
83*8975f5c5SAndroid Build Coastguard Worker 
84*8975f5c5SAndroid Build Coastguard Worker     fds[0] = {wl_display_get_fd(mDisplay), POLLIN, 0};
85*8975f5c5SAndroid Build Coastguard Worker 
86*8975f5c5SAndroid Build Coastguard Worker     mY      = 0;
87*8975f5c5SAndroid Build Coastguard Worker     mX      = 0;
88*8975f5c5SAndroid Build Coastguard Worker     mWidth  = width;
89*8975f5c5SAndroid Build Coastguard Worker     mHeight = height;
90*8975f5c5SAndroid Build Coastguard Worker 
91*8975f5c5SAndroid Build Coastguard Worker     return true;
92*8975f5c5SAndroid Build Coastguard Worker }
93*8975f5c5SAndroid Build Coastguard Worker 
disableErrorMessageDialog()94*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::disableErrorMessageDialog() {}
95*8975f5c5SAndroid Build Coastguard Worker 
destroy()96*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::destroy()
97*8975f5c5SAndroid Build Coastguard Worker {
98*8975f5c5SAndroid Build Coastguard Worker     if (mWindow)
99*8975f5c5SAndroid Build Coastguard Worker     {
100*8975f5c5SAndroid Build Coastguard Worker         wl_egl_window_destroy(mWindow);
101*8975f5c5SAndroid Build Coastguard Worker         mWindow = nullptr;
102*8975f5c5SAndroid Build Coastguard Worker     }
103*8975f5c5SAndroid Build Coastguard Worker 
104*8975f5c5SAndroid Build Coastguard Worker     if (mSurface)
105*8975f5c5SAndroid Build Coastguard Worker     {
106*8975f5c5SAndroid Build Coastguard Worker         wl_surface_destroy(mSurface);
107*8975f5c5SAndroid Build Coastguard Worker         mSurface = nullptr;
108*8975f5c5SAndroid Build Coastguard Worker     }
109*8975f5c5SAndroid Build Coastguard Worker 
110*8975f5c5SAndroid Build Coastguard Worker     if (mCompositor)
111*8975f5c5SAndroid Build Coastguard Worker     {
112*8975f5c5SAndroid Build Coastguard Worker         wl_compositor_destroy(mCompositor);
113*8975f5c5SAndroid Build Coastguard Worker         mCompositor = nullptr;
114*8975f5c5SAndroid Build Coastguard Worker     }
115*8975f5c5SAndroid Build Coastguard Worker }
116*8975f5c5SAndroid Build Coastguard Worker 
resetNativeWindow()117*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::resetNativeWindow() {}
118*8975f5c5SAndroid Build Coastguard Worker 
setNativeDisplay(EGLNativeDisplayType display)119*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::setNativeDisplay(EGLNativeDisplayType display)
120*8975f5c5SAndroid Build Coastguard Worker {
121*8975f5c5SAndroid Build Coastguard Worker     mDisplay = reinterpret_cast<wl_display *>(display);
122*8975f5c5SAndroid Build Coastguard Worker }
123*8975f5c5SAndroid Build Coastguard Worker 
getNativeWindow() const124*8975f5c5SAndroid Build Coastguard Worker EGLNativeWindowType WaylandWindow::getNativeWindow() const
125*8975f5c5SAndroid Build Coastguard Worker {
126*8975f5c5SAndroid Build Coastguard Worker     return reinterpret_cast<EGLNativeWindowType>(mWindow);
127*8975f5c5SAndroid Build Coastguard Worker }
128*8975f5c5SAndroid Build Coastguard Worker 
getNativeDisplay() const129*8975f5c5SAndroid Build Coastguard Worker EGLNativeDisplayType WaylandWindow::getNativeDisplay() const
130*8975f5c5SAndroid Build Coastguard Worker {
131*8975f5c5SAndroid Build Coastguard Worker     return reinterpret_cast<EGLNativeDisplayType>(mDisplay);
132*8975f5c5SAndroid Build Coastguard Worker }
133*8975f5c5SAndroid Build Coastguard Worker 
messageLoop()134*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::messageLoop()
135*8975f5c5SAndroid Build Coastguard Worker {
136*8975f5c5SAndroid Build Coastguard Worker     while (wl_display_prepare_read(mDisplay) != 0)
137*8975f5c5SAndroid Build Coastguard Worker         wl_display_dispatch_pending(mDisplay);
138*8975f5c5SAndroid Build Coastguard Worker     if (wl_display_flush(mDisplay) < 0 && errno != EAGAIN)
139*8975f5c5SAndroid Build Coastguard Worker     {
140*8975f5c5SAndroid Build Coastguard Worker         wl_display_cancel_read(mDisplay);
141*8975f5c5SAndroid Build Coastguard Worker         return;
142*8975f5c5SAndroid Build Coastguard Worker     }
143*8975f5c5SAndroid Build Coastguard Worker     if (poll(fds, 1, 0) > 0)
144*8975f5c5SAndroid Build Coastguard Worker     {
145*8975f5c5SAndroid Build Coastguard Worker         wl_display_read_events(mDisplay);
146*8975f5c5SAndroid Build Coastguard Worker         wl_display_dispatch_pending(mDisplay);
147*8975f5c5SAndroid Build Coastguard Worker     }
148*8975f5c5SAndroid Build Coastguard Worker     else
149*8975f5c5SAndroid Build Coastguard Worker     {
150*8975f5c5SAndroid Build Coastguard Worker         wl_display_cancel_read(mDisplay);
151*8975f5c5SAndroid Build Coastguard Worker     }
152*8975f5c5SAndroid Build Coastguard Worker }
153*8975f5c5SAndroid Build Coastguard Worker 
setMousePosition(int x,int y)154*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::setMousePosition(int x, int y) {}
155*8975f5c5SAndroid Build Coastguard Worker 
setOrientation(int width,int height)156*8975f5c5SAndroid Build Coastguard Worker bool WaylandWindow::setOrientation(int width, int height)
157*8975f5c5SAndroid Build Coastguard Worker {
158*8975f5c5SAndroid Build Coastguard Worker     return true;
159*8975f5c5SAndroid Build Coastguard Worker }
160*8975f5c5SAndroid Build Coastguard Worker 
setPosition(int x,int y)161*8975f5c5SAndroid Build Coastguard Worker bool WaylandWindow::setPosition(int x, int y)
162*8975f5c5SAndroid Build Coastguard Worker {
163*8975f5c5SAndroid Build Coastguard Worker     return true;
164*8975f5c5SAndroid Build Coastguard Worker }
165*8975f5c5SAndroid Build Coastguard Worker 
resize(int width,int height)166*8975f5c5SAndroid Build Coastguard Worker bool WaylandWindow::resize(int width, int height)
167*8975f5c5SAndroid Build Coastguard Worker {
168*8975f5c5SAndroid Build Coastguard Worker     wl_egl_window_resize(mWindow, width, height, 0, 0);
169*8975f5c5SAndroid Build Coastguard Worker 
170*8975f5c5SAndroid Build Coastguard Worker     mWidth  = width;
171*8975f5c5SAndroid Build Coastguard Worker     mHeight = height;
172*8975f5c5SAndroid Build Coastguard Worker 
173*8975f5c5SAndroid Build Coastguard Worker     return true;
174*8975f5c5SAndroid Build Coastguard Worker }
175*8975f5c5SAndroid Build Coastguard Worker 
setVisible(bool isVisible)176*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::setVisible(bool isVisible) {}
177*8975f5c5SAndroid Build Coastguard Worker 
signalTestEvent()178*8975f5c5SAndroid Build Coastguard Worker void WaylandWindow::signalTestEvent() {}
179*8975f5c5SAndroid Build Coastguard Worker 
IsWaylandWindowAvailable()180*8975f5c5SAndroid Build Coastguard Worker bool IsWaylandWindowAvailable()
181*8975f5c5SAndroid Build Coastguard Worker {
182*8975f5c5SAndroid Build Coastguard Worker     wl_display *display = wl_display_connect(nullptr);
183*8975f5c5SAndroid Build Coastguard Worker     if (!display)
184*8975f5c5SAndroid Build Coastguard Worker     {
185*8975f5c5SAndroid Build Coastguard Worker         return false;
186*8975f5c5SAndroid Build Coastguard Worker     }
187*8975f5c5SAndroid Build Coastguard Worker     wl_display_disconnect(display);
188*8975f5c5SAndroid Build Coastguard Worker     return true;
189*8975f5c5SAndroid Build Coastguard Worker }
190