# -*- python -*-

# This example uses option parsing and CRTC searching.
# `Screens.find_by_crtc` and `Screen.find_by_crtc`, are
# not used, those are not useful for anything. They can
# be used for seaching the number of connected monitors,
# but `Screen.crtc_count` is much more effective.


# This file is dual-licensed under GNU General Public License
# version 3 and GNU Free Documentation License version 1.3.


# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# 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/>.


# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# Permission is granted to copy, distribute and/or modify this document
# under the terms of the GNU Free Documentation License, Version 1.3
# or any later version published by the Free Software Foundation;
# with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
# You should have received a copy of the GNU General Public License
# along with this software package.  If not, see <http://www.gnu.org/licenses/>.


# We want to use the ad-hoc mode options.
uses_adhoc_opts = True

# Parse gamma option from ad-hoc mode options.
gamma_ = parser.opts['--gamma']
if gamma_ is None:
    gamma_ = [1, 1, 1]
else:
    if len(gamma_) > 1:
        print('--gamma can only be used once')
        sys.exit(1)
    gamma_ = [float(x) for x in gamma_[0].split(':')]

# Parse temperature option from ad-hoc mode options.
temperature_ = parser.opts['--temperature']
if temperature_ is None:
    temperature_ = 5000
else:
    if len(temperature_) > 1:
        print('--temperature can only be used once')
        sys.exit(1)
    temperature_ = float(temperature_[0])


# Read configuration script options.
parser = ArgParser('Colour temputare controller',
                   '%s [-p] -c %s -- [options]' % (sys.argv[0], conf_opts[0]),
                   'Example configuration script using option parsing\n'
                   'and CRTC searching functions.',
                   None, True, ArgParser.standard_abbreviations())

parser.add_argumentless(['-h', '-?', '--help'], 0, 'Print this help information')
parser.add_argumented(['-n', '--name'], 0, 'NAME', 'Select output by name')
parser.add_argumented(['-e', '--edid'], 0, 'EDID', 'Select output by monitor extended display identification data')
parser.add_argumented(['-s', '--size'], 0, 'WIDTH_MM:HEIGHT_MM', 'Select output by monitor size')

parser.parse(conf_opts)
parser.support_alternatives()

if parser.opts['--help'] is not None:
    parser.help()
    sys.exit(0)


# Find CRTC:s.
screens = list_screens('drm' if ttymode else 'randr')
outputs = []

# Find CRTC:s by name.
if parser.opts['--name'] is not None:
    for name in parser.opts['--name']:
        outputs += screens.find_by_name(name)

# Find CRTC:s by monitor extended display identification data.
if parser.opts['--edid'] is not None:
    for edid in parser.opts['--edid']:
        outputs += screens.find_by_edid(edid)

# Find CRTC:s by monitor size.
if parser.opts['--size'] is not None:
    for size in parser.opts['--size']:
        outputs += screens.find_by_size(*[int(x) for x in size.split(':')])


# Adjust colour curves.
for output in outputs:
    # Perform adjustments.
    start_over()
    temperature(temperature_, lambda t : divide_by_maximum(cmf_10deg(t)))
    gamma(*gamma_)
    
    # Apply adjustments.
    (drm if ttymode else randr)(output.crtc, screen = output.screen)