#!/bin/sh
#
#  xfce4
#
#  Copyright (C) 2017 Jarno Suni (8@iki.fi)
#
#  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
set -eu
# Save the state of DPMS; rely on the output format of `xset q`.
xstate=$(xset q | awk '/^DPMS /{getline; printf "%s %s %s ", $2, $4, $6;
	getline; if($3=="Enabled"){print "+"}else{print "-"}exit}')
# Enable monitor power management
xset dpms 0 0 15
$*
exit_code=$?
# Restore DPMS state
xset dpms $(printf %s "$xstate" | cut -d' ' -f1-3)
xset $(printf %s "$xstate" | cut -d' ' -f4)dpms
exit $exit_code
