]> code.delx.au - monosys/blob - scripts/xuserrun
pulseaudio.service requires bluetooth.target
[monosys] / scripts / xuserrun
1 #!/bin/bash
2
3 #
4 # Run a command as the currently active X11 user
5 #
6
7 seat="seat0"
8
9 # determine location of loginctl
10 LOGINCTL=$(command -v loginctl || command -v systemd-loginctl)
11 if [[ -e LOGINCTL ]]; then
12 echo "Error: Unable to find loginctl executable"
13 exit 1
14 fi
15
16 get_session_info() {
17 local session="$1"
18 local varname="$2"
19 local IFS=$'\n'
20 eval declare -Ag $varname
21 for row in $(loginctl show-session "$session"); do
22 key="$(echo "${row}"|cut -d= -f1)"
23 val="$(echo "${row}"|cut -d= -f2-)"
24 eval ${varname}[\"${key}\"]=\"${val}\"
25 done
26 }
27
28 escape() {
29 for arg in "$@" ; do
30 printf "%q " "$arg";
31 done;
32 }
33
34 active_session="$(loginctl show-seat ${seat}|grep ActiveSession|cut -d= -f2)"
35 if [[ $? -ne 0 || -z $active_session ]]; then
36 echo "Error: Unable to determine active session"
37 exit 1
38 fi
39
40 get_session_info $active_session session_info
41
42 if [[ ${session_info[Type]} != "x11" ]]; then
43 echo "Error: Active session is not x11"
44 exit 2
45 fi
46
47 current_user="$(id -u -n)"
48
49 if [[ ${current_user} == ${session_info[Name]} ]]; then
50 # already correct user, no need to su
51 DISPLAY="${session_info[Display]}" "$@"
52 else
53 # run command as user
54 DISPLAY="${session_info[Display]}" su -c - "${session_info[Name]}" "$(escape "$@")"
55 fi