summaryrefslogtreecommitdiffstats
path: root/src/monitor.py
blob: b8f5cff48000732afc4247cc6187d51e3fa737b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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 Affero 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 Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys

# /usr/lib
LIBDIR = 'bin'
sys.path.append(LIBDIR)

from blueshift_randr import *
randr_opened = False


def translate_to_integers():
    '''
    Translate the curves from float to integer
    
    @param  :(int, int, int)  The red curve, the green curve and the blue curve, mapped to integers
    '''
    R_curve, G_curve, B_curve = [0] * i_size, [0] * i_size, [0] * i_size
    for i_curve, o_curve in ((r_curve, R_curve), (g_curve, G_curve), (b_curve, B_curve)):
        for i in range(i_size):
            o_curve[i] = int(i_curve[i] * (o_size - 1) + 0.5)
            if clip_result:
                o_curve[i] = min(max(0, o_curve[i]), (o_size - 1))
    return (R_curve, G_curve, B_curve)    


def close_c_bindings():
    global randr_opened
    if randr_opened:
        randr_close()


def randr(*crtcs):
    '''
    Applies colour curves using the X11 extension randr
    
    @param  *crtcs  The CRT controllers to use, all are used if none are specified
    '''
    global randr_opened
    crtcs = sum([1 << i for i in list(crtcs)])
    if crtcs == 0:
        crtcs = -1;
    
    (R_curve, G_curve, B_curve) = translate_to_integers()
    if not randr_opened:
        if randr_open(0) == 0: ## TODO support specifying screen
            randr_opened = True
        else:
            sys.exit(1)
    if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0:
        sys.exit(1)


def print_curves(*crtcs):
    '''
    Prints the curves to stdout
    '''
    (R_curve, G_curve, B_curve) = translate_to_integers()
    print(R_curve)
    print(G_curve)
    print(B_curve)