Repository navigation
Expand file tree
/
Copy pathmouse.c
More file actions
186 lines (155 loc) · 5.56 KB
/
Copy pathmouse.c
File metadata and controls
186 lines (155 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "mouse.h"
#include "screen.h"
#include "deadbeef_rand.h"
#include "microsleep.h"
#include <math.h> /* For floor() */
#if defined(IS_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#elif defined(USE_X11)
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include "xdisplay.h"
#endif
#if !defined(M_SQRT2)
#define M_SQRT2 1.4142135623730950488016887 /* Fix for MSVC. */
#endif
void moveMouse(MMPoint point)
{
#if defined(IS_MACOSX)
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPoint(point),
0);
CGEventPost(kCGSessionEventTap, move);
CFRelease(move);
#elif defined(USE_X11)
Display *display = XGetMainDisplay();
XWarpPointer(display, None, DefaultRootWindow(display),
0, 0, 0, 0, point.x, point.y);
XFlush(display);
#elif defined(IS_WINDOWS)
point.x *= 0xFFFF / GetSystemMetrics(SM_CXSCREEN);
point.y *= 0xFFFF / GetSystemMetrics(SM_CYSCREEN);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
(DWORD)point.x, (DWORD)point.y, 0, 0);
#endif
}
MMPoint getMousePos()
{
#if defined(IS_MACOSX)
CGEventRef event = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(event);
CFRelease(event);
return MMPointFromCGPoint(point);
#elif defined(USE_X11)
int x, y; /* This is all we care about. Seriously. */
Window garb1, garb2; /* Why you can't specify NULL as a parameter */
int garb_x, garb_y; /* is beyond me. */
unsigned int more_garbage;
Display *display = XGetMainDisplay();
XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2,
&x, &y, &garb_x, &garb_y, &more_garbage);
return MMPointMake(x, y);
#elif defined(IS_WINDOWS)
POINT point;
GetCursorPos(&point);
return MMPointFromPOINT(point);
#endif
}
/* Some convenience macros for converting our enums to the system API types. */
#if defined(IS_MACOSX)
#define MMMouseToCGEventType(down, button) \
(down ? MMMouseDownToCGEventType(button) : MMMouseUpToCGEventType(button))
#define MMMouseDownToCGEventType(button) \
((button) == (LEFT_BUTTON) ? kCGEventLeftMouseDown \
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseDown \
: kCGEventOtherMouseDown))
#define MMMouseUpToCGEventType(button) \
((button) == LEFT_BUTTON ? kCGEventLeftMouseUp \
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseUp \
: kCGEventOtherMouseUp))
#elif defined(IS_WINDOWS)
#define MMMouseToMEventF(down, button) \
(down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button))
#define MMMouseUpToMEventF(button) \
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTUP \
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTUP \
: MOUSEEVENTF_MIDDLEUP))
#define MMMouseDownToMEventF(button) \
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN \
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
: MOUSEEVENTF_MIDDLEDOWN))
#endif
void toggleMouse(bool down, MMMouseButton button)
{
#if defined(IS_MACOSX)
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL,
mouseType,
currentPos,
(CGMouseButton)button);
CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
#elif defined(USE_X11)
Display *display = XGetMainDisplay();
XTestFakeButtonEvent(display, button, down ? True : False, CurrentTime);
XFlush(display);
#elif defined(IS_WINDOWS)
mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0);
#endif
}
void clickMouse(MMMouseButton button)
{
toggleMouse(true, button);
toggleMouse(false, button);
}
/*
* A crude, fast hypot() approximation to get around the fact that hypot() is
* not a standard ANSI C function.
*
* It is not particularly accurate but that does not matter for our use case.
*
* Taken from this StackOverflow answer:
* http://stackoverflow.com/questions/3506404/fast-hypotenuse-algorithm-for-embedded-processor#3507882
*
*/
static double crude_hypot(double x, double y)
{
double big = fabs(x); /* max(|x|, |y|) */
double small = fabs(y); /* min(|x|, |y|) */
if (big > small) {
double temp = big;
big = small;
small = temp;
}
return ((M_SQRT2 - 1.0) * small) + big;
}
bool smoothlyMoveMouse(MMPoint endPoint)
{
MMPoint pos = getMousePos();
MMSize screenSize = getMainDisplaySize();
double velo_x = 0.0, velo_y = 0.0;
double distance;
while ((distance = crude_hypot((double)pos.x - endPoint.x,
(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;
velo_y += (gravity * ((double)endPoint.y - pos.y)) / distance;
/* Normalize velocity to get a unit vector of length 1. */
veloDistance = crude_hypot(velo_x, velo_y);
velo_x /= veloDistance;
velo_y /= veloDistance;
pos.x += floor(velo_x + 0.5);
pos.y += floor(velo_y + 0.5);
/* Make sure we are in the screen boundaries!
* (Strange things will happen if we are not.) */
if (pos.x >= screenSize.width || pos.y >= screenSize.height) {
return false;
}
moveMouse(pos);
/* Wait 1 - 3 milliseconds. */
microsleep(DEADBEEF_UNIFORM(1.0, 3.0));
}
return true;
}