Skip to content

Instantly share code, notes, and snippets.

@idelsink
Last active August 17, 2022 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idelsink/3dfd4a3ca1e37a8d6e69560b71cabb56 to your computer and use it in GitHub Desktop.
Save idelsink/3dfd4a3ca1e37a8d6e69560b71cabb56 to your computer and use it in GitHub Desktop.
A script to automatically do things when it detects a dock/undock 'event'. Only works when polling the script continuously
# - Get the 'docked' state from the systemd-logind D-Bus interface
# See: https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
# - Get the 'value' from the dbus-send command.
# The reply is in the format of ` variant boolean <value>`
# Where <value> is either `true` or `false`
_is_docked=$(dbus-send \
--system \
--print-reply=literal \
--reply-timeout=120000 \
--type=method_call \
--dest='org.freedesktop.login1' \
'/org/freedesktop/login1' \
org.freedesktop.DBus.Properties.Get \
string:'org.freedesktop.login1.Manager' string:'Docked' \
| awk '{print $3}')
if [[ "${_is_docked}" == "true" ]]; then
# Laptop (https://emojipedia.org/laptop/) + Monitor (https://emojipedia.org/desktop-computer/ + Variant selector 16 https://emojipedia.org/emoji/%EF%B8%8F/)
printf '\xF0\x9F\x92\xBB\xF0\x9F\x96\xA5\xEF\xB8\x8F'
dconf write /org/gnome/desktop/interface/text-scaling-factor 1.00
else
# Laptop (https://emojipedia.org/laptop/)
printf '\xF0\x9F\x92\xBB' # https://emojipedia.org/laptop/
dconf write /org/gnome/desktop/interface/text-scaling-factor 1.25
fi
#!/usr/bin/env bash
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
function docked() {
# Set the scaling factor to the default value
dconf write /org/gnome/desktop/interface/text-scaling-factor 1.00
}
function undocked() {
# Set the scaling factor to accessibility mode 'large-text'
dconf write /org/gnome/desktop/interface/text-scaling-factor 1.25
}
# - Get the 'docked' state from the systemd-logind D-Bus interface
# See: https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
# - Get the 'value' from the dbus-send command.
# The reply is in the format of ` variant boolean <value>`
# Where <value> is either `true` or `false`
function is_docked() {
dbus-send \
--system \
--print-reply=literal \
--reply-timeout=120000 \
--type=method_call \
--dest='org.freedesktop.login1' \
'/org/freedesktop/login1' \
org.freedesktop.DBus.Properties.Get \
string:'org.freedesktop.login1.Manager' string:'Docked' \
| awk '{print $3}'
}
function was_docked() {
local was_docked_filename="/run/user/${UID}/was-docked.systemd-logind-docked-events"
if [ ! -f "${was_docked_filename}" ]; then
touch "${was_docked_filename}"
fi
cat "${was_docked_filename}"
}
function set_dock_state() {
local was_docked_filename="/run/user/${UID}/was-docked.systemd-logind-docked-events"
echo "${1}" > "${was_docked_filename}"
}
function main() {
if [[ "$(is_docked)" == "true" ]]; then
# Docked
echo "💻🖥️"
else
# Not docked
echo "💻"
fi
if [[ "$(is_docked)" != "$(was_docked)" ]]; then
if [[ "$(is_docked)" == "true" ]]; then
docked
else
undocked
fi
set_dock_state "$(is_docked)"
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment