diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 45b4e368..1028e238 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -69,9 +69,9 @@ static bool wlr_libinput_backend_start(struct wlr_backend *_backend) { 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); - 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, "Set WLR_LIBINPUT_NO_DEVICES=1 to suppress this check"); return false; @@ -97,9 +97,10 @@ static void wlr_libinput_backend_destroy(struct wlr_backend *_backend) { if (!_backend) { return; } - struct wlr_libinput_backend *backend = (struct wlr_libinput_backend *)_backend; - 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_libinput_backend *backend = + (struct wlr_libinput_backend *)_backend; + 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; wl_list_for_each_safe(wlr_dev, next, wlr_devices, link) { 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); } - wlr_list_free(backend->wlr_device_lists); + wlr_list_finish(&backend->wlr_device_lists); wl_event_source_remove(backend->input_event); libinput_unref(backend->libinput_context); free(backend); @@ -148,7 +149,7 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, } 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)); goto error_backend; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 3ca41124..c14aef01 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -147,7 +147,7 @@ static void handle_device_added(struct wlr_libinput_backend *backend, if (wl_list_length(wlr_devices) > 0) { 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 { 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); wlr_input_device_destroy(dev); } - for (size_t i = 0; i < backend->wlr_device_lists->length; i++) { - if (backend->wlr_device_lists->items[i] == wlr_devices) { - wlr_list_del(backend->wlr_device_lists, i); + for (size_t i = 0; i < backend->wlr_device_lists.length; i++) { + if (backend->wlr_device_lists.items[i] == wlr_devices) { + wlr_list_del(&backend->wlr_device_lists, i); break; } } diff --git a/dummy.c b/dummy.c deleted file mode 100644 index 870fde8a..00000000 --- a/dummy.c +++ /dev/null @@ -1,2 +0,0 @@ -// This is used to make meson happy -void dummy(void); diff --git a/examples/pointer.c b/examples/pointer.c index 8fafb3d6..f75e1bce 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -46,7 +46,7 @@ struct sample_state { struct wl_listener touch_up; struct wl_listener touch_down; struct wl_listener touch_cancel; - struct wlr_list *touch_points; + struct wl_list touch_points; struct wl_listener tablet_tool_axis; struct wl_listener tablet_tool_proxmity; @@ -57,22 +57,25 @@ struct sample_state { struct touch_point { int32_t touch_id; double x, y; + struct wl_list link; }; static void warp_to_touch(struct sample_state *sample, struct wlr_input_device *dev) { - if (sample->touch_points->length == 0) { + if (wl_list_empty(&sample->touch_points)) { return; } double x = 0, y = 0; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *point = sample->touch_points->items[i]; + size_t n = 0; + struct touch_point *point; + wl_list_for_each(point, &sample->touch_points, link) { x += point->x; y += point->y; + n++; } - x /= sample->touch_points->length; - y /= sample->touch_points->length; + x /= n; + y /= n; wlr_cursor_warp_absolute(sample->cursor, dev, x, y); } @@ -197,10 +200,11 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) { static void handle_touch_up(struct wl_listener *listener, void *data) { struct sample_state *sample = wl_container_of(listener, sample, touch_up); struct wlr_event_touch_up *event = data; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *point = sample->touch_points->items[i]; + + struct touch_point *point, *tmp; + wl_list_for_each_safe(point, tmp, &sample->touch_points, link) { if (point->touch_id == event->touch_id) { - wlr_list_del(sample->touch_points, i); + wl_list_remove(&point->link); break; } } @@ -215,9 +219,7 @@ static void handle_touch_down(struct wl_listener *listener, void *data) { point->touch_id = event->touch_id; point->x = event->x_mm / event->width_mm; point->y = event->y_mm / event->height_mm; - if (wlr_list_add(sample->touch_points, point) == -1) { - free(point); - } + wl_list_insert(&sample->touch_points, &point->link); warp_to_touch(sample, event->device); } @@ -226,8 +228,9 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) { struct sample_state *sample = wl_container_of(listener, sample, touch_motion); struct wlr_event_touch_motion *event = data; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *point = sample->touch_points->items[i]; + + struct touch_point *point; + wl_list_for_each(point, &sample->touch_points, link) { if (point->touch_id == event->touch_id) { point->x = event->x_mm / event->width_mm; point->y = event->y_mm / event->height_mm; @@ -257,7 +260,6 @@ int main(int argc, char *argv[]) { struct sample_state state = { .default_color = { 0.25f, 0.25f, 0.25f, 1 }, .clear_color = { 0.25f, 0.25f, 0.25f, 1 }, - .touch_points = wlr_list_create(), }; state.config = parse_args(argc, argv); @@ -266,6 +268,7 @@ int main(int argc, char *argv[]) { wlr_cursor_attach_output_layout(state.cursor, state.layout); wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box); wl_list_init(&state.devices); + wl_list_init(&state.touch_points); // pointer events wl_signal_add(&state.cursor->events.motion, &state.cursor_motion); diff --git a/examples/touch.c b/examples/touch.c index d1891525..f18b5d26 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -24,12 +24,13 @@ struct sample_state { struct wlr_renderer *renderer; struct wlr_texture *cat_texture; - struct wlr_list *touch_points; + struct wl_list touch_points; }; struct touch_point { int32_t touch_id; double x, y; + struct wl_list link; }; static void handle_output_frame(struct output_state *output, struct timespec *ts) { @@ -44,14 +45,14 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); float matrix[16]; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *p = sample->touch_points->items[i]; + struct touch_point *p; + wl_list_for_each(p, &sample->touch_points, link) { wlr_texture_get_matrix(sample->cat_texture, &matrix, &wlr_output->transform_matrix, (int)(p->x * width) - sample->cat_texture->width / 2, (int)(p->y * height) - sample->cat_texture->height / 2); wlr_render_with_matrix(sample->renderer, - sample->cat_texture, &matrix); + sample->cat_texture, &matrix); } wlr_renderer_end(sample->renderer); @@ -65,17 +66,15 @@ static void handle_touch_down(struct touch_state *tstate, int32_t touch_id, point->touch_id = touch_id; point->x = x / width; point->y = y / height; - if (wlr_list_add(sample->touch_points, point) == -1) { - free(point); - } + wl_list_insert(&sample->touch_points, &point->link); } static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) { struct sample_state *sample = tstate->compositor->data; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *point = sample->touch_points->items[i]; + struct touch_point *point, *tmp; + wl_list_for_each_safe(point, tmp, &sample->touch_points, link) { if (point->touch_id == touch_id) { - wlr_list_del(sample->touch_points, i); + wl_list_remove(&point->link); break; } } @@ -84,8 +83,8 @@ static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) { static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id, double x, double y, double width, double height) { struct sample_state *sample = tstate->compositor->data; - for (size_t i = 0; i < sample->touch_points->length; ++i) { - struct touch_point *point = sample->touch_points->items[i]; + struct touch_point *point; + wl_list_for_each(point, &sample->touch_points, link) { if (point->touch_id == touch_id) { point->x = x / width; point->y = y / height; @@ -95,9 +94,9 @@ static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id, } int main(int argc, char *argv[]) { - struct sample_state state = { - .touch_points = wlr_list_create() - }; + struct sample_state state; + wl_list_init(&state.touch_points); + struct compositor_state compositor = { 0, .data = &state, .output_frame_cb = handle_output_frame, diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index e08965e5..4fad9678 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -14,7 +14,6 @@ #include #include #include -#include #include "iface.h" #include "properties.h" diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 93b859a7..be2557ed 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -18,7 +18,7 @@ struct wlr_libinput_backend { 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 { diff --git a/include/wlr/types/wlr_list.h b/include/wlr/types/wlr_list.h index 8ddd30b6..78a30995 100644 --- a/include/wlr/types/wlr_list.h +++ b/include/wlr/types/wlr_list.h @@ -2,6 +2,7 @@ #define WLR_UTIL_LIST_H #include +#include struct wlr_list { 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); -void wlr_list_free(struct wlr_list *list); -void wlr_list_foreach(struct wlr_list *list, void (*callback)(void *item)); +bool wlr_list_init(struct wlr_list *list); + +/** + * 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. - * 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. - * Returns: new list length or `-1` on failure + * Place `item` into index `index` in the list. + * 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 - * 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 + * Remove an item from the list. */ 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); + /** - * 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); + /** - * Append each item in `source` to `list` - * Does not modify `source` - * Returns: new list length or `-1` on failure + * Append each item in `source` to `list`. + * Does not modify `source`. + * Returns: new list length or `-1` on failure. */ -int wlr_list_cat(struct wlr_list *list, 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)); -// Return index for first item in list that returns 0 for given compare -// function or -1 if none matches. -int wlr_list_seq_find(struct wlr_list *list, - int compare(const void *item, const void *cmp_to), - const void *cmp_to); +ssize_t wlr_list_cat(struct wlr_list *list, const struct wlr_list *source); + +/** + * Sort a list using `qsort`. + */ +void wlr_list_qsort(struct wlr_list *list, + int compare(const void *left, const void *right)); + +/** + * 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 diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index 6518b703..9b493d88 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -5,7 +5,6 @@ #include #include #include -#include #ifdef HAS_XCB_ICCCM #include @@ -86,7 +85,6 @@ struct wlr_xwayland_surface { char *class; char *instance; struct wlr_xwayland_surface *parent; - struct wlr_list *state; // list of xcb_atom_t pid_t pid; xcb_atom_t *window_type; diff --git a/meson.build b/meson.build index 642dc38d..8ace085c 100644 --- a/meson.build +++ b/meson.build @@ -131,7 +131,6 @@ wlr_deps = [ lib_wlr = library( 'wlroots', - files('dummy.c'), link_whole: wlr_parts, dependencies: wlr_deps, include_directories: wlr_inc, diff --git a/rootston/desktop.c b/rootston/desktop.c index cf5a8cdc..65730807 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -181,15 +181,12 @@ bool view_center(struct roots_view *view) { void view_destroy(struct roots_view *view) { wl_signal_emit(&view->events.destroy, view); - wl_list_remove(&view->link); free(view); } void view_init(struct roots_view *view, struct roots_desktop *desktop) { view->desktop = desktop; wl_signal_init(&view->events.destroy); - - wl_list_insert(&desktop->views, &view->link); } void view_setup(struct roots_view *view) { diff --git a/rootston/wl_shell.c b/rootston/wl_shell.c index 7359c878..d0f5989b 100644 --- a/rootston/wl_shell.c +++ b/rootston/wl_shell.c @@ -83,6 +83,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&roots_surface->request_set_maximized.link); wl_list_remove(&roots_surface->set_state.link); wl_list_remove(&roots_surface->surface_commit.link); + wl_list_remove(&roots_surface->view->link); view_destroy(roots_surface->view); free(roots_surface); } @@ -131,6 +132,7 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { view->close = close; roots_surface->view = view; view_init(view, desktop); + wl_list_insert(&desktop->views, &view->link); view_setup(view); diff --git a/rootston/xdg_shell_v6.c b/rootston/xdg_shell_v6.c index 656231c7..ea19753b 100644 --- a/rootston/xdg_shell_v6.c +++ b/rootston/xdg_shell_v6.c @@ -165,6 +165,7 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&roots_xdg_surface->destroy.link); wl_list_remove(&roots_xdg_surface->request_move.link); wl_list_remove(&roots_xdg_surface->request_resize.link); + wl_list_remove(&roots_xdg_surface->view->link); view_destroy(roots_xdg_surface->view); free(roots_xdg_surface); } @@ -220,6 +221,7 @@ void handle_xdg_shell_v6_surface(struct wl_listener *listener, void *data) { view->close = close; roots_surface->view = view; view_init(view, desktop); + wl_list_insert(&desktop->views, &view->link); view_setup(view); } diff --git a/rootston/xwayland.c b/rootston/xwayland.c index c92c424b..dfa602b1 100644 --- a/rootston/xwayland.c +++ b/rootston/xwayland.c @@ -95,6 +95,8 @@ static void maximize(struct roots_view *view, bool maximized) { static void handle_destroy(struct wl_listener *listener, void *data) { struct roots_xwayland_surface *roots_surface = wl_container_of(listener, roots_surface, destroy); + struct wlr_xwayland_surface *xwayland_surface = + roots_surface->view->xwayland_surface; wl_list_remove(&roots_surface->destroy.link); wl_list_remove(&roots_surface->request_configure.link); wl_list_remove(&roots_surface->request_move.link); @@ -102,6 +104,9 @@ static void handle_destroy(struct wl_listener *listener, void *data) { wl_list_remove(&roots_surface->request_maximize.link); wl_list_remove(&roots_surface->map_notify.link); wl_list_remove(&roots_surface->unmap_notify.link); + if (xwayland_surface->mapped) { + wl_list_remove(&roots_surface->view->link); + } view_destroy(roots_surface->view); free(roots_surface); } @@ -243,6 +248,7 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { view->close = close; roots_surface->view = view; view_init(view, desktop); + wl_list_insert(&desktop->views, &view->link); if (!surface->override_redirect) { view_setup(view); diff --git a/types/wlr_list.c b/types/wlr_list.c index 33e3ea8a..565a155b 100644 --- a/types/wlr_list.c +++ b/types/wlr_list.c @@ -3,26 +3,23 @@ #include #include #include +#include #include -struct wlr_list *wlr_list_create(void) { - struct wlr_list *list = malloc(sizeof(struct wlr_list)); - if (!list) { - return NULL; - } +bool wlr_list_init(struct wlr_list *list) { list->capacity = 10; list->length = 0; - list->items = malloc(sizeof(void*) * list->capacity); - if (!list->items) { - free(list); - return NULL; + list->items = malloc(sizeof(void *) * list->capacity); + if (list->items == NULL) { + return false; } - return list; + return true; } static bool list_resize(struct wlr_list *list) { 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) { return false; } @@ -32,24 +29,17 @@ static bool list_resize(struct wlr_list *list) { return true; } -void wlr_list_free(struct wlr_list *list) { - if (list == NULL) { - return; - } +void wlr_list_finish(struct wlr_list *list) { free(list->items); - free(list); } -void wlr_list_foreach(struct wlr_list *list, void (*callback)(void *item)) { - if (list == NULL || callback == NULL) { - return; - } +void wlr_list_for_each(struct wlr_list *list, void (*callback)(void *item)) { for (size_t i = 0; i < list->length; 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)) { return -1; } @@ -57,15 +47,12 @@ int wlr_list_add(struct wlr_list *list, void *item) { return list->length; } -int wlr_list_push(struct wlr_list *list, void *item) { - return wlr_list_add(list, item); -} - -int wlr_list_insert(struct wlr_list *list, size_t index, void *item) { +ssize_t wlr_list_insert(struct wlr_list *list, size_t index, void *item) { if (!list_resize(list)) { 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->items[index] = item; 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) { 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 *_ = 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); - return _; + return last; } void *wlr_list_peek(struct wlr_list *list) { + if (list->length == 0) { + return NULL; + } 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 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; return -1; } @@ -98,13 +92,13 @@ int wlr_list_cat(struct wlr_list *list, struct wlr_list *source) { 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); } -int wlr_list_seq_find(struct wlr_list *list, - int compare(const void *item, const void *data), - const void *data) { +ssize_t wlr_list_find(struct wlr_list *list, + int compare(const void *item, const void *data), const void *data) { for (size_t i = 0; i < list->length; i++) { void *item = list->items[i]; if (compare(item, data) == 0) { diff --git a/types/wlr_output.c b/types/wlr_output.c index 411dd404..94fe4c3b 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include diff --git a/xwayland/xwm.c b/xwayland/xwm.c index febe1ea2..39cd6362 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -221,7 +221,6 @@ static void wlr_xwayland_surface_destroy( free(xsurface->title); free(xsurface->class); free(xsurface->instance); - wlr_list_free(xsurface->state); free(xsurface->window_type); free(xsurface->protocols); free(xsurface->hints);