parent
4ca1e77fdb
commit
d7d21bb0f8
@ -0,0 +1,185 @@
|
|||||||
|
#ifndef _SWAY_COMMANDS_H
|
||||||
|
#define _SWAY_COMMANDS_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates the result of a command's execution.
|
||||||
|
*/
|
||||||
|
enum cmd_status {
|
||||||
|
CMD_SUCCESS, /**< The command was successful */
|
||||||
|
CMD_FAILURE, /**< The command resulted in an error */
|
||||||
|
CMD_INVALID, /**< Unknown command or parser error */
|
||||||
|
CMD_DEFER, /**< Command execution deferred */
|
||||||
|
// Config Blocks
|
||||||
|
CMD_BLOCK_END,
|
||||||
|
CMD_BLOCK_MODE,
|
||||||
|
CMD_BLOCK_BAR,
|
||||||
|
CMD_BLOCK_BAR_COLORS,
|
||||||
|
CMD_BLOCK_INPUT,
|
||||||
|
CMD_BLOCK_COMMANDS,
|
||||||
|
CMD_BLOCK_IPC,
|
||||||
|
CMD_BLOCK_IPC_EVENTS,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the result of executing a command.
|
||||||
|
*/
|
||||||
|
struct cmd_results {
|
||||||
|
enum cmd_status status;
|
||||||
|
char *input;
|
||||||
|
/**
|
||||||
|
* Human friendly error message, or NULL on success
|
||||||
|
*/
|
||||||
|
char *error;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum expected_args {
|
||||||
|
EXPECTED_MORE_THAN,
|
||||||
|
EXPECTED_AT_LEAST,
|
||||||
|
EXPECTED_LESS_THAN,
|
||||||
|
EXPECTED_EQUAL_TO
|
||||||
|
};
|
||||||
|
|
||||||
|
struct cmd_results *checkarg(int argc, const char *name,
|
||||||
|
enum expected_args type, int val);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse and handles a command.
|
||||||
|
*/
|
||||||
|
struct cmd_results *handle_command(char *command);
|
||||||
|
/**
|
||||||
|
* Allocates a cmd_results object.
|
||||||
|
*/
|
||||||
|
struct cmd_results *cmd_results_new(enum cmd_status status, const char* input, const char *error, ...);
|
||||||
|
/**
|
||||||
|
* Frees a cmd_results object.
|
||||||
|
*/
|
||||||
|
void free_cmd_results(struct cmd_results *results);
|
||||||
|
/**
|
||||||
|
* Serializes cmd_results to a JSON string.
|
||||||
|
*
|
||||||
|
* Free the JSON string later on.
|
||||||
|
*/
|
||||||
|
const char *cmd_results_to_json(struct cmd_results *results);
|
||||||
|
|
||||||
|
typedef struct cmd_results *sway_cmd(int argc, char **argv);
|
||||||
|
|
||||||
|
sway_cmd cmd_assign;
|
||||||
|
sway_cmd cmd_bar;
|
||||||
|
sway_cmd cmd_bindcode;
|
||||||
|
sway_cmd cmd_bindsym;
|
||||||
|
sway_cmd cmd_border;
|
||||||
|
sway_cmd cmd_client_focused;
|
||||||
|
sway_cmd cmd_client_focused_inactive;
|
||||||
|
sway_cmd cmd_client_unfocused;
|
||||||
|
sway_cmd cmd_client_urgent;
|
||||||
|
sway_cmd cmd_client_placeholder;
|
||||||
|
sway_cmd cmd_client_background;
|
||||||
|
sway_cmd cmd_clipboard;
|
||||||
|
sway_cmd cmd_commands;
|
||||||
|
sway_cmd cmd_debuglog;
|
||||||
|
sway_cmd cmd_default_border;
|
||||||
|
sway_cmd cmd_default_floating_border;
|
||||||
|
sway_cmd cmd_exec;
|
||||||
|
sway_cmd cmd_exec_always;
|
||||||
|
sway_cmd cmd_exit;
|
||||||
|
sway_cmd cmd_floating;
|
||||||
|
sway_cmd cmd_floating_maximum_size;
|
||||||
|
sway_cmd cmd_floating_minimum_size;
|
||||||
|
sway_cmd cmd_floating_mod;
|
||||||
|
sway_cmd cmd_floating_scroll;
|
||||||
|
sway_cmd cmd_focus;
|
||||||
|
sway_cmd cmd_focus_follows_mouse;
|
||||||
|
sway_cmd cmd_font;
|
||||||
|
sway_cmd cmd_for_window;
|
||||||
|
sway_cmd cmd_force_focus_wrapping;
|
||||||
|
sway_cmd cmd_fullscreen;
|
||||||
|
sway_cmd cmd_gaps;
|
||||||
|
sway_cmd cmd_hide_edge_borders;
|
||||||
|
sway_cmd cmd_include;
|
||||||
|
sway_cmd cmd_input;
|
||||||
|
sway_cmd cmd_ipc;
|
||||||
|
sway_cmd cmd_kill;
|
||||||
|
sway_cmd cmd_layout;
|
||||||
|
sway_cmd cmd_log_colors;
|
||||||
|
sway_cmd cmd_mark;
|
||||||
|
sway_cmd cmd_mode;
|
||||||
|
sway_cmd cmd_mouse_warping;
|
||||||
|
sway_cmd cmd_move;
|
||||||
|
sway_cmd cmd_new_float;
|
||||||
|
sway_cmd cmd_new_window;
|
||||||
|
sway_cmd cmd_no_focus;
|
||||||
|
sway_cmd cmd_orientation;
|
||||||
|
sway_cmd cmd_output;
|
||||||
|
sway_cmd cmd_permit;
|
||||||
|
sway_cmd cmd_reject;
|
||||||
|
sway_cmd cmd_reload;
|
||||||
|
sway_cmd cmd_resize;
|
||||||
|
sway_cmd cmd_scratchpad;
|
||||||
|
sway_cmd cmd_seamless_mouse;
|
||||||
|
sway_cmd cmd_set;
|
||||||
|
sway_cmd cmd_show_marks;
|
||||||
|
sway_cmd cmd_smart_gaps;
|
||||||
|
sway_cmd cmd_split;
|
||||||
|
sway_cmd cmd_splith;
|
||||||
|
sway_cmd cmd_splitt;
|
||||||
|
sway_cmd cmd_splitv;
|
||||||
|
sway_cmd cmd_sticky;
|
||||||
|
sway_cmd cmd_unmark;
|
||||||
|
sway_cmd cmd_workspace;
|
||||||
|
sway_cmd cmd_ws_auto_back_and_forth;
|
||||||
|
sway_cmd cmd_workspace_layout;
|
||||||
|
|
||||||
|
sway_cmd bar_cmd_activate_button;
|
||||||
|
sway_cmd bar_cmd_binding_mode_indicator;
|
||||||
|
sway_cmd bar_cmd_bindsym;
|
||||||
|
sway_cmd bar_cmd_colors;
|
||||||
|
sway_cmd bar_cmd_context_button;
|
||||||
|
sway_cmd bar_cmd_font;
|
||||||
|
sway_cmd bar_cmd_mode;
|
||||||
|
sway_cmd bar_cmd_modifier;
|
||||||
|
sway_cmd bar_cmd_output;
|
||||||
|
sway_cmd bar_cmd_height;
|
||||||
|
sway_cmd bar_cmd_hidden_state;
|
||||||
|
sway_cmd bar_cmd_icon_theme;
|
||||||
|
sway_cmd bar_cmd_id;
|
||||||
|
sway_cmd bar_cmd_position;
|
||||||
|
sway_cmd bar_cmd_secondary_button;
|
||||||
|
sway_cmd bar_cmd_separator_symbol;
|
||||||
|
sway_cmd bar_cmd_status_command;
|
||||||
|
sway_cmd bar_cmd_pango_markup;
|
||||||
|
sway_cmd bar_cmd_strip_workspace_numbers;
|
||||||
|
sway_cmd bar_cmd_swaybar_command;
|
||||||
|
sway_cmd bar_cmd_tray_output;
|
||||||
|
sway_cmd bar_cmd_tray_padding;
|
||||||
|
sway_cmd bar_cmd_wrap_scroll;
|
||||||
|
sway_cmd bar_cmd_workspace_buttons;
|
||||||
|
|
||||||
|
sway_cmd bar_colors_cmd_active_workspace;
|
||||||
|
sway_cmd bar_colors_cmd_background;
|
||||||
|
sway_cmd bar_colors_cmd_focused_background;
|
||||||
|
sway_cmd bar_colors_cmd_binding_mode;
|
||||||
|
sway_cmd bar_colors_cmd_focused_workspace;
|
||||||
|
sway_cmd bar_colors_cmd_inactive_workspace;
|
||||||
|
sway_cmd bar_colors_cmd_separator;
|
||||||
|
sway_cmd bar_colors_cmd_focused_separator;
|
||||||
|
sway_cmd bar_colors_cmd_statusline;
|
||||||
|
sway_cmd bar_colors_cmd_focused_statusline;
|
||||||
|
sway_cmd bar_colors_cmd_urgent_workspace;
|
||||||
|
|
||||||
|
sway_cmd input_cmd_accel_profile;
|
||||||
|
sway_cmd input_cmd_click_method;
|
||||||
|
sway_cmd input_cmd_drag_lock;
|
||||||
|
sway_cmd input_cmd_dwt;
|
||||||
|
sway_cmd input_cmd_events;
|
||||||
|
sway_cmd input_cmd_left_handed;
|
||||||
|
sway_cmd input_cmd_middle_emulation;
|
||||||
|
sway_cmd input_cmd_natural_scroll;
|
||||||
|
sway_cmd input_cmd_pointer_accel;
|
||||||
|
sway_cmd input_cmd_scroll_method;
|
||||||
|
sway_cmd input_cmd_tap;
|
||||||
|
|
||||||
|
sway_cmd cmd_ipc_cmd;
|
||||||
|
sway_cmd cmd_ipc_events;
|
||||||
|
sway_cmd cmd_ipc_event_cmd;
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,247 @@
|
|||||||
|
#define _XOPEN_SOURCE 500
|
||||||
|
#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"
|
||||||
|
#include "stringop.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
struct cmd_handler {
|
||||||
|
char *command;
|
||||||
|
sway_cmd *handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns error object, or NULL if check succeeds.
|
||||||
|
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
switch (type) {
|
||||||
|
case EXPECTED_MORE_THAN:
|
||||||
|
if (argc > val) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
||||||
|
"(expected more than %d argument%s, got %d)",
|
||||||
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
||||||
|
break;
|
||||||
|
case EXPECTED_AT_LEAST:
|
||||||
|
if (argc >= val) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
||||||
|
"(expected at least %d argument%s, got %d)",
|
||||||
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
||||||
|
break;
|
||||||
|
case EXPECTED_LESS_THAN:
|
||||||
|
if (argc < val) {
|
||||||
|
return NULL;
|
||||||
|
};
|
||||||
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
||||||
|
"(expected less than %d argument%s, got %d)",
|
||||||
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
||||||
|
break;
|
||||||
|
case EXPECTED_EQUAL_TO:
|
||||||
|
if (argc == val) {
|
||||||
|
return NULL;
|
||||||
|
};
|
||||||
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
||||||
|
"(expected %d arguments, got %d)", name, val, argc);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i = 1; i < len; ++i) {
|
||||||
|
if (!isxdigit(color[i])) {
|
||||||
|
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy color to buffer
|
||||||
|
strncpy(buffer, color, len);
|
||||||
|
// add default alpha channel if color was defined without it
|
||||||
|
if (len == 7) {
|
||||||
|
buffer[7] = 'f';
|
||||||
|
buffer[8] = 'f';
|
||||||
|
}
|
||||||
|
buffer[9] = '\0';
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Keep alphabetized */
|
||||||
|
static struct cmd_handler handlers[] = {
|
||||||
|
{ "exit", cmd_exit },
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
|
||||||
|
struct cmd_handler d = { .command=line };
|
||||||
|
struct cmd_handler *res = NULL;
|
||||||
|
sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT);
|
||||||
|
/* TODO
|
||||||
|
if (block == CMD_BLOCK_BAR) {
|
||||||
|
res = bsearch(&d, bar_handlers,
|
||||||
|
sizeof(bar_handlers) / sizeof(struct cmd_handler),
|
||||||
|
sizeof(struct cmd_handler), handler_compare);
|
||||||
|
} else if (block == CMD_BLOCK_BAR_COLORS){
|
||||||
|
res = bsearch(&d, bar_colors_handlers,
|
||||||
|
sizeof(bar_colors_handlers) / sizeof(struct cmd_handler),
|
||||||
|
sizeof(struct cmd_handler), handler_compare);
|
||||||
|
} else if (block == CMD_BLOCK_INPUT) {
|
||||||
|
res = bsearch(&d, input_handlers,
|
||||||
|
sizeof(input_handlers) / sizeof(struct cmd_handler),
|
||||||
|
sizeof(struct cmd_handler), handler_compare);
|
||||||
|
} else if (block == CMD_BLOCK_IPC) {
|
||||||
|
res = bsearch(&d, ipc_handlers,
|
||||||
|
sizeof(ipc_handlers) / sizeof(struct cmd_handler),
|
||||||
|
sizeof(struct cmd_handler), handler_compare);
|
||||||
|
} else if (block == CMD_BLOCK_IPC_EVENTS) {
|
||||||
|
res = bsearch(&d, ipc_event_handlers,
|
||||||
|
sizeof(ipc_event_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 res;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct cmd_results *handle_command(char *_exec) {
|
||||||
|
// Even though this function will process multiple commands we will only
|
||||||
|
// return the last error, if any (for now). (Since we have access to an
|
||||||
|
// error string we could e.g. concatonate all errors there.)
|
||||||
|
struct cmd_results *results = NULL;
|
||||||
|
char *exec = strdup(_exec);
|
||||||
|
char *head = exec;
|
||||||
|
char *cmdlist;
|
||||||
|
char *cmd;
|
||||||
|
|
||||||
|
head = exec;
|
||||||
|
do {
|
||||||
|
// Split command list
|
||||||
|
cmdlist = argsep(&head, ";");
|
||||||
|
cmdlist += strspn(cmdlist, whitespace);
|
||||||
|
do {
|
||||||
|
// Split commands
|
||||||
|
cmd = argsep(&cmdlist, ",");
|
||||||
|
cmd += strspn(cmd, whitespace);
|
||||||
|
if (strcmp(cmd, "") == 0) {
|
||||||
|
sway_log(L_INFO, "Ignoring empty command.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sway_log(L_INFO, "Handling command '%s'", cmd);
|
||||||
|
//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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
|
||||||
|
if (!handler) {
|
||||||
|
if (results) {
|
||||||
|
free_cmd_results(results);
|
||||||
|
}
|
||||||
|
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
|
||||||
|
free_argv(argc, argv);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
free_argv(argc, argv);
|
||||||
|
} while(cmdlist);
|
||||||
|
} while(head);
|
||||||
|
cleanup:
|
||||||
|
free(exec);
|
||||||
|
if (!results) {
|
||||||
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
sway_log(L_ERROR, "Unable to allocate command results");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
results->status = status;
|
||||||
|
if (input) {
|
||||||
|
results->input = strdup(input); // input is the command name
|
||||||
|
} else {
|
||||||
|
results->input = NULL;
|
||||||
|
}
|
||||||
|
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->input) {
|
||||||
|
free(results->input);
|
||||||
|
}
|
||||||
|
if (results->error) {
|
||||||
|
free(results->error);
|
||||||
|
}
|
||||||
|
free(results);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *cmd_results_to_json(struct cmd_results *results) {
|
||||||
|
json_object *result_array = json_object_new_array();
|
||||||
|
json_object *root = json_object_new_object();
|
||||||
|
json_object_object_add(root, "success",
|
||||||
|
json_object_new_boolean(results->status == CMD_SUCCESS));
|
||||||
|
if (results->input) {
|
||||||
|
json_object_object_add(
|
||||||
|
root, "input", json_object_new_string(results->input));
|
||||||
|
}
|
||||||
|
if (results->error) {
|
||||||
|
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);
|
||||||
|
free(result_array);
|
||||||
|
free(root);
|
||||||
|
return json;
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include "sway/commands.h"
|
||||||
|
|
||||||
|
void sway_terminate(int exit_code);
|
||||||
|
|
||||||
|
struct cmd_results *cmd_exit(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
/* TODO
|
||||||
|
if (config->reading) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "exit", "Can't be used in config file.");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
if ((error = checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
sway_terminate(0);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue