aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/bar.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-05-30 13:20:02 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-06-02 08:07:44 -0400
commit7c810dc344c28d1876c5ee158cb0806289d0f813 (patch)
treedbe756bceca42ea6f9a6cf5e5771037417bb64c3 /sway/commands/bar.c
parentMerge pull request #2080 from frsfnrrg/keyboard-remodeling (diff)
downloadsway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.gz
sway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.zst
sway-7c810dc344c28d1876c5ee158cb0806289d0f813.zip
Make command block implementation generic
Diffstat (limited to 'sway/commands/bar.c')
-rw-r--r--sway/commands/bar.c118
1 files changed, 88 insertions, 30 deletions
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index ff111163..358b3893 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -1,3 +1,4 @@
1#define _XOPEN_SOURCE 500
1#include <string.h> 2#include <string.h>
2#include <strings.h> 3#include <strings.h>
3#include <wlr/util/log.h> 4#include <wlr/util/log.h>
@@ -5,53 +6,110 @@
5#include "sway/config.h" 6#include "sway/config.h"
6#include "util.h" 7#include "util.h"
7 8
9// Must be in alphabetical order for bsearch
10static struct cmd_handler bar_handlers[] = {
11 { "activate_button", bar_cmd_activate_button },
12 { "binding_mode_indicator", bar_cmd_binding_mode_indicator },
13 { "bindsym", bar_cmd_bindsym },
14 { "colors", bar_cmd_colors },
15 { "context_button", bar_cmd_context_button },
16 { "font", bar_cmd_font },
17 { "height", bar_cmd_height },
18 { "hidden_state", bar_cmd_hidden_state },
19 { "icon_theme", bar_cmd_icon_theme },
20 { "id", bar_cmd_id },
21 { "mode", bar_cmd_mode },
22 { "modifier", bar_cmd_modifier },
23 { "output", bar_cmd_output },
24 { "pango_markup", bar_cmd_pango_markup },
25 { "position", bar_cmd_position },
26 { "secondary_button", bar_cmd_secondary_button },
27 { "separator_symbol", bar_cmd_separator_symbol },
28 { "status_command", bar_cmd_status_command },
29 { "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
30 { "swaybar_command", bar_cmd_swaybar_command },
31 { "tray_output", bar_cmd_tray_output },
32 { "tray_padding", bar_cmd_tray_padding },
33 { "workspace_buttons", bar_cmd_workspace_buttons },
34 { "wrap_scroll", bar_cmd_wrap_scroll },
35};
36
37// Must be in alphabetical order for bsearch
38static struct cmd_handler bar_config_handlers[] = {
39 { "hidden_state", bar_cmd_hidden_state },
40 { "mode", bar_cmd_mode }
41};
42
8struct cmd_results *cmd_bar(int argc, char **argv) { 43struct cmd_results *cmd_bar(int argc, char **argv) {
9 struct cmd_results *error = NULL; 44 struct cmd_results *error = NULL;
10 if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { 45 if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
11 return error; 46 return error;
12 } 47 }
13 48
14 if (config->reading && strcmp("{", argv[0]) != 0) { 49 if (!config->reading) {
15 return cmd_results_new(CMD_INVALID, "bar", 50 if (!find_handler(argv[0], bar_config_handlers,
16 "Expected '{' at start of bar config definition."); 51 sizeof(bar_config_handlers))) {
52 return cmd_results_new(CMD_FAILURE, "bar",
53 "Can only be used in config file.");
54 }
55 return subcommand(argv, argc, bar_config_handlers,
56 sizeof(bar_config_handlers));
17 } 57 }
18 58
19 if (!config->reading) { 59 if (argc > 1) {
20 if (argc > 1) { 60 struct bar_config *bar = NULL;
21 if (strcasecmp("mode", argv[0]) == 0) { 61 if (!find_handler(argv[0], bar_handlers, sizeof(bar_handlers))
22 return bar_cmd_mode(argc-1, argv + 1); 62 && find_handler(argv[1], bar_handlers, sizeof(bar_handlers))) {
63 for (int i = 0; i < config->bars->length; ++i) {
64 struct bar_config *item = config->bars->items[i];
65 if (strcmp(item->id, argv[0]) == 0) {
66 wlr_log(L_DEBUG, "Selecting bar: %s", argv[0]);
67 bar = item;
68 break;
69 }
23 } 70 }
71 if (!bar) {
72 wlr_log(L_DEBUG, "Creating bar: %s", argv[0]);
73 bar = default_bar_config();
74 if (!bar) {
75 return cmd_results_new(CMD_FAILURE, "bar",
76 "Unable to allocate bar state");
77 }
24 78
25 if (strcasecmp("hidden_state", argv[0]) == 0) { 79 bar->id = strdup(argv[0]);
26 return bar_cmd_hidden_state(argc-1, argv + 1);
27 } 80 }
81 config->current_bar = bar;
82 ++argv; --argc;
28 } 83 }
29 return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
30 } 84 }
31 85
32 // Create new bar with default values 86 if (!config->current_bar) {
33 struct bar_config *bar = default_bar_config(); 87 // Create new bar with default values
34 if (!bar) { 88 struct bar_config *bar = default_bar_config();
35 return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state"); 89 if (!bar) {
36 } 90 return cmd_results_new(CMD_FAILURE, "bar",
91 "Unable to allocate bar state");
92 }
37 93
38 // set bar id 94 // set bar id
39 for (int i = 0; i < config->bars->length; ++i) { 95 for (int i = 0; i < config->bars->length; ++i) {
40 if (bar == config->bars->items[i]) { 96 if (bar == config->bars->items[i]) {
41 const int len = 5 + numlen(i); // "bar-" + i + \0 97 const int len = 5 + numlen(i); // "bar-" + i + \0
42 bar->id = malloc(len * sizeof(char)); 98 bar->id = malloc(len * sizeof(char));
43 if (bar->id) { 99 if (bar->id) {
44 snprintf(bar->id, len, "bar-%d", i); 100 snprintf(bar->id, len, "bar-%d", i);
45 } else { 101 } else {
46 return cmd_results_new(CMD_FAILURE, 102 return cmd_results_new(CMD_FAILURE,
47 "bar", "Unable to allocate bar ID"); 103 "bar", "Unable to allocate bar ID");
104 }
105 break;
48 } 106 }
49 break;
50 } 107 }
108
109 // Set current bar
110 config->current_bar = bar;
111 wlr_log(L_DEBUG, "Creating bar %s", bar->id);
51 } 112 }
52 113
53 // Set current bar 114 return subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
54 config->current_bar = bar;
55 wlr_log(L_DEBUG, "Configuring bar %s", bar->id);
56 return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
57} 115}