diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:10:33 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:11:08 -0400 |
commit | 8efee109ad2ab4861f25e54e9f6d1ceb06203791 (patch) | |
tree | 9cd16bb63d16285620dc16a09da5582f211c0a87 | |
parent | Clean up imported bar commands (diff) | |
download | sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.tar.gz sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.tar.zst sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.zip |
Implement modes
-rw-r--r-- | include/sway/ipc-server.h | 1 | ||||
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/mode.c | 59 | ||||
-rw-r--r-- | sway/ipc-server.c | 14 | ||||
-rw-r--r-- | sway/meson.build | 1 |
5 files changed, 74 insertions, 2 deletions
diff --git a/include/sway/ipc-server.h b/include/sway/ipc-server.h index 1f6fffff..b4db75c3 100644 --- a/include/sway/ipc-server.h +++ b/include/sway/ipc-server.h | |||
@@ -13,5 +13,6 @@ struct sockaddr_un *ipc_user_sockaddr(void); | |||
13 | void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); | 13 | void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change); |
14 | void ipc_event_window(swayc_t *window, const char *change); | 14 | void ipc_event_window(swayc_t *window, const char *change); |
15 | void ipc_event_barconfig_update(struct bar_config *bar); | 15 | void ipc_event_barconfig_update(struct bar_config *bar); |
16 | void ipc_event_mode(const char *mode); | ||
16 | 17 | ||
17 | #endif | 18 | #endif |
diff --git a/sway/commands.c b/sway/commands.c index 38e2f764..bcc777ed 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -100,6 +100,7 @@ static struct cmd_handler handlers[] = { | |||
100 | { "exec_always", cmd_exec_always }, | 100 | { "exec_always", cmd_exec_always }, |
101 | { "include", cmd_include }, | 101 | { "include", cmd_include }, |
102 | { "input", cmd_input }, | 102 | { "input", cmd_input }, |
103 | { "mode", cmd_mode }, | ||
103 | { "output", cmd_output }, | 104 | { "output", cmd_output }, |
104 | { "seat", cmd_seat }, | 105 | { "seat", cmd_seat }, |
105 | { "workspace", cmd_workspace }, | 106 | { "workspace", cmd_workspace }, |
diff --git a/sway/commands/mode.c b/sway/commands/mode.c new file mode 100644 index 00000000..c30a8bac --- /dev/null +++ b/sway/commands/mode.c | |||
@@ -0,0 +1,59 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <stdbool.h> | ||
3 | #include <string.h> | ||
4 | #include <strings.h> | ||
5 | #include "sway/commands.h" | ||
6 | #include "sway/config.h" | ||
7 | #include "sway/ipc-server.h" | ||
8 | #include "list.h" | ||
9 | #include "log.h" | ||
10 | |||
11 | struct cmd_results *cmd_mode(int argc, char **argv) { | ||
12 | struct cmd_results *error = NULL; | ||
13 | if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { | ||
14 | return error; | ||
15 | } | ||
16 | |||
17 | const char *mode_name = argv[0]; | ||
18 | bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0); | ||
19 | if (new_mode && !config->reading) { | ||
20 | return cmd_results_new(CMD_FAILURE, | ||
21 | "mode", "Can only be used in config file."); | ||
22 | } | ||
23 | struct sway_mode *mode = NULL; | ||
24 | // Find mode | ||
25 | for (int i = 0; i < config->modes->length; ++i) { | ||
26 | struct sway_mode *test = config->modes->items[i]; | ||
27 | if (strcasecmp(test->name, mode_name) == 0) { | ||
28 | mode = test; | ||
29 | break; | ||
30 | } | ||
31 | } | ||
32 | // Create mode if it doesn't exist | ||
33 | if (!mode && new_mode) { | ||
34 | mode = calloc(1, sizeof(struct sway_mode)); | ||
35 | if (!mode) { | ||
36 | return cmd_results_new(CMD_FAILURE, | ||
37 | "mode", "Unable to allocate mode"); | ||
38 | } | ||
39 | mode->name = strdup(mode_name); | ||
40 | mode->keysym_bindings = create_list(); | ||
41 | mode->keycode_bindings = create_list(); | ||
42 | list_add(config->modes, mode); | ||
43 | } | ||
44 | if (!mode) { | ||
45 | error = cmd_results_new(CMD_INVALID, | ||
46 | "mode", "Unknown mode `%s'", mode_name); | ||
47 | return error; | ||
48 | } | ||
49 | if ((config->reading && new_mode) || (!config->reading && !new_mode)) { | ||
50 | wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); | ||
51 | } | ||
52 | // Set current mode | ||
53 | config->current_mode = mode; | ||
54 | if (!new_mode) { | ||
55 | // trigger IPC mode event | ||
56 | ipc_event_mode(config->current_mode->name); | ||
57 | } | ||
58 | return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); | ||
59 | } | ||
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 8250b3a0..c3b589a6 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -290,7 +290,7 @@ void ipc_event_window(swayc_t *window, const char *change) { | |||
290 | 290 | ||
291 | const char *json_string = json_object_to_json_string(obj); | 291 | const char *json_string = json_object_to_json_string(obj); |
292 | ipc_send_event(json_string, IPC_EVENT_WINDOW); | 292 | ipc_send_event(json_string, IPC_EVENT_WINDOW); |
293 | json_object_put(obj); // free | 293 | json_object_put(obj); |
294 | } | 294 | } |
295 | 295 | ||
296 | void ipc_event_barconfig_update(struct bar_config *bar) { | 296 | void ipc_event_barconfig_update(struct bar_config *bar) { |
@@ -299,7 +299,17 @@ void ipc_event_barconfig_update(struct bar_config *bar) { | |||
299 | 299 | ||
300 | const char *json_string = json_object_to_json_string(json); | 300 | const char *json_string = json_object_to_json_string(json); |
301 | ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE); | 301 | ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE); |
302 | json_object_put(json); // free | 302 | json_object_put(json); |
303 | } | ||
304 | |||
305 | void ipc_event_mode(const char *mode) { | ||
306 | wlr_log(L_DEBUG, "Sending mode::%s event", mode); | ||
307 | json_object *obj = json_object_new_object(); | ||
308 | json_object_object_add(obj, "change", json_object_new_string(mode)); | ||
309 | |||
310 | const char *json_string = json_object_to_json_string(obj); | ||
311 | ipc_send_event(json_string, IPC_EVENT_MODE); | ||
312 | json_object_put(obj); | ||
303 | } | 313 | } |
304 | 314 | ||
305 | int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { | 315 | int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) { |
diff --git a/sway/meson.build b/sway/meson.build index 54c03061..1e7ee7ae 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -16,6 +16,7 @@ sway_sources = files( | |||
16 | 'commands/include.c', | 16 | 'commands/include.c', |
17 | 'commands/input.c', | 17 | 'commands/input.c', |
18 | 'commands/layout.c', | 18 | 'commands/layout.c', |
19 | 'commands/mode.c', | ||
19 | 'commands/seat.c', | 20 | 'commands/seat.c', |
20 | 'commands/seat/attach.c', | 21 | 'commands/seat/attach.c', |
21 | 'commands/seat/fallback.c', | 22 | 'commands/seat/fallback.c', |