You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.1 KiB
163 lines
4.1 KiB
8 years ago
|
#define _XOPEN_SOURCE 500
|
||
8 years ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
8 years ago
|
#include "sway/security.h"
|
||
8 years ago
|
#include "sway/commands.h"
|
||
|
#include "sway/config.h"
|
||
|
#include "ipc.h"
|
||
|
#include "log.h"
|
||
|
#include "util.h"
|
||
|
|
||
8 years ago
|
static struct ipc_policy *current_policy = NULL;
|
||
|
|
||
8 years ago
|
struct cmd_results *cmd_ipc(int argc, char **argv) {
|
||
|
struct cmd_results *error = NULL;
|
||
8 years ago
|
if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 2))) {
|
||
8 years ago
|
return error;
|
||
|
}
|
||
8 years ago
|
if ((error = check_security_config())) {
|
||
|
return error;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
char *program = NULL;
|
||
8 years ago
|
|
||
8 years ago
|
if (!strcmp(argv[0], "*")) {
|
||
|
program = strdup(argv[0]);
|
||
|
} else if (!(program = resolve_path(argv[0]))) {
|
||
|
return cmd_results_new(
|
||
|
CMD_INVALID, "ipc", "Unable to resolve IPC Policy target.");
|
||
|
}
|
||
8 years ago
|
if (config->reading && strcmp("{", argv[1]) != 0) {
|
||
8 years ago
|
return cmd_results_new(CMD_INVALID, "ipc",
|
||
|
"Expected '{' at start of IPC config definition.");
|
||
|
}
|
||
|
|
||
|
if (!config->reading) {
|
||
|
return cmd_results_new(CMD_FAILURE, "ipc", "Can only be used in config file.");
|
||
|
}
|
||
|
|
||
8 years ago
|
current_policy = alloc_ipc_policy(program);
|
||
8 years ago
|
list_add(config->ipc_policies, current_policy);
|
||
8 years ago
|
|
||
8 years ago
|
free(program);
|
||
8 years ago
|
return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL);
|
||
|
}
|
||
|
|
||
|
struct cmd_results *cmd_ipc_events(int argc, char **argv) {
|
||
|
struct cmd_results *error = NULL;
|
||
|
if ((error = checkarg(argc, "events", EXPECTED_EQUAL_TO, 1))) {
|
||
|
return error;
|
||
|
}
|
||
|
|
||
|
if (config->reading && strcmp("{", argv[0]) != 0) {
|
||
|
return cmd_results_new(CMD_INVALID, "events",
|
||
|
"Expected '{' at start of IPC event config definition.");
|
||
|
}
|
||
|
|
||
|
if (!config->reading) {
|
||
|
return cmd_results_new(CMD_FAILURE, "events", "Can only be used in config file.");
|
||
|
}
|
||
|
|
||
|
return cmd_results_new(CMD_BLOCK_IPC_EVENTS, NULL, NULL);
|
||
|
}
|
||
|
|
||
|
struct cmd_results *cmd_ipc_cmd(int argc, char **argv) {
|
||
|
struct cmd_results *error = NULL;
|
||
|
if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) {
|
||
|
return error;
|
||
|
}
|
||
|
|
||
|
bool enabled;
|
||
|
if (strcmp(argv[0], "enabled") == 0) {
|
||
|
enabled = true;
|
||
|
} else if (strcmp(argv[0], "disabled") == 0) {
|
||
|
enabled = false;
|
||
|
} else {
|
||
|
return cmd_results_new(CMD_INVALID, argv[-1],
|
||
|
"Argument must be one of 'enabled' or 'disabled'");
|
||
|
}
|
||
|
|
||
|
struct {
|
||
|
char *name;
|
||
8 years ago
|
enum ipc_feature type;
|
||
8 years ago
|
} types[] = {
|
||
8 years ago
|
{ "*", IPC_FEATURE_ALL_COMMANDS },
|
||
8 years ago
|
{ "command", IPC_FEATURE_COMMAND },
|
||
|
{ "workspaces", IPC_FEATURE_GET_WORKSPACES },
|
||
|
{ "outputs", IPC_FEATURE_GET_OUTPUTS },
|
||
|
{ "tree", IPC_FEATURE_GET_TREE },
|
||
|
{ "marks", IPC_FEATURE_GET_MARKS },
|
||
|
{ "bar-config", IPC_FEATURE_GET_BAR_CONFIG },
|
||
|
{ "inputs", IPC_FEATURE_GET_INPUTS },
|
||
8 years ago
|
{ "clipboard", IPC_FEATURE_GET_CLIPBOARD },
|
||
8 years ago
|
};
|
||
|
|
||
|
uint32_t type = 0;
|
||
|
|
||
|
for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
|
||
|
if (strcmp(types[i].name, argv[-1]) == 0) {
|
||
|
type = types[i].type;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (enabled) {
|
||
8 years ago
|
current_policy->features |= type;
|
||
|
sway_log(L_DEBUG, "Enabled IPC %s feature", argv[-1]);
|
||
8 years ago
|
} else {
|
||
8 years ago
|
current_policy->features &= ~type;
|
||
8 years ago
|
sway_log(L_DEBUG, "Disabled IPC %s feature", argv[-1]);
|
||
|
}
|
||
|
|
||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||
|
}
|
||
|
|
||
|
struct cmd_results *cmd_ipc_event_cmd(int argc, char **argv) {
|
||
|
struct cmd_results *error = NULL;
|
||
|
if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) {
|
||
|
return error;
|
||
|
}
|
||
|
|
||
|
bool enabled;
|
||
|
if (strcmp(argv[0], "enabled") == 0) {
|
||
|
enabled = true;
|
||
|
} else if (strcmp(argv[0], "disabled") == 0) {
|
||
|
enabled = false;
|
||
|
} else {
|
||
|
return cmd_results_new(CMD_INVALID, argv[-1],
|
||
|
"Argument must be one of 'enabled' or 'disabled'");
|
||
|
}
|
||
|
|
||
|
struct {
|
||
|
char *name;
|
||
8 years ago
|
enum ipc_feature type;
|
||
8 years ago
|
} types[] = {
|
||
8 years ago
|
{ "*", IPC_FEATURE_ALL_EVENTS },
|
||
8 years ago
|
{ "workspace", IPC_FEATURE_EVENT_WORKSPACE },
|
||
|
{ "output", IPC_FEATURE_EVENT_OUTPUT },
|
||
|
{ "mode", IPC_FEATURE_EVENT_MODE },
|
||
|
{ "window", IPC_FEATURE_EVENT_WINDOW },
|
||
|
{ "binding", IPC_FEATURE_EVENT_BINDING },
|
||
|
{ "input", IPC_FEATURE_EVENT_INPUT },
|
||
8 years ago
|
};
|
||
|
|
||
|
uint32_t type = 0;
|
||
|
|
||
|
for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
|
||
|
if (strcmp(types[i].name, argv[-1]) == 0) {
|
||
|
type = types[i].type;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (enabled) {
|
||
8 years ago
|
current_policy->features |= type;
|
||
|
sway_log(L_DEBUG, "Enabled IPC %s event", argv[-1]);
|
||
8 years ago
|
} else {
|
||
8 years ago
|
current_policy->features &= ~type;
|
||
8 years ago
|
sway_log(L_DEBUG, "Disabled IPC %s event", argv[-1]);
|
||
|
}
|
||
|
|
||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||
|
}
|