diff --git a/include/common.h b/include/common.h index ee7f68b..a3a1dbf 100644 --- a/include/common.h +++ b/include/common.h @@ -57,6 +57,7 @@ struct nvnc_display; struct nvnc_common { void* userdata; + nvnc_cleanup_fn cleanup_fn; }; struct cut_text { diff --git a/include/neatvnc.h b/include/neatvnc.h index 6d1e2ab..64e7115 100644 --- a/include/neatvnc.h +++ b/include/neatvnc.h @@ -51,6 +51,7 @@ typedef bool (*nvnc_auth_fn)(const char* username, const char* password, void* userdata); typedef void (*nvnc_cut_text_fn)(struct nvnc*, const char* text, uint32_t len); typedef void (*nvnc_fb_release_fn)(struct nvnc_fb*, void* context); +typedef void (*nvnc_cleanup_fn)(void* userdata); extern const char nvnc_version[]; @@ -61,7 +62,7 @@ void nvnc_close(struct nvnc* self); void nvnc_add_display(struct nvnc*, struct nvnc_display*); void nvnc_remove_display(struct nvnc*, struct nvnc_display*); -void nvnc_set_userdata(void* self, void* userdata); +void nvnc_set_userdata(void* self, void* userdata, nvnc_cleanup_fn); void* nvnc_get_userdata(const void* self); struct nvnc* nvnc_client_get_server(const struct nvnc_client* client); diff --git a/src/fb.c b/src/fb.c index b27b681..08dca95 100644 --- a/src/fb.c +++ b/src/fb.c @@ -94,6 +94,10 @@ uint32_t nvnc_fb_get_fourcc_format(const struct nvnc_fb* fb) static void nvnc__fb_free(struct nvnc_fb* fb) { + nvnc_cleanup_fn cleanup = fb->common.cleanup_fn; + if (cleanup) + cleanup(fb->common.userdata); + free(fb->addr); free(fb); } diff --git a/src/server.c b/src/server.c index 3ff5327..1eaefb1 100644 --- a/src/server.c +++ b/src/server.c @@ -99,6 +99,10 @@ static void client_close(struct nvnc_client* client) { log_debug("client_close(%p): ref %d\n", client, client->ref); + nvnc_cleanup_fn cleanup = client->common.cleanup_fn; + if (cleanup) + cleanup(client->common.userdata); + nvnc_client_fn fn = client->cleanup_fn; if (fn) fn(client); @@ -1177,6 +1181,10 @@ void nvnc_close(struct nvnc* self) { struct nvnc_client* client; + nvnc_cleanup_fn cleanup = self->common.cleanup_fn; + if (cleanup) + cleanup(self->common.userdata); + if (self->display) nvnc_display_unref(self->display); @@ -1441,10 +1449,11 @@ void nvnc__damage_region(struct nvnc* self, const struct pixman_region16* damage } EXPORT -void nvnc_set_userdata(void* self, void* userdata) +void nvnc_set_userdata(void* self, void* userdata, nvnc_cleanup_fn cleanup_fn) { struct nvnc_common* common = self; common->userdata = userdata; + common->cleanup_fn = cleanup_fn; } EXPORT