Skip to content

Commit c794a4c

Browse files
committed
click, mousedown, mouseup: optional position arguments
getmouselocation: optional --window argument
1 parent 7b63eb4 commit c794a4c

File tree

8 files changed

+212
-53
lines changed

8 files changed

+212
-53
lines changed

cmd_click.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ int cmd_click(context_t *context) {
2626
{ 0, 0, 0, 0 },
2727
};
2828
static const char *usage =
29-
"Usage: %s [options] <button>\n"
29+
"Usage: %s [options] <button> [<x> <y>]\n"
3030
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n"
3131
"--window WINDOW - specify a window to send click to\n"
3232
"--repeat REPEATS - number of times to click. Default is 1\n"
3333
"--delay MILLISECONDS - delay in milliseconds between clicks.\n"
3434
" This has no effect if you do not use --repeat.\n"
3535
" Default is 100ms\n"
3636
"\n"
37-
"Button is a button number. Generally, left = 1, middle = 2, \n"
38-
"right = 3, wheel up = 4, wheel down = 5\n";
37+
"Button is a button number. Generally, left = 1, middle = 2,\n"
38+
"right = 3, wheel up = 4, wheel down = 5\n"
39+
"With x and y specified the event is dispatched in given position\n"
40+
"(relative to WINDOW if specified) without moving the mouse pointer.\n";
3941
int option_index;
4042

4143
while ((c = getopt_long_only(context->argc, context->argv, "+cw:h",
@@ -84,14 +86,24 @@ int cmd_click(context_t *context) {
8486
}
8587

8688
button = atoi(context->argv[0]);
89+
consume_args(context, 1);
90+
91+
int x = COORDINATE_NONE;
92+
int y = COORDINATE_NONE;
93+
if (context->argc >= 2) {
94+
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
95+
x = atoi(context->argv[0]);
96+
y = atoi(context->argv[1]);
97+
consume_args(context, 2);
98+
}
8799

88100
window_each(context, window_arg, {
89101
if (clear_modifiers) {
90102
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
91103
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
92104
}
93105

94-
ret = xdo_click_window_multiple(context->xdo, window, button, repeat, delay);
106+
ret = xdo_click_window_multiple(context->xdo, window, button, x, y, repeat, delay);
95107
if (ret != XDO_SUCCESS) {
96108
fprintf(stderr, "xdo_click_window failed on window %ld\n", window);
97109
return ret;
@@ -103,6 +115,6 @@ int cmd_click(context_t *context) {
103115
}
104116
}); /* window_each(...) */
105117

106-
consume_args(context, 1);
118+
free(window_arg);
107119
return ret;
108120
}

cmd_getmouselocation.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
#include "xdo_cmd.h"
22

33
int cmd_getmouselocation(context_t *context) {
4-
int x, y, screen_num;
5-
Window window;
4+
int rx, ry, screen_num;
5+
Window window_under_cursor;
66
int ret;
77
char *cmd = context->argv[0];
8+
char *window_arg = NULL;
89

910
int c;
1011
static struct option longopts[] = {
1112
{ "help", no_argument, NULL, 'h' },
1213
{ "shell", no_argument, NULL, 's' },
1314
{ "prefix", required_argument, NULL, 'p' },
15+
{ "window", required_argument, NULL, 'w' },
1416
{ 0, 0, 0, 0 },
1517
};
1618
static const char *usage =
1719
"Usage: %s [--shell] [--prefix <STR>]\n"
18-
"--shell - output shell variables for use with eval\n"
19-
"--prefix STR - use prefix for shell variables names (max 16 chars) \n";
20+
"--window <windowid> - get mouse location relative to window\n"
21+
"--shell - output shell variables for use with eval\n"
22+
"--prefix STR - use prefix for shell variables names (max 16 chars)\n";
2023
int option_index;
2124
int output_shell = 0;
2225
char out_prefix[17] = {'\0'};
@@ -29,6 +32,9 @@ int cmd_getmouselocation(context_t *context) {
2932
consume_args(context, context->argc);
3033
return EXIT_SUCCESS;
3134
break;
35+
case 'w':
36+
window_arg = strdup(optarg);
37+
break;
3238
case 's':
3339
output_shell = 1;
3440
break;
@@ -44,20 +50,40 @@ int cmd_getmouselocation(context_t *context) {
4450

4551
consume_args(context, optind);
4652

47-
ret = xdo_get_mouse_location2(context->xdo, &x, &y, &screen_num, &window);
53+
ret = xdo_get_mouse_location2(context->xdo, &rx, &ry, &screen_num, &window_under_cursor);
54+
55+
int x = rx;
56+
int y = ry;
57+
58+
int wx = COORDINATE_NONE;
59+
int wy = COORDINATE_NONE;
60+
if (window_arg) {
61+
window_each(context, window_arg, {
62+
xdo_translate_coordinates_to_window(context->xdo, window, rx, ry, &wx, &wy);
63+
x = wx;
64+
y = wy;
65+
}); /* window_each(...) */
66+
}
4867

4968
if (output_shell) {
50-
xdotool_output(context, "%sX=%d", out_prefix, x);
51-
xdotool_output(context, "%sY=%d", out_prefix, y);
69+
xdotool_output(context, "%sX=%d", out_prefix, rx);
70+
xdotool_output(context, "%sY=%d", out_prefix, ry);
71+
if (window_arg) {
72+
xdotool_output(context, "%sWX=%d", out_prefix, wx);
73+
xdotool_output(context, "%sWY=%d", out_prefix, wy);
74+
}
5275
xdotool_output(context, "%sSCREEN=%d", out_prefix, screen_num);
53-
xdotool_output(context, "%sWINDOW=%d", out_prefix, window);
76+
xdotool_output(context, "%sWINDOW=%d", out_prefix, window_under_cursor);
5477
} else {
5578
/* only print if we're the last command */
5679
if (context->argc == 0) {
57-
xdotool_output(context, "x:%d y:%d screen:%d window:%ld", x, y, screen_num, window);
80+
xdotool_output(context, "x:%d y:%d screen:%d window:%ld", x, y, screen_num, window_under_cursor);
5881
}
59-
window_save(context, window);
82+
window_save(context, window_under_cursor);
6083
}
84+
85+
free(window_arg);
86+
6187
return ret;
6288
}
6389

cmd_mousedown.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ int cmd_mousedown(context_t *context) {
1818
{ 0, 0, 0, 0 },
1919
};
2020
static const char *usage =
21-
"Usage: %s [--clearmodifiers] [--window WINDOW] <button>\n"
21+
"Usage: %s [--clearmodifiers] [--window WINDOW] <button> [<x> <y>]\n"
2222
"--window <windowid> - specify a window to send keys to\n"
23-
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n";
23+
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n"
24+
"\n"
25+
"With x and y specified the event is dispatched in given position\n"
26+
"(relative to WINDOW if specified) without moving the mouse pointer.\n";
2427
int option_index;
2528

2629
while ((c = getopt_long_only(context->argc, context->argv, "+chw:",
@@ -52,14 +55,24 @@ int cmd_mousedown(context_t *context) {
5255
}
5356

5457
button = atoi(context->argv[0]);
58+
consume_args(context, 1);
59+
60+
int x = COORDINATE_NONE;
61+
int y = COORDINATE_NONE;
62+
if (context->argc >= 2) {
63+
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
64+
x = atoi(context->argv[0]);
65+
y = atoi(context->argv[1]);
66+
consume_args(context, 2);
67+
}
5568

5669
window_each(context, window_arg, {
5770
if (clear_modifiers) {
5871
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
5972
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
6073
}
6174

62-
ret = xdo_mouse_down(context->xdo, window, button);
75+
ret = xdo_mouse_down(context->xdo, window, button, x, y);
6376

6477
if (clear_modifiers) {
6578
xdo_set_active_modifiers(context->xdo, window, active_mods, active_mods_n);
@@ -72,8 +85,6 @@ int cmd_mousedown(context_t *context) {
7285
}
7386
}); /* window_each(...) */
7487

75-
consume_args(context, 1);
7688
free(window_arg);
77-
7889
return ret;
7990
}

cmd_mouseup.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ int cmd_mouseup(context_t *context) {
1818
{ 0, 0, 0, 0 },
1919
};
2020
static const char *usage =
21-
"Usage: %s [--clearmodifiers] [--window WINDOW] <button>\n"
21+
"Usage: %s [--clearmodifiers] [--window WINDOW] <button> [<x> <y>]\n"
2222
"--window <windowid> - specify a window to send keys to\n"
23-
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n";
23+
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n"
24+
"\n"
25+
"With x and y specified the event is dispatched in given position\n"
26+
"(relative to WINDOW if specified) without moving the mouse pointer.\n";
2427
int option_index;
2528

2629
while ((c = getopt_long_only(context->argc, context->argv, "+cw:h",
@@ -52,14 +55,24 @@ int cmd_mouseup(context_t *context) {
5255
}
5356

5457
button = atoi(context->argv[0]);
58+
consume_args(context, 1);
59+
60+
int x = COORDINATE_NONE;
61+
int y = COORDINATE_NONE;
62+
if (context->argc >= 2) {
63+
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
64+
x = atoi(context->argv[0]);
65+
y = atoi(context->argv[1]);
66+
consume_args(context, 2);
67+
}
5568

5669
window_each(context, window_arg, {
5770
if (clear_modifiers) {
5871
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
5972
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
6073
}
6174

62-
ret = xdo_mouse_up(context->xdo, window, button);
75+
ret = xdo_mouse_up(context->xdo, window, button, x, y);
6376

6477
if (clear_modifiers) {
6578
xdo_set_active_modifiers(context->xdo, window, active_mods, active_mods_n);
@@ -73,7 +86,6 @@ int cmd_mouseup(context_t *context) {
7386
}); /* window_each(...) */
7487

7588
free(window_arg);
76-
consume_args(context, 1);
7789
return ret;
7890
}
7991

0 commit comments

Comments
 (0)