#!/usr/bin/env python3
# Copyright © 2014, 2015, 2016, 2017 Mattias Andrée (maandree@kth.se)
#
# 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 .
# This module is responsible for access to the monitors.
import os
import sys
from subprocess import Popen, PIPE
from aux import *
from curve import *
import libgammaman
import libgamma
def close_c_bindings():
'''
Close all C bindings and let them free resources and close connections
'''
libgammaman.close()
def get_gamma(crtc = 0, screen = 0, display = None, *, method = None):
'''
Gets the current colour curves
@param crtc:int The CRTC of the monitor to read from
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
@param method:str? The adjustment method
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
# Get CRTC objet
method = libgammaman.get_method(method)
crtc = libgammaman.get_crtc(crtc, screen, display, method)
# Create gamma ramps
ramps = libgamma.GammaRamps(i_size, i_size, i_size)
# Get gamma
crtc.get_gamma(ramps)
return ramps_to_function(ramps.red, ramps.green, ramps.blue)
def set_gamma(*crtcs, screen = 0, display = None, method = None):
'''
Applies colour curves
@param crtcs:*int The CRT controllers to use, all are used if none are specified
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
@param method:str? The adjustment method
'''
for crtc in (0,) if len(crtcs) == 0 else crtcs:
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
# Get CRTC objet
method = libgammaman.get_method(method)
crtc = libgammaman.get_crtc(crtc, screen, display, method)
# Create gamma ramps
ramps = libgamma.GammaRamps(i_size, i_size, i_size)
# Set gamma
ramps.red[:] = R_curve
ramps.green[:] = G_curve
ramps.blue[:] = B_curve
crtc.set_gamma(ramps)
def randr_get(crtc = 0, screen = 0, display = None):
'''
Gets the current colour curves using the X11 extension RandR
@param crtc:int The CRTC of the monitor to read from
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
return get_gamma(crtc, screen, display, method = 'randr')
def vidmode_get(crtc = 0, screen = 0, display = None):
'''
Gets the current colour curves using the X11 extension VidMode
@param crtc:int The CRTC of the monitor to read from, dummy parameter
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
return get_gamma(crtc, screen, display, method = 'vidmode')
def drm_get(crtc = 0, screen = 0, display = None):
'''
Gets the current colour curves using DRM
@param crtc:int The CRTC of the monitor to read from
@param screen:int The graphics card to which the monitors belong, named `screen` for compatibility with `randr_get` and `vidmode_get`
@param display:str? Dummy parameter for compatibility with `randr_get` and `vidmode_get`
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
return get_gamma(crtc, screen, display, method = 'drm')
def w32gdi_get(crtc = 0, screen = 0, display = None):
'''
Gets the current colour curves using W32 GDI
@param crtc:int The CRTC of the monitor to read from
@param screen:int Dummy parameter for compatibility with `randr_get`, `vidmode_get` and `drm_get`
@param display:str? Dummy parameter for compatibility with `randr_get` and `vidmode_get`
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
return get_gamma(crtc, screen, display, method = 'w32gdi')
def quartz_get(crtc = 0, screen = 0, display = None):
'''
Gets the current colour curves using Quartz
@param crtc:int The CRTC of the monitor to read from
@param screen:int Dummy parameter for compatibility with `randr_get`, `vidmode_get` and `drm_get`
@param display:str? Dummy parameter for compatibility with `randr_get` and `vidmode_get`
@return :()→void Function to invoke to apply the curves that was used when this function was invoked
'''
return get_gamma(crtc, screen, display, method = 'quartz')
def randr(*crtcs, screen = 0, display = None):
'''
Applies colour curves using the X11 extension RandR
@param crtcs:*int The CRT controllers to use, all are used if none are specified
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
'''
set_gamma(*crtcs, screen = screen, display = display, method = 'randr')
def vidmode(*crtcs, screen = 0, display = None):
'''
Applies colour curves using the X11 extension VidMode
@param crtcs:*int The CRT controllers to use, all are used if none are specified, dummy parameter
@param screen:int The screen to which the monitors belong
@param display:str? The display to which to connect, `None` for current display
'''
set_gamma(*crtcs, screen = screen, display = display, method = 'vidmode')
def drm(*crtcs, screen = 0, display = None):
'''
Applies colour curves using DRM
@param crtcs:*int The CRT controllers to use, all are used if none are specified
@param screen:int The graphics card to which the monitors belong,
named `screen` for compatibility with `randr` and `vidmode`
@param display:str? Dummy parameter for compatibility with `randr` and `vidmode`
'''
set_gamma(*crtcs, screen = screen, display = display, method = 'drm')
def w32gdi(*crtcs, screen = 0, display = None):
'''
Applies colour curves using W32 GDI
@param crtcs:*int The CRT controllers to use, all are used if none are specified
@param screen:int Dummy parameter for compatibility with `randr`, `vidmode` and `drm`
@param display:str? Dummy parameter for compatibility with `randr` and `vidmode`
'''
set_gamma(*crtcs, screen = screen, display = display, method = 'w32gdi')
def quartz(*crtcs, screen = 0, display = None):
'''
Applies colour curves using Quartz
@param crtcs:*int The CRT controllers to use, all are used if none are specified
@param screen:int Dummy parameter for compatibility with `randr`, `vidmode` and `drm`
@param display:str? Dummy parameter for compatibility with `randr` and `vidmode`
'''
set_gamma(*crtcs, screen = screen, display = display, method = 'quartz')
def print_curves(*crtcs, screen = 0, display = None, compact = False):
'''
Prints the curves to stdout
@param crtcs:*int Dummy parameter
@param screen:int Dummy parameter
@param display:str? Dummy parameter
@param compact:bool Whether to print in compact form
'''
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
if compact:
# Print each colour curve with run-length encoding
for curve in (R_curve, G_curve, B_curve):
# Print beginning
print('[', end = '')
last, count = None, 0
for i in range(i_size):
if curve[i] == last:
# Count repetition
count += 1
else:
# Print value
if last is not None:
print('%i {%i}, ' % (last, count), end = '')
# Store new value
last = curve[i]
# Restart counter
count = 1
# Print last value and ending
print('%i {%i}]' % (last, count))
else:
# Print the red colour curve
print(R_curve)
# Print the green colour curve
print(G_curve)
# Print the blue colour curve
print(B_curve)
class Screens:
'''
Information about all screens
'''
def __init__(self):
'''
Constructor
'''
self.screens = None
def __find(self, f):
'''
Find monitors in each screen
@param f:(Screen)→list