Skip to content

Commit 2c4ea68

Browse files
committed
Do not use device as a singleton
The device instance should be able to store a state (e.g. the maximum size requested by the user), so it should not be a singleton.
1 parent 8799413 commit 2c4ea68

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

server/src/com/genymobile/scrcpy/DesktopConnection.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ private static LocalSocket connect(String abstractName) throws IOException {
3333
return localSocket;
3434
}
3535

36-
public static DesktopConnection open(String deviceName, int width, int height) throws IOException {
36+
public static DesktopConnection open(Device device) throws IOException {
3737
LocalSocket socket = connect(SOCKET_NAME);
38+
39+
ScreenInfo initialScreenInfo = device.getScreenInfo();
40+
int width = initialScreenInfo.getLogicalWidth();
41+
int height = initialScreenInfo.getLogicalHeight();
42+
3843
DesktopConnection connection = new DesktopConnection(socket);
39-
connection.send(deviceName, width, height);
44+
connection.send(Device.getDeviceName(), width, height);
4045
return connection;
4146
}
4247

server/src/com/genymobile/scrcpy/Device.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ public interface RotationListener {
1313
void onRotationChanged(int rotation);
1414
}
1515

16-
private static final Device INSTANCE = new Device();
1716
private final ServiceManager serviceManager = new ServiceManager();
1817

1918
private ScreenInfo screenInfo;
2019
private RotationListener rotationListener;
2120

22-
private Device() {
21+
public Device() {
2322
screenInfo = readScreenInfo();
2423
registerRotationWatcher(new IRotationWatcher.Stub() {
2524
@Override
@@ -37,10 +36,6 @@ public void onRotationChanged(int rotation) throws RemoteException {
3736
});
3837
}
3938

40-
public static Device getInstance() {
41-
return INSTANCE;
42-
}
43-
4439
public synchronized ScreenInfo getScreenInfo() {
4540
if (screenInfo == null) {
4641
screenInfo = readScreenInfo();

server/src/com/genymobile/scrcpy/EventController.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
public class EventController {
1515

16+
private final Device device;
1617
private final InputManager inputManager;
1718
private final DesktopConnection connection;
1819

@@ -22,9 +23,10 @@ public class EventController {
2223
private final MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() };
2324
private final MotionEvent.PointerCoords[] pointerCoords = { new MotionEvent.PointerCoords() };
2425

25-
public EventController(DesktopConnection connection) {
26+
public EventController(Device device, DesktopConnection connection) {
27+
this.device = device;
2628
this.connection = connection;
27-
inputManager = Device.getInstance().getInputManager();
29+
inputManager = device.getInputManager();
2830
initPointer();
2931
}
3032

@@ -102,7 +104,7 @@ private boolean injectMouse(int action, int buttons, Position position) {
102104
if (action == MotionEvent.ACTION_DOWN) {
103105
lastMouseDown = now;
104106
}
105-
Point point = Device.getInstance().getPhysicalPoint(position);
107+
Point point = device.getPhysicalPoint(position);
106108
if (point == null) {
107109
// ignore event
108110
return false;
@@ -114,7 +116,7 @@ private boolean injectMouse(int action, int buttons, Position position) {
114116

115117
private boolean injectScroll(Position position, int hScroll, int vScroll) {
116118
long now = SystemClock.uptimeMillis();
117-
Point point = Device.getInstance().getPhysicalPoint(position);
119+
Point point = device.getPhysicalPoint(position);
118120
if (point == null) {
119121
// ignore event
120122
return false;

server/src/com/genymobile/scrcpy/ScrCpyServer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ public class ScrCpyServer {
77
private static final String TAG = "scrcpy";
88

99
private static void scrcpy() throws IOException {
10-
String deviceName = Device.getDeviceName();
11-
ScreenInfo initialScreenInfo = Device.getInstance().getScreenInfo();
12-
int width = initialScreenInfo.getLogicalWidth();
13-
int height = initialScreenInfo.getLogicalHeight();
14-
try (DesktopConnection connection = DesktopConnection.open(deviceName, width, height)) {
10+
final Device device = new Device();
11+
try (DesktopConnection connection = DesktopConnection.open(device)) {
1512
final ScreenStreamer streamer = new ScreenStreamer(connection);
16-
Device.getInstance().setRotationListener(new Device.RotationListener() {
13+
device.setRotationListener(new Device.RotationListener() {
1714
@Override
1815
public void onRotationChanged(int rotation) {
1916
streamer.reset();
2017
}
2118
});
2219

2320
// asynchronous
24-
startEventController(connection);
21+
startEventController(device, connection);
2522

2623
try {
2724
// synchronous
@@ -32,12 +29,12 @@ public void onRotationChanged(int rotation) {
3229
}
3330
}
3431

35-
private static void startEventController(final DesktopConnection connection) {
32+
private static void startEventController(final Device device, final DesktopConnection connection) {
3633
new Thread(new Runnable() {
3734
@Override
3835
public void run() {
3936
try {
40-
new EventController(connection).control();
37+
new EventController(device, connection).control();
4138
} catch (IOException e) {
4239
e.printStackTrace();
4340
}

0 commit comments

Comments
 (0)