syntax overhaul

This commit is contained in:
FelixKratz 2021-10-18 18:10:51 +02:00
parent f4b3cd7b44
commit 99be427720
11 changed files with 317 additions and 351 deletions

View file

@ -170,7 +170,7 @@ sketchybar -m batch --add item <name> <position> [optional: <modifier>]
where the *name* should not contain whitespaces, it can be used to further configure the item, which is covered later.
The *position* is the placement in the bar and can be either *left*, *right* or *center*.
The list of modifiers for item creation is short currently:
* *nospace*: This item reserves no space in the bar and the next item starts exactly where this item starts (good for stacked widgets)
* *nospace*: This item reserves no space in the bar and the next item starts exactly where this item starts (good for stacked widgets) (DEPRECATED on HEAD, use *width* property)
### Adding a component
```bash
@ -207,32 +207,32 @@ A list of properties available to the *set* command is listed below:
* *associated_space*: on which space to show this item (can be multiple, not specifying anything will show item on all spaces)
* *associated_display*: on which displays to show this item (can be multiple, not specifying anything will show item on all displays)
* *icon*: the icon of the item
* *icon_font*: the font for the icon
* *icon_color*: the color of the icon
* *icon_highlight_color*: the highlight color of the icon (e.g. for active space icon)
* *icon_padding_left*: left padding of icon (default: 0)
* *icon_padding_right*: right padding of icon (default: 0)
* *icon_highlight*: wether the icon is highlighted with the *icon_highlight_color* (values: *on*, *off*, *toggle*, default: *off*)
* *icon.font*: the font for the icon
* *icon.color*: the color of the icon
* *icon.highlight_color*: the highlight color of the icon (e.g. for active space icon)
* *icon.padding_left*: left padding of icon (default: 0)
* *icon.padding_right*: right padding of icon (default: 0)
* *icon.highlight*: wether the icon is highlighted with the *icon_highlight_color* (values: *on*, *off*, *toggle*, default: *off*)
* *label*: the label of the item
* *label_font*: the font for the label
* *label_color*: the color of the label
* *label_highlight_color*: the highlight color of the label (e.g. for active space icon)
* *label_padding_left*: left padding of label (default: 0)
* *label_padding_right*: right padding of label (default: 0)
* *label_highlight*: wether the label is highlighted with the *label_highlight_color* (values: *on*, *off*, *toggle*, default: *off*)
* *draws_background*: wether the item should draw a background (values: *on*, *off*, *toggle*, default: *off*)
* *background_color*: draws a rectangular background for this item in the given color (this automatically activates *draws_background*)
* *background_height*: the height of the background, the background will always be centered vertically around the center of the item
* *background_border_color*: the color of the backgrounds border
* *background_corner_radius*: the corner radius of the items background (default: 0)
* *background_border_width*: the border width of the items background (default: 0)
* *background_padding_left*: the left padding applied around the background of the item (default: 0)
* *background_padding_right*: the right padding applied around the background of the item (default: 0)
* *width*: overrides the width of the item (useful for items which frequently change in width and thus move all other items) (HEAD only)
* *label.font*: the font for the label
* *label.color*: the color of the label
* *label.highlight_color*: the highlight color of the label (e.g. for active space icon)
* *label.padding_left*: left padding of label (default: 0)
* *label.padding_right*: right padding of label (default: 0)
* *label.highlight*: wether the label is highlighted with the *label_highlight_color* (values: *on*, *off*, *toggle*, default: *off*)
* *background.drawing*: wether the item should draw a background (values: *on*, *off*, *toggle*, default: *off*)
* *background.color*: draws a rectangular background for this item in the given color (this automatically activates *draws_background*)
* *background.height*: the height of the background, the background will always be centered vertically around the center of the item
* *background.border_color*: the color of the backgrounds border
* *background.corner_radius*: the corner radius of the items background (default: 0)
* *background.border_width*: the border width of the items background (default: 0)
* *background.padding_left*: the left padding applied around the background of the item (default: 0)
* *background.padding_right*: the right padding applied around the background of the item (default: 0)
* *width*: overrides the width of the item (useful for items which frequently change in width and thus move all other items) (values: width in points and *dynamic*) (HEAD only)
* *y_offset*: the vertical offset of this item (default: 0)
* *graph_color*: color of the associated graph
* *graph_fill_color*: optional property to override the automatically calculated fill color of the graph
* *graph_line_width*: sets the line width of the associated graph
* *graph.color*: color of the associated graph
* *graph.fill_color*: optional property to override the automatically calculated fill color of the graph
* *graph.line_width*: sets the line width of the associated graph
* *update_freq*: time in seconds between script executions
* *script*: a script to run every *update_freq* seconds
* *click_script*: script to run when left clicking on item (Note: This is also possible via the *mouse_clicked* event, see #subscribing-items-to-system-events-for-their-script-execution)
@ -252,22 +252,22 @@ batching is again possible via:
sketchybar -m batch --default <property>=<value> ... <property>=<value>
```
this currently works for the properties:
* *label_font*
* *label_color*
* *label_highlight_color*
* *label_padding_left*
* *label_padding_right*
* *icon_font*
* *icon_color*
* *icon_highlight_color*
* *icon_padding_left*
* *icon_padding_right*
* *draws_background*
* *background_height*
* *background_color*
* *background_border_color*
* *background_corner_radius*
* *background_border_width*
* *label.font*
* *label.color*
* *label.highlight_color*
* *label.padding_left*
* *label.padding_right*
* *icon.font*
* *icon.color*
* *icon.highlight_color*
* *icon.padding_left*
* *icon.padding_right*
* *background.drawing*
* *background.height*
* *background.color*
* *background.border_color*
* *background.corner_radius*
* *background.border_width*
* *y_offset*
* *update_freq*
* *script* (HEAD only)
@ -341,12 +341,6 @@ sketchybar -m remove item <name>
```
This also works for components, just reference it by name.
### Freeze and unfreeze the bar
```bash
sketchybar -m freeze <on/off>
```
This stops the redrawing of the bar entirely and "freezes" it.
## Querying
*SketchyBar* can be queried for information about a number of things.
### Bar Properties
@ -386,9 +380,9 @@ $SID holds the space id and $DID the display id.
By default the space component invokes the script:
```bash
if [ "$SELECTED" = "true" ]; then
sketchybar -m set $NAME icon_highlight on
sketchybar -m set $NAME icon.highlight on
else
sketchybar -m set $NAME icon_highlight off
sketchybar -m set $NAME icon.highlight off
fi
```
which you can freely configure to your liking by supplying a different script to the space component.
@ -452,9 +446,9 @@ E.g., if I wanted a colored background around *all* my space components (which a
```bash
sketchybar -m batch --add component bracket primary_spaces code writing reading entertainment \
\
--set primary_spaces background_color=0xffffffff \
background_corner_radius=4 \
background_height=20
--set primary_spaces background.color=0xffffffff \
background.corner_radius=4 \
background.height=20
```
this draws a white background below all my space components. I plan to expand the capability of item brackets significantly in the future.

View file

@ -70,3 +70,27 @@ bool background_set_padding_right(struct background* background, uint32_t pad) {
background->padding_right = pad;
return true;
}
static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message) {
if (token_equals(property, PROPERTY_DRAWING))
return background_set_enabled(background, evaluate_boolean_state(get_token(&message), background->enabled));
else if (token_equals(property, PROPERTY_HEIGHT))
return background_set_height(background, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_CORNER_RADIUS))
return background_set_corner_radius(background, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_BORDER_WIDTH))
return background_set_border_width(background, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_COLOR))
return background_set_color(background, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_BORDER_COLOR))
return background_set_border_color(background, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_PADDING_LEFT))
return background_set_padding_left(background, token_to_int(get_token(&message)));
else if (token_equals(property, PROPERTY_PADDING_RIGHT))
return background_set_padding_right(background, token_to_int(get_token(&message)));
else {
fprintf(rsp, "Unknown property: %s \n", property.text);
printf("Unknown property: %s \n", property.text);
}
return false;
}

View file

@ -22,4 +22,6 @@ bool background_set_corner_radius(struct background* background, uint32_t corner
bool background_set_padding_left(struct background* background, uint32_t pad);
bool background_set_padding_right(struct background* background, uint32_t pad);
static bool background_parse_sub_domain(struct background* background, FILE* rsp, struct token property, char* message);
#endif // !BACKGROUND_H

View file

@ -172,9 +172,9 @@ void bar_item_set_type(struct bar_item* bar_item, char type) {
if (type == BAR_COMPONENT_SPACE) {
bar_item_set_script(bar_item, string_copy("if [ \"$SELECTED\" = \"true\" ]; then "
"sketchybar -m set $NAME icon_highlight on;"
"sketchybar -m set $NAME icon.highlight on;"
"else "
"sketchybar -m set $NAME icon_highlight off;"
"sketchybar -m set $NAME icon.highlight off;"
" fi"));
bar_item->update_mask |= UPDATE_SPACE_CHANGE;

View file

@ -1,4 +1,5 @@
#include "graph.h"
void graph_init(struct graph* graph, uint32_t width) {
graph->width = width;
graph->y = malloc(sizeof(float) * width);
@ -35,3 +36,22 @@ uint32_t graph_get_length(struct graph* graph) {
if (graph->enabled) return graph->width;
return 0;
}
static bool graph_parse_sub_domain(struct graph* graph, FILE* rsp, struct token property, char* message) {
if (token_equals(property, PROPERTY_COLOR)) {
graph->line_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
return true;
} else if (token_equals(property, PROPERTY_FILL_COLOR)) {
graph->fill_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
graph->overrides_fill_color = true;
return true;
} else if (token_equals(property, PROPERTY_LINE_WIDTH)) {
graph->line_width = token_to_float(get_token(&message));
return true;
}
else {
fprintf(rsp, "Unknown property: %s \n", property.text);
printf("Unknown property: %s \n", property.text);
}
return false;
}

View file

@ -24,6 +24,7 @@ void graph_destruct(struct graph* graph);
float graph_get_y(struct graph* graph, uint32_t i);
uint32_t graph_get_length(struct graph* graph);
static bool graph_parse_sub_domain(struct graph* graph, FILE* rsp, struct token property, char* message);
#endif

View file

@ -13,98 +13,6 @@ extern struct event_loop g_event_loop;
extern struct bar_manager g_bar_manager;
extern bool g_verbose;
static bool token_equals(struct token token, char *match) {
char *at = match;
for (int i = 0; i < token.length; ++i, ++at) {
if ((*at == 0) || (token.text[i] != *at)) {
return false;
}
}
return *at == 0;
}
static char *token_to_string(struct token token) {
char *result = malloc(token.length + 1);
if (!result) return NULL;
memcpy(result, token.text, token.length);
result[token.length] = '\0';
return result;
}
static uint32_t token_to_uint32t(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return strtoul(buffer, NULL, 0);
}
static int token_to_int(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return (int) strtol(buffer, NULL, 0);
}
static float token_to_float(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return strtof(buffer, NULL);
}
static struct token get_token(char **message) {
struct token token;
token.text = *message;
while (**message) {
++(*message);
}
token.length = *message - token.text;
if ((*message)[0] == '\0' && (*message)[1] != '\0') {
++(*message);
} else {
// NOTE(koekeishiya): don't go past the null-terminator
}
return token;
}
static void get_key_value_pair(char *token, char **key, char **value, char split) {
*key = token;
while (*token) {
if (token[0] == split) break;
++token;
}
if (*token != split) {
*key = NULL;
*value = NULL;
} else if (token[1]) {
*token = '\0';
*value = token + 1;
} else {
*token = '\0';
*value = NULL;
}
}
static void pack_key_value_pair(char* cursor, char* key, char* value) {
uint32_t key_len = strlen(key);
uint32_t val_len = value ? strlen(value) : 0;
memcpy(cursor, key, key_len);
cursor += key_len;
*cursor++ = '\0';
memcpy(cursor, value, val_len);
cursor += val_len;
*cursor++ = '\0';
*cursor++ = '\0';
}
static bool evaluate_boolean_state(struct token state, bool previous_state) {
if (token_equals(state, ARGUMENT_COMMON_VAL_ON) || token_equals(state, ARGUMENT_COMMON_VAL_YES) || token_equals(state, ARGUMENT_COMMON_VAL_TRUE)) return true;
else if (token_equals(state, ARGUMENT_COMMON_VAL_TOGGLE)) return !previous_state;
@ -131,14 +39,6 @@ static void handle_domain_subscribe(FILE* rsp, struct token domain, char* messag
bar_item_parse_subscribe_message(bar_item, message);
}
// Syntax: sketchybar -m freeze <on/off>
static void handle_domain_freeze(FILE* rsp, struct token domain, char* message) {
struct token state = get_token(&message);
g_bar_manager.frozen = evaluate_boolean_state(state, g_bar_manager.frozen);
bar_manager_refresh(&g_bar_manager, true);
}
// Syntax: sketchybar -m trigger <event>
static void handle_domain_trigger(FILE* rsp, struct token domain, char* message) {
struct token event = get_token(&message);
@ -237,9 +137,6 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
fprintf(rsp, "Command: %s not found \n", command.text);
return;
}
struct token modifier = get_token(&message);
if (token_equals(modifier, ARGUMENT_COMMON_NO_SPACE))
bar_item->has_const_width = true;
bar_item_set_name(bar_item, token_to_string(name));
bar_manager_refresh(&g_bar_manager, true);
@ -249,15 +146,30 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
bool needs_update = false;
struct token property = get_token(&message);
if (token_equals(property, COMMAND_SET_ICON)) {
struct token subdom;
struct token entry;
get_key_value_pair(property.text, &subdom.text, &entry.text, '.');
if (subdom.text && entry.text) {
subdom.length = strlen(subdom.text);
entry.length = strlen(entry.text);
if (token_equals(subdom, SUB_DOMAIN_ICON))
needs_update = text_parse_sub_domain(&bar_item->icon, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_LABEL))
needs_update = text_parse_sub_domain(&bar_item->label, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_BACKGROUND))
needs_update = background_parse_sub_domain(&bar_item->background, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_GRAPH))
needs_update = graph_parse_sub_domain(&bar_item->graph, rsp, entry, message);
else {
fprintf(rsp, "Invalid subdomain: %s \n", subdom.text);
printf("Invalid subdomain: %s \n", subdom.text);
}
}
else if (token_equals(property, PROPERTY_ICON)) {
needs_update = text_set_string(&bar_item->icon, token_to_string(get_token(&message)), false);
} else if (token_equals(property, COMMAND_SET_LABEL)) {
} else if (token_equals(property, PROPERTY_LABEL)) {
needs_update = text_set_string(&bar_item->label, token_to_string(get_token(&message)), false);
} else if (token_equals(property, COMMAND_SET_LABEL_COLOR)) {
needs_update = text_set_color(&bar_item->label, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_ICON_COLOR)) {
needs_update = text_set_color(&bar_item->icon, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_SCRIPTING) || token_equals(property, COMMAND_SET_UPDATES)) {
} else if (token_equals(property, PROPERTY_UPDATES)) {
struct token token = get_token(&message);
if (token_equals(token, ARGUMENT_UPDATES_WHEN_SHOWN)) {
bar_item->updates = true;
@ -267,23 +179,9 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
bar_item->updates = evaluate_boolean_state(token, bar_item->updates);
bar_item->updates_only_when_shown = false;
}
} else if (token_equals(property, COMMAND_SET_DRAWING)) {
} else if (token_equals(property, PROPERTY_DRAWING)) {
bar_item_set_drawing(bar_item, evaluate_boolean_state(get_token(&message), bar_item->drawing));
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT)) {
bar_item->label.highlight = evaluate_boolean_state(get_token(&message), bar_item->label.highlight);
needs_update = text_update_color(&bar_item->label);
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT)) {
bar_item->icon.highlight = evaluate_boolean_state(get_token(&message), bar_item->icon.highlight);
needs_update = text_update_color(&bar_item->icon);
} else if (token_equals(property, COMMAND_SET_DRAWS_BACKGROUND)) {
needs_update = background_set_enabled(&bar_item->background, evaluate_boolean_state(get_token(&message), bar_item->background.enabled));
} else if (token_equals(property, COMMAND_SET_BACKGROUND_HEIGHT)) {
needs_update = background_set_height(&bar_item->background, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_BACKGROUND_CORNER_RADIUS)) {
needs_update = background_set_corner_radius(&bar_item->background, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_BACKGROUND_BORDER_WIDTH)) {
needs_update = background_set_border_width(&bar_item->background, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_WIDTH)) {
} else if (token_equals(property, PROPERTY_WIDTH)) {
struct token token = get_token(&message);
if (token_equals(token, "dynamic"))
bar_item->has_const_width = false;
@ -292,91 +190,39 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
bar_item->custom_width = token_to_uint32t(token);
}
needs_update = true;
} else if (token_equals(property, COMMAND_SET_ICON_FONT)) {
needs_update = text_set_font(&bar_item->icon, string_copy(message), false);
} else if (token_equals(property, COMMAND_SET_LABEL_FONT)) {
needs_update = text_set_font(&bar_item->label, string_copy(message), false);
} else if (token_equals(property, COMMAND_SET_SCRIPT)) {
} else if (token_equals(property, PROPERTY_SCRIPT)) {
bar_item_set_script(bar_item, string_copy(message));
} else if (token_equals(property, COMMAND_SET_CLICK_SCRIPT)) {
} else if (token_equals(property, PROPERTY_CLICK_SCRIPT)) {
bar_item_set_click_script(bar_item, string_copy(message));
} else if (token_equals(property, COMMAND_SET_UPDATE_FREQ)) {
} else if (token_equals(property, PROPERTY_UPDATE_FREQ)) {
bar_item->update_frequency = token_to_uint32t(get_token(&message));
} else if (token_equals(property, COMMAND_SET_GRAPH_COLOR)) {
bar_item->graph.line_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_GRAPH_FILL_COLOR)) {
bar_item->graph.fill_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
bar_item->graph.overrides_fill_color = true;
needs_update = true;
} else if (token_equals(property, COMMAND_SET_GRAPH_LINE_WIDTH)) {
bar_item->graph.line_width = token_to_float(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_BACKGROUND_COLOR)) {
needs_update = background_set_color(&bar_item->background, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_BACKGROUND_BORDER_COLOR)) {
needs_update = background_set_border_color(&bar_item->background, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT_COLOR)) {
bar_item->icon.highlight_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
needs_update = text_update_color(&bar_item->icon);
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT_COLOR)) {
bar_item->label.highlight_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
needs_update = text_update_color(&bar_item->label);
} else if (token_equals(property, COMMAND_SET_POSITION)) {
} else if (token_equals(property, PROPERTY_POSITION)) {
bar_item->position = get_token(&message).text[0];
} else if (token_equals(property, COMMAND_SET_ASSOCIATED_SPACE)) {
} else if (token_equals(property, PROPERTY_ASSOCIATED_SPACE)) {
struct token token = get_token(&message);
for (int i = 0; i < token.length; i++) {
int sep = -1;
if (token.text[i] == ',') token.text[i] = '\0', sep = i;
bar_item_append_associated_space(bar_item, 1 << strtoul(&token.text[sep + 1], NULL, 0));
}
} else if (token_equals(property, COMMAND_SET_ASSOCIATED_DISPLAY)) {
} else if (token_equals(property, PROPERTY_ASSOCIATED_DISPLAY)) {
struct token token = get_token(&message);
for (int i = 0; i < token.length; i++) {
int sep = -1;
if (token.text[i] == ',') token.text[i] = '\0', sep = i;
bar_item_append_associated_display(bar_item, 1 << strtoul(&token.text[sep + 1], NULL, 0));
}
} else if (token_equals(property, COMMAND_SET_ICON_PADDING_LEFT)) {
bar_item->icon.padding_left = token_to_int(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_ICON_PADDING_RIGHT)) {
bar_item->icon.padding_right = token_to_int(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_LEFT)) {
bar_item->label.padding_left = token_to_int(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
bar_item->label.padding_right = token_to_int(get_token(&message));
needs_update = true;
} else if (token_equals(property, COMMAND_SET_BACKGROUND_PADDING_LEFT)) {
needs_update = background_set_padding_left(&bar_item->background, token_to_int(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_BACKGROUND_PADDING_RIGHT)) {
needs_update = background_set_padding_right(&bar_item->background, token_to_int(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_YOFFSET)) {
} else if (token_equals(property, PROPERTY_YOFFSET)) {
bar_item_set_yoffset(bar_item, token_to_int(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_CACHE_SCRIPTS)) {
} else if (token_equals(property, PROPERTY_CACHE_SCRIPTS)) {
bar_item->cache_scripts = evaluate_boolean_state(get_token(&message), bar_item->cache_scripts);
} else if (token_equals(property, COMMAND_SET_LAZY)) {
} else if (token_equals(property, PROPERTY_LAZY)) {
bar_item->lazy = evaluate_boolean_state(get_token(&message), bar_item->lazy);
} else if (token_equals(property, COMMAND_DEFAULT_RESET)) {
bar_item_init(&g_bar_manager.default_item, NULL);
}
// DEPRECATED
else if (token_equals(property, COMMAND_SET_ENABLED)) {
printf("Command: enabled soon to be deprecated: Use drawing and scripting commands \n");
fprintf(rsp, "Command: enabled soon to be deprecated: Use drawing and scripting commands \n");
bar_item->drawing = evaluate_boolean_state(get_token(&message), bar_item->drawing);
bar_item->updates = evaluate_boolean_state(get_token(&message), bar_item->updates);
} else if (token_equals(property, COMMAND_SET_HIDDEN)) {
printf("Command: hidden soon to be deprecated: Use drawing command \n");
fprintf(rsp, "Command: hidden soon to be deprecated: Use drawing command \n");
bar_item->drawing = evaluate_boolean_state(get_token(&message), bar_item->drawing);
} else {
fprintf(rsp, "unknown command '%s' for domain 'set'\n", property.text);
printf("unknown command '%s' for domain 'set'\n", property.text);
fprintf(rsp, "Invalid propery: %s \n", property.text);
printf("Invalid propery: %s \n", property.text);
}
if (needs_update) bar_item_needs_update(bar_item);
@ -406,37 +252,22 @@ static void handle_domain_bar(FILE *rsp, struct token domain, char *message) {
struct token command = get_token(&message);
bool needs_refresh = false;
if (token_equals(command, COMMAND_BAR_BACKGROUND)) {
needs_refresh = background_set_color(&g_bar_manager.background, token_to_uint32t(get_token(&message)));
} else if (token_equals(command, COMMAND_BAR_HEIGHT)) {
needs_refresh = background_set_height(&g_bar_manager.background, atoi(get_token(&message).text));
if (needs_refresh) bar_manager_resize(&g_bar_manager);
} else if (token_equals(command, COMMAND_BAR_BORDER_WIDTH)) {
needs_refresh = background_set_border_width(&g_bar_manager.background, atoi(get_token(&message).text));
} else if (token_equals(command, COMMAND_BAR_BORDER_COLOR)) {
needs_refresh = background_set_border_color(&g_bar_manager.background, token_to_uint32t(get_token(&message)));
} else if (token_equals(command, COMMAND_BAR_MARGIN)) {
if (token_equals(command, PROPERTY_MARGIN)) {
struct token token = get_token(&message);
g_bar_manager.margin = token_to_uint32t(token);
needs_refresh = true;
} else if (token_equals(command, COMMAND_BAR_YOFFSET)) {
} else if (token_equals(command, PROPERTY_YOFFSET)) {
struct token token = get_token(&message);
g_bar_manager.y_offset = token_to_uint32t(token);
needs_refresh = true;
if (needs_refresh) bar_manager_resize(&g_bar_manager);
} else if (token_equals(command, COMMAND_BAR_CORNER_RADIUS)) {
needs_refresh = background_set_corner_radius(&g_bar_manager.background, token_to_uint32t(get_token(&message)));
} else if (token_equals(command, COMMAND_BAR_BLUR_RADIUS)) {
} else if (token_equals(command, PROPERTY_BLUR_RADIUS)) {
struct token token = get_token(&message);
bar_manager_set_background_blur(&g_bar_manager, token_to_uint32t(token));
} else if (token_equals(command, COMMAND_BAR_PADDING_LEFT)) {
needs_refresh = background_set_padding_left(&g_bar_manager.background, atoi(get_token(&message).text));
} else if (token_equals(command, COMMAND_BAR_PADDING_RIGHT)) {
needs_refresh = background_set_padding_right(&g_bar_manager.background, atoi(get_token(&message).text));
} else if (token_equals(command, COMMAND_BAR_FONT_SMOOTHING)) {
} else if (token_equals(command, PROPERTY_FONT_SMOOTHING)) {
struct token state = get_token(&message);
bar_manager_set_font_smoothing(&g_bar_manager, evaluate_boolean_state(state, g_bar_manager.font_smoothing));
} else if (token_equals(command, COMMAND_BAR_HIDDEN)) {
} else if (token_equals(command, PROPERTY_HIDDEN)) {
struct token state = get_token(&message);
struct token select = get_token(&message);
uint32_t adid = 0;
@ -447,10 +278,10 @@ static void handle_domain_bar(FILE *rsp, struct token domain, char *message) {
else
printf("No bar on display %u \n", adid);
} else bar_manager_set_hidden(&g_bar_manager, adid, evaluate_boolean_state(state, g_bar_manager.any_bar_hidden));
} else if (token_equals(command, COMMAND_BAR_TOPMOST)) {
} else if (token_equals(command, PROPERTY_TOPMOST)) {
struct token token = get_token(&message);
bar_manager_set_topmost(&g_bar_manager, evaluate_boolean_state(token, g_bar_manager.topmost));
} else if (token_equals(command, COMMAND_BAR_DISPLAY)) {
} else if (token_equals(command, PROPERTY_DISPLAY)) {
int length = strlen(message);
if (length <= 0) {
fprintf(rsp, "%s\n", g_bar_manager.display);
@ -461,7 +292,7 @@ static void handle_domain_bar(FILE *rsp, struct token domain, char *message) {
printf("value for '%.*s' must be either 'main' or 'all'.\n", command.length, command.text);
fprintf(rsp, "value for '%.*s' must be either 'main' or 'all'.\n", command.length, command.text);
}
} else if (token_equals(command, COMMAND_BAR_POSITION)) {
} else if (token_equals(command, PROPERTY_POSITION)) {
if (strlen(message) <= 0) {
fprintf(rsp, "%s\n", g_bar_manager.position);
printf("%s\n", g_bar_manager.position);
@ -472,12 +303,13 @@ static void handle_domain_bar(FILE *rsp, struct token domain, char *message) {
bar_manager_set_position(&g_bar_manager, string_copy(message));
}
}
else {
fprintf(rsp, "unknown command '%s' for domain 'bar'\n", command.text);
printf("unknown command '%s' for domain 'bar'\n", command.text);
}
else
needs_refresh = background_parse_sub_domain(&g_bar_manager.background, rsp, command, message);
if (needs_refresh) bar_manager_refresh(&g_bar_manager, true);
if (needs_refresh) {
bar_manager_resize(&g_bar_manager);
bar_manager_refresh(&g_bar_manager, true);
}
}
static char* reformat_batch_key_value_pair(struct token token) {
@ -539,7 +371,7 @@ static void handle_domain_batch(FILE* rsp, struct token domain, char* message) {
if (message && message[0] == '-') break;
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_BAR) || token_equals(command, COMMAND_BATCH_CONFIG)) {
} else if (token_equals(command, COMMAND_BATCH_BAR)) {
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* rbr_msg = reformat_batch_key_value_pair(token);
@ -597,7 +429,7 @@ void handle_message(int sockfd, char* message) {
handle_domain_batch(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_PUSH)) {
handle_domain_push(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_BAR) || token_equals(domain, DOMAIN_CONFIG)) {
} else if (token_equals(domain, DOMAIN_BAR)) {
handle_domain_bar(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_ADD)){
handle_domain_add(rsp, domain, message);
@ -613,8 +445,6 @@ void handle_message(int sockfd, char* message) {
handle_domain_default(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_TRIGGER)) {
handle_domain_trigger(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_FREEZE)) {
handle_domain_freeze(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_QUERY)) {
handle_domain_query(rsp, domain, message);
} else {

View file

@ -1,10 +1,7 @@
#ifndef MESSAGE_H
#define MESSAGE_H
// TODO: Next major version: Make use of dot operators for commands and remove deprecated commands
#define DOMAIN_BATCH "batch"
#define COMMAND_BATCH_CONFIG "--config" // Add deprecation notice
#define COMMAND_BATCH_BAR "--bar"
#define COMMAND_BATCH_ADD "--add"
#define COMMAND_BATCH_SET "--set"
@ -14,91 +11,71 @@
#define DOMAIN_ADD "add"
#define COMMAND_ADD_ITEM "item"
#define COMMAND_ADD_COMPONENT "component"
#define COMMAND_ADD_PLUGIN "plugin"
#define COMMAND_ADD_EVENT "event"
#define DOMAIN_REMOVE "remove"
#define DOMAIN_UPDATE "update"
#define DOMAIN_FREEZE "freeze"
#define DOMAIN_PUSH "push"
#define DOMAIN_TRIGGER "trigger"
#define DOMAIN_CLEAR "clear"
#define DOMAIN_DEFAULT "default"
#define COMMAND_DEFAULT_RESET "reset"
#define DOMAIN_SET "set"
#define COMMAND_SET_ENABLED "enabled" // TODO: Add deprecation notice
#define COMMAND_SET_HIDDEN "hidden" // TOD0: Add deprecation notice
#define COMMAND_SET_DRAWING "drawing"
#define COMMAND_SET_SCRIPTING "scripting" // TODO: Add deprecation notice
#define COMMAND_SET_UPDATES "updates"
#define COMMAND_SET_POSITION "position"
#define COMMAND_SET_ASSOCIATED_DISPLAY "associated_display"
#define COMMAND_SET_ASSOCIATED_SPACE "associated_space"
#define COMMAND_SET_UPDATE_FREQ "update_freq"
#define COMMAND_SET_SCRIPT "script"
#define COMMAND_SET_CLICK_SCRIPT "click_script"
#define COMMAND_SET_ICON_PADDING_LEFT "icon_padding_left"
#define COMMAND_SET_ICON_PADDING_RIGHT "icon_padding_right"
#define COMMAND_SET_LABEL_PADDING_LEFT "label_padding_left"
#define COMMAND_SET_LABEL_PADDING_RIGHT "label_padding_right"
#define COMMAND_SET_ICON "icon"
#define COMMAND_SET_ICON_FONT "icon_font"
#define COMMAND_SET_ICON_COLOR "icon_color"
#define COMMAND_SET_ICON_HIGHLIGHT_COLOR "icon_highlight_color"
#define COMMAND_SET_ICON_HIGHLIGHT "icon_highlight"
#define COMMAND_SET_DRAWS_BACKGROUND "draws_background"
#define COMMAND_SET_BACKGROUND_COLOR "background_color"
#define COMMAND_SET_BACKGROUND_BORDER_COLOR "background_border_color"
#define COMMAND_SET_BACKGROUND_HEIGHT "background_height"
#define COMMAND_SET_BACKGROUND_CORNER_RADIUS "background_corner_radius"
#define COMMAND_SET_BACKGROUND_BORDER_WIDTH "background_border_width"
#define COMMAND_SET_BACKGROUND_PADDING_LEFT "background_padding_left"
#define COMMAND_SET_BACKGROUND_PADDING_RIGHT "background_padding_right"
#define COMMAND_SET_YOFFSET "y_offset"
#define COMMAND_SET_WIDTH "width"
#define COMMAND_SET_GRAPH_COLOR "graph_color"
#define COMMAND_SET_GRAPH_FILL_COLOR "graph_fill_color"
#define COMMAND_SET_GRAPH_LINE_WIDTH "graph_line_width"
#define COMMAND_SET_LABEL "label"
#define COMMAND_SET_LABEL_COLOR "label_color"
#define COMMAND_SET_LABEL_FONT "label_font"
#define COMMAND_SET_LABEL_HIGHLIGHT_COLOR "label_highlight_color"
#define COMMAND_SET_LABEL_HIGHLIGHT "label_highlight"
#define COMMAND_SET_CACHE_SCRIPTS "cache_scripts"
#define COMMAND_SET_LAZY "lazy"
#define SUB_DOMAIN_ICON "icon"
#define SUB_DOMAIN_LABEL "label"
#define SUB_DOMAIN_BACKGROUND "background"
#define SUB_DOMAIN_GRAPH "graph"
#define PROPERTY_FONT "font"
#define PROPERTY_COLOR "color"
#define PROPERTY_HIGHLIGHT "highlight"
#define PROPERTY_HIGHLIGHT_COLOR "highlight_color"
#define PROPERTY_PADDING_LEFT "padding_left"
#define PROPERTY_PADDING_RIGHT "padding_right"
#define PROPERTY_HEIGHT "height"
#define PROPERTY_BORDER_COLOR "border_color"
#define PROPERTY_BORDER_WIDTH "border_width"
#define PROPERTY_CORNER_RADIUS "corner_radius"
#define PROPERTY_FILL_COLOR "fill_color"
#define PROPERTY_LINE_WIDTH "line_width"
#define PROPERTY_BLUR_RADIUS "blur_radius"
#define PROPERTY_DRAWING "drawing"
#define PROPERTY_UPDATES "updates"
#define PROPERTY_POSITION "position"
#define PROPERTY_ASSOCIATED_DISPLAY "associated_display"
#define PROPERTY_ASSOCIATED_SPACE "associated_space"
#define PROPERTY_UPDATE_FREQ "update_freq"
#define PROPERTY_SCRIPT "script"
#define PROPERTY_CLICK_SCRIPT "click_script"
#define PROPERTY_ICON "icon"
#define PROPERTY_YOFFSET "y_offset"
#define PROPERTY_WIDTH "width"
#define PROPERTY_LABEL "label"
#define PROPERTY_CACHE_SCRIPTS "cache_scripts"
#define PROPERTY_LAZY "lazy"
#define DOMAIN_BAR "bar"
#define PROPERTY_POSITION "position"
#define PROPERTY_MARGIN "margin"
#define PROPERTY_DISPLAY "display"
#define PROPERTY_TOPMOST "topmost"
#define PROPERTY_HIDDEN "hidden"
#define PROPERTY_FONT_SMOOTHING "font_smoothing"
#define DOMAIN_SUBSCRIBE "subscribe"
#define COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED "front_app_switched"
#define COMMAND_SUBSCRIBE_SPACE_CHANGE "space_change"
#define COMMAND_SUBSCRIBE_DISPLAY_CHANGE "display_change"
#define COMMAND_SUBSCRIBE_SYSTEM_WOKE "system_woke"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED "mouse_entered"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED "mouse_exited"
#define COMMAND_SUBSCRIBE_MOUSE_CLICKED "mouse_clicked"
#define DOMAIN_CONFIG "config" // Add deprecation notice
#define DOMAIN_BAR "bar"
#define COMMAND_DEBUG_OUTPUT "debug_output"
#define COMMAND_BAR_BACKGROUND "bar_color"
#define COMMAND_BAR_BORDER_COLOR "border_color"
#define COMMAND_BAR_BORDER_WIDTH "border_width"
#define COMMAND_BAR_POSITION "position"
#define COMMAND_BAR_HEIGHT "height"
#define COMMAND_BAR_YOFFSET "y_offset"
#define COMMAND_BAR_MARGIN "margin"
#define COMMAND_BAR_CORNER_RADIUS "corner_radius"
#define COMMAND_BAR_BLUR_RADIUS "blur_radius"
#define COMMAND_BAR_PADDING_LEFT "padding_left"
#define COMMAND_BAR_PADDING_RIGHT "padding_right"
#define COMMAND_BAR_DISPLAY "display"
#define COMMAND_BAR_TOPMOST "topmost"
#define COMMAND_BAR_HIDDEN "hidden"
#define COMMAND_BAR_FONT_SMOOTHING "font_smoothing"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED "mouse.entered"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED "mouse.exited"
#define COMMAND_SUBSCRIBE_MOUSE_CLICKED "mouse.clicked"
#define DOMAIN_QUERY "query"
#define COMMAND_QUERY_DEFAULT_ITEMS "default_menu_items"
@ -113,7 +90,6 @@
#define ARGUMENT_COMMON_VAL_FALSE "false"
#define ARGUMENT_COMMON_VAL_NO "no"
#define ARGUMENT_COMMON_VAL_TOGGLE "toggle"
#define ARGUMENT_COMMON_NO_SPACE "nospace" // TODO: Deprecate this
#define ARGUMENT_UPDATES_WHEN_SHOWN "when_shown"
@ -123,12 +99,6 @@
#define BAR_POSITION_BOTTOM "bottom"
#define BAR_POSITION_TOP "top"
struct token {
char *text;
unsigned int length;
};
static SOCKET_DAEMON_HANDLER(message_handler);
void handle_message(int sockfd, char *message);

View file

@ -55,6 +55,102 @@ static inline bool string_equals(const char *a, const char *b) {
return a && b && strcmp(a, b) == 0;
}
struct token {
char *text;
unsigned int length;
};
static bool token_equals(struct token token, char *match) {
char *at = match;
for (int i = 0; i < token.length; ++i, ++at) {
if ((*at == 0) || (token.text[i] != *at)) {
return false;
}
}
return *at == 0;
}
static char *token_to_string(struct token token) {
char *result = malloc(token.length + 1);
if (!result) return NULL;
memcpy(result, token.text, token.length);
result[token.length] = '\0';
return result;
}
static uint32_t token_to_uint32t(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return strtoul(buffer, NULL, 0);
}
static int token_to_int(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return (int) strtol(buffer, NULL, 0);
}
static float token_to_float(struct token token) {
char buffer[token.length + 1];
memcpy(buffer, token.text, token.length);
buffer[token.length] = '\0';
return strtof(buffer, NULL);
}
static struct token get_token(char **message) {
struct token token;
token.text = *message;
while (**message) {
++(*message);
}
token.length = *message - token.text;
if ((*message)[0] == '\0' && (*message)[1] != '\0') {
++(*message);
} else {
// NOTE(koekeishiya): don't go past the null-terminator
}
return token;
}
static void get_key_value_pair(char *token, char **key, char **value, char split) {
*key = token;
while (*token) {
if (token[0] == split) break;
++token;
}
if (*token != split) {
*key = NULL;
*value = NULL;
} else if (token[1]) {
*token = '\0';
*value = token + 1;
} else {
*token = '\0';
*value = NULL;
}
}
static void pack_key_value_pair(char* cursor, char* key, char* value) {
uint32_t key_len = strlen(key);
uint32_t val_len = value ? strlen(value) : 0;
memcpy(cursor, key, key_len);
cursor += key_len;
*cursor++ = '\0';
memcpy(cursor, value, val_len);
cursor += val_len;
*cursor++ = '\0';
*cursor++ = '\0';
}
static inline uint32_t get_set_bit_position(uint32_t mask) {
if (mask == 0) return UINT32_MAX;
uint32_t pos = 0;

View file

@ -122,3 +122,30 @@ void text_destroy(struct text* text) {
text_destroy_line(text);
text_clear_pointers(text);
}
static bool text_parse_sub_domain(struct text* text, FILE* rsp, struct token property, char* message) {
if (token_equals(property, PROPERTY_COLOR))
return text_set_color(text, token_to_uint32t(get_token(&message)));
else if (token_equals(property, PROPERTY_HIGHLIGHT)) {
text->highlight = evaluate_boolean_state(get_token(&message), text->highlight);
return text_update_color(text);
}
else if (token_equals(property, PROPERTY_FONT))
return text_set_font(text, string_copy(message), false);
else if (token_equals(property, PROPERTY_HIGHLIGHT_COLOR)) {
text->highlight_color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
return text_update_color(text);
}
else if (token_equals(property, PROPERTY_PADDING_LEFT)) {
text->padding_left = token_to_int(get_token(&message));
return true;
} else if (token_equals(property, PROPERTY_PADDING_RIGHT)) {
text->padding_right = token_to_int(get_token(&message));
return true;
}
else {
fprintf(rsp, "Unknown property: %s \n", property.text);
printf("Unknown property: %s \n", property.text);
}
return false;
}

View file

@ -32,4 +32,6 @@ uint32_t text_get_length(struct text* text);
uint32_t text_get_height(struct text* text);
bool text_update_color(struct text* text);
static bool text_parse_sub_domain(struct text* text, FILE* rsp, struct token property, char* message);
#endif // !TEXT_H_