From 3ed650f2e65ed383fc123069f8d266787a8db567 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Wed, 18 Jun 2025 17:24:04 +0800 Subject: [PATCH] feat: ext-workspace-v1 support --- meson.build | 3 + protocols/meson.build | 1 + src/common/array.h | 73 ++ src/common/list.h | 28 + src/ext-workspace/ext-workspace-internal.h | 22 + src/ext-workspace/ext-workspace.c | 743 +++++++++++++++++++++ src/ext-workspace/ext-workspace.h | 112 ++++ src/ext-workspace/output.c | 162 +++++ src/ext-workspace/tag-worksapce.h | 138 ++++ src/ext-workspace/transaction-addon.c | 64 ++ src/ext-workspace/transaction-addon.h | 82 +++ src/maomao.c | 93 ++- 12 files changed, 1514 insertions(+), 7 deletions(-) create mode 100644 src/common/array.h create mode 100644 src/common/list.h create mode 100644 src/ext-workspace/ext-workspace-internal.h create mode 100644 src/ext-workspace/ext-workspace.c create mode 100644 src/ext-workspace/ext-workspace.h create mode 100644 src/ext-workspace/output.c create mode 100644 src/ext-workspace/tag-worksapce.h create mode 100644 src/ext-workspace/transaction-addon.c create mode 100644 src/ext-workspace/transaction-addon.h diff --git a/meson.build b/meson.build index 8e88c9f3..0dbc9092 100644 --- a/meson.build +++ b/meson.build @@ -70,6 +70,9 @@ executable('maomao', 'src/maomao.c', 'src/common/util.c', 'src/common/mem.c', + 'src/ext-workspace/ext-workspace.c', + 'src/ext-workspace/output.c', + 'src/ext-workspace/transaction-addon.c', wayland_sources, dependencies : [ libm, diff --git a/protocols/meson.build b/protocols/meson.build index 6079908f..1069157c 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -18,6 +18,7 @@ wayland_xmls = [ wl_protocol_dir + '/staging/ext-image-copy-capture/ext-image-copy-capture-v1.xml', wl_protocol_dir + '/staging/ext-image-capture-source/ext-image-capture-source-v1.xml', wl_protocol_dir + '/staging/ext-foreign-toplevel-list/ext-foreign-toplevel-list-v1.xml', + wl_protocol_dir + '/staging/ext-workspace/ext-workspace-v1.xml', 'wlr-foreign-toplevel-management-unstable-v1.xml', 'dwl-ipc-unstable-v2.xml', 'wlr-layer-shell-unstable-v1.xml', diff --git a/src/common/array.h b/src/common/array.h new file mode 100644 index 00000000..adf845a5 --- /dev/null +++ b/src/common/array.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include +#include +#include + +/* + * Wayland's wl_array API is a bit sparse consisting only of + * - init + * - release + * - add + * - copy + * - for_each + * + * The purpose of this header is the gather any generic wl_array helpers we + * create. + * + * We take the liberty of using the wl_ suffix here to make it look a bit + * prettier. If Wayland extend the API in future, we will sort the clash then. + */ + +/** + * wl_array_len() - return length of wl_array + * @array: wl_array for which to calculate length + * Note: The pointer type might not be 'char' but this is the approach that + * wl_array_for_each() takes, so we align with their style. + */ +static inline size_t wl_array_len(struct wl_array *array) { + return array->size / sizeof(const char *); +} + +/** + * Iterates in reverse over an array. + * @pos: pointer that each array element will be assigned to + * @array: wl_array to iterate over + */ +#define wl_array_for_each_reverse(pos, array) \ + for (pos = !(array)->data ? NULL \ + : (void *)((const char *)(array)->data + \ + (array)->size - sizeof(pos)); \ + pos && (const char *)pos >= (const char *)(array)->data; (pos)--) + +/** + * array_add() - add item to wl_array and exit on allocation error + * @_arr: wl_array to add the item to + * @_val: the item to add to the array + * + * Let us illustrate the function of this macro by an example: + * + * uint32_t value = 5; + * array_add(array, value); + * + * ...is the equivalent of the code below which is how you would + * otherwise use the wl_array API: + * + * uint32_t *elm = wl_array_add(array, sizeof(uint32_t)); + * if (!elm) { + * perror("failed to allocate memory"); + * exit(EXIT_FAILURE); + * } + * *elm = value; + */ +#define array_add(_arr, _val) \ + do { \ + __typeof__(_val) *_entry = \ + wl_array_add((_arr), sizeof(__typeof__(_val))); \ + if (!_entry) { \ + perror("Failed to allocate memory"); \ + exit(EXIT_FAILURE); \ + } \ + *_entry = (_val); \ + } while (0) diff --git a/src/common/list.h b/src/common/list.h new file mode 100644 index 00000000..9dfcef5e --- /dev/null +++ b/src/common/list.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include + +/** + * wl_list_append() - add a new element to the end of a list + * @list: list head to add it before + * @elm: new element to be added (link of the containing struct to be precise) + * + * Note: In labwc, most lists are queues where we want to add new elements to + * the end of the list. As wl_list_insert() adds elements at the front of the + * list (like a stack) - without this helper-function - we have to use + * wl_list_insert(list.prev, element) which is verbose and not intuitive to + * anyone new to this API. + */ +static inline void wl_list_append(struct wl_list *list, struct wl_list *elm) { + wl_list_insert(list->prev, elm); +} + +/** + * WL_LIST_INIT() - initialize a list when defining it + * @head: pointer to the head of the list to be initialized + * + * For example, this can be used like this: + * static struct wl_list list = WL_LIST_INIT(&list); + */ +#define WL_LIST_INIT(head) {.prev = (head), .next = (head)} \ No newline at end of file diff --git a/src/ext-workspace/ext-workspace-internal.h b/src/ext-workspace/ext-workspace-internal.h new file mode 100644 index 00000000..89695e75 --- /dev/null +++ b/src/ext-workspace/ext-workspace-internal.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Based on labwc (https://github.com/labwc/labwc) */ + +struct wl_resource; +struct dwl_ext_workspace_group; +struct dwl_ext_workspace_manager; + +enum pending_ext_workspaces_change { + /* group events */ + WS_PENDING_WS_CREATE = 1 << 0, + + /* ws events*/ + WS_PENDING_WS_ACTIVATE = 1 << 1, + WS_PENDING_WS_DEACTIVATE = 1 << 2, + WS_PENDING_WS_REMOVE = 1 << 3, + WS_PENDING_WS_ASSIGN = 1 << 4, +}; + +void ext_group_output_send_initial_state(struct dwl_ext_workspace_group *group, + struct wl_resource *group_resource); + +void ext_manager_schedule_done_event(struct dwl_ext_workspace_manager *manager); diff --git a/src/ext-workspace/ext-workspace.c b/src/ext-workspace/ext-workspace.c new file mode 100644 index 00000000..ec640bc3 --- /dev/null +++ b/src/ext-workspace/ext-workspace.c @@ -0,0 +1,743 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include "ext-workspace.h" +#include "../common/array.h" +#include "../common/list.h" +#include "../common/mem.h" +#include "ext-workspace-internal.h" +#include "ext-workspace-v1-protocol.h" +#include "transaction-addon.h" +#include +#include +#include + +/* + * .--------------------. + * | TODO | + * |--------------------| + * | - go through xml | + * | and verify impl | + * | - assert pub API | + * `--------------------´ + * + */ + +/* Only used within an assert() */ +#ifndef NDEBUG +#define EXT_WORKSPACE_V1_VERSION 1 +#endif + +/* + * Set when creating a new workspace state so we + * don't end up having to send the state twice. + */ +#define WS_STATE_INVALID 0xffffffff + +struct ws_create_workspace_event { + char *name; + struct { + struct wl_listener transaction_op_destroy; + } on; +}; + +/* Workspace */ +static void workspace_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void workspace_handle_activate(struct wl_client *client, + struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + /* Workspace was destroyed from the compositor side */ + return; + } + struct dwl_ext_workspace *workspace = addon->data; + dwl_transaction_op_add(addon->ctx, WS_PENDING_WS_ACTIVATE, workspace, + /*data*/ NULL); +} + +static void workspace_handle_deactivate(struct wl_client *client, + struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + /* Workspace was destroyed from the compositor side */ + return; + } + struct dwl_ext_workspace *workspace = addon->data; + dwl_transaction_op_add(addon->ctx, WS_PENDING_WS_DEACTIVATE, workspace, + /*data*/ NULL); +} + +static void workspace_handle_assign(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *new_group_resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + /* Workspace was destroyed from the compositor side */ + return; + } + struct dwl_ext_workspace *workspace = addon->data; + struct dwl_wl_resource_addon *grp_addon = + wl_resource_get_user_data(new_group_resource); + if (!grp_addon) { + /* Group was destroyed from the compositor side */ + return; + } + struct dwl_ext_workspace_group *new_grp = grp_addon->data; + dwl_transaction_op_add(addon->ctx, WS_PENDING_WS_ASSIGN, workspace, + new_grp); +} + +static void workspace_handle_remove(struct wl_client *client, + struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + /* Workspace was destroyed from the compositor side */ + return; + } + struct dwl_ext_workspace *workspace = addon->data; + dwl_transaction_op_add(addon->ctx, WS_PENDING_WS_REMOVE, workspace, + /*data*/ NULL); +} + +static const struct ext_workspace_handle_v1_interface workspace_impl = { + .destroy = workspace_handle_destroy, + .activate = workspace_handle_activate, + .deactivate = workspace_handle_deactivate, + .assign = workspace_handle_assign, + .remove = workspace_handle_remove, +}; + +static void workspace_instance_resource_destroy(struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (addon) { + dwl_resource_addon_destroy(addon); + wl_resource_set_user_data(resource, NULL); + } + + wl_list_remove(wl_resource_get_link(resource)); +} + +static struct wl_resource * +workspace_resource_create(struct dwl_ext_workspace *workspace, + struct wl_resource *manager_resource, + struct dwl_transaction_session_context *ctx) { + struct wl_client *client = wl_resource_get_client(manager_resource); + struct wl_resource *resource = + wl_resource_create(client, &ext_workspace_handle_v1_interface, + wl_resource_get_version(manager_resource), 0); + if (!resource) { + wl_client_post_no_memory(client); + return NULL; + } + + struct dwl_wl_resource_addon *addon = dwl_resource_addon_create(ctx); + addon->data = workspace; + + wl_resource_set_implementation(resource, &workspace_impl, addon, + workspace_instance_resource_destroy); + + wl_list_insert(&workspace->resources, wl_resource_get_link(resource)); + return resource; +} + +/* Workspace internal helpers */ +static void workspace_send_state(struct dwl_ext_workspace *workspace, + struct wl_resource *target) { + if (target) { + ext_workspace_handle_v1_send_state(target, workspace->state); + } else { + struct wl_resource *resource; + wl_resource_for_each(resource, &workspace->resources) { + ext_workspace_handle_v1_send_state(resource, workspace->state); + } + } +} + +static void workspace_send_initial_state(struct dwl_ext_workspace *workspace, + struct wl_resource *resource) { + ext_workspace_handle_v1_send_capabilities(resource, + workspace->capabilities); + if (workspace->coordinates.size > 0) { + ext_workspace_handle_v1_send_coordinates(resource, + &workspace->coordinates); + } + if (workspace->name) { + ext_workspace_handle_v1_send_name(resource, workspace->name); + } + if (workspace->id) { + ext_workspace_handle_v1_send_id(resource, workspace->id); + } +} + +static void workspace_set_state(struct dwl_ext_workspace *workspace, + enum ext_workspace_handle_v1_state state, + bool enabled) { + if (!!(workspace->state_pending & state) == enabled) { + return; + } + + if (enabled) { + workspace->state_pending |= state; + } else { + workspace->state_pending &= ~state; + } + ext_manager_schedule_done_event(workspace->manager); +} + +/* Group */ +static void +ws_create_workspace_handle_transaction_op_destroy(struct wl_listener *listener, + void *data) { + struct ws_create_workspace_event *ev = + wl_container_of(listener, ev, on.transaction_op_destroy); + wl_list_remove(&ev->on.transaction_op_destroy.link); + free(ev->name); + free(ev); +} + +static void group_handle_create_workspace(struct wl_client *client, + struct wl_resource *resource, + const char *name) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + return; + } + + struct dwl_ext_workspace_group *group = addon->data; + struct ws_create_workspace_event *ev = znew(*ev); + ev->name = xstrdup(name); + + struct dwl_transaction_op *transaction_op = + dwl_transaction_op_add(addon->ctx, WS_PENDING_WS_CREATE, group, ev); + + ev->on.transaction_op_destroy.notify = + ws_create_workspace_handle_transaction_op_destroy; + wl_signal_add(&transaction_op->events.destroy, + &ev->on.transaction_op_destroy); +} + +static void group_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct ext_workspace_group_handle_v1_interface group_impl = { + .create_workspace = group_handle_create_workspace, + .destroy = group_handle_destroy, +}; + +static void group_instance_resource_destroy(struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (addon) { + dwl_resource_addon_destroy(addon); + wl_resource_set_user_data(resource, NULL); + } + wl_list_remove(wl_resource_get_link(resource)); +} + +static struct wl_resource * +group_resource_create(struct dwl_ext_workspace_group *group, + struct wl_resource *manager_resource, + struct dwl_transaction_session_context *ctx) { + struct wl_client *client = wl_resource_get_client(manager_resource); + struct wl_resource *resource = + wl_resource_create(client, &ext_workspace_group_handle_v1_interface, + wl_resource_get_version(manager_resource), 0); + if (!resource) { + wl_client_post_no_memory(client); + return NULL; + } + + struct dwl_wl_resource_addon *addon = dwl_resource_addon_create(ctx); + addon->data = group; + + wl_resource_set_implementation(resource, &group_impl, addon, + group_instance_resource_destroy); + + wl_list_insert(&group->resources, wl_resource_get_link(resource)); + return resource; +} + +/* Group internal helpers */ +static void group_send_state(struct dwl_ext_workspace_group *group, + struct wl_resource *resource) { + ext_workspace_group_handle_v1_send_capabilities(resource, + group->capabilities); + + ext_group_output_send_initial_state(group, resource); +} + +/* Manager itself */ +static void manager_handle_commit(struct wl_client *client, + struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (!addon) { + return; + } + + struct dwl_ext_workspace *workspace; + struct dwl_ext_workspace_group *group; + struct dwl_transaction_op *trans_op, *trans_op_tmp; + dwl_transaction_for_each_safe(trans_op, trans_op_tmp, addon->ctx) { + switch (trans_op->change) { + case WS_PENDING_WS_CREATE: + group = trans_op->src; + struct ws_create_workspace_event *ev = trans_op->data; + wl_signal_emit_mutable(&group->events.create_workspace, ev->name); + break; + case WS_PENDING_WS_ACTIVATE: + workspace = trans_op->src; + wl_signal_emit_mutable(&workspace->events.activate, NULL); + break; + case WS_PENDING_WS_DEACTIVATE: + workspace = trans_op->src; + wl_signal_emit_mutable(&workspace->events.deactivate, NULL); + break; + case WS_PENDING_WS_REMOVE: + workspace = trans_op->src; + wl_signal_emit_mutable(&workspace->events.remove, NULL); + break; + case WS_PENDING_WS_ASSIGN: + workspace = trans_op->src; + wl_signal_emit_mutable(&workspace->events.assign, trans_op->data); + break; + default: + wlr_log(WLR_ERROR, "Invalid transaction state: %u", + trans_op->change); + } + + dwl_transaction_op_destroy(trans_op); + } +} + +static void manager_handle_stop(struct wl_client *client, + struct wl_resource *resource) { + ext_workspace_manager_v1_send_finished(resource); + wl_resource_destroy(resource); +} + +static const struct ext_workspace_manager_v1_interface manager_impl = { + .commit = manager_handle_commit, + .stop = manager_handle_stop, +}; + +static void manager_instance_resource_destroy(struct wl_resource *resource) { + struct dwl_wl_resource_addon *addon = wl_resource_get_user_data(resource); + if (addon) { + dwl_resource_addon_destroy(addon); + wl_resource_set_user_data(resource, NULL); + } + wl_list_remove(wl_resource_get_link(resource)); +} + +static void manager_handle_bind(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { + struct dwl_ext_workspace_manager *manager = data; + struct wl_resource *manager_resource = wl_resource_create( + client, &ext_workspace_manager_v1_interface, version, id); + if (!manager_resource) { + wl_client_post_no_memory(client); + return; + } + + struct dwl_wl_resource_addon *addon = + dwl_resource_addon_create(/* session context*/ NULL); + addon->data = manager; + + wl_resource_set_implementation(manager_resource, &manager_impl, addon, + manager_instance_resource_destroy); + + wl_list_insert(&manager->resources, wl_resource_get_link(manager_resource)); + + struct dwl_ext_workspace *workspace; + struct dwl_ext_workspace_group *group; + wl_list_for_each(group, &manager->groups, link) { + /* Create group resource */ + struct wl_resource *group_resource = + group_resource_create(group, manager_resource, addon->ctx); + ext_workspace_manager_v1_send_workspace_group(manager_resource, + group_resource); + group_send_state(group, group_resource); + wl_list_for_each(workspace, &manager->workspaces, link) { + if (workspace->group != group) { + continue; + } + /* Create workspace resource for the current group */ + struct wl_resource *workspace_resource = workspace_resource_create( + workspace, manager_resource, addon->ctx); + ext_workspace_manager_v1_send_workspace(manager_resource, + workspace_resource); + workspace_send_initial_state(workspace, workspace_resource); + workspace_send_state(workspace, workspace_resource); + ext_workspace_group_handle_v1_send_workspace_enter( + group_resource, workspace_resource); + } + } + + /* Create workspace resource for workspaces not belonging to any group */ + wl_list_for_each(workspace, &manager->workspaces, link) { + if (workspace->group) { + continue; + } + struct wl_resource *workspace_resource = + workspace_resource_create(workspace, manager_resource, addon->ctx); + ext_workspace_manager_v1_send_workspace(manager_resource, + workspace_resource); + workspace_send_initial_state(workspace, workspace_resource); + workspace_send_state(workspace, workspace_resource); + } + ext_workspace_manager_v1_send_done(manager_resource); +} + +static void manager_handle_display_destroy(struct wl_listener *listener, + void *data) { + struct dwl_ext_workspace_manager *manager = + wl_container_of(listener, manager, on.display_destroy); + + struct dwl_ext_workspace_group *group, *tmp; + wl_list_for_each_safe(group, tmp, &manager->groups, link) { + dwl_ext_workspace_group_destroy(group); + } + + struct dwl_ext_workspace *ws, *ws_tmp; + wl_list_for_each_safe(ws, ws_tmp, &manager->workspaces, link) { + dwl_ext_workspace_destroy(ws); + } + + if (manager->idle_source) { + wl_event_source_remove(manager->idle_source); + } + + wl_list_remove(&manager->on.display_destroy.link); + wl_global_destroy(manager->global); + free(manager); +} + +/* Manager internal helpers */ +static void manager_idle_send_done(void *data) { + struct dwl_ext_workspace_manager *manager = data; + + struct dwl_ext_workspace *workspace; + wl_list_for_each(workspace, &manager->workspaces, link) { + if (workspace->state != workspace->state_pending) { + workspace->state = workspace->state_pending; + workspace_send_state(workspace, /*target*/ NULL); + } + } + + struct wl_resource *resource; + wl_resource_for_each(resource, &manager->resources) { + ext_workspace_manager_v1_send_done(resource); + } + manager->idle_source = NULL; +} + +/* Internal API */ +void ext_manager_schedule_done_event( + struct dwl_ext_workspace_manager *manager) { + if (manager->idle_source) { + return; + } + if (!manager->event_loop) { + return; + } + manager->idle_source = wl_event_loop_add_idle( + manager->event_loop, manager_idle_send_done, manager); +} + +static void send_group_workspace_event(struct dwl_ext_workspace_group *group, + struct dwl_ext_workspace *workspace, + void (*fn)(struct wl_resource *grp_res, + struct wl_resource *ws_res)) { + struct dwl_wl_resource_addon *workspace_addon, *group_addon; + struct wl_resource *workspace_resource, *group_resource; + wl_resource_for_each(workspace_resource, &workspace->resources) { + workspace_addon = wl_resource_get_user_data(workspace_resource); + wl_resource_for_each(group_resource, &group->resources) { + group_addon = wl_resource_get_user_data(group_resource); + if (group_addon->ctx != workspace_addon->ctx) { + continue; + } + fn(group_resource, workspace_resource); + break; + } + } +} + +/* Public API */ +struct dwl_ext_workspace_manager * +dwl_ext_workspace_manager_create(struct wl_display *display, uint32_t caps, + uint32_t version) { + assert(display); + assert(version <= EXT_WORKSPACE_V1_VERSION); + + struct dwl_ext_workspace_manager *manager = znew(*manager); + manager->global = + wl_global_create(display, &ext_workspace_manager_v1_interface, version, + manager, manager_handle_bind); + + if (!manager->global) { + free(manager); + return NULL; + } + + manager->caps = caps; + manager->event_loop = wl_display_get_event_loop(display); + + manager->on.display_destroy.notify = manager_handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->on.display_destroy); + + wl_list_init(&manager->groups); + wl_list_init(&manager->workspaces); + wl_list_init(&manager->resources); + return manager; +} + +struct dwl_ext_workspace_group * +dwl_ext_workspace_group_create(struct dwl_ext_workspace_manager *manager) { + assert(manager); + + struct dwl_ext_workspace_group *group = znew(*group); + group->manager = manager; + group->capabilities = manager->caps & WS_CAP_GRP_ALL; + + wl_list_init(&group->outputs); + wl_list_init(&group->resources); + wl_signal_init(&group->events.create_workspace); + wl_signal_init(&group->events.destroy); + + wl_list_append(&manager->groups, &group->link); + + struct wl_resource *resource, *tmp; + wl_resource_for_each_safe(resource, tmp, &manager->resources) { + struct dwl_wl_resource_addon *addon = + wl_resource_get_user_data(resource); + assert(addon && addon->ctx); + struct wl_resource *group_resource = + group_resource_create(group, resource, addon->ctx); + ext_workspace_manager_v1_send_workspace_group(resource, group_resource); + group_send_state(group, group_resource); + } + ext_manager_schedule_done_event(manager); + + return group; +} + +void dwl_ext_workspace_group_destroy(struct dwl_ext_workspace_group *group) { + assert(group); + wl_signal_emit_mutable(&group->events.destroy, NULL); + + struct dwl_ext_workspace *workspace; + wl_list_for_each(workspace, &group->manager->workspaces, link) { + if (workspace->group == group) { + send_group_workspace_event( + group, workspace, + ext_workspace_group_handle_v1_send_workspace_leave); + workspace->group = NULL; + } + } + + struct wl_resource *resource, *res_tmp; + wl_resource_for_each_safe(resource, res_tmp, &group->resources) { + ext_workspace_group_handle_v1_send_removed(resource); + struct dwl_wl_resource_addon *addon = + wl_resource_get_user_data(resource); + dwl_resource_addon_destroy(addon); + wl_resource_set_user_data(resource, NULL); + wl_list_remove(wl_resource_get_link(resource)); + wl_list_init(wl_resource_get_link(resource)); + } + + /* Cancel pending transaction ops involving this group */ + struct dwl_transaction_op *trans_op, *trans_op_tmp; + wl_resource_for_each(resource, &group->manager->resources) { + struct dwl_wl_resource_addon *addon = + wl_resource_get_user_data(resource); + dwl_transaction_for_each_safe(trans_op, trans_op_tmp, addon->ctx) { + if (trans_op->src == group || trans_op->data == group) { + dwl_transaction_op_destroy(trans_op); + } + } + } + + ext_manager_schedule_done_event(group->manager); + + wl_list_remove(&group->link); + free(group); +} + +struct dwl_ext_workspace * +dwl_ext_workspace_create(struct dwl_ext_workspace_manager *manager, + const char *id) { + assert(manager); + + struct dwl_ext_workspace *workspace = znew(*workspace); + /* + * Ensures we are sending workspace->state_pending on the done event, + * regardless if the compositor has changed any state in between here + * and the scheduled done event or not. + * + * Without this we might have to send the state twice, first here and + * then again in the scheduled done event when there were any changes. + */ + workspace->state = WS_STATE_INVALID; + workspace->capabilities = (manager->caps & WS_CAP_WS_ALL) >> 16; + workspace->manager = manager; + if (id) { + workspace->id = xstrdup(id); + } + + wl_list_init(&workspace->resources); + wl_array_init(&workspace->coordinates); + wl_signal_init(&workspace->events.activate); + wl_signal_init(&workspace->events.deactivate); + wl_signal_init(&workspace->events.remove); + wl_signal_init(&workspace->events.assign); + wl_signal_init(&workspace->events.destroy); + + wl_list_append(&manager->workspaces, &workspace->link); + + /* Notify clients */ + struct dwl_wl_resource_addon *manager_addon; + struct wl_resource *manager_resource, *workspace_resource; + wl_resource_for_each(manager_resource, &manager->resources) { + manager_addon = wl_resource_get_user_data(manager_resource); + workspace_resource = workspace_resource_create( + workspace, manager_resource, manager_addon->ctx); + ext_workspace_manager_v1_send_workspace(manager_resource, + workspace_resource); + workspace_send_initial_state(workspace, workspace_resource); + } + + ext_manager_schedule_done_event(manager); + + return workspace; +} + +void dwl_ext_workspace_assign_to_group(struct dwl_ext_workspace *workspace, + struct dwl_ext_workspace_group *group) { + assert(workspace); + + if (workspace->group == group) { + return; + } + + if (workspace->group) { + /* Send leave event for the old group */ + send_group_workspace_event( + workspace->group, workspace, + ext_workspace_group_handle_v1_send_workspace_leave); + ext_manager_schedule_done_event(workspace->manager); + } + workspace->group = group; + + if (!group) { + return; + } + + /* Send enter event for the new group */ + send_group_workspace_event( + group, workspace, ext_workspace_group_handle_v1_send_workspace_enter); + ext_manager_schedule_done_event(workspace->manager); +} + +void dwl_ext_workspace_set_name(struct dwl_ext_workspace *workspace, + const char *name) { + assert(workspace); + assert(name); + + if (!workspace->name || strcmp(workspace->name, name)) { + free(workspace->name); + workspace->name = xstrdup(name); + struct wl_resource *resource; + wl_resource_for_each(resource, &workspace->resources) { + ext_workspace_handle_v1_send_name(resource, workspace->name); + } + } + ext_manager_schedule_done_event(workspace->manager); +} + +void dwl_ext_workspace_set_active(struct dwl_ext_workspace *workspace, + bool enabled) { + assert(workspace); + workspace_set_state(workspace, EXT_WORKSPACE_HANDLE_V1_STATE_ACTIVE, + enabled); +} + +void dwl_ext_workspace_set_urgent(struct dwl_ext_workspace *workspace, + bool enabled) { + assert(workspace); + workspace_set_state(workspace, EXT_WORKSPACE_HANDLE_V1_STATE_URGENT, + enabled); +} + +void dwl_ext_workspace_set_hidden(struct dwl_ext_workspace *workspace, + bool enabled) { + assert(workspace); + workspace_set_state(workspace, EXT_WORKSPACE_HANDLE_V1_STATE_HIDDEN, + enabled); +} + +void dwl_ext_workspace_set_coordinates(struct dwl_ext_workspace *workspace, + struct wl_array *coordinates) { + assert(workspace); + assert(coordinates); + + wl_array_release(&workspace->coordinates); + wl_array_init(&workspace->coordinates); + wl_array_copy(&workspace->coordinates, coordinates); + + struct wl_resource *resource; + wl_resource_for_each(resource, &workspace->resources) { + ext_workspace_handle_v1_send_coordinates(resource, + &workspace->coordinates); + } + ext_manager_schedule_done_event(workspace->manager); +} + +void dwl_ext_workspace_destroy(struct dwl_ext_workspace *workspace) { + assert(workspace); + + wl_signal_emit_mutable(&workspace->events.destroy, NULL); + + if (workspace->group) { + send_group_workspace_event( + workspace->group, workspace, + ext_workspace_group_handle_v1_send_workspace_leave); + } + + struct wl_resource *ws_res, *ws_tmp; + wl_resource_for_each_safe(ws_res, ws_tmp, &workspace->resources) { + ext_workspace_handle_v1_send_removed(ws_res); + struct dwl_wl_resource_addon *ws_addon = + wl_resource_get_user_data(ws_res); + dwl_resource_addon_destroy(ws_addon); + wl_resource_set_user_data(ws_res, NULL); + wl_list_remove(wl_resource_get_link(ws_res)); + wl_list_init(wl_resource_get_link(ws_res)); + } + ext_manager_schedule_done_event(workspace->manager); + + /* Cancel pending transaction ops involving this workspace */ + struct wl_resource *resource; + struct dwl_transaction_op *trans_op, *trans_op_tmp; + wl_resource_for_each(resource, &workspace->manager->resources) { + struct dwl_wl_resource_addon *addon = + wl_resource_get_user_data(resource); + dwl_transaction_for_each_safe(trans_op, trans_op_tmp, addon->ctx) { + if (trans_op->src == workspace) { + dwl_transaction_op_destroy(trans_op); + } + } + } + + wl_list_remove(&workspace->link); + wl_array_release(&workspace->coordinates); + zfree(workspace->id); + zfree(workspace->name); + free(workspace); +} diff --git a/src/ext-workspace/ext-workspace.h b/src/ext-workspace/ext-workspace.h new file mode 100644 index 00000000..3ebf932d --- /dev/null +++ b/src/ext-workspace/ext-workspace.h @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include +#include + +struct wlr_output; + +struct dwl_ext_workspace_manager { + struct wl_global *global; + struct wl_list groups; + struct wl_list workspaces; + uint32_t caps; + struct wl_event_source *idle_source; + struct wl_event_loop *event_loop; + + struct { + struct wl_listener display_destroy; + } on; + + struct wl_list resources; +}; + +struct dwl_ext_workspace_group { + struct dwl_ext_workspace_manager *manager; + uint32_t capabilities; + struct { + struct wl_signal create_workspace; + struct wl_signal destroy; + } events; + + struct wl_list link; + struct wl_list outputs; + struct wl_list resources; +}; + +struct dwl_ext_workspace { + struct dwl_ext_workspace_manager *manager; + struct dwl_ext_workspace_group *group; + char *id; + char *name; + struct wl_array coordinates; + uint32_t capabilities; + uint32_t state; /* enum dwl_ext_workspace_state */ + uint32_t state_pending; /* enum dwl_ext_workspace_state */ + + struct { + struct wl_signal activate; + struct wl_signal deactivate; + struct wl_signal remove; + struct wl_signal assign; + struct wl_signal destroy; + } events; + + struct wl_list link; + struct wl_list resources; +}; + +enum dwl_ext_workspace_caps { + WS_CAP_NONE = 0, + WS_CAP_GRP_ALL = 0x0000ffff, + WS_CAP_WS_ALL = 0xffff0000, + + /* group caps */ + WS_CAP_GRP_WS_CREATE = 1 << 0, + + /* workspace caps */ + WS_CAP_WS_ACTIVATE = 1 << 16, + WS_CAP_WS_DEACTIVATE = 1 << 17, + WS_CAP_WS_REMOVE = 1 << 18, + WS_CAP_WS_ASSIGN = 1 << 19, +}; + +struct dwl_ext_workspace_manager * +dwl_ext_workspace_manager_create(struct wl_display *display, uint32_t caps, + uint32_t version); + +struct dwl_ext_workspace_group * +dwl_ext_workspace_group_create(struct dwl_ext_workspace_manager *manager); + +void dwl_ext_workspace_group_output_enter(struct dwl_ext_workspace_group *group, + struct wlr_output *output); + +void dwl_ext_workspace_group_output_leave(struct dwl_ext_workspace_group *group, + struct wlr_output *output); + +void dwl_ext_workspace_group_destroy(struct dwl_ext_workspace_group *group); + +/* Create a new workspace, id may be NULL */ +struct dwl_ext_workspace * +dwl_ext_workspace_create(struct dwl_ext_workspace_manager *manager, + const char *id); + +void dwl_ext_workspace_assign_to_group(struct dwl_ext_workspace *workspace, + struct dwl_ext_workspace_group *group); + +void dwl_ext_workspace_set_name(struct dwl_ext_workspace *workspace, + const char *name); + +void dwl_ext_workspace_set_active(struct dwl_ext_workspace *workspace, + bool enabled); + +void dwl_ext_workspace_set_urgent(struct dwl_ext_workspace *workspace, + bool enabled); + +void dwl_ext_workspace_set_hidden(struct dwl_ext_workspace *workspace, + bool enabled); + +void dwl_ext_workspace_set_coordinates(struct dwl_ext_workspace *workspace, + struct wl_array *coordinates); + +void dwl_ext_workspace_destroy(struct dwl_ext_workspace *workspace); \ No newline at end of file diff --git a/src/ext-workspace/output.c b/src/ext-workspace/output.c new file mode 100644 index 00000000..b544d0d8 --- /dev/null +++ b/src/ext-workspace/output.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include "../common/mem.h" +#include "ext-workspace-internal.h" +#include "ext-workspace-v1-protocol.h" +#include "ext-workspace.h" +#include +#include +#include + +struct group_output { + struct wlr_output *wlr_output; + struct dwl_ext_workspace_group *group; + struct { + struct wl_listener group_destroy; + struct wl_listener output_bind; + struct wl_listener output_destroy; + } on; + + struct wl_list link; +}; + +/* Internal helpers */ +static void group_output_send_event( + struct wl_list *group_resources, struct wl_list *output_resources, + void (*notifier)(struct wl_resource *group, struct wl_resource *output)) { + struct wl_client *client; + struct wl_resource *group_resource, *output_resource; + wl_resource_for_each(group_resource, group_resources) { + client = wl_resource_get_client(group_resource); + wl_resource_for_each(output_resource, output_resources) { + if (wl_resource_get_client(output_resource) == client) { + notifier(group_resource, output_resource); + } + } + } +} + +static void group_output_destroy(struct group_output *group_output) { + group_output_send_event(&group_output->group->resources, + &group_output->wlr_output->resources, + ext_workspace_group_handle_v1_send_output_leave); + + ext_manager_schedule_done_event(group_output->group->manager); + + wl_list_remove(&group_output->link); + wl_list_remove(&group_output->on.group_destroy.link); + wl_list_remove(&group_output->on.output_bind.link); + wl_list_remove(&group_output->on.output_destroy.link); + free(group_output); +} + +/* Event handlers */ +static void handle_output_bind(struct wl_listener *listener, void *data) { + struct group_output *group_output = + wl_container_of(listener, group_output, on.output_bind); + + struct wlr_output_event_bind *event = data; + struct wl_client *client = wl_resource_get_client(event->resource); + + bool sent = false; + struct wl_resource *group_resource; + wl_resource_for_each(group_resource, &group_output->group->resources) { + if (wl_resource_get_client(group_resource) == client) { + ext_workspace_group_handle_v1_send_output_enter(group_resource, + event->resource); + sent = true; + } + } + if (!sent) { + return; + } + + struct wl_resource *manager_resource; + struct wl_list *manager_resources = + &group_output->group->manager->resources; + wl_resource_for_each(manager_resource, manager_resources) { + if (wl_resource_get_client(manager_resource) == client) { + ext_workspace_manager_v1_send_done(manager_resource); + } + } +} + +static void handle_output_destroy(struct wl_listener *listener, void *data) { + struct group_output *group_output = + wl_container_of(listener, group_output, on.output_destroy); + group_output_destroy(group_output); +} + +static void handle_group_destroy(struct wl_listener *listener, void *data) { + struct group_output *group_output = + wl_container_of(listener, group_output, on.group_destroy); + group_output_destroy(group_output); +} + +/* Internal API*/ +void ext_group_output_send_initial_state(struct dwl_ext_workspace_group *group, + struct wl_resource *group_resource) { + struct group_output *group_output; + struct wl_resource *output_resource; + struct wl_client *client = wl_resource_get_client(group_resource); + wl_list_for_each(group_output, &group->outputs, link) { + wl_resource_for_each(output_resource, + &group_output->wlr_output->resources) { + if (wl_resource_get_client(output_resource) == client) { + ext_workspace_group_handle_v1_send_output_enter( + group_resource, output_resource); + } + } + } +} + +/* Public API */ +void dwl_ext_workspace_group_output_enter(struct dwl_ext_workspace_group *group, + struct wlr_output *wlr_output) { + struct group_output *group_output; + wl_list_for_each(group_output, &group->outputs, link) { + if (group_output->wlr_output == wlr_output) { + return; + } + } + group_output = znew(*group_output); + group_output->wlr_output = wlr_output; + group_output->group = group; + + group_output->on.group_destroy.notify = handle_group_destroy; + wl_signal_add(&group->events.destroy, &group_output->on.group_destroy); + + group_output->on.output_bind.notify = handle_output_bind; + wl_signal_add(&wlr_output->events.bind, &group_output->on.output_bind); + + group_output->on.output_destroy.notify = handle_output_destroy; + wl_signal_add(&wlr_output->events.destroy, + &group_output->on.output_destroy); + + wl_list_insert(&group->outputs, &group_output->link); + + group_output_send_event(&group_output->group->resources, + &group_output->wlr_output->resources, + ext_workspace_group_handle_v1_send_output_enter); + + ext_manager_schedule_done_event(group->manager); +} + +void dwl_ext_workspace_group_output_leave(struct dwl_ext_workspace_group *group, + struct wlr_output *wlr_output) { + struct group_output *tmp; + struct group_output *group_output = NULL; + wl_list_for_each(tmp, &group->outputs, link) { + if (tmp->wlr_output == wlr_output) { + group_output = tmp; + break; + } + } + if (!group_output) { + wlr_log(WLR_ERROR, "output %s was never entered", wlr_output->name); + return; + } + + group_output_destroy(group_output); +} diff --git a/src/ext-workspace/tag-worksapce.h b/src/ext-workspace/tag-worksapce.h new file mode 100644 index 00000000..ced3e1f8 --- /dev/null +++ b/src/ext-workspace/tag-worksapce.h @@ -0,0 +1,138 @@ +#include "../common/list.h" +#include "../common/mem.h" +#include "ext-workspace.h" + +typedef struct Monitor Monitor; + +/* Double use: as config in config/rcxml.c and as instance in workspaces.c */ +struct workspace { + struct wl_list link; + + char *name; + unsigned int tag; + struct wlr_scene_tree *tree; + Monitor *m; + + struct dwl_ext_workspace *ext_workspace; + struct { + struct wl_listener activate; + struct wl_listener deactivate; + struct wl_listener assign; + struct wl_listener remove; + } on_ext; +}; + +/* Workspaces */ +struct dwl_ext_workspace_manager *ext_manager; +struct wl_list workspaces; /* struct workspace.link */ + +/* + * update_focus should normally be set to true. It is set to false only + * when this function is called from desktop_focus_view(), in order to + * avoid unnecessary extra focus changes and possible recursion. + */ +void workspaces_switch_to(struct workspace *target) { + if (target == target->m->workspace_current) { + return; + } + + /* Disable the old workspace */ + wlr_scene_node_set_enabled(&target->m->workspace_current->tree->node, + false); + + dwl_ext_workspace_set_active(target->m->workspace_current->ext_workspace, + false); + + unsigned int tag; + tag = 1 << (target->tag - 1); + if (target->tag == 0) { + toggleoverview(&(Arg){.i = -1}); + return; + } else { + view(&(Arg){.ui = tag}, true); + } + + /* Enable the new workspace */ + wlr_scene_node_set_enabled(&target->tree->node, true); + + /* Save the last visited workspace */ + target->m->workspace_last = target->m->workspace_current; + + /* Make sure new views will spawn on the new workspace */ + target->m->workspace_current = target; + + dwl_ext_workspace_set_active(target->ext_workspace, true); +} + +/* ext workspace handlers */ +static void handle_ext_workspace_activate(struct wl_listener *listener, + void *data) { + struct workspace *workspace = + wl_container_of(listener, workspace, on_ext.activate); + workspaces_switch_to(workspace); + wlr_log(WLR_INFO, "ext activating workspace %s", workspace->name); +} + +/* Internal API */ +static void add_workspace(int tag, Monitor *m) { + char *name = NULL; + switch (tag) { + case 1: + name = "1"; + break; + case 2: + name = "2"; + break; + case 3: + name = "3"; + break; + case 4: + name = "4"; + break; + case 5: + name = "5"; + break; + case 6: + name = "6"; + break; + case 7: + name = "7"; + break; + case 8: + name = "8"; + break; + case 9: + name = "9"; + break; + } + struct workspace *workspace = znew(*workspace); + workspace->name = xstrdup(name); + workspace->tag = tag; + workspace->m = m; + workspace->tree = wlr_scene_tree_create(ws_tree); + wl_list_append(&workspaces, &workspace->link); + if (!m->workspace_current) { + m->workspace_current = workspace; + } else { + wlr_scene_node_set_enabled(&workspace->tree->node, false); + } + + bool active = m->workspace_current == workspace; + + /* ext */ + workspace->ext_workspace = + dwl_ext_workspace_create(ext_manager, /*id*/ NULL); + dwl_ext_workspace_assign_to_group(workspace->ext_workspace, m->ext_group); + dwl_ext_workspace_set_name(workspace->ext_workspace, name); + dwl_ext_workspace_set_active(workspace->ext_workspace, active); + + workspace->on_ext.activate.notify = handle_ext_workspace_activate; + wl_signal_add(&workspace->ext_workspace->events.activate, + &workspace->on_ext.activate); +} + +void workspaces_init() { + + ext_manager = dwl_ext_workspace_manager_create(dpy, WS_CAP_WS_ACTIVATE, 1); + wl_list_init(&workspaces); +} diff --git a/src/ext-workspace/transaction-addon.c b/src/ext-workspace/transaction-addon.c new file mode 100644 index 00000000..628ddf48 --- /dev/null +++ b/src/ext-workspace/transaction-addon.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include "transaction-addon.h" +#include "../common/list.h" +#include "../common/mem.h" +#include +#include + +void dwl_transaction_op_destroy(struct dwl_transaction_op *trans_op) { + wl_signal_emit_mutable(&trans_op->events.destroy, trans_op); + wl_list_remove(&trans_op->link); + free(trans_op); +} + +static void transaction_destroy(struct wl_list *list) { + struct dwl_transaction_op *trans_op, *trans_op_tmp; + wl_list_for_each_safe(trans_op, trans_op_tmp, list, link) { + dwl_transaction_op_destroy(trans_op); + } +} + +void dwl_resource_addon_destroy(struct dwl_wl_resource_addon *addon) { + assert(addon); + assert(addon->ctx); + + addon->ctx->ref_count--; + assert(addon->ctx->ref_count >= 0); + + if (!addon->ctx->ref_count) { + transaction_destroy(&addon->ctx->transaction_ops); + free(addon->ctx); + } + + free(addon); +} + +struct dwl_wl_resource_addon * +dwl_resource_addon_create(struct dwl_transaction_session_context *ctx) { + struct dwl_wl_resource_addon *addon = znew(*addon); + if (!ctx) { + ctx = znew(*ctx); + wl_list_init(&ctx->transaction_ops); + } + addon->ctx = ctx; + addon->ctx->ref_count++; + return addon; +} + +struct dwl_transaction_op * +dwl_transaction_op_add(struct dwl_transaction_session_context *ctx, + uint32_t pending_change, void *src, void *data) { + assert(ctx); + + struct dwl_transaction_op *trans_op = znew(*trans_op); + trans_op->change = pending_change; + trans_op->src = src; + trans_op->data = data; + + wl_signal_init(&trans_op->events.destroy); + wl_list_append(&ctx->transaction_ops, &trans_op->link); + + return trans_op; +} diff --git a/src/ext-workspace/transaction-addon.h b/src/ext-workspace/transaction-addon.h new file mode 100644 index 00000000..fbfea400 --- /dev/null +++ b/src/ext-workspace/transaction-addon.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Based on labwc (https://github.com/labwc/labwc) */ + +#include + +struct dwl_transaction_op { + uint32_t change; + void *src; + void *data; + + struct { + struct wl_signal destroy; + } events; + + // Private + struct wl_list link; +}; + +struct dwl_transaction_session_context { + int ref_count; + struct wl_list transaction_ops; +}; + +struct dwl_wl_resource_addon { + struct dwl_transaction_session_context *ctx; + void *data; +}; + +/* + * Creates a new addon which can be attached to a wl_resource via + * wl_resource_set_user_data() and retrieved via wl_resource_get_user_data(). + * + * Usually the ctx argument should be addon->ctx of the parent wl_resource. + * If it is NULL it will be created automatically which can be used for top + * level wl_resources (when a client binds a wl_global from the registry). + * + * The context refcount is increased by one after this call. + */ +struct dwl_wl_resource_addon * +dwl_resource_addon_create(struct dwl_transaction_session_context *ctx); + +/* + * A generic transaction operation attached to + * a session context transaction operation list. + * + * All arguments other than the context are user defined. + * Use of an enum for pending_change is suggested. + * + * The client is responsible for eventually freeing the data + * passed in the void *src and *data arguments by listening + * to the events.destroy signal. The transaction operations can be + * looped through by using dwl_transaction_for_each(trans_op, ctx). + */ +struct dwl_transaction_op * +dwl_transaction_op_add(struct dwl_transaction_session_context *ctx, + uint32_t pending_change, void *src, void *data); + +/* + * Removes the transaction operation from the ctx list and frees it. + * + * Does *not* free any passed in src or data arguments. + * Use the events.destroy signal for that if necessary. + */ +void dwl_transaction_op_destroy(struct dwl_transaction_op *transaction_op); + +/* + * Destroys the addon. + * + * The context refcount is decreased by one. If it reaches + * zero the context will be free'd alongside the addon itself. + * If the context is destroyed all pending transaction operations + * are destroyed as well. + */ +void dwl_resource_addon_destroy(struct dwl_wl_resource_addon *addon); + +/* Convenience wrappers for looping through the pending transaction ops of a ctx + */ +#define dwl_transaction_for_each(transaction_op, ctx) \ + wl_list_for_each(transaction_op, &(ctx)->transaction_ops, link) + +#define dwl_transaction_for_each_safe(trans_op, trans_op_tmp, ctx) \ + wl_list_for_each_safe(trans_op, trans_op_tmp, &(ctx)->transaction_ops, link) diff --git a/src/maomao.c b/src/maomao.c index 6ff854df..a7378fa0 100644 --- a/src/maomao.c +++ b/src/maomao.c @@ -355,6 +355,8 @@ typedef struct { const char *name; } Layout; +struct workspace; + struct Monitor { struct wl_list link; struct wlr_output *wlr_output; @@ -386,6 +388,10 @@ struct Monitor { int gamma_lut_changed; int asleep; unsigned int visible_clients; + + struct workspace *workspace_current; + struct workspace *workspace_last; + struct dwl_ext_workspace_group *ext_group; }; typedef struct { @@ -657,7 +663,7 @@ static int hidecursor(void *data); static bool check_hit_no_border(Client *c); static void reset_keyboard_layout(void); static void client_update_oldmonname_record(Client *c, Monitor *m); - +static unsigned int get_tags_first_tag_num(unsigned int source_tags); #include "data/static_keymap.h" #include "dispatch/dispatch.h" @@ -674,6 +680,7 @@ static struct wlr_foreign_toplevel_manager_v1 *foreign_toplevel_manager; static struct wlr_backend *backend; static struct wlr_backend *headless_backend; static struct wlr_scene *scene; +struct wlr_scene_tree *ws_tree; static struct wlr_scene_tree *layers[NUM_LAYERS]; static struct wlr_renderer *drw; static struct wlr_allocator *alloc; @@ -816,6 +823,7 @@ static struct wlr_xwayland *xwayland; #include "client/client.h" #include "config/parse_config.h" +#include "ext-workspace/tag-worksapce.h" #include "layout/layout.h" #include "text_input/ime.h" @@ -2972,6 +2980,7 @@ void cleanupmon(struct wl_listener *listener, void *data) { Monitor *m = wl_container_of(listener, m, destroy); LayerSurface *l, *tmp; unsigned int i; + // struct dwl_ext_workspace *w; /* m->layers[i] are intentionally not unlinked */ for (i = 0; i < LENGTH(m->layers); i++) { @@ -2979,6 +2988,10 @@ void cleanupmon(struct wl_listener *listener, void *data) { wlr_layer_surface_v1_destroy(l->layer_surface); } + // clean ext-workspaces grouplab + dwl_ext_workspace_group_output_leave(m->ext_group, m->wlr_output); + dwl_ext_workspace_group_destroy(m->ext_group); + wl_list_remove(&m->destroy.link); wl_list_remove(&m->frame.link); wl_list_remove(&m->link); @@ -3461,8 +3474,6 @@ void createmon(struct wl_listener *listener, void *data) { } } - printstatus(); - /* The xdg-protocol specifies: * * If the fullscreened surface is not opaque, the compositor must make @@ -3486,6 +3497,14 @@ void createmon(struct wl_listener *listener, void *data) { wlr_output_layout_add_auto(output_layout, wlr_output); else wlr_output_layout_add(output_layout, wlr_output, m->m.x, m->m.y); + m->ext_group = dwl_ext_workspace_group_create(ext_manager); + dwl_ext_workspace_group_output_enter(m->ext_group, m->wlr_output); + + for (i = 1; i <= LENGTH(tags); i++) { + add_workspace(i, m); + } + + printstatus(); } void // fix for 0.5 @@ -5070,14 +5089,49 @@ void pointerfocus(Client *c, struct wlr_surface *surface, double sx, double sy, wlr_seat_pointer_notify_motion(seat, time, sx, sy); } -void // 17 -printstatus(void) { - Monitor *m = NULL; +void printstatus(void) { + Monitor *m; + unsigned int current_tag; + struct workspace *w; + Client *c; + bool has_clients = false; + bool is_active = false; + wl_list_for_each(m, &mons, link) { if (!m->wlr_output->enabled) { continue; } - dwl_ipc_output_printstatus(m); // 更新waybar上tag的状态 这里很关键 + + // Get current tag for this monitor + current_tag = get_tags_first_tag_num(m->tagset[m->seltags]); + // Update workspace active states + if (workspaces.next != &workspaces) { // If workspaces list is not empty + wl_list_for_each(w, &workspaces, link) { + if (w && w->m == m) { + is_active = (w->tag == current_tag) && !m->isoverview; + has_clients = false; + wl_list_for_each(c, &clients, link) { + if (c->tags & 1 << (w->tag - 1) & TAGMASK) { + has_clients = true; + break; + } + } + if (is_active) { + dwl_ext_workspace_set_active(w->ext_workspace, + is_active); + } else if (has_clients) { + dwl_ext_workspace_set_hidden(w->ext_workspace, false); + dwl_ext_workspace_set_active(w->ext_workspace, false); + } else { + dwl_ext_workspace_set_active(w->ext_workspace, false); + dwl_ext_workspace_set_hidden(w->ext_workspace, true); + } + } + } + } + + // Update IPC output status + dwl_ipc_output_printstatus(m); } } @@ -6243,6 +6297,27 @@ unsigned int get_tags_first_tag(unsigned int source_tags) { } } +unsigned int get_tags_first_tag_num(unsigned int source_tags) { + unsigned int i, tag; + tag = 0; + + if (!source_tags) { + return selmon->pertag->curtag; + } + + for (i = 0; !(tag & 1) && source_tags != 0 && i < LENGTH(tags); i++) { + tag = source_tags >> i; + } + + if (i == 1) { + return 1; + } else if (i > 9) { + return 9; + } else { + return i; + } +} + void show_hide_client(Client *c) { unsigned int target = get_tags_first_tag(c->oldtags); tag_client(&(Arg){.ui = target}, c); @@ -6411,6 +6486,8 @@ void setup(void) { drag_icon = wlr_scene_tree_create(&scene->tree); wlr_scene_node_place_below(&drag_icon->node, &layers[LyrBlock]->node); + ws_tree = wlr_scene_tree_create(&scene->tree); + /* Create a renderer with the default implementation */ if (!(drw = wlr_renderer_autocreate(backend))) die("couldn't create renderer"); @@ -6620,6 +6697,8 @@ void setup(void) { wlr_xdg_foreign_registry_create(dpy); wlr_xdg_foreign_v1_create(dpy, foreign_registry); wlr_xdg_foreign_v2_create(dpy, foreign_registry); + + workspaces_init(); #ifdef XWAYLAND /* * Initialise the XWayland X server.