input config handler context

This commit is contained in:
Tony Crisci 2018-01-20 11:32:07 -05:00
parent 3dd915876d
commit 9e0595f26b
21 changed files with 69 additions and 25 deletions

View file

@ -350,6 +350,11 @@ struct sway_config {
list_t *command_policies; list_t *command_policies;
list_t *feature_policies; list_t *feature_policies;
list_t *ipc_policies; list_t *ipc_policies;
// Context for command handlers
struct {
struct input_config *input_config;
} handler_context;
}; };
void pid_workspace_add(struct pid_workspace *pw); void pid_workspace_add(struct pid_workspace *pw);
@ -375,6 +380,9 @@ bool read_config(FILE *file, struct sway_config *config);
* Free config struct * Free config struct
*/ */
void free_config(struct sway_config *config); void free_config(struct sway_config *config);
void config_clear_handler_context(struct sway_config *config);
void free_sway_variable(struct sway_variable *var); void free_sway_variable(struct sway_variable *var);
/** /**
* Does variable replacement for a string based on the config's currently loaded variables. * Does variable replacement for a string based on the config's currently loaded variables.

View file

@ -5,7 +5,6 @@
#include "sway/config.h" #include "sway/config.h"
#include "list.h" #include "list.h"
extern struct input_config *current_input_config;
extern struct seat_config *current_seat_config; extern struct seat_config *current_seat_config;
/** /**

View file

@ -70,10 +70,7 @@ void apply_input_config(struct input_config *input) {
list_add(config->input_configs, input); list_add(config->input_configs, input);
} }
struct input_config *old_input_config = current_input_config;
current_input_config = input;
sway_input_manager_apply_input_config(input_manager, input); sway_input_manager_apply_input_config(input_manager, input);
current_input_config = old_input_config;
} }
void apply_seat_config(struct seat_config *seat) { void apply_seat_config(struct seat_config *seat) {

View file

@ -11,8 +11,12 @@ struct cmd_results *cmd_input(int argc, char **argv) {
} }
if (config->reading && strcmp("{", argv[1]) == 0) { if (config->reading && strcmp("{", argv[1]) == 0) {
current_input_config = new_input_config(argv[0]); free_input_config(config->handler_context.input_config);
wlr_log(L_DEBUG, "entering input block: %s", current_input_config->identifier); config->handler_context.input_config = new_input_config(argv[0]);
if (!config->handler_context.input_config) {
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
}
wlr_log(L_DEBUG, "entering input block: %s", argv[0]);
return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL); return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL);
} }
@ -20,15 +24,16 @@ struct cmd_results *cmd_input(int argc, char **argv) {
return error; return error;
} }
bool has_context = (config->handler_context.input_config != NULL);
if (!has_context) {
// caller did not give a context so create one just for this command
config->handler_context.input_config = new_input_config(argv[0]);
}
int argc_new = argc-2; int argc_new = argc-2;
char **argv_new = argv+2; char **argv_new = argv+2;
struct cmd_results *res; struct cmd_results *res;
struct input_config *old_input_config = current_input_config;
current_input_config = new_input_config(argv[0]);
if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
}
if (strcasecmp("accel_profile", argv[1]) == 0) { if (strcasecmp("accel_profile", argv[1]) == 0) {
res = input_cmd_accel_profile(argc_new, argv_new); res = input_cmd_accel_profile(argc_new, argv_new);
} else if (strcasecmp("click_method", argv[1]) == 0) { } else if (strcasecmp("click_method", argv[1]) == 0) {
@ -64,7 +69,12 @@ struct cmd_results *cmd_input(int argc, char **argv) {
} else { } else {
res = cmd_results_new(CMD_INVALID, "input <device>", "Unknown command %s", argv[1]); res = cmd_results_new(CMD_INVALID, "input <device>", "Unknown command %s", argv[1]);
} }
free_input_config(current_input_config);
current_input_config = old_input_config; if (!has_context) {
// clean up the context we created earlier
free_input_config(config->handler_context.input_config);
config->handler_context.input_config = NULL;
}
return res; return res;
} }

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_accel_profile(int argc, char **argv) {
if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "accel_profile", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "accel_profile", return cmd_results_new(CMD_FAILURE, "accel_profile",
"No input device defined."); "No input device defined.");

View file

@ -6,12 +6,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_click_method(int argc, char **argv) { struct cmd_results *input_cmd_click_method(int argc, char **argv) {
wlr_log(L_DEBUG, "click_method for device: %d %s",
current_input_config==NULL, current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "click_method", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "click_method", return cmd_results_new(CMD_FAILURE, "click_method",
"No input device defined."); "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_drag_lock(int argc, char **argv) {
if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "drag_lock", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, return cmd_results_new(CMD_FAILURE,
"drag_lock", "No input device defined."); "drag_lock", "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_dwt(int argc, char **argv) {
if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "dwt", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined."); return cmd_results_new(CMD_FAILURE, "dwt", "No input device defined.");
} }

View file

@ -6,16 +6,18 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_events(int argc, char **argv) { struct cmd_results *input_cmd_events(int argc, char **argv) {
wlr_log(L_DEBUG, "events for device: %s",
current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "events", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "events", return cmd_results_new(CMD_FAILURE, "events",
"No input device defined."); "No input device defined.");
} }
wlr_log(L_DEBUG, "events for device: %s",
current_input_config->identifier);
struct input_config *new_config = struct input_config *new_config =
new_input_config(current_input_config->identifier); new_input_config(current_input_config->identifier);

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_left_handed(int argc, char **argv) {
if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "left_handed", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "left_handed", return cmd_results_new(CMD_FAILURE, "left_handed",
"No input device defined."); "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_middle_emulation(int argc, char **argv) {
if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "middle_emulation", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "middle_emulation", return cmd_results_new(CMD_FAILURE, "middle_emulation",
"No input device defined."); "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_natural_scroll(int argc, char **argv) {
if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "natural_scroll", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "natural_scoll", return cmd_results_new(CMD_FAILURE, "natural_scoll",
"No input device defined."); "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_pointer_accel(int argc, char **argv) {
if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "pointer_accel", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, return cmd_results_new(CMD_FAILURE,
"pointer_accel", "No input device defined."); "pointer_accel", "No input device defined.");

View file

@ -9,6 +9,8 @@ struct cmd_results *input_cmd_scroll_method(int argc, char **argv) {
if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "scroll_method", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "scroll_method", return cmd_results_new(CMD_FAILURE, "scroll_method",
"No input device defined."); "No input device defined.");

View file

@ -6,11 +6,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_tap(int argc, char **argv) { struct cmd_results *input_cmd_tap(int argc, char **argv) {
wlr_log(L_DEBUG, "tap for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "tap", EXPECTED_AT_LEAST, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "tap", "No input device defined."); return cmd_results_new(CMD_FAILURE, "tap", "No input device defined.");
} }

View file

@ -5,11 +5,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) { struct cmd_results *input_cmd_xkb_layout(int argc, char **argv) {
wlr_log(L_DEBUG, "xkb layout for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_layout", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "xkb_layout", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "xkb_layout", "No input device defined."); return cmd_results_new(CMD_FAILURE, "xkb_layout", "No input device defined.");
} }

View file

@ -5,11 +5,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_xkb_model(int argc, char **argv) { struct cmd_results *input_cmd_xkb_model(int argc, char **argv) {
wlr_log(L_DEBUG, "xkb model for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_model", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "xkb_model", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "xkb_model", "No input device defined."); return cmd_results_new(CMD_FAILURE, "xkb_model", "No input device defined.");
} }

View file

@ -5,11 +5,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_xkb_options(int argc, char **argv) { struct cmd_results *input_cmd_xkb_options(int argc, char **argv) {
wlr_log(L_DEBUG, "xkb options for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_options", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "xkb_options", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "xkb_options", "No input device defined."); return cmd_results_new(CMD_FAILURE, "xkb_options", "No input device defined.");
} }

View file

@ -5,11 +5,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) { struct cmd_results *input_cmd_xkb_rules(int argc, char **argv) {
wlr_log(L_DEBUG, "xkb rules for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_rules", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "xkb_rules", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "xkb_rules", "No input device defined."); return cmd_results_new(CMD_FAILURE, "xkb_rules", "No input device defined.");
} }

View file

@ -5,11 +5,12 @@
#include "log.h" #include "log.h"
struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) { struct cmd_results *input_cmd_xkb_variant(int argc, char **argv) {
wlr_log(L_DEBUG, "xkb variant for device: %s", current_input_config->identifier);
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_variant", EXPECTED_EQUAL_TO, 1))) { if ((error = checkarg(argc, "xkb_variant", EXPECTED_EQUAL_TO, 1))) {
return error; return error;
} }
struct input_config *current_input_config =
config->handler_context.input_config;
if (!current_input_config) { if (!current_input_config) {
return cmd_results_new(CMD_FAILURE, "xkb_variant", "No input device defined."); return cmd_results_new(CMD_FAILURE, "xkb_variant", "No input device defined.");
} }

View file

@ -54,6 +54,8 @@ static void free_mode(struct sway_mode *mode) {
} }
void free_config(struct sway_config *config) { void free_config(struct sway_config *config) {
config_clear_handler_context(config);
int i; int i;
if (!config) { if (!config) {
@ -480,6 +482,11 @@ bool load_include_configs(const char *path, struct sway_config *config) {
return true; return true;
} }
void config_clear_handler_context(struct sway_config *config) {
free_input_config(config->handler_context.input_config);
memset(&config->handler_context, 0, sizeof(config->handler_context));
}
bool read_config(FILE *file, struct sway_config *config) { bool read_config(FILE *file, struct sway_config *config) {
bool success = true; bool success = true;
@ -592,8 +599,6 @@ bool read_config(FILE *file, struct sway_config *config) {
case CMD_BLOCK_INPUT: case CMD_BLOCK_INPUT:
wlr_log(L_DEBUG, "End of input block"); wlr_log(L_DEBUG, "End of input block");
free_input_config(current_input_config);
current_input_config = NULL;
block = CMD_BLOCK_END; block = CMD_BLOCK_END;
break; break;
@ -635,6 +640,7 @@ bool read_config(FILE *file, struct sway_config *config) {
default:; default:;
} }
config_clear_handler_context(config);
default:; default:;
} }
free(line); free(line);