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

# The functions contained on this file are specific for configuring screen
# resolutions through xrandr. These are necessary for using multihead on
# cards with more than one output.

# XXX: in this file we use the word SCREEN to refer to a video card output,
# not an X screen.

# SCREEN_X_ORIGIN sets the original position to open a window (0, 1024 ...)
SCREEN_X_ORIGIN=
SCREEN_SIZES=
SCREEN_AMOUNT=
OUTPUT_NAMES=
XRANDR_INFO_FILE=${MDM_ETC}/xrandr.info

# This function configures the screen layout
function xrandr_configure_layout () {

    local i

    xrandr_detect_screen_info

    # In some video cards this is needed:
    # We hope crtc numbers always start from 0 and increase one by one
    # after changing crtcs, we have to set modes to enable the output
    for (( i=0; i < SCREEN_AMOUNT; i++)); do
	xrandr --output ${OUTPUT_NAMES[i]} --crtc $i
	xrandr --output ${OUTPUT_NAMES[i]} --mode ${SCREEN_SIZES[i]}
    done

    for (( i=1; i < SCREEN_AMOUNT; i++)); do
        xrandr --output ${OUTPUT_NAMES[i]} --right-of ${OUTPUT_NAMES[i-1]}
    done
}

# This function sets $SCREEN_SIZES, $OUTPUT_NAMES and $SCREEN_AMOUNT.
function xrandr_detect_screen_info () {

    SCREEN_SIZES=(`xrandr | grep -A1 "\<connected"    |
                  tr -s ' ' | egrep "^ [0-9]*x[0-9]*" |
		  cut -d' ' -f2`)

    # According to driver in use, different outputs may occur, that is
    # the reason for us to check if there is a value on $SCREEN_SIZES
    if [ "${#SCREEN_SIZES[@]}" = "0" ]; then
        SCREEN_SIZES=`xrandr | grep "maximum" | awk '{print $(NF-2)"x"$NF}'`
    fi

    # In case the xrandr version is older than 1.2:
    if [ "${#SCREEN_SIZES[@]}" = "0" ]; then
        SCREEN_SIZES=`xrandr | egrep -m1 "( |\*)[0-9] " | awk '{print $2"x"$4}'`
    fi

    OUTPUT_NAMES=(`xrandr | grep "\<connected" | cut -d' ' -f1`)

    SCREEN_AMOUNT=${#OUTPUT_NAMES[@]}

    # If our xrandr version is too old, we don't even have an output name, and
    # SCREEN_AMOUNT is zero. So we won't do anything with these screens. But it
    # would be nice to set a value to OUTPUT_NAMES so that the xrandr.info file
    # don't get ugly =)
    if [ "$SCREEN_AMOUNT" = "0" ]; then
	OUTPUT_NAMES[0]="none"
    fi
}

# Used to detect x/y coordenates of screens (upper-left corner).
# It puts them on the $SCREEN_X_ORIGIN array.
function xrandr_set_screen_positions () {

    local i

    SCREEN_X_ORIGIN[0]=0
    for (( i=0; i < (SCREEN_AMOUNT-1); i++ )); do
	SCREEN_X_ORIGIN[i+1]=$((`echo ${SCREEN_SIZES[i]} |
				 cut -d'x' -f1` + ${SCREEN_X_ORIGIN[i]}))
    done
}

# Append an info file containing xrandr information.
function xrandr_append_info_file () {

    xrandr_detect_screen_info
    xrandr_set_screen_positions

    # Generating a header
    if [ ! -f "$XRANDR_INFO_FILE" ]; then
        echo "Xrandr info file generated on: `date`" >> $XRANDR_INFO_FILE
    fi
    echo                                         >> $XRANDR_INFO_FILE
    echo "DISPLAY=`echo $DISPLAY`"               >> $XRANDR_INFO_FILE
    echo "SCREEN_AMOUNT=$SCREEN_AMOUNT"          >> $XRANDR_INFO_FILE
    echo "OUTPUT_NAMES=${OUTPUT_NAMES[@]}"       >> $XRANDR_INFO_FILE
    echo "SCREEN_SIZES=${SCREEN_SIZES[@]}"       >> $XRANDR_INFO_FILE
    echo "SCREEN_X_ORIGIN=${SCREEN_X_ORIGIN[@]}" >> $XRANDR_INFO_FILE
    echo "------------ end ------------"         >> $XRANDR_INFO_FILE

    # Unsetting all vector positions
    unset SCREEN_X_ORIGIN
}
