Skip to content

Commit

Permalink
More cleanup (i3#3479)
Browse files Browse the repository at this point in the history
  • Loading branch information
buchankn committed Feb 23, 2023
1 parent 36541af commit f252e68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
5 changes: 1 addition & 4 deletions include/sparse_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ typedef struct sparse_surface_t {
/* The drawables which are being represented. */
surface_t side_surface[SPARSE_SURFACE_SIDE_COUNT];

/* Whether the pixmap for this side has been allocated */
bool side_pixmap_allocated[SPARSE_SURFACE_SIDE_COUNT];

/* x and y offset of each drawable rectangle */
int x_offset[SPARSE_SURFACE_SIDE_COUNT];
int y_offset[SPARSE_SURFACE_SIDE_COUNT];
Expand Down Expand Up @@ -68,7 +65,7 @@ void sparse_surface_create_and_init(xcb_connection_t *conn, sparse_surface_t *sp
* Destroys the sparse surface.
*
*/
void sparse_surface_free(xcb_connection_t *conn, sparse_surface_t *sparse_surface, bool clear_id);
void sparse_surface_free(xcb_connection_t *conn, sparse_surface_t *sparse_surface);

/**
* Clears a sparse surface with the given color.
Expand Down
39 changes: 17 additions & 22 deletions src/sparse_surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ extern xcb_visualtype_t *visual_type;
void sparse_surface_free_one_side(xcb_connection_t *conn, sparse_surface_t *sparse_surface, sparse_surface_side side, bool clear_id);

bool sparse_surface_initialized(sparse_surface_t *sparse_surface) {
if (!sparse_surface->side_pixmap_allocated[SPARSE_SURFACE_TOP] &&
!sparse_surface->side_pixmap_allocated[SPARSE_SURFACE_BOTTOM] &&
!sparse_surface->side_pixmap_allocated[SPARSE_SURFACE_LEFT] &&
!sparse_surface->side_pixmap_allocated[SPARSE_SURFACE_RIGHT]) {
if (sparse_surface->side_surface[SPARSE_SURFACE_TOP].id == XCB_NONE &&
sparse_surface->side_surface[SPARSE_SURFACE_BOTTOM].id == XCB_NONE &&
sparse_surface->side_surface[SPARSE_SURFACE_LEFT].id == XCB_NONE &&
sparse_surface->side_surface[SPARSE_SURFACE_RIGHT].id == XCB_NONE ) {
ELOG("Surface %p is not initialized, skipping drawing.\n", sparse_surface);
return false;
}
Expand Down Expand Up @@ -114,17 +114,15 @@ void sparse_surface_create_and_init(xcb_connection_t *conn, sparse_surface_t *sp

for(i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if(sparse_surface->side_surface[i].width == 0 || sparse_surface->side_surface[i].height == 0) {
if (sparse_surface->side_pixmap_allocated[i]) {
if (sparse_surface->side_surface[i].id != XCB_NONE) {
sparse_surface_free_one_side(conn, sparse_surface, i, true);
}
} else {
/* Generate an id if the surface doesn't exist, or remove the surface if it already exists */
if (sparse_surface->side_surface[i].id == XCB_NONE) {
sparse_surface->side_surface[i].id = xcb_generate_id(conn);
}

if (sparse_surface->side_pixmap_allocated[i]) {
if (sparse_surface->side_surface[i].id != XCB_NONE) {
sparse_surface_free_one_side(conn, sparse_surface, i, false);
} else {
sparse_surface->side_surface[i].id = xcb_generate_id(conn);
}

/* create the pixmap for this side */
Expand Down Expand Up @@ -155,8 +153,6 @@ void sparse_surface_create_and_init(xcb_connection_t *conn, sparse_surface_t *sp
cairo_translate(sparse_surface->side_surface[i].cr,
-(sparse_surface->x_offset[i]),
-(sparse_surface->y_offset[i]));

sparse_surface->side_pixmap_allocated[i] = true;
}
}
}
Expand All @@ -166,9 +162,9 @@ void sparse_surface_create_and_init(xcb_connection_t *conn, sparse_surface_t *sp
* Destroys the sparse surface.
*
*/
void sparse_surface_free(xcb_connection_t *conn, sparse_surface_t *sparse_surface, bool clear_id) {
void sparse_surface_free(xcb_connection_t *conn, sparse_surface_t *sparse_surface) {
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
sparse_surface_free_one_side(conn, sparse_surface, i, clear_id);
sparse_surface_free_one_side(conn, sparse_surface, i, true);
}
}

Expand All @@ -177,14 +173,13 @@ void sparse_surface_free(xcb_connection_t *conn, sparse_surface_t *sparse_surfac
*
*/
void sparse_surface_free_one_side(xcb_connection_t *conn, sparse_surface_t *sparse_surface, sparse_surface_side side, bool clear_id) {
if (sparse_surface->side_pixmap_allocated[side]) {
if (sparse_surface->side_surface[side].id != XCB_NONE) {
surface_free(conn,
&(sparse_surface->side_surface[side].cr),
sparse_surface->side_surface[side].gc,
&(sparse_surface->side_surface[side].surface));

xcb_free_pixmap(conn, sparse_surface->side_surface[side].id);
sparse_surface->side_pixmap_allocated[side] = false;

if(clear_id) {
sparse_surface->side_surface[side].id = XCB_NONE;
Expand All @@ -198,7 +193,7 @@ void sparse_surface_free_one_side(xcb_connection_t *conn, sparse_surface_t *spar
*/
void sparse_surface_clear(sparse_surface_t *sparse_surface, color_t color) {
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if (sparse_surface->side_pixmap_allocated[i]) {
if (sparse_surface->side_surface[i].id != XCB_NONE) {
draw_util_clear_surface(&(sparse_surface->side_surface[i]), color);
}
}
Expand All @@ -219,7 +214,7 @@ void sparse_surface_copy(sparse_surface_t *src_sparse, surface_t *dest, double s
double src_y_adjusted;

for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if (src_sparse->side_pixmap_allocated[i]) {
if (src_sparse->side_surface[i].id != XCB_NONE) {
cairo_save(dest->cr);

/* src_x and src_y are relative to the entire window+border area.
Expand Down Expand Up @@ -257,7 +252,7 @@ void sparse_surface_text(i3String *text, sparse_surface_t *surface, color_t fg_c
/* Draw to each of the four border pixmaps. This ensures that any draws that cross pixmap boundaries are
* drawn correctly. */
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if(surface->side_pixmap_allocated[i]) {
if(surface->side_surface[i].id != XCB_NONE) {
draw_util_text(text, &(surface->side_surface[i]), fg_color, bg_color, x, y, max_width);
}
}
Expand All @@ -271,7 +266,7 @@ void sparse_surface_rectangle(sparse_surface_t *surface, color_t color, double x
/* Draw to each of the four border pixmaps. This ensures that any draws that cross pixmap boundaries are
* drawn correctly. */
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if(surface->side_pixmap_allocated[i]) {
if(surface->side_surface[i].id != XCB_NONE) {
draw_util_rectangle(&(surface->side_surface[i]), color, x, y, w, h);
}
}
Expand All @@ -287,7 +282,7 @@ void sparse_surface_image(cairo_surface_t *image, sparse_surface_t *surface, int
/* Draw to each of the four border pixmaps. This ensures that any draws that cross pixmap boundaries are
* drawn correctly. */
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if(surface->side_pixmap_allocated[i]) {
if(surface->side_surface[i].id != XCB_NONE) {
draw_util_image(image, &(surface->side_surface[i]), x, y, width, height);
}
}
Expand All @@ -298,7 +293,7 @@ void sparse_surface_image(cairo_surface_t *image, sparse_surface_t *surface, int
*/
void sparse_surface_disable_graphics_exposure_events(xcb_connection_t *conn, sparse_surface_t *surface) {
for(int i = 0; i < SPARSE_SURFACE_SIDE_COUNT; i++) {
if(surface->side_pixmap_allocated[i]) {
if(surface->side_surface[i].id != XCB_NONE) {
xcb_change_gc(conn, surface->side_surface[i].gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/x.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static void _x_con_kill(Con *con) {
}

draw_util_surface_free(conn, &(con->frame));
sparse_surface_free(conn, &(con->frame_buffer), true);
sparse_surface_free(conn, &(con->frame_buffer));
state = state_for_frame(con->frame.id);
CIRCLEQ_REMOVE(&state_head, state, state);
CIRCLEQ_REMOVE(&old_state_head, state, old_state);
Expand Down Expand Up @@ -989,15 +989,10 @@ void x_push_node(Con *con) {
* previously having a border or titlebar. */
if (!is_pixmap_needed && sparse_surface_initialized(&(con->frame_buffer))) {
/* Free the sparse surface, and the ID as well */
sparse_surface_free(conn, &(con->frame_buffer), true);
sparse_surface_free(conn, &(con->frame_buffer));
}

if (is_pixmap_needed && (has_rect_changed || !sparse_surface_initialized(&(con->frame_buffer)))) {
if (sparse_surface_initialized(&(con->frame_buffer))) {
/* Free the sparse surface, but keep the ID */
sparse_surface_free(conn, &(con->frame_buffer), false);
}

uint16_t win_depth = root_depth;
if (con->window)
win_depth = con->window->depth;
Expand Down

0 comments on commit f252e68

Please sign in to comment.