mirror of
https://github.com/FelixKratz/SketchyBar
synced 2025-02-17 04:58:28 +00:00
added click actions and some tweaking to the graph logic
This commit is contained in:
parent
b9faebcec8
commit
646df4b6c8
20 changed files with 313 additions and 59 deletions
|
@ -5,8 +5,8 @@ CPU_INFO=$(ps -eo pcpu,user)
|
|||
CPU_SYS=$(echo "$CPU_INFO" | grep -v $(whoami) | sed "s/[^ 0-9\.]//g" | awk "{sum+=\$1} END {print sum/(100.0 * $CORE_COUNT)}")
|
||||
CPU_USER=$(echo "$CPU_INFO" | grep $(whoami) | sed "s/[^ 0-9\.]//g" | awk "{sum+=\$1} END {print sum/(100.0 * $CORE_COUNT)}")
|
||||
|
||||
sketchybar -m push cpu_sys 0 $CPU_SYS
|
||||
sketchybar -m push cpu_user 0 $CPU_USER
|
||||
sketchybar -m push cpu_sys $CPU_SYS
|
||||
sketchybar -m push cpu_user $CPU_USER
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -2,5 +2,5 @@
|
|||
MAX_DOWN=110
|
||||
MAX_UP=45
|
||||
UPDOWN=$(ifstat -i "en0" -b 0.1 1 | tail -n1)
|
||||
sketchybar -m push network_down 0 $(echo $UPDOWN | awk "{ print \$1 / (1000.0 * $MAX_DOWN) }")
|
||||
sketchybar -m push network_up 0 $(echo $UPDOWN | awk "{ print \$2 / (1000.0 * $MAX_UP) }")
|
||||
sketchybar -m push network_down $(echo $UPDOWN | awk "{ print \$1 / (1000.0 * $MAX_DOWN) }")
|
||||
sketchybar -m push network_up $(echo $UPDOWN | awk "{ print \$2 / (1000.0 * $MAX_UP) }")
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
sketchybar -m set random label $RANDOM
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ sketchybar -m set code icon_color 0xffffffff
|
|||
sketchybar -m set code icon_highlight_color 0xff48aa2a
|
||||
sketchybar -m set code label_padding_left 4
|
||||
sketchybar -m set code label code
|
||||
sketchybar -m set code click_script "yabai -m space --focus 1"
|
||||
|
||||
sketchybar -m add item spaceSep left
|
||||
sketchybar -m set spaceSep associated_display 1
|
||||
|
@ -32,6 +33,7 @@ sketchybar -m set writing icon_highlight_color 0xfffab402
|
|||
sketchybar -m set writing icon #
|
||||
sketchybar -m set writing label_padding_left 4
|
||||
sketchybar -m set writing label tex
|
||||
sketchybar -m set writing click_script "yabai -m space --focus 2"
|
||||
|
||||
sketchybar -m add item spaceSep2 left
|
||||
sketchybar -m set spaceSep2 associated_display 1
|
||||
|
@ -48,6 +50,7 @@ sketchybar -m set reading icon_highlight_color 0xff7fe5f0
|
|||
sketchybar -m set reading icon
|
||||
sketchybar -m set reading label_padding_left 4
|
||||
sketchybar -m set reading label web
|
||||
sketchybar -m set reading click_script "yabai -m space --focus 3"
|
||||
|
||||
sketchybar -m add item spaceSep3 left
|
||||
sketchybar -m set spaceSep3 associated_display 1
|
||||
|
@ -64,6 +67,7 @@ sketchybar -m set entertainment icon_highlight_color 0xfffff68f
|
|||
sketchybar -m set entertainment icon #鈴
|
||||
sketchybar -m set entertainment label_padding_left 4
|
||||
sketchybar -m set entertainment label idle
|
||||
sketchybar -m set entertainment click_script "yabai -m space --focus 4"
|
||||
|
||||
sketchybar -m add component space zoom left
|
||||
sketchybar -m set zoom associated_display 2
|
||||
|
@ -91,6 +95,7 @@ sketchybar -m set vimIndicator icon ""
|
|||
sketchybar -m set vimIndicator icon_padding_right 4
|
||||
sketchybar -m set vimIndicator label "normal"
|
||||
sketchybar -m set vimIndicator label_padding_right 10
|
||||
sketchybar -m set vimIndicator click_script "gvim"
|
||||
|
||||
sketchybar -m add item clock right
|
||||
sketchybar -m set clock update_freq 10
|
||||
|
@ -109,6 +114,7 @@ sketchybar -m set mailIndicator icon_font "Hack Nerd Font:Bold:20.0"
|
|||
sketchybar -m set mailIndicator icon
|
||||
sketchybar -m set mailIndicator label_padding_left 4
|
||||
sketchybar -m set mailIndicator icon_padding_left 10
|
||||
sketchybar -m set mailIndicator click_script "osascript -e 'tell application \"Mail\" to activate'"
|
||||
|
||||
sketchybar -m add item githubIndicator right
|
||||
sketchybar -m set githubIndicator associated_space 1
|
||||
|
@ -118,6 +124,7 @@ sketchybar -m set githubIndicator icon_font "Hack Nerd Font:Bold:18.0"
|
|||
sketchybar -m set githubIndicator icon #
|
||||
sketchybar -m set githubIndicator label_padding_left 4
|
||||
sketchybar -m set githubIndicator icon_padding_left 10
|
||||
sketchybar -m set githubIndicator click_script "open https://github.com"
|
||||
|
||||
sketchybar -m add component graph cpu_user right 99 nospace
|
||||
sketchybar -m set cpu_user script "~/.config/sketchybar/plugins/cpu_graph.sh"
|
||||
|
@ -173,6 +180,8 @@ 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 label test
|
||||
#sketchybar -m set random click_script "~/.config/sketchybar/plugins/random.sh"
|
||||
#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
|
||||
|
|
49
src/bar.c
49
src/bar.c
|
@ -189,35 +189,6 @@ static char * focused_window_title()
|
|||
}
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
static int mission_control_index(uint64_t sid)
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int desktop_cnt = 1;
|
||||
|
||||
CFArrayRef display_spaces_ref = SLSCopyManagedDisplaySpaces(g_connection);
|
||||
int display_spaces_count = CFArrayGetCount(display_spaces_ref);
|
||||
|
||||
for (int i = 0; i < display_spaces_count; ++i) {
|
||||
CFDictionaryRef display_ref = CFArrayGetValueAtIndex(display_spaces_ref, i);
|
||||
CFArrayRef spaces_ref = CFDictionaryGetValue(display_ref, CFSTR("Spaces"));
|
||||
int spaces_count = CFArrayGetCount(spaces_ref);
|
||||
|
||||
for (int j = 0; j < spaces_count; ++j) {
|
||||
CFDictionaryRef space_ref = CFArrayGetValueAtIndex(spaces_ref, j);
|
||||
CFNumberRef sid_ref = CFDictionaryGetValue(space_ref, CFSTR("id64"));
|
||||
CFNumberGetValue(sid_ref, CFNumberGetType(sid_ref), &result);
|
||||
if (sid == result) goto out;
|
||||
|
||||
++desktop_cnt;
|
||||
}
|
||||
}
|
||||
|
||||
desktop_cnt = 0;
|
||||
out:
|
||||
CFRelease(display_spaces_ref);
|
||||
return desktop_cnt;
|
||||
}
|
||||
|
||||
int bar_get_center_length(struct bar_manager* bar_manager) {
|
||||
int total_length = 0;
|
||||
for (int i = 0; i < bar_manager->bar_item_count; i++) {
|
||||
|
@ -259,20 +230,19 @@ void bar_refresh(struct bar *bar)
|
|||
|
||||
for (int i = 0; i < g_bar_manager.bar_item_count; i++) {
|
||||
struct bar_item* bar_item = g_bar_manager.bar_items[i];
|
||||
|
||||
if(bar_item->associated_display > 0 && !(bar_item->associated_display & (1 << did))) continue;
|
||||
if((strcmp(bar_item->identifier, BAR_COMPONENT_SPACE) != 0) && bar_item->associated_space > 0 && !(bar_item->associated_space & (1 << sid))) continue;
|
||||
|
||||
struct bar_line* label = &bar_item->label_line;
|
||||
struct bar_line* icon = &bar_item->icon_line;
|
||||
CGPoint icon_position = bar_align_line(bar, *icon, ALIGN_CENTER, ALIGN_CENTER);
|
||||
CGPoint label_position = bar_align_line(bar, *label, ALIGN_CENTER, ALIGN_CENTER);
|
||||
|
||||
|
||||
if(bar_item->associated_display > 0 && !(bar_item->associated_display & (1 << did))) continue;
|
||||
if((strcmp(bar_item->identifier, BAR_COMPONENT_SPACE) != 0) && bar_item->associated_space > 0 && !(bar_item->associated_space & (1 << sid))) continue;
|
||||
|
||||
if (bar_item->position == BAR_POSITION_LEFT) {
|
||||
icon_position.x = bar_left_final_item_x + bar_item->icon_spacing_left;
|
||||
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_spacing_right + bar_item->label_spacing_left;
|
||||
bar_draw_line(bar, *icon, icon_position.x, icon_position.y);
|
||||
bar_draw_line(bar, *label, label_position.x, label_position.y);
|
||||
|
||||
if (!bar_item->nospace)
|
||||
bar_left_final_item_x = label_position.x + label->bounds.size.width + bar_item->label_spacing_right;
|
||||
if (bar_draw_graphs(bar, bar_item, bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label_spacing_right : bar_left_final_item_x, false)) {
|
||||
|
@ -283,8 +253,6 @@ void bar_refresh(struct bar *bar)
|
|||
else if (bar_item->position == BAR_POSITION_RIGHT) {
|
||||
label_position.x = bar_right_first_item_x - label->bounds.size.width - bar_item->label_spacing_right;
|
||||
icon_position.x = label_position.x - icon->bounds.size.width - bar_item->icon_spacing_right - bar_item->label_spacing_left;
|
||||
bar_draw_line(bar, *icon, icon_position.x, icon_position.y);
|
||||
bar_draw_line(bar, *label, label_position.x, label_position.y);
|
||||
|
||||
if (!bar_item->nospace)
|
||||
bar_right_first_item_x = icon_position.x - bar_item->icon_spacing_left;
|
||||
|
@ -296,8 +264,6 @@ void bar_refresh(struct bar *bar)
|
|||
else if (bar_item->position == BAR_POSITION_CENTER) {
|
||||
icon_position.x = bar_center_first_item_x + bar_item->icon_spacing_left;
|
||||
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_spacing_right + bar_item->label_spacing_left;
|
||||
bar_draw_line(bar, *icon, icon_position.x, icon_position.y);
|
||||
bar_draw_line(bar, *label, label_position.x, label_position.y);
|
||||
|
||||
if (!bar_item->nospace)
|
||||
bar_center_first_item_x = label_position.x + label->bounds.size.width + bar_item->label_spacing_right;
|
||||
|
@ -306,6 +272,11 @@ void bar_refresh(struct bar *bar)
|
|||
bar_center_first_item_x += bar_item->graph_data.graph_width;
|
||||
}
|
||||
}
|
||||
bar_draw_line(bar, *icon, icon_position.x, icon_position.y);
|
||||
bar_draw_line(bar, *label, label_position.x, label_position.y);
|
||||
bar_item->label_line.bounds.origin = label_position;
|
||||
bar_item->icon_line.bounds.origin = icon_position;
|
||||
bar_item_set_bounding_rect_for_space(bar_item, sid);
|
||||
}
|
||||
|
||||
CGContextFlush(bar->context);
|
||||
|
|
|
@ -13,6 +13,7 @@ void bar_item_init(struct bar_item* bar_item) {
|
|||
bar_item->type = BAR_ITEM;
|
||||
bar_item->update_frequency = 1000;
|
||||
bar_item->script = "";
|
||||
bar_item->on_click_script = "~/.config/sketchybar/plugins/random.sh";
|
||||
bar_item->position = BAR_POSITION_RIGHT;
|
||||
bar_item->associated_display = 0;
|
||||
bar_item->associated_space = 0;
|
||||
|
@ -24,6 +25,8 @@ void bar_item_init(struct bar_item* bar_item) {
|
|||
bar_item->label_spacing_right = 0;
|
||||
bar_item->label_color = rgba_color_from_hex(0xffffffff);
|
||||
bar_item->has_graph = false;
|
||||
bar_item->num_rects = 0;
|
||||
bar_item->bounding_rects = NULL;
|
||||
|
||||
bar_item_set_icon(bar_item, string_copy(""), bar_item->icon_color);
|
||||
bar_item_set_icon_font(bar_item, string_copy("Hack Nerd Font:Bold:14.0"));
|
||||
|
@ -69,6 +72,13 @@ void bar_item_set_script(struct bar_item* bar_item, char* script) {
|
|||
bar_item->script = script;
|
||||
}
|
||||
|
||||
void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
|
||||
if (script != bar_item->on_click_script && !bar_item->on_click_script) {
|
||||
free(bar_item->on_click_script);
|
||||
}
|
||||
bar_item->on_click_script = script;
|
||||
}
|
||||
|
||||
void bar_item_set_icon(struct bar_item* bar_item, char* icon, struct rgba_color color) {
|
||||
if (bar_item->icon_line.line) {
|
||||
bar_destroy_line(bar_item->icon_line);
|
||||
|
@ -115,3 +125,33 @@ void bar_item_set_label_font(struct bar_item* bar_item, char *font_string) {
|
|||
|
||||
bar_item->label_font = bar_create_font(font_string);
|
||||
}
|
||||
|
||||
void bar_item_on_click(struct bar_item* bar_item) {
|
||||
printf("%s \n", bar_item->name);
|
||||
if (strcmp(bar_item->on_click_script, "") != 0)
|
||||
fork_exec(bar_item->on_click_script, NULL);
|
||||
}
|
||||
|
||||
CGRect bar_item_construct_bounding_rect(struct bar_item* bar_item) {
|
||||
CGRect bounding_rect;
|
||||
bounding_rect.origin = bar_item->icon_line.bounds.origin;
|
||||
bounding_rect.size.width = CGRectGetMaxX(bar_item->label_line.bounds) - CGRectGetMinX(bar_item->icon_line.bounds);
|
||||
bounding_rect.size.height = CGRectGetMaxY(bar_item->label_line.bounds) > CGRectGetMaxY(bar_item->icon_line.bounds) ? CGRectGetMaxY(bar_item->label_line.bounds) : CGRectGetMaxY(bar_item->icon_line.bounds);
|
||||
return bounding_rect;
|
||||
}
|
||||
|
||||
void bar_item_set_bounding_rect_for_space(struct bar_item* bar_item, uint32_t sid) {
|
||||
if (bar_item->num_rects < sid) {
|
||||
bar_item->bounding_rects = (CGRect**) realloc(bar_item->bounding_rects, sizeof(CGRect*) * sid);
|
||||
memset(bar_item->bounding_rects, 0, sizeof(CGRect*) * sid);
|
||||
bar_item->num_rects = sid;
|
||||
}
|
||||
if (!bar_item->bounding_rects[sid - 1]) {
|
||||
bar_item->bounding_rects[sid - 1] = malloc(sizeof(CGRect));
|
||||
memset(bar_item->bounding_rects[sid - 1], 0, sizeof(CGRect));
|
||||
}
|
||||
CGRect rect = bar_item_construct_bounding_rect(bar_item);
|
||||
|
||||
bar_item->bounding_rects[sid - 1]->origin = rect.origin;
|
||||
bar_item->bounding_rects[sid - 1]->size = rect.size;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ struct bar_item {
|
|||
uint32_t update_frequency;
|
||||
// Execute with exec_fork, callback from command via messages
|
||||
char* script;
|
||||
char* on_click_script;
|
||||
|
||||
// Name by which to refer to the bar_item in the configuration
|
||||
char* name;
|
||||
|
@ -55,6 +56,10 @@ struct bar_item {
|
|||
|
||||
// Update Events
|
||||
uint32_t update_mask;
|
||||
|
||||
// Bounding Boxes for click events
|
||||
uint32_t num_rects;
|
||||
CGRect** bounding_rects;
|
||||
};
|
||||
|
||||
struct bar_item* bar_item_create();
|
||||
|
@ -63,6 +68,7 @@ void bar_item_update_component(struct bar_item* bar_item, uint32_t did, uint32_t
|
|||
void bar_item_init(struct bar_item* bar_item);
|
||||
void bar_item_set_name(struct bar_item* bar_item, char* name);
|
||||
void bar_item_set_script(struct bar_item* bar_item, char* script);
|
||||
void bar_item_set_click_script(struct bar_item* bar_item, char* script);
|
||||
void bar_item_set_padding_left(struct bar_item* bar_item, uint32_t pad);
|
||||
void bar_item_set_padding_right(struct bar_item* bar_item, uint32_t pad);
|
||||
void bar_item_set_icon(struct bar_item* bar_item, char* icon, struct rgba_color color);
|
||||
|
@ -72,4 +78,9 @@ void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color);
|
|||
void bar_item_set_label_font(struct bar_item* bar_item, char *font_string);
|
||||
void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string);
|
||||
|
||||
void bar_item_on_click(struct bar_item* bar_item);
|
||||
|
||||
CGRect bar_item_construct_bounding_rect(struct bar_item* bar_item);
|
||||
void bar_item_set_bounding_rect_for_space(struct bar_item* bar_item, uint32_t sid);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -132,6 +132,17 @@ void bar_manager_check_bar_items_for_update_pattern(struct bar_manager* bar_mana
|
|||
}
|
||||
}
|
||||
|
||||
struct bar_item* bar_manager_get_item_by_point(struct bar_manager* bar_manager, CGPoint point, uint32_t sid) {
|
||||
for (int i = 0; i < bar_manager->bar_item_count; i++) {
|
||||
struct bar_item* bar_item = bar_manager->bar_items[i];
|
||||
if (bar_item->num_rects < sid || bar_item->bounding_rects[sid - 1] == NULL) continue;
|
||||
if (cgrect_contains_point(bar_item->bounding_rects[sid - 1], &point)) {
|
||||
return bar_item;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -41,4 +41,7 @@ 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);
|
||||
|
||||
struct bar_item* bar_manager_get_item_by_point(struct bar_manager* bar_manager, CGPoint point, uint32_t sid);
|
||||
|
||||
#endif
|
||||
|
|
27
src/event.c
27
src/event.c
|
@ -5,7 +5,7 @@ extern struct process_manager g_process_manager;
|
|||
extern struct display_manager g_display_manager;
|
||||
extern struct bar_manager g_bar_manager;
|
||||
extern struct application_manager g_application_manager;
|
||||
extern bool g_mission_control_active;
|
||||
extern struct mouse_state g_mouse_state;
|
||||
extern int g_connection;
|
||||
|
||||
enum event_type event_type_from_string(const char *str)
|
||||
|
@ -248,3 +248,28 @@ out:
|
|||
|
||||
return EVENT_SUCCESS;
|
||||
}
|
||||
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DOWN) {
|
||||
return EVENT_SUCCESS;
|
||||
}
|
||||
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP)
|
||||
{
|
||||
CGPoint point = CGEventGetLocation(context);
|
||||
printf("Mouse Up Event \n");
|
||||
uint32_t sid = mission_control_index(display_space_id(display_manager_active_display_id()));
|
||||
printf("sid: %i \n", sid);
|
||||
struct bar_item* bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, sid);
|
||||
if (bar_item) bar_item_on_click(bar_item);
|
||||
else { printf("No item found \n"); }
|
||||
|
||||
return EVENT_SUCCESS;
|
||||
}
|
||||
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED) {
|
||||
return EVENT_SUCCESS;
|
||||
}
|
||||
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_MOVED) {
|
||||
return EVENT_SUCCESS;
|
||||
}
|
||||
|
|
18
src/event.h
18
src/event.h
|
@ -20,6 +20,10 @@ static EVENT_CALLBACK(EVENT_HANDLER_SYSTEM_WOKE);
|
|||
static EVENT_CALLBACK(EVENT_HANDLER_BAR_REFRESH);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_SHELL_REFRESH);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_DAEMON_MESSAGE);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DOWN);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED);
|
||||
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_MOVED);
|
||||
|
||||
#define EVENT_QUEUED 0x0
|
||||
#define EVENT_PROCESSED 0x1
|
||||
|
@ -50,6 +54,11 @@ enum event_type
|
|||
BAR_REFRESH,
|
||||
SHELL_REFRESH,
|
||||
DAEMON_MESSAGE,
|
||||
MOUSE_DOWN,
|
||||
MOUSE_UP,
|
||||
MOUSE_DRAGGED,
|
||||
MOUSE_MOVED,
|
||||
|
||||
|
||||
EVENT_TYPE_COUNT
|
||||
};
|
||||
|
@ -74,6 +83,10 @@ static const char *event_type_str[] =
|
|||
[BAR_REFRESH] = "bar_refresh",
|
||||
[SHELL_REFRESH] = "shell_refresh",
|
||||
[DAEMON_MESSAGE] = "daemon_message",
|
||||
[MOUSE_DOWN] = "mouse_down",
|
||||
[MOUSE_UP] = "mouse_up",
|
||||
[MOUSE_DRAGGED] = "mouse_dragged",
|
||||
[MOUSE_MOVED] = "mouse_moved",
|
||||
|
||||
[EVENT_TYPE_COUNT] = "event_type_count"
|
||||
};
|
||||
|
@ -91,6 +104,11 @@ static event_callback *event_handler[] =
|
|||
[DISPLAY_MOVED] = EVENT_HANDLER_DISPLAY_MOVED,
|
||||
[DISPLAY_RESIZED] = EVENT_HANDLER_DISPLAY_RESIZED,
|
||||
[DISPLAY_CHANGED] = EVENT_HANDLER_DISPLAY_CHANGED,
|
||||
[MOUSE_DOWN] = EVENT_HANDLER_MOUSE_DOWN,
|
||||
[MOUSE_UP] = EVENT_HANDLER_MOUSE_UP,
|
||||
[MOUSE_DRAGGED] = EVENT_HANDLER_MOUSE_DRAGGED,
|
||||
[MOUSE_MOVED] = EVENT_HANDLER_MOUSE_MOVED,
|
||||
|
||||
[MENU_BAR_HIDDEN_CHANGED] = EVENT_HANDLER_MENU_BAR_HIDDEN_CHANGED,
|
||||
[SYSTEM_WOKE] = EVENT_HANDLER_SYSTEM_WOKE,
|
||||
[BAR_REFRESH] = EVENT_HANDLER_BAR_REFRESH,
|
||||
|
|
77
src/event_tap.c
Normal file
77
src/event_tap.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include "event_tap.h"
|
||||
|
||||
extern struct event_loop g_event_loop;
|
||||
extern struct mouse_state g_mouse_state;
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wswitch"
|
||||
static bool consume_mouse_click;
|
||||
static EVENT_TAP_CALLBACK(mouse_handler)
|
||||
{
|
||||
switch (type) {
|
||||
case kCGEventTapDisabledByTimeout:
|
||||
case kCGEventTapDisabledByUserInput: {
|
||||
struct event_tap *event_tap = (struct event_tap *) reference;
|
||||
CGEventTapEnable(event_tap->handle, 1);
|
||||
} break;
|
||||
case kCGEventLeftMouseDown:
|
||||
case kCGEventRightMouseDown: {
|
||||
struct event *event = event_create(&g_event_loop, MOUSE_DOWN , (void *) CFRetain(cgevent));
|
||||
event_loop_post(&g_event_loop, event);
|
||||
} break;
|
||||
case kCGEventLeftMouseUp:
|
||||
case kCGEventRightMouseUp: {
|
||||
struct event *event = event_create(&g_event_loop, MOUSE_UP, (void *) CFRetain(cgevent));
|
||||
event_loop_post(&g_event_loop, event);
|
||||
} break;
|
||||
case kCGEventLeftMouseDragged:
|
||||
case kCGEventRightMouseDragged: {
|
||||
struct event *event = event_create(&g_event_loop, MOUSE_DRAGGED, (void *) CFRetain(cgevent));
|
||||
event_loop_post(&g_event_loop, event);
|
||||
} break;
|
||||
case kCGEventMouseMoved: {
|
||||
struct event *event = event_create(&g_event_loop, MOUSE_MOVED, (void *) CFRetain(cgevent));
|
||||
event_loop_post(&g_event_loop, event);
|
||||
} break;
|
||||
}
|
||||
|
||||
return cgevent;
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
bool event_tap_enabled(struct event_tap *event_tap)
|
||||
{
|
||||
bool result = (event_tap->handle && CGEventTapIsEnabled(event_tap->handle));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool event_tap_begin(struct event_tap *event_tap, uint32_t mask, event_tap_callback *callback)
|
||||
{
|
||||
event_tap->mask = mask;
|
||||
event_tap->handle = CGEventTapCreate(kCGSessionEventTap,
|
||||
kCGHeadInsertEventTap,
|
||||
kCGEventTapOptionDefault,
|
||||
event_tap->mask,
|
||||
callback,
|
||||
event_tap);
|
||||
|
||||
bool result = event_tap_enabled(event_tap);
|
||||
if (result) {
|
||||
event_tap->runloop_source = CFMachPortCreateRunLoopSource(NULL, event_tap->handle, 0);
|
||||
CFRunLoopAddSource(CFRunLoopGetMain(), event_tap->runloop_source, kCFRunLoopCommonModes);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void event_tap_end(struct event_tap *event_tap)
|
||||
{
|
||||
if (event_tap_enabled(event_tap)) {
|
||||
CGEventTapEnable(event_tap->handle, false);
|
||||
CFMachPortInvalidate(event_tap->handle);
|
||||
CFRunLoopRemoveSource(CFRunLoopGetMain(), event_tap->runloop_source, kCFRunLoopCommonModes);
|
||||
CFRelease(event_tap->runloop_source);
|
||||
CFRelease(event_tap->handle);
|
||||
event_tap->handle = NULL;
|
||||
}
|
||||
}
|
36
src/event_tap.h
Normal file
36
src/event_tap.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#ifndef EVENT_TAP_H
|
||||
#define EVENT_TAP_H
|
||||
|
||||
#define EVENT_MASK_MOUSE (1 << kCGEventMouseMoved) | \
|
||||
(1 << kCGEventLeftMouseDown) | \
|
||||
(1 << kCGEventLeftMouseUp) | \
|
||||
(1 << kCGEventLeftMouseDragged) | \
|
||||
(1 << kCGEventRightMouseDown) | \
|
||||
(1 << kCGEventRightMouseUp) | \
|
||||
(1 << kCGEventRightMouseDragged)
|
||||
|
||||
#define EVENT_MASK_ALT kCGEventFlagMaskAlternate
|
||||
#define EVENT_MASK_SHIFT kCGEventFlagMaskShift
|
||||
#define EVENT_MASK_CMD kCGEventFlagMaskCommand
|
||||
#define EVENT_MASK_CTRL kCGEventFlagMaskControl
|
||||
#define EVENT_MASK_FN kCGEventFlagMaskSecondaryFn
|
||||
|
||||
struct event_tap
|
||||
{
|
||||
CFMachPortRef handle;
|
||||
CFRunLoopSourceRef runloop_source;
|
||||
CGEventMask mask;
|
||||
};
|
||||
|
||||
#define EVENT_TAP_CALLBACK(name) \
|
||||
CGEventRef name(CGEventTapProxy proxy, \
|
||||
CGEventType type, \
|
||||
CGEventRef cgevent, \
|
||||
void *reference)
|
||||
typedef EVENT_TAP_CALLBACK(event_tap_callback);
|
||||
|
||||
bool event_tap_enabled(struct event_tap *event_tap);
|
||||
bool event_tap_begin(struct event_tap *event_tap, uint32_t mask, event_tap_callback *callback);
|
||||
void event_tap_end(struct event_tap *event_tap);
|
||||
|
||||
#endif
|
|
@ -1,30 +1,26 @@
|
|||
#include "graph_data.h"
|
||||
void graph_data_init(struct graph_data* graph_data, uint32_t graph_width) {
|
||||
graph_data->graph_width = graph_width;
|
||||
graph_data->x = malloc(sizeof(float) * graph_width);
|
||||
graph_data->y = malloc(sizeof(float) * graph_width);
|
||||
memset(graph_data->x, 0, sizeof(float) * graph_width);
|
||||
memset(graph_data->y, 0, sizeof(float) * graph_width);
|
||||
graph_data->cursor = 0;
|
||||
graph_data->color = rgba_color_from_hex(0xcccccc);
|
||||
graph_data->fill = true;
|
||||
graph_data->ready = true;
|
||||
}
|
||||
|
||||
void graph_data_destruct(struct graph_data* graph_data) {
|
||||
free(graph_data->x);
|
||||
if (!graph_data->ready) return;
|
||||
free(graph_data->y);
|
||||
}
|
||||
|
||||
float graph_data_get_x(struct graph_data* graph_data, uint32_t i) {
|
||||
return graph_data->x[ (graph_data->cursor + i)%graph_data->graph_width ];
|
||||
}
|
||||
|
||||
float graph_data_get_y(struct graph_data* graph_data, uint32_t i) {
|
||||
if (!graph_data->ready) return 0.f;
|
||||
return graph_data->y[ (graph_data->cursor + i)%graph_data->graph_width ];
|
||||
}
|
||||
|
||||
void graph_data_push_back(struct graph_data* graph_data, float x, float y) {
|
||||
graph_data->x[graph_data->cursor] = x;
|
||||
void graph_data_push_back(struct graph_data* graph_data, float y) {
|
||||
if (!graph_data->ready) return;
|
||||
graph_data->y[graph_data->cursor] = y;
|
||||
|
||||
++graph_data->cursor;
|
||||
|
|
|
@ -2,18 +2,17 @@
|
|||
#define GRAPH_DATA_H
|
||||
|
||||
struct graph_data {
|
||||
bool ready;
|
||||
struct rgba_color color;
|
||||
bool fill;
|
||||
uint32_t cursor;
|
||||
uint32_t graph_width;
|
||||
float* x;
|
||||
float* y;
|
||||
};
|
||||
|
||||
void graph_data_init(struct graph_data* graph_data, uint32_t graph_width);
|
||||
void graph_data_push_back(struct graph_data* graph_data, float x, float y);
|
||||
void graph_data_push_back(struct graph_data* graph_data, float y);
|
||||
void graph_data_destruct(struct graph_data* graph_data);
|
||||
float graph_data_get_x(struct graph_data* graph_data, uint32_t i);
|
||||
float graph_data_get_y(struct graph_data* graph_data, uint32_t i);
|
||||
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
|
||||
#include "event_loop.h"
|
||||
#include "event.h"
|
||||
#include "event_tap.h"
|
||||
#include "workspace.h"
|
||||
#include "message.h"
|
||||
#include "display.h"
|
||||
|
@ -47,9 +48,11 @@
|
|||
#include "graph_data.h"
|
||||
#include "bar_item.h"
|
||||
#include "bar_manager.h"
|
||||
#include "mouse.h"
|
||||
|
||||
#include "event_loop.c"
|
||||
#include "event.c"
|
||||
#include "event_tap.c"
|
||||
#include "workspace.m"
|
||||
#include "message.c"
|
||||
#include "display.c"
|
||||
|
|
|
@ -27,6 +27,7 @@ extern bool g_verbose;
|
|||
#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"
|
||||
|
@ -160,6 +161,7 @@ static void handle_domain_subscribe(FILE* rsp, struct token domain, char* messag
|
|||
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));
|
||||
if (item_index_for_name < 0) return;
|
||||
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
|
||||
|
||||
if (token_equals(event, COMMAND_SUBSCRIBE_SYSTEM_WOKE)) {
|
||||
|
@ -181,15 +183,15 @@ static void handle_domain_subscribe(FILE* rsp, struct token domain, char* messag
|
|||
static void handle_domain_clear(FILE* rsp, struct token domain, char* message) {
|
||||
}
|
||||
|
||||
// Syntax: sketchybar -m push <name> <x> <y>
|
||||
// Syntax: sketchybar -m push <name> <y>
|
||||
static void handle_domain_push(FILE* rsp, struct token domain, char* message) {
|
||||
struct token name = get_token(&message);
|
||||
struct token x = get_token(&message);
|
||||
struct token y = get_token(&message);
|
||||
|
||||
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, token_to_string(name));
|
||||
if (item_index_for_name < 0) return;
|
||||
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
|
||||
graph_data_push_back(&bar_item->graph_data, token_to_float(x), token_to_float(y));
|
||||
graph_data_push_back(&bar_item->graph_data, token_to_float(y));
|
||||
}
|
||||
|
||||
// Syntax: sketchybar -m add <item|component|plugin> (<identifier>) <name> <position>
|
||||
|
@ -263,6 +265,8 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
|
|||
bar_item_set_label_font(bar_item, string_copy(message));
|
||||
} else if (token_equals(property, COMMAND_SET_SCRIPT)) {
|
||||
bar_item_set_script(bar_item, string_copy(message));
|
||||
} else if (token_equals(property, COMMAND_SET_CLICK_SCRIPT)) {
|
||||
bar_item_set_click_script(bar_item, string_copy(message));
|
||||
} else if (token_equals(property, COMMAND_SET_UPDATE_FREQ)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->update_frequency = token_to_uint32t(value);
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#define HELPERS_H
|
||||
|
||||
extern AXError _AXUIElementGetWindow(AXUIElementRef ref, uint32_t *wid);
|
||||
extern CFArrayRef SLSCopyManagedDisplaySpaces(int cid);
|
||||
extern int g_connection;
|
||||
|
||||
static const char *bool_str[] = { "off", "on" };
|
||||
|
||||
|
@ -46,6 +48,12 @@ static inline bool string_equals(const char *a, const char *b)
|
|||
return a && b && strcmp(a, b) == 0;
|
||||
}
|
||||
|
||||
static bool cgrect_contains_point(CGRect* r, CGPoint* p)
|
||||
{
|
||||
return p->x >= r->origin.x && p->x <= r->origin.x + r->size.width &&
|
||||
p->y >= r->origin.y && p->y <= r->origin.y + r->size.height;
|
||||
}
|
||||
|
||||
static inline char *string_escape_quote(char *s)
|
||||
{
|
||||
if (!s) return NULL;
|
||||
|
@ -165,6 +173,36 @@ static inline pid_t ax_window_pid(AXUIElementRef ref)
|
|||
return *(pid_t *)((void *) ref + 0x10);
|
||||
}
|
||||
|
||||
|
||||
static inline int mission_control_index(uint64_t sid)
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int desktop_cnt = 1;
|
||||
|
||||
CFArrayRef display_spaces_ref = SLSCopyManagedDisplaySpaces(g_connection);
|
||||
int display_spaces_count = CFArrayGetCount(display_spaces_ref);
|
||||
|
||||
for (int i = 0; i < display_spaces_count; ++i) {
|
||||
CFDictionaryRef display_ref = CFArrayGetValueAtIndex(display_spaces_ref, i);
|
||||
CFArrayRef spaces_ref = CFDictionaryGetValue(display_ref, CFSTR("Spaces"));
|
||||
int spaces_count = CFArrayGetCount(spaces_ref);
|
||||
|
||||
for (int j = 0; j < spaces_count; ++j) {
|
||||
CFDictionaryRef space_ref = CFArrayGetValueAtIndex(spaces_ref, j);
|
||||
CFNumberRef sid_ref = CFDictionaryGetValue(space_ref, CFSTR("id64"));
|
||||
CFNumberGetValue(sid_ref, CFNumberGetType(sid_ref), &result);
|
||||
if (sid == result) goto out;
|
||||
|
||||
++desktop_cnt;
|
||||
}
|
||||
}
|
||||
|
||||
desktop_cnt = 0;
|
||||
out:
|
||||
CFRelease(display_spaces_ref);
|
||||
return desktop_cnt;
|
||||
}
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
static inline bool psn_equals(ProcessSerialNumber *a, ProcessSerialNumber *b)
|
||||
|
|
8
src/mouse.h
Normal file
8
src/mouse.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef MOUSE_H
|
||||
#define MOUSE_H
|
||||
|
||||
struct mouse_state {
|
||||
CGPoint down_location;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -28,6 +28,8 @@ struct display_manager g_display_manager;
|
|||
struct application_manager g_application_manager;
|
||||
struct daemon g_daemon;
|
||||
struct bar_manager g_bar_manager;
|
||||
struct mouse_state g_mouse_state;
|
||||
struct event_tap g_event_tap;
|
||||
int g_connection;
|
||||
|
||||
char g_socket_file[MAXLEN];
|
||||
|
@ -257,6 +259,7 @@ int main(int argc, char **argv)
|
|||
workspace_event_handler_begin(&g_workspace_context);
|
||||
application_manager_begin(&g_application_manager);
|
||||
bar_manager_begin(&g_bar_manager);
|
||||
event_tap_begin(&g_event_tap, EVENT_MASK_MOUSE, mouse_handler);
|
||||
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1204, NULL);
|
||||
|
||||
if (!socket_daemon_begin_un(&g_daemon, g_socket_file, message_handler)) {
|
||||
|
|
Loading…
Add table
Reference in a new issue