From 9f433c5087321c22751f3d1afacf403e787c257d Mon Sep 17 00:00:00 2001 From: B Stack Date: Mon, 9 Mar 2020 23:03:09 -0400 Subject: WIP: initial work for ncurses ripped entirely from http://adamlamers.com/post/FTPD9KNRA8CT and added numeral support, 1-9. --- logout-manager-ncurses.py | 163 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100755 logout-manager-ncurses.py diff --git a/logout-manager-ncurses.py b/logout-manager-ncurses.py new file mode 100755 index 0000000..7356569 --- /dev/null +++ b/logout-manager-ncurses.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +# File: logout-manager-tui.py +# License: MIT +# Author: adamlamers, bgstack15 +# Startdate: 2020-03-09 17:06 +# Title: ncurses based logout manager +# Usage: +# +# Reference: +# https://docs.python.org/3/howto/curses.html +# ripped straight from http://adamlamers.com/post/FTPD9KNRA8CT +# https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 +# Improve: +# add "disabled" option in menu, with default=enabled +# Documentation: + +import curses, sys, os +sys.path.append("/home/bgirton/dev/logout-manager") +import lmlib + +class CursesMenu(object): + + INIT = {'type' : 'init'} + + def __init__(self, menu_options): + self.screen = curses.initscr() + self.menu_options = menu_options + self.selected_option = 0 + self._previously_selected_option = None + self.running = True + + #init curses and curses input + curses.noecho() + curses.cbreak() + curses.start_color() + curses.curs_set(0) #Hide cursor + self.screen.keypad(1) + + #set up color pair for highlighted option + curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) + self.hilite_color = curses.color_pair(1) + self.normal_color = curses.A_NORMAL + + def prompt_selection(self, parent=None): + if parent is None: + lastoption = "Cancel" + else: + lastoption = "Return to previous menu ({})".format(parent['title']) + + option_count = len(self.menu_options['options']) + + input_key = None + + ENTER_KEY = ord('\n') + #NUM_KEYS = [ord('1')] + #NUM_KEYS = [ord(1),ord(2)...] + NUM_KEYS = [ord(str(i)) for i in range(1,option_count+2)] + done = False + while not done: + if self.selected_option != self._previously_selected_option: + self._previously_selected_option = self.selected_option + + self.screen.border(0) + self._draw_title() + for option in range(option_count): + if self.selected_option == option: + self._draw_option(option, self.hilite_color) + else: + self._draw_option(option, self.normal_color) + + if self.selected_option == option_count: + self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + lastoption), self.hilite_color) + else: + self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + lastoption), self.normal_color) + + max_y, max_x = self.screen.getmaxyx() + if input_key is not None: + self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option)) + self.screen.refresh() + + + input_key = self.screen.getch() + down_keys = [curses.KEY_DOWN, ord('j')] + up_keys = [curses.KEY_UP, ord('k')] + exit_keys = [ord('q')] + + if input_key in down_keys: + if self.selected_option < option_count: + self.selected_option += 1 + else: + self.selected_option = 0 + + if input_key in up_keys: + if self.selected_option > 0: + self.selected_option -= 1 + else: + self.selected_option = option_count + + if input_key in exit_keys: + self.selected_option = option_count #auto select exit and return + break + + if input_key == ENTER_KEY or input_key in NUM_KEYS: + if input_key in NUM_KEYS: + self.selected_option=int(chr(input_key))-1 + done = True + return self.selected_option + + def _draw_option(self, option_number, style): + self.screen.addstr(5 + option_number, + 4, + "{:2} - {}".format(option_number+1, self.menu_options['options'][option_number]['title']), + style) + + def _draw_title(self): + self.screen.addstr(2, 2, self.menu_options['title'], curses.A_STANDOUT) + self.screen.addstr(4, 2, self.menu_options['subtitle'], curses.A_BOLD) + + def display(self): + selected_option = self.prompt_selection() + i, _ = self.screen.getmaxyx() + curses.endwin() + #os.system('clear') + if selected_option < len(self.menu_options['options']): + selected_opt = self.menu_options['options'][selected_option] + return selected_opt + else: + self.running = False + return {'title' : 'Cancel', 'type' : 'exitmenu'} + +menu = {'title' : 'Logout Manager', + 'type' : 'menu', + 'subtitle' : 'Use arrows or number keys'} + +option_1 = {'title' : 'Hello World', + 'type' : 'command', + 'command' : 'echo Hello World!'} + +# build menu object from config. +config = lmlib.Initialize_config() +actions = lmlib.Actions + +menu['options'] = [ + {'title': 'Lock', 'type': 'action', 'action': 'lock'}, + {'title': 'Logout', 'type': 'action', 'action': 'logout'}, + {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate'}, + {'title': 'Shutdown', 'type': 'action', 'action': 'shutdown'}, + {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} + ] + +m = CursesMenu(menu) +selected_action = m.display() + +if selected_action['type'] == 'exitmenu': + print("Cancelled") +elif selected_action['type'] == 'command': + os.system(selected_action['command']) +elif selected_action['type'] == 'action': + #a = selected_action['action']: + func = getattr(globals()['actions'],selected_action['action']) + func(config) -- cgit From ce5457600a93240c13985babfcded0e17296f3a8 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 09:34:35 -0400 Subject: add zeroindex bool I need to experiment with any submenu capabilities. It passes a parent object at some point, and also has strings about "Return to previous menu," but no example is given for passing a submenu. --- logout-manager-ncurses.py | 50 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/logout-manager-ncurses.py b/logout-manager-ncurses.py index 7356569..99801d7 100755 --- a/logout-manager-ncurses.py +++ b/logout-manager-ncurses.py @@ -13,6 +13,10 @@ # Improve: # add "disabled" option in menu, with default=enabled # Documentation: +# Improvements for CursesMenu class over origin: +# accepts number key inputs +# accepts enabled attribute +# add "zeroindex" bool import curses, sys, os sys.path.append("/home/bgirton/dev/logout-manager") @@ -28,6 +32,11 @@ class CursesMenu(object): self.selected_option = 0 self._previously_selected_option = None self.running = True + self._zero_offset = 1 + try: + self._zero_offset = 0 if bool(self.menu_options['zeroindex']) else 1 + except: + pass #init curses and curses input curses.noecho() @@ -52,9 +61,7 @@ class CursesMenu(object): input_key = None ENTER_KEY = ord('\n') - #NUM_KEYS = [ord('1')] - #NUM_KEYS = [ord(1),ord(2)...] - NUM_KEYS = [ord(str(i)) for i in range(1,option_count+2)] + NUM_KEYS = [ord(str(i)) for i in range(self._zero_offset,option_count+1+self._zero_offset)] done = False while not done: if self.selected_option != self._previously_selected_option: @@ -69,15 +76,15 @@ class CursesMenu(object): self._draw_option(option, self.normal_color) if self.selected_option == option_count: - self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, lastoption), self.hilite_color) else: - self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, lastoption), self.normal_color) max_y, max_x = self.screen.getmaxyx() if input_key is not None: - self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option)) + self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option+self._zero_offset)) self.screen.refresh() @@ -104,19 +111,28 @@ class CursesMenu(object): if input_key == ENTER_KEY or input_key in NUM_KEYS: if input_key in NUM_KEYS: - self.selected_option=int(chr(input_key))-1 + self.selected_option=int(chr(input_key))-self._zero_offset done = True + try: + done = self.menu_options['options'][self.selected_option]['enabled'] + except: + pass return self.selected_option def _draw_option(self, option_number, style): - self.screen.addstr(5 + option_number, + thistext = self.menu_options['options'][option_number]['title'] + try: + if self.menu_options['options'][option_number]['enabled'] == False: thistext += " (disabled)" + except: + pass + self.screen.addstr(4 + option_number, 4, - "{:2} - {}".format(option_number+1, self.menu_options['options'][option_number]['title']), + "{:2} - {}".format(option_number+self._zero_offset, thistext), style) def _draw_title(self): self.screen.addstr(2, 2, self.menu_options['title'], curses.A_STANDOUT) - self.screen.addstr(4, 2, self.menu_options['subtitle'], curses.A_BOLD) + self.screen.addstr(3, 2, self.menu_options['subtitle'], curses.A_BOLD) def display(self): selected_option = self.prompt_selection() @@ -130,10 +146,6 @@ class CursesMenu(object): self.running = False return {'title' : 'Cancel', 'type' : 'exitmenu'} -menu = {'title' : 'Logout Manager', - 'type' : 'menu', - 'subtitle' : 'Use arrows or number keys'} - option_1 = {'title' : 'Hello World', 'type' : 'command', 'command' : 'echo Hello World!'} @@ -142,13 +154,19 @@ option_1 = {'title' : 'Hello World', config = lmlib.Initialize_config() actions = lmlib.Actions -menu['options'] = [ +menu = { + 'title' : 'Logout Manager', + 'type' : 'menu', + 'subtitle' : 'Use arrows or number keys', + 'zeroindex' : True, + 'options' : [ {'title': 'Lock', 'type': 'action', 'action': 'lock'}, {'title': 'Logout', 'type': 'action', 'action': 'logout'}, - {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate'}, + {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate', 'enabled': config.can_hibernate}, {'title': 'Shutdown', 'type': 'action', 'action': 'shutdown'}, {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} ] +} m = CursesMenu(menu) selected_action = m.display() -- cgit From 86d9bf400c78ad535adaf0da82a31a149d8795f7 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 16:52:22 -0400 Subject: use /etc/default/logout-manager and conf files --- lmlib.py | 6 +++--- logout-manager-gtk.py | 23 ++++++++++++++++++++--- logout-manager-ncurses.py | 38 +++++++++++++++++++++++++------------- logout-manager-tcl.py | 29 ++++++++++++++++++++++------- 4 files changed, 70 insertions(+), 26 deletions(-) diff --git a/lmlib.py b/lmlib.py index fe6d4a4..7c9dc1e 100644 --- a/lmlib.py +++ b/lmlib.py @@ -15,7 +15,7 @@ import configparser, platform, os -logout_manager_version="2019-06-21a" +logout_manager_version="2020-03-10a" class Actions: @@ -192,10 +192,10 @@ def get_gtk3_default_icon_theme(): print("Found gtk3 default theme:",name) return name -def Initialize_config(): +def Initialize_config(infile): # Read config config_in = configparser.ConfigParser() - config_in.read('logout-manager.conf') + config_in.read(infile) config = Config() try: ci = config_in['logout-manager'] diff --git a/logout-manager-gtk.py b/logout-manager-gtk.py index c6f7a6b..553fc41 100755 --- a/logout-manager-gtk.py +++ b/logout-manager-gtk.py @@ -24,15 +24,31 @@ # only show debug info when DEBUG=1 or similar. # support global conf file, and user conf file # far future: provide graphical way to change commands run +# Dependencies: +# Devuan: python3-dotenv # Documentation: -import gi, sys +import gi, os, platform, sys gi.require_version("Gtk", "3.0") from gi.repository import Gtk from gi.repository import Gdk from gi.repository.GdkPixbuf import Pixbuf from pathlib import Path -sys.path.append("/home/bgirton/dev/logout-manager") +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) import lmlib # graphical classes and functions @@ -225,7 +241,8 @@ class MainWindow(Gtk.Window): print("Cancel any logout action.") Gtk.main_quit() -config = lmlib.Initialize_config() +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) actions = lmlib.Actions # MAIN LOOP diff --git a/logout-manager-ncurses.py b/logout-manager-ncurses.py index 99801d7..ab3b99b 100755 --- a/logout-manager-ncurses.py +++ b/logout-manager-ncurses.py @@ -1,25 +1,41 @@ #!/usr/bin/env python3 -# File: logout-manager-tui.py +# File: logout-manager-ncurses.py # License: MIT # Author: adamlamers, bgstack15 # Startdate: 2020-03-09 17:06 # Title: ncurses based logout manager # Usage: -# +# logout-manager-ncurses.py # Reference: # https://docs.python.org/3/howto/curses.html # ripped straight from http://adamlamers.com/post/FTPD9KNRA8CT # https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 +# https://robinislam.me/blog/reading-environment-variables-in-python/ # Improve: -# add "disabled" option in menu, with default=enabled +# Dependencies: +# Devuan: python3-dotenv # Documentation: # Improvements for CursesMenu class over origin: # accepts number key inputs # accepts enabled attribute # add "zeroindex" bool -import curses, sys, os -sys.path.append("/home/bgirton/dev/logout-manager") +import curses, os, platform, sys +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) import lmlib class CursesMenu(object): @@ -146,19 +162,16 @@ class CursesMenu(object): self.running = False return {'title' : 'Cancel', 'type' : 'exitmenu'} -option_1 = {'title' : 'Hello World', - 'type' : 'command', - 'command' : 'echo Hello World!'} - -# build menu object from config. -config = lmlib.Initialize_config() +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) actions = lmlib.Actions +# MAIN LOOP menu = { 'title' : 'Logout Manager', 'type' : 'menu', 'subtitle' : 'Use arrows or number keys', - 'zeroindex' : True, + 'zeroindex' : False, 'options' : [ {'title': 'Lock', 'type': 'action', 'action': 'lock'}, {'title': 'Logout', 'type': 'action', 'action': 'logout'}, @@ -167,7 +180,6 @@ menu = { {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} ] } - m = CursesMenu(menu) selected_action = m.display() diff --git a/logout-manager-tcl.py b/logout-manager-tcl.py index 8c4bc6a..127bd54 100755 --- a/logout-manager-tcl.py +++ b/logout-manager-tcl.py @@ -7,6 +7,7 @@ # Purpose: A tcl/tk graphical program for selecting shutdown, logout, etc. # History: # Usage: +# logout-manager-tcl.py # References: # http://effbot.org/tkinterbook/button.htm # http://effbot.org/tkinterbook/tkinter-application-windows.htm @@ -22,18 +23,17 @@ # tooltips https://stackoverflow.com/questions/3221956/how-do-i-display-tooltips-in-tkinter/41381685#41381685 # Improve: # Dependencies: -# devuan: python3-tk python3-pil.imagetk python3-cairosvg +# Devuan: python3-tk python3-pil.imagetk python3-cairosvg # el7: python36-tkinter python36-pillow-tk ( pip3 install cairosvg ) -import glob, re +import glob, os, platform, re, sys import tkinter as tk from functools import partial from pathlib import Path from sys import path +from dotenv import load_dotenv # loading PIL.ImageTk after tkinter makes ImageTk use the PIL version, which supports PNG. This is important on tcl < 8.6 (that is, el7) from PIL import Image, ImageTk -path.append("/home/bgirton/dev/logout-manager") -import lmlib LM_USE_SVG = 0 try: @@ -43,8 +43,20 @@ except: print("WARNING: Unable to import cairosvg. No svg images will be displayed.") LM_USE_SVG = 0 -config = lmlib.Initialize_config() -actions = lmlib.Actions +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib # graphical classes and functions print("Loading graphics...") @@ -388,9 +400,12 @@ class App: #def something(event=None): # print("Got here!") -root = tk.Tk() +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions # MAIN LOOP +root = tk.Tk() root.title("Log out options") imgicon = get_scaled_icon(config.get_logout_icon(),24,config.get_icon_theme()) root.tk.call('wm','iconphoto', root._w, imgicon) -- cgit From b499fd8bbf3d455cbbdf57acc4091b911d96ee22 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 17:23:25 -0400 Subject: rearrange src and add readme --- README.md | 38 ++++ lmlib.py | 286 ----------------------- logout-manager-gtk.py | 252 -------------------- logout-manager-ncurses.py | 193 ---------------- logout-manager-tcl.py | 417 ---------------------------------- logout-manager.conf | 16 -- src/etc/default/logout-manager | 2 + src/etc/logout-manager.conf | 16 ++ src/usr/bin/logout-manager-cli.py | 0 src/usr/bin/logout-manager-gtk.py | 252 ++++++++++++++++++++ src/usr/bin/logout-manager-ncurses.py | 193 ++++++++++++++++ src/usr/bin/logout-manager-tcl.py | 417 ++++++++++++++++++++++++++++++++++ src/usr/share/logout-manager/lmlib.py | 286 +++++++++++++++++++++++ 13 files changed, 1204 insertions(+), 1164 deletions(-) create mode 100644 README.md delete mode 100644 lmlib.py delete mode 100755 logout-manager-gtk.py delete mode 100755 logout-manager-ncurses.py delete mode 100755 logout-manager-tcl.py delete mode 100644 logout-manager.conf create mode 100644 src/etc/default/logout-manager create mode 100644 src/etc/logout-manager.conf create mode 100755 src/usr/bin/logout-manager-cli.py create mode 100755 src/usr/bin/logout-manager-gtk.py create mode 100755 src/usr/bin/logout-manager-ncurses.py create mode 100755 src/usr/bin/logout-manager-tcl.py create mode 100644 src/usr/share/logout-manager/lmlib.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..0716dff --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# README for logout-manager +## Introduction +Logout Manager is a python3 utility that provides a simple menu for logout-type actions. The supported actions are presented: + * Lock + * Logout + * Hibernate (if supported by hardware) + * Shutdown + * Reboot + +## Alternatives +[oblogout](https://launchpad.net/oblogout) looks really old so I did not investigate personally, but it sounds like it does the same thing I am trying to do. +`apt-cache search logout` shows [lxsession-logout](http://manpages.ubuntu.com/manpages/precise/en/man1/lxsession-logout.1.html) which was compiled, as well as does not provide configurable options for changing executed commands or icons. + +## License +[logout-manager-ncurses.py](src/usr/bin/logout-manager-ncurses.py) is licensed under the [MIT license](http://choosealicense.com/licenses/mit) and is derived almost entirely from [adamlamers](http://adamlamers.com/post/FTPD9KNRA8CT). +Everything else is licensed under [CC-BY-SA 4.0](https://choosealicense.com/licenses/cc-by-sa-4.0/). + +## Description +This project is partially a programming playground for the [original author](https://bgstack15.wordpress.com) and partially a useful project for his migration to [Fluxbox](http://fluxbox.org/) on the desktop. + +## Upsides +* This project is the first to [demonstrate SVG images in tkinter in python3](https://bgstack15.wordpress.com/2019/07/13/display-svg-in-tkinter-python3/) that I could find on the Internet. +* I have learned how to work with ncurses, gtk, and tcl in python3. +* This will make Fluxbox systems easier to use for general users. + +## Downsides +This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. + +## Improve +* add makefile + * add the standard 'list' option + * add a `list_dependencies` option or similar, which does the grep command + + grep -h -A5 -riIE dependencies * | awk 'tolower($0) ~ /devuan/ {$1="";$2="";print}' | sort | uniq | xargs + +* add logout-manager-cli.py +* add debian/ dir + * and dsc file diff --git a/lmlib.py b/lmlib.py deleted file mode 100644 index 7c9dc1e..0000000 --- a/lmlib.py +++ /dev/null @@ -1,286 +0,0 @@ -#!/usr/bin/env python3 -# File: lmlib.py -# License: CC-BY-SA 4.0 -# Author: bgstack15 -# Startdate: 2019-06-12 -# Title: Python libs for logout-manager -# Purpose: Store the common elements for operating a logout-manager -# History: -# Usage: -# In a logout-manager-gtk.py program -# Reference: -# platform info https://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python/10091465#10091465 -# Improve: -# Documentation: - -import configparser, platform, os - -logout_manager_version="2020-03-10a" - -class Actions: - - @staticmethod - def hibernate(config, event=None): - #print("need to run the /sys/power/state trick, if available") - print(config.get_hibernate_command()) - - @staticmethod - def lock(config, event=None): - #print("please lock the screen.") - print(config.get_lock_command()) - - @staticmethod - def logout(config, event=None): - #print("please log out of current session!") - print(config.get_logout_command()) - - @staticmethod - def reboot(config, event=None): - #print("please reboot.") - print(config.get_reboot_command()) - - @staticmethod - def shutdown(config, event=None): - #print("please shut yourself down!") - print(config.get_shutdown_command()) - -class Config: - def __init__(self): - # load defaults which can be overwritten - self.hibernate_command = "" - self.lock_command = "" - self.logout_command = "" - self.reboot_command = "" - self.shutdown_command = "" - self.hibernate_icon = "system-hibernate" - self.hibernate_fallback_icon = "system-hibernate" - self.lock_icon = "system-lock-screen" - self.lock_fallback_icon = "system-lock-screen" - self.logout_icon = "system-log-out" - self.logout_fallback_icon = "system-log-out" - self.reboot_icon = "system-reboot" - self.reboot_fallback_icon = "system-reboot" - self.shutdown_icon = "system-shutdown" - self.shutdown_fallback_icon = "system-shutdown" - self.icon_size = 24 - self.icon_theme = "default" - self.gtk3_default_icon_theme = "hicolor" - self.icon_category = "actions" - self.can_hibernate = False - self.application_icon=("system-log-out") - - def set_hibernate_command(self,hibernate_command): - self.hibernate_command = hibernate_command - - def set_lock_command(self,lock_command): - self.lock_command = lock_command - - def set_logout_command(self,logout_command): - self.logout_command = logout_command - - def set_reboot_command(self,reboot_command): - self.reboot_command = reboot_command - - def set_shutdown_command(self,shutdown_command): - self.shutdown_command = shutdown_command - - def set_hibernate_icon(self,hibernate_icon): - self.hibernate_icon = hibernate_icon - - def set_lock_icon(self,lock_icon): - self.lock_icon = lock_icon - - def set_logout_icon(self,logout_icon): - self.logout_icon = logout_icon - - def set_reboot_icon(self,reboot_icon): - self.reboot_icon = reboot_icon - - def set_shutdown_icon(self,shutdown_icon): - self.shutdown_icon = shutdown_icon - - def set_icon_size(self,icon_size): - self.icon_size = int(icon_size) - - def set_icon_theme(self,icon_theme): - self.icon_theme = icon_theme - - def set_gtk3_default_icon_theme(self,icon_theme): - self.gtk3_default_icon_theme= icon_theme - - def set_icon_category(self,icon_category): - self.icon_category = icon_category - - def set_can_hibernate(self,can_hibernate): - print("Setting can_hibernate:",can_hibernate) - self.can_hibernate = bool(can_hibernate) - - def get_hibernate_command(self): - return self.hibernate_command - - def get_lock_command(self): - return self.lock_command - - def get_logout_command(self): - return self.logout_command - - def get_reboot_command(self): - return self.reboot_command - - def get_shutdown_command(self): - return self.shutdown_command - - def get_hibernate_icon(self): - return self.hibernate_icon - - def get_lock_icon(self): - return self.lock_icon - - def get_logout_icon(self): - return self.logout_icon - - def get_reboot_icon(self): - return self.reboot_icon - - def get_shutdown_icon(self): - return self.shutdown_icon - - def get_hibernate_fallback_icon(self): - return self.hibernate_fallback_icon - - def get_lock_fallback_icon(self): - return self.lock_fallback_icon - - def get_logout_fallback_icon(self): - return self.logout_fallback_icon - - def get_reboot_fallback_icon(self): - return self.reboot_fallback_icon - - def get_shutdown_fallback_icon(self): - return self.shutdown_fallback_icon - - def get_icon_size(self): - return self.icon_size - - def get_icon_theme(self): - return self.icon_theme - - def get_gtk3_default_icon_theme(self): - return self.gtk3_default_icon_theme - - def get_icon_category(self): - return self.icon_category - - def get_can_hibernate(self): - return self.can_hibernate - -def get_gtk3_default_icon_theme(): - # abstracted so it does not clutter get_scaled_icon - name = "hicolor" - gtk3_config_path = os.path.join(os.path.expanduser("~"),".config","gtk-3.0","settings.ini") - gtk3_config = configparser.ConfigParser() - gtk3_config.read(gtk3_config_path) - try: - if 'Settings' in gtk3_config: - name = gtk3_config['Settings']['gtk-icon-theme-name'] - elif 'settings' in gtk3_config: - name = gtk3_config['settings']['gtk-icon-theme-name'] - except: - # supposed failsafe: keep name = hicolor - pass - print("Found gtk3 default theme:",name) - return name - -def Initialize_config(infile): - # Read config - config_in = configparser.ConfigParser() - config_in.read(infile) - config = Config() - try: - ci = config_in['logout-manager'] - except: - # no definition - print("Using default commands") - - try: - ci_icons = config_in['icons'] - except: - # no definition - print("Using default icons") - - # load up our custom class, which stores the defaults in case we do not set them here - if 'hibernate_command' in ci: - config.set_hibernate_command(ci['hibernate_command']) - if 'lock_command' in ci: - config.set_lock_command(ci['lock_command']) - if 'logout_command' in ci: - config.set_logout_command(ci['logout_command']) - if 'reboot_command' in ci: - config.set_reboot_command(ci['reboot_command']) - if 'shutdown_command' in ci: - config.set_shutdown_command(ci['shutdown_command']) - if 'hibernate' in ci_icons: - config.set_hibernate_icon(ci_icons['hibernate']) - if 'lock' in ci_icons: - config.set_lock_icon(ci_icons['lock']) - if 'logout' in ci_icons: - config.set_logout_icon(ci_icons['logout']) - if 'reboot' in ci_icons: - config.set_reboot_icon(ci_icons['reboot']) - if 'shutdown' in ci_icons: - config.set_shutdown_icon(ci_icons['shutdown']) - if 'size' in ci_icons: - config.set_icon_size(ci_icons['size']) - if 'theme' in ci_icons: - config.set_icon_theme(ci_icons['theme']) - # store the info about if hibernate is an option - can_hibernate = False - try: - with open('/sys/power/state') as r: - line = r.read() - if 'disk' in line: can_hibernate = True - except: - pass - config.set_can_hibernate(can_hibernate) - - # read gtk3_default_icon_theme - config.set_gtk3_default_icon_theme(get_gtk3_default_icon_theme()) - if config.get_icon_theme() == "default": - config.set_icon_theme(config.get_gtk3_default_icon_theme()) - - # set icon category - # written primarily for el7 which uses "app" for the system-reboot icons, etc. - a = platform.dist() - try: - if a[0] == "redhat" and int(a[1].split(".")[0]) <= 7: - config.set_icon_category("apps") - except: - pass - - # DEBUG, raw from conf file and system status - print("Raw values:") - for item in config_in.sections(): - print("["+item+"]") - for key in config_in[item]: - print(key+" = "+config_in[item][key]) - print("Can hibernate:",can_hibernate) - - # DEBUG, stored values - print("Stored values:") - print(config.get_hibernate_command()) - print(config.get_lock_command()) - print(config.get_logout_command()) - print(config.get_reboot_command()) - print(config.get_shutdown_command()) - print(config.get_hibernate_icon()) - print(config.get_lock_icon()) - print(config.get_logout_icon()) - print(config.get_reboot_icon()) - print(config.get_shutdown_icon()) - print(config.get_icon_size()) - print(config.get_icon_theme()) - print(config.get_icon_category()) - print("Can hibernate:",config.get_can_hibernate()) - - return config diff --git a/logout-manager-gtk.py b/logout-manager-gtk.py deleted file mode 100755 index 553fc41..0000000 --- a/logout-manager-gtk.py +++ /dev/null @@ -1,252 +0,0 @@ -#!/usr/bin/env python3 -# File: logout-manager-gtk.py -# License: CC-BY-SA 4.0 -# Author: bgstack15 -# Startdate: 2019-06-01 -# Title: GTK3 based logout manager -# Purpose: Primarily for fluxbox, this tool provides a graphical menu for various session control commands like shutdown, logout, and reboot -# History: -# Usage: -# This is a bit for reference, but also to provide myself a little shutdown options menu, like xfce4, because fluxbox doesn't really provide one. -# Reference: -# https://www.linuxquestions.org/questions/slackware-14/how-do-i-run-menu-and-logout-from-the-command-line-in-fluxbox-864919/ -# /mnt/public/work/python/hotplug2/ -# icon handling https://python-gtk-3-tutorial.readthedocs.io/en/latest/iconview.html -# accelerator keys https://askubuntu.com/questions/655452/python-gtk3-keyboard-accelerators -# gtk3 widget signals https://developer.gnome.org/gtk3/unstable/GtkWidget.html#GtkWidget-button-press-event -# /usr/share/wicd/gtk/gui.py netentry.py wicd.ui -# combined with next ref: scale down valid icon https://stackoverflow.com/questions/42800482/how-to-set-size-of-a-gtk-image-in-python -# https://stackoverflow.com/questions/6090241/how-can-i-get-the-full-file-path-of-an-icon-name -# use custom icon theme https://lazka.github.io/pgi-docs/Gtk-3.0/classes/IconTheme.html#Gtk.IconTheme.set_custom_theme -# https://stackoverflow.com/questions/4090804/how-can-i-pass-variables-between-two-classes-windows-in-pygtk -# Improve: -# actually execute the commands -# only show debug info when DEBUG=1 or similar. -# support global conf file, and user conf file -# far future: provide graphical way to change commands run -# Dependencies: -# Devuan: python3-dotenv -# Documentation: - -import gi, os, platform, sys -gi.require_version("Gtk", "3.0") -from gi.repository import Gtk -from gi.repository import Gdk -from gi.repository.GdkPixbuf import Pixbuf -from pathlib import Path -from dotenv import load_dotenv - -# all this to load the libpath -try: - defaultdir="/etc/sysconfig" - thisplatform = platform.platform().lower() - if 'debian' in thisplatform or 'devuan' in thisplatform: - defaultdir="/etc/default" - # load_dotenv keeps existing environment variables as higher precedent - load_dotenv(os.path.join(defaultdir,"logout-manager")) -except: - pass -if 'LOGOUT_MANAGER_LIBPATH' in os.environ: - for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): - sys.path.append(i) -import lmlib - -# graphical classes and functions -def get_scaled_icon(icon_name, size=24, fallback_icon_name = "", icon_theme = "default"): - # return a Gtk.Image.new_from_pixbuf - - # ripped from https://stackoverflow.com/questions/42800482/how-to-set-size-of-a-gtk-image-in-python and combined with https://stackoverflow.com/questions/6090241/how-can-i-get-the-full-file-path-of-an-icon-name - # further ref for lookup_icon function: https://lazka.github.io/pgi-docs/Gtk-3.0/flags.html#Gtk.IconLookupFlags - # if a file exists by the specific name, use it. - if Path(icon_name).is_file(): - iconfilename = icon_name - else: - if icon_theme != "default": - this_theme = Gtk.IconTheme.new() - this_theme.set_custom_theme(icon_theme) - else: - this_theme = Gtk.IconTheme.get_default() - try: - icon_info = this_theme.lookup_icon(icon_name, size, 0) - iconfilename = icon_info.get_filename() - except: - try: - icon_info = this_theme.lookup_icon(fallback_icon_name, size, 0) - iconfilename = icon_info.get_filename() - except: - # no icon in the current theme. Try a hard-coded fallback: - try: - # if debuglev 3 - print("Error: could not find default icon for", icon_name+", so using fallback.") - this_theme = Gtk.IconTheme.new() - this_theme.set_custom_theme("Numix-Circle") - icon_info = this_theme.lookup_icon(icon_name, size, 0) - iconfilename = icon_info.get_filename() - except: - print("Error: Could not find any icon for", icon_name) - return None - #print(iconfilename) - return Gtk.Image.new_from_pixbuf(Pixbuf.new_from_file_at_scale( - filename=iconfilename, - width=size, height=size, preserve_aspect_ratio=True)) - -class MainWindow(Gtk.Window): - def __init__(self, config, actions): - self.actions = actions - self.config = config - Gtk.Window.__init__(self, title="Log out options") - # for window icon - liststore = Gtk.ListStore(Pixbuf, str) - iconview = Gtk.IconView.new() - iconview.set_model(liststore) - iconview.set_pixbuf_column(0) - iconview.set_text_column(1) - pixbuf24 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 24, 0) - pixbuf32 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 32, 0) - pixbuf48 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 48, 0) - pixbuf64 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 64, 0) - pixbuf96 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 96, 0) - self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96]); - - # accel is for when you are not using the "set_use_underline" function. - #accel = Gtk.AccelGroup() - #accel.connect(Gdk.keyval_from_name('D'), Gdk.ModifierType.MOD1_MASK, 0, self.on_button2_accel) - #self.add_accel_group(accel) - - # buttons - self.grid = Gtk.Grid() - self.add(self.grid) - - self.button0 = Gtk.Button(label="Loc_k") - self.button0.connect("button-press-event", self.on_button0_press_event) - self.button0.connect("activate", self.on_button0_press_event) # activate covers ALT+L action and spacebar when selected - self.buttonicon0 = get_scaled_icon(config.get_lock_icon(), config.get_icon_size(), config.get_lock_fallback_icon(), config.get_icon_theme()) - self.button0.set_image(self.buttonicon0) - self.button0.set_tooltip_text("Hide session and require authentication to return to it") - self.button0.set_always_show_image(True) - self.button0.set_use_underline(True) - self.grid.add(self.button0) - - self.button1 = Gtk.Button(label="_Logout") - self.button1.connect("button-press-event", self.on_button1_press_event) - self.button1.connect("activate", self.on_button1_press_event) # activate covers ALT+L action and spacebar when selected - self.buttonicon1 = get_scaled_icon(config.get_logout_icon(), config.get_icon_size(), config.get_logout_fallback_icon(), config.get_icon_theme()) - self.button1.set_image(self.buttonicon1) - self.button1.set_tooltip_text("Close the current user session") - self.button1.set_always_show_image(True) - self.button1.set_use_underline(True) - self.grid.add(self.button1) - - self.buttonHibernate = Gtk.Button(label="_Hibernate") - self.buttonHibernate.connect("button-press-event", self.on_buttonHibernate_press_event) - self.buttonHibernate.connect("activate", self.on_buttonHibernate_press_event) # activate covers ALT+L action and spacebar when selected - #self.buttoniconHibernate = Gtk.Image() - #self.buttoniconHibernate.set_from_icon_name("system-hibernate", 24) - self.buttoniconHibernate = get_scaled_icon(config.get_hibernate_icon(), config.get_icon_size(), config.get_hibernate_fallback_icon(), config.get_icon_theme()) - self.buttonHibernate.set_image(self.buttoniconHibernate) - self.buttonHibernate.set_tooltip_text("Save state to disk and power off") - self.buttonHibernate.set_always_show_image(True) - self.buttonHibernate.set_use_underline(True) - self.buttonHibernate.set_sensitive(True if config.get_can_hibernate() else False) - self.grid.add(self.buttonHibernate) - - self.button2 = Gtk.Button(label="_Shutdown") - self.button2.connect("button-press-event", self.on_button2_press_event) - self.button2.connect("activate", self.on_button2_accel) - # unnecessary because the "activate" suffices above. - #self.button2.connect("mnemonic-activate", self.on_button2_accel) - self.buttonicon2 = Gtk.Image() - self.buttonicon2 = get_scaled_icon(config.get_shutdown_icon(), config.get_icon_size(), config.get_shutdown_fallback_icon(), config.get_icon_theme()) - self.button2.set_image(self.buttonicon2) - self.button2.set_tooltip_text("Power off the computer") - self.button2.set_always_show_image(True) - self.button2.set_use_underline(True) - self.grid.add(self.button2) - - self.button3 = Gtk.Button(label="_Reboot") - self.button3.connect("button-press-event", self.on_button3_press_event) - self.button3.connect("activate", self.on_button3_press_event) - self.buttonicon3 = Gtk.Image() - self.buttonicon3 = get_scaled_icon(config.get_reboot_icon(), config.get_icon_size(), config.get_reboot_fallback_icon(), config.get_icon_theme()) - self.button3.set_image(self.buttonicon3) - self.button3.set_tooltip_text("Reboot the computer back to the login screen") - self.button3.set_always_show_image(True) - self.button3.set_use_underline(True) - self.grid.add(self.button3) - - self.button4 = Gtk.Button(label="_Cancel") - self.button4.connect("button-press-event", self.on_button4_press_event) - self.button4.connect("activate", self.on_button4_press_event) - self.button4.set_tooltip_text("Do nothing; just close this window") - self.button4.set_use_underline(True) - self.grid.attach(self.button4, 0, 1, 8, 1) - - # hibernate button - def on_buttonHibernate_press_event(self, *args): - self.do_hibernate(self.buttonHibernate) - - # lock button - def on_button0_press_event(self, *args): - self.do_lock(self.button0) - - # logout button - def on_button1_press_event(self, *args): - self.do_logout(self.button1) - - # shutdown button - def on_button2_press_event(self, widget, event): - # check if left or right click - if event.type == Gdk.EventType.BUTTON_PRESS: - if event.button == 1: - self.do_shutdown(widget) - # eventbutton == 3 is the right-click, and its reference is my hello3.py - #elif event.button == 3: - # self.on_button1_right_clicked(widget) - - # global accelerator key, when not using the set_use_underline function - ## shutdown button from accelerator key - #def on_button2_accel(self, *args): - # self.do_shutdown(self.button2) - - # accelerator key from set_use_underline function - # shutdown button from accelerator key - def on_button2_accel(self, *args): - self.do_shutdown(self.button2) - - # reboot button - def on_button3_press_event(self, *args): - self.do_reboot(self.button3) - - # cancel button - def on_button4_press_event(self, *args): - self.cancel(self.button4) - - def do_shutdown(self, *args): - #print(dir(self.props)) - self.actions.shutdown(self.config) - - def do_hibernate(self, widget): - self.actions.hibernate(self.config) - - def do_lock(self, widget): - self.actions.lock(self.config) - - def do_logout(self, widget): - self.actions.logout(self.config) - - def do_reboot(self, widget): - self.actions.reboot(self.config) - - def cancel(self, widget): - print("Cancel any logout action.") - Gtk.main_quit() - -# load configs -config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) -actions = lmlib.Actions - -# MAIN LOOP -win = MainWindow(config, actions) -win.connect("destroy", Gtk.main_quit) -win.show_all() -Gtk.main() diff --git a/logout-manager-ncurses.py b/logout-manager-ncurses.py deleted file mode 100755 index ab3b99b..0000000 --- a/logout-manager-ncurses.py +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env python3 -# File: logout-manager-ncurses.py -# License: MIT -# Author: adamlamers, bgstack15 -# Startdate: 2020-03-09 17:06 -# Title: ncurses based logout manager -# Usage: -# logout-manager-ncurses.py -# Reference: -# https://docs.python.org/3/howto/curses.html -# ripped straight from http://adamlamers.com/post/FTPD9KNRA8CT -# https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 -# https://robinislam.me/blog/reading-environment-variables-in-python/ -# Improve: -# Dependencies: -# Devuan: python3-dotenv -# Documentation: -# Improvements for CursesMenu class over origin: -# accepts number key inputs -# accepts enabled attribute -# add "zeroindex" bool - -import curses, os, platform, sys -from dotenv import load_dotenv - -# all this to load the libpath -try: - defaultdir="/etc/sysconfig" - thisplatform = platform.platform().lower() - if 'debian' in thisplatform or 'devuan' in thisplatform: - defaultdir="/etc/default" - # load_dotenv keeps existing environment variables as higher precedent - load_dotenv(os.path.join(defaultdir,"logout-manager")) -except: - pass -if 'LOGOUT_MANAGER_LIBPATH' in os.environ: - for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): - sys.path.append(i) -import lmlib - -class CursesMenu(object): - - INIT = {'type' : 'init'} - - def __init__(self, menu_options): - self.screen = curses.initscr() - self.menu_options = menu_options - self.selected_option = 0 - self._previously_selected_option = None - self.running = True - self._zero_offset = 1 - try: - self._zero_offset = 0 if bool(self.menu_options['zeroindex']) else 1 - except: - pass - - #init curses and curses input - curses.noecho() - curses.cbreak() - curses.start_color() - curses.curs_set(0) #Hide cursor - self.screen.keypad(1) - - #set up color pair for highlighted option - curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) - self.hilite_color = curses.color_pair(1) - self.normal_color = curses.A_NORMAL - - def prompt_selection(self, parent=None): - if parent is None: - lastoption = "Cancel" - else: - lastoption = "Return to previous menu ({})".format(parent['title']) - - option_count = len(self.menu_options['options']) - - input_key = None - - ENTER_KEY = ord('\n') - NUM_KEYS = [ord(str(i)) for i in range(self._zero_offset,option_count+1+self._zero_offset)] - done = False - while not done: - if self.selected_option != self._previously_selected_option: - self._previously_selected_option = self.selected_option - - self.screen.border(0) - self._draw_title() - for option in range(option_count): - if self.selected_option == option: - self._draw_option(option, self.hilite_color) - else: - self._draw_option(option, self.normal_color) - - if self.selected_option == option_count: - self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, - lastoption), self.hilite_color) - else: - self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, - lastoption), self.normal_color) - - max_y, max_x = self.screen.getmaxyx() - if input_key is not None: - self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option+self._zero_offset)) - self.screen.refresh() - - - input_key = self.screen.getch() - down_keys = [curses.KEY_DOWN, ord('j')] - up_keys = [curses.KEY_UP, ord('k')] - exit_keys = [ord('q')] - - if input_key in down_keys: - if self.selected_option < option_count: - self.selected_option += 1 - else: - self.selected_option = 0 - - if input_key in up_keys: - if self.selected_option > 0: - self.selected_option -= 1 - else: - self.selected_option = option_count - - if input_key in exit_keys: - self.selected_option = option_count #auto select exit and return - break - - if input_key == ENTER_KEY or input_key in NUM_KEYS: - if input_key in NUM_KEYS: - self.selected_option=int(chr(input_key))-self._zero_offset - done = True - try: - done = self.menu_options['options'][self.selected_option]['enabled'] - except: - pass - return self.selected_option - - def _draw_option(self, option_number, style): - thistext = self.menu_options['options'][option_number]['title'] - try: - if self.menu_options['options'][option_number]['enabled'] == False: thistext += " (disabled)" - except: - pass - self.screen.addstr(4 + option_number, - 4, - "{:2} - {}".format(option_number+self._zero_offset, thistext), - style) - - def _draw_title(self): - self.screen.addstr(2, 2, self.menu_options['title'], curses.A_STANDOUT) - self.screen.addstr(3, 2, self.menu_options['subtitle'], curses.A_BOLD) - - def display(self): - selected_option = self.prompt_selection() - i, _ = self.screen.getmaxyx() - curses.endwin() - #os.system('clear') - if selected_option < len(self.menu_options['options']): - selected_opt = self.menu_options['options'][selected_option] - return selected_opt - else: - self.running = False - return {'title' : 'Cancel', 'type' : 'exitmenu'} - -# load configs -config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) -actions = lmlib.Actions - -# MAIN LOOP -menu = { - 'title' : 'Logout Manager', - 'type' : 'menu', - 'subtitle' : 'Use arrows or number keys', - 'zeroindex' : False, - 'options' : [ - {'title': 'Lock', 'type': 'action', 'action': 'lock'}, - {'title': 'Logout', 'type': 'action', 'action': 'logout'}, - {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate', 'enabled': config.can_hibernate}, - {'title': 'Shutdown', 'type': 'action', 'action': 'shutdown'}, - {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} - ] -} -m = CursesMenu(menu) -selected_action = m.display() - -if selected_action['type'] == 'exitmenu': - print("Cancelled") -elif selected_action['type'] == 'command': - os.system(selected_action['command']) -elif selected_action['type'] == 'action': - #a = selected_action['action']: - func = getattr(globals()['actions'],selected_action['action']) - func(config) diff --git a/logout-manager-tcl.py b/logout-manager-tcl.py deleted file mode 100755 index 127bd54..0000000 --- a/logout-manager-tcl.py +++ /dev/null @@ -1,417 +0,0 @@ -#!/usr/bin/env python3 -# File: logout-manager-tcl.py -# License: CC-BY-SA 4.0 -# Author: bgstack15 -# Startdate: 2019-06-12 20:05 -# Title: Tcl/tk-based logout manager -# Purpose: A tcl/tk graphical program for selecting shutdown, logout, etc. -# History: -# Usage: -# logout-manager-tcl.py -# References: -# http://effbot.org/tkinterbook/button.htm -# http://effbot.org/tkinterbook/tkinter-application-windows.htm -# http://effbot.org/tkinterbook/ -# pass parameters to function of tkinter.Button(command=) https://stackoverflow.com/questions/38749620/python-3-tkinter-button-commands#38750155 -# alternate for passing params https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter -# https://stackoverflow.com/questions/18537918/set-window-icon#18538416 -# the exact syntax for master.bind https://stackoverflow.com/questions/16082243/how-to-bind-ctrl-in-python-tkinter -# https://pillow.readthedocs.io/en/stable/reference/ImageTk.html -# gtk-3.0 default icon theme https://coderwall.com/p/no3qfa/setting-gtk2-and-gtk3-theme-via-config-file -# homedir https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python -# natural sort https://stackoverflow.com/questions/46228101/sort-list-of-strings-by-two-substrings-using-lambda-function/46228199#46228199 -# tooltips https://stackoverflow.com/questions/3221956/how-do-i-display-tooltips-in-tkinter/41381685#41381685 -# Improve: -# Dependencies: -# Devuan: python3-tk python3-pil.imagetk python3-cairosvg -# el7: python36-tkinter python36-pillow-tk ( pip3 install cairosvg ) - -import glob, os, platform, re, sys -import tkinter as tk -from functools import partial -from pathlib import Path -from sys import path -from dotenv import load_dotenv -# loading PIL.ImageTk after tkinter makes ImageTk use the PIL version, which supports PNG. This is important on tcl < 8.6 (that is, el7) -from PIL import Image, ImageTk - -LM_USE_SVG = 0 -try: - from cairosvg import svg2png - LM_USE_SVG = 1 -except: - print("WARNING: Unable to import cairosvg. No svg images will be displayed.") - LM_USE_SVG = 0 - -# all this to load the libpath -try: - defaultdir="/etc/sysconfig" - thisplatform = platform.platform().lower() - if 'debian' in thisplatform or 'devuan' in thisplatform: - defaultdir="/etc/default" - # load_dotenv keeps existing environment variables as higher precedent - load_dotenv(os.path.join(defaultdir,"logout-manager")) -except: - pass -if 'LOGOUT_MANAGER_LIBPATH' in os.environ: - for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): - sys.path.append(i) -import lmlib - -# graphical classes and functions -print("Loading graphics...") - -class Tooltip: - - ''' - It creates a tooltip for a given widget as the mouse goes on it. - - see: - - http://stackoverflow.com/questions/3221956/ - what-is-the-simplest-way-to-make-tooltips- - in-tkinter/36221216#36221216 - - http://www.daniweb.com/programming/software-development/ - code/484591/a-tooltip-class-for-tkinter - - - Originally written by vegaseat on 2014.09.09. - - - Modified to include a delay time by Victor Zaccardo on 2016.03.25. - - - Modified - - to correct extreme right and extreme bottom behavior, - - to stay inside the screen whenever the tooltip might go out on - the top but still the screen is higher than the tooltip, - - to use the more flexible mouse positioning, - - to add customizable background color, padding, waittime and - wraplength on creation - by Alberto Vassena on 2016.11.05. - - Tested on Ubuntu 16.04/16.10, running Python 3.5.2 - - TODO: themes styles support - ''' - - def __init__(self, widget, - *, - bg='#FFFFEA', - pad=(5, 3, 5, 3), - text='widget info', - waittime=400, - wraplength=250): - - self.waittime = waittime # in miliseconds, originally 500 - self.wraplength = wraplength # in pixels, originally 180 - self.widget = widget - self.text = text - self.widget.bind("", self.onEnter) - self.widget.bind("", self.onLeave) - self.widget.bind("", self.onLeave) - self.bg = bg - self.pad = pad - self.id = None - self.tw = None - - def onEnter(self, event=None): - self.schedule() - - def onLeave(self, event=None): - self.unschedule() - self.hide() - - def schedule(self): - self.unschedule() - self.id = self.widget.after(self.waittime, self.show) - - def unschedule(self): - id_ = self.id - self.id = None - if id_: - self.widget.after_cancel(id_) - - def show(self): - def tip_pos_calculator(widget, label, - *, - tip_delta=(10, 5), pad=(5, 3, 5, 3)): - - w = widget - - s_width, s_height = w.winfo_screenwidth(), w.winfo_screenheight() - - width, height = (pad[0] + label.winfo_reqwidth() + pad[2], - pad[1] + label.winfo_reqheight() + pad[3]) - - mouse_x, mouse_y = w.winfo_pointerxy() - - x1, y1 = mouse_x + tip_delta[0], mouse_y + tip_delta[1] - x2, y2 = x1 + width, y1 + height - - x_delta = x2 - s_width - if x_delta < 0: - x_delta = 0 - y_delta = y2 - s_height - if y_delta < 0: - y_delta = 0 - - offscreen = (x_delta, y_delta) != (0, 0) - - if offscreen: - - if x_delta: - x1 = mouse_x - tip_delta[0] - width - - if y_delta: - y1 = mouse_y - tip_delta[1] - height - - offscreen_again = y1 < 0 # out on the top - - if offscreen_again: - # No further checks will be done. - - # TIP: - # A further mod might automagically augment the - # wraplength when the tooltip is too high to be - # kept inside the screen. - y1 = 0 - - return x1, y1 - - bg = self.bg - pad = self.pad - widget = self.widget - - # creates a toplevel window - self.tw = tk.Toplevel(widget) - - # Leaves only the label and removes the app window - self.tw.wm_overrideredirect(True) - - win = tk.Frame(self.tw, - background=bg, - borderwidth=0) - label = tk.Label(win, - text=self.text, - justify=tk.LEFT, - background=bg, - relief=tk.SOLID, - borderwidth=0, - wraplength=self.wraplength) - - label.grid(padx=(pad[0], pad[2]), - pady=(pad[1], pad[3]), - sticky=tk.NSEW) - win.grid() - - x, y = tip_pos_calculator(widget, label) - - self.tw.wm_geometry("+%d+%d" % (x, y)) - - def hide(self): - tw = self.tw - if tw: - tw.destroy() - self.tw = None - -def tryint(s): - try: - return int(s) - except: - return s - -def sort_sizes(x): - # Original reference so#46228101 - value = x.split("/")[5] - return mynum(value, "all") - -def mynum(x, type = "all"): - # return the complicated numerical value for the weird size options - f = re.split("[^0-9]+",x) - try: - f0 = int(f[0]) - except: - f0 = 0 - try: - f1 = int(f[1]) - except: - f1 = 0 - if type == "all": - return f0 * 100 + f1 if len(f) >= 2 else 0 - else: - return f0 - -def find_best_size_match(size, thelist): - # return item from sorted thelist whose split("/")[5] is the first to meet or exceed the requested size - try: - default = thelist[-1] - except: - default = None - return next(( i for i in thelist if mynum(i.split("/")[5],"real") >= size ), default) - -def get_filename_of_icon(name, theme = "hicolor", size = 48, category = "actions"): - # poor man's attempt at walking through fd.o icon theme - filename = None - # example: Adwaita system-log-out - - if theme == "default" or theme is None: - try: - theme = lmlib.get_gtk3_default_icon_theme() - except: - theme = "hicolor" - - # first, find all files underneath /usr/share/icons/$THEME/$SIZE - print("Finding filename of icon, theme=",theme,"category=",category,"name=",name) - # to exclude the scalable/ contents, replace dir 5 asterisk with [0-9]* - results = [] - base_dir="/usr/share/icons/" - file_filters = ".*" - if LM_USE_SVG == 0: - file_filters = ".{png,PNG}" - # I have no idea if this is xdg icon theme compliant, but it is a valiant attempt. - # 1. try (requested) req-theme, req-category, req-name first - results = glob.glob(base_dir+theme+"/*/"+category+"/"+name+file_filters) - # 2. try req-theme, (generic) gen-category, req-name - if len(results) == 0: - # no results with that category, so try all categories - results = glob.glob(base_dir+theme+"/*/*/"+name+file_filters) - # 3. try "gnome", req-category, req-name - if len(results) == 0: - results = glob.glob(base_dir+"gnome"+"/*/"+category+"/"+name+file_filters) - # 4. try "gnome", gen-category, req-name - if len(results) == 0: - results = glob.glob(base_dir+"gnome"+"/*/*/"+name+file_filters) - # 5. try "hicolor", req-category, req-name - if len(results) == 0: - results = glob.glob(base_dir+"hicolor"+"/*/"+category+"/"+name+file_filters) - # 6. try "hicolor", gen-category, req-name - if len(results) == 0: - results = glob.glob(base_dir+"hicolor"+"/*/*/"+name+file_filters) - - # the sort arranges it so a Numix/24 dir comes before a Numix/24@2x dir - results = sorted(results, key=sort_sizes) - #print(results) - # now find the first one that matches - filename = find_best_size_match(size,results) - return filename - -def photoimage_from_svg(filename = "",size = "48"): - # this one works, but does not allow me to set the size. - # this is kept as an example of how to open a svg without saving to a file. - # open svg - item = svg2png(url=filename, parent_width = size, parent_height = size) - return ImageTk.PhotoImage(data=item) - -def empty_photoimage(size=24): - photo = Image.new("RGBA",[size,size]) - return ImageTk.PhotoImage(image=photo) - -def image_from_svg(filename = "",size = "48"): - # open svg - if LM_USE_SVG == 1: - svg2png(url=filename,write_to="/tmp/lm_temp_image.png",parent_width = size,parent_height = size) - photo = Image.open("/tmp/lm_temp_image.png") - else: - photo = Image.new("RGBA",[size,size]) - return photo - -def get_scaled_icon(icon_name, size = 24, icon_theme = "default", fallback_icon_name = ""): - iconfilename = None - - # if name is a specific filename, just use it. - if Path(icon_name).is_file(): - #print("This is a file:",icon_name) - iconfilename = icon_name - else: - - if icon_theme == "default": - # this should not happen, because the Initialize_config should have checked gtk3 default value. - icon_theme = "hicolor" - # so now that icon_theme is defined, let us go find the icon that matches the requested name and size, in the actions category - #print("Using icon theme",icon_theme) - iconfilename = get_filename_of_icon(name=icon_name, theme=icon_theme, size=size, category=config.get_icon_category()) - - # So now we think we have derived the correct filename - try: - print("Trying icon file",iconfilename) - # try an svg - if re.compile(".*\.svg").match(iconfilename): - print("Trying svg...") - photo = image_from_svg(filename=iconfilename, size=size) - else: - photo = Image.open(iconfilename) - except Exception as f: - print("Error with icon file.") - print(f) - return empty_photoimage() - photo.thumbnail(size=[size, size]) - try: - photo = ImageTk.PhotoImage(photo) - except Exception as e: - print("Error was ",e) - # If I ever add HiDPI support, multiple size here by the factor. So, size * 1.25 - return photo - -class App: - def __init__(self, master): - frame = tk.Frame(master) - frame.grid(row=0) - - self.photoLock = get_scaled_icon(config.get_lock_icon(), config.get_icon_size(), config.get_icon_theme()) - self.buttonLock = tk.Button(frame, text="Lock", underline=3, command=partial(actions.lock,config), image=self.photoLock, compound=tk.LEFT) - master.bind_all("", partial(actions.lock,config)) - Tooltip(self.buttonLock, text="Hide session and require authentication to return to it") - self.buttonLock.grid(row=0,column=0) - - self.photoLogout = get_scaled_icon(config.get_logout_icon(), config.get_icon_size(), config.get_icon_theme()) - self.buttonLogout = tk.Button(frame, text="Logout", underline=0, command=lambda: actions.logout(config), image=self.photoLogout, compound=tk.LEFT) - master.bind_all("", partial(actions.logout,config)) - Tooltip(self.buttonLogout, text="Close the current user session") - self.buttonLogout.grid(row=0,column=1) - - self.photoHibernate = get_scaled_icon(config.get_hibernate_icon(), config.get_icon_size(), config.get_icon_theme()) - self.buttonHibernate = tk.Button(frame, text="Hibernate", underline=0, command=lambda: actions.hibernate(config), image=self.photoHibernate, compound=tk.LEFT) - master.bind_all("", partial(actions.hibernate,config)) - Tooltip(self.buttonHibernate, text="Save state to disk and power off") - self.buttonHibernate.grid(row=0,column=2) - - self.photoShutdown = get_scaled_icon(config.get_shutdown_icon(), config.get_icon_size(), config.get_icon_theme()) - self.buttonShutdown = tk.Button(frame, text="Shutdown", underline=0, command=lambda: actions.shutdown(config), image=self.photoShutdown, compound=tk.LEFT) - master.bind_all("", partial(actions.shutdown,config)) - Tooltip(self.buttonShutdown, text="Power off the computer") - self.buttonShutdown.grid(row=0,column=3) - - self.photoReboot = get_scaled_icon(config.get_reboot_icon(), config.get_icon_size(), config.get_icon_theme()) - self.buttonReboot = tk.Button(frame, text="Reboot", underline=0, command=lambda: actions.reboot(config), image=self.photoReboot, compound=tk.LEFT) - master.bind_all("", partial(actions.reboot,config)) - Tooltip(self.buttonReboot, text="Reboot the computer back to the login screen") - self.buttonReboot.grid(row=0,column=4) - - self.buttonCancel = tk.Button(frame, text="Cancel", underline=0, command=self.quitaction) - master.bind_all("", self.quitaction) - Tooltip(self.buttonCancel, text="Do nothing; just close this window") - self.buttonCancel.grid(row=1,columnspan=8,sticky=tk.W+tk.E) - - # Found this after trial and error. - def quitaction(self,b=None): - print("Cancel any logout action.") - root.destroy() - -# Left here as an example for a mster.bind_all that works. -#def something(event=None): -# print("Got here!") - -# load configs -config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) -actions = lmlib.Actions - -# MAIN LOOP -root = tk.Tk() -root.title("Log out options") -imgicon = get_scaled_icon(config.get_logout_icon(),24,config.get_icon_theme()) -root.tk.call('wm','iconphoto', root._w, imgicon) -app = App(root) -root.mainloop() -try: - root.destroy() -except: - pass diff --git a/logout-manager.conf b/logout-manager.conf deleted file mode 100644 index 1a14909..0000000 --- a/logout-manager.conf +++ /dev/null @@ -1,16 +0,0 @@ -[logout-manager] -hibernate_command="printf 'disk' | sudo tee /sys/power/state" -lock_command="xscreensaver --locknow" -logout_command="logout from something" -reboot_command="sudo shutdown -r now" -shutdown_command="sudo shutdown -h now" - -[icons] -size = 24 -#theme = default -# use names as used by the icon theme, or give a full path here -hibernate = system-hibernate -lock = system-lock-screen -logout = system-log-out -reboot = system-reboot -shutdown = system-shutdown diff --git a/src/etc/default/logout-manager b/src/etc/default/logout-manager new file mode 100644 index 0000000..1f4ecf6 --- /dev/null +++ b/src/etc/default/logout-manager @@ -0,0 +1,2 @@ +LOGOUT_MANAGER_LIBPATH=/usr/share/logout-manager +LOGOUT_MANAGER_CONF=/etc/logout-manager.conf diff --git a/src/etc/logout-manager.conf b/src/etc/logout-manager.conf new file mode 100644 index 0000000..1a14909 --- /dev/null +++ b/src/etc/logout-manager.conf @@ -0,0 +1,16 @@ +[logout-manager] +hibernate_command="printf 'disk' | sudo tee /sys/power/state" +lock_command="xscreensaver --locknow" +logout_command="logout from something" +reboot_command="sudo shutdown -r now" +shutdown_command="sudo shutdown -h now" + +[icons] +size = 24 +#theme = default +# use names as used by the icon theme, or give a full path here +hibernate = system-hibernate +lock = system-lock-screen +logout = system-log-out +reboot = system-reboot +shutdown = system-shutdown diff --git a/src/usr/bin/logout-manager-cli.py b/src/usr/bin/logout-manager-cli.py new file mode 100755 index 0000000..e69de29 diff --git a/src/usr/bin/logout-manager-gtk.py b/src/usr/bin/logout-manager-gtk.py new file mode 100755 index 0000000..553fc41 --- /dev/null +++ b/src/usr/bin/logout-manager-gtk.py @@ -0,0 +1,252 @@ +#!/usr/bin/env python3 +# File: logout-manager-gtk.py +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2019-06-01 +# Title: GTK3 based logout manager +# Purpose: Primarily for fluxbox, this tool provides a graphical menu for various session control commands like shutdown, logout, and reboot +# History: +# Usage: +# This is a bit for reference, but also to provide myself a little shutdown options menu, like xfce4, because fluxbox doesn't really provide one. +# Reference: +# https://www.linuxquestions.org/questions/slackware-14/how-do-i-run-menu-and-logout-from-the-command-line-in-fluxbox-864919/ +# /mnt/public/work/python/hotplug2/ +# icon handling https://python-gtk-3-tutorial.readthedocs.io/en/latest/iconview.html +# accelerator keys https://askubuntu.com/questions/655452/python-gtk3-keyboard-accelerators +# gtk3 widget signals https://developer.gnome.org/gtk3/unstable/GtkWidget.html#GtkWidget-button-press-event +# /usr/share/wicd/gtk/gui.py netentry.py wicd.ui +# combined with next ref: scale down valid icon https://stackoverflow.com/questions/42800482/how-to-set-size-of-a-gtk-image-in-python +# https://stackoverflow.com/questions/6090241/how-can-i-get-the-full-file-path-of-an-icon-name +# use custom icon theme https://lazka.github.io/pgi-docs/Gtk-3.0/classes/IconTheme.html#Gtk.IconTheme.set_custom_theme +# https://stackoverflow.com/questions/4090804/how-can-i-pass-variables-between-two-classes-windows-in-pygtk +# Improve: +# actually execute the commands +# only show debug info when DEBUG=1 or similar. +# support global conf file, and user conf file +# far future: provide graphical way to change commands run +# Dependencies: +# Devuan: python3-dotenv +# Documentation: + +import gi, os, platform, sys +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository.GdkPixbuf import Pixbuf +from pathlib import Path +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib + +# graphical classes and functions +def get_scaled_icon(icon_name, size=24, fallback_icon_name = "", icon_theme = "default"): + # return a Gtk.Image.new_from_pixbuf + + # ripped from https://stackoverflow.com/questions/42800482/how-to-set-size-of-a-gtk-image-in-python and combined with https://stackoverflow.com/questions/6090241/how-can-i-get-the-full-file-path-of-an-icon-name + # further ref for lookup_icon function: https://lazka.github.io/pgi-docs/Gtk-3.0/flags.html#Gtk.IconLookupFlags + # if a file exists by the specific name, use it. + if Path(icon_name).is_file(): + iconfilename = icon_name + else: + if icon_theme != "default": + this_theme = Gtk.IconTheme.new() + this_theme.set_custom_theme(icon_theme) + else: + this_theme = Gtk.IconTheme.get_default() + try: + icon_info = this_theme.lookup_icon(icon_name, size, 0) + iconfilename = icon_info.get_filename() + except: + try: + icon_info = this_theme.lookup_icon(fallback_icon_name, size, 0) + iconfilename = icon_info.get_filename() + except: + # no icon in the current theme. Try a hard-coded fallback: + try: + # if debuglev 3 + print("Error: could not find default icon for", icon_name+", so using fallback.") + this_theme = Gtk.IconTheme.new() + this_theme.set_custom_theme("Numix-Circle") + icon_info = this_theme.lookup_icon(icon_name, size, 0) + iconfilename = icon_info.get_filename() + except: + print("Error: Could not find any icon for", icon_name) + return None + #print(iconfilename) + return Gtk.Image.new_from_pixbuf(Pixbuf.new_from_file_at_scale( + filename=iconfilename, + width=size, height=size, preserve_aspect_ratio=True)) + +class MainWindow(Gtk.Window): + def __init__(self, config, actions): + self.actions = actions + self.config = config + Gtk.Window.__init__(self, title="Log out options") + # for window icon + liststore = Gtk.ListStore(Pixbuf, str) + iconview = Gtk.IconView.new() + iconview.set_model(liststore) + iconview.set_pixbuf_column(0) + iconview.set_text_column(1) + pixbuf24 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 24, 0) + pixbuf32 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 32, 0) + pixbuf48 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 48, 0) + pixbuf64 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 64, 0) + pixbuf96 = Gtk.IconTheme.get_default().load_icon(config.application_icon, 96, 0) + self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96]); + + # accel is for when you are not using the "set_use_underline" function. + #accel = Gtk.AccelGroup() + #accel.connect(Gdk.keyval_from_name('D'), Gdk.ModifierType.MOD1_MASK, 0, self.on_button2_accel) + #self.add_accel_group(accel) + + # buttons + self.grid = Gtk.Grid() + self.add(self.grid) + + self.button0 = Gtk.Button(label="Loc_k") + self.button0.connect("button-press-event", self.on_button0_press_event) + self.button0.connect("activate", self.on_button0_press_event) # activate covers ALT+L action and spacebar when selected + self.buttonicon0 = get_scaled_icon(config.get_lock_icon(), config.get_icon_size(), config.get_lock_fallback_icon(), config.get_icon_theme()) + self.button0.set_image(self.buttonicon0) + self.button0.set_tooltip_text("Hide session and require authentication to return to it") + self.button0.set_always_show_image(True) + self.button0.set_use_underline(True) + self.grid.add(self.button0) + + self.button1 = Gtk.Button(label="_Logout") + self.button1.connect("button-press-event", self.on_button1_press_event) + self.button1.connect("activate", self.on_button1_press_event) # activate covers ALT+L action and spacebar when selected + self.buttonicon1 = get_scaled_icon(config.get_logout_icon(), config.get_icon_size(), config.get_logout_fallback_icon(), config.get_icon_theme()) + self.button1.set_image(self.buttonicon1) + self.button1.set_tooltip_text("Close the current user session") + self.button1.set_always_show_image(True) + self.button1.set_use_underline(True) + self.grid.add(self.button1) + + self.buttonHibernate = Gtk.Button(label="_Hibernate") + self.buttonHibernate.connect("button-press-event", self.on_buttonHibernate_press_event) + self.buttonHibernate.connect("activate", self.on_buttonHibernate_press_event) # activate covers ALT+L action and spacebar when selected + #self.buttoniconHibernate = Gtk.Image() + #self.buttoniconHibernate.set_from_icon_name("system-hibernate", 24) + self.buttoniconHibernate = get_scaled_icon(config.get_hibernate_icon(), config.get_icon_size(), config.get_hibernate_fallback_icon(), config.get_icon_theme()) + self.buttonHibernate.set_image(self.buttoniconHibernate) + self.buttonHibernate.set_tooltip_text("Save state to disk and power off") + self.buttonHibernate.set_always_show_image(True) + self.buttonHibernate.set_use_underline(True) + self.buttonHibernate.set_sensitive(True if config.get_can_hibernate() else False) + self.grid.add(self.buttonHibernate) + + self.button2 = Gtk.Button(label="_Shutdown") + self.button2.connect("button-press-event", self.on_button2_press_event) + self.button2.connect("activate", self.on_button2_accel) + # unnecessary because the "activate" suffices above. + #self.button2.connect("mnemonic-activate", self.on_button2_accel) + self.buttonicon2 = Gtk.Image() + self.buttonicon2 = get_scaled_icon(config.get_shutdown_icon(), config.get_icon_size(), config.get_shutdown_fallback_icon(), config.get_icon_theme()) + self.button2.set_image(self.buttonicon2) + self.button2.set_tooltip_text("Power off the computer") + self.button2.set_always_show_image(True) + self.button2.set_use_underline(True) + self.grid.add(self.button2) + + self.button3 = Gtk.Button(label="_Reboot") + self.button3.connect("button-press-event", self.on_button3_press_event) + self.button3.connect("activate", self.on_button3_press_event) + self.buttonicon3 = Gtk.Image() + self.buttonicon3 = get_scaled_icon(config.get_reboot_icon(), config.get_icon_size(), config.get_reboot_fallback_icon(), config.get_icon_theme()) + self.button3.set_image(self.buttonicon3) + self.button3.set_tooltip_text("Reboot the computer back to the login screen") + self.button3.set_always_show_image(True) + self.button3.set_use_underline(True) + self.grid.add(self.button3) + + self.button4 = Gtk.Button(label="_Cancel") + self.button4.connect("button-press-event", self.on_button4_press_event) + self.button4.connect("activate", self.on_button4_press_event) + self.button4.set_tooltip_text("Do nothing; just close this window") + self.button4.set_use_underline(True) + self.grid.attach(self.button4, 0, 1, 8, 1) + + # hibernate button + def on_buttonHibernate_press_event(self, *args): + self.do_hibernate(self.buttonHibernate) + + # lock button + def on_button0_press_event(self, *args): + self.do_lock(self.button0) + + # logout button + def on_button1_press_event(self, *args): + self.do_logout(self.button1) + + # shutdown button + def on_button2_press_event(self, widget, event): + # check if left or right click + if event.type == Gdk.EventType.BUTTON_PRESS: + if event.button == 1: + self.do_shutdown(widget) + # eventbutton == 3 is the right-click, and its reference is my hello3.py + #elif event.button == 3: + # self.on_button1_right_clicked(widget) + + # global accelerator key, when not using the set_use_underline function + ## shutdown button from accelerator key + #def on_button2_accel(self, *args): + # self.do_shutdown(self.button2) + + # accelerator key from set_use_underline function + # shutdown button from accelerator key + def on_button2_accel(self, *args): + self.do_shutdown(self.button2) + + # reboot button + def on_button3_press_event(self, *args): + self.do_reboot(self.button3) + + # cancel button + def on_button4_press_event(self, *args): + self.cancel(self.button4) + + def do_shutdown(self, *args): + #print(dir(self.props)) + self.actions.shutdown(self.config) + + def do_hibernate(self, widget): + self.actions.hibernate(self.config) + + def do_lock(self, widget): + self.actions.lock(self.config) + + def do_logout(self, widget): + self.actions.logout(self.config) + + def do_reboot(self, widget): + self.actions.reboot(self.config) + + def cancel(self, widget): + print("Cancel any logout action.") + Gtk.main_quit() + +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions + +# MAIN LOOP +win = MainWindow(config, actions) +win.connect("destroy", Gtk.main_quit) +win.show_all() +Gtk.main() diff --git a/src/usr/bin/logout-manager-ncurses.py b/src/usr/bin/logout-manager-ncurses.py new file mode 100755 index 0000000..1500d85 --- /dev/null +++ b/src/usr/bin/logout-manager-ncurses.py @@ -0,0 +1,193 @@ +#!/usr/bin/env python3 +# File: logout-manager-ncurses.py +# License: MIT +# Author: adamlamers, bgstack15 +# Startdate: 2020-03-09 17:06 +# Title: ncurses based logout manager +# Usage: +# logout-manager-ncurses.py +# Reference: +# https://docs.python.org/3/howto/curses.html +# ripped straight from http://adamlamers.com/post/FTPD9KNRA8CT +# https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 +# https://robinislam.me/blog/reading-environment-variables-in-python/ +# Improve: +# Dependencies: +# Devuan: python3-dotenv +# Documentation: +# Improvements for CursesMenu class over origin: +# accepts number key inputs +# accepts enabled attribute +# add "zeroindex" bool + +import curses, os, platform, sys +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib + +class CursesMenu(object): + + INIT = {'type' : 'init'} + + def __init__(self, menu_options): + self.screen = curses.initscr() + self.menu_options = menu_options + self.selected_option = 0 + self._previously_selected_option = None + self.running = True + self._zero_offset = 1 + try: + self._zero_offset = 0 if bool(self.menu_options['zeroindex']) else 1 + except: + pass + + #init curses and curses input + curses.noecho() + curses.cbreak() + curses.start_color() + curses.curs_set(0) #Hide cursor + self.screen.keypad(1) + + #set up color pair for highlighted option + curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) + self.hilite_color = curses.color_pair(1) + self.normal_color = curses.A_NORMAL + + def prompt_selection(self, parent=None): + if parent is None: + lastoption = "Cancel" + else: + lastoption = "Return to previous menu ({})".format(parent['title']) + + option_count = len(self.menu_options['options']) + + input_key = None + + ENTER_KEY = ord('\n') + NUM_KEYS = [ord(str(i)) for i in range(self._zero_offset,option_count+1+self._zero_offset)] + done = False + while not done: + if self.selected_option != self._previously_selected_option: + self._previously_selected_option = self.selected_option + + self.screen.border(0) + self._draw_title() + for option in range(option_count): + if self.selected_option == option: + self._draw_option(option, self.hilite_color) + else: + self._draw_option(option, self.normal_color) + + if self.selected_option == option_count: + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, + lastoption), self.hilite_color) + else: + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, + lastoption), self.normal_color) + + max_y, max_x = self.screen.getmaxyx() + if input_key is not None: + self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option+self._zero_offset)) + self.screen.refresh() + + + input_key = self.screen.getch() + down_keys = [curses.KEY_DOWN, ord('j')] + up_keys = [curses.KEY_UP, ord('k')] + exit_keys = [ord('q')] + + if input_key in down_keys: + if self.selected_option < option_count: + self.selected_option += 1 + else: + self.selected_option = 0 + + if input_key in up_keys: + if self.selected_option > 0: + self.selected_option -= 1 + else: + self.selected_option = option_count + + if input_key in exit_keys: + self.selected_option = option_count #auto select exit and return + break + + if input_key == ENTER_KEY or input_key in NUM_KEYS: + if input_key in NUM_KEYS: + self.selected_option=int(chr(input_key))-self._zero_offset + done = True + try: + done = self.menu_options['options'][self.selected_option]['enabled'] + except: + pass + return self.selected_option + + def _draw_option(self, option_number, style): + thistext = self.menu_options['options'][option_number]['title'] + try: + if self.menu_options['options'][option_number]['enabled'] == False: thistext += " (disabled)" + except: + pass + self.screen.addstr(4 + option_number, + 4, + "{:2} - {}".format(option_number+self._zero_offset, thistext), + style) + + def _draw_title(self): + self.screen.addstr(2, 2, self.menu_options['title'], curses.A_STANDOUT) + self.screen.addstr(3, 2, self.menu_options['subtitle'], curses.A_BOLD) + + def display(self): + selected_option = self.prompt_selection() + i, _ = self.screen.getmaxyx() + curses.endwin() + #os.system('clear') + if selected_option < len(self.menu_options['options']): + selected_opt = self.menu_options['options'][selected_option] + return selected_opt + else: + self.running = False + return {'title' : 'Cancel', 'type' : 'exitmenu'} + +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions + +# MAIN LOOP +menu = { + 'title' : 'Logout Manager', + 'type' : 'menu', + 'subtitle' : 'Use arrows or number keys', + 'zeroindex' : False, + 'options' : [ + {'title': 'Lock', 'type': 'action', 'action': 'lock'}, + {'title': 'Logout', 'type': 'action', 'action': 'logout'}, + {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate', 'enabled': config.can_hibernate}, + {'title': 'Shutdown', 'type': 'action', 'action': 'shutdown'}, + {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} + ] +} +m = CursesMenu(menu) +selected_action = m.display() + +if selected_action['type'] == 'exitmenu': + print("Cancel any logout action.") +elif selected_action['type'] == 'command': + os.system(selected_action['command']) +elif selected_action['type'] == 'action': + #a = selected_action['action']: + func = getattr(globals()['actions'],selected_action['action']) + func(config) diff --git a/src/usr/bin/logout-manager-tcl.py b/src/usr/bin/logout-manager-tcl.py new file mode 100755 index 0000000..127bd54 --- /dev/null +++ b/src/usr/bin/logout-manager-tcl.py @@ -0,0 +1,417 @@ +#!/usr/bin/env python3 +# File: logout-manager-tcl.py +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2019-06-12 20:05 +# Title: Tcl/tk-based logout manager +# Purpose: A tcl/tk graphical program for selecting shutdown, logout, etc. +# History: +# Usage: +# logout-manager-tcl.py +# References: +# http://effbot.org/tkinterbook/button.htm +# http://effbot.org/tkinterbook/tkinter-application-windows.htm +# http://effbot.org/tkinterbook/ +# pass parameters to function of tkinter.Button(command=) https://stackoverflow.com/questions/38749620/python-3-tkinter-button-commands#38750155 +# alternate for passing params https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter +# https://stackoverflow.com/questions/18537918/set-window-icon#18538416 +# the exact syntax for master.bind https://stackoverflow.com/questions/16082243/how-to-bind-ctrl-in-python-tkinter +# https://pillow.readthedocs.io/en/stable/reference/ImageTk.html +# gtk-3.0 default icon theme https://coderwall.com/p/no3qfa/setting-gtk2-and-gtk3-theme-via-config-file +# homedir https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python +# natural sort https://stackoverflow.com/questions/46228101/sort-list-of-strings-by-two-substrings-using-lambda-function/46228199#46228199 +# tooltips https://stackoverflow.com/questions/3221956/how-do-i-display-tooltips-in-tkinter/41381685#41381685 +# Improve: +# Dependencies: +# Devuan: python3-tk python3-pil.imagetk python3-cairosvg +# el7: python36-tkinter python36-pillow-tk ( pip3 install cairosvg ) + +import glob, os, platform, re, sys +import tkinter as tk +from functools import partial +from pathlib import Path +from sys import path +from dotenv import load_dotenv +# loading PIL.ImageTk after tkinter makes ImageTk use the PIL version, which supports PNG. This is important on tcl < 8.6 (that is, el7) +from PIL import Image, ImageTk + +LM_USE_SVG = 0 +try: + from cairosvg import svg2png + LM_USE_SVG = 1 +except: + print("WARNING: Unable to import cairosvg. No svg images will be displayed.") + LM_USE_SVG = 0 + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib + +# graphical classes and functions +print("Loading graphics...") + +class Tooltip: + + ''' + It creates a tooltip for a given widget as the mouse goes on it. + + see: + + http://stackoverflow.com/questions/3221956/ + what-is-the-simplest-way-to-make-tooltips- + in-tkinter/36221216#36221216 + + http://www.daniweb.com/programming/software-development/ + code/484591/a-tooltip-class-for-tkinter + + - Originally written by vegaseat on 2014.09.09. + + - Modified to include a delay time by Victor Zaccardo on 2016.03.25. + + - Modified + - to correct extreme right and extreme bottom behavior, + - to stay inside the screen whenever the tooltip might go out on + the top but still the screen is higher than the tooltip, + - to use the more flexible mouse positioning, + - to add customizable background color, padding, waittime and + wraplength on creation + by Alberto Vassena on 2016.11.05. + + Tested on Ubuntu 16.04/16.10, running Python 3.5.2 + + TODO: themes styles support + ''' + + def __init__(self, widget, + *, + bg='#FFFFEA', + pad=(5, 3, 5, 3), + text='widget info', + waittime=400, + wraplength=250): + + self.waittime = waittime # in miliseconds, originally 500 + self.wraplength = wraplength # in pixels, originally 180 + self.widget = widget + self.text = text + self.widget.bind("", self.onEnter) + self.widget.bind("", self.onLeave) + self.widget.bind("", self.onLeave) + self.bg = bg + self.pad = pad + self.id = None + self.tw = None + + def onEnter(self, event=None): + self.schedule() + + def onLeave(self, event=None): + self.unschedule() + self.hide() + + def schedule(self): + self.unschedule() + self.id = self.widget.after(self.waittime, self.show) + + def unschedule(self): + id_ = self.id + self.id = None + if id_: + self.widget.after_cancel(id_) + + def show(self): + def tip_pos_calculator(widget, label, + *, + tip_delta=(10, 5), pad=(5, 3, 5, 3)): + + w = widget + + s_width, s_height = w.winfo_screenwidth(), w.winfo_screenheight() + + width, height = (pad[0] + label.winfo_reqwidth() + pad[2], + pad[1] + label.winfo_reqheight() + pad[3]) + + mouse_x, mouse_y = w.winfo_pointerxy() + + x1, y1 = mouse_x + tip_delta[0], mouse_y + tip_delta[1] + x2, y2 = x1 + width, y1 + height + + x_delta = x2 - s_width + if x_delta < 0: + x_delta = 0 + y_delta = y2 - s_height + if y_delta < 0: + y_delta = 0 + + offscreen = (x_delta, y_delta) != (0, 0) + + if offscreen: + + if x_delta: + x1 = mouse_x - tip_delta[0] - width + + if y_delta: + y1 = mouse_y - tip_delta[1] - height + + offscreen_again = y1 < 0 # out on the top + + if offscreen_again: + # No further checks will be done. + + # TIP: + # A further mod might automagically augment the + # wraplength when the tooltip is too high to be + # kept inside the screen. + y1 = 0 + + return x1, y1 + + bg = self.bg + pad = self.pad + widget = self.widget + + # creates a toplevel window + self.tw = tk.Toplevel(widget) + + # Leaves only the label and removes the app window + self.tw.wm_overrideredirect(True) + + win = tk.Frame(self.tw, + background=bg, + borderwidth=0) + label = tk.Label(win, + text=self.text, + justify=tk.LEFT, + background=bg, + relief=tk.SOLID, + borderwidth=0, + wraplength=self.wraplength) + + label.grid(padx=(pad[0], pad[2]), + pady=(pad[1], pad[3]), + sticky=tk.NSEW) + win.grid() + + x, y = tip_pos_calculator(widget, label) + + self.tw.wm_geometry("+%d+%d" % (x, y)) + + def hide(self): + tw = self.tw + if tw: + tw.destroy() + self.tw = None + +def tryint(s): + try: + return int(s) + except: + return s + +def sort_sizes(x): + # Original reference so#46228101 + value = x.split("/")[5] + return mynum(value, "all") + +def mynum(x, type = "all"): + # return the complicated numerical value for the weird size options + f = re.split("[^0-9]+",x) + try: + f0 = int(f[0]) + except: + f0 = 0 + try: + f1 = int(f[1]) + except: + f1 = 0 + if type == "all": + return f0 * 100 + f1 if len(f) >= 2 else 0 + else: + return f0 + +def find_best_size_match(size, thelist): + # return item from sorted thelist whose split("/")[5] is the first to meet or exceed the requested size + try: + default = thelist[-1] + except: + default = None + return next(( i for i in thelist if mynum(i.split("/")[5],"real") >= size ), default) + +def get_filename_of_icon(name, theme = "hicolor", size = 48, category = "actions"): + # poor man's attempt at walking through fd.o icon theme + filename = None + # example: Adwaita system-log-out + + if theme == "default" or theme is None: + try: + theme = lmlib.get_gtk3_default_icon_theme() + except: + theme = "hicolor" + + # first, find all files underneath /usr/share/icons/$THEME/$SIZE + print("Finding filename of icon, theme=",theme,"category=",category,"name=",name) + # to exclude the scalable/ contents, replace dir 5 asterisk with [0-9]* + results = [] + base_dir="/usr/share/icons/" + file_filters = ".*" + if LM_USE_SVG == 0: + file_filters = ".{png,PNG}" + # I have no idea if this is xdg icon theme compliant, but it is a valiant attempt. + # 1. try (requested) req-theme, req-category, req-name first + results = glob.glob(base_dir+theme+"/*/"+category+"/"+name+file_filters) + # 2. try req-theme, (generic) gen-category, req-name + if len(results) == 0: + # no results with that category, so try all categories + results = glob.glob(base_dir+theme+"/*/*/"+name+file_filters) + # 3. try "gnome", req-category, req-name + if len(results) == 0: + results = glob.glob(base_dir+"gnome"+"/*/"+category+"/"+name+file_filters) + # 4. try "gnome", gen-category, req-name + if len(results) == 0: + results = glob.glob(base_dir+"gnome"+"/*/*/"+name+file_filters) + # 5. try "hicolor", req-category, req-name + if len(results) == 0: + results = glob.glob(base_dir+"hicolor"+"/*/"+category+"/"+name+file_filters) + # 6. try "hicolor", gen-category, req-name + if len(results) == 0: + results = glob.glob(base_dir+"hicolor"+"/*/*/"+name+file_filters) + + # the sort arranges it so a Numix/24 dir comes before a Numix/24@2x dir + results = sorted(results, key=sort_sizes) + #print(results) + # now find the first one that matches + filename = find_best_size_match(size,results) + return filename + +def photoimage_from_svg(filename = "",size = "48"): + # this one works, but does not allow me to set the size. + # this is kept as an example of how to open a svg without saving to a file. + # open svg + item = svg2png(url=filename, parent_width = size, parent_height = size) + return ImageTk.PhotoImage(data=item) + +def empty_photoimage(size=24): + photo = Image.new("RGBA",[size,size]) + return ImageTk.PhotoImage(image=photo) + +def image_from_svg(filename = "",size = "48"): + # open svg + if LM_USE_SVG == 1: + svg2png(url=filename,write_to="/tmp/lm_temp_image.png",parent_width = size,parent_height = size) + photo = Image.open("/tmp/lm_temp_image.png") + else: + photo = Image.new("RGBA",[size,size]) + return photo + +def get_scaled_icon(icon_name, size = 24, icon_theme = "default", fallback_icon_name = ""): + iconfilename = None + + # if name is a specific filename, just use it. + if Path(icon_name).is_file(): + #print("This is a file:",icon_name) + iconfilename = icon_name + else: + + if icon_theme == "default": + # this should not happen, because the Initialize_config should have checked gtk3 default value. + icon_theme = "hicolor" + # so now that icon_theme is defined, let us go find the icon that matches the requested name and size, in the actions category + #print("Using icon theme",icon_theme) + iconfilename = get_filename_of_icon(name=icon_name, theme=icon_theme, size=size, category=config.get_icon_category()) + + # So now we think we have derived the correct filename + try: + print("Trying icon file",iconfilename) + # try an svg + if re.compile(".*\.svg").match(iconfilename): + print("Trying svg...") + photo = image_from_svg(filename=iconfilename, size=size) + else: + photo = Image.open(iconfilename) + except Exception as f: + print("Error with icon file.") + print(f) + return empty_photoimage() + photo.thumbnail(size=[size, size]) + try: + photo = ImageTk.PhotoImage(photo) + except Exception as e: + print("Error was ",e) + # If I ever add HiDPI support, multiple size here by the factor. So, size * 1.25 + return photo + +class App: + def __init__(self, master): + frame = tk.Frame(master) + frame.grid(row=0) + + self.photoLock = get_scaled_icon(config.get_lock_icon(), config.get_icon_size(), config.get_icon_theme()) + self.buttonLock = tk.Button(frame, text="Lock", underline=3, command=partial(actions.lock,config), image=self.photoLock, compound=tk.LEFT) + master.bind_all("", partial(actions.lock,config)) + Tooltip(self.buttonLock, text="Hide session and require authentication to return to it") + self.buttonLock.grid(row=0,column=0) + + self.photoLogout = get_scaled_icon(config.get_logout_icon(), config.get_icon_size(), config.get_icon_theme()) + self.buttonLogout = tk.Button(frame, text="Logout", underline=0, command=lambda: actions.logout(config), image=self.photoLogout, compound=tk.LEFT) + master.bind_all("", partial(actions.logout,config)) + Tooltip(self.buttonLogout, text="Close the current user session") + self.buttonLogout.grid(row=0,column=1) + + self.photoHibernate = get_scaled_icon(config.get_hibernate_icon(), config.get_icon_size(), config.get_icon_theme()) + self.buttonHibernate = tk.Button(frame, text="Hibernate", underline=0, command=lambda: actions.hibernate(config), image=self.photoHibernate, compound=tk.LEFT) + master.bind_all("", partial(actions.hibernate,config)) + Tooltip(self.buttonHibernate, text="Save state to disk and power off") + self.buttonHibernate.grid(row=0,column=2) + + self.photoShutdown = get_scaled_icon(config.get_shutdown_icon(), config.get_icon_size(), config.get_icon_theme()) + self.buttonShutdown = tk.Button(frame, text="Shutdown", underline=0, command=lambda: actions.shutdown(config), image=self.photoShutdown, compound=tk.LEFT) + master.bind_all("", partial(actions.shutdown,config)) + Tooltip(self.buttonShutdown, text="Power off the computer") + self.buttonShutdown.grid(row=0,column=3) + + self.photoReboot = get_scaled_icon(config.get_reboot_icon(), config.get_icon_size(), config.get_icon_theme()) + self.buttonReboot = tk.Button(frame, text="Reboot", underline=0, command=lambda: actions.reboot(config), image=self.photoReboot, compound=tk.LEFT) + master.bind_all("", partial(actions.reboot,config)) + Tooltip(self.buttonReboot, text="Reboot the computer back to the login screen") + self.buttonReboot.grid(row=0,column=4) + + self.buttonCancel = tk.Button(frame, text="Cancel", underline=0, command=self.quitaction) + master.bind_all("", self.quitaction) + Tooltip(self.buttonCancel, text="Do nothing; just close this window") + self.buttonCancel.grid(row=1,columnspan=8,sticky=tk.W+tk.E) + + # Found this after trial and error. + def quitaction(self,b=None): + print("Cancel any logout action.") + root.destroy() + +# Left here as an example for a mster.bind_all that works. +#def something(event=None): +# print("Got here!") + +# load configs +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions + +# MAIN LOOP +root = tk.Tk() +root.title("Log out options") +imgicon = get_scaled_icon(config.get_logout_icon(),24,config.get_icon_theme()) +root.tk.call('wm','iconphoto', root._w, imgicon) +app = App(root) +root.mainloop() +try: + root.destroy() +except: + pass diff --git a/src/usr/share/logout-manager/lmlib.py b/src/usr/share/logout-manager/lmlib.py new file mode 100644 index 0000000..7c9dc1e --- /dev/null +++ b/src/usr/share/logout-manager/lmlib.py @@ -0,0 +1,286 @@ +#!/usr/bin/env python3 +# File: lmlib.py +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2019-06-12 +# Title: Python libs for logout-manager +# Purpose: Store the common elements for operating a logout-manager +# History: +# Usage: +# In a logout-manager-gtk.py program +# Reference: +# platform info https://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python/10091465#10091465 +# Improve: +# Documentation: + +import configparser, platform, os + +logout_manager_version="2020-03-10a" + +class Actions: + + @staticmethod + def hibernate(config, event=None): + #print("need to run the /sys/power/state trick, if available") + print(config.get_hibernate_command()) + + @staticmethod + def lock(config, event=None): + #print("please lock the screen.") + print(config.get_lock_command()) + + @staticmethod + def logout(config, event=None): + #print("please log out of current session!") + print(config.get_logout_command()) + + @staticmethod + def reboot(config, event=None): + #print("please reboot.") + print(config.get_reboot_command()) + + @staticmethod + def shutdown(config, event=None): + #print("please shut yourself down!") + print(config.get_shutdown_command()) + +class Config: + def __init__(self): + # load defaults which can be overwritten + self.hibernate_command = "" + self.lock_command = "" + self.logout_command = "" + self.reboot_command = "" + self.shutdown_command = "" + self.hibernate_icon = "system-hibernate" + self.hibernate_fallback_icon = "system-hibernate" + self.lock_icon = "system-lock-screen" + self.lock_fallback_icon = "system-lock-screen" + self.logout_icon = "system-log-out" + self.logout_fallback_icon = "system-log-out" + self.reboot_icon = "system-reboot" + self.reboot_fallback_icon = "system-reboot" + self.shutdown_icon = "system-shutdown" + self.shutdown_fallback_icon = "system-shutdown" + self.icon_size = 24 + self.icon_theme = "default" + self.gtk3_default_icon_theme = "hicolor" + self.icon_category = "actions" + self.can_hibernate = False + self.application_icon=("system-log-out") + + def set_hibernate_command(self,hibernate_command): + self.hibernate_command = hibernate_command + + def set_lock_command(self,lock_command): + self.lock_command = lock_command + + def set_logout_command(self,logout_command): + self.logout_command = logout_command + + def set_reboot_command(self,reboot_command): + self.reboot_command = reboot_command + + def set_shutdown_command(self,shutdown_command): + self.shutdown_command = shutdown_command + + def set_hibernate_icon(self,hibernate_icon): + self.hibernate_icon = hibernate_icon + + def set_lock_icon(self,lock_icon): + self.lock_icon = lock_icon + + def set_logout_icon(self,logout_icon): + self.logout_icon = logout_icon + + def set_reboot_icon(self,reboot_icon): + self.reboot_icon = reboot_icon + + def set_shutdown_icon(self,shutdown_icon): + self.shutdown_icon = shutdown_icon + + def set_icon_size(self,icon_size): + self.icon_size = int(icon_size) + + def set_icon_theme(self,icon_theme): + self.icon_theme = icon_theme + + def set_gtk3_default_icon_theme(self,icon_theme): + self.gtk3_default_icon_theme= icon_theme + + def set_icon_category(self,icon_category): + self.icon_category = icon_category + + def set_can_hibernate(self,can_hibernate): + print("Setting can_hibernate:",can_hibernate) + self.can_hibernate = bool(can_hibernate) + + def get_hibernate_command(self): + return self.hibernate_command + + def get_lock_command(self): + return self.lock_command + + def get_logout_command(self): + return self.logout_command + + def get_reboot_command(self): + return self.reboot_command + + def get_shutdown_command(self): + return self.shutdown_command + + def get_hibernate_icon(self): + return self.hibernate_icon + + def get_lock_icon(self): + return self.lock_icon + + def get_logout_icon(self): + return self.logout_icon + + def get_reboot_icon(self): + return self.reboot_icon + + def get_shutdown_icon(self): + return self.shutdown_icon + + def get_hibernate_fallback_icon(self): + return self.hibernate_fallback_icon + + def get_lock_fallback_icon(self): + return self.lock_fallback_icon + + def get_logout_fallback_icon(self): + return self.logout_fallback_icon + + def get_reboot_fallback_icon(self): + return self.reboot_fallback_icon + + def get_shutdown_fallback_icon(self): + return self.shutdown_fallback_icon + + def get_icon_size(self): + return self.icon_size + + def get_icon_theme(self): + return self.icon_theme + + def get_gtk3_default_icon_theme(self): + return self.gtk3_default_icon_theme + + def get_icon_category(self): + return self.icon_category + + def get_can_hibernate(self): + return self.can_hibernate + +def get_gtk3_default_icon_theme(): + # abstracted so it does not clutter get_scaled_icon + name = "hicolor" + gtk3_config_path = os.path.join(os.path.expanduser("~"),".config","gtk-3.0","settings.ini") + gtk3_config = configparser.ConfigParser() + gtk3_config.read(gtk3_config_path) + try: + if 'Settings' in gtk3_config: + name = gtk3_config['Settings']['gtk-icon-theme-name'] + elif 'settings' in gtk3_config: + name = gtk3_config['settings']['gtk-icon-theme-name'] + except: + # supposed failsafe: keep name = hicolor + pass + print("Found gtk3 default theme:",name) + return name + +def Initialize_config(infile): + # Read config + config_in = configparser.ConfigParser() + config_in.read(infile) + config = Config() + try: + ci = config_in['logout-manager'] + except: + # no definition + print("Using default commands") + + try: + ci_icons = config_in['icons'] + except: + # no definition + print("Using default icons") + + # load up our custom class, which stores the defaults in case we do not set them here + if 'hibernate_command' in ci: + config.set_hibernate_command(ci['hibernate_command']) + if 'lock_command' in ci: + config.set_lock_command(ci['lock_command']) + if 'logout_command' in ci: + config.set_logout_command(ci['logout_command']) + if 'reboot_command' in ci: + config.set_reboot_command(ci['reboot_command']) + if 'shutdown_command' in ci: + config.set_shutdown_command(ci['shutdown_command']) + if 'hibernate' in ci_icons: + config.set_hibernate_icon(ci_icons['hibernate']) + if 'lock' in ci_icons: + config.set_lock_icon(ci_icons['lock']) + if 'logout' in ci_icons: + config.set_logout_icon(ci_icons['logout']) + if 'reboot' in ci_icons: + config.set_reboot_icon(ci_icons['reboot']) + if 'shutdown' in ci_icons: + config.set_shutdown_icon(ci_icons['shutdown']) + if 'size' in ci_icons: + config.set_icon_size(ci_icons['size']) + if 'theme' in ci_icons: + config.set_icon_theme(ci_icons['theme']) + # store the info about if hibernate is an option + can_hibernate = False + try: + with open('/sys/power/state') as r: + line = r.read() + if 'disk' in line: can_hibernate = True + except: + pass + config.set_can_hibernate(can_hibernate) + + # read gtk3_default_icon_theme + config.set_gtk3_default_icon_theme(get_gtk3_default_icon_theme()) + if config.get_icon_theme() == "default": + config.set_icon_theme(config.get_gtk3_default_icon_theme()) + + # set icon category + # written primarily for el7 which uses "app" for the system-reboot icons, etc. + a = platform.dist() + try: + if a[0] == "redhat" and int(a[1].split(".")[0]) <= 7: + config.set_icon_category("apps") + except: + pass + + # DEBUG, raw from conf file and system status + print("Raw values:") + for item in config_in.sections(): + print("["+item+"]") + for key in config_in[item]: + print(key+" = "+config_in[item][key]) + print("Can hibernate:",can_hibernate) + + # DEBUG, stored values + print("Stored values:") + print(config.get_hibernate_command()) + print(config.get_lock_command()) + print(config.get_logout_command()) + print(config.get_reboot_command()) + print(config.get_shutdown_command()) + print(config.get_hibernate_icon()) + print(config.get_lock_icon()) + print(config.get_logout_icon()) + print(config.get_reboot_icon()) + print(config.get_shutdown_icon()) + print(config.get_icon_size()) + print(config.get_icon_theme()) + print(config.get_icon_category()) + print("Can hibernate:",config.get_can_hibernate()) + + return config -- cgit From 2ba2adae6525f9cf9169e7ef3d50eaabba57feb4 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 19:37:51 -0400 Subject: add lm-cli --- src/usr/bin/logout-manager-cli.py | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/usr/bin/logout-manager-cli.py b/src/usr/bin/logout-manager-cli.py index e69de29..db754b6 100755 --- a/src/usr/bin/logout-manager-cli.py +++ b/src/usr/bin/logout-manager-cli.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# File: logout-manager-cli.py +# License: CC-BY-SA 4.0 +# Author: bgstack15 +# Startdate: 2020-03-10 18:40 +# Title: cli logout manager +# Purpose: Feature completeness in this package +# History: +# Usage: +# logout-manager-cli.py +# Reference: +# https://stackoverflow.com/questions/39092149/argparse-how-to-make-mutually-exclusive-arguments-optional/39092229#39092229 +# https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 +# Improve: +# Dependencies: +# Devuan: python3-dotenv +# Documentation: + +import os, platform, sys, argparse +from dotenv import load_dotenv + +# all this to load the libpath +try: + defaultdir="/etc/sysconfig" + thisplatform = platform.platform().lower() + if 'debian' in thisplatform or 'devuan' in thisplatform: + defaultdir="/etc/default" + # load_dotenv keeps existing environment variables as higher precedent + load_dotenv(os.path.join(defaultdir,"logout-manager")) +except: + pass +if 'LOGOUT_MANAGER_LIBPATH' in os.environ: + for i in os.environ['LOGOUT_MANAGER_LIBPATH'].split(":"): + sys.path.append(i) +import lmlib + +def eprint(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) + +logout_manager_cli_version="2020-03-10" + +parser = argparse.ArgumentParser(description="run logout-manager commands using cli") +parser.add_argument('action', help='which action to take',nargs='?', choices=('lock','logout','hibernate','shutdown','reboot')) +parser.add_argument("-d","--debug", nargs='?', default=0, type=int, choices=range(0,11), help="Set debug level.") +parser.add_argument("-n","--dryrun", action='store_true', help="only report. Useful for checking if hibernate is allowed.") +parser.add_argument("-V","--version", action="version", version="%(prog)s " + logout_manager_cli_version) + +args = parser.parse_args() + +# load configs +# in cli, must happen after arparse to benefit from debug value +config = lmlib.Initialize_config(os.environ['LOGOUT_MANAGER_CONF']) +actions = lmlib.Actions + +# MAIN LOOP +allowed_actions=['lock','logout','shutdown','reboot'] +if config.can_hibernate: + allowed_actions.append('hibernate') + +if args.action in allowed_actions: + func = getattr(globals()['actions'],args.action) + func(config) +elif args.action: + eprint("Unable to take action: %s" % str(args.action)) + sys.exit(1) + +# if we get here, no action was used +parser.print_help() +sys.exit(2) -- cgit From ffea6275fcb08b36c3d7f5182785cbd329f15e40 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 20:40:39 -0400 Subject: add desktop file, some readme contents --- README.md | 40 +---------- src/Makefile | 85 +++++++++++++++++++++++ src/etc/default/logout-manager | 2 - src/etc/sysconfig/logout-manager | 2 + src/usr/share/applications/logout-manager.desktop | 12 ++++ src/usr/share/doc/logout-manager/README.md | 38 ++++++++++ 6 files changed, 139 insertions(+), 40 deletions(-) create mode 100644 src/Makefile delete mode 100644 src/etc/default/logout-manager create mode 100644 src/etc/sysconfig/logout-manager create mode 100644 src/usr/share/applications/logout-manager.desktop create mode 100644 src/usr/share/doc/logout-manager/README.md diff --git a/README.md b/README.md index 0716dff..aa626bb 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,2 @@ -# README for logout-manager -## Introduction -Logout Manager is a python3 utility that provides a simple menu for logout-type actions. The supported actions are presented: - * Lock - * Logout - * Hibernate (if supported by hardware) - * Shutdown - * Reboot - -## Alternatives -[oblogout](https://launchpad.net/oblogout) looks really old so I did not investigate personally, but it sounds like it does the same thing I am trying to do. -`apt-cache search logout` shows [lxsession-logout](http://manpages.ubuntu.com/manpages/precise/en/man1/lxsession-logout.1.html) which was compiled, as well as does not provide configurable options for changing executed commands or icons. - -## License -[logout-manager-ncurses.py](src/usr/bin/logout-manager-ncurses.py) is licensed under the [MIT license](http://choosealicense.com/licenses/mit) and is derived almost entirely from [adamlamers](http://adamlamers.com/post/FTPD9KNRA8CT). -Everything else is licensed under [CC-BY-SA 4.0](https://choosealicense.com/licenses/cc-by-sa-4.0/). - -## Description -This project is partially a programming playground for the [original author](https://bgstack15.wordpress.com) and partially a useful project for his migration to [Fluxbox](http://fluxbox.org/) on the desktop. - -## Upsides -* This project is the first to [demonstrate SVG images in tkinter in python3](https://bgstack15.wordpress.com/2019/07/13/display-svg-in-tkinter-python3/) that I could find on the Internet. -* I have learned how to work with ncurses, gtk, and tcl in python3. -* This will make Fluxbox systems easier to use for general users. - -## Downsides -This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. - -## Improve -* add makefile - * add the standard 'list' option - * add a `list_dependencies` option or similar, which does the grep command - - grep -h -A5 -riIE dependencies * | awk 'tolower($0) ~ /devuan/ {$1="";$2="";print}' | sort | uniq | xargs - -* add logout-manager-cli.py -* add debian/ dir - * and dsc file +# Overview for logout-manager +See the [full readme](src/usr/share/doc/logout-manager/README.md) farther down in the source tree. diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..6af4efb --- /dev/null +++ b/src/Makefile @@ -0,0 +1,85 @@ +# File: Makefile for logout-manager +# Location: logout-manager source package +# Author: bgstack15 +# Startdate: 2020-03-10 +# Title: Makefile for logout-manager source package +# Purpose: To use traditional Unix make utility +# History: +# Usage: +# Reference: +# https://stackoverflow.com/questions/4219255/how-do-you-get-the-list-of-targets-in-a-makefile/26339924#26339924 +# https://stackoverflow.com/questions/19105241/how-do-you-conditionally-call-a-target-based-on-a-target-variable-makefile/19107231#19107231 +# https://stackoverflow.com/questions/5917576/sort-a-text-file-by-line-length-including-spaces +# https://superuser.com/questions/352289/bash-scripting-test-for-empty-directory/667100#667100 +# bgscripts Makefile +# Improve: +# Document: +# Dependencies: + +APPNAME = logout-manager +APPVERSION = 0.0.1 +SRCDIR = $(CURDIR) +prefix = /usr +SYSCONFDIR = $(DESTDIR)/etc +DEFAULTDIR = $(DESTDIR)/etc/sysconfig +# for debian use '$(DESTDIR)/etc/default' +BINDIR = $(DESTDIR)$(prefix)/bin +SHAREDIR = $(DESTDIR)$(prefix)/share +LIBEXECDIR = $(DESTDIR)$(prefix)/libexec +DOCDIR = $(SHAREDIR)/doc/$(APPNAME) +APPDIR = $(SHAREDIR)/$(APPNAME) +APPSDIR = $(SHAREDIR)/applications + +awkbin :=$(shell which awk) +cpbin :=$(shell which cp) +echobin :=$(shell which echo) +findbin :=$(shell which find) +grepbin :=$(shell which grep) +installbin :=$(shell which install) +rmbin :=$(shell which rm) +sedbin :=$(shell which sed) +sortbin :=$(shell which sort) +truebin :=$(shell which true) +xargsbin :=$(shell which xargs) +lnbin :=$(shell which ln) + +all: + ${echobin} "No compilation in this package." + +.PHONY: clean install uninstall list deplist deplist_opts + +list: + @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | ${awkbin} -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | ${sortbin} | ${grepbin} -E -v -e '^[^[:alnum:]]' -e '^$@$$' + +deplist: + @if test -z "$(DISTRO)" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." ; exit 1 ; fi + @${grepbin} -h --exclude='Makefile' -A5 -riIE dependencies $(SRCDIR) | awk -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | sort | uniq | xargs + +deplist_opts: + @${echobin} "el7" 1>&2 + @${echobin} "devuan" 1>&2 + +install: + @${echobin} Installing files to ${DESTDIR} + ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} + ${cpbin} -pr ${SRCDIR}/etc/*.* ${SYSCONFDIR} + ${cpbin} -pr ${SRCDIR}/etc/sysconfig/* ${DEFAULTDIR} + ${cpbin} -pr ${SRCDIR}/usr/bin/* ${BINDIR} + ${cpbin} -pr ${SRCDIR}/usr/share/applications/* ${APPSDIR} + ${cpbin} -pr ${SRCDIR}/usr/share/${APPNAME}/*.* ${APPDIR} + ${cpbin} -pr ${SRCDIR}/usr/share/doc/${APPNAME}/* ${DOCDIR} + # symlink, when alternatives is not being used + ${lnbin} -s logout-manager-gtk.py ${BINDIR}/logout-manager + +uninstall: + @${echobin} SRCDIR=${SRCDIR} + ${rmbin} -f $$( ${findbin} ${SRCDIR} -mindepth 1 ! -type d -printf '%p\n' | ${sedbin} -r -e "s:^${SRCDIR}:${DESTDIR}:" ) ${DEFAULTDIR}/${APPNAME} ${BINDIR}/logout-manager + + # absolute minimum directories to remove + #${rmbin} -rf ${APPDIR} ${SYSCONFDIR}/${APPNAME} ${DOCDIR} + + # remove all installed directories that are now blank. + rmdir ${DEFAULTDIR} 2>/dev/null ; for word in $$( ${findbin} ${SRCDIR} -mindepth 1 -type d -printf '%p\n' | ${sedbin} -r -e "s:^${SRCDIR}:${DESTDIR}:" | ${awkbin} '{ print length, $$0 }' | sort -rn | ${awkbin} '{print $$2}' ) ; do ${findbin} $${word} -mindepth 1 1>/dev/null 2>&1 | read 1>/dev/null 2>&1 || { rmdir "$${word}" 2>/dev/null || ${truebin} ; } ; done + +clean: + -${echobin} "target $@ not implemented yet! Gotta say unh." diff --git a/src/etc/default/logout-manager b/src/etc/default/logout-manager deleted file mode 100644 index 1f4ecf6..0000000 --- a/src/etc/default/logout-manager +++ /dev/null @@ -1,2 +0,0 @@ -LOGOUT_MANAGER_LIBPATH=/usr/share/logout-manager -LOGOUT_MANAGER_CONF=/etc/logout-manager.conf diff --git a/src/etc/sysconfig/logout-manager b/src/etc/sysconfig/logout-manager new file mode 100644 index 0000000..1f4ecf6 --- /dev/null +++ b/src/etc/sysconfig/logout-manager @@ -0,0 +1,2 @@ +LOGOUT_MANAGER_LIBPATH=/usr/share/logout-manager +LOGOUT_MANAGER_CONF=/etc/logout-manager.conf diff --git a/src/usr/share/applications/logout-manager.desktop b/src/usr/share/applications/logout-manager.desktop new file mode 100644 index 0000000..4522a66 --- /dev/null +++ b/src/usr/share/applications/logout-manager.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Categories=Settings;HardwareSettings; +Comment=Prompt for common actions including lock screen, logout, etc. +Exec=/usr/bin/logout-manager +GenericName=Logout menu +Icon=system-log-out +Keywords=shutdown;hibernate;lockscreen;logout;reboot; +Name=Logout... +StartupNotify=true +Terminal=false +Type=Application +Version=1.0 diff --git a/src/usr/share/doc/logout-manager/README.md b/src/usr/share/doc/logout-manager/README.md new file mode 100644 index 0000000..baa6aeb --- /dev/null +++ b/src/usr/share/doc/logout-manager/README.md @@ -0,0 +1,38 @@ +# README for logout-manager +## Introduction +Logout Manager is a python3 utility that provides a simple menu for logout-type actions. The supported actions are presented: + * Lock + * Logout + * Hibernate (if supported by hardware) + * Shutdown + * Reboot + +## Alternatives +[oblogout](https://launchpad.net/oblogout) looks really old so I did not investigate personally, but it sounds like it does the same thing I am trying to do. +`apt-cache search logout` shows [lxsession-logout](http://manpages.ubuntu.com/manpages/precise/en/man1/lxsession-logout.1.html) which was compiled, as well as does not provide configurable options for changing executed commands or icons. + +## License +[logout-manager-ncurses.py](src/usr/bin/logout-manager-ncurses.py) is licensed under the [MIT license](http://choosealicense.com/licenses/mit) and is derived almost entirely from [adamlamers](http://adamlamers.com/post/FTPD9KNRA8CT). +Everything else is licensed under [CC-BY-SA 4.0](https://choosealicense.com/licenses/cc-by-sa-4.0/). + +## Description +This project is partially a programming playground for the [original author](https://bgstack15.wordpress.com) and partially a useful project for his migration to [Fluxbox](http://fluxbox.org/) on the desktop. + +## Upsides +* This project is the first to [demonstrate SVG images in tkinter in python3](https://bgstack15.wordpress.com/2019/07/13/display-svg-in-tkinter-python3/) that I could find on the Internet. +* I have learned how to work with ncurses, gtk, and tcl in python3. +* This will make Fluxbox systems easier to use for general users. + +## Downsides +This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. + +## Improve +* add makefile + * add the standard 'list' option + * add a `list_dependencies` option or similar, which does the grep command + + grep -h -A5 -riIE dependencies * | awk 'tolower($0) ~ /devuan/ {$1="";$2="";print}' | sort | uniq | xargs + +* add libexec/ helper scripts for actually running commands +* add debian/ dir + * and dsc file -- cgit From d118309b08c28c7ef7656828fe092d6c85a2e344 Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 21:18:03 -0400 Subject: WIP: add lm-helper lm-helper has most functionality there. needs work on logout, and bash_completion function. --- src/etc/logout-manager.conf | 10 +++--- src/usr/libexec/logout-manager/lm-helper | 37 ++++++++++++++++++++++ src/usr/share/doc/logout-manager/README.md | 9 ++++++ .../doc/logout-manager/logout-manager.conf.example | 16 ++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100755 src/usr/libexec/logout-manager/lm-helper create mode 100644 src/usr/share/doc/logout-manager/logout-manager.conf.example diff --git a/src/etc/logout-manager.conf b/src/etc/logout-manager.conf index 1a14909..c37dfdf 100644 --- a/src/etc/logout-manager.conf +++ b/src/etc/logout-manager.conf @@ -1,9 +1,9 @@ [logout-manager] -hibernate_command="printf 'disk' | sudo tee /sys/power/state" -lock_command="xscreensaver --locknow" -logout_command="logout from something" -reboot_command="sudo shutdown -r now" -shutdown_command="sudo shutdown -h now" +hibernate_command="/usr/libexec/logout-manager/lm-helper hibernate" +lock_command="/usr/libexec/logout-manager/lm-helper lock" +logout_command="/usr/libexec/logout-manager/lm-helper logout" +reboot_command="/usr/libexec/logout-manager/lm-helper reboot" +shutdown_command="/usr/libexec/logout-manager/lm-helper shutdown" [icons] size = 24 diff --git a/src/usr/libexec/logout-manager/lm-helper b/src/usr/libexec/logout-manager/lm-helper new file mode 100755 index 0000000..c5e37f1 --- /dev/null +++ b/src/usr/libexec/logout-manager/lm-helper @@ -0,0 +1,37 @@ +#!/bin/sh +case "${1}" in + help) # show this help screen + { + echo "Usage: ${0}: [command]" + echo "used by logout-manager to perform actions like reboot, lock screen, etc." + echo "" + echo "Commands:" + grep -E '^\s{3}[A-Za-z]+\)' "${0}" | tr -dc '[A-Za-z\n ]' | sed -r -e 's/\s+/ /g;' | grep -v "HIDDEN\s*$" | while read a therest ; do echo " ${a}: ${therest}" ; done + } + ;; + options) # used by bash_completion function HIDDEN + grep -E '^\s{3}[A-Za-z]+\)' "${0}" | tr -dc '[A-Za-z\n]' | grep -vE 'options|help' + ;; + lock) # lock the current screen + xscreensaver --locknow + ;; + logout) # log out the current user of the graphical session + echo "Gotta say unh! Feature not yet implemented." 1>&2 + exit 1 + ;; + hibernate) # save system state to disk and power off + # linux only + printf 'disk' | tee /sys/power/state + ;; + shutdown) # power off + shutdown -h now + ;; + reboot) # restart the system + shutdown -r now + ;; + *) # HIDE + echo "invalid choice: ${1}" 1>&2 + exit 1 + ;; +esac +: diff --git a/src/usr/share/doc/logout-manager/README.md b/src/usr/share/doc/logout-manager/README.md index baa6aeb..7b8ed3d 100644 --- a/src/usr/share/doc/logout-manager/README.md +++ b/src/usr/share/doc/logout-manager/README.md @@ -7,6 +7,15 @@ Logout Manager is a python3 utility that provides a simple menu for logout-type * Shutdown * Reboot +## Customization +The `lm-helper` logout command needs to be customized for every desktop environment. Some may need extra configurationon the window manager/desktop environment side. +### Fluxbox +For Fluxbox, you need to set a value in ~/.fluxbox/init + + session.screen0.allowRemoteActions: true + +Be aware that this is insecure. See man `fluxbox-remote(1)`. + ## Alternatives [oblogout](https://launchpad.net/oblogout) looks really old so I did not investigate personally, but it sounds like it does the same thing I am trying to do. `apt-cache search logout` shows [lxsession-logout](http://manpages.ubuntu.com/manpages/precise/en/man1/lxsession-logout.1.html) which was compiled, as well as does not provide configurable options for changing executed commands or icons. diff --git a/src/usr/share/doc/logout-manager/logout-manager.conf.example b/src/usr/share/doc/logout-manager/logout-manager.conf.example new file mode 100644 index 0000000..1a14909 --- /dev/null +++ b/src/usr/share/doc/logout-manager/logout-manager.conf.example @@ -0,0 +1,16 @@ +[logout-manager] +hibernate_command="printf 'disk' | sudo tee /sys/power/state" +lock_command="xscreensaver --locknow" +logout_command="logout from something" +reboot_command="sudo shutdown -r now" +shutdown_command="sudo shutdown -h now" + +[icons] +size = 24 +#theme = default +# use names as used by the icon theme, or give a full path here +hibernate = system-hibernate +lock = system-lock-screen +logout = system-log-out +reboot = system-reboot +shutdown = system-shutdown -- cgit From e59268441a9f46f806e74c9bc8ddc9a0b224c60d Mon Sep 17 00:00:00 2001 From: B Stack Date: Tue, 10 Mar 2020 22:05:48 -0400 Subject: add DRYRUN and bash_completion to lm-helper --- src/Makefile | 9 ++++-- src/etc/bash_completion.d/logout-manager | 12 ++++++++ src/usr/libexec/logout-manager/lm-helper | 52 +++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/etc/bash_completion.d/logout-manager diff --git a/src/Makefile b/src/Makefile index 6af4efb..9fdad77 100644 --- a/src/Makefile +++ b/src/Makefile @@ -29,6 +29,7 @@ LIBEXECDIR = $(DESTDIR)$(prefix)/libexec DOCDIR = $(SHAREDIR)/doc/$(APPNAME) APPDIR = $(SHAREDIR)/$(APPNAME) APPSDIR = $(SHAREDIR)/applications +BASHCDIR = $(SYSCONFDIR)/bash_completion.d awkbin :=$(shell which awk) cpbin :=$(shell which cp) @@ -36,12 +37,13 @@ echobin :=$(shell which echo) findbin :=$(shell which find) grepbin :=$(shell which grep) installbin :=$(shell which install) +lnbin :=$(shell which ln) rmbin :=$(shell which rm) sedbin :=$(shell which sed) sortbin :=$(shell which sort) truebin :=$(shell which true) +uniqbin :=$(shell which uniq) xargsbin :=$(shell which xargs) -lnbin :=$(shell which ln) all: ${echobin} "No compilation in this package." @@ -53,7 +55,7 @@ list: deplist: @if test -z "$(DISTRO)" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." ; exit 1 ; fi - @${grepbin} -h --exclude='Makefile' -A5 -riIE dependencies $(SRCDIR) | awk -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | sort | uniq | xargs + @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | ${sortbin} | ${uniqbin} | ${xargsbin} deplist_opts: @${echobin} "el7" 1>&2 @@ -61,8 +63,9 @@ deplist_opts: install: @${echobin} Installing files to ${DESTDIR} - ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} + ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} ${cpbin} -pr ${SRCDIR}/etc/*.* ${SYSCONFDIR} + ${cpbin} -pr ${SRCDIR}/etc/bash_completion.d/* ${BASHCDIR} ${cpbin} -pr ${SRCDIR}/etc/sysconfig/* ${DEFAULTDIR} ${cpbin} -pr ${SRCDIR}/usr/bin/* ${BINDIR} ${cpbin} -pr ${SRCDIR}/usr/share/applications/* ${APPSDIR} diff --git a/src/etc/bash_completion.d/logout-manager b/src/etc/bash_completion.d/logout-manager new file mode 100644 index 0000000..fd1267f --- /dev/null +++ b/src/etc/bash_completion.d/logout-manager @@ -0,0 +1,12 @@ +# File: /etc/bash_completion.d/logout-manager +# Reference: +# bgscripts-core: /usr/bin/bp +# man complete + +_lm_helper() { + local cur prev words cword; + _init_completion || return + COMPREPLY=($( compgen -W "$( ~/dev/logout-manager/src/usr/libexec/logout-manager/lm-helper options )" -- "$cur" )) + return 0 +} && \ +complete -F _lm_helper -o bashdefault lm-helper diff --git a/src/usr/libexec/logout-manager/lm-helper b/src/usr/libexec/logout-manager/lm-helper index c5e37f1..e791f28 100755 --- a/src/usr/libexec/logout-manager/lm-helper +++ b/src/usr/libexec/logout-manager/lm-helper @@ -1,4 +1,6 @@ #!/bin/sh +# Dependencies: +# Devuan: wmctrl case "${1}" in help) # show this help screen { @@ -10,24 +12,58 @@ case "${1}" in } ;; options) # used by bash_completion function HIDDEN - grep -E '^\s{3}[A-Za-z]+\)' "${0}" | tr -dc '[A-Za-z\n]' | grep -vE 'options|help' + grep -E '^\s{3}[A-Za-z]+\)' "${0}" | awk '{print $1}' | tr -dc '[A-Za-z\n]' | grep -vE 'options|help' ;; lock) # lock the current screen - xscreensaver --locknow + if test -z "${DRYRUN}" ; + then + xscreensaver --locknow + else + echo "xscreensaver --locknow" + fi ;; logout) # log out the current user of the graphical session - echo "Gotta say unh! Feature not yet implemented." 1>&2 - exit 1 + # determine DE/WM and act accordingly + _wm="$( wmctrl -m | awk '/Name:/{$1="";print;}' | xargs )" + case "${_wm}" in + Fluxbox) + if test -z "${DRYRUN}" ; + then + fluxbox-remote exit + else + echo "fluxbox-remote exit" + fi + ;; + *) + echo "Gotta say unh! Feature not yet implemented for \"${_wm}\"" 1>&2 + exit 1 + ;; + esac ;; hibernate) # save system state to disk and power off - # linux only - printf 'disk' | tee /sys/power/state + # this method is linux only + if test -z "${DRYRUN}" ; + then + printf 'disk' | tee /sys/power/state + else + echo "printf 'disk' | tee /sys/power/state" + fi ;; shutdown) # power off - shutdown -h now + if test -z "${DRYRUN}" ; + then + shutdown -h now + else + echo "shutdown -h now" + fi ;; reboot) # restart the system - shutdown -r now + if test -z "${DRYRUN}" ; + then + shutdown -r now + else + echo "shutdown -r now" + fi ;; *) # HIDE echo "invalid choice: ${1}" 1>&2 -- cgit From b278cce1e12a7ce9dcd49bfbab7cf2fa7e3b1d35 Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 11 Mar 2020 08:34:06 -0400 Subject: add usage of sudo --- src/Makefile | 7 +++++-- src/etc/logout-manager.conf | 6 +++--- src/etc/sudoers.d/30_logout-manager_sudo | 3 +++ src/usr/libexec/logout-manager/lm-helper | 3 ++- src/usr/share/doc/logout-manager/README.md | 7 ------- 5 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 src/etc/sudoers.d/30_logout-manager_sudo diff --git a/src/Makefile b/src/Makefile index 9fdad77..fa385cb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,6 +14,7 @@ # bgscripts Makefile # Improve: # Document: +# Includes a nice way to dynamically generate dependencies as self-reported by all the files. # Dependencies: APPNAME = logout-manager @@ -30,6 +31,7 @@ DOCDIR = $(SHAREDIR)/doc/$(APPNAME) APPDIR = $(SHAREDIR)/$(APPNAME) APPSDIR = $(SHAREDIR)/applications BASHCDIR = $(SYSCONFDIR)/bash_completion.d +SUDOERSDIR = $(SYSCONFDIR)/sudoers.d awkbin :=$(shell which awk) cpbin :=$(shell which cp) @@ -55,7 +57,7 @@ list: deplist: @if test -z "$(DISTRO)" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." ; exit 1 ; fi - @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | ${sortbin} | ${uniqbin} | ${xargsbin} + @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | awk 'BEGIN{cmd="${xargsbin} -n1"} $$0 !~ /\(/{print $$0 | cmd ; close(cmd);} $$0 ~ /\(/{print;}' | ${sortbin} | ${uniqbin} | ${xargsbin} deplist_opts: @${echobin} "el7" 1>&2 @@ -63,7 +65,7 @@ deplist_opts: install: @${echobin} Installing files to ${DESTDIR} - ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} + ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} ${SUDOERSDIR} ${cpbin} -pr ${SRCDIR}/etc/*.* ${SYSCONFDIR} ${cpbin} -pr ${SRCDIR}/etc/bash_completion.d/* ${BASHCDIR} ${cpbin} -pr ${SRCDIR}/etc/sysconfig/* ${DEFAULTDIR} @@ -71,6 +73,7 @@ install: ${cpbin} -pr ${SRCDIR}/usr/share/applications/* ${APPSDIR} ${cpbin} -pr ${SRCDIR}/usr/share/${APPNAME}/*.* ${APPDIR} ${cpbin} -pr ${SRCDIR}/usr/share/doc/${APPNAME}/* ${DOCDIR} + ${installbin} -m 0640 -t ${SUDOERSDIR} ${SRCDIR}/etc/sudoers.d/* # symlink, when alternatives is not being used ${lnbin} -s logout-manager-gtk.py ${BINDIR}/logout-manager diff --git a/src/etc/logout-manager.conf b/src/etc/logout-manager.conf index c37dfdf..51b86d1 100644 --- a/src/etc/logout-manager.conf +++ b/src/etc/logout-manager.conf @@ -1,9 +1,9 @@ [logout-manager] -hibernate_command="/usr/libexec/logout-manager/lm-helper hibernate" lock_command="/usr/libexec/logout-manager/lm-helper lock" logout_command="/usr/libexec/logout-manager/lm-helper logout" -reboot_command="/usr/libexec/logout-manager/lm-helper reboot" -shutdown_command="/usr/libexec/logout-manager/lm-helper shutdown" +hibernate_command="sudo /usr/libexec/logout-manager/lm-helper hibernate" +reboot_command="sudo /usr/libexec/logout-manager/lm-helper reboot" +shutdown_command="sudo /usr/libexec/logout-manager/lm-helper shutdown" [icons] size = 24 diff --git a/src/etc/sudoers.d/30_logout-manager_sudo b/src/etc/sudoers.d/30_logout-manager_sudo new file mode 100644 index 0000000..ba621eb --- /dev/null +++ b/src/etc/sudoers.d/30_logout-manager_sudo @@ -0,0 +1,3 @@ +# File: /etc/sudoers.d/30_logout-manager_sudo +Defaults env_keep += "DRYRUN VERBOSE" +ALL ALL = (root) NOPASSWD: /usr/libexec/logout-manager/lm-helper * diff --git a/src/usr/libexec/logout-manager/lm-helper b/src/usr/libexec/logout-manager/lm-helper index e791f28..6372827 100755 --- a/src/usr/libexec/logout-manager/lm-helper +++ b/src/usr/libexec/logout-manager/lm-helper @@ -1,6 +1,7 @@ #!/bin/sh # Dependencies: -# Devuan: wmctrl +# Devuan: wmctrl sudo +# el7: wmctrl sudo case "${1}" in help) # show this help screen { diff --git a/src/usr/share/doc/logout-manager/README.md b/src/usr/share/doc/logout-manager/README.md index 7b8ed3d..cfc3843 100644 --- a/src/usr/share/doc/logout-manager/README.md +++ b/src/usr/share/doc/logout-manager/README.md @@ -36,12 +36,5 @@ This project is partially a programming playground for the [original author](htt This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. ## Improve -* add makefile - * add the standard 'list' option - * add a `list_dependencies` option or similar, which does the grep command - - grep -h -A5 -riIE dependencies * | awk 'tolower($0) ~ /devuan/ {$1="";$2="";print}' | sort | uniq | xargs - -* add libexec/ helper scripts for actually running commands * add debian/ dir * and dsc file -- cgit From 21a2eee53ad514061b2c8d5d21ec69fec219784d Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 11 Mar 2020 10:45:48 -0400 Subject: add initial debuild stuff move bash completion to recommended one from lint improve make deplist with SEPARATOR --- .gitignore | 5 ++++ debian/README.Debian | 5 ++++ debian/changelog | 5 ++++ debian/compat | 1 + debian/control | 17 +++++++++++++ debian/copyright | 29 ++++++++++++++++++++++ debian/do-not-install | 1 + debian/logout-manager.dsc | 14 +++++++++++ debian/logout-manager.install | 0 debian/logout-manager.lintian-overrides | 4 +++ debian/patches/series | 1 + debian/rules | 18 ++++++++++++++ debian/source/format | 1 + debian/source/lintian-overrides | 2 ++ debian/source/local-options | 2 ++ debian/watch | 2 ++ src/Makefile | 6 ++--- src/etc/bash_completion.d/logout-manager | 12 --------- src/usr/bin/logout-manager-cli.py | 2 +- .../bash-completion/completions/logout-manager | 12 +++++++++ src/usr/share/doc/logout-manager/README.md | 10 ++++---- 21 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 debian/README.Debian create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/copyright create mode 100644 debian/do-not-install create mode 100644 debian/logout-manager.dsc create mode 100644 debian/logout-manager.install create mode 100644 debian/logout-manager.lintian-overrides create mode 100644 debian/patches/series create mode 100755 debian/rules create mode 100644 debian/source/format create mode 100644 debian/source/lintian-overrides create mode 100644 debian/source/local-options create mode 100644 debian/watch delete mode 100644 src/etc/bash_completion.d/logout-manager create mode 100644 src/usr/share/bash-completion/completions/logout-manager diff --git a/.gitignore b/.gitignore index 8707833..371e499 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ *.2019-* *.swp __pycache__ +*debhelper* +debian/logout-manager/ +debian/files +*.substvars +gitmessage diff --git a/debian/README.Debian b/debian/README.Debian new file mode 100644 index 0000000..810fad6 --- /dev/null +++ b/debian/README.Debian @@ -0,0 +1,5 @@ +logout-manager for Devuan + +No changes + + -- Ben Stack Wed, 11 Mar 2020 08:38:11 -0400 diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..b206280 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +logout-manager (0.0.1-1) obs; urgency=low + + * Initial release. + + -- Ben Stack Wed, 11 Mar 2020 08:38:11 -0400 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +12 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..f7edf05 --- /dev/null +++ b/debian/control @@ -0,0 +1,17 @@ +Source: logout-manager +Section: x11 +Priority: optional +Maintainer: Ben Stack +Build-Depends: debhelper (>=12~) +Standards-Version: 4.1.4 +Homepage: https://bgstack15.wordpress.com/ + +Package: logout-manager +Architecture: all +Multi-Arch: foreign +Depends: ${misc:Depends}, ${shlibs:Depends} +Description: provide simple menu for logout-type actions + Designed for minimal DEs and window managers that + lack a menu for logging out, this tool provides + such a menu. + diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..6b39eaf --- /dev/null +++ b/debian/copyright @@ -0,0 +1,29 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: logout-manager +Source: +# +# Please double check copyright with the licensecheck(1) command. + +Files: .gitignore + README.md + src/Makefile + src/etc/bash_completion.d/logout-manager + src/etc/logout-manager.conf + src/etc/sudoers.d/30_logout-manager_sudo + src/etc/sysconfig/logout-manager + src/usr/bin/logout-manager-cli.py + src/usr/bin/logout-manager-gtk.py + src/usr/bin/logout-manager-ncurses.py + src/usr/bin/logout-manager-tcl.py + src/usr/libexec/logout-manager/lm-helper + src/usr/share/applications/logout-manager.desktop + src/usr/share/doc/logout-manager/README.md + src/usr/share/doc/logout-manager/logout-manager.conf.example + src/usr/share/logout-manager/__pycache__/lmlib.cpython-37.pyc + src/usr/share/logout-manager/lmlib.py +Copyright: __NO_COPYRIGHT_NOR_LICENSE__ +License: __NO_COPYRIGHT_NOR_LICENSE__ + +#---------------------------------------------------------------------------- +# Files marked as NO_LICENSE_TEXT_FOUND may be covered by the following +# license/copyright files. diff --git a/debian/do-not-install b/debian/do-not-install new file mode 100644 index 0000000..e752d8b --- /dev/null +++ b/debian/do-not-install @@ -0,0 +1 @@ +usr/bin/logout-manager diff --git a/debian/logout-manager.dsc b/debian/logout-manager.dsc new file mode 100644 index 0000000..63ef881 --- /dev/null +++ b/debian/logout-manager.dsc @@ -0,0 +1,14 @@ +Format: 3.0 (quilt) +Source: logout-manager +Binary: logout-manager +Architecture: all +Version: 0.0.1-1 +Maintainer: Ben Stack +Homepage: https://bgstack15.wordpress.com/ +Standards-Version: 4.1.4 +Build-Depends: debhelper (>= 12~) +Package-List: + logout-manager deb x11 optional arch=all +Files: + 00000000000000000000000000000000 1 logout-manager.orig.tar.gz + 00000000000000000000000000000000 1 logout-manager.debian.tar.xz diff --git a/debian/logout-manager.install b/debian/logout-manager.install new file mode 100644 index 0000000..e69de29 diff --git a/debian/logout-manager.lintian-overrides b/debian/logout-manager.lintian-overrides new file mode 100644 index 0000000..01f15e1 --- /dev/null +++ b/debian/logout-manager.lintian-overrides @@ -0,0 +1,4 @@ +binary-without-manpage +copyright-has-url-from-dh_make-boilerplate +copyright-without-copyright-notice +script-with-language-extension diff --git a/debian/patches/series b/debian/patches/series new file mode 100644 index 0000000..4a97dfa --- /dev/null +++ b/debian/patches/series @@ -0,0 +1 @@ +# You must remove unused comment lines for the released package. diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..c1fee5d --- /dev/null +++ b/debian/rules @@ -0,0 +1,18 @@ +#!/usr/bin/make -f +# You must remove unused comment lines for the released package. +#export DH_VERBOSE = 1 +#export DEB_BUILD_MAINT_OPTIONS = hardening=+all +#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic +#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed +APPNAME=logout-manager + +%: + dh $@ --sourcedirectory=src + +override_dh_auto_install: + dh_auto_install -- prefix=/usr DEFAULTDIR='$$(DESTDIR)/etc/default' + +override_dh_gencontrol: + printf "misc:Depends=" > debian/${APPNAME}.substvars + make -C src deplist DISTRO=devuan SEPARATOR=',' | grep -vE 'make\[[0-9]' >> debian/${APPNAME}.substvars + dh_gencontrol diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/debian/source/lintian-overrides b/debian/source/lintian-overrides new file mode 100644 index 0000000..2da37ed --- /dev/null +++ b/debian/source/lintian-overrides @@ -0,0 +1,2 @@ +file-without-copyright-information +missing-license-paragraph-in-dep5-copyright diff --git a/debian/source/local-options b/debian/source/local-options new file mode 100644 index 0000000..00131ee --- /dev/null +++ b/debian/source/local-options @@ -0,0 +1,2 @@ +#abort-on-upstream-changes +#unapply-patches diff --git a/debian/watch b/debian/watch new file mode 100644 index 0000000..fc70498 --- /dev/null +++ b/debian/watch @@ -0,0 +1,2 @@ +# You must remove unused comment lines for the released package. +version=4 diff --git a/src/Makefile b/src/Makefile index fa385cb..1ef600d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -30,7 +30,7 @@ LIBEXECDIR = $(DESTDIR)$(prefix)/libexec DOCDIR = $(SHAREDIR)/doc/$(APPNAME) APPDIR = $(SHAREDIR)/$(APPNAME) APPSDIR = $(SHAREDIR)/applications -BASHCDIR = $(SYSCONFDIR)/bash_completion.d +BASHCDIR = $(SHAREDIR)/bash-completion/completions SUDOERSDIR = $(SYSCONFDIR)/sudoers.d awkbin :=$(shell which awk) @@ -57,7 +57,7 @@ list: deplist: @if test -z "$(DISTRO)" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." ; exit 1 ; fi - @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | awk 'BEGIN{cmd="${xargsbin} -n1"} $$0 !~ /\(/{print $$0 | cmd ; close(cmd);} $$0 ~ /\(/{print;}' | ${sortbin} | ${uniqbin} | ${xargsbin} + @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | awk 'BEGIN{cmd="${xargsbin} -n1"} $$0 !~ /\(/{print $$0 | cmd ; close(cmd);} $$0 ~ /\(/{print;}' | ${sortbin} | ${uniqbin} | ${sedbin} -r -e 's/$$/$(SEPARATOR)/' | ${xargsbin} deplist_opts: @${echobin} "el7" 1>&2 @@ -67,12 +67,12 @@ install: @${echobin} Installing files to ${DESTDIR} ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} ${SUDOERSDIR} ${cpbin} -pr ${SRCDIR}/etc/*.* ${SYSCONFDIR} - ${cpbin} -pr ${SRCDIR}/etc/bash_completion.d/* ${BASHCDIR} ${cpbin} -pr ${SRCDIR}/etc/sysconfig/* ${DEFAULTDIR} ${cpbin} -pr ${SRCDIR}/usr/bin/* ${BINDIR} ${cpbin} -pr ${SRCDIR}/usr/share/applications/* ${APPSDIR} ${cpbin} -pr ${SRCDIR}/usr/share/${APPNAME}/*.* ${APPDIR} ${cpbin} -pr ${SRCDIR}/usr/share/doc/${APPNAME}/* ${DOCDIR} + ${installbin} -m 0644 -t ${BASHCDIR} ${SRCDIR}/usr/share/bash-completion/completions/* ${installbin} -m 0640 -t ${SUDOERSDIR} ${SRCDIR}/etc/sudoers.d/* # symlink, when alternatives is not being used ${lnbin} -s logout-manager-gtk.py ${BINDIR}/logout-manager diff --git a/src/etc/bash_completion.d/logout-manager b/src/etc/bash_completion.d/logout-manager deleted file mode 100644 index fd1267f..0000000 --- a/src/etc/bash_completion.d/logout-manager +++ /dev/null @@ -1,12 +0,0 @@ -# File: /etc/bash_completion.d/logout-manager -# Reference: -# bgscripts-core: /usr/bin/bp -# man complete - -_lm_helper() { - local cur prev words cword; - _init_completion || return - COMPREPLY=($( compgen -W "$( ~/dev/logout-manager/src/usr/libexec/logout-manager/lm-helper options )" -- "$cur" )) - return 0 -} && \ -complete -F _lm_helper -o bashdefault lm-helper diff --git a/src/usr/bin/logout-manager-cli.py b/src/usr/bin/logout-manager-cli.py index db754b6..64ea133 100755 --- a/src/usr/bin/logout-manager-cli.py +++ b/src/usr/bin/logout-manager-cli.py @@ -13,7 +13,7 @@ # https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string/12025554#12025554 # Improve: # Dependencies: -# Devuan: python3-dotenv +# Devuan: python3-dotenv python3 # Documentation: import os, platform, sys, argparse diff --git a/src/usr/share/bash-completion/completions/logout-manager b/src/usr/share/bash-completion/completions/logout-manager new file mode 100644 index 0000000..fd1267f --- /dev/null +++ b/src/usr/share/bash-completion/completions/logout-manager @@ -0,0 +1,12 @@ +# File: /etc/bash_completion.d/logout-manager +# Reference: +# bgscripts-core: /usr/bin/bp +# man complete + +_lm_helper() { + local cur prev words cword; + _init_completion || return + COMPREPLY=($( compgen -W "$( ~/dev/logout-manager/src/usr/libexec/logout-manager/lm-helper options )" -- "$cur" )) + return 0 +} && \ +complete -F _lm_helper -o bashdefault lm-helper diff --git a/src/usr/share/doc/logout-manager/README.md b/src/usr/share/doc/logout-manager/README.md index cfc3843..d34f02c 100644 --- a/src/usr/share/doc/logout-manager/README.md +++ b/src/usr/share/doc/logout-manager/README.md @@ -9,6 +9,7 @@ Logout Manager is a python3 utility that provides a simple menu for logout-type ## Customization The `lm-helper` logout command needs to be customized for every desktop environment. Some may need extra configurationon the window manager/desktop environment side. + ### Fluxbox For Fluxbox, you need to set a value in ~/.fluxbox/init @@ -29,12 +30,11 @@ This project is partially a programming playground for the [original author](htt ## Upsides * This project is the first to [demonstrate SVG images in tkinter in python3](https://bgstack15.wordpress.com/2019/07/13/display-svg-in-tkinter-python3/) that I could find on the Internet. +* This project demonstrates how to have the Makefile and debian/rules build a dependency list, from the Dependencies tags of the files themselves. * I have learned how to work with ncurses, gtk, and tcl in python3. * This will make Fluxbox systems easier to use for general users. +* Does not use dbus! ## Downsides -This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. - -## Improve -* add debian/ dir - * and dsc file +* This whole thing is more complex than just logging out of my user session, and selecting a logout-type action from the display manager. +* Depends on sudo instead of using native tools. -- cgit From be87206a369b09e595e498504eef1540e2c91c41 Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 11 Mar 2020 12:35:10 -0400 Subject: use alternatives, and actually run commands --- debian/logout-manager.postinst | 9 +++++++++ debian/logout-manager.prerm | 11 +++++++++++ src/Makefile | 5 ++++- src/usr/share/logout-manager/lmlib.py | 21 +++++++++++++++------ 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 debian/logout-manager.postinst create mode 100644 debian/logout-manager.prerm diff --git a/debian/logout-manager.postinst b/debian/logout-manager.postinst new file mode 100644 index 0000000..b98d697 --- /dev/null +++ b/debian/logout-manager.postinst @@ -0,0 +1,9 @@ +#!/bin/sh -e +#DEBHELPER# +case "$1" in + configure|abort-upgrade|abort-remove|abort-deconfigure) + update-alternatives --install /usr/bin/logout-manager logout-manager /usr/bin/logout-manager-gtk.py 80 + update-alternatives --install /usr/bin/logout-manager logout-manager /usr/bin/logout-manager-tcl.py 70 + update-alternatives --install /usr/bin/logout-manager logout-manager /usr/bin/logout-manager-ncurses.py 60 + ;; +esac diff --git a/debian/logout-manager.prerm b/debian/logout-manager.prerm new file mode 100644 index 0000000..c0c50d3 --- /dev/null +++ b/debian/logout-manager.prerm @@ -0,0 +1,11 @@ +#!/bin/sh -e +#DEBHELPER# +case "$1" in + remove|deconfigure) + update-alternatives --remove logout-manager /usr/bin/logout-manager-gtk.py + update-alternatives --remove logout-manager /usr/bin/logout-manager-tcl.py + update-alternatives --remove logout-manager /usr/bin/logout-manager-ncurses.py + ;; + upgrade|failed-upgrade) + ;; +esac diff --git a/src/Makefile b/src/Makefile index 1ef600d..236f079 100644 --- a/src/Makefile +++ b/src/Makefile @@ -65,7 +65,9 @@ deplist_opts: install: @${echobin} Installing files to ${DESTDIR} - ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} ${SUDOERSDIR} + ${installbin} -d ${SYSCONFDIR} ${DEFAULTDIR} ${BINDIR} \ + ${APPSDIR} ${APPDIR} ${DOCDIR} ${BASHCDIR} ${SUDOERSDIR} \ + ${LIBEXECDIR}/${APPNAME} ${cpbin} -pr ${SRCDIR}/etc/*.* ${SYSCONFDIR} ${cpbin} -pr ${SRCDIR}/etc/sysconfig/* ${DEFAULTDIR} ${cpbin} -pr ${SRCDIR}/usr/bin/* ${BINDIR} @@ -74,6 +76,7 @@ install: ${cpbin} -pr ${SRCDIR}/usr/share/doc/${APPNAME}/* ${DOCDIR} ${installbin} -m 0644 -t ${BASHCDIR} ${SRCDIR}/usr/share/bash-completion/completions/* ${installbin} -m 0640 -t ${SUDOERSDIR} ${SRCDIR}/etc/sudoers.d/* + ${installbin} -m 0755 -t ${LIBEXECDIR}/${APPNAME} ${SRCDIR}/usr/libexec/${APPNAME}/* # symlink, when alternatives is not being used ${lnbin} -s logout-manager-gtk.py ${BINDIR}/logout-manager diff --git a/src/usr/share/logout-manager/lmlib.py b/src/usr/share/logout-manager/lmlib.py index 7c9dc1e..40ee3a0 100644 --- a/src/usr/share/logout-manager/lmlib.py +++ b/src/usr/share/logout-manager/lmlib.py @@ -13,36 +13,45 @@ # Improve: # Documentation: -import configparser, platform, os +import configparser, platform, os, subprocess logout_manager_version="2020-03-10a" class Actions: + def __take_action(command): + print(command) + command=str(command).split() + command2=[] + for i in command: + command2.append(str(i.strip('"'))) + command=command2 + subprocess.run(command) + @staticmethod def hibernate(config, event=None): #print("need to run the /sys/power/state trick, if available") - print(config.get_hibernate_command()) + Actions.__take_action(config.get_hibernate_command()) @staticmethod def lock(config, event=None): #print("please lock the screen.") - print(config.get_lock_command()) + Actions.__take_action(config.get_lock_command()) @staticmethod def logout(config, event=None): #print("please log out of current session!") - print(config.get_logout_command()) + Actions.__take_action(config.get_logout_command()) @staticmethod def reboot(config, event=None): #print("please reboot.") - print(config.get_reboot_command()) + Actions.__take_action(config.get_reboot_command()) @staticmethod def shutdown(config, event=None): #print("please shut yourself down!") - print(config.get_shutdown_command()) + Actions.__take_action(config.get_shutdown_command()) class Config: def __init__(self): -- cgit From d3c92eb58d61e7a7cb8a66e4f8cd4fcba2d27b6b Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 11 Mar 2020 13:14:40 -0400 Subject: minor fixes --- debian/logout-manager.conffiles | 2 ++ src/Makefile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 debian/logout-manager.conffiles diff --git a/debian/logout-manager.conffiles b/debian/logout-manager.conffiles new file mode 100644 index 0000000..65e8592 --- /dev/null +++ b/debian/logout-manager.conffiles @@ -0,0 +1,2 @@ +etc/logout-manager.conf +etc/default/logout-manager diff --git a/src/Makefile b/src/Makefile index 236f079..96607d2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -57,7 +57,7 @@ list: deplist: @if test -z "$(DISTRO)" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." ; exit 1 ; fi - @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | awk 'BEGIN{cmd="${xargsbin} -n1"} $$0 !~ /\(/{print $$0 | cmd ; close(cmd);} $$0 ~ /\(/{print;}' | ${sortbin} | ${uniqbin} | ${sedbin} -r -e 's/$$/$(SEPARATOR)/' | ${xargsbin} + @${grepbin} -h --exclude='Makefile' --exclude-dir='doc' -A5 -riIE dependencies $(SRCDIR) | ${awkbin} -v 'distro=$(DISTRO)' 'tolower($$0) ~ distro {$$1="";$$2="";print}' | ${awkbin} 'BEGIN{cmd="${xargsbin} -n1"} $$0 !~ /\(/{print $$0 | cmd ; close(cmd);} $$0 ~ /\(/{print;}' | ${sortbin} | ${uniqbin} | ${sedbin} -r -e 's/$$/$(SEPARATOR)/' | ${xargsbin} deplist_opts: @${echobin} "el7" 1>&2 -- cgit