-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathdecoration_provider.cpp
458 lines (381 loc) · 13.5 KB
/
decoration_provider.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* 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 "decoration_provider.h"
#include "wallpaper_config.h"
#include "wayland_app.h"
#include "wayland_surface.h"
#include "wayland_shm.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <poll.h>
#include <sys/eventfd.h>
#include <locale>
#include <codecvt>
#include <map>
#include <string>
#include <cstring>
#include <boost/throw_exception.hpp>
#include <system_error>
#include <iostream>
using namespace mir::geometry;
namespace
{
struct BackgroundInfo;
struct preferred_codecvt : std::codecvt_byname<wchar_t, char, std::mbstate_t>
{
preferred_codecvt() : std::codecvt_byname<wchar_t, char, std::mbstate_t>("C") {}
~preferred_codecvt() = default;
};
struct Printer
{
Printer();
~Printer();
Printer(Printer const&) = delete;
Printer& operator=(Printer const&) = delete;
void printhelp(BackgroundInfo const& region);
private:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
std::wstring_convert<preferred_codecvt> converter;
#pragma GCC diagnostic pop
bool working = false;
FT_Library lib;
FT_Face face;
};
Printer::Printer()
{
if (FT_Init_FreeType(&lib))
return;
if (FT_New_Face(lib, wallpaper::font_file().c_str(), 0, &face))
{
std::cerr << "WARNING: failed to load font: \"" << wallpaper::font_file() << "\"\n";
FT_Done_FreeType(lib);
return;
}
FT_Set_Pixel_Sizes(face, 0, 10);
working = true;
}
Printer::~Printer()
{
if (working)
{
FT_Done_Face(face);
FT_Done_FreeType(lib);
}
}
struct BackgroundInfo : WaylandSurface
{
BackgroundInfo(
WaylandApp const* app,
WaylandOutput const* output)
: WaylandSurface{app},
output{output},
shm{app->shm()}
{
set_fullscreen(output->wl());
commit();
app->roundtrip();
}
void configured() override
{
needs_redraw = true;
}
~BackgroundInfo()
{
app()->roundtrip();
}
/// returns the size (in pixels) the buffer should be
auto buffer_size() const -> Size { return configured_size() * output->scale(); }
// Screen description
WaylandOutput const* const output;
WaylandShm shm;
// Content
void* content_area = nullptr;
bool needs_redraw{true};
private:
BackgroundInfo(BackgroundInfo const&) = delete;
BackgroundInfo& operator=(BackgroundInfo const&) = delete;
};
void Printer::printhelp(BackgroundInfo const& region)
{
if (!working)
return;
auto const region_size = region.buffer_size();
if (region_size.width <= Width{} || region_size.height <= Height{})
return;
static char const* const helptext[] =
{
"Welcome to miral-shell",
"",
"Keyboard shortcuts:",
"",
" o Terminal: Ctrl-Alt-T/Ctrl-Alt-Shift-T",
" o X Terminal: Ctrl-Alt-X (if X11 is enabled)",
"",
" o Switch apps: Alt-Tab, tap or click on the corresponding window",
" o Next (previous) app window: Alt-` (Alt-Shift-`)",
"",
" o Move window: Alt-leftmousebutton drag (three finger drag)",
" o Resize window: Alt-middle_button drag (three finger pinch)",
"",
" o Maximize/restore current window (to display size). : Alt-F11",
" o Maximize/restore current window (to display height): Shift-F11",
" o Maximize/restore current window (to display width) : Ctrl-F11",
"",
" o Switch workspace: Meta-Alt-[F1|F2|F3|F4]",
" o Switch workspace taking active window: Meta-Ctrl-[F1|F2|F3|F4]",
"",
" o To exit: Ctrl-Alt-BkSp",
};
auto const min_char_width = std::min({region_size.width.as_int()/60, region_size.height.as_int()/35, 20});
FT_Set_Pixel_Sizes(face, min_char_width, 0);
Width help_width;
Height help_height;
DeltaY line_height;
for (char const* rawline : helptext)
{
auto const line_text = converter.from_bytes(rawline);
auto line_width = Width{};
for (auto const& character : line_text)
{
FT_Load_Glyph(face, FT_Get_Char_Index(face, character), FT_LOAD_DEFAULT);
auto const glyph = face->glyph;
FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL);
line_width = line_width + DeltaX{glyph->advance.x >> 6};
line_height = std::max(line_height, DeltaY{glyph->bitmap.rows * 1.5});
}
help_width = std::max(help_width, line_width);
help_height = help_height + line_height;
}
auto base_pos_y = 0.5*(region_size.height - help_height);
static auto const region_pixel_bytes = 4;
unsigned char* const region_buffer = reinterpret_cast<char unsigned*>(region.content_area);
for (auto const* rawline : helptext)
{
auto base_pos_x = 0.5*(region_size.width - help_width);
auto const line_text = converter.from_bytes(rawline);
for (auto const& character : line_text)
{
FT_Load_Glyph(face, FT_Get_Char_Index(face, character), FT_LOAD_DEFAULT);
auto const glyph = face->glyph;
FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL);
auto const glyph_size = Size{glyph->bitmap.width, glyph->bitmap.rows};
auto const pos = Point{glyph->bitmap_left, -glyph->bitmap_top} + Displacement{base_pos_x, base_pos_y};
auto const region_top_left_in_glyph_space = Point{} + (pos - Point{}) * -1; // literally just -pos
auto const region_lower_right_in_glyph_space = region_top_left_in_glyph_space + as_displacement(region_size);
auto const clipped_glyph_upper_left = Point{
std::max(X{}, region_top_left_in_glyph_space.x),
std::max(Y{}, region_top_left_in_glyph_space.y)};
auto const clipped_glyph_lower_right = Point{
std::min(X{} + as_displacement(glyph_size).dx, region_lower_right_in_glyph_space.x),
std::min(Y{} + as_displacement(glyph_size).dy, region_lower_right_in_glyph_space.y)};
unsigned char* const glyph_buffer = glyph->bitmap.buffer;
auto glyph_pixel = Point{};
auto region_pixel = Point{};
for (
glyph_pixel.y = clipped_glyph_upper_left.y;
glyph_pixel.y < clipped_glyph_lower_right.y;
glyph_pixel.y += DeltaY{1})
{
region_pixel.y = glyph_pixel.y + (pos.y - Y{});
unsigned char* glyph_buffer_row = glyph_buffer + glyph_pixel.y.as_int() * glyph->bitmap.pitch;
unsigned char* const region_buffer_row =
region_buffer + ((long)region_pixel.y.as_int() * (long)region_size.width.as_int() * region_pixel_bytes);
for (
glyph_pixel.x = clipped_glyph_upper_left.x;
glyph_pixel.x < clipped_glyph_lower_right.x;
glyph_pixel.x += DeltaX{1})
{
region_pixel.x = glyph_pixel.x + (pos.x - X{});
double const source_value = glyph_buffer_row[glyph_pixel.x.as_int()] / 255.0;
(void)source_value;
unsigned char* const region_buffer_offset =
region_buffer_row + ((long long)region_pixel.x.as_int() * region_pixel_bytes);
for (
unsigned char* region_ptr = region_buffer_offset;
region_ptr < region_buffer_offset + region_pixel_bytes;
region_ptr++)
{
*region_ptr = 0xff - (int)((0xff - *region_ptr) * (1 - source_value));
}
}
}
base_pos_x = base_pos_x + DeltaX{glyph->advance.x >> 6};
}
base_pos_y = base_pos_y + line_height;
}
}
struct DecorationProviderClient : WaylandApp
{
public:
DecorationProviderClient(wl_display* display);
~DecorationProviderClient();
// Called after events were dispatched and now the loop is idle
void events_dispatched();
private:
void draw_background(BackgroundInfo& ctx) const;
void output_ready(WaylandOutput const*) override;
void output_changed(WaylandOutput const* output) override;
void output_gone(WaylandOutput const*) override;
std::map<WaylandOutput const*, std::shared_ptr<BackgroundInfo>> outputs;
};
DecorationProviderClient::DecorationProviderClient(wl_display* display)
{
wayland_init(display);
}
void DecorationProviderClient::events_dispatched()
{
bool needs_flush = false;
for (auto const& output : outputs)
{
if (output.second->needs_redraw)
{
draw_background(*output.second);
output.second->needs_redraw = false;
needs_flush = true;
}
}
if (needs_flush)
{
wl_display_flush(display());
}
}
void DecorationProviderClient::output_ready(WaylandOutput const* output)
{
outputs.insert({output, std::make_shared<BackgroundInfo>(this, output)});
}
void DecorationProviderClient::output_changed(WaylandOutput const* output)
{
auto const p = outputs.find(output);
if (p != end(outputs))
{
p->second->needs_redraw = true;
}
}
void DecorationProviderClient::output_gone(WaylandOutput const* output)
{
outputs.erase(output);
}
void DecorationProviderClient::draw_background(BackgroundInfo& ctx) const
{
auto const width = ctx.buffer_size().width.as_int();
auto const height = ctx.buffer_size().height.as_int();
if (width <= 0 || height <= 0)
return;
auto const stride = 4*width;
auto const buffer = ctx.shm.get_buffer(ctx.buffer_size(), Stride{stride});
ctx.content_area = buffer->data();
uint8_t const bottom_colour[] = { 0x20, 0x54, 0xe9 }; // Ubuntu orange
uint8_t const top_colour[] = { 0x33, 0x33, 0x33 }; // Cool grey
char* row = static_cast<decltype(row)>(ctx.content_area);
for (int j = 0; j < height; j++)
{
uint8_t pattern[4];
for (auto i = 0; i != 3; ++i)
pattern[i] = (j*bottom_colour[i] + (height-j)*top_colour[i])/height;
pattern[3] = 0xff;
uint32_t* pixel = (uint32_t*)row;
for (int i = 0; i < width; i++)
memcpy(pixel + i, pattern, sizeof pixel[i]);
row += stride;
}
static Printer printer;
printer.printhelp(ctx);
ctx.attach_buffer(buffer->use(), ctx.output->scale());
ctx.commit();
}
DecorationProviderClient::~DecorationProviderClient()
{
}
}
using namespace mir::geometry;
DecorationProvider::DecorationProvider() :
shutdown_signal{::eventfd(0, EFD_CLOEXEC)}
{
if (shutdown_signal == mir::Fd::invalid)
{
BOOST_THROW_EXCEPTION((
std::system_error{errno, std::system_category(), "Failed to create shutdown notifier"}));
}
}
DecorationProvider::~DecorationProvider() = default;
void DecorationProvider::stop()
{
if (eventfd_write(shutdown_signal, 1) == -1)
{
BOOST_THROW_EXCEPTION((
std::system_error{
errno,
std::system_category(),
"Failed to shutdown internal decoration client"}));
}
}
void DecorationProvider::operator()(wl_display* display)
{
DecorationProviderClient self(display);
enum FdIndices {
display_fd = 0,
shutdown,
indices
};
pollfd fds[indices];
fds[display_fd] = {wl_display_get_fd(display), POLLIN, 0};
fds[shutdown] = {shutdown_signal, POLLIN, 0};
while (!(fds[shutdown].revents & (POLLIN | POLLERR)))
{
while (wl_display_prepare_read(display) != 0)
{
if (wl_display_dispatch_pending(display) == -1)
{
BOOST_THROW_EXCEPTION((
std::system_error{errno, std::system_category(), "Failed to dispatch Wayland events"}));
}
}
self.events_dispatched();
if (poll(fds, indices, -1) == -1)
{
wl_display_cancel_read(display);
BOOST_THROW_EXCEPTION((
std::system_error{errno, std::system_category(), "Failed to wait for event"}));
}
if (fds[display_fd].revents & (POLLIN | POLLERR))
{
if (wl_display_read_events(display))
{
BOOST_THROW_EXCEPTION((
std::system_error{errno, std::system_category(), "Failed to read Wayland events"}));
}
}
else
{
wl_display_cancel_read(display);
}
}
}
void DecorationProvider::operator()(std::weak_ptr<mir::scene::Session> const& session)
{
std::lock_guard lock{mutex};
this->weak_session = session;
}
auto DecorationProvider::session() const -> std::shared_ptr<mir::scene::Session>
{
std::lock_guard lock{mutex};
return weak_session.lock();
}
bool DecorationProvider::is_decoration(miral::Window const& window) const
{
return window.application() == session();
}