#!/usr/bin/env python3
# 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 .
import os
import sys
from subprocess import Popen, PIPE
from aux import *
from curve import *
LIBDIR = 'bin'
'''
:str Path to libraries, '/usr/lib' is standard
'''
LIBEXECDIR = 'bin'
'''
:str Path to executable libraries, '/usr/libexec' is standard
'''
## Add the path to libraries to the list of paths to Python modules
sys.path.append(LIBDIR)
## Load DRM module
try:
from blueshift_drm import *
except:
# Not compiled with DRM support
pass
## Load W32 GDI module
try:
from blueshift_w32gdi import *
except:
# Not compiled with W32 GDI support
pass
## Load Quartz module
try:
from blueshift_quartz import *
except:
# Not compiled with Quartz support
pass
randr_opened = None
'''
:(int, str)? The index of the, with RandR, opened X screen and X display, if any
'''
vidmode_opened = None
'''
:(int, str)? The index of the, with VidMode, opened X screen and X display, if any
'''
w32gdi_opened = False
'''
:bool Whether W32 GDI is in use
'''
quartz_opened = False
'''
:bool Whether Quartz is in use
'''
def close_c_bindings():
'''
Close all C bindings and let them free resources and close connections
'''
global randr_opened, vidmode_opened, w32gdi_opened, quartz_opened
if randr_opened is not None:
# Close RandR connection if open
from blueshift_randr import randr_close
randr_opened = None
randr_close()
if vidmode_opened is not None:
# Close VidMode connection if open
from blueshift_vidmode import vidmode_close
vidmode_opened = None
vidmode_close()
if w32gdi_opened:
# Close W32 GDI connection if open
w32gdi_opened = False
w32gdi_close()
if quartz_opened:
# Close Quartz connection if open
quartz_opened = False
quartz_close()
# Close DRM connection if open
drm_manager.close()
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
'''
from blueshift_randr import randr_open, randr_read, randr_close
global randr_opened
# Open new RandR connection if non is open or one is open to the wrong screen or display
if (randr_opened is None) or not (randr_opened == (screen, display)):
# Close RandR connection, if any, because its is connected to the wrong screen or display
if randr_opened is not None:
randr_close()
# Open RandR connection
if randr_open(screen, display if display is None else display.encode('utf-8')) == 0:
randr_opened = (screen, display)
else:
raise Exception('Cannot open RandR connection')
# Read current curves and create function
return ramps_to_function(*(randr_read(crtc)))
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
'''
from blueshift_vidmode import vidmode_open, vidmode_read, vidmode_close
global vidmode_opened
# Open new VidMode connection if non is open or one is open to the wrong screen or display
if (vidmode_opened is None) or not (vidmode_opened == (screen, display)):
# Close VidMode connection, if any, because its is connected to the wrong screen or display
if vidmode_opened is not None:
vidmode_close()
# Open VidMode connection
if vidmode_open(screen, display if display is None else display.encode('utf-8')):
vidmode_opened = (screen, display)
else:
raise Exception('Cannot open VidMode connection')
# Read current curves and create function
return ramps_to_function(*(vidmode_read()))
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
'''
# Get DRM connection, open if necessary
connection = drm_manager.open_card(screen)
# Read current curves and create function
return ramps_to_function(*(drm_get_gamma_ramps(connection, crtc, i_size)))
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
'''
global w32gdi_opened
# Open W32 GDI connection if necessary
if not w32gdi_opened:
w32gdi_opened = True
if (not w32gdi_open() == 0):
raise Exception("Could not open W32 GDI connection")
# Read current curves and create function
return ramps_to_function(*(w32gdi_read(crtc)))
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
'''
global quartz_opened
# Open Quartz connection if necessary
if not quartz_opened:
quartz_opened = True
if (not quartz_open() == 0):
raise Exception("Could not open Quartz connection")
# Read current curves and create function
return ramps_to_function(*(quartz_read(crtc)))
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
'''
from blueshift_randr import randr_open, randr_apply, randr_close
global randr_opened
# Select CRTC:s
crtcs = list(crtcs) if len(crtcs) > 0 else [-1]
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
# Open new RandR connection if non is open or one is open to the wrong screen or display
if (randr_opened is None) or not (randr_opened == (screen, display)):
# Close RandR connection, if any, because its is connected to the wrong screen or display
if randr_opened is not None:
randr_close()
# Open RandR connection
if randr_open(screen, display if display is None else display.encode('utf-8')) == 0:
randr_opened = (screen, display)
else:
raise Exception('Cannot open RandR connection')
try:
# Apply adjustments
if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0:
raise Exception('Cannot use RandR to apply colour adjustments')
except OverflowError:
pass # Happens on exit by TERM signal
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
'''
from blueshift_vidmode import vidmode_open, vidmode_apply, vidmode_close
global vidmode_opened
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
# Open new vidmode connection if non is open or one is open to the wrong screen or display
if (vidmode_opened is None) or not (vidmode_opened == (screen, display)):
# Close vidmode connection, if any, because its is connected to the wrong screen or display
if vidmode_opened is not None:
vidmode_close()
# Open vidmode connection
if vidmode_open(screen, display if display is None else display.encode('utf-8')):
vidmode_opened = (screen, display)
else:
raise Exception('Cannot open VidMode connection')
try:
# Apply adjustments
if not vidmode_apply(R_curve, G_curve, B_curve) == 0:
raise Exception('Cannot use VidMode to apply colour adjustments')
except OverflowError:
pass # Happens on exit by TERM signal
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`
'''
# Get DRM connection, open if necessary
connection = drm_manager.open_card(screen)
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
try:
# Select all CRTC:s if none have been selected
if len(crtcs) == 0:
crtcs = range(drm_crtc_count(connection))
# Apply adjustments
drm_set_gamma_ramps(connection, list(crtcs), i_size, R_curve, G_curve, B_curve)
except OverflowError:
pass # Happens on exit by TERM signal
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`
'''
global w32gdi_opened
# Open W32 GDI connection if necessary
if not w32gdi_opened:
w32gdi_opened = True
if (not w32gdi_open() == 0):
raise Exception("Could not open W32 GDI connection")
# Convert curves to [0, 0xFFFF] integer lists
(R_curve, G_curve, B_curve) = translate_to_integers()
try:
# Select all CRTC:s if none have been selected
if len(crtcs) == 0:
crtcs = range(w32gdi_crtc_count())
# Apply adjustments
w32gdi_apply(list(crtcs), R_curve, G_curve, B_curve)
except OverflowError:
pass # Happens on exit by TERM signal
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`
'''
global quartz_opened
# Open Quartz connection if necessary
if not quartz_opened:
quartz_opened = True
if (not quartz_open() == 0):
raise Exception("Could not open Quartz connection")
try:
# Select all CRTC:s if none have been selected
if len(crtcs) == 0:
crtcs = range(quartz_crtc_count())
# Apply adjustments
quartz_apply(list(crtcs), r_curve, g_curve, b_curve)
except OverflowError:
pass # Happens on exit by TERM signal
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