aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'dot_config/i3/scripts/executable_power-profiles')
-rw-r--r--dot_config/i3/scripts/executable_power-profiles190
1 files changed, 190 insertions, 0 deletions
diff --git a/dot_config/i3/scripts/executable_power-profiles b/dot_config/i3/scripts/executable_power-profiles
new file mode 100644
index 0000000..feb63dc
--- /dev/null
+++ b/dot_config/i3/scripts/executable_power-profiles
@@ -0,0 +1,190 @@
1#!/usr/bin/env bash
2#
3# Use rofi/zenity to change system runstate thanks to systemd.
4#
5# Note: this currently relies on associative array support in the shell.
6#
7# Inspired from i3pystatus wiki:
8# https://github.com/enkore/i3pystatus/wiki/Shutdown-Menu
9#
10# Copyright 2015 Benjamin Chrétien <chretien at lirmm dot fr>
11#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21
22# You should have received a copy of the GNU General Public License
23# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25# power-profiles-daemon implementation:
26# needs package power-profiles-daemon installed and the service running see here:
27# https://wiki.archlinux.org/title/CPU_frequency_scaling#power-profiles-daemon
28# used in i3-blocks: ~/.config/i3/i3blocks.conf together with: ~/.config/i3/scripts/ppd-status
29
30
31#######################################################################
32# BEGIN CONFIG #
33#######################################################################
34
35# Use a custom lock script
36#LOCKSCRIPT="i3lock-extra -m pixelize"
37
38# Colors: FG (foreground), BG (background), HL (highlighted)
39FG_COLOR="#bbbbbb"
40BG_COLOR="#111111"
41HLFG_COLOR="#111111"
42HLBG_COLOR="#bbbbbb"
43BORDER_COLOR="#222222"
44
45# Options not related to colors
46#ROFI_TEXT=":"
47#ROFI_OPTIONS=(-width -11 -location 0 -hide-scrollbar -bw 30 -color-window "#dd310027,#dd0310027,#dd310027" -padding 5)
48#ROFI_OPTIONS=(-width -18 -location 4 -hide-scrollbar -color-window "#cc310027,#00a0009a,#cc310027" -padding 5 -font "Sourcecode Pro Regular 10, FontAwesome 9")
49ROFI_OPTIONS=(-theme ~/.config/rofi/power-profiles.rasi)
50# Zenity options
51ZENITY_TITLE="Power Profiles"
52ZENITY_TEXT="Set Profiles:"
53ZENITY_OPTIONS=(--column= --hide-header)
54
55#######################################################################
56# END CONFIG #
57#######################################################################
58
59# Whether to ask for user's confirmation
60enable_confirmation=false
61
62# Preferred launcher if both are available
63preferred_launcher="rofi"
64
65usage="$(basename "$0") [-h] [-c] [-p name] -- display a menu for shutdown, reboot, lock etc.
66
67where:
68 -h show this help text
69 -c ask for user confirmation
70 -p preferred launcher (rofi or zenity)
71
72This script depends on:
73 - systemd,
74 - i3,
75 - rofi or zenity."
76
77# Check whether the user-defined launcher is valid
78launcher_list=(rofi zenity)
79function check_launcher() {
80 if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
81 echo "Supported launchers: ${launcher_list[*]}"
82 exit 1
83 else
84 # Get array with unique elements and preferred launcher first
85 # Note: uniq expects a sorted list, so we cannot use it
86 i=1
87 launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \
88 | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' '))
89 fi
90}
91
92# Parse CLI arguments
93while getopts "hcp:" option; do
94 case "${option}" in
95 h) echo "${usage}"
96 exit 0
97 ;;
98 c) enable_confirmation=true
99 ;;
100 p) preferred_launcher="${OPTARG}"
101 check_launcher "${preferred_launcher}"
102 ;;
103 *) exit 1
104 ;;
105 esac
106done
107
108# Check whether a command exists
109function command_exists() {
110 command -v "$1" &> /dev/null 2>&1
111}
112
113# systemctl required
114if ! command_exists systemctl ; then
115 exit 1
116fi
117
118# menu defined as an associative array
119typeset -A menu
120
121# Menu with keys/commands
122
123menu=(
124 [ Performance]="powerprofilesctl set performance"
125 [ Balanced]="powerprofilesctl set balanced"
126 [ Power Saver]="powerprofilesctl set power-saver"
127 [ Cancel]=""
128)
129
130menu_nrows=${#menu[@]}
131
132# Menu entries that may trigger a confirmation message
133menu_confirm="Shutdown Reboot Hibernate Suspend Halt Logout"
134
135launcher_exe=""
136launcher_options=""
137rofi_colors=""
138
139function prepare_launcher() {
140 if [[ "$1" == "rofi" ]]; then
141 rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \
142 -hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}")
143 launcher_exe="rofi"
144 launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \
145 "${rofi_colors}" "${ROFI_OPTIONS[@]}")
146 elif [[ "$1" == "zenity" ]]; then
147 launcher_exe="zenity"
148 launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \
149 "${ZENITY_OPTIONS[@]}")
150 fi
151}
152
153for l in "${launcher_list[@]}"; do
154 if command_exists "${l}" ; then
155 prepare_launcher "${l}"
156 break
157 fi
158done
159
160# No launcher available
161if [[ -z "${launcher_exe}" ]]; then
162 exit 1
163fi
164
165launcher=(${launcher_exe} "${launcher_options[@]}")
166selection="$(printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}")"
167
168function ask_confirmation() {
169 if [ "${launcher_exe}" == "rofi" ]; then
170 confirmed=$(echo -e "Yes\nNo" | rofi -dmenu -i -lines 2 -p "${selection}?" \
171 "${rofi_colors}" "${ROFI_OPTIONS[@]}")
172 [ "${confirmed}" == "Yes" ] && confirmed=0
173 elif [ "${launcher_exe}" == "zenity" ]; then
174 zenity --question --text "Are you sure you want to ${selection,,}?"
175 confirmed=$?
176 fi
177
178 if [ "${confirmed}" == 0 ]; then
179 i3-msg -q "exec --no-startup-id ${menu[${selection}]}"
180 fi
181}
182
183if [[ $? -eq 0 && ! -z ${selection} ]]; then
184 if [[ "${enable_confirmation}" = true && \
185 ${menu_confirm} =~ (^|[[:space:]])"${selection}"($|[[:space:]]) ]]; then
186 ask_confirmation
187 else
188 i3-msg -q "exec --no-startup-id ${menu[${selection}]}"
189 fi
190fi
Powered by cgit v1.2.3 (git 2.41.0)