forked from project-repo/cagebreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.c
309 lines (276 loc) · 8.91 KB
/
message.c
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
// Copyright 2020 - 2023, project-repo and the cagebreak contributors
// SPDX -License-Identifier: MIT
#include <cairo/cairo.h>
#include <drm_fourcc.h>
#include <pango/pangocairo.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wlr/backend.h>
#include <wlr/interfaces/wlr_buffer.h>
#include <wlr/render/allocator.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_output_damage.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/log.h>
#include "message.h"
#include "output.h"
#include "pango.h"
#include "server.h"
#include "util.h"
struct msg_buffer {
struct wlr_buffer base;
void *data;
uint32_t format;
size_t stride;
};
static void
msg_buffer_destroy(struct wlr_buffer *wlr_buffer) {
struct msg_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
free(buffer->data);
free(buffer);
}
static bool
msg_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer, uint32_t flags,
void **data, uint32_t *format,
size_t *stride) {
struct msg_buffer *buffer = wl_container_of(wlr_buffer, buffer, base);
if(data != NULL) {
*data = (void *)buffer->data;
}
if(format != NULL) {
*format = buffer->format;
}
if(stride != NULL) {
*stride = buffer->stride;
}
return true;
}
static void
msg_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
// This space is intentionally left blank
}
static const struct wlr_buffer_impl msg_buffer_impl = {
.destroy = msg_buffer_destroy,
.begin_data_ptr_access = msg_buffer_begin_data_ptr_access,
.end_data_ptr_access = msg_buffer_end_data_ptr_access,
};
static struct msg_buffer *
msg_buffer_create(uint32_t width, uint32_t height, uint32_t stride) {
struct msg_buffer *buffer = calloc(1, sizeof(*buffer));
if(buffer == NULL) {
return NULL;
}
wlr_buffer_init(&buffer->base, &msg_buffer_impl, width, height);
buffer->format = DRM_FORMAT_ARGB8888;
buffer->stride = stride;
buffer->data = malloc(buffer->stride * height);
if(buffer->data == NULL) {
free(buffer);
return NULL;
}
return buffer;
}
cairo_subpixel_order_t
to_cairo_subpixel_order(const enum wl_output_subpixel subpixel) {
switch(subpixel) {
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
return CAIRO_SUBPIXEL_ORDER_RGB;
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
return CAIRO_SUBPIXEL_ORDER_BGR;
case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
return CAIRO_SUBPIXEL_ORDER_VRGB;
case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
return CAIRO_SUBPIXEL_ORDER_VBGR;
default:
return CAIRO_SUBPIXEL_ORDER_DEFAULT;
}
return CAIRO_SUBPIXEL_ORDER_DEFAULT;
}
struct msg_buffer *
create_message_texture(const char *string, const struct cg_output *output) {
const int WIDTH_PADDING = 8;
const int HEIGHT_PADDING = 2;
double scale = output->wlr_output->scale;
int width = 0;
int height = 0;
// We must use a non-nil cairo_t for cairo_set_font_options to work.
// Therefore, we cannot use cairo_create(NULL).
cairo_surface_t *dummy_surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
// This occurs when we are fuzzing. In that case, do nothing
if(dummy_surface == NULL) {
return NULL;
}
cairo_t *c = cairo_create(dummy_surface);
cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST);
cairo_font_options_t *fo = cairo_font_options_create();
cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
cairo_font_options_set_subpixel_order(
fo, to_cairo_subpixel_order(output->wlr_output->subpixel));
cairo_set_font_options(c, fo);
get_text_size(c, output->server->message_config.font, &width, &height, NULL,
scale, "%s", string);
width += 2 * WIDTH_PADDING;
height += 2 * HEIGHT_PADDING;
cairo_surface_destroy(dummy_surface);
cairo_destroy(c);
cairo_surface_t *surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cairo = cairo_create(surface);
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
cairo_set_font_options(cairo, fo);
cairo_font_options_destroy(fo);
float *bg_col = output->server->message_config.bg_color;
cairo_set_source_rgba(cairo, bg_col[0], bg_col[1], bg_col[2], bg_col[3]);
cairo_paint(cairo);
float *fg_col = output->server->message_config.fg_color;
cairo_set_source_rgba(cairo, fg_col[0], fg_col[1], fg_col[2], fg_col[3]);
cairo_set_line_width(cairo, 2);
cairo_rectangle(cairo, 0, 0, width, height);
cairo_stroke(cairo);
cairo_move_to(cairo, WIDTH_PADDING, HEIGHT_PADDING);
pango_printf(cairo, output->server->message_config.font, scale, "%s",
string);
cairo_surface_flush(surface);
unsigned char *data = cairo_image_surface_get_data(surface);
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
struct msg_buffer *buf = msg_buffer_create(width, height, stride);
void *data_ptr;
if(!wlr_buffer_begin_data_ptr_access(&buf->base,
WLR_BUFFER_DATA_PTR_ACCESS_WRITE,
&data_ptr, NULL, NULL)) {
wlr_log(WLR_ERROR, "Failed to get pointer access to message buffer");
return NULL;
}
memcpy(data_ptr, data, stride * height);
wlr_buffer_end_data_ptr_access(&buf->base);
cairo_surface_destroy(surface);
cairo_destroy(cairo);
return buf;
}
#if CG_HAS_FANALYZE
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
#endif
void
message_set_output(struct cg_output *output, const char *string,
struct wlr_box *box, enum cg_message_align align) {
struct cg_message *message = malloc(sizeof(struct cg_message));
if(!message) {
wlr_log(WLR_ERROR, "Error allocating message structure");
free(box);
return;
}
struct msg_buffer *buf = create_message_texture(string, output);
if(!buf) {
wlr_log(WLR_ERROR, "Could not create message texture");
free(box);
free(message);
return;
}
message->position = box;
wl_list_insert(&output->messages, &message->link);
double scale = output->wlr_output->scale;
int width = buf->base.width / scale;
int height = buf->base.height / scale;
message->position->width = width;
message->position->height = height;
switch(align) {
case CG_MESSAGE_TOP_RIGHT: {
message->position->x -= width;
break;
}
case CG_MESSAGE_BOTTOM_LEFT: {
message->position->y -= height;
break;
}
case CG_MESSAGE_BOTTOM_RIGHT: {
message->position->x -= width;
message->position->y -= height;
break;
}
case CG_MESSAGE_CENTER: {
message->position->x -= width / 2;
message->position->y -= height / 2;
break;
}
case CG_MESSAGE_TOP_LEFT:
default:
break;
}
struct wlr_scene_output *scene_output =
wlr_scene_get_scene_output(output->server->scene, output->wlr_output);
if(scene_output == NULL) {
return;
}
message->message =
wlr_scene_buffer_create(&scene_output->scene->tree, &buf->base);
wlr_scene_node_raise_to_top(&message->message->node);
wlr_scene_node_set_enabled(&message->message->node, true);
struct wlr_box outp_box;
wlr_output_layout_get_box(output->server->output_layout, output->wlr_output,
&outp_box);
wlr_scene_buffer_set_dest_size(message->message, width, height);
wlr_scene_node_set_position(&message->message->node,
message->position->x + outp_box.x,
message->position->y + outp_box.y);
}
void
message_printf(struct cg_output *output, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
char *buffer = malloc_vsprintf_va_list(fmt, ap);
va_end(ap);
if(buffer == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate buffer in message_printf");
return;
}
struct wlr_box *box = malloc(sizeof(struct wlr_box));
if(box == NULL) {
wlr_log(WLR_ERROR, "Failed to allocate box in message_printf");
free(buffer);
return;
}
struct wlr_box output_box;
wlr_output_layout_get_box(output->server->output_layout, output->wlr_output,
&output_box);
box->x = output_box.width;
box->y = 0;
box->width = 0;
box->height = 0;
message_set_output(output, buffer, box, CG_MESSAGE_TOP_RIGHT);
free(buffer);
alarm(output->server->message_config.display_time);
}
#if CG_HAS_FANALYZE
#pragma GCC diagnostic pop
#endif
void
message_printf_pos(struct cg_output *output, struct wlr_box *position,
const enum cg_message_align align, const char *fmt, ...) {
uint16_t buf_len = 256;
char *buffer = (char *)malloc(buf_len * sizeof(char));
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, buf_len, fmt, ap);
va_end(ap);
message_set_output(output, buffer, position, align);
free(buffer);
alarm(output->server->message_config.display_time);
}
void
message_clear(struct cg_output *output) {
struct cg_message *message, *tmp;
wl_list_for_each_safe(message, tmp, &output->messages, link) {
wl_list_remove(&message->link);
struct wlr_buffer *buf = message->message->buffer;
wlr_scene_node_destroy(&message->message->node);
free(message->position);
buf->impl->destroy(buf);
free(message);
}
}