├── .gitignore ├── LICENSE ├── README.md └── src └── xpub.sh /.gitignore: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | !src/ 4 | src/* 5 | !src/xpub.sh 6 | 7 | !README.md 8 | !LICENSE 9 | !.gitignore 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016 Thomas "Ventto" Venriès 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Xpub 2 | ==== 3 | 4 | [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/Ventto/xpub/blob/master/LICENSE) 5 | [![Vote for xpub](https://img.shields.io/badge/AUR-Vote_for-yellow.svg)](https://aur.archlinux.org/packages/xpub/) 6 | 7 | *"Xpub is a Shell script to get user's display environment variables of any X graphical session from anywhere."* 8 | 9 | ## Perks 10 | 11 | * [x] **No requirement**: POSIX-compliant. 12 | * [x] **Omniscient**: Provides X environment variables of any session from any user. 13 | * [x] **Usefull**: Run graphical commands from udevrules (see below). 14 | * [x] **Extra**: Display graphical command on a specific session. 15 | * [x] **Support**: XWayland users, keep calm. 16 | 17 | # Installation 18 | 19 | * Package (AUR) 20 | 21 | ```bash 22 | $ yaourt -S xpub 23 | ``` 24 | 25 | * Manually 26 | 27 | ```bash 28 | $ git clone https://github.com/Ventto/xpub.git 29 | $ cd xpub 30 | $ chmod +x src/xpub.sh 31 | ``` 32 | 33 | # Usage 34 | 35 | ``` 36 | Usage: xpub [-t TTY] 37 | 38 | Without option, prints the X session information of the current user. 39 | 40 | -h: Prints this help and exits. 41 | -v: Prints the version and exits. 42 | -t: prints the current TTY's user X session information. 43 | ``` 44 | 45 | # Examples 46 | 47 | 48 | ### From terminal 49 | 50 | *sudo* is required. 51 | 52 | * Get information of your current session: 53 | 54 | ```bash 55 | $ xpub 56 | TTY=tty2 57 | XUSER=alice 58 | XAUTHORITY=/home/alice/.Xauthority 59 | DISPLAY=:0 60 | DBUS_SESSION_BUS_ADDRESS=/path 61 | ``` 62 | 63 | * Get information of a specific session: 64 | 65 | ```bash 66 | $ xpub -t tty2 67 | XUSER=alice 68 | XAUTHORITY=/home/alice/.Xauthority 69 | DISPLAY=:0 70 | DBUS_SESSION_BUS_ADDRESS=/path 71 | ``` 72 | 73 | ### Udev rules 74 | 75 | ```python 76 | IMPORT{program}="/usr/bin/xpub", \ 77 | RUN+="/bin/su $env{XUSER} -c '/usr/bin/notify-send Hello'" 78 | ``` 79 | 80 | After editing your rules, you may need to run `udevadm control --reload-rules`. 81 | 82 | ### For *root* 83 | 84 | ```bash 85 | $ export $(xpub) ; su "${XUSER}" -c '/usr/bin/notify-send Hello' 86 | ``` 87 | 88 | ### Shell scripts 89 | 90 | ```bash 91 | xenv=$(xpub 2>/tmp/xpub.log) 92 | 93 | if [ $# -ne 0 ]; then 94 | exit 1 95 | else 96 | export ${xenv} 97 | fi 98 | 99 | su "${XUSER}" -c "/usr/bin/notify-send Hello" 100 | ``` 101 | -------------------------------------------------------------------------------- /src/xpub.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2015-2016 Thomas "Ventto" Venriès 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy of 8 | # this software and associated documentation files (the "Software"), to deal in 9 | # the Software without restriction, including without limitation the rights to 10 | # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 11 | # the Software, and to permit persons to whom the Software is furnished to do so, 12 | # subject to the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be included in all 15 | # copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 19 | # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20 | # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | usage() { 24 | echo 'Usage: xpub [-t TTY] 25 | 26 | -h: Prints this help and exits. 27 | -v: Prints the version and exits. 28 | -t: Prints the logged user and its display environment variables 29 | from a graphical-session TTY or from the current one if no argument.' 30 | } 31 | 32 | version() { 33 | echo 'Xpub 0.6b 34 | 35 | Copyright (C) 2016 Thomas "Ventto" Venries. 36 | 37 | License MIT: . 38 | 39 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 40 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 41 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.' 42 | } 43 | 44 | main () { 45 | isXWayland=false 46 | tFlag=false 47 | 48 | while getopts 'hvt:' opt; do 49 | case $opt in 50 | t) OPTARG="$(echo "${OPTARG}" | tr '[:upper:]' '[:lower:]')" 51 | if ! printf '%s' "${OPTARG}" | grep -E '^tty[0-9]$' > /dev/null; then 52 | usage ; exit 2 53 | fi 54 | tArg="${OPTARG}" 55 | tFlag=true ;; 56 | h) usage ; exit ;; 57 | v) version ; exit ;; 58 | \?) usage ; exit ;; 59 | :) usage ; exit ;; 60 | esac 61 | done 62 | 63 | shift $((OPTIND - 1)) 64 | 65 | [ "$(id -u)" -ne 0 ] && { echo 'Run it with sudo.'; exit 1; } 66 | 67 | ${tFlag} && xtty="${tArg}" || xtty="$(cat /sys/class/tty/tty0/active)" 68 | 69 | xuser="$(who | grep "${xtty}" | head -n 1 | cut -d' ' -f1)" 70 | 71 | [ -z "${xuser}" ] && { echo "No user found from ${xtty}." 1>&2; exit 1; } 72 | 73 | xpids="$(ps -A | grep 'Xorg' | awk '{print $1}')" 74 | vterm="vt$(printf '%s' "${xtty}" | sed -e 's/tty//g')" 75 | 76 | if [ -n "${xpids}" ]; then 77 | for xpid in ${xpids}; do 78 | xdisplay="$(ps -o cmd= "${xpid}" | grep "${vterm}" | grep -E -o ':[0-9]')" 79 | if [ "$?" -eq 0 ]; then 80 | xdisplay="$(echo "${xdisplay}" | head -n1)" 81 | break 82 | fi 83 | done 84 | fi 85 | 86 | if [ -z "${xdisplay}" ]; then 87 | #Trying to get the active display from XWayland 88 | xdisplay="$(ps -A -o tty= -o cmd= | grep Xwayland | \ 89 | grep -v 'grep' | grep "${xtty}" | awk '{print $3}')" 90 | 91 | if [ -z "${xdisplay}" ]; then 92 | echo "No X or XWayland process found from ${xtty}." 93 | exit 1 94 | fi 95 | 96 | isXWayland=true 97 | fi 98 | 99 | for pid in $(ps -u "${xuser}" -o pid=); do 100 | env="/proc/${pid}/environ" 101 | display="$(cat "${env}" | tr '\0' '\n' | grep -E '^DISPLAY=' | cut -d= -f2)" 102 | 103 | if [ -z "${display}" ] || [ "${display}" != "${xdisplay}" ]; then 104 | continue 105 | fi 106 | 107 | dbus="$(cat "${env}" | tr '\0' '\n' | grep -E '^DBUS_SESSION_BUS_ADDRESS=')" 108 | 109 | if [ -n "${dbus}" ]; then 110 | ! $isXWayland && xauth="$(cat "${env}" | tr '\0' '\n' | grep -E '^XAUTHORITY=')" 111 | break 112 | fi 113 | done 114 | 115 | if [ -z "${dbus}" ]; then 116 | echo 'No session bus address found.' 1>&2 117 | exit 1 118 | fi 119 | 120 | # XWayland does not need Xauthority 121 | if ! $isXWayland && [ -z "${xauth}" ]; then 122 | if [ ! -r "/home/${xuser}/.Xauthority" ]; then 123 | echo 'No Xauthority found.' 1>&2 124 | exit 1 125 | fi 126 | xauth="XAUTHORITY=/home/${xuser}/.Xauthority" 127 | fi 128 | 129 | $tFlag && echo "XUSER=${xuser}" || printf "%s\n%s\n" "TTY=${xtty}" "XUSER=${xuser}" 130 | ! $isXWayland && echo "${xauth}" 131 | 132 | printf "%s\n%s" "DISPLAY=${xdisplay}" "${dbus}" 133 | } 134 | 135 | main "$@" 136 | --------------------------------------------------------------------------------