summaryrefslogtreecommitdiff
path: root/monitor_menu/__main__.py
blob: b1811f689a255b79372fe4e7c2503648af98f156 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3

import argparse
import dataclasses
import enum
import json
import logging
import os
import pathlib
import subprocess
from typing import Any
import yaml

import dialog


class NoMatchingProfile(ValueError):
    pass


class LogLevel:
    def __init__(self, value: str) -> None:
        try:
            self.numerical_level = logging.getLevelNamesMapping()[value.upper()]
        except KeyError:
            raise ValueError("invalid value")


class GraphicalServer(enum.StrEnum):
    XORG = enum.auto()
    WAYLAND = enum.auto()


@dataclasses.dataclass
class Monitor:
    output: str

    xrandr_opts: list[str] = dataclasses.field(default_factory=list)
    wlr_randr_opts: list[str] = dataclasses.field(default_factory=list)
    position: str | None = None
    background: str | None = None
    primary: bool = False

    @classmethod
    def from_json_dict(cls, json_dict: dict[str, Any]) -> "Monitor":
        def convert_key_name(name: str) -> str:
            return name.replace("-", "_")

        return cls(**{convert_key_name(key): value for key, value in json_dict.items()})


@dataclasses.dataclass
class Profile:
    name: str
    monitors: list[Monitor] = dataclasses.field(default_factory=list)

    identifier: str | None = None
    xrandr_opts: list[str] = dataclasses.field(default_factory=list)
    wlr_randr_opts: list[str] = dataclasses.field(default_factory=list)

    @classmethod
    def from_json_dict(cls, json_dict: dict[str, Any]) -> "Profile":
        def convert_key_name(name: str) -> str:
            return name.replace("-", "_")

        kwargs = {}
        for key, value in json_dict.items():
            key = convert_key_name(key)
            if key == "monitors":
                value = [Monitor.from_json_dict(item) for item in value]
            kwargs[key] = value
        return cls(**kwargs)

    def apply(self, graphical_server: GraphicalServer | None):
        if graphical_server is None:
            try:
                graphical_server = GraphicalServer(os.environ["XDG_SESSION_TYPE"])
            except KeyError:
                if "WAYLAND_DISPLAY" in os.environ:
                    graphical_server = GraphicalServer.WAYLAND
                elif "DISPLAY" in os.environ:
                    graphical_server = GraphicalServer.XORG

        match graphical_server:
            case GraphicalServer.XORG:
                self.apply_xorg()
            case GraphicalServer.WAYLAND:
                self.apply_wayland()
            case None:
                log.debug("Cannot determine graphical server type")
                raise Exception

    def apply_xorg(self):
        # We build the command line starting from just "xrandr" and adding
        # arguments.
        xrandr_cmd = ['xrandr']
        feh_cmd = ['feh', '--bg-fill']

        try:
            xrandr_cmd += self.xrandr_opts
        except KeyError:
            pass

        for monitor in self.monitors:
            xrandr_cmd.extend(monitor.xrandr_opts)

            if monitor.background is not None:
                feh_cmd.append(monitor.background)

        logging.debug("Executing: %s", xrandr_cmd)
        subprocess.run(xrandr_cmd, check=False)
        logging.debug("Executing: %s", feh_cmd)
        subprocess.run(feh_cmd, check=False)

    def apply_wayland(self):
        # We build the command line starting from just "xrandr" and adding
        # arguments.
        wlr_randr_cmd = ['wlr-randr']

        try:
            wlr_randr_cmd += self.wlr_randr_opts
        except KeyError:
            pass

        for monitor in self.monitors:
            wlr_randr_cmd.extend(monitor.wlr_randr_opts)

        logging.debug("Executing: %s", wlr_randr_cmd)
        subprocess.run(wlr_randr_cmd, check=False)


class MonitorMenu:
    def __init__(self, profiles: list[Profile]) -> None:
        self.profiles = profiles
        self.d = dialog.Dialog(autowidgetsize=True)

    def run(self, *, graphical_server: GraphicalServer | None = None, apply_exit=False):
        choices = []

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

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

            if code in (self.d.ESC, self.d.CANCEL):
                return

            try:
                self.profiles[int(profile_idx)].apply(graphical_server=graphical_server)
                if apply_exit:
                    return
            except IndexError:
                raise NoMatchingProfile from None


def load_profiles_config(path: pathlib.Path | None):
    if path is not None:
        with path.open() as fh:
            if path.suffix == ".json":
                return json.load(fh)
            # Expect YAML by default
            return yaml.safe_load(fh)

    # Try monitor-profiles.yaml and revert to monitor-profiles.json if the
    # first one does not exist.
    try:
        with pathlib.Path("~/.config/monitor-profiles.yaml").expanduser().open() as fh:
            return yaml.safe_load(fh)
    except FileNotFoundError:
        with pathlib.Path("~/.config/monitor-profiles.json").expanduser().open() as fh:
            return json.load(fh)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log-level", "--loglevel", type=LogLevel, default="info")
    parser.add_argument("--graphical-server", choices=list(GraphicalServer),
                        type=GraphicalServer)
    parser.add_argument(
            "--profiles-config",
            metavar="PATH",
            type=pathlib.Path,
            help="Configuration file to use."
            " By default $HOME/.config/monitor-profiles.yaml (preferred)"
            " or $HOME/.config/monitor-profiles.json")
    subparser = parser.add_subparsers(dest="command")
    run_parser = subparser.add_parser("run")
    run_parser.add_argument("--apply-exit", action="store_true")
    apply_parser = subparser.add_parser("apply")
    identifier_group = apply_parser.add_mutually_exclusive_group(required=True)
    identifier_group.add_argument("--index", type=int)
    identifier_group.add_argument("--id", type=str)

    args = parser.parse_args()
    logging.basicConfig(level=args.log_level.numerical_level)

    data = load_profiles_config(args.profiles_config)

    profiles = [Profile.from_json_dict(item) for item in data]

    match args.command:
        case None:
            MonitorMenu(profiles).run(graphical_server=args.graphical_server)
        case "run":
            MonitorMenu(profiles).run(apply_exit=args.apply_exit, graphical_server=args.graphical_server)
        case "apply":
            if args.index is not None:
                try:
                    profiles[args.index].apply(graphical_server=args.graphical_server)
                except IndexError:
                    raise NoMatchingProfile from None
            else:
                try:
                    next(filter(lambda p: p.identifier == args.id, profiles)).apply(graphical_server=args.graphical_server)
                except StopIteration:
                    raise NoMatchingProfile from None


if __name__ == '__main__':
    main()