#; kate: space-indent on; indent-width 2; syntax bash;

function RequireWineServer()
{
  # Automatic start of wineserver fails sometimes when spawning from daemon, thus make sure it is running beforehand.
  # For QS, wineserver must run before profile creation (or be patched)
  (
    UnlockStartup	# wineserver shall not inherit the lock
    exec "$TV_BIN_DIR/wine/bin/wineserver"
  )
}

# Delay until Xrandr changed the screen configuration. Wine does not like it if the screen configuration changes.
# e.g. If the screen "grows", The windows will still be confined to the old area, the tray icon may not work
# NOTE: To enable this, create the file "echo 15 > /opt/teamviewer/config/waitxrandr"
# where 15 is the amount of seconds to wait at most.
function XRandRWait()
{
  local xrandrConf="$TV_BASE_DIR/config/waitxrandr"
  local xrandrDelay
  local orgConf
  local chgConf
  local secs=0

#  [ -f "$xrandrConf" ] || return  # now using a default delay
  cmdExists xrandr     || return

  xrandrDelay=$(cat "$xrandrConf" 2> /dev/null)
  if ! (isStrNumeric "$xrandrDelay" && (( xrandrDelay >= 0  && xrandrDelay <= 100 ))); then
    echo "XRandRWait: No value set. Using default."
    xrandrDelay=3
  fi

  if [ "$(ps -p $PPID -o comm=)" != 'teamviewerd' ]; then
    echo "XRandRWait: Started by user."; return
  fi

  orgConf=$(XRandRCurConf)

  until [ $secs = $xrandrDelay ]; do
    chgConf=$(XRandRCurConf)

    if [ "$orgConf" != "$chgConf" ]; then
      break
    fi

    sleep 1
    let secs+=1
  done

  echo "Waited $secs seconds (of $xrandrDelay) for xrandr"
}

function XRandRCurConf()
{
  xrandr --current | tr '\n' '_'
}
