source: epoptes/trunk/fuentes/epoptes-client/endsession @ 295

Last change on this file since 295 was 295, checked in by mabarracus, 4 years ago

copy trusty epoptes code

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1#!/bin/sh
2###########################################################################
3# Requests a logout, reboot or shutdown.
4# Currently it supports Gnome, KDE, XFCE and LXDE.
5# Usage: endsession --logout|--reboot|--shutdown
6#
7# Copyright (C) 2011 Alkis Georgopoulos <alkisg@gmail.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# On Debian GNU/Linux systems, the complete text of the GNU General
23# Public License can be found in `/usr/share/common-licenses/GPL'.
24###########################################################################
25
26die() {
27    echo "$@" >&2
28    exit 1
29}
30
31is_root() {
32    test $(id -u) -eq 0
33}
34
35is_ltsp() {
36    test -n "$LTSP_CLIENT"
37}
38
39do_logout() {
40    # Reset the xprop in case the user asks for a reboot, cancels it
41    # (e.g. unsaved work), and then he asks for a logout instead.
42    if [ $action = "logout" ] && is_ltsp; then
43        # LTSP_LOGOUT_ACTION might not exist, but we don't care
44        xprop -root -remove LTSP_LOGOUT_ACTION 2>/dev/null
45    fi
46
47    # Gnome
48    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
49      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
50      org.gnome.SessionManager.Logout uint32:1 2>&1 && return
51
52    # KDE
53    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
54      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
55      int32:0 int32:0 int32:0 2>&1 && return
56
57    # Mate
58    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
59      --dest=org.mate.SessionManager /org/mate/SessionManager \
60      org.mate.SessionManager.Logout uint32:1 2>&1 && return
61
62    # XFCE
63    xfce4-session-logout --logout 2>&1 && return
64
65    # LXDE
66    test -n "$_LXSESSION_PID" && kill "$_LXSESSION_PID" && return
67
68    die "I don't know how to logout in this environment"
69}
70
71do_reboot() {
72    if is_root; then
73        reboot && return
74    elif is_ltsp; then
75        # Notify ldm that we want to reboot after logoff
76        xprop -root -f LTSP_LOGOUT_ACTION 8s -set LTSP_LOGOUT_ACTION REBOOT
77        do_logout && return
78    fi
79
80    # Gnome
81    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
82      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
83      org.gnome.SessionManager.RequestReboot 2>&1 && return
84
85    # KDE
86    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
87      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
88      int32:0 int32:1 int32:0 2>&1 && return
89   
90    # Mate
91    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
92      --dest=org.mate.SessionManager /org/mate/SessionManager \
93      org.mate.SessionManager.RequestReboot 2>&1 && return
94   
95    # XFCE and LXDE
96    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
97      --dest=org.freedesktop.PowerManagement /org/freedesktop/PowerManagement \
98      org.freedesktop.PowerManagement.Reboot 2>&1 && return
99
100    # ConsoleKit is the last resort since it doesn't allow inhibiting
101    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
102      --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager \
103      org.freedesktop.ConsoleKit.Manager.Restart 2>&1 && return
104
105    die "I don't know how to reboot in this environment"
106}
107
108do_shutdown() {
109    if is_root; then
110        poweroff && return
111    elif is_ltsp; then
112        # Notify ldm that we want to poweroff after logoff
113        xprop -root -f LTSP_LOGOUT_ACTION 8s -set LTSP_LOGOUT_ACTION HALT
114        do_logout && return
115    fi
116
117    # Gnome
118    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
119      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
120      org.gnome.SessionManager.RequestShutdown 2>&1 && return
121
122    # KDE
123    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
124      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
125      int32:0 int32:2 int32:0 2>&1 && return
126   
127    # Mate
128    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
129      --dest=org.mate.SessionManager /org/mate/SessionManager \
130      org.mate.SessionManager.RequestShutdown 2>&1 && return
131
132    # XFCE and LXDE
133    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
134      --dest=org.freedesktop.PowerManagement /org/freedesktop/PowerManagement \
135      org.freedesktop.PowerManagement.Shutdown && return
136
137    # ConsoleKit is the last resort since it doesn't allow inhibiting
138    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
139      --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager \
140      org.freedesktop.ConsoleKit.Manager.Stop 2>&1 && return
141
142    die "I don't know how to shutdown in this environment"
143}
144
145# main
146# Hide all dbus output unless DEBUG is set.
147test -z "$DEBUG" && exec >/dev/null
148
149case "$1" in
150    -l|--logout)
151        action=logout
152        ;;
153    -r|--reboot)
154        action=reboot
155        ;;
156    -s|--shutdown)
157        action=shutdown
158        ;;
159    *)
160        die "Usage: endsession --logout|--reboot|--shutdown"
161        ;;
162esac
163
164do_$action
Note: See TracBrowser for help on using the repository browser.