fvwm-convert-2.6: Fix StartFunction handling.
[fvwm.git] / bin / fvwm-menu-xlock.in
blob9e6a4bf5bb88e8bfefa41897cbfec2399f01540a
1 #!@PERL@
3 # Copyright (c) 1999-2009 Mikhael Goikhman
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 # Filter this script to pod2man to get a man page:
20 #   pod2man -c "Fvwm Utilities" fvwm-menu-xlock | nroff -man | less -e
22 use Getopt::Long;
24 my $version = "@VERSION@";
26 my $name  = 'XLockMenu';
27 my $title = 'XLock Modes';
28 my $itemf = '%n\t(%d)';  # may contain %n, %d, %D.
29 my $icont = '';
30 my $iconi = '';
31 my $wm_icons = 0;
32 my $special_first = 0;
34 GetOptions(
35         "help|h|?"  => \&show_help,
36         "version|V" => \&show_version,
37         "name=s"    => \$name,
38         "title=s"   => \$title,
39         "item=s"    => \$itemf,
40         "icon-title=s"  => \$icont,
41         "icon-item=s"   => \$iconi,
42         "wm-icons"      => \$wm_icons,
43         "special-first" => \$special_first,
44 ) || wrong_usage();
46 if ($wm_icons) {
47         $icont ||= "";
48         $iconi ||= "menu/lock.xpm";
51 my $icont_str = $icont ? "%$icont%" : "";
52 my $iconi_str = $iconi ? "%$iconi%" : "";
53 my $params = @ARGV ? ' ' . join(' ', @ARGV) : '';
54 my $lines1 = "";  # non-special mode lines
55 my $lines2 = "";  # special mode lines
57 my $start = 0;
58 my $special = 0;
59 $itemf =~ s/\\t/\t/g;
60 open(XL, "xlock -display NONE -help 2>&1 |")
61         || die "Exec echo 'Could not run xlock'\n";
63 print "DestroyMenu $name\n";
64 print "AddToMenu $name \"$icont_str$title\" Title\n";
66 while (<XL>) {
67         chomp;
69         /where mode is one of:/ && do {
70                 $start = 1;
71                 next;
72         };
73         if ($start && $_) {
74                 my ($misc, $name, $dsc) = split(/\s+/, $_, 3);
75                 next if $name =~ /^-/;
76                 my $dsc2 = $dsc =~ /^Shows (.*)$/ ? $1 : $dsc;
77                 my $item_str = $itemf;
78                 $item_str =~ s/\\t/\t/g;
79                 &expand_width_specifier(\$item_str, 'n', $name);
80                 &expand_width_specifier(\$item_str, 'd', $dsc);
81                 &expand_width_specifier(\$item_str, 'D', $dsc2);
83                 $special = 1 if !$special && $name eq 'blank';
84                 ($special ? $lines2 : $lines1) .=
85                         qq(+ "$iconi_str$item_str" Exec xlock$params -mode $name\n);
86         }
89 close XL;
90 print $special_first
91         ? qq($lines2+ "" Nop\n$lines1)
92         : qq($lines1+ "" Nop\n$lines2);
94 exit(0);
96 # ---------------------------------------------------------------------------
98 # Substitutes all %N1*N2x in $name by properly stripped and justified $value.
99 sub expand_width_specifier (\$$$) {
100         my ($name, $char, $value) = @_;
101         $$name =~ s/%(-?\d+)?(\*(-?)(\d+))?$char/
102                 my $value = !$2 || $4 <= 3 || $4 > length($value) ? $value : $3
103                         ? "..." . substr($value, -$4 + 3, $4 - 3)
104                         : substr($value, 0, $4 - 3) . "...";
105                 $1? sprintf("%$1s", $value): $value;
106         /ge;
109 sub show_help {
110         print "A small perl script which builds xlock menu for fvwm.\n\n";
111         print "Usage: $0 [OPTIONS] [-- XLOCK-OPTIONS]\n";
112         print "Options:\n";
113         print "\t--help           show this help and exit\n";
114         print "\t--version        show the version and exit\n";
115         print "\t--name=NAME      menu name,  default is '$name'\n";
116         print "\t--title=NAME     menu title, default is '$title'\n";
117         print "\t--item=NAME      menu item format, default is '$itemf'\n";
118         print "\t--icon-title=XPM menu title icon, default is no\n";
119         print "\t--icon-item=XPM  menu item  icon, default is no\n";
120         print "\t--wm-icons       define icon names to use with wm-icons\n";
121         print "\t--special-first  put special modes first\n";
122         print "Short options are ok if not ambiguous: -h, -t.\n";
123         print "\nSome useful xlock(1) options, 'xlock -h' for more:\n";
124         print "\t-delay usecs     delay between batches of animations\n";
125         print "\t-nolock          screensaver, don't lock the display\n";
126         print "\t-inwindow        run in window as opposite to -inroot\n";
127         print "\t-sound           turn on sound if enabled\n";
128         print "\t-nice level      decrease the process priority (0 .. 19)\n";
129         exit 0;
132 sub show_version {
133         print "$version\n";
134         exit 0;
137 sub wrong_usage {
138         print STDERR "Try '$0 --help' for more information.\n";
139         exit -1;
142 __END__
144 # ---------------------------------------------------------------------------
146 =head1 NAME
148 fvwm-menu-xlock - builds xlock menu definition for fvwm
150 =head1 SYNOPSIS
152 B<fvwm-menu-xlock>
153 [ B<--help>|B<-h>|B<-?> ]
154 [ B<--version>|B<-V> ]
155 [ B<--name>|B<-n> name ]
156 [ B<--title>|B<-t> title ]
157 [ B<--item> format ]
158 [ B<--icon-title> icon ]
159 [ B<--icon-item> icon ]
160 [ B<--special-first>|B<-s> ]
161 [ -- xlock params ]
163 =head1 DESCRIPTION
165 A simple perl script which parses xlock's output to build an fvwm
166 menu definition of all xlock's modes.
168 =head1 OPTIONS
170 =over 4
172 =item B<--help>
174 show the help and exit
176 =item B<--version>
178 show the version and exit
180 =item B<--name> name
182 define menu name in the following argument.
183 Default is "XLockMenu"
185 =item B<--title> title
187 define menu title in the following argument.
188 Default is "XLock Modes".
190 =item B<--item> format
192 define menu item format in the following argument,
193 default is '%n\t(%d)'.
194 TAB can be specified as '\t', but in .fvwm2rc you should specify a double
195 backslash or a real TAB.
197 Format specifiers:
199   %n - mode name
200   %d - mode description
201   %D - mode description without "Shows " prefix if any
203 These specifiers can receive an optional integer size, positive for right
204 adjusted string or negative for left adjusted, example: %8x; and optional
205 *num or *-num, which means to leave only the first or last (if minus) num of
206 chars, the num must be greater than 3, since the striped part is replaced
207 with "...", example: %*30x. Both can be combined: %-10*-20x, this instructs to
208 get only the 20 last characters, but if the length is less then 10 - to fill
209 with up to 10 spaces on the right.
211 =item B<--icon-title> icon
213 =item B<--icon-item> icon
215 define menu icon for title and regular item accordingly.
216 Default is no menu icons (equivalent to an empty icon argument).
218 =item B<--wm-icons>
220 define icon names suitable for use with wm-icons package.
221 Currently this is equivalent to: --icon-title '' --icon-item
222 menu/lock.xpm.
224 =item B<--special-first>
226 instructs to include special modes (usually black, bomb and random) first.
228 =back
230 Option parameters can be specified either using '=' or in the next argument.
231 Short options are ok if not ambiguous: -h, -t; but be careful with
232 short options, what is now unambiguous, can became ambiguous in the next
233 versions.
235 Additional arguments (after B<-->) will be passed to xlock.
237 Please see the B<xlock>(1) man page for the xlock options.
239 =head1 USAGE
241 Add these lines to your fvwm configuration file:
243   PipeRead 'fvwm-menu-xlock --name MenuSSaver --title "Screensaver" \
244     --icon-item mini-bball.xpm --special-first -- -nice 19 -nolock'
245   PipeRead 'fvwm-menu-xlock --name MenuSLock --title "Lock Screen" \
246     --icon-item mini-rball.xpm --special-first -- -nice 19'
247   AddToMenu "Utilities" "Screensaver%mini-monitor.xpm%" Popup MenuSSaver
248   AddToMenu "Utilities" "Screenlock%mini-lock.xpm%"     Popup MenuSLock
250 =head1 AUTHORS
252 Charles K. Hines <chuck_hines@vnet.ibm.com>, initial version.
254 Mikhael Goikhman <migo@homemail.com>, from 24 Feb 1999.
256 =head1 COPYING
258 The script is distributed by the same terms as fvwm itself.
259 See GNU General Public License for details.
261 =head1 BUGS
263 Depends on the output of xlock. Will produce an empty menu if the structure
264 of the output is changed.
266 Report bugs to fvwm-bug@fvwm.org.
268 =cut
270 # ***************************************************************************