Skip to content

Commit 36b117c

Browse files
xiaoliudxiaoxiang781216
authored andcommitted
apps/graphics: Refactor monkey using input generator library
Signed-off-by: liuchan3 <liuchan3@xiaomi.com>
1 parent 7bfd5e5 commit 36b117c

File tree

11 files changed

+113
-207
lines changed

11 files changed

+113
-207
lines changed

graphics/input/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ if(CONFIG_GRAPHICS_INPUT_MONKEY)
4141
${CONFIG_GRAPHICS_INPUT_MONKEY_STACKSIZE}
4242
MODULE
4343
${CONFIG_GRAPHICS_INPUT_MONKEY}
44+
DEPENDS
45+
input_generator
4446
SRCS
4547
${SRCS})
4648
endif()

graphics/input/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55

66
config GRAPHICS_INPUT_GENERATOR
7-
bool "Input generator library"
7+
bool
88
default n
99
---help---
1010
This is a simple input generator library that can be used to
@@ -14,6 +14,7 @@ menuconfig GRAPHICS_INPUT_MONKEY
1414
tristate "Monkey test"
1515
select UINPUT_TOUCH
1616
select UINPUT_BUTTONS
17+
select GRAPHICS_INPUT_GENERATOR
1718
select LIBC_PRINT_EXTENSION
1819
default n
1920

graphics/input/monkey/monkey.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
****************************************************************************/
2626

2727
#include <nuttx/config.h>
28+
#include <graphics/input_gen.h>
2829
#include <stdlib.h>
2930
#include <string.h>
3031
#include <inttypes.h>
@@ -41,8 +42,6 @@
4142

4243
#define MONKEY_DEV_PATH_TOUCH "/dev/input0"
4344
#define MONKEY_DEV_PATH_BUTTON "/dev/buttons"
44-
#define MONKEY_DEV_PATH_UTOUCH "/dev/utouch"
45-
#define MONKEY_DEV_PATH_UBUTTON "/dev/ubutton"
4645

4746
#define MONKEY_DEV_CREATE_MATCH(monkey, type_mask, type) \
4847
do { \
@@ -60,6 +59,23 @@ do { \
6059
} \
6160
} while (0)
6261

62+
#define MONKEY_DEV_CREATE_UINPUT(monkey, _type) \
63+
do { \
64+
if (input_gen_query_device(monkey->input_gen_ctx, INPUT_GEN_DEV_##_type)) \
65+
{ \
66+
FAR struct monkey_dev_s *dev = calloc(1, sizeof(struct monkey_dev_s)); \
67+
if (!dev) \
68+
{ \
69+
MONKEY_LOG_ERROR("Failed to create virtual device"); \
70+
goto failed; \
71+
} \
72+
dev->type = MONKEY_DEV_TYPE_##_type; \
73+
dev->is_available = true; \
74+
(monkey)->devs[(monkey)->dev_num] = dev; \
75+
(monkey)->dev_num++; \
76+
} \
77+
} while (0)
78+
6379
/****************************************************************************
6480
* Public Functions
6581
****************************************************************************/
@@ -75,8 +91,15 @@ FAR struct monkey_s *monkey_create(int dev_type_mask)
7591

7692
if (MONKEY_IS_UINPUT_TYPE(dev_type_mask))
7793
{
78-
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, UTOUCH);
79-
MONKEY_DEV_CREATE_MATCH(monkey, dev_type_mask, UBUTTON);
94+
if (input_gen_create(&monkey->input_gen_ctx,
95+
MONKEY_GET_DEV_TYPE(dev_type_mask)) < 0)
96+
{
97+
MONKEY_LOG_ERROR("input generator create failed");
98+
goto failed;
99+
}
100+
101+
MONKEY_DEV_CREATE_UINPUT(monkey, UTOUCH);
102+
MONKEY_DEV_CREATE_UINPUT(monkey, UBUTTON);
80103
}
81104
else
82105
{
@@ -110,6 +133,12 @@ void monkey_delete(FAR struct monkey_s *monkey)
110133
int i;
111134
MONKEY_ASSERT_NULL(monkey);
112135

136+
if (monkey->input_gen_ctx)
137+
{
138+
input_gen_reset_devices(monkey->input_gen_ctx, INPUT_GEN_DEV_ALL);
139+
input_gen_destroy(monkey->input_gen_ctx);
140+
}
141+
113142
for (i = 0; i < monkey->dev_num; i++)
114143
{
115144
monkey_dev_delete(monkey->devs[i]);

graphics/input/monkey/monkey_dev.c

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <errno.h>
2828
#include <fcntl.h>
2929
#include <inttypes.h>
30+
#include <graphics/input_gen.h>
3031
#include <nuttx/input/buttons.h>
3132
#include <nuttx/input/touchscreen.h>
3233
#include <poll.h>
@@ -48,41 +49,43 @@
4849
* Name: utouch_write
4950
****************************************************************************/
5051

51-
static void utouch_write(int fd, int x, int y, int touch_down)
52+
static void utouch_write(input_gen_ctx_t input_gen_ctx,
53+
FAR const struct monkey_dev_state_s *state)
5254
{
5355
struct touch_sample_s sample;
56+
sample.npoints = 1;
57+
input_gen_fill_point(sample.point, state->data.touch.x,
58+
state->data.touch.y,
59+
state->data.touch.is_pressed ? TOUCH_DOWN : TOUCH_UP);
5460

55-
if (touch_down)
61+
if (input_gen_write_raw(input_gen_ctx, INPUT_GEN_DEV_UTOUCH, &sample,
62+
sizeof(struct touch_sample_s)) < 0)
5663
{
57-
sample.point[0].x = x;
58-
sample.point[0].y = y;
59-
sample.point[0].pressure = 42;
60-
sample.point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
61-
TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
64+
MONKEY_LOG_WARN("unsupported device type: %d", INPUT_GEN_DEV_UTOUCH);
65+
return;
6266
}
63-
else
64-
{
65-
sample.point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
66-
}
67-
68-
sample.npoints = 1;
69-
sample.point[0].h = 1;
70-
sample.point[0].w = 1;
7167

72-
write(fd, &sample, sizeof(struct touch_sample_s));
7368
MONKEY_LOG_INFO("%s at x = %d, y = %d",
74-
touch_down ? "PRESS " : "RELEASE", x, y);
69+
state->data.touch.is_pressed ? "PRESS" : "RELEASE",
70+
state->data.touch.x, state->data.touch.y);
7571
}
7672

7773
/****************************************************************************
7874
* Name: ubutton_write
7975
****************************************************************************/
8076

81-
static void ubutton_write(int fd, uint32_t btn_bits)
77+
static void ubutton_write(input_gen_ctx_t input_gen_ctx,
78+
FAR const struct monkey_dev_state_s *state)
8279
{
83-
btn_buttonset_t buttonset = btn_bits;
84-
write(fd, &buttonset, sizeof(buttonset));
85-
MONKEY_LOG_INFO("btn = 0x%08X", btn_bits);
80+
btn_buttonset_t buttonset = state->data.button.value;
81+
if (input_gen_write_raw(input_gen_ctx, INPUT_GEN_DEV_UBUTTON, &buttonset,
82+
sizeof(buttonset)) < 0)
83+
{
84+
MONKEY_LOG_WARN("unsupported device type: %d", INPUT_GEN_DEV_UBUTTON);
85+
return;
86+
}
87+
88+
MONKEY_LOG_INFO("btn = 0x%08X", state->data.button.value);
8689
}
8790

8891
/****************************************************************************
@@ -152,18 +155,12 @@ FAR struct monkey_dev_s *monkey_dev_create(FAR const char *dev_path,
152155
int fd;
153156

154157
MONKEY_ASSERT_NULL(dev_path);
158+
MONKEY_ASSERT(!MONKEY_IS_UINPUT_TYPE(type));
155159

156160
dev = calloc(1, sizeof(struct monkey_dev_s));
157161
MONKEY_ASSERT_NULL(dev);
158162

159-
if (MONKEY_IS_UINPUT_TYPE(type))
160-
{
161-
oflag = O_RDWR | O_NONBLOCK;
162-
}
163-
else
164-
{
165-
oflag = O_RDONLY | O_NONBLOCK;
166-
}
163+
oflag = O_RDONLY | O_NONBLOCK;
167164

168165
fd = open(dev_path, oflag);
169166
if (fd < 0)
@@ -204,13 +201,6 @@ void monkey_dev_delete(FAR struct monkey_dev_s *dev)
204201

205202
if (dev->fd > 0)
206203
{
207-
/* Reset input state */
208-
209-
struct monkey_dev_state_s state;
210-
memset(&state, 0, sizeof(state));
211-
state.type = dev->type;
212-
monkey_dev_set_state(dev, &state);
213-
214204
MONKEY_LOG_NOTICE("close fd: %d", dev->fd);
215205
close(dev->fd);
216206
}
@@ -222,23 +212,24 @@ void monkey_dev_delete(FAR struct monkey_dev_s *dev)
222212
* Name: monkey_dev_set_state
223213
****************************************************************************/
224214

225-
void monkey_dev_set_state(FAR struct monkey_dev_s *dev,
215+
void monkey_dev_set_state(input_gen_ctx_t input_gen_ctx,
226216
FAR const struct monkey_dev_state_s *state)
227217
{
228-
MONKEY_ASSERT_NULL(dev);
218+
MONKEY_ASSERT_NULL(input_gen_ctx);
229219

230-
switch (MONKEY_GET_DEV_TYPE(dev->type))
220+
switch (MONKEY_GET_DEV_TYPE(state->type))
231221
{
232222
case MONKEY_DEV_TYPE_TOUCH:
233-
utouch_write(dev->fd,
234-
state->data.touch.x,
235-
state->data.touch.y,
236-
state->data.touch.is_pressed);
237-
break;
223+
{
224+
utouch_write(input_gen_ctx, state);
225+
break;
226+
}
238227

239228
case MONKEY_DEV_TYPE_BUTTON:
240-
ubutton_write(dev->fd, state->data.button.value);
241-
break;
229+
{
230+
ubutton_write(input_gen_ctx, state);
231+
break;
232+
}
242233

243234
default:
244235
break;
@@ -280,7 +271,7 @@ bool monkey_dev_get_state(FAR struct monkey_dev_s *dev,
280271
}
281272

282273
/****************************************************************************
283-
* Name: monkey_dev_get_state
274+
* Name: monkey_dev_get_type
284275
****************************************************************************/
285276

286277
enum monkey_dev_type_e monkey_dev_get_type(

graphics/input/monkey/monkey_dev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void monkey_dev_delete(FAR struct monkey_dev_s *dev);
6969
* Name: monkey_dev_set_state
7070
****************************************************************************/
7171

72-
void monkey_dev_set_state(FAR struct monkey_dev_s *dev,
72+
void monkey_dev_set_state(input_gen_ctx_t input_gen_ctx,
7373
FAR const struct monkey_dev_state_s *state);
7474

7575
/****************************************************************************

graphics/input/monkey/monkey_event.c

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,12 @@
2626

2727
#include <string.h>
2828
#include <unistd.h>
29+
#include <graphics/input_gen.h>
2930
#include "monkey_dev.h"
3031
#include "monkey_event.h"
3132
#include "monkey_log.h"
3233
#include "monkey_utils.h"
3334

34-
/****************************************************************************
35-
* Private Functions
36-
****************************************************************************/
37-
38-
/****************************************************************************
39-
* Name: monkey_exec_darg
40-
****************************************************************************/
41-
42-
static bool monkey_exec_darg(FAR struct monkey_dev_s *dev,
43-
FAR struct monkey_dev_state_s *state,
44-
FAR const struct monkey_event_param_s *param)
45-
{
46-
int t = 0;
47-
uint32_t start;
48-
state->data.touch.is_pressed = true;
49-
50-
start = monkey_tick_get();
51-
52-
while (t < param->duration)
53-
{
54-
t = monkey_tick_elaps(monkey_tick_get(), start);
55-
state->data.touch.x = monkey_map(t, 0, param->duration,
56-
param->x1, param->x2);
57-
state->data.touch.y = monkey_map(t, 0, param->duration,
58-
param->y1, param->y2);
59-
monkey_dev_set_state(dev, state);
60-
61-
if (usleep(1000) < 0)
62-
{
63-
return false;
64-
}
65-
}
66-
67-
return true;
68-
}
69-
7035
/****************************************************************************
7136
* Public Functions
7237
****************************************************************************/
@@ -143,60 +108,46 @@ bool monkey_event_exec(FAR struct monkey_s *monkey,
143108
FAR const struct monkey_event_param_s *param)
144109
{
145110
bool retval = false;
146-
struct monkey_dev_state_s state;
147-
memset(&state, 0, sizeof(struct monkey_dev_state_s));
148-
state.type = monkey_dev_get_type(dev);
149-
111+
enum monkey_dev_type_e dev_type = monkey_dev_get_type(dev);
150112
MONKEY_LOG_INFO("dev=0x%x event=%d(%s) duration=%d"
151113
" x1=%d y1=%d x2=%d y2=%d",
152-
state.type,
114+
dev_type,
153115
param->event,
154116
monkey_event_type2name(param->event),
155117
param->duration,
156118
param->x1, param->y1,
157119
param->x2, param->y2);
158120

159-
if (state.type & MONKEY_DEV_TYPE_TOUCH)
121+
if (dev_type & MONKEY_DEV_TYPE_TOUCH)
160122
{
161-
state.data.touch.x = param->x1;
162-
state.data.touch.y = param->y1;
163-
state.data.touch.is_pressed = true;
164-
monkey_dev_set_state(dev, &state);
165-
166123
if (param->event == MONKEY_EVENT_DRAG)
167124
{
168-
retval = monkey_exec_darg(dev, &state, param);
125+
retval = !input_gen_drag(monkey->input_gen_ctx,
126+
param->x1, param->y1,
127+
param->x2, param->y2,
128+
param->duration);
169129
}
170130
else
171131
{
172-
retval = usleep(param->duration * 1000) == 0;
132+
/* Use swipe with same start and end point to simulate click or
133+
* longpress.
134+
*/
135+
136+
retval = !input_gen_swipe(monkey->input_gen_ctx,
137+
param->x1, param->y1,
138+
param->x1, param->y1,
139+
param->duration);
173140
}
174-
175-
if (!retval)
176-
{
177-
MONKEY_LOG_NOTICE("detect monkey killed");
178-
}
179-
180-
state.data.touch.is_pressed = false;
181-
monkey_dev_set_state(dev, &state);
182141
}
183-
else if (state.type & MONKEY_DEV_TYPE_BUTTON)
142+
else if (dev_type & MONKEY_DEV_TYPE_BUTTON)
184143
{
185-
/* press button */
186-
187-
state.data.button.value = 1 << monkey->config.btn_bit;
188-
monkey_dev_set_state(dev, &state);
189-
190-
retval = usleep(param->duration * 1000) == 0;
191-
192-
/* release button */
193-
194-
state.data.button.value = 0;
195-
monkey_dev_set_state(dev, &state);
144+
retval = !input_gen_button_longpress(monkey->input_gen_ctx,
145+
1 << monkey->config.btn_bit,
146+
param->duration);
196147
}
197148
else
198149
{
199-
MONKEY_LOG_WARN("unsupport device type: %d", state.type);
150+
MONKEY_LOG_WARN("unsupported device type: %d", dev_type);
200151
}
201152

202153
return retval;

graphics/input/monkey/monkey_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static enum monkey_wait_res_e monkey_wait(uint32_t ms)
532532
}
533533
else
534534
{
535-
MONKEY_LOG_ERROR("Unknow error: %d", errcode);
535+
MONKEY_LOG_ERROR("Unknown error: %d", errcode);
536536
}
537537
}
538538
else if (ret == SIGTSTP)

0 commit comments

Comments
 (0)