1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wayland_app.h"
#include <cstring>
#include <algorithm>
// If building against newer Wayland protocol definitions we may miss trailing fields
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
wl_callback_listener const WaylandCallback::callback_listener {
[]/* done */(void* data, wl_callback*, uint32_t)
{
auto const cb = static_cast<WaylandCallback*>(data);
cb->func();
delete cb;
},
};
wl_output_listener const WaylandOutput::output_listener = {
handle_geometry,
handle_mode,
handle_done,
handle_scale,
};
#pragma GCC diagnostic pop
void WaylandCallback::create(wl_callback* callback, std::function<void()>&& func)
{
auto const cb = new WaylandCallback({callback, wl_callback_destroy}, std::move(func));
wl_callback_add_listener(callback, &callback_listener, cb);
// Will destroy itself when called
}
namespace
{
void wl_output_release_if_valid(struct wl_output* wl_output)
{
if (wl_output_get_version(wl_output) >= WL_OUTPUT_RELEASE_SINCE_VERSION)
wl_output_release(wl_output);
}
}
WaylandOutput::WaylandOutput(WaylandApp* app, wl_output* output)
: app{app},
wl_{output, wl_output_release_if_valid},
has_initialized{false},
state_dirty{false},
scale_{1},
transform_{WL_OUTPUT_TRANSFORM_NORMAL}
{
wl_output_add_listener(output, &output_listener, this);
}
void WaylandOutput::handle_geometry(
void* data,
struct wl_output* /*wl_output*/,
int32_t /*x*/,
int32_t /*y*/,
int32_t /*physical_width*/,
int32_t /*physical_height*/,
int32_t /*subpixel*/,
const char* /*make*/,
const char* /*model*/,
int32_t transform)
{
auto self = static_cast<WaylandOutput*>(data);
if (self->transform_ != transform)
{
self->transform_ = transform;
self->state_dirty = true;
}
}
void WaylandOutput::handle_mode(
void* /*data*/,
struct wl_output* /*wl_output*/,
uint32_t /*flags*/,
int32_t /*width*/,
int32_t /*height*/,
int32_t /*refresh*/)
{
}
void WaylandOutput::handle_done(void* data, struct wl_output* /*wl_output*/)
{
auto self = static_cast<WaylandOutput*>(data);
if (!self->has_initialized)
{
self->app->output_ready(self);
self->state_dirty = false;
self->has_initialized = true;
}
else if (self->state_dirty)
{
self->app->output_changed(self);
self->state_dirty = false;
}
}
void WaylandOutput::handle_scale(void* data, struct wl_output* /*wl_output*/, int32_t factor)
{
auto self = static_cast<WaylandOutput*>(data);
self->scale_ = factor;
}
wl_registry_listener const WaylandApp::registry_listener = {
handle_new_global,
handle_global_remove
};
WaylandApp::WaylandApp()
: display_{nullptr, [](auto){ return 0; }}
{
}
WaylandApp::WaylandApp(wl_display* display)
: WaylandApp{}
{
wayland_init(display);
}
void WaylandApp::wayland_init(wl_display* display)
{
display_ = {display, wl_display_roundtrip};
registry_ = {wl_display_get_registry(display), wl_registry_destroy};
wl_registry_add_listener(registry_, ®istry_listener, this);
// Populate globals
roundtrip();
// Populate properties for outputs and other globals created in the first pass
roundtrip();
}
void WaylandApp::roundtrip() const
{
wl_display_roundtrip(display());
}
void WaylandApp::handle_new_global(
void* data,
struct wl_registry* registry,
uint32_t id,
char const* interface,
uint32_t version)
{
auto const self = static_cast<WaylandApp*>(data);
if (strcmp(interface, "wl_compositor") == 0)
{
self->compositor_ = {
static_cast<wl_compositor*>(wl_registry_bind(registry, id, &wl_compositor_interface, 3)),
wl_compositor_destroy};
self->global_remove_handlers[id] = [self](){ self->compositor_ = {}; };
}
else if (strcmp(interface, "wl_shm") == 0)
{
self->shm_ = {
static_cast<wl_shm*>(wl_registry_bind(registry, id, &wl_shm_interface, 1)),
wl_shm_destroy};
self->global_remove_handlers[id] = [self](){ self->shm_ = {}; };
// Normally we'd add a listener to pick up the supported formats here
// As luck would have it, I know that argb8888 is the only format we support :)
}
else if (strcmp(interface, "wl_seat") == 0)
{
self->seat_ = {
static_cast<wl_seat*>(wl_registry_bind(registry, id, &wl_seat_interface, 4)),
wl_seat_destroy};
self->global_remove_handlers[id] = [self](){ self->seat_ = {}; };
}
else if (strcmp(interface, "wl_shell") == 0)
{
self->shell_ = {
static_cast<wl_shell*>(wl_registry_bind(registry, id, &wl_shell_interface, 1)),
wl_shell_destroy};
self->global_remove_handlers[id] = [self](){ self->shell_ = {}; };
}
else if (strcmp(interface, "wl_output") == 0)
{
auto const output_resource = static_cast<wl_output*>(wl_registry_bind(registry, id, &wl_output_interface, std::min(version, 3u)));
// shared_ptr instead of unique_ptr only so it can be captured by the lambda
auto output = std::make_shared<WaylandOutput>(self, output_resource);
// global_remove_handlers will hold on to the output until it is removed
self->global_remove_handlers[id] = [self, output = std::move(output)]()
{
self->output_gone(output.get());
};
}
}
void WaylandApp::handle_global_remove(void* data, struct wl_registry* /*registry*/, uint32_t id)
{
auto const self = static_cast<WaylandApp*>(data);
auto const handler = self->global_remove_handlers.find(id);
if (handler != self->global_remove_handlers.end())
{
handler->second();
self->global_remove_handlers.erase(handler);
}
}
|