#!/bin/bash

# bspwm_conf_xrandr --- BSPWM Xrandr settings for my laptop.
#
# Copyright (c) 2019 Protesilaos Stavrou <info@protesilaos.com>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

# XrandR functions
# ----------------

_laptop() {
	xrandr --output LVDS1 --mode 1366x768 --pos 0x0

    # This is specific to my Lenovo ThinkPad X220.  It improves the
    # reproduction of colours, especially when working with light themes.
    # Adjustments to these values have no effect if a program such as
    # `redshift' is running.
	xrandr --output LVDS1 --brightness 1.0 --gamma 0.76:0.75:0.68
}

_multihead() {
	_laptop

	# Configure the external display on the VGA port.  Making it the
	# `primary' can cause problems in certain contexts that assume this
	# to be the left- and top- most monitor.
	xrandr --output VGA1 --primary --mode 1920x1080 --pos 1366x0
}

# Conditions for running this script
# ----------------------------------

# If the VGA1 port connects to a monitor, the output of the variable
# will not be empty.  In which case it is assumed that I am using my
# external display, whose resolution I know in advance.  If the variable
# is empty, then no external monitor is connected.
#
# This is a simplistic approach which will not work if the external
# monitor has another resolution.  It will also fail if executed from
# another machine, say, another laptop that uses an HDMI connection
# instead.
if [ -n "$(xrandr --query | grep 'VGA1 connected')" ]; then
	_multihead
else
    _laptop
fi
