Little something I did for myself

in #coding7 months ago (edited)

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.

Sort:  

A tech geek uh? Very brilliant indeed!

Thanks! 😊

You are not stingy, you are sensible, that is what I say when they tell me that I should have bought this or that, you solved the problem of the cut, that is the important thing.

Thank you! It's just that that an uninterruptible power source would ultimately protect my computer from physical damage if something like this happens. I don't fear short term data loss, since I'm using journaling file systems anyway, but if a disk were to break, it would present a lot of more concerning problems.

It's great to see creativity and skill come together. Keep up the awesome work. It ain't easy brother

Wow! Very useful and practical skills to have at your fingertips. But it all seems like alien language to me!🙀😂

Heh... well I can't say I'm very good at coding either, but it's sometimes fun to create something simple that is also usable. I probably wouldn't be able to code things like games or full-blown apps like word processors though. It would be nice to learn, but there are times when it all looks alien to me too. 😅

Very nice! I should set my wacom up properly with bash too.. I just use the default settings right now, which is none... 🤔

Hey, thanks! Glad you liked it! 😊 Just to clarify: "eavom"? Did you possibly mean "environment variables"? Setting those up properly in bash can make a huge difference. I'm not a very conscientious with my variables either, and that sometimes leads me to unnecessary hurdles. It's however gotten a bit easier these days.

Omg.... My f ING keyboard autocorrect... I meant to say my wacom...

Oh, that's what it was? Lol! I know, Wacoms seem to work out of the box, but when you actually want to use them, there's a bit too much fiddling for the average person. I had to install some additional drivers for my son's Wacom Intuos tablet so he could use it in the 'osu!' rhythm game. It was quite a hassle.