summaryrefslogtreecommitdiffstats
path: root/src/libgammaman.py
blob: fb339078740bf241b0c3ada74628ef4bf8e0c3d7 (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
#!/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 <http://www.gnu.org/licenses/>.

# This module is responsible for keeping track of resources for
# the monitor module.

import libgamma


cache = {}
'''
Resource cache
'''


def get_method(name):
    '''
    Translate an adjustment method name into an ID
    
    @param   name:str?  The adjustment method's name
    @return  :int       The adjustment method's ID
    '''
    method = { 'randr'   : libgamma.LIBGAMMA_METHOD_X_RANDR
             , 'vidmode' : libgamma.LIBGAMMA_METHOD_X_VIDMODE
             , 'drm'     : libgamma.LIBGAMMA_METHOD_LINUX_DRM
             , 'w32gdi'  : libgamma.LIBGAMMA_METHOD_W32_GDI
             , 'quartz'  : libgamma.LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS
             , 'dummy'   : libgamma.LIBGAMMA_METHOD_DUMMY
             , None      : None
             }
    method = method[name] if name in method else None
    if name is None:
        method = libgamma.list_methods(0)[0]
    elif method is None:
        raise Exception('Invalid method: %s' % name)
    elif not libgamma.is_method_available(method):
        raise Exception('Invalid method: %s' % name)
    return method


def get_display(display, method):
    '''
    Get a display
    
    @param   display:str?       The display ID
    @param   method:int         The adjustment method
    @return  :libgamma.Display  Display object
    '''
    if display is None:
        display = libgamma.method_default_site(method)
    if method not in cache:
        cache[method] = {}
    cache_displays = cache[method]
    if display not in cache_displays:
        site = libgamma.Site(method, display)
        cache_displays[display] = site
        site.cache_screens = {}
    return cache_displays[display]


def get_screen(screen, display, method):
    '''
    Get a screen
    
    @param   screen:int        The screen index
    @param   display:str?      The display ID
    @param   method:int        The adjustment method
    @return  :libgamma.Screen  Screen object
    '''
    display = get_display(display, method)
    cache_screens = display.cache_screens
    if screen not in cache_screens:
        partition = libgamma.Partition(display, screen)
        cache_screens[screen] = partition
        partition.cache_crtcs = {}
    return cache_screens[screen]


def get_crtc(crtc, screen, display, method):
    '''
    Get a CRTC
    
    @param   crtc:int        The CRTC index
    @param   screen:int      The screen index
    @param   display:str?    The display ID
    @param   method:int      The adjustment method
    @return  :libgamma.CRTC  CRTC object
    '''
    screen = get_screen(screen, display, method)
    cache_crtcs = screen.cache_crtcs
    if crtc not in cache_crtcs:
        monitor = libgamma.CRTC(screen, crtc)
        cache_crtcs[crtc] = monitor
        (monitor.info, _) = monitor.information(~0)
    return cache_crtcs[crtc]


def close():
    '''
    Release all resources
    '''
    global cache
    del cache
    cache = {}