Compare commits

...

7 commits

Author SHA1 Message Date
dabruh
0e1d39e826 Create Xserver display configuration script 2024-04-16 16:03:13 +02:00
dabruh
ac3fee7596 Source profile script 2024-04-12 18:31:56 +02:00
dabruh
597c73c9c3 Add XDG_DATA_DIRS 2024-04-12 18:31:38 +02:00
dabruh
8732f97c2b Make script POSIX compliant 2024-04-12 18:31:25 +02:00
dabruh
1611525077 Prepend homebrew rather than append to PATH 2024-04-12 18:26:30 +02:00
dabruh
1e94ea2f71 Adjust variable casing and defaults 2024-04-12 18:26:30 +02:00
dabruh
3671d21be5 Adjust variable paths 2024-04-12 18:26:02 +02:00
3 changed files with 418 additions and 83 deletions

169
.config/shell/profile Normal file → Executable file
View file

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Default programs:
export BROWSER=firefox
@ -12,106 +12,125 @@ export VISUAL=code
# Misc vars
export CHROME_EXECUTABLE=chromium # For Flutter
# Set XDG directories:
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
# Clean home:
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export ANDROID_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/Android/Sdk"
export CARGO_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/cargo"
export KREW_ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/krew"
export FLUTTER_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/flutter"
export GOPATH="${XDG_DATA_HOME:-$HOME/.local/share}/go"
export HISTFILE="${XDG_DATA_HOME:-$HOME/.local/share}/history"
export XINITRC="${XDG_CONFIG_HOME:-$HOME/.config}/x11/xinitrc"
export ZDOTDIR="${XDG_CONFIG_HOME:-$HOME/.config}/zsh"
export NVM_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/nvm"
export NUGET_PACKAGES="${XDG_DATA_HOME:-$HOME/.local/share}/nuget"
export ANDROID_HOME="$XDG_DATA_HOME/Android/Sdk"
export CARGO_HOME="$XDG_DATA_HOME/cargo"
export KREW_ROOT="$XDG_DATA_HOME/krew"
export FLUTTER_HOME="$XDG_DATA_HOME/flutter"
export GOPATH="$XDG_DATA_HOME/go"
export HISTFILE="$XDG_DATA_HOME/history"
export XINITRC="$XDG_CONFIG_HOME/x11/xinitrc"
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
export NVM_DIR="$XDG_CONFIG_HOME/nvm"
export NUGET_PACKAGES="$XDG_DATA_HOME/nuget"
# Color man pages:
export LESS=-r
export LESS_TERMCAP_mb=$'\E[01;32m'
export LESS_TERMCAP_md=$'\E[01;32m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;47;34m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;36m'
export LESS='-r'
export LESS_TERMCAP_mb
export LESS_TERMCAP_md
export LESS_TERMCAP_me
export LESS_TERMCAP_se
export LESS_TERMCAP_so
export LESS_TERMCAP_ue
export LESS_TERMCAP_us
LESS_TERMCAP_mb="$(printf '\033[01;32m')"
LESS_TERMCAP_md="$(printf '\033[01;32m')"
LESS_TERMCAP_me="$(printf '\033[0m')"
LESS_TERMCAP_se="$(printf '\033[0m')"
LESS_TERMCAP_so="$(printf '\033[01;47;34m')"
LESS_TERMCAP_ue="$(printf '\033[0m')"
LESS_TERMCAP_us="$(printf '\033[01;36m')"
# Other program settings:
export DISPLAY_DPI=96
# Remove an entry from PATH
rm_path_entry() {
local shell="${SHELL##*/}" rm_entry="${1:?Entry not set}" new_path path_arr
_tr="$(which tr 2>/dev/null || echo /usr/bin/tr)"
_sed="$(which sed 2>/dev/null || echo /usr/bin/sed)"
if [[ "$shell" == "zsh" ]]; then
IFS=" " read -A path_arr <<<"${PATH//:/ }"
else
IFS=" " read -a path_arr <<<"${PATH//:/ }"
# Trims the specified leading and trailing characters from the string
trim() {
string="${1:?Missing string}"
char="${2:?Missing trim character}"
echo "$string" | $_sed "s/^$char*//; s/$char*$//"
}
rm_env_path_entry() {
src_entries="${1:?Missing source entries}"
del_entry="${2:?Missing entry to delete}"
new_entries=""
echo "$src_entries" | $_tr ':' '\n' | while read -r entry; do
if [ "$entry" != "$del_entry" ]; then
new_entries="$new_entries:$entry"
fi
for path_entry in "${path_arr[@]}"; do
grep -Eq "^$rm_entry$" <<<"$path_entry" && continue
new_path="$new_path:$path_entry"
done
PATH="${new_path#:}"
trim "$new_entries" ":"
}
# Remove one or more entries from PATH
rm_path_entries() {
local rm_entry
modify_env_path() {
mode="${1:?Missing mode}"
entries="${2:?Missing entries}"
change="${3:?Missing change}"
for rm_entry in "$@"; do
rm_path_entry "$rm_entry"
done
}
entries="$(trim "$entries" ":")"
# Prepend one or more entries to PATH.
# The order of the supplied args will be kept, e.g. PATH="$1:$2:$PATH"
prepend_path_with_entries() {
local addl_paths entry
for entry in "$@"; do
! [ -d "$entry" ] && continue
addl_paths="$addl_paths:$entry"
case "$mode" in
prepend)
temp_entries="" # Will hold the new entries in the correct order
echo "$change" | $_tr ':' '\n' | while read -r entry; do
[ -z "$entry" ] && continue # Skip empty entries
[ -d "$entry" ] || continue # Skip non-existent directories
temp_entries="$temp_entries:$entry"
done
PATH="${addl_paths#:}:$PATH"
}
# Append one or more entries to PATH.
# The order of the supplied args will be kept, e.g. PATH="$PATH:$1:$2"
append_path_with_entries() {
local addl_paths entry
for entry in "$@"; do
! [ -d "$entry" ] && continue
addl_paths="$addl_paths:$entry"
# Merge the added entries to the existing entries
# Trim the leading colon and append the new entries
entries="${temp_entries#:}:$entries"
;;
append)
echo "$change" | $_tr ':' '\n' | while read -r entry; do
[ -z "$entry" ] && continue # Skip empty entries
[ -d "$entry" ] || continue # Skip non-existent directories
entries="$entries:$entry"
done
;;
delete)
echo "$change" | $_tr ':' '\n' | while read -r entry; do
[ -z "$entry" ] && continue # Skip empty entries
entries=$(rm_env_path_entry "$entries" "$entry")
done
;;
*)
echo "Invalid mode: $mode (choose 'append', 'prepend', 'delete')" >&2
return 1
;;
esac
PATH="$PATH:${addl_paths#:}"
trim "$entries" ":"
}
prepend_paths=(
"$HOME/.local/bin"
"$HOME/.cargo/bin"
"$FLUTTER_HOME/bin"
"$GOPATH/bin"
"$KREW_ROOT/bin"
)
append_paths=()
prepend_paths="$HOME/.local/bin:$HOME/.cargo/bin:$FLUTTER_HOME/bin:$GOPATH/bin:$KREW_ROOT/bin"
HOMEBREW_PREFIX="$XDG_DATA_HOME/homebrew"
if [ -x "$HOMEBREW_PREFIX/bin/brew" ]; then
eval "$("$HOMEBREW_PREFIX/bin/brew" shellenv)"
append_paths+=("$HOMEBREW_PREFIX/bin" "$HOMEBREW_PREFIX/sbin")
prepend_paths="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:$prepend_paths"
fi
rm_path_entries "${prepend_paths[@]}" "${append_paths[@]}" # Ensure we clean up first
prepend_path_with_entries "${prepend_paths[@]}"
append_path_with_entries "${append_paths[@]}"
PATH="$(modify_env_path delete "$PATH" "$prepend_paths")"
PATH="$(modify_env_path prepend "$PATH" "$prepend_paths")"
prepend_data_dirs="/var/lib/snapd/desktop:/usr/share:/usr/local/share"
XDG_DATA_DIRS="$(modify_env_path delete "$XDG_DATA_DIRS" "$prepend_data_dirs")"
XDG_DATA_DIRS="$(modify_env_path prepend "$XDG_DATA_DIRS" "$prepend_data_dirs")"
# Add the profile-extras file yourself if you wish to override anything:
PROFILE_EXTRAS="${XDG_CONFIG_HOME:-$HOME/.config}/shell/profile-extras"
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS"
PROFILE_EXTRAS="$XDG_CONFIG_HOME/shell/profile-extras"
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" || true

View file

@ -7,15 +7,19 @@ xrandr --dpi "${DISPLAY_DPI:-96}"
xrdb "${XDG_CONFIG_HOME:-$HOME/.config}/x11/xresources" &
xrdbpid=$!
DISPLAY_SCRIPT=~/.local/share/xrandr/$(hostname | cut -d. -f1)
([ -e "$DISPLAY_SCRIPT" ] && $DISPLAY_SCRIPT) || xrandr --auto
display_script="${XDG_DATA_HOME:-$HOME/.local/share}/xrandr/$(hostname | cut -d. -f1)"
([ -e "$display_script" ] && $display_script) || xrandr --auto
AUTOSTART="picom"
autostart="picom"
for PROGRAM in $AUTOSTART; do
which "$PROGRAM" || continue
pidof -s "$PROGRAM" || "$PROGRAM" &
for program in $autostart; do
which "$program" || continue
pidof -s "$program" || "$program" &
done >/dev/null 2>&1
# Ensure that xrdb has finished running before moving on to start the WM/DE.
[ -n "$xrdbpid" ] && wait "$xrdbpid"
# Source .config/shell/profile
profile_path="${XDG_CONFIG_HOME:-$HOME/.config}/shell/profile"
[ -f "$profile_path" ] && . "$profile_path"

312
.local/bin/xmonconf Executable file
View file

@ -0,0 +1,312 @@
#!/bin/bash
# This script is used to apply monitor profiles based on the connected monitors and USB devices.
set -euo pipefail
# Get the number of monitors connected to the system
get_monitor_count() {
xrandr --listmonitors | head -n1 | cut -d' ' -f2
}
# Get the list of connected and disconnected monitors.
# Includes information about the primary monitor
# E.g:
# eDP-1,connected,primary
# HDMI-1,disconnected
# DP-1,disconnected
_get_monitors() {
xrandr | grep -Eo '^.* ((dis|)connected)( primary|) ' | sed 's/ /,/g' | sed 's/,$//g'
}
# Function that gets all monitors and takes an optional amount of arguments to filter the list.
# Filters are applied as an OR operation.
# E.g:
# get_monitors connected primary
# get_monitors connected primary eDP-1
get_monitors() {
local monitors
monitors=$(_get_monitors)
# If no filters are set, return all monitors
if [ "$#" -eq 0 ]; then
echo "$monitors"
return 0
fi
for monitor in $monitors; do
for filter in "$@"; do
for part in ${monitor//,/ }; do
if [ "$part" = "$filter" ]; then
echo "$monitor"
continue 3 # Continue to the next monitor
fi
done
done
done
}
# monitor_exist checks if the specified monitor is connected to the system
# $1: The monitor name to search for
monitor_exist() {
local monitor=${1:?Monitor not set}
local connected_monitors
connected_monitors=$(get_monitors connected)
for connected_monitor in $connected_monitors; do
if [ "$(cut -d',' -f1 <<<"$connected_monitor")" = "$monitor" ]; then
return 0
fi
done
return 1
}
# monitors_exist checks if the specified monitors are connected to the system
# $1..$n: The monitor names to search for
monitors_exist() {
local monitor
for monitor in "$@"; do
if ! monitor_exist "$monitor"; then
return 1
fi
done
return 0
}
# Get the list of USB devices using lsusb, outputs for example "06cb:00f0 Synaptics, Inc."
get_usb_devices() {
lsusb | grep -Eo ' ID .*' | cut -d' ' -f3-
}
# Get the list of USB device names using lsusb, outputs for example "Synaptics, Inc."
get_usb_device_names() {
get_usb_devices | cut -d' ' -f2-
}
# Get the list of USB devices that match the name
get_usb_devices_by_name() {
local name=${1:?Name not set}
get_usb_device_names | while read -r device; do
if [ "$device" = "$name" ]; then
echo "$device"
fi
done
}
# Get the list of USB devices that match the pattern
get_usb_devices_by_pattern() {
local pattern=${1:?Pattern not set}
get_usb_device_names | grep -E "$pattern"
}
# Returns the list of USB devices that match the criteria
# $1: Required, either "all" or "any"
# $2: Mode, either "exact" or "pattern"
# $3..$n: The name or pattern to search for
usb_devices_exist() {
local required=${1:?Required not set}
local mode=${2:?Mode not set}
shift 2
local devices matches=0
# Check lenght of arguments
if [ "$#" -eq 0 ]; then
echo "No patterns set" 1>&2
return 1
fi
# Check if required and mode args are valid
if ! [[ "$required" =~ ^(all|any)$ ]]; then
echo "Invalid 'required' argument: $required" 1>&2
return 1
elif ! [[ "$mode" =~ ^(exact|pattern)$ ]]; then
echo "Invalid 'mode' argument: $mode" 1>&2
return 1
fi
# Process each pattern
for pattern in "$@"; do
if [ -z "$pattern" ]; then
echo "Got empty pattern" 1>&2
return 1
fi
if [ "$mode" = "exact" ]; then
devices="$(get_usb_devices_by_name "$pattern")"
elif [ "$mode" = "pattern" ]; then
devices="$(get_usb_devices_by_pattern "$pattern")"
fi
# Check if at least one device was found
if [ -n "$devices" ]; then
matches=$((matches + 1))
echo "$devices"
fi
done
# Check if all patterns had a match
if [ "$required" = "all" ]; then
if [ "$matches" -eq "$#" ]; then
return 0
else
echo "Amount of devices does not match the amount of patterns" 1>&2
return 1
fi
# Check if at least one pattern had a match
elif [ "$required" = "any" ]; then
if [ "$matches" -ge "0" ]; then
return 0
else
echo "Amount of devices does not match the amount of patterns" 1>&2
return 1
fi
fi
}
# Get the list of empty i3 workspaces
get_empty_i3_workspaces() {
i3-msg -t get_tree | jq -r '
recurse(.nodes[]?) | # Recursively descend into "nodes" arrays
select(.type == "workspace") | # Select elements where "type" is "workspace"
select(.name | test("^[^_].*")) | # Select elements where "name" does not start with "_"
select(.nodes | length == 0) | # Further select those where "nodes" array is empty
.name # Output the "name" of these workspaces
'
}
i3_workspace_is_empty() {
local workspace_name=${1:?Workspace name not set}
local empty_workspace
empty_workspace=$(get_empty_i3_workspaces)
for workspace in $empty_workspace; do
if [ "$workspace" = "$workspace_name" ]; then
return 0
fi
done
return 1
}
# Get a comma separated list of i3 workspace names and the output they are on
get_i3_workspaces() {
i3-msg -t get_workspaces | jq -r '.[] | .name + "," + .output'
}
get_i3_workspace_prop() {
local workspace_name=${1:?Workspace name not set}
local prop=${2:?Property not set}
i3-msg -t get_workspaces | jq -r ".[] | select(.name == \"$workspace_name\") | .$prop"
}
i3_workspace_exists() {
local workspace_name=${1:?Workspace name not set}
local workspaces name
if [[ -n "$(get_i3_workspace_prop "$workspace_name" name)" ]]; then
return 0
fi
return 1
}
# This function selects a workspace by name and moves it to the desired monitor.
# If the workspace name is an integer, it will allow incrementing the workspace name by the given number
move_i3_workspace() {
local workspace_name=${1:?Workspace name not set}
local monitor_name=${2:?Monitor name not set}
local increment=${3:-0}
local new_workspace_name="$workspace_name"
if ! i3_workspace_exists "$workspace_name"; then
echo "Workspace $workspace_name not found" 1>&2
return 1
elif ! monitor_exist "$monitor_name"; then
echo "Monitor $monitor_name not found" 1>&2
return 1
fi
# Ensure the new output is not the same as the current output
if [[ "$(get_i3_workspace_prop "$workspace_name" output)" == "$monitor_name" ]]; then
echo "Workspace $workspace_name is already on monitor $monitor_name" 1>&2
return 1
fi
# Increment the workspace name if it is an integer
if [[ "$workspace_name" =~ ^[0-9]+$ ]]; then
new_workspace_name=$((workspace_name + increment))
elif [ "$increment" -ne 0 ]; then
echo "Workspace name is not an integer. Unable to increment/decrement." 1>&2
return 1
fi
# Check if the new workspace name already exists and is not the same as the current workspace name
if i3_workspace_exists "$new_workspace_name"; then
if [[ "$new_workspace_name" != "$workspace_name" ]]; then
echo "Workspace $new_workspace_name already exists" 1>&2
return 1
fi
fi
echo "Moving workspace $workspace_name to monitor $monitor_name as $new_workspace_name"
i3-msg "workspace $workspace_name" 1>/dev/null # Select the workspace
i3-msg "rename workspace $workspace_name to $new_workspace_name" 1>/dev/null
i3-msg "move workspace to output $monitor_name" 1>/dev/null
}
# This function moves all workspaces that are not on the given monitor to the given monitor
move_nonexistent_i3_workspaces() {
local monitor_name=${1:?Monitor name not set}
local increment=${2:-0}
local workspaces
workspaces=$(get_i3_workspaces)
echo "Moving workspaces to monitor $monitor_name"
for workspace in $workspaces; do
local workspace_name
local workspace_output
workspace_name=$(echo "$workspace" | cut -d',' -f1)
workspace_output=$(echo "$workspace" | cut -d',' -f2)
if i3_workspace_is_empty "$workspace_name"; then
echo "Skipping empty workspace $workspace_name"
continue
fi
if ! monitor_exist "$workspace_output"; then
move_i3_workspace "$workspace_name" "$monitor_name" "$increment"
fi
done
}
main() {
primary_monitor=$(get_monitors primary | cut -d',' -f1)
monitor_count=$(get_monitors connected | wc -l)
echo "Monitors connected: $monitor_count"
if [ "$monitor_count" -eq 1 ]; then
echo "Applying laptop-only profile"
xrandr --auto
elif usb_devices_exist all exact "HP, Inc HP USB-C Universal Dock" >/dev/null && monitors_exist eDP-1 DVI-I-2-2 DVI-I-3-3; then
echo "Applying work profile"
xrandr \
--output eDP-1 --primary --mode 1920x1200 --pos 1200x1474 --rotate normal \
--output DVI-I-2-2 --mode 1920x1200 --pos 0x0 --rotate left \
--output DVI-I-3-3 --mode 1920x1200 --pos 1200x274 --rotate normal
else
echo "No profile found"
return 0
fi
move_nonexistent_i3_workspaces "$primary_monitor" 10
}
# Run if invoked directly
if [ "$0" = "${BASH_SOURCE[0]}" ]; then
main
fi