Debian Menu System - The internals of the menu package

[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ next ]

# Debian Menu System
Chapter 7 - The internals of the menu package

## 7.1 The update-menus program

On startup, update-menus checks the file `/var/run/update-menus.pid` and the pid in it. If there's an `update-menus` process with that pid it kills it. If `/var/lib/dpkg/lock` exists, it checks to see if dpkg supports triggers. If so, it uses dpkg-trigger to trigger a real update-menus run later. Otherwise, it forks to background and returns control to dpkg. The background process checks the `/var/lib/dpkg/lock` file approx. every two second until the file's gone.

Once it's decided to run, whether in the background after dpkg exits, or in the foreground when used with a trigger-capable dpkg, `update-menus` reads the menu-entry files in the following directories: `/etc/menu`, `/usr/lib/menu`, `/usr/share/menu`, `/usr/share/menu/default`. (if a user runs `update-menus`, it will add `~/.menu` to the front of that list). For every menu entry line in each file it checks if the corresponding package is installed. The menu entries of all packages marked as installed by dpkg are added together in one big buffer that is kept in memory (exception: executable menu entry files are executed, and stdout is placed in the buffer).

Once it's read all menu entry files, `update-menus` starts all executable scripts in `/etc/menu-methods/`, hands the scripts the previously created buffer via stdin. (If `update-menus` is run by a user, it will first try to run the scripts in `~/.menu-methods`, and only if that directory doesn't exist, it will run the scripts in `/etc/menu-methods`).

Note that as an aid to debugging, one can use

         update-menus --stdout > /tmp/menu-stdin

and then view the file `/tmp/menu-stdin` to see exactly what `update-menus` handed the menu-methods on their stdin.

This may also be useful for people writing `/etc/menu-method/*` scripts: Running `update-menus` every time you changed something in the script may be quite time-consuming. So, it's much easier to run `update-menus --stdout` once, and then run

           /etc/menu-methods/mymethod < /tmp/menu-stdin

(and, if that also takes too long, just try editing /tmp/menu-stdin, and removing 90% or so of all entries)

## 7.2 The install-menu program

The files `/etc/menu-methods/$wm` are executable config files that start with the line

           #!/usr/bin/install-menu

and thus start that program, handing it the configuration file for the specific window manager in the first command line argument. This configuration consists of:

1. the compatibility mode ("menu-1" or "menu-2").

2. where the various files should be stored/read.

3. what "needs" are supported, and what wrapper files should be used for each "type".

4. how to remove the generated menu files.

See `/usr/share/doc/menu/examples/` of the menu package for more comments.

Options to `install-menu`:

           --remove  Remove the menu files instead of generating them.
           --verbose Output outline of operations that are performed.

Some window managers don't support an `include'-like statement in their `system.*rc` files (like `m4` or `cpp` preprocessing); they cannot read the `menudefs.hook` file generated by install-menu from their `system.*rc` config file. To still be able to use them, `install-menu` will copy the file `$path/$examplercfile` to `$path/$rcfile` (with `$examplercfile` and `$rcfile` defined in the `install-menu` config file, and `$path` either the `$rootprefix` or `${HOME}/$userprefix`, depending on whether root or user executed the file.), and replace all occurrences of ``install-menu-defs'' with the `$genmenu` file it just generated.

As an example, consider the following: `examplercfile=system.foo-wm-example`, `rcfile=system.foo-wm`, `genmenu=menudefs.hook` and `rootprefix=/var/lib/foo-wm/menu`. Now, if `install-menu` gets run, it will first generate the file `/var/lib/foo-wm/menu/menudefs.hook`. Next, it will line-by-line read the file `/var/lib/foo-wm/menu/system.foo-wm-example` and copy its contents to `/var/lib/foo-wm/menu/system.foo-wm`, replacing every occurrence of the string `install-menu-defs` with the contents of the file `/var/lib/foo-wm/menu/menudefs.hook`.

To activate the file copying in this way, simply define the `$examplercfile` and `$rcfile` variables in the `install-menu` configuration file (for example, see `/etc/menu-methods/fvwm`), and make sure there is a `$path/$examplercfile` (`$path` being either `$rootprefix`, or `$userprefix`.)

If you are writing a menu method, you can use the following to make debugging it somewhat more easily:

1. use `update-menus --stdout > /tmp/menu-stdin` to create a list of menu entries in `/tmp/menu-stdin` and then

2. you can run just your menu-method with (if it's called wm):

              ./wm -v < /tmp/menu-stdin

## 7.3 The install-menu config script definitions

The menu-methods in `/etc/menu-methods/*` are basically made up of a lot of ``tag=string'' definitions, telling `install-menu` how to generate a `system.${wm}rc` script. This way you can tune the look of generated `system.${wm}rc` to your needs.

In the following, something like

           treewalk="c(m)"

means that the treewalk variable by default has the value "c(m)".

For examples of what these scripts can look like, see `/usr/share/doc/menu/examples/*`.

`compat="menu-1"`

  Two mode are defined:

  `"menu-1"`

    menu directives are terminated by an end-of-line character.

  `"menu-2"`

    menu directives are terminated by a semicolon character.

  This must be just after the `!include "menu.h"` directive so that `menu.h` can use its own compat mode.

`outputencoding="UTF-8"`

  Set the encoding used for output files. Use `iconv --list` to get the list of supported encoding. Useful values include "UTF-8" and "ISO-8859-1". The special value "LOCALE" means that the current locale encoding will be used. If set to an empty string, no translations are performed. This is the default.

`outputlanguage=""`

  If set to "C" automatic translations will be disabled. Note that you can still use translate() to perform explicit translation.

`supported`
`endsupported`

  Between the `supported` and `endsupported` keywords you define what "needs" are supported by this window manager. So, the following is an example for a wm that supports both needs=x11 and needs=text:

             function q($s) = "\"" esc($s,"\\\"") "\""
             supported
               x11 =" ShowEntry(" q(title()) ", " q($command) ")"
               text=" ShowEntry(" q(title()) ", " q(term())   ")"
             endsupported

  For the variable substitution (and functions, not shown above), see the next paragraph. In the above example, you'll notice that for the menu entries that "need=text", the term() function is used. This is a user-supplied function that will run $command in a X terminal emulator. Also, as X11 is higher up in the supported list above than text, a package that supplies both a "needs=X11" and a "needs=text" entry will have the needs=X11 entry installed, in favour of the needs=text entry. You can continue lines on the next line with a backslash ("\"), but make sure you don't add any spaces after the backslash.

`startmenu=""`
`endmenu=""`
`submenutitle=""`

  These define what to print for the beginning/end of a menu, and how to the print a menu entry that pops up another menu. They are substituted the same way as the "supported" stuff is; see next paragraph.

`treewalk="c(m)"`

  This string defines in what order to dump the `$startmenu`, `$endmenu`, and `$submenutitle` (and its children). Each char in the string refers to:

               c  : dump children of menu.
               m  : dump this menu's $submenutitles
               (  : dump $startmenu
               )  : dump $endmenu
               M  : dump all $submenutitles of this menu and this menu's children.

  The default is "c(m)". For olvwm, one needs: "(M)"

`genmenu=""`

  The menu file to generate (usually something like `system."$wm"rc`). The file itself may depend on the level or title that is currently being worked on, like

               genmenu="/subdir/" replacewith($section," ","_") "/rc.menu"

  (Substitution works just like in the supported stuff, see above). Note that the files made this way are truncated upon opening, so if you have a genmenu like the example above, then your `endmenu=` will override the startmenu stuff (but you probably only need one of the two anyway).

`rootsection="/Debian"`

  the prefix every `$section` variable gets.

`prerun=""`
`postrun=""`

  The commands to run before and after, respectively, the actual generation of the `menudefs.hook` (genmenu) file. Commands will be executed by `sh`. Example:

             prerun="rm -rf " prefix() "/*"
             postrun="killall -USR1 fvwm2"

  (Substitution works just like the supported stuff, see below).

`preruntest=""`

  Just like prerun, but if the return value of the command is non-zero, menu will quit.

`also_run=""`

  If non-zero, install-menus will, after generating the output files, also load the file also_run, and use the new assignments to treewalk, genmenu, etc. to generate more output. This second time, variables like `prerun` and all of the hint stuff are ignored.

`removemenu=""`

  The command to run when the menu-method is invoked with the option `--remove`. This should remove all the autogenerated menu files. If this option is not present, then install menu will remove `genmenu` if it is a constant string and `rcfile` if it is defined, and try to remove `prefix()` if it is empty.

`onlyrunasroot=false`
`onlyrunasuser=false`

  If `onlyrunasroot` is set to true, `install-menu` will quit silently when run as a user. Similarly for `onlyrunasuser`. `onlyrunasroot` is deprecated since it is simpler to just not define `userprefix`. On the other hand, `onlyrunasuser` might be needed if you use `rcfile` since `rootprefix` is used as a fallback location for the template.

`preoutput="#Automatically generated file. Do not edit (see /usr/share/doc/menu/html)\n\n"`
`postoutput=""`

  Text to put at the beginning resp. end of the generated file ($genmenu).

`command=""`

  A command to run instead of `install-menus`. This command used to be needed to get around limitations due to compatibility stuff. But that compatibility with pre menu-1 stuff has been dropped, and isn't needed any more.

  Example:

             command="cat > /tmp/menu-stdin"

`hotkeyexclude=""`

  Keys not to use for hotkey generation. You can use the same variables and functions here as in for example the startmenu sections.

  Example:

             hotkeyexclude="q" $section

`hotkeycase="insensitive"`

  can be either "insensitive" or "sensitive". Determines whether the hotkeys can be of mixed case (`fvwm2` reads the hotkeys case-insensitive, `pdmenu` case-sensitive). In case of the titles "Xa" and "xb", hotkey case-insensitive will generate "X" and "b", whereas case-sensitive would generate "X" and "x".

`sort=$sort ":" $title`

  Entries within one menu will be alphabetically sorted by whatever sort returns. So, if you do `sort=ifelse($command, "1", "0"):$title`, then all submenus will appear above the commands in a submenu. (A submenu always has `$command=""`). Or, as Joey Hess writes:

             You can add another field to the menu items, with whatever name you like,
             let's say it's called priority. Then add this line to
             /etc/menu-methods/*:

             sort=ifelse($priority, $priority, "9")

             This has the result of sorting things so items with a low priority sort to the
             top, and items with no priority default to priority 9 and sort to the bottom.

             (Note that it compares the strings alphabetically, not numerically.)

`rcfile=""`

  If the window manager doesn't support an "include filename" or "read(filename)" statement in it's config file, you can rename the wm's config file to `system."$wm"rc-menu`, and insert a "install-menu-defs" line (without the quotes, or whitespace around it, and "install-menu-defs" must be the only thing on the line) in the `system."$wm"rc-menu` file. This will then get replaced by the `$genmenu` file that was just created (see also `$examplercfile`).

`examplercfile=""`

  if needed (see `rcfile`), this is the `system.rc"$wm"-menu` file. In that case, make `rcfile=system.rc"$wm"`.

`rootprefix=""`

  The prefix to use when running as root (applies to $genmenu, $rcfile, $examplercfile and other old cache files). If it is not defined, the menu-method will be skipped when run as root.

`userprefix=""`

  As `rootprefix`, but when running as user. userprefix is relative to the user home directory, unless it start with 2 slashes, in which case it is treated as an absolute path. If it is not defined, the menu-method will be skipped when run as a user.

`hint_optimize=false`

  If set to true, menu will try to generate an `optimal' tree, using the variables below. If set to false, menu will keep the sections as they are specified in the menu entry files (and ignore any hint stuff).

`hint_nentry=6`

  Optimal number of entries in a submenu. It's a float, so you can set it to 5.5 if you cannot decide between 5 and 6. Also, values less than 3 probably don't work very well at the moment.

`hint_topnentry=5`

  Same as hint_nentry, but for the top level menu. Often here are other entries, added by the window-manager itself (like Exit, Xterm, whatever) that menu doesn't know about, so that you may want to instruct menu to put less entries in the top level menu.

`hint_mixedpenalty=15.0`

  Penalty for `mixed' menus. Mixed menus are those with both submenus and direct commands in them.

`hint_minhintfreq=0.1`

  Minimal relative frequency for the hints before they are considered. Internal variable to speed up the tree generation. If you find menu slow, increase this value (to, say 0.2 or 0.3).

`hint_mlpenalty=2000`

  `max local penalty', while evaluating the possible trees, menu gives `penalties' for submenus that don't contain the desired number of submenus. The penalty is sqrt(n_entry_opt-n_entry), and eventually will be calculated as a sum of all nodes. But to speed things up, menu will discard possibilities in which any node has a `local' penalty of more than hint_mlpenalty. Increase this value if you think menu is overlooking your favorite tree (also decrease minhintfreq), decrease this value if you think menu is wasting too much time. Because of hint_max_ntry, the influence of this variable is nearly zero nowadays.

`hint_max_ntry=4`

  menu will recursively, for each node, try the hint_max_ntry best local menu-divisions.

`hints_max_iter_hint=5`

  The search for what hints to use in one menu is rather expensive. But due to the way things are sorted, menu seems to always find the `best' match in the first 2% of iterations. Thus, a way to speed things up is simply to cut of menu searching after `some' iterations are done. This value controls this, and limits the number of iterations to 5+hint_max_iter_hint*number_of_possible_hints. Set this value to negative to disable this.

`hint_debug=false`

  Set to true if you want to see loads and loads of debug output.

## 7.4 Hints, tree optimization

The hints actually work in a rather strange way: when `hint_optimize=true` then all `$section` elements are added to the specified `$hints` variable, and the order (`/Applications/Editors` or `/Editors/Applications`) of the resulting hints is completely ignored. Then, the hints for each menu entry are handed to the optimization routine, which will calculate a reasonable tree for those hints. That tree must comply with the following:

When a user looks for a program "Program" with, say, hints "Good,Bulky,Heaven", then, while walking through the tree, it should at every node visited be clear for the user what submenu to select (or the menu should have "Program" directly in it). So, the top-level menu may look like

           Good
           Hell
           Microsoft

because then a searcher for a menu entry with hints "Good,Bulky,Heaven" will know to select the submenu "Good". The toplevel menu may not look like

           Good
           Hell
           Heaven

as now it isn't clear whether to visit the Good or the Heaven submenu.

That rule allows usually for many different trees, and the task of the optimization procedure is to select, in a finite amount of time, the tree that best matches the user's desire about the optimum number of menu entries.

[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ next ]

Debian Menu System