diff --git a/nixos/i3/autorandr.nix b/nixos/i3/autorandr.nix new file mode 100644 index 00000000..6a512987 --- /dev/null +++ b/nixos/i3/autorandr.nix @@ -0,0 +1,52 @@ + +{ config, pkgs, ... }: +{ + programs.autorandr = { + enable = true; + + hooks = { + postswitch = { + "restart-compton" = "systemctl --user restart compton"; + "restart-i3" = "i3-msg restart"; + "restart-services" = "systemctl --user restart kdeconnect-indicator.service kdeconnect.service network-manager-applet.service pasystray.service polybar.service"; + }; + }; + + profiles = { + "foureighty-alone" = { + fingerprint = { + eDP1 = "00ffffffffffff0006af362300000000001b0104a51f117802f4f5a4544d9c270f505400000001010101010101010101010101010101e65f00a0a0a040503020350035ae100000180000000f0000000000000000000000000020000000fe0041554f0a202020202020202020000000fe004231343051414e30322e33200a00b2"; + }; + config = { + eDP1 = { + enable = true; + primary = true; + mode = "2560x1440"; + dpi = 144; + }; + }; + }; + "foureighty-docked" = { + fingerprint = { + eDP1 = "00ffffffffffff0006af362300000000001b0104a51f117802f4f5a4544d9c270f505400000001010101010101010101010101010101e65f00a0a0a040503020350035ae100000180000000f0000000000000000000000000020000000fe0041554f0a202020202020202020000000fe004231343051414e30322e33200a00b2"; + DP1 ="00ffffffffffff0026cd4d66f3030000271d0103803c22782ef6d5a7544b9e250d5054bfef80714f8140818081c09500b300d1c001014dd000a0f0703e8030203500544f2100001a000000ff0031313636333933393031303131000000fd00184c1fa03c000a202020202020000000fc00504c3237393255480a2020202001bd020340f35410050403020716011f12131420151106615d5e5f23090707830100006d030c001000387820006001020367d85dc40178c000e3050301e40f000001023a801871382d40582c4500544f2100001e565e00a0a0a0295030203500544f2100001af45100a0f070198030203500544f2100001e000000000000000000d6"; + }; + config = { + DP1 = { + enable = true; + primary = true; + position = "0x0"; + mode = "3840x2160"; + dpi = 192; + }; + eDP1 = { + enable = false; + primary = false; + position = "3840x0"; + mode = "2560x1440"; + }; + }; + }; + }; + }; +} diff --git a/nixos/i3/battery-popup.sh b/nixos/i3/battery-popup.sh new file mode 100755 index 00000000..404e7eb4 --- /dev/null +++ b/nixos/i3/battery-popup.sh @@ -0,0 +1,246 @@ +#!/usr/bin/env bash + +################################################################################ +# A script that shows a battery warning on i3wm # +# # +# It supports multiple batteries # +# (like my thinkpad T450s has) # +# # +# When tcl/tk (wish) is installed, it shows a nice popup # +# Which you can configure to show on all workspaces # +# by adding the following to your i3 config: # +# "for_window [title="Battery Warning"] sticky enable" # +# # +# By default, the script will show two messages: # +# One at 10% and one at 5% battery # +# # +# The script takes the following options: # +# -L : The percentage at which the first popup shows (default: 10) # +# # +# -l : The percentage at which the second popup shows # +# Default: half of the percentage given by -L # +# # +# -m : The message to show to the User # +# # +# -t : The time interval the script waits before checking the battery again. # +# Give this a value in seconds: 5s, 10s, or in minutes: 5m # +# Default: 5m # +# # +# -s : Play a sound file. This uses the command 'aplay' and depends on # +# a working pulseaudio installation # +# # +# -v : The volume to play audio at. Expects a number 0-100. # +# # +# -n : Use notify-send for message. # +# # +# -N : Don't use Tcl/Tk dialog. Use i3-nagbar. # +# # +# By R-J Ekker, 2016 # +# Thanks to: # +# - Louis-Jacob Lebel (https://github.com/lebel-louisjacob) # +# - Martin Jablečník (https://github.com/Applemann) # +################################################################################ + +error () { + echo "$1" >&2 + echo "Exiting" >&2 + exit "$2" +} + +while getopts 's:v:L:l:m:t:s:F:i:nND' opt; do + case $opt in + L) + [[ $OPTARG =~ ^[0-9]+$ ]] || error "${opt}: ${OPTARG} is not a number" 2 + UPPER_LIMIT="${OPTARG}" + ;; + l) + [[ $OPTARG =~ ^[0-9]+$ ]] || error "${opt}: ${OPTARG} is not a number" 2 + LOWER_LIMIT="${OPTARG}" + ;; + m) + MESSAGE="${OPTARG}" + ;; + n) + USE_NOTIFY_SEND="y" + ;; + i) + NOTIFY_ICON="${OPTARG}" + ;; + N) + DONT_USE_WISH="-n" + ;; + t) + [[ $OPTARG =~ ^[0-9]+[ms]?$ ]] || error "${opt}: ${OPTARG} is not a valid period" 2 + SLEEP_TIME="${OPTARG}" + ;; + s) + [ -f "$OPTARG" ] || error "${opt}: ${OPTARG}: no such file" 2 + SOUND_TO_PLAY="${OPTARG}" + ;; + v) + SOUND_VOLUME_PERC="${OPTARG}" + [[ $OPTARG -ge 0 && $OPTARG -le 100 ]] || error "${opt}: ${OPTARG}: not an integer between 0 and 100" 2 + SOUND_VOLUME=$(( "$OPTARG" * 65536 / 100 )) + ;; + D) + # Print some extra info + DEBUG="y" + ;; + F) + # Redirect debugging info to logfile + # if -D not specified this will log nothing + LOGFILE="${OPTARG}" + ;; + :) + error "Option -$OPTARG requires an argument." 2 + ;; + \?) + exit 2 + ;; + esac +done + +# This function returns an awk script +# Which prints the battery percentage +# It's an ugly way to include a nicely indented awk script here +get_awk_source() { + cat </dev/null + +get_battery_perc() { + awk -f <(get_awk_source) "${BATTERIES[@]}" +} + +show_popup() { + WISH_SCRIPT="wm state . withdrawn; tk_messageBox -icon warning -title \"Battery Warning\" -message \"${1}\"; exit" + echo "$WISH_SCRIPT" | wish +} + +show_nagbar(){ + i3-msg "exec i3-nagbar -m \"${1}\"" +} + +show_notify(){ + GNOME_ICON="/usr/share/icons/gnome/scalable/status/battery-low-symbolic.svg" + XFCE_ICON="/usr/share/icons/elementary-xfce/status/48/battery-low.png" + # try to find nice notify icon + if [[ -z $NOTIFY_ICON ]]; then + if [[ -f $GNOME_ICON ]]; then + NOTIFY_ICON="${GNOME_ICON}" + elif [[ -f $XFCE_ICON ]]; then + NOTIFY_ICON="${XFCE_ICON}" + fi + fi + [[ -n $NOTIFY_ICON ]] && NOTIFY_OPT="-i ${NOTIFY_ICON}" + notify-send -u critical "${1}" ${NOTIFY_OPT} +} + +play_sound(){ + if [[ -n $SOUND_TO_PLAY ]]; then + paplay "$SOUND_TO_PLAY" --volume $SOUND_VOLUME + fi +} + +show_message(){ + play_sound & + debug "$1" + if [[ -n $USE_NOTIFY_SEND ]] && which notify-send; then + show_notify "$1" + elif [[ -z $DONT_USE_WISH ]] && which wish; then + show_popup "$1" + else + show_nagbar "$1" + fi +} >&2 + +debug(){ + [[ -n $DEBUG ]] && echo "$1" +} + +main (){ + # Setting defaults + UPPER_LIMIT="${UPPER_LIMIT:-10}" + UPPER_HALF=$(( UPPER_LIMIT / 2 )) + LOWER_LIMIT=${LOWER_LIMIT:-$UPPER_HALF} + MESSAGE="${MESSAGE:-Warning: Battery is getting low}" + SLEEP_TIME="${SLEEP_TIME:-5m}" + # Note: BATTERIES is an array + BATTERIES=( /sys/class/power_supply/BAT*/uevent ) + SOUND_VOLUME="${SOUND_VOLUME:-65536}" + + debug "Upper ${UPPER_LIMIT}; Lower ${LOWER_LIMIT}; sleep ${SLEEP_TIME}" + debug "Current: $(get_battery_perc)%" + [[ -n $SOUND_TO_PLAY ]] && debug "Playing: \"${SOUND_TO_PLAY}\", Volume: ${SOUND_VOLUME_PERC}%" + + LIMIT="${UPPER_LIMIT}" + # This will be set to "y" after first click + # So we know when to stop nagging + POPUP_CLICKED="" + + while true; do + debug "Checking.. " + + PERC=$(get_battery_perc) + debug "got ${PERC}%" + + if is_battery_discharging; then + debug "Battery is discharging" + + if [[ $PERC -lt $LIMIT ]]; then + debug "showing warning" + show_message "${MESSAGE}" + + if [[ -z $POPUP_CLICKED ]]; then + # first click; set limit lower + POPUP_CLICKED="y" + LIMIT=${LOWER_LIMIT} + else + # We clicked twice; No more popups + LIMIT=0 + fi + fi + else + # restart messages, reset limits + POPUP_CLICKED="" + if [[ $PERC -gt $UPPER_LIMIT ]]; then + LIMIT=${UPPER_LIMIT} + else + LIMIT=${LOWER_LIMIT} + fi + fi + debug "sleeping ${SLEEP_TIME}; current limit ${LIMIT}%; ${POPUP_CLICKED:+Popup was clicked}" + sleep "${SLEEP_TIME}" + done +} + + +if [[ -n $LOGFILE ]]; then + exec >>"$LOGFILE" 2>&1 +fi + +main + diff --git a/nixos/i3/dunst.nix b/nixos/i3/dunst.nix new file mode 100644 index 00000000..f804d39f --- /dev/null +++ b/nixos/i3/dunst.nix @@ -0,0 +1,6 @@ +{ config, pkgs, ... }: +{ + services.dunst = { + enable = true; + }; +} diff --git a/nixos/i3/home.nix b/nixos/i3/home.nix index aa78073c..fc736a52 100644 --- a/nixos/i3/home.nix +++ b/nixos/i3/home.nix @@ -1,5 +1,12 @@ { config, pkgs, ... }: { + imports = [ + ./polybar/polybar.nix + ./i3.nix + ./dunst.nix + ./autorandr.nix + ]; + services = { compton = { enable = true; @@ -17,57 +24,5 @@ enable = true; }; - programs.autorandr = { - enable = true; - hooks = { - postswitch = { - "restart-compton" = "systemctl --user restart compton"; - "restart-i3" = "i3-msg restart"; - "restart-services" = "systemctl --user restart kdeconnect-indicator.service kdeconnect.service network-manager-applet.service pasystray.service polybar.service"; - }; - }; - - profiles = { - "foureighty-alone" = { - fingerprint = { - eDP1 = "00ffffffffffff0006af362300000000001b0104a51f117802f4f5a4544d9c270f505400000001010101010101010101010101010101e65f00a0a0a040503020350035ae100000180000000f0000000000000000000000000020000000fe0041554f0a202020202020202020000000fe004231343051414e30322e33200a00b2"; - }; - config = { - eDP1 = { - enable = true; - primary = true; - mode = "2560x1440"; - dpi = 144; - }; - }; - }; - "foureighty-docked" = { - fingerprint = { - eDP1 = "00ffffffffffff0006af362300000000001b0104a51f117802f4f5a4544d9c270f505400000001010101010101010101010101010101e65f00a0a0a040503020350035ae100000180000000f0000000000000000000000000020000000fe0041554f0a202020202020202020000000fe004231343051414e30322e33200a00b2"; - DP1 ="00ffffffffffff0026cd4d66f3030000271d0103803c22782ef6d5a7544b9e250d5054bfef80714f8140818081c09500b300d1c001014dd000a0f0703e8030203500544f2100001a000000ff0031313636333933393031303131000000fd00184c1fa03c000a202020202020000000fc00504c3237393255480a2020202001bd020340f35410050403020716011f12131420151106615d5e5f23090707830100006d030c001000387820006001020367d85dc40178c000e3050301e40f000001023a801871382d40582c4500544f2100001e565e00a0a0a0295030203500544f2100001af45100a0f070198030203500544f2100001e000000000000000000d6"; - }; - config = { - DP1 = { - enable = true; - primary = true; - position = "0x0"; - mode = "3840x2160"; - dpi = 192; - }; - eDP1 = { - enable = false; - primary = false; - position = "3840x0"; - mode = "2560x1440"; - }; - }; - }; - }; - }; - - imports = [ - ./polybar/polybar.nix - ./i3.nix - ]; } diff --git a/nixos/i3/i3.nix b/nixos/i3/i3.nix index 2dc4b885..c26d5807 100644 --- a/nixos/i3/i3.nix +++ b/nixos/i3/i3.nix @@ -13,6 +13,7 @@ in { command = "exec setxkbmap -layout pl"; always = true; notification = false; } { command = "exec autorandr -c"; always = false; notification = false; } { command = "exec $HOME/dev/dotfiles/nixos/i3/lock.sh"; always = false; notification = false; } + { command = "exec $HOME/dev/dotfiles/nixos/i3/battery-popup.sh"; always = false; notification = false; } { command = "exec xdg-mime default org.gnome.Evince.desktop application/pdf"; always = false; notification = false; } ];