From 7a97491f9cebf9c83cb1fad924b1238ebd1b4b42 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 28 May 2023 08:28:21 +0200 Subject: [PATCH] Optimized msi-keyboard-color and caffeine --- group_vars/all | 1 + playbook.yml | 2 +- roles/pc_application-caffeine/README.md | 25 +++++++ .../pc_application-caffeine/defaults/main.yml | 2 - .../pc_application-caffeine/handlers/main.yml | 10 --- roles/pc_application-caffeine/meta/main.yml | 16 ++++- roles/pc_application-caffeine/tasks/main.yml | 17 +++-- .../templates/caffeine.desktop.j2 | 10 +++ .../templates/caffeine.service.j2 | 8 --- .../README.md | 42 ++++++++++++ .../files/keyboard_color.sh | 65 +++++++++++++++++++ .../meta/main.yml | 11 ++++ .../tasks/main.yml | 9 +++ .../tasks/setup_script.yml | 8 +++ .../tasks/setup_timers.yml | 30 +++++++++ .../templates/keyboard-color.service.j2 | 9 +++ .../templates/keyboard-color.timer.j2 | 9 +++ roles/pc_driver-msi-perkeyrgb/Readme.md | 4 -- roles/pc_driver-msi-perkeyrgb/meta/main.yml | 2 - roles/pc_driver-msi-perkeyrgb/tasks/main.yml | 9 --- 20 files changed, 245 insertions(+), 44 deletions(-) create mode 100644 roles/pc_application-caffeine/README.md delete mode 100644 roles/pc_application-caffeine/defaults/main.yml delete mode 100644 roles/pc_application-caffeine/handlers/main.yml create mode 100644 roles/pc_application-caffeine/templates/caffeine.desktop.j2 delete mode 100644 roles/pc_application-caffeine/templates/caffeine.service.j2 create mode 100644 roles/pc_application-msi-keyboard-color/README.md create mode 100644 roles/pc_application-msi-keyboard-color/files/keyboard_color.sh create mode 100644 roles/pc_application-msi-keyboard-color/meta/main.yml create mode 100644 roles/pc_application-msi-keyboard-color/tasks/main.yml create mode 100644 roles/pc_application-msi-keyboard-color/tasks/setup_script.yml create mode 100644 roles/pc_application-msi-keyboard-color/tasks/setup_timers.yml create mode 100644 roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 create mode 100644 roles/pc_application-msi-keyboard-color/templates/keyboard-color.timer.j2 delete mode 100644 roles/pc_driver-msi-perkeyrgb/Readme.md delete mode 100644 roles/pc_driver-msi-perkeyrgb/meta/main.yml delete mode 100644 roles/pc_driver-msi-perkeyrgb/tasks/main.yml diff --git a/group_vars/all b/group_vars/all index f10907c0..58742922 100644 --- a/group_vars/all +++ b/group_vars/all @@ -16,6 +16,7 @@ on_calendar_docker_compose_restart_unhealthy: "*-*-* 09,10,11,12,13,14,15,16,17, on_calendar_pull_primary_backups: "*-*-* 21:30:00" on_calendar_renew_lets_encrypt_certificates: "*-*-* 12,00:30:00" on_calendar_deploy_mailu_certificates: "*-*-* 13,01:30:00" +on_calendar_msi_keyboard_color: "*-*-* *:00,15,30,45:00" # Space Variables size_percent_maximum_backup: 75 diff --git a/playbook.yml b/playbook.yml index a7a90dfc..3e2995db 100644 --- a/playbook.yml +++ b/playbook.yml @@ -249,7 +249,7 @@ hosts: msi_perkeyrgb become: true roles: - - pc_driver-msi-perkeyrgb + - pc_application-msi-keyboard-color - name: setup ssh hosts hosts: ssh diff --git a/roles/pc_application-caffeine/README.md b/roles/pc_application-caffeine/README.md new file mode 100644 index 00000000..3522374a --- /dev/null +++ b/roles/pc_application-caffeine/README.md @@ -0,0 +1,25 @@ +# Ansible Role: caffeine_auto_start + +An Ansible Role that installs `caffeine-ng` on Archlinux systems and sets it to start automatically when the user logs in. + +## Requirements + +- The target system should be Archlinux. +- Ansible 2.4 or higher. +- The [kewlfft.aur.aur](https://galaxy.ansible.com/kewlfft/aur) Ansible Galaxy role for managing AUR packages. + +## Role Variables + +None. + +## License + +AGPL 3 + +## Author + +Kevin Veen-Birkenbach - kevin@veen.world - https://www.veen.world/ + +## Background + +The code for this role was created in an interactive conversation with OpenAI's language model. You can view the original conversation [here](https://chat.openai.com/share/fa846dac-6068-4386-b3e7-b75f1248ec82). diff --git a/roles/pc_application-caffeine/defaults/main.yml b/roles/pc_application-caffeine/defaults/main.yml deleted file mode 100644 index 33ebdaf6..00000000 --- a/roles/pc_application-caffeine/defaults/main.yml +++ /dev/null @@ -1,2 +0,0 @@ -# Defaults for caffeine_auto_start role -caffeine_executable_path: "/usr/bin/caffeine" diff --git a/roles/pc_application-caffeine/handlers/main.yml b/roles/pc_application-caffeine/handlers/main.yml deleted file mode 100644 index 691b5cae..00000000 --- a/roles/pc_application-caffeine/handlers/main.yml +++ /dev/null @@ -1,10 +0,0 @@ -# handlers file for caffeine_auto_start -- name: reload systemd - command: systemctl daemon-reload - -- name: enable and start caffeine - systemd: - name: caffeine.service - state: started - enabled: yes - daemon_reload: yes diff --git a/roles/pc_application-caffeine/meta/main.yml b/roles/pc_application-caffeine/meta/main.yml index 292eb72a..09d965b4 100644 --- a/roles/pc_application-caffeine/meta/main.yml +++ b/roles/pc_application-caffeine/meta/main.yml @@ -1,2 +1,16 @@ +--- dependencies: -- pc_system-aur-helper \ No newline at end of file + - pc_system-aur-helper +galaxy_info: + author: Kevin Veen-Birkenbach + description: Ansible role for installing caffeine-ng and setting it to start at user login. + license: AGPL 3 + min_ansible_version: 2.4 + platforms: + - name: Archlinux + versions: + - all + galaxy_tags: + - caffeine + - autostart + - archlinux diff --git a/roles/pc_application-caffeine/tasks/main.yml b/roles/pc_application-caffeine/tasks/main.yml index a22c6a58..c8b53c1d 100644 --- a/roles/pc_application-caffeine/tasks/main.yml +++ b/roles/pc_application-caffeine/tasks/main.yml @@ -1,13 +1,16 @@ -- name: install caffeine +--- +- name: Install caffeine kewlfft.aur.aur: use: yay name: - caffeine-ng -- name: Copy caffeine systemd service file +- name: Create autostart directory if it doesn't exist + file: + path: "{{ ansible_env.HOME }}/.config/autostart" + state: directory + +- name: Copy caffeine.desktop file to autostart directory template: - src: caffeine.service.j2 - dest: /etc/systemd/system/caffeine.service - notify: - - reload systemd - - enable and start caffeine + src: caffeine.desktop.j2 + dest: "{{ ansible_env.HOME }}/.config/autostart/caffeine.desktop" diff --git a/roles/pc_application-caffeine/templates/caffeine.desktop.j2 b/roles/pc_application-caffeine/templates/caffeine.desktop.j2 new file mode 100644 index 00000000..843196ae --- /dev/null +++ b/roles/pc_application-caffeine/templates/caffeine.desktop.j2 @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Exec=/usr/bin/caffeine +Hidden=false +NoDisplay=false +X-GNOME-Autostart-enabled=true +Name[en_US]=caffeine +Name=caffeine +Comment[en_US]= +Comment= diff --git a/roles/pc_application-caffeine/templates/caffeine.service.j2 b/roles/pc_application-caffeine/templates/caffeine.service.j2 deleted file mode 100644 index 0db8da26..00000000 --- a/roles/pc_application-caffeine/templates/caffeine.service.j2 +++ /dev/null @@ -1,8 +0,0 @@ -[Unit] -Description=Caffeine auto start - -[Service] -ExecStart={{ caffeine_executable_path }} - -[Install] -WantedBy=multi-user.target diff --git a/roles/pc_application-msi-keyboard-color/README.md b/roles/pc_application-msi-keyboard-color/README.md new file mode 100644 index 00000000..4a0a9dff --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/README.md @@ -0,0 +1,42 @@ +# Ansible Role: keyboard-color + +Ansible role to set up dynamic keyboard color change on MSI laptops. + +## Requirements + +- An MSI laptop +- The `msi-perkeyrgb` tool installed on the system +- Ansible 2.9 or later + +## Role Variables + +Available variables are listed below, along with their default values: + +```yaml +vendor_and_product_id: "" +``` + +The `vendor_and_product_id` variable is required and should be set to the vendor and product ID of the MSI laptop. + +## Dependencies + +- `pc_system-aur-helper` + +## Example Playbook + +```yaml +- hosts: all + roles: + - keyboard-color + vars: + vendor_and_product_id: "your_vendor_and_product_id" +``` + +## Author + +This role was created by [Kevin Veen-Birkenbach](https://github.com/kevinveenbirkenbach). + +## Chat Conversation + +To see how this role was developed, you can refer to the [Chat Conversation](https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3) that produced this software. + diff --git a/roles/pc_application-msi-keyboard-color/files/keyboard_color.sh b/roles/pc_application-msi-keyboard-color/files/keyboard_color.sh new file mode 100644 index 00000000..587bbdca --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/files/keyboard_color.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# Check if the vendor and product ID is provided +if [ -z "$1" ]; then + echo "Error: Vendor and product ID is missing." + echo "Usage: /opt/keyboard_color.sh " + exit 1 +fi + +# Extract the vendor and product ID from the command-line argument +vendor_and_product_id=$1 + +# Define the color transition parameters +fade_duration=1800 # 30 minutes in seconds +transition_start="06:00" +transition_end="06:30" +red_start="18:00" +red_end="06:00" +red_color="ff0000" + +# Get the current time in HH:MM format +current_time=$(date +%H:%M) + +# Check if it's within the red period +if [[ "$current_time" > "$red_start" || "$current_time" < "$red_end" ]]; then + # Set the color to red directly + sudo msi-perkeyrgb --model GS65 -s "$red_color" --id "$vendor_and_product_id" +else + # Check if it's within the fading period + if [[ "$current_time" > "$transition_start" && "$current_time" < "$transition_end" ]]; then + # Calculate the transition ratio within the fading period + start_seconds=$(date -d "$transition_start" +%s) + end_seconds=$(date -d "$transition_end" +%s) + current_seconds=$(date -d "$current_time" +%s) + transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }") + + # Calculate the current color based on the transition ratio + r=$(awk "BEGIN { value = 255 - 255 * $transition_ratio; printf(\"%.0f\", value) }") + g=0 + b=0 + + # Convert the RGB values to hexadecimal format + current_color=$(printf '%02x%02x%02x' $r $g $b) + + # Set the color using msi-perkeyrgb + sudo msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id" + else + # Calculate the transition ratio based on the time of day + start_seconds=$(date -d "$transition_end" +%s) + end_seconds=$(date -d "$red_start" +%s) + current_seconds=$(date -d "$current_time" +%s) + transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }") + + # Calculate the current color based on the transition ratio + r=$(awk "BEGIN { value = 255 * $transition_ratio; printf(\"%.0f\", value) }") + g=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }") + b=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }") + + # Convert the RGB values to hexadecimal format + current_color=$(printf '%02x%02x%02x' $r $g $b) + + # Set the color using msi-perkeyrgb + sudo msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id" + fi +fi diff --git a/roles/pc_application-msi-keyboard-color/meta/main.yml b/roles/pc_application-msi-keyboard-color/meta/main.yml new file mode 100644 index 00000000..88c0f0c6 --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/meta/main.yml @@ -0,0 +1,11 @@ +galaxy_info: + role_name: keyboard-color + author: Kevin Veen-Birkenbach + description: "Ansible role to set up dynamic keyboard color change on MSI laptops" + min_ansible_version: 2.9 + platforms: + - name: Linux + versions: + - all + dependencies: + - pc_system-aur-helper diff --git a/roles/pc_application-msi-keyboard-color/tasks/main.yml b/roles/pc_application-msi-keyboard-color/tasks/main.yml new file mode 100644 index 00000000..0d76aa84 --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/tasks/main.yml @@ -0,0 +1,9 @@ +--- +- name: Install MSI packages + kewlfft.aur.aur: + use: yay + name: + - msi-perkeyrgb + +- include_tasks: setup_script.yml +- include_tasks: setup_timers.yml diff --git a/roles/pc_application-msi-keyboard-color/tasks/setup_script.yml b/roles/pc_application-msi-keyboard-color/tasks/setup_script.yml new file mode 100644 index 00000000..6129f668 --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/tasks/setup_script.yml @@ -0,0 +1,8 @@ +--- +- name: Copy keyboard_color.sh script + copy: + src: keyboard_color.sh + dest: /opt/keyboard_color.sh + mode: 0755 + tags: + - keyboard-color diff --git a/roles/pc_application-msi-keyboard-color/tasks/setup_timers.yml b/roles/pc_application-msi-keyboard-color/tasks/setup_timers.yml new file mode 100644 index 00000000..6b104fd4 --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/tasks/setup_timers.yml @@ -0,0 +1,30 @@ +--- +- name: Copy keyboard-color.timer file + template: + src: keyboard-color.timer + dest: /etc/systemd/system/keyboard-color.j2 + mode: 0644 + tags: + - keyboard-color + +- name: Copy keyboard-color.service file + template: + src: keyboard-color.service.j2 + dest: /etc/systemd/system/keyboard-color.service + mode: 0644 + tags: + - keyboard-color + +- name: Reload systemd daemon + systemd: + daemon_reload: yes + tags: + - keyboard-color + +- name: Enable and start keyboard-color.timer + systemd: + name: keyboard-color.timer + state: started + enabled: yes + tags: + - keyboard-color diff --git a/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 new file mode 100644 index 00000000..e2fcff03 --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Keyboard Color Service + +[Service] +Type=oneshot +ExecStart=/opt/keyboard_color.sh {{ vendor_and_product_id }} + +[Install] +WantedBy=default.target diff --git a/roles/pc_application-msi-keyboard-color/templates/keyboard-color.timer.j2 b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.timer.j2 new file mode 100644 index 00000000..91cbdcdd --- /dev/null +++ b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.timer.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Keyboard Color Timer + +[Timer] +OnCalendar={{on_calendar_msi_keyboard_color}} +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/roles/pc_driver-msi-perkeyrgb/Readme.md b/roles/pc_driver-msi-perkeyrgb/Readme.md deleted file mode 100644 index 11552454..00000000 --- a/roles/pc_driver-msi-perkeyrgb/Readme.md +++ /dev/null @@ -1,4 +0,0 @@ -# pc_driver-msi-perkeyrgb -# further information -- https://github.com/Askannz/msi-perkeyrgb -- https://wiki.archlinux.org/title/MSI_GS66_11UX \ No newline at end of file diff --git a/roles/pc_driver-msi-perkeyrgb/meta/main.yml b/roles/pc_driver-msi-perkeyrgb/meta/main.yml deleted file mode 100644 index 292eb72a..00000000 --- a/roles/pc_driver-msi-perkeyrgb/meta/main.yml +++ /dev/null @@ -1,2 +0,0 @@ -dependencies: -- pc_system-aur-helper \ No newline at end of file diff --git a/roles/pc_driver-msi-perkeyrgb/tasks/main.yml b/roles/pc_driver-msi-perkeyrgb/tasks/main.yml deleted file mode 100644 index 0368e201..00000000 --- a/roles/pc_driver-msi-perkeyrgb/tasks/main.yml +++ /dev/null @@ -1,9 +0,0 @@ -- name: Install MSI packages - kewlfft.aur.aur: - use: yay - name: - - msi-perkeyrgb - -- name: Change preset of keyboard {{vendor_and_product_id}} to {{preset}} - ansible.builtin.shell: sudo msi-perkeyrgb --model GS65 -p "{{preset}}" --id "{{vendor_and_product_id}}" - become: true \ No newline at end of file