Skip to content

Commit

Permalink
Support WLR_INPUT_DEVICE_SWITCH in sway
Browse files Browse the repository at this point in the history
This commit adds support for laptop lid and tablet
mode switches as provided by evdev/libinput and
handled by wlroots.

Adds a new bindswitch command with syntax:
bindswitch <switch>:<state> <command>

Where <switch> is one of:
tablet for WLR_SWITCH_TYPE_TABLET_MODE
lid for WLR_SWITCH_TYPE_LID

<state> is one of:
on for WLR_SWITCH_STATE_ON
off for WLR_SWITCH_STATE_OFF
toggle for WLR_SWITCH_STATE_TOGGLE

(Note that WLR_SWITCH_STATE_TOGGLE doesn't map to
libinput and will trigger at both on and off events)
  • Loading branch information
tokyovigilante authored and RedSoxFan committed Mar 20, 2019
1 parent bfa20e6 commit bdb4024
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/sway/commands.h
Expand Up @@ -101,6 +101,7 @@ struct sway_container *container_find_resize_parent(struct sway_container *con,
sway_cmd cmd_assign;
sway_cmd cmd_bar;
sway_cmd cmd_bindcode;
sway_cmd cmd_bindswitch;
sway_cmd cmd_bindsym;
sway_cmd cmd_border;
sway_cmd cmd_client_noop;
Expand Down
15 changes: 15 additions & 0 deletions include/sway/config.h
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <wlr/interfaces/wlr_switch.h>
#include <wlr/types/wlr_box.h>
#include <xkbcommon/xkbcommon.h>
#include "../include/config.h"
Expand All @@ -29,6 +30,7 @@ enum binding_input_type {
BINDING_KEYSYM,
BINDING_MOUSECODE,
BINDING_MOUSESYM,
BINDING_SWITCH
};

enum binding_flags {
Expand Down Expand Up @@ -60,6 +62,16 @@ struct sway_mouse_binding {
char *command;
};

/**
* A laptop switch binding and an associated command.
*/
struct sway_switch_binding {
enum wlr_switch_type type;
enum wlr_switch_state state;
uint32_t flags;
char *command;
};

/**
* Focus on window activation.
*/
Expand All @@ -78,6 +90,7 @@ struct sway_mode {
list_t *keysym_bindings;
list_t *keycode_bindings;
list_t *mouse_bindings;
list_t *switch_bindings;
bool pango;
};

Expand Down Expand Up @@ -603,6 +616,8 @@ int sway_binding_cmp_keys(const void *a, const void *b);

void free_sway_binding(struct sway_binding *sb);

void free_switch_binding(struct sway_switch_binding *binding);

void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding);

void load_swaybar(struct bar_config *bar);
Expand Down
1 change: 1 addition & 0 deletions include/sway/input/seat.h
Expand Up @@ -27,6 +27,7 @@ struct sway_seat_device {
struct sway_seat *sway_seat;
struct sway_input_device *input_device;
struct sway_keyboard *keyboard;
struct sway_switch *switch_device;
struct wl_list link; // sway_seat::devices
};

Expand Down
19 changes: 19 additions & 0 deletions include/sway/input/switch.h
@@ -0,0 +1,19 @@
#ifndef _SWAY_INPUT_SWITCH_H
#define _SWAY_INPUT_SWITCH_H

#include "sway/input/seat.h"

struct sway_switch {
struct sway_seat_device *seat_device;

struct wl_listener switch_toggle;
};

struct sway_switch *sway_switch_create(struct sway_seat *seat,
struct sway_seat_device *device);

void sway_switch_configure(struct sway_switch *sway_switch);

void sway_switch_destroy(struct sway_switch *sway_switch);

#endif
2 changes: 2 additions & 0 deletions sway/commands.c
Expand Up @@ -47,6 +47,7 @@ static struct cmd_handler handlers[] = {
{ "assign", cmd_assign },
{ "bar", cmd_bar },
{ "bindcode", cmd_bindcode },
{ "bindswitch", cmd_bindswitch },
{ "bindsym", cmd_bindsym },
{ "client.background", cmd_client_noop },
{ "client.focused", cmd_client_focused },
Expand Down Expand Up @@ -386,6 +387,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
&& handler->handle != cmd_mode
&& handler->handle != cmd_bindsym
&& handler->handle != cmd_bindcode
&& handler->handle != cmd_bindswitch
&& handler->handle != cmd_set
&& handler->handle != cmd_for_window
&& (*argv[i] == '\"' || *argv[i] == '\'')) {
Expand Down
118 changes: 118 additions & 0 deletions sway/commands/bind.c
Expand Up @@ -29,6 +29,28 @@ void free_sway_binding(struct sway_binding *binding) {
free(binding);
}

void free_switch_binding(struct sway_switch_binding *binding) {
if (!binding) {
return;
}
free(binding->command);
free(binding);
}

/**
* Returns true if the bindings have the same switch type and state combinations.
*/
static bool binding_switch_compare(struct sway_switch_binding *binding_a,
struct sway_switch_binding *binding_b) {
if (binding_a->type != binding_b->type) {
return false;
}
if (binding_a->state != binding_b->state) {
return false;
}
return true;
}

/**
* Returns true if the bindings have the same key and modifier combinations.
* Note that keyboard layout is not considered, so the bindings might actually
Expand Down Expand Up @@ -325,6 +347,102 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
return cmd_bindsym_or_bindcode(argc, argv, true);
}

struct cmd_results *cmd_bindswitch(int argc, char **argv) {

struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bindswitch", EXPECTED_AT_LEAST, 2))) {
return error;
}
struct sway_switch_binding *binding = calloc(1, sizeof(struct sway_switch_binding));
if (!binding) {
return cmd_results_new(CMD_FAILURE, "Unable to allocate binding");
}

bool warn = true;

// Handle flags
while (argc > 0) {
if (strcmp("--locked", argv[0]) == 0) {
binding->flags |= BINDING_LOCKED;
} else if (strcmp("--no-warn", argv[0]) == 0) {
warn = false;
} else {
break;
}
argv++;
argc--;
}

if (argc < 2) {
free(binding);
return cmd_results_new(CMD_FAILURE,
"Invalid bindswitch command (expected at least 2 "
"non-option arguments, got %d)", argc);
}
binding->command = join_args(argv + 1, argc - 1);

list_t *split = split_string(argv[0], ":");
if (split->length != 2) {
free_switch_binding(binding);
return cmd_results_new(CMD_FAILURE,
"Invalid bindswitch command (expected two arguments with "
"format <switch>:<state> <action>, got %d)", argc);
}
if (strcmp(split->items[0], "tablet") == 0) {
binding->type = WLR_SWITCH_TYPE_TABLET_MODE;
} else if (strcmp(split->items[0], "lid") == 0) {
binding->type = WLR_SWITCH_TYPE_LID;
} else {
free_switch_binding(binding);
return cmd_results_new(CMD_FAILURE,
"Invalid bindswitch command (expected switch binding: "
"unknown switch %s)", split->items[0]);
}
if (strcmp(split->items[1], "on") == 0) {
binding->state = WLR_SWITCH_STATE_ON;
} else if (strcmp(split->items[1], "off") == 0) {
binding->state = WLR_SWITCH_STATE_OFF;
} else if (strcmp(split->items[1], "toggle") == 0) {
binding->state = WLR_SWITCH_STATE_TOGGLE;
} else {
free_switch_binding(binding);
return cmd_results_new(CMD_FAILURE,
"Invalid bindswitch command "
"(expected switch state: unknown state %d)",
split->items[0]);
}
list_free_items_and_destroy(split);

list_t *mode_bindings = config->current_mode->switch_bindings;

// overwrite the binding if it already exists
bool overwritten = false;
for (int i = 0; i < mode_bindings->length; ++i) {
struct sway_switch_binding *config_binding = mode_bindings->items[i];
if (binding_switch_compare(binding, config_binding)) {
sway_log(SWAY_INFO, "Overwriting binding '%s' from `%s` to `%s`",
argv[0], binding->command, config_binding->command);
if (warn) {
config_add_swaynag_warning("Overwriting binding"
"'%s' to `%s` from `%s`",
argv[0], binding->command,
config_binding->command);
}
free_switch_binding(config_binding);
mode_bindings->items[i] = binding;
overwritten = true;
}
}

if (!overwritten) {
list_add(mode_bindings, binding);
}

sway_log(SWAY_DEBUG, "bindswitch - Bound %s to command `%s`",
argv[0], binding->command);
return cmd_results_new(CMD_SUCCESS, NULL);
}

/**
* Execute the command associated to a binding
*/
Expand Down
2 changes: 2 additions & 0 deletions sway/commands/mode.c
Expand Up @@ -12,6 +12,7 @@
// Must be in order for the bsearch
static struct cmd_handler mode_handlers[] = {
{ "bindcode", cmd_bindcode },
{ "bindswitch", cmd_bindswitch },
{ "bindsym", cmd_bindsym }
};

Expand Down Expand Up @@ -54,6 +55,7 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
mode->keysym_bindings = create_list();
mode->keycode_bindings = create_list();
mode->mouse_bindings = create_list();
mode->switch_bindings = create_list();
mode->pango = pango;
list_add(config->modes, mode);
}
Expand Down
7 changes: 7 additions & 0 deletions sway/config.c
Expand Up @@ -56,6 +56,12 @@ static void free_mode(struct sway_mode *mode) {
}
list_free(mode->mouse_bindings);
}
if (mode->switch_bindings) {
for (int i = 0; i < mode->switch_bindings->length; i++) {
free_switch_binding(mode->switch_bindings->items[i]);
}
list_free(mode->switch_bindings);
}
free(mode);
}

Expand Down Expand Up @@ -195,6 +201,7 @@ static void config_defaults(struct sway_config *config) {
if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup;
if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup;
if (!(config->current_mode->switch_bindings = create_list())) goto cleanup;
list_add(config->modes, config->current_mode);

config->floating_mod = 0;
Expand Down
47 changes: 47 additions & 0 deletions sway/input/input-manager.c
Expand Up @@ -155,6 +155,47 @@ static void input_manager_libinput_reset_keyboard(
libinput_device, send_events));
}

static void input_manager_libinput_config_switch(
struct sway_input_device *input_device) {
struct wlr_input_device *wlr_device = input_device->wlr_device;
struct input_config *ic = input_device_get_config(input_device);
struct libinput_device *libinput_device;

if (!ic || !wlr_input_device_is_libinput(wlr_device)) {
return;
}

libinput_device = wlr_libinput_get_device_handle(wlr_device);
sway_log(SWAY_DEBUG, "input_manager_libinput_config_switch(%s)",
ic->identifier);

if (ic->send_events != INT_MIN) {
sway_log(SWAY_DEBUG, "libinput_config_switch(%s) send_events_set_mode(%d)",
ic->identifier, ic->send_events);
log_libinput_config_status(libinput_device_config_send_events_set_mode(
libinput_device, ic->send_events));
}
}

static void input_manager_libinput_reset_switch(
struct sway_input_device *input_device) {
struct wlr_input_device *wlr_device = input_device->wlr_device;
struct libinput_device *libinput_device;

if (!wlr_input_device_is_libinput(wlr_device)) {
return;
}

libinput_device = wlr_libinput_get_device_handle(wlr_device);

uint32_t send_events =
libinput_device_config_send_events_get_default_mode(libinput_device);
sway_log(SWAY_DEBUG, "libinput_reset_switch(%s) send_events_set_mode(%d)",
input_device->identifier, send_events);
log_libinput_config_status(libinput_device_config_send_events_set_mode(
libinput_device, send_events));
}

static void input_manager_libinput_config_touch(
struct sway_input_device *input_device) {
struct wlr_input_device *wlr_device = input_device->wlr_device;
Expand Down Expand Up @@ -471,6 +512,8 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
input_manager_libinput_config_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_config_keyboard(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
input_manager_libinput_config_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_config_touch(input_device);
}
Expand Down Expand Up @@ -624,6 +667,8 @@ void input_manager_apply_input_config(struct input_config *input_config) {
input_manager_libinput_config_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_config_keyboard(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
input_manager_libinput_config_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_config_touch(input_device);
}
Expand All @@ -642,6 +687,8 @@ void input_manager_reset_input(struct sway_input_device *input_device) {
input_manager_libinput_reset_pointer(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
input_manager_libinput_reset_keyboard(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
input_manager_libinput_reset_switch(input_device);
} else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
input_manager_libinput_reset_touch(input_device);
}
Expand Down
18 changes: 14 additions & 4 deletions sway/input/seat.c
Expand Up @@ -15,6 +15,7 @@
#include "sway/input/input-manager.h"
#include "sway/input/keyboard.h"
#include "sway/input/seat.h"
#include "sway/input/switch.h"
#include "sway/ipc-server.h"
#include "sway/layers.h"
#include "sway/output.h"
Expand Down Expand Up @@ -482,8 +483,8 @@ static void seat_update_capabilities(struct sway_seat *seat) {
case WLR_INPUT_DEVICE_TABLET_TOOL:
caps |= WL_SEAT_CAPABILITY_POINTER;
break;
case WLR_INPUT_DEVICE_TABLET_PAD:
case WLR_INPUT_DEVICE_SWITCH:
case WLR_INPUT_DEVICE_TABLET_PAD:
break;
}
}
Expand Down Expand Up @@ -570,6 +571,15 @@ static void seat_configure_keyboard(struct sway_seat *seat,
}
}

static void seat_configure_switch(struct sway_seat *seat,
struct sway_seat_device *seat_device) {
if (!seat_device->switch_device) {
sway_switch_create(seat, seat_device);
}
seat_apply_input_config(seat, seat_device);
sway_switch_configure(seat_device->switch_device);
}

static void seat_configure_touch(struct sway_seat *seat,
struct sway_seat_device *sway_device) {
wlr_cursor_attach_input_device(seat->cursor->cursor,
Expand Down Expand Up @@ -611,6 +621,9 @@ void seat_configure_device(struct sway_seat *seat,
case WLR_INPUT_DEVICE_KEYBOARD:
seat_configure_keyboard(seat, seat_device);
break;
case WLR_INPUT_DEVICE_SWITCH:
seat_configure_switch(seat, seat_device);
break;
case WLR_INPUT_DEVICE_TOUCH:
seat_configure_touch(seat, seat_device);
break;
Expand All @@ -620,9 +633,6 @@ void seat_configure_device(struct sway_seat *seat,
case WLR_INPUT_DEVICE_TABLET_PAD:
sway_log(SWAY_DEBUG, "TODO: configure tablet pad");
break;
case WLR_INPUT_DEVICE_SWITCH:
sway_log(SWAY_DEBUG, "TODO: configure switch device");
break;
}
}

Expand Down

0 comments on commit bdb4024

Please sign in to comment.