Toggle window decorations on Linux GTK3 with Python3

The Internet is full of outdated Python code for doing things with windows, so here is what I got working today in a Python 3, GTK 3 environment.

This script toggles the window decorations on the active window on and off. I have it bound to Ctrl+NumPadMinus for easy access.

#!/usr/bin/env python3

import gi
gi.require_version('Gdk', '3.0')
gi.require_version('GdkX11', '3.0')
gi.require_version('Wnck', '3.0')
from gi.repository import Gdk
from gi.repository import GdkX11
from gi.repository import Wnck


def active_window(screen):
    for window in screen.get_windows():
       if window.is_active() == True:
            return window


def toggle_decorations(w):
    if w.get_decorations().decorations == 0:
        w.set_decorations(Gdk.WMDecoration.ALL)
    else:
        w.set_decorations(0)


screen = Wnck.Screen.get_default()
screen.force_update()
display = GdkX11.X11Display.get_default()
window = active_window(screen)
window_id = window.get_xid()

w = GdkX11.X11Window.foreign_new_for_display(display, window_id)
toggle_decorations(w)


window = None
screen = None
Wnck.shutdown()

Set the date (EXIF) of a photo on Linux

To set the date when a photo was taken, install ExifTool e.g.:

sudo apt install libimage-exiftool-perl   # If on Ubuntu
sudo dnf install perl-Image-ExifTool.noarch  # If on Fedora

And modify the photo with a command like this:

exiftool -DateTimeOriginal='2020-08-13 12:00 UTC' myphoto.jpg

More info on the Exif tags you can edit is at ExifTool’s docs.

Example of a systemd service file

Here is an almost-minimal example of a systemd service file, that I use to run the Mastodon bot of my generative art playground Graft.

I made a dedicated user just to run this service, and installed Graft into /home/graft/apps/graft under that username. Now, as root, I edited a file called /etc/systemd/system/graft.service and made it look like this:

[Service]
ExecStart=/home/graft/apps/graft/bot-mastodon
User=graft
Group=graft
WorkingDirectory=/home/graft/apps/graft/
[Install]
WantedBy=multi-user.target

Now I can start the graft service like any other service like this:

sudo systemctl start graft

and find out its status with:

sudo systemctl status graft

If I want it to run on startup I can do:

sudo systemctl enable graft

and it will. Easy!

If I want to look at its output, it’s:

sudo journalctl -u graft

As a reward for reading this far, here’s a little animation you can make with Graft:

If you want the service to be restarted whenever it fails, under [Service] add something like this:

Restart=always
RestartSec=3

More info at: Jon Archer’s blog post.

Installing Flarum on Ubuntu 18.04

I am setting up a forum for sharing levels for my game Rabbit Escape, and I have decided to try and use Flarum, because it looks really usable and responsive, has features we need like liking posts and following authors, and I think it will be reasonably OK to write the custom features we want.

So, I want a dev environment on my local Ubuntu 18.04 machine, and the first step to that is a standard install.

Warning: at the time of writing the Flarum docs say it does not work with PHP 7.2, which is what is included with Ubuntu 18.04, so this may not work. (So far it looks OK for me.)

Here’s how I got it working:

sudo apt install \
    apache2 \
    libapache2-mod-php \
    mariadb-server \
    php-mysql \
    php-json \
    php-gd \
    php-tokenizer \
    php-mbstring \
    php-curl

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

# Get the next line from https://getcomposer.org/download/
# Don't copy it exactly!
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

mkdir $HOME/bin
php composer-setup.php --install-dir=$HOME/bin/ --filename=composer
rm composer-setup.php

cd /var/www/html
sudo mkdir flarum
sudo chown $(whoami) flarum

# Log out and in again here to get composer to be in your PATH
cd flarum
composer create-project flarum/flarum . --stability=beta

sudo chgrp -R www-data .
sudo chmod -R 775 .

sudo a2enmod rewrite 

Next I granted the permissions for Flarum’s .htaccess file to change settings by modifying /etc/apache2/apache2.conf and adding this after the existing <Directory entries:

<Directory "/var/www/html/flarum/">
    AllowOverride All
</Directory>

and restarted Apache:

sudo systemctl restart apache2

Now I created a database in MariaDB:

$ sudo mariadb
MariaDB [(none)]> CREATE USER flarumuser IDENTIFIED BY 'flarumpassword';
MariaDB [(none)]> CREATE DATABASE flarumdb;
MariaDB [(none)]> GRANT ALL ON flarumdb.* TO flarumuser;

and went to http://localhost/flarum in my browser, and entered these details:

MySQL host: localhost
MySQL database: flarumdb
MySQL user: flarumuser
MySQL password: flarumpassword
table prefix: flarum_

Admin user: admin
Admin email: <my email address>
Admin password: adminpassword

Once I typed all those correctly and submitted, I had to wait a while, and then after it printed some details I refreshed the page and saw a local Flarum ready to customise.

Next, I looked into how to get an extension development environment up and running.

If you want to find and share levels for Rabbit Escape, check up on our progress setting up the forum at https://artificialworlds.net/rabbit-escape/levels.

Preventing Audacity from crashing when using PulseAudio

I found that Audacity would crash whenever the Playback Device in the Devices section of Preferences was set to “pulse” or “default”.

This can be fixed by launching Audacity like this:

Exec=env PULSE_LATENCY_MSEC=100 audacity

I fixed it “permanently” by modifying my Audacity launcher file, which is in /usr/share/applications/audacity.desktop.

I changed the line starting “Exec=” to:

Exec=env PULSE_LATENCY_MSEC=100 audacity %F

More info is on the Audacity forum.