refactor pretty much everything. some features are still missing.
[lilass.git] / screen.py
1 #!/usr/bin/python3
2 # DSL - easy Display Setup for Laptops
3 # Copyright (C) 2012-2015 Ralf Jung <post@ralfj.de>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 import re, subprocess
20 from enum import Enum
21
22 ## utility functions
23
24 # execute a process, return output as iterator, throw exception if there was an error
25 # you *must* iterate to the end if you use this!
26 def processOutputGen(*args):
27     with subprocess.Popen(args, stdout=subprocess.PIPE) as p:
28         for line in p.stdout:
29             yield line.decode("utf-8")
30     if p.returncode != 0:
31         raise Exception("Error executing "+str(args))
32 def processOutputIt(*args):
33     return list(processOutputGen(*args)) # list() iterates over the generator
34
35 ## the classes
36
37 class RelativeScreenPosition(Enum):
38     '''Represents the relative position of the external screen to the internal one'''
39     LEFT      = 0
40     RIGHT     = 1
41     ABOVE     = 2
42     BELOW     = 3
43     MIRROR    = 4
44
45
46 class Resolution:
47     '''Represents a resolution of a screen'''
48     def __init__(self, width, height):
49         self.width = width
50         self.height = height
51     
52     def __eq__(self, other):
53         if not isinstance(other, Resolution):
54             return False
55         return self.width == other.width and self.height == other.height
56     
57     def __ne__(self, other):
58         return not self.__eq__(other)
59     
60     def __str__(self):
61         # get ratio
62         ratio = int(round(16.0*self.height/self.width))
63         if ratio == 12: # 16:12 = 4:3
64             strRatio = '4:3'
65         elif ratio == 13: # 16:12.8 = 5:4
66             strRatio = '5:4'
67         else: # let's just hope this will never be 14 or more...
68             strRatio = '16:%d' % ratio
69         return '%dx%d (%s)' %(self.width, self.height, strRatio)
70     
71     def __repr__(self):
72         return 'screen.Resolution('+self.forXrandr()+')'
73     
74     def forXrandr(self):
75         return str(self.width)+'x'+str(self.height)
76
77
78 class ScreenSetup:
79     '''Represents a screen configuration (relative to some notion of an "internal" and an "external" screen): Which screens are enabled with which resolution, how
80        are they positioned, which is the primary screen.'''
81     def __init__(self, intResolution, extResolution, relPosition = None, extIsPrimary = True):
82         '''The resolutions can be None to disable the screen, instances of Resolution. The last two arguments only matter if both screens are enabled.'''
83         assert intResolution is None or isinstance(intResolution, Resolution)
84         assert extResolution is None or isinstance(extResolution, Resolution)
85         
86         self.intResolution = intResolution
87         self.extResolution = extResolution
88         self.relPosition = relPosition
89         self.extIsPrimary = extIsPrimary or self.intResolution is None # external is always primary if it is the only one
90     
91     def getInternalArgs(self):
92         if self.intResolution is None:
93             return ["--off"]
94         args = ["--mode", self.intResolution.forXrandr()] # set internal screen to desired resolution
95         if not self.extIsPrimary:
96             args.append('--primary')
97         return args
98     
99     def getExternalArgs(self, intName):
100         if self.extResolution is None:
101             return ["--off"]
102         args = ["--mode", self.extResolution.forXrandr()] # set external screen to desired resolution
103         if self.extIsPrimary:
104             args.append('--primary')
105         if self.intResolution is None:
106             return args
107         # set position
108         args += [{
109                 RelativeScreenPosition.LEFT  : '--left-of',
110                 RelativeScreenPosition.RIGHT : '--right-of',
111                 RelativeScreenPosition.ABOVE : '--above',
112                 RelativeScreenPosition.BELOW : '--below',
113                 RelativeScreenPosition.MIRROR: '--same-as',
114             }[self.relPosition], intName]
115         return args
116
117
118 class ScreenSituation:
119     connectors = {} # maps connector names to lists of Resolution (empty list -> disabled connector)
120     internalConnector = None # name of the internal connector (will be an enabled one)
121     externalConnector = None # name of the used external connector (an enabled one), or None
122     
123     '''Represents the "screen situation" a machine can be in: Which connectors exist, which resolutions do they have, what are the names for the internal and external screen'''
124     def __init__(self, internalConnectorNames, externalConnectorNames = None):
125         '''Both arguments are lists of connector names. The first one which exists and has a screen attached is chosen for that class. <externalConnectorNames> can be None to
126            just choose any remaining connector.'''
127         # which connectors are there?
128         self._getXrandrInformation()
129         print(self.connectors)
130         # figure out which is the internal connector
131         self.internalConnector = self._findAvailableConnector(internalConnectorNames)
132         if self.internalConnector is None:
133             raise Exception("Could not automatically find internal connector, please use ~/.dsl.conf to specify it manually.")
134         print(self.internalConnector)
135         # and the external one
136         if externalConnectorNames is None:
137             externalConnectorNames = list(self.connectors.keys())
138             externalConnectorNames.remove(self.internalConnector)
139         self.externalConnector = self._findAvailableConnector(externalConnectorNames)
140         print(self.externalConnector)
141     
142     # Run xrandr and fill the dict of connector names mapped to lists of available resolutions.
143     def _getXrandrInformation(self):
144         connector = None # current connector
145         for line in processOutputGen("xrandr", "-q"):
146             # screen?
147             m = re.search(r'^Screen [0-9]+: ', line)
148             if m is not None: # ignore this line
149                 connector = None
150                 continue
151             # new connector?
152             m = re.search(r'^([\w\-]+) (dis)?connected ', line)
153             if m is not None:
154                 connector = m.groups()[0]
155                 assert connector not in self.connectors
156                 self.connectors[connector] = []
157                 continue
158             # new resolution?
159             m = re.search(r'^   ([\d]+)x([\d]+) +', line)
160             if m is not None:
161                 resolution = Resolution(int(m.groups()[0]), int(m.groups()[1]))
162                 assert connector is not None
163                 self.connectors[connector].append(resolution)
164                 continue
165             # unknown line
166             # not fatal, e.g. xrandr shows strange stuff when a display is enabled, but not connected
167             print("Warning: Unknown xrandr line %s" % line)
168     
169     # return the first available connector from those listed in <tryConnectors>, skipping disabled connectors
170     def _findAvailableConnector(self, tryConnectors):
171         for connector in tryConnectors:
172             if connector in self.connectors and len(self.connectors[connector]): # if the connector exists and is active (i.e. there is a resolution)
173                 return connector
174         return None
175     
176     # return available internal resolutions
177     def internalResolutions(self):
178         return self.connectors[self.internalConnector]
179     
180     # return available external resolutions (or None, if there is no external screen connected)
181     def externalResolutions(self):
182         if self.externalConnector is None:
183             return None
184         return self.connectors[self.externalConnector]
185     
186     # return resolutions available for both internal and external screen
187     def commonResolutions(self):
188         internalRes = self.internalResolutions()
189         externalRes = self.externalResolutions()
190         assert externalRes is not None
191         return [res for res in externalRes if res in internalRes]
192     
193     # compute the xrandr call
194     def forXrandr(self, setup):
195         # turn all screens off
196         connectorArgs = {} # maps connector names to xrand arguments
197         for c in self.connectors.keys():
198             connectorArgs[c] = ["--off"]
199         # set arguments for the relevant ones
200         connectorArgs[self.internalConnector] = setup.getInternalArgs()
201         if self.externalConnector is not None:
202             connectorArgs[self.externalConnector] = setup.getExternalArgs(self.internalConnector)
203         else:
204             assert setup.extResolution is None, "There's no external screen to set a resolution for"
205         # now compose the arguments
206         call = ["xrandr"]
207         for name in connectorArgs:
208             call += ["--output", name] + connectorArgs[name]
209         return call
210