add drag and move mouse multiple screens support

This commit is contained in:
vcaesar 2019-11-30 14:28:38 -04:00
parent a3da8915b9
commit 4df22c5baa
4 changed files with 26 additions and 26 deletions

View File

@ -16,7 +16,7 @@ int mouseDelay = 10;
// int keyboardDelay = 10; // int keyboardDelay = 10;
// int CheckMouseButton(const char * const b, // int CheckMouseButton(const char * const b,
// MMMouseButton * const button){ // MMMouseButton * const button){
// if (!button) return -1; // if (!button) return -1;
@ -35,24 +35,24 @@ int mouseDelay = 10;
// return 0; // return 0;
// } // }
int move_mouse(size_t x, size_t y){ int move_mouse(int32_t x, int32_t y){
MMPoint point; MMSignedPoint point;
// int x = 103; // int x = 103;
// int y = 104; // int y = 104;
point = MMPointMake(x, y); point = MMSignedPointMake(x, y);
moveMouse(point); moveMouse(point);
return 0; return 0;
} }
int drag_mouse(size_t x, size_t y, MMMouseButton button){ int drag_mouse(int32_t x, int32_t y, MMMouseButton button){
// const size_t x = 10; // const size_t x = 10;
// const size_t y = 20; // const size_t y = 20;
// MMMouseButton button = LEFT_BUTTON; // MMMouseButton button = LEFT_BUTTON;
MMPoint point; MMSignedPoint point;
point = MMPointMake(x, y); point = MMSignedPointMake(x, y);
dragMouse(point, button); dragMouse(point, button);
microsleep(mouseDelay); microsleep(mouseDelay);
@ -105,7 +105,7 @@ int mouse_toggle(char* d, MMMouseButton button){
toggleMouse(down, button); toggleMouse(down, button);
microsleep(mouseDelay); microsleep(mouseDelay);
return 0; return 0;
} }

View File

@ -65,20 +65,20 @@ typedef int MMMouseWheelDirection;
/* Immediately moves the mouse to the given point on-screen. /* Immediately moves the mouse to the given point on-screen.
* It is up to the caller to ensure that this point is within the * It is up to the caller to ensure that this point is within the
* screen boundaries. */ * screen boundaries. */
void moveMouse(MMPoint point); void moveMouse(MMSignedPoint point);
/* Like moveMouse, moves the mouse to the given point on-screen, but marks /* Like moveMouse, moves the mouse to the given point on-screen, but marks
* the event as the mouse being dragged on platforms where it is supported. * the event as the mouse being dragged on platforms where it is supported.
* It is up to the caller to ensure that this point is within the screen * It is up to the caller to ensure that this point is within the screen
* boundaries. */ * boundaries. */
void dragMouse(MMPoint point, const MMMouseButton button); void dragMouse(MMSignedPoint point, const MMMouseButton button);
/* Smoothly moves the mouse from the current position to the given point. /* Smoothly moves the mouse from the current position to the given point.
* deadbeef_srand() should be called before using this function. * deadbeef_srand() should be called before using this function.
* *
* Returns false if unsuccessful (i.e. a point was hit that is outside of the * Returns false if unsuccessful (i.e. a point was hit that is outside of the
* screen boundaries), or true if successful. */ * screen boundaries), or true if successful. */
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed); bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed);
// bool smoothlyMoveMouse(MMPoint point); // bool smoothlyMoveMouse(MMPoint point);
/* Returns the coordinates of the mouse on the current screen. */ /* Returns the coordinates of the mouse on the current screen. */

View File

@ -66,7 +66,7 @@
* @param event The mouse move event (by ref). * @param event The mouse move event (by ref).
* @param point The new mouse x and y. * @param point The new mouse x and y.
*/ */
void calculateDeltas(CGEventRef *event, MMPoint point){ void calculateDeltas(CGEventRef *event, MMSignedPoint point){
/** /**
* The next few lines are a workaround for games not detecting mouse moves. * The next few lines are a workaround for games not detecting mouse moves.
* See this issue for more information: * See this issue for more information:
@ -91,10 +91,10 @@ void calculateDeltas(CGEventRef *event, MMPoint point){
* Move the mouse to a specific point. * Move the mouse to a specific point.
* @param point The coordinates to move the mouse to (x, y). * @param point The coordinates to move the mouse to (x, y).
*/ */
void moveMouse(MMPoint point){ void moveMouse(MMSignedPoint point){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPoint(point), CGPointFromMMSignedPoint(point),
kCGMouseButtonLeft); kCGMouseButtonLeft);
calculateDeltas(&move, point); calculateDeltas(&move, point);
@ -108,7 +108,7 @@ void moveMouse(MMPoint point){
XSync(display, false); XSync(display, false);
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
// Mouse motion is now done using SendInput with MOUSEINPUT. // Mouse motion is now done using SendInput with MOUSEINPUT.
// We use Absolute mouse positioning // We use Absolute mouse positioning
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \ #define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1)) ((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))
@ -129,7 +129,7 @@ void moveMouse(MMPoint point){
#endif #endif
} }
void dragMouse(MMPoint point, const MMMouseButton button){ void dragMouse(MMSignedPoint point, const MMMouseButton button){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
const CGEventType dragType = MMMouseDragToCGEventType(button); const CGEventType dragType = MMMouseDragToCGEventType(button);
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType, CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
@ -181,7 +181,7 @@ void toggleMouse(bool down, MMMouseButton button){
const CGEventType mouseType = MMMouseToCGEventType(down, button); const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL, CGEventRef event = CGEventCreateMouseEvent(NULL,
mouseType, currentPos, (CGMouseButton)button); mouseType, currentPos, (CGMouseButton)button);
CGEventPost(kCGSessionEventTap, event); CGEventPost(kCGSessionEventTap, event);
CFRelease(event); CFRelease(event);
#elif defined(USE_X11) #elif defined(USE_X11)
@ -192,7 +192,7 @@ void toggleMouse(bool down, MMMouseButton button){
// mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0); // mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0);
INPUT mouseInput; INPUT mouseInput;
mouseInput.type = INPUT_MOUSE; mouseInput.type = INPUT_MOUSE;
mouseInput.mi.dx = 0; mouseInput.mi.dx = 0;
mouseInput.mi.dy = 0; mouseInput.mi.dy = 0;
@ -272,7 +272,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
/* Make scroll magnitude negative if we're scrolling down. */ /* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection; cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
event = CGEventCreateScrollWheelEvent(NULL, event = CGEventCreateScrollWheelEvent(NULL,
kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0); kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
CGEventPost(kCGHIDEventTap, event); CGEventPost(kCGHIDEventTap, event);
@ -408,10 +408,10 @@ bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
double velo_x = 0.0, velo_y = 0.0; double velo_x = 0.0, velo_y = 0.0;
double distance; double distance;
while ((distance = while ((distance =
crude_hypot((double)pos.x - endPoint.x, (double)pos.y - endPoint.y) crude_hypot((double)pos.x - endPoint.x, (double)pos.y - endPoint.y)
) > 1.0) { ) > 1.0) {
double gravity = DEADBEEF_UNIFORM(5.0, 500.0); double gravity = DEADBEEF_UNIFORM(5.0, 500.0);
// double gravity = DEADBEEF_UNIFORM(lowSpeed, highSpeed); // double gravity = DEADBEEF_UNIFORM(lowSpeed, highSpeed);
double veloDistance; double veloDistance;
@ -432,7 +432,7 @@ bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
return false; return false;
} }
moveMouse(pos); moveMouse(MMSignedPointMake((int32_t)pos.x, (int32_t)pos.y));
/* Wait 1 - 3 milliseconds. */ /* Wait 1 - 3 milliseconds. */
microsleep(DEADBEEF_UNIFORM(lowSpeed, highSpeed)); microsleep(DEADBEEF_UNIFORM(lowSpeed, highSpeed));

View File

@ -359,8 +359,8 @@ func MoveMouse(x, y int) {
// Move move the mouse // Move move the mouse
func Move(x, y int) { func Move(x, y int) {
cx := C.size_t(x) cx := C.int32_t(x)
cy := C.size_t(y) cy := C.int32_t(y)
C.move_mouse(cx, cy) C.move_mouse(cx, cy)
} }
@ -373,8 +373,8 @@ func DragMouse(x, y int, args ...string) {
func Drag(x, y int, args ...string) { func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.size_t(x) cx := C.int32_t(x)
cy := C.size_t(y) cy := C.int32_t(y)
if len(args) > 0 { if len(args) > 0 {
button = CheckMouse(args[0]) button = CheckMouse(args[0])