You are not logged in.

#26 2019-02-25 14:19:58

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

hhh wrote:

I'm using a yad script

Yad/Zenity scripts are nice.

Vertical dialog box. Logout command is window manager agnostic:

#!/bin/sh

yad --form --class=WmanExit --width=172 --borders=5 --height=360 --undecorated --skip-taskbar --image=utilities-system-monitor --image-on-top --center \
--field="  Log Out!gnome-logout!Log Out":fbtn "pkill `wmctrl -m | awk '/Name/ {print tolower($2)}'`" \
--field="  Lock Screen!gnome-lockscreen!Lock Screen":fbtn "slock" \
--field="  Hibernate!gnome-session-hibernate!Hibernate":fbtn "systemctl hybernate" \
--field="  Suspend!gnome-session-suspend!Suspend":fbtn "systemctl suspend" \
--field="  Restart!system-reboot!Restart":fbtn "systemctl reboot" \
--field="  Shut Down!gnome-shutdown!Shut Down":fbtn "systemctl poweroff" \
--button=gtk-cancel \

You must unlearn what you have learned.
    -- yoda

Offline

#27 2019-02-26 01:27:24

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

PackRat wrote:
hhh wrote:

I'm using a yad script

Yad/Zenity scripts are nice.

Vertical dialog box. Logout command is window manager agnostic:

#!/bin/sh

yad --form --class=WmanExit --width=172 --borders=5 --height=360 --undecorated --skip-taskbar --image=utilities-system-monitor --image-on-top --center \
--field="  Log Out!gnome-logout!Log Out":fbtn "pkill `wmctrl -m | awk '/Name/ {print tolower($2)}'`" \
--field="  Lock Screen!gnome-lockscreen!Lock Screen":fbtn "slock" \
--field="  Hibernate!gnome-session-hibernate!Hibernate":fbtn "systemctl hybernate" \
--field="  Suspend!gnome-session-suspend!Suspend":fbtn "systemctl suspend" \
--field="  Restart!system-reboot!Restart":fbtn "systemctl reboot" \
--field="  Shut Down!gnome-shutdown!Shut Down":fbtn "systemctl poweroff" \
--button=gtk-cancel \

Oh, that is very nice. Any way to get some alignment happening on the icons and text?

Offline

#28 2019-02-26 01:31:03

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

I think you can if you dig into yad options a bit deeper.

I put that script together pretty quick when we were updating the VSIDO logout script.

Edit -

man yad | grep align
              Set alignment of field labels. Possible types are left, center
              Set alignment of bar labels. Possible types are left, center or
              Set alignment of bar labels. TYPE can be left, center or right.

Looks doable, set some additional options.

Last edited by PackRat (2019-02-26 01:37:35)


You must unlearn what you have learned.
    -- yoda

Offline

#29 2019-02-26 17:54:37

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

It's the icons that are messing up the alignment - at least for this type of form/button:

screen-import-Tuesday_1551203465.th.png

#!/bin/sh

yad --form --class=WmanExit --width=172 --borders=5 --height=360 --undecorated --center --skip-taskbar \
--field="  Log Out!!Log Out":fbtn "pkill `wmctrl -m | awk '/Name/ {print tolower($2)}'`" \
--field="  Lock Screen!!Lock Screen":fbtn "slock" \
--field="  Hibernate!!Hibernate":fbtn "systemctl hybernate" \
--field="  Suspend!!Suspend":fbtn "systemctl suspend" \
--field="  Restart!!Restart":fbtn "systemctl reboot" \
--field="  Shut Down!!Shut Down":fbtn "systemctl poweroff" \

--button=gtk-cancel \

The "OK" button also shows up. Don't know enough about yad to was philosophic about that.

Also, one recommendation should BL decide to go with a yad script for logout or anything else. Use the "--class=SomeClass" flag with the script. Giving the dialog window a class name allows the user to then set up per-app settings with their window manager of choice, i.e set it to float in tiling wm's.

Last edited by PackRat (2019-02-26 17:55:47)


You must unlearn what you have learned.
    -- yoda

Offline

#30 2019-02-27 00:42:44

Bearded_Blunder
Dodging A Bullet
From: Seat: seat0; vc7
Registered: 2015-09-29
Posts: 730

Re: bl-exit replacement suggestions

I like vertical that way too, however, it bugs me using sleep or hibernate when I wake the machine back up, there's the dialogue still, have to click cancel to get rid of it..

How would one fix that? Doesn't happen with plain yad --button= but btn or fbtn in a form field makes yad a bit stubborn.


Blessed is he who expecteth nothing, for he shall not be disappointed...
If there's an obscure or silly way to break it, but you don't know what.. Just ask me

Offline

#31 2019-02-27 14:55:38

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

Bearded_Blunder wrote:

it bugs me using sleep or hibernate when I wake the machine back up, there's the dialogue still, have to click cancel to get rid of it..

Quick hack, add

--close-on-unfocus

to options before defining fields.

Otherwise, the button commands will need to be defined with an exit status in the script


You must unlearn what you have learned.
    -- yoda

Offline

#32 2019-03-01 07:35:28

Bearded_Blunder
Dodging A Bullet
From: Seat: seat0; vc7
Registered: 2015-09-29
Posts: 730

Re: bl-exit replacement suggestions

This is only half serious, though functional, please take it in the semi-humorous manner it was intended.

The main point was to show with that little function and the contained one-liner which can replace almost any instance of `systemctl powerstuff` just how easy incorporating elogind support as well as systemd support is in something like this. For an individual it's obviously pointless, for a distribution covering the about 1:50 of us popcon says are using "other init" might just be justified.  The function would be easy to put into pretty much any yad based script, virtually sed & paste. Or simply repeat the one-liner as needed replacing "$1".

Difficult to get much simpler without excluding those who choose not to join the systemd clan. I suppose as a hack workaround a short shell script located at /bin/systemctl or a symlink would work on elogind systems also, this seems cleaner and less prone to unintended consequences, would be a shame to swap a default exit script with elogind support for one without.

I've given it a spin in Bunsen, GNOME, xfce, KDE, Cinnamon, and MATE where after making sure yad is there it just works, needs tweaks for LXQt and LXDE, basically xcreensaver doesn't talk to systemd, or rather doesn't listen when systemd talks fixable with xss-lock to act as intermediary, or obviously an edit to the lock command issued by the script, light-locker and others "Yust Verk!(MT)".


For systemd users:

  • Uses systemctl

  • Uses systemd's loginctl

  • Even the function for "Those other guys" ends in ctl so it must be good big_smile

  • Sings "Happy birthday Lennart Poettering every October 15th!" (Not yet implimented)

For non-systemd users:

  • Doesn't tongue (Uses elogind)

For devs:

  • Simple bash+yad easy to maintain.

  • Easy to prettify the yad, icons & such.

  • 100% drop in replacement for bl-exit, compatible CLI support + extras to make showing hibernate or hybrid-sleep trivial.

  • Added menu if invoked without arguments at a TTY.

  • DFSG compliant licence.

(Debian fought it for years but since caved.  Let's see if Bunsen are as stuffy.)

What more could you ask? Well OK eye-candy, sorry guys artistic isn't my forte, plus I've not played with yad before.

#!/usr/bin/env bash

########################################################################
##                                                                    ##
## Init and desktop agnostic "yad" based Logout/Exit dialogue.        ##
##                                                                    ##
## Written in response to the seeimingly widely held belief that      ##
## 'systemctl' is what you _have_ to use for power operations if you  ##
## have systemd, regardless that dbus calls are arguably superior.    ##
##                                                                    ##
## Since systemd users like systemd specific commands so much, here's ##
## a system exit dialogue that uses systemd's calls for every power   ##
## or session control action, while remaining compatible with         ##
## non-systemd systems.                                               ##
##                                                                    ##
## Originally written for Bunsenlabs "Lithium" by Bearded_Blunder     ##
## <init.choice@mxstuff.org> February 2019                            ##
##                                                                    ##
## Copyright © 2019 MXStuff.org <init.choice@mxstuff.org>             ##
##                                                                    ##
## This program is free software. It comes without any warranty, to   ##
## the extent permitted by applicable law. You can redistribute it    ##
## and/or modify it under the terms of the Do What The Fuck You Want  ##
## To Public License, Version 2, as published by Sam Hocevar. See     ##
## http://www.wtfpl.net/ for more details.                            ##
##                                                                    ##
########################################################################

USAGE="\nbl-exit [OPTION]\n\n\
With no arguments default yad (GUI) or text menu at a TTY\n\n\
Only one argument is read, they cannot be combined\n\
-h --help\tShow this help\n\
-k --lock\tLock session\tDoes not work for a tty session\n\
-l --logout\tLogout\t\tAlso works for tty.\n\
-s --suspend\tSuspend\tState determined by ACPI S0 S1 or S3\n\
-i --hibernate\tSuspend to Disk\tRequires enough swap to store RAM\n\
-y --hybrid\tHybrid Sleep\tACPI must support sleep state S3\n\
-b --reboot\tReboot\n\
-p --poweroff\tShut down\n\n\
GUI Options\n\
-m --simple\t\t(GUI) Yad dialogue\n\
-d --show-hibernate\t(GUI) Adds hibernate option\n\
-a --show-all\t\t(GUI) Adds hibernate and hybrid-sleep\n\
-v --vertical\t\t(GUI) Yad stacked buttons\n\
-D --vertical-hibernate (GUI) Stacked, including hibernate\n\
-A --vertical-all\t(GUI) Stacked, all available options\n"



########################################################################
#                  Compatibility function details                      #
#                                                                      #
# logoutctl() takes the following `systemctl` arguments:               #
#                                                                      #
# poweroff                                                             #
# reboot                                                               #
# suspend                                                              #
# hibernate                                                            #
# hybrid-sleep                                                         #
# ? suspend-then-hibernate                                             #
# elogind supports it (documented), undocumented for systemd. It does  #
# _something_ on my systemd systems YMMV. Not implimented in the main  #
# script owing to not being documented by systemctl --help.            #
#                                                                      #
# Other systemctl arguments are not transferred to the elogind project #
# and will break if a similar systemctl || loginctl approach is used.  #
#                                                                      #
# I've only handled loginctl lock-/terminate-session here because it   #
# gives me a chance to deal with killing yad forms in one location.    #
#                                                                      #
########################################################################

logoutctl(){
    if [[ ! -z $YAD_PID ]]; then
        kill -SIGUSR1 $YAD_PID
    fi
    case $1 in
        cancel )
            echo -e "\ncancelled"
            exit 0 ;;
        terminate-session | lock-session )
            echo  -e "\n$1"
            loginctl $1 $XDG_SESSION_ID
            exit $? ;;
        suspend | hibernate | hybrid-sleep | suspend-then-hibernate | reboot | poweroff )
            echo -e "\n$1"
            #***********************************************#
            # The systemd or elogind compatibility magic.   #
            #***********************************************#
            [[ $(command -v systemctl) ]] && systemctl "$1" || loginctl "$1"
            exit $? ;;
    esac
}
export -f logoutctl

# If a yad picker or "lock" was chosen, test we're not at a TTY.
if [[ -z $@ ]] || [[ $1 =~ (-m|--simple|-d|--show-hibernate|-a|--show-all|-v|--vertical|-D|--vertical-hibernate|-A|--vertical-all|-k|--lock)$ ]]; then
    # Probably better ways, but: "Using systemd" :P Chops out 'x11;' in most DE's 'wayland;' in gnome and 'tty;' at a TTY or with no DM
    if [[ $(loginctl session-status | grep Service: | cut -d " " -f5) = 'tty;' ]]; then
         # We're at a TTY - Are we being asked to lock a TTY?
         if [[ $1 =~ (-k|--lock)$ ]]; then
             echo -e "Error: Graphical environment needed for screen locking."
             echo -e "It can be done with the vlock package installed, using the"
             echo -e "command 'vlock'. This script does not handle that case."
             echo -e "If you have a graphical environment a possible cause of seeing"
             echo -e "this message is that no Display Manager is installed."
             exit 1
         fi
         # TTY exit menu.
         clear
         TTY_EXIT="Please choose an option "
         select option in Logout Sleep Hibernate Hybrid-Sleep Reboot Power-Off Quit-Dialogue
         do
         case $option in
             Logout)        logoutctl terminate-session;;
             Sleep)         logoutctl suspend;;
             Hibernate)     logoutctl hibernate;;
             Hybrid-Sleep)  logoutctl hybrid-sleep;;
             Reboot)        logoutctl reboot;;
             Power-Off)     logoutctl poweroff;;
             Quit-Dialogue) break;;
         esac
    done
    fi
fi

# Default setting
if [[ -z $@ ]];then
    OP_MODE="--vertical-hibernate"
    # Change above for a different system-wide default
    #
    # TODO: Potentially read ~/.config/bunsen/somefile for default,
    #       maybe add changing default with --set-default.
else
    OP_MODE=$1
fi

case $OP_MODE in
    # command line use
    -h|--help)         echo -e "$USAGE"
                           exit 0
                       ;;
    -k|--lock)         logoutctl lock-session;;
    -l|--logout)       logoutctl terminate-session;;
    -s|--suspend)      logoutctl suspend;;
    -i|--hibernate)    logoutctl hibernate;;
    -y|--hybrid)       logoutctl hybrid-sleep;;
    -b|--reboot)       logoutctl reboot;;
    -p|--poweroff)     logoutctl poweroff;;

    ####################################################################
    #            GUI sections "yad hacking" starts here                #
    ####################################################################

    # Simple yad
    -m|--simple) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                     --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                     --image-on-top --image="gtk-quit" \
                     --button="Cancel":'bash -c "logoutctl cancel"' \
                     --button="Lock":'bash -c "logoutctl lock-session"' \
                     --button="Logout":'bash -c "logoutctl terminate-session"' \
                     --button="Sleep":'bash -c "logoutctl suspend"' \
                     --button="Reboot":'bash -c "logoutctl reboot"' \
                     --button="Shut down":'bash -c "logoutctl poweroff"' \
                 ;;

    # yad with hibernate
    -d|--show-hibernate) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                             --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                             --image-on-top --image="gtk-quit" \
                             --button="Cancel":'bash -c "logoutctl cancel"' \
                             --button="Lock":'bash -c "logoutctl lock-session"' \
                             --button="Logout":'bash -c "logoutctl terminate-session"' \
                             --button="Sleep":'bash -c "logoutctl suspend"' \
                             --button="Hibernate":'bash -c "logoutctl hibernate"' \
                             --button="Reboot":'bash -c "logoutctl reboot"' \
                             --button="Shut down":'bash -c "logoutctl poweroff"' \
                         ;;

    # yad with hibernate and hybrid sleep
    -a|--show-all) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                       --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                       --image-on-top --image="gtk-quit" \
                       --button="Cancel":'bash -c "logoutctl cancel"' \
                       --button="Lock":'bash -c "logoutctl lock-session"' \
                       --button="Logout":'bash -c "logoutctl terminate-session"' \
                       --button="Sleep":'bash -c "logoutctl suspend"' \
                       --button="Hibernate":'bash -c "logoutctl hibernate"' \
                       --button="Hyb sleep":'bash -c "logoutctl hybrid-sleep"' \
                       --button="Reboot":'bash -c "logoutctl reboot"' \
                       --button="Shut down":'bash -c "logoutctl poweroff"' \
                   ;;
                   
    # VericaL GUI
    -v|--vertical) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                       --no-buttons --undecorated --center --on-top --skip-taskbar \
                       --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                       --image-on-top --image="gtk-quit" \
                       --field="Cancel":fbtn 'bash -c "logoutctl cancel"' \
                       --field="Lock":fbtn 'bash -c "logoutctl lock-session"' \
                       --field="Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="Sleep":fbtn 'bash -c "logoutctl suspend"' \
                       --field="Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                       --field="Shut Down":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

    # Vertical GUI With hibernate
    -D|--vertical-hibernate) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                                 --no-buttons --undecorated --center --on-top --skip-taskbar \
                                 --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                                 --image-on-top --image="gtk-quit" \
                                 --field="Cancel":fbtn 'bash -c "logoutctl cancel"' \
                                 --field="Lock":fbtn 'bash -c "logoutctl lock-session"' \
                                 --field="Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                                 --field="Sleep":fbtn 'bash -c "logoutctl suspend"' \
                                 --field="Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                                 --field="Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                                 --field="Shut Down":fbtn 'bash -c "logoutctl poweroff"' \
                                 | grep -v "|" &
                             exit 0
                             ;;  

    # Vertical GUI All available options
    -A|--vertical-all) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                            --no-buttons --undecorated --center --on-top --skip-taskbar \
                            --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                            --image-on-top --image="gtk-quit" \
                            --field="Cancel":fbtn 'bash -c "logoutctl cancel"' \
                            --field="Lock":fbtn 'bash -c "logoutctl lock-session"' \
                            --field="Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                            --field="Sleep":fbtn 'bash -c "logoutctl suspend"' \
                            --field="Hyb Sleep":fbtn 'bash -c "logoutctl hybrid-sleep"' \
                            --field="Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                            --field="Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                            --field="Shut Down":fbtn 'bash -c "logoutctl poweroff"' \
                            | grep -v "|" &
                       exit 0
                       ;;

    # typo land
    *)            echo -e "\nUnknown operation $1"
                  echo -e "$USAGE"
                  exit 1
                  ;;
esac

########################################################################
##                                                                    ##
## Version: 0.1~alpha-1                                               ##
##                                                                    ##
## Depends: bash,systemd | elogind,policykit-1                        ##
##                                                                    ##
## Recommends: yad,light-locker,x-display-manager                     ##
##                                                                    ##
## Note: re "Depends:" `systemctl` requires root or sudo unless       ##
##       policykit-1 is silently providing it. Debian very helpfully  ##
##       configured rootless systemd shutdown/reboot/etc. via         ##
##       policykit-1, `sudo apt purge policykit-1` and try            ##
##       `systemctl reboot` if you disbelieve.                        ##
##       You'll lose a lot of programs in the process though.         ##
##       yad not a hard depend because command line use is possible   ##
##       without it.                                                  ##
##                                                                    ##
## KNOWN ISSUES: xscreensaver, i3lock, and slock, need the xss-lock   ##
##               package installing and configuring for systemd's     ##
##               loginctl to be able to lock the session using them,  ##
##               or edits to this script.                             ##
##                                                                    ##
##               If no display manager is installed the script will   ##
##               detect the session as a TTY rather than graphical,   ##
##               in which case the TTY picker can be run in a         ##
##               terminal emulator, or simply supply command line     ##
##               arguments and run without GUI.                       ##
##                                                                    ##
## BUGS: Probably.                                                    ##
##                                                                    ##
## Customization: Copy to ~/bin Then hack away `man yad` is more use  ##
##                than any help file or manual page I could write.    ##
##                Prettification options abound.                      ##
##                                                                    ##
## Dedicated to: Debian Ecosystem Init Diversity Team                 ##
##               <debian-init-diversity@chiark.greenend.org.uk>       ##
##               May their efforts meet with every success.           ##
##                                                                    ##
########################################################################

Blocks for yad:
These could have been generated at runtime according to specified command line arguments and only had the two blocks, one each for horizontal and vertical. However, since the objection to the old script seems to be speed and overhead, I've used the inelegant method, and simply hard coded them, which cuts processing a tiny smidgen at the expense of a longer script.

I need to learn more bash, I'm a DOS batch-file writer dammit! Or was till I forgot how. Now I scroo everything up cd / in Windows & cd \ in Linux... Actually the first one works since about Win2k.  Mixing up "%VAR%" & "$VAR" doesn't though. As for my yad well you only need to fire this up!
Anyone who actually does do bash could doubtless clean up and greatly improve the script.

In deference to @PackRat and his comment:

--class=WmanExit

Duly specified.

This was only going to be a couple of dozen lines example, I got a tad carried away. ops

Minimalist edition:

#!/usr/bin/env bash

# Init agnostic exit script. Minimalist edition.
logoutctl(){
    [[ -z $YAD_PID ]] || kill -SIGUSR1 $YAD_PID
    case $1 in
        cancel ) exit 0;;
        terminate-session | lock-session ) loginctl $1 $XDG_SESSION_ID;;
        suspend | hibernate | hybrid-sleep | reboot | poweroff ) [[ $(command -v systemctl) ]] && systemctl "$1" || loginctl "$1";;
    esac
}
export -f logoutctl

yad --form --class=WmanExit --undecorated --center --on-top --skip-taskbar \
--sticky --form --columns 1 --width=200 --borders=7 --no-buttons \
--field="Cancel":fbtn 'bash -c "logoutctl cancel"' \
--field="Lock":fbtn 'bash -c "logoutctl lock-session"' \
--field="Logout":fbtn 'bash -c "logoutctl terminate-session"' \
--field="Sleep":fbtn 'bash -c "logoutctl suspend"' \
--field="Hyb Sleep":fbtn 'bash -c "logoutctl hybrid-sleep"' \
--field="Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
--field="Restart PC":fbtn 'bash -c "logoutctl reboot"' \
--field="Shut Down":fbtn 'bash -c "logoutctl poweroff"' &> /dev/null &

Last edited by Bearded_Blunder (2019-03-01 07:37:27)


Blessed is he who expecteth nothing, for he shall not be disappointed...
If there's an obscure or silly way to break it, but you don't know what.. Just ask me

Offline

#33 2019-03-01 07:51:45

damo
....moderator....
Registered: 2015-08-20
Posts: 5,057

Re: bl-exit replacement suggestions

Nice work smile

....now if only we could have keyboard accelerators as well hmm


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#34 2019-03-01 08:00:03

Bearded_Blunder
Dodging A Bullet
From: Seat: seat0; vc7
Registered: 2015-09-29
Posts: 730

Re: bl-exit replacement suggestions

I knew I'd miss something tongue


Blessed is he who expecteth nothing, for he shall not be disappointed...
If there's an obscure or silly way to break it, but you don't know what.. Just ask me

Offline

#35 2019-03-01 08:20:00

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,721
Website

Re: bl-exit replacement suggestions

Thanks!


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Offline

#36 2019-03-01 08:51:45

Bearded_Blunder
Dodging A Bullet
From: Seat: seat0; vc7
Registered: 2015-09-29
Posts: 730

Re: bl-exit replacement suggestions

damo wrote:

Nice work smile

....now if only we could have keyboard accelerators as well hmm

Your command is my wish or something  tongue  v0.2

#!/usr/bin/env bash

########################################################################
##                                                                    ##
## Init and desktop agnostic "yad" based Logout/Exit dialogue.        ##
##                                                                    ##
## Written in response to the seeimingly widely held belief that      ##
## 'systemctl' is what you _have_ to use for power operations if you  ##
## have systemd, regardless that dbus calls are arguably superior.    ##
##                                                                    ##
## Since systemd users like systemd specific commands so much, here's ##
## a system exit dialogue that uses systemd's calls for every power   ##
## or session control action, while remaining compatible with         ##
## non-systemd systems.                                               ##
##                                                                    ##
## Originally written for Bunsenlabs "Lithium" by Bearded_Blunder     ##
## <init.choice@mxstuff.org> February 2019                            ##
##                                                                    ##
## Copyright © 2019 MXStuff.org <init.choice@mxstuff.org>             ##
##                                                                    ##
## This program is free software. It comes without any warranty, to   ##
## the extent permitted by applicable law. You can redistribute it    ##
## and/or modify it under the terms of the Do What The Fuck You Want  ##
## To Public License, Version 2, as published by Sam Hocevar. See     ##
## http://www.wtfpl.net/ for more details.                            ##
##                                                                    ##
########################################################################

USAGE="\nbl-exit [OPTION]\n\n\
With no arguments default yad (GUI) or text menu at a TTY\n\n\
Only one argument is read, they cannot be combined\n\
-h --help\tShow this help\n\
-k --lock\tLock session\tDoes not work for a tty session\n\
-l --logout\tLogout\t\tAlso works for tty.\n\
-s --suspend\tSuspend\tState determined by ACPI S0 S1 or S3\n\
-i --hibernate\tSuspend to Disk\tRequires enough swap to store RAM\n\
-y --hybrid\tHybrid Sleep\tACPI must support sleep state S3\n\
-b --reboot\tReboot\n\
-p --poweroff\tShut down\n\n\
GUI Options\n\
-m --simple\t\t(GUI) Yad dialogue\n\
-d --show-hibernate\t(GUI) Adds hibernate option\n\
-a --show-all\t\t(GUI) Adds hibernate and hybrid-sleep\n\
-v --vertical\t\t(GUI) Yad stacked buttons\n\
-D --vertical-hibernate (GUI) Stacked, including hibernate\n\
-A --vertical-all\t(GUI) Stacked, all available options\n"



########################################################################
#                  Compatibility function details                      #
#                                                                      #
# logoutctl() takes the following `systemctl` arguments:               #
#                                                                      #
# poweroff                                                             #
# reboot                                                               #
# suspend                                                              #
# hibernate                                                            #
# hybrid-sleep                                                         #
# ? suspend-then-hibernate                                             #
# elogind supports it (documented), undocumented for systemd. It does  #
# _something_ on my systemd systems YMMV. Not implimented in the main  #
# script owing to not being documented by systemctl --help.            #
#                                                                      #
# Other systemctl arguments are not transferred to the elogind project #
# and will break if a similar systemctl || loginctl approach is used.  #
#                                                                      #
# I've only handled loginctl lock-/terminate-session here because it   #
# gives me a chance to deal with killing yad forms in one location.    #
#                                                                      #
########################################################################

logoutctl(){
    if [[ ! -z $YAD_PID ]]; then
        kill -SIGUSR1 $YAD_PID
    fi
    case $1 in
        cancel )
            echo -e "\ncancelled"
            exit 0 ;;
        terminate-session | lock-session )
            echo  -e "\n$1"
            loginctl $1 $XDG_SESSION_ID
            exit $? ;;
        suspend | hibernate | hybrid-sleep | suspend-then-hibernate | reboot | poweroff )
            echo -e "\n$1"
            #***********************************************#
            # The systemd or elogind compatibility magic.   #
            #***********************************************#
            [[ $(command -v systemctl) ]] && systemctl "$1" || loginctl "$1"
            exit $? ;;
    esac
}
export -f logoutctl

# If a yad picker or "lock" was chosen, test we're not at a TTY.
if [[ -z $@ ]] || [[ $1 =~ (-m|--simple|-d|--show-hibernate|-a|--show-all|-v|--vertical|-D|--vertical-hibernate|-A|--vertical-all|-k|--lock)$ ]]; then
    # Probably better ways, but: "Using systemd" :P Chops out 'x11;' in most DE's 'wayland;' in gnome and 'tty;' at a TTY or with no DM
    if [[ $(loginctl session-status | grep Service: | cut -d " " -f5) = 'tty;' ]]; then
         # We're at a TTY - Are we being asked to lock a TTY?
         if [[ $1 =~ (-k|--lock)$ ]]; then
             echo -e "Error: Graphical environment needed for screen locking."
             echo -e "It can be done with the vlock package installed, using the"
             echo -e "command 'vlock'. This script does not handle that case."
             echo -e "If you have a graphical environment a possible cause of seeing"
             echo -e "this message is that no Display Manager is installed."
             exit 1
         fi
         # TTY exit menu.
         clear
         TTY_EXIT="Please choose an option "
         select option in Logout Sleep Hibernate Hybrid-Sleep Reboot Power-Off Quit-Dialogue
         do
         case $option in
             Logout)        logoutctl terminate-session;;
             Sleep)         logoutctl suspend;;
             Hibernate)     logoutctl hibernate;;
             Hybrid-Sleep)  logoutctl hybrid-sleep;;
             Reboot)        logoutctl reboot;;
             Power-Off)     logoutctl poweroff;;
             Quit-Dialogue) break;;
         esac
    done
    fi
fi

# Default setting
if [[ -z $@ ]];then
    OP_MODE="--vertical-hibernate"
    # Change above for a different system-wide default
    #
    # TODO: Potentially read ~/.config/bunsen/somefile for default,
    #       maybe add changing default with --set-default.
else
    OP_MODE=$1
fi

case $OP_MODE in
    # command line use
    -h|--help)         echo -e "$USAGE"
                           exit 0
                       ;;
    -k|--lock)         logoutctl lock-session;;
    -l|--logout)       logoutctl terminate-session;;
    -s|--suspend)      logoutctl suspend;;
    -i|--hibernate)    logoutctl hibernate;;
    -y|--hybrid)       logoutctl hybrid-sleep;;
    -b|--reboot)       logoutctl reboot;;
    -p|--poweroff)     logoutctl poweroff;;

    ####################################################################
    #            GUI sections "yad hacking" starts here                #
    ####################################################################

    # Simple yad
    -m|--simple) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                     --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                     --image-on-top --image="gtk-quit" \
                     --button="_Cancel":'bash -c "logoutctl cancel"' \
                     --button="Loc_k":'bash -c "logoutctl lock-session"' \
                     --button="_Logout":'bash -c "logoutctl terminate-session"' \
                     --button="_Sleep":'bash -c "logoutctl suspend"' \
                     --button="_Restart PC":'bash -c "logoutctl reboot"' \
                     --button="Shu_t Down":'bash -c "logoutctl poweroff"' \
                 ;;

    # yad with hibernate
    -d|--show-hibernate) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                             --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                             --image-on-top --image="gtk-quit" \
                             --button="_Cancel":'bash -c "logoutctl cancel"' \
                             --button="Loc_k":'bash -c "logoutctl lock-session"' \
                             --button="_Logout":'bash -c "logoutctl terminate-session"' \
                             --button="_Sleep":'bash -c "logoutctl suspend"' \
                             --button="_Hibernate":'bash -c "logoutctl hibernate"' \
                             --button="_Restart PC":'bash -c "logoutctl reboot"' \
                             --button="Shu_t Down":'bash -c "logoutctl poweroff"' \
                         ;;

    # yad with hibernate and hybrid sleep
    -a|--show-all) yad --class=WmanExit --undecorated --center --on-top --skip-taskbar \
                       --sticky --text-align=right --text="$HOSTNAME:\nLogout $USER?" \
                       --image-on-top --image="gtk-quit" \
                       --button="_Cancel":'bash -c "logoutctl cancel"' \
                       --button="Loc_k":'bash -c "logoutctl lock-session"' \
                       --button="_Logout":'bash -c "logoutctl terminate-session"' \
                       --button="_Sleep":'bash -c "logoutctl suspend"' \
                       --button="H_yb Sleep":'bash -c "logoutctl hybrid-sleep"' \
                       --button="_Hibernate":'bash -c "logoutctl hibernate"' \
                       --button="_Restart PC":'bash -c "logoutctl reboot"' \
                       --button="Shu_t Down":'bash -c "logoutctl poweroff"' \
                   ;;
                   
    # VericaL GUI
    -v|--vertical) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                       --no-buttons --undecorated --center --on-top --skip-taskbar \
                       --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                       --image-on-top --image="gtk-quit" \
                       --field="_Cancel":fbtn 'bash -c "logoutctl cancel"' \
                       --field="Loc_k":fbtn 'bash -c "logoutctl lock-session"' \
                       --field="_Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="_Sleep":fbtn 'bash -c "logoutctl suspend"' \
                       --field="_Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                       --field="Shu_t Down":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

    # Vertical GUI With hibernate
    -D|--vertical-hibernate) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                                 --no-buttons --undecorated --center --on-top --skip-taskbar \
                                 --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                                 --image-on-top --image="gtk-quit" \
                                 --field="_Cancel":fbtn 'bash -c "logoutctl cancel"' \
                                 --field="Loc_k":fbtn 'bash -c "logoutctl lock-session"' \
                                 --field="_Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                                 --field="_Sleep":fbtn 'bash -c "logoutctl suspend"' \
                                 --field="_Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                                 --field="_Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                                 --field="Shu_t Down":fbtn 'bash -c "logoutctl poweroff"' \
                                 | grep -v "|" &
                             exit 0
                             ;;  

    # Vertical GUI All available options
    -A|--vertical-all) yad --form --columns 1 --width=200 --borders=7 --class=WmanExit \
                            --no-buttons --undecorated --center --on-top --skip-taskbar \
                            --sticky --text="$HOSTNAME\nLogout $USER?" --text-align=center \
                            --image-on-top --image="gtk-quit" \
                            --field="_Cancel":fbtn 'bash -c "logoutctl cancel"' \
                            --field="Loc_k":fbtn 'bash -c "logoutctl lock-session"' \
                            --field="_Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                            --field="_Sleep":fbtn 'bash -c "logoutctl suspend"' \
                            --field="H_yb Sleep":fbtn 'bash -c "logoutctl hybrid-sleep"' \
                            --field="_Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                            --field="_Restart PC":fbtn 'bash -c "logoutctl reboot"' \
                            --field="Shu_t Down":fbtn 'bash -c "logoutctl poweroff"' \
                            | grep -v "|" &
                       exit 0
                       ;;

    # typo land
    *)            echo -e "\nUnknown operation $1"
                  echo -e "$USAGE"
                  exit 1
                  ;;
esac

########################################################################
##                                                                    ##
## Version: 0.2~alpha-1                                               ##
##                                                                    ##
## Depends: bash,systemd | elogind,policykit-1                        ##
##                                                                    ##
## Recommends: yad,light-locker,x-display-manager                     ##
##                                                                    ##
## Note: re "Depends:" `systemctl` requires root or sudo unless       ##
##       policykit-1 is silently providing it. Debian very helpfully  ##
##       configured rootless systemd shutdown/reboot/etc. via         ##
##       policykit-1, `sudo apt purge policykit-1` and try            ##
##       `systemctl reboot` if you disbelieve.                        ##
##       You'll lose a lot of programs in the process though.         ##
##       yad not a hard depend because command line use is possible   ##
##       without it.                                                  ##
##                                                                    ##
## KNOWN ISSUES: xscreensaver, i3lock, and slock, need the xss-lock   ##
##               package installing and configuring for systemd's     ##
##               loginctl to be able to lock the session using them,  ##
##               or edits to this script.                             ##
##                                                                    ##
##               If no display manager is installed the script will   ##
##               detect the session as a TTY rather than graphical,   ##
##               in which case the TTY picker can be run in a         ##
##               terminal emulator, or simply supply command line     ##
##               arguments and run without GUI.                       ##
##                                                                    ##
## BUGS: Probably.                                                    ##
##                                                                    ##
## Customization: Copy to ~/bin Then hack away `man yad` is more use  ##
##                than any help file or manual page I could write.    ##
##                Prettification options abound.                      ##
##                                                                    ##
## Dedicated to: Debian Ecosystem Init Diversity Team                 ##
##               <debian-init-diversity@chiark.greenend.org.uk>       ##
##               May their efforts meet with every success.           ##
##                                                                    ##
########################################################################
#
# Version: 0.2~alpha-1 Added keyboard accelerators. 
#                      Fixed typo on one button.
#
# Version: 0.1~alpha-1 Initial script.
#

Blessed is he who expecteth nothing, for he shall not be disappointed...
If there's an obscure or silly way to break it, but you don't know what.. Just ask me

Offline

#37 2019-03-01 13:43:31

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

Nice work.


You must unlearn what you have learned.
    -- yoda

Offline

#38 2019-03-01 16:26:34

damo
....moderator....
Registered: 2015-08-20
Posts: 5,057

Re: bl-exit replacement suggestions

Excellent work - kudos smile


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#39 2019-03-01 19:14:53

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

Um, @Bearded_Blunder?

Flipping fantastic, sir!

Screenshot_2019-03-01_14-07-53.md.png

I am not a programmer, but it took me only 30 minutes to hack this together. Thank you very much!

    # Vertical Simple GUI
    -v|--vertical-simple) yad --form --columns 1 --width=200 --title "Exit" --borders=7 --class=WmanExit --window-icon=system-shutdown \
                       --no-buttons --undecorated --center --on-top \
                       --image=gnome-logout \
                       --field="_Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="_Suspend":fbtn 'bash -c "logoutctl suspend"' \
                       --field="Re_boot":fbtn 'bash -c "logoutctl reboot"' \
                       --field="_Power Off":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

-edit- Changed the Reboot accelerator to Alt-b, r has a conflict on my system.

Last edited by hhh (2019-03-01 19:21:49)

Offline

#40 2019-03-01 20:42:34

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

Oh, hell yes.

Screenshot_2019-03-01_15-39-03.th.png

    # Vertical Simple GUI
    -v|--vertical-simple) yad --form --columns 1 --width=130 --title "Exit" --borders=7 --class=WmanExit --window-icon=gnome-logout \
                       --no-buttons --undecorated --center --on-top \
                       --field="Log Out!gnome-logout!_Logout":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="Suspend!gnome-session-suspend!_Suspend":fbtn 'bash -c "logoutctl suspend"' \
                       --field="Reboot!system-reboot!Re_boot":fbtn 'bash -c "logoutctl reboot"' \
                       --field="Shut Down!gnome-shutdown!_Power Off":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

Offline

#41 2019-03-01 20:47:19

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

Aw, hell no. The icons killed my accelerators. Is there a fix?

Offline

#42 2019-03-01 21:06:59

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

hhh wrote:

Aw, hell no. The icons killed my accelerators. Is there a fix?

Never mind, found it. I'll post it after I finish and check everything.

Offline

#43 2019-03-01 21:16:03

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

Showing the Alt key pressed and mouse-hover...

Screenshot_2019-03-01_16-13-47.md.png

    -v|--vertical-simple) yad --form --columns 1 --width=130 --title "Exit" --borders=7 --class=WmanExit --window-icon=gnome-logout \
                       --no-buttons --undecorated --center --on-top \
                       --field=" _Logout!gnome-logout!":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field=" _Suspend!gnome-session-suspend!":fbtn 'bash -c "logoutctl suspend"' \
                       --field=" Re_boot!system-reboot!":fbtn 'bash -c "logoutctl reboot"' \
                       --field=" _Power Off!gnome-shutdown!":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

I'd love to see what it looks like with the form contents aligned left, but I am utterly failing. Help!

Offline

#44 2019-03-01 21:59:25

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

I'm starting to think that the form buttons can only be center justified text. I searched for a while and every example of a yad form was like that.


You must unlearn what you have learned.
    -- yoda

Offline

#45 2019-03-01 22:02:05

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

Damn. Close enough to what I envisioned, though, I can certainly live with this. It has everything you need, and it's fast, simple, pretty damn easy to customize and pretty!

@Bearded_Blunder, thanks again! This is a great contribution, and an unexpected one at that. smile

Offline

#46 2019-03-01 22:04:37

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

I'm guessing the "solution" would be to not use a form and script individual buttons instead?

Offline

#47 2019-03-01 22:52:24

damo
....moderator....
Registered: 2015-08-20
Posts: 5,057

Re: bl-exit replacement suggestions

Now we're cookin'! Reproducing the current bl-exit, with tooltips instead of text labels smile ....

exit.th.jpg

IMG_PATH="/usr/share/images/bunsen/exit/light"
BTN_CANCEL="$IMG_PATH/cancel.png"
BTN_LOGOUT="$IMG_PATH/logout.png"
BTN_SLEEP="$IMG_PATH/sleep.png"
BTN_RESTART="$IMG_PATH/reboot.png"
BTN_SHUTDOWN="$IMG_PATH/poweroff.png"
BTN_HIBERNATE="$IMG_PATH/hibernate.png"


-m|--simple) yad --form --title "Exit" --columns 6 --borders=20 --class=WmanExit --window-icon=gnome-logout \
                       --no-buttons --undecorated --center --on-top \
                       --field="!$BTN_CANCEL!Cancel":fbtn 'bash -c "logoutctl cancel"' \
                       --field="!$BTN_LOGOUT!Log out":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="!$BTN_SLEEP!Suspend":fbtn 'bash -c "logoutctl suspend"' \
                       --field="!$BTN_HIBERNATE!Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                       --field="!$BTN_RESTART!Reboot":fbtn 'bash -c "logoutctl reboot"' \
                       --field="!$BTN_SHUTDOWN!Shutdown":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#48 2019-03-01 23:00:05

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

hhh wrote:

I'm guessing the "solution" would be to not use a form and script individual buttons instead?

Yes, but that may be restricted to horizontal. Although, I suspect you can script xy offsets.


You must unlearn what you have learned.
    -- yoda

Offline

#49 2019-03-01 23:01:19

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,089

Re: bl-exit replacement suggestions

damo wrote:

Now we're cookin'! Reproducing the current bl-exit, with tooltips instead of text labels smile ....

https://cdn.scrot.moe/images/2019/03/01/exit.th.jpg

IMG_PATH="/usr/share/images/bunsen/exit/light"
BTN_CANCEL="$IMG_PATH/cancel.png"
BTN_LOGOUT="$IMG_PATH/logout.png"
BTN_SLEEP="$IMG_PATH/sleep.png"
BTN_RESTART="$IMG_PATH/reboot.png"
BTN_SHUTDOWN="$IMG_PATH/poweroff.png"
BTN_HIBERNATE="$IMG_PATH/hibernate.png"


-m|--simple) yad --form --title "Exit" --columns 6 --borders=20 --class=WmanExit --window-icon=gnome-logout \
                       --no-buttons --undecorated --center --on-top \
                       --field="!$BTN_CANCEL!Cancel":fbtn 'bash -c "logoutctl cancel"' \
                       --field="!$BTN_LOGOUT!Log out":fbtn 'bash -c "logoutctl terminate-session"' \
                       --field="!$BTN_SLEEP!Suspend":fbtn 'bash -c "logoutctl suspend"' \
                       --field="!$BTN_HIBERNATE!Hibernate":fbtn 'bash -c "logoutctl hibernate"' \
                       --field="!$BTN_RESTART!Reboot":fbtn 'bash -c "logoutctl reboot"' \
                       --field="!$BTN_SHUTDOWN!Shutdown":fbtn 'bash -c "logoutctl poweroff"' \
                       | grep -v "|" &
                   exit 0
                   ;;

Well played.


You must unlearn what you have learned.
    -- yoda

Offline

#50 2019-03-01 23:03:53

hhh
Meep!
Registered: 2015-09-17
Posts: 8,312
Website

Re: bl-exit replacement suggestions

You left out keyboard accelerators, or am I mistaken? But something like this is all that's needed, right? Not tested...

--field="!$BTN_LOGOUT!_Log out!":fbtn 'bash -c "logoutctl terminate-session"' \

Last edited by hhh (2019-03-01 23:05:57)

Offline

Board footer

Powered by FluxBB