-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtap-id.c
274 lines (232 loc) · 6.02 KB
/
tap-id.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2022 Codethink
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <cyaml/cyaml.h>
#include "cmd/cmd.h"
#include "cmd/private.h"
#include "msg/msg.h"
#include "util/cli.h"
#include "util/log.h"
#include "util/util.h"
#include "util/decode.h"
/* The log messages arrive as an array of arrays as a JSON string.
* These are the schema to decode that to a `char ***` type. */
static struct tap_id_ctx {
const char *id;
int vw;
int vh;
int msg_id_vw;
int msg_id_vh;
int msg_id_pos;
} tap_id_ctx;
/**
* Construct a JavaScript script to query an element's position.
*
* \param[in] id Identifies which element to get the position of.
* \return Script to execute, or NULL on error.
*/
static char *cmd_tap_id__get_pos_script(const char *id)
{
int written;
char *str = NULL;
static const char *id_position_script =
"JSON.stringify(document"
" .getElementById('%s')"
" .getBoundingClientRect());";
written = asprintf(&str, id_position_script, id);
if (written < 0) {
return NULL;
}
return str;
}
static const struct cli_table_entry cli_entries[] = {
CMD_CLI_COMMON("tap-id"),
{
.p = true,
.l = "ID",
.t = CLI_STRING,
.v.s = &tap_id_ctx.id,
.d = "An element ID attribute to tap."
},
};
static const struct cli_table cli = {
.entries = cli_entries,
.count = (sizeof(cli_entries))/(sizeof(*cli_entries)),
.min_positional = 3,
};
static bool cmd_tap_id_init(int argc, const char **argv,
struct cmd_options *options, void **pw_out)
{
char *script;
if (!cmd_cli_parse(argc, argv, &cli, options)) {
return false;
}
script = cmd_tap_id__get_pos_script(tap_id_ctx.id);
if (script == NULL) {
cdt_log(CDT_LOG_ERROR, "Failed to generate script for id %s",
tap_id_ctx.id);
return false;
}
/* Get the viewport width. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = "window.innerWidth",
},
},
}, &tap_id_ctx.msg_id_vw);
/* Get the viewport height. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = "window.innerHeight",
},
},
}, &tap_id_ctx.msg_id_vh);
/* Send element position acquisition script. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = script,
},
},
}, &tap_id_ctx.msg_id_pos);
free(script);
script = NULL;
*pw_out = NULL;
return true;
}
struct element_pos {
int x;
int y;
int w;
int h;
int b;
int r;
};
static const struct cyaml_schema_field element_pos_fields_schema[] = {
CYAML_FIELD_INT("x", CYAML_FLAG_DEFAULT, struct element_pos, x),
CYAML_FIELD_INT("y", CYAML_FLAG_DEFAULT, struct element_pos, y),
CYAML_FIELD_INT("width", CYAML_FLAG_DEFAULT, struct element_pos, w),
CYAML_FIELD_INT("height", CYAML_FLAG_DEFAULT, struct element_pos, h),
CYAML_FIELD_INT("bottom", CYAML_FLAG_DEFAULT, struct element_pos, b),
CYAML_FIELD_INT("right", CYAML_FLAG_DEFAULT, struct element_pos, r),
CYAML_FIELD_END
};
static const struct cyaml_schema_value element_pos_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct element_pos,
element_pos_fields_schema),
};
static const cyaml_config_t config = {
.flags = CYAML_CFG_IGNORE_UNKNOWN_KEYS,
.log_level = CYAML_LOG_WARNING,
.mem_fn = cyaml_mem,
.log_fn = cyaml_log,
};
static void cmd_tap_id__do_tap(const char *pos_msg)
{
struct element_pos *pos;
cyaml_err_t res;
int id;
res = cyaml_load_data((const uint8_t *)pos_msg, strlen(pos_msg),
&config, &element_pos_schema,
(void **)&pos, NULL);
if (res != CYAML_OK) {
cdt_log(CDT_LOG_ERROR,
"Failed to parse response: %s",
cyaml_strerror(res));
cdt_log(CDT_LOG_ERROR,
"Error could not locate ID: %s",
tap_id_ctx.id);
return;
}
if (pos->x < 0 || pos->r >= tap_id_ctx.vw ||
pos->y < 0 || pos->b >= tap_id_ctx.vh) {
cdt_log(CDT_LOG_ERROR,
"Element '%s' outside viewport!",
tap_id_ctx.id);
cdt_log(CDT_LOG_NOTICE, " Viewport width : %i", tap_id_ctx.vw);
cdt_log(CDT_LOG_NOTICE, " Viewport height : %i", tap_id_ctx.vh);
cdt_log(CDT_LOG_NOTICE, " Element left : %i", pos->x);
cdt_log(CDT_LOG_NOTICE, " Element right : %i", pos->r);
cdt_log(CDT_LOG_NOTICE, " Element top : %i", pos->y);
cdt_log(CDT_LOG_NOTICE, " Element bottom : %i", pos->b);
return;
}
cdt_log(CDT_LOG_NOTICE, "Tapping '%s' at: (%d, %d)",
tap_id_ctx.id,
pos->x + pos->w / 2,
pos->y + pos->h / 2);
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_TOUCH_EVENT_START,
.data = {
.touch_event = {
.x = pos->x + pos->w / 2,
.y = pos->y + pos->h / 2,
},
},
}, &id);
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_TOUCH_EVENT_END,
}, &id);
cyaml_free(&config, &element_pos_schema, pos, 0);
}
static void cmd_tap_id_msg(void *pw, int id, const char *msg, size_t len)
{
(void)(pw);
cdt_log(CDT_LOG_INFO, "Received message with id %i: %*s",
id, (int)len, msg);
if (id == tap_id_ctx.msg_id_vw) {
bool res = decode_extract_response_value_int(msg, len,
&tap_id_ctx.vw);
if (res == false) {
cdt_log(CDT_LOG_ERROR,
"Error: Could not get viewport width");
return;
}
} else if (id == tap_id_ctx.msg_id_vh) {
bool res = decode_extract_response_value_int(msg, len,
&tap_id_ctx.vh);
if (res == false) {
cdt_log(CDT_LOG_ERROR,
"Error: Could not get viewport height");
return;
}
} else if (id == tap_id_ctx.msg_id_pos) {
char *value;
value = decode_extract_response_value(msg, len);
if (value == NULL) {
cdt_log(CDT_LOG_ERROR,
"Error: Could not locate ID: '%s'",
tap_id_ctx.id);
return;
}
cmd_tap_id__do_tap(value);
free(value);
}
}
static void cmd_tap_id_help(int argc, const char **argv);
const struct cmd_table cmd_tap_id = {
.cmd = "tap-id",
.init = cmd_tap_id_init,
.help = cmd_tap_id_help,
.msg = cmd_tap_id_msg,
};
static void cmd_tap_id_help(int argc, const char **argv)
{
cli_help(&cli, (argc > 0) ? argv[0] : "cdt");
}