Genymobile / Genymobile/scrcpy

[Feature Request] Run commands/script on disconnect

Open
#3,906 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C
Stars
150k
Forks
13.7k
Avg merge
4d 13h
Merged PRs (30d)
2

Description

- [X] I have checked that a similar [feature request](https://github.com/Genymobile/scrcpy/issues?q=is%3Aopen+is%3Aissue+label%3A%22feature+request%22) does not already exist.

**Is your feature request related to a problem? Please describe.**
A momentary network disruption would break the running `scrcpy` connection, as expected. However, this leaves the phone in the unlocked state it was in when `scrcpy` was being used, posing a potential security and/or privacy risk. Also since the introduction of the new audio forwarding feature, if loud media was being played on the phone but listened through headphones on PC, the media continues to play on the phone loudspeaker in these situations, which may be undesirable. Finally, I run `adb shell wm size` to make the screen size more comfortable for desktop use before starting the mirroring session, which is reverted when `scrcpy` window is closed via my launcher script. A network problem could prevent the final `adb shell wm resize` revert action from being run on the device which leaves it with a display setting unsuitable for phone use.

**Describe the solution you'd like**
Option to specify a shell script placed locally on the device to be run by `scrcpy-server` on exit (whether normal or abnormal). Alternatively, in a similar fashion to how the `scrcpy-server` binary is uploaded, the script could be kept on the PC and uploaded on each connect. In this script, the user would be able to run any cleanup tasks specific to their setup. This would certainly at least solve the problems I stated above, by allowing me to run `input keyevent 26` (lock screen), `input keyevent VOLUME_MUTE`, and `wm resize` locally in the phone on disconnect.

**Describe alternatives you've considered**
I wonder if as a workaround, it might be possible to somehow detect the `scrcpy` disconnect on the phone, but that seems like a more fragile solution than the one proposed. Not much else, but any input on alternatives/workarounds would be highly appreciated.

**Additional context**
N/A.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing scrcpy-server exit handling and the existing connection flow, then compare it with the local launcher script and the mentioned adb shell cleanup commands. Define how a locally stored or uploaded shell script would be selected and invoked on both normal and abnormal disconnects; done means the requested cleanup commands run reliably in those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, c, shell
Domain
desktop-dev, mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.