From 411078815953ef0a29e61b49868e646ad899fb01 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 21:48:20 +0200 Subject: [PATCH 1/2] move wl_compositor into wlroots as wlr_compositor --- examples/{compositor/main.c => compositor.c} | 7 +-- examples/meson.build | 7 +-- .../wlr/types/wlr_compositor.h | 9 ++-- types/meson.build | 1 + .../wl_compositor.c => types/wlr_compositor.c | 45 ++++++++++--------- 5 files changed, 36 insertions(+), 33 deletions(-) rename examples/{compositor/main.c => compositor.c} (97%) rename examples/compositor.h => include/wlr/types/wlr_compositor.h (53%) rename examples/compositor/wl_compositor.c => types/wlr_compositor.c (63%) diff --git a/examples/compositor/main.c b/examples/compositor.c similarity index 97% rename from examples/compositor/main.c rename to examples/compositor.c index 4c6e213d..54ab5d5f 100644 --- a/examples/compositor/main.c +++ b/examples/compositor.c @@ -18,17 +18,17 @@ #include #include #include +#include "wlr/types/wlr_compositor.h" #include #include #include "shared.h" -#include "compositor.h" // TODO: move to common header? int os_create_anonymous_file(off_t size); struct sample_state { struct wlr_renderer *renderer; - struct wl_compositor_state compositor; + struct wlr_compositor compositor; struct wlr_wl_shell *wl_shell; struct wlr_seat *wl_seat; struct wlr_xdg_shell_v6 *xdg_shell; @@ -153,7 +153,7 @@ int main() { exit(EXIT_FAILURE); } wl_display_init_shm(compositor.display); - wl_compositor_init(compositor.display, &state.compositor, state.renderer); + wlr_compositor_init(&state.compositor, compositor.display, state.renderer); state.wl_shell = wlr_wl_shell_create(compositor.display); state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display); state.data_device_manager = wlr_data_device_manager_create(compositor.display); @@ -185,6 +185,7 @@ int main() { wlr_data_device_manager_destroy(state.data_device_manager); wlr_xdg_shell_v6_destroy(state.xdg_shell); wlr_wl_shell_destroy(state.wl_shell); + wlr_compositor_finish(&state.compositor); wlr_renderer_destroy(state.renderer); compositor_fini(&compositor); } diff --git a/examples/meson.build b/examples/meson.build index e2c0189c..d4d96984 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -9,11 +9,6 @@ executable('touch', 'touch.c', dependencies: wlroots, link_with: lib_shared) executable('tablet', 'tablet.c', dependencies: wlroots, link_with: lib_shared) executable('output-layout', 'output-layout.c', dependencies: wlroots, link_with: lib_shared) -compositor_src = [ - 'compositor/main.c', - 'compositor/wl_compositor.c', -] - -executable('compositor', compositor_src, +executable('compositor', 'compositor.c', dependencies: wlroots, link_with: lib_shared) diff --git a/examples/compositor.h b/include/wlr/types/wlr_compositor.h similarity index 53% rename from examples/compositor.h rename to include/wlr/types/wlr_compositor.h index b382cc7f..47abeeac 100644 --- a/examples/compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -3,7 +3,7 @@ #include #include -struct wl_compositor_state { +struct wlr_compositor { struct wl_global *wl_global; struct wl_list wl_resources; struct wlr_renderer *renderer; @@ -11,10 +11,11 @@ struct wl_compositor_state { struct wl_listener destroy_surface_listener; }; -void wl_compositor_init(struct wl_display *display, - struct wl_compositor_state *state, struct wlr_renderer *renderer); +void wlr_compositor_finish(struct wlr_compositor *wlr_compositor); +void wlr_compositor_init(struct wlr_compositor *wlr_compositor, + struct wl_display *display, struct wlr_renderer *renderer); struct wlr_surface; -void wl_compositor_surface_destroyed(struct wl_compositor_state *compositor, +void wl_compositor_surface_destroyed(struct wlr_compositor *wlr_compositor, struct wlr_surface *surface); #endif diff --git a/types/meson.build b/types/meson.build index 82175f6d..83b4647c 100644 --- a/types/meson.build +++ b/types/meson.build @@ -14,6 +14,7 @@ lib_wlr_types = static_library('wlr_types', files( 'wlr_touch.c', 'wlr_xdg_shell_v6.c', 'wlr_wl_shell.c', + 'wlr_compositor.c', ), include_directories: wlr_inc, dependencies: [wayland_server, pixman, wlr_protos]) diff --git a/examples/compositor/wl_compositor.c b/types/wlr_compositor.c similarity index 63% rename from examples/compositor/wl_compositor.c rename to types/wlr_compositor.c index 0bab345d..e3b2199e 100644 --- a/examples/compositor/wl_compositor.c +++ b/types/wlr_compositor.c @@ -4,7 +4,7 @@ #include #include #include -#include "compositor.h" +#include static void destroy_surface_listener(struct wl_listener *listener, void *data) { wl_list_remove(wl_resource_get_link(data)); @@ -12,15 +12,16 @@ static void destroy_surface_listener(struct wl_listener *listener, void *data) { static void wl_compositor_create_surface(struct wl_client *client, struct wl_resource *resource, uint32_t id) { - struct wl_compositor_state *state = wl_resource_get_user_data(resource); + struct wlr_compositor *compositor = wl_resource_get_user_data(resource); struct wl_resource *surface_resource = wl_resource_create(client, &wl_surface_interface, wl_resource_get_version(resource), id); - struct wlr_surface *surface = wlr_surface_create(surface_resource, state->renderer); - surface->compositor_data = state; + struct wlr_surface *surface = wlr_surface_create(surface_resource, + compositor->renderer); + surface->compositor_data = compositor; surface->compositor_listener.notify = &destroy_surface_listener; wl_resource_add_destroy_listener(surface_resource, &surface->compositor_listener); - wl_list_insert(&state->surfaces, wl_resource_get_link(surface_resource)); + wl_list_insert(&compositor->surfaces, wl_resource_get_link(surface_resource)); } static void wl_compositor_create_region(struct wl_client *client, @@ -34,9 +35,9 @@ struct wl_compositor_interface wl_compositor_impl = { }; static void wl_compositor_destroy(struct wl_resource *resource) { - struct wl_compositor_state *state = wl_resource_get_user_data(resource); + struct wlr_compositor *compositor = wl_resource_get_user_data(resource); struct wl_resource *_resource = NULL; - wl_resource_for_each(_resource, &state->wl_resources) { + wl_resource_for_each(_resource, &compositor->wl_resources) { if (_resource == resource) { struct wl_list *link = wl_resource_get_link(_resource); wl_list_remove(link); @@ -45,10 +46,10 @@ static void wl_compositor_destroy(struct wl_resource *resource) { } } -static void wl_compositor_bind(struct wl_client *wl_client, void *_state, +static void wl_compositor_bind(struct wl_client *wl_client, void *_compositor, uint32_t version, uint32_t id) { - struct wl_compositor_state *state = _state; - assert(wl_client && state); + struct wlr_compositor *compositor = _compositor; + assert(wl_client && compositor); if (version > 4) { wlr_log(L_ERROR, "Client requested unsupported wl_compositor version, disconnecting"); wl_client_destroy(wl_client); @@ -57,16 +58,20 @@ static void wl_compositor_bind(struct wl_client *wl_client, void *_state, struct wl_resource *wl_resource = wl_resource_create( wl_client, &wl_compositor_interface, version, id); wl_resource_set_implementation(wl_resource, &wl_compositor_impl, - state, wl_compositor_destroy); - wl_list_insert(&state->wl_resources, wl_resource_get_link(wl_resource)); + compositor, wl_compositor_destroy); + wl_list_insert(&compositor->wl_resources, wl_resource_get_link(wl_resource)); } -void wl_compositor_init(struct wl_display *display, - struct wl_compositor_state *state, struct wlr_renderer *renderer) { - struct wl_global *wl_global = wl_global_create(display, - &wl_compositor_interface, 4, state, wl_compositor_bind); - state->wl_global = wl_global; - state->renderer = renderer; - wl_list_init(&state->wl_resources); - wl_list_init(&state->surfaces); +void wlr_compositor_finish(struct wlr_compositor *compositor) { + wl_global_destroy(compositor->wl_global); +} + +void wlr_compositor_init(struct wlr_compositor *compositor, + struct wl_display *display, struct wlr_renderer *renderer) { + struct wl_global *wl_global = wl_global_create(display, + &wl_compositor_interface, 4, compositor, wl_compositor_bind); + compositor->wl_global = wl_global; + compositor->renderer = renderer; + wl_list_init(&compositor->wl_resources); + wl_list_init(&compositor->surfaces); } From 1ca08dc4cb5d7b05cb82025e7c69ccabd04c3ca8 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 23:16:19 +0200 Subject: [PATCH 2/2] wlr_compositor: change _init/finish to _create/destroy --- examples/compositor.c | 8 ++++---- include/wlr/types/wlr_compositor.h | 6 +++--- types/wlr_compositor.c | 13 ++++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/compositor.c b/examples/compositor.c index 54ab5d5f..d1252994 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -28,7 +28,7 @@ int os_create_anonymous_file(off_t size); struct sample_state { struct wlr_renderer *renderer; - struct wlr_compositor compositor; + struct wlr_compositor *wlr_compositor; struct wlr_wl_shell *wl_shell; struct wlr_seat *wl_seat; struct wlr_xdg_shell_v6 *xdg_shell; @@ -95,7 +95,7 @@ static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycod struct wl_resource *res = NULL; struct wlr_seat_handle *seat_handle = NULL; - wl_list_for_each(res, &sample->compositor.surfaces, link) { + wl_list_for_each(res, &sample->wlr_compositor->surfaces, link) { break; } @@ -153,7 +153,7 @@ int main() { exit(EXIT_FAILURE); } wl_display_init_shm(compositor.display); - wlr_compositor_init(&state.compositor, compositor.display, state.renderer); + state.wlr_compositor = wlr_compositor_create(compositor.display, state.renderer); state.wl_shell = wlr_wl_shell_create(compositor.display); state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display); state.data_device_manager = wlr_data_device_manager_create(compositor.display); @@ -185,7 +185,7 @@ int main() { wlr_data_device_manager_destroy(state.data_device_manager); wlr_xdg_shell_v6_destroy(state.xdg_shell); wlr_wl_shell_destroy(state.wl_shell); - wlr_compositor_finish(&state.compositor); + wlr_compositor_destroy(state.wlr_compositor); wlr_renderer_destroy(state.renderer); compositor_fini(&compositor); } diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 47abeeac..1b2f890f 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -11,9 +11,9 @@ struct wlr_compositor { struct wl_listener destroy_surface_listener; }; -void wlr_compositor_finish(struct wlr_compositor *wlr_compositor); -void wlr_compositor_init(struct wlr_compositor *wlr_compositor, - struct wl_display *display, struct wlr_renderer *renderer); +void wlr_compositor_destroy(struct wlr_compositor *wlr_compositor); +struct wlr_compositor *wlr_compositor_create(struct wl_display *display, + struct wlr_renderer *renderer); struct wlr_surface; void wl_compositor_surface_destroyed(struct wlr_compositor *wlr_compositor, diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index e3b2199e..c153f525 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -62,16 +62,23 @@ static void wl_compositor_bind(struct wl_client *wl_client, void *_compositor, wl_list_insert(&compositor->wl_resources, wl_resource_get_link(wl_resource)); } -void wlr_compositor_finish(struct wlr_compositor *compositor) { +void wlr_compositor_destroy(struct wlr_compositor *compositor) { wl_global_destroy(compositor->wl_global); + free(compositor); } -void wlr_compositor_init(struct wlr_compositor *compositor, - struct wl_display *display, struct wlr_renderer *renderer) { +struct wlr_compositor *wlr_compositor_create(struct wl_display *display, + struct wlr_renderer *renderer) { + struct wlr_compositor *compositor = calloc(1, sizeof(struct wlr_compositor)); + if (!compositor) { + wlr_log_errno(L_ERROR, "Could not allocate wlr compositor"); + return NULL; + } struct wl_global *wl_global = wl_global_create(display, &wl_compositor_interface, 4, compositor, wl_compositor_bind); compositor->wl_global = wl_global; compositor->renderer = renderer; wl_list_init(&compositor->wl_resources); wl_list_init(&compositor->surfaces); + return compositor; }