From 6392abe35b32c66c642c25a7c6911021862d3413 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Wed, 6 Jan 2016 17:01:45 +0100 Subject: [PATCH] Implement IPC binding event (keyboard) This implements the IPC binding event for keyboard bindings. It is slightly different from the i3 implementation [1] since sway supports more than one non-modifier key in a binding. Thus the json interface has been changed from: { ... "symbol": "t", ... } to: { ... "symbols": [ "t" ], ... } [1] http://i3wm.org/docs/ipc.html#_binding_event --- include/ipc-server.h | 4 ++++ sway/handlers.c | 6 ++++++ sway/ipc-server.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/include/ipc-server.h b/include/ipc-server.h index 47026bfd..96b7902f 100644 --- a/include/ipc-server.h +++ b/include/ipc-server.h @@ -21,6 +21,10 @@ void ipc_event_mode(const char *mode); * the name of that modifier. */ void ipc_event_modifier(uint32_t modifier, const char *state); +/** + * Send IPC keyboard binding event. + */ +void ipc_event_binding_keyboard(struct sway_binding *sb); const char *swayc_type_string(enum swayc_types type); #endif diff --git a/sway/handlers.c b/sway/handlers.c index e0acebea..693e11eb 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -347,11 +347,14 @@ static bool handle_bindsym(struct sway_binding *binding) { } if (match) { + struct sway_binding *binding_copy = sway_binding_dup(binding); struct cmd_results *res = handle_command(binding->command); if (res->status != CMD_SUCCESS) { sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); } + ipc_event_binding_keyboard(binding_copy); free_cmd_results(res); + free_sway_binding(binding_copy); return true; } @@ -362,11 +365,14 @@ static bool handle_bindsym_release(struct sway_binding *binding) { if (binding->keys->length == 1) { xkb_keysym_t *key = binding->keys->items[0]; if (check_released_key(*key)) { + struct sway_binding *binding_copy = sway_binding_dup(binding); struct cmd_results *res = handle_command(binding->command); if (res->status != CMD_SUCCESS) { sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); } + ipc_event_binding_keyboard(binding_copy); free_cmd_results(res); + free_sway_binding(binding_copy); return true; } } diff --git a/sway/ipc-server.c b/sway/ipc-server.c index f3a4647b..d8d8434c 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -298,6 +298,8 @@ void ipc_client_handle_command(struct ipc_client *client) { client->subscribed_events |= IPC_EVENT_MODE; } else if (strcmp(event_type, "modifier") == 0) { client->subscribed_events |= IPC_EVENT_MODIFIER; + } else if (strcmp(event_type, "binding") == 0) { + client->subscribed_events |= IPC_EVENT_BINDING; } else { ipc_send_reply(client, "{\"success\": false}", 18); ipc_client_disconnect(client); @@ -633,3 +635,48 @@ void ipc_event_modifier(uint32_t modifier, const char *state) { json_object_put(obj); // free } + +static void ipc_event_binding(json_object *sb_obj) { + json_object *obj = json_object_new_object(); + json_object_object_add(obj, "change", json_object_new_string("run")); + json_object_object_add(obj, "binding", sb_obj); + + const char *json_string = json_object_to_json_string(obj); + ipc_send_event(json_string, IPC_EVENT_BINDING); + + json_object_put(obj); // free +} + +void ipc_event_binding_keyboard(struct sway_binding *sb) { + json_object *sb_obj = json_object_new_object(); + json_object_object_add(sb_obj, "command", json_object_new_string(sb->command)); + + const char *names[10]; + + int len = get_modifier_names(names, sb->modifiers); + int i; + json_object *modifiers = json_object_new_array(); + for (i = 0; i < len; ++i) { + json_object_array_add(modifiers, json_object_new_string(names[i])); + } + + json_object_object_add(sb_obj, "event_state_mask", modifiers); + + // TODO: implement bindcode + json_object_object_add(sb_obj, "input_code", json_object_new_int(0)); + + json_object *symbols = json_object_new_array(); + uint32_t keysym; + char buffer[64]; + for (i = 0; i < sb->keys->length; ++i) { + keysym = *(uint32_t *)sb->keys->items[i]; + if (xkb_keysym_get_name(keysym, buffer, 64) > 0) { + json_object_array_add(symbols, json_object_new_string(buffer)); + } + } + + json_object_object_add(sb_obj, "symbols", symbols); + json_object_object_add(sb_obj, "input_type", json_object_new_string("keyboard")); + + ipc_event_binding(sb_obj); +}