34
34
#include < stdlib.h>
35
35
#include < stdbool.h>
36
36
#include < assert.h>
37
+ #include < string>
37
38
#include < unistd.h>
38
39
#include < sys/mman.h>
39
40
#include < sys/stat.h>
@@ -78,6 +79,8 @@ using ::android::hardware::hidl_string;
78
79
79
80
struct buffer ;
80
81
82
+ #define FAKE_TOUCH_ID 1111
83
+
81
84
void
82
85
destroy_buffer (struct buffer * buf) {
83
86
wl_buffer_destroy (buf->buffer );
@@ -812,28 +815,48 @@ static const struct wl_keyboard_listener keyboard_listener = {
812
815
static void
813
816
pointer_handle_enter (void *data, struct wl_pointer *pointer,
814
817
uint32_t serial, struct wl_surface *surface,
815
- wl_fixed_t , wl_fixed_t )
818
+ wl_fixed_t sx , wl_fixed_t sy )
816
819
{
820
+ ALOGI (" Pointer enter event: serial=%u, surface=%p, sx=%f, sy=%f" ,
821
+ serial, surface, wl_fixed_to_double (sx), wl_fixed_to_double (sy));
817
822
struct display *display = (struct display *)data;
818
823
display->pointer_surface = surface;
824
+
825
+ if (display->pointer_surface ) {
826
+ display->pointer_surface_sx = sx;
827
+ display->pointer_surface_sy = sy;
828
+ }
829
+
819
830
if (display->cursor_surface )
820
831
wl_pointer_set_cursor (pointer, serial,
821
832
display->cursor_surface , 0 , 0 );
822
833
}
823
834
835
+ static void
836
+ touch_handle_cancel (void *data, struct wl_touch *);
824
837
static void
825
838
pointer_handle_leave (void *data, struct wl_pointer *pointer,
826
839
uint32_t serial, struct wl_surface *)
827
840
{
841
+ ALOGI (" Pointer leave event: serial=%u, surface=%p" , serial, nullptr );
828
842
struct display *display = (struct display *)data;
843
+
844
+ if (display->fakeTouchEnabled ) {
845
+ display->fakeTouchEnabled = false ;
846
+ touch_handle_cancel (display, nullptr );
847
+ }
848
+
829
849
display->pointer_surface = NULL ;
830
850
if (display->cursor_surface )
831
851
wl_pointer_set_cursor (pointer, serial, NULL , 0 , 0 );
832
852
}
833
853
854
+ static void
855
+ touch_handle_motion (void *data, struct wl_touch *,
856
+ uint32_t , int32_t id, wl_fixed_t x_w, wl_fixed_t y_w);
834
857
static void
835
858
pointer_handle_motion (void *data, struct wl_pointer *,
836
- uint32_t , wl_fixed_t sx, wl_fixed_t sy)
859
+ uint32_t time , wl_fixed_t sx, wl_fixed_t sy)
837
860
{
838
861
struct display * display = (struct display *)data;
839
862
struct input_event event[5 ];
@@ -851,6 +874,15 @@ pointer_handle_motion(void *data, struct wl_pointer *,
851
874
ALOGE (" %s:%d error in touch clock_gettime: %s" ,
852
875
__FILE__, __LINE__, strerror (errno));
853
876
}
877
+
878
+ display->pointer_surface_sx = sx;
879
+ display->pointer_surface_sy = sy;
880
+
881
+ if (display->fakeTouchEnabled ) {
882
+ touch_handle_motion (display, nullptr , time , FAKE_TOUCH_ID, sx, sy);
883
+ return ;
884
+ }
885
+
854
886
x = wl_fixed_to_int (sx);
855
887
y = wl_fixed_to_int (sy);
856
888
if (display->scale != 1 ) {
@@ -899,6 +931,17 @@ handle_relative_motion(void *data, struct zwp_relative_pointer_v1*,
899
931
__FILE__, __LINE__, strerror (errno));
900
932
}
901
933
934
+ if (display->pointer_surface ) {
935
+ display->pointer_surface_sx += dx;
936
+ display->pointer_surface_sy += dy;
937
+
938
+ if (display->fakeTouchEnabled ) {
939
+ touch_handle_motion (display, nullptr , 0 , FAKE_TOUCH_ID,
940
+ display->pointer_surface_sx , display->pointer_surface_sy );
941
+ return ;
942
+ }
943
+ }
944
+
902
945
ADD_EVENT (EV_REL, REL_X, (int )acc_x);
903
946
ADD_EVENT (EV_REL, REL_Y, (int )acc_y);
904
947
ADD_EVENT (EV_SYN, SYN_REPORT, 0 );
@@ -911,9 +954,16 @@ handle_relative_motion(void *data, struct zwp_relative_pointer_v1*,
911
954
ALOGE (" Failed to write event for InputFlinger: %s" , strerror (errno));
912
955
}
913
956
957
+ static void
958
+ touch_handle_down (void *data, struct wl_touch *,
959
+ uint32_t , uint32_t , struct wl_surface *surface,
960
+ int32_t id, wl_fixed_t x_w, wl_fixed_t y_w);
961
+ static void
962
+ touch_handle_up (void *data, struct wl_touch *,
963
+ uint32_t , uint32_t , int32_t id);
914
964
static void
915
965
pointer_handle_button (void *data, struct wl_pointer *,
916
- uint32_t , uint32_t , uint32_t button,
966
+ uint32_t serial , uint32_t time , uint32_t button,
917
967
uint32_t state)
918
968
{
919
969
struct display * display = (struct display *)data;
@@ -931,6 +981,39 @@ pointer_handle_button(void *data, struct wl_pointer *,
931
981
ALOGE (" %s:%d error in touch clock_gettime: %s" ,
932
982
__FILE__, __LINE__, strerror (errno));
933
983
}
984
+
985
+ // pointer fake to touch if no touch device
986
+ bool fake_touch = false ;
987
+ // force fake touch even if have a touch device
988
+ const bool force_fake_touch = property_get_bool (" persist.waydroid.force_fake_touch_global" , false );
989
+
990
+ if (force_fake_touch) {
991
+ fake_touch = true ;
992
+ } else {
993
+ fake_touch = !display->touch && property_get_bool (" persist.waydroid.fake_touch_global" , false );
994
+ }
995
+
996
+ ALOGI (" Pointer button event: button=%u, state=%u, fakeTouch=%d, pointer_surface=%p" ,
997
+ button, state, fake_touch, display->pointer_surface );
998
+
999
+ if (button == BTN_LEFT && fake_touch) {
1000
+ const bool fake_touch = property_get_bool (" persist.waydroid.fake_touch_global" , false );
1001
+ // Ensure the config can be changed at runtime
1002
+ if (fake_touch) {
1003
+ // to touch event
1004
+ if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
1005
+ display->fakeTouchEnabled = true ;
1006
+ touch_handle_down (display, nullptr , serial, time ,
1007
+ display->pointer_surface , FAKE_TOUCH_ID,
1008
+ display->pointer_surface_sx , display->pointer_surface_sy );
1009
+ } else {
1010
+ display->fakeTouchEnabled = false ;
1011
+ touch_handle_up (display, nullptr , serial, time , FAKE_TOUCH_ID);
1012
+ }
1013
+ return ;
1014
+ }
1015
+ }
1016
+
934
1017
ADD_EVENT (EV_KEY, button, state);
935
1018
ADD_EVENT (EV_SYN, SYN_REPORT, 0 );
936
1019
@@ -1064,6 +1147,8 @@ touch_handle_down(void *data, struct wl_touch *,
1064
1147
int x, y;
1065
1148
unsigned int res, n = 0 ;
1066
1149
1150
+ ALOGI (" Touch down event: id=%d, x=%f, y=%f" , id, wl_fixed_to_double (x_w), wl_fixed_to_double (y_w));
1151
+
1067
1152
if (ensure_pipe (display, INPUT_TOUCH))
1068
1153
return ;
1069
1154
@@ -1103,6 +1188,8 @@ touch_handle_up(void *data, struct wl_touch *,
1103
1188
struct timespec rt;
1104
1189
unsigned int res, n = 0 ;
1105
1190
1191
+ ALOGI (" Touch up event: id=%d" , id);
1192
+
1106
1193
if (ensure_pipe (display, INPUT_TOUCH))
1107
1194
return ;
1108
1195
@@ -1261,6 +1348,8 @@ seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t wl_caps)
1261
1348
{
1262
1349
struct display *d = (struct display *)data;
1263
1350
enum wl_seat_capability caps = (enum wl_seat_capability) wl_caps;
1351
+ bool fakeTouch = false ;
1352
+ d->fakeTouchEnabled = false ;
1264
1353
1265
1354
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer ) {
1266
1355
d->pointer = wl_seat_get_pointer (seat);
@@ -1271,6 +1360,8 @@ seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t wl_caps)
1271
1360
mkfifo (INPUT_PIPE_NAME[INPUT_POINTER], S_IRWXO | S_IRWXG | S_IRWXU);
1272
1361
chown (INPUT_PIPE_NAME[INPUT_POINTER], 1000 , 1000 );
1273
1362
wl_pointer_add_listener (d->pointer , &pointer_listener, d);
1363
+
1364
+ fakeTouch = !(wl_caps & WL_SEAT_CAPABILITY_TOUCH);
1274
1365
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer ) {
1275
1366
remove (INPUT_PIPE_NAME[INPUT_POINTER]);
1276
1367
wl_pointer_destroy (d->pointer );
@@ -1303,6 +1394,15 @@ seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t wl_caps)
1303
1394
wl_touch_destroy (d->touch );
1304
1395
d->touch = NULL ;
1305
1396
}
1397
+
1398
+ if (fakeTouch) {
1399
+ assert (!d->touch );
1400
+ d->input_fd [INPUT_TOUCH] = -1 ;
1401
+ mkfifo (INPUT_PIPE_NAME[INPUT_TOUCH], S_IRWXO | S_IRWXG | S_IRWXU);
1402
+ chown (INPUT_PIPE_NAME[INPUT_TOUCH], 1000 , 1000 );
1403
+ for (int i = 0 ; i < MAX_TOUCHPOINTS; i++)
1404
+ d->touch_id [i] = -1 ;
1405
+ }
1306
1406
}
1307
1407
1308
1408
static void
0 commit comments