avoid needless list construction
[lilass.git] / zenity_dialogue.py
1 # DSL - easy Display Setup for Laptops
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 from dsl import RelativeScreenPosition, ScreenSetup, res2user, processOutputIt
19
20 def userChoose (title, choices, returns, fallback):
21         assert len(choices) == len(returns)
22         args = ["zenity", "--list", "--text="+title, "--column="]+choices
23         switch = dict (list(zip (choices,returns)))
24         for line in processOutputIt(*args):
25                 return switch.get(line.strip(), fallback)
26         return fallback
27
28 def run (internalResolutions, externalResolutions):
29         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], None)
30         if relpos == None:
31                 return None
32         intres = internalResolutions[0]
33         if relpos != RelativeScreenPosition.EXTERNAL_ONLY:
34                 intres = userChoose ("internal display resolution", list(map(res2user,internalResolutions)), internalResolutions, internalResolutions[0])
35         extres = userChoose ("external display resolution", list(map(res2user,externalResolutions)), externalResolutions, externalResolutions[0])
36         extprim = userChoose ("Which display should be the primary display?", ["internal display", "external display"], [False, True], None)
37         if extprim == None:
38                 return None
39         return ScreenSetup(relpos,intres,extres,extprim)