aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/autorandr.nix
blob: 02fc77c1e5890af1a650eb82e7efd92cfeccf907 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.autorandr;

  matrixOf = n: m: elemType:
    mkOptionType rec {
      name = "matrixOf";
      description =
        "${toString n}×${toString m} matrix of ${elemType.description}s";
      check = xss:
        let listOfSize = l: xs: isList xs && length xs == l;
        in listOfSize n xss
        && all (xs: listOfSize m xs && all elemType.check xs) xss;
      merge = mergeOneOption;
      getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "*" "*" ]);
      getSubModules = elemType.getSubModules;
      substSubModules = mod: matrixOf n m (elemType.substSubModules mod);
      functor = (defaultFunctor name) // { wrapped = elemType; };
    };

  profileModule = types.submodule {
    options = {
      fingerprint = mkOption {
        type = types.attrsOf types.str;
        description = ''
          Output name to EDID mapping.
          Use <code>autorandr --fingerprint</code> to get current setup values.
        '';
        default = { };
      };

      config = mkOption {
        type = types.attrsOf configModule;
        description = "Per output profile configuration.";
        default = { };
      };

      hooks = mkOption {
        type = profileHooksModule;
        description = "Profile hook scripts.";
        default = { };
      };
    };
  };

  configModule = types.submodule {
    options = {
      enable = mkOption {
        type = types.bool;
        description = "Whether to enable the output.";
        default = true;
      };

      primary = mkOption {
        type = types.bool;
        description = "Whether output should be marked as primary";
        default = false;
      };

      position = mkOption {
        type = types.str;
        description = "Output position";
        default = "";
        example = "5760x0";
      };

      mode = mkOption {
        type = types.str;
        description = "Output resolution.";
        default = "";
        example = "3840x2160";
      };

      rate = mkOption {
        type = types.str;
        description = "Output framerate.";
        default = "";
        example = "60.00";
      };

      gamma = mkOption {
        type = types.str;
        description = "Output gamma configuration.";
        default = "";
        example = "1.0:0.909:0.833";
      };

      rotate = mkOption {
        type = types.nullOr (types.enum [ "normal" "left" "right" "inverted" ]);
        description = "Output rotate configuration.";
        default = null;
        example = "left";
      };

      transform = mkOption {
        type = types.nullOr (matrixOf 3 3 types.float);
        default = null;
        example = literalExample ''
          [
            [ 0.6 0.0 0.0 ]
            [ 0.0 0.6 0.0 ]
            [ 0.0 0.0 1.0 ]
          ]
        '';
        description = ''
          Refer to
          <citerefentry>
            <refentrytitle>xrandr</refentrytitle>
            <manvolnum>1</manvolnum>
          </citerefentry>
          for the documentation of the transform matrix.
        '';
      };

      dpi = mkOption {
        type = types.nullOr types.ints.positive;
        description = "Output DPI configuration.";
        default = null;
        example = 96;
      };

      scale = mkOption {
        type = types.nullOr (types.submodule {
          options = {
            method = mkOption {
              type = types.enum [ "factor" "pixel" ];
              description = "Output scaling method.";
              default = "factor";
              example = "pixel";
            };

            x = mkOption {
              type = types.either types.float types.ints.positive;
              description = "Horizontal scaling factor/pixels.";
            };

            y = mkOption {
              type = types.either types.float types.ints.positive;
              description = "Vertical scaling factor/pixels.";
            };
          };
        });
        description = ''
          Output scale configuration.
          </para><para>
          Either configure by pixels or a scaling factor. When using pixel method the
          <citerefentry>
            <refentrytitle>xrandr</refentrytitle>
            <manvolnum>1</manvolnum>
          </citerefentry>
          option
          <parameter class="command">--scale-from</parameter>
          will be used; when using factor method the option
          <parameter class="command">--scale</parameter>
          will be used.
          </para><para>
          This option is a shortcut version of the transform option and they are mutually
          exclusive.
        '';
        default = null;
        example = literalExample ''
          {
            x = 1.25;
            y = 1.25;
          }
        '';
      };
    };
  };

  hookType = types.lines;

  globalHooksModule = types.submodule {
    options = {
      postswitch = mkOption {
        type = types.attrsOf hookType;
        description = "Postswitch hook executed after mode switch.";
        default = { };
      };

      preswitch = mkOption {
        type = types.attrsOf hookType;
        description = "Preswitch hook executed before mode switch.";
        default = { };
      };

      predetect = mkOption {
        type = types.attrsOf hookType;
        description = ''
          Predetect hook executed before autorandr attempts to run xrandr.
        '';
        default = { };
      };
    };
  };

  profileHooksModule = types.submodule {
    options = {
      postswitch = mkOption {
        type = hookType;
        description = "Postswitch hook executed after mode switch.";
        default = "";
      };

      preswitch = mkOption {
        type = hookType;
        description = "Preswitch hook executed before mode switch.";
        default = "";
      };

      predetect = mkOption {
        type = hookType;
        description = ''
          Predetect hook executed before autorandr attempts to run xrandr.
        '';
        default = "";
      };
    };
  };

  hookToFile = folder: name: hook:
    nameValuePair "autorandr/${folder}/${name}" {
      source = "${pkgs.writeShellScriptBin "hook" hook}/bin/hook";
    };
  profileToFiles = name: profile:
    with profile;
    mkMerge ([
      {
        "autorandr/${name}/setup".text = concatStringsSep "\n"
          (mapAttrsToList fingerprintToString fingerprint);
        "autorandr/${name}/config".text =
          concatStringsSep "\n" (mapAttrsToList configToString profile.config);
      }
      (mkIf (hooks.postswitch != "")
        (listToAttrs [ (hookToFile name "postswitch" hooks.postswitch) ]))
      (mkIf (hooks.preswitch != "")
        (listToAttrs [ (hookToFile name "preswitch" hooks.preswitch) ]))
      (mkIf (hooks.predetect != "")
        (listToAttrs [ (hookToFile name "predetect" hooks.predetect) ]))
    ]);
  fingerprintToString = name: edid: "${name} ${edid}";
  configToString = name: config:
    if config.enable then ''
      output ${name}
      ${optionalString (config.position != "") "pos ${config.position}"}
      ${optionalString config.primary "primary"}
      ${optionalString (config.dpi != null) "dpi ${toString config.dpi}"}
      ${optionalString (config.gamma != "") "gamma ${config.gamma}"}
      ${optionalString (config.mode != "") "mode ${config.mode}"}
      ${optionalString (config.rate != "") "rate ${config.rate}"}
      ${optionalString (config.rotate != null) "rotate ${config.rotate}"}
      ${optionalString (config.scale != null)
      ((if config.scale.method == "factor" then "scale" else "scale-from")
        + " ${toString config.scale.x}x${toString config.scale.y}")}
      ${optionalString (config.transform != null) ("transform "
        + concatMapStringsSep "," toString (flatten config.transform))}
    '' else ''
      output ${name}
      off
    '';

in {
  options = {
    programs.autorandr = {
      enable = mkEnableOption "Autorandr";

      hooks = mkOption {
        type = globalHooksModule;
        description = "Global hook scripts";
        default = { };
        example = literalExample ''
          {
            postswitch = {
              "notify-i3" = "''${pkgs.i3}/bin/i3-msg restart";
              "change-background" = readFile ./change-background.sh;
              "change-dpi" = '''
                case "$AUTORANDR_CURRENT_PROFILE" in
                  default)
                    DPI=120
                    ;;
                  home)
                    DPI=192
                    ;;
                  work)
                    DPI=144
                    ;;
                  *)
                    echo "Unknown profle: $AUTORANDR_CURRENT_PROFILE"
                    exit 1
                esac

                echo "Xft.dpi: $DPI" | ''${pkgs.xorg.xrdb}/bin/xrdb -merge
              '''
            };
          }
        '';
      };

      profiles = mkOption {
        type = types.attrsOf profileModule;
        description = "Autorandr profiles specification.";
        default = { };
        example = literalExample ''
          {
            "work" = {
              fingerprint = {
                eDP1 = "<EDID>";
                DP1 = "<EDID>";
              };
              config = {
                eDP1.enable = false;
                DP1 = {
                  enable = true;
                  primary = true;
                  position = "0x0";
                  mode = "3840x2160";
                  gamma = "1.0:0.909:0.833";
                  rate = "60.00";
                  rotate = "left";
                };
              };
              hooks.postswitch = readFile ./work-postswitch.sh;
            };
          }
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = flatten (mapAttrsToList (profile:
      { config, ... }:
      mapAttrsToList (output: opts: {
        assertion = opts.scale == null || opts.transform == null;
        message = ''
          Cannot use the profile output options 'scale' and 'transform' simultaneously.
          Check configuration for: programs.autorandr.profiles.${profile}.config.${output}
        '';
      }) config) cfg.profiles);

    home.packages = [ pkgs.autorandr ];
    xdg.configFile = mkMerge ([
      (mapAttrs' (hookToFile "postswitch.d") cfg.hooks.postswitch)
      (mapAttrs' (hookToFile "preswitch.d") cfg.hooks.preswitch)
      (mapAttrs' (hookToFile "predetect.d") cfg.hooks.predetect)
      (mkMerge (mapAttrsToList profileToFiles cfg.profiles))
    ]);
  };

  meta.maintainers = [ maintainers.uvnikita ];
}