From 589a6f528ae31b5753812498e175b080f1ce4ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 25 Aug 2021 09:38:26 +0200 Subject: [PATCH] module/foreign-toplevel: track outputs each toplevel is mapped on MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Bind the foreign-toplevel-manager object *after* the first round of global objects. This ensures we bind all pre-existing wl-output objects before binding the toplevel manager. This is important, since otherwise we wont get any output_enter() events for the initial set of toplevels. * Bind xdg-output-manager, to be able to bind xdg-output objects for each wl-output. * Add xdg-output-listener to each wl/xdg-output, to be able to get the outputs’ names. * Add a list of outputs to each toplevel. The output_enter() event adds to this list, and output_leave() removes from it. * Add option ‘all-monitors’. When not set (the default), toplevels are only included in the generated content if they are mapped on the same output as the bar itself. When *not* set, all toplevels are always included in the generated content. --- doc/yambar-modules-foreign-toplevel.5.scd | 5 + modules/foreign-toplevel.c | 328 ++++++++++++++++++++-- modules/meson.build | 2 +- 3 files changed, 312 insertions(+), 23 deletions(-) diff --git a/doc/yambar-modules-foreign-toplevel.5.scd b/doc/yambar-modules-foreign-toplevel.5.scd index bb63024..6a65ec3 100644 --- a/doc/yambar-modules-foreign-toplevel.5.scd +++ b/doc/yambar-modules-foreign-toplevel.5.scd @@ -52,6 +52,11 @@ Note: Wayland only. : particle : yes : Template particle that will be instantiated once for each window +| all-monitors +: bool +: no +: When set to true, only windows on the same monitor the bar will be + used. The default is false. # EXAMPLES diff --git a/modules/foreign-toplevel.c b/modules/foreign-toplevel.c index dafc60f..fbe9342 100644 --- a/modules/foreign-toplevel.c +++ b/modules/foreign-toplevel.c @@ -15,6 +15,22 @@ #include "../particles/dynlist.h" #include "wlr-foreign-toplevel-management-unstable-v1.h" +#include "xdg-output-unstable-v1.h" + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static const int required_manager_interface_version = 2; + +struct output { + struct module *mod; + + uint32_t wl_name; + struct wl_output *wl_output; + struct zxdg_output_v1 *xdg_output; + + char *name; + bool use_output_release; +}; struct toplevel { struct module *mod; @@ -27,14 +43,35 @@ struct toplevel { bool minimized; bool activated; bool fullscreen; + + tll(const struct output *) outputs; }; struct private { struct particle *template; + uint32_t manager_wl_name; struct zwlr_foreign_toplevel_manager_v1 *manager; + struct zxdg_output_manager_v1 *xdg_output_manager; + + bool all_monitors; tll(struct toplevel) toplevels; + tll(struct output) outputs; }; +static void +output_free(struct output *output) +{ + free(output->name); + if (output->xdg_output != NULL) + zxdg_output_v1_destroy(output->xdg_output); + if (output->wl_output != NULL) { + if (output->use_output_release) + wl_output_release(output->wl_output); + else + wl_output_destroy(output->wl_output); + } +} + static void toplevel_free(struct toplevel *top) { @@ -43,6 +80,7 @@ toplevel_free(struct toplevel *top) free(top->app_id); free(top->title); + tll_free(top->outputs); } static void @@ -51,10 +89,8 @@ destroy(struct module *mod) struct private *m = mod->private; m->template->destroy(m->template); - tll_foreach(m->toplevels, it) { - toplevel_free(&it->item); - tll_remove(m->toplevels, it); - } + assert(tll_length(m->toplevels) == 0); + assert(tll_length(m->outputs) == 0); free(m); module_default_destroy(mod); @@ -73,11 +109,34 @@ content(struct module *mod) mtx_lock(&mod->lock); - size_t toplevel_count = tll_length(m->toplevels); + const size_t toplevel_count = tll_length(m->toplevels); + size_t show_count = 0; struct exposable *toplevels[toplevel_count]; - size_t idx = 0; + const char *current_output = mod->bar->output_name(mod->bar); + tll_foreach(m->toplevels, it) { + const struct toplevel *top = &it->item; + + bool show = false; + + if (m->all_monitors) + show = true; + else if (current_output != NULL) { + tll_foreach(top->outputs, it2) { + const struct output *output = it2->item; + if (output->name != NULL && + strcmp(output->name, current_output) == 0) + { + show = true; + break; + } + } + } + + if (!show) + continue; + struct tag_set tags = { .tags = (struct tag *[]){ tag_new_string(mod, "app-id", it->item.app_id), @@ -90,12 +149,12 @@ content(struct module *mod) .count = 6, }; - toplevels[idx++] = m->template->instantiate(m->template, &tags); + toplevels[show_count++] = m->template->instantiate(m->template, &tags); tag_set_destroy(&tags); } mtx_unlock(&mod->lock); - return dynlist_exposable_new(toplevels, toplevel_count, 0, 0); + return dynlist_exposable_new(toplevels, show_count, 0, 0); } static bool @@ -110,6 +169,53 @@ verify_iface_version(const char *iface, uint32_t version, uint32_t wanted) return false; } +static void +xdg_output_handle_logical_position(void *data, + struct zxdg_output_v1 *xdg_output, + int32_t x, int32_t y) +{ +} + +static void +xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output, + int32_t width, int32_t height) +{ +} + +static void +xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output) +{ +} + +static void +xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output, + const char *name) +{ + struct output *output = data; + struct module *mod = output->mod; + + mtx_lock(&mod->lock); + { + free(output->name); + output->name = name != NULL ? strdup(name) : NULL; + } + mtx_unlock(&mod->lock); +} + +static void +xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output, + const char *description) +{ +} + +static struct zxdg_output_v1_listener xdg_output_listener = { + .logical_position = xdg_output_handle_logical_position, + .logical_size = xdg_output_handle_logical_size, + .done = xdg_output_handle_done, + .name = xdg_output_handle_name, + .description = xdg_output_handle_description, +}; + static void title(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, const char *title) { @@ -137,17 +243,88 @@ app_id(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, const char *a } static void -output_enter(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, struct wl_output *output) +output_enter(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, + struct wl_output *wl_output) { + struct toplevel *top = data; + struct module *mod = top->mod; + struct private *m = mod->private; + + mtx_lock(&mod->lock); + + const struct output *output = NULL; + tll_foreach(m->outputs, it) { + if (it->item.wl_output == wl_output) { + output = &it->item; + break; + } + } + + if (output == NULL) { + LOG_ERR("output-enter event on untracked output"); + goto out; + } + + tll_foreach(top->outputs, it) { + if (it->item == output) { + LOG_ERR("output-enter event on output we're already on"); + goto out; + } + } + + LOG_DBG("mapped: %s:%s on %s", top->app_id, top->title, output->name); + tll_push_back(top->outputs, output); + +out: + mtx_unlock(&mod->lock); } static void -output_leave(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, struct wl_output *output) +output_leave(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, + struct wl_output *wl_output) { + struct toplevel *top = data; + struct module *mod = top->mod; + struct private *m = mod->private; + + mtx_lock(&mod->lock); + + const struct output *output = NULL; + tll_foreach(m->outputs, it) { + if (it->item.wl_output == wl_output) { + output = &it->item; + break; + } + } + + if (output == NULL) { + LOG_ERR("output-leave event on untracked output"); + goto out; + } + + bool output_removed = false; + tll_foreach(top->outputs, it) { + if (it->item == output) { + LOG_DBG("unmapped: %s:%s from %s", + top->app_id, top->title, output->name); + tll_remove(top->outputs, it); + output_removed = true; + break; + } + } + + if (!output_removed) { + LOG_ERR("output-leave event on an output we're not on"); + goto out; + } + +out: + mtx_unlock(&mod->lock); } static void -state(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, struct wl_array *states) +state(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle, + struct wl_array *states) { struct toplevel *top = data; @@ -188,8 +365,10 @@ static void closed(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle) { struct toplevel *top = data; - struct private *m = top->mod->private; + struct module *mod = top->mod; + struct private *m = mod->private; + mtx_lock(&mod->lock); tll_foreach(m->toplevels, it) { if (it->item.handle == handle) { toplevel_free(top); @@ -197,6 +376,7 @@ closed(void *data, struct zwlr_foreign_toplevel_handle_v1 *handle) break; } } + mtx_unlock(&mod->lock); } static void @@ -230,10 +410,14 @@ toplevel(void *data, .handle = handle, }; - tll_push_back(m->toplevels, toplevel); + mtx_lock(&mod->lock); + { + tll_push_back(m->toplevels, toplevel); - zwlr_foreign_toplevel_handle_v1_add_listener( - handle, &toplevel_listener, &tll_back(m->toplevels)); + zwlr_foreign_toplevel_handle_v1_add_listener( + handle, &toplevel_listener, &tll_back(m->toplevels)); + } + mtx_unlock(&mod->lock); } static void @@ -253,6 +437,22 @@ static const struct zwlr_foreign_toplevel_manager_v1_listener manager_listener = .finished = &finished, }; +static void +output_xdg_output(struct output *output) +{ + struct private *m = output->mod->private; + + if (m->xdg_output_manager == NULL) + return; + if (output->xdg_output != NULL) + return; + + output->xdg_output = zxdg_output_manager_v1_get_xdg_output( + m->xdg_output_manager, output->wl_output); + zxdg_output_v1_add_listener( + output->xdg_output, &xdg_output_listener, output); +} + static void handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) @@ -261,22 +461,79 @@ handle_global(void *data, struct wl_registry *registry, struct private *m = mod->private; if (strcmp(interface, zwlr_foreign_toplevel_manager_v1_interface.name) == 0) { + if (!verify_iface_version(interface, version, required_manager_interface_version)) + return; + + m->manager_wl_name = name; + } + + else if (strcmp(interface, wl_output_interface.name) == 0) { + const uint32_t required = 1; + if (!verify_iface_version(interface, version, required)) + return; + + struct output output = { + .mod = mod, + .wl_name = name, + .wl_output = wl_registry_bind( + registry, name, + &wl_output_interface, min(version, WL_OUTPUT_RELEASE_SINCE_VERSION)), + .use_output_release = version >= WL_OUTPUT_RELEASE_SINCE_VERSION, + }; + + mtx_lock(&mod->lock); + tll_push_back(m->outputs, output); + output_xdg_output(&tll_back(m->outputs)); + mtx_unlock(&mod->lock); + } + + else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) { const uint32_t required = 2; if (!verify_iface_version(interface, version, required)) return; - m->manager = wl_registry_bind( - registry, name, - &zwlr_foreign_toplevel_manager_v1_interface, required); + m->xdg_output_manager = wl_registry_bind( + registry, name, &zxdg_output_manager_v1_interface, required); - zwlr_foreign_toplevel_manager_v1_add_listener( - m->manager, &manager_listener, mod); + mtx_lock(&mod->lock); + tll_foreach(m->outputs, it) + output_xdg_output(&it->item); + mtx_unlock(&mod->lock); } } static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { + struct module *mod = data; + struct private *m = mod->private; + + mtx_lock(&mod->lock); + + tll_foreach(m->outputs, it) { + const struct output *output = &it->item; + if (output->wl_name == name) { + + /* Loop all toplevels */ + tll_foreach(m->toplevels, it2) { + + /* And remove this output from their list of tracked + * outputs */ + tll_foreach(it2->item.outputs, it3) { + if (it3->item == output) { + tll_remove(it2->item.outputs, it3); + break; + } + } + } + + tll_remove(m->outputs, it); + goto out; + } + } + +out: + mtx_unlock(&mod->lock); } static const struct wl_registry_listener registry_listener = { @@ -307,6 +564,20 @@ run(struct module *mod) wl_display_roundtrip(display); + if (m->manager_wl_name == 0) { + LOG_ERR( + "compositor does not implement the foreign-toplevel-manager interface"); + goto out; + } + + m->manager = wl_registry_bind( + registry, m->manager_wl_name, + &zwlr_foreign_toplevel_manager_v1_interface, + required_manager_interface_version); + + zwlr_foreign_toplevel_manager_v1_add_listener( + m->manager, &manager_listener, mod); + while (true) { wl_display_flush(display); @@ -344,6 +615,13 @@ out: tll_remove(m->toplevels, it); } + tll_foreach(m->outputs, it) { + output_free(&it->item); + tll_remove(m->outputs, it); + } + + if (m->xdg_output_manager != NULL) + zxdg_output_manager_v1_destroy(m->xdg_output_manager); if (m->manager != NULL) zwlr_foreign_toplevel_manager_v1_destroy(m->manager); if (registry != NULL) @@ -354,10 +632,11 @@ out: } static struct module * -ftop_new(struct particle *label) +ftop_new(struct particle *label, bool all_monitors) { struct private *m = calloc(1, sizeof(*m)); m->template = label; + m->all_monitors = all_monitors; struct module *mod = module_common_new(); mod->private = m; @@ -372,13 +651,18 @@ static struct module * from_conf(const struct yml_node *node, struct conf_inherit inherited) { const struct yml_node *c = yml_get_value(node, "content"); - return ftop_new(conf_to_particle(c, inherited)); + const struct yml_node *all_monitors = yml_get_value(node, "all-monitors"); + + return ftop_new( + conf_to_particle(c, inherited), + all_monitors != NULL ? yml_value_as_bool(all_monitors) : false); } static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { + {"all-monitors", false, &conf_verify_bool}, MODULE_COMMON_ATTRS, }; diff --git a/modules/meson.build b/modules/meson.build index 23111ae..dbaa7a7 100644 --- a/modules/meson.build +++ b/modules/meson.build @@ -72,7 +72,7 @@ if backend_wayland endforeach mod_data += { - 'foreign-toplevel': [[ftop_proto_headers + ftop_proto_src], [dynlist]], + 'foreign-toplevel': [[wl_proto_src + wl_proto_headers + ftop_proto_headers + ftop_proto_src], [dynlist]], } endif