~ubuntu-branches/ubuntu/trusty/acpi-support/trusty

« back to all changes in this revision

Viewing changes to lib/power-funcs

  • Committer: Package Import Robot
  • Author(s): Steve Langasek
  • Date: 2012-03-03 15:49:47 UTC
  • Revision ID: package-import@ubuntu.com-20120303154947-nwrf3p1dt9dh3afu
Tags: 0.139
* lib/power-funcs: adjust getXuser and getXconsole to use consolekit
  instead of ps/who to identify the "foreground" session and the owner
  of a given X session, since lightdm is currently failing to record this
  information in utmp.  Also, add headers for each of these functions
  documenting how they're supposed to work, since it's completely
  non-obvious and some of the scripts already in acpi-support are using
  them wrong.  Thanks to Ayan George <ayan.george@canonical.com> for the
  preliminary patch.  LP: #933626, #893271.
* rotatescreen.sh: fix wrong use of getXconsole where getXuser is needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
PATH="$PATH:/usr/bin/X11"
6
6
POWERSTATE="/var/lib/acpi-support/powerstate"
7
7
 
 
8
# getXuser: get the user for the specified X display, or if none is
 
9
# specified, then the user for whatever X display we find.
 
10
# input: $displaynum: the X display to query
 
11
# output: $XAUTHORITY: the path to the xauth data used for connecting to the
 
12
#                      detected X display.
 
13
#         $user: the username of the user who owns the display.
8
14
getXuser() {
9
 
        user=$(who | awk "/:$displaynum)/ { print \$1; exit }")
 
15
        local display userhome uid
 
16
        if [ -z "$displaynum" ]; then
 
17
                display=.+
 
18
        else
 
19
                display=":$displaynum"
 
20
        fi
 
21
        uid=$(ck-list-sessions | awk 'BEGIN { unix_user = ""; } /^Session/ { unix_user = ""; } /unix-user =/ { gsub(/'\''/,"",$3); unix_user = $3; } /x11-display = '\'$display\''/ { print unix_user; exit (0); }')
10
22
 
11
 
        if [ x"$user" = x"" ]; then
12
 
                user=$(who | awk "/:$displaynum/ { print \$1; exit }")
13
 
        fi
14
 
        if [ x"$user" != x"" ]; then
15
 
                userhome=`getent passwd $user | cut -d: -f6`
 
23
        if [ -n "$uid" ]; then
 
24
                user=$(getent passwd $uid | cut -d: -f1)
 
25
                userhome=$(getent passwd $user | cut -d: -f6)
16
26
                export XAUTHORITY=$userhome/.Xauthority
17
27
        else
 
28
                user=
18
29
                export XAUTHORITY=""
19
30
        fi
20
31
}
21
32
 
 
33
# getXconsole: get the information for the active X console, if any.
 
34
#       calls getXuser to get information related to the logged-in user.
 
35
# input: none
 
36
# output: $XAUTHORITY: the path to the xauth data used for connecting to the
 
37
#                      detected X display.
 
38
#         $DISPLAY: the X display
 
39
#         $user: the username of the user who owns the display.
22
40
getXconsole() {
23
 
        console=`fgconsole`;
24
 
        displaynum=`ps t tty$console | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
25
 
        if [ x"$displaynum" != x"" ]; then
 
41
        local displaynum
 
42
 
 
43
        displaynum=$(ck-list-sessions | awk 'BEGIN { active = 0; } /^Session/ { active = 0; } /active = TRUE/ { active = 1; } active && /x11-display = '\':.+\''/ { gsub(/'\':*'/,"",$3); print $3; exit (0); }')
 
44
 
 
45
        if [ -n "$displaynum" ]; then
26
46
                export DISPLAY=":$displaynum"
27
47
                getXuser
28
48
        fi