aboutsummaryrefslogtreecommitdiff
path: root/sway
diff options
context:
space:
mode:
Diffstat (limited to 'sway')
-rw-r--r--sway/config.c18
-rw-r--r--sway/handlers.c35
-rw-r--r--sway/ipc-server.c53
3 files changed, 96 insertions, 10 deletions
diff --git a/sway/config.c b/sway/config.c
index 95d8f339..d923eea5 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -721,6 +721,24 @@ void free_sway_mouse_binding(struct sway_mouse_binding *binding) {
free(binding);
}
+struct sway_binding *sway_binding_dup(struct sway_binding *sb) {
+ struct sway_binding *new_sb = malloc(sizeof(struct sway_binding));
+
+ new_sb->order = sb->order;
+ new_sb->modifiers = sb->modifiers;
+ new_sb->command = strdup(sb->command);
+
+ new_sb->keys = create_list();
+ int i;
+ for (i = 0; i < sb->keys->length; ++i) {
+ xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
+ *key = *(xkb_keysym_t *)sb->keys->items[i];
+ list_add(new_sb->keys, key);
+ }
+
+ return new_sb;
+}
+
struct bar_config *default_bar_config(void) {
struct bar_config *bar = NULL;
bar = malloc(sizeof(struct bar_config));
diff --git a/sway/handlers.c b/sway/handlers.c
index e0acebea..76778450 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -336,6 +336,29 @@ static void handle_view_state_request(wlc_handle view, enum wlc_view_state_bit s
return;
}
+static void handle_binding_command(struct sway_binding *binding) {
+ struct sway_binding *binding_copy = binding;
+ bool reload = false;
+ // if this is a reload command we need to make a duplicate of the
+ // binding since it will be gone after the reload has completed.
+ if (strcasecmp(binding->command, "reload") == 0) {
+ binding_copy = sway_binding_dup(binding);
+ reload = true;
+ }
+
+ 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);
+
+ if (reload) { // free the binding if we made a copy
+ free_sway_binding(binding_copy);
+ }
+
+ free_cmd_results(res);
+}
+
static bool handle_bindsym(struct sway_binding *binding) {
bool match = false;
int i;
@@ -347,11 +370,7 @@ static bool handle_bindsym(struct sway_binding *binding) {
}
if (match) {
- 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);
- }
- free_cmd_results(res);
+ handle_binding_command(binding);
return true;
}
@@ -362,11 +381,7 @@ 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 cmd_results *res = handle_command(binding->command);
- if (res->status != CMD_SUCCESS) {
- sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error);
- }
- free_cmd_results(res);
+ handle_binding_command(binding);
return true;
}
}
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index f3a4647b..bde20931 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -298,6 +298,10 @@ 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;
+#if SWAY_BINDING_EVENT
+ } else if (strcmp(event_type, "binding") == 0) {
+ client->subscribed_events |= IPC_EVENT_BINDING;
+#endif
} else {
ipc_send_reply(client, "{\"success\": false}", 18);
ipc_client_disconnect(client);
@@ -633,3 +637,52 @@ void ipc_event_modifier(uint32_t modifier, const char *state) {
json_object_put(obj); // free
}
+
+#if SWAY_BINDING_EVENT
+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
+}
+#endif
+
+void ipc_event_binding_keyboard(struct sway_binding *sb) {
+#if SWAY_BINDING_EVENT
+ 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);
+#endif
+}