Skip to content

Commit

Permalink
#1678 #1635 add signals for space_created and space_destroyed. Remove…
Browse files Browse the repository at this point in the history
… assigned label when a space is destroyed.
  • Loading branch information
koekeishiya committed Aug 12, 2023
1 parent e44e1a6 commit a51af04
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]
### Changed
- Allow window swap commands to work on windows that are in the same stack, using the stack window selectors [#960](https://github.com/koekeishiya/yabai/issues/960)
- Properly remove assigned label when a space is destroyed [#1678](https://github.com/koekeishiya/yabai/issues/1678)
- Add signals for `space_created` and `space_destroyed` [#1635](https://github.com/koekeishiya/yabai/issues/1365)

## [5.0.6] - 2023-05-27
### Changed
Expand Down
18 changes: 16 additions & 2 deletions doc/yabai.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: yabai
.\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 2.0.18
.\" Date: 2023-05-27
.\" Date: 2023-08-12
.\" Manual: Yabai Manual
.\" Source: Yabai
.\" Language: English
.\"
.TH "YABAI" "1" "2023-05-27" "Yabai" "Yabai Manual"
.TH "YABAI" "1" "2023-08-12" "Yabai" "Yabai Manual"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.ss \n[.ss] 0
Expand Down Expand Up @@ -1004,6 +1004,20 @@ Eligible for \fBapp\fP, \fBtitle\fP and \fBactive\fP filter.
Passes one argument: $YABAI_WINDOW_ID
.RE
.sp
\fBspace_created\fP
.RS 4
Triggered when a space is created.
.br
Passes one argument: $YABAI_SPACE_ID
.RE
.sp
\fBspace_destroyed\fP
.RS 4
Triggered when a space is destroyed.
.br
Passes one argument: $YABAI_SPACE_ID
.RE
.sp
\fBspace_changed\fP
.RS 4
Triggered when the active space has changed.
Expand Down
8 changes: 8 additions & 0 deletions doc/yabai.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,14 @@ EVENT
Eligible for *app*, *title* and *active* filter. +
Passes one argument: $YABAI_WINDOW_ID

*space_created*::
Triggered when a space is created. +
Passes one argument: $YABAI_SPACE_ID

*space_destroyed*::
Triggered when a space is destroyed. +
Passes one argument: $YABAI_SPACE_ID

*space_changed*::
Triggered when the active space has changed. +
Passes two arguments: $YABAI_SPACE_ID, $YABAI_RECENT_SPACE_ID
Expand Down
81 changes: 79 additions & 2 deletions src/event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,81 @@ static EVENT_HANDLER(SLS_WINDOW_IS_INVISIBLE)
}
}

static EVENT_HANDLER(SLS_SPACE_ADD_WINDOW)
{
uint32_t window_id = (uint32_t) param1;
struct window *window = window_manager_find_window(&g_window_manager, window_id);
if (!window) return;

if (!__sync_bool_compare_and_swap(&window->id_ptr, &window->id, &window->id)) {
debug("%s: %d has been marked invalid by the system, ignoring event..\n", __FUNCTION__, window_id);
return;
}

uint64_t sid = (uint64_t)(intptr_t) context;
debug("%s: %d added to %lld\n", __FUNCTION__, window_id, sid);

if (window_manager_should_manage_window(window) && !window_manager_find_managed_window(&g_window_manager, window)) {
struct view *view = space_manager_tile_window_on_space(&g_space_manager, window, sid);
window_manager_add_managed_window(&g_window_manager, window, view);
}

// event_signal_push(SIGNAL_SPACE_ADD_WINDOW, context);
}

static EVENT_HANDLER(SLS_SPACE_REMOVE_WINDOW)
{
uint32_t window_id = (uint32_t) param1;
struct window *window = window_manager_find_window(&g_window_manager, window_id);
if (!window) return;

if (!__sync_bool_compare_and_swap(&window->id_ptr, &window->id, &window->id)) {
debug("%s: %d has been marked invalid by the system, ignoring event..\n", __FUNCTION__, window_id);
return;
}

uint64_t sid = (uint64_t)(intptr_t) context;
debug("%s: %d removed from %lld\n", __FUNCTION__, window_id, sid);

struct view *view = window_manager_find_managed_window(&g_window_manager, window);
if (view) {
struct window_node *node = view_remove_window_node(view, window);
window_manager_remove_managed_window(&g_window_manager, window->id);

if (node) {
if (space_is_visible(view->sid)) {
window_node_flush(node);
} else {
view->is_dirty = true;
}
}
}

if (window_manager_should_manage_window(window)) {
struct view *view = space_manager_tile_window_on_space(&g_space_manager, window, window_space(window));
window_manager_add_managed_window(&g_window_manager, window, view);
}

// event_signal_push(SIGNAL_SPACE_REMOVE_WINDOW, context);
}

static EVENT_HANDLER(SLS_SPACE_CREATED)
{
uint64_t sid = (uint64_t)(intptr_t) context;
debug("%s: %lld\n", __FUNCTION__, sid);
event_signal_push(SIGNAL_SPACE_CREATED, context);
}

static EVENT_HANDLER(SLS_SPACE_DESTROYED)
{
uint64_t sid = (uint64_t)(intptr_t) context;
debug("%s: %lld\n", __FUNCTION__, sid);

space_manager_remove_label_for_space(&g_space_manager, sid);

event_signal_push(SIGNAL_SPACE_DESTROYED, context);
}

static EVENT_HANDLER(SPACE_CHANGED)
{
g_space_manager.last_space_id = g_space_manager.current_space_id;
Expand Down Expand Up @@ -1283,8 +1358,10 @@ static EVENT_HANDLER(MISSION_CONTROL_EXIT)
SLSOrderWindow(g_connection, feedback_wid, 1, 0);
}

if (g_mission_control_active == 1 || g_mission_control_active == 2) {
window_manager_correct_for_mission_control_changes(&g_space_manager, &g_window_manager);
if (!workspace_is_macos_ventura()) {
if (g_mission_control_active == 1 || g_mission_control_active == 2) {
window_manager_correct_for_mission_control_changes(&g_space_manager, &g_window_manager);
}
}

event_signal_push(SIGNAL_MISSION_CONTROL_EXIT, NULL);
Expand Down
4 changes: 4 additions & 0 deletions src/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
EVENT_TYPE_ENTRY(SLS_WINDOW_ORDER_CHANGED) \
EVENT_TYPE_ENTRY(SLS_WINDOW_IS_VISIBLE) \
EVENT_TYPE_ENTRY(SLS_WINDOW_IS_INVISIBLE) \
EVENT_TYPE_ENTRY(SLS_SPACE_ADD_WINDOW) \
EVENT_TYPE_ENTRY(SLS_SPACE_REMOVE_WINDOW) \
EVENT_TYPE_ENTRY(SLS_SPACE_CREATED) \
EVENT_TYPE_ENTRY(SLS_SPACE_DESTROYED) \
EVENT_TYPE_ENTRY(SPACE_CHANGED) \
EVENT_TYPE_ENTRY(DISPLAY_ADDED) \
EVENT_TYPE_ENTRY(DISPLAY_REMOVED) \
Expand Down
10 changes: 10 additions & 0 deletions src/event_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ void event_signal_push(enum signal_type type, void *context)
es->title = window_title_ts(window);
es->active = g_window_manager.focused_window_id == window->id;
} break;
case SIGNAL_SPACE_CREATED:
case SIGNAL_SPACE_DESTROYED: {
uint64_t sid = (uint64_t)(uintptr_t) context;

es->arg_name[0] = ts_alloc_unaligned(arg_size);
es->arg_value[0] = ts_alloc_unaligned(arg_size);

snprintf(es->arg_name[0], arg_size, "%s", "YABAI_SPACE_ID");
snprintf(es->arg_value[0], arg_size, "%lld", sid);
} break;
case SIGNAL_SPACE_CHANGED: {
es->arg_name[0] = ts_alloc_unaligned(arg_size);
es->arg_value[0] = ts_alloc_unaligned(arg_size);
Expand Down
4 changes: 4 additions & 0 deletions src/event_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum signal_type
SIGNAL_WINDOW_DEMINIMIZED,
SIGNAL_WINDOW_TITLE_CHANGED,

SIGNAL_SPACE_CREATED,
SIGNAL_SPACE_DESTROYED,
SIGNAL_SPACE_CHANGED,

SIGNAL_DISPLAY_ADDED,
Expand Down Expand Up @@ -61,6 +63,8 @@ static const char *signal_type_str[] =
[SIGNAL_WINDOW_DEMINIMIZED] = "window_deminimized",
[SIGNAL_WINDOW_TITLE_CHANGED] = "window_title_changed",

[SIGNAL_SPACE_CREATED] = "space_created",
[SIGNAL_SPACE_DESTROYED] = "space_destroyed",
[SIGNAL_SPACE_CHANGED] = "space_changed",

[SIGNAL_DISPLAY_ADDED] = "display_added",
Expand Down
15 changes: 15 additions & 0 deletions src/yabai.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ static CONNECTION_CALLBACK(connection_handler)
event_loop_post(&g_event_loop, SLS_WINDOW_IS_VISIBLE, (void *) (intptr_t) (* (uint32_t *) data), 0);
} else if (type == 816) {
event_loop_post(&g_event_loop, SLS_WINDOW_IS_INVISIBLE, (void *) (intptr_t) (* (uint32_t *) data), 0);
} else if (type == 1325) {
event_loop_post(&g_event_loop, SLS_SPACE_ADD_WINDOW, (void *) (intptr_t) (* (uint64_t *) data), (int) (* (uint32_t *) (data + 8)));
} else if (type == 1326) {
event_loop_post(&g_event_loop, SLS_SPACE_REMOVE_WINDOW, (void *) (intptr_t) (* (uint64_t *) data), (int) (* (uint32_t *) (data + 8)));
} else if (type == 1327) {
event_loop_post(&g_event_loop, SLS_SPACE_CREATED, (void *) (intptr_t) (* (uint64_t *) data), 0);
} else if (type == 1328) {
event_loop_post(&g_event_loop, SLS_SPACE_DESTROYED, (void *) (intptr_t) (* (uint64_t *) data), 0);
}
}

Expand Down Expand Up @@ -347,6 +355,13 @@ int main(int argc, char **argv)
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 815, NULL);
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 816, NULL);

if (workspace_is_macos_ventura()) {
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1325, NULL);
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1326, NULL);
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1327, NULL);
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1328, NULL);
}

window_manager_init(&g_window_manager);
space_manager_begin(&g_space_manager);
window_manager_begin(&g_space_manager, &g_window_manager);
Expand Down

0 comments on commit a51af04

Please sign in to comment.