summaryrefslogtreecommitdiffstats
path: root/src/monitor.py
blob: 11643d12952a7ba748990c31abfdc25cf4a6235c (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/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
from subprocess import Popen, PIPE

from curve import *

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

# /usr/libexec
LIBEXECDIR = 'bin'

randr_opened = None
vidmode_opened = None


def translate_to_integers():
    '''
    Translate the curves from float to integer
    
    @param  :(list<int>, list<int>, list<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():
    '''
    Close all C bindings and let them free resources and close connections
    '''
    global randr_opened, vidmode_opened
    if randr_opened is not None:
        from blueshift_randr import randr_close
        randr_opened = None
        randr_close()
    if vidmode_opened is not None:
        from blueshift_vidmode import vidmode_close
        vidmode_opened = None
        vidmode_close()


def randr_get(crtc = 0, screen = 0):
    '''
    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 that the monitor belong to
    @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
    if (randr_opened is None) or not (randr_opened == screen):
        if randr_opened is not None:
            randr_close()
        if randr_open(screen) == 0:
            randr_opened = screen
        else:
            sys.exit(1)
    (r, g, b) = randr_read(crtc)
    r = [y / 65535 for y in r]
    g = [y / 65535 for y in g]
    b = [y / 65535 for y in b]
    def fcurve(R_curve, G_curve, B_curve):
        for curve, cur in curves(R_curve, G_curve, B_curve):
            for i in range(i_size):
                y = int(curve[i] * (len(cur) - 1) + 0.5)
                y = min(max(0, y), len(cur) - 1)
                curve[i] = cur[y]
    return lambda : fcurve(r, g, b)


def vidmode_get(crtc = 0, screen = 0):
    '''
    Gets the current colour curves using the X11 extension vidmode
    
    @param   crtc:int    The CRTC of the monitor to read from
    @param   screen:int  The screen that the monitor belong to
    @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
    if (vidmode_opened is None) or not (vidmode_opened == screen):
        if vidmode_opened is not None:
            vidmode_close()
        if vidmode_open(screen) == 0:
            vidmode_opened = screen
        else:
            sys.exit(1)
    (r, g, b) = vidmode_read(crtc)
    r = [y / 65535 for y in r]
    g = [y / 65535 for y in g]
    b = [y / 65535 for y in b]
    def fcurve(R_curve, G_curve, B_curve):
        for curve, cur in curves(R_curve, G_curve, B_curve):
            for i in range(i_size):
                y = int(curve[i] * (len(cur) - 1) + 0.5)
                y = min(max(0, y), len(cur) - 1)
                curve[i] = cur[y]
    return lambda : fcurve(r, g, b)


def randr(*crtcs, screen = 0):
    '''
    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 that the monitors belong to
    '''
    from blueshift_randr import randr_open, randr_apply, randr_close
    global randr_opened
    crtcs = sum([1 << i for i in list(crtcs)])
    if crtcs == 0:
        crtcs = (1 << 64) - 1
    
    (R_curve, G_curve, B_curve) = translate_to_integers()
    if (randr_opened is None) or not (randr_opened == screen):
        if randr_opened is not None:
            randr_close()
        if randr_open(screen) == 0:
            randr_opened = screen
        else:
            sys.exit(1)
    try:
        if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0:
            sys.exit(1)
    except OverflowError:
        pass # Happens on exit by TERM signal


def vidmode(*crtcs, screen = 0):
    '''
    Applies colour curves using the X11 extension vidmode
    
    @param  crtcs:*int  The CRT controllers to use, all are used if none are specified
    @param  screen:int  The screen that the monitors belong to
    '''
    from blueshift_vidmode import vidmode_open, vidmode_apply, vidmode_close
    global vidmode_opened
    crtcs = sum([1 << i for i in list(crtcs)])
    if crtcs == 0:
        crtcs = (1 << 64) - 1
    
    (R_curve, G_curve, B_curve) = translate_to_integers()
    if (vidmode_opened is None) or not (vidmode_opened == screen):
        if vidmode_opened is not None:
            vidmode_close()
        if vidmode_open(screen) == 0:
            vidmode_opened = screen
        else:
            sys.exit(1)
    try:
        if not vidmode_apply(crtcs, R_curve, G_curve, B_curve) == 0:
            sys.exit(1)
    except OverflowError:
        pass # Happens on exit by TERM signal


def print_curves(*crtcs, screen = 0):
    '''
    Prints the curves to stdout
    
    @param  crtcs:*int  Dummy parameter
    @param  screen:int  Dummy parameter
    '''
    (R_curve, G_curve, B_curve) = translate_to_integers()
    print(R_curve)
    print(G_curve)
    print(B_curve)



def list_monitors():
    process = Popen([LIBEXECDIR + "/blueshift_idcrtc"], stdout = PIPE)
    lines = process.communicate()[0].decode('utf-8', 'error').split('\n')
    lines = [line.strip() for line in lines]
    screens, screen, output = None, None, None
    for line in lines:
        if line.startswith('Screen count: '):
            screens = [None] * int(line[len('Screen count: '):])
        elif line.startswith('Screen: '):
            screen_i = int(line[len('Screen: '):])
            screen = {}
            screens[screen_i] = screen
        elif line.startswith('CRTC count: '):
            screen['crtc'] = int(line[len('CRTC count: '):])
        elif line.startswith('Output count: '):
            screen['output'] = [None] * int(line[len('Output count: '):])
        elif line.startswith('Output: '):
            output_i = int(line[len('Output: '):])
            output = {'name' : None, 'connected' : False, 'width' : None, 'height' : None, 'crtc' : None}
            screen['output'][output_i] = output
        elif line.startswith('Name: '):
            output['name'] = line[len('Name: '):]
        elif line.startswith('Connection: '):
            output['connected'] = line[len('Connection: '):] == 'connected'
        elif line.startswith('Size: '):
            width, height = [int(x) for x in line[len('Size: '):].split(' ')]
            output['width'] = width
            output['height'] = height
        elif line.startswith('CRTC: '):
            output['crtc'] = int(line[len('CRTC: '):])
    return screens