Merge pull request #411 from acrisci/refactor/config-cleanup

Refactor: cleanup config
This commit is contained in:
Drew DeVault 2017-11-11 11:04:36 -05:00 committed by GitHub
commit 2bee288090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 73 additions and 59 deletions

View File

@ -2,9 +2,8 @@
#define _ROOTSTON_CONFIG_H #define _ROOTSTON_CONFIG_H
#include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_input_device.h> #include <wlr/types/wlr_input_device.h>
#include <wlr/types/wlr_cursor.h>
struct output_config { struct roots_output_config {
char *name; char *name;
enum wl_output_transform transform; enum wl_output_transform transform;
int x, y; int x, y;
@ -16,7 +15,7 @@ struct output_config {
} mode; } mode;
}; };
struct device_config { struct roots_device_config {
char *name; char *name;
char *mapped_output; char *mapped_output;
struct wlr_box *mapped_box; struct wlr_box *mapped_box;
@ -24,7 +23,7 @@ struct device_config {
struct wl_list link; struct wl_list link;
}; };
struct binding_config { struct roots_binding_config {
uint32_t modifiers; uint32_t modifiers;
xkb_keysym_t *keysyms; xkb_keysym_t *keysyms;
size_t keysyms_len; size_t keysyms_len;
@ -32,7 +31,7 @@ struct binding_config {
struct wl_list link; struct wl_list link;
}; };
struct keyboard_config { struct roots_keyboard_config {
char *name; char *name;
uint32_t meta_key; uint32_t meta_key;
char *rules; char *rules;
@ -45,7 +44,7 @@ struct keyboard_config {
struct roots_config { struct roots_config {
bool xwayland; bool xwayland;
// TODO: Multiple cursors, multiseat
struct { struct {
char *mapped_output; char *mapped_output;
struct wlr_box *mapped_box; struct wlr_box *mapped_box;
@ -59,29 +58,37 @@ struct roots_config {
char *startup_cmd; char *startup_cmd;
}; };
struct roots_config *parse_args(int argc, char *argv[]); /**
* Create a roots config from the given command line arguments. Command line
* arguments can specify the location of the config file. If it is not
* specified, the default location will be used.
*/
struct roots_config *roots_config_create_from_args(int argc, char *argv[]);
/**
* Destroy the config and free its resources.
*/
void roots_config_destroy(struct roots_config *config); void roots_config_destroy(struct roots_config *config);
/** /**
* Get configuration for the output. If the output is not configured, returns * Get configuration for the output. If the output is not configured, returns
* NULL. * NULL.
*/ */
struct output_config *config_get_output(struct roots_config *config, struct roots_output_config *roots_config_get_output(struct roots_config *config,
struct wlr_output *output); struct wlr_output *output);
/** /**
* Get configuration for the device. If the device is not configured, returns * Get configuration for the device. If the device is not configured, returns
* NULL. * NULL.
*/ */
struct device_config *config_get_device(struct roots_config *config, struct roots_device_config *roots_config_get_device(struct roots_config *config,
struct wlr_input_device *device); struct wlr_input_device *device);
/** /**
* Get configuration for the keyboard. If the keyboard is not configured, * Get configuration for the keyboard. If the keyboard is not configured,
* returns NULL. A NULL device returns the default config for keyboards. * returns NULL. A NULL device returns the default config for keyboards.
*/ */
struct keyboard_config *config_get_keyboard(struct roots_config *config, struct roots_keyboard_config *roots_config_get_keyboard(
struct wlr_input_device *device); struct roots_config *config, struct wlr_input_device *device);
#endif #endif

View File

@ -10,7 +10,7 @@ struct roots_keyboard {
struct roots_input *input; struct roots_input *input;
struct roots_seat *seat; struct roots_seat *seat;
struct wlr_input_device *device; struct wlr_input_device *device;
struct keyboard_config *config; struct roots_keyboard_config *config;
struct wl_list link; struct wl_list link;
struct wl_listener keyboard_key; struct wl_listener keyboard_key;

View File

@ -115,7 +115,8 @@ static uint32_t parse_modifier(const char *symname) {
void add_binding_config(struct wl_list *bindings, const char* combination, void add_binding_config(struct wl_list *bindings, const char* combination,
const char* command) { const char* command) {
struct binding_config *bc = calloc(1, sizeof(struct binding_config)); struct roots_binding_config *bc =
calloc(1, sizeof(struct roots_binding_config));
xkb_keysym_t keysyms[ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP]; xkb_keysym_t keysyms[ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP];
char *symnames = strdup(combination); char *symnames = strdup(combination);
@ -151,7 +152,7 @@ void add_binding_config(struct wl_list *bindings, const char* combination,
static void config_handle_keyboard(struct roots_config *config, static void config_handle_keyboard(struct roots_config *config,
const char *device_name, const char *name, const char *value) { const char *device_name, const char *name, const char *value) {
struct keyboard_config *kc; struct roots_keyboard_config *kc;
bool found = false; bool found = false;
wl_list_for_each(kc, &config->keyboards, link) { wl_list_for_each(kc, &config->keyboards, link) {
if (strcmp(kc->name, device_name) == 0) { if (strcmp(kc->name, device_name) == 0) {
@ -161,7 +162,7 @@ static void config_handle_keyboard(struct roots_config *config,
} }
if (!found) { if (!found) {
kc = calloc(1, sizeof(struct keyboard_config)); kc = calloc(1, sizeof(struct roots_keyboard_config));
kc->name = strdup(device_name); kc->name = strdup(device_name);
wl_list_insert(&config->keyboards, &kc->link); wl_list_insert(&config->keyboards, &kc->link);
} }
@ -207,7 +208,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
} }
} else if (strncmp(output_prefix, section, strlen(output_prefix)) == 0) { } else if (strncmp(output_prefix, section, strlen(output_prefix)) == 0) {
const char *output_name = section + strlen(output_prefix); const char *output_name = section + strlen(output_prefix);
struct output_config *oc; struct roots_output_config *oc;
bool found = false; bool found = false;
wl_list_for_each(oc, &config->outputs, link) { wl_list_for_each(oc, &config->outputs, link) {
@ -218,7 +219,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
} }
if (!found) { if (!found) {
oc = calloc(1, sizeof(struct output_config)); oc = calloc(1, sizeof(struct roots_output_config));
oc->name = strdup(output_name); oc->name = strdup(output_name);
oc->transform = WL_OUTPUT_TRANSFORM_NORMAL; oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
oc->scale = 1; oc->scale = 1;
@ -281,7 +282,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
} else if (strncmp(device_prefix, section, strlen(device_prefix)) == 0) { } else if (strncmp(device_prefix, section, strlen(device_prefix)) == 0) {
const char *device_name = section + strlen(device_prefix); const char *device_name = section + strlen(device_prefix);
struct device_config *dc; struct roots_device_config *dc;
bool found = false; bool found = false;
wl_list_for_each(dc, &config->devices, link) { wl_list_for_each(dc, &config->devices, link) {
if (strcmp(dc->name, device_name) == 0) { if (strcmp(dc->name, device_name) == 0) {
@ -291,7 +292,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
} }
if (!found) { if (!found) {
dc = calloc(1, sizeof(struct device_config)); dc = calloc(1, sizeof(struct roots_device_config));
dc->name = strdup(device_name); dc->name = strdup(device_name);
dc->seat = strdup("seat0"); dc->seat = strdup("seat0");
wl_list_insert(&config->devices, &dc->link); wl_list_insert(&config->devices, &dc->link);
@ -311,7 +312,8 @@ static int config_ini_handler(void *user, const char *section, const char *name,
} }
} else if (strcmp(section, "keyboard") == 0) { } else if (strcmp(section, "keyboard") == 0) {
config_handle_keyboard(config, "", name, value); config_handle_keyboard(config, "", name, value);
} else if (strncmp(keyboard_prefix, section, strlen(keyboard_prefix)) == 0) { } else if (strncmp(keyboard_prefix,
section, strlen(keyboard_prefix)) == 0) {
const char *device_name = section + strlen(keyboard_prefix); const char *device_name = section + strlen(keyboard_prefix);
config_handle_keyboard(config, device_name, name, value); config_handle_keyboard(config, device_name, name, value);
} else if (strcmp(section, "bindings") == 0) { } else if (strcmp(section, "bindings") == 0) {
@ -323,7 +325,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
return 1; return 1;
} }
struct roots_config *parse_args(int argc, char *argv[]) { struct roots_config *roots_config_create_from_args(int argc, char *argv[]) {
struct roots_config *config = calloc(1, sizeof(struct roots_config)); struct roots_config *config = calloc(1, sizeof(struct roots_config));
if (config == NULL) { if (config == NULL) {
return NULL; return NULL;
@ -370,7 +372,8 @@ struct roots_config *parse_args(int argc, char *argv[]) {
add_binding_config(&config->bindings, "Logo+Shift+E", "exit"); add_binding_config(&config->bindings, "Logo+Shift+E", "exit");
add_binding_config(&config->bindings, "Ctrl+q", "close"); add_binding_config(&config->bindings, "Ctrl+q", "close");
add_binding_config(&config->bindings, "Alt+Tab", "next_window"); add_binding_config(&config->bindings, "Alt+Tab", "next_window");
struct keyboard_config *kc = calloc(1, sizeof(struct keyboard_config)); struct roots_keyboard_config *kc =
calloc(1, sizeof(struct roots_keyboard_config));
kc->meta_key = WLR_MODIFIER_LOGO; kc->meta_key = WLR_MODIFIER_LOGO;
kc->name = strdup(""); kc->name = strdup("");
wl_list_insert(&config->keyboards, &kc->link); wl_list_insert(&config->keyboards, &kc->link);
@ -386,13 +389,13 @@ struct roots_config *parse_args(int argc, char *argv[]) {
} }
void roots_config_destroy(struct roots_config *config) { void roots_config_destroy(struct roots_config *config) {
struct output_config *oc, *otmp = NULL; struct roots_output_config *oc, *otmp = NULL;
wl_list_for_each_safe(oc, otmp, &config->outputs, link) { wl_list_for_each_safe(oc, otmp, &config->outputs, link) {
free(oc->name); free(oc->name);
free(oc); free(oc);
} }
struct device_config *dc, *dtmp = NULL; struct roots_device_config *dc, *dtmp = NULL;
wl_list_for_each_safe(dc, dtmp, &config->devices, link) { wl_list_for_each_safe(dc, dtmp, &config->devices, link) {
free(dc->name); free(dc->name);
free(dc->seat); free(dc->seat);
@ -401,7 +404,7 @@ void roots_config_destroy(struct roots_config *config) {
free(dc); free(dc);
} }
struct keyboard_config *kc, *ktmp = NULL; struct roots_keyboard_config *kc, *ktmp = NULL;
wl_list_for_each_safe(kc, ktmp, &config->bindings, link) { wl_list_for_each_safe(kc, ktmp, &config->bindings, link) {
free(kc->name); free(kc->name);
free(kc->rules); free(kc->rules);
@ -412,7 +415,7 @@ void roots_config_destroy(struct roots_config *config) {
free(kc); free(kc);
} }
struct binding_config *bc, *btmp = NULL; struct roots_binding_config *bc, *btmp = NULL;
wl_list_for_each_safe(bc, btmp, &config->bindings, link) { wl_list_for_each_safe(bc, btmp, &config->bindings, link) {
free(bc->keysyms); free(bc->keysyms);
free(bc->command); free(bc->command);
@ -425,9 +428,9 @@ void roots_config_destroy(struct roots_config *config) {
free(config); free(config);
} }
struct output_config *config_get_output(struct roots_config *config, struct roots_output_config *roots_config_get_output(struct roots_config *config,
struct wlr_output *output) { struct wlr_output *output) {
struct output_config *o_config; struct roots_output_config *o_config;
wl_list_for_each(o_config, &config->outputs, link) { wl_list_for_each(o_config, &config->outputs, link) {
if (strcmp(o_config->name, output->name) == 0) { if (strcmp(o_config->name, output->name) == 0) {
return o_config; return o_config;
@ -437,9 +440,9 @@ struct output_config *config_get_output(struct roots_config *config,
return NULL; return NULL;
} }
struct device_config *config_get_device(struct roots_config *config, struct roots_device_config *roots_config_get_device(struct roots_config *config,
struct wlr_input_device *device) { struct wlr_input_device *device) {
struct device_config *d_config; struct roots_device_config *d_config;
wl_list_for_each(d_config, &config->devices, link) { wl_list_for_each(d_config, &config->devices, link) {
if (strcmp(d_config->name, device->name) == 0) { if (strcmp(d_config->name, device->name) == 0) {
return d_config; return d_config;
@ -449,9 +452,9 @@ struct device_config *config_get_device(struct roots_config *config,
return NULL; return NULL;
} }
struct keyboard_config *config_get_keyboard(struct roots_config *config, struct roots_keyboard_config *roots_config_get_keyboard(
struct wlr_input_device *device) { struct roots_config *config, struct wlr_input_device *device) {
struct keyboard_config *kc; struct roots_keyboard_config *kc;
wl_list_for_each(kc, &config->keyboards, link) { wl_list_for_each(kc, &config->keyboards, link) {
if ((device != NULL && strcmp(kc->name, device->name) == 0) || if ((device != NULL && strcmp(kc->name, device->name) == 0) ||
(device == NULL && strcmp(kc->name, "") == 0)) { (device == NULL && strcmp(kc->name, "") == 0)) {

View File

@ -44,7 +44,8 @@ static void input_add_notify(struct wl_listener *listener, void *data) {
struct roots_input *input = wl_container_of(listener, input, input_add); struct roots_input *input = wl_container_of(listener, input, input_add);
char *seat_name = "seat0"; char *seat_name = "seat0";
struct device_config *dc = config_get_device(input->config, device); struct roots_device_config *dc =
roots_config_get_device(input->config, device);
if (dc) { if (dc) {
seat_name = dc->seat; seat_name = dc->seat;
} }

View File

@ -92,7 +92,7 @@ static bool keyboard_keysym_press(struct roots_keyboard *keyboard,
uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard);
struct wl_list *bindings = &keyboard->input->server->config->bindings; struct wl_list *bindings = &keyboard->input->server->config->bindings;
struct binding_config *bc; struct roots_binding_config *bc;
wl_list_for_each(bc, bindings, link) { wl_list_for_each(bc, bindings, link) {
if (modifiers ^ bc->modifiers) { if (modifiers ^ bc->modifiers) {
continue; continue;
@ -210,8 +210,8 @@ void roots_keyboard_handle_modifiers(struct roots_keyboard *r_keyboard) {
wlr_seat_keyboard_notify_modifiers(seat); wlr_seat_keyboard_notify_modifiers(seat);
} }
static void keyboard_config_merge(struct keyboard_config *config, static void keyboard_config_merge(struct roots_keyboard_config *config,
struct keyboard_config *fallback) { struct roots_keyboard_config *fallback) {
if (fallback == NULL) { if (fallback == NULL) {
return; return;
} }
@ -248,15 +248,16 @@ struct roots_keyboard *roots_keyboard_create(struct wlr_input_device *device,
keyboard->device = device; keyboard->device = device;
keyboard->input = input; keyboard->input = input;
struct keyboard_config *config = calloc(1, sizeof(struct keyboard_config)); struct roots_keyboard_config *config =
calloc(1, sizeof(struct roots_keyboard_config));
if (config == NULL) { if (config == NULL) {
free(keyboard); free(keyboard);
return NULL; return NULL;
} }
keyboard_config_merge(config, config_get_keyboard(input->config, device)); keyboard_config_merge(config, roots_config_get_keyboard(input->config, device));
keyboard_config_merge(config, config_get_keyboard(input->config, NULL)); keyboard_config_merge(config, roots_config_get_keyboard(input->config, NULL));
struct keyboard_config env_config = { struct roots_keyboard_config env_config = {
.rules = getenv("XKB_DEFAULT_RULES"), .rules = getenv("XKB_DEFAULT_RULES"),
.model = getenv("XKB_DEFAULT_MODEL"), .model = getenv("XKB_DEFAULT_MODEL"),
.layout = getenv("XKB_DEFAULT_LAYOUT"), .layout = getenv("XKB_DEFAULT_LAYOUT"),

View File

@ -13,7 +13,7 @@
struct roots_server server = { 0 }; struct roots_server server = { 0 };
int main(int argc, char **argv) { int main(int argc, char **argv) {
assert(server.config = parse_args(argc, argv)); assert(server.config = roots_config_create_from_args(argc, argv));
assert(server.wl_display = wl_display_create()); assert(server.wl_display = wl_display_create());
assert(server.wl_event_loop = wl_display_get_event_loop(server.wl_display)); assert(server.wl_event_loop = wl_display_get_event_loop(server.wl_display));

View File

@ -181,7 +181,8 @@ static void output_frame_notify(struct wl_listener *listener, void *data) {
output->last_frame = desktop->last_frame = now; output->last_frame = desktop->last_frame = now;
} }
static void set_mode(struct wlr_output *output, struct output_config *oc) { static void set_mode(struct wlr_output *output,
struct roots_output_config *oc) {
struct wlr_output_mode *mode, *best = NULL; struct wlr_output_mode *mode, *best = NULL;
int mhz = (int)(oc->mode.refresh_rate * 1000); int mhz = (int)(oc->mode.refresh_rate * 1000);
wl_list_for_each(mode, &output->modes, link) { wl_list_for_each(mode, &output->modes, link) {
@ -225,7 +226,8 @@ void output_add_notify(struct wl_listener *listener, void *data) {
wl_signal_add(&wlr_output->events.frame, &output->frame); wl_signal_add(&wlr_output->events.frame, &output->frame);
wl_list_insert(&desktop->outputs, &output->link); wl_list_insert(&desktop->outputs, &output->link);
struct output_config *output_config = config_get_output(config, wlr_output); struct roots_output_config *output_config =
roots_config_get_output(config, wlr_output);
if (output_config) { if (output_config) {
if (output_config->mode.width) { if (output_config->mode.width) {
set_mode(wlr_output, output_config); set_mode(wlr_output, output_config);

View File

@ -117,8 +117,8 @@ static void seat_reset_device_mappings(struct roots_seat *seat, struct wlr_input
struct roots_config *config = seat->input->config; struct roots_config *config = seat->input->config;
wlr_cursor_map_input_to_output(cursor, device, NULL); wlr_cursor_map_input_to_output(cursor, device, NULL);
struct device_config *dconfig; struct roots_device_config *dconfig;
if ((dconfig = config_get_device(config, device))) { if ((dconfig = roots_config_get_device(config, device))) {
wlr_cursor_map_input_to_region(cursor, device, dconfig->mapped_box); wlr_cursor_map_input_to_region(cursor, device, dconfig->mapped_box);
} }
} }
@ -127,8 +127,8 @@ static void seat_set_device_output_mappings(struct roots_seat *seat,
struct wlr_input_device *device, struct wlr_output *output) { struct wlr_input_device *device, struct wlr_output *output) {
struct wlr_cursor *cursor = seat->cursor->cursor; struct wlr_cursor *cursor = seat->cursor->cursor;
struct roots_config *config = seat->input->config; struct roots_config *config = seat->input->config;
struct device_config *dconfig; struct roots_device_config *dconfig;
dconfig = config_get_device(config, device); dconfig = roots_config_get_device(config, device);
if (dconfig && dconfig->mapped_output && if (dconfig && dconfig->mapped_output &&
strcmp(dconfig->mapped_output, output->name) == 0) { strcmp(dconfig->mapped_output, output->name) == 0) {
wlr_cursor_map_input_to_output(cursor, device, output); wlr_cursor_map_input_to_output(cursor, device, output);