bar_cmd_tray_bind: Use mouse button helpers

This modifies `bar_cmd_tray_bindsym` to use `get_mouse_bindsym` for
parsing mouse buttons. This also introduces `bar_cmd_tray_bindcode`,
which will use `get_mouse_bindcode` for parsing mouse buttons. Like with
sway bindings, the two commands are encapsulated in a single file to
maximize shared code.

This also modifies tray bindings to work off of events codes rather than
x11 buttons, which allows for any mouse buttons to be used.

For `get_bar_config`, `event_code` has been added to the `tray_bindings`
section and will include to event code for the button. If the event code
can be mapped to a x11 button, `input_code` will still be the x11 button
number. Otherwise, `input_code` will be `0`.
This commit is contained in:
Brian Ashworth 2019-01-15 21:25:28 -05:00 committed by emersion
parent 247817f68c
commit 02bbefda20
19 changed files with 229 additions and 143 deletions

View file

@ -28,6 +28,7 @@ static struct cmd_handler bar_handlers[] = {
{ "status_padding", bar_cmd_status_padding },
{ "strip_workspace_name", bar_cmd_strip_workspace_name },
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
{ "tray_bindcode", bar_cmd_tray_bindcode },
{ "tray_bindsym", bar_cmd_tray_bindsym },
{ "tray_output", bar_cmd_tray_output },
{ "tray_padding", bar_cmd_tray_padding },

View file

@ -46,27 +46,7 @@ static struct cmd_results *bar_cmd_bind(int argc, char **argv, bool code) {
free_bar_binding(binding);
return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]);
}
const char *name = libevdev_event_code_get_name(EV_KEY, binding->button);
if (!name) {
switch (binding->button) {
case SWAY_SCROLL_UP:
name = "SWAY_SCROLL_UP";
break;
case SWAY_SCROLL_DOWN:
name = "SWAY_SCROLL_DOWN";
break;
case SWAY_SCROLL_LEFT:
name = "SWAY_SCROLL_LEFT";
break;
case SWAY_SCROLL_RIGHT:
name = "SWAY_SCROLL_RIGHT";
break;
default:
// Unreachable
break;
}
}
const char *name = get_mouse_button_name(binding->button);
binding->command = join_args(argv + 1, argc - 1);

View file

@ -0,0 +1,97 @@
#include <strings.h>
#include "config.h"
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/input/cursor.h"
#include "log.h"
static struct cmd_results *tray_bind(int argc, char **argv, bool code) {
#if HAVE_TRAY
const char *command = code ? "bar tray_bindcode" : "bar tray_bindsym";
struct cmd_results *error = NULL;
if ((error = checkarg(argc, command, EXPECTED_EQUAL_TO, 2))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
struct tray_binding *binding = calloc(1, sizeof(struct tray_binding));
if (!binding) {
return cmd_results_new(CMD_FAILURE, "Unable to allocate tray binding");
}
char *message = NULL;
if (code) {
binding->button = get_mouse_bindcode(argv[0], &message);
} else {
binding->button = get_mouse_bindsym(argv[0], &message);
}
if (message) {
free(binding);
error = cmd_results_new(CMD_INVALID, message);
free(message);
return error;
} else if (!binding->button) {
free(binding);
return cmd_results_new(CMD_INVALID, "Unknown button %s", argv[0]);
}
const char *name = get_mouse_button_name(binding->button);
static const char *commands[] = {
"ContextMenu",
"Activate",
"SecondaryActivate",
"ScrollDown",
"ScrollLeft",
"ScrollRight",
"ScrollUp",
"nop"
};
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
if (strcasecmp(argv[1], commands[i]) == 0) {
binding->command = commands[i];
}
}
if (!binding->command) {
return cmd_results_new(CMD_INVALID, "[Bar %s] Invalid tray command %s",
config->current_bar->id, argv[1]);
}
bool overwritten = false;
struct tray_binding *other = NULL;
wl_list_for_each(other, &config->current_bar->tray_bindings, link) {
if (other->button == binding->button) {
overwritten = true;
other->command = binding->command;
free(binding);
binding = other;
wlr_log(WLR_DEBUG,
"[bar %s] Updated tray binding for %u (%s) to %s",
config->current_bar->id, binding->button, name,
binding->command);
break;
}
}
if (!overwritten) {
wl_list_insert(&config->current_bar->tray_bindings, &binding->link);
wlr_log(WLR_DEBUG, "[bar %s] Added tray binding for %u (%s) to %s",
config->current_bar->id, binding->button, name,
binding->command);
}
return cmd_results_new(CMD_SUCCESS, NULL);
#else
return cmd_results_new(CMD_INVALID,
"Sway has been compiled without tray support");
#endif
}
struct cmd_results *bar_cmd_tray_bindcode(int argc, char **argv) {
return tray_bind(argc, argv, true);
}
struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) {
return tray_bind(argc, argv, false);
}

View file

@ -1,55 +0,0 @@
#include <strings.h>
#include "config.h"
#include "sway/commands.h"
#include "sway/config.h"
#include "log.h"
struct cmd_results *bar_cmd_tray_bindsym(int argc, char **argv) {
#if HAVE_TRAY
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "tray_bindsym", EXPECTED_EQUAL_TO, 2))) {
return error;
}
if (!config->current_bar) {
return cmd_results_new(CMD_FAILURE, "No bar defined.");
}
int button = 0;
if (strncasecmp(argv[0], "button", strlen("button")) == 0 &&
strlen(argv[0]) == strlen("button0")) {
button = argv[0][strlen("button")] - '0';
}
if (button < 1 || button > 9) {
return cmd_results_new(CMD_FAILURE,
"[Bar %s] Only buttons 1 to 9 are supported",
config->current_bar->id);
}
static const char *commands[] = {
"ContextMenu",
"Activate",
"SecondaryActivate",
"ScrollDown",
"ScrollLeft",
"ScrollRight",
"ScrollUp",
"nop"
};
for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
if (strcasecmp(argv[1], commands[i]) == 0) {
wlr_log(WLR_DEBUG, "[Bar %s] Binding button %d to %s",
config->current_bar->id, button, commands[i]);
config->current_bar->tray_bindings[button] = commands[i];
return cmd_results_new(CMD_SUCCESS, NULL);
}
}
return cmd_results_new(CMD_INVALID,
"[Bar %s] Invalid command %s", config->current_bar->id, argv[1]);
#else
return cmd_results_new(CMD_INVALID,
"Sway has been compiled without tray support");
#endif
}