# DSL - easy Display Setup for Laptops
-# Copyright (C) 2012 Ralf Jung <post@ralfj.de>
+# Copyright (C) 2012 Ralf Jung <post@ralfj.de>
+# Copyright (C) 2012-2015 Constantin Berhard<constantin@exxxtremesys.lu>
#
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
-# along with this program (gpl.txt); if not, write to the Free Software
+# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-import subprocess
-from dsl import RelativeScreenPosition, ScreenSetup, res2user
+from screen import RelativeScreenPosition, ScreenSetup, processOutputIt
-def userChoose (title, choices, returns):
- assert len(choices) == len(returns)
- p = subprocess.Popen(["zenity", "--list", "--text="+title, "--column="]+choices, stdout=subprocess.PIPE)
- switch = zip (choices,returns)
- for line in p.stdout:
- return switch.get(line, None)
+def userChoose (title, choices, returns, fallback):
+ assert len(choices) == len(returns)
+ args = ["zenity", "--list", "--text="+title, "--column="]+choices
+ switch = dict (list(zip (choices,returns)))
+ try:
+ for line in processOutputIt(*args):
+ return switch.get(line.strip(), fallback)
+ except Exception:
+ # on user cancel, the return code of zenity is nonzero
+ return fallback
+ return fallback
def run (internalResolutions, externalResolutions):
- relpos = userChoose ("Position of external screen", ["Left of internal screen", "Right of internal screen", "Use external screen only"], [RelativeScreenPosition.LEFT, RelativeScreenPosition.RIGHT, RelativeScreenPosition.EXTERNAL_ONLY])
- if relpos == None:
- return None
- intres = internalResolutions[0]
- if relpos != RelativeScreenPosition.EXTERNAL_ONLY:
- intres = userChoose ("internal display resolution", map(res2user,internalResolutions), internalResolutions)
- if intres == None:
- return None
- extres = userChoose ("external display resolution", map(res2user,externalResolutions), externalResolutions)
- if extres == None:
- return None
- extprim = userChoose ("Which display should be the primary display?", ["internal display", "external display"], [False, True])
- if extprim == None:
- return None
- return ScreenSetup(relpos,intres,extres,extprim)
+ relpos = userChoose ("Position of external screen", ["Left of internal screen", "Right of internal screen"], [RelativeScreenPosition.LEFT, RelativeScreenPosition.RIGHT], None)
+ if relpos == None:
+ return None
+ intres = internalResolutions[0]
+ extres = externalResolutions[0]
+ extprim = True
+ extres = userChoose ("external display resolution", list(map(str,externalResolutions)), externalResolutions, None)
+ if extres == None:
+ return None
+ if extprim == None:
+ extprim = userChoose ("Which display should be the primary display?", ["internal display", "external display"], [False, True], None)
+ if extprim == None:
+ return None
+ return ScreenSetup(intres, extres, relpos, extIsPrimary = extprim)