aboutsummaryrefslogtreecommitdiffstats
path: root/sway
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-12-14 08:11:51 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-12-14 08:11:51 -0500
commit3ef6173c15e5c3df2208df81f5e15e8fb8ca6d2c (patch)
treef1b0ebb5decb035f70e4a96f9b1efb246049a4e1 /sway
parentMerge pull request #310 from fluxchief/set-focused-container-fix (diff)
parentImplement bar option: tray_padding <px> [px] (diff)
downloadsway-3ef6173c15e5c3df2208df81f5e15e8fb8ca6d2c.tar.gz
sway-3ef6173c15e5c3df2208df81f5e15e8fb8ca6d2c.tar.zst
sway-3ef6173c15e5c3df2208df81f5e15e8fb8ca6d2c.zip
Merge pull request #311 from mikkeloscar/bar-config-parser
Implement bar { } config parsing
Diffstat (limited to 'sway')
-rw-r--r--sway/commands.c104
-rw-r--r--sway/config.c30
2 files changed, 127 insertions, 7 deletions
diff --git a/sway/commands.c b/sway/commands.c
index 74b307e6..4e9d2796 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -32,6 +32,7 @@ struct cmd_handler {
32 sway_cmd *handle; 32 sway_cmd *handle;
33}; 33};
34 34
35static sway_cmd cmd_bar;
35static sway_cmd cmd_bindsym; 36static sway_cmd cmd_bindsym;
36static sway_cmd cmd_debuglog; 37static sway_cmd cmd_debuglog;
37static sway_cmd cmd_exec; 38static sway_cmd cmd_exec;
@@ -63,6 +64,9 @@ static sway_cmd cmd_sticky;
63static sway_cmd cmd_workspace; 64static sway_cmd cmd_workspace;
64static sway_cmd cmd_ws_auto_back_and_forth; 65static sway_cmd cmd_ws_auto_back_and_forth;
65 66
67static sway_cmd bar_cmd_tray_padding;
68static sway_cmd bar_cmd_workspace_buttons;
69
66swayc_t *sp_view; 70swayc_t *sp_view;
67int sp_index = 0; 71int sp_index = 0;
68 72
@@ -1099,6 +1103,43 @@ static struct cmd_results *cmd_resize(int argc, char **argv) {
1099 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 1103 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
1100} 1104}
1101 1105
1106static struct cmd_results *cmd_bar(int argc, char **argv) {
1107 struct cmd_results *error = NULL;
1108 if ((error = checkarg(argc, "bar", EXPECTED_EQUAL_TO, 1))) {
1109 return error;
1110 }
1111
1112 if (strcmp("{", argv[0]) != 0) {
1113 return cmd_results_new(CMD_INVALID, "bar",
1114 "Expected '{' at start of bar config definition.");
1115 }
1116
1117 if (!config->reading) {
1118 return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
1119 }
1120
1121 // Create new bar from default bar config
1122 struct bar_config *bar = NULL;
1123 bar = malloc(sizeof*bar);
1124 bar->mode = strdup(config->bar.mode);
1125 bar->hidden_state = strdup(config->bar.hidden_state);
1126 bar->modifier = config->bar.modifier;
1127 bar->position = config->bar.position;
1128 bar->status_command = strdup(config->bar.status_command);
1129 bar->font = strdup(config->bar.font);
1130 bar->bar_height = config->bar.bar_height;
1131 bar->workspace_buttons = config->bar.workspace_buttons;
1132 bar->strip_workspace_numbers = config->bar.strip_workspace_numbers;
1133 bar->binding_mode_indicator = config->bar.binding_mode_indicator;
1134 bar->tray_padding = config->bar.tray_padding;
1135 list_add(config->bars, bar);
1136
1137 // Set current bar
1138 config->current_bar = bar;
1139 sway_log(L_DEBUG, "Configuring bar");
1140 return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
1141}
1142
1102static swayc_t *fetch_view_from_scratchpad() { 1143static swayc_t *fetch_view_from_scratchpad() {
1103 if (sp_index >= scratchpad->length) { 1144 if (sp_index >= scratchpad->length) {
1104 sp_index = 0; 1145 sp_index = 0;
@@ -1446,6 +1487,7 @@ static struct cmd_results *cmd_ws_auto_back_and_forth(int argc, char **argv) {
1446 1487
1447/* Keep alphabetized */ 1488/* Keep alphabetized */
1448static struct cmd_handler handlers[] = { 1489static struct cmd_handler handlers[] = {
1490 { "bar", cmd_bar },
1449 { "bindsym", cmd_bindsym }, 1491 { "bindsym", cmd_bindsym },
1450 { "debuglog", cmd_debuglog }, 1492 { "debuglog", cmd_debuglog },
1451 { "default_orientation", cmd_orientation }, 1493 { "default_orientation", cmd_orientation },
@@ -1479,6 +1521,51 @@ static struct cmd_handler handlers[] = {
1479 { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, 1521 { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
1480}; 1522};
1481 1523
1524static struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) {
1525 struct cmd_results *error = NULL;
1526 if ((error = checkarg(argc, "tray_padding", EXPECTED_AT_LEAST, 1))) {
1527 return error;
1528 }
1529
1530 if (!config->current_bar) {
1531 return cmd_results_new(CMD_FAILURE, "tray_padding", "No bar defined.");
1532 }
1533
1534 int padding = atoi(argv[0]);
1535 if (padding < 0) {
1536 return cmd_results_new(CMD_INVALID, "tray_padding",
1537 "Invalid padding value %s, minimum is 0", argv[0]);
1538 }
1539
1540 if (argc > 1 && strcasecmp("px", argv[1]) != 0) {
1541 return cmd_results_new(CMD_INVALID, "tray_padding",
1542 "Unknown unit %s", argv[1]);
1543 }
1544 config->current_bar->tray_padding = padding;
1545 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
1546}
1547
1548static struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) {
1549 struct cmd_results *error = NULL;
1550 if ((error = checkarg(argc, "workspace_buttons", EXPECTED_EQUAL_TO, 1))) {
1551 return error;
1552 }
1553
1554 if (!config->current_bar) {
1555 return cmd_results_new(CMD_FAILURE, "workspace_buttons", "No bar defined.");
1556 }
1557
1558 if (strcasecmp("yes", argv[0]) == 0) {
1559 config->current_bar->workspace_buttons = true;
1560 } else if (strcasecmp("no", argv[0]) == 0) {
1561 config->current_bar->workspace_buttons = false;
1562 } else {
1563 error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]);
1564 return error;
1565 }
1566 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
1567}
1568
1482static struct cmd_handler bar_handlers[] = { 1569static struct cmd_handler bar_handlers[] = {
1483 { "binding_mode_indicator", NULL }, 1570 { "binding_mode_indicator", NULL },
1484 { "bindsym", NULL }, 1571 { "bindsym", NULL },
@@ -1494,8 +1581,8 @@ static struct cmd_handler bar_handlers[] = {
1494 { "status_command", NULL }, 1581 { "status_command", NULL },
1495 { "strip_workspace_numbers", NULL }, 1582 { "strip_workspace_numbers", NULL },
1496 { "tray_output", NULL }, 1583 { "tray_output", NULL },
1497 { "tray_padding", NULL }, 1584 { "tray_padding", bar_cmd_tray_padding },
1498 { "workspace_buttons", NULL }, 1585 { "workspace_buttons", bar_cmd_workspace_buttons },
1499}; 1586};
1500 1587
1501static int handler_compare(const void *_a, const void *_b) { 1588static int handler_compare(const void *_a, const void *_b) {
@@ -1505,14 +1592,17 @@ static int handler_compare(const void *_a, const void *_b) {
1505} 1592}
1506 1593
1507static struct cmd_handler *find_handler(char *line, enum cmd_status block) { 1594static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
1508 struct cmd_handler *h = handlers;
1509 if (block == CMD_BLOCK_BAR) {
1510 h = bar_handlers;
1511 }
1512 struct cmd_handler d = { .command=line }; 1595 struct cmd_handler d = { .command=line };
1513 struct cmd_handler *res = bsearch(&d, h, 1596 struct cmd_handler *res = NULL;
1597 if (block == CMD_BLOCK_BAR) {
1598 res = bsearch(&d, bar_handlers,
1599 sizeof(bar_handlers) / sizeof(struct cmd_handler),
1600 sizeof(struct cmd_handler), handler_compare);
1601 } else {
1602 res = bsearch(&d, handlers,
1514 sizeof(handlers) / sizeof(struct cmd_handler), 1603 sizeof(handlers) / sizeof(struct cmd_handler),
1515 sizeof(struct cmd_handler), handler_compare); 1604 sizeof(struct cmd_handler), handler_compare);
1605 }
1516 return res; 1606 return res;
1517} 1607}
1518 1608
diff --git a/sway/config.c b/sway/config.c
index 6c22556f..bb7ecf9e 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -38,6 +38,14 @@ static void free_mode(struct sway_mode *mode) {
38 free(mode); 38 free(mode);
39} 39}
40 40
41static void free_bar(struct bar_config *bar) {
42 free(bar->mode);
43 free(bar->hidden_state);
44 free(bar->status_command);
45 free(bar->font);
46 free(bar);
47}
48
41void free_output_config(struct output_config *oc) { 49void free_output_config(struct output_config *oc) {
42 free(oc->name); 50 free(oc->name);
43 free(oc); 51 free(oc);
@@ -61,6 +69,11 @@ static void free_config(struct sway_config *config) {
61 } 69 }
62 list_free(config->modes); 70 list_free(config->modes);
63 71
72 for (i = 0; i < config->bars->length; ++i) {
73 free_bar(config->bars->items[i]);
74 }
75 list_free(config->bars);
76
64 free_flat_list(config->cmd_queue); 77 free_flat_list(config->cmd_queue);
65 78
66 for (i = 0; i < config->workspace_outputs->length; ++i) { 79 for (i = 0; i < config->workspace_outputs->length; ++i) {
@@ -88,6 +101,7 @@ static bool file_exists(const char *path) {
88static void config_defaults(struct sway_config *config) { 101static void config_defaults(struct sway_config *config) {
89 config->symbols = create_list(); 102 config->symbols = create_list();
90 config->modes = create_list(); 103 config->modes = create_list();
104 config->bars = create_list();
91 config->workspace_outputs = create_list(); 105 config->workspace_outputs = create_list();
92 config->criteria = create_list(); 106 config->criteria = create_list();
93 config->output_configs = create_list(); 107 config->output_configs = create_list();
@@ -130,6 +144,7 @@ static void config_defaults(struct sway_config *config) {
130 config->bar.workspace_buttons = true; 144 config->bar.workspace_buttons = true;
131 config->bar.strip_workspace_numbers = false; 145 config->bar.strip_workspace_numbers = false;
132 config->bar.binding_mode_indicator = true; 146 config->bar.binding_mode_indicator = true;
147 config->bar.tray_padding = 2;
133} 148}
134 149
135static char *get_config_path(void) { 150static char *get_config_path(void) {
@@ -248,11 +263,26 @@ bool read_config(FILE *file, bool is_active) {
248 } 263 }
249 break; 264 break;
250 265
266 case CMD_BLOCK_BAR:
267 if (block == CMD_BLOCK_END) {
268 block = CMD_BLOCK_BAR;
269 } else {
270 sway_log(L_ERROR, "Invalid block '%s'", line);
271 }
272 break;
273
251 case CMD_BLOCK_END: 274 case CMD_BLOCK_END:
252 switch(block) { 275 switch(block) {
253 case CMD_BLOCK_MODE: 276 case CMD_BLOCK_MODE:
254 sway_log(L_DEBUG, "End of mode block"); 277 sway_log(L_DEBUG, "End of mode block");
255 config->current_mode = config->modes->items[0]; 278 config->current_mode = config->modes->items[0];
279 block = CMD_BLOCK_END;
280 break;
281
282 case CMD_BLOCK_BAR:
283 sway_log(L_DEBUG, "End of bar block");
284 config->current_bar = NULL;
285 block = CMD_BLOCK_END;
256 break; 286 break;
257 287
258 case CMD_BLOCK_END: 288 case CMD_BLOCK_END: