basic configuration

This commit is contained in:
Tony Crisci 2017-12-14 11:11:56 -05:00
parent c173d30b92
commit 92fef27eaa
15 changed files with 543 additions and 174 deletions

View file

@ -17,6 +17,7 @@ enum cmd_status {
CMD_BLOCK_BAR, CMD_BLOCK_BAR,
CMD_BLOCK_BAR_COLORS, CMD_BLOCK_BAR_COLORS,
CMD_BLOCK_INPUT, CMD_BLOCK_INPUT,
CMD_BLOCK_SEAT,
CMD_BLOCK_COMMANDS, CMD_BLOCK_COMMANDS,
CMD_BLOCK_IPC, CMD_BLOCK_IPC,
CMD_BLOCK_IPC_EVENTS, CMD_BLOCK_IPC_EVENTS,
@ -42,6 +43,7 @@ enum expected_args {
}; };
void input_cmd_apply(struct input_config *input); void input_cmd_apply(struct input_config *input);
void seat_cmd_apply(struct seat_config *seat);
struct cmd_results *checkarg(int argc, const char *name, struct cmd_results *checkarg(int argc, const char *name,
enum expected_args type, int val); enum expected_args type, int val);
@ -111,6 +113,7 @@ sway_cmd cmd_gaps;
sway_cmd cmd_hide_edge_borders; sway_cmd cmd_hide_edge_borders;
sway_cmd cmd_include; sway_cmd cmd_include;
sway_cmd cmd_input; sway_cmd cmd_input;
sway_cmd cmd_seat;
sway_cmd cmd_ipc; sway_cmd cmd_ipc;
sway_cmd cmd_kill; sway_cmd cmd_kill;
sway_cmd cmd_layout; sway_cmd cmd_layout;
@ -193,6 +196,8 @@ sway_cmd input_cmd_pointer_accel;
sway_cmd input_cmd_scroll_method; sway_cmd input_cmd_scroll_method;
sway_cmd input_cmd_tap; sway_cmd input_cmd_tap;
sway_cmd seat_cmd_attach;
sway_cmd cmd_ipc_cmd; sway_cmd cmd_ipc_cmd;
sway_cmd cmd_ipc_events; sway_cmd cmd_ipc_events;
sway_cmd cmd_ipc_event_cmd; sway_cmd cmd_ipc_event_cmd;

View file

@ -69,7 +69,22 @@ struct input_config {
bool capturable; bool capturable;
struct wlr_box region; struct wlr_box region;
char *seat; };
/**
* Options for misc device configurations that happen in the seat block
*/
struct seat_attachment_config {
char *identifier;
// TODO other things are configured here for some reason
};
/**
* Options for multiseat and other misc device configurations
*/
struct seat_config {
char *name;
list_t *attachments; // list of seat_attachment configs
}; };
/** /**
@ -260,6 +275,7 @@ struct sway_config {
list_t *pid_workspaces; list_t *pid_workspaces;
list_t *output_configs; list_t *output_configs;
list_t *input_configs; list_t *input_configs;
list_t *seat_configs;
list_t *criteria; list_t *criteria;
list_t *no_focus; list_t *no_focus;
list_t *active_bar_modifiers; list_t *active_bar_modifiers;
@ -358,9 +374,16 @@ struct cmd_results *check_security_config();
int input_identifier_cmp(const void *item, const void *data); int input_identifier_cmp(const void *item, const void *data);
struct input_config *new_input_config(const char* identifier); struct input_config *new_input_config(const char* identifier);
void merge_input_config(struct input_config *dst, struct input_config *src); void merge_input_config(struct input_config *dst, struct input_config *src);
void apply_input_config(struct input_config *ic, struct libinput_device *dev);
void free_input_config(struct input_config *ic); void free_input_config(struct input_config *ic);
int seat_name_cmp(const void *item, const void *data);
struct seat_config *new_seat_config(const char* name);
void merge_seat_config(struct seat_config *dst, struct seat_config *src);
void free_seat_config(struct seat_config *ic);
struct seat_attachment_config *seat_attachment_config_new();
struct seat_attachment_config *seat_config_get_attachment(
struct seat_config *seat_config, char *identifier);
int output_name_cmp(const void *item, const void *data); int output_name_cmp(const void *item, const void *data);
void merge_output_config(struct output_config *dst, struct output_config *src); void merge_output_config(struct output_config *dst, struct output_config *src);
/** Sets up a WLC output handle based on a given output_config. /** Sets up a WLC output handle based on a given output_config.

View file

@ -6,6 +6,7 @@
#include "list.h" #include "list.h"
extern struct input_config *current_input_config; extern struct input_config *current_input_config;
extern struct seat_config *current_seat_config;
/** /**
* The global singleton input manager * The global singleton input manager
@ -17,7 +18,6 @@ struct sway_input_device {
char *identifier; char *identifier;
struct wlr_input_device *wlr_device; struct wlr_input_device *wlr_device;
struct input_config *config; struct input_config *config;
struct sway_keyboard *keyboard; // managed by the seat
struct wl_list link; struct wl_list link;
}; };
@ -40,7 +40,10 @@ void sway_input_manager_set_focus(struct sway_input_manager *input,
void sway_input_manager_configure_xcursor(struct sway_input_manager *input); void sway_input_manager_configure_xcursor(struct sway_input_manager *input);
void sway_input_manager_apply_config(struct sway_input_manager *input, void sway_input_manager_apply_input_config(struct sway_input_manager *input,
struct input_config *config); struct input_config *input_config);
void sway_input_manager_apply_seat_config(struct sway_input_manager *input,
struct seat_config *seat_config);
#endif #endif

View file

@ -1,15 +1,18 @@
#include "sway/input/seat.h" #include "sway/input/seat.h"
struct sway_keyboard { struct sway_keyboard {
struct sway_seat *seat; struct sway_seat_device *seat_device;
struct sway_input_device *device;
struct wl_list link; // sway_seat::keyboards struct wl_list link; // sway_seat::keyboards
struct xkb_keymap *keymap;
struct wl_listener keyboard_key; struct wl_listener keyboard_key;
struct wl_listener keyboard_modifiers; struct wl_listener keyboard_modifiers;
}; };
struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
struct sway_input_device *device); struct sway_seat_device *device);
void sway_keyboard_configure(struct sway_keyboard *keyboard);
void sway_keyboard_destroy(struct sway_keyboard *keyboard); void sway_keyboard_destroy(struct sway_keyboard *keyboard);

View file

@ -4,16 +4,25 @@
#include <wlr/types/wlr_seat.h> #include <wlr/types/wlr_seat.h>
#include "sway/input/input-manager.h" #include "sway/input/input-manager.h"
struct sway_seat_device {
struct sway_seat *sway_seat;
struct sway_input_device *input_device;
struct sway_keyboard *keyboard;
struct seat_attachment_config *attachment_config;
struct wl_list link; // sway_seat::devices
};
struct sway_seat { struct sway_seat {
struct wlr_seat *seat; struct wlr_seat *wlr_seat;
struct seat_config *config;
struct sway_cursor *cursor; struct sway_cursor *cursor;
struct sway_input_manager *input; struct sway_input_manager *input;
swayc_t *focus; swayc_t *focus;
list_t *devices;
struct wl_listener focus_destroy; struct wl_listener focus_destroy;
struct wl_list devices; // sway_seat_device::link
struct wl_list link; // input_manager::seats struct wl_list link; // input_manager::seats
}; };
@ -23,6 +32,9 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input,
void sway_seat_add_device(struct sway_seat *seat, void sway_seat_add_device(struct sway_seat *seat,
struct sway_input_device *device); struct sway_input_device *device);
void sway_seat_configure_device(struct sway_seat *seat,
struct sway_input_device *device);
void sway_seat_remove_device(struct sway_seat *seat, void sway_seat_remove_device(struct sway_seat *seat,
struct sway_input_device *device); struct sway_input_device *device);
@ -30,4 +42,6 @@ void sway_seat_configure_xcursor(struct sway_seat *seat);
void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container); void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container);
void sway_seat_set_config(struct sway_seat *seat, struct seat_config *seat_config);
#endif #endif

View file

@ -71,7 +71,24 @@ void input_cmd_apply(struct input_config *input) {
} }
current_input_config = input; current_input_config = input;
sway_input_manager_apply_config(input_manager, input); sway_input_manager_apply_input_config(input_manager, input);
}
void seat_cmd_apply(struct seat_config *seat) {
int i;
i = list_seq_find(config->seat_configs, seat_name_cmp, seat->name);
if (i >= 0) {
// merge existing config
struct seat_config *sc = config->seat_configs->items[i];
merge_seat_config(sc, seat);
free_seat_config(seat);
seat = sc;
} else {
list_add(config->seat_configs, seat);
}
current_seat_config = seat;
sway_input_manager_apply_seat_config(input_manager, seat);
} }
/** /**
@ -115,6 +132,7 @@ static struct cmd_handler handlers[] = {
{ "exit", cmd_exit }, { "exit", cmd_exit },
{ "include", cmd_include }, { "include", cmd_include },
{ "input", cmd_input }, { "input", cmd_input },
{ "seat", cmd_seat },
}; };
static int handler_compare(const void *_a, const void *_b) { static int handler_compare(const void *_a, const void *_b) {
@ -135,19 +153,27 @@ static struct cmd_handler input_handlers[] = {
{ "natural_scroll", input_cmd_natural_scroll }, { "natural_scroll", input_cmd_natural_scroll },
{ "pointer_accel", input_cmd_pointer_accel }, { "pointer_accel", input_cmd_pointer_accel },
{ "scroll_method", input_cmd_scroll_method }, { "scroll_method", input_cmd_scroll_method },
{ "seat", input_cmd_seat },
{ "tap", input_cmd_tap }, { "tap", input_cmd_tap },
}; };
// must be in order for the bsearch
static struct cmd_handler seat_handlers[] = {
{ "attach", seat_cmd_attach },
};
static struct cmd_handler *find_handler(char *line, enum cmd_status block) { static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
struct cmd_handler d = { .command=line }; struct cmd_handler d = { .command=line };
struct cmd_handler *res = NULL; struct cmd_handler *res = NULL;
sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT); sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_SEAT);
if (block == CMD_BLOCK_INPUT) { if (block == CMD_BLOCK_INPUT) {
res = bsearch(&d, input_handlers, res = bsearch(&d, input_handlers,
sizeof(input_handlers) / sizeof(struct cmd_handler), sizeof(input_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare); sizeof(struct cmd_handler), handler_compare);
} else if (block == CMD_BLOCK_SEAT) {
res = bsearch(&d, seat_handlers,
sizeof(seat_handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare);
} else { } else {
res = bsearch(&d, handlers, res = bsearch(&d, handlers,
sizeof(handlers) / sizeof(struct cmd_handler), sizeof(handlers) / sizeof(struct cmd_handler),

View file

@ -1,28 +0,0 @@
#define _XOPEN_SOURCE 700
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/input/input-manager.h"
#include "log.h"
struct cmd_results *input_cmd_seat(int argc, char **argv) {
sway_log(L_DEBUG, "seat for device: %d %s",
current_input_config==NULL, current_input_config->identifier);
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "seat",
"No input device defined.");
}
struct input_config *new_config =
new_input_config(current_input_config->identifier);
// TODO validate seat name
free(new_config->seat);
new_config->seat = strdup(argv[0]);
input_cmd_apply(new_config);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

35
sway/commands/seat.c Normal file
View file

@ -0,0 +1,35 @@
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/input/input-manager.h"
#include "log.h"
struct cmd_results *cmd_seat(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 2))) {
return error;
}
if (config->reading && strcmp("{", argv[1]) == 0) {
current_seat_config = new_seat_config(argv[0]);
sway_log(L_DEBUG, "entering seat block: %s", current_seat_config->name);
return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL);
}
if (argc > 2) {
int argc_new = argc-2;
char **argv_new = argv+2;
struct cmd_results *res;
current_seat_config = new_seat_config(argv[0]);
if (strcasecmp("attach", argv[1]) == 0) {
res = seat_cmd_attach(argc_new, argv_new);
} else {
res = cmd_results_new(CMD_INVALID, "seat <name>", "Unknown command %s", argv[1]);
}
current_seat_config = NULL;
return res;
}
return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL);
}

View file

@ -0,0 +1,26 @@
#define _XOPEN_SOURCE 700
#include <string.h>
#include <strings.h>
#include "sway/input/input-manager.h"
#include "sway/commands.h"
#include "sway/config.h"
#include "log.h"
#include "stringop.h"
struct cmd_results *seat_cmd_attach(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "attach", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (!current_seat_config) {
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 *new_attachment = seat_attachment_config_new();
new_attachment->identifier = strdup(argv[0]);
list_add(new_config->attachments, new_attachment);
seat_cmd_apply(new_config);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}

View file

@ -45,6 +45,7 @@ static void config_defaults(struct sway_config *config) {
if (!(config->criteria = create_list())) goto cleanup; if (!(config->criteria = create_list())) goto cleanup;
if (!(config->no_focus = create_list())) goto cleanup; if (!(config->no_focus = create_list())) goto cleanup;
if (!(config->input_configs = create_list())) goto cleanup; if (!(config->input_configs = create_list())) goto cleanup;
if (!(config->seat_configs = create_list())) goto cleanup;
if (!(config->output_configs = create_list())) goto cleanup; if (!(config->output_configs = create_list())) goto cleanup;
if (!(config->cmd_queue = create_list())) goto cleanup; if (!(config->cmd_queue = create_list())) goto cleanup;
@ -258,9 +259,7 @@ struct input_config *new_input_config(const char* identifier) {
void merge_input_config(struct input_config *dst, struct input_config *src) { void merge_input_config(struct input_config *dst, struct input_config *src) {
if (src->identifier) { if (src->identifier) {
if (dst->identifier) { free(dst->identifier);
free(dst->identifier);
}
dst->identifier = strdup(src->identifier); dst->identifier = strdup(src->identifier);
} }
if (src->accel_profile != INT_MIN) { if (src->accel_profile != INT_MIN) {
@ -300,7 +299,6 @@ void free_input_config(struct input_config *ic) {
return; return;
} }
free(ic->identifier); free(ic->identifier);
free(ic->seat);
free(ic); free(ic);
} }
@ -310,6 +308,128 @@ int input_identifier_cmp(const void *item, const void *data) {
return strcmp(ic->identifier, identifier); return strcmp(ic->identifier, identifier);
} }
struct seat_config *new_seat_config(const char* name) {
struct seat_config *seat = calloc(1, sizeof(struct seat_config));
if (!seat) {
sway_log(L_DEBUG, "Unable to allocate seat config");
return NULL;
}
sway_log(L_DEBUG, "new_seat_config(%s)", name);
seat->name = strdup(name);
if (!sway_assert(seat->name, "could not allocate name for seat")) {
return NULL;
}
seat->attachments = create_list();
if (!sway_assert(seat->attachments,
"could not allocate seat attachments list")) {
return NULL;
}
return seat;
}
struct seat_attachment_config *seat_attachment_config_new() {
struct seat_attachment_config *attachment =
calloc(1, sizeof(struct seat_attachment_config));
if (!attachment) {
sway_log(L_DEBUG, "cannot allocate attachment config");
return NULL;
}
return attachment;
}
static void seat_attachment_config_free(
struct seat_attachment_config *attachment) {
free(attachment->identifier);
free(attachment);
return;
}
static struct seat_attachment_config *seat_attachment_config_copy(
struct seat_attachment_config *attachment) {
struct seat_attachment_config *copy = seat_attachment_config_new();
if (!copy) {
return NULL;
}
copy->identifier = strdup(attachment->identifier);
return copy;
}
static void merge_seat_attachment_config(struct seat_attachment_config *dest,
struct seat_attachment_config *source) {
// nothing to merge yet, but there will be some day
}
void merge_seat_config(struct seat_config *dest, struct seat_config *source) {
if (source->name) {
free(dest->name);
dest->name = strdup(source->name);
}
for (int i = 0; i < source->attachments->length; ++i) {
struct seat_attachment_config *source_attachment =
source->attachments->items[i];
bool found = false;
for (int j = 0; j < dest->attachments->length; ++j) {
struct seat_attachment_config *dest_attachment =
dest->attachments->items[j];
if (strcmp(source_attachment->identifier,
dest_attachment->identifier) == 0) {
merge_seat_attachment_config(dest_attachment,
source_attachment);
found = true;
}
}
if (!found) {
struct seat_attachment_config *copy =
seat_attachment_config_copy(source_attachment);
if (copy) {
list_add(dest->attachments, copy);
}
}
}
}
void free_seat_config(struct seat_config *seat) {
if (!seat) {
return;
}
free(seat->name);
for (int i = 0; i < seat->attachments->length; ++i) {
struct seat_attachment_config *attachment =
seat->attachments->items[i];
seat_attachment_config_free(attachment);
}
list_free(seat->attachments);
free(seat);
}
int seat_name_cmp(const void *item, const void *data) {
const struct seat_config *sc = item;
const char *name = data;
return strcmp(sc->name, name);
}
struct seat_attachment_config *seat_config_get_attachment(
struct seat_config *seat_config, char *identifier) {
for (int i = 0; i < seat_config->attachments->length; ++i) {
struct seat_attachment_config *attachment =
seat_config->attachments->items[i];
if (strcmp(attachment->identifier, identifier) == 0) {
return attachment;
}
}
return NULL;
}
bool load_main_config(const char *file, bool is_active) { bool load_main_config(const char *file, bool is_active) {
char *path; char *path;
if (file != NULL) { if (file != NULL) {
@ -368,7 +488,7 @@ bool load_main_config(const char *file, bool is_active) {
char *_path = secconfigs->items[i]; char *_path = secconfigs->items[i];
if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 ||
(((s.st_mode & 0777) != 0644) && (((s.st_mode & 0777) != 0644) &&
(s.st_mode & 0777) != 0444)) { (s.st_mode & 0777) != 0444)) {
sway_log(L_ERROR, sway_log(L_ERROR,
"Refusing to load %s - it must be owned by root " "Refusing to load %s - it must be owned by root "
"and mode 644 or 444", _path); "and mode 644 or 444", _path);
@ -547,6 +667,14 @@ bool read_config(FILE *file, struct sway_config *config) {
} }
break; break;
case CMD_BLOCK_SEAT:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_SEAT;
} else {
sway_log(L_ERROR, "Invalid block '%s'", line);
}
break;
case CMD_BLOCK_BAR: case CMD_BLOCK_BAR:
if (block == CMD_BLOCK_END) { if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_BAR; block = CMD_BLOCK_BAR;
@ -601,6 +729,12 @@ bool read_config(FILE *file, struct sway_config *config) {
block = CMD_BLOCK_END; block = CMD_BLOCK_END;
break; break;
case CMD_BLOCK_SEAT:
sway_log(L_DEBUG, "End of seat block");
current_seat_config = NULL;
block = CMD_BLOCK_END;
break;
case CMD_BLOCK_BAR: case CMD_BLOCK_BAR:
sway_log(L_DEBUG, "End of bar block"); sway_log(L_DEBUG, "End of bar block");
config->current_bar = NULL; config->current_bar = NULL;

View file

@ -24,7 +24,7 @@ static void cursor_update_position(struct sway_cursor *cursor) {
static void cursor_send_pointer_motion(struct sway_cursor *cursor, static void cursor_send_pointer_motion(struct sway_cursor *cursor,
uint32_t time) { uint32_t time) {
struct wlr_seat *seat = cursor->seat->seat; struct wlr_seat *seat = cursor->seat->wlr_seat;
struct wlr_surface *surface = NULL; struct wlr_surface *surface = NULL;
double sx, sy; double sx, sy;
swayc_t *swayc = swayc_t *swayc =
@ -72,7 +72,7 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) {
sway_seat_set_focus(cursor->seat, swayc); sway_seat_set_focus(cursor->seat, swayc);
} }
wlr_seat_pointer_notify_button(cursor->seat->seat, event->time_msec, wlr_seat_pointer_notify_button(cursor->seat->wlr_seat, event->time_msec,
event->button, event->state); event->button, event->state);
} }
@ -80,7 +80,7 @@ static void handle_cursor_axis(struct wl_listener *listener, void *data) {
struct sway_cursor *cursor = struct sway_cursor *cursor =
wl_container_of(listener, cursor, axis); wl_container_of(listener, cursor, axis);
struct wlr_event_pointer_axis *event = data; struct wlr_event_pointer_axis *event = data;
wlr_seat_pointer_notify_axis(cursor->seat->seat, event->time_msec, wlr_seat_pointer_notify_axis(cursor->seat->wlr_seat, event->time_msec,
event->orientation, event->delta); event->orientation, event->delta);
} }
@ -173,7 +173,7 @@ struct sway_cursor *sway_cursor_create(struct sway_seat *seat) {
wl_signal_add(&wlr_cursor->events.tablet_tool_tip, &cursor->tool_tip); wl_signal_add(&wlr_cursor->events.tablet_tool_tip, &cursor->tool_tip);
cursor->tool_tip.notify = handle_tool_tip; cursor->tool_tip.notify = handle_tool_tip;
wl_signal_add(&seat->seat->events.request_set_cursor, wl_signal_add(&seat->wlr_seat->events.request_set_cursor,
&cursor->request_set_cursor); &cursor->request_set_cursor);
cursor->request_set_cursor.notify = handle_request_set_cursor; cursor->request_set_cursor.notify = handle_request_set_cursor;

View file

@ -18,12 +18,13 @@ static const char *default_seat = "seat0";
struct sway_input_manager *input_manager; struct sway_input_manager *input_manager;
struct input_config *current_input_config = NULL; struct input_config *current_input_config = NULL;
struct seat_config *current_seat_config = NULL;
static struct sway_seat *input_manager_get_seat( static struct sway_seat *input_manager_get_seat(
struct sway_input_manager *input, const char *seat_name) { struct sway_input_manager *input, const char *seat_name) {
struct sway_seat *seat = NULL; struct sway_seat *seat = NULL;
wl_list_for_each(seat, &input->seats, link) { wl_list_for_each(seat, &input->seats, link) {
if (strcmp(seat->seat->name, seat_name) == 0) { if (strcmp(seat->wlr_seat->name, seat_name) == 0) {
return seat; return seat;
} }
} }
@ -58,56 +59,88 @@ static char *get_device_identifier(struct wlr_input_device *device) {
return identifier; return identifier;
} }
static struct sway_input_device *input_sway_device_from_wlr(struct sway_input_manager *input, static struct sway_input_device *input_sway_device_from_wlr(
struct wlr_input_device *device) { struct sway_input_manager *input, struct wlr_input_device *device) {
struct sway_input_device *sway_device = NULL; struct sway_input_device *input_device = NULL;
wl_list_for_each(sway_device, &input->devices, link) { wl_list_for_each(input_device, &input->devices, link) {
if (sway_device->wlr_device == device) { if (input_device->wlr_device == device) {
return sway_device; return input_device;
} }
} }
return NULL; return NULL;
} }
static struct sway_input_device *input_sway_device_from_config(struct sway_input_manager *input, static struct sway_input_device *input_sway_device_from_config(
struct input_config *config) { struct sway_input_manager *input, struct input_config *config) {
struct sway_input_device *sway_device = NULL; struct sway_input_device *input_device = NULL;
wl_list_for_each(sway_device, &input->devices, link) { wl_list_for_each(input_device, &input->devices, link) {
if (strcmp(sway_device->identifier, config->identifier) == 0) { if (strcmp(input_device->identifier, config->identifier) == 0) {
return sway_device; return input_device;
} }
} }
return NULL; return NULL;
} }
static struct sway_input_device *input_sway_device_from_identifier(
struct sway_input_manager *input, char *identifier) {
struct sway_input_device *input_device = NULL;
wl_list_for_each(input_device, &input->devices, link) {
if (strcmp(input_device->identifier, identifier) == 0) {
return input_device;
}
}
return NULL;
}
static bool input_has_seat_configuration(struct sway_input_manager *input) {
struct sway_seat *seat = NULL;
wl_list_for_each(seat, &input->seats, link) {
if (seat->config) {
return true;
}
}
return false;
}
static void input_add_notify(struct wl_listener *listener, void *data) { static void input_add_notify(struct wl_listener *listener, void *data) {
struct sway_input_manager *input = struct sway_input_manager *input =
wl_container_of(listener, input, input_add); wl_container_of(listener, input, input_add);
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
struct sway_input_device *sway_device = struct sway_input_device *input_device =
calloc(1, sizeof(struct sway_input_device)); calloc(1, sizeof(struct sway_input_device));
if (!sway_assert(sway_device, "could not allocate input device")) { if (!sway_assert(input_device, "could not allocate input device")) {
return; return;
} }
sway_device->wlr_device = device; input_device->wlr_device = device;
sway_device->identifier = get_device_identifier(device); input_device->identifier = get_device_identifier(device);
wl_list_insert(&input->devices, &sway_device->link); wl_list_insert(&input->devices, &input_device->link);
// find config // find config
for (int i = 0; i < config->input_configs->length; ++i) { for (int i = 0; i < config->input_configs->length; ++i) {
struct input_config *input_config = config->input_configs->items[i]; struct input_config *input_config = config->input_configs->items[i];
if (strcmp(input_config->identifier, sway_device->identifier) == 0) { if (strcmp(input_config->identifier, input_device->identifier) == 0) {
sway_device->config = input_config; input_device->config = input_config;
break; break;
} }
} }
const char *seat_name = struct sway_seat *seat = NULL;
(sway_device->config ? sway_device->config->seat : default_seat); if (!input_has_seat_configuration(input)) {
struct sway_seat *seat = input_manager_get_seat(input, seat_name); seat = input_manager_get_seat(input, default_seat);
sway_seat_add_device(seat, sway_device); sway_seat_add_device(seat, input_device);
return;
}
wl_list_for_each(seat, &input->seats, link) {
if (seat->config &&
(seat_config_get_attachment(seat->config, input_device->identifier) ||
seat_config_get_attachment(seat->config, "*"))) {
sway_seat_add_device(seat, input_device);
}
}
} }
static void input_remove_notify(struct wl_listener *listener, void *data) { static void input_remove_notify(struct wl_listener *listener, void *data) {
@ -115,21 +148,21 @@ static void input_remove_notify(struct wl_listener *listener, void *data) {
wl_container_of(listener, input, input_remove); wl_container_of(listener, input, input_remove);
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
struct sway_input_device *sway_device = struct sway_input_device *input_device =
input_sway_device_from_wlr(input, device); input_sway_device_from_wlr(input, device);
if (!sway_assert(sway_device, "could not find sway device")) { if (!sway_assert(input_device, "could not find sway device")) {
return; return;
} }
struct sway_seat *seat = NULL; struct sway_seat *seat = NULL;
wl_list_for_each(seat, &input->seats, link) { wl_list_for_each(seat, &input->seats, link) {
sway_seat_remove_device(seat, sway_device); sway_seat_remove_device(seat, input_device);
} }
wl_list_remove(&sway_device->link); wl_list_remove(&input_device->link);
free(sway_device->identifier); free(input_device->identifier);
free(sway_device); free(input_device);
} }
struct sway_input_manager *sway_input_manager_create( struct sway_input_manager *sway_input_manager_create(
@ -139,7 +172,6 @@ struct sway_input_manager *sway_input_manager_create(
if (!input) { if (!input) {
return NULL; return NULL;
} }
// XXX probably don't need the full server
input->server = server; input->server = server;
wl_list_init(&input->devices); wl_list_init(&input->devices);
@ -177,22 +209,53 @@ void sway_input_manager_set_focus(struct sway_input_manager *input,
} }
} }
void sway_input_manager_apply_config(struct sway_input_manager *input, void sway_input_manager_apply_input_config(struct sway_input_manager *input,
struct input_config *input_config) { struct input_config *input_config) {
struct sway_input_device *sway_device = struct sway_input_device *input_device =
input_sway_device_from_config(input, input_config); input_sway_device_from_config(input, input_config);
if (!sway_device) { if (!input_device) {
return; return;
} }
input_device->config = input_config;
struct sway_seat *seat = NULL; struct sway_seat *seat = NULL;
wl_list_for_each(seat, &input->seats, link) { wl_list_for_each(seat, &input->seats, link) {
sway_seat_remove_device(seat, sway_device); sway_seat_configure_device(seat, input_device);
}
}
void sway_input_manager_apply_seat_config(struct sway_input_manager *input,
struct seat_config *seat_config) {
struct sway_seat *seat = input_manager_get_seat(input, seat_config->name);
// the old config is invalid so clear it
sway_seat_set_config(seat, NULL);
// clear devices
struct sway_input_device *input_device = NULL;
wl_list_for_each(input_device, &input->devices, link) {
sway_seat_remove_device(seat, input_device);
} }
const char *seat_name = (input_config->seat ? input_config->seat : default_seat); if (seat_config_get_attachment(seat_config, "*")) {
seat = input_manager_get_seat(input, seat_name); wl_list_for_each(input_device, &input->devices, link) {
sway_seat_add_device(seat, sway_device); sway_seat_add_device(seat, input_device);
}
} else {
for (int i = 0; i < seat_config->attachments->length; ++i) {
struct seat_attachment_config *attachment =
seat_config->attachments->items[i];
struct sway_input_device *device =
input_sway_device_from_identifier(input,
attachment->identifier);
if (device) {
sway_seat_add_device(seat, device);
}
}
}
sway_seat_set_config(seat, seat_config);
} }
void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { void sway_input_manager_configure_xcursor(struct sway_input_manager *input) {

View file

@ -5,32 +5,46 @@
static void handle_keyboard_key(struct wl_listener *listener, void *data) { static void handle_keyboard_key(struct wl_listener *listener, void *data) {
struct sway_keyboard *keyboard = struct sway_keyboard *keyboard =
wl_container_of(listener, keyboard, keyboard_key); wl_container_of(listener, keyboard, keyboard_key);
struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat;
struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;
struct wlr_event_keyboard_key *event = data; struct wlr_event_keyboard_key *event = data;
wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device->wlr_device); wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap);
wlr_seat_keyboard_notify_key(keyboard->seat->seat, event->time_msec, wlr_seat_set_keyboard(wlr_seat, wlr_device);
event->keycode, event->state); wlr_seat_keyboard_notify_key(wlr_seat, event->time_msec,event->keycode,
event->state);
} }
static void handle_keyboard_modifiers(struct wl_listener *listener, static void handle_keyboard_modifiers(struct wl_listener *listener,
void *data) { void *data) {
struct sway_keyboard *keyboard = struct sway_keyboard *keyboard =
wl_container_of(listener, keyboard, keyboard_modifiers); wl_container_of(listener, keyboard, keyboard_modifiers);
wlr_seat_set_keyboard(keyboard->seat->seat, keyboard->device->wlr_device); struct wlr_seat *wlr_seat = keyboard->seat_device->sway_seat->wlr_seat;
wlr_seat_keyboard_notify_modifiers(keyboard->seat->seat); struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;
wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap);
wlr_seat_set_keyboard(wlr_seat, wlr_device);
wlr_seat_keyboard_notify_modifiers(wlr_seat);
} }
struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
struct sway_input_device *device) { struct sway_seat_device *device) {
struct sway_keyboard *keyboard = struct sway_keyboard *keyboard =
calloc(1, sizeof(struct sway_keyboard)); calloc(1, sizeof(struct sway_keyboard));
if (!sway_assert(keyboard, "could not allocate sway keyboard")) { if (!sway_assert(keyboard, "could not allocate sway keyboard")) {
return NULL; return NULL;
} }
keyboard->device = device; keyboard->seat_device = device;
keyboard->seat = seat; device->keyboard = keyboard;
// TODO keyboard config wl_list_init(&keyboard->keyboard_key.link);
wl_list_init(&keyboard->keyboard_modifiers.link);
return keyboard;
}
void sway_keyboard_configure(struct sway_keyboard *keyboard) {
struct xkb_rule_names rules; struct xkb_rule_names rules;
memset(&rules, 0, sizeof(rules)); memset(&rules, 0, sizeof(rules));
rules.rules = getenv("XKB_DEFAULT_RULES"); rules.rules = getenv("XKB_DEFAULT_RULES");
@ -38,27 +52,32 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.layout = getenv("XKB_DEFAULT_LAYOUT");
rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.variant = getenv("XKB_DEFAULT_VARIANT");
rules.options = getenv("XKB_DEFAULT_OPTIONS"); rules.options = getenv("XKB_DEFAULT_OPTIONS");
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!sway_assert(context, "cannot create XKB context")) { if (!sway_assert(context, "cannot create XKB context")) {
return NULL; return;
} }
wlr_keyboard_set_keymap(device->wlr_device->keyboard, keyboard->keymap =
xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(keyboard->seat_device->input_device->wlr_device->keyboard, keyboard->keymap);
xkb_context_unref(context); xkb_context_unref(context);
wl_signal_add(&device->wlr_device->keyboard->events.key, wl_list_remove(&keyboard->keyboard_key.link);
wl_signal_add(
&keyboard->seat_device->input_device->wlr_device->keyboard->events.key,
&keyboard->keyboard_key); &keyboard->keyboard_key);
keyboard->keyboard_key.notify = handle_keyboard_key; keyboard->keyboard_key.notify = handle_keyboard_key;
wl_signal_add(&device->wlr_device->keyboard->events.modifiers, wl_list_remove(&keyboard->keyboard_modifiers.link);
wl_signal_add(
&keyboard->seat_device->input_device->wlr_device->keyboard->events.modifiers,
&keyboard->keyboard_modifiers); &keyboard->keyboard_modifiers);
keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers; keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers;
return keyboard;
} }
void sway_keyboard_destroy(struct sway_keyboard *keyboard) { void sway_keyboard_destroy(struct sway_keyboard *keyboard) {
xkb_keymap_unref(keyboard->keymap);
wl_list_remove(&keyboard->keyboard_key.link); wl_list_remove(&keyboard->keyboard_key.link);
wl_list_remove(&keyboard->keyboard_modifiers.link); wl_list_remove(&keyboard->keyboard_modifiers.link);
wl_list_remove(&keyboard->link); wl_list_remove(&keyboard->link);

View file

@ -9,6 +9,18 @@
#include "sway/view.h" #include "sway/view.h"
#include "log.h" #include "log.h"
static void seat_device_destroy(struct sway_seat_device *seat_device) {
if (!seat_device) {
return;
}
sway_keyboard_destroy(seat_device->keyboard);
wlr_cursor_detach_input_device(seat_device->sway_seat->cursor->cursor,
seat_device->input_device->wlr_device);
wl_list_remove(&seat_device->link);
free(seat_device);
}
struct sway_seat *sway_seat_create(struct sway_input_manager *input, struct sway_seat *sway_seat_create(struct sway_input_manager *input,
const char *seat_name) { const char *seat_name) {
struct sway_seat *seat = calloc(1, sizeof(struct sway_seat)); struct sway_seat *seat = calloc(1, sizeof(struct sway_seat));
@ -16,22 +28,22 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input,
return NULL; return NULL;
} }
seat->seat = wlr_seat_create(input->server->wl_display, seat_name); seat->wlr_seat = wlr_seat_create(input->server->wl_display, seat_name);
if (!sway_assert(seat->seat, "could not allocate seat")) { if (!sway_assert(seat->wlr_seat, "could not allocate seat")) {
return NULL; return NULL;
} }
seat->cursor = sway_cursor_create(seat); seat->cursor = sway_cursor_create(seat);
if (!seat->cursor) { if (!seat->cursor) {
wlr_seat_destroy(seat->seat); wlr_seat_destroy(seat->wlr_seat);
free(seat); free(seat);
return NULL; return NULL;
} }
seat->input = input; seat->input = input;
seat->devices = create_list(); wl_list_init(&seat->devices);
wlr_seat_set_capabilities(seat->seat, wlr_seat_set_capabilities(seat->wlr_seat,
WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_KEYBOARD |
WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_POINTER |
WL_SEAT_CAPABILITY_TOUCH); WL_SEAT_CAPABILITY_TOUCH);
@ -43,88 +55,94 @@ struct sway_seat *sway_seat_create(struct sway_input_manager *input,
return seat; return seat;
} }
static void seat_add_pointer(struct sway_seat *seat, static void seat_configure_pointer(struct sway_seat *seat,
struct sway_input_device *sway_device) { struct sway_seat_device *sway_device) {
// TODO pointer configuration // TODO pointer configuration
wlr_cursor_attach_input_device(seat->cursor->cursor, wlr_cursor_attach_input_device(seat->cursor->cursor,
sway_device->wlr_device); sway_device->input_device->wlr_device);
} }
static void seat_add_keyboard(struct sway_seat *seat, static void seat_configure_keyboard(struct sway_seat *seat,
struct sway_input_device *device) { struct sway_seat_device *seat_device) {
// TODO keyboard configuration if (!seat_device->keyboard) {
sway_keyboard_create(seat, device); sway_keyboard_create(seat, seat_device);
wlr_seat_set_keyboard(seat->seat, device->wlr_device); }
sway_keyboard_configure(seat_device->keyboard);
} }
bool sway_seat_has_device(struct sway_seat *seat, static struct sway_seat_device *sway_seat_get_device(struct sway_seat *seat,
struct sway_input_device *device) { struct sway_input_device *input_device) {
return false; struct sway_seat_device *seat_device = NULL;
wl_list_for_each(seat_device, &seat->devices, link) {
if (seat_device->input_device == input_device) {
return seat_device;
}
}
return NULL;
}
void sway_seat_configure_device(struct sway_seat *seat,
struct sway_input_device *input_device) {
struct sway_seat_device *seat_device =
sway_seat_get_device(seat, input_device);
if (!seat_device) {
return;
}
if (seat->config) {
seat_device->attachment_config =
seat_config_get_attachment(seat->config, input_device->identifier);
}
switch (input_device->wlr_device->type) {
case WLR_INPUT_DEVICE_POINTER:
seat_configure_pointer(seat, seat_device);
break;
case WLR_INPUT_DEVICE_KEYBOARD:
seat_configure_keyboard(seat, seat_device);
wlr_seat_set_keyboard(seat->wlr_seat,
seat_device->input_device->wlr_device);
break;
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_TABLET_TOOL:
sway_log(L_DEBUG, "TODO: configure other devices");
break;
}
} }
void sway_seat_add_device(struct sway_seat *seat, void sway_seat_add_device(struct sway_seat *seat,
struct sway_input_device *device) { struct sway_input_device *input_device) {
if (sway_seat_has_device(seat, device)) { if (sway_seat_get_device(seat, input_device)) {
return; return;
} }
sway_log(L_DEBUG, "input add: %s", device->identifier); struct sway_seat_device *seat_device =
switch (device->wlr_device->type) { calloc(1, sizeof(struct sway_seat_device));
case WLR_INPUT_DEVICE_POINTER: if (!seat_device) {
seat_add_pointer(seat, device); sway_log(L_DEBUG, "could not allocate seat device");
break; return;
case WLR_INPUT_DEVICE_KEYBOARD:
seat_add_keyboard(seat, device);
break;
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_TABLET_TOOL:
sway_log(L_DEBUG, "TODO: add other devices");
break;
} }
list_add(seat->devices, device); seat_device->sway_seat = seat;
} seat_device->input_device = input_device;
wl_list_insert(&seat->devices, &seat_device->link);
static void seat_remove_keyboard(struct sway_seat *seat, sway_seat_configure_device(seat, input_device);
struct sway_input_device *device) {
if (device && device->keyboard) {
sway_keyboard_destroy(device->keyboard);
}
}
static void seat_remove_pointer(struct sway_seat *seat,
struct sway_input_device *device) {
wlr_cursor_detach_input_device(seat->cursor->cursor, device->wlr_device);
} }
void sway_seat_remove_device(struct sway_seat *seat, void sway_seat_remove_device(struct sway_seat *seat,
struct sway_input_device *device) { struct sway_input_device *input_device) {
sway_log(L_DEBUG, "input remove: %s", device->identifier); sway_log(L_DEBUG, "input remove: %s", input_device->identifier);
if (!sway_seat_has_device(seat, device)) { struct sway_seat_device *seat_device =
sway_seat_get_device(seat, input_device);
if (!seat_device) {
return; return;
} }
switch (device->wlr_device->type) { seat_device_destroy(seat_device);
case WLR_INPUT_DEVICE_POINTER:
seat_remove_pointer(seat, device);
break;
case WLR_INPUT_DEVICE_KEYBOARD:
seat_remove_keyboard(seat, device);
break;
case WLR_INPUT_DEVICE_TOUCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_TABLET_TOOL:
sway_log(L_DEBUG, "TODO: remove other devices");
break;
}
for (int i = 0; i < seat->devices->length; ++i) {
if (seat->devices->items[i] == device) {
list_del(seat->devices, i);
break;
}
}
} }
void sway_seat_configure_xcursor(struct sway_seat *seat) { void sway_seat_configure_xcursor(struct sway_seat *seat) {
@ -135,7 +153,8 @@ void sway_seat_configure_xcursor(struct sway_seat *seat) {
seat->cursor->xcursor_manager = seat->cursor->xcursor_manager =
wlr_xcursor_manager_create("default", 24); wlr_xcursor_manager_create("default", 24);
if (sway_assert(seat->cursor->xcursor_manager, if (sway_assert(seat->cursor->xcursor_manager,
"Cannot create XCursor manager for theme %s", cursor_theme)) { "Cannot create XCursor manager for theme %s",
cursor_theme)) {
return; return;
} }
} }
@ -183,7 +202,7 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) {
view->iface.set_activated(view, true); view->iface.set_activated(view, true);
wl_signal_add(&container->events.destroy, &seat->focus_destroy); wl_signal_add(&container->events.destroy, &seat->focus_destroy);
seat->focus_destroy.notify = handle_focus_destroy; seat->focus_destroy.notify = handle_focus_destroy;
wlr_seat_keyboard_notify_enter(seat->seat, view->surface); wlr_seat_keyboard_notify_enter(seat->wlr_seat, view->surface);
} }
seat->focus = container; seat->focus = container;
@ -195,3 +214,29 @@ void sway_seat_set_focus(struct sway_seat *seat, swayc_t *container) {
} }
} }
void sway_seat_set_config(struct sway_seat *seat,
struct seat_config *seat_config) {
// clear configs
seat->config = NULL;
struct sway_seat_device *seat_device = NULL;
wl_list_for_each(seat_device, &seat->devices, link) {
seat_device->attachment_config = NULL;
}
if (!seat_config) {
return;
}
// add configs
seat->config = seat_config;
wl_list_for_each(seat_device, &seat->devices, link) {
seat_device->attachment_config =
seat_config_get_attachment(seat_config,
seat_device->input_device->identifier);
sway_seat_configure_device(seat, seat_device->input_device);
}
}

View file

@ -11,7 +11,8 @@ sway_sources = files(
'commands/exec_always.c', 'commands/exec_always.c',
'commands/include.c', 'commands/include.c',
'commands/input.c', 'commands/input.c',
'commands/input/seat.c', 'commands/seat.c',
'commands/seat/attach.c',
'commands/input/accel_profile.c', 'commands/input/accel_profile.c',
'commands/input/click_method.c', 'commands/input/click_method.c',
'commands/input/drag_lock.c', 'commands/input/drag_lock.c',