aboutsummaryrefslogtreecommitdiffstats
path: root/xorg-xrandr/setres/set.py
blob: 27533bcf1463ca45c66f3768056f2c23a7dcbf0b (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
class Display:
    def __init__(self, display = None, deepclone = False):
        self.outputs = []
        self.fb      = None
        self.fbmm    = None ## TODO automatically
        self.dpi     = None ## TODO automatically
        if display is not None:
            self.outputs = [Output(output) if deepclone else output for output in display.outputs]
            self.fb   = display.fb
            self.fbmm = display.fbmm
            self.dpi  = display.dpi

    def to_xrandr(self):
        ret = ['xrandr']
        if self.fb   is not None:  ret += ['--fb',   self.mode]
        if self.fbmm is not None:  ret += ['--fbmm', self.pos]
        if self.dpi  is not None:  ret += ['--dpi',  self.rate]
        for output in self.outputs:
            ret += output.to_xrandr()
        return ret


class Output:
    def __init__(self, output):
        isnew = not isinstance(output, Output)
        self.output     = output if isnew else output.output
        self.mode       = None   if isnew else output.mode
        self.pos        = None   if isnew else output.pos
        self.rate       = None   if isnew else output.rate
        self.reflect    = None   if isnew else output.reflect
        self.rotate     = None   if isnew else output.rotate
        self.off        = False  if isnew else output.off
        self.crtc       = None   if isnew else output.crtc
        self.gamma      = None   if isnew else output.gamma
        self.brightness = None   if isnew else output.brightness
        self.panning    = None   if isnew else output.panning
        self.transform  = None   if isnew else output.transform
        self.scale      = None   if isnew else output.scale
        self.scale_from = None   if isnew else output.scale_from
        self.relpos     = None   if isnew else output.relpos
        self.relto      = None   if isnew else output.relto
        self.primary    = False  if isnew else output.primary

    def to_xrandr(self):
        ret = ['--output', self.output]
        if self.mode       is not None:  ret += ['--mode',       self.mode]
        if self.pos        is not None:  ret += ['--pos',        self.pos]
        if self.rate       is not None:  ret += ['--rate',       self.rate]
        if self.reflect    is not None:  ret += ['--reflect',    self.reflect]
        if self.rotate     is not None:  ret += ['--rotate',     self.rate]
        if self.crtc       is not None:  ret += ['--crtc',       self.crtc]
        if self.gamma      is not None:  ret += ['--gamma',      self.gamma]
        if self.brightness is not None:  ret += ['--brightness', self.brightness]
        if self.panning    is not None:  ret += ['--panning',    self.panning]
        if self.transform  is not None:  ret += ['--transform',  self.transform]
        if self.scale      is not None:  ret += ['--scale',      self.scale]
        if self.scale_from is not None:  ret += ['--scale-from', self.scale_from]
        if self.primary:                 ret += ['--primary']
        if self.relpos is not None and self.relto is not None:
            ret += ['--' + self.relpos, self.relto]
        return ret if not self.off else ['--output', self.output, '--off']


def apply_setup(display):
    import sys
    from subprocess import Popen
    p = Popen(display.to_xrandr(), stdin = sys.stdin, stdout = sys.stdout, stderr = sys.stderr)
    return p.wait() == 0