Skip to content

Instantly share code, notes, and snippets.

@ingenieroariel
Created April 5, 2019 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ingenieroariel/b313ec408662050bf8be98a330015150 to your computer and use it in GitHub Desktop.
Save ingenieroariel/b313ec408662050bf8be98a330015150 to your computer and use it in GitHub Desktop.
{ config, lib, pkgs, ... }:
let
rotate = pkgs.writeShellScriptBin "rotate" ''
${pkgs.python3}/bin/python3 ${rotate-py}
'';
rotate-py = pkgs.writeText "rotate-py" ''
import subprocess as sp
import os
ORIENTATIONS = {
"bottom-up": "inverted",
"normal": "normal",
"left-up": "left",
"right-up": "right",
}
def rotar(output):
cosa = output.rstrip().decode("utf-8")
if "orientation changed:" in cosa:
_, iio_orientation = cosa.split(": ")
xrandr_orientation = ORIENTATIONS[iio_orientation]
xrandr_cmd = ["/run/current-system/sw/bin/xrandr", "--output", "eDP-1", "--rotate", xrandr_orientation]
print(" ".join(xrandr_cmd))
os.system(" ".join(xrandr_cmd))
def run_long_running_cmd(args, responders=None):
process = sp.Popen(args, stdout=sp.PIPE, stderr=sp.STDOUT)
with process.stdout:
while True:
output = process.stdout.readline()
exit_status = process.poll()
if responders is not None:
for responder in responders:
response = responder(output)
if response is not None:
process.stdin.write(response)
if output:
print(output)
elif exit_status is not None:
break
logger.info('Finished running subprocess.')
return exit_status
if __name__=="__main__":
run_long_running_cmd("${pkgs.iio-sensor-proxy}/bin/monitor-sensor", responders=[ rotar,])
'';
in {
systemd.services.voltear = {
enable = true;
description = "Darle vuelta a la pantalla";
serviceConfig = {
LimitNOFILE=65536;
PermissionsStartOnly=true;
Type="simple";
ExecStart = "${pkgs.python3}/bin/python3 ${rotate-py}";
ExecStop = "/run/current-system/sw/bin/true";
User = "ani";
};
wantedBy = [ "default.target" ];
};
}
[root@fonseca:/etc/nixos]# systemctl status voltear.service
● voltear.service - Darle vuelta a la pantalla
Loaded: loaded (/nix/store/83mh5mv6vd9m79jgyhl1zind2v1qwhbs-unit-voltear.service/voltear.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2019-04-05 11:45:54 -05; 2min 8s ago
Main PID: 28983 (python3)
Tasks: 4 (limit: 4915)
Memory: 6.9M
CGroup: /system.slice/voltear.service
├─28983 /nix/store/0n8slcq8p5x31kc9hncabsqq9y3fpkzp-python3-3.7.3/bin/python3 /nix/store/5lgi26vrh98r6vdsl91rj6xzc994a0wa-rotate-py
└─28984 /nix/store/xxgwk7qjy0x81b2sclics10ykhdxbaf5-iio-sensor-proxy-2.5/bin/monitor-sensor
abr 05 11:45:54 fonseca systemd[1]: Started Darle vuelta a la pantalla.
abr 05 11:45:58 fonseca python3[28983]: Can't open display
abr 05 11:46:00 fonseca python3[28983]: Can't open display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment