Auto-Open scrcpy if Smartphone is plugged in

I go crazy with a simple task I try to solve:

I want to open a Dialog in KDE which ask “Do you want to open scrcpy?”.
If user click yes, it should be started.
BTW: scrcpy is an application which shows your Android-Phone Screen at your desktop

What I’ve done so far

/etc/udev/rules.d/99-android.rules:

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="2717", ATTR{idProduct}=="ff48", RUN+="/usr/bin/logger 'scrcpy rule triggered' && /usr/local/bin/ask_scrcpy.sh"

/usr/local/bin/ask_scrcpy.sh

#!/bin/bash
sleep 2
export DISPLAY=:0
export XDG_RUNTIME_DIR=/run/user/$(id -u suther)
runuser -u suther -- kdialog --yesno "Möchten Sie scrcpy starten?" && scrcpy &

File-Permissions

4 -rwxr-xr-x 1 root root 167 18. Jan 12:23 /usr/local/bin/ask_scrcpy.sh
4 -rw-r--r-- 1 root root 345 18. Jan 12:08 /etc/udev/rules.d/99-android.rules

If I run ask_scrcpy.sh as user, I got the message:

runuser: not permitted to non-root user

if I run it as root, it work as expected!

su root -c /usr/local/bin/ask_scrcpy.sh

But if I unplug and replug the smartphone, nothing happend… even if the journalctl -f command show me, that the script ask_scrcpy.sh was triggerd:

Jan 18 12:26:57 optimusprime kernel: usb 1-10: New USB device found, idVendor=2717, idProduct=ff48, bcdDevice= 4.19
Jan 18 12:26:57 optimusprime kernel: usb 1-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 18 12:26:57 optimusprime kernel: usb 1-10: Product: POCO F3
Jan 18 12:26:57 optimusprime kernel: usb 1-10: Manufacturer: Xiaomi
Jan 18 12:26:57 optimusprime kernel: usb 1-10: SerialNumber: 7d05afea
Jan 18 12:26:57 optimusprime root[51655]: scrcpy rule triggered && /usr/local/bin/ask_scrcpy.sh

Any hints how to solve this?

Ok, I am one step further.

If I change the ask_scrcpy.sh like that:

#!/bin/bash
sleep 2

export DISPLAY=:0
export XAUTHORITY=$XAUTHORITY
export XDG_RUNTIME_DIR=/run/user/$(id -u $USER)

xhost +SI:localuser:suther

sudo -u  $USER dbus-launch -- kdialog --yesno "Möchten Sie scrcpy starten?" && /usr/bin/scrcpy &

the kdialog appear.

Buf if I click yes to start scrcpy, nothing happend.

I’m not sure whether I fully understand how scrcpy works, but in your script, you are invoking a conditional operand — i.e. &&, the logical “AND” — which will be interpreted by the shell before the rest of the line gets executed.

Therefore, I think you need to put the whole command after $USER between quotes in order to prevent the shell from considering the operand as dividing the entire line into two separate commands. :point_down:

sudo -u  $USER "dbus-launch -- kdialog --yesno 'Möchten Sie scrcpy starten?' && /usr/bin/scrcpy" &

Thanks for your answer.
If I do it like this:

sudo -u $USER 'dbus-launch -- kdialog --yesno "Möchten Sie scrcpy starten?" && /usr/bin/scrcpy' &

it result this on CLI if I run the script:

/usr/local/bin/ask_scrcpy.sh
localuser:suther being added to access control list
[suther@optimusprime ~]$ sudo: dbus-launch – kdialog --yesno “Möchten Sie scrcpy starten?” && /usr/bin/scrcpy: Command not found

Typo?

Shouldn’t
sudo -u $USER dbus-launch -- kdialog --yesno "Möchten Sie scrcpy starten?" && /usr/bin/scrcpy &

be replaced by
sudo -u $USER dbus-launch --kdialog --yesno "Möchten Sie scrcpy starten?" && /usr/bin/scrcpy &

?

Insofar as I can see, there is no --kdialog option to dbus-launch, so the space between dbus-launch and kdialog is intended to tell the shell that there are no dbus-launch-specific options being passed anymore in whatever follows on that line — that’s what the “space dash dash space” syntax is intended for.

1 Like

I’m not sure if it helps, but this is how I use kdialog:

sudo -u mirdarthos XDG_RUNTIME_DIR=/run/user/1000 DISPLAY=:0 kdialog --title='<theTitle>' --passivepopup='<b><theMessage></b> can even have HTML' 5

This is part of a command I used to send a popup notification over SSH.

1 Like

One of the big problems I’ve hit when trying to do something like this is that udev triggered processes can only run for a short time before the system kills them. So it’s probably getting killed even before your sleep has finished.
So I suggest you try changing scrcpy.sh to read something like:

nohup &

Though it may actually be simpler to put all the code that the trigger triggers in another script file.
You may need to explicitly set the $WAYLAND-DISPLAY or $DISPLAY variable.

2 Likes