From f4a5a0ead4c8b155985c242db1fa5de5fa4807a0 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Sat, 16 Dec 2017 11:25:59 -0500 Subject: [PATCH] put seat and input config in their own files --- sway/config.c | 222 -------------------------------------------- sway/config/input.c | 105 +++++++++++++++++++++ sway/config/seat.c | 127 +++++++++++++++++++++++++ sway/meson.build | 2 + 4 files changed, 234 insertions(+), 222 deletions(-) create mode 100644 sway/config/input.c create mode 100644 sway/config/seat.c diff --git a/sway/config.c b/sway/config.c index 4e34aa8c..b591ae9e 100644 --- a/sway/config.c +++ b/sway/config.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #ifdef __linux__ @@ -229,227 +228,6 @@ static int qstrcmp(const void* a, const void* b) { return strcmp(*((char**) a), *((char**) b)); } -struct input_config *new_input_config(const char* identifier) { - struct input_config *input = calloc(1, sizeof(struct input_config)); - if (!input) { - sway_log(L_DEBUG, "Unable to allocate input config"); - return NULL; - } - sway_log(L_DEBUG, "new_input_config(%s)", identifier); - if (!(input->identifier = strdup(identifier))) { - free(input); - sway_log(L_DEBUG, "Unable to allocate input config"); - return NULL; - } - - input->tap = INT_MIN; - input->drag_lock = INT_MIN; - input->dwt = INT_MIN; - input->send_events = INT_MIN; - input->click_method = INT_MIN; - input->middle_emulation = INT_MIN; - input->natural_scroll = INT_MIN; - input->accel_profile = INT_MIN; - input->pointer_accel = FLT_MIN; - input->scroll_method = INT_MIN; - input->left_handed = INT_MIN; - - return input; -} - -void merge_input_config(struct input_config *dst, struct input_config *src) { - if (src->identifier) { - free(dst->identifier); - dst->identifier = strdup(src->identifier); - } - if (src->accel_profile != INT_MIN) { - dst->accel_profile = src->accel_profile; - } - if (src->click_method != INT_MIN) { - dst->click_method = src->click_method; - } - if (src->drag_lock != INT_MIN) { - dst->drag_lock = src->drag_lock; - } - if (src->dwt != INT_MIN) { - dst->dwt = src->dwt; - } - if (src->middle_emulation != INT_MIN) { - dst->middle_emulation = src->middle_emulation; - } - if (src->natural_scroll != INT_MIN) { - dst->natural_scroll = src->natural_scroll; - } - if (src->pointer_accel != FLT_MIN) { - dst->pointer_accel = src->pointer_accel; - } - if (src->scroll_method != INT_MIN) { - dst->scroll_method = src->scroll_method; - } - if (src->send_events != INT_MIN) { - dst->send_events = src->send_events; - } - if (src->tap != INT_MIN) { - dst->tap = src->tap; - } - if (src->xkb_layout) { - free(dst->xkb_layout); - dst->xkb_layout = strdup(src->xkb_layout); - } - if (src->xkb_model) { - free(dst->xkb_model); - dst->xkb_model = strdup(src->xkb_model); - } - if (src->xkb_options) { - free(dst->xkb_options); - dst->xkb_options = strdup(src->xkb_options); - } - if (src->xkb_rules) { - free(dst->xkb_rules); - dst->xkb_rules = strdup(src->xkb_rules); - } - if (src->xkb_variant) { - free(dst->xkb_variant); - dst->xkb_variant = strdup(src->xkb_variant); - } -} - -void free_input_config(struct input_config *ic) { - if (!ic) { - return; - } - free(ic->identifier); - free(ic); -} - -int input_identifier_cmp(const void *item, const void *data) { - const struct input_config *ic = item; - const char *identifier = data; - 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) { char *path; if (file != NULL) { diff --git a/sway/config/input.c b/sway/config/input.c new file mode 100644 index 00000000..6f8d31f7 --- /dev/null +++ b/sway/config/input.c @@ -0,0 +1,105 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include "sway/config.h" +#include "log.h" + +struct input_config *new_input_config(const char* identifier) { + struct input_config *input = calloc(1, sizeof(struct input_config)); + if (!input) { + sway_log(L_DEBUG, "Unable to allocate input config"); + return NULL; + } + sway_log(L_DEBUG, "new_input_config(%s)", identifier); + if (!(input->identifier = strdup(identifier))) { + free(input); + sway_log(L_DEBUG, "Unable to allocate input config"); + return NULL; + } + + input->tap = INT_MIN; + input->drag_lock = INT_MIN; + input->dwt = INT_MIN; + input->send_events = INT_MIN; + input->click_method = INT_MIN; + input->middle_emulation = INT_MIN; + input->natural_scroll = INT_MIN; + input->accel_profile = INT_MIN; + input->pointer_accel = FLT_MIN; + input->scroll_method = INT_MIN; + input->left_handed = INT_MIN; + + return input; +} + +void merge_input_config(struct input_config *dst, struct input_config *src) { + if (src->identifier) { + free(dst->identifier); + dst->identifier = strdup(src->identifier); + } + if (src->accel_profile != INT_MIN) { + dst->accel_profile = src->accel_profile; + } + if (src->click_method != INT_MIN) { + dst->click_method = src->click_method; + } + if (src->drag_lock != INT_MIN) { + dst->drag_lock = src->drag_lock; + } + if (src->dwt != INT_MIN) { + dst->dwt = src->dwt; + } + if (src->middle_emulation != INT_MIN) { + dst->middle_emulation = src->middle_emulation; + } + if (src->natural_scroll != INT_MIN) { + dst->natural_scroll = src->natural_scroll; + } + if (src->pointer_accel != FLT_MIN) { + dst->pointer_accel = src->pointer_accel; + } + if (src->scroll_method != INT_MIN) { + dst->scroll_method = src->scroll_method; + } + if (src->send_events != INT_MIN) { + dst->send_events = src->send_events; + } + if (src->tap != INT_MIN) { + dst->tap = src->tap; + } + if (src->xkb_layout) { + free(dst->xkb_layout); + dst->xkb_layout = strdup(src->xkb_layout); + } + if (src->xkb_model) { + free(dst->xkb_model); + dst->xkb_model = strdup(src->xkb_model); + } + if (src->xkb_options) { + free(dst->xkb_options); + dst->xkb_options = strdup(src->xkb_options); + } + if (src->xkb_rules) { + free(dst->xkb_rules); + dst->xkb_rules = strdup(src->xkb_rules); + } + if (src->xkb_variant) { + free(dst->xkb_variant); + dst->xkb_variant = strdup(src->xkb_variant); + } +} + +void free_input_config(struct input_config *ic) { + if (!ic) { + return; + } + free(ic->identifier); + free(ic); +} + +int input_identifier_cmp(const void *item, const void *data) { + const struct input_config *ic = item; + const char *identifier = data; + return strcmp(ic->identifier, identifier); +} diff --git a/sway/config/seat.c b/sway/config/seat.c new file mode 100644 index 00000000..3a2fdaa6 --- /dev/null +++ b/sway/config/seat.c @@ -0,0 +1,127 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include "sway/config.h" +#include "log.h" + +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; +} diff --git a/sway/meson.build b/sway/meson.build index c1f75b13..3d38c7c9 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -32,6 +32,8 @@ sway_sources = files( 'commands/output.c', 'config.c', 'config/output.c', + 'config/seat.c', + 'config/input.c', 'ipc-json.c', 'ipc-server.c', 'desktop/output.c',