# -*- python -*-

# This example identifies which monitors you have plugged
# in to the computer, and applied their proper calibration.


# 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/>.


# The colour temperature to apply.
temp = 6500

# List all connected outputs.
outputs = list_screens('drm' if ttymode else 'randr').find_by_connected(True)

# EDID of the primary monitors
edid_0 = '00ffffffffffff0010ac00504d5730372f0c01030e281e962b0cc9a057479b2712484ca44380a959a94f615971594559c28f31590101863d00c05100304040a013006c231100001e000000fd0030aa1e821d000a202020202020000000fc0044454c4c2050313133300a2020000000ff00364432353232424c3730574d0a00ea'

# EDID of the secondary monitor
edid_1 = '00ffffffffffff0010ac005045413035320c01030e281e962b0cc9a057479b2712484ca44380a959a94f615971594559c28f31590101863d00c05100304040a013006c231100001e000000fd0030aa1e821d000a202020202020000000fc0044454c4c2050313133300a2020000000ff003644323532324339353041450a0039'

# Gamma of the monitors
gammas = [(1.16, 1.15, 1.11), (1.10, 1.16, 1.10)]

# Beginning of possible warning messages.
warn_size = 'Warning: unknown monitor at %s and size of %i mm by %i mm'
warn_nosize = 'Warning: unknown monitor at %s and unknown size'

# Configure each monitor.
for output in outputs:
    # Data that identifies the monitor.
    edid = output.edid
    monitor = (output.name, output.widthmm, output.heightmm, edid is not None)
    known_size = (output.widthmm is not None) and (output.heightmm is not None)
    
    # Default gamma settings.
    monitor_gamma = (1, 1, 1)
    
    # Get the correct gamma settings for the monitor.
    if   (monitor == ('DVI-0',  364, 291, False)) or (edid == edid_0):  monitor_gamma = gammas[0]  # using RandR
    elif (monitor == ('VGA-0',  364, 291, False)) or (edid == edid_1):  monitor_gamma = gammas[1]  # using RandR
    elif (monitor == ('DVII-0', 400, 300, False)) or (edid == edid_0):  monitor_gamma = gammas[0]  # using DRM
    elif (monitor == ('VGA-0',  400, 300, False)) or (edid == edid_1):  monitor_gamma = gammas[1]  # using DRM
    elif edid is not None:
        # Base gamma corrections on the EDID.
        try:
            edid_parsed = EDID(edid)
            monitor_gamma = (edid_parsed.gamma_correction,)
            msg = '%s using automatic gamma correction, estimated gamma was %.2f'
            msg %= (warn_size, edid_parsed.gamma)
            print(msg % (monitor[0], edid_parsed.widthmm, edid_parsed.heightmm))
        except:
            if not known_size:
                print('%s and with unsupported EDID' % warn_nosize % monitor[0])
            else:
                print('%s with unsupported EDID' % warn_size % monitor[:3])
    elif not known_size:
        print('%s and without an EDID' % warn_nosize % monitor[0])
    else:
        print('%s without an EDID' % warn_size % monitor[:3])
    
    # Perform adjustments.
    start_over()
    temperature(temp, lambda t : divide_by_maximum(cmf_10deg(t)))
    gamma(*monitor_gamma)
    
    # Apply adjustments.
    (drm if ttymode else randr)(output.crtc, screen = output.screen)