summary refs log tree commit diff stats
path: root/bin
diff options
context:
space:
mode:
authorChristian Krinitsin <code@krinitsin.xyz>2024-06-04 22:00:30 +0200
committerChristian Krinitsin <code@krinitsin.xyz>2024-06-04 22:00:30 +0200
commitf470ff39e653eea2ea17d8dcc8c9c1bf1185a4ff (patch)
treef916c30d92c9654efe4b73bdca92770510287097 /bin
parent0bf547ad73c360b8e86c7e632735cdd926b38cb4 (diff)
parent2dc33f72ec5ffacfc48ba301c3c2d5de8f2c6b89 (diff)
downloaddotfiles-f470ff39e653eea2ea17d8dcc8c9c1bf1185a4ff.tar.gz
dotfiles-f470ff39e653eea2ea17d8dcc8c9c1bf1185a4ff.zip
Merge branch 'main' of github.com:ckrinitsin/dotfiles
Diffstat (limited to 'bin')
-rwxr-xr-xbin/bluetooth-devices39
-rwxr-xr-xbin/blur-lock11
-rwxr-xr-xbin/gaps60
-rwxr-xr-xbin/powermenu186
-rwxr-xr-xbin/volume_brightness102
5 files changed, 391 insertions, 7 deletions
diff --git a/bin/bluetooth-devices b/bin/bluetooth-devices
index 5cc4876..f5fe284 100755
--- a/bin/bluetooth-devices
+++ b/bin/bluetooth-devices
@@ -2,11 +2,34 @@
 
 #
 # This program lists all paired devices which you can select from, the selected one
-# will be connected
+# will be connected / disconnected 
 #   
 
-# Opens dmenu prompt, which lets you decide which device you want to connect to
-DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu)
+connect_bluetooth() {
+    local MAC=$1
+    local DEVICE=$2
+    if bluetoothctl connect $MAC | grep -q 'successful' 
+    then
+        notify-send -t 5000 -r 2954 -u normal "  Connected successfully from" "     $DEVICE"
+    else 
+        notify-send -t 5000 -r 2954 -u normal "  Couldn't connect from" "     $DEVICE"
+    fi
+}
+
+
+disconnect_bluetooth() {
+    local MAC=$1
+    local DEVICE=$2
+    if bluetoothctl disconnect $MAC | grep -q 'Successful' 
+    then
+        notify-send -t 5000 -r 2954 -u normal "  Disconnected successfully from" "     $DEVICE"
+    else 
+        notify-send -t 5000 -r 2954 -u normal "  Couldn't disconnect from" "     $DEVICE"
+    fi
+}
+
+# Opens dmenu prompt, which lets you decide which device you want to connect to / disconnect from
+DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu -i)
 
 # If dmenu was cancelled, exit program
 if [ $? -ne 0 ]; then
@@ -16,10 +39,12 @@ fi
 # Get MAC adress of the device you selected
 MAC=$(bluetoothctl devices | grep "$DEVICE" | sed 's/[^ ]* //' | cut -d ' ' -f1)
 
-# Send a notify whether the connection was successful 
-if bluetoothctl connect $MAC | grep -q 'successful' 
+# If bluetooth device is already connected, disconnect, else connect
+CONNECTED=$(bluetoothctl devices Connected | cut -f3 -d ' ')
+if echo $CONNECTED | grep $DEVICE
 then
-    notify-send -t 5000 -r 2954 -u normal "  Connected successfully to" "     $DEVICE"
+    disconnect_bluetooth $MAC $DEVICE
 else 
-    notify-send -t 5000 -r 2954 -u normal "  Couldn't connect to" "     $DEVICE"
+    connect_bluetooth $MAC $DEVICE
 fi
+
diff --git a/bin/blur-lock b/bin/blur-lock
new file mode 100755
index 0000000..7b5aa6a
--- /dev/null
+++ b/bin/blur-lock
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+
+PICTURE=/tmp/swaylock.png
+SCREENSHOT="grim $PICTURE"
+
+BLUR="5x4"
+
+$SCREENSHOT
+convert $PICTURE -blur $BLUR $PICTURE
+swaylock -i $PICTURE
+rm $PICTURE
diff --git a/bin/gaps b/bin/gaps
new file mode 100755
index 0000000..10ac7a2
--- /dev/null
+++ b/bin/gaps
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+
+# 
+# Interactive gaps, depending on number of applications on a workspace
+#
+
+import i3ipc
+i3 = i3ipc.Connection()
+print("starting")
+
+#################### -- window management -- ####################
+def two_gap():
+    i3.command('gaps left current set 200')
+    i3.command('gaps right current set 200')
+    
+def one_gap():    
+    i3.command('gaps right current set 400')
+    i3.command('gaps left current set 400')
+
+def remove_gaps():
+    i3.command('gaps left current set 0')
+    i3.command('gaps right current set 0')    
+   
+def make_window_normal(workspace):
+    i3.command('fullscreen disable')
+    
+def make_window_fullscreen(workspace):
+    i3.command('fullscreen enable')
+    
+def manage_new_close_window(self, e):
+    focused = i3.get_tree().find_focused()
+    workspace = focused.workspace()
+    monitor = workspace.ipc_data['output']
+
+    if monitor != "HDMI-A-1":
+        return
+
+    y = len(workspace.nodes)
+    
+    if y > 2:
+        remove_gaps()
+        return
+
+    if y == 2:
+        two_gap()
+        return
+    
+    one_gap()
+    return
+########################### -- end -- ###########################
+
+
+i3.on('window::new', manage_new_close_window)
+i3.on('window::close', manage_new_close_window)
+i3.on('window::move', manage_new_close_window)
+i3.on('window::focus', manage_new_close_window)
+i3.on('workspace::empty', manage_new_close_window)
+i3.on('workspace::init', manage_new_close_window)
+
+i3.main()
diff --git a/bin/powermenu b/bin/powermenu
new file mode 100755
index 0000000..ac1d677
--- /dev/null
+++ b/bin/powermenu
@@ -0,0 +1,186 @@
+#!/usr/bin/env bash
+#
+# Use rofi/zenity to change system runstate thanks to systemd.
+#
+# Note: this currently relies on associative array support in the shell.
+#
+# Inspired from i3pystatus wiki:
+# https://github.com/enkore/i3pystatus/wiki/Shutdown-Menu
+#
+# Copyright 2015 Benjamin Chrétien <chretien at lirmm dot fr>
+#
+# This program 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.
+
+# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# modified to work with latest rofi update by joekamprad <joekamprad@endeavouros.com>
+
+#######################################################################
+#                            BEGIN CONFIG                             #
+#######################################################################
+
+# Use a custom lock script
+#LOCKSCRIPT="i3lock-extra -m pixelize"
+
+# Colors: FG (foreground), BG (background), HL (highlighted)
+FG_COLOR="#bbbbbb"
+BG_COLOR="#111111"
+HLFG_COLOR="#111111"
+HLBG_COLOR="#bbbbbb"
+BORDER_COLOR="#222222"
+
+# Options not related to colors (most rofi options do not work anymore)
+ROFI_OPTIONS=(-theme ~/.config/rofi/powermenu.rasi)
+# Zenity options
+ZENITY_TITLE="Power Menu"
+ZENITY_TEXT="Action:"
+ZENITY_OPTIONS=(--column= --hide-header)
+
+#######################################################################
+#                             END CONFIG                              #
+#######################################################################
+
+# Whether to ask for user's confirmation
+enable_confirmation=false
+
+# Preferred launcher if both are available
+preferred_launcher="rofi"
+
+usage="$(basename "$0") [-h] [-c] [-p name] -- display a menu for shutdown, reboot, lock etc.
+
+where:
+    -h  show this help text
+    -c  ask for user confirmation
+    -p  preferred launcher (rofi or zenity)
+
+This script depends on:
+  - systemd,
+  - sway,
+  - rofi or zenity."
+
+# Check whether the user-defined launcher is valid
+launcher_list=(rofi zenity)
+function check_launcher() {
+  if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
+    echo "Supported launchers: ${launcher_list[*]}"
+    exit 1
+  else
+    # Get array with unique elements and preferred launcher first
+    # Note: uniq expects a sorted list, so we cannot use it
+    i=1
+    launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \
+      | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' '))
+  fi
+}
+
+# Parse CLI arguments
+while getopts "hcp:" option; do
+  case "${option}" in
+    h) echo "${usage}"
+       exit 0
+       ;;
+    c) enable_confirmation=true
+       ;;
+    p) preferred_launcher="${OPTARG}"
+       check_launcher "${preferred_launcher}"
+       ;;
+    *) exit 1
+       ;;
+  esac
+done
+
+# Check whether a command exists
+function command_exists() {
+  command -v "$1" &> /dev/null 2>&1
+}
+
+# systemctl required
+if ! command_exists systemctl ; then
+  exit 1
+fi
+
+# menu defined as an associative array
+typeset -A menu
+
+# Menu with keys/commands
+
+menu=(
+  [  Shutdown]="systemctl poweroff"
+  [  Reboot]="systemctl reboot"
+  [  Suspend]="systemctl suspend"
+  [  Hibernate]="systemctl hibernate"
+  [  Lock]="~/.config/waybar/scripts/blur-lock"
+  [  Logout]="swaymsg exit"
+  [  Cancel]=""
+)
+
+menu_nrows=${#menu[@]}
+
+# Menu entries that may trigger a confirmation message
+menu_confirm="Shutdown Reboot Hibernate Suspend Halt Logout"
+
+launcher_exe=""
+launcher_options=""
+rofi_colors=""
+
+function prepare_launcher() {
+  if [[ "$1" == "rofi" ]]; then
+    rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \
+        -hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}")
+    launcher_exe="rofi"
+    launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \
+        "${rofi_colors}" "${ROFI_OPTIONS[@]}")
+  elif [[ "$1" == "zenity" ]]; then
+    launcher_exe="zenity"
+    launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \
+        "${ZENITY_OPTIONS[@]}")
+  fi
+}
+
+for l in "${launcher_list[@]}"; do
+  if command_exists "${l}" ; then
+    prepare_launcher "${l}"
+    break
+  fi
+done
+
+# No launcher available
+if [[ -z "${launcher_exe}" ]]; then
+  exit 1
+fi
+
+launcher=(${launcher_exe} "${launcher_options[@]}")
+selection="$(printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}")"
+
+function ask_confirmation() {
+  if [ "${launcher_exe}" == "rofi" ]; then
+    confirmed=$(echo -e "Yes\nNo" | rofi -dmenu -i -lines 2 -p "${selection}?" \
+      "${rofi_colors}" "${ROFI_OPTIONS[@]}")
+    [ "${confirmed}" == "Yes" ] && confirmed=0
+  elif [ "${launcher_exe}" == "zenity" ]; then
+    zenity --question --text "Are you sure you want to ${selection,,}?"
+    confirmed=$?
+  fi
+
+  if [ "${confirmed}" == 0 ]; then
+    swaymsg -q "exec --no-startup-id ${menu[${selection}]}"
+  fi
+}
+
+if [[ $? -eq 0 && ! -z ${selection} ]]; then
+  if [[ "${enable_confirmation}" = true && \
+        ${menu_confirm} =~ (^|[[:space:]])"${selection}"($|[[:space:]]) ]]; then
+    ask_confirmation
+  else
+    swaymsg -q "exec --no-startup-id ${menu[${selection}]}"
+  fi
+fi
diff --git a/bin/volume_brightness b/bin/volume_brightness
new file mode 100755
index 0000000..f6174ca
--- /dev/null
+++ b/bin/volume_brightness
@@ -0,0 +1,102 @@
+#!/bin/bash
+# original source: https://gitlab.com/Nmoleo/i3-volume-brightness-indicator
+
+# taken from here: https://gitlab.com/Nmoleo/i3-volume-brightness-indicator
+
+# See README.md for usage instructions
+bar_color="#d3c6aa"
+volume_step=2
+brightness_step=2.5
+max_volume=100
+
+# Uses regex to get volume from pactl
+function get_volume {
+    pactl get-sink-volume @DEFAULT_SINK@ | grep -Po '[0-9]{1,3}(?=%)' | head -1
+}
+
+# Uses regex to get mute status from pactl
+function get_mute {
+    pactl get-sink-mute @DEFAULT_SINK@ | grep -Po '(?<=Mute: )(yes|no)'
+}
+
+# Uses regex to get brightness from xbacklight
+function get_brightness {
+    brightnessctl | grep "Current brightness" | cut -d '(' -f 2 | cut -d '%' -f 1 
+}
+
+# Returns a mute icon, a volume-low icon, or a volume-high icon, depending on the volume
+function get_volume_icon {
+    volume=$(get_volume)
+    mute=$(get_mute)
+    if [ "$mute" == "yes" ] ; then
+        volume_icon="  "
+    elif [ "$volume" -eq 0 ]; then 
+        volume_icon="  " 
+    elif [ "$volume" -lt 50 ]; then
+        volume_icon="  "
+    else
+        volume_icon="  "
+    fi
+}
+
+# Always returns the same icon - I couldn't get the brightness-low icon to work with fontawesome
+function get_brightness_icon {
+    brightness_icon="  "
+}
+
+# Displays a volume notification using dunstify
+function show_volume_notif {
+    volume=$(get_mute)
+    get_volume_icon
+    if [ "$mute" == "yes" ]; then
+        dunstify -t 1000 -r 2593 -u normal "$volume_icon" -h int:value:0 -h string:hlcolor:$bar_color
+    else
+        dunstify -t 1000 -r 2593 -u normal "$volume_icon $volume%" -h int:value:$volume -h string:hlcolor:$bar_color
+    fi
+}
+
+# Displays a brightness notification using dunstify
+function show_brightness_notif {
+    brightness=$(get_brightness)
+    get_brightness_icon
+    dunstify -t 1000 -r 2593 -u normal "$brightness_icon $brightness%" -h int:value:$brightness -h string:hlcolor:$bar_color
+}
+
+# Main function - Takes user input, "volume_up", "volume_down", "brightness_up", or "brightness_down"
+case $1 in
+    volume_up)
+    # Unmutes and increases volume, then displays the notification
+    pactl set-sink-mute @DEFAULT_SINK@ 0
+    volume=$(get_volume)
+    if [ $(( "$volume" + "$volume_step" )) -gt $max_volume ]; then
+        pactl set-sink-volume @DEFAULT_SINK@ $max_volume%
+    else
+        pactl set-sink-volume @DEFAULT_SINK@ +$volume_step%
+    fi
+    show_volume_notif
+    ;;
+
+    volume_down)
+    # Raises volume and displays the notification
+    pactl set-sink-volume @DEFAULT_SINK@ -$volume_step%
+    show_volume_notif
+    ;;
+
+    volume_mute)
+    # Toggles mute and displays the notification
+    pactl set-sink-mute @DEFAULT_SINK@ toggle
+    show_volume_notif
+    ;;
+
+    brightness_up)
+    # Increases brightness and displays the notification
+    brightnessctl set +$brightness_step% 
+    show_brightness_notif
+    ;;
+
+    brightness_down)
+    # Decreases brightness and displays the notification
+    brightnessctl set $brightness_step%- 
+    show_brightness_notif
+    ;;
+esac