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

Changed mouseScroll to use X and Y as direction. #194

Merged
merged 11 commits into from
Feb 23, 2017
132 changes: 68 additions & 64 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void moveMouse(MMPoint point)
mouseInput.mi.dwExtraInfo = 0;
mouseInput.mi.mouseData = 0;
SendInput(1, &mouseInput, sizeof(mouseInput));

#endif
}

Expand Down Expand Up @@ -202,101 +202,105 @@ void clickMouse(MMMouseButton button)
*/
void doubleClick(MMMouseButton button)
{

#if defined(IS_MACOSX)

/* Double click for Mac. */
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);

CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
/* Set event to double click. */
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);

/* Set event to double click. */
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);

CGEventPost(kCGHIDEventTap, event);

CGEventSetType(event, mouseTypeUP);
CGEventPost(kCGHIDEventTap, event);

CFRelease(event);
CGEventPost(kCGHIDEventTap, event);

CGEventSetType(event, mouseTypeUP);
CGEventPost(kCGHIDEventTap, event);

CFRelease(event);

#else

/* Double click for everything else. */
clickMouse(button);
microsleep(200);
clickMouse(button);

#endif
}

/**
* Function used to scroll the screen in the required direction.
* This uses the magnitude to scroll the required amount in the direction.
* TODO Requires further fine tuning based on the requirements.
*/
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
void scrollMouse(int x, int y)
{
#if defined(IS_WINDOWS)
// Fix for #97 https://github.com/octalmage/robotjs/issues/97,
// C89 needs variables declared on top of functions (mouseScrollInput)
INPUT mouseScrollInput;
#endif

/* Direction should only be considered based on the scrollDirection. This
* Should not interfere. */
int cleanScrollMagnitude = abs(scrollMagnitude);
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN))
{
return;
}

/* Set up the OS specific solution */
#if defined(__APPLE__)
#if defined(IS_WINDOWS)
// Fix for #97 https://github.com/octalmage/robotjs/issues/97,
// C89 needs variables declared on top of functions (mouseScrollInput)
INPUT mouseScrollInputH;
INPUT mouseScrollInputV;
#endif

CGWheelCount wheel = 1;
CGEventRef event;
/* Direction should only be considered based on the scrollDirection. This
* Should not interfere. */

/* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
/* Set up the OS specific solution */
#if defined(__APPLE__)

event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
CGEventPost(kCGHIDEventTap, event);
CGEventRef event;

#elif defined(USE_X11)
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
CGEventPost(kCGHIDEventTap, event);

int x;
int dir = 4; /* Button 4 is up, 5 is down. */
Display *display = XGetMainDisplay();
CFRelease(event);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant that there are a few places where spaces are used instead of tabs. If you select the characters before CFRelease release you'll see what I mean. I can fix them after this gets merged though, if you'd like!


if (scrollDirection == DIRECTION_DOWN)
{
dir = 5;
}
#elif defined(USE_X11)

for (x = 0; x < cleanScrollMagnitude; x++)
{
XTestFakeButtonEvent(display, dir, 1, CurrentTime);
XTestFakeButtonEvent(display, dir, 0, CurrentTime);
}
int ydir = 4; /* Button 4 is up, 5 is down. */
int xdir = 6;
Display *display = XGetMainDisplay();

XFlush(display);
if (y < 0){
ydir = 5;
}
if (x < 0){
xdir = 7;
}

#elif defined(IS_WINDOWS)
int xi;
int yi;
for (xi = 0; xi < abs(x); xi++) {
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
}
for (yi = 0; yi < abs(y); yi++) {
YTestFakeButtonEvent(display, ydir, 1, CurrentTime);
YTestFakeButtonEvent(display, ydir, 0, CurrentTime);
}

mouseScrollInput.type = INPUT_MOUSE;
mouseScrollInput.mi.dx = 0;
mouseScrollInput.mi.dy = 0;
mouseScrollInput.mi.dwFlags = MOUSEEVENTF_WHEEL;
mouseScrollInput.mi.time = 0;
mouseScrollInput.mi.dwExtraInfo = 0;
mouseScrollInput.mi.mouseData = WHEEL_DELTA * scrollDirection * cleanScrollMagnitude;
XFlush(display);

SendInput(1, &mouseScrollInput, sizeof(mouseScrollInput));
#elif defined(IS_WINDOWS)

#endif
mouseScrollInputH.type = INPUT_MOUSE;
mouseScrollInputH.mi.dx = 0;
mouseScrollInputH.mi.dy = 0;
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_WHEEL;
mouseScrollInputH.mi.time = 0;
mouseScrollInputH.mi.dwExtraInfo = 0;
mouseScrollInputH.mi.mouseData = WHEEL_DELTA * x;

mouseScrollInputV.type = INPUT_MOUSE;
mouseScrollInputV.mi.dx = 0;
mouseScrollInputV.mi.dy = 0;
mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_HWHEEL;
mouseScrollInputV.mi.time = 0;
mouseScrollInputV.mi.dwExtraInfo = 0;
mouseScrollInputV.mi.mouseData = WHEEL_DELTA * y;

SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH));
SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV));
#endif
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void doubleClick(MMMouseButton button);

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

#endif /* MOUSE_H */

Expand Down
Loading