-
Notifications
You must be signed in to change notification settings - Fork 943
Add error-returning mouse click helpers (ClickE) #750
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -156,20 +156,29 @@ MMPointInt32 location() { | |||||||||
| } | ||||||||||
|
|
||||||||||
| /* Press down a button, or release it. */ | ||||||||||
| void toggleMouse(bool down, MMMouseButton button) { | ||||||||||
| int toggleMouseErr(bool down, MMMouseButton button) { | ||||||||||
| #if defined(IS_MACOSX) | ||||||||||
| const CGPoint currentPos = CGPointFromMMPointInt32(location()); | ||||||||||
| const CGEventType mouseType = MMMouseToCGEventType(down, button); | ||||||||||
| CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); | ||||||||||
| CGEventRef event = CGEventCreateMouseEvent(source, mouseType, currentPos, (CGMouseButton)button); | ||||||||||
|
|
||||||||||
| if (event == NULL) { | ||||||||||
| CFRelease(source); | ||||||||||
| return (int)kCGErrorCannotComplete; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| CGEventPost(kCGHIDEventTap, event); | ||||||||||
| CFRelease(event); | ||||||||||
| CFRelease(source); | ||||||||||
|
|
||||||||||
| return 0; | ||||||||||
| #elif defined(USE_X11) | ||||||||||
| Display *display = XGetMainDisplay(); | ||||||||||
| XTestFakeButtonEvent(display, button, down ? True : False, CurrentTime); | ||||||||||
| Status status = XTestFakeButtonEvent(display, button, down ? True : False, CurrentTime); | ||||||||||
| XSync(display, false); | ||||||||||
|
|
||||||||||
| return status ? 0 : 1; | ||||||||||
| #elif defined(IS_WINDOWS) | ||||||||||
| // mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0); | ||||||||||
| INPUT mouseInput; | ||||||||||
|
|
@@ -181,18 +190,31 @@ void toggleMouse(bool down, MMMouseButton button) { | |||||||||
| mouseInput.mi.time = 0; | ||||||||||
| mouseInput.mi.dwExtraInfo = 0; | ||||||||||
| mouseInput.mi.mouseData = 0; | ||||||||||
| SendInput(1, &mouseInput, sizeof(mouseInput)); | ||||||||||
| UINT sent = SendInput(1, &mouseInput, sizeof(mouseInput)); | ||||||||||
| return sent == 1 ? 0 : (int)GetLastError(); | ||||||||||
| #endif | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void clickMouse(MMMouseButton button){ | ||||||||||
| toggleMouse(true, button); | ||||||||||
| void toggleMouse(bool down, MMMouseButton button) { | ||||||||||
| toggleMouseErr(down, button); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| int clickMouseErr(MMMouseButton button){ | ||||||||||
| int err = toggleMouseErr(true, button); | ||||||||||
| if (err != 0) { | ||||||||||
| return err; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| microsleep(5.0); | ||||||||||
| toggleMouse(false, button); | ||||||||||
|
|
||||||||||
| return toggleMouseErr(false, button); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* Special function for sending double clicks, needed for MacOS. */ | ||||||||||
| void doubleClick(MMMouseButton button){ | ||||||||||
| void clickMouse(MMMouseButton button){ | ||||||||||
| clickMouseErr(button); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| int doubleClickErr(MMMouseButton button){ | ||||||||||
| #if defined(IS_MACOSX) | ||||||||||
| /* Double click for Mac. */ | ||||||||||
| const CGPoint currentPos = CGPointFromMMPointInt32(location()); | ||||||||||
|
|
@@ -202,6 +224,11 @@ void doubleClick(MMMouseButton button){ | |||||||||
| CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); | ||||||||||
| CGEventRef event = CGEventCreateMouseEvent(source, mouseTypeDown, currentPos, kCGMouseButtonLeft); | ||||||||||
|
Comment on lines
224
to
225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded Line 225 uses 🔎 Suggested fix- CGEventRef event = CGEventCreateMouseEvent(source, mouseTypeDown, currentPos, kCGMouseButtonLeft);
+ CGEventRef event = CGEventCreateMouseEvent(source, mouseTypeDown, currentPos, (CGMouseButton)button);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| if (event == NULL) { | ||||||||||
| CFRelease(source); | ||||||||||
| return (int)kCGErrorCannotComplete; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* Set event to double click. */ | ||||||||||
| CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2); | ||||||||||
| CGEventPost(kCGHIDEventTap, event); | ||||||||||
|
|
@@ -211,14 +238,26 @@ void doubleClick(MMMouseButton button){ | |||||||||
|
|
||||||||||
| CFRelease(event); | ||||||||||
| CFRelease(source); | ||||||||||
|
|
||||||||||
| return 0; | ||||||||||
| #else | ||||||||||
| /* Double click for everything else. */ | ||||||||||
| clickMouse(button); | ||||||||||
| int err = clickMouseErr(button); | ||||||||||
| if (err != 0) { | ||||||||||
| return err; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| microsleep(200); | ||||||||||
| clickMouse(button); | ||||||||||
|
|
||||||||||
| return clickMouseErr(button); | ||||||||||
| #endif | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* Special function for sending double clicks, needed for MacOS. */ | ||||||||||
| void doubleClick(MMMouseButton button){ | ||||||||||
| doubleClickErr(button); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* Function used to scroll the screen in the required direction. */ | ||||||||||
| void scrollMouseXY(int x, int y) { | ||||||||||
| #if defined(IS_WINDOWS) | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing NULL check for X11 display.
XGetMainDisplay()can returnNULLif it fails to open a display (as shown inbase/xdisplay_c.h). CallingXTestFakeButtonEventwith a NULL display will cause undefined behavior or crash.🔎 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents