Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jNullj committed Jul 21, 2022
0 parents commit 02cad6a
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pkg.tar.zst
src/
pkg/
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

This license does not cover the The Arch Linux logo (logo.svg) and name, The Arch Linux name and logo are recognized trademakrs. Some rights reserved.

Copyright (c) 2022 jnullj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions fox-neat-wallpaper.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Package
Target = *
[Action]
Description = Updating fox-neat-wallpaper for relevant users
Depends = imagemagick
When = PostTransaction
Exec = /bin/bash /opt/fox-neat-wallpaper/hook-script.sh
9 changes: 9 additions & 0 deletions fox-neat-wallpaper.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Update fox-neat-wallpaper for running user

[Service]
Type=simple
ExecStart=/bin/bash /opt/fox-neat-wallpaper/fox-neat-wallpaper.sh update

[Install]
WantedBy=default.target
148 changes: 148 additions & 0 deletions fox-neat-wallpaper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/bin/bash
IMG_NAME=fox-neat-wallpaper.png
WALLPAPER_PATH=$HOME/.fox-neat-wallpaper
INSTALL_PATH=/opt/fox-neat-wallpaper
IMAGE_SIZE_X=1920
IMAGE_SIZE_Y=1080
IMAGE_SIZE=${IMAGE_SIZE_X}x${IMAGE_SIZE_Y}
LOGO_SIZE_PRECENT=60

# reset those when running from root shell
# this is added for usage of xfconf-query
# otherwise dbus query from user as root fails
export XDG_RUNTIME_DIR="/run/user/$UID"
export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"

# functions to refresh/update global veriables about the user/system

get_display_resolution () {
IMAGE_SIZE=$(xfconf-query -c displays -p /Default/eDP/Resolution)
IMAGE_SIZE_X=$(echo $IMAGE_SIZE | cut -d'x' -f1)
IMAGE_SIZE_Y=$(echo $IMAGE_SIZE | cut -d'x' -f2)
}

get_wallpaper_user_path () {
WALLPAPER_PATH=$HOME/.fox-neat-wallpaper
}

# echo a list of outdated packages seperated by a new line using pacman-contrip's checkupdates
get_outdated_packages () {
local updates_output # delcare before setting to insure exit code is picked, otherwise bash will first set then make local which will allways exit with 0
local ret_code
updates_output="$(checkupdates)"
ret_code=$?
if [ $ret_code -ne 0 ] && [ $ret_code -ne 2 ]; then
# signal there is an issue and stop wallpaper rewrite
return 1
fi
echo "$updates_output" | while read -r pkgname old_ver arrow new_ver; do
echo "$pkgname"
done
}

get_current_packages () {
echo "$(pacman -Q)"
}

# generate a list of all installed packages with outdated packages marked in red using pango supported tags
html_mark_outdated_packages () {
local color="red"
local packages="$(get_current_packages)"
local outdated # delcare before setting to insure exit code is picked, otherwise bash will first set then make local which will allways exit with 0
outdated="$(get_outdated_packages)"
if [ $? -ne 0 ]; then
# checkupdates might fail (for example if there is no network)
# if that happends there is no point in updating anything
return 1
fi
if [ -z "$outdated" ]; then
# if empty avoid running the while loop
# as sed would fit any string for empty $oldpkg
# also avoid spending any additional time on marking red text, as there is none
packages=$(echo $packages | tr '\n' ' ')
echo "$packages"
return
fi
while read -r oldpkg; do
packages="$( echo "$packages" | sed 's,\('^"$oldpkg"'.*$\),<span foreground="'"$color"'">\1</span>,' )"
done <<< "$(echo "$outdated")"
packages=$(echo $packages | tr '\n' ' ') # change newline to spaces for rendering the image
echo "$packages"
}

generate_wallpaper () {
cd /tmp
local pango_text # delcare before setting to insure exit code is picked, otherwise bash will first set then make local which will allways exit with 0
pango_text="$(html_mark_outdated_packages)\n" # newline at the end for the justify to space out the last line
# if html_mark_outdated_packages failed, dont remove the existing wallpaper with an empty one
if [ $? -ne 0 ]; then
return 1
fi
# generate image of background text
convert -background black -fill green -font Liberation-Mono -size $IMAGE_SIZE -define pango:justify=true pango:"$pango_text" $IMG_NAME
# add logo to the background
convert $IMG_NAME -size $(expr $IMAGE_SIZE_X \* $LOGO_SIZE_PRECENT / 100)x -background none $INSTALL_PATH/logo.svg -gravity center -extent $IMAGE_SIZE -layers flatten $IMG_NAME
# move the created wallpaper to user folder
mkdir -p $WALLPAPER_PATH
mv $IMG_NAME $WALLPAPER_PATH
}

# set xfce4 wallpaper for user using xfconf-query
set_wallpaper () {
xfconf-query \
--channel xfce4-desktop \
--property /backdrop/screen0/monitoreDP/workspace0/last-image \
--set $WALLPAPER_PATH/$IMG_NAME
}

enable_timer () {
systemctl --user enable fox-neat-wallpaper.service
systemctl --user enable fox-neat-wallpaper.timer
systemctl --user start fox-neat-wallpaper.service
systemctl --user start fox-neat-wallpaper.timer
}

disable_timer () {
systemctl --user disable fox-neat-wallpaper.service
systemctl --user disable fox-neat-wallpaper.timer
systemctl --user stop fox-neat-wallpaper.timer
}

help_msg () {
echo "Usage: fox-neat-wallpaper [action]"
echo ""
echo "Actions:"
echo " update: update current wallpaper image based on current installed packages for current user"
echo " set: automaticly set as background in xfce4 for current user"
echo " all: runs both update and set"
echo " enable-timer: enable a timer to auto-update the wallpaper for current user"
echo " disable-timer: disable a timer to auto-update the wallpaper for current user"
}

# update user values
get_display_resolution
get_wallpaper_user_path

case "$1" in
"update")
generate_wallpaper
;;
"set")
set_wallpaper
;;
"all")
generate_wallpaper
set_wallpaper
;;
"enable-timer")
enable_timer
;;
"disable-timer")
disable_timer
;;
"help")
help_msg
;;
*)
echo "unkown command - use help for available actions"
esac
12 changes: 12 additions & 0 deletions fox-neat-wallpaper.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Update fox-neat-wallpaper for running user

[Timer]
Persistent=true
# there is no rush, delay by 10min to avoid startup slowdown
OnBootSec=10min
OnUnitInactiveSec=3h
Unit=fox-neat-wallpaper.service

[Install]
WantedBy=timers.target
32 changes: 32 additions & 0 deletions hook-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
INSTALL_PATH=/opt/fox-neat-wallpaper

check_user_uses_xfce () {
if [ -d "/home/$1/.config/xfce4" ]
then
return 0
else
return 1
fi
}

check_user_uses_app () {
if [ -d "/home/$1/.fox-neat-wallpaper" ]
then
return 0
else
return 1
fi
}

run_per_user () {
getent passwd | while IFS=: read -r name password uid gid gecos home shell; do
if check_user_uses_xfce $name && check_user_uses_app $name
then
echo "generating wallpaper for $name"
su $name -c "$shell $1"
fi
done
}

run_per_user "$INSTALL_PATH/fox-neat-wallpaper.sh all"
45 changes: 45 additions & 0 deletions logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Fox neat wallpaper

![Banner](readme_pictures/banner.png)

This is a simple script that generates a wallpaper based on installed packages and marks which ones are outdated.
This script is made to be used for archlinux with the xfce4 desktop environment, one might use it with other desktop environment menually.

## Installation

Archlinux package will soon be added to the aur.
If you wish to install menually you should first make sure you have all dependencies:

```bash
# 1. clone the repository
git clone https://github.com/jNullj/fox-neat-wallpaper
cd fox-neat-wallpaper
# copy files to /opt/fox-neat-wallpaper
cp *.sh /opt/fox-neat-wallpaper
cp *.svg /opt/fox-neat-wallpaper
# copy pacman hook, systemd timer and service
cp fox-neat-wallpaper.hook /etc/pacman.d/hooks/fox-neat-wallpaper.hook
cp fox-neat-wallpaper.service /usr/lib/systemd/user/fox-neat-wallpaper.service
cp fox-neat-wallpaper.timer /usr/lib/systemd/user/fox-neat-wallpaper.service
# create a link of the exectuable to /usr/bin
ln -rTsF /opt/fox-neat-wallpaper/fox-neat-wallpaper.sh /usr/bin/fox-neat-wallpaper
```

## Usage

With a user you want to enable the wallpaper simply type
```bash
# Enable the wallpaper and create one
fox-neat-wallpaper all
```

To enable automatic updating of the wallpaper based of outdated packages also enable the systembd timer using:
```bash
# Enable timer for automatic updates
fox-neat-wallpaper enable-timer
```

If you use a desktop environment other then xfce4 you should menually set the wallpaper image. The script saves the wallpaper at `~/.fox-neat-wallpaper`
If you use other desktop environment and wish to automate this, pull requests are welcomed.

If you wish to replace the logo you could use your own by replacing the filename in the script.

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## License

The Arch Linux name and [logo](logo.svg) are recognized trademarks. Some rights reserved.
This project is created by the community and not an official project made or endorsed by Arch Linux.

For everything else: [MIT](LICENSE)

## Notice

The Arch Linux name and [logo](logo.svg) are recognized trademarks. Some rights reserved.
This project is created by the community and not an official project made or endorsed by Arch Linux.
Binary file added readme_pictures/banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 02cad6a

Please sign in to comment.