-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpopup_renderer.c
More file actions
310 lines (262 loc) · 8.76 KB
/
popup_renderer.c
File metadata and controls
310 lines (262 loc) · 8.76 KB
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
#ifdef ENABLE_POPUP
#include <cairo.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <pango/pango-fontmap.h>
#include <pango/pangocairo.h>
#include "input-method-unstable-v2-client-protocol.h"
#include "wlpinyin.h"
#define ITEM_SPACING 8
#define ROW_SPACING 4
#define CORNER_RADIUS 4
#define MAX_BACK_ROWS 2
#define MAX_FWD_ROWS 5
static int DEFAULT_SHM_SIZE = 4096;
static void draw_rounded_rectangle(cairo_t *cr,
double x,
double y,
double width,
double height,
double radius) {
cairo_move_to(cr, x + radius, y);
cairo_line_to(cr, x + width - radius, y);
cairo_curve_to(cr, x + width, y, x + width, y, x + width, y + radius);
cairo_line_to(cr, x + width, y + height - radius);
cairo_curve_to(cr, x + width, y + height, x + width, y + height,
x + width - radius, y + height);
cairo_line_to(cr, x + radius, y + height);
cairo_curve_to(cr, x, y + height, x, y + height, x, y + height - radius);
cairo_line_to(cr, x, y + radius);
cairo_curve_to(cr, x, y, x, y, x + radius, y);
}
static void popup_handle_frame_done(void *data,
struct wl_callback *cb,
uint32_t serial) {
UNUSED(serial);
wl_callback_destroy(cb);
struct wlpinyin_state *state = data;
state->frame_callback_done = true;
if (state->pending_render)
im_panel_update(state);
}
int im_panel_update(struct wlpinyin_state *state) {
char buf[256];
int bufptr = 0;
im_preedit_t preedit = im_engine_preedit(state->engine);
zwp_input_method_v2_set_preedit_string(state->input_method, preedit.text,
preedit.begin, preedit.end);
im_context_t ctx = im_engine_context(state->engine);
/* Empty, show nothing */
if (ctx.page_size == 0) {
wl_surface_attach(state->popup_surface, NULL, 0, 0);
wl_surface_commit(state->popup_surface);
state->pending_render = false;
return 0;
}
/* If not ready, just return */
if (!state->frame_callback_done) {
state->pending_render = true;
return 0;
}
/* Setup new frame callback */
state->frame_callback_done = false;
struct wl_callback *cb = wl_surface_frame(state->popup_surface);
static const struct wl_callback_listener frame_listener = {
.done = popup_handle_frame_done,
};
wl_callback_add_listener(cb, &frame_listener, state);
int start_row, end_row;
if (ctx.page_no == 0) {
start_row = 0;
end_row = 1;
} else {
start_row = MAX(0, ctx.page_no - MAX_BACK_ROWS);
end_row = start_row + MAX_FWD_ROWS;
}
int start_idx = start_row * ctx.page_size;
/* Measure column widths */
int row_width[50] = {0};
int row_height = 0;
int real_end_row = 0;
im_engine_cand_begin(state->engine, start_idx);
int i;
for (i = start_idx; im_engine_cand_next(state->engine); i++) {
int row = i / ctx.page_size;
int col = i % ctx.page_size;
if (row >= end_row) {
i--;
break;
}
const char *text = im_engine_cand_get(state->engine);
bufptr =
snprintf(&buf[bufptr], sizeof(buf) - bufptr, "%d %s", col + 1, text);
pango_layout_set_text(state->popup_pango_layout, buf, bufptr);
PangoRectangle text_rect;
pango_layout_get_pixel_extents(state->popup_pango_layout, NULL, &text_rect);
int item_width = text_rect.width + ITEM_SPACING * 2;
row_width[col] = MAX(row_width[col], item_width);
row_height = MAX(row_height, text_rect.height + ROW_SPACING * 2);
}
int row = i / ctx.page_size;
int col = i % ctx.page_size;
if (col == 0 && !im_engine_cand_next(state->engine))
real_end_row = row;
else
real_end_row = row + 1;
im_engine_cand_end(state->engine);
/* Calculate panel size */
int panel_width = 0;
for (int i = 0; i < ctx.page_size; i++)
panel_width += row_width[i];
int panel_height = row_height * MAX(1, real_end_row - start_row);
/* Resize buffer if needed */
int panel_stride =
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, panel_width);
int buffer_newsz = panel_stride * panel_height;
if (state->shm_size < buffer_newsz) {
if (state->popup_data) {
munmap(state->popup_data, state->shm_size);
state->popup_data = NULL;
}
if (ftruncate(state->shm_pool_fd, buffer_newsz) < 0) {
wlpinyin_err("fail to resize shm: %s", strerror(errno));
return -1;
}
wl_shm_pool_resize(state->shm_pool, buffer_newsz);
state->popup_data = mmap(NULL, buffer_newsz, PROT_READ | PROT_WRITE,
MAP_SHARED, state->shm_pool_fd, 0);
if (state->popup_data == MAP_FAILED) {
wlpinyin_err("mmap failed: %s", strerror(errno));
state->popup_data = NULL;
return -1;
}
state->shm_size = buffer_newsz;
}
/* Recreate buffer */
if (state->shm_buffer)
wl_buffer_destroy(state->shm_buffer);
state->shm_buffer =
wl_shm_pool_create_buffer(state->shm_pool, 0, panel_width, panel_height,
panel_stride, WL_SHM_FORMAT_ARGB8888);
/* Create Cairo surface */
cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data(
state->popup_data, CAIRO_FORMAT_ARGB32, panel_width, panel_height,
panel_stride);
cairo_t *cr = cairo_create(cairo_surface);
/* Clear */
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_paint(cr);
/* Draw background */
draw_rounded_rectangle(cr, 0, 0, panel_width, panel_height, CORNER_RADIUS);
cairo_set_source_rgba(cr, 0.25, 0.25, 0.27, 0.95);
cairo_fill(cr);
/* Draw candidates in grid layout */
im_engine_cand_begin(state->engine, start_idx);
for (int i = start_idx; im_engine_cand_next(state->engine); i++) {
const char *text = im_engine_cand_get(state->engine);
int row = i / ctx.page_size;
int col = i % ctx.page_size;
if (row > real_end_row)
break;
int x = 0;
for (int c = 0; c < col; c++)
x += row_width[c];
int y = (row - start_row) * row_height;
bufptr = 0;
bufptr = snprintf(&buf[bufptr], sizeof(buf) - bufptr, "%d ", col + 1);
if (row != ctx.page_no)
for (int i = 0; i < bufptr; i++)
buf[i] = ' ';
bufptr += snprintf(&buf[bufptr], sizeof(buf) - bufptr, "%s", text);
pango_layout_set_text(state->popup_pango_layout, buf, bufptr);
PangoRectangle text_rect;
pango_layout_get_pixel_extents(state->popup_pango_layout, NULL, &text_rect);
/* Draw highlight background */
if (row == ctx.page_no && col == ctx.highlighted_index) {
draw_rounded_rectangle(cr, x, y, row_width[col], row_height, 0);
cairo_set_source_rgba(cr, 0.3, 0.5, 0.8, 1.0);
cairo_fill(cr);
}
cairo_set_source_rgba(cr, 0.95, 0.95, 0.95, 1.0);
cairo_move_to(cr, x + ITEM_SPACING, y + ROW_SPACING);
pango_cairo_show_layout(cr, state->popup_pango_layout);
}
im_engine_cand_end(state->engine);
cairo_destroy(cr);
cairo_surface_destroy(cairo_surface);
/* Commit to wayland */
wl_surface_attach(state->popup_surface, state->shm_buffer, 0, 0);
wl_surface_damage(state->popup_surface, 0, 0, panel_width, panel_height);
wl_surface_commit(state->popup_surface);
state->pending_render = false;
return 0;
}
int im_panel_init(struct wlpinyin_state *state) {
if (!state->wl_shm) {
wlpinyin_err("wl_shm not available");
return -1;
}
state->popup_surface = wl_compositor_create_surface(state->compositor);
if (!state->popup_surface) {
wlpinyin_err("failed to create popup surface");
return -1;
}
state->popup_surface_v2 = zwp_input_method_v2_get_input_popup_surface(
state->input_method, state->popup_surface);
state->shm_pool_fd = memfd_create("wlpinyin", 0);
if (state->shm_pool_fd < 0) {
wlpinyin_err("fail to create shm: %s", strerror(errno));
return -1;
}
if (ftruncate(state->shm_pool_fd, DEFAULT_SHM_SIZE) < 0) {
wlpinyin_err("fail to init shm buffer: %s", strerror(errno));
close(state->shm_pool_fd);
return -1;
}
state->shm_pool =
wl_shm_create_pool(state->wl_shm, state->shm_pool_fd, DEFAULT_SHM_SIZE);
state->popup_pango_ctx =
pango_font_map_create_context(pango_cairo_font_map_get_default());
state->popup_pango_layout = pango_layout_new(state->popup_pango_ctx);
state->frame_callback_done = true;
state->pending_render = true;
return 0;
}
void im_panel_destroy(struct wlpinyin_state *state) {
if (state->shm_buffer) {
wl_buffer_destroy(state->shm_buffer);
state->shm_buffer = NULL;
}
if (state->popup_pango_layout) {
g_object_unref(state->popup_pango_layout);
state->popup_pango_layout = NULL;
}
if (state->popup_pango_ctx) {
g_object_unref(state->popup_pango_ctx);
state->popup_pango_ctx = NULL;
}
if (state->popup_data) {
munmap(state->popup_data, state->shm_size);
state->popup_data = NULL;
}
if (state->shm_pool) {
wl_shm_pool_destroy(state->shm_pool);
state->shm_pool = NULL;
}
if (state->shm_pool_fd >= 0) {
close(state->shm_pool_fd);
state->shm_pool_fd = -1;
}
if (state->popup_surface_v2) {
zwp_input_popup_surface_v2_destroy(state->popup_surface_v2);
state->popup_surface_v2 = NULL;
}
if (state->popup_surface) {
wl_surface_destroy(state->popup_surface);
state->popup_surface = NULL;
}
}
#endif /* ENABLE_POPUP */