Skip to content

Commit

Permalink
#127 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed Feb 13, 2020
1 parent 821ccf4 commit f88b9a8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 80 deletions.
70 changes: 6 additions & 64 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
105 changes: 90 additions & 15 deletions src/space_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/space_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f88b9a8

Please sign in to comment.