#!/bin/bash

# Copyright (C) 2004-2007 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# 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 your 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 Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This configuration runs Xephyr on XDMCP

# TODO: use XAUTHORITY correctly, we MUST have a secure environment

XAUTH_FILE=${MDM_RUN}/Xauth
XEPHYR_WRAPPER=${MDM_SCRIPTS}/xephyr-wrapper

function display_manager_init () {
    # This function should stop running processes, remove logs, links, locks, or
    # anything your display manager might need.
    # Consider calling display_manager_stop here if you don't need anything
    # else.
    display_manager_stop
}

function display_manager_start_monoseat () {
    # This function is called when the computer has only 1 video card.
    # You should open only 1 xserver and that's it: normal usage, not multiseat
    # Do NOT configure devices or stuff like that
    # We assume $DISPLAY is already set here.

    X -config $MDM_XORG_CONF			\
       -${CONNECTION_TYPE} ${CONNECTION_TARGET} 2>&1 &

    PID=$!
    echo $PID > $MDM_PIDS/xephyr-xdmcp-X.pid
    wait $PID
    rm -f $MDM_PIDS/xephyr-xdmcp-X.pid
}

function display_manager_start_underneath_xserver () {
    # In case we're using nested xservers (like Xephyr), we'll need to start an
    # xserver to run the nested servers on top of it
    # Don't put login screen on this server!
    # We assume $DISPLAY is already set here.

    # Without -noreset the configuration process doesn't work properly
    X -noreset -config $MDM_XORG_CONF 2>&1 &
    PID=$!
    echo $PID > $MDM_PIDS/xephyr-xdmcp-X.pid
    
    # XXX Wait for sigusr1, that X will send when it gets ready 
    sleep 5

    xauth -f ${XAUTH_FILE} generate :0
}

function display_manager_start_seat () {
    # This is where we start the xserver that will have a login screen
    # In case of multiseat with Xephyr, this function starts the Xephyrs and
    # puts the login screens on them.
    # We assume $DISPLAY is already set here.
    # This function is called by mdm-start-seat, which gives you variables:
    #   - $KEYBOARD and $MOUSE, which point to evdev device nodes.
    #   - $SEAT_DISPLAY variable corresponds to the number that the user will
    #     have to press on the keyboard. Might be useful here.
    #   - $MY_SCREEN_SIZE for the size of screen (needed when running nested
    #     xservers)
    #   - $MY_XKB_LAYOUT and $MY_XKB_MODEL, for the keyboard associated with the
    #      seat

    echo "xephyr-wrapper  :${SEAT_DISPLAY} -br"
    echo "       -parent $SEAT_WINDOW_ID"
    echo "       -mouse evdev,,device=$MOUSE"
    echo "       -keybd"
    echo -n "        evdev,,device=$KEYBOARD,xkbmodel=$MY_XKB_MODEL,"
    echo             "xkblayout=$MY_XKB_LAYOUT"
    echo "       -${CONNECTION_TYPE} ${CONNECTION_TARGET} 2>&1 &"

    $XEPHYR_WRAPPER :${SEAT_DISPLAY} -br				             \
    	    -parent ${SEAT_WINDOW_ID} 				             \
    	    -mouse evdev,,device=$MOUSE					     \
	    -keybd 							     \
     evdev,,device=$KEYBOARD,xkbmodel=$MY_XKB_MODEL,xkblayout=$MY_XKB_LAYOUT \
	    -${CONNECTION_TYPE} ${CONNECTION_TARGET} 2>&1 &

    PID=$!
    echo $PID > $MDM_PIDS/Xephyr.${SEAT_DISPLAY}.pid
    wait $PID
    rm -f $MDM_PIDS/xephyr-xdmcp-Xephyr.${SEAT_DISPLAY}.pid
}

function display_manager_stop () {
    # This function should kill everything it has to kill, so that we REALLY
    # stop all the seats, kill all the Xephyrs, remove locks, links and stuff
    # like that.
    for i in `ls $MDM_PIDS/xephyr-xdmcp*.pid 2> /dev/null`; do
	kill `cat $i` 2> /dev/null
	rm -f $i
    done

    rm -f $XAUTH_FILE
}
