Skip to content

Commit

Permalink
ipc: add an input event
Browse files Browse the repository at this point in the history
This adds an ipc event related to input devices. Currently the
following changes are supported:
- added: when an input device becomes available
- removed: when an input device is no longer available
- xkb_keymap_changed: (keyboards only) the keymap changed
- xkb_layout_changed: (keyboards only) the effective layout changed
  • Loading branch information
RedSoxFan committed Jul 17, 2019
1 parent 95c444d commit 7a0d9ff
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum ipc_command_type {

// sway-specific event types
IPC_EVENT_BAR_STATE_UPDATE = ((1<<31) | 20),
IPC_EVENT_INPUT = ((1<<31) | 21),
};

#endif
1 change: 1 addition & 0 deletions include/sway/input/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct sway_keyboard {
struct sway_seat_device *seat_device;

struct xkb_keymap *keymap;
xkb_layout_index_t effective_layout;

struct wl_listener keyboard_key;
struct wl_listener keyboard_modifiers;
Expand Down
2 changes: 2 additions & 0 deletions include/sway/ipc-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _SWAY_IPC_SERVER_H
#include <sys/socket.h>
#include "sway/config.h"
#include "sway/input/input-manager.h"
#include "sway/tree/container.h"
#include "ipc.h"

Expand All @@ -19,5 +20,6 @@ void ipc_event_bar_state_update(struct bar_config *bar);
void ipc_event_mode(const char *mode, bool pango);
void ipc_event_shutdown(const char *reason);
void ipc_event_binding(struct sway_binding *binding);
void ipc_event_input(const char *change, struct sway_input_device *device);

#endif
5 changes: 5 additions & 0 deletions sway/input/input-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "sway/config.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
#include "sway/ipc-server.h"
#include "sway/server.h"
#include "stringop.h"
#include "list.h"
Expand Down Expand Up @@ -584,6 +585,8 @@ static void handle_device_destroy(struct wl_listener *listener, void *data) {
seat_remove_device(seat, input_device);
}

ipc_event_input("removed", input_device);

wl_list_remove(&input_device->link);
wl_list_remove(&input_device->device_destroy.link);
free(input_device->identifier);
Expand Down Expand Up @@ -656,6 +659,8 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
"device '%s' is not configured on any seats",
input_device->identifier);
}

ipc_event_input("added", input_device);
}

static void handle_inhibit_activate(struct wl_listener *listener, void *data) {
Expand Down
29 changes: 29 additions & 0 deletions sway/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ static void handle_keyboard_modifiers(struct wl_listener *listener,

uint32_t modifiers = wlr_keyboard_get_modifiers(wlr_device->keyboard);
determine_bar_visibility(modifiers);

if (wlr_device->keyboard->modifiers.group != keyboard->effective_layout) {
keyboard->effective_layout = wlr_device->keyboard->modifiers.group;
ipc_event_input("xkb_layout_changed",
keyboard->seat_device->input_device);
}
}

struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
Expand Down Expand Up @@ -570,8 +576,23 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) {
}
}

bool keymap_changed = false;
bool effective_layout_changed = keyboard->effective_layout != 0;
if (keyboard->keymap) {
char *old_keymap_string = xkb_keymap_get_as_string(keyboard->keymap,
XKB_KEYMAP_FORMAT_TEXT_V1);
char *new_keymap_string = xkb_keymap_get_as_string(keymap,
XKB_KEYMAP_FORMAT_TEXT_V1);
keymap_changed = strcmp(old_keymap_string, new_keymap_string);
free(old_keymap_string);
free(new_keymap_string);
} else {
keymap_changed = true;
}

xkb_keymap_unref(keyboard->keymap);
keyboard->keymap = keymap;
keyboard->effective_layout = 0;
wlr_keyboard_set_keymap(wlr_device->keyboard, keyboard->keymap);

xkb_mod_mask_t locked_mods = 0;
Expand Down Expand Up @@ -621,6 +642,14 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) {
wl_signal_add(&wlr_device->keyboard->events.modifiers,
&keyboard->keyboard_modifiers);
keyboard->keyboard_modifiers.notify = handle_keyboard_modifiers;

if (keymap_changed) {
ipc_event_input("xkb_keymap_changed",
keyboard->seat_device->input_device);
} else if (effective_layout_changed) {
ipc_event_input("xkb_layout_changed",
keyboard->seat_device->input_device);
}
}

void sway_keyboard_destroy(struct sway_keyboard *keyboard) {
Expand Down
17 changes: 17 additions & 0 deletions sway/ipc-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,21 @@ static void ipc_event_tick(const char *payload) {
json_object_put(json);
}

void ipc_event_input(const char *change, struct sway_input_device *device) {
if (!ipc_has_event_listeners(IPC_EVENT_INPUT)) {
return;
}
sway_log(SWAY_DEBUG, "Sending input event");

json_object *json = json_object_new_object();
json_object_object_add(json, "change", json_object_new_string(change));
json_object_object_add(json, "input", ipc_json_describe_input(device));

const char *json_string = json_object_to_json_string(json);
ipc_send_event(json_string, IPC_EVENT_INPUT);
json_object_put(json);
}

int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
struct ipc_client *client = data;

Expand Down Expand Up @@ -716,6 +731,8 @@ void ipc_client_handle_command(struct ipc_client *client, uint32_t payload_lengt
} else if (strcmp(event_type, "tick") == 0) {
client->subscribed_events |= event_mask(IPC_EVENT_TICK);
is_tick = true;
} else if (strcmp(event_type, "input") == 0) {
client->subscribed_events |= event_mask(IPC_EVENT_INPUT);
} else {
const char msg[] = "{\"success\": false}";
ipc_send_reply(client, payload_type, msg, strlen(msg));
Expand Down
35 changes: 35 additions & 0 deletions sway/sway-ipc.7.scd
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,9 @@ available:
|- 0x80000014
: bar_status_update
: Send when the visibility of a bar should change due to a modifier
|- 0x80000015
: input
: Sent when something related to input devices changes


## 0x80000000. WORKSPACE
Expand Down Expand Up @@ -1702,6 +1705,38 @@ event is a single object with the following properties:
}
```

## 0x80000015. INPUT

Sent when something related to the input devices changes. The event is a single
object with the following properties:

[- *PROPERTY*
:- *DATA TYPE*
:- *DESCRIPTION*
|- change
: string
:[ What has changed
|- input
: object
: An object representing the input that is identical the ones GET_INPUTS gives

The following change types are currently available:
[- *TYPE*
:- *DESCRIPTION*
|- added
:[ The input device became available
|- removed
: The input device is no longer available
|- xkb_keymap_changed
: (Keyboards only) The keymap for the keyboard has changed
|- xkb_layout_changed
: (Keyboards only) The effective layout in the keymap has changed

*Example Event:*
```
TODO
```

# SEE ALSO

*sway*(1) *sway*(5) *sway-bar*(5) *swaymsg*(1) *sway-input*(5) *sway-output*(5)

0 comments on commit 7a0d9ff

Please sign in to comment.