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 click event not captured on unfocused displays #392

Closed
john2zy opened this issue Aug 31, 2023 · 32 comments
Closed

Mouse click event not captured on unfocused displays #392

john2zy opened this issue Aug 31, 2023 · 32 comments

Comments

@john2zy
Copy link

john2zy commented Aug 31, 2023

After upgrading to v2.16.2, clicking on an icon that's not on the current focused display won't produce a mouse click event. For example, I'm now typing in the browser on display 1; if I click a sketchbar icon on display 2, the callback script won't be called. I've tested with a script that prints a message when clicked and confirmed no event was received.

I'm using sketchybar with yabai v5.0.7. Is it possible that yabai's space focus logic interferes with sketchybar?

@FelixKratz
Copy link
Owner

FelixKratz commented Aug 31, 2023

This is a side-effect of the new mouse/event system. The new API I am using seems to not report any mouse events on non-focussed displays.

I realize that this suboptimal but going back to the rusty Carbon API would be even more suboptimal I think. Maybe I can improve on the current design by doing some more research.

@john2zy
Copy link
Author

john2zy commented Sep 1, 2023

Thank you! Maybe I can dig into this if I manage to find time.

@john2zy
Copy link
Author

john2zy commented Sep 22, 2023

v2.16.3 fixed this issue for me. Cheers.

@mrpmohiburrahman
Copy link

mrpmohiburrahman commented Sep 27, 2023

screenrecord.mp4

Hi, @FelixKratz,

I have upgraded to mac os sonoma today, and faced with slightly similar issue.
In my case the clicking on the window numbers is not working even in the focused display.

Sketchybar: v 2.16.3
Yabai: 5.0.8

screenshot

@FelixKratz
Copy link
Owner

FelixKratz commented Sep 27, 2023

It is a change in Sonoma that causes mouse events to not work at all. I will soon prepare a patch for Sonoma, then the original issue will be solved as well.

@FelixKratz
Copy link
Owner

All of these issues should be fixed in v.2.16.4

@johnallen3d
Copy link

Hey @FelixKratz - I'm playing around with adding popup windows to my bar today and have found that I can't get clicks to trigger. In fact, I'm realizing that none of my, recent working, click_script are triggering. After reading this it hit me that I installed the latest Sanoma update (14.2.1) a couple of days ago. Could that be related?

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

I am on 14.2.1 as well and my click scripts seem to be working can you provide a minimal example which does not work for you?

Edit:
This minimal example works for me:
sketchybarrc

sketchybar --bar color=0xff000000
sketchybar --add item test left --set test label="Click Me" click_script='osascript -e "display dialog \"Works\""'

@johnallen3d
Copy link

My minimal example was the example you shared for popups: #12

However, thanks for sharing ☝️, that's much simpler and also not triggering clicks. I'm running sketchybar in the foreground and seeing nothing in STDOUT/ERR.

❯ which sketchybar
/opt/homebrew/bin/sketchybar

❯ sketchybar -v
sketchybar-v2.19.6

What else... I'm using AeroSpace (not Yabai), I have SIP disabled. 🤔 If there's some other way I can debug I'm game. Also, I'm happy to open a new Issue.

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

We can try some other things to narrow down the problem some more:

  • What does sketchybar --query test return?
  • Do sliders work? E.g. can you slide the slider with the mouse?
sketchybar --add slider slider left 100 --set slider slider.background.color=0xffffffff slider.highlight_color=0xffff0000 slider.background.height=15
  • Do regular mouse.clicked events work? E.g.:
sketchybar --add item test left --set test label="Click Me" script='osascript -e "display dialog \"Hallo\""' --subscribe test mouse.clicked
  • Does a restart help?

@johnallen3d
Copy link

  1. query test
❯ sketchybar --query test
{
	"name": "test",
	"type": "item",
	"geometry": {
		"drawing": "on",
		"position": "left",
		"associated_space_mask": 0,
		"associated_display_mask": 0,
		"ignore_association": "off",
		"y_offset": 0,
		"width": 0,
		"padding_left": 0,
		"padding_right": -1,
		"background": {
			"drawing": "off",
			"color": "0x0",
			"border_color": "0x0",
			"border_width": 0,
			"height": 0,
			"corner_radius": 0,
			"padding_left": 0,
			"padding_right": 0,
			"y_offset": 0,
			"clip": 0.000000,
			"image": {
				"value": "(null)",
				"drawing": "off",
				"scale": 1.000000
			},
			"shadow": {
				"drawing": "off",
				"color": "0xff000000",
				"angle": 30,
				"distance": 5
			}
		}
	},
	"icon": {
		"value": "",
		"drawing": "on",
		"highlight": "off",
		"color": "0xffffffff",
		"highlight_color": "0xff000000",
		"padding_left": 0,
		"padding_right": 0,
		"y_offset": 0,
		"font": "Hack Nerd Font:Bold:14.00",
		"width": 0,
		"align": "left",
		"background": {
			"drawing": "off",
			"color": "0x0",
			"border_color": "0x0",
			"border_width": 0,
			"height": 0,
			"corner_radius": 0,
			"padding_left": 0,
			"padding_right": 0,
			"y_offset": 0,
			"clip": 0.000000,
			"image": {
				"value": "(null)",
				"drawing": "off",
				"scale": 1.000000
			},
			"shadow": {
				"drawing": "off",
				"color": "0xff000000",
				"angle": 30,
				"distance": 5
			}
		},
		"shadow": {
			"drawing": "off",
			"color": "0xff000000",
			"angle": 30,
			"distance": 5
		}
	},
	"label": {
		"value": "Click Me",
		"drawing": "on",
		"highlight": "off",
		"color": "0xffffffff",
		"highlight_color": "0xff000000",
		"padding_left": 0,
		"padding_right": 0,
		"y_offset": 0,
		"font": "Hack Nerd Font:Bold:14.00",
		"width": 0,
		"align": "left",
		"background": {
			"drawing": "off",
			"color": "0x0",
			"border_color": "0x0",
			"border_width": 0,
			"height": 0,
			"corner_radius": 0,
			"padding_left": 0,
			"padding_right": 0,
			"y_offset": 0,
			"clip": 0.000000,
			"image": {
				"value": "(null)",
				"drawing": "off",
				"scale": 1.000000
			},
			"shadow": {
				"drawing": "off",
				"color": "0xff000000",
				"angle": 30,
				"distance": 5
			}
		},
		"shadow": {
			"drawing": "off",
			"color": "0xff000000",
			"angle": 30,
			"distance": 5
		}
	},
	"scripting": {
		"script": "(null)",
		"click_script": "osascript -e \"display dialog \\"Works\\"\"",
		"update_freq": 0,
		"update_mask": 0,
		"updates": "on"
	},
	"bounding_rects": {
		"display-1": {
			"origin": [ 20.000000, 0.000000 ],
			"size": [ 68.000000, 25.000000 ]
		},
		"display-2": {
			"origin": [ 1372.000000, -349.000000 ],
			"size": [ 68.000000, 25.000000 ]
		}
	}
}
  1. sliders - no
  2. regular mouse.clicked - no
  3. restart (sketchybar and macOS) - no

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

Ok, next steps for debugging are in code, are you able to build from source?

First interesting test would be:
put printf("Mouse Event\n"); above this line:

event_post(&event);

and next step after that would be:
put printf("item: %llu\n", window); here:

build the binary and see what it prints when you try to click the test item.

@johnallen3d
Copy link

OK, thanks for sticking with my @FelixKratz. I do see "Mouse Event" but I do not see "item: " when adding these debug lines.

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

Can you try to find out in which line of this function the mouse handling errors out?

SketchyBar/src/event.c

Lines 70 to 98 in 6ff8ffa

CGPoint point = CGEventGetLocation(context);
uint32_t wid = get_wid_from_cg_event(context);
CGEventType type = CGEventGetType(context);
uint32_t mouse_button_code = CGEventGetIntegerValueField(context, kCGMouseEventButtonNumber);
uint32_t modifier_keys = CGEventGetFlags(context);
uint32_t adid = display_arrangement(display_active_display_id());
struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager,
wid,
adid );
struct bar* bar = bar_manager_get_bar_by_wid(&g_bar_manager, wid);
struct popup* popup = bar_manager_get_popup_by_wid(&g_bar_manager, wid);
if (!bar_item && !popup && !bar) return;
if (!bar_item || bar_item->type == BAR_COMPONENT_GROUP) {
bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, adid);
}
struct window* window = NULL;
CGPoint point_in_window_coords = CGPointZero;
if (bar_item) {
window = bar_item_get_window(bar_item, adid);
if (window) {
point_in_window_coords.x = point.x - window->origin.x;
point_in_window_coords.y = point.y - window->origin.y;
}
}

Maybe first do a sanity check that the event actually reaches this function with a print in the first line and then progressively work through all the return paths (which there is only one) and check where the mouse handling errors out.

  • What does uint32_t wid = get_wid_from_cg_event(context); contain?
  • What does uint32_t adid = display_arrangement(display_active_display_id()); contain?

BTW are you using arm or intel?

@johnallen3d
Copy link

johnallen3d commented Jan 15, 2024

OK, I don't know what I did wrong last time but I am getting all the way through event_mouse_up. 🤦‍♂️

wid: 362
adid: 0
after bar_manager_refresh

I'm on Arm.

edit - maybe I should look at bar_item_on_click now?

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

Yes, have a look inside bar_item_on_click. Does it survive the first check? I.e. is bar_item not NULL? If the click works the control flow should eventually reach the function fork_exec

@johnallen3d
Copy link

johnallen3d commented Jan 15, 2024

Nope, it does not survive here:

if (!bar_item) return;

@johnallen3d
Copy link

bar_item appears to be 0

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

Oh I overlooked something above that is probably already the problem.... adid should not be zero.

Can you check why this function returns zero?

SketchyBar/src/display.c

Lines 101 to 131 in 6ff8ffa

int display_arrangement(uint32_t did) {
if (display_active_display_count() == 1) {
uint32_t result = 0;
uint32_t count = 0;
CGGetActiveDisplayList(1, &result, &count);
if (did == result && count == 1) return 1;
else return 0;
}
CFStringRef uuid = display_uuid(did);
if (!uuid) return 0;
CFArrayRef displays = SLSCopyManagedDisplays(g_connection);
if (!displays) {
CFRelease(uuid);
return 0;
}
int result = 0;
int displays_count = CFArrayGetCount(displays);
for (int i = 0; i < displays_count; ++i) {
if (CFEqual(CFArrayGetValueAtIndex(displays, i), uuid)) {
result = i + 1;
break;
}
}
CFRelease(displays);
CFRelease(uuid);
return result;
}

Is it because of an early return due to some error, or does the function go all the way to the bottom?

  • Can you check what happens if you run sketchybar on a single monitor?

@johnallen3d
Copy link

Was just grabbing all the values through adid:

point: 4653974661560795136
wid: 514
type: 2
mouse_button_code: 0
modifier_keys: 0
adid: 0

Will checkout display_arrangement now.

@johnallen3d
Copy link

johnallen3d commented Jan 15, 2024

Looks like display_uuid(did) is returning 0. So not making it through that function. Digging further, CGDisplayCreateUUIDFromDisplayID(did) is returning 0.

@FelixKratz
Copy link
Owner

Can you check the value of did?

@johnallen3d
Copy link

did is 2

@johnallen3d
Copy link

Ok, interesting, I just disconnected my external and the click worked as expected (eg. I saw the "Works" dialog). When I reconnect it does not work. 🤔

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

The function display_uuid failing is bad, several other things will probably not work as well, such as

sketchybar --set test display=active

should only render the test item (from above) on the active display. Can you check if this is indeed not working by switching focus between displays?

@johnallen3d
Copy link

On both displays:

❯ sketchybar --set test display=active

no value returned, though exit code is 👍. However the item itself seems to be destroyed when running the command (disappears from bar).

FWIW - I can reliably reproduce this. External connected I never see the dialog, external disconnected I always see the dialog.

@johnallen3d
Copy link

I can also say that about a week ago, while converting my config to Lua, click_script was working just fine with an external monitor connected. The difference being, since then, the macOS update.

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

Can you try the latest commit? d2299c8

@johnallen3d
Copy link

Same behaviour, works w/o external, does not work w/ external. At a glance, adid is still 0 with the external connected.

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

I think I know why. It has to do with the option: "Displays have separate spaces" in System Settings under Desktop & Dock.
If you activate it it and reboot, clicks work. If it is deactivated I can reproduce the issue. Now I will be able to provide a fix.

The problem with "Displays have separate spaces" being off is that there seems to be no event emitted when the active display changes. It would probably be best to include a check on startup that prevents sketchybar from running if this option is not set appropriately.

@johnallen3d
Copy link

OK, nice find! It's possible I changed this setting in that time span as well (sorry I forgot) when switching from Yabai to Aerospace related docs. I've had some odd issues with that setting enabled so I had switched it off.

@FelixKratz
Copy link
Owner

FelixKratz commented Jan 15, 2024

I have included a check on startup. Just too many things are completely broken when the active display id is not reported properly. Maybe I find another way to get the id of the active display which works with "Displays have separate spaces" off, then the bar should again work with this option turned off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants