summaryrefslogtreecommitdiff
path: root/sysutils/consolekit/files/ck-get-x11-display-device
blob: 7efc08471660c342d7fe69da4e7688c09383b6f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

FSTAT="/usr/bin/fstat"
GREP="/usr/bin/grep"
AWK="/usr/bin/awk"
SED="/usr/bin/sed"
CAT="/bin/cat"

dispnum=0
if [ x"$1" = x"--display" ]; then
    if [ $# != 2 ]; then
	echo "usage: $0 [--display DISPLAY]"
	exit 1
    fi
    DISPLAY=$2
fi
if [ ! -z "${DISPLAY}" ]; then
    dispnum=$(echo ${DISPLAY} | ${SED} -E -e 's|:([0-9]+).*|\1|')
fi

if [ ! -f "/tmp/.X${dispnum}-lock" ]; then
    echo "ERROR: Failed to find X lock file for display ${dispnum}"
    exit 1
fi
pid=$(${CAT} "/tmp/.X${dispnum}-lock")

device=$(${FSTAT} -p ${pid} | ${GREP} ttyv | ${AWK} '{print $8}')

if [ -z "${device}" ]; then
    echo "ERROR: Failed to find TTY device for X server on display ${dispnum}"
    exit 1
fi

ret=""
for dev in ${device}; do
    if [ "${dev}" \> "${ret}" ]; then
	ret=${dev}
    fi
done

echo "/dev/${dev}"