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>
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
286277enum monkey_dev_type_e monkey_dev_get_type (
0 commit comments