~emersion/chayang

928cb37f9a242a43cfe97d2ce63a1521d6bfd5a2 — Simon Ser 1 year, 3 months ago 4ad19de
Fix wl_callback memory leak

And better behave when multiple configure events are received.
2 files changed, 15 insertions(+), 2 deletions(-)

M include/chayang.h
M main.c
M include/chayang.h => include/chayang.h +1 -0
@@ 28,6 28,7 @@ struct chayang_output {
	struct wl_surface *surface;
	struct zwlr_layer_surface_v1 *layer_surface;
	struct wp_viewport *viewport;
	struct wl_callback *frame_callback;

	int32_t surface_width, surface_height;
};

M main.c => main.c +14 -2
@@ 1,3 1,4 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>


@@ 31,7 32,12 @@ static void cancel(struct chayang *state) {

static void frame_callback_handle_done(void *data, struct wl_callback *callback, uint32_t time) {
	struct chayang_output *output = data;
	if (callback == NULL) {
		return;
	}
	assert(callback == output->frame_callback);
	wl_callback_destroy(callback);
	output->frame_callback = NULL;
	repaint_output(output);
}



@@ 53,8 59,11 @@ static void repaint_output(struct chayang_output *output) {

	wp_viewport_set_destination(output->viewport, output->surface_width, output->surface_height);

	struct wl_callback *frame_callback = wl_surface_frame(output->surface);
	wl_callback_add_listener(frame_callback, &frame_callback_listener, output);
	if (output->frame_callback != NULL) {
		wl_callback_destroy(output->frame_callback);
	}
	output->frame_callback = wl_surface_frame(output->surface);
	wl_callback_add_listener(output->frame_callback, &frame_callback_listener, output);

	wl_surface_damage(output->surface, 0, 0, INT32_MAX, INT32_MAX);
	wl_surface_commit(output->surface);


@@ 63,6 72,9 @@ static void repaint_output(struct chayang_output *output) {
}

static void destroy_output(struct chayang_output *output) {
	if (output->frame_callback != NULL) {
		wl_callback_destroy(output->frame_callback);
	}
	wp_viewport_destroy(output->viewport);
	zwlr_layer_surface_v1_destroy(output->layer_surface);
	wl_surface_destroy(output->surface);