Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Fully Dynamic fluxbox Menu

5.00/5 (1 vote)
18 Sep 2017CPOL 6.1K  
I needed the ability to generate a menu dynamically in Fluxbox for various things that change on a regular basis. … Continue reading →

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:

Bash
#!/bin/sh

# FILE: /usr/bin/fbpipemenu 

# 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.


# TO UTILIZE THIS SCRIPT:
#
#     # copy file to /usr/bin/
#     cp fbpipemenu /usr/bin/fbpipemenu
#
#     # make a symlink in the directory you wish to make into a dynamic menu
#     ln -s /usr/bin/fbpipemenu ~/.fluxbox/examplemenu/.updatemenupipe
#     
#     # update fluxbox menu file
#     [submenu] (examplemenu)
#       [include] (~/.fluxbox/examplemenu/.menupipe)
#     [end]
#
#     # update fluxbox startup file
#     # NOTE: .updatemenupipe is the script the sends the menu 
#     #       to the input side of the pipe
#     ~/.fluxbox/examplemenu/.updatemenupipe &
#     exec fluxbox

EXEC_PATH=`dirname ${0}`

if [ ! -p ${EXEC_PATH}/.menupipe ] ; then
  mkfifo ${EXEC_PATH}/.menupipe
fi

function buildmenu {

  RESULT=""
  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}) {${CURRENT_PATH}/${ITEM}}"
    fi

  done
}

while : 
do 
  # BLOCKS ON WRITE TO .menupipe
  printf "$( buildmenu ${EXEC_PATH} )\n\n" > ${EXEC_PATH}/.menupipe

  # LOG TO CALLING PROCESS STDERR
  printf "fbpipemenu: output menu ${0}" 1>&2
done

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)