Skip to content

Commit

Permalink
click, mousedown, mouseup: optional position arguments
Browse files Browse the repository at this point in the history
getmouselocation: optional --window argument
  • Loading branch information
phd committed Feb 24, 2024
1 parent 7b63eb4 commit b3a508d
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 53 deletions.
22 changes: 17 additions & 5 deletions cmd_click.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ int cmd_click(context_t *context) {
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s [options] <button>\n"
"Usage: %s [options] <button> [<x> <y>]\n"
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n"
"--window WINDOW - specify a window to send click to\n"
"--repeat REPEATS - number of times to click. Default is 1\n"
"--delay MILLISECONDS - delay in milliseconds between clicks.\n"
" This has no effect if you do not use --repeat.\n"
" Default is 100ms\n"
"\n"
"Button is a button number. Generally, left = 1, middle = 2, \n"
"right = 3, wheel up = 4, wheel down = 5\n";
"Button is a button number. Generally, left = 1, middle = 2,\n"
"right = 3, wheel up = 4, wheel down = 5\n"
"With x and y specified the event is dispatched in given position\n"
"(relative to WINDOW if specified) without moving the mouse pointer.\n";
int option_index;

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

button = atoi(context->argv[0]);
consume_args(context, 1);

int x = COORDINATE_NONE;
int y = COORDINATE_NONE;
if (context->argc >= 2) {
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
x = atoi(context->argv[0]);
y = atoi(context->argv[1]);
consume_args(context, 2);
}

window_each(context, window_arg, {
if (clear_modifiers) {
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
}

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

consume_args(context, 1);
free(window_arg);
return ret;
}
46 changes: 36 additions & 10 deletions cmd_getmouselocation.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#include "xdo_cmd.h"

int cmd_getmouselocation(context_t *context) {
int x, y, screen_num;
Window window;
int rx, ry, screen_num;
Window window_under_cursor;
int ret;
char *cmd = context->argv[0];
char *window_arg = NULL;

int c;
static struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "shell", no_argument, NULL, 's' },
{ "prefix", required_argument, NULL, 'p' },
{ "window", required_argument, NULL, 'w' },
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s [--shell] [--prefix <STR>]\n"
"--shell - output shell variables for use with eval\n"
"--prefix STR - use prefix for shell variables names (max 16 chars) \n";
"--window <windowid> - get mouse location relative to window\n"
"--shell - output shell variables for use with eval\n"
"--prefix STR - use prefix for shell variables names (max 16 chars)\n";
int option_index;
int output_shell = 0;
char out_prefix[17] = {'\0'};
Expand All @@ -29,6 +32,9 @@ int cmd_getmouselocation(context_t *context) {
consume_args(context, context->argc);
return EXIT_SUCCESS;
break;
case 'w':
window_arg = strdup(optarg);
break;
case 's':
output_shell = 1;
break;
Expand All @@ -44,20 +50,40 @@ int cmd_getmouselocation(context_t *context) {

consume_args(context, optind);

ret = xdo_get_mouse_location2(context->xdo, &x, &y, &screen_num, &window);
ret = xdo_get_mouse_location2(context->xdo, &rx, &ry, &screen_num, &window_under_cursor);

int x = rx;
int y = ry;

int wx = COORDINATE_NONE;
int wy = COORDINATE_NONE;
if (window_arg) {
window_each(context, window_arg, {
xdo_translate_coordinates_to_window(context->xdo, window, rx, ry, &wx, &wy);
x = wx;
y = wy;
}); /* window_each(...) */
}

if (output_shell) {
xdotool_output(context, "%sX=%d", out_prefix, x);
xdotool_output(context, "%sY=%d", out_prefix, y);
xdotool_output(context, "%sX=%d", out_prefix, rx);
xdotool_output(context, "%sY=%d", out_prefix, ry);
if (window_arg) {
xdotool_output(context, "%sWX=%d", out_prefix, wx);
xdotool_output(context, "%sWY=%d", out_prefix, wy);
}
xdotool_output(context, "%sSCREEN=%d", out_prefix, screen_num);
xdotool_output(context, "%sWINDOW=%d", out_prefix, window);
xdotool_output(context, "%sWINDOW=%d", out_prefix, window_under_cursor);
} else {
/* only print if we're the last command */
if (context->argc == 0) {
xdotool_output(context, "x:%d y:%d screen:%d window:%ld", x, y, screen_num, window);
xdotool_output(context, "x:%d y:%d screen:%d window:%ld", x, y, screen_num, window_under_cursor);
}
window_save(context, window);
window_save(context, window_under_cursor);
}

free(window_arg);

return ret;
}

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

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

button = atoi(context->argv[0]);
consume_args(context, 1);

int x = COORDINATE_NONE;
int y = COORDINATE_NONE;
if (context->argc >= 2) {
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
x = atoi(context->argv[0]);
y = atoi(context->argv[1]);
consume_args(context, 2);
}

window_each(context, window_arg, {
if (clear_modifiers) {
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
}

ret = xdo_mouse_down(context->xdo, window, button);
ret = xdo_mouse_down(context->xdo, window, button, x, y);

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

consume_args(context, 1);
free(window_arg);

return ret;
}
20 changes: 16 additions & 4 deletions cmd_mouseup.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ int cmd_mouseup(context_t *context) {
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s [--clearmodifiers] [--window WINDOW] <button>\n"
"Usage: %s [--clearmodifiers] [--window WINDOW] <button> [<x> <y>]\n"
"--window <windowid> - specify a window to send keys to\n"
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n";
"--clearmodifiers - reset active modifiers (alt, etc) while typing\n"
"\n"
"With x and y specified the event is dispatched in given position\n"
"(relative to WINDOW if specified) without moving the mouse pointer.\n";
int option_index;

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

button = atoi(context->argv[0]);
consume_args(context, 1);

int x = COORDINATE_NONE;
int y = COORDINATE_NONE;
if (context->argc >= 2) {
if (is_numeric(context->argv[0]) && is_numeric(context->argv[1]))
x = atoi(context->argv[0]);
y = atoi(context->argv[1]);
consume_args(context, 2);
}

window_each(context, window_arg, {
if (clear_modifiers) {
xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n);
}

ret = xdo_mouse_up(context->xdo, window, button);
ret = xdo_mouse_up(context->xdo, window, button, x, y);

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

free(window_arg);
consume_args(context, 1);
return ret;
}

Loading

0 comments on commit b3a508d

Please sign in to comment.