From 879941355dcae24a6c1ed4226b5bc524143af0b9 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 29 Jan 2018 14:52:22 +0100 Subject: [PATCH] Swap position/point names A point is a 2D vector. A position represent a point relative to the screen size. --- app/src/common.h | 6 +-- app/src/controlevent.c | 14 +++---- app/src/controlevent.h | 4 +- app/src/convert.c | 18 ++++---- app/src/convert.h | 2 +- app/src/scrcpy.c | 14 +++---- app/tests/test_control_event_serialize.c | 8 ++-- server/Makefile | 2 +- .../com/genymobile/scrcpy/ControlEvent.java | 14 +++---- .../genymobile/scrcpy/ControlEventReader.java | 16 +++---- server/src/com/genymobile/scrcpy/Device.java | 8 ++-- .../genymobile/scrcpy/EventController.java | 26 ++++++------ server/src/com/genymobile/scrcpy/Point.java | 17 +------- .../src/com/genymobile/scrcpy/Position.java | 42 +++++++++++++++++++ .../src/com/genymobile/scrcpy/RawPoint.java | 27 ------------ 15 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 server/src/com/genymobile/scrcpy/Position.java delete mode 100644 server/src/com/genymobile/scrcpy/RawPoint.java diff --git a/app/src/common.h b/app/src/common.h index 02d7a977..2b5c0238 100644 --- a/app/src/common.h +++ b/app/src/common.h @@ -11,16 +11,16 @@ struct size { Uint16 height; }; -struct position { +struct point { Uint16 x; Uint16 y; }; -struct point { +struct position { // The video screen size may be different from the real device screen size, // so store to which size the absolute position apply, to scale it accordingly. struct size screen_size; - struct position position; + struct point point; }; #endif diff --git a/app/src/controlevent.c b/app/src/controlevent.c index 46062dcf..53cbad5a 100644 --- a/app/src/controlevent.c +++ b/app/src/controlevent.c @@ -17,11 +17,11 @@ static inline void write32(Uint8 *buf, Uint32 value) { buf[3] = value; } -static void write_point(Uint8 *buf, const struct point *point) { - write16(&buf[0], point->position.x); - write16(&buf[2], point->position.y); - write16(&buf[4], point->screen_size.width); - write16(&buf[6], point->screen_size.height); +static void write_position(Uint8 *buf, const struct position *position) { + write16(&buf[0], position->point.x); + write16(&buf[2], position->point.y); + write16(&buf[4], position->screen_size.width); + write16(&buf[6], position->screen_size.height); } int control_event_serialize(const struct control_event *event, unsigned char *buf) { @@ -45,10 +45,10 @@ int control_event_serialize(const struct control_event *event, unsigned char *bu case CONTROL_EVENT_TYPE_MOUSE: buf[1] = event->mouse_event.action; write32(&buf[2], event->mouse_event.buttons); - write_point(&buf[6], &event->mouse_event.point); + write_position(&buf[6], &event->mouse_event.position); return 14; case CONTROL_EVENT_TYPE_SCROLL: - write_point(&buf[1], &event->scroll_event.point); + write_position(&buf[1], &event->scroll_event.position); write32(&buf[9], (Uint32) event->scroll_event.hscroll); write32(&buf[13], (Uint32) event->scroll_event.vscroll); return 17; diff --git a/app/src/controlevent.h b/app/src/controlevent.h index 1f358cfc..077fb7ef 100644 --- a/app/src/controlevent.h +++ b/app/src/controlevent.h @@ -33,10 +33,10 @@ struct control_event { struct { enum android_motionevent_action action; enum android_motionevent_buttons buttons; - struct point point; + struct position position; } mouse_event; struct { - struct point point; + struct position position; Sint32 hscroll; Sint32 vscroll; } scroll_event; diff --git a/app/src/convert.c b/app/src/convert.c index 0cae970d..ec6f1de0 100644 --- a/app/src/convert.c +++ b/app/src/convert.c @@ -144,32 +144,32 @@ SDL_bool mouse_button_from_sdl_to_android(const SDL_MouseButtonEvent *from, } to->mouse_event.buttons = convert_mouse_buttons(SDL_BUTTON(from->button)); - to->mouse_event.point.screen_size = screen_size; - to->mouse_event.point.position.x = (Uint16) from->x; - to->mouse_event.point.position.y = (Uint16) from->y; + to->mouse_event.position.screen_size = screen_size; + to->mouse_event.position.point.x = (Uint16) from->x; + to->mouse_event.position.point.y = (Uint16) from->y; return SDL_TRUE; } SDL_bool mouse_motion_from_sdl_to_android(const SDL_MouseMotionEvent *from, - const struct size screen_size, + struct size screen_size, struct control_event *to) { to->type = CONTROL_EVENT_TYPE_MOUSE; to->mouse_event.action = AMOTION_EVENT_ACTION_MOVE; to->mouse_event.buttons = convert_mouse_buttons(from->state); - to->mouse_event.point.screen_size = screen_size; - to->mouse_event.point.position.x = from->x; - to->mouse_event.point.position.y = from->y; + to->mouse_event.position.screen_size = screen_size; + to->mouse_event.position.point.x = from->x; + to->mouse_event.position.point.y = from->y; return SDL_TRUE; } SDL_bool mouse_wheel_from_sdl_to_android(const SDL_MouseWheelEvent *from, - const struct point point, + struct position position, struct control_event *to) { to->type = CONTROL_EVENT_TYPE_SCROLL; - to->scroll_event.point = point; + to->scroll_event.position = position; int mul = from->direction == SDL_MOUSEWHEEL_NORMAL ? 1 : -1; to->scroll_event.hscroll = mul * from->x; diff --git a/app/src/convert.h b/app/src/convert.h index a2922a93..5a535a37 100644 --- a/app/src/convert.h +++ b/app/src/convert.h @@ -29,7 +29,7 @@ SDL_bool mouse_motion_from_sdl_to_android(const SDL_MouseMotionEvent *from, // on Android, a scroll event requires the current mouse position SDL_bool mouse_wheel_from_sdl_to_android(const SDL_MouseWheelEvent *from, - struct point point, + struct position position, struct control_event *to); #endif diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0586d515..b1e60a8e 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -104,12 +104,12 @@ static inline struct size get_window_size(SDL_Window *window) { return size; } -static inline struct position get_mouse_position() { +static inline struct point get_mouse_point() { int x; int y; SDL_GetMouseState(&x, &y); SDL_assert_release(x >= 0 && x < 0x10000 && y >= 0 && y < 0x10000); - return (struct position) { + return (struct point) { .x = (Uint16) x, .y = (Uint16) y, }; @@ -322,9 +322,9 @@ static void handle_mouse_button(const SDL_MouseButtonEvent *event, struct size s } } -static void handle_mouse_wheel(const SDL_MouseWheelEvent *event, struct point point) { +static void handle_mouse_wheel(const SDL_MouseWheelEvent *event, struct position position) { struct control_event control_event; - if (mouse_wheel_from_sdl_to_android(event, point, &control_event)) { + if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) { if (!controller_push_event(&controller, &control_event)) { SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send wheel button event"); } @@ -366,11 +366,11 @@ void event_loop(void) { handle_mouse_motion(&event.motion, frame_size); break; case SDL_MOUSEWHEEL: { - struct point point = { + struct position position = { .screen_size = frame_size, - .position = get_mouse_position(), + .point = get_mouse_point(), }; - handle_mouse_wheel(&event.wheel, point); + handle_mouse_wheel(&event.wheel, position); break; } case SDL_MOUSEBUTTONDOWN: diff --git a/app/tests/test_control_event_serialize.c b/app/tests/test_control_event_serialize.c index c0fd9f48..b983fea9 100644 --- a/app/tests/test_control_event_serialize.c +++ b/app/tests/test_control_event_serialize.c @@ -51,8 +51,8 @@ static void test_serialize_mouse_event() { .mouse_event = { .action = AMOTION_EVENT_ACTION_DOWN, .buttons = AMOTION_EVENT_BUTTON_PRIMARY, - .point = { - .position = { + .position = { + .point = { .x = 260, .y = 1026, }, @@ -82,8 +82,8 @@ static void test_serialize_scroll_event() { struct control_event event = { .type = CONTROL_EVENT_TYPE_SCROLL, .scroll_event = { - .point = { - .position = { + .position = { + .point = { .x = 260, .y = 1026, }, diff --git a/server/Makefile b/server/Makefile index 1cfbb580..22cea8e4 100644 --- a/server/Makefile +++ b/server/Makefile @@ -27,7 +27,7 @@ SRC := com/genymobile/scrcpy/ScrCpyServer.java \ com/genymobile/scrcpy/EventController.java \ com/genymobile/scrcpy/Ln.java \ com/genymobile/scrcpy/Point.java \ - com/genymobile/scrcpy/RawPoint.java \ + com/genymobile/scrcpy/Position.java \ com/genymobile/scrcpy/ScreenInfo.java \ com/genymobile/scrcpy/ScreenStreamer.java \ com/genymobile/scrcpy/ScreenStreamerSession.java \ diff --git a/server/src/com/genymobile/scrcpy/ControlEvent.java b/server/src/com/genymobile/scrcpy/ControlEvent.java index 76074662..521038b0 100644 --- a/server/src/com/genymobile/scrcpy/ControlEvent.java +++ b/server/src/com/genymobile/scrcpy/ControlEvent.java @@ -16,7 +16,7 @@ public class ControlEvent { private int action; // KeyEvent.ACTION_* or MotionEvent.ACTION_* private int keycode; // KeyEvent.KEYCODE_* private int buttons; // MotionEvent.BUTTON_* - private Point point; + private Position position; private int hScroll; private int vScroll; @@ -39,19 +39,19 @@ public class ControlEvent { return event; } - public static ControlEvent createMotionControlEvent(int action, int buttons, Point point) { + public static ControlEvent createMotionControlEvent(int action, int buttons, Position position) { ControlEvent event = new ControlEvent(); event.type = TYPE_MOUSE; event.action = action; event.buttons = buttons; - event.point = point; + event.position = position; return event; } - public static ControlEvent createScrollControlEvent(Point point, int hScroll, int vScroll) { + public static ControlEvent createScrollControlEvent(Position position, int hScroll, int vScroll) { ControlEvent event = new ControlEvent(); event.type = TYPE_SCROLL; - event.point = point; + event.position = position; event.hScroll = hScroll; event.vScroll = vScroll; return event; @@ -81,8 +81,8 @@ public class ControlEvent { return buttons; } - public Point getPoint() { - return point; + public Position getPosition() { + return position; } public int getHScroll() { diff --git a/server/src/com/genymobile/scrcpy/ControlEventReader.java b/server/src/com/genymobile/scrcpy/ControlEventReader.java index a37063cb..fec2aab1 100644 --- a/server/src/com/genymobile/scrcpy/ControlEventReader.java +++ b/server/src/com/genymobile/scrcpy/ControlEventReader.java @@ -74,17 +74,17 @@ public class ControlEventReader { } int action = toUnsigned(buffer.get()); int buttons = buffer.getInt(); - Point point = readPoint(buffer); - return ControlEvent.createMotionControlEvent(action, buttons, point); + Position position = readPosition(buffer); + return ControlEvent.createMotionControlEvent(action, buttons, position); } case ControlEvent.TYPE_SCROLL: { if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) { break; } - Point point = readPoint(buffer); - int hscroll = buffer.getInt(); - int vscroll = buffer.getInt(); - return ControlEvent.createScrollControlEvent(point, hscroll, vscroll); + Position position = readPosition(buffer); + int hScroll = buffer.getInt(); + int vScroll = buffer.getInt(); + return ControlEvent.createScrollControlEvent(position, hScroll, vScroll); } default: Ln.w("Unknown event type: " + type); @@ -95,12 +95,12 @@ public class ControlEventReader { return null; } - private static Point readPoint(ByteBuffer buffer) { + private static Position readPosition(ByteBuffer buffer) { int x = toUnsigned(buffer.getShort()); int y = toUnsigned(buffer.getShort()); int screenWidth = toUnsigned(buffer.getShort()); int screenHeight = toUnsigned(buffer.getShort()); - return new Point(x, y, screenWidth, screenHeight); + return new Position(x, y, screenWidth, screenHeight); } private static int toUnsigned(short value) { diff --git a/server/src/com/genymobile/scrcpy/Device.java b/server/src/com/genymobile/scrcpy/Device.java index f157f8c1..829089fe 100644 --- a/server/src/com/genymobile/scrcpy/Device.java +++ b/server/src/com/genymobile/scrcpy/Device.java @@ -48,13 +48,13 @@ public class Device { return screenInfo; } - public RawPoint getPhysicalPoint(Point point) { + public Point getPhysicalPoint(Position position) { ScreenInfo screenInfo = getScreenInfo(); int deviceWidth = screenInfo.getLogicalWidth(); int deviceHeight = screenInfo.getLogicalHeight(); - int scaledX = point.getX() * deviceWidth / point.getScreenWidth(); - int scaledY = point.getY() * deviceHeight / point.getScreenHeight(); - return new RawPoint(scaledX, scaledY); + int scaledX = position.getX() * deviceWidth / position.getScreenWidth(); + int scaledY = position.getY() * deviceHeight / position.getScreenHeight(); + return new Point(scaledX, scaledY); } private ScreenInfo readScreenInfo() { diff --git a/server/src/com/genymobile/scrcpy/EventController.java b/server/src/com/genymobile/scrcpy/EventController.java index a1a2e20e..9fc07a0e 100644 --- a/server/src/com/genymobile/scrcpy/EventController.java +++ b/server/src/com/genymobile/scrcpy/EventController.java @@ -43,10 +43,10 @@ public class EventController { coords.touchMinor = 1; } - private void setPointerCoords(RawPoint rawPoint) { + private void setPointerCoords(Point point) { MotionEvent.PointerCoords coords = pointerCoords[0]; - coords.x = rawPoint.getX(); - coords.y = rawPoint.getY(); + coords.x = point.getX(); + coords.y = point.getY(); } private void setScroll(int hScroll, int vScroll) { @@ -72,10 +72,10 @@ public class EventController { injectText(controlEvent.getText()); break; case ControlEvent.TYPE_MOUSE: - injectMouse(controlEvent.getAction(), controlEvent.getButtons(), controlEvent.getPoint()); + injectMouse(controlEvent.getAction(), controlEvent.getButtons(), controlEvent.getPosition()); break; case ControlEvent.TYPE_SCROLL: - injectScroll(controlEvent.getPoint(), controlEvent.getHScroll(), controlEvent.getVScroll()); + injectScroll(controlEvent.getPosition(), controlEvent.getHScroll(), controlEvent.getVScroll()); } return true; } @@ -97,29 +97,29 @@ public class EventController { return true; } - private boolean injectMouse(int action, int buttons, Point point) { + private boolean injectMouse(int action, int buttons, Position position) { long now = SystemClock.uptimeMillis(); if (action == MotionEvent.ACTION_DOWN) { lastMouseDown = now; } - RawPoint rawPoint = Device.getInstance().getPhysicalPoint(point); - if (rawPoint == null) { + Point point = Device.getInstance().getPhysicalPoint(position); + if (point == null) { // ignore event return false; } - setPointerCoords(rawPoint); + setPointerCoords(point); MotionEvent event = MotionEvent.obtain(lastMouseDown, now, action, 1, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); return injectEvent(event); } - private boolean injectScroll(Point point, int hScroll, int vScroll) { + private boolean injectScroll(Position position, int hScroll, int vScroll) { long now = SystemClock.uptimeMillis(); - RawPoint rawPoint = Device.getInstance().getPhysicalPoint(point); - if (rawPoint == null) { + Point point = Device.getInstance().getPhysicalPoint(position); + if (point == null) { // ignore event return false; } - setPointerCoords(rawPoint); + setPointerCoords(point); setScroll(hScroll, vScroll); MotionEvent event = MotionEvent.obtain(lastMouseDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_MOUSE, 0); return injectEvent(event); diff --git a/server/src/com/genymobile/scrcpy/Point.java b/server/src/com/genymobile/scrcpy/Point.java index ade349d1..22ed3309 100644 --- a/server/src/com/genymobile/scrcpy/Point.java +++ b/server/src/com/genymobile/scrcpy/Point.java @@ -1,17 +1,12 @@ package com.genymobile.scrcpy; public class Point { - private int x; private int y; - private int screenWidth; - private int screenHeight; - public Point(int x, int y, int screenWidth, int screenHeight) { + public Point(int x, int y) { this.x = x; this.y = y; - this.screenWidth = screenWidth; - this.screenHeight = screenHeight; } public int getX() { @@ -22,21 +17,11 @@ public class Point { return y; } - public int getScreenWidth() { - return screenWidth; - } - - public int getScreenHeight() { - return screenHeight; - } - @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + - ", screenWidth=" + screenWidth + - ", screenHeight=" + screenHeight + '}'; } } diff --git a/server/src/com/genymobile/scrcpy/Position.java b/server/src/com/genymobile/scrcpy/Position.java new file mode 100644 index 00000000..9b6495d8 --- /dev/null +++ b/server/src/com/genymobile/scrcpy/Position.java @@ -0,0 +1,42 @@ +package com.genymobile.scrcpy; + +public class Position { + + private int x; + private int y; + private int screenWidth; + private int screenHeight; + + public Position(int x, int y, int screenWidth, int screenHeight) { + this.x = x; + this.y = y; + this.screenWidth = screenWidth; + this.screenHeight = screenHeight; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getScreenWidth() { + return screenWidth; + } + + public int getScreenHeight() { + return screenHeight; + } + + @Override + public String toString() { + return "Point{" + + "x=" + x + + ", y=" + y + + ", screenWidth=" + screenWidth + + ", screenHeight=" + screenHeight + + '}'; + } +} diff --git a/server/src/com/genymobile/scrcpy/RawPoint.java b/server/src/com/genymobile/scrcpy/RawPoint.java deleted file mode 100644 index d9ac5c8e..00000000 --- a/server/src/com/genymobile/scrcpy/RawPoint.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.genymobile.scrcpy; - -public class RawPoint { - private int x; - private int y; - - public RawPoint(int x, int y) { - this.x = x; - this.y = y; - } - - public int getX() { - return x; - } - - public int getY() { - return y; - } - - @Override - public String toString() { - return "RawPoint{" + - "x=" + x + - ", y=" + y + - '}'; - } -}