From f88b9a812ee7690657be14db4580bde95c83b3d1 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Thu, 13 Feb 2020 21:22:49 +0100 Subject: [PATCH] #127 cleanup --- src/message.c | 70 +++-------------------------- src/space_manager.c | 105 +++++++++++++++++++++++++++++++++++++------- src/space_manager.h | 5 ++- 3 files changed, 100 insertions(+), 80 deletions(-) diff --git a/src/message.c b/src/message.c index 297db09f..26402cc6 100644 --- a/src/message.c +++ b/src/message.c @@ -1300,79 +1300,21 @@ static void handle_domain_space(FILE *rsp, struct token domain, char *message) } else if (token_equals(command, COMMAND_SPACE_MOVE)) { struct selector selector = parse_space_selector(rsp, &message, acting_sid); if (selector.did_parse && selector.sid) { - uint32_t acting_did = space_display_id(acting_sid); - uint32_t selector_did = space_display_id(selector.sid); - - if (acting_sid == selector.sid) { + enum space_op_error result = space_manager_move_space_to_space(acting_sid, selector.sid); + if (result == SPACE_OP_ERROR_SAME_SPACE) { daemon_fail(rsp, "cannot move space to itself.\n"); - } else if (acting_did != selector_did) { + } else if (result == SPACE_OP_ERROR_SAME_DISPLAY) { daemon_fail(rsp, "cannot move space across display boundaries. use --display instead.\n"); - } else { - uint64_t acting_prev_sid = space_manager_prev_space(acting_sid); - uint64_t selector_prev_sid = space_manager_prev_space(selector.sid); - - uint32_t acting_prev_did = acting_prev_sid ? space_display_id(acting_prev_sid) : 0; - uint32_t selector_prev_did = selector_prev_sid ? space_display_id(selector_prev_sid) : 0; - - bool acting_sid_is_first = !acting_prev_sid || acting_prev_did != acting_did; - bool selector_sid_is_first = !selector_prev_sid || selector_prev_did != selector_did; - - if (acting_sid_is_first && !selector_sid_is_first) { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - } else if (!acting_sid_is_first && selector_sid_is_first) { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - space_manager_move_space_after_space(selector.sid, acting_sid, false); - } else if (!acting_sid_is_first && !selector_sid_is_first) { - if (space_manager_mission_control_index(acting_sid) > space_manager_mission_control_index(selector.sid)) { - space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); - } else { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - } - } } } } else if (token_equals(command, COMMAND_SPACE_SWAP)) { struct selector selector = parse_space_selector(rsp, &message, acting_sid); if (selector.did_parse && selector.sid) { - uint32_t acting_did = space_display_id(acting_sid); - uint32_t selector_did = space_display_id(selector.sid); - - if (acting_sid == selector.sid) { + enum space_op_error result = space_manager_swap_space_with_space(acting_sid, selector.sid); + if (result == SPACE_OP_ERROR_SAME_SPACE) { daemon_fail(rsp, "cannot swap space with itself.\n"); - } else if (acting_did != selector_did) { + } else if (result == SPACE_OP_ERROR_SAME_DISPLAY) { daemon_fail(rsp, "cannot swap space across display boundaries. use --display instead.\n"); - } else { - uint64_t acting_prev_sid = space_manager_prev_space(acting_sid); - uint64_t selector_prev_sid = space_manager_prev_space(selector.sid); - - uint32_t acting_prev_did = acting_prev_sid ? space_display_id(acting_prev_sid) : 0; - uint32_t selector_prev_did = selector_prev_sid ? space_display_id(selector_prev_sid) : 0; - - bool acting_sid_is_first = !acting_prev_sid || acting_prev_did != acting_did; - bool selector_sid_is_first = !selector_prev_sid || selector_prev_did != selector_did; - - int acting_mci = space_manager_mission_control_index(acting_sid); - int selector_mci = space_manager_mission_control_index(selector.sid); - - if (acting_sid_is_first && !selector_sid_is_first && selector_mci - acting_mci == 1) { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - } else if (!acting_sid_is_first && selector_sid_is_first && acting_mci - selector_mci == 1) { - space_manager_move_space_after_space(selector.sid, acting_sid, selector.sid == space_manager_active_space()); - } else if (acting_sid_is_first && !selector_sid_is_first) { - space_manager_move_space_after_space(selector.sid, acting_sid, false); - space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); - } else if (!acting_sid_is_first && selector_sid_is_first) { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - space_manager_move_space_after_space(selector.sid, acting_prev_sid, false); - } else if (!acting_sid_is_first && !selector_sid_is_first) { - if (acting_mci > selector_mci) { - space_manager_move_space_after_space(selector.sid, acting_sid, false); - space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); - } else { - space_manager_move_space_after_space(acting_sid, selector.sid, acting_sid == space_manager_active_space()); - space_manager_move_space_after_space(selector.sid, acting_prev_sid, false); - } - } } } } else if (token_equals(command, COMMAND_SPACE_DISPLAY)) { diff --git a/src/space_manager.c b/src/space_manager.c index aa082f7b..eb57516d 100644 --- a/src/space_manager.c +++ b/src/space_manager.c @@ -616,7 +616,29 @@ void space_manager_focus_space(uint64_t sid) socket_close(sockfd); } -void space_manager_move_space_after_space(uint64_t src_sid, uint64_t dst_sid, bool focus) +static inline bool space_manager_is_space_last_user_space(uint64_t sid) +{ + bool result = true; + + int count; + uint64_t *space_list = display_space_list(space_display_id(sid), &count); + if (!space_list) return true; + + for (int i = 0; i < count; ++i) { + uint64_t c_sid = space_list[i]; + if (sid == c_sid) continue; + + if (space_is_user(c_sid)) { + result = false; + break; + } + } + free(space_list); + + return result; +} + +static void space_manager_move_space_after_space(uint64_t src_sid, uint64_t dst_sid, bool focus) { int sockfd; char message[MAXLEN]; @@ -632,27 +654,80 @@ void space_manager_move_space_after_space(uint64_t src_sid, uint64_t dst_sid, bo socket_close(sockfd); } -static inline bool -space_manager_is_space_last_user_space(uint64_t sid) +enum space_op_error space_manager_swap_space_with_space(uint64_t acting_sid, uint64_t selector_sid) { - bool result = true; + uint32_t acting_did = space_display_id(acting_sid); + uint32_t selector_did = space_display_id(selector_sid); - int count; - uint64_t *space_list = display_space_list(space_display_id(sid), &count); - if (!space_list) return true; + if (acting_sid == selector_sid) return SPACE_OP_ERROR_SAME_SPACE; + if (acting_did != selector_did) return SPACE_OP_ERROR_SAME_DISPLAY; - for (int i = 0; i < count; ++i) { - uint64_t c_sid = space_list[i]; - if (sid == c_sid) continue; + uint64_t acting_prev_sid = space_manager_prev_space(acting_sid); + uint64_t selector_prev_sid = space_manager_prev_space(selector_sid); - if (space_is_user(c_sid)) { - result = false; - break; + uint32_t acting_prev_did = acting_prev_sid ? space_display_id(acting_prev_sid) : 0; + uint32_t selector_prev_did = selector_prev_sid ? space_display_id(selector_prev_sid) : 0; + + bool acting_sid_is_first = !acting_prev_sid || acting_prev_did != acting_did; + bool selector_sid_is_first = !selector_prev_sid || selector_prev_did != selector_did; + + int acting_mci = space_manager_mission_control_index(acting_sid); + int selector_mci = space_manager_mission_control_index(selector_sid); + + if (acting_sid_is_first && !selector_sid_is_first && selector_mci - acting_mci == 1) { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + } else if (!acting_sid_is_first && selector_sid_is_first && acting_mci - selector_mci == 1) { + space_manager_move_space_after_space(selector_sid, acting_sid, selector_sid == space_manager_active_space()); + } else if (acting_sid_is_first && !selector_sid_is_first) { + space_manager_move_space_after_space(selector_sid, acting_sid, false); + space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); + } else if (!acting_sid_is_first && selector_sid_is_first) { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + space_manager_move_space_after_space(selector_sid, acting_prev_sid, false); + } else if (!acting_sid_is_first && !selector_sid_is_first) { + if (acting_mci > selector_mci) { + space_manager_move_space_after_space(selector_sid, acting_sid, false); + space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); + } else { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + space_manager_move_space_after_space(selector_sid, acting_prev_sid, false); } } - free(space_list); - return result; + return SPACE_OP_ERROR_SUCCESS; +} + +enum space_op_error space_manager_move_space_to_space(uint64_t acting_sid, uint64_t selector_sid) +{ + uint32_t acting_did = space_display_id(acting_sid); + uint32_t selector_did = space_display_id(selector_sid); + + if (acting_sid == selector_sid) return SPACE_OP_ERROR_SAME_SPACE; + if (acting_did != selector_did) return SPACE_OP_ERROR_SAME_DISPLAY; + + uint64_t acting_prev_sid = space_manager_prev_space(acting_sid); + uint64_t selector_prev_sid = space_manager_prev_space(selector_sid); + + uint32_t acting_prev_did = acting_prev_sid ? space_display_id(acting_prev_sid) : 0; + uint32_t selector_prev_did = selector_prev_sid ? space_display_id(selector_prev_sid) : 0; + + bool acting_sid_is_first = !acting_prev_sid || acting_prev_did != acting_did; + bool selector_sid_is_first = !selector_prev_sid || selector_prev_did != selector_did; + + if (acting_sid_is_first && !selector_sid_is_first) { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + } else if (!acting_sid_is_first && selector_sid_is_first) { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + space_manager_move_space_after_space(selector_sid, acting_sid, false); + } else if (!acting_sid_is_first && !selector_sid_is_first) { + if (space_manager_mission_control_index(acting_sid) > space_manager_mission_control_index(selector_sid)) { + space_manager_move_space_after_space(acting_sid, selector_prev_sid, acting_sid == space_manager_active_space()); + } else { + space_manager_move_space_after_space(acting_sid, selector_sid, acting_sid == space_manager_active_space()); + } + } + + return SPACE_OP_ERROR_SUCCESS; } enum space_op_error space_manager_move_space_to_display(struct space_manager *sm, uint64_t sid, uint32_t did) diff --git a/src/space_manager.h b/src/space_manager.h index 7ee9ece9..f37ff01c 100644 --- a/src/space_manager.h +++ b/src/space_manager.h @@ -40,6 +40,8 @@ enum space_op_error SPACE_OP_ERROR_INVALID_SRC = 3, SPACE_OP_ERROR_INVALID_DST = 4, SPACE_OP_ERROR_INVALID_TYPE = 5, + SPACE_OP_ERROR_SAME_SPACE = 6, + SPACE_OP_ERROR_SAME_DISPLAY = 7, }; bool space_manager_has_separate_spaces(void); @@ -84,7 +86,8 @@ void space_manager_rotate_space(struct space_manager *sm, uint64_t sid, int degr void space_manager_mirror_space(struct space_manager *sm, uint64_t sid, enum window_node_split axis); void space_manager_move_window_to_space(uint64_t sid, struct window *window); void space_manager_focus_space(uint64_t sid); -void space_manager_move_space_after_space(uint64_t src_sid, uint64_t dst_sid, bool focus); +enum space_op_error space_manager_swap_space_with_space(uint64_t acting_sid, uint64_t selector_sid); +enum space_op_error space_manager_move_space_to_space(uint64_t acting_sid, uint64_t selector_sid); enum space_op_error space_manager_move_space_to_display(struct space_manager *sm, uint64_t sid, uint32_t did); enum space_op_error space_manager_destroy_space(uint64_t sid); void space_manager_add_space(uint64_t sid);