Revamp seat configs

This makes seat configs work like output and input configs do. This also
adds support for wildcard seat configs. A seat config is still created
in the main seat command handler, but instead of creating a new one in
the subcommands and destroying the main seat command's instance, the
seat subcommands modify the main one. The seat config is then stored,
where it is merged appropriately. The seat config returned from
`store_seat_config` is then applied. When attempting to apply a wildcard
seat config, a seat specific config is queried for and if found, that is
used. Otherwise, the wildcard config is applied directly.

Additionally, instead of adding input devices to the default seat
directly when there is no seat configs, a seat config for the default
seat is created with only fallback set to true, which is more explicit.
It also fixes an issue where running a seat command at runtime (with no
seat config in the sway config), would result in all input devices being
removed from the default seat and leaving sway in an unusable state.

Also, instead of checking for any seat config, the search is for a seat
config with a fallback option seat. This makes it so if there are only
seat configs with fallback set to -1, the default seat is still created
since there is no explicit notion on what to do regarding fallbacks.
However, if there is even a single fallback 0, then the default seat is
not used as a fallback. This will be needed for seat subcommands like
hide_cursor where the user may only want to set that property without
effecting anything else.
This commit is contained in:
Brian Ashworth 2018-12-27 00:46:55 -05:00 committed by emersion
parent 4503ad2d19
commit 3e8f548d1d
7 changed files with 96 additions and 65 deletions

View file

@ -536,7 +536,7 @@ struct seat_attachment_config *seat_attachment_config_new(void);
struct seat_attachment_config *seat_config_get_attachment( struct seat_attachment_config *seat_config_get_attachment(
struct seat_config *seat_config, char *identifier); struct seat_config *seat_config, char *identifier);
void apply_seat_config(struct seat_config *seat); struct seat_config *store_seat_config(struct seat_config *seat);
int output_name_cmp(const void *item, const void *data); int output_name_cmp(const void *item, const void *data);

View file

@ -42,21 +42,6 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
: NULL; : NULL;
} }
void apply_seat_config(struct seat_config *seat_config) {
int i = list_seq_find(config->seat_configs, seat_name_cmp, seat_config->name);
if (i >= 0) {
// merge existing config
struct seat_config *sc = config->seat_configs->items[i];
merge_seat_config(sc, seat_config);
free_seat_config(seat_config);
seat_config = sc;
} else {
list_add(config->seat_configs, seat_config);
}
input_manager_apply_seat_config(seat_config);
}
/* Keep alphabetized */ /* Keep alphabetized */
static struct cmd_handler handlers[] = { static struct cmd_handler handlers[] = {
{ "assign", cmd_assign }, { "assign", cmd_assign },

View file

@ -26,9 +26,16 @@ struct cmd_results *cmd_seat(int argc, char **argv) {
struct cmd_results *res = config_subcommand(argv + 1, argc - 1, struct cmd_results *res = config_subcommand(argv + 1, argc - 1,
seat_handlers, sizeof(seat_handlers)); seat_handlers, sizeof(seat_handlers));
if (res && res->status != CMD_SUCCESS) {
free_seat_config(config->handler_context.seat_config); free_seat_config(config->handler_context.seat_config);
config->handler_context.seat_config = NULL; config->handler_context.seat_config = NULL;
return res; return res;
} }
struct seat_config *sc =
store_seat_config(config->handler_context.seat_config);
input_manager_apply_seat_config(sc);
config->handler_context.seat_config = NULL;
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -1,10 +1,7 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <string.h> #include <string.h>
#include <strings.h>
#include "sway/input/input-manager.h"
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/config.h" #include "sway/config.h"
#include "log.h"
#include "stringop.h" #include "stringop.h"
struct cmd_results *seat_cmd_attach(int argc, char **argv) { struct cmd_results *seat_cmd_attach(int argc, char **argv) {
@ -12,19 +9,17 @@ struct cmd_results *seat_cmd_attach(int argc, char **argv) {
if ((error = checkarg(argc, "attach", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "attach", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct seat_config *current_seat_config = if (!config->handler_context.seat_config) {
config->handler_context.seat_config;
if (!current_seat_config) {
return cmd_results_new(CMD_FAILURE, "attach", "No seat defined"); return cmd_results_new(CMD_FAILURE, "attach", "No seat defined");
} }
struct seat_config *new_config = new_seat_config(current_seat_config->name); struct seat_attachment_config *attachment = seat_attachment_config_new();
struct seat_attachment_config *new_attachment = seat_attachment_config_new(); if (!attachment) {
new_attachment->identifier = strdup(argv[0]); return cmd_results_new(CMD_FAILURE, "attach",
list_add(new_config->attachments, new_attachment); "Failed to allocate seat attachment config");
if (!config->validating) {
apply_seat_config(new_config);
} }
attachment->identifier = strdup(argv[0]);
list_add(config->handler_context.seat_config->attachments, attachment);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -1,27 +1,18 @@
#include <string.h>
#include <strings.h>
#include "sway/config.h" #include "sway/config.h"
#include "sway/commands.h" #include "sway/commands.h"
#include "sway/input/input-manager.h"
#include "util.h" #include "util.h"
struct cmd_results *seat_cmd_fallback(int argc, char **argv) { struct cmd_results *seat_cmd_fallback(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "fallback", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "fallback", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct seat_config *current_seat_config = if (!config->handler_context.seat_config) {
config->handler_context.seat_config;
if (!current_seat_config) {
return cmd_results_new(CMD_FAILURE, "fallback", "No seat defined"); return cmd_results_new(CMD_FAILURE, "fallback", "No seat defined");
} }
struct seat_config *new_config =
new_seat_config(current_seat_config->name);
new_config->fallback = parse_boolean(argv[0], false); config->handler_context.seat_config->fallback =
parse_boolean(argv[0], false);
if (!config->validating) {
apply_seat_config(new_config);
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }

View file

@ -11,7 +11,6 @@ struct seat_config *new_seat_config(const char* name) {
return NULL; return NULL;
} }
wlr_log(WLR_DEBUG, "new_seat_config(%s)", name);
seat->name = strdup(name); seat->name = strdup(name);
if (!sway_assert(seat->name, "could not allocate name for seat")) { if (!sway_assert(seat->name, "could not allocate name for seat")) {
free(seat); free(seat);
@ -30,6 +29,52 @@ struct seat_config *new_seat_config(const char* name) {
return seat; return seat;
} }
static void merge_wildcard_on_all(struct seat_config *wildcard) {
for (int i = 0; i < config->seat_configs->length; i++) {
struct seat_config *sc = config->seat_configs->items[i];
if (strcmp(wildcard->name, sc->name) != 0) {
wlr_log(WLR_DEBUG, "Merging seat * config on %s", sc->name);
merge_seat_config(sc, wildcard);
}
}
}
struct seat_config *store_seat_config(struct seat_config *sc) {
bool wildcard = strcmp(sc->name, "*") == 0;
if (wildcard) {
merge_wildcard_on_all(sc);
}
int i = list_seq_find(config->seat_configs, seat_name_cmp, sc->name);
if (i >= 0) {
wlr_log(WLR_DEBUG, "Merging on top of existing seat config");
struct seat_config *current = config->seat_configs->items[i];
merge_seat_config(current, sc);
free_seat_config(sc);
sc = current;
} else if (!wildcard) {
wlr_log(WLR_DEBUG, "Adding non-wildcard seat config");
i = list_seq_find(config->seat_configs, seat_name_cmp, "*");
if (i >= 0) {
wlr_log(WLR_DEBUG, "Merging on top of seat * config");
struct seat_config *current = new_seat_config(sc->name);
merge_seat_config(current, config->seat_configs->items[i]);
merge_seat_config(current, sc);
free_seat_config(sc);
sc = current;
}
list_add(config->seat_configs, sc);
} else {
// New wildcard config. Just add it
wlr_log(WLR_DEBUG, "Adding seat * config");
list_add(config->seat_configs, sc);
}
wlr_log(WLR_DEBUG, "Config stored for seat %s", sc->name);
return sc;
}
struct seat_attachment_config *seat_attachment_config_new(void) { struct seat_attachment_config *seat_attachment_config_new(void) {
struct seat_attachment_config *attachment = struct seat_attachment_config *attachment =
calloc(1, sizeof(struct seat_attachment_config)); calloc(1, sizeof(struct seat_attachment_config));
@ -65,11 +110,6 @@ static void merge_seat_attachment_config(struct seat_attachment_config *dest,
} }
void merge_seat_config(struct seat_config *dest, struct seat_config *source) { void merge_seat_config(struct seat_config *dest, struct seat_config *source) {
if (source->name) {
free(dest->name);
dest->name = strdup(source->name);
}
if (source->fallback != -1) { if (source->fallback != -1) {
dest->fallback = source->fallback; dest->fallback = source->fallback;
} }

View file

@ -82,11 +82,12 @@ static struct sway_input_device *input_sway_device_from_wlr(
return NULL; return NULL;
} }
static bool input_has_seat_configuration(void) { static bool input_has_seat_fallback_configuration(void) {
struct sway_seat *seat = NULL; struct sway_seat *seat = NULL;
wl_list_for_each(seat, &server.input->seats, link) { wl_list_for_each(seat, &server.input->seats, link) {
struct seat_config *seat_config = seat_get_config(seat); struct seat_config *seat_config = seat_get_config(seat);
if (seat_config) { if (seat_config && strcmp(seat_config->name, "*") != 0
&& seat_config->fallback != -1) {
return true; return true;
} }
} }
@ -296,11 +297,12 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
input_device->device_destroy.notify = handle_device_destroy; input_device->device_destroy.notify = handle_device_destroy;
struct sway_seat *seat = NULL; struct sway_seat *seat = NULL;
if (!input_has_seat_configuration()) { if (!input_has_seat_fallback_configuration()) {
wlr_log(WLR_DEBUG, "no seat configuration, using default seat"); wlr_log(WLR_DEBUG, "no seat config - creating default seat config");
seat = input_manager_get_default_seat(); seat = input_manager_get_default_seat();
seat_add_device(seat, input_device); struct seat_config *sc = new_seat_config(seat->wlr_seat->name);
return; sc->fallback = true;
store_seat_config(sc);
} }
bool added = false; bool added = false;
@ -459,14 +461,25 @@ void input_manager_apply_input_config(struct input_config *input_config) {
} }
void input_manager_apply_seat_config(struct seat_config *seat_config) { void input_manager_apply_seat_config(struct seat_config *seat_config) {
wlr_log(WLR_DEBUG, "applying new seat config for seat %s", wlr_log(WLR_DEBUG, "applying seat config for seat %s", seat_config->name);
seat_config->name); if (strcmp(seat_config->name, "*") == 0) {
struct sway_seat *seat = NULL;
wl_list_for_each(seat, &server.input->seats, link) {
// Only apply the wildcard config directly if there is no seat
// specific config
struct seat_config *sc = seat_get_config(seat);
if (!sc) {
sc = seat_config;
}
seat_apply_config(seat, sc);
}
} else {
struct sway_seat *seat = input_manager_get_seat(seat_config->name); struct sway_seat *seat = input_manager_get_seat(seat_config->name);
if (!seat) { if (!seat) {
return; return;
} }
seat_apply_config(seat, seat_config); seat_apply_config(seat, seat_config);
}
// for every device, try to add it to a seat and if no seat has it // for every device, try to add it to a seat and if no seat has it
// attached, add it to the fallback seats. // attached, add it to the fallback seats.