Add free functions for allocated resources

This commit is contained in:
Drew DeVault 2017-04-25 21:26:29 -04:00
parent 1e8970b4a9
commit 1aed987301
10 changed files with 96 additions and 10 deletions

View File

@ -59,4 +59,6 @@ include_directories(include)
add_subdirectory(backend) add_subdirectory(backend)
add_subdirectory(common) add_subdirectory(common)
add_subdirectory(wayland)
add_subdirectory(example) add_subdirectory(example)

View File

@ -12,5 +12,6 @@ add_library(wlr-backend
target_link_libraries(wlr-backend target_link_libraries(wlr-backend
wlr-common wlr-common
wlr-wayland
${WAYLAND_LIBRARIES} ${WAYLAND_LIBRARIES}
) )

View File

@ -9,7 +9,18 @@ void wlr_wl_backend_free(struct wlr_wl_backend *backend) {
if (!backend) { if (!backend) {
return; return;
} }
// TODO: free more shit // TODO: Free surfaces
for (size_t i = 0; backend->outputs && i < backend->outputs->length; ++i) {
struct wlr_wl_output *output = backend->outputs->items[i];
wlr_wl_output_free(output);
}
list_free(backend->outputs);
if (backend->seat) wlr_wl_seat_free(backend->seat);
if (backend->shm) wl_shm_destroy(backend->shm);
if (backend->shell) wl_shell_destroy(backend->shell);
if (backend->compositor) wl_compositor_destroy(backend->compositor);
if (backend->registry) wl_registry_destroy(backend->registry);
if (backend->remote_display) wl_display_disconnect(backend->remote_display);
free(backend); free(backend);
} }

View File

@ -25,7 +25,7 @@ static void registry_wl_seat(struct wlr_wl_backend *backend,
wl_seat_add_listener(wl_seat, &seat_listener, seat); wl_seat_add_listener(wl_seat, &seat_listener, seat);
return; return;
error: error:
//wlr_wl_seat_free(seat); TODO wlr_wl_seat_free(seat);
return; return;
} }
@ -47,7 +47,7 @@ static void registry_wl_output(struct wlr_wl_backend *backend,
wl_output_add_listener(wl_output, &output_listener, output); wl_output_add_listener(wl_output, &output_listener, output);
return; return;
error: error:
//wlr_wl_output_free(output); TODO wlr_wl_output_free(output);
return; return;
} }

View File

@ -1,19 +1,30 @@
#define _XOPEN_SOURCE 500
#include <string.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "backend/wayland.h" #include "backend/wayland.h"
#include "common/log.h"
static void wl_output_handle_mode(void *data, struct wl_output *wl_output, static void wl_output_handle_mode(void *data, struct wl_output *wl_output,
uint32_t flags, int32_t width, int32_t height, int32_t refresh) { uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
struct wlr_wl_output *output = data; struct wlr_wl_output *output = data;
assert(output->wl_output == wl_output); assert(output->wl_output == wl_output);
struct wlr_wl_output_mode *mode = calloc(sizeof(struct wlr_wl_output_mode), 1); struct wlr_wl_output_mode *mode;
if (!(mode = calloc(sizeof(struct wlr_wl_output_mode), 1))) {
wlr_log(L_ERROR, "Failed to allocate wlr_wl_output_mode");
return;
}
mode->flags = flags; mode->flags = flags;
mode->width = width; mode->width = width;
mode->height = height; mode->height = height;
mode->refresh = refresh; mode->refresh = refresh;
list_add(output->modes, mode); list_add(output->modes, mode);
wlr_log(L_DEBUG, "Got mode for output %p: %dx%d @ %.2fHz%s%s",
wl_output, width, height, refresh / 1000.0,
(flags & WL_OUTPUT_MODE_PREFERRED) ? " (preferred)" : "",
(flags & WL_OUTPUT_MODE_CURRENT) ? " (current)" : "");
} }
static void wl_output_handle_geometry(void *data, struct wl_output *wl_output, static void wl_output_handle_geometry(void *data, struct wl_output *wl_output,
@ -26,15 +37,19 @@ static void wl_output_handle_geometry(void *data, struct wl_output *wl_output,
output->phys_width = physical_width; output->phys_width = physical_width;
output->phys_height = physical_height; output->phys_height = physical_height;
output->subpixel = subpixel; output->subpixel = subpixel;
output->make = make; output->make = strdup(make);
output->model = model; output->model = strdup(model);
output->transform = transform; output->transform = transform;
wlr_log(L_DEBUG, "Got info for output %p %dx%d (%dmm x %dmm) %s %s",
wl_output, (int)x, (int)y, (int)physical_width, (int)physical_height,
make, model);
} }
static void wl_output_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) { static void wl_output_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) {
struct wlr_wl_output *output = data; struct wlr_wl_output *output = data;
assert(output->wl_output == wl_output); assert(output->wl_output == wl_output);
output->scale = factor; output->scale = factor;
wlr_log(L_DEBUG, "Got scale factor for output %p: %d", wl_output, factor);
} }
static void wl_output_handle_done(void *data, struct wl_output *wl_output) { static void wl_output_handle_done(void *data, struct wl_output *wl_output) {

View File

@ -1,6 +1,8 @@
#define _XOPEN_SOURCE 500
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "backend/wayland.h" #include "backend/wayland.h"
#include "common/log.h" #include "common/log.h"
@ -13,6 +15,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
assert(backend); assert(backend);
if ((caps & WL_SEAT_CAPABILITY_POINTER)) { if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
wlr_log(L_DEBUG, "seat %p offered pointer", wl_seat);
struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat); struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat);
struct wlr_wl_pointer *pointer; struct wlr_wl_pointer *pointer;
if (!(pointer = calloc(sizeof(struct wlr_wl_pointer), 1))) { if (!(pointer = calloc(sizeof(struct wlr_wl_pointer), 1))) {
@ -25,6 +28,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
} }
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
wlr_log(L_DEBUG, "seat %p offered keyboard", wl_seat);
struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat);
struct wlr_wl_keyboard *keyboard; struct wlr_wl_keyboard *keyboard;
if (!(keyboard = calloc(sizeof(struct wlr_wl_pointer), 1))) { if (!(keyboard = calloc(sizeof(struct wlr_wl_pointer), 1))) {
@ -42,7 +46,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) {
struct wlr_wl_seat *seat = data; struct wlr_wl_seat *seat = data;
assert(seat->wl_seat == wl_seat); assert(seat->wl_seat == wl_seat);
seat->name = name; seat->name = strdup(name);
} }
const struct wl_seat_listener seat_listener = { const struct wl_seat_listener seat_listener = {

View File

@ -7,11 +7,13 @@
struct wlr_wl_seat { struct wlr_wl_seat {
struct wl_seat *wl_seat; struct wl_seat *wl_seat;
uint32_t capabilities; uint32_t capabilities;
const char *name; char *name;
list_t *keyboards; list_t *keyboards;
list_t *pointers; list_t *pointers;
}; };
void wlr_wl_seat_free(struct wlr_wl_seat *seat);
struct wlr_wl_output_mode { struct wlr_wl_output_mode {
uint32_t flags; // enum wl_output_mode uint32_t flags; // enum wl_output_mode
int32_t width, height; int32_t width, height;
@ -21,8 +23,8 @@ struct wlr_wl_output_mode {
struct wlr_wl_output { struct wlr_wl_output {
struct wl_output *wl_output; struct wl_output *wl_output;
uint32_t flags; uint32_t flags;
const char *make; char *make;
const char *model; char *model;
uint32_t scale; uint32_t scale;
int32_t x, y; int32_t x, y;
int32_t phys_width, phys_height; // mm int32_t phys_width, phys_height; // mm
@ -32,6 +34,8 @@ struct wlr_wl_output {
struct wlr_wl_output_mode *current_mode; struct wlr_wl_output_mode *current_mode;
}; };
void wlr_wl_output_free(struct wlr_wl_output *output);
struct wlr_wl_keyboard { struct wlr_wl_keyboard {
struct wl_keyboard *wl_keyboard; struct wl_keyboard *wl_keyboard;
}; };

14
wayland/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
include_directories(
${PROTOCOLS_INCLUDE_DIRS}
${WAYLAND_INCLUDE_DIR}
)
add_library(wlr-wayland
types/wlr_wl_seat.c
types/wlr_wl_output.c
)
target_link_libraries(wlr-wayland
wlr-common
${WAYLAND_LIBRARIES}
)

View File

@ -0,0 +1,16 @@
#include <stdlib.h>
#include <wayland-client.h>
#include "wlr/wayland.h"
#include "wlr/common/list.h"
void wlr_wl_output_free(struct wlr_wl_output *output) {
if (!output) return;
if (output->wl_output) wl_output_destroy(output->wl_output);
if (output->make) free(output->make);
if (output->model) free(output->model);
for (size_t i = 0; output->modes && i < output->modes->length; ++i) {
free(output->modes->items[i]);
}
list_free(output->modes);
free(output);
}

View File

@ -0,0 +1,19 @@
#include <stdlib.h>
#include <wayland-client.h>
#include "wlr/wayland.h"
#include "wlr/common/list.h"
void wlr_wl_seat_free(struct wlr_wl_seat *seat) {
if (!seat) return;
if (seat->wl_seat) wl_seat_destroy(seat->wl_seat);
if (seat->name) free(seat->name);
if (seat->keyboards) {
// TODO: free children
list_free(seat->keyboards);
}
if (seat->pointers) {
// TODO: free children
list_free(seat->keyboards);
}
free(seat);
}