#!/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                  #
#                                                                         #
###########################################################################

PATH=${PATH}:/usr/X11R6/bin # for xlock, xset etc.

# first get helper functions (e.g. DEBUG, load_scheme, ...)
. "${0%/*}/scripts/helper_functions"
. "${PUB_SCRIPT_DIR}/x_helper_functions"

# ugly. Do not dpms off the screen if we are only going to sleep state.
case "$1" in
    global.suspend2disk|global.suspend2ram|global.standby)
        SCREENSAVER_DPMS_OFF=no ;;
    *) ;;
esac

# Lock the screen using xlock.
xlock_screen_saver(){
    local LOCK_MODE=""
    DEBUG "process function: xlock_screen_saver" DEBUG

    get_x_user
    [ "$SCREENSAVER_BLANKONLY" == "yes" ] && LOCK_MODE="-mode blank"
    if [ "$X_USER" == "root" ]; then
	$DPMS xlock $LOCK_MODE -display $DISP &
    else
    	[ -n "$X_USER" -a -n "$DISP" ] && su - $X_USER -c "$DPMS xlock $LOCK_MODE -display $DISP &"
    fi
    if [ $? != "0" ]; then
	DEBUG "xlock failed for user '$X_USER'" DIAG
	return 1
    fi
    return 0
}

#####################################################################
# kde_screen_saver and xscreensaver_screen_saver are no longer used #
# use a graphical frontend like kpowersave instead                  #
# these are only here for your hacking pleasure...                  #
#####################################################################

# for KDE...
kde_screen_saver(){
    local LOCK_MODE=""
    local LOCK_MODE2=""
    DEBUG "process function: kde_screen_saver" DEBUG

    get_x_user
    local KDE_LOCK="DISPLAY=$DISP $KDE_BINDIR/dcop kdesktop KScreensaverIface"
    [ "$SCREENSAVER_BLANKONLY" == "yes" ] && {
        LOCK_MODE="$KDE_LOCK setBlankonly true;"
        LOCK_MODE2="$DEE_LOCK setBlankonly false;"
    }

    [ -n "$X_USER" -a -n "$DISP" ] && su - $X_USER -c \
        "$DPMS $LOCK_MODE $KDE_LOCK lock; X=\$?; $LOCK_MODE2 exit \$X"
    if [ $? != "0" ]; then
        DEBUG "Could not activate KDE screen saver." DIAG
        return 1;
    fi
    return 0
}

# for GNOME or other xscreensaver-users
xscreensaver_screen_saver(){
    local LOCK_MODE=""
    DEBUG "process function: xscreensaver_screen_saver" DEBUG

    get_x_user
    [ "$SCREENSAVER_BLANKONLY" == "yes" ] && \
        LOCK_MODE="xscreensaver-command -display $DISP -throttle &&"

    [ -n "$X_USER" -a -n "$DISP" ] && su - $X_USER -c \
        "$DPMS $LOCK_MODE xscreensaver-command -display $DISP -lock"
    if [ $? != "0" ]; then
        DEBUG "Could not activate xscreensaver." DIAG
        return 1;
    fi
    return 0
}

# we already log this at DIAG level in helper_functions
DEBUG "process script: screen_saver" INFO
get_x_users
i=0; ret=0

while [ "${X_USERS[$i]}" ]; do
    X_USER=${X_USERS[$i]}; DISP=${DISPS[$i]};
    # ugly hack
    if [ "$SCREENSAVER_DPMS_OFF" != "no" ]; then
        DPMS="xset -display $DISP dpms force off &"     # asynchronous or it may block
    else
        DPMS=""
    fi
    xlock_screen_saver
    if [ $? != "0" ]; then
       DEBUG "screen_saver failed for user '$X_USER' event ${EVENT_TYPE}${EVENT_SUBTYPE:+_$EVENT_SUBTYPE}${EVENT_EXT:+_$EVENT_EXT}" DIAG
       ret=1
    fi
    let i++
done
$SCRIPT_RETURN $EV_ID $ret "Script screen_saver"
EXIT $ret
