-
Notifications
You must be signed in to change notification settings - Fork 320
/
cmd_key.c
158 lines (138 loc) · 4.64 KB
/
cmd_key.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
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
#include "xdo_cmd.h"
#include <string.h> /* for strcmp */
/* This function handles all of these commands:
* xdotool key
* xdtoool keyup
* xdotool keydown
*/
int cmd_key(context_t *context) {
int ret = 0;
int i, j;
int c;
char *cmd = *context->argv;
charcodemap_t *active_mods = NULL;
int active_mods_n;
useconds_t key_delay = 12000;
useconds_t repeat_delay = 0;
int repeat = 1;
const char *window_arg = NULL;
int free_arg = 0;
/* Options */
int clear_modifiers = 0;
static struct option longopts[] = {
{ "clearmodifiers", no_argument, NULL, 'c' },
{ "delay", required_argument, NULL, 'd' },
{ "repeat-delay", required_argument, NULL, 'R' },
{ "help", no_argument, NULL, 'h' },
{ "window", required_argument, NULL, 'w' },
{ "repeat", required_argument, NULL, 'r' },
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: %s [options] <keysequence> [keysequence ...]\n"
"--clearmodifiers - clear active keyboard modifiers during keystrokes\n"
"--delay DELAY - Use DELAY milliseconds between keystrokes\n"
"--repeat TIMES - How many times to repeat the key sequence\n"
"--repeat-delay DELAY - DELAY milliseconds between repetitions\n"
"--window WINDOW - send keystrokes to a specific window\n"
"Each keysequence can be any number of modifiers and keys, separated by plus (+)\n"
" For example: alt+r\n"
"\n"
"Any letter or key symbol such as Shift_L, Return, Dollar, a, space are valid,\n"
"including those not currently available on your keyboard.\n"
"\n"
"If no window is given, and there are windows in the stack, %1 is used. Otherwise\n"
"the currently-focused window is used\n";
int option_index;
while ((c = getopt_long_only(context->argc, context->argv, "+d:hcw:",
longopts, &option_index)) != -1) {
switch (c) {
case 'w':
window_arg = strdup(optarg);
free_arg = 1;
break;
case 'c':
clear_modifiers = 1;
break;
case 'h':
printf(usage, cmd);
consume_args(context, context->argc);
return EXIT_SUCCESS;
break;
case 'd':
/* Argument is in milliseconds, keysequence delay is in microseconds. */
key_delay = strtoul(optarg, NULL, 0) * 1000;
break;
case 'r':
repeat = atoi(optarg);
if (repeat < 1) {
fprintf(stderr, "Invalid '--repeat' value given: %s\n", optarg);
return EXIT_FAILURE;
}
break;
case 'R': // --repeat-delay
/* Argument is in milliseconds, keysequence delay is in microseconds. */
repeat_delay = strtoul(optarg, NULL, 0) * 1000;
break;
default:
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
}
consume_args(context, optind);
if (context->argc == 0) {
fprintf(stderr, "You specified the wrong number of args.\n");
fprintf(stderr, usage, cmd);
return 1;
}
/* use %1 if there is a window stack */
if (window_arg == NULL && context->nwindows > 0) {
window_arg = "%1";
}
int (*keyfunc)(const xdo_t *, Window, const char *, useconds_t) = NULL;
if (!strcmp(cmd, "key")) {
keyfunc = xdo_send_keysequence_window;
} else if (!strcmp(cmd, "keyup")) {
keyfunc = xdo_send_keysequence_window_up;
} else if (!strcmp(cmd, "keydown")) {
keyfunc = xdo_send_keysequence_window_down;
} else {
fprintf(stderr, "Unknown command '%s'\n", cmd);
return 1;
}
int max_arg = context->argc;
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);
}
for (j = 0; j < repeat; j++) {
for (i = 0; i < context->argc; i++) {
if (is_command(context->argv[i])) {
max_arg = i;
break;
}
int tmp = keyfunc(context->xdo, window, context->argv[i], key_delay);
if (tmp != 0) {
fprintf(stderr,
"xdo_send_keysequence_window reported an error for string '%s'\n",
context->argv[i]);
}
ret += tmp;
} /* each keysequence */
/* Sleep if --repeat-delay given and not on the last repetition */
if (repeat_delay > 0 && j < (repeat-1)) {
usleep(repeat_delay);
}
} /* repeat */
if (clear_modifiers) {
xdo_set_active_modifiers(context->xdo, window, active_mods, active_mods_n);
free(active_mods);
}
}); /* window_each(...) */
if (free_arg) {
free((char *)window_arg);
}
consume_args(context, max_arg);
return ret;
}