sway-patched-tray-menu/sway/commands.c

525 lines
15 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <json.h>
#include "sway/commands.h"
2017-12-05 10:40:55 +01:00
#include "sway/config.h"
2018-01-20 22:21:45 +01:00
#include "sway/criteria.h"
2017-12-11 10:17:14 +01:00
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/tree/view.h"
#include "stringop.h"
#include "log.h"
// Returns error object, or NULL if check succeeds.
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
const char *error_name = NULL;
switch (type) {
case EXPECTED_AT_LEAST:
if (argc < val) {
error_name = "at least ";
}
break;
case EXPECTED_AT_MOST:
if (argc > val) {
error_name = "at most ";
}
break;
case EXPECTED_EQUAL_TO:
if (argc != val) {
error_name = "";
}
}
return error_name ?
cmd_results_new(CMD_INVALID, "Invalid %s command "
"(expected %s%d argument%s, got %d)",
name, error_name, val, val != 1 ? "s" : "", argc)
: NULL;
}
2018-03-29 23:20:03 +02:00
/* Keep alphabetized */
static const struct cmd_handler handlers[] = {
{ "assign", cmd_assign },
2018-03-29 23:20:03 +02:00
{ "bar", cmd_bar },
2017-12-27 16:08:18 +01:00
{ "bindcode", cmd_bindcode },
{ "bindswitch", cmd_bindswitch },
2017-12-27 16:08:18 +01:00
{ "bindsym", cmd_bindsym },
{ "client.background", cmd_client_noop },
{ "client.focused", cmd_client_focused },
{ "client.focused_inactive", cmd_client_focused_inactive },
{ "client.placeholder", cmd_client_noop },
{ "client.unfocused", cmd_client_unfocused },
{ "client.urgent", cmd_client_urgent },
2018-05-01 13:46:28 +02:00
{ "default_border", cmd_default_border },
{ "default_floating_border", cmd_default_floating_border },
2017-12-04 22:43:49 +01:00
{ "exec", cmd_exec },
{ "exec_always", cmd_exec_always },
{ "floating_maximum_size", cmd_floating_maximum_size },
{ "floating_minimum_size", cmd_floating_minimum_size },
{ "floating_modifier", cmd_floating_modifier },
2018-07-19 08:41:02 +02:00
{ "focus", cmd_focus },
{ "focus_follows_mouse", cmd_focus_follows_mouse },
{ "focus_on_window_activation", cmd_focus_on_window_activation },
2018-05-28 05:20:21 +02:00
{ "focus_wrapping", cmd_focus_wrapping },
2018-05-02 15:07:52 +02:00
{ "font", cmd_font },
2018-05-03 15:24:13 +02:00
{ "for_window", cmd_for_window },
{ "force_display_urgency_hint", cmd_force_display_urgency_hint },
{ "force_focus_wrapping", cmd_force_focus_wrapping },
2018-06-09 15:34:56 +02:00
{ "fullscreen", cmd_fullscreen },
{ "gaps", cmd_gaps },
2018-05-12 00:44:56 +02:00
{ "hide_edge_borders", cmd_hide_edge_borders },
2017-12-11 10:17:14 +01:00
{ "input", cmd_input },
2018-03-30 04:10:33 +02:00
{ "mode", cmd_mode },
2018-03-31 16:49:52 +02:00
{ "mouse_warping", cmd_mouse_warping },
{ "new_float", cmd_new_float },
{ "new_window", cmd_new_window },
2018-07-16 14:18:12 +02:00
{ "no_focus", cmd_no_focus },
2017-12-06 12:36:06 +01:00
{ "output", cmd_output },
{ "popup_during_fullscreen", cmd_popup_during_fullscreen },
2017-12-14 17:11:56 +01:00
{ "seat", cmd_seat },
{ "set", cmd_set },
2018-05-15 05:14:18 +02:00
{ "show_marks", cmd_show_marks },
{ "smart_borders", cmd_smart_borders },
2018-06-09 15:34:56 +02:00
{ "smart_gaps", cmd_smart_gaps },
2018-09-11 09:17:19 +02:00
{ "tiling_drag", cmd_tiling_drag },
{ "tiling_drag_threshold", cmd_tiling_drag_threshold },
{ "title_align", cmd_title_align },
{ "titlebar_border_thickness", cmd_titlebar_border_thickness },
{ "titlebar_padding", cmd_titlebar_padding },
{ "unbindcode", cmd_unbindcode },
2019-04-12 18:28:48 +02:00
{ "unbindswitch", cmd_unbindswitch },
{ "unbindsym", cmd_unbindsym },
2018-01-31 05:09:21 +01:00
{ "workspace", cmd_workspace },
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
};
2018-03-29 23:20:03 +02:00
/* Config-time only commands. Keep alphabetized */
static const struct cmd_handler config_handlers[] = {
2018-03-30 16:43:55 +02:00
{ "default_orientation", cmd_default_orientation },
2020-07-23 11:21:12 +02:00
{ "include", cmd_include },
2018-03-29 23:49:44 +02:00
{ "swaybg_command", cmd_swaybg_command },
{ "swaynag_command", cmd_swaynag_command },
{ "workspace_layout", cmd_workspace_layout },
2018-11-17 12:29:59 +01:00
{ "xwayland", cmd_xwayland },
2018-02-24 19:34:47 +01:00
};
2018-03-29 23:20:03 +02:00
/* Runtime-only commands. Keep alphabetized */
static const struct cmd_handler command_handlers[] = {
{ "border", cmd_border },
{ "create_output", cmd_create_output },
{ "exit", cmd_exit },
2018-05-04 14:24:25 +02:00
{ "floating", cmd_floating },
{ "fullscreen", cmd_fullscreen },
{ "inhibit_idle", cmd_inhibit_idle },
{ "kill", cmd_kill },
{ "layout", cmd_layout },
2018-05-14 14:47:10 +02:00
{ "mark", cmd_mark },
2019-09-25 16:35:41 +02:00
{ "max_render_time", cmd_max_render_time },
{ "move", cmd_move },
2018-08-07 12:25:04 +02:00
{ "nop", cmd_nop },
2018-04-03 06:47:45 +02:00
{ "opacity", cmd_opacity },
{ "reload", cmd_reload },
{ "rename", cmd_rename },
2018-04-05 03:32:31 +02:00
{ "resize", cmd_resize },
{ "scratchpad", cmd_scratchpad },
{ "shortcuts_inhibitor", cmd_shortcuts_inhibitor },
2018-03-31 06:44:17 +02:00
{ "split", cmd_split },
{ "splith", cmd_splith },
{ "splitt", cmd_splitt },
{ "splitv", cmd_splitv },
2018-05-24 14:30:44 +02:00
{ "sticky", cmd_sticky },
2018-05-26 17:02:21 +02:00
{ "swap", cmd_swap },
{ "title_format", cmd_title_format },
2018-05-14 14:47:10 +02:00
{ "unmark", cmd_unmark },
{ "urgent", cmd_urgent },
};
static int handler_compare(const void *_a, const void *_b) {
const struct cmd_handler *a = _a;
const struct cmd_handler *b = _b;
return strcasecmp(a->command, b->command);
}
const struct cmd_handler *find_handler(char *line,
const struct cmd_handler *handlers, size_t handlers_size) {
if (!handlers || !handlers_size) {
return NULL;
2017-12-11 10:17:14 +01:00
}
const struct cmd_handler query = { .command = line };
return bsearch(&query, handlers,
handlers_size / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
}
2017-12-11 10:17:14 +01:00
static const struct cmd_handler *find_handler_ex(char *line,
const struct cmd_handler *config_handlers, size_t config_handlers_size,
const struct cmd_handler *command_handlers, size_t command_handlers_size,
const struct cmd_handler *handlers, size_t handlers_size) {
const struct cmd_handler *handler = NULL;
2018-02-24 19:34:47 +01:00
if (config->reading) {
handler = find_handler(line, config_handlers, config_handlers_size);
} else if (config->active) {
handler = find_handler(line, command_handlers, command_handlers_size);
}
return handler ? handler : find_handler(line, handlers, handlers_size);
}
static const struct cmd_handler *find_core_handler(char *line) {
return find_handler_ex(line, config_handlers, sizeof(config_handlers),
command_handlers, sizeof(command_handlers),
handlers, sizeof(handlers));
}
static void set_config_node(struct sway_node *node, bool node_overridden) {
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 13:00:10 +02:00
config->handler_context.node = node;
config->handler_context.container = NULL;
config->handler_context.workspace = NULL;
config->handler_context.node_overridden = node_overridden;
if (node == NULL) {
return;
}
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 13:00:10 +02:00
switch (node->type) {
case N_CONTAINER:
config->handler_context.container = node->sway_container;
config->handler_context.workspace = node->sway_container->pending.workspace;
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 13:00:10 +02:00
break;
case N_WORKSPACE:
config->handler_context.workspace = node->sway_workspace;
break;
case N_ROOT:
case N_OUTPUT:
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 13:00:10 +02:00
break;
}
}
list_t *execute_command(char *_exec, struct sway_seat *seat,
struct sway_container *con) {
char *cmd;
char matched_delim = ';';
list_t *containers = NULL;
bool using_criteria = false;
if (seat == NULL) {
// passing a NULL seat means we just pick the default seat
seat = input_manager_get_default_seat();
if (!sway_assert(seat, "could not find a seat to run the command on")) {
return NULL;
}
}
char *exec = strdup(_exec);
char *head = exec;
list_t *res_list = create_list();
if (!res_list || !exec) {
return NULL;
}
config->handler_context.seat = seat;
do {
for (; isspace(*head); ++head) {}
// Extract criteria (valid for this command list only).
if (matched_delim == ';') {
using_criteria = false;
if (*head == '[') {
char *error = NULL;
struct criteria *criteria = criteria_parse(head, &error);
if (!criteria) {
list_add(res_list,
cmd_results_new(CMD_INVALID, "%s", error));
free(error);
goto cleanup;
}
list_free(containers);
containers = criteria_get_containers(criteria);
head += strlen(criteria->raw);
criteria_destroy(criteria);
using_criteria = true;
// Skip leading whitespace
for (; isspace(*head); ++head) {}
}
}
// Split command list
cmd = argsep(&head, ";,", &matched_delim);
for (; isspace(*cmd); ++cmd) {}
if (strcmp(cmd, "") == 0) {
sway_log(SWAY_INFO, "Ignoring empty command.");
continue;
}
sway_log(SWAY_INFO, "Handling command '%s'", cmd);
//TODO better handling of argv
int argc;
char **argv = split_args(cmd, &argc);
if (strcmp(argv[0], "exec") != 0 &&
strcmp(argv[0], "exec_always") != 0 &&
strcmp(argv[0], "mode") != 0) {
for (int i = 1; i < argc; ++i) {
if (*argv[i] == '\"' || *argv[i] == '\'') {
strip_quotes(argv[i]);
}
}
}
const struct cmd_handler *handler = find_core_handler(argv[0]);
if (!handler) {
list_add(res_list, cmd_results_new(CMD_INVALID,
"Unknown/invalid command '%s'", argv[0]));
free_argv(argc, argv);
goto cleanup;
}
// Var replacement, for all but first argument of set
for (int i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
argv[i] = do_var_replacement(argv[i]);
}
if (!using_criteria) {
if (con) {
set_config_node(&con->node, true);
} else {
set_config_node(seat_get_focus_inactive(seat, &root->node),
false);
}
struct cmd_results *res = handler->handle(argc-1, argv+1);
list_add(res_list, res);
if (res->status == CMD_INVALID) {
free_argv(argc, argv);
goto cleanup;
}
} else if (containers->length == 0) {
list_add(res_list,
cmd_results_new(CMD_FAILURE, "No matching node."));
} else {
struct cmd_results *fail_res = NULL;
for (int i = 0; i < containers->length; ++i) {
struct sway_container *container = containers->items[i];
set_config_node(&container->node, true);
struct cmd_results *res = handler->handle(argc-1, argv+1);
if (res->status == CMD_SUCCESS) {
free_cmd_results(res);
} else {
// last failure will take precedence
if (fail_res) {
free_cmd_results(fail_res);
}
fail_res = res;
if (res->status == CMD_INVALID) {
list_add(res_list, fail_res);
free_argv(argc, argv);
goto cleanup;
}
2018-01-20 22:21:45 +01:00
}
2017-11-23 03:39:35 +01:00
}
list_add(res_list,
fail_res ? fail_res : cmd_results_new(CMD_SUCCESS, NULL));
}
free_argv(argc, argv);
} while(head);
cleanup:
free(exec);
list_free(containers);
return res_list;
}
// this is like execute_command above, except:
2017-12-05 10:40:55 +01:00
// 1) it ignores empty commands (empty lines)
// 2) it does variable substitution
// 3) it doesn't split commands (because the multiple commands are supposed to
// be chained together)
// 4) execute_command handles all state internally while config_command has
// some state handled outside (notably the block mode, in read_config)
struct cmd_results *config_command(char *exec, char **new_block) {
2017-12-05 10:40:55 +01:00
struct cmd_results *results = NULL;
int argc;
char **argv = split_args(exec, &argc);
// Check for empty lines
2017-12-05 10:40:55 +01:00
if (!argc) {
results = cmd_results_new(CMD_SUCCESS, NULL);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
// Check for the start of a block
if (argc > 1 && strcmp(argv[argc - 1], "{") == 0) {
*new_block = join_args(argv, argc - 1);
results = cmd_results_new(CMD_BLOCK, NULL);
goto cleanup;
}
// Check for the end of a block
if (strcmp(argv[argc - 1], "}") == 0) {
results = cmd_results_new(CMD_BLOCK_END, NULL);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
// Make sure the command is not stored in a variable
if (*argv[0] == '$') {
argv[0] = do_var_replacement(argv[0]);
char *temp = join_args(argv, argc);
free_argv(argc, argv);
argv = split_args(temp, &argc);
free(temp);
if (!argc) {
results = cmd_results_new(CMD_SUCCESS, NULL);
goto cleanup;
}
}
// Determine the command handler
sway_log(SWAY_INFO, "Config command: %s", exec);
const struct cmd_handler *handler = find_core_handler(argv[0]);
if (!handler || !handler->handle) {
const char *error = handler
? "Command '%s' is shimmed, but unimplemented"
: "Unknown/invalid command '%s'";
results = cmd_results_new(CMD_INVALID, error, argv[0]);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
// Do variable replacement
if (handler->handle == cmd_set && argc > 1 && *argv[1] == '$') {
// Escape the variable name so it does not get replaced by one shorter
char *temp = calloc(1, strlen(argv[1]) + 2);
temp[0] = '$';
strcpy(&temp[1], argv[1]);
free(argv[1]);
argv[1] = temp;
}
char *command = do_var_replacement(join_args(argv, argc));
sway_log(SWAY_INFO, "After replacement: %s", command);
free_argv(argc, argv);
argv = split_args(command, &argc);
free(command);
// Strip quotes and unescape the string
for (int i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
if (handler->handle != cmd_exec && handler->handle != cmd_exec_always
&& handler->handle != cmd_mode
&& handler->handle != cmd_bindsym
&& handler->handle != cmd_bindcode
&& handler->handle != cmd_bindswitch
2018-09-30 18:35:45 +02:00
&& handler->handle != cmd_set
&& handler->handle != cmd_for_window
&& (*argv[i] == '\"' || *argv[i] == '\'')) {
strip_quotes(argv[i]);
}
2017-12-05 10:40:55 +01:00
unescape_string(argv[i]);
}
// Run command
results = handler->handle(argc - 1, argv + 1);
2017-12-05 10:40:55 +01:00
cleanup:
free_argv(argc, argv);
return results;
}
struct cmd_results *config_subcommand(char **argv, int argc,
const struct cmd_handler *handlers, size_t handlers_size) {
char *command = join_args(argv, argc);
sway_log(SWAY_DEBUG, "Subcommand: %s", command);
free(command);
const struct cmd_handler *handler = find_handler(argv[0], handlers,
handlers_size);
if (!handler) {
return cmd_results_new(CMD_INVALID,
"Unknown/invalid command '%s'", argv[0]);
}
if (handler->handle) {
return handler->handle(argc - 1, argv + 1);
}
return cmd_results_new(CMD_INVALID,
"The command '%s' is shimmed, but unimplemented", argv[0]);
}
2017-12-05 10:40:55 +01:00
struct cmd_results *config_commands_command(char *exec) {
struct cmd_results *results = NULL;
int argc;
char **argv = split_args(exec, &argc);
if (!argc) {
results = cmd_results_new(CMD_SUCCESS, NULL);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
// Find handler for the command this is setting a policy for
char *cmd = argv[0];
if (strcmp(cmd, "}") == 0) {
results = cmd_results_new(CMD_BLOCK_END, NULL);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
const struct cmd_handler *handler = find_handler(cmd, NULL, 0);
2017-12-05 10:40:55 +01:00
if (!handler && strcmp(cmd, "*") != 0) {
results = cmd_results_new(CMD_INVALID,
"Unknown/invalid command '%s'", cmd);
2017-12-05 10:40:55 +01:00
goto cleanup;
}
results = cmd_results_new(CMD_SUCCESS, NULL);
2017-12-05 10:40:55 +01:00
cleanup:
free_argv(argc, argv);
return results;
}
struct cmd_results *cmd_results_new(enum cmd_status status,
const char *format, ...) {
struct cmd_results *results = malloc(sizeof(struct cmd_results));
if (!results) {
sway_log(SWAY_ERROR, "Unable to allocate command results");
return NULL;
}
results->status = status;
if (format) {
char *error = malloc(256);
va_list args;
va_start(args, format);
if (error) {
vsnprintf(error, 256, format, args);
}
va_end(args);
results->error = error;
} else {
results->error = NULL;
}
return results;
}
void free_cmd_results(struct cmd_results *results) {
if (results->error) {
free(results->error);
}
free(results);
}
char *cmd_results_to_json(list_t *res_list) {
json_object *result_array = json_object_new_array();
for (int i = 0; i < res_list->length; ++i) {
struct cmd_results *results = res_list->items[i];
json_object *root = json_object_new_object();
json_object_object_add(root, "success",
json_object_new_boolean(results->status == CMD_SUCCESS));
if (results->error) {
json_object_object_add(root, "parse_error",
json_object_new_boolean(results->status == CMD_INVALID));
json_object_object_add(
root, "error", json_object_new_string(results->error));
}
json_object_array_add(result_array, root);
}
const char *json = json_object_to_json_string(result_array);
char *res = strdup(json);
json_object_put(result_array);
return res;
}