2018-09-03 17:08:49 +10:00
|
|
|
#define _POSIX_C_SOURCE 200809
|
2017-11-23 13:20:41 +11:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include "sway/commands.h"
|
2017-12-05 20:40:55 +11:00
|
|
|
#include "sway/config.h"
|
2018-01-21 08:21:45 +11:00
|
|
|
#include "sway/criteria.h"
|
2017-12-05 20:40:55 +11:00
|
|
|
#include "sway/security.h"
|
2017-12-11 20:17:14 +11:00
|
|
|
#include "sway/input/input-manager.h"
|
2018-01-22 00:46:31 +11:00
|
|
|
#include "sway/input/seat.h"
|
2018-05-13 08:16:36 +10:00
|
|
|
#include "sway/tree/view.h"
|
2017-11-23 13:20:41 +11:00
|
|
|
#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) {
|
2018-10-23 20:32:05 +11:00
|
|
|
const char *error_name = NULL;
|
2017-11-23 13:20:41 +11:00
|
|
|
switch (type) {
|
|
|
|
case EXPECTED_AT_LEAST:
|
2018-10-23 20:32:05 +11:00
|
|
|
if (argc < val) {
|
|
|
|
error_name = "at least ";
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
|
|
|
break;
|
2018-10-23 20:40:03 +11:00
|
|
|
case EXPECTED_AT_MOST:
|
|
|
|
if (argc > val) {
|
|
|
|
error_name = "at most ";
|
2018-10-23 20:32:05 +11:00
|
|
|
}
|
2017-11-23 13:20:41 +11:00
|
|
|
break;
|
|
|
|
case EXPECTED_EQUAL_TO:
|
2018-10-23 20:32:05 +11:00
|
|
|
if (argc != val) {
|
|
|
|
error_name = "";
|
|
|
|
}
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
2018-10-23 20:32:05 +11:00
|
|
|
return error_name ?
|
|
|
|
cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
|
|
"(expected %s%d argument%s, got %d)",
|
|
|
|
name, error_name, val, val != 1 ? "s" : "", argc)
|
|
|
|
: NULL;
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
|
|
|
|
2018-03-30 08:20:03 +11:00
|
|
|
/* Keep alphabetized */
|
2017-11-23 13:20:41 +11:00
|
|
|
static struct cmd_handler handlers[] = {
|
2018-04-24 14:59:49 +10:00
|
|
|
{ "assign", cmd_assign },
|
2018-03-30 08:20:03 +11:00
|
|
|
{ "bar", cmd_bar },
|
2017-12-28 02:08:18 +11:00
|
|
|
{ "bindcode", cmd_bindcode },
|
|
|
|
{ "bindsym", cmd_bindsym },
|
2018-10-08 05:44:37 +11:00
|
|
|
{ "client.background", cmd_client_noop },
|
2018-04-30 21:24:13 +10:00
|
|
|
{ "client.focused", cmd_client_focused },
|
|
|
|
{ "client.focused_inactive", cmd_client_focused_inactive },
|
2018-10-08 05:44:37 +11:00
|
|
|
{ "client.placeholder", cmd_client_noop },
|
2018-04-30 21:24:13 +10:00
|
|
|
{ "client.unfocused", cmd_client_unfocused },
|
|
|
|
{ "client.urgent", cmd_client_urgent },
|
2018-05-01 21:46:28 +10:00
|
|
|
{ "default_border", cmd_default_border },
|
2018-07-17 10:14:33 +10:00
|
|
|
{ "default_floating_border", cmd_default_floating_border },
|
2017-12-05 08:43:49 +11:00
|
|
|
{ "exec", cmd_exec },
|
|
|
|
{ "exec_always", cmd_exec_always },
|
2018-07-11 22:16:48 +10:00
|
|
|
{ "floating_maximum_size", cmd_floating_maximum_size },
|
|
|
|
{ "floating_minimum_size", cmd_floating_minimum_size },
|
2018-07-18 16:13:28 +10:00
|
|
|
{ "floating_modifier", cmd_floating_modifier },
|
2018-07-19 16:41:02 +10:00
|
|
|
{ "focus", cmd_focus },
|
2018-04-01 01:11:15 +11:00
|
|
|
{ "focus_follows_mouse", cmd_focus_follows_mouse },
|
2018-09-02 15:03:58 +10:00
|
|
|
{ "focus_on_window_activation", cmd_focus_on_window_activation },
|
2018-05-28 13:20:21 +10:00
|
|
|
{ "focus_wrapping", cmd_focus_wrapping },
|
2018-05-02 23:07:52 +10:00
|
|
|
{ "font", cmd_font },
|
2018-05-03 23:24:13 +10:00
|
|
|
{ "for_window", cmd_for_window },
|
2018-07-20 19:37:27 +10:00
|
|
|
{ "force_display_urgency_hint", cmd_force_display_urgency_hint },
|
2018-05-29 01:09:46 +10:00
|
|
|
{ "force_focus_wrapping", cmd_force_focus_wrapping },
|
2018-06-09 23:34:56 +10:00
|
|
|
{ "fullscreen", cmd_fullscreen },
|
|
|
|
{ "gaps", cmd_gaps },
|
2018-05-12 08:44:56 +10:00
|
|
|
{ "hide_edge_borders", cmd_hide_edge_borders },
|
2017-12-06 04:47:57 +11:00
|
|
|
{ "include", cmd_include },
|
2017-12-11 20:17:14 +11:00
|
|
|
{ "input", cmd_input },
|
2018-03-30 13:10:33 +11:00
|
|
|
{ "mode", cmd_mode },
|
2018-04-01 01:49:52 +11:00
|
|
|
{ "mouse_warping", cmd_mouse_warping },
|
2018-09-01 21:28:13 +10:00
|
|
|
{ "new_float", cmd_default_floating_border },
|
|
|
|
{ "new_window", cmd_default_border },
|
2018-07-16 22:18:12 +10:00
|
|
|
{ "no_focus", cmd_no_focus },
|
2017-12-06 22:36:06 +11:00
|
|
|
{ "output", cmd_output },
|
2018-10-07 21:40:05 +11:00
|
|
|
{ "popup_during_fullscreen", cmd_popup_during_fullscreen },
|
2017-12-15 03:11:56 +11:00
|
|
|
{ "seat", cmd_seat },
|
2018-06-05 12:24:34 +10:00
|
|
|
{ "set", cmd_set },
|
2018-05-15 13:14:18 +10:00
|
|
|
{ "show_marks", cmd_show_marks },
|
2018-10-02 02:56:27 +10:00
|
|
|
{ "smart_borders", cmd_smart_borders },
|
2018-06-09 23:34:56 +10:00
|
|
|
{ "smart_gaps", cmd_smart_gaps },
|
2018-09-11 17:17:19 +10:00
|
|
|
{ "tiling_drag", cmd_tiling_drag },
|
2019-01-02 00:45:21 +11:00
|
|
|
{ "tiling_drag_threshold", cmd_tiling_drag_threshold },
|
2018-11-26 14:08:58 +11:00
|
|
|
{ "title_align", cmd_title_align },
|
2018-11-18 06:06:48 +11:00
|
|
|
{ "titlebar_border_thickness", cmd_titlebar_border_thickness },
|
|
|
|
{ "titlebar_padding", cmd_titlebar_padding },
|
2018-01-31 15:09:21 +11:00
|
|
|
{ "workspace", cmd_workspace },
|
2018-04-09 00:44:59 +10:00
|
|
|
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
|
2017-11-23 13:20:41 +11:00
|
|
|
};
|
|
|
|
|
2018-03-30 08:20:03 +11:00
|
|
|
/* Config-time only commands. Keep alphabetized */
|
2018-02-25 05:34:47 +11:00
|
|
|
static struct cmd_handler config_handlers[] = {
|
2018-03-31 01:43:55 +11:00
|
|
|
{ "default_orientation", cmd_default_orientation },
|
2018-03-30 08:49:44 +11:00
|
|
|
{ "swaybg_command", cmd_swaybg_command },
|
2018-08-03 11:37:29 +10:00
|
|
|
{ "swaynag_command", cmd_swaynag_command },
|
2018-05-29 12:55:06 +10:00
|
|
|
{ "workspace_layout", cmd_workspace_layout },
|
2018-02-25 05:34:47 +11:00
|
|
|
};
|
|
|
|
|
2018-03-30 08:20:03 +11:00
|
|
|
/* Runtime-only commands. Keep alphabetized */
|
2018-02-25 05:20:34 +11:00
|
|
|
static struct cmd_handler command_handlers[] = {
|
2018-04-30 21:24:13 +10:00
|
|
|
{ "border", cmd_border },
|
2018-09-19 21:54:27 +10:00
|
|
|
{ "create_output", cmd_create_output },
|
2018-02-25 05:20:34 +11:00
|
|
|
{ "exit", cmd_exit },
|
2018-05-04 22:24:25 +10:00
|
|
|
{ "floating", cmd_floating },
|
|
|
|
{ "fullscreen", cmd_fullscreen },
|
2018-02-25 05:20:34 +11:00
|
|
|
{ "kill", cmd_kill },
|
|
|
|
{ "layout", cmd_layout },
|
2018-05-14 22:47:10 +10:00
|
|
|
{ "mark", cmd_mark },
|
2018-04-01 11:21:26 +10:00
|
|
|
{ "move", cmd_move },
|
2018-08-07 20:25:04 +10:00
|
|
|
{ "nop", cmd_nop },
|
2018-04-03 14:47:45 +10:00
|
|
|
{ "opacity", cmd_opacity },
|
2018-02-25 05:20:34 +11:00
|
|
|
{ "reload", cmd_reload },
|
2018-04-23 13:04:16 +10:00
|
|
|
{ "rename", cmd_rename },
|
2018-04-05 11:32:31 +10:00
|
|
|
{ "resize", cmd_resize },
|
2018-07-22 14:10:40 +10:00
|
|
|
{ "scratchpad", cmd_scratchpad },
|
2018-03-31 15:44:17 +11:00
|
|
|
{ "split", cmd_split },
|
|
|
|
{ "splith", cmd_splith },
|
|
|
|
{ "splitt", cmd_splitt },
|
|
|
|
{ "splitv", cmd_splitv },
|
2018-05-24 22:30:44 +10:00
|
|
|
{ "sticky", cmd_sticky },
|
2018-05-27 01:02:21 +10:00
|
|
|
{ "swap", cmd_swap },
|
2018-05-05 12:36:50 +10:00
|
|
|
{ "title_format", cmd_title_format },
|
2018-05-14 22:47:10 +10:00
|
|
|
{ "unmark", cmd_unmark },
|
2018-07-15 22:43:33 +10:00
|
|
|
{ "urgent", cmd_urgent },
|
2018-02-25 05:20:34 +11:00
|
|
|
};
|
|
|
|
|
2017-11-23 13:20:41 +11:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:20:02 +10:00
|
|
|
struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers,
|
|
|
|
int handlers_size) {
|
2017-11-23 13:20:41 +11:00
|
|
|
struct cmd_handler d = { .command=line };
|
|
|
|
struct cmd_handler *res = NULL;
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "find_handler(%s)", line);
|
2017-12-11 20:17:14 +11:00
|
|
|
|
2018-02-25 05:20:34 +11:00
|
|
|
bool config_loading = config->reading || !config->active;
|
|
|
|
|
|
|
|
if (!config_loading) {
|
|
|
|
res = bsearch(&d, command_handlers,
|
|
|
|
sizeof(command_handlers) / sizeof(struct cmd_handler),
|
2017-12-11 20:17:14 +11:00
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2018-02-25 05:20:34 +11:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
2017-12-11 20:17:14 +11:00
|
|
|
}
|
|
|
|
|
2018-02-25 05:34:47 +11:00
|
|
|
if (config->reading) {
|
|
|
|
res = bsearch(&d, config_handlers,
|
|
|
|
sizeof(config_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:20:02 +10:00
|
|
|
if (!cmd_handlers) {
|
|
|
|
cmd_handlers = handlers;
|
|
|
|
handlers_size = sizeof(handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = bsearch(&d, cmd_handlers,
|
|
|
|
handlers_size / sizeof(struct cmd_handler),
|
2018-02-25 05:20:34 +11:00
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
|
|
|
|
2017-11-23 13:20:41 +11:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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 21:00:10 +10:00
|
|
|
static void set_config_node(struct sway_node *node) {
|
|
|
|
config->handler_context.node = node;
|
2018-09-19 23:21:04 +10:00
|
|
|
config->handler_context.container = NULL;
|
|
|
|
config->handler_context.workspace = NULL;
|
|
|
|
|
|
|
|
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 21:00:10 +10:00
|
|
|
switch (node->type) {
|
|
|
|
case N_CONTAINER:
|
|
|
|
config->handler_context.container = node->sway_container;
|
|
|
|
config->handler_context.workspace = node->sway_container->workspace;
|
|
|
|
break;
|
|
|
|
case N_WORKSPACE:
|
|
|
|
config->handler_context.workspace = node->sway_workspace;
|
|
|
|
break;
|
2018-09-19 23:21:04 +10:00
|
|
|
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 21:00:10 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 13:42:09 +11:00
|
|
|
list_t *execute_command(char *_exec, struct sway_seat *seat,
|
2018-09-23 08:38:15 +10:00
|
|
|
struct sway_container *con) {
|
2018-11-28 13:42:09 +11:00
|
|
|
list_t *res_list = create_list();
|
2017-11-23 13:20:41 +11:00
|
|
|
char *exec = strdup(_exec);
|
|
|
|
char *head = exec;
|
|
|
|
char *cmdlist;
|
|
|
|
char *cmd;
|
2018-05-13 08:16:36 +10:00
|
|
|
list_t *views = NULL;
|
2017-11-23 13:20:41 +11:00
|
|
|
|
2018-02-25 04:50:24 +11:00
|
|
|
if (seat == NULL) {
|
|
|
|
// passing a NULL seat means we just pick the default seat
|
2018-10-18 22:20:00 +11:00
|
|
|
seat = input_manager_get_default_seat();
|
2018-02-25 04:50:24 +11:00
|
|
|
if (!sway_assert(seat, "could not find a seat to run the command on")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config->handler_context.seat = seat;
|
|
|
|
|
2017-11-23 13:20:41 +11:00
|
|
|
head = exec;
|
|
|
|
do {
|
2018-01-21 08:21:45 +11:00
|
|
|
// Extract criteria (valid for this command list only).
|
2018-05-15 11:24:16 +10:00
|
|
|
config->handler_context.using_criteria = false;
|
2018-01-21 08:21:45 +11:00
|
|
|
if (*head == '[') {
|
2018-05-13 08:16:36 +10:00
|
|
|
char *error = NULL;
|
|
|
|
struct criteria *criteria = criteria_parse(head, &error);
|
|
|
|
if (!criteria) {
|
2018-11-28 13:42:09 +11:00
|
|
|
list_add(res_list, cmd_results_new(CMD_INVALID, head,
|
|
|
|
"%s", error));
|
2018-05-13 08:16:36 +10:00
|
|
|
free(error);
|
2018-01-21 08:21:45 +11:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-05-13 08:16:36 +10:00
|
|
|
views = criteria_get_views(criteria);
|
|
|
|
head += strlen(criteria->raw);
|
|
|
|
criteria_destroy(criteria);
|
2018-05-15 11:24:16 +10:00
|
|
|
config->handler_context.using_criteria = true;
|
2018-01-21 08:21:45 +11:00
|
|
|
// Skip leading whitespace
|
2018-12-09 22:52:55 +11:00
|
|
|
for (; isspace(*head); ++head) {}
|
2018-01-21 08:21:45 +11:00
|
|
|
}
|
2017-11-23 13:20:41 +11:00
|
|
|
// Split command list
|
|
|
|
cmdlist = argsep(&head, ";");
|
2018-12-09 22:52:55 +11:00
|
|
|
for (; isspace(*cmdlist); ++cmdlist) {}
|
2017-11-23 13:20:41 +11:00
|
|
|
do {
|
|
|
|
// Split commands
|
|
|
|
cmd = argsep(&cmdlist, ",");
|
2018-12-09 22:52:55 +11:00
|
|
|
for (; isspace(*cmd); ++cmd) {}
|
2017-11-23 13:20:41 +11:00
|
|
|
if (strcmp(cmd, "") == 0) {
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_INFO, "Ignoring empty command.");
|
2017-11-23 13:20:41 +11:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_INFO, "Handling command '%s'", cmd);
|
2017-11-23 13:20:41 +11:00
|
|
|
//TODO better handling of argv
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(cmd, &argc);
|
|
|
|
if (strcmp(argv[0], "exec") != 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (*argv[i] == '\"' || *argv[i] == '\'') {
|
|
|
|
strip_quotes(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 03:20:02 +10:00
|
|
|
struct cmd_handler *handler = find_handler(argv[0], NULL, 0);
|
2017-11-23 13:20:41 +11:00
|
|
|
if (!handler) {
|
2018-11-28 13:42:09 +11:00
|
|
|
list_add(res_list, cmd_results_new(CMD_INVALID, cmd,
|
|
|
|
"Unknown/invalid command"));
|
2017-11-23 13:20:41 +11:00
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-21 08:21:45 +11:00
|
|
|
|
2018-06-05 12:24:34 +10:00
|
|
|
// 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]);
|
|
|
|
unescape_string(argv[i]);
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:24:16 +10:00
|
|
|
if (!config->handler_context.using_criteria) {
|
2018-12-05 18:20:40 +11:00
|
|
|
// The container or workspace which this command will run on.
|
|
|
|
struct sway_node *node = con ? &con->node :
|
|
|
|
seat_get_focus_inactive(seat, &root->node);
|
2018-09-23 08:38:15 +10:00
|
|
|
set_config_node(node);
|
2018-02-25 04:50:24 +11:00
|
|
|
struct cmd_results *res = handler->handle(argc-1, argv+1);
|
2018-11-28 13:42:09 +11:00
|
|
|
list_add(res_list, res);
|
|
|
|
if (res->status == CMD_INVALID) {
|
2018-02-25 04:50:24 +11:00
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
2018-01-21 08:21:45 +11:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-13 08:16:36 +10:00
|
|
|
for (int i = 0; i < views->length; ++i) {
|
|
|
|
struct sway_view *view = views->items[i];
|
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 21:00:10 +10:00
|
|
|
set_config_node(&view->container->node);
|
2018-01-21 08:21:45 +11:00
|
|
|
struct cmd_results *res = handler->handle(argc-1, argv+1);
|
2018-11-28 13:42:09 +11:00
|
|
|
list_add(res_list, res);
|
|
|
|
if (res->status == CMD_INVALID) {
|
2018-01-21 08:21:45 +11:00
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-11-23 13:39:35 +11:00
|
|
|
}
|
|
|
|
}
|
2017-11-23 13:20:41 +11:00
|
|
|
free_argv(argc, argv);
|
|
|
|
} while(cmdlist);
|
|
|
|
} while(head);
|
|
|
|
cleanup:
|
|
|
|
free(exec);
|
2018-07-22 14:10:40 +10:00
|
|
|
list_free(views);
|
2018-11-28 13:42:09 +11:00
|
|
|
return res_list;
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
|
|
|
|
2018-02-25 04:50:24 +11:00
|
|
|
// this is like execute_command above, except:
|
2017-12-05 20:40:55 +11: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)
|
2018-02-25 04:50:24 +11:00
|
|
|
// 4) execute_command handles all state internally while config_command has
|
|
|
|
// some state handled outside (notably the block mode, in read_config)
|
2018-05-31 03:20:02 +10:00
|
|
|
struct cmd_results *config_command(char *exec) {
|
2017-12-05 20:40:55 +11:00
|
|
|
struct cmd_results *results = NULL;
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(exec, &argc);
|
2018-10-26 03:58:05 +11:00
|
|
|
|
|
|
|
// Check for empty lines
|
2017-12-05 20:40:55 +11:00
|
|
|
if (!argc) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:58:05 +11:00
|
|
|
// Check for the start of a block
|
2018-05-31 03:20:02 +10:00
|
|
|
if (argc > 1 && strcmp(argv[argc - 1], "{") == 0) {
|
2018-07-11 22:16:48 +10:00
|
|
|
char *block = join_args(argv, argc - 1);
|
2018-05-31 03:20:02 +10:00
|
|
|
results = cmd_results_new(CMD_BLOCK, block, NULL);
|
|
|
|
free(block);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:58:05 +11:00
|
|
|
// Check for the end of a block
|
2018-05-31 03:20:02 +10:00
|
|
|
if (strcmp(argv[argc - 1], "}") == 0) {
|
2017-12-05 20:40:55 +11:00
|
|
|
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-10-26 03:58:05 +11:00
|
|
|
|
|
|
|
// 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, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the command handler
|
|
|
|
wlr_log(WLR_INFO, "Config command: %s", exec);
|
2018-05-31 03:20:02 +10:00
|
|
|
struct cmd_handler *handler = find_handler(argv[0], NULL, 0);
|
2018-10-26 03:58:05 +11:00
|
|
|
if (!handler || !handler->handle) {
|
2017-12-05 20:40:55 +11:00
|
|
|
char *input = argv[0] ? argv[0] : "(empty)";
|
2018-10-26 03:58:05 +11:00
|
|
|
char *error = handler
|
|
|
|
? "This command is shimmed, but unimplemented"
|
|
|
|
: "Unknown/invalid command";
|
|
|
|
results = cmd_results_new(CMD_INVALID, input, error);
|
2017-12-05 20:40:55 +11:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-10-26 03:58:05 +11:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
wlr_log(WLR_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) {
|
2018-10-01 00:39:29 +10:00
|
|
|
if (handler->handle != cmd_exec && handler->handle != cmd_exec_always
|
2018-11-28 17:28:03 +11:00
|
|
|
&& handler->handle != cmd_mode
|
2018-10-01 00:39:29 +10:00
|
|
|
&& handler->handle != cmd_bindsym
|
|
|
|
&& handler->handle != cmd_bindcode
|
2018-10-01 02:35:45 +10:00
|
|
|
&& handler->handle != cmd_set
|
2018-10-01 00:39:29 +10:00
|
|
|
&& (*argv[i] == '\"' || *argv[i] == '\'')) {
|
2018-09-19 11:44:40 +10:00
|
|
|
strip_quotes(argv[i]);
|
|
|
|
}
|
2017-12-05 20:40:55 +11:00
|
|
|
unescape_string(argv[i]);
|
|
|
|
}
|
2018-10-26 03:58:05 +11:00
|
|
|
|
|
|
|
// Run command
|
|
|
|
results = handler->handle(argc - 1, argv + 1);
|
2017-12-05 20:40:55 +11:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free_argv(argc, argv);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:23:11 +10:00
|
|
|
struct cmd_results *config_subcommand(char **argv, int argc,
|
|
|
|
struct cmd_handler *handlers, size_t handlers_size) {
|
2018-05-31 03:20:02 +10:00
|
|
|
char *command = join_args(argv, argc);
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_DEBUG, "Subcommand: %s", command);
|
2018-05-31 03:20:02 +10:00
|
|
|
free(command);
|
|
|
|
|
|
|
|
struct cmd_handler *handler = find_handler(argv[0], handlers,
|
|
|
|
handlers_size);
|
|
|
|
if (!handler) {
|
|
|
|
char *input = argv[0] ? argv[0] : "(empty)";
|
|
|
|
return cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
|
|
|
|
}
|
|
|
|
if (handler->handle) {
|
|
|
|
return handler->handle(argc - 1, argv + 1);
|
|
|
|
}
|
|
|
|
return cmd_results_new(CMD_INVALID, argv[0],
|
|
|
|
"This command is shimmed, but unimplemented");
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:40:55 +11: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, NULL);
|
|
|
|
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, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:20:02 +10:00
|
|
|
struct cmd_handler *handler = find_handler(cmd, NULL, 0);
|
2017-12-05 20:40:55 +11:00
|
|
|
if (!handler && strcmp(cmd, "*") != 0) {
|
2018-06-30 23:12:10 +10:00
|
|
|
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
|
2017-12-05 20:40:55 +11:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum command_context context = 0;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
char *name;
|
|
|
|
enum command_context context;
|
|
|
|
} context_names[] = {
|
|
|
|
{ "config", CONTEXT_CONFIG },
|
|
|
|
{ "binding", CONTEXT_BINDING },
|
|
|
|
{ "ipc", CONTEXT_IPC },
|
|
|
|
{ "criteria", CONTEXT_CRITERIA },
|
|
|
|
{ "all", CONTEXT_ALL },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) {
|
|
|
|
if (strcmp(context_names[j].name, argv[i]) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == sizeof(context_names) / sizeof(context_names[0])) {
|
|
|
|
results = cmd_results_new(CMD_INVALID, cmd,
|
|
|
|
"Invalid command context %s", argv[i]);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
context |= context_names[j].context;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_policy *policy = NULL;
|
|
|
|
for (int i = 0; i < config->command_policies->length; ++i) {
|
|
|
|
struct command_policy *p = config->command_policies->items[i];
|
|
|
|
if (strcmp(p->command, cmd) == 0) {
|
|
|
|
policy = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!policy) {
|
|
|
|
policy = alloc_command_policy(cmd);
|
2018-06-30 23:12:10 +10:00
|
|
|
if (!sway_assert(policy, "Unable to allocate security policy")) {
|
|
|
|
results = cmd_results_new(CMD_INVALID, cmd,
|
|
|
|
"Unable to allocate memory");
|
|
|
|
goto cleanup;
|
2017-12-05 20:40:55 +11:00
|
|
|
}
|
2018-06-30 23:12:10 +10:00
|
|
|
list_add(config->command_policies, policy);
|
2017-12-05 20:40:55 +11:00
|
|
|
}
|
|
|
|
policy->context = context;
|
|
|
|
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_INFO, "Set command policy for %s to %d",
|
2017-12-05 20:40:55 +11:00
|
|
|
policy->command, policy->context);
|
|
|
|
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free_argv(argc, argv);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2017-11-23 13:20:41 +11:00
|
|
|
struct cmd_results *cmd_results_new(enum cmd_status status,
|
|
|
|
const char *input, const char *format, ...) {
|
|
|
|
struct cmd_results *results = malloc(sizeof(struct cmd_results));
|
|
|
|
if (!results) {
|
2018-07-10 07:54:30 +10:00
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate command results");
|
2017-11-23 13:20:41 +11:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
results->status = status;
|
|
|
|
if (input) {
|
|
|
|
results->input = strdup(input); // input is the command name
|
|
|
|
} else {
|
|
|
|
results->input = NULL;
|
|
|
|
}
|
|
|
|
if (format) {
|
2018-08-03 11:37:29 +10:00
|
|
|
char *error = malloc(256);
|
2017-11-23 13:20:41 +11:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
if (error) {
|
2018-08-03 11:37:29 +10:00
|
|
|
vsnprintf(error, 256, format, args);
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
results->error = error;
|
|
|
|
} else {
|
|
|
|
results->error = NULL;
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_cmd_results(struct cmd_results *results) {
|
|
|
|
if (results->input) {
|
|
|
|
free(results->input);
|
|
|
|
}
|
|
|
|
if (results->error) {
|
|
|
|
free(results->error);
|
|
|
|
}
|
|
|
|
free(results);
|
|
|
|
}
|
|
|
|
|
2018-11-28 13:42:09 +11:00
|
|
|
char *cmd_results_to_json(list_t *res_list) {
|
2017-11-23 13:20:41 +11:00
|
|
|
json_object *result_array = json_object_new_array();
|
2018-11-28 13:42:09 +11:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
if (results->input) {
|
|
|
|
json_object_object_add(
|
|
|
|
root, "input", json_object_new_string(results->input));
|
|
|
|
}
|
|
|
|
json_object_array_add(result_array, root);
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
|
|
|
const char *json = json_object_to_json_string(result_array);
|
2018-07-05 08:07:59 +10:00
|
|
|
char *res = strdup(json);
|
|
|
|
json_object_put(result_array);
|
|
|
|
return res;
|
2017-11-23 13:20:41 +11:00
|
|
|
}
|
2018-03-30 08:20:03 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check and add color to buffer.
|
|
|
|
*
|
|
|
|
* return error object, or NULL if color is valid.
|
|
|
|
*/
|
|
|
|
struct cmd_results *add_color(const char *name,
|
|
|
|
char *buffer, const char *color) {
|
|
|
|
int len = strlen(color);
|
|
|
|
if (len != 7 && len != 9) {
|
|
|
|
return cmd_results_new(CMD_INVALID, name,
|
|
|
|
"Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
if (color[0] != '#') {
|
|
|
|
return cmd_results_new(CMD_INVALID, name,
|
|
|
|
"Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
for (int i = 1; i < len; ++i) {
|
|
|
|
if (!isxdigit(color[i])) {
|
|
|
|
return cmd_results_new(CMD_INVALID, name,
|
|
|
|
"Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 23:35:23 +10:00
|
|
|
strcpy(buffer, color);
|
2018-03-30 08:20:03 +11:00
|
|
|
// add default alpha channel if color was defined without it
|
|
|
|
if (len == 7) {
|
|
|
|
buffer[7] = 'f';
|
|
|
|
buffer[8] = 'f';
|
|
|
|
}
|
|
|
|
buffer[9] = '\0';
|
|
|
|
return NULL;
|
|
|
|
}
|