scripts
This commit is contained in:
parent
e64b64d3f6
commit
6f02789d3f
1
config/.config/bspwm/noswallow
Normal file
1
config/.config/bspwm/noswallow
Normal file
@ -0,0 +1 @@
|
||||
xev
|
1
config/.config/bspwm/terminals
Normal file
1
config/.config/bspwm/terminals
Normal file
@ -0,0 +1 @@
|
||||
St
|
70
config/.scripts/bspswallow
Executable file
70
config/.scripts/bspswallow
Executable file
@ -0,0 +1,70 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Get class of a wid
|
||||
get_class() {
|
||||
id=$1
|
||||
if [ -z "$id" ]; then
|
||||
echo ""
|
||||
else
|
||||
xprop -id "$id" | sed -n \
|
||||
-e '/WM_CLASS/s/\( "\)/\n/g; s/[^\n]*\n\([^"]*\)[^\n]*/\1\n/g' \
|
||||
-e '/WM_COMMAND/s/[^"]*/\n\n/1; s/\n"//1; s/\n\([^"]*\)".*/\n\1/; /\n/p' | sed '/^$/d' | head -n1
|
||||
fi
|
||||
}
|
||||
|
||||
get_pid() {
|
||||
xprop _NET_WM_PID -id "$1" | awk '/[0-9]+/ {print $3}'
|
||||
}
|
||||
|
||||
get_ppid() {
|
||||
ps -o ppid= -p $1
|
||||
}
|
||||
|
||||
check_swallowing_process() {
|
||||
pid=$(get_pid $1)
|
||||
[ -z $pid ] && return 0
|
||||
gppid=$(get_ppid $(get_ppid $pid))
|
||||
grep -i "^$(ps -o cmd= -p $gppid | awk '{print $1}' | sed 's_^.*/__')$" ~/.config/bspwm/terminals && return 0
|
||||
[ "$(get_ppid "$(get_ppid "$(get_pid "$1")")")" = "$(get_pid "$2")" ] && return 0 || return 1
|
||||
}
|
||||
|
||||
swallow() {
|
||||
addedtodesktop=$2
|
||||
lasttermdesktop=$(bspc query -D -n last)
|
||||
|
||||
swallowerid=$1
|
||||
swallowingid=$(bspc query -N -n last)
|
||||
if [ "$addedtodesktop" = "$lasttermdesktop" ] && check_swallowing_process "$swallowerid" "$swallowingid"; then
|
||||
grep -q "^$(get_class "$swallowerid")$" ~/.config/bspwm/{noswallow, terminals} && return
|
||||
grep -i "^$(get_class "$swallowingid")$" ~/.config/bspwm/terminals || return
|
||||
echo "$swallowerid $swallowingid" >> /tmp/swallowids
|
||||
bspc node "$swallowingid" --flag hidden=on
|
||||
fi
|
||||
}
|
||||
|
||||
spit() {
|
||||
spitterid=$1
|
||||
spitterdesktop=$2
|
||||
grep "^$spitterid" /tmp/swallowids || return
|
||||
spittingid=$(grep "^$spitterid" /tmp/swallowids | head -n1 | awk '{print $2}')
|
||||
|
||||
bspc node "$spittingid" --flag hidden=off
|
||||
|
||||
termdesktop=$(bspc query -D -n "$spittingid")
|
||||
[ "$termdesktop" = "$spitterdesktop" ] || bspc node "$spittingid" -d "$spitterdesktop"
|
||||
|
||||
bspc node "$spittingid" -f
|
||||
sed -i "/^$spitterid/d" /tmp/swallowids
|
||||
}
|
||||
|
||||
bspc subscribe node_add node_remove | while read -r event
|
||||
do
|
||||
case $(echo "$event" | awk '{ print $1 }') in
|
||||
node_add)
|
||||
swallow $(echo "$event" | awk '{print $5 " " $3}')
|
||||
;;
|
||||
node_remove)
|
||||
spit $(echo "$event" | awk '{print $4 " " $3}')
|
||||
;;
|
||||
esac
|
||||
done
|
82
config/.scripts/displayselect
Executable file
82
config/.scripts/displayselect
Executable file
@ -0,0 +1,82 @@
|
||||
#!/bin/sh
|
||||
|
||||
# A UI for detecting and selecting all displays. Probes xrandr for connected
|
||||
# displays and lets user select one to use. User may also select "manual
|
||||
# selection" which opens arandr.
|
||||
|
||||
twoscreen() { # If multi-monitor is selected and there are two screens.
|
||||
|
||||
mirror=$(printf "no\\nyes" | dmenu -i -p "Mirror displays?")
|
||||
# Mirror displays using native resolution of external display and a scaled
|
||||
# version for the internal display
|
||||
if [ "$mirror" = "yes" ]; then
|
||||
external=$(echo "$screens" | dmenu -i -p "Optimize resolution for:")
|
||||
internal=$(echo "$screens" | grep -v "$external")
|
||||
|
||||
res_external=$(xrandr --query | sed -n "/^$external/,/\+/p" | \
|
||||
tail -n 1 | awk '{print $1}')
|
||||
res_internal=$(xrandr --query | sed -n "/^$internal/,/\+/p" | \
|
||||
tail -n 1 | awk '{print $1}')
|
||||
|
||||
res_ext_x=$(echo "$res_external" | sed 's/x.*//')
|
||||
res_ext_y=$(echo "$res_external" | sed 's/.*x//')
|
||||
res_int_x=$(echo "$res_internal" | sed 's/x.*//')
|
||||
res_int_y=$(echo "$res_internal" | sed 's/.*x//')
|
||||
|
||||
scale_x=$(echo "$res_ext_x / $res_int_x" | bc -l)
|
||||
scale_y=$(echo "$res_ext_y / $res_int_y" | bc -l)
|
||||
|
||||
xrandr --output "$external" --auto --scale 1.0x1.0 \
|
||||
--output "$internal" --auto --same-as "$external" \
|
||||
--scale "$scale_x"x"$scale_y"
|
||||
else
|
||||
|
||||
primary=$(echo "$screens" | dmenu -i -p "Select primary display:")
|
||||
secondary=$(echo "$screens" | grep -v ^"$primary"$)
|
||||
direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?")
|
||||
xrandr --output "$primary" --auto --scale 1.0x1.0 --output "$secondary" --"$direction"-of "$primary" --auto --scale 1.0x1.0
|
||||
fi
|
||||
}
|
||||
|
||||
morescreen() { # If multi-monitor is selected and there are more than two screens.
|
||||
primary=$(echo "$screens" | dmenu -i -p "Select primary display:")
|
||||
secondary=$(echo "$screens" | grep -v ^"$primary"$ | dmenu -i -p "Select secondary display:")
|
||||
direction=$(printf "left\\nright" | dmenu -i -p "What side of $primary should $secondary be on?")
|
||||
tertiary=$(echo "$screens" | grep -v ^"$primary"$ | grep -v ^"$secondary"$ | dmenu -i -p "Select third display:")
|
||||
xrandr --output "$primary" --auto --output "$secondary" --"$direction"-of "$primary" --auto --output "$tertiary" --"$(printf "left\\nright" | grep -v "$direction")"-of "$primary" --auto
|
||||
}
|
||||
|
||||
multimon() { # Multi-monitor handler.
|
||||
case "$(echo "$screens" | wc -l)" in
|
||||
2) twoscreen ;;
|
||||
*) morescreen ;;
|
||||
esac ;}
|
||||
|
||||
onescreen() { # If only one output available or chosen.
|
||||
xrandr --output "$1" --auto --scale 1.0x1.0 $(echo "$allposs" | grep -v "\b$1" | awk '{print "--output", $1, "--off"}' | paste -sd ' ' -)
|
||||
}
|
||||
|
||||
postrun() { # Stuff to run to clean up.
|
||||
setbg # Fix background if screen size/arangement has changed.
|
||||
{ killall dunst ; setsid -f dunst ;} >/dev/null 2>&1 # Restart dunst to ensure proper location on screen
|
||||
}
|
||||
|
||||
# Get all possible displays
|
||||
allposs=$(xrandr -q | grep "connected")
|
||||
|
||||
# Get all connected screens.
|
||||
screens=$(echo "$allposs" | awk '/ connected/ {print $1}')
|
||||
|
||||
# If there's only one screen
|
||||
[ "$(echo "$screens" | wc -l)" -lt 2 ] &&
|
||||
{ onescreen "$screens"; postrun; notify-send "💻 Only one screen detected." "Using it in its optimal settings..."; exit ;}
|
||||
|
||||
# Get user choice including multi-monitor and manual selection:
|
||||
chosen=$(printf "%s\\nmulti-monitor\\nmanual selection" "$screens" | dmenu -i -p "Select display arangement:") &&
|
||||
case "$chosen" in
|
||||
"manual selection") arandr ; exit ;;
|
||||
"multi-monitor") multimon ;;
|
||||
*) onescreen "$chosen" ;;
|
||||
esac
|
||||
|
||||
postrun
|
123
config/.scripts/dmenurecord
Executable file
123
config/.scripts/dmenurecord
Executable file
@ -0,0 +1,123 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Usage:
|
||||
# `$0`: Ask for recording type via dmenu
|
||||
# `$0 screencast`: Record both audio and screen
|
||||
# `$0 video`: Record only screen
|
||||
# `$0 audio`: Record only audio
|
||||
# `$0 kill`: Kill existing recording
|
||||
#
|
||||
# If there is already a running instance, user will be prompted to end it.
|
||||
|
||||
getdim() { xrandr | grep -oP '(?<=current ).*(?=,)' | tr -d ' ' ;}
|
||||
|
||||
updateicon() { \
|
||||
echo "$1" > /tmp/recordingicon
|
||||
pkill -RTMIN+9 "${STATUSBAR:-dwmblocks}"
|
||||
}
|
||||
|
||||
killrecording() {
|
||||
recpid="$(cat /tmp/recordingpid)"
|
||||
kill -15 "$recpid"
|
||||
rm -f /tmp/recordingpid
|
||||
updateicon ""
|
||||
pkill -RTMIN+9 "${STATUSBAR:-dwmblocks}"
|
||||
}
|
||||
|
||||
screencast() { \
|
||||
ffmpeg -y \
|
||||
-f x11grab \
|
||||
-framerate 30 \
|
||||
-s "$(getdim)" \
|
||||
-i "$DISPLAY" \
|
||||
-r 24 \
|
||||
-use_wallclock_as_timestamps 1 \
|
||||
-f alsa -thread_queue_size 1024 -i default \
|
||||
-c:v h264 \
|
||||
-crf 0 -preset ultrafast -c:a aac \
|
||||
"$HOME/Videos/Capture/screencast-$(date '+%y%m%d-%H%M-%S').mp4" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "⏺️🎙️"
|
||||
}
|
||||
|
||||
video() { ffmpeg \
|
||||
-f x11grab \
|
||||
-framerate 30 \
|
||||
-s "$(getdim)" \
|
||||
-i "$DISPLAY" \
|
||||
-c:v libx264 -qp 0 -r 30 \
|
||||
"$HOME/Videos/Capture/video-$(date '+%y%m%d-%H%M-%S').mkv" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "⏺️"
|
||||
}
|
||||
|
||||
webcamhidef() { ffmpeg \
|
||||
-f v4l2 \
|
||||
-i /dev/video0 \
|
||||
-video_size 1920x1080 \
|
||||
"$HOME/Videos/Capture/webcam-$(date '+%y%m%d-%H%M-%S').mkv" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "🎥"
|
||||
}
|
||||
|
||||
webcam() { ffmpeg \
|
||||
-f v4l2 \
|
||||
-i /dev/video0 \
|
||||
-video_size 640x480 \
|
||||
"$HOME/Videos/Capture/webcam-$(date '+%y%m%d-%H%M-%S').mkv" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "🎥"
|
||||
}
|
||||
|
||||
|
||||
audio() { \
|
||||
ffmpeg \
|
||||
-f alsa -i default \
|
||||
-c:a flac \
|
||||
"$HOME/Videos/Capture/audio-$(date '+%y%m%d-%H%M-%S').flac" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "🎙️"
|
||||
}
|
||||
|
||||
askrecording() { \
|
||||
choice=$(printf "screencast\\nvideo\\nvideo selected\\naudio\\nwebcam\\nwebcam (hi-def)" | dmenu -i -p "Select recording style:")
|
||||
case "$choice" in
|
||||
screencast) screencast;;
|
||||
audio) audio;;
|
||||
video) video;;
|
||||
*selected) videoselected;;
|
||||
webcam) webcam;;
|
||||
"webcam (hi-def)") webcamhidef;;
|
||||
esac
|
||||
}
|
||||
|
||||
asktoend() { \
|
||||
response=$(printf "No\\nYes" | dmenu -i -p "Recording still active. End recording?") &&
|
||||
[ "$response" = "Yes" ] && killrecording
|
||||
}
|
||||
|
||||
videoselected()
|
||||
{
|
||||
slop -f "%x %y %w %h" > /tmp/slop
|
||||
read -r X Y W H < /tmp/slop
|
||||
rm /tmp/slop
|
||||
|
||||
ffmpeg \
|
||||
-f x11grab \
|
||||
-framerate 30 \
|
||||
-video_size "$W"x"$H" \
|
||||
-i :0.0+"$X,$Y" \
|
||||
-c:v libx264 -qp 0 -r 30 \
|
||||
"$HOME/Videos/Capture/box-$(date '+%y%m%d-%H%M-%S').mkv" &
|
||||
echo $! > /tmp/recordingpid
|
||||
updateicon "⏺️"
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
screencast) screencast;;
|
||||
audio) audio;;
|
||||
video) video;;
|
||||
*selected) videoselected;;
|
||||
kill) killrecording;;
|
||||
*) ([ -f /tmp/recordingpid ] && asktoend && exit) || askrecording;;
|
||||
esac
|
119
config/.scripts/mounter
Executable file
119
config/.scripts/mounter
Executable file
@ -0,0 +1,119 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Mounts Android Phones and USB drives (encrypted or not). This script will
|
||||
# replace the older `dmenumount` which had extra steps and couldn't handle
|
||||
# encrypted drives.
|
||||
# TODO: Try decrypt for drives in crtypttab
|
||||
# TODO: Add some support for connecting iPhones (although they are annoying).
|
||||
|
||||
IFS='
|
||||
'
|
||||
# Function for escaping cell-phone names.
|
||||
escape(){ echo "$@" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g" ;}
|
||||
|
||||
# Check for phones.
|
||||
phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/📱/")"
|
||||
mountedphones="$(grep "simple-mtpfs" /etc/mtab)"
|
||||
# If there are already mounted phones, remove them from the list of mountables.
|
||||
[ -n "$mountedphones" ] && phones="$(for phone in $phones; do
|
||||
for mounted in $mountedphones; do
|
||||
escphone="$(escape "$phone")"
|
||||
[[ "$mounted" =~ "$escphone" ]] && break 1
|
||||
done && continue 1
|
||||
echo "$phone"
|
||||
done)"
|
||||
|
||||
# Check for drives.
|
||||
lsblkoutput="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")"
|
||||
# Get all LUKS drives
|
||||
allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)"
|
||||
# Get a list of the LUKS drive UUIDs already decrypted.
|
||||
decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")"
|
||||
# Functioning for formatting drives correctly for dmenu:
|
||||
filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}' ; }
|
||||
|
||||
# Get only LUKS drives that are not decrypted.
|
||||
unopenedluks="$(for drive in $allluks; do
|
||||
uuid="${drive%% *}"
|
||||
uuid="${uuid//-}" # This is a bashism.
|
||||
[ -n "$decrypted" ] && for open in $decrypted; do
|
||||
[ "$uuid" = "$open" ] && break 1
|
||||
done && continue 1
|
||||
echo "🔒 $drive"
|
||||
done | filter)"
|
||||
|
||||
# Get all normal, non-encrypted or decrypted partitions that are not mounted.
|
||||
normalparts="$(echo "$lsblkoutput"| grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/💾 /" | filter )"
|
||||
|
||||
# Add all to one variable. If no mountable drives found, exit.
|
||||
alldrives="$(echo "$phones
|
||||
$unopenedluks
|
||||
$normalparts" | sed "/^$/d;s/ *$//")"
|
||||
|
||||
# Quit the script if a sequential command fails.
|
||||
set -e
|
||||
|
||||
test -n "$alldrives"
|
||||
|
||||
# Feed all found drives to dmenu and get user choice.
|
||||
chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)"
|
||||
|
||||
# Function for prompting user for a mountpoint.
|
||||
getmount(){
|
||||
mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")"
|
||||
test -n "$mp"
|
||||
if [ ! -d "$mp" ]; then
|
||||
mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?")
|
||||
[ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
|
||||
fi
|
||||
}
|
||||
|
||||
attemptmount(){
|
||||
# Attempt to mount without a mountpoint, to see if drive is in fstab.
|
||||
sudo -A mount "$chosen" || return 1
|
||||
notify-send "💾Drive Mounted." "$chosen mounted."
|
||||
exit
|
||||
}
|
||||
|
||||
case "$chosen" in
|
||||
💾*)
|
||||
chosen="${chosen%% *}"
|
||||
chosen="${chosen:1}" # This is a bashism.
|
||||
parttype="$(echo "$lsblkoutput" | grep "$chosen")"
|
||||
attemptmount || getmount
|
||||
case "${parttype##* }" in
|
||||
vfat) sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000 ;;
|
||||
btrfs) sudo -A mount "$chosen" "$mp" ;;
|
||||
*) sudo -A mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)" ;;
|
||||
esac
|
||||
notify-send "💾Drive Mounted." "$chosen mounted to $mp."
|
||||
;;
|
||||
|
||||
🔒*)
|
||||
chosen="${chosen%% *}"
|
||||
chosen="${chosen:1}" # This is a bashism.
|
||||
# Number the drive.
|
||||
while true; do
|
||||
[ -f "/dev/mapper/usb$num" ] || break
|
||||
num="$(printf "%02d" "$((num +1))")"
|
||||
done
|
||||
|
||||
# Decrypt in a terminal window
|
||||
${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$chosen" "usb$num"
|
||||
# Check if now decrypted.
|
||||
test -b "/dev/mapper/usb$num"
|
||||
|
||||
attemptmount || getmount
|
||||
sudo -A mount "/dev/mapper/usb$num" "$mp" -o uid="$(id -u)",gid="$(id -g)"
|
||||
notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp."
|
||||
;;
|
||||
|
||||
📱*)
|
||||
notify-send "❗Note" "Remember to allow file access on your phone now."
|
||||
getmount
|
||||
number="${chosen%%:*}"
|
||||
number="${chosen:1}" # This is a bashism.
|
||||
sudo -A simple-mtpfs -o allow_other -o fsname="simple-mtpfs-$(escape "$chosen")" --device "$number" "$mp"
|
||||
notify-send "🤖 Android Mounted." "Android device mounted to $mp."
|
||||
;;
|
||||
esac
|
28
config/.scripts/unmounter
Executable file
28
config/.scripts/unmounter
Executable file
@ -0,0 +1,28 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Unmount USB drives or Android phones. Replaces the older `dmenuumount`. Fewer
|
||||
# prompt and also de-decrypts LUKS drives that are unmounted.
|
||||
|
||||
set -e
|
||||
|
||||
mounteddroids="$(grep simple-mtpfs /etc/mtab | awk '{print "📱" $2}')"
|
||||
lsblkoutput="$(lsblk -nrpo "name,type,size,mountpoint")"
|
||||
mounteddrives="$(echo "$lsblkoutput" | awk '($2=="part"||$2="crypt")&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "💾%s (%s)\n",$4,$3}')"
|
||||
|
||||
allunmountable="$(echo "$mounteddroids
|
||||
$mounteddrives" | sed "/^$/d;s/ *$//")"
|
||||
test -n "$allunmountable"
|
||||
|
||||
chosen="$(echo "$allunmountable" | dmenu -i -p "Unmount which drive?")"
|
||||
chosen="${chosen%% *}"
|
||||
test -n "$chosen"
|
||||
|
||||
sudo -A umount -l "/${chosen#*/}"
|
||||
notify-send "Device unmounted." "$chosen has been unmounted."
|
||||
|
||||
# Close the chosen drive if decrypted.
|
||||
cryptid="$(echo "$lsblkoutput" | grep "/${chosen#*/}$")"
|
||||
cryptid="${cryptid%% *}"
|
||||
test -b /dev/mapper/"${cryptid##*/}"
|
||||
sudo -A cryptsetup close "$cryptid"
|
||||
notify-send "🔒Device dencryption closed." "Drive is now securely locked again."
|
Loading…
Reference in New Issue
Block a user