I needed the ability to generate a menu dynamically in Fluxbox for various things that change on a regular basis.
Unfortunately, there doesn’t appear to be a facility built into fluxbox to allow for this. So I spent some time and built a partially dynamic menu that updates with the click of an ‘update’ menu item at the root menu.
I was trying to come up with a good way for the menu to update on the fly. Since fluxbox simply reads a menu file (when you use the [include] function) I needed a file that when read, it returns a dynamic response and begins the process again. It’s not completely in time, but it at least refreshes on the fly. So what is a file that provides these properties?
A named pipe, or a FIFO is a file in which one process writes to the file and another process can then read from the file. The best part is that the write is blocked until the reader reads the file. Once the contents are read, the process starts over again.
And I give you:
#!/bin/sh # Copyright (c) 2017, Christopher M. Stephan # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ############################################################################### # # fbpipemenu -- # # This shell script is a nearly just-in-time dynamic menu system for fluxbox # which uses a fifo (named pipe) in the same directory for autopopulating the # menu items. # # This works because when you right click on the desktop or click the menu # button on the toolbar the [include] function inside of the menu will cause # fluxbox to read an external file (as if it were a normal text file) and # populate the menu field with the resulting data. fbpipemenu takes advantage # of how fluxbox is reading the file by replacing the file with a pipe (fifo) # connected to the output while loop at the bottom of this script. # # Process: # # 1. This script starts, and verifies its state, attempts to perform any # install or setup functions if its ran as the FILE. (and not a SYMLINK) # # RECURSIVE LOOP BEGINS # # 2. The script then if ran as a SYMLINK generates a copy of the menu from # the folder where the symlink of this file is stored. # 2. The script then attempts to write the resulting menu to the receive # end of the pipe (fifo) file. # 2. The operating system then blocks the completion of the script until the # pipe (fifo) file is read by fluxbox. (When a user opens a menu) # 3. Fluxbox then starts and it's event loop begins handling user input. # 4. The user opens a menu, which fluxbox reads the pending menu from the # transmit end of the pipe (fifo) file. # 5. Fluxbox outputs the menu. # # RECURSIVE LOOP END --- GO BACK TO 2. # # To my knolwedge, this is the closest we'll ever get to real-time without # sacrificing the minimal overhead that fluxbox provides (or submitting # patches to the core fluxbox repo to add this fuctionality. Basically the # time between when the last menu open ran and the current is the drift from # real-time as we get stuck blocking waiting to output the data. # STORAGE LOCATION FOR PRIMARY SCRIPT BASE_BIN="/usr/bin" # NAME OF EXECUTABLE EXEC_FILE="fbpipemenu" # SYMLINK NAME SYMLINK_FILE=${EXEC_FILE} # MENUITEMS FILE NAME IN DYNAMIC FOLDER MENUITEMS_FIFO="menupipe" # DEBUGGING DEBUG=false # DEFAULT ACTION ACTION='${CURRENT_PATH}/${ITEM}' ############## ACTUAL SCRIPT ################ if [ ! -L ${0} ] ; then USAGE=" USAGE: $0 [OPTIONS] PATH --install installs script to ${BASE_BIN}. --create-dynamic-folder creates a dynamic menu at filesystem path. --help generate this output --debug debug output " else USAGE=" USAGE: $0 [OPTIONS] PATH * NOTE: SYMLINK TO ${BASE_BIN}. --include-icons --action-exec uses exec call to start file (assumes executable) --action-xdg-open uses xdg-open to open file-type --help generate this output --debug debug output " fi EXEC_PATH=`dirname ${0}` # RECURSIVE MENU BUILDING STARTING WITH PATH AS ROOT OF MENU. # function buildmenu { CURRENT_PATH=${1} for ITEM in `ls ${CURRENT_PATH}` ; do if [ -d "${CURRENT_PATH}/${ITEM}" ] ; then echo "[submenu] (${ITEM})" LAST_IFS=${IFS} IFS=$'\n' for LINE in `buildmenu ${CURRENT_PATH}/${ITEM}` ; do echo " ${LINE}" done IFS=${LAST_IFS} echo "[end]" fi if [ -x "${CURRENT_PATH}/${ITEM}" ] ; then echo "[exec] (${ITEM}) {`eval echo ${ACTION}`}" fi done } if [ $# > 0 ] ; then # PROCESS ARGUMENTS.. for OPT in $@ ; do case ${OPT} in --install) INSTALL=true ;; --create-dynamic-folder) CREATE_DYNAMIC_FOLDER=true ;; --xorg-dialogs) XORG_DIALOGS=true ;; ############################################################# ## ACTION OPTIONS FOR MENU ITEMS ## --include-icons) INCLUDE_ICONS=true printf "Our appologies, --include-icons is not implemented.\n${USAGE}" 1>&2 && exit ;; --action-exec) ACTION='${CURRENT_PATH}/${ITEM}' ;; --action-xdg-open) ACTION='xdg-open ${CURRENT_PATH}/${ITEM}' ;; ## ## ############################################################# --debug) DEBUG=true ;; --help) printf "${USAGE}" exit ;; esac done fi # CHECK IF FILE IS NOT A SYMLINK. if [ ! -L ${0} ] ; then ############################################### # FILE IS REGULAR FILE, INSTALLER/CREATE MODE # ############################################### # ATTEMPT INSTALLATION if [ "${INSTALL}" == "true" ] ; then if [ `whoami` == "root" ] ; then echo -n "ATTEMPTING INSTALLATION TO ${BASE_BIN}..." COPYRESULT=`cp ${0} ${BASE_BIN}/${EXEC_FILE} 2>&1` [ $? -ne 0 ] && printf "FAILED!\nCOPY ${BASE_BIN}/${EXEC_FILE} TO ${BASE_BIN}/ COULD NOT PROCEED.\n\n${COPYRESULT}\n${USAGE}" 1>&2 && exit echo SUCCEEDED! exit else if [ "${XORG_DIALOGS}" == "true" ] ; then Xdialog --title "Install" --password --inputbox "Sudo : " 10 40 2>&1 | sudo ${0} --install else echo -n "SUDO " sudo ${0} --install fi [ $? -ne 0 ] && printf "FAILED!\nBAD SUDO?\n${USAGE}" 1>&2 && exit fi fi # CREATE DYNAMIC FOLDER if [ $# -ge 1 ] && [ "${CREATE_DYNAMIC_FOLDER}" == "true" ] ; then echo -n "CREATING DYNAMIC MENU..." DYNAMICMENU_PATH=$( eval echo "\$$#" ) DYNAMICMENU_PATH=$( dirname ${DYNAMICMENU_PATH} )/$( basename ${DYNAMICMENU_PATH} ) [ ! -d ${DYNAMICMENU_PATH} ] && printf "BAD PATH: ${DYNAMICMENU_PATH}\n${USAGE}" 1>&2 && exit echo " ${DYNAMICMENU_PATH}" if [ "${DEBUG}" == "true" ] ; then echo "DYNAMICMENU_PATH=${DYNAMICMENU_PATH}" 1>&2 echo "BASE_BIN=${BASE_BIN}" 1>&2 echo "EXEC_FILE=${EXEC_FILE}" 1>&2 fi # CREATE DIRECTORY AND LINK IF THEY DON'T EXIST. [ ! -d "${DYNAMICMENU_PATH}" ] && mkdir -p ${DYNAMICMENU_PATH}/ if [ ! -L "${DYNAMICMENU_PATH}/.${SYMLINK_FILE}" ] ; then /bin/ln -s ${BASE_BIN}/${EXEC_FILE} ${DYNAMICMENU_PATH}/.${SYMLINK_FILE} echo "Created SYMLINK .${SYMLINK_FILE} in '${DYNAMICMENU_PATH}' -> ${BASE_BIN}/${EXEC_FILE}." 1>&2 fi # CREATE PIPE (FIFO) FILE if [ ! -p ${DYNAMICMENU_PATH}/.${MENUITEMS_FIFO} ] ; then mkfifo ${DYNAMICMENU_PATH}/.${MENUITEMS_FIFO} fi printf "\nAdd the following to your fluxbox menu file where you wish to \ninclude this as a submenu or simply utilize the [include] line:\n\n" 1>&2 printf "[submenu] (${DYNAMICMENU_PATH})\n [include] (${DYNAMICMENU_PATH}/.${MENUITEMS_FIFO})\n[end]\n\n" 1>&2 fi else ########################################### # FILE IS A SYMLINK, MENU GENERATION MODE # ########################################### # BEGIN MENU GENERATION LOOP. while : do # BLOCKS ON WRITE TO .menupipe MENUFILE="$( buildmenu ${EXEC_PATH} )\n\n" printf "${MENUFILE}" > ${EXEC_PATH}/.${MENUITEMS_FIFO} if [ "${DEBUG}" == "true" ] ; then # LOG TO CALLING PROCESS STDERR printf "\nfbpipemenu: output menu ${0}\n\n" 1>&2 PREV_IFS=$IFS IFS=$'\n' for LINE in `printf "${MENUFILE}"` ; do printf " ${LINE}\n" 1>&2 done IFS=${PREV_IFS} fi done fi