-
Notifications
You must be signed in to change notification settings - Fork 316
/
cmd_mousemove_relative.c
125 lines (109 loc) · 3.59 KB
/
cmd_mousemove_relative.c
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
#include "xdo_cmd.h"
#include <math.h>
int cmd_mousemove_relative(context_t *context) {
int x, y;
int ret = 0;
char *cmd = *context->argv;
int polar_coordinates = 0;
int clear_modifiers = 0;
int opsync = 0;
int origin_x = -1, origin_y = -1;
charcodemap_t *active_mods = NULL;
int active_mods_n;
int c;
enum {
opt_unused, opt_help, opt_sync, opt_clearmodifiers, opt_polar
};
static struct option longopts[] = {
{ "help", no_argument, NULL, opt_help },
{ "sync", no_argument, NULL, opt_sync },
{ "polar", no_argument, NULL, opt_polar },
{ "clearmodifiers", no_argument, NULL, opt_clearmodifiers },
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s [options] <x> <y>\n"
"-c, --clearmodifiers - reset active modifiers (alt, etc) while typing\n"
"-p, --polar - Use polar coordinates. X as an angle, Y as distance\n"
"--sync - only exit once the mouse has moved\n"
"\n"
"Using polar coordinate mode makes 'x' the angle (in degrees) and\n"
"'y' the distance.\n"
"\n"
"If you want to use negative numbers for a coordinate, you'll need to\n"
"invoke it this way (with the '--'):\n"
" %s -- -20 -15\n"
"otherwise, normal usage looks like this:\n"
" %s 100 140\n";
int option_index;
while ((c = getopt_long_only(context->argc, context->argv, "+cph",
longopts, &option_index)) != -1) {
switch (c) {
case 'h':
case opt_help:
printf(usage, cmd, cmd, cmd);
consume_args(context, context->argc);
return EXIT_SUCCESS;
break;
case 'p':
case opt_polar:
polar_coordinates = 1;
break;
case opt_sync:
opsync = 1;
break;
case 'c':
case opt_clearmodifiers:
clear_modifiers = 1;
break;
default:
fprintf(stderr, usage, cmd, cmd, cmd);
return EXIT_FAILURE;
}
}
consume_args(context, optind);
if (context->argc < 2) {
fprintf(stderr, usage, cmd, cmd, cmd);
fprintf(stderr, "You specified the wrong number of args (expected 2).\n");
return EXIT_FAILURE;
}
x = atoi(context->argv[0]);
y = atoi(context->argv[1]);
consume_args(context, 2);
/* Quit early if we don't have to move. */
if (x == 0 && y == 0) {
return EXIT_SUCCESS;
}
if (polar_coordinates) {
/* The original request for polar support was that '0' degrees is up
* and that rotation was clockwise, so 0 is up, 90 right, 180 down, 270
* left. This conversion can be done with (360 - degrees) + 90 */
double radians = ((360 - x) + 90) * (M_PI / 180);
double distance = y;
x = (cos(radians) * distance);
/* Negative sin, since screen Y coordinates are descending, where cartesian
* is ascending */
y = (-sin(radians) * distance);
}
if (clear_modifiers) {
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
xdo_clear_active_modifiers(context->xdo, CURRENTWINDOW, active_mods, active_mods_n);
}
if (opsync) {
xdo_get_mouse_location(context->xdo, &origin_x, &origin_y, NULL);
}
ret = xdo_move_mouse_relative(context->xdo, x, y);
if (ret) {
fprintf(stderr, "xdo_move_mouse_relative reported an error\n");
} else {
if (opsync) {
/* Wait until the mouse moves away from its current position */
xdo_wait_for_mouse_move_from(context->xdo, origin_x, origin_y);
}
}
if (clear_modifiers) {
xdo_set_active_modifiers(context->xdo, CURRENTWINDOW, active_mods, active_mods_n);
free(active_mods);
}
return ret;
}