mirror of
https://github.com/swaywm/sway.git
synced 2026-05-01 06:46:25 -04:00
Merge a13133a591 into aca9f6b2a2
This commit is contained in:
commit
60bcf879d7
1 changed files with 248 additions and 160 deletions
232
sway/commands.c
232
sway/commands.c
|
|
@ -129,6 +129,11 @@ static sway_cmd bar_colors_cmd_urgent_workspace;
|
||||||
|
|
||||||
static struct cmd_results *add_color(const char*, char*, const char*);
|
static struct cmd_results *add_color(const char*, char*, const char*);
|
||||||
|
|
||||||
|
static struct cmd_results *find_and_execute_handler(int argc, char **argv,
|
||||||
|
const struct cmd_handler *handlers,
|
||||||
|
size_t handlers_size,
|
||||||
|
const char *expected_syntax);
|
||||||
|
|
||||||
swayc_t *sp_view;
|
swayc_t *sp_view;
|
||||||
int sp_index = 0;
|
int sp_index = 0;
|
||||||
|
|
||||||
|
|
@ -1006,41 +1011,47 @@ static struct cmd_results *cmd_mouse_warping(int argc, char **argv) {
|
||||||
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct cmd_results *cmd_move(int argc, char **argv) {
|
static const char *const cmd_move_expected_syntax =
|
||||||
|
"Expected 'move <left|right|up|down>' or "
|
||||||
|
"'move <container|window> to workspace <name>' or "
|
||||||
|
"'move <container|window|workspace> to output <name|direction>' or"
|
||||||
|
"'move position mouse'";
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_left(int argc, char **argv) {
|
||||||
|
move_container(get_focused_container(&root_container), MOVE_LEFT);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_right(int argc, char **argv) {
|
||||||
|
move_container(get_focused_container(&root_container), MOVE_RIGHT);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_up(int argc, char **argv) {
|
||||||
|
move_container(get_focused_container(&root_container), MOVE_UP);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_down(int argc, char **argv) {
|
||||||
|
move_container(get_focused_container(&root_container), MOVE_DOWN);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_container_to_workspace(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if (config->reading) return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file.");
|
if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 1))) {
|
||||||
if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
|
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
const char* expected_syntax = "Expected 'move <left|right|up|down>' or "
|
|
||||||
"'move <container|window> to workspace <name>' or "
|
|
||||||
"'move <container|window|workspace> to output <name|direction>' or "
|
|
||||||
"'move position mouse'";
|
|
||||||
swayc_t *view = get_focused_container(&root_container);
|
swayc_t *view = get_focused_container(&root_container);
|
||||||
|
|
||||||
if (strcasecmp(argv[0], "left") == 0) {
|
|
||||||
move_container(view, MOVE_LEFT);
|
|
||||||
} else if (strcasecmp(argv[0], "right") == 0) {
|
|
||||||
move_container(view, MOVE_RIGHT);
|
|
||||||
} else if (strcasecmp(argv[0], "up") == 0) {
|
|
||||||
move_container(view, MOVE_UP);
|
|
||||||
} else if (strcasecmp(argv[0], "down") == 0) {
|
|
||||||
move_container(view, MOVE_DOWN);
|
|
||||||
} else if (strcasecmp(argv[0], "container") == 0 || strcasecmp(argv[0], "window") == 0) {
|
|
||||||
// "move container ...
|
|
||||||
if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 4))) {
|
|
||||||
return error;
|
|
||||||
} else if ( strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "workspace") == 0) {
|
|
||||||
// move container to workspace x
|
|
||||||
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
||||||
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
|
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *ws_name = argv[3];
|
const char *ws_name = argv[0];
|
||||||
swayc_t *ws;
|
swayc_t *ws;
|
||||||
if (argc == 5 && strcasecmp(ws_name, "number") == 0) {
|
if (argc == 2 && strcasecmp(ws_name, "number") == 0) {
|
||||||
// move "container to workspace number x"
|
// move "container to workspace number x"
|
||||||
ws_name = argv[4];
|
ws_name = argv[1];
|
||||||
ws = workspace_by_number(ws_name);
|
ws = workspace_by_number(ws_name);
|
||||||
} else {
|
} else {
|
||||||
ws = workspace_by_name(ws_name);
|
ws = workspace_by_name(ws_name);
|
||||||
|
|
@ -1050,16 +1061,27 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
ws = workspace_create(ws_name);
|
ws = workspace_create(ws_name);
|
||||||
}
|
}
|
||||||
move_container_to(view, get_focused_container(ws));
|
move_container_to(view, get_focused_container(ws));
|
||||||
} else if (strcasecmp(argv[1], "to") == 0 && strcasecmp(argv[2], "output") == 0) {
|
|
||||||
// move container to output x
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_container_to_output(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 1))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
swayc_t *view = get_focused_container(&root_container);
|
||||||
|
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
|
||||||
|
}
|
||||||
|
|
||||||
swayc_t *output = NULL;
|
swayc_t *output = NULL;
|
||||||
struct wlc_point abs_pos;
|
struct wlc_point abs_pos;
|
||||||
get_absolute_center_position(view, &abs_pos);
|
get_absolute_center_position(view, &abs_pos);
|
||||||
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
if (!(output = output_by_name(argv[0], &abs_pos))) {
|
||||||
return cmd_results_new(CMD_FAILURE, "move", "Can only move containers and views.");
|
|
||||||
} else if (!(output = output_by_name(argv[3], &abs_pos))) {
|
|
||||||
return cmd_results_new(CMD_FAILURE, "move",
|
return cmd_results_new(CMD_FAILURE, "move",
|
||||||
"Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
|
"Can't find output with name/direction '%s' @ (%i,%i)", argv[0], abs_pos.x, abs_pos.y);
|
||||||
} else {
|
} else {
|
||||||
swayc_t *container = get_focused_container(output);
|
swayc_t *container = get_focused_container(output);
|
||||||
if (container->is_floating) {
|
if (container->is_floating) {
|
||||||
|
|
@ -1068,21 +1090,46 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
move_container_to(view, container);
|
move_container_to(view, container);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return cmd_results_new(CMD_INVALID, "move", expected_syntax);
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_container(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "move container/window", EXPECTED_AT_LEAST, 2))) {
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
} else if (strcasecmp(argv[0], "workspace") == 0) {
|
|
||||||
// move workspace (to output x)
|
if (strcasecmp(argv[0], "to")) {
|
||||||
|
return cmd_results_new(CMD_INVALID, "move", cmd_move_expected_syntax);
|
||||||
|
}
|
||||||
|
/* Skip the 'to' part of the command */
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
|
static const struct cmd_handler handlers[] = {
|
||||||
|
{"output", cmd_move_container_to_output},
|
||||||
|
{"workspace", cmd_move_container_to_workspace},
|
||||||
|
};
|
||||||
|
|
||||||
|
return find_and_execute_handler(argc, argv, handlers, sizeof(handlers) / sizeof(handlers[0]), cmd_move_expected_syntax);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_workspace(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 3))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
swayc_t *output = NULL;
|
swayc_t *output = NULL;
|
||||||
struct wlc_point abs_pos;
|
struct wlc_point abs_pos;
|
||||||
|
swayc_t *view = get_focused_container(&root_container);
|
||||||
get_absolute_center_position(view, &abs_pos);
|
get_absolute_center_position(view, &abs_pos);
|
||||||
if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 4))) {
|
if (strcasecmp(argv[0], "to") != 0 || strcasecmp(argv[1], "output") != 0) {
|
||||||
return error;
|
return cmd_results_new(CMD_INVALID, "move", cmd_move_expected_syntax);
|
||||||
} else if (strcasecmp(argv[1], "to") != 0 || strcasecmp(argv[2], "output") != 0) {
|
} else if (!(output = output_by_name(argv[2], &abs_pos))) {
|
||||||
return cmd_results_new(CMD_INVALID, "move", expected_syntax);
|
|
||||||
} else if (!(output = output_by_name(argv[3], &abs_pos))) {
|
|
||||||
return cmd_results_new(CMD_FAILURE, "move workspace",
|
return cmd_results_new(CMD_FAILURE, "move workspace",
|
||||||
"Can't find output with name/direction '%s' @ (%i,%i)", argv[3], abs_pos.x, abs_pos.y);
|
"Can't find output with name/direction '%s' @ (%i,%i)", argv[2], abs_pos.x, abs_pos.y);
|
||||||
}
|
}
|
||||||
if (view->type == C_WORKSPACE) {
|
if (view->type == C_WORKSPACE) {
|
||||||
// This probably means we're moving an empty workspace, but
|
// This probably means we're moving an empty workspace, but
|
||||||
|
|
@ -1092,12 +1139,15 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE);
|
swayc_t *workspace = swayc_parent_by_type(view, C_WORKSPACE);
|
||||||
move_workspace_to(workspace, output);
|
move_workspace_to(workspace, output);
|
||||||
}
|
}
|
||||||
} else if (strcasecmp(argv[0], "scratchpad") == 0) {
|
|
||||||
// move scratchpad ...
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_scratchpad(int argc, char **argv) {
|
||||||
|
swayc_t *view = get_focused_container(&root_container);
|
||||||
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
if (view->type != C_CONTAINER && view->type != C_VIEW) {
|
||||||
return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
|
return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views.");
|
||||||
}
|
}
|
||||||
swayc_t *view = get_focused_container(&root_container);
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < scratchpad->length; i++) {
|
for (i = 0; i < scratchpad->length; i++) {
|
||||||
if (scratchpad->items[i] == view) {
|
if (scratchpad->items[i] == view) {
|
||||||
|
|
@ -1119,14 +1169,20 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
focused = swayc_active_workspace();
|
focused = swayc_active_workspace();
|
||||||
}
|
}
|
||||||
set_focused_container(focused);
|
set_focused_container(focused);
|
||||||
} else if (strcasecmp(argv[0], "position") == 0) {
|
|
||||||
if ((error = checkarg(argc, "move workspace", EXPECTED_EQUAL_TO, 2))) {
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move_position(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "move container/window", EXPECTED_EQUAL_TO, 1))) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (strcasecmp(argv[1], "mouse")) {
|
if (strcasecmp(argv[0], "mouse")) {
|
||||||
return cmd_results_new(CMD_INVALID, "move", expected_syntax);
|
return cmd_results_new(CMD_FAILURE, "move position", "Can only move to mouse.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
swayc_t *view = get_focused_container(&root_container);
|
||||||
if (view->is_floating) {
|
if (view->is_floating) {
|
||||||
swayc_t *output = swayc_parent_by_type(view, C_OUTPUT);
|
swayc_t *output = swayc_parent_by_type(view, C_OUTPUT);
|
||||||
struct wlc_geometry g;
|
struct wlc_geometry g;
|
||||||
|
|
@ -1147,12 +1203,34 @@ static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
|
|
||||||
wlc_view_set_geometry(view->handle, 0, &g);
|
wlc_view_set_geometry(view->handle, 0, &g);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return cmd_results_new(CMD_INVALID, "move", expected_syntax);
|
|
||||||
}
|
|
||||||
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct cmd_results *cmd_move(int argc, char **argv) {
|
||||||
|
if (config->reading) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "move", "Can't be used in config file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "move", EXPECTED_AT_LEAST, 1))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct cmd_handler handlers[] = {
|
||||||
|
{"container", cmd_move_container},
|
||||||
|
{"down", cmd_move_down},
|
||||||
|
{"left", cmd_move_left},
|
||||||
|
{"position", cmd_move_position},
|
||||||
|
{"right", cmd_move_right},
|
||||||
|
{"scratchpad", cmd_move_scratchpad},
|
||||||
|
{"up", cmd_move_up},
|
||||||
|
{"window", cmd_move_container},
|
||||||
|
{"workspace", cmd_move_workspace},
|
||||||
|
};
|
||||||
|
|
||||||
|
return find_and_execute_handler(argc, argv, handlers, sizeof(handlers) / sizeof(handlers[0]), cmd_move_expected_syntax);
|
||||||
|
}
|
||||||
|
|
||||||
static struct cmd_results *cmd_new_float(int argc, char **argv) {
|
static struct cmd_results *cmd_new_float(int argc, char **argv) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
if ((error = checkarg(argc, "new_float", EXPECTED_AT_LEAST, 1))) {
|
if ((error = checkarg(argc, "new_float", EXPECTED_AT_LEAST, 1))) {
|
||||||
|
|
@ -3497,28 +3575,38 @@ static int handler_compare(const void *_a, const void *_b) {
|
||||||
return strcasecmp(a->command, b->command);
|
return strcasecmp(a->command, b->command);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
|
static struct cmd_handler *__find_handler(char *line, const struct cmd_handler *handlers, size_t handlers_size) {
|
||||||
struct cmd_handler d = { .command=line };
|
struct cmd_handler d = {.command = line};
|
||||||
struct cmd_handler *res = NULL;
|
return bsearch(&d, handlers, handlers_size, sizeof(struct cmd_handler), handler_compare);
|
||||||
sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT);
|
}
|
||||||
if (block == CMD_BLOCK_BAR) {
|
|
||||||
res = bsearch(&d, bar_handlers,
|
static struct cmd_results *find_and_execute_handler(int argc, char **argv,
|
||||||
sizeof(bar_handlers) / sizeof(struct cmd_handler),
|
const struct cmd_handler *handlers,
|
||||||
sizeof(struct cmd_handler), handler_compare);
|
size_t handlers_size,
|
||||||
} else if (block == CMD_BLOCK_BAR_COLORS){
|
const char *expected_syntax) {
|
||||||
res = bsearch(&d, bar_colors_handlers,
|
char *command = argv[0];
|
||||||
sizeof(bar_colors_handlers) / sizeof(struct cmd_handler),
|
struct cmd_handler *handler = __find_handler(command, handlers, handlers_size);
|
||||||
sizeof(struct cmd_handler), handler_compare);
|
if (!handler) {
|
||||||
} else if (block == CMD_BLOCK_INPUT) {
|
return cmd_results_new(CMD_INVALID, command, expected_syntax);
|
||||||
sway_log(L_DEBUG, "lookng at input handlers");
|
|
||||||
res = bsearch(&d, input_handlers,
|
|
||||||
sizeof(input_handlers) / sizeof(struct cmd_handler),
|
|
||||||
sizeof(struct cmd_handler), handler_compare);
|
|
||||||
} else {
|
|
||||||
res = bsearch(&d, handlers,
|
|
||||||
sizeof(handlers) / sizeof(struct cmd_handler),
|
|
||||||
sizeof(struct cmd_handler), handler_compare);
|
|
||||||
}
|
}
|
||||||
|
return handler->handle(argc - 1, argv + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
|
||||||
|
struct cmd_handler *res = NULL;
|
||||||
|
|
||||||
|
sway_log(L_DEBUG, "find_handler(%s) %d", line, block);
|
||||||
|
|
||||||
|
if (block == CMD_BLOCK_BAR) {
|
||||||
|
res = __find_handler(line, bar_handlers, sizeof(bar_handlers) / sizeof(bar_handlers[0]));
|
||||||
|
} else if (block == CMD_BLOCK_BAR_COLORS) {
|
||||||
|
res = __find_handler(line, bar_colors_handlers, sizeof(bar_colors_handlers) / sizeof(bar_colors_handlers[0]));
|
||||||
|
} else if (block == CMD_BLOCK_INPUT) {
|
||||||
|
res = __find_handler(line, input_handlers, sizeof(input_handlers) / sizeof(input_handlers[0]));
|
||||||
|
} else {
|
||||||
|
res = __find_handler(line, handlers, sizeof(handlers) / sizeof(handlers[0]));
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue