Refactor wlr_list inside wlroots

This commit is contained in:
emersion 2017-11-19 00:17:40 +01:00
parent fa36ac90f7
commit 016744ef4d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
9 changed files with 87 additions and 82 deletions

View File

@ -69,9 +69,9 @@ static bool wlr_libinput_backend_start(struct wlr_backend *_backend) {
no_devs = NULL; no_devs = NULL;
} }
} }
if (!no_devs && backend->wlr_device_lists->length == 0) { if (!no_devs && backend->wlr_device_lists.length == 0) {
wlr_libinput_readable(libinput_fd, WL_EVENT_READABLE, backend); wlr_libinput_readable(libinput_fd, WL_EVENT_READABLE, backend);
if (backend->wlr_device_lists->length == 0) { if (backend->wlr_device_lists.length == 0) {
wlr_log(L_ERROR, "libinput initialization failed, no input devices"); wlr_log(L_ERROR, "libinput initialization failed, no input devices");
wlr_log(L_ERROR, "Set WLR_LIBINPUT_NO_DEVICES=1 to suppress this check"); wlr_log(L_ERROR, "Set WLR_LIBINPUT_NO_DEVICES=1 to suppress this check");
return false; return false;
@ -97,9 +97,10 @@ static void wlr_libinput_backend_destroy(struct wlr_backend *_backend) {
if (!_backend) { if (!_backend) {
return; return;
} }
struct wlr_libinput_backend *backend = (struct wlr_libinput_backend *)_backend; struct wlr_libinput_backend *backend =
for (size_t i = 0; i < backend->wlr_device_lists->length; i++) { (struct wlr_libinput_backend *)_backend;
struct wl_list *wlr_devices = backend->wlr_device_lists->items[i]; for (size_t i = 0; i < backend->wlr_device_lists.length; i++) {
struct wl_list *wlr_devices = backend->wlr_device_lists.items[i];
struct wlr_input_device *wlr_dev, *next; struct wlr_input_device *wlr_dev, *next;
wl_list_for_each_safe(wlr_dev, next, wlr_devices, link) { wl_list_for_each_safe(wlr_dev, next, wlr_devices, link) {
wl_signal_emit(&backend->backend.events.input_remove, wlr_dev); wl_signal_emit(&backend->backend.events.input_remove, wlr_dev);
@ -107,7 +108,7 @@ static void wlr_libinput_backend_destroy(struct wlr_backend *_backend) {
} }
free(wlr_devices); free(wlr_devices);
} }
wlr_list_free(backend->wlr_device_lists); wlr_list_finish(&backend->wlr_device_lists);
wl_event_source_remove(backend->input_event); wl_event_source_remove(backend->input_event);
libinput_unref(backend->libinput_context); libinput_unref(backend->libinput_context);
free(backend); free(backend);
@ -148,7 +149,7 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display,
} }
wlr_backend_init(&backend->backend, &backend_impl); wlr_backend_init(&backend->backend, &backend_impl);
if (!(backend->wlr_device_lists = wlr_list_create())) { if (!wlr_list_init(&backend->wlr_device_lists)) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
goto error_backend; goto error_backend;
} }

View File

@ -147,7 +147,7 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
if (wl_list_length(wlr_devices) > 0) { if (wl_list_length(wlr_devices) > 0) {
libinput_device_set_user_data(libinput_dev, wlr_devices); libinput_device_set_user_data(libinput_dev, wlr_devices);
wlr_list_add(backend->wlr_device_lists, wlr_devices); wlr_list_push(&backend->wlr_device_lists, wlr_devices);
} else { } else {
free(wlr_devices); free(wlr_devices);
} }
@ -177,9 +177,9 @@ static void handle_device_removed(struct wlr_libinput_backend *backend,
wl_signal_emit(&backend->backend.events.input_remove, dev); wl_signal_emit(&backend->backend.events.input_remove, dev);
wlr_input_device_destroy(dev); wlr_input_device_destroy(dev);
} }
for (size_t i = 0; i < backend->wlr_device_lists->length; i++) { for (size_t i = 0; i < backend->wlr_device_lists.length; i++) {
if (backend->wlr_device_lists->items[i] == wlr_devices) { if (backend->wlr_device_lists.items[i] == wlr_devices) {
wlr_list_del(backend->wlr_device_lists, i); wlr_list_del(&backend->wlr_device_lists, i);
break; break;
} }
} }

View File

@ -14,7 +14,6 @@
#include <wlr/backend/drm.h> #include <wlr/backend/drm.h>
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/render/egl.h> #include <wlr/render/egl.h>
#include <wlr/types/wlr_list.h>
#include "iface.h" #include "iface.h"
#include "properties.h" #include "properties.h"

View File

@ -18,7 +18,7 @@ struct wlr_libinput_backend {
struct wl_listener session_signal; struct wl_listener session_signal;
struct wlr_list *wlr_device_lists; struct wlr_list wlr_device_lists; // list of struct wl_list
}; };
struct wlr_libinput_input_device { struct wlr_libinput_input_device {

View File

@ -2,6 +2,7 @@
#define WLR_UTIL_LIST_H #define WLR_UTIL_LIST_H
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
struct wlr_list { struct wlr_list {
size_t capacity; size_t capacity;
@ -10,51 +11,65 @@ struct wlr_list {
}; };
/** /**
* Creates a new list, may return `NULL` on failure * Initialize a list. Returns true on success, false on failure.
*/ */
struct wlr_list *wlr_list_create(void); bool wlr_list_init(struct wlr_list *list);
void wlr_list_free(struct wlr_list *list);
void wlr_list_foreach(struct wlr_list *list, void (*callback)(void *item)); /**
* Deinitialize a list.
*/
void wlr_list_finish(struct wlr_list *list);
/**
* Executes `callback` on each element in the list.
*/
void wlr_list_for_each(struct wlr_list *list, void (*callback)(void *item));
/** /**
* Add `item` to the end of a list. * Add `item` to the end of a list.
* Returns: new list length or `-1` on failure * Returns: new list length or `-1` on failure.
*/ */
int wlr_list_add(struct wlr_list *list, void *item); ssize_t wlr_list_push(struct wlr_list *list, void *item);
/** /**
* Add `item` to the end of a list. * Place `item` into index `index` in the list.
* Returns: new list length or `-1` on failure * Returns: new list length or `-1` on failure.
*/ */
int wlr_list_push(struct wlr_list *list, void *item); ssize_t wlr_list_insert(struct wlr_list *list, size_t index, void *item);
/** /**
* Place `item` into index `index` in the list * Remove an item from the list.
* Returns: new list length or `-1` on failure
*/
int wlr_list_insert(struct wlr_list *list, size_t index, void *item);
/**
* Remove an item from the list
*/ */
void wlr_list_del(struct wlr_list *list, size_t index); void wlr_list_del(struct wlr_list *list, size_t index);
/** /**
* Remove and return an item from the end of the list * Remove and return an item from the end of the list.
*/ */
void *wlr_list_pop(struct wlr_list *list); void *wlr_list_pop(struct wlr_list *list);
/** /**
* Get a reference to the last item of a list without removal * Get a reference to the last item of a list without removal.
*/ */
void *wlr_list_peek(struct wlr_list *list); void *wlr_list_peek(struct wlr_list *list);
/** /**
* Append each item in `source` to `list` * Append each item in `source` to `list`.
* Does not modify `source` * Does not modify `source`.
* Returns: new list length or `-1` on failure * Returns: new list length or `-1` on failure.
*/ */
int wlr_list_cat(struct wlr_list *list, struct wlr_list *source); ssize_t wlr_list_cat(struct wlr_list *list, const struct wlr_list *source);
// See qsort. Remember to use *_qsort functions as compare functions,
// because they dereference the left and right arguments first! /**
void wlr_list_qsort(struct wlr_list *list, int compare(const void *left, const void *right)); * Sort a list using `qsort`.
// Return index for first item in list that returns 0 for given compare */
// function or -1 if none matches. void wlr_list_qsort(struct wlr_list *list,
int wlr_list_seq_find(struct wlr_list *list, int compare(const void *left, const void *right));
int compare(const void *item, const void *cmp_to),
const void *cmp_to); /**
* Return the index of the first item in the list that returns 0 for the given
* `compare` function, or -1 if none matches.
*/
ssize_t wlr_list_find(struct wlr_list *list,
int compare(const void *item, const void *cmp_to), const void *cmp_to);
#endif #endif

View File

@ -5,7 +5,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_compositor.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include <wlr/types/wlr_list.h>
#ifdef HAS_XCB_ICCCM #ifdef HAS_XCB_ICCCM
#include <xcb/xcb_icccm.h> #include <xcb/xcb_icccm.h>
@ -86,7 +85,6 @@ struct wlr_xwayland_surface {
char *class; char *class;
char *instance; char *instance;
struct wlr_xwayland_surface *parent; struct wlr_xwayland_surface *parent;
struct wlr_list *state; // list of xcb_atom_t
pid_t pid; pid_t pid;
xcb_atom_t *window_type; xcb_atom_t *window_type;

View File

@ -3,26 +3,23 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
#include <sys/types.h>
#include <wlr/types/wlr_list.h> #include <wlr/types/wlr_list.h>
struct wlr_list *wlr_list_create(void) { bool wlr_list_init(struct wlr_list *list) {
struct wlr_list *list = malloc(sizeof(struct wlr_list));
if (!list) {
return NULL;
}
list->capacity = 10; list->capacity = 10;
list->length = 0; list->length = 0;
list->items = malloc(sizeof(void *) * list->capacity); list->items = malloc(sizeof(void *) * list->capacity);
if (!list->items) { if (list->items == NULL) {
free(list); return false;
return NULL;
} }
return list; return true;
} }
static bool list_resize(struct wlr_list *list) { static bool list_resize(struct wlr_list *list) {
if (list->length == list->capacity) { if (list->length == list->capacity) {
void *new_items = realloc(list->items, sizeof(void*) * (list->capacity + 10)); void *new_items = realloc(list->items,
sizeof(void *) * (list->capacity + 10));
if (!new_items) { if (!new_items) {
return false; return false;
} }
@ -32,24 +29,17 @@ static bool list_resize(struct wlr_list *list) {
return true; return true;
} }
void wlr_list_free(struct wlr_list *list) { void wlr_list_finish(struct wlr_list *list) {
if (list == NULL) {
return;
}
free(list->items); free(list->items);
free(list);
} }
void wlr_list_foreach(struct wlr_list *list, void (*callback)(void *item)) { void wlr_list_for_each(struct wlr_list *list, void (*callback)(void *item)) {
if (list == NULL || callback == NULL) {
return;
}
for (size_t i = 0; i < list->length; i++) { for (size_t i = 0; i < list->length; i++) {
callback(list->items[i]); callback(list->items[i]);
} }
} }
int wlr_list_add(struct wlr_list *list, void *item) { ssize_t wlr_list_push(struct wlr_list *list, void *item) {
if (!list_resize(list)) { if (!list_resize(list)) {
return -1; return -1;
} }
@ -57,15 +47,12 @@ int wlr_list_add(struct wlr_list *list, void *item) {
return list->length; return list->length;
} }
int wlr_list_push(struct wlr_list *list, void *item) { ssize_t wlr_list_insert(struct wlr_list *list, size_t index, void *item) {
return wlr_list_add(list, item);
}
int wlr_list_insert(struct wlr_list *list, size_t index, void *item) {
if (!list_resize(list)) { if (!list_resize(list)) {
return -1; return -1;
} }
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); memmove(&list->items[index + 1], &list->items[index],
sizeof(void *) * (list->length - index));
list->length++; list->length++;
list->items[index] = item; list->items[index] = item;
return list->length; return list->length;
@ -73,24 +60,31 @@ int wlr_list_insert(struct wlr_list *list, size_t index, void *item) {
void wlr_list_del(struct wlr_list *list, size_t index) { void wlr_list_del(struct wlr_list *list, size_t index) {
list->length--; list->length--;
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); memmove(&list->items[index], &list->items[index + 1],
sizeof(void *) * (list->length - index));
} }
void *wlr_list_pop(struct wlr_list *list) { void *wlr_list_pop(struct wlr_list *list) {
void *_ = list->items[list->length - 1]; if (list->length == 0) {
return NULL;
}
void *last = list->items[list->length - 1];
wlr_list_del(list, list->length - 1); wlr_list_del(list, list->length - 1);
return _; return last;
} }
void *wlr_list_peek(struct wlr_list *list) { void *wlr_list_peek(struct wlr_list *list) {
if (list->length == 0) {
return NULL;
}
return list->items[list->length - 1]; return list->items[list->length - 1];
} }
int wlr_list_cat(struct wlr_list *list, struct wlr_list *source) { ssize_t wlr_list_cat(struct wlr_list *list, const struct wlr_list *source) {
size_t old_len = list->length; size_t old_len = list->length;
size_t i; size_t i;
for (i = 0; i < source->length; ++i) { for (i = 0; i < source->length; ++i) {
if (wlr_list_add(list, source->items[i]) == -1) { if (wlr_list_push(list, source->items[i]) == -1) {
list->length = old_len; list->length = old_len;
return -1; return -1;
} }
@ -98,13 +92,13 @@ int wlr_list_cat(struct wlr_list *list, struct wlr_list *source) {
return list->length; return list->length;
} }
void wlr_list_qsort(struct wlr_list *list, int compare(const void *left, const void *right)) { void wlr_list_qsort(struct wlr_list *list,
int compare(const void *left, const void *right)) {
qsort(list->items, list->length, sizeof(void *), compare); qsort(list->items, list->length, sizeof(void *), compare);
} }
int wlr_list_seq_find(struct wlr_list *list, ssize_t wlr_list_find(struct wlr_list *list,
int compare(const void *item, const void *data), int compare(const void *item, const void *data), const void *data) {
const void *data) {
for (size_t i = 0; i < list->length; i++) { for (size_t i = 0; i < list->length; i++) {
void *item = list->items[i]; void *item = list->items[i];
if (compare(item, data) == 0) { if (compare(item, data) == 0) {

View File

@ -9,7 +9,6 @@
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_surface.h> #include <wlr/types/wlr_surface.h>
#include <wlr/interfaces/wlr_output.h> #include <wlr/interfaces/wlr_output.h>
#include <wlr/types/wlr_list.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <GLES2/gl2.h> #include <GLES2/gl2.h>
#include <wlr/render/matrix.h> #include <wlr/render/matrix.h>

View File

@ -217,7 +217,6 @@ static void wlr_xwayland_surface_destroy(
free(xsurface->title); free(xsurface->title);
free(xsurface->class); free(xsurface->class);
free(xsurface->instance); free(xsurface->instance);
wlr_list_free(xsurface->state);
free(xsurface->window_type); free(xsurface->window_type);
free(xsurface->protocols); free(xsurface->protocols);
free(xsurface->hints); free(xsurface->hints);