File: x_helper_functions

package info (click to toggle)
powersave 0.14.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,764 kB
  • ctags: 999
  • sloc: sh: 11,357; cpp: 8,103; ansic: 2,631; makefile: 388
file content (163 lines) | stat: -rwxr-xr-x 5,434 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the   #
# Free Software Foundation; either version 2 of the License, or (at you   #
# option) any later version.                                              #
#                                                                         #
# This program is distributed in the hope that it will be useful, but     #
# WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       #
# General Public License for more details.                                #
#                                                                         #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., #
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  #
#                                                                         #
###########################################################################

# first get helper functions (e.g. DEBUG, load_scheme, ...)
#. "/usr/lib/powersave/scripts/helper_functions"

# program locations:
KDIALOG_BIN=$KDE_BINDIR/kdialog
ZENITY_BIN=$GNOME_BINDIR/zenity
XMESSAGE_BIN=/usr/X11R6/bin/xmessage

# this fixes an obscure bug if you restart powersaved from an root terminal
# that has access to the X server. 
unset XAUTHORITY

get_x_user(){
    [ -n "$X_USER" -a -n "$DISP" ] && return 0
    local DUMMY
    read DUMMY X_USER DISP DUMMY < <($WTTYHX -v)
    return 0
}

get_x_users(){
    local DUMMY i
    i=0
    KDE_RUNNING[$i]=false
    while read X_USERS[$i] DISPS[$i] DUMMY; do
      su - ${X_USERS[$i]} -c "DISPLAY=${DISPS[$i]} $KDE_BINDIR/dcopfind kdesktop >/dev/null 2>&1" && \
            KDE_RUNNING[$i]=true
      let i++
    done < <($WTTYHX -a)
    return 0
}

# chosses the appropriate popup which is available
# it is one of kdialog, zenity or simply xmessage
# this should be handled by graphical clients like kpowersave. We leave
# this in here for those who want to do homemade notification, a stock
# installation will soon only use the xmessage popup as a fallback.
choose_popup(){
    local MESSAGE="$1"
    local LEVEL="$2"
    local RET="1"

    # x_helper_functions
    if $KDE_RUNNING; then
	popup_kdialog "$MESSAGE" "$LEVEL"
	RET=$?
    elif $ZENITY; then
	popup_zenity "$MESSAGE" "$LEVEL"
	RET=$?
    else
	popup_xmessage "$MESSAGE" "$LEVEL"
	RET=$?
    fi
    return $RET
}

check_x_access(){
    local RET
    [ -n "$X_USER" -a -n "$DISP" ] || return 1
    su - $X_USER -c "DISPLAY=$DISP xset q > /dev/null"
    RET=$?
    return $RET
}
    
# the popup_* functions pop up a window containing $MESSAGE, a ok or a yes and a no button.
#
# no KDE, no GNOME: just xmessage. Not too nice.
popup_xmessage(){
    local ARG RET
    local MESSSAGE="$1"
    local LEVEL="$2"
    local WAIT="&"
    case "$LEVEL" in
        QUESTION)
            ARG="-buttons Yes,No" 
            WAIT="" ;;
        ERROR|WARN|INFO)
            ARG="" ;;
        *)  ARG=""
            DEBUG "wrong LEVEL in popup_xmessage: '$LEVEL', assuming 'empty'" WARN ;;
    esac
    get_x_user
    if check_x_access; then
        su - $X_USER -c "DISPLAY=$DISP $XMESSAGE_BIN -center -fn \
            '-misc-fixed-bold-r-*-*-18-*-*-*-*-*-iso10646-1' $ARG\
            '`echo $MESSAGE|fmt -w 60 `' $WAIT"  # we re-format it to 60 chars
        RET=$?
    else
        RET=255
    fi
    [ "$RET" = "101" ] && RET=0 # Yes
    [ "$RET" = "102" ] && RET=1 # No
    return $RET
}

# you want this actually only if you are really running KDE...
popup_kdialog(){
    local ARG
    local MSG="`echo $MESSAGE`" # kdialog does word-wrapping
    local WAIT="&"
    case "$LEVEL" in
        ERROR)    ARG="--error" ;;
        WARN)     ARG="--sorry" ;;
        INFO)     ARG="--msgbox" ;;
	QUESTION) ARG="--yesno"  
	          WAIT=""
		  ;;	          
        *)        ARG="--msgbox"
                DEBUG "wrong LEVEL in popup_kdialog: '$LEVEL', assuming 'INFO'" WARN ;;
    esac
    get_x_user
    [ "$X_USER" -a -n "$DISP" ] && \
        su - $X_USER -c "DISPLAY=$DISP $KDIALOG_BIN $ARG \"$MSG\" $WAIT" 
    return $?
}


# zenity is more lightweight than kdialog, but it's not in the default install
popup_zenity(){
    local ARG="--question"
    local MSG="`echo $MESSAGE`" # zenity does word-wrapping
    local WAIT="&"
    case "$LEVEL" in
        ERROR)    ARG="--error" ;;
        WARN)     ARG="--info" ;;
        INFO)     ARG="--info" ;;
	QUESTION) ARG="--question"
	          WAIT=""
		  ;;	
        *)        ARG="--info"
                  DEBUG "wrong LEVEL in popup_zenity: '$LEVEL', assuming 'INFO'" WARN ;;
    esac
    get_x_user
    if check_x_access; then
        su - $X_USER -c "DISPLAY=$DISP $ZENITY_BIN $ARG --text=\"$MSG\" $WAIT" 
        return $?
    else
	return 255
    fi
}