Skip to content

Instantly share code, notes, and snippets.

@mk-pmb
Last active August 16, 2023 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk-pmb/7fa99f1c56d39bc4f3949adbc3fab10f to your computer and use it in GitHub Desktop.
Save mk-pmb/7fa99f1c56d39bc4f3949adbc3fab10f to your computer and use it in GitHub Desktop.
Example for drawing a custom spinner into MPV's window.
#!/bin/bash
# -*- coding: utf-8, tab-width: 2 -*-
#
# mpv_spinner_demo - Example for drawing a custom spinner into MPV's window.
# Copyright (C) 2023 Marcel Krause <mk+copyleft@pimpmybyte.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
set -o pipefail -o errexit
BGCOLOR='black' # <-- any ImageMagick color names/syntax
SPINNER_FRAMES=(
# Using the spinner from NetworkManager
/usr/share/icons/Humanity/apps/24/nm-stage02-connecting*.svg
)
[ -f "${SPINNER_FRAMES[0]}" ] || exit 4$(local -p >&2
echo "E: First spinner image frame is not a file." >&2)
mpv --force-window=immediate <(sleep 10m) & MPV_PID=$!
SECONDS=0
while [ "$SECONDS" -lt 3 ]; do
WIN_ID="$(wmctrl -xpl | sed -nre 's!^(0x\S+) *\S+ *'"$MPV_PID"' .*$!\1!p')"
[ -z "$WIN_ID" ] || break
# Window not yet open, let's retry soon:
sleep 0.1s
done
[ -n "$WIN_ID" ] || exit 4$(
echo "E: Failed to find window ID for pid $MPV_PID" >&2)
function mpv_alive () { kill -0 "$MPV_PID" 2>/dev/null; }
while mpv_alive; do
for SPIN in "${SPINNER_FRAMES[@]}"; do
mpv_alive || break
# echo "Drawing $SPIN into window $WIN_ID"
convert "$SPIN" -background "$BGCOLOR" -flatten png24:- \
| display -window "$WIN_ID" -resample 500% -backdrop - \
|| true # <-- no idea why "display" exits with return value 1.
sleep 0.1s
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment