Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse scroll events #380

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,35 @@ 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, 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\"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, "%d", scroll_delta);
env_vars_set(&env_vars,
string_copy("SCROLL_DELTA"),
string_copy(delta_ver_str));

if (bar_item->update_mask & UPDATE_MOUSE_SCROLLED)
bar_item_update(bar_item,
COMMAND_SUBSCRIBE_MOUSE_SCROLLED,
true,
&env_vars );


env_vars_destroy(&env_vars);
}

void bar_item_mouse_entered(struct bar_item* bar_item) {
if (bar_item->update_mask & UPDATE_MOUSE_ENTERED)
bar_item_update(bar_item, COMMAND_SUBSCRIBE_MOUSE_ENTERED, true, NULL);
Expand Down
1 change: 1 addition & 0 deletions src/bar_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +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, 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
29 changes: 29 additions & 0 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,35 @@ 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, 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, "%d", scroll_delta);
env_vars_set(&env_vars,
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("DID"),
string_copy(adid_str));

bar_manager_custom_events_trigger(bar_manager,
COMMAND_SUBSCRIBE_MOUSE_SCROLLED_GLOBAL,
&env_vars );
env_vars_destroy(&env_vars);
}

void bar_manager_handle_mouse_entered(struct bar_manager* bar_manager, struct bar_item* bar_item) {
if (!bar_item) return;
bar_item_mouse_entered(bar_item);
Expand Down
1 change: 1 addition & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +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, 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
2 changes: 2 additions & 0 deletions src/custom_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ void custom_events_init(struct custom_events* custom_events) {
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_ENTERED), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_EXITED), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_CLICKED), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_SCROLLED), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_SYSTEM_WILL_SLEEP), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_ENTERED_GLOBAL), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_EXITED_GLOBAL), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_MOUSE_SCROLLED_GLOBAL), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_VOLUME_CHANGE), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_BRIGHTNESS_CHANGE), NULL);
custom_events_append(custom_events, string_copy(COMMAND_SUBSCRIBE_POWER_SOURCE_CHANGE), NULL);
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
79 changes: 79 additions & 0 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,85 @@ 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_wid_from_cg_event(context);
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
+ g_scroll_info.delta_y,
bar->adid);
}

g_scroll_info.delta_y = 0;
CFRelease(context);
return EVENT_SUCCESS;
}

struct popup* popup = bar_manager_get_popup_by_wid(&g_bar_manager, wid);
if (popup) {
// 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
+ g_scroll_info.delta_y,
bar->adid);
}

g_scroll_info.delta_y = 0;
CFRelease(context);
return EVENT_SUCCESS;
}

struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager,
wid,
adid );

if (!bar_item || bar_item->type == BAR_COMPONENT_GROUP) {
bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, adid);
}

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;
}


EVENT_CALLBACK(EVENT_HANDLER_VOLUME_CHANGED) {
bar_manager_handle_volume_change(&g_bar_manager, *(float*)context);
free(context);
Expand Down
4 changes: 4 additions & 0 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP);
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED);
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_ENTERED);
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_EXITED);
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_SCROLLED);
EVENT_CALLBACK(EVENT_HANDLER_VOLUME_CHANGED);
EVENT_CALLBACK(EVENT_HANDLER_WIFI_CHANGED);
EVENT_CALLBACK(EVENT_HANDLER_BRIGHTNESS_CHANGED);
Expand Down Expand Up @@ -62,6 +63,7 @@ enum event_type {
MOUSE_DRAGGED,
MOUSE_ENTERED,
MOUSE_EXITED,
MOUSE_SCROLLED,
VOLUME_CHANGED,
WIFI_CHANGED,
BRIGHTNESS_CHANGED,
Expand Down Expand Up @@ -92,6 +94,7 @@ static const char *event_type_str[] = {
[MOUSE_DRAGGED] = "mouse_dragged",
[MOUSE_ENTERED] = "mouse_entered",
[MOUSE_EXITED] = "mouse_exited",
[MOUSE_SCROLLED] = "mouse_scrolled",
[VOLUME_CHANGED] = "volume_changed",
[WIFI_CHANGED] = "wifi_changed",
[BRIGHTNESS_CHANGED] = "brightness_changed",
Expand All @@ -114,6 +117,7 @@ static event_callback *event_handler[] = {
[MOUSE_DRAGGED] = EVENT_HANDLER_MOUSE_DRAGGED,
[MOUSE_ENTERED] = EVENT_HANDLER_MOUSE_ENTERED,
[MOUSE_EXITED] = EVENT_HANDLER_MOUSE_EXITED,
[MOUSE_SCROLLED] = EVENT_HANDLER_MOUSE_SCROLLED,
[VOLUME_CHANGED] = EVENT_HANDLER_VOLUME_CHANGED,
[WIFI_CHANGED] = EVENT_HANDLER_WIFI_CHANGED,
[BRIGHTNESS_CHANGED] = EVENT_HANDLER_BRIGHTNESS_CHANGED,
Expand Down
2 changes: 2 additions & 0 deletions src/misc/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED "mouse.entered"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED "mouse.exited"
#define COMMAND_SUBSCRIBE_MOUSE_CLICKED "mouse.clicked"
#define COMMAND_SUBSCRIBE_MOUSE_SCROLLED "mouse.scrolled"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED_GLOBAL "mouse.entered.global"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED_GLOBAL "mouse.exited.global"
#define COMMAND_SUBSCRIBE_MOUSE_SCROLLED_GLOBAL "mouse.scrolled.global"

#define DOMAIN_QUERY "--query"
#define COMMAND_QUERY_DEFAULT_ITEMS "default_menu_items"
Expand Down
10 changes: 9 additions & 1 deletion src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ static const EventTypeSpec mouse_events [] = {
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseDragged },
{ kEventClassMouse, kEventMouseEntered },
{ kEventClassMouse, kEventMouseExited }
{ kEventClassMouse, kEventMouseExited },
{ kEventClassMouse, kEventMouseWheelMoved }
};


Expand Down Expand Up @@ -45,6 +46,13 @@ static pascal OSStatus mouse_handler(EventHandlerCallRef next, EventRef e, void
event_loop_post(&g_event_loop, event);
break;
}
case kEventMouseWheelMoved: {
struct event *event = event_create(&g_event_loop,
MOUSE_SCROLLED,
(void *) CFRetain(CopyEventCGEvent(e)));
event_loop_post(&g_event_loop, event);
break;
}
default:
break;
}
Expand Down