diff --git a/README.md b/README.md index 18ed3cd6..c5e49d3c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +This properly fixes the multi-monitor support for windows(tested only on 10, assuming 7+ will work)

+Also supports user defined speed of smooth mouse movement (original randomness updated to alter a bit based on distance)

+=================Original ReadMe To Follow==================

@@ -153,8 +156,7 @@ Soon! This is a bit more complicated than the rest of the features, so I saved i We've been implementing keys as we need them. Feel free to create an issue or submit a pull request! #### How about multi-monitor support? - -The library doesn't have explicit multi-monitor support, so anything that works is kind of on accident. Subscribe to [#88](https://github.com/octalmage/robotjs/issues/88) for updates. +Multiple monitor support working. Tested in windows only, need additional feedback for mac/nix. For any other questions please [submit an issue](https://github.com/octalmage/robotjs/issues/new). diff --git a/src/mouse.c b/src/mouse.c index f7bb11d6..3509d1cd 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -132,8 +132,8 @@ void moveMouse(MMSignedPoint point) //Mouse motion is now done using SendInput with MOUSEINPUT. We use Absolute mouse positioning #define MOUSE_COORD_TO_ABS(coord, width_or_height) ((65536 * (coord) / width_or_height) + ((coord) < 0 ? -1 : 1)) - size_t x = MOUSE_COORD_TO_ABS(point.x-vscreenMinX, vscreenWidth); - size_t y = MOUSE_COORD_TO_ABS(point.y-vscreenMinY, vscreenHeight); + int32_t x = MOUSE_COORD_TO_ABS(point.x - vscreenMinX, vscreenWidth); + int32_t y = MOUSE_COORD_TO_ABS(point.y - vscreenMinY, vscreenHeight); INPUT mouseInput = {0}; mouseInput.type = INPUT_MOUSE; @@ -162,7 +162,7 @@ void dragMouse(MMSignedPoint point, const MMMouseButton button) #endif } -MMPoint getMousePos() +MMSignedPoint getMousePos() { #if defined(IS_MACOSX) CGEventRef event = CGEventCreate(NULL); @@ -185,7 +185,7 @@ MMPoint getMousePos() POINT point; GetCursorPos(&point); - return MMPointFromPOINT(point); + return MMSignedPointFromPOINT(point); #endif } @@ -371,15 +371,17 @@ static double crude_hypot(double x, double y) return ((M_SQRT2 - 1.0) * small) + big; } -bool smoothlyMoveMouse(MMPoint endPoint,double speed) +bool smoothlyMoveMouse(MMPoint endPoint, double speed) { - MMPoint pos = getMousePos(); - MMSize screenSize = getMainDisplaySize(); + MMSignedPoint pos = getMousePos(); + MMSignedSize screenSize = getMainDisplaySize(); double velo_x = 0.0, velo_y = 0.0; double distance; - + if (vscreenWidth < 0 || vscreenHeight < 0) + updateScreenMetrics(); + double bdist = (distance = crude_hypot((double)pos.x - endPoint.x,(double)pos.y - endPoint.y)); while ((distance = crude_hypot((double)pos.x - endPoint.x, - (double)pos.y - endPoint.y)) > 1.0) { + (double)pos.y - endPoint.y)) > 1.0) { double gravity = DEADBEEF_UNIFORM(5.0, 500.0); double veloDistance; velo_x += (gravity * ((double)endPoint.x - pos.x)) / distance; @@ -399,10 +401,10 @@ bool smoothlyMoveMouse(MMPoint endPoint,double speed) return false; } - moveMouse(MMSignedPointMake((int32_t)pos.x, (int32_t)pos.y)); + moveMouse(MMSignedPointMake(pos.x, pos.y)); /* Wait 1 - (speed) milliseconds. */ - microsleep(DEADBEEF_UNIFORM(0.7, speed)); + microsleep(DEADBEEF_UNIFORM((min(0.7,speed) + (1-(distance / ((bdist + 0.0001) * 2)))), (max(0.7, speed) - (distance / ((bdist + 0.0001) * 1.5))))); } return true; diff --git a/src/mouse.h b/src/mouse.h index 764f876c..a8135ffa 100644 --- a/src/mouse.h +++ b/src/mouse.h @@ -82,7 +82,7 @@ void dragMouse(MMSignedPoint point, const MMMouseButton button); bool smoothlyMoveMouse(MMPoint point,double speed); /* Returns the coordinates of the mouse on the current screen. */ -MMPoint getMousePos(void); +MMSignedPoint getMousePos(void); /* Holds down or releases the mouse with the given button in the current * position. */ diff --git a/src/robotjs.cc b/src/robotjs.cc index f96d98fc..8ecf114f 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -140,7 +140,7 @@ NAN_METHOD(moveMouseSmooth) NAN_METHOD(getMousePos) { - MMPoint pos = getMousePos(); + MMSignedPoint pos = getMousePos(); //Return object with .x and .y. Local obj = Nan::New(); @@ -713,7 +713,7 @@ NAN_METHOD(getPixelColor) NAN_METHOD(getScreenSize) { //Get display size. - MMSize displaySize = getMainDisplaySize(); + MMSignedSize displaySize = getMainDisplaySize(); //Create our return object. Local obj = Nan::New(); @@ -770,7 +770,7 @@ NAN_METHOD(captureScreen) y = 0; //Get screen size. - MMSize displaySize = getMainDisplaySize(); + MMSignedSize displaySize = getMainDisplaySize(); w = displaySize.width; h = displaySize.height; } diff --git a/src/screen.c b/src/screen.c index e43b6fda..7ffe5cb6 100644 --- a/src/screen.c +++ b/src/screen.c @@ -8,7 +8,7 @@ #include "xdisplay.h" #endif -MMSize getMainDisplaySize(void) +MMSignedSize getMainDisplaySize(void) { #if defined(IS_MACOSX) CGDirectDisplayID displayID = CGMainDisplayID(); @@ -21,13 +21,13 @@ MMSize getMainDisplaySize(void) return MMSizeMake((size_t)DisplayWidth(display, screen), (size_t)DisplayHeight(display, screen)); #elif defined(IS_WINDOWS) - return MMSizeMake((size_t)GetSystemMetrics(SM_CXSCREEN), - (size_t)GetSystemMetrics(SM_CYSCREEN)); + return MMSignedSizeMake((size_t)GetSystemMetrics(SM_CXVIRTUALSCREEN), + (size_t)GetSystemMetrics(SM_CYVIRTUALSCREEN)); #endif } bool pointVisibleOnMainDisplay(MMPoint point) { - MMSize displaySize = getMainDisplaySize(); + MMSignedSize displaySize = getMainDisplaySize(); return point.x < displaySize.width && point.y < displaySize.height; } diff --git a/src/screen.h b/src/screen.h index 68860ff9..c16d259b 100644 --- a/src/screen.h +++ b/src/screen.h @@ -16,7 +16,7 @@ extern "C" #endif /* Returns the size of the main display. */ -MMSize getMainDisplaySize(void); +MMSignedSize getMainDisplaySize(void); /* Convenience function that returns whether the given point is in the bounds * of the main screen. */ diff --git a/src/types.h b/src/types.h index a7713dc1..c3b6d928 100644 --- a/src/types.h +++ b/src/types.h @@ -31,6 +31,13 @@ struct _MMSize { typedef struct _MMSize MMSize; +struct _MMSignedSize { + int32_t width; + int32_t height; +}; + +typedef struct _MMSignedSize MMSignedSize; + struct _MMRect { MMPoint origin; MMSize size; @@ -61,6 +68,13 @@ H_INLINE MMSize MMSizeMake(size_t width, size_t height) size.height = height; return size; } +H_INLINE MMSignedSize MMSignedSizeMake(int32_t width, int32_t height) +{ + MMSignedSize size; + size.width = width; + size.height = height; + return size; +} H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height) { @@ -83,6 +97,7 @@ H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height) #elif defined(IS_WINDOWS) #define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y) +#define MMSignedPointFromPOINT(p) MMSignedPointMake((int32_t)p.x, (int32_t)p.y) #endif