Scrcpy: Support autorun when phone is plugged

Created on 23 Nov 2018  路  11Comments  路  Source: Genymobile/scrcpy

This is a nice feature to have in gnirehtet. I wonder if it's easy to implement in scrcpy.

feature request

Most helpful comment

Ah, sorry, I read the question too fast :) I added the _feature request_ label.

If it's just for 1 device, you can do something like:

while :; do adb wait-for-device && scrcpy; done

(except that it will rerun _scrcpy_ as soon as you close the window)

All 11 comments

Something like:

./gnirehtet autorun

?

:wink:

exactly, but for scrcpy

Ah, sorry, I read the question too fast :) I added the _feature request_ label.

If it's just for 1 device, you can do something like:

while :; do adb wait-for-device && scrcpy; done

(except that it will rerun _scrcpy_ as soon as you close the window)

Cool, thanks

udev rule is another option in Linux.

@npes87184 Yes :+1:

Here is an exemple (for gnirehtet): https://github.com/Genymobile/gnirehtet/issues/49#issuecomment-343525703

I have written a udev rule and systemd.service to achieve this feature.

Below is detail

requirement:

  • Linux system
  • udev
  • systemd
  • root permission

Step:

  1. add /etc/udev/rules.d/99-scrcpy.rules as:
ACTION=="add" \
, SUBSYSTEM=="usb" \
, ENV{DEVTYPE}=="usb_device" \
, ATTRS{idVendor}=="XXXX" \
, ATTRS{idProduct}=="XXXX" \
, TAG+="systemd" \
, ENV{SYSTEMD_WANTS}="scrcpy.service"

where you should specify idVendor and idProduct for your device (simply use lsusb).

  1. sudo udevadm control --reload-rules to reload udev.rule
  2. add /etc/systemd/system/scrcpy.service as:
[Unit]
Description=Start scrcpy when correct usb device is plugged

[Service]
Type=simple
Environment=XAUTHORITY=/home/<username>/.Xauthority
Environment=DISPLAY=:0
ExecStartPre=/bin/sleep 3
ExecStart=/usr/local/bin/scrcpy

[Install]
WantedBy=multi-user.target

where you should specify your username to get correct cookie. also, you should setup DISPLAY environment variable based on your environment.

  1. systemctl daemon-reload to reload systemd service

That's all!

FAQ:

1. Why not using RUN+= in udev.rule?

it will show Dec 25 21:52:25 npes87184 systemd-udevd[471]: seq 22149 '/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.1' is taking a long time in syslog. In additional, other udev event will be delayed.

2. why ENV{DEVTYPE}=="usb_device" in udev.rule?

to prevent triggering more than once.

Reference:

  1. Proper(-ish) way to start long-running systemd service on udev event (device hotplug)
  2. Setting DISPLAY in systemd service file

Hi,

I just wrote and published a tiny tool to execute a command whenever a device is adb-connected: autoadb.

autoadb scrcpy -s {}

_({} is replaced by the serial of the devices)_

Here are binaries (I did not create a release yet, and the Windows version is not tested):

  • autoadb (Linux 64 bits)
    _SHA256: 54618ae4ffdf725ad603aef3ab546bf9ead43bae15a429770ea9bcb355741efd_
  • autoadb.exe (Windows 64 bits)
    _SHA256: d367fd842c9a061141cc4a6c82cb444b079f42697306fd7dd39fc082d47b0c51_

Feedbacks welcome.

Great! It works as expected. However I also have a local fork with libusb keyboard support that requires usb id like 18d1:4ee7. Is it possible to let autoadb provide that info?

Is it possible to let autoadb provide that info?

The adb daemon does not provide this info on device connection:

printf '0012host:track-devices' | nc localhost 5037

So you have to resolve it manually from the serial :confused:

hmm, perhaps rust ecosystem has some usb related stuff which might help?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fernandofreamunde picture fernandofreamunde  路  4Comments

cutoseo picture cutoseo  路  4Comments

jonnybrooks picture jonnybrooks  路  3Comments

YurieCo picture YurieCo  路  4Comments

fleytman picture fleytman  路  4Comments