migrate placeholder windows to draw_util (#2646)

This commit is contained in:
mihaicmn 2017-01-21 17:53:09 +02:00 committed by Michael Stapelberg
parent c78afab5f8
commit 564945bc14
1 changed files with 20 additions and 38 deletions

View File

@ -15,6 +15,8 @@
#include <sanitizer/lsan_interface.h> #include <sanitizer/lsan_interface.h>
#endif #endif
#define TEXT_PADDING logical_px(2)
typedef struct placeholder_state { typedef struct placeholder_state {
/** The X11 placeholder window. */ /** The X11 placeholder window. */
xcb_window_t window; xcb_window_t window;
@ -24,10 +26,8 @@ typedef struct placeholder_state {
/** Current size of the placeholder window (to detect size changes). */ /** Current size of the placeholder window (to detect size changes). */
Rect rect; Rect rect;
/** The pixmap to render on (back buffer). */ /** The drawable surface */
xcb_pixmap_t pixmap; surface_t surface;
/** The graphics context for “pixmap”. */
xcb_gcontext_t gc;
TAILQ_ENTRY(placeholder_state) TAILQ_ENTRY(placeholder_state)
state; state;
@ -138,17 +138,15 @@ void restore_connect(void) {
} }
static void update_placeholder_contents(placeholder_state *state) { static void update_placeholder_contents(placeholder_state *state) {
xcb_change_gc(restore_conn, state->gc, XCB_GC_FOREGROUND, const color_t foreground = config.client.placeholder.text;
(uint32_t[]){config.client.placeholder.background.colorpixel}); const color_t background = config.client.placeholder.background;
xcb_poly_fill_rectangle(restore_conn, state->pixmap, state->gc, 1,
(xcb_rectangle_t[]){{0, 0, state->rect.width, state->rect.height}}); draw_util_clear_surface(&(state->surface), background);
// TODO: make i3font functions per-connection, at least these two for now…? // TODO: make i3font functions per-connection, at least these two for now…?
xcb_flush(restore_conn); xcb_flush(restore_conn);
xcb_aux_sync(restore_conn); xcb_aux_sync(restore_conn);
set_font_colors(state->gc, config.client.placeholder.text, config.client.placeholder.background);
Match *swallows; Match *swallows;
int n = 0; int n = 0;
TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) { TAILQ_FOREACH(swallows, &(state->con->swallow_head), matches) {
@ -175,7 +173,10 @@ static void update_placeholder_contents(placeholder_state *state) {
DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized); DLOG("con %p (placeholder 0x%08x) line %d: %s\n", state->con, state->window, n, serialized);
i3String *str = i3string_from_utf8(serialized); i3String *str = i3string_from_utf8(serialized);
draw_text(str, state->pixmap, state->gc, NULL, 2, (n * (config.font.height + 2)) + 2, state->rect.width - 2); draw_util_text(str, &(state->surface), foreground, background,
TEXT_PADDING,
(n * (config.font.height + TEXT_PADDING)) + TEXT_PADDING,
state->rect.width - 2 * TEXT_PADDING);
i3string_free(str); i3string_free(str);
n++; n++;
free(serialized); free(serialized);
@ -186,7 +187,7 @@ static void update_placeholder_contents(placeholder_state *state) {
int text_width = predict_text_width(line); int text_width = predict_text_width(line);
int x = (state->rect.width / 2) - (text_width / 2); int x = (state->rect.width / 2) - (text_width / 2);
int y = (state->rect.height / 2) - (config.font.height / 2); int y = (state->rect.height / 2) - (config.font.height / 2);
draw_text(line, state->pixmap, state->gc, NULL, x, y, text_width); draw_util_text(line, &(state->surface), foreground, background, x, y, text_width);
i3string_free(line); i3string_free(line);
xcb_flush(conn); xcb_flush(conn);
xcb_aux_sync(conn); xcb_aux_sync(conn);
@ -228,11 +229,8 @@ static void open_placeholder_window(Con *con) {
state->window = placeholder; state->window = placeholder;
state->con = con; state->con = con;
state->rect = con->rect; state->rect = con->rect;
state->pixmap = xcb_generate_id(restore_conn);
xcb_create_pixmap(restore_conn, root_depth, state->pixmap, draw_util_surface_init(conn, &(state->surface), placeholder, get_visualtype(root_screen), state->rect.width, state->rect.height);
state->window, state->rect.width, state->rect.height);
state->gc = xcb_generate_id(restore_conn);
xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
update_placeholder_contents(state); update_placeholder_contents(state);
TAILQ_INSERT_TAIL(&state_head, state, state); TAILQ_INSERT_TAIL(&state_head, state, state);
@ -286,8 +284,7 @@ bool restore_kill_placeholder(xcb_window_t placeholder) {
continue; continue;
xcb_destroy_window(restore_conn, state->window); xcb_destroy_window(restore_conn, state->window);
xcb_free_pixmap(restore_conn, state->pixmap); draw_util_surface_free(restore_conn, &(state->surface));
xcb_free_gc(restore_conn, state->gc);
TAILQ_REMOVE(&state_head, state, state); TAILQ_REMOVE(&state_head, state, state);
free(state); free(state);
DLOG("placeholder window 0x%08x destroyed.\n", placeholder); DLOG("placeholder window 0x%08x destroyed.\n", placeholder);
@ -306,14 +303,8 @@ static void expose_event(xcb_expose_event_t *event) {
DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con); DLOG("refreshing window 0x%08x contents (con %p)\n", state->window, state->con);
/* Since we render to our pixmap on every change anyways, expose events update_placeholder_contents(state);
* only tell us that the X server lost (parts of) the window contents. We
* can handle that by copying the appropriate part from our pixmap to the
* window. */
xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
event->x, event->y, event->x, event->y,
event->width, event->height);
xcb_flush(restore_conn);
return; return;
} }
@ -338,19 +329,10 @@ static void configure_notify(xcb_configure_notify_event_t *event) {
state->rect.width = event->width; state->rect.width = event->width;
state->rect.height = event->height; state->rect.height = event->height;
xcb_free_pixmap(restore_conn, state->pixmap); draw_util_surface_set_size(&(state->surface), state->rect.width, state->rect.height);
xcb_free_gc(restore_conn, state->gc);
state->pixmap = xcb_generate_id(restore_conn);
xcb_create_pixmap(restore_conn, root_depth, state->pixmap,
state->window, state->rect.width, state->rect.height);
state->gc = xcb_generate_id(restore_conn);
xcb_create_gc(restore_conn, state->gc, state->pixmap, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
update_placeholder_contents(state); update_placeholder_contents(state);
xcb_copy_area(restore_conn, state->pixmap, state->window, state->gc,
0, 0, 0, 0, state->rect.width, state->rect.height);
xcb_flush(restore_conn);
return; return;
} }