Skip to content

Commit

Permalink
adapt mouse scroll events
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Jun 25, 2023
1 parent 63db0a6 commit ef9c8e0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
42 changes: 11 additions & 31 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,34 +242,31 @@ void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t mouse_
env_vars_destroy(&env_vars);
}

void bar_item_on_scroll(struct bar_item* bar_item, uint32_t type, long scroll_delta_vert) {
void bar_item_on_scroll(struct bar_item* bar_item, int scroll_delta) {
if (!bar_item) return;

struct env_vars env_vars;
env_vars_init(&env_vars);
char info_str[256];
snprintf(info_str, 256, "{\n"
"\t\"scroll_delta_vert\": %ld\n"
"}\n",
scroll_delta_vert );
"\t\"delta\": %d\n"
"}\n",
scroll_delta );

env_vars_set(&env_vars, string_copy("INFO"), string_copy(info_str));

char delta_ver_str[32];
snprintf(delta_ver_str, 32, "%ld", scroll_delta_vert);
snprintf(delta_ver_str, 32, "%d", scroll_delta);
env_vars_set(&env_vars,
string_copy("DELTA_VERT"),
string_copy("SCROLL_DELTA"),
string_copy(delta_ver_str));

if (bar_item->scroll_script && strlen(bar_item->scroll_script) > 0) {
for (int i = 0; i < bar_item->signal_args.env_vars.count; i++) {
env_vars_set(&env_vars,
string_copy(bar_item->signal_args.env_vars.vars[i]->key),
string_copy(bar_item->signal_args.env_vars.vars[i]->value));
}
if (bar_item->update_mask & UPDATE_MOUSE_SCROLLED)
bar_item_update(bar_item,
COMMAND_SUBSCRIBE_MOUSE_SCROLLED,
true,
&env_vars );

fork_exec(bar_item->scroll_script, &env_vars);
}

env_vars_destroy(&env_vars);
}
Expand Down Expand Up @@ -323,21 +320,6 @@ static void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
bar_item->click_script = path;
}

static void bar_item_set_scroll_script(struct bar_item* bar_item, char* script) {
if (!script) return;

if (bar_item->scroll_script && strcmp(bar_item->scroll_script, script) == 0) {
free(script);
return;
}

if (script != bar_item->scroll_script && bar_item->scroll_script)
free(bar_item->scroll_script);

char* path = resolve_path(script);
bar_item->scroll_script = path;
}

static bool bar_item_set_yoffset(struct bar_item* bar_item, int offset) {
if (bar_item->y_offset == offset) return false;
bar_item->y_offset = offset;
Expand Down Expand Up @@ -1086,8 +1068,6 @@ void bar_item_parse_set_message(struct bar_item* bar_item, char* message, FILE*
bar_item_set_script(bar_item, token_to_string(get_token(&message)));
} else if (token_equals(property, PROPERTY_CLICK_SCRIPT)) {
bar_item_set_click_script(bar_item, token_to_string(get_token(&message)));
} else if (token_equals(property, PROPERTY_SCROLL_SCRIPT)) {
bar_item_set_scroll_script(bar_item, token_to_string(get_token(&message)));
} else if (token_equals(property, PROPERTY_UPDATE_FREQ)) {
bar_item->update_frequency = token_to_uint32t(get_token(&message));
} else if (token_equals(property, PROPERTY_POSITION)) {
Expand Down
3 changes: 1 addition & 2 deletions src/bar_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ struct bar_item {

char* script;
char* click_script;
char* scroll_script;
struct signal_args signal_args;

// The position in the bar: l,r,c
Expand Down Expand Up @@ -107,7 +106,7 @@ void bar_item_needs_update(struct bar_item* bar_item);
bool bar_item_update(struct bar_item* bar_item, char* sender, bool forced, struct env_vars* env_vars);

void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t mouse_button_code, uint32_t modifier, CGPoint point);
void bar_item_on_scroll(struct bar_item* bar_item, uint32_t type, long scroll_delta_vert);
void bar_item_on_scroll(struct bar_item* bar_item, int scroll_delta);
void bar_item_on_drag(struct bar_item* bar_item, CGPoint point);
void bar_item_mouse_entered(struct bar_item* bar_item);
void bar_item_mouse_exited(struct bar_item* bar_item);
Expand Down
16 changes: 12 additions & 4 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,19 +781,27 @@ void bar_manager_handle_mouse_exited_global(struct bar_manager* bar_manager) {
NULL );
}

void bar_manager_handle_mouse_scrolled_global(struct bar_manager* bar_manager, long scroll_delta_vert, uint32_t adid) {
void bar_manager_handle_mouse_scrolled_global(struct bar_manager* bar_manager, int scroll_delta, uint32_t adid) {
struct env_vars env_vars;
env_vars_init(&env_vars);
char delta_ver_str[32];
snprintf(delta_ver_str, 32, "%ld", scroll_delta_vert);
snprintf(delta_ver_str, 32, "%d", scroll_delta);
env_vars_set(&env_vars,
string_copy("DELTA_VERT"),
string_copy("SCROLL_DELTA"),
string_copy(delta_ver_str));

char info_str[256];
snprintf(info_str, 256, "{\n"
"\t\"delta\": %d\n"
"}\n",
scroll_delta );

env_vars_set(&env_vars, string_copy("INFO"), string_copy(info_str));

char adid_str[32];
snprintf(adid_str, 32, "%u", adid);
env_vars_set(&env_vars,
string_copy("DISPLAY"),
string_copy("DID"),
string_copy(adid_str));

bar_manager_custom_events_trigger(bar_manager,
Expand Down
2 changes: 1 addition & 1 deletion src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void bar_manager_resize(struct bar_manager* bar_manager);

void bar_manager_handle_mouse_entered_global(struct bar_manager* bar_manager);
void bar_manager_handle_mouse_exited_global(struct bar_manager* bar_manager);
void bar_manager_handle_mouse_scrolled_global(struct bar_manager* bar_manager, long scroll_delta_vert, uint32_t did);
void bar_manager_handle_mouse_scrolled_global(struct bar_manager* bar_manager, int scroll_delta, uint32_t did);
void bar_manager_handle_mouse_entered(struct bar_manager* bar_manager, struct bar_item* bar_item);
void bar_manager_handle_mouse_exited(struct bar_manager* bar_manager, struct bar_item* bar_item);
void bar_manager_handle_front_app_switch(struct bar_manager* bar_manager, char* info);
Expand Down
18 changes: 10 additions & 8 deletions src/custom_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
#define UPDATE_MOUSE_ENTERED 1ULL << 4
#define UPDATE_MOUSE_EXITED 1ULL << 5
#define UPDATE_MOUSE_CLICKED 1ULL << 6
#define UPDATE_SYSTEM_WILL_SLEEP 1ULL << 7
#define UPDATE_ENTERED_GLOBAL 1ULL << 8
#define UPDATE_EXITED_GLOBAL 1ULL << 9
#define UPDATE_VOLUME_CHANGE 1ULL << 10
#define UPDATE_BRIGHTNESS_CHANGE 1ULL << 11
#define UPDATE_POWER_SOURCE_CHANGE 1ULL << 12
#define UPDATE_WIFI_CHANGE 1ULL << 13
#define UPDATE_MEDIA_CHANGE 1ULL << 14
#define UPDATE_MOUSE_SCROLLED 1ULL << 7
#define UPDATE_SYSTEM_WILL_SLEEP 1ULL << 8
#define UPDATE_ENTERED_GLOBAL 1ULL << 9
#define UPDATE_EXITED_GLOBAL 1ULL << 10
#define UPDATE_SCROLLED_GLOBAL 1ULL << 11
#define UPDATE_VOLUME_CHANGE 1ULL << 12
#define UPDATE_BRIGHTNESS_CHANGE 1ULL << 13
#define UPDATE_POWER_SOURCE_CHANGE 1ULL << 14
#define UPDATE_WIFI_CHANGE 1ULL << 15
#define UPDATE_MEDIA_CHANGE 1ULL << 16

extern void* g_workspace_context;
extern void workspace_create_custom_observer(void** context, char* name);
Expand Down
37 changes: 32 additions & 5 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,43 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_EXITED) {
return EVENT_SUCCESS;
}

#define SCROLL_TIMEOUT 150000000
struct {
uint64_t timestamp;
int delta_y;

} g_scroll_info;

EVENT_CALLBACK(EVENT_HANDLER_MOUSE_SCROLLED) {
CGPoint point = CGEventGetLocation(context);
uint32_t wid = get_window_id_from_cg_event(context);
CGEventType type = CGEventGetType(context);
long scroll_delta_vert = CGEventGetIntegerValueField(context, kCGScrollWheelEventDeltaAxis1);
int scroll_delta = CGEventGetIntegerValueField(context, kCGScrollWheelEventDeltaAxis1);
uint32_t adid = display_arrangement(display_active_display_id());

uint64_t event_time = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX);
if (g_scroll_info.timestamp + SCROLL_TIMEOUT > event_time) {
g_scroll_info.delta_y += scroll_delta;
CFRelease(context);
return EVENT_SUCCESS;
} else {
if (g_scroll_info.timestamp + 2*SCROLL_TIMEOUT < event_time)
g_scroll_info.delta_y = 0;
g_scroll_info.timestamp = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX);
}


struct bar* bar = bar_manager_get_bar_by_wid(&g_bar_manager, wid);
if (bar) {
// Handle global mouse scrolled event
if (bar->mouse_over
&& !bar_manager_mouse_over_any_popup(&g_bar_manager)) {
bar_manager_handle_mouse_scrolled_global(&g_bar_manager, scroll_delta_vert, bar->adid);
bar_manager_handle_mouse_scrolled_global(&g_bar_manager,
scroll_delta
+ g_scroll_info.delta_y,
bar->adid);
}

g_scroll_info.delta_y = 0;
CFRelease(context);
return EVENT_SUCCESS;
}
Expand All @@ -299,9 +321,13 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_SCROLLED) {
// Handle global mouse scrolled event
if (popup->mouse_over
&& !bar_manager_mouse_over_any_bar(&g_bar_manager)) {
bar_manager_handle_mouse_scrolled_global(&g_bar_manager, scroll_delta_vert, bar->adid);
bar_manager_handle_mouse_scrolled_global(&g_bar_manager,
scroll_delta
+ g_scroll_info.delta_y,
bar->adid);
}

g_scroll_info.delta_y = 0;
CFRelease(context);
return EVENT_SUCCESS;
}
Expand All @@ -314,11 +340,12 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_SCROLLED) {
bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, adid);
}

bar_item_on_scroll(bar_item, type, scroll_delta_vert);
bar_item_on_scroll(bar_item, scroll_delta + g_scroll_info.delta_y);

if (bar_item && bar_item->needs_update)
bar_manager_refresh(&g_bar_manager, false);

g_scroll_info.delta_y = 0;
CFRelease(context);
return EVENT_SUCCESS;
}
Expand Down
1 change: 0 additions & 1 deletion src/misc/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
#define PROPERTY_UPDATE_FREQ "update_freq"
#define PROPERTY_SCRIPT "script"
#define PROPERTY_CLICK_SCRIPT "click_script"
#define PROPERTY_SCROLL_SCRIPT "scroll_script"
#define PROPERTY_ICON "icon"
#define PROPERTY_YOFFSET "y_offset"
#define PROPERTY_WIDTH "width"
Expand Down

0 comments on commit ef9c8e0

Please sign in to comment.