Debian Codegraph @

xbmc/windowing/wayland/ShellSurfaceXdgShell.cpp

raw
1 /*
2 * Copyright (C) 2017-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "ShellSurfaceXdgShell.h"
10
11 #include "Registry.h"
12 #include "messaging/ApplicationMessenger.h"
13
14 using namespace KODI::WINDOWING::WAYLAND;
15
16 namespace
17 {
18
19 IShellSurface::State ConvertStateFlag(wayland::xdg_toplevel_state flag)
20 {
21 switch(flag)
22 {
23 case wayland::xdg_toplevel_state::activated:
24 return IShellSurface::STATE_ACTIVATED;
25 case wayland::xdg_toplevel_state::fullscreen:
26 return IShellSurface::STATE_FULLSCREEN;
27 case wayland::xdg_toplevel_state::maximized:
28 return IShellSurface::STATE_MAXIMIZED;
29 case wayland::xdg_toplevel_state::resizing:
30 return IShellSurface::STATE_RESIZING;
31 default:
32 throw std::runtime_error(std::string("Unknown xdg_toplevel state flag ") + std::to_string(static_cast<std::underlying_type<decltype(flag)>::type> (flag)));
33 }
34 }
35
36 }
37
38 CShellSurfaceXdgShell* CShellSurfaceXdgShell::TryCreate(IShellSurfaceHandler& handler, CConnection& connection, const wayland::surface_t& surface, std::string const& title, std::string const& class_)
39 {
40 wayland::xdg_wm_base_t shell;
41 CRegistry registry{connection};
42 registry.RequestSingleton(shell, 1, 1, false);
43 registry.Bind();
44
45 if (shell)
46 {
47 return new CShellSurfaceXdgShell(handler, connection.GetDisplay(), shell, surface, title, class_);
48 }
49 else
50 {
51 return nullptr;
52 }
53 }
54
55 CShellSurfaceXdgShell::CShellSurfaceXdgShell(IShellSurfaceHandler& handler, wayland::display_t& display, const wayland::xdg_wm_base_t& shell, const wayland::surface_t& surface, std::string const& title, std::string const& app_id)
56 : m_handler{handler}, m_display{display}, m_shell{shell}, m_surface{surface}, m_xdgSurface{m_shell.get_xdg_surface(m_surface)}, m_xdgToplevel{m_xdgSurface.get_toplevel()}
57 {
58 m_shell.on_ping() = [this](std::uint32_t serial)
59 {
60 m_shell.pong(serial);
61 };
62 m_xdgSurface.on_configure() = [this](std::uint32_t serial)
63 {
64 m_handler.OnConfigure(serial, m_configuredSize, m_configuredState);
65 };
66 m_xdgToplevel.on_close() = [this]()
67 {
68 m_handler.OnClose();
69 };
70 m_xdgToplevel.on_configure() = [this](std::int32_t width, std::int32_t height,
71 const std::vector<wayland::xdg_toplevel_state>& states) {
72 m_configuredSize.Set(width, height);
73 m_configuredState.reset();
74 for (auto state : states)
75 {
76 m_configuredState.set(ConvertStateFlag(state));
77 }
78 };
79 m_xdgToplevel.set_app_id(app_id);
80 m_xdgToplevel.set_title(title);
81 // Set sensible minimum size
82 m_xdgToplevel.set_min_size(300, 200);
83 }
84
85 void CShellSurfaceXdgShell::Initialize()
86 {
87 // Commit surface to confirm role
88 // Don't do it in constructor since SetFullScreen might be called before
89 m_surface.commit();
90 // Make sure we get the initial configure before continuing
91 m_display.roundtrip();
92 }
93
94 void CShellSurfaceXdgShell::AckConfigure(std::uint32_t serial)
95 {
96 m_xdgSurface.ack_configure(serial);
97 }
98
99 CShellSurfaceXdgShell::~CShellSurfaceXdgShell() noexcept
100 {
101 // xdg_shell is picky: must destroy toplevel role before surface
102 m_xdgToplevel.proxy_release();
103 m_xdgSurface.proxy_release();
104 }
105
106 void CShellSurfaceXdgShell::SetFullScreen(const wayland::output_t& output, float)
107 {
108 // xdg_shell does not support refresh rate setting at the moment
109 m_xdgToplevel.set_fullscreen(output);
110 }
111
112 void CShellSurfaceXdgShell::SetWindowed()
113 {
114 m_xdgToplevel.unset_fullscreen();
115 }
116
117 void CShellSurfaceXdgShell::SetMaximized()
118 {
119 m_xdgToplevel.set_maximized();
120 }
121
122 void CShellSurfaceXdgShell::UnsetMaximized()
123 {
124 m_xdgToplevel.unset_maximized();
125 }
126
127 void CShellSurfaceXdgShell::SetMinimized()
128 {
129 m_xdgToplevel.set_minimized();
130 }
131
132 void CShellSurfaceXdgShell::SetWindowGeometry(CRectInt geometry)
133 {
134 m_xdgSurface.set_window_geometry(geometry.x1, geometry.y1, geometry.Width(), geometry.Height());
135 }
136
137 void CShellSurfaceXdgShell::StartMove(const wayland::seat_t& seat, std::uint32_t serial)
138 {
139 m_xdgToplevel.move(seat, serial);
140 }
141
142 void CShellSurfaceXdgShell::StartResize(const wayland::seat_t& seat, std::uint32_t serial, wayland::shell_surface_resize edge)
143 {
144 // wl_shell shell_surface_resize is identical to xdg_shell resize_edge
145 m_xdgToplevel.resize(seat, serial, static_cast<std::uint32_t> (edge));
146 }
147
148 void CShellSurfaceXdgShell::ShowShellContextMenu(const wayland::seat_t& seat, std::uint32_t serial, CPointInt position)
149 {
150 m_xdgToplevel.show_window_menu(seat, serial, position.x, position.y);
151 }