diff options
author | Ian Fan <ianfan0@gmail.com> | 2018-07-08 20:34:47 +0100 |
---|---|---|
committer | Ian Fan <ianfan0@gmail.com> | 2018-07-10 12:37:37 +0100 |
commit | 23c1c26c3fedf5470dbee9fe97c2374a48588863 (patch) | |
tree | 751b5569e4e05149c7a7cd7e0d8084be5ff62063 | |
parent | Add get_binding_modes message type to ipc (diff) | |
download | sway-23c1c26c3fedf5470dbee9fe97c2374a48588863.tar.gz sway-23c1c26c3fedf5470dbee9fe97c2374a48588863.tar.zst sway-23c1c26c3fedf5470dbee9fe97c2374a48588863.zip |
Add get_config message type to ipc
-rw-r--r-- | include/ipc.h | 1 | ||||
-rw-r--r-- | include/sway/config.h | 4 | ||||
-rw-r--r-- | sway/commands/output/background.c | 2 | ||||
-rw-r--r-- | sway/commands/reload.c | 2 | ||||
-rw-r--r-- | sway/config.c | 53 | ||||
-rw-r--r-- | sway/ipc-json.c | 2 | ||||
-rw-r--r-- | sway/ipc-server.c | 12 | ||||
-rw-r--r-- | swaymsg/main.c | 15 | ||||
-rw-r--r-- | swaymsg/swaymsg.1.scd | 3 |
9 files changed, 76 insertions, 18 deletions
diff --git a/include/ipc.h b/include/ipc.h index c9c5b1cd..6f6795b3 100644 --- a/include/ipc.h +++ b/include/ipc.h | |||
@@ -14,6 +14,7 @@ enum ipc_command_type { | |||
14 | IPC_GET_BAR_CONFIG = 6, | 14 | IPC_GET_BAR_CONFIG = 6, |
15 | IPC_GET_VERSION = 7, | 15 | IPC_GET_VERSION = 7, |
16 | IPC_GET_BINDING_MODES = 8, | 16 | IPC_GET_BINDING_MODES = 8, |
17 | IPC_GET_CONFIG = 9, | ||
17 | 18 | ||
18 | // sway-specific command types | 19 | // sway-specific command types |
19 | IPC_GET_INPUTS = 100, | 20 | IPC_GET_INPUTS = 100, |
diff --git a/include/sway/config.h b/include/sway/config.h index ac668c24..d5e4116f 100644 --- a/include/sway/config.h +++ b/include/sway/config.h | |||
@@ -341,6 +341,7 @@ struct sway_config { | |||
341 | int gaps_outer; | 341 | int gaps_outer; |
342 | 342 | ||
343 | list_t *config_chain; | 343 | list_t *config_chain; |
344 | const char *current_config_path; | ||
344 | const char *current_config; | 345 | const char *current_config; |
345 | 346 | ||
346 | enum sway_container_border border; | 347 | enum sway_container_border border; |
@@ -496,7 +497,4 @@ void config_update_font_height(bool recalculate); | |||
496 | /* Global config singleton. */ | 497 | /* Global config singleton. */ |
497 | extern struct sway_config *config; | 498 | extern struct sway_config *config; |
498 | 499 | ||
499 | /* Config file currently being read */ | ||
500 | extern const char *current_config_path; | ||
501 | |||
502 | #endif | 500 | #endif |
diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c index c2c138f8..4ed56c2a 100644 --- a/sway/commands/output/background.c +++ b/sway/commands/output/background.c | |||
@@ -80,7 +80,7 @@ struct cmd_results *output_cmd_background(int argc, char **argv) { | |||
80 | if (config->reading && *src != '/') { | 80 | if (config->reading && *src != '/') { |
81 | // src file is inside configuration dir | 81 | // src file is inside configuration dir |
82 | 82 | ||
83 | char *conf = strdup(config->current_config); | 83 | char *conf = strdup(config->current_config_path); |
84 | if (!conf) { | 84 | if (!conf) { |
85 | wlr_log(WLR_ERROR, "Failed to duplicate string"); | 85 | wlr_log(WLR_ERROR, "Failed to duplicate string"); |
86 | free(src); | 86 | free(src); |
diff --git a/sway/commands/reload.c b/sway/commands/reload.c index 9fc213c4..c6715f9c 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c | |||
@@ -7,7 +7,7 @@ struct cmd_results *cmd_reload(int argc, char **argv) { | |||
7 | if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { | 7 | if ((error = checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0))) { |
8 | return error; | 8 | return error; |
9 | } | 9 | } |
10 | if (!load_main_config(config->current_config, true)) { | 10 | if (!load_main_config(config->current_config_path, true)) { |
11 | return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); | 11 | return cmd_results_new(CMD_FAILURE, "reload", "Error(s) reloading config."); |
12 | } | 12 | } |
13 | 13 | ||
diff --git a/sway/config.c b/sway/config.c index d0e0e432..c59f4f0d 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -117,6 +117,7 @@ void free_config(struct sway_config *config) { | |||
117 | free(config->floating_scroll_left_cmd); | 117 | free(config->floating_scroll_left_cmd); |
118 | free(config->floating_scroll_right_cmd); | 118 | free(config->floating_scroll_right_cmd); |
119 | free(config->font); | 119 | free(config->font); |
120 | free((char *)config->current_config_path); | ||
120 | free((char *)config->current_config); | 121 | free((char *)config->current_config); |
121 | free(config); | 122 | free(config); |
122 | } | 123 | } |
@@ -205,6 +206,7 @@ static void config_defaults(struct sway_config *config) { | |||
205 | if (!(config->active_bar_modifiers = create_list())) goto cleanup; | 206 | if (!(config->active_bar_modifiers = create_list())) goto cleanup; |
206 | 207 | ||
207 | if (!(config->config_chain = create_list())) goto cleanup; | 208 | if (!(config->config_chain = create_list())) goto cleanup; |
209 | config->current_config_path = NULL; | ||
208 | config->current_config = NULL; | 210 | config->current_config = NULL; |
209 | 211 | ||
210 | // borders | 212 | // borders |
@@ -304,8 +306,6 @@ static char *get_config_path(void) { | |||
304 | return NULL; // Not reached | 306 | return NULL; // Not reached |
305 | } | 307 | } |
306 | 308 | ||
307 | const char *current_config_path; | ||
308 | |||
309 | static bool load_config(const char *path, struct sway_config *config) { | 309 | static bool load_config(const char *path, struct sway_config *config) { |
310 | if (path == NULL) { | 310 | if (path == NULL) { |
311 | wlr_log(WLR_ERROR, "Unable to find a config file!"); | 311 | wlr_log(WLR_ERROR, "Unable to find a config file!"); |
@@ -313,7 +313,6 @@ static bool load_config(const char *path, struct sway_config *config) { | |||
313 | } | 313 | } |
314 | 314 | ||
315 | wlr_log(WLR_INFO, "Loading config from %s", path); | 315 | wlr_log(WLR_INFO, "Loading config from %s", path); |
316 | current_config_path = path; | ||
317 | 316 | ||
318 | struct stat sb; | 317 | struct stat sb; |
319 | if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { | 318 | if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
@@ -333,7 +332,6 @@ static bool load_config(const char *path, struct sway_config *config) { | |||
333 | wlr_log(WLR_ERROR, "Error(s) loading config!"); | 332 | wlr_log(WLR_ERROR, "Error(s) loading config!"); |
334 | } | 333 | } |
335 | 334 | ||
336 | current_config_path = NULL; | ||
337 | return true; | 335 | return true; |
338 | } | 336 | } |
339 | 337 | ||
@@ -358,7 +356,7 @@ bool load_main_config(const char *file, bool is_active) { | |||
358 | config->active = true; | 356 | config->active = true; |
359 | } | 357 | } |
360 | 358 | ||
361 | config->current_config = path; | 359 | config->current_config_path = path; |
362 | list_add(config->config_chain, path); | 360 | list_add(config->config_chain, path); |
363 | 361 | ||
364 | config->reading = true; | 362 | config->reading = true; |
@@ -428,7 +426,7 @@ bool load_main_config(const char *file, bool is_active) { | |||
428 | static bool load_include_config(const char *path, const char *parent_dir, | 426 | static bool load_include_config(const char *path, const char *parent_dir, |
429 | struct sway_config *config) { | 427 | struct sway_config *config) { |
430 | // save parent config | 428 | // save parent config |
431 | const char *parent_config = config->current_config; | 429 | const char *parent_config = config->current_config_path; |
432 | 430 | ||
433 | char *full_path; | 431 | char *full_path; |
434 | int len = strlen(path); | 432 | int len = strlen(path); |
@@ -466,25 +464,25 @@ static bool load_include_config(const char *path, const char *parent_dir, | |||
466 | } | 464 | } |
467 | } | 465 | } |
468 | 466 | ||
469 | config->current_config = real_path; | 467 | config->current_config_path = real_path; |
470 | list_add(config->config_chain, real_path); | 468 | list_add(config->config_chain, real_path); |
471 | int index = config->config_chain->length - 1; | 469 | int index = config->config_chain->length - 1; |
472 | 470 | ||
473 | if (!load_config(real_path, config)) { | 471 | if (!load_config(real_path, config)) { |
474 | free(real_path); | 472 | free(real_path); |
475 | config->current_config = parent_config; | 473 | config->current_config_path = parent_config; |
476 | list_del(config->config_chain, index); | 474 | list_del(config->config_chain, index); |
477 | return false; | 475 | return false; |
478 | } | 476 | } |
479 | 477 | ||
480 | // restore current_config | 478 | // restore current_config_path |
481 | config->current_config = parent_config; | 479 | config->current_config_path = parent_config; |
482 | return true; | 480 | return true; |
483 | } | 481 | } |
484 | 482 | ||
485 | bool load_include_configs(const char *path, struct sway_config *config) { | 483 | bool load_include_configs(const char *path, struct sway_config *config) { |
486 | char *wd = getcwd(NULL, 0); | 484 | char *wd = getcwd(NULL, 0); |
487 | char *parent_path = strdup(config->current_config); | 485 | char *parent_path = strdup(config->current_config_path); |
488 | const char *parent_dir = dirname(parent_path); | 486 | const char *parent_dir = dirname(parent_path); |
489 | 487 | ||
490 | if (chdir(parent_dir) < 0) { | 488 | if (chdir(parent_dir) < 0) { |
@@ -561,6 +559,23 @@ static char *expand_line(const char *block, const char *line, bool add_brace) { | |||
561 | } | 559 | } |
562 | 560 | ||
563 | bool read_config(FILE *file, struct sway_config *config) { | 561 | bool read_config(FILE *file, struct sway_config *config) { |
562 | bool reading_main_config = false; | ||
563 | char *current_config, *config_pos; | ||
564 | long config_size = 0; | ||
565 | if (config->current_config == NULL) { | ||
566 | reading_main_config = true; | ||
567 | |||
568 | fseek(file, 0, SEEK_END); | ||
569 | config_size = ftell(file); | ||
570 | rewind(file); | ||
571 | |||
572 | config_pos = current_config = malloc(config_size + 1); | ||
573 | if (current_config == NULL) { | ||
574 | wlr_log(WLR_ERROR, "Unable to allocate buffer for config contents"); | ||
575 | return false; | ||
576 | } | ||
577 | } | ||
578 | |||
564 | bool success = true; | 579 | bool success = true; |
565 | int line_number = 0; | 580 | int line_number = 0; |
566 | char *line; | 581 | char *line; |
@@ -573,6 +588,14 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
573 | } | 588 | } |
574 | line_number++; | 589 | line_number++; |
575 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); | 590 | wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); |
591 | |||
592 | if (reading_main_config) { | ||
593 | size_t l = strlen(line); | ||
594 | memcpy(config_pos, line, l); // don't copy terminating character | ||
595 | config_pos += l; | ||
596 | *config_pos++ = '\n'; | ||
597 | } | ||
598 | |||
576 | line = strip_whitespace(line); | 599 | line = strip_whitespace(line); |
577 | if (line[0] == '#') { | 600 | if (line[0] == '#') { |
578 | free(line); | 601 | free(line); |
@@ -592,6 +615,8 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
592 | if (!expanded) { | 615 | if (!expanded) { |
593 | list_foreach(stack, free); | 616 | list_foreach(stack, free); |
594 | list_free(stack); | 617 | list_free(stack); |
618 | free(line); | ||
619 | free(current_config); | ||
595 | return false; | 620 | return false; |
596 | } | 621 | } |
597 | wlr_log(WLR_DEBUG, "Expanded line: %s", expanded); | 622 | wlr_log(WLR_DEBUG, "Expanded line: %s", expanded); |
@@ -607,7 +632,7 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
607 | case CMD_FAILURE: | 632 | case CMD_FAILURE: |
608 | case CMD_INVALID: | 633 | case CMD_INVALID: |
609 | wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number, | 634 | wlr_log(WLR_ERROR, "Error on line %i '%s': %s (%s)", line_number, |
610 | line, res->error, config->current_config); | 635 | line, res->error, config->current_config_path); |
611 | success = false; | 636 | success = false; |
612 | break; | 637 | break; |
613 | 638 | ||
@@ -652,6 +677,10 @@ bool read_config(FILE *file, struct sway_config *config) { | |||
652 | list_foreach(stack, free); | 677 | list_foreach(stack, free); |
653 | list_free(stack); | 678 | list_free(stack); |
654 | 679 | ||
680 | if (reading_main_config) { | ||
681 | current_config[config_size - 1] = '\0'; | ||
682 | config->current_config = current_config; | ||
683 | } | ||
655 | return success; | 684 | return success; |
656 | } | 685 | } |
657 | 686 | ||
diff --git a/sway/ipc-json.c b/sway/ipc-json.c index b9289e25..3d0e88f0 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c | |||
@@ -2,6 +2,7 @@ | |||
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <ctype.h> | 3 | #include <ctype.h> |
4 | #include "log.h" | 4 | #include "log.h" |
5 | #include "sway/config.h" | ||
5 | #include "sway/ipc-json.h" | 6 | #include "sway/ipc-json.h" |
6 | #include "sway/tree/container.h" | 7 | #include "sway/tree/container.h" |
7 | #include "sway/tree/workspace.h" | 8 | #include "sway/tree/workspace.h" |
@@ -41,6 +42,7 @@ json_object *ipc_json_get_version() { | |||
41 | json_object_object_add(version, "major", json_object_new_int(major)); | 42 | json_object_object_add(version, "major", json_object_new_int(major)); |
42 | json_object_object_add(version, "minor", json_object_new_int(minor)); | 43 | json_object_object_add(version, "minor", json_object_new_int(minor)); |
43 | json_object_object_add(version, "patch", json_object_new_int(patch)); | 44 | json_object_object_add(version, "patch", json_object_new_int(patch)); |
45 | json_object_object_add(version, "loaded_config_file_name", json_object_new_string(config->current_config_path)); | ||
44 | 46 | ||
45 | return version; | 47 | return version; |
46 | } | 48 | } |
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 70a4141e..c5161a6b 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -17,6 +17,7 @@ | |||
17 | #include <unistd.h> | 17 | #include <unistd.h> |
18 | #include <wayland-server.h> | 18 | #include <wayland-server.h> |
19 | #include "sway/commands.h" | 19 | #include "sway/commands.h" |
20 | #include "sway/config.h" | ||
20 | #include "sway/ipc-json.h" | 21 | #include "sway/ipc-json.h" |
21 | #include "sway/ipc-server.h" | 22 | #include "sway/ipc-server.h" |
22 | #include "sway/output.h" | 23 | #include "sway/output.h" |
@@ -681,6 +682,17 @@ void ipc_client_handle_command(struct ipc_client *client) { | |||
681 | goto exit_cleanup; | 682 | goto exit_cleanup; |
682 | } | 683 | } |
683 | 684 | ||
685 | case IPC_GET_CONFIG: | ||
686 | { | ||
687 | json_object *json = json_object_new_object(); | ||
688 | json_object_object_add(json, "config", json_object_new_string(config->current_config)); | ||
689 | const char *json_string = json_object_to_json_string(json); | ||
690 | client_valid = | ||
691 | ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); | ||
692 | json_object_put(json); // free | ||
693 | goto exit_cleanup; | ||
694 | } | ||
695 | |||
684 | default: | 696 | default: |
685 | wlr_log(WLR_INFO, "Unknown IPC command type %i", client->current_command); | 697 | wlr_log(WLR_INFO, "Unknown IPC command type %i", client->current_command); |
686 | goto exit_cleanup; | 698 | goto exit_cleanup; |
diff --git a/swaymsg/main.c b/swaymsg/main.c index 42e488f3..4c068f69 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -240,6 +240,12 @@ static void pretty_print_version(json_object *v) { | |||
240 | printf("sway version %s\n", json_object_get_string(ver)); | 240 | printf("sway version %s\n", json_object_get_string(ver)); |
241 | } | 241 | } |
242 | 242 | ||
243 | static void pretty_print_config(json_object *c) { | ||
244 | json_object *config; | ||
245 | json_object_object_get_ex(c, "config", &config); | ||
246 | printf("%s\n", json_object_get_string(config)); | ||
247 | } | ||
248 | |||
243 | static void pretty_print_clipboard(json_object *v) { | 249 | static void pretty_print_clipboard(json_object *v) { |
244 | if (success(v, true)) { | 250 | if (success(v, true)) { |
245 | if (json_object_is_type(v, json_type_array)) { | 251 | if (json_object_is_type(v, json_type_array)) { |
@@ -277,7 +283,7 @@ static void pretty_print(int type, json_object *resp) { | |||
277 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && | 283 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && |
278 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && | 284 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && |
279 | type != IPC_GET_VERSION && type != IPC_GET_CLIPBOARD && | 285 | type != IPC_GET_VERSION && type != IPC_GET_CLIPBOARD && |
280 | type != IPC_GET_SEATS) { | 286 | type != IPC_GET_SEATS && type != IPC_GET_CONFIG) { |
281 | printf("%s\n", json_object_to_json_string_ext(resp, | 287 | printf("%s\n", json_object_to_json_string_ext(resp, |
282 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 288 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
283 | return; | 289 | return; |
@@ -288,6 +294,11 @@ static void pretty_print(int type, json_object *resp) { | |||
288 | return; | 294 | return; |
289 | } | 295 | } |
290 | 296 | ||
297 | if (type == IPC_GET_CONFIG) { | ||
298 | pretty_print_config(resp); | ||
299 | return; | ||
300 | } | ||
301 | |||
291 | if (type == IPC_GET_CLIPBOARD) { | 302 | if (type == IPC_GET_CLIPBOARD) { |
292 | pretty_print_clipboard(resp); | 303 | pretty_print_clipboard(resp); |
293 | return; | 304 | return; |
@@ -409,6 +420,8 @@ int main(int argc, char **argv) { | |||
409 | type = IPC_GET_VERSION; | 420 | type = IPC_GET_VERSION; |
410 | } else if (strcasecmp(cmdtype, "get_binding_modes") == 0) { | 421 | } else if (strcasecmp(cmdtype, "get_binding_modes") == 0) { |
411 | type = IPC_GET_BINDING_MODES; | 422 | type = IPC_GET_BINDING_MODES; |
423 | } else if (strcasecmp(cmdtype, "get_config") == 0) { | ||
424 | type = IPC_GET_CONFIG; | ||
412 | } else if (strcasecmp(cmdtype, "get_clipboard") == 0) { | 425 | } else if (strcasecmp(cmdtype, "get_clipboard") == 0) { |
413 | type = IPC_GET_CLIPBOARD; | 426 | type = IPC_GET_CLIPBOARD; |
414 | } else { | 427 | } else { |
diff --git a/swaymsg/swaymsg.1.scd b/swaymsg/swaymsg.1.scd index f9b600b9..59a706d4 100644 --- a/swaymsg/swaymsg.1.scd +++ b/swaymsg/swaymsg.1.scd | |||
@@ -62,6 +62,9 @@ _swaymsg_ [options...] [message] | |||
62 | *get\_binding\_modes* | 62 | *get\_binding\_modes* |
63 | Gets a JSON-encoded list of currently configured binding modes. | 63 | Gets a JSON-encoded list of currently configured binding modes. |
64 | 64 | ||
65 | *get\_config* | ||
66 | Gets a JSON-encoded copy of the current configuration. | ||
67 | |||
65 | *get\_clipboard* | 68 | *get\_clipboard* |
66 | Get JSON-encoded information about the clipboard. | 69 | Get JSON-encoded information about the clipboard. |
67 | Returns the current clipboard mime-types if called without | 70 | Returns the current clipboard mime-types if called without |