Full Code Alchemist ::: 耶郎 ::: avatar

Little something I did for myself

gamer00

Published: 22 May 2024 › Updated: 22 May 2024Little something I did for myself

Little something I did for myself

We lost power today, for some 20 to 30 seconds. It was enough for my computer however, since as a full-blown desktop computer it doesn't have a battery like those laptops, and I was always too cheap to invest in a UPS backup power system to protect my computer from things like power outages.

Anyhow... when I managed to get my computer back up running and all, I noticed I didn't have sound anymore. Had to do some hacking, and I eventually managed to get pipewire (the sound server) running again.

So to prevent this from happening again, I wrote a simple script that checks whether pipewire is running or not, and restarts it if it's not.

Since I had had a recurring problem of losing my Finnish keybindings and the Wacom drawing tablet also seemed to forget that I'm left-handed, I also added some magic to set things right, and in some cases, left.

So in the end, this is what I created, in ~/.config/qtile/autostart.sh:

#!/bin/sh

LOGFILE=~/.config/qtile/autostart.log

echo "Starting autostart script at $(date)" >> $LOGFILE

# Function to check if a process is running
is_running() {
        pgrep "$1" > /dev/null 2>&1
}

# Check and Start PipeWire if not running
if is_running "pipewire"; then
        echo "PipeWire is already running" >> $LOGFILE
else
        echo "Starting PipeWire" >> $LOGFILE
        pipewire &
        notify-send "Qtile" "PipeWire started"
        sleep 1  # Delay for 1 second
fi

# Check and Start WirePlumber if not running
if is_running "wireplumber"; then
        echo "WirePlumber is already running" >> $LOGFILE
else
        echo "Starting WirePlumber" >> $LOGFILE
        wireplumber &
        notify-send "Qtile" "WirePlumber started"
        sleep 1  # Delay for 1 second
fi

# Set keymap to Finnish
echo "Setting keymap to Finnish" >> $LOGFILE
setxkbmap fi
notify-send "Qtile" "Keymap set to Finnish"
sleep 1  # Delay for 1 second

# Set Wacom tablet to left-handed mode
echo "Setting Wacom tablet to left-handed mode" >> $LOGFILE
~/.bin/Scripts/wacom-left-handed.sh
notify-send "Qtile" "Wacom tablet set to left-handed mode"
sleep 1  # Delay for 1 second


# Send a notification
echo "Sending notification" >> $LOGFILE
notify-send "Qtile" "Autostart script executed"

It logs everything too, ain't that cool?

So, after the script was written, I set it as an executable: chmod +x ~/.config/qtile/autostart.sh

To run it from inside Qtile every time the configuration is refreshed with Ctrl+Mod4+r (Mod4 is usually the "Windows" key on many PC keyboards), I added these hooks (the parts that start with @hook.subscribe.) into the qtile configuration that is in ~/.config/qtile/config.py:

import os # <-- This had to be added
from libqtile import bar, layout, widget, hook
from libqtile.config import Click, Drag, Group, Key, Match, Screen
from libqtile.lazy import lazy
from libqtile.utils import guess_terminal
import subprocess

@hook.subscribe.startup_once # Only runs at initial startup
def autostart_once():
    home = os.path.expanduser('~')
    with open(home + '/.config/qtile/autostart.log', 'a') as f:
        f.write("Running autostart hook\n")
    subprocess.call([home + '/.config/qtile/autostart.sh'])

@hook.subscribe.startup # Runs every time Qtile is 'refreshed'
def autostart():
    home = os.path.expanduser('~')
    with open(home + '/.config/qtile/autostart.log', 'a') as f:
        f.write("Running restart hook\n")
    subprocess.call([home + '/.config/qtile/autostart.sh'])

# Rest of the configuration below


Image created with DALL·E

The darned thing didn't want to work, until I took a look at the Qtile logfile ~/.local/share/qtile/qtile.log and noticed there was an error message about the missing os library module.

So I had to add import os to the configuration too. Now it works fine.

Leave Little something I did for myself to:

Written by

Gaming, FLOSS, Arts, Photography, Weiqi/Go/Baduk... All photos are my own unless otherwise specified.

Read more #coding posts


Best Posts From Full Code Alchemist ::: 耶郎 :::

We have not curated any of gamer00's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Full Code Alchemist ::: 耶郎 :::