Skip to content

Commit

Permalink
Allow negative coordinates for mouseMove and dragMouse
Browse files Browse the repository at this point in the history
  • Loading branch information
hristoterezov committed Jun 30, 2017
1 parent 6f1ce41 commit bb63305
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* @param event The mouse move event (by ref).
* @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.
Expand All @@ -88,11 +88,11 @@ void calculateDeltas(CGEventRef *event, MMPoint point)
* Move the mouse to a specific point.
* @param point The coordinates to move the mouse to (x, y).
*/
void moveMouse(MMPoint point)
void moveMouse(MMSignedPoint point)
{
#if defined(IS_MACOSX)
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPoint(point),
CGPointFromMMSignedPoint(point),
kCGMouseButtonLeft);

calculateDeltas(&move, point);
Expand Down Expand Up @@ -122,12 +122,12 @@ void moveMouse(MMPoint point)
#endif
}

void dragMouse(MMPoint point, const MMMouseButton button)
void dragMouse(MMSignedPoint point, const MMMouseButton button)
{
#if defined(IS_MACOSX)
const CGEventType dragType = MMMouseDragToCGEventType(button);
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
CGPointFromMMPoint(point),
CGPointFromMMSignedPoint(point),
(CGMouseButton)button);
calculateDeltas(&drag, point);

Expand Down Expand Up @@ -355,7 +355,7 @@ bool smoothlyMoveMouse(MMPoint endPoint)
return false;
}

moveMouse(pos);
moveMouse(MMSignedPointMake((int32_t)pos.x, (int32_t)pos.y));

/* Wait 1 - 3 milliseconds. */
microsleep(DEADBEEF_UNIFORM(1.0, 3.0));
Expand Down
12 changes: 6 additions & 6 deletions src/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
extern "C"
{
#endif
#if defined(IS_MACOSX)
Expand Down Expand Up @@ -49,7 +49,7 @@ extern "C"
#define MMMouseButtonIsValid(button) \
(button == LEFT_BUTTON || button == RIGHT_BUTTON || \
button == CENTER_BUTTON)

enum __MMMouseWheelDirection
{
DIRECTION_DOWN = -1,
Expand All @@ -60,13 +60,13 @@ typedef int MMMouseWheelDirection;
/* Immediately moves the mouse to the given point on-screen.
* It is up to the caller to ensure that this point is within the
* screen boundaries. */
void moveMouse(MMPoint point);
void moveMouse(MMSignedPoint point);

/* 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.
* It is up to the caller to ensure that this point is within the screen
* 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.
* deadbeef_srand() should be called before using this function.
Expand All @@ -88,12 +88,12 @@ void clickMouse(MMMouseButton button);
/* Double clicks the mouse with the given button. */
void doubleClick(MMMouseButton button);

/* Scrolls the mouse in the stated direction.
/* Scrolls the mouse in the stated direction.
* TODO: Add a smoothly scroll mouse next. */
void scrollMouse(int x, int y);

#endif /* MOUSE_H */

#ifdef __cplusplus
}
#endif
#endif
18 changes: 9 additions & 9 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ NAN_METHOD(dragMouse)
return Nan::ThrowError("Invalid number of arguments.");
}

const size_t x = info[0]->Int32Value();
const size_t y = info[1]->Int32Value();
const int32_t x = info[0]->Int32Value();
const int32_t y = info[1]->Int32Value();
MMMouseButton button = LEFT_BUTTON;

if (info.Length() == 3)
Expand All @@ -80,8 +80,8 @@ NAN_METHOD(dragMouse)
}
}

MMPoint point;
point = MMPointMake(x, y);
MMSignedPoint point;
point = MMSignedPointMake(x, y);
dragMouse(point, button);
microsleep(mouseDelay);

Expand All @@ -94,11 +94,11 @@ NAN_METHOD(moveMouse)
{
return Nan::ThrowError("Invalid number of arguments.");
}
size_t x = info[0]->Int32Value();
size_t y = info[1]->Int32Value();
int32_t x = info[0]->Int32Value();
int32_t y = info[1]->Int32Value();

MMPoint point;
point = MMPointMake(x, y);
MMSignedPoint point;
point = MMSignedPointMake(x, y);
moveMouse(point);
microsleep(mouseDelay);

Expand Down Expand Up @@ -247,7 +247,7 @@ NAN_METHOD(scrollMouse)
{
return Nan::ThrowError("Invalid number of arguments.");
}

int x = info[0]->Int32Value();
int y = info[1]->Int32Value();

Expand Down
20 changes: 20 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "os.h"
#include "inline_keywords.h" /* For H_INLINE */
#include <stddef.h>
#include <stdint.h>

/* Some generic, cross-platform types. */

Expand All @@ -15,6 +16,14 @@ struct _MMPoint {

typedef struct _MMPoint MMPoint;


struct _MMSignedPoint {
int32_t x;
int32_t y;
};

typedef struct _MMSignedPoint MMSignedPoint;

struct _MMSize {
size_t width;
size_t height;
Expand All @@ -37,6 +46,14 @@ H_INLINE MMPoint MMPointMake(size_t x, size_t y)
return point;
}

H_INLINE MMSignedPoint MMSignedPointMake(int32_t x, int32_t y)
{
MMSignedPoint point;
point.x = x;
point.y = y;
return point;
}

H_INLINE MMSize MMSizeMake(size_t width, size_t height)
{
MMSize size;
Expand All @@ -60,6 +77,9 @@ H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
#define CGPointFromMMPoint(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointFromCGPoint(p) MMPointMake((size_t)(p).x, (size_t)(p).y)

#define CGPointFromMMSignedPoint(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMSignedPointFromCGPoint(p) MMPointMake((int32_t)(p).x, (int32_t)(p).y)

#elif defined(IS_WINDOWS)

#define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y)
Expand Down

0 comments on commit bb63305

Please sign in to comment.