Support imv image viewer

- Allow the user to tell kunst to use imv instead of sxiv via new flag
  `--viewer`
- If `--viewer` is not passed, prefer imv over sxiv in wayland sessions
- In sway sessions, use `swaymsg` to resize imv to preferred dimensions
This commit is contained in:
Rohan Kumar 2020-02-28 14:05:08 -08:00
parent d0477c0fcb
commit 9258513fa9
No known key found for this signature in database
GPG Key ID: 36B154A782AEA0AC
2 changed files with 35 additions and 14 deletions

View File

@ -9,7 +9,7 @@
</p> </p>
## Dependencies ## Dependencies
- ```sxiv``` - ```sxiv``` or ```imv```
- ```imagemagick``` - ```imagemagick```
- ```bash``` - ```bash```
- ```ffmpeg``` - ```ffmpeg```

47
kunst
View File

@ -4,18 +4,19 @@
# ┴ ┴└─┘┘└┘└─┘ ┴ # ┴ ┴└─┘┘└┘└─┘ ┴
# Created by Siddharth Dushantha # Created by Siddharth Dushantha
# #
# Dependencies: sxiv, imagemagick, bash, ffmpeg, mpc, jq, mpd # Dependencies: sxiv or imv, imagemagick, bash, ffmpeg, mpc, jq, mpd
VERSION=1.2.4 VERSION=1.2.4
COVER=/tmp/kunst.jpg COVER=/tmp/kunst.jpg
MUSIC_DIR=~/Music/ MUSIC_DIR=~/Music/
SIZE=250x250 WIDTH=250
SIZE="${WIDTH}x${WIDTH}"
POSITION="+0+0" POSITION="+0+0"
show_help() { show_help() {
echo "usage: kunst [-h] [--size "px"] [--position "+x+y"][--music_dir "path/to/dir"] [--silent] [--version]" echo "usage: kunst [-h] [--size "px"] [--position "+x+y"] [--viewer <imv|x11>] [--music_dir "path/to/dir"] [--silent] [--version]"
echo " " echo " "
echo "┬┌─┬ ┬┌┐┌┌─┐┌┬┐" echo "┬┌─┬ ┬┌┐┌┌─┐┌┬┐"
echo "├┴┐│ ││││└─┐ │" echo "├┴┐│ ││││└─┐ │"
@ -25,7 +26,8 @@ show_help() {
echo "optional arguments:" echo "optional arguments:"
echo " -h, --help show this help message and exit" echo " -h, --help show this help message and exit"
echo " --size what size to display the album art in" echo " --size what size to display the album art in"
echo " --position the position where the album art should be displayed" echo " --position the position where the album art should be displayed"
echo " --viewer whether to use imv or sxiv"
echo " --music_dir the music directory which MPD plays from" echo " --music_dir the music directory which MPD plays from"
echo " --silent dont show the output" echo " --silent dont show the output"
echo " --version show the version of kunst you are using" echo " --version show the version of kunst you are using"
@ -46,6 +48,10 @@ while true; do
shift; shift;
POSITION=$1 POSITION=$1
;; ;;
--viewer)
shift;
VIEWER=$1
;;
--music_dir) --music_dir)
shift; shift;
MUSIC_DIR=$1 MUSIC_DIR=$1
@ -69,6 +75,13 @@ while true; do
shift shift
done done
# If available, use imv instead of sxiv on Wayland,
# unless specified otherwise by the user.
if [ -n "$SWAYSOCK" ] || [ "$XDG_SESSION_TYPE" = 'wayland' ] \
&& [ -z "$VIEWER" ] && command -v imv >/dev/null; then
VIEWER=imv
fi
# This is a base64 endcoded image which will be used if # This is a base64 endcoded image which will be used if
# the file does not have an emmbeded album art. # the file does not have an emmbeded album art.
# The image is an image of a music note # The image is an image of a music note
@ -177,8 +190,8 @@ update_cover() {
pre_exit() { pre_exit() {
# Get the proccess ID of kunst and kill it. # Get the proccess ID of kunst and kill it.
# We are dumping the output of kill to /dev/null # We are dumping the output of kill to /dev/null
# because if the user quits sxiv before they # because if the user quits the image viewer
# exit kunst, an error will be shown # before they exit kunst, an error will be shown
# from kill and we dont want that # from kill and we dont want that
kill -9 $(cat /tmp/kunst.pid) &> /dev/null kill -9 $(cat /tmp/kunst.pid) &> /dev/null
@ -215,11 +228,17 @@ main() {
if [ $FIRST_RUN == true ]; then if [ $FIRST_RUN == true ]; then
FIRST_RUN=false FIRST_RUN=false
# Display the album art using sxiv # Display the album art
sxiv -g $SIZE$POSITION -b $COVER -N "Kunst" & if [ "$VIEWER" = 'imv' ]; then
# if running SwayWM, tell sway to resize the imv window
# Save the process ID so that we can kill [ -n "$SWAYSOCK" ] \
# sxiv when the user exits the script && swaymsg for_window '[app_id="^imv$" title="^imv.*\/tmp\/kunst\.(jpg|png)"]' resize set width "$WIDTH" px height "$WIDTH" px
imv "$COVER" &
else
sxiv -g $SIZE$POSITION -b $COVER -N "Kunst" &
fi
# Save the process ID so that we can kill the
# image viewer when the user exits the script
echo $! >/tmp/kunst.pid echo $! >/tmp/kunst.pid
fi fi
@ -231,12 +250,14 @@ main() {
if [ ! $SILENT ];then if [ ! $SILENT ];then
echo "kunst: received event from mpd" echo "kunst: received event from mpd"
fi fi
imv-msg "$(cat /tmp/kunst.pid)" close all
imv-msg "$(cat /tmp/kunst.pid)" open "$COVER"
done done
} }
# Disable CTRL-Z because if we allowed this key press, # Disable CTRL-Z because if we allowed this key press,
# then the script would exit but, sxiv would still be # then the script would exit but, the image viewer
# running # would still be running
trap "" SIGTSTP trap "" SIGTSTP
trap pre_exit EXIT trap pre_exit EXIT