Last active
February 3, 2024 12:24
-
-
Save BOSSoNe0013/94e828fa8d9ff4c1a455c13e6c6b76e7 to your computer and use it in GitHub Desktop.
Watch screen status on GNU/Linux and change devices lightning modes/colors accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# _____ _____ _ _ _ _ _ _ | |
# / ___| / ___| | | | | | | | | | | | | |
# \ `--. ___ _ __ ___ ___ _ __ \ `--.| |_ __ _| |_ _ _ ___ | | | | __ _| |_ ___| |__ ___ _ __ | |
# `--. \/ __| '__/ _ \/ _ \ '_ \ `--. \ __/ _` | __| | | / __| | |/\| |/ _` | __/ __| '_ \ / _ \ '__| | |
# /\__/ / (__| | | __/ __/ | | | /\__/ / || (_| | |_| |_| \__ \ \ /\ / (_| | || (__| | | | __/ | | |
# \____/ \___|_| \___|\___|_| |_| \____/ \__\__,_|\__|\__,_|___/ \/ \/ \__,_|\__\___|_| |_|\___|_| | |
# | |
# Copyright (C) 2023 Cyril Bosselut | |
# | |
# WatchScreenStatus 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 3 of the License, or | |
# (at your option) any later version. | |
# | |
# WatchScreenStatus 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 WatchScreenStatus If not, see <http://www.gnu.org/licenses/>. | |
PID="$$" | |
v_major=1 | |
v_minor=0 | |
v_patch=0 | |
VERSION="$v_major.$v_minor.$v_patch" | |
i=1 | |
sp="/-\|" | |
RED="FF0000" | |
GREEN="00FF00" | |
BLUE="0000FF" | |
YELLOW="FFFF00" | |
PINK="FF06B5" | |
CYAN="00FFFF" | |
ORANGE="FF2000" | |
AMBER="FF0400" | |
GOLD="FF4000" | |
DEFAULT_MOUSE_COLOR=$ORANGE | |
DEFAULT_KEYBOARD_COLOR=$ORANGE | |
DEFAULT_MACRO_KEYBOARD_COLOR=$ORANGE | |
DEFAULT_GPU_COLOR=$AMBER | |
DEFAULT_MOTHERBOARD_COLOR=$ORANGE | |
DEFAULT_PUMP_COLOR=$ORANGE | |
DEFAULT_CASE_COLOR="$ORANGE,$ORANGE" | |
MOUSE_COLOR=$DEFAULT_MOUSE_COLOR | |
KEYBOARD_COLOR=$DEFAULT_KEYBOARD_COLOR | |
MACRO_KEYBOARD_COLOR=$DEFAULT_KEYBOARD_COLOR | |
GPU_COLOR=$DEFAULT_GPU_COLOR | |
MOTHERBOARD_COLOR=$DEFAULT_MOTHERBOARD_COLOR | |
CASE_COLOR=$DEFAULT_CASE_COLOR | |
PUMP_COLOR=$DEFAULT_PUMP_COLOR | |
STANDBY_COLOR=$RED | |
PREVIOUS_SCREEN_STATE="unknown" | |
WSS_RUNNING=true | |
CASE_NAME="Lian Li Uni Hub - AL" | |
GPU_NAME="Gigabyte AORUS RTX4080 MASTER 16G" | |
MOTHERBOARD_NAME="MSI MEG Z490 UNIFY (MS-7C71)" | |
KEYBOARD_NAME="Razer BlackWidow Chroma V2" | |
MACRO_KEYBOARD_NAME="Macro Keyboard" | |
MOUSE_NAME="Roccat Burst Pro" | |
function onexit { | |
if [[ $WSS_RUNNING == true ]]; then | |
wss_log "Stopping WSS" | |
WSS_RUNNING=false | |
updateColors | |
lockfile-remove $LOCK_FILE | |
fi | |
exit | |
} | |
function wss_log { | |
echo "$1" 1>&2 | |
logger --id="$PID" -t "WSS" "$1" | |
} | |
function setStaticColors { | |
openrgb -d "$GPU_NAME" -m "Color Shift" -c $GPU_COLOR -b 100 -s 100 \ | |
-d "$CASE_NAME" -m Runway -c "$CASE_COLOR" -b 100 \ | |
-d "$MOTHERBOARD_NAME" -m Static -c $MOTHERBOARD_COLOR -b 100 \ | |
-d "$MOTHERBOARD_NAME" -z 2 -c $PUMP_COLOR -b 100 \ | |
-d "$MOUSE_NAME" -m Direct -c $MOUSE_COLOR -b 100 \ | |
-d "$KEYBOARD_NAME" -m Direct -c $KEYBOARD_COLOR -b 100 \ | |
-d "$MACRO_KEYBOARD_NAME" -m Direct -c $MACRO_KEYBOARD_COLOR -b 100 1>&2 & | |
} | |
function getStandbyColor { | |
if [[ -n ${CUSTOM_STANDBY_COLOR} ]]; then | |
echo $CUSTOM_STANDBY_COLOR | |
else | |
echo $STANDBY_COLOR | |
fi | |
} | |
function updateColors { | |
MOUSE_COLOR=$DEFAULT_MOUSE_COLOR | |
KEYBOARD_COLOR=$DEFAULT_KEYBOARD_COLOR | |
MACRO_KEYBOARD_COLOR=$DEFAULT_MACRO_KEYBOARD_COLOR | |
GPU_COLOR=$DEFAULT_GPU_COLOR | |
MOTHERBOARD_COLOR=$DEFAULT_MOTHERBOARD_COLOR | |
PUMP_COLOR=$DEFAULT_PUMP_COLOR | |
CASE_COLOR=$DEFAULT_CASE_COLOR | |
if [[ $WSS_RUNNING == false ]]; then | |
wss_log "Mouse & keyboard switch to default color" | |
setStaticColors & | |
curl -s -f https://neo.b1project.com:5000/devices/31/set/lights -d "method=light_off&room=bureau&device=31&args=%7B%22target%22%3A-1%2C+%22color%22%3A%22255,0,0%22%7D" 1>&2 > /dev/null | |
elif [[ $SCREEN_STATE != "on" ]]; then | |
color=`getStandbyColor` | |
wss_log "Standby color: $color" | |
KEYBOARD_COLOR=$color | |
MACRO_KEYBOARD_COLOR=$color | |
MOUSE_COLOR=$color | |
GPU_COLOR=$color | |
MOTHERBOARD_COLOR=$color | |
PUMP_COLOR=$color | |
CASE_COLOR="$color,$color" | |
wss_log "Mouse, keyboard, case and GPU switch to standby color" | |
setStaticColors & | |
curl -s -f https://neo.b1project.com:5000/devices/31/set/lights -d "method=light_color&room=bureau&device=31&args=%7B%22target%22%3A-1%2C+%22color%22%3A%22255,0,0%22%7D" 1>&2 > /dev/null | |
else | |
wss_log "Mouse, case and GPU switch to MIAMI preset" | |
KEYBOARD_COLOR=$BLUE | |
MACRO_KEYBOARD_COLOR=$BLUE | |
MOUSE_COLOR="$PINK,$BLUE" | |
GPU_COLOR="$PINK,$BLUE,$BLUE" | |
MOTHERBOARD_COLOR=$BLUE | |
PUMP_COLOR=$PINK | |
CASE_COLOR="$PINK,$BLUE" | |
setStaticColors & | |
curl -s -f https://neo.b1project.com:5000/devices/31/set/lights -d "method=light_color&room=bureau&device=31&args=%7B%22target%22%3A-1%2C+%22color%22%3A%220,0,255%22%7D" 1>&2 > /dev/null | |
sleep 3 | |
wss_log "Keyboard switch to RAINBOW preset" | |
polychromatic-cli -e Rainbow2 > /dev/null | |
fi | |
} | |
function checkDPMSStatus { | |
DPMS_STATE=`xset q | grep 'DPMS is ' | cut -d' ' -f5| tr '[:upper:]' '[:lower:]'` | |
if [[ $DPMS_STATE == "disabled" ]]; then | |
wss_log "Enabling DPMS" | |
echo "" 1>&2 | |
xset +dpms | |
fi | |
} | |
function checkScreenStatus { | |
SCREEN_STATE=`xset q | grep 'Monitor is ' | cut -d' ' -f6| tr '[:upper:]' '[:lower:]'` | |
if [[ $SCREEN_STATE == "" ]]; then | |
sleep 0.5 | |
SCREEN_STATE=`xset q | grep 'Monitor is ' | cut -d' ' -f5| tr '[:upper:]' '[:lower:]'` | |
if [[ $SCREEN_STATE == "" ]]; then | |
wss_log "Can't check screen state" | |
return 0 | |
fi | |
fi | |
if [[ $SCREEN_STATE != $PREVIOUS_SCREEN_STATE ]]; then | |
wss_log "Screen state changed from ${PREVIOUS_SCREEN_STATE} to ${SCREEN_STATE}" | |
PREVIOUS_SCREEN_STATE=$SCREEN_STATE | |
updateColors & | |
echo "" 1>&2 | |
else | |
echo -n -e "\b${sp:i++%${#sp}:1}" 1>&2 | |
fi | |
} | |
function main { | |
echo "###########################################################################################################" 1>&2 | |
echo "# _____ _____ _ _ _ _ _ _ #" 1>&2 | |
echo "# / ___| / ___| | | | | | | | | | | | #" 1>&2 | |
echo "# \ \`--. ___ _ __ ___ ___ _ __ \ \`--.| |_ __ _| |_ _ _ ___ | | | | __ _| |_ ___| |__ ___ _ __ #" 1>&2 | |
echo "# \`--. \/ __| '__/ _ \/ _ \ '_ \ \`--. \ __/ _\` | __| | | / __| | |/\| |/ _\` | __/ __| '_ \ / _ \ '__| #" 1>&2 | |
echo "# /\__/ / (__| | | __/ __/ | | | /\__/ / || (_| | |_| |_| \__ \ \ /\ / (_| | || (__| | | | __/ | #" 1>&2 | |
echo "# \____/ \___|_| \___|\___|_| |_| \____/ \__\__,_|\__|\__,_|___/ \/ \/ \__,_|\__\___|_| |_|\___|_| #" 1>&2 | |
echo "# #" 1>&2 | |
echo "###########################################################################################################" 1>&2 | |
echo "Ver: $VERSION" 1>&2 | |
echo "" 1>&2 | |
wss_log "Starting screen status watcher with PID: $PID" | |
xbindkeys_autostart | |
while $WSS_RUNNING == true; do | |
checkDPMSStatus | |
checkScreenStatus | |
sleep 1 | |
done | |
wss_log "Closing screen status watcher" | |
} | |
if [[ $1 == "--version" ]] || [[ $1 == "-v" ]]; then | |
echo $VERSION | |
else | |
LOCK_FILE=/tmp/watch_screen_status-$UID | |
lockfile-create --retry 0 --use-pid $LOCK_FILE || exit | |
trap onexit EXIT HUP INT TERM | |
main | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment