summaryrefslogtreecommitdiff
path: root/home/apps/kanshi.nix
blob: ed7622af94d65c909fc3fcc60273cb5c5809b51a (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
{
  config,
  lib,
  ...
}: let
  inherit (lib) mkIf;
  cfg = config.apps.kanshi;

  laptopMonitors = lib.lists.filter (mon: mon.laptop) config.monitors;
  dockedMonitors = lib.lists.filter (mon: !mon.laptop) config.monitors;

  mkOutput = mon: status:
    lib.mkMerge [
      {
        criteria =
          if mon.desc == ""
          then mon.port
          else mon.desc;
        scale = mon.scale;
        inherit status;
      }
      (lib.mkIf (mon.position.x != 0 || mon.position.y != 0) {
        position = "${toString mon.position.x},${toString mon.position.y}";
      })
      (lib.mkIf (mon.size.x != 0 && mon.size.y != 0) {
        mode = "${toString mon.size.x}x${toString mon.size.y}";
      })
    ];

  mkEnabledOutput = mon: (mkOutput mon "enable");
  mkDisabledOutput = mon: (mkOutput mon "disable");
in {
  config = mkIf cfg.enable {
    services.kanshi = {
      enable = true;

      settings = [
        {
          profile = {
            name = "laptop";
            outputs = map mkEnabledOutput laptopMonitors;
          };
        }
        {
          profile = {
            name = "docked";
            outputs = (map mkEnabledOutput dockedMonitors) ++ (map mkDisabledOutput laptopMonitors);
          };
        }
      ];
    };
  };
}