~earboxer/suggpicker

6566a7f57d695d888650d43225844f9641414919 — ArenM 2 years ago 1c4df6a
only commit surface when it changed

This is based off of a commit in wvkbd

Previously wvkbd (and suggpicker) would commit a (usually unchanged) surface at the framerate of the compositor,
this only commits the buffer when we render something new.
4 files changed, 8 insertions(+), 27 deletions(-)

M drw.c
M drw.h
M keyboard.c
M main.c
M drw.c => drw.c +0 -18
@@ 20,31 20,13 @@ drwsurf_resize(struct drwsurf *ds, uint32_t w, uint32_t h, uint32_t s) {
	setup_buffer(ds);
}

static void surface_frame_callback(void *data, struct wl_callback *cb,
                                   uint32_t time);

static struct wl_callback_listener frame_listener = {.done =
                                                       surface_frame_callback};

void
drwsurf_flip(struct drwsurf *ds) {
	ds->cb = wl_surface_frame(ds->surf);
	wl_callback_add_listener(ds->cb, &frame_listener, (void *)ds);

	wl_surface_attach(ds->surf, ds->buf, 0, 0);
	wl_surface_commit(ds->surf);
}

void
surface_frame_callback(void *data, struct wl_callback *cb, uint32_t time) {
	struct drwsurf *ds = (struct drwsurf *)data;
	wl_callback_destroy(cb);
	ds->cb = NULL;

	drwsurf_flip(ds);
}

void
drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
              uint32_t w, uint32_t h, const char *label) {


M drw.h => drw.h +0 -1
@@ 15,7 15,6 @@ struct drwsurf {
	struct wl_surface *surf;
	struct wl_buffer *buf;
	struct wl_shm *shm;
	struct wl_callback *cb;
	unsigned char *pool_data;

	cairo_t *cairo;

M keyboard.c => keyboard.c +2 -1
@@ 73,6 73,7 @@ kbd_unpress_key(struct kbd *kb) {
	if (kb->last_press) {
		kbd_draw_key(kb, kb->last_press, Unpress);
		kb->last_press = NULL;
		drwsurf_flip(kb->surf);
	}
}



@@ 83,7 84,6 @@ kbd_release_key(struct kbd *kb) {
		// Important so autocompleted words get typed in time
		fflush(stdout);
		kbd_unpress_key(kb);
		kbd_draw_layout(kb);
	}
}



@@ 104,6 104,7 @@ kbd_press_key(struct kbd *kb, struct key *k) {
	if (k->label) {
		kb->last_press = k;
		kbd_draw_key(kb, k, Press);
		drwsurf_flip(kb->surf);
	}
}


M main.c => main.c +6 -7
@@ 307,6 307,8 @@ layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
	}

	zwlr_layer_surface_v1_ack_configure(surface, serial);

	drwsurf_flip(&draw_surf);
}

void


@@ 339,10 341,6 @@ hide(int sigint) {
	zwlr_layer_surface_v1_destroy(layer_surface);
	wl_surface_destroy(draw_surf.surf);
	layer_surface = NULL;
	if (draw_surf.cb) {
		wl_callback_destroy(draw_surf.cb);
		draw_surf.cb = NULL;
	}
}

void


@@ 399,6 397,7 @@ handle_input(FILE *fd, struct key *sugg, struct kbd *kb) {
		key->label = NULL;
		kbd_init_suggs(sugg, kb->w, kb->h);
		kbd_draw_layout(kb);
		drwsurf_flip(kb->surf);
	}

	free(line);


@@ 506,9 505,9 @@ main(int argc, char **argv) {
		while(layer_surface && poll(fds, 2, -1) != -1) {
			if (fds[0].revents & POLLIN) {
				handle_input(stdin, keyboard.suggs, &keyboard);
			}
			if (wl_display_dispatch(display) == -1) {
				break;
			} else if(fds[1].revents & POLLIN) {
				//error check?
				wl_display_dispatch(display);
			}
			wl_display_flush(display);
		}