Skip to content

Commit

Permalink
Create UhidManager only on first use
Browse files Browse the repository at this point in the history
There is no need to create a UhidManager instance (with its thread) if
no UHID is used.

PR #4473 <#4473>
  • Loading branch information
rom1v committed Feb 29, 2024
1 parent 87da68e commit f557188
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Controller implements AsyncProcessor {

private Thread thread;

private final UhidManager uhidManager;
private UhidManager uhidManager;

private final Device device;
private final ControlChannel controlChannel;
Expand All @@ -52,7 +52,13 @@ public Controller(Device device, ControlChannel controlChannel, CleanUp cleanUp,
this.powerOn = powerOn;
initPointers();
sender = new DeviceMessageSender(controlChannel);
uhidManager = new UhidManager(sender);
}

private UhidManager getUhidManager() {
if (uhidManager == null) {
uhidManager = new UhidManager(sender);
}
return uhidManager;
}

private void initPointers() {
Expand Down Expand Up @@ -99,7 +105,9 @@ public void start(TerminationListener listener) {
Ln.e("Controller error", e);
} finally {
Ln.d("Controller stopped");
uhidManager.closeAll();
if (uhidManager != null) {
uhidManager.closeAll();
}
listener.onTerminated(true);
}
}, "control-recv");
Expand Down Expand Up @@ -195,10 +203,10 @@ private boolean handleEvent() throws IOException {
device.rotateDevice();
break;
case ControlMessage.TYPE_UHID_CREATE:
uhidManager.open(msg.getId(), msg.getData());
getUhidManager().open(msg.getId(), msg.getData());
break;
case ControlMessage.TYPE_UHID_INPUT:
uhidManager.writeInput(msg.getId(), msg.getData());
getUhidManager().writeInput(msg.getId(), msg.getData());
break;
default:
// do nothing
Expand Down

0 comments on commit f557188

Please sign in to comment.