summaryrefslogtreecommitdiff
path: root/monitor-menu.py
blob: 4aeec257a8622ec385912e35cde1e62d2c4de25f (plain)
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
#!/usr/bin/env python3

from os.path import expanduser

import dialog
import json
import subprocess

d = dialog.Dialog()

class MonitorMenu():
    def __init__(self, config_file='~/.config/monitor-profiles.json'):
        with open(expanduser(config_file)) as fd:
            self.profiles = json.load(fd)

    def run(self):
        choices = []

        i = 0
        for p in self.profiles:
            choices.append((str(i), p['name']))
            i += 1

        code, profile_idx = d.menu('Select the profile you want to use.', choices=choices)

        if code == d.ESC or code == d.CANCEL:
            return

        profile = self.profiles[int(profile_idx)]

        # We build the command line starting from just "xrandr" and adding arguments.
        cmd = ['xrandr']

        try:
            cmd += profile['xrandr-opts']
        except KeyError:
            pass

        for monitor in profile['monitors']:
            cmd += monitor['xrandr-opts']

        subprocess.run(cmd)


def main():
    menu = MonitorMenu()

    menu.run()

if __name__ == '__main__':
    main()