#!/bin/bash

########################################################################
#
#PROJECT:      Unified Sessions Manager
#AUTHOR:       Arno-Can Uestuensoez - acue@UnifiedSessionsManager.org
#MAINTAINER:   Arno-Can Uestuensoez - acue_sf1@sourceforge.net
#SHORT:        ctys
#CALLFULLNAME: Commutate To Your Session
#LICENCE:      GPL3
#VERSION:      01_02_007a17
#
########################################################################
#
# Copyright (C) 2007 Arno-Can Uestuensoez (UnifiedSessionsManager.org)
#
########################################################################


_myLIBNAME_geometry="${BASH_SOURCE}"
_myLIBVERS_geometry="01.02.002c01"
libManInfoAdd "${_myLIBNAME_geometry}" "${_myLIBVERS_geometry}"

export _myLIBBASE_geometry="`dirname ${_myLIBNAME_geometry}`"


#FUNCBEG###############################################################
#NAME:
#  getScreenOffset
#
#TYPE:
#  bash-function
#
#DESCRIPTION:
#  Reads by default from "/etc/X11/xorg.conf" or from any other when 
#  given, the Screen-Label, xoffset, and yoffset for given ServerLayout
#  and screen, where screen could be given by it's index or label.
#
#ATTENTION:
#  SPECIAL REQUIREMENT:
#   Current implementation requires "Identifier" as first
#   entry in LayoutSections
#
#EXAMPLE:
#
#  XORGCFG=${1:-/etc/X11/xorg.conf}
#  DEBUG=1
#
#  echo
#  echo "Layout[all] Screen0 -> $XORGCFG"
#  getScreenOffset $DEBUG "Layout[all]" "Screen0" 0 $XORGCFG
#  echo "ret=$?"
#  echo
#
#PARAMETERS:
#
# $1: DEBUG:        0=off, else on
# $2: SECTIONNAME:  Literal name of LayoutSection to use, if empty first is taken.
# $3: SCREENALIAS:  Literal name of Screen section (used in LayoutSection), if
#                   empty SCREENNR is used
# $4: SCREENNR:     Index number of screen, if SCREENALIAS given, this will be 
#                   ignored. Range is (0<=N)
# $5: XORGCFG:      Pathname to used configuration file for Xorg, if empty
#                   default wil be used.
#
#OUTPUT:
#  RETURN:
#
#  VALUES:
#    "%s %d %d", $3, $4, $5
#      $3: Screen label from xorg.conf
#      $4: xoffset
#      $5: yoffset
#  
#FUNCEND###############################################################
function getScreenOffset () {
  local XORGCFG=${5:-/etc/X11/xorg.conf}
  local D=$1

#printVerboseDebug ${DBG_LVL_UIE} $LINENO $BASH_SOURCE "<${0} ${*}>"

  cat ${XORGCFG}|\
    sed 's/"//g'|\
    awk -v dbg=${DBG_LVL_OVERKILL} -v d=$D -v sect=${2} -v nr=${4} -v alias=${3} -f ${_myLIBBASE_geometry}/geometry01.awk
}
export -f getScreenOffset





#FUNCBEG###############################################################
#NAME:
#  expandGeometry
#
#TYPE:
#  bash-function
#
#DESCRIPTION:
#
#
#
# Reminder/hints:
#   CTRL_GEOMETRY=`expandGeometry "${OPTARG}"`;
#   "<x-size>x<y-size>[[+,-]<x-offset>[+,-]<y-offset>]"
#   "<x-size>x<y-size>[:[<ScreenSection>|<ScreenIndex>][:[ServerLayout:]]]
#
#EXAMPLE:
# Test-Examples:
# -standard:xorg.conf
#   "500x500"
#   "500x500:Screen4"
#   "500x500:5:Layout[all]"
#   "500x500:Screen6:Layout[all]"
#
# -custom:xorg.conf
#   "400x400"
#   "400x400:Screen3"
#   "400x400:4:Layout[0,1]"
#   "400x400:Screen5:Layout[0,1]"
#
#PARAMETERS:
#
# $1: DEBUG:       
#     0=off, else on
# $2: GEOMETRYX:    
#     Extended geometry option.
#
#         <geometry>=<x-size>x<y-size>+<xoffset>+<yoffset>
#
#       or the positioning relevant part of 
#
#        <geometryExtended>=
#          <geometry>[:[<ScreenSection>|<ScreenIndex>][:[ServerLayout]]]
#
# $3: Config file.
#     Default:/etc/X11/xorg.conf
#
#OUTPUT:
#  RETURN:
#
#  VALUES:
#    As appropriate to given result:
#
#         <xsize>
#      or <xsize>x<ysize>
#      or <xsize>x<ysize>+<xoffset>
#      or <xsize>x<ysize>+<xoffset>+<yoffset>
#
#FUNCEND###############################################################
function expandGeometry () {
  local XORGCFG=${3:-/etc/X11/xorg.conf}
  local D=$1

  printVerboseDebug ${DBG_LVL_SUPPORT} $LINENO $BASH_SOURCE "$FUNCNAME $*"

  echo $2|\
    awk -F':' -v dbg=${DBG_LVL_OVERKILL} -v d=${D} -v xorgconf=${XORGCFG} -f ${_myLIBBASE_geometry}/geometry02.awk

}


#getScreenSize
#getScreenOffset
