First commit

This commit is contained in:
Yorick Barbanneau 2022-11-20 00:32:22 +01:00
commit ea4c167527
4 changed files with 160 additions and 0 deletions

54
README.md Normal file
View file

@ -0,0 +1,54 @@
Kanshi Ansible role
-------------------
This role install and configure [Kanshi](https://git.sr.ht/~emersion/kanshi})
service.
## Variables
This role needs two variables for creating Kanshi configuration file
`sway_output` provided by [ansible-sway](https://git.epha.se/ephase/ansible-sway)
role and `kanshi_profiles`.
`sway_outputs` is not mandatory butuseful to provide id ans options for outputs.
Here is an example of `kanshi_profile`
```yaml
kanshi_profiles:
- profile: 'laptop'
screens:
- id: 'eDP-1'
- profile: 'home'
screens:
- id: 'eDP-1'
enable: false
- id: 'iiyama_27_4k'
position: '1080,0'
- id: 'iiyama_23_fhd'
position: '0,0'
mode: '1920x1024'
transform: 90
options:
- id: 'eDP-1'
option: 'bg'
value: '~/images/bg.jpg'
commands:
- swaymsg workspace 1, move workspace to +eDP1+
```
* `profile`: name of the profile
* `screens`: list of outputs
* `id`: id of the screen, could be the real name, ouput name like *HDMI-1*
(both given by `swaymsg -t output`) or id referenced in `sway_outputs`
* `position`: absolute position of the screen
* `mode`: resolution of the screen
* `scale`: scale factor for the screen
* `transform`: rotation (90, 180 or 270 degres) for the screen
* `options`: list of options:
* `id`: see above in *screens* section
* `options`: option name
* `value`: value
* `commands`: list of command to execute when switching to profile

11
files/kanshi.service Normal file
View file

@ -0,0 +1,11 @@
[Unit]
Description=Dynamic output configuration for Wayland compositors
Documentation=https://sr.ht/~emersion/kanshi
BindsTo=sway-session.target
[Service]
Type=simple
ExecStart=/usr/bin/kanshi
[Install]
WantedBy=sway-session.target

38
tasks/main.yml Normal file
View file

@ -0,0 +1,38 @@
---
- name: Install Kanshi package
ansible.builtin.package:
name: kanshi
state: present
become: true
- name: Create Sway config dirs
ansible.builtin.file:
path: '{{ ansible_user_dir ~ "/.config/sway/conf.d" }}'
state: directory
mode: 0750
owner: '{{ ansible_user_uid }}'
group: '{{ ansible_user_gid }}'
- name: Render templates for Kanshi config file
ansible.builtin.template:
src: config.j2
dest: '{{ ansible_user_dir}}/.config/kanshi/config'
owner: '{{ ansible_user_uid }}'
group: '{{ ansible_user_gid }}'
lstrip_blocks: yes
trim_blocks: yes
mode: 0640
#any_errors_fatal: true
- name: copy systemd service file {{ item }}
ansible.builtin.copy:
src: 'kanshi.service'
dest: '{{ ansible_user_dir }}/.config/systemd/user/'
mode: 0640
- name: activate service
ansible.builtin.systemd:
name: 'kanshi.service'
scope: user
state: started
enabled: yes

57
templates/config.j2 Normal file
View file

@ -0,0 +1,57 @@
# {{ ansible_managed }}
{% if kanshi_profiles is defined %}
{% for p in kanshi_profiles %}
profile {{ p.profile | default('') }} {
{# Process screens #}
{% for s in p.screens %}
{% set current = (sway_outputs | selectattr('id','==', s.id))[0] %}
{{- ' output "' ~ current['name'] | default(s.id) ~ '"' -}}
{% if s.status is defined and s.status == 'disable' %}
{{- ' disable' -}}
{% else %}
{% if s.status is defined and s.status == 'enable' %}
{{- ' enable' -}}
{% endif %}
{% if s.position is defined %}
{{- ' position ' ~ s.position -}}
{% endif %}
{% if s.transform is defined %}
{#
use sway config transform here is not a good idea because
rotation will be applied two times
#}
{{- ' transform ' ~ s.transform -}}
{% endif %}
{% if s.scale is defined %}
{{- ' scale ' ~ s.scale -}}
{% elif current['options']['scale'] is defined %}
{{- ' scale ' ~ current['options']['scale'] -}}
{% endif %}
{% endif %}
{% endfor -%}
{# process options#}
{% if p.options is defined %}
{% for o in p.options %}
{% set current = (sway_outputs | selectattr('id','==', o.id))[0] %}
{{- ' output "' ~ current['name'] | default(s.id) ~ '" ' -}}
{{ o.option }} {{ o.value }}
{% endfor -%}
{% endif -%}
{# Process command#}
{% if p.commands is defined %}
{% for c in p.commands %}
{% set cmd = namespace(cmd = c) %}
{% if sway_outputs is defined %}
{% for o in sway_outputs %}
{% set cmd.cmd = ( cmd.cmd | replace('+'+o.id+'+', '"'+o.name+'"')) %}
{% endfor %}
{% endif %}
{{- ' exec ' ~ cmd.cmd }}
{% endfor -%}
{% endif -%}
}
{% endfor %}
{% endif %}