add system event subscribe syntax

This commit is contained in:
FelixKratz 2021-08-14 21:32:25 +02:00
parent e82fd9f443
commit 8b07957c4c
7 changed files with 102 additions and 1 deletions

View file

@ -162,6 +162,18 @@ sketchybar -m set topproc icon_padding_left 10
sketchybar -m set topproc label_padding_right 5
sketchybar -m set topproc script "/Users/felix/.config/sketchybar/plugins/topproc.sh"
sketchybar -m set topproc update_freq 15
# This is how to subscribe an item/component to a certain system event
# It is possible to subscribe to more than one event
#sketchybar -m add item random center
#sketchybar -m set random script "/Users/felix/.config/sketchybar/plugins/random.sh"
#sketchybar -m subscribe random front_app_switched
#sketchybar -m subscribe random title_change
#sketchybar -m subscribe random space_change
#sketchybar -m subscribe random window_focus
#sketchybar -m subscribe random display_change
#sketchybar -m subscribe random system_woke
sketchybar -m update
echo "sketchybar configuration loaded.."

View file

@ -11,7 +11,7 @@ void bar_item_init(struct bar_item* bar_item) {
bar_item->counter = 0;
bar_item->name = "";
bar_item->type = BAR_ITEM;
bar_item->update_frequency = 10;
bar_item->update_frequency = 1000;
bar_item->script = "";
bar_item->position = BAR_POSITION_RIGHT;
bar_item->associated_display = 0;

View file

@ -5,6 +5,13 @@
#define BAR_COMPONENT 'c'
#define BAR_PLUGIN 'p'
#define UPDATE_FRONT_APP_SWITCHED 1
#define UPDATE_WINDOW_FOCUS 1 << 1
#define UPDATE_SPACE_CHANGE 1 << 2
#define UPDATE_DISPLAY_CHANGE 1 << 3
#define UPDATE_SYSTEM_WOKE 1 << 4
#define UPDATE_TITLE_CHANGE 1 << 5
struct bar_item {
bool nospace;
int counter;
@ -44,6 +51,9 @@ struct bar_item {
// Graph Data
bool has_graph;
struct graph_data graph_data;
// Update Events
uint32_t update_mask;
};
struct bar_item* bar_item_create();

View file

@ -123,3 +123,36 @@ void bar_manager_begin(struct bar_manager *bar_manager)
}
}
}
void bar_manager_check_bar_items_for_update_pattern(struct bar_manager* bar_manager, uint32_t pattern) {
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
if (bar_item->update_mask & pattern)
bar_item_script_update(bar_item, true);
}
}
void bar_manager_handle_front_app_switch(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_FRONT_APP_SWITCHED);
}
void bar_manager_handle_window_focus(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_WINDOW_FOCUS);
}
void bar_manager_handle_title_change(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_TITLE_CHANGE);
}
void bar_manager_handle_space_change(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_SPACE_CHANGE);
}
void bar_manager_handle_display_change(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_DISPLAY_CHANGE);
}
void bar_manager_handle_system_woke(struct bar_manager* bar_manager) {
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_SYSTEM_WOKE);
}

View file

@ -35,4 +35,10 @@ void bar_manager_resize(struct bar_manager *bar_manager);
void bar_manager_begin(struct bar_manager *bar_manager);
void bar_manager_init(struct bar_manager *bar_manager);
void bar_manager_handle_front_app_switch(struct bar_manager* bar_manager);
void bar_manager_handle_window_focus(struct bar_manager* bar_manager);
void bar_manager_handle_title_change(struct bar_manager* bar_manager);
void bar_manager_handle_space_change(struct bar_manager* bar_manager);
void bar_manager_handle_display_change(struct bar_manager* bar_manager);
void bar_manager_handle_system_woke(struct bar_manager* bar_manager);
#endif

View file

@ -115,6 +115,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_APPLICATION_TERMINATED)
static EVENT_CALLBACK(EVENT_HANDLER_APPLICATION_FRONT_SWITCHED)
{
debug("%s\n", __FUNCTION__);
bar_manager_handle_front_app_switch(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
@ -123,6 +124,8 @@ static EVENT_CALLBACK(EVENT_HANDLER_APPLICATION_FRONT_SWITCHED)
static EVENT_CALLBACK(EVENT_HANDLER_WINDOW_FOCUSED)
{
debug("%s\n", __FUNCTION__);
bar_manager_handle_window_focus(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
@ -133,6 +136,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_WINDOW_TITLE_CHANGED)
debug("%s\n", __FUNCTION__);
// TODO: we can optimize by checking if it the focused window
bar_manager_handle_title_change(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
@ -142,6 +146,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_SPACE_CHANGED)
{
debug("%s\n", __FUNCTION__);
bar_manager_handle_space_change(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
@ -154,6 +159,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_DISPLAY_CHANGED)
debug("%s: %d\n", __FUNCTION__, g_display_manager.current_display_id);
bar_manager_handle_display_change(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
@ -201,6 +207,7 @@ static EVENT_CALLBACK(EVENT_HANDLER_MENU_BAR_HIDDEN_CHANGED)
static EVENT_CALLBACK(EVENT_HANDLER_SYSTEM_WOKE)
{
debug("%s:\n", __FUNCTION__);
bar_manager_handle_system_woke(&g_bar_manager);
bar_manager_refresh(&g_bar_manager);
return EVENT_SUCCESS;
}

View file

@ -40,6 +40,14 @@ extern bool g_verbose;
#define COMMAND_SET_LABEL_COLOR "label_color"
#define COMMAND_SET_LABEL_FONT "label_font"
#define DOMAIN_SUBSCRIBE "subscribe"
#define COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED "front_app_switched"
#define COMMAND_SUBSCRIBE_WINDOW_FOCUS "window_focus"
#define COMMAND_SUBSCRIBE_SPACE_CHANGE "space_change"
#define COMMAND_SUBSCRIBE_DISPLAY_CHANGE "display_change"
#define COMMAND_SUBSCRIBE_TITLE_CHANGE "title_change"
#define COMMAND_SUBSCRIBE_SYSTEM_WOKE "system_woke"
#define DOMAIN_CONFIG "config"
#define COMMAND_CONFIG_DEBUG_OUTPUT "debug_output"
#define COMMAND_CONFIG_BAR_BACKGROUND "bar_color"
@ -146,6 +154,29 @@ static void daemon_fail(FILE *rsp, char *fmt, ...)
view_flush(view); \
}
// Syntax: sketchybar -m subscribe <name> <event>
static void handle_domain_subscribe(FILE* rsp, struct token domain, char* message) {
struct token name = get_token(&message);
struct token event = get_token(&message);
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, token_to_string(name));
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
if (token_equals(event, COMMAND_SUBSCRIBE_SYSTEM_WOKE)) {
bar_item->update_mask |= UPDATE_SYSTEM_WOKE;
} else if (token_equals(event, COMMAND_SUBSCRIBE_SPACE_CHANGE)) {
bar_item->update_mask |= UPDATE_SPACE_CHANGE;
} else if (token_equals(event, COMMAND_SUBSCRIBE_TITLE_CHANGE)) {
bar_item->update_mask |= UPDATE_TITLE_CHANGE;
} else if (token_equals(event, COMMAND_SUBSCRIBE_DISPLAY_CHANGE)) {
bar_item->update_mask |= UPDATE_DISPLAY_CHANGE;
} else if (token_equals(event, COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED)) {
bar_item->update_mask |= UPDATE_FRONT_APP_SWITCHED;
} else if (token_equals(event, COMMAND_SUBSCRIBE_WINDOW_FOCUS)) {
bar_item->update_mask |= UPDATE_WINDOW_FOCUS;
}
}
// Syntax: sketchybar -m clear <name>
static void handle_domain_clear(FILE* rsp, struct token domain, char* message) {
}
@ -373,6 +404,8 @@ void handle_message(FILE *rsp, char *message)
handle_domain_push(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_CLEAR)) {
handle_domain_clear(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_SUBSCRIBE)) {
handle_domain_subscribe(rsp, domain, message);
} else {
daemon_fail(rsp, "unknown domain '%.*s'\n", domain.length, domain.text);
}