| 1 |
#include <poll.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <xcb/randr.h> |
| 6 |
#include <xcb/xcb_event.h> |
| 7 |
|
| 8 |
#include "log.h" |
| 9 |
#include "misc.h" |
| 10 |
#include "ui.h" |
| 11 |
#include "x.h" |
| 12 |
|
| 13 |
struct ui { |
| 14 |
xcb_fontable_t normal_font; |
| 15 |
xcb_fontable_t bold_font; |
| 16 |
}; |
| 17 |
|
| 18 |
static xcb_pixmap_t |
| 19 |
ui_message_box_draw_text(struct ui *ui, struct x_connection *c, xcb_window_t window, |
| 20 |
struct ui_message_box_content *content) { |
| 21 |
xcb_pixmap_t pixmap = x_new_id(c); |
| 22 |
uint16_t width = |
| 23 |
content->size.width > UINT16_MAX ? UINT16_MAX : (uint16_t)content->size.width; |
| 24 |
uint16_t height = |
| 25 |
content->size.height > UINT16_MAX ? UINT16_MAX : (uint16_t)content->size.height; |
| 26 |
if (!XCB_AWAIT_VOID(xcb_create_pixmap, c, c->screen_info->root_depth, pixmap, |
| 27 |
window, width, height)) { |
| 28 |
return XCB_NONE; |
| 29 |
} |
| 30 |
|
| 31 |
xcb_gcontext_t gc = x_new_id(c); |
| 32 |
{ |
| 33 |
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND; |
| 34 |
uint32_t value_list[3] = {c->screen_info->black_pixel, |
| 35 |
c->screen_info->black_pixel}; |
| 36 |
|
| 37 |
if (!XCB_AWAIT_VOID(xcb_create_gc, c, gc, pixmap, mask, value_list)) { |
| 38 |
return XCB_NONE; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
xcb_poly_fill_rectangle( |
| 43 |
c->c, pixmap, gc, 1, |
| 44 |
&(xcb_rectangle_t){.x = 0, .y = 0, .width = width, .height = height}); |
| 45 |
|
| 46 |
const char yellow_name[] = "yellow"; |
| 47 |
const char red_name[] = "red"; |
| 48 |
auto r = XCB_AWAIT(xcb_alloc_named_color, c, c->screen_info->default_colormap, |
| 49 |
ARR_SIZE(yellow_name) - 1, yellow_name); |
| 50 |
if (r == NULL) { |
| 51 |
return XCB_NONE; |
| 52 |
} |
| 53 |
auto yellow_pixel = r->pixel; |
| 54 |
free(r); |
| 55 |
r = XCB_AWAIT(xcb_alloc_named_color, c, c->screen_info->default_colormap, |
| 56 |
ARR_SIZE(red_name) - 1, red_name); |
| 57 |
if (r == NULL) { |
| 58 |
return XCB_NONE; |
| 59 |
} |
| 60 |
auto red_pixel = r->pixel; |
| 61 |
free(r); |
| 62 |
|
| 63 |
for (unsigned i = 0; i < content->num_lines; i++) { |
| 64 |
auto line = &content->lines[i]; |
| 65 |
uint32_t color = 0; |
| 66 |
switch (line->color) { |
| 67 |
case UI_COLOR_WHITE: color = c->screen_info->white_pixel; break; |
| 68 |
case UI_COLOR_YELLOW: color = yellow_pixel; break; |
| 69 |
case UI_COLOR_RED: color = red_pixel; break; |
| 70 |
} |
| 71 |
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_FONT; |
| 72 |
uint32_t value_list[2] = { |
| 73 |
color, line->style == UI_STYLE_BOLD ? ui->bold_font : ui->normal_font}; |
| 74 |
int16_t x = (int16_t)(line->position.x > INT16_MAX ? INT16_MAX |
| 75 |
: line->position.x), |
| 76 |
y = (int16_t)(line->position.y > INT16_MAX ? INT16_MAX |
| 77 |
: line->position.y); |
| 78 |
xcb_change_gc(c->c, gc, mask, value_list); |
| 79 |
xcb_image_text_8(c->c, (uint8_t)strlen(line->text), pixmap, gc, x, y, |
| 80 |
line->text); |
| 81 |
} |
| 82 |
xcb_free_gc(c->c, gc); |
| 83 |
return pixmap; |
| 84 |
} |
| 85 |
|
| 86 |
void ui_message_box_place(struct x_connection *c, struct ui_message_box_content *content, |
| 87 |
int16_t *x, int16_t *y) { |
| 88 |
auto r = XCB_AWAIT(xcb_randr_get_screen_resources_current, c, c->screen_info->root); |
| 89 |
if (r == NULL) { |
| 90 |
return; |
| 91 |
} |
| 92 |
auto pointer = XCB_AWAIT(xcb_query_pointer, c, c->screen_info->root); |
| 93 |
if (pointer == NULL) { |
| 94 |
free(r); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
auto num_crtc = xcb_randr_get_screen_resources_current_crtcs_length(r); |
| 99 |
auto crtcs = xcb_randr_get_screen_resources_current_crtcs(r); |
| 100 |
for (int i = 0; i < num_crtc; i++) { |
| 101 |
auto crtc_info_ptr = |
| 102 |
XCB_AWAIT(xcb_randr_get_crtc_info, c, crtcs[i], r->config_timestamp); |
| 103 |
if (crtc_info_ptr == NULL || |
| 104 |
crtc_info_ptr->status != XCB_RANDR_SET_CONFIG_SUCCESS) { |
| 105 |
free(crtc_info_ptr); |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
auto crtc_info = *crtc_info_ptr; |
| 110 |
free(crtc_info_ptr); |
| 111 |
|
| 112 |
if (content->scale == 0) { |
| 113 |
content->scale = crtc_info.width / 1280.0; |
| 114 |
} |
| 115 |
|
| 116 |
vec2 size = { |
| 117 |
(content->size.width + (int)content->margin * 2) * content->scale, |
| 118 |
(content->size.height + (int)content->margin * 2) * content->scale, |
| 119 |
}; |
| 120 |
|
| 121 |
if (pointer->root_x >= crtc_info.x && |
| 122 |
pointer->root_x < crtc_info.x + crtc_info.width && |
| 123 |
pointer->root_y >= crtc_info.y && |
| 124 |
pointer->root_y < crtc_info.y + crtc_info.height) { |
| 125 |
auto tmp_x = |
| 126 |
crtc_info.x + max2((crtc_info.width - size.width) / 2, 0); |
| 127 |
auto tmp_y = |
| 128 |
crtc_info.y + max2((crtc_info.height - size.height) / 2, 0); |
| 129 |
*x = (int16_t)clamp(tmp_x, INT16_MIN, INT16_MAX); |
| 130 |
*y = (int16_t)clamp(tmp_y, INT16_MIN, INT16_MAX); |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
free(pointer); |
| 135 |
free(r); |
| 136 |
} |
| 137 |
const int64_t FPS = 60; |
| 138 |
bool ui_message_box_show(struct ui *ui, struct x_connection *c, |
| 139 |
struct ui_message_box_content *content, unsigned timeout) { |
| 140 |
struct timespec next_render; |
| 141 |
if (clock_gettime(CLOCK_MONOTONIC, &next_render) < 0) { |
| 142 |
log_error("Failed to get current time"); |
| 143 |
return false; |
| 144 |
} |
| 145 |
struct timespec close_time = next_render; |
| 146 |
close_time.tv_sec += (long)timeout; |
| 147 |
|
| 148 |
int16_t x = 0, y = 0; |
| 149 |
ui_message_box_place(c, content, &x, &y); |
| 150 |
|
| 151 |
ivec2 size = ivec2_add( |
| 152 |
content->size, (ivec2){(int)content->margin * 2, (int)content->margin * 2}); |
| 153 |
size.width = (int)(size.width * content->scale); |
| 154 |
size.height = (int)(size.height * content->scale); |
| 155 |
|
| 156 |
xcb_window_t win = x_new_id(c); |
| 157 |
uint16_t width = to_u16_saturated(size.width), |
| 158 |
height = to_u16_saturated(size.height), |
| 159 |
inner_width = to_u16_saturated(content->size.width * content->scale), |
| 160 |
inner_height = to_u16_saturated(content->size.height * content->scale); |
| 161 |
int16_t margin = to_i16_checked(content->margin * content->scale); |
| 162 |
|
| 163 |
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK; |
| 164 |
uint32_t values[3] = {c->screen_info->black_pixel, 1, |
| 165 |
XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_BUTTON_PRESS | |
| 166 |
XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION | |
| 167 |
XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW}; |
| 168 |
bool success = XCB_AWAIT_VOID(xcb_create_window, c, c->screen_info->root_depth, |
| 169 |
win, c->screen_info->root, x, y, width, height, |
| 170 |
0, XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 171 |
c->screen_info->root_visual, mask, values); |
| 172 |
if (!success) { |
| 173 |
return success; |
| 174 |
} |
| 175 |
auto rendered_content = ui_message_box_draw_text(ui, c, win, content); |
| 176 |
xcb_render_picture_t content_picture = x_create_picture_with_visual_and_pixmap( |
| 177 |
c, c->screen_info->root_visual, rendered_content, 0, NULL); |
| 178 |
xcb_render_picture_t target_picture = x_create_picture_with_visual_and_pixmap( |
| 179 |
c, c->screen_info->root_visual, win, 0, NULL); |
| 180 |
if (content_picture == XCB_NONE || target_picture == XCB_NONE) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
xcb_render_transform_t transform = { |
| 185 |
.matrix11 = DOUBLE_TO_XFIXED(1.0F / content->scale), |
| 186 |
.matrix22 = DOUBLE_TO_XFIXED(1.0F / content->scale), |
| 187 |
.matrix33 = DOUBLE_TO_XFIXED(1.0), |
| 188 |
}; |
| 189 |
if (!XCB_AWAIT_VOID(xcb_render_set_picture_transform, c, content_picture, transform)) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
const char filter_name[] = "bilinear"; |
| 194 |
if (!XCB_AWAIT_VOID(xcb_render_set_picture_filter, c, content_picture, |
| 195 |
ARR_SIZE(filter_name) - 1, filter_name, 0, NULL)) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
if (!XCB_AWAIT_VOID(xcb_map_window, c, win)) { |
| 200 |
xcb_destroy_window(c->c, win); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
xcb_generic_event_t *event; |
| 205 |
bool quit = false; |
| 206 |
while (!quit) { |
| 207 |
struct timespec now; |
| 208 |
clock_gettime(CLOCK_MONOTONIC, &now); |
| 209 |
|
| 210 |
int wait_time = 0; |
| 211 |
double remaining = 0; |
| 212 |
bool should_render = false; |
| 213 |
if (timespec_cmp(now, next_render) < 0) { |
| 214 |
wait_time = (int)((next_render.tv_sec - now.tv_sec) * 1000 + |
| 215 |
(next_render.tv_nsec - now.tv_nsec) / 1000000); |
| 216 |
} |
| 217 |
struct pollfd fds = {.fd = xcb_get_file_descriptor(c->c), .events = POLLIN}; |
| 218 |
poll(&fds, 1, wait_time); |
| 219 |
clock_gettime(CLOCK_MONOTONIC, &now); |
| 220 |
if (timespec_cmp(next_render, now) <= 0) { |
| 221 |
should_render = true; |
| 222 |
} |
| 223 |
if (timespec_cmp(close_time, now) <= 0) { |
| 224 |
quit = true; |
| 225 |
} else { |
| 226 |
remaining = (double)(close_time.tv_sec - now.tv_sec) + |
| 227 |
(double)(close_time.tv_nsec - now.tv_nsec) / 1000000000; |
| 228 |
} |
| 229 |
while ((event = xcb_poll_for_event(c->c)) != NULL) { |
| 230 |
switch (XCB_EVENT_RESPONSE_TYPE(event)) { |
| 231 |
case XCB_EXPOSE: |
| 232 |
xcb_render_fill_rectangles( |
| 233 |
c->c, XCB_RENDER_PICT_OP_SRC, target_picture, |
| 234 |
(xcb_render_color_t){.alpha = 0xffff}, 1, |
| 235 |
(const xcb_rectangle_t[]){ |
| 236 |
{.x = 0, .y = 0, .width = width, .height = height}}); |
| 237 |
xcb_render_composite(c->c, XCB_RENDER_PICT_OP_SRC, |
| 238 |
content_picture, XCB_NONE, |
| 239 |
target_picture, 0, 0, 0, 0, margin, |
| 240 |
margin, inner_width, inner_height); |
| 241 |
should_render = true; |
| 242 |
break; |
| 243 |
case XCB_KEY_RELEASE:; |
| 244 |
xcb_key_release_event_t *kr = (xcb_key_release_event_t *)event; |
| 245 |
|
| 246 |
switch (kr->detail) { |
| 247 |
case 9: quit = true; break; |
| 248 |
} |
| 249 |
break; |
| 250 |
case XCB_ENTER_NOTIFY: |
| 251 |
xcb_grab_keyboard(c->c, 0, win, XCB_CURRENT_TIME, |
| 252 |
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); |
| 253 |
break; |
| 254 |
case XCB_LEAVE_NOTIFY: |
| 255 |
xcb_ungrab_keyboard(c->c, XCB_CURRENT_TIME); |
| 256 |
break; |
| 257 |
} |
| 258 |
free(event); |
| 259 |
} |
| 260 |
if (should_render) { |
| 261 |
const uint16_t bar_height = (uint16_t)(10 * content->scale); |
| 262 |
const uint16_t bar_width = |
| 263 |
to_u16_saturated(width * remaining / timeout); |
| 264 |
next_render.tv_sec = now.tv_sec; |
| 265 |
next_render.tv_nsec = now.tv_nsec + 1000000000 / FPS; |
| 266 |
xcb_render_fill_rectangles( |
| 267 |
c->c, XCB_RENDER_PICT_OP_SRC, target_picture, |
| 268 |
(xcb_render_color_t){}, 1, |
| 269 |
(const xcb_rectangle_t[]){ |
| 270 |
{.x = 0, |
| 271 |
.y = (int16_t)clamp(height - bar_height, 0, INT16_MAX), |
| 272 |
.width = width, |
| 273 |
.height = bar_height}}); |
| 274 |
xcb_render_fill_rectangles( |
| 275 |
c->c, XCB_RENDER_PICT_OP_SRC, target_picture, |
| 276 |
(xcb_render_color_t){ |
| 277 |
.alpha = 0xffff, .red = 0xffff, .green = 0xffff, .blue = 0xffff}, |
| 278 |
1, |
| 279 |
(const xcb_rectangle_t[]){ |
| 280 |
{.x = (int16_t)((width - bar_width) / 2), |
| 281 |
.y = (int16_t)clamp(height - bar_height, 0, INT16_MAX), |
| 282 |
.width = bar_width, |
| 283 |
.height = bar_height}}); |
| 284 |
if (next_render.tv_nsec >= 1000000000) { |
| 285 |
next_render.tv_sec++; |
| 286 |
next_render.tv_nsec -= 1000000000; |
| 287 |
} |
| 288 |
} |
| 289 |
xcb_flush(c->c); |
| 290 |
} |
| 291 |
return true; |
| 292 |
} |
| 293 |
|
| 294 |
static bool ui_message_box_line_extent(struct ui *ui, struct x_connection *c, |
| 295 |
struct ui_message_box_line *line) { |
| 296 |
auto len = (uint32_t)strlen(line->text); |
| 297 |
if (len > UINT8_MAX) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
auto text16 = ccalloc(len, xcb_char2b_t); |
| 301 |
auto font = line->style == UI_STYLE_BOLD ? ui->bold_font : ui->normal_font; |
| 302 |
for (uint32_t i = 0; i < len; i++) { |
| 303 |
text16[i].byte1 = 0; |
| 304 |
text16[i].byte2 = (uint8_t)line->text[i]; |
| 305 |
} |
| 306 |
auto r = XCB_AWAIT(xcb_query_text_extents, c, font, len, text16); |
| 307 |
free(text16); |
| 308 |
|
| 309 |
if (!r) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
line->size.width = r->overall_width; |
| 313 |
line->size.height = r->font_ascent + r->font_descent; |
| 314 |
line->position.y = r->font_ascent; |
| 315 |
free(r); |
| 316 |
return true; |
| 317 |
} |
| 318 |
|
| 319 |
bool ui_message_box_content_plan(struct ui *ui, struct x_connection *c, |
| 320 |
struct ui_message_box_content *content) { |
| 321 |
if (content->margin > INT_MAX) { |
| 322 |
log_error("Margin is too large"); |
| 323 |
return false; |
| 324 |
} |
| 325 |
ivec2 size = {}; |
| 326 |
for (unsigned i = 0; i < content->num_lines; i++) { |
| 327 |
if (!ui_message_box_line_extent(ui, c, &content->lines[i])) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
content->lines[i].position.y += size.height; |
| 331 |
size.height += |
| 332 |
content->lines[i].size.height + (int)content->lines[i].pad_bottom; |
| 333 |
if (content->lines[i].size.width > size.width) { |
| 334 |
size.width = content->lines[i].size.width; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
content->size = size; |
| 339 |
|
| 340 |
for (unsigned i = 0; i < content->num_lines; i++) { |
| 341 |
switch (content->lines[i].justify) { |
| 342 |
case UI_JUSTIFY_LEFT: content->lines[i].position.x = 0; break; |
| 343 |
case UI_JUSTIFY_CENTER: |
| 344 |
content->lines[i].position.x = |
| 345 |
(size.width - content->lines[i].size.width) / 2; |
| 346 |
break; |
| 347 |
case UI_JUSTIFY_RIGHT: |
| 348 |
content->lines[i].position.x = |
| 349 |
size.width - content->lines[i].size.width; |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
return true; |
| 354 |
} |
| 355 |
|
| 356 |
struct ui *ui_new(struct x_connection *c) { |
| 357 |
const char normal_font[] = "fixed"; |
| 358 |
const char bold_font[] = "-*-fixed-bold-*"; |
| 359 |
auto ui = ccalloc(1, struct ui); |
| 360 |
ui->normal_font = x_new_id(c); |
| 361 |
ui->bold_font = x_new_id(c); |
| 362 |
auto cookie1 = xcb_open_font_checked(c->c, ui->normal_font, |
| 363 |
ARR_SIZE(normal_font) - 1, normal_font); |
| 364 |
auto cookie2 = |
| 365 |
xcb_open_font_checked(c->c, ui->bold_font, ARR_SIZE(bold_font) - 1, bold_font); |
| 366 |
|
| 367 |
xcb_generic_error_t *e = xcb_request_check(c->c, cookie1); |
| 368 |
if (e != NULL) { |
| 369 |
log_error_x_error(c, e, "Cannot open the fixed font"); |
| 370 |
free(e); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
e = xcb_request_check(c->c, cookie2); |
| 374 |
if (e != NULL) { |
| 375 |
ui->bold_font = ui->normal_font; |
| 376 |
log_error_x_error(c, e, |
| 377 |
"Cannot open the bold font, falling back to normal " |
| 378 |
"font"); |
| 379 |
free(e); |
| 380 |
} |
| 381 |
return ui; |
| 382 |
} |
| 383 |
|
| 384 |
void ui_destroy(struct ui *ui, struct x_connection *c) { |
| 385 |
xcb_close_font(c->c, ui->normal_font); |
| 386 |
if (ui->bold_font != ui->normal_font) { |
| 387 |
xcb_close_font(c->c, ui->bold_font); |
| 388 |
} |
| 389 |
xcb_flush(c->c); |
| 390 |
} |