Use an enum instead of a marker string for map_to_

This commit is contained in:
Tadeo Kondrak 2019-11-01 11:37:29 -06:00 committed by Simon Ser
parent 4829f1c26a
commit 7f54495b5e
5 changed files with 45 additions and 40 deletions

View file

@ -108,6 +108,12 @@ struct calibration_matrix {
float matrix[6]; float matrix[6];
}; };
enum input_config_mapped_to {
MAPPED_TO_DEFAULT,
MAPPED_TO_OUTPUT,
MAPPED_TO_REGION
};
/** /**
* options for input devices * options for input devices
*/ */
@ -147,6 +153,8 @@ struct input_config {
int xkb_capslock; int xkb_capslock;
struct input_config_mapped_from_region *mapped_from_region; struct input_config_mapped_from_region *mapped_from_region;
enum input_config_mapped_to mapped_to;
char *mapped_to_output; char *mapped_to_output;
struct wlr_box *mapped_to_region; struct wlr_box *mapped_to_region;

View file

@ -16,6 +16,7 @@ struct cmd_results *input_cmd_map_to_output(int argc, char **argv) {
return cmd_results_new(CMD_FAILURE, "No input device defined."); return cmd_results_new(CMD_FAILURE, "No input device defined.");
} }
ic->mapped_to = MAPPED_TO_OUTPUT;
ic->mapped_to_output = strdup(argv[0]); ic->mapped_to_output = strdup(argv[0]);
return cmd_results_new(CMD_SUCCESS, NULL); return cmd_results_new(CMD_SUCCESS, NULL);

View file

@ -15,9 +15,7 @@ struct cmd_results *input_cmd_map_to_region(int argc, char **argv) {
return cmd_results_new(CMD_FAILURE, "No input device defined"); return cmd_results_new(CMD_FAILURE, "No input device defined");
} }
// This is used to clear the current output mapping. ic->mapped_to = MAPPED_TO_REGION;
ic->mapped_to_output = strdup("");
ic->mapped_to_region = calloc(1, sizeof(struct wlr_box)); ic->mapped_to_region = calloc(1, sizeof(struct wlr_box));
const char *errstr; const char *errstr;

View file

@ -134,6 +134,9 @@ void merge_input_config(struct input_config *dst, struct input_config *src) {
memcpy(dst->mapped_from_region, src->mapped_from_region, memcpy(dst->mapped_from_region, src->mapped_from_region,
sizeof(struct input_config_mapped_from_region)); sizeof(struct input_config_mapped_from_region));
} }
if (src->mapped_to) {
dst->mapped_to = src->mapped_to;
}
if (src->mapped_to_output) { if (src->mapped_to_output) {
free(dst->mapped_to_output); free(dst->mapped_to_output);
dst->mapped_to_output = strdup(src->mapped_to_output); dst->mapped_to_output = strdup(src->mapped_to_output);

View file

@ -575,62 +575,57 @@ static void seat_reset_input_config(struct sway_seat *seat,
static void seat_apply_input_config(struct sway_seat *seat, static void seat_apply_input_config(struct sway_seat *seat,
struct sway_seat_device *sway_device) { struct sway_seat_device *sway_device) {
const char *mapped_to_output = NULL; struct input_config *ic =
struct wlr_box *mapped_to_region = NULL; input_device_get_config(sway_device->input_device);
if (ic == NULL) {
return;
}
struct input_config *ic = input_device_get_config(
sway_device->input_device);
if (ic != NULL) {
sway_log(SWAY_DEBUG, "Applying input config to %s", sway_log(SWAY_DEBUG, "Applying input config to %s",
sway_device->input_device->identifier); sway_device->input_device->identifier);
// We use an empty string as a marker to clear the mapped_to_output const char *mapped_to_output = ic->mapped_to_output;
// property, because a NULL set in a handler_context isn't preserved. struct wlr_box *mapped_to_region = ic->mapped_to_region;
if (ic->mapped_to_output != NULL && ic->mapped_to_output[0] == '\0') {
free(ic->mapped_to_output);
ic->mapped_to_output = NULL;
wlr_cursor_map_input_to_output(seat->cursor->cursor,
sway_device->input_device->wlr_device, NULL);
}
mapped_to_output = ic->mapped_to_output; switch (ic->mapped_to) {
if (mapped_to_output != NULL) { case MAPPED_TO_DEFAULT:
// Output has just been set, clear region setting.
free(ic->mapped_to_region);
ic->mapped_to_region = NULL;
wlr_cursor_map_input_to_region(seat->cursor->cursor,
sway_device->input_device->wlr_device, NULL);
}
mapped_to_region = ic->mapped_to_region;
}
if (mapped_to_output == NULL && mapped_to_region == NULL) {
mapped_to_output = sway_device->input_device->wlr_device->output_name; mapped_to_output = sway_device->input_device->wlr_device->output_name;
if (mapped_to_output == NULL) {
return;
} }
/* fallthrough */
if (mapped_to_output != NULL) { case MAPPED_TO_OUTPUT:
sway_log(SWAY_DEBUG, "Mapping input device %s to output %s", sway_log(SWAY_DEBUG, "Mapping input device %s to output %s",
sway_device->input_device->identifier, mapped_to_output); sway_device->input_device->identifier, mapped_to_output);
if (strcmp("*", mapped_to_output) == 0) { if (strcmp("*", mapped_to_output) == 0) {
wlr_cursor_map_input_to_output(seat->cursor->cursor, wlr_cursor_map_input_to_output(seat->cursor->cursor,
sway_device->input_device->wlr_device, NULL); sway_device->input_device->wlr_device, NULL);
wlr_cursor_map_input_to_region(seat->cursor->cursor,
sway_device->input_device->wlr_device, NULL);
sway_log(SWAY_DEBUG, "Reset output mapping"); sway_log(SWAY_DEBUG, "Reset output mapping");
return; return;
} }
struct sway_output *output = output_by_name_or_id(mapped_to_output); struct sway_output *output = output_by_name_or_id(mapped_to_output);
if (output) { if (!output) {
return;
}
wlr_cursor_map_input_to_output(seat->cursor->cursor, wlr_cursor_map_input_to_output(seat->cursor->cursor,
sway_device->input_device->wlr_device, output->wlr_output); sway_device->input_device->wlr_device, output->wlr_output);
sway_log(SWAY_DEBUG, "Mapped to output %s", output->wlr_output->name); wlr_cursor_map_input_to_region(seat->cursor->cursor,
} sway_device->input_device->wlr_device, NULL);
} else if (mapped_to_region != NULL) { sway_log(SWAY_DEBUG,
"Mapped to output %s", output->wlr_output->name);
return;
case MAPPED_TO_REGION:
sway_log(SWAY_DEBUG, "Mapping input device %s to %d,%d %dx%d", sway_log(SWAY_DEBUG, "Mapping input device %s to %d,%d %dx%d",
sway_device->input_device->identifier, sway_device->input_device->identifier,
mapped_to_region->x, mapped_to_region->y, mapped_to_region->x, mapped_to_region->y,
mapped_to_region->width, mapped_to_region->height); mapped_to_region->width, mapped_to_region->height);
wlr_cursor_map_input_to_output(seat->cursor->cursor,
sway_device->input_device->wlr_device, NULL);
wlr_cursor_map_input_to_region(seat->cursor->cursor, wlr_cursor_map_input_to_region(seat->cursor->cursor,
sway_device->input_device->wlr_device, mapped_to_region); sway_device->input_device->wlr_device, mapped_to_region);
return;
} }
} }