-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.c
333 lines (281 loc) · 10.9 KB
/
render.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include <wayland-client.h>
#include "action.h"
#include "cairo.h"
#include "background-image.h"
#include "waylogout.h"
// glib might or might not have already defined MIN,
// depending on whether we have pixbuf or not...
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define M_PI 3.14159265358979323846
const float TYPE_INDICATOR_RANGE = M_PI / 3.0f;
const float TYPE_INDICATOR_BORDER_THICKNESS = M_PI / 128.0f;
static void set_color_for_state(cairo_t *cairo, bool selected,
struct waylogout_colorset *colorset) {
cairo_set_source_u32(cairo, selected ? colorset->selected : colorset->normal);
}
void render_frame_background(struct waylogout_surface *surface, bool commit) {
struct waylogout_state *state = surface->state;
int buffer_width = surface->width * surface->scale;
int buffer_height = surface->height * surface->scale;
if (buffer_width == 0 || buffer_height == 0) {
return; // not yet configured
}
struct pool_buffer *buffer = get_next_buffer(state->shm,
surface->buffers, buffer_width, buffer_height);
if (buffer == NULL) {
return;
}
cairo_t *cairo = buffer->cairo;
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
cairo_save(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_source_u32(cairo, state->args.colors.background);
cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_BILINEAR);
cairo_paint(cairo);
if (surface->image && state->args.mode != BACKGROUND_MODE_SOLID_COLOR) {
cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
if (fade_is_complete(&surface->fade)) {
if (!surface->scaled_image) {
surface->scaled_image =
scale_background_image(surface->image, state->args.mode,
buffer_width, buffer_height);
}
render_background_image(cairo, surface->scaled_image, 1);
} else {
if (!surface->screencopy.scaled_image) {
surface->screencopy.scaled_image =
scale_background_image(surface->screencopy.original_image,
state->args.mode, buffer_width, buffer_height);
}
render_background_image(cairo, surface->screencopy.scaled_image, 1);
if (!surface->scaled_image) {
surface->scaled_image =
scale_background_image(surface->image, state->args.mode,
buffer_width, buffer_height);
}
render_background_image(cairo, surface->scaled_image, surface->fade.alpha);
}
}
cairo_restore(cairo);
cairo_identity_matrix(cairo);
wl_surface_set_buffer_scale(surface->surface, surface->scale);
wl_surface_attach(surface->surface, buffer->buffer, 0, 0);
wl_surface_damage_buffer(surface->surface, 0, 0, INT32_MAX, INT32_MAX);
if (commit) {
wl_surface_commit(surface->surface);
}
}
void render_background_fade(struct waylogout_surface *surface, uint32_t time) {
if (fade_is_complete(&surface->fade)) {
return;
}
fade_update(&surface->fade, time);
render_frame_background(surface, true);
render_frames(surface);
}
void render_frame(struct waylogout_action *action,
struct waylogout_surface *surface,
struct waylogout_frame_common fr_common) {
struct waylogout_state *state = surface->state;
bool selected = (action == state->selected_action);
// find child surface for this action
struct waylogout_action_surface *action_surface;
wl_array_for_each(action_surface, &surface->children) {
if (action_surface->action == action)
break;
}
int buffer_width = action_surface->indicator_width;
int buffer_height = action_surface->indicator_height;
int new_width = fr_common.indicator_diameter;
int new_height = fr_common.indicator_diameter;
double indicator_xcenter = fr_common.x_center -
((state->rows[action->row] - 1) / 2.0f - fr_common.n_drawn) * fr_common.x_offset;
double dbl_subsurf_xcenter = indicator_xcenter -
buffer_width / (2.0f * surface->scale) + 2 / (1.0f * surface->scale);
int subsurf_xcenter = dbl_subsurf_xcenter;
int subsurf_ycenter = fr_common.y_center -
(state->args.radius + state->args.thickness) -
fr_common.y_row_offset;
bool will_render_depressed = false;
if (selected && state->selected_action_depressed) {
will_render_depressed = true;
subsurf_xcenter += 2;
subsurf_ycenter += 2;
}
wl_subsurface_set_position(action_surface->subsurface, subsurf_xcenter, subsurf_ycenter);
struct pool_buffer *buffer = get_next_buffer(state->shm,
action->indicator_buffers, buffer_width, buffer_height);
if (buffer == NULL) {
return;
}
cairo_t *cairo = buffer->cairo;
cairo_set_antialias(cairo, 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(surface->subpixel));
cairo_set_font_options(cairo, fo);
cairo_font_options_destroy(fo);
cairo_identity_matrix(cairo);
// Clear
cairo_save(cairo);
cairo_set_source_rgba(cairo, 0, 0, 0, 0);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_paint(cairo);
cairo_restore(cairo);
double relative_xcenter = buffer_width / 2.0f;
double relative_ycenter = fr_common.indicator_diameter / 2.0f;
// Fill inner circle
cairo_set_line_width(cairo, 0);
cairo_arc(cairo, relative_xcenter, relative_ycenter,
fr_common.arc_radius - fr_common.arc_thickness / 2, 0, 2 * M_PI);
set_color_for_state(cairo, selected, &state->args.colors.inside);
cairo_fill_preserve(cairo);
cairo_stroke(cairo);
// Draw ring
cairo_set_line_width(cairo, fr_common.arc_thickness);
cairo_arc(cairo, relative_xcenter, relative_ycenter,
fr_common.arc_radius, 0, 2 * M_PI);
set_color_for_state(cairo, selected, &state->args.colors.ring);
cairo_stroke(cairo);
// Draw symbol and label
set_color_for_state(cairo, selected, &state->args.colors.text);
cairo_text_extents_t extents;
cairo_font_extents_t fe;
double x,y;
y = 0.0f;
bool show_label = state->args.labels || (state->args.selection_label && selected);
if (show_label) {
cairo_set_font_size(cairo, fr_common.label_font_size);
cairo_select_font_face(cairo, state->args.font,
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
char* text = action->label;
cairo_text_extents(cairo, text, &extents);
cairo_font_extents(cairo, &fe);
x = relative_xcenter - (extents.width / 2 + extents.x_bearing);
y = relative_ycenter + (fe.height / 2 - fe.descent);
cairo_move_to(cairo, x, y);
cairo_show_text(cairo, text);
cairo_close_path(cairo);
cairo_new_sub_path(cairo);
if (new_width < extents.width)
new_width = extents.width;
}
char *symbol = action->symbol;
cairo_set_font_size(cairo, selected ?
fr_common.selected_symbol_font_size : fr_common.symbol_font_size);
cairo_select_font_face(
cairo,
state->args.fa_font,
CAIRO_FONT_SLANT_NORMAL,
// CAIRO_FONT_WEIGHT_NORMAL
CAIRO_FONT_WEIGHT_BOLD
);
cairo_text_extents(cairo, symbol, &extents);
cairo_font_extents(cairo, &fe);
x = relative_xcenter - (extents.width / 2 + extents.x_bearing);
y = relative_ycenter;
if (show_label)
y = 3 * y / 5;
y += (fe.height / 2 - fe.descent);
if (show_label)
y += fe.height / 5;
cairo_move_to(cairo, x, y);
cairo_show_text(cairo, symbol);
cairo_close_path(cairo);
cairo_new_sub_path(cairo);
if (new_width < extents.width) {
new_width = extents.width;
}
// Draw inner + outer border of the circle
set_color_for_state(cairo, selected, &state->args.colors.line);
cairo_set_line_width(cairo, fr_common.line_width);
cairo_arc(cairo, relative_xcenter, relative_ycenter,
fr_common.inner_radius, 0, 2 * M_PI);
cairo_stroke(cairo);
cairo_arc(cairo, relative_xcenter, relative_ycenter,
fr_common.outer_radius, 0, 2 * M_PI);
cairo_stroke(cairo);
// Ensure buffer size is multiple of buffer scale - required by protocol
new_height += surface->scale - (new_height % surface->scale);
new_width += surface->scale - (new_width % surface->scale);
if (buffer_width != new_width || buffer_height != new_height) {
destroy_buffer(buffer);
action_surface->indicator_width = new_width;
action_surface->indicator_height = new_height;
render_frames(surface);
return;
}
wl_surface_set_buffer_scale(action_surface->surface, surface->scale);
wl_surface_attach(action_surface->surface, buffer->buffer, 0, 0);
wl_surface_damage_buffer(action_surface->surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(action_surface->surface);
wl_surface_commit(surface->surface);
action->rendered_depressed = will_render_depressed;
}
void render_frames(struct waylogout_surface *surface) {
struct waylogout_state *state = surface->state;
struct waylogout_frame_common fr_common;
fr_common.arc_radius = state->args.radius * surface->scale;
fr_common.arc_thickness = state->args.thickness * surface->scale;
fr_common.line_width = 2.0 * surface->scale;
fr_common.inner_radius = fr_common.arc_radius - fr_common.arc_thickness / 2;
fr_common.outer_radius = fr_common.arc_radius + fr_common.arc_thickness / 2;
fr_common.indicator_diameter = fr_common.arc_radius * 2
+ fr_common.arc_thickness + fr_common.line_width;
int indicator_sep = (state->args.indicator_sep > 0)
? (int) state->args.indicator_sep
: (int) (surface->width * surface->scale - state->longest_row * fr_common.indicator_diameter)
/ (state->longest_row + 1)
;
if (indicator_sep < 0)
indicator_sep = fr_common.arc_thickness;
uint32_t diameter_plus_sep = fr_common.indicator_diameter + indicator_sep;
fr_common.x_offset = diameter_plus_sep / surface->scale;
double y_row_offset_step = diameter_plus_sep / surface->scale;
double y_row_offset = y_row_offset_step * ((int) state->args.rows / 2);
if (state->args.rows % 2 == 0)
y_row_offset -= y_row_offset_step / 2.0f;
fr_common.y_row_offset = y_row_offset;
fr_common.x_center = (state->args.override_indicator_x_position)
? state->args.indicator_x_position
: surface->width / 2;
fr_common.y_center = (state->args.override_indicator_y_position)
? state->args.indicator_y_position
: surface->height / 2;
if (state->args.symbol_font_size > 0)
fr_common.symbol_font_size = state->args.symbol_font_size;
else if (state->args.labels)
if (state->args.label_font_size > 0)
fr_common.symbol_font_size = state->args.label_font_size;
else
fr_common.symbol_font_size = fr_common.arc_radius / 3.0f;
else
fr_common.symbol_font_size = fr_common.arc_radius / 1.5f;
if (state->args.label_font_size > 0)
fr_common.label_font_size = state->args.label_font_size;
else
fr_common.label_font_size = fr_common.arc_radius / 3.0f;
if (state->args.selection_label && !state->args.labels)
fr_common.selected_symbol_font_size = fr_common.label_font_size;
else
fr_common.selected_symbol_font_size = fr_common.symbol_font_size;
struct waylogout_action *action;
fr_common.n_drawn = 0; // per row
uint8_t row_index = 0;
wl_list_for_each(action, &state->actions, link) {
render_frame(action, surface, fr_common);
++fr_common.n_drawn;
if (fr_common.n_drawn == state->rows[row_index]) {
fr_common.y_row_offset -= y_row_offset_step;
fr_common.n_drawn = 0;
++row_index;
}
}
}