forked from jordansissel/xdotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_search.c
194 lines (178 loc) · 6.06 KB
/
cmd_search.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
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
187
188
189
190
191
192
193
194
#include "xdo_cmd.h"
#include <string.h>
int cmd_search(context_t *context) {
Window *list;
xdo_search_t search;
unsigned int nwindows;
unsigned int i;
int c;
int op_sync = False;
int search_title = 0;
int search_name = 0;
int search_class = 0;
int search_classname = 0;
typedef enum {
opt_unused, opt_title, opt_onlyvisible, opt_name, opt_class, opt_maxdepth,
opt_pid, opt_help, opt_any, opt_all, opt_screen, opt_classname, opt_desktop,
opt_limit, opt_sync
} optlist_t;
struct option longopts[] = {
{ "all", no_argument, NULL, opt_all },
{ "any", no_argument, NULL, opt_any },
{ "class", no_argument, NULL, opt_class },
{ "classname", no_argument, NULL, opt_classname },
{ "help", no_argument, NULL, opt_help },
{ "maxdepth", required_argument, NULL, opt_maxdepth },
{ "name", no_argument, NULL, opt_name },
{ "onlyvisible", 0, NULL, opt_onlyvisible },
{ "pid", required_argument, NULL, opt_pid },
{ "screen", required_argument, NULL, opt_screen },
{ "title", no_argument, NULL, opt_title },
{ "desktop", required_argument, NULL, opt_desktop },
{ "limit", required_argument, NULL, opt_limit },
{ "sync", no_argument, NULL, opt_sync },
{ 0, 0, 0, 0 },
};
static const char *usage =
"Usage: xdotool %s "
"[options] regexp_pattern\n"
"--class check regexp_pattern agains the window class\n"
"--classname check regexp_pattern agains the window classname\n"
"--maxdepth N set search depth to N. Default is infinite.\n"
" -1 also means infinite.\n"
"--onlyvisible matches only windows currently visible\n"
"--pid PID only show windows belonging to specific process\n"
" Not supported by all X11 applications\n"
"--screen N only search a specific screen. Default is all screens\n"
"--desktop N only search a specific desktop number\n"
"--limit N break search after N results\n"
"--name check regexp_pattern agains the window name\n"
"--title DEPRECATED. Same as --name.\n"
"--all Require all conditions match a window. Default is --any\n"
"--any Windows matching any condition will be reported\n"
"--sync Wait until a search result is found.\n"
"-h, --help show this help output\n"
"\n"
"If none of --name, --classname, or --class are specified, the \n"
"defaults are: --name --classname --class\n";
memset(&search, 0, sizeof(xdo_search_t));
search.max_depth = -1;
search.require = SEARCH_ANY;
char *cmd = *context->argv;
int option_index;
while ((c = getopt_long_only(context->argc, context->argv, "+h",
longopts, &option_index)) != -1) {
switch (c) {
case 0:
break;
case 'h':
case opt_help:
printf(usage, cmd);
consume_args(context, context->argc);
return EXIT_SUCCESS;
case opt_maxdepth:
search.max_depth = strtol(optarg, NULL, 0);
break;
case opt_pid:
search.pid = atoi(optarg);
search.searchmask |= SEARCH_PID;
break;
case opt_any:
search.require = SEARCH_ANY;
break;
case opt_all:
search.require = SEARCH_ALL;
break;
case opt_screen:
search.screen = strtoul(optarg, NULL, 0);
search.searchmask |= SEARCH_SCREEN;
break;
case opt_onlyvisible:
search.only_visible = True;
search.searchmask |= SEARCH_ONLYVISIBLE;
break;
case opt_class:
search_class = True;
break;
case opt_classname:
search_classname = True;
break;
case opt_title:
fprintf(stderr, "This flag is deprecated. Assuming you mean --name (the"
" window name).\n");
/* fall through */
case opt_name:
search_name = True;
break;
case opt_desktop:
search.desktop = strtol(optarg, NULL, 0);
search.searchmask |= SEARCH_DESKTOP;
break;
case opt_limit:
search.limit = atoi(optarg);
break;
case opt_sync:
op_sync = True;
break;
default:
fprintf(stderr, "Invalid usage\n");
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
}
consume_args(context, optind);
/* We require a pattern or a pid to search for */
if (context->argc < 1 && search.pid == 0) {
fprintf(stderr, usage, cmd);
return EXIT_FAILURE;
}
if (!search_title && !search_name && !search_class && !search_classname
&& context->argc > 0) {
fprintf(stderr, "Defaulting to search window name, class, and classname\n");
search.searchmask |= (SEARCH_NAME | SEARCH_CLASS | SEARCH_CLASSNAME);
search_name = 1;
search_class = 1;
search_classname = 1;
}
if (context->argc > 0) {
if (search_title) {
search.searchmask |= SEARCH_NAME;
search.winname = context->argv[0];
}
if (search_name) {
search.searchmask |= SEARCH_NAME;
search.winname = context->argv[0];
}
if (search_class) {
search.searchmask |= SEARCH_CLASS;
search.winclass = context->argv[0];
}
if (search_classname) {
search.searchmask |= SEARCH_CLASSNAME;
search.winclassname = context->argv[0];
}
}
consume_args(context, 1);
do {
xdo_search_windows(context->xdo, &search, &list, &nwindows);
if (context->argc == 0) {
/* only print if we're the last command */
for (i = 0; i < nwindows; i++) {
window_print(list[i]);
}
}
if (op_sync && nwindows == 0) {
xdotool_debug(context, "No search results, still waiting...");
/* TODO(sissel): Make this tunable */
usleep(500000);
}
} while (op_sync && nwindows == 0);
/* Free old list as it's malloc'd by xdo_search_windows */
if (context->windows != NULL) {
free(context->windows);
}
context->windows = list;
context->nwindows = nwindows;
/* error if number of windows found is zero (behave like grep) */
return (nwindows ? EXIT_SUCCESS : EXIT_FAILURE);
}