aboutsummaryrefslogtreecommitdiff
path: root/sway
diff options
context:
space:
mode:
Diffstat (limited to 'sway')
-rw-r--r--sway/commands.c3
-rw-r--r--sway/commands/set.c71
-rw-r--r--sway/config.c7
-rw-r--r--sway/meson.build1
4 files changed, 80 insertions, 2 deletions
diff --git a/sway/commands.c b/sway/commands.c
index c1c6dc5d..f01329db 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -138,6 +138,7 @@ static struct cmd_handler handlers[] = {
{ "input", cmd_input },
{ "output", cmd_output },
{ "seat", cmd_seat },
+ { "set", cmd_set },
};
static int handler_compare(const void *_a, const void *_b) {
@@ -290,7 +291,7 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) {
int i;
// Var replacement, for all but first argument of set
// TODO commands
- for (i = /*handler->handle == cmd_set ? 2 :*/ 1; i < argc; ++i) {
+ for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
argv[i] = do_var_replacement(argv[i]);
unescape_string(argv[i]);
}
diff --git a/sway/commands/set.c b/sway/commands/set.c
new file mode 100644
index 00000000..dcd928ba
--- /dev/null
+++ b/sway/commands/set.c
@@ -0,0 +1,71 @@
+#define _XOPEN_SOURCE 700
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "list.h"
+#include "log.h"
+#include "stringop.h"
+
+// sort in order of longest->shortest
+static int compare_set_qsort(const void *_l, const void *_r) {
+ struct sway_variable const *l = *(void **)_l;
+ struct sway_variable const *r = *(void **)_r;
+ return strlen(r->name) - strlen(l->name);
+}
+
+void free_sway_variable(struct sway_variable *var) {
+ if (!var) {
+ return;
+ }
+ free(var->name);
+ free(var->value);
+ free(var);
+}
+
+struct cmd_results *cmd_set(int argc, char **argv) {
+ char *tmp;
+ struct cmd_results *error = NULL;
+ if (!config->reading) return cmd_results_new(CMD_FAILURE, "set", "Can only be used in config file.");
+ if ((error = checkarg(argc, "set", EXPECTED_AT_LEAST, 2))) {
+ return error;
+ }
+
+ if (argv[0][0] != '$') {
+ sway_log(L_INFO, "Warning: variable '%s' doesn't start with $", argv[0]);
+
+ size_t size = snprintf(NULL, 0, "$%s", argv[0]);
+ tmp = malloc(size + 1);
+ if (!tmp) {
+ return cmd_results_new(CMD_FAILURE, "set", "Not possible to create variable $'%s'", argv[0]);
+ }
+ snprintf(tmp, size+1, "$%s", argv[0]);
+
+ argv[0] = tmp;
+ }
+
+ struct sway_variable *var = NULL;
+ // Find old variable if it exists
+ int i;
+ for (i = 0; i < config->symbols->length; ++i) {
+ var = config->symbols->items[i];
+ if (strcmp(var->name, argv[0]) == 0) {
+ break;
+ }
+ var = NULL;
+ }
+ if (var) {
+ free(var->value);
+ } else {
+ var = malloc(sizeof(struct sway_variable));
+ if (!var) {
+ return cmd_results_new(CMD_FAILURE, "set", "Unable to allocate variable");
+ }
+ var->name = strdup(argv[0]);
+ list_add(config->symbols, var);
+ list_qsort(config->symbols, compare_set_qsort);
+ }
+ var->value = join_args(argv + 1, argc - 1);
+ return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
diff --git a/sway/config.c b/sway/config.c
index 627ed94f..e0a93e19 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -61,7 +61,12 @@ void free_config(struct sway_config *config) {
}
// TODO: handle all currently unhandled lists as we add implementations
- list_free(config->symbols);
+ if (config->symbols) {
+ for (i = 0; i < config->symbols->length; i++) {
+ free_sway_variable(config->symbols->items[i]);
+ }
+ list_free(config->symbols);
+ }
if (config->modes) {
for (i = 0; i < config->modes->length; i++) {
free_mode(config->modes->items[i]);
diff --git a/sway/meson.build b/sway/meson.build
index 01d5ef36..30ec166b 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -15,6 +15,7 @@ sway_sources = files(
'commands/seat.c',
'commands/seat/attach.c',
'commands/seat/fallback.c',
+ 'commands/set.c',
'commands/input/accel_profile.c',
'commands/input/click_method.c',
'commands/input/drag_lock.c',