2018-07-29 21:27:34 +10:00
|
|
|
#include <stdbool.h>
|
2018-02-23 10:03:46 +11:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include "sway/commands.h"
|
2018-04-28 11:26:14 +10:00
|
|
|
#include "sway/tree/arrange.h"
|
2018-03-30 14:41:33 +11:00
|
|
|
#include "sway/tree/container.h"
|
2018-02-23 10:03:46 +11:00
|
|
|
#include "log.h"
|
|
|
|
|
2018-07-29 21:27:34 +10:00
|
|
|
static bool parse_layout_string(char *s, enum sway_container_layout *ptr) {
|
|
|
|
if (strcasecmp(s, "splith") == 0) {
|
|
|
|
*ptr = L_HORIZ;
|
|
|
|
} else if (strcasecmp(s, "splitv") == 0) {
|
|
|
|
*ptr = L_VERT;
|
|
|
|
} else if (strcasecmp(s, "tabbed") == 0) {
|
|
|
|
*ptr = L_TABBED;
|
|
|
|
} else if (strcasecmp(s, "stacking") == 0) {
|
|
|
|
*ptr = L_STACKED;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* expected_syntax =
|
|
|
|
"Expected 'layout default|tabbed|stacking|splitv|splith' or "
|
|
|
|
"'layout toggle [split|all]' or "
|
|
|
|
"'layout toggle [split|tabbed|stacking|splitv|splith] [split|tabbed|stacking|splitv|splith]...'";
|
|
|
|
|
2018-02-23 10:03:46 +11:00
|
|
|
struct cmd_results *cmd_layout(int argc, char **argv) {
|
|
|
|
struct cmd_results *error = NULL;
|
|
|
|
if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) {
|
|
|
|
return error;
|
|
|
|
}
|
2018-03-30 14:41:33 +11:00
|
|
|
struct sway_container *parent = config->handler_context.current_container;
|
2018-02-23 10:03:46 +11:00
|
|
|
|
2018-05-25 09:26:23 +10:00
|
|
|
if (container_is_floating(parent)) {
|
|
|
|
return cmd_results_new(CMD_FAILURE, "layout",
|
|
|
|
"Unable to change layout of floating windows");
|
2018-02-23 10:03:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
while (parent->type == C_VIEW) {
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:03:37 +10:00
|
|
|
enum sway_container_layout prev = parent->layout;
|
|
|
|
bool assigned_directly = parse_layout_string(argv[0], &parent->layout);
|
|
|
|
if (!assigned_directly) {
|
|
|
|
if (strcasecmp(argv[0], "default") == 0) {
|
|
|
|
parent->layout = parent->prev_split_layout;
|
|
|
|
} else if (strcasecmp(argv[0], "toggle") == 0) {
|
2018-07-29 21:27:34 +10:00
|
|
|
if (argc == 1) {
|
|
|
|
parent->layout =
|
|
|
|
parent->layout == L_STACKED ? L_TABBED :
|
2018-08-02 01:03:37 +10:00
|
|
|
parent->layout == L_TABBED ? parent->prev_split_layout : L_STACKED;
|
2018-07-29 21:27:34 +10:00
|
|
|
} else if (argc == 2) {
|
|
|
|
if (strcasecmp(argv[1], "all") == 0) {
|
|
|
|
parent->layout =
|
|
|
|
parent->layout == L_HORIZ ? L_VERT :
|
|
|
|
parent->layout == L_VERT ? L_STACKED :
|
|
|
|
parent->layout == L_STACKED ? L_TABBED : L_HORIZ;
|
|
|
|
} else if (strcasecmp(argv[1], "split") == 0) {
|
2018-08-02 01:03:37 +10:00
|
|
|
parent->layout =
|
|
|
|
parent->layout == L_HORIZ ? L_VERT :
|
|
|
|
parent->layout == L_VERT ? L_HORIZ : parent->prev_split_layout;
|
2018-07-29 21:27:34 +10:00
|
|
|
} else {
|
|
|
|
return cmd_results_new(CMD_INVALID, "layout", expected_syntax);
|
|
|
|
}
|
2018-02-23 10:03:46 +11:00
|
|
|
} else {
|
2018-07-29 21:27:34 +10:00
|
|
|
enum sway_container_layout parsed_layout;
|
|
|
|
int curr = 1;
|
|
|
|
for (; curr < argc; curr++) {
|
2018-08-02 01:03:37 +10:00
|
|
|
bool valid = parse_layout_string(argv[curr], &parsed_layout);
|
|
|
|
if ((valid && parsed_layout == parent->layout) ||
|
|
|
|
(strcmp(argv[curr], "split") == 0 &&
|
|
|
|
(parent->layout == L_VERT || parent->layout == L_HORIZ))) {
|
2018-07-29 21:27:34 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = curr + 1; i != curr; ++i) {
|
|
|
|
// cycle round to find next valid layout
|
|
|
|
if (i >= argc) {
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
if (parse_layout_string(argv[i], &parent->layout)) {
|
|
|
|
break;
|
2018-08-02 01:03:37 +10:00
|
|
|
} else if (strcmp(argv[i], "split") == 0) {
|
|
|
|
parent->layout =
|
|
|
|
parent->layout == L_HORIZ ? L_VERT :
|
|
|
|
parent->layout == L_VERT ? L_HORIZ : parent->prev_split_layout;
|
|
|
|
break;
|
2018-07-29 21:27:34 +10:00
|
|
|
} // invalid layout strings are silently ignored
|
|
|
|
}
|
2018-02-23 10:03:46 +11:00
|
|
|
}
|
2018-07-29 21:27:34 +10:00
|
|
|
} else {
|
|
|
|
return cmd_results_new(CMD_INVALID, "layout", expected_syntax);
|
2018-02-23 10:03:46 +11:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 21:27:34 +10:00
|
|
|
if (parent->layout == L_NONE) {
|
|
|
|
parent->layout = container_get_default_layout(parent);
|
|
|
|
}
|
2018-08-02 01:03:37 +10:00
|
|
|
if (prev != parent->layout) {
|
|
|
|
if (prev != L_TABBED && prev != L_STACKED) {
|
|
|
|
parent->prev_split_layout = prev;
|
|
|
|
}
|
|
|
|
container_notify_subtree_changed(parent);
|
|
|
|
arrange_windows(parent);
|
|
|
|
}
|
2018-02-23 10:03:46 +11:00
|
|
|
|
|
|
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
}
|