「FvwmScript」- 用于创建图形用户界面的工具

模块FvwmScript允许您构建许多图形应用程序,如桌面附件、带弹出菜单的按钮面板、模式对话框……

在启动时,FvwmScript读取命令行中指定的文件(Module FvwmScript path/to/file)。该文件包含脚本,并且此脚本未包含在Fvwm的配置文件中。

使用键盘可以完全控制FvwmScript脚本。 (Shift)-Tab环绕在小部件周围;Return模拟鼠标单击;箭头可以移动光标或更改小部件的值;Escape“取消”菜单和PopupMenu。

调用模块

可以通过在.fvwm2rc文件中插入Module FvwmScript name_of_script行来调用FvwmScript。文件“name_of_script”可以以斜杠开头,在这种情况下,它是一个绝对路径,并且读取该文件。如果“name_of_script”不是以斜杠开头,则FvwmScript将在几个不同的位置查找。如果.fvwm2rc包含命令行*FvwmScript:Path path_of_the_script_directory,则FvwmScript将尝试在该目录中查找。如果这不起作用,FvwmScript将尝试系统配置目录和用户配置目录,如fvwm手册页中的“Read”命令所述。

如果要在fvwm初始化期间生成FvwmScript,则可以将启动FvwmScript的命令单独放置在一行上,或者可以绑定到菜单或鼠标按钮或按键,以稍后调用它。

配置选项

可以在配置文件中使用以下命令(有关详细信息,请参阅fvwm(1),MODULE COMMANDS部分)。仅当脚本中未使用相应的脚本命令时,才使用它们,即这些命令不能和脚本混在一起

*FvwmScript: DefaultFont font
指定要使用的默认字体。 如果未使用此命令或带有Font命令的脚本指定,则假定使用fixed字体。

*FvwmScript: DefaultFore color
指定要使用的默认前景色。 如果未使用此命令或使用ForeColor命令在脚本中指定,则使用黑色。

*FvwmScript: DefaultBack color
指定要使用的默认背景颜色。 如果未使用此命令或使用BackColor命令在脚本中指定,则使用grey85。

*FvwmScript: DefaultHilight color
指定要使用的默认高亮颜色。 如果未使用此命令或在带有HilightColor命令的脚本中指定,则使用grey100。

*FvwmScript: DefaultShadow color
指定要使用的默认阴影颜色。 如果未使用此命令或使用ShadowColor命令在脚本中指定,则使用grey55。

*FvwmScript: DefaultColorset colorset
告诉模块使用颜色集colorset作为默认颜色集。

脚本结构

FvwmScript使用特定的编程语言。脚本由五部分组成:

  • 第一部分:标题包含窗口的一般特征,以及所有窗口小部件的默认属性。
  • 第二部分:包含在脚本启动时执行的指令。
  • 第三部分:包含每秒执行的周期性任务。
  • 第四部分:包含在退出时执行的函数指令。
  • 第五部分:包含小部件的描述,决定了小部件中显示的内容。

小部件由11种类型的项组成:text labels, single-line text inputs, radio buttons, checkbox, push buttons, horizontal and vertical scrollbars, rectangles, pop up menus, swallowexecs, mini scrollbars。

第一部分:脚本标题

语法如下:

WindowTitle {string}
此选项设置窗口标题。

WindowSize width height
此选项设置窗口大小。参数width和height为数值。

WindowPosition x y
该选项设置窗口位置。参数x和y是数值。

ForeColor {color}
设置所有控件的默认前景色。

BackColor {color}
设置所有控件的背景色。

HilightColor {color}
设置所有控件的默认高亮颜色。

ShadowColor {color}
设置所有控件的默认阴影颜色。

Colorset {n}
设置所有控件的默认颜色集。

Font {font}
设置所有控件的默认字体。

UseGettext [locale_path]
允许使用由WindowLocaleTitle,LocaleTitle,ChangeLocaleTitle指令和Gettext函数使用的gettext机制。 如果未给出参数,则使用默认的FvwmScript语言环境目录。 此目录位于语言环境fvwm安装目录下,文本域为FvwmScript(install_prefix/share/locale/*/LC_MESSAGES/FvwmScript.mo)。 您可以使用与LocalePath fvwm命令相同的方式重置此目录或添加一些目录(请参阅fvwm手册页)。该指令应放在WindowLocaleTitle指令之前。

WindowLocaleTitle string
此选项设置窗口标题,但使用使用UseGettext定义的区域设置目录。

第二部分:脚本初始化

这部分包含将在启动时执行的指令。 例如:

Init
Begin
Do “Exec cat tada.voc > /dev/dsp”

WarpPointer 1

Set $ToDo=Restart
End

这些指令用于播放声音,将指针移动到窗口小部件1,并在每次启动时将$ToDo初始化为“Restart”。

第三部分:脚本周期任务

脚本的这一部分包含每秒执行的指令。 例如:

PeriodicTasks
Begin
If (RemainderOfDiv (GetTime) 10)==0 Then
Do {Exec xcalc}
End

此示例显示如何每10秒启动xcalc程序。

第四部分:脚本退出函数

脚本的这一部分包含脚本退出时执行的指令(在Quit指令之后,或使用Close,Delete,Destroy这些fvwm命令关闭窗口时)。 例如

QuitFunc

Begin
Do {Echo bye, bye}
End

请注意,如果使用KillModule指令来关闭脚本,则不会执行依赖脚本和fvwm之间存在通信链接的某些指令或函数(例如Do命令)。 要使用fvwm命令平滑地终止脚本,请参阅COMMANDS部分。

第五部分:小部件描述

脚本的第五部分包含脚本中每个窗口小部件的描述。 每个小部件描述都有两个部分:

  • 第一部分:描述初始属性;
  • 第二部分:包含小部件接收消息时,执行的指令。

所有小部件都可以发送和接收消息。所有消息都由一个数字标识。当用户操作小部件时,消息“UserAction”被发送到小部件。

第一部分的语法

Widget id # 一个1到999之间的数字(包含)

Property
Type string

Size width height

Position x y # 必须为每个窗口小部件指定该参数。

Title { string }

LocaleTitle { string } # 可以用来代替Title,以便使用UseGettext定义的语言环境目录。

Value int

MaxValue int

MinValue int

Font string

ForeColor { color }

BackColor { color }

HilightColor { color }

ShadowColor { color }

Colorset int

Flags flagsOpt

指令Flags的flagsOpt选项是一个以空格分隔的列表,其中包含一个或多个关键字Hidden,NoReliefString,NoFocus,Left/Center/Right。关键字Hidden用于指定窗口小部件在启动时是否隐藏。NoReliefString指定是否使用浮雕绘制字符串。 NoFocus指定窗口小部件是否可以获得键盘焦点。 默认情况下,所有窗口小部件都可以获得焦点,但Rectangle、HDipstick、VDipstick除外。 此外,当您使用(Shift-)Tab快捷方式在小部件周围循环时,会跳过NoFocus小部件。Left/Center/Right指定文本位置。 这些仅适用于ItemDraw,List,Menu,PopupMenu和PushButton。 对于其他小部件,默认值为ItemDraw为Center;PushButton为Left。

第二部分的语法

Main

Case message of
SingleClic:
Begin

# list of instructions which will be

# executed when widget receives

# message “SingleClic”. This message is

# generated by the user.

End

1 :

Begin

# list of instructions which will be

# executed when widget receives

# message 1

End
End

参数

大多数命令都使用参数。 有两种参数:数字和字符串。数值参数是介于-32000和+32000之间的值。字符串总是用大括号括起来。

变量始终以字符“$”开头,并且可以包含数字和字符串。

小部件列表

支持15种小部件。

CheckBox: Display check box with a string.

Title: title of the check box.

Value: if Value is equal to 1, the box is checked else it is not.

The Size property is ignored.

HDipstick: Display a horizontal dipstick.
This widget can be used to display disk usage.

Value: specify the current value of the dipstick.

MinValue: specify the minimum value of the dipstick.

MaxValue: specify the maximum value of the dipstick.

A minimum size of 30×11 is imposed.

HScrollBar: Display an horizontal scrollbar.

Value: position of the thumb.

MaxValue: upper limit of Value.

MinValue: lower limit of Value.

The height property is ignored and a minimum width is imposed. The width should be at least the range plus 37 if all values are to be selectable e.g. a min of 0 and max of 10 has a range of 11 and therefore should have a minimum width of 48.

ItemDraw: Display an icon and/or a string.

Title: string to display.

Icon: icon to display.

MaxValue: x coordinate of the cursor.

MinValue: y coordinate of the cursor.

The size is made large enough to contain the title and/or the icon.

List: Display a list.
List lets user to choose between various options.

Value: specify which option is selected.

MinValue: First visible option.

Title: title contains options displayed in the list. The syntax is the following: {Option 1|Option 2|…|Option N}. All menus are displayed at the top of window.

A minimum height of three items is imposed and the width is made to be at least 108.

Menu: Display a menu whom lets user to choose a option.
Items of type Menu are layed out from left to right along the top of the window. The size and position properties are ignored.

Value: specify which option is selected.

Title: title contains options displayed in the menu. The syntax is the following: {Option 1|Option 2|…|Option N}.

MiniScroll: Display a very small vertical scrollbar.

Value: position of the thumb.

MaxValue: upper limit of Value.

MinValue: lower limit of Value.

The size is set to 19×34.

PopupMenu: Display a pop up menu.

Value: specify what option is selected.

Title: the title has the following syntax: {Option 1|Option 2|…|Option N}.”Option 1|Option 2|…|Option N” is the pop up menu which is displayed when pressing mouse button.

The size property is ignored.

PushButton: Display push button with an icon and/or a string.

Title: this string has the following syntax {Title of the button|Option 1|Option 2|Option3|…|Option N}. “Option 1|Option 2|…|Option N” is the pop up menu which is displayed when pressing the right button.

Icon: icon to display.

The button is made large enough to fit the icon and or label.

RadioButton: Display radio button with a string.

Title: title of the radio button.

Value: if Value is equal to 1, the box is checked else it is not.

The size property is ignored

Rectangle: Display a rectangle.
This type of widget can be used to decorate window.

SwallowExec
This type of widget causes FvwmScript to spawn an process, and capture the first window whose name or resource is equal to Title, and display it in the script window.

Title: specify the window name which be captured and displayed in the script window.

SwallowExec: specify the command line to execute to spawn the process. Modules can also be swallowed.

Value: specify the looking of the border. Possible value: -1, 0, 1.

The size is made to be at least 30×30

TextField: Display a text input field.
The text input field can be used to edit a single-line string.

Title: content of text field.

Value: position of the insert point.

MinValue: position of the end of the selection.

MaxValue: first visible character of the title

The height property is ignored, the width is made to be at least 40 pixels wider than the initial contents.

VDipstick: Display a vertical dipstick.

Value: specify the current value of the dipstick.

MinValue: specify the minimum value of the dipstick.

MaxValue: specify the maximum value of the dipstick.

The size is made to be at least 11×30.

VScrollBar: Display a vertical scrollbar.

Value: position of the thumb.

MaxValue: upper limit of Value.

MinValue: lower limit of Value.

The width property is ignored and a minimum height is imposed. The height should be at least the range plus 37 if all values are to be selectable e.g. a min of 0 and max of 10 has a range of 11 and therefore should have a minimum height of 48.

指令

Here is the description of all instructions.

HideWidget id
hide the widget numbered id.

ShowWidget id
show the widget numbered id.

ChangeValue id1 id2
Set the value of the widget numbered id1 to id2.

ChangeMaxValue id1 id2
Set the maximum value of the widget numbered id1 to id2.

ChangeMinValue id1 id2
Set the minimum value of the widget numbered id1 to id2.

ChangeTitle id1 id2
Set the title of the widget numbered id1 to id2.

ChangeWindowTitle string
Set the title of the window to string.

ChangeWindowTitleFromArg numarg
Set the title of the window to the value of the numarg-th script argument.

ChangeLocaleTitle id1 id2
As ChangeTitle but use the locale catalog(s) defined with UseGettext.

ChangeIcon id1 id2
Set the icon of the widget numbered id1 to id2.

ChangeForeColor id1 {color}
Set the foreground color of the widget numbered id1 to {color}.

ChangeBackColor id1 {color}
Set the background color of the widget numbered id1 to {color}.

ChangeColorSet id1 id2
Set the colorset of the widget numbered id1 to id2. Specifying widget 0 sets the main window colorset.

ChangePosition id1 x y
Move the widget numbered id1 to position (x,y).

ChangeSize id1 width height
Set the size of the widget numbered id1 to (width,height).

ChangeFont id1 newfont
Set the font of the widget numbered id1 to newfont.

WarpPointer id
Warp the mouse pointer into the widget numbered id.

WriteToFile filename {str1} {str2} etc
Write to the file filename the string which is the concatenation of all arguments str1, str2, etc.

Do {command args}
Execute the fvwm command inside the Do block. Any fvwm command as described in the fvwm2 man page can be used. Commands are sent from this module to the fvwm main program for processing. The length of the command and arguments can not exceed 988 characters.

Set $var={str1} {str2} etc
Concatenate all arguments to a string and set the variable $var to this string.

Quit
quit the program.

SendSignal id1 id2
Send a message numbered id2 to widget id1.

SendToScript id_script {str11} {str2} etc
Send a message to the script identified by id_script. The message is the concatenation of str1, str2…

Key Keyname Modifier id sig str1 str2 etc
Binds a keyboard key to the instruction

SendSignal id sig

and sets the “last string” to the concatenation of str1, str2… (see the LastString function). The Keyname and Modifiers fields are defined as in the fvwm Key command.

函数

All functions use arguments. Functions can return both a string and a number. The syntax is:

(function argument1 argument2 etc)

Here is the complete list of arguments:

(GetTitle id)
Return the title of the widget numbered id.

(GetValue id)
Return the current value of the widget numbered id.

(GetMinValue id)
Return the current Min value of the widget numbered id.

(GetMaxValue id)
Return the current Max value of the widget numbered id.

(GetFore id)
Return the current RGB foreground value of the widget numbered id in the hex format RRGGBB.

(GetBack id)
Return the current RGB background value of the widget numbered id in the hex format RRGGBB.

(GetHilight id)
Return the current RGB hilight value of the widget numbered id in the hex format RRGGBB.

(GetShadow id)
Return the current RGB shadow value of the widget numbered id in the hex format RRGGBB.

(GetOutput {str} int1 int2)
Executes the command str, gets the standard output and returns the word which is in the line int1 and in the position int2. If int2 is equal to -1, GetOutput returns the complete line.

(NumToHex int)
Return the hexadecimal value of int.

(HexToNum {str})
Return the decimal value of str, str must be an hexadecimal value.

(Add int1 int2)
Return the result of (int1+int2).

(Mult int1 int2)
Return the result of (int1*int2).

(Div int1 int2)
Return the result of (int1/int2).

(StrCopy {str} int1 int2)
Return the string whom is between position int1 and int2. For example, (StrCopy {Hello} 1 2) returns {He}

(LaunchScript {str})
This function launches the script named str and returns an identification number. This number is necessary to use the functions SendToScript and ReceiveFromScript. The string str contains the script name and some arguments.

(GetScriptArgument {int})
This function returns the argument script used in the function LaunchScript. If int is equal to zero, GetScriptArgument returns the name of the script.

(GetScriptFather)
This function returns the identification number of the script father.

(ReceivFromScript {int})
This function returns the message sent by the script numbered int.

(RemainderOfDiv {int1 int2}): t
This function returns the remainder of the division (int1/int2).

(GetTime)
This function returns the time in seconds.

(GetPid)
This function returns the process id of the script.

(Gettext {str})
This function return the translation of str by using the locale catalog(s) defined with UseGettext.

(SendMsgAndGet {comId} {cmd} bool)
Sends the command cmd with identifier comId to an external program ready to communicate with the script using a protocol specific to FvwmScript. If bool is 0 FvwmScript does not wait for an answer from the external program. In this case the returned value is 1 if the message can be sent to the external program and 0 if this is not the case. If bool is 1, then FvwmScript waits for an answer from the external program and the return value is this answer (a line of no more than 32000 characters). If the communication fails, the returned value is 0. See the section A COMMUNICATION PROTOCOL for a description of the communication protocol used.

(Parse {str} int)
where str must be a string of the form:
X1S1X2S2X3S3…SnXn
where the Xn are numbers containing four decimal digits and where Sn are strings of length exactly Xn. The returned value is the string Sint. If int is out of range (e.g., >n) the returned value is the empty string. If str is not of the specified form, the return value is unpredictable (but empty in the average). This function is useful to handle strings returned by the SendMsgAndGet function.

(LastString)
This function returns the “current working string” for the Key instruction and the SendString command (see the COMMANDS section). At startup this string is empty, but when a Key binding is detected (respectively, a SendString command is received), then this string is set to the string associated to the instruction (respectively, to the command).

有条件的循环

There are three kinds of conditional loops. The instruction “If-Then-Else” has the following syntax:

If $ToDo=={Open xcalc} Then
Do {Exec xcalc &} # List of instructions
Else
Begin
Do {Exec killall xcalc &} # List of instructions

Do {Exec echo xcalc killed > /dev/console}
End

The second part “Else-Begin-End” is optional. If the loop contains only one instruction, Begin and End can be omitted. The instruction “While-Do” has the following syntax:

While $i<5 Do

Begin
Set $i=(Add i 1) # List of instructions
End

Two strings can be compared with “==” and two numbers can be compared with “<“, “<=”, “==”, “>=”, “>”. The loop “For-Do-Begin-End” has the following syntax:

For $i=1 To 20 Do

Begin
Do {Exec xcalc &} # List of instructions
End

命令

The following fvwm command may be executed at any time

SendToModule ScriptName SendString id sig str

it sends to any module with alias or name which matches ScriptName the string

SendString id sig str

When an FvwmScript receives such a message it sends to the Widget id the signal numbered sig and the string str can be obtained with the LastString function. Let us give an example. Say that you have a script MyScript with the widget:

Widget 50

Property

Type PushButton

Title {Quit}



Main

Case message of

SingleClic:

Begin
Quit
End

1 :

Begin
Set $str = (LastString)

If $str == {Quit} Then

Quit

Else

ChangeTitle 33 $str
End

End

Then the command

SendToModule MyScript SendString 50 1 str

forces MyScript to exit if str is equal to “Quit” and if not it changes the title of Widget 33 to str.

This command can be used to change the window title

SendToModule ScriptName ChangeWindowTitle newTitle [oldTitle]

it causes that any module with alias or name which matches ScriptName changes its associated window title to newTitle. The optional argument oldTitle makes sense when there are several instances of the same script. It permits one to avoid changing the name of all these instances by specifying the name of the window associated to the target script (see the example below).

+ I Module FvwmScript FvwmStorageSend “/dev/hda6”

+ I Wait FvwmStorageSend

+ I SendToModule FvwmStorageSend ChangeWindowTitle HDA6

+ I Module FvwmScript FvwmStorageSend “/dev/hda1”

+ I Wait FvwmStorageSend

+ I SendToModule FvwmStorageSend ChangeWindowTitle HDA1 FvwmStorageSend

Without the FvwmStorageSend argument in the last case, the SendToModule command would have changed to HDA1 the name of both instances of FvwmStorageSend.

使用示例

You will find examples of scripts in the fvwm configuration directory.

FvwmScript-BellSetup, FvwmScript-KeyboardSetup, FvwmScript-PointerSetup and FvwmScript-ScreenSetup are a set of scripts that modify X settings. These scripts save preferences into a file named ~/.xinit-fvwmrc (If you want to use another file name, give it as the first argument of the script). If you want to load these preferences at every startup, you have to include the line “.xinit-fvwmrc” in your .xinitrc (or .xsession) file before starting fvwm.

FvwmScript-BaseConfig modifies fvwm focus and paging mouse policy, window placement, opacity and other features of the move and resize commands, snap attraction and shading animation. This script saves preferences into a file named .FvwmBaseConfig in the user’s data directory (i.e., $HOME/.fvwm or $FVWM_USERDIR if set). If you want to load these preferences at every startup you must add the line “Read .FvwmBaseConfig” in your fvwm configuration file. If you want to use another file name, give it as the first argument of the script. When you click on Ok or Apply an fvwm function that you may define named BaseConfigOkFunc or BaseConfigApplyFunc is called. This allows for reloading specific application styles that the script has destroyed (e.g., AddToFunc BaseConfigOkFunc I Read MyAppStyle).

FvwmScript-Buttons is a buttons panel which can replace FvwmButtons (this script supports popup menus and requires xload, xclock, FvwmPager, TkDesk). FvwmScript-Colorset allows you to edit your colorset. FvwmScript-Date allows you to set date and time. FvwmScript-FileBrowser is a file browser used by the other scripts. FvwmScript-Find is an elementary front-end to find. FvwmScript-Quit allows one to quit fvwm, restart fvwm or some other window manager, or shut down and reboot the computer. FvwmScript-ScreenDump is a screen dumper. FvwmScript-WidgetDemo is a pure example script. See the next section for FvwmScript-ComExample.

A COMMUNICATION PROTOCOL

FvwmScript is a weak (but simple) programming language. If you need to deal with a lot of data and/or you need to use complex algorithms you should use an external program (in perl for example) and “send” the desired information to your FvwmScript script. The first approach is to use the GetOutput function. This is simple but you should rerun your external program each time you need information from it (and this may cause performances problems). The second approach is to use the SendMsgAndGet function which extends FvwmScript by using any programming language which can deal with named pipes (fifos). We describe this solution in this section. (A third approach is to use fvwm-themes-com from the fvwm-themes package, but in fact the SendMsgAndGet method is an implementation of fvwm-themes-com inside FvwmScript and this gives better performance).

Basically, you start an “external” program (the program for short) from your FvwmScript script (the script for short). This program runs in the background and you use the SendMsgAndGet function in your script to ask questions or to give instructions to the program. The program must strictly respect a certain communication protocol. First of all there is an identifier comId for the communication, it should contain the process id of the script for a good implementation of the protocol (use the GetPid function and pass the comId via an option to the program). The protocol uses two fifos, in the fvwm user directory, named: .tmp-com-in-comId and .tmp-com-out-comId. The program should create and listen on the .tmp-com-in-comId fifo. Then, when FvwmScript executes a function of the form:

Set $answer = (SendMsgAndGet {comId} {cmd} bool)

FvwmScript writes the cmd on this fifo. This way the program can read the cmd and can execute the appropriate action (it should remove the in fifo to support multi-communications). If bool is 0, FvwmScript does not wait for an answer from the program and return 1 if the previous actions succeed and 0 if they failed (then the program should “go back” to the in fifo). If bool is 1, then FvwmScript waits (20 sec) for an answer from the program and in turn returns the answer to the script (note that bool is not passed to the program as it must know which commands need an answer). To answer, the program creates the .tmp-com-out-comId fifo and writes the answer on it. The program should wait until FvwmScript reads the answer and then it should remove the out fifo and go back to the in fifo. The answer should consist of one line of no more than 32000 characters (take a look at the Parse function to handle multiple lines as one line).

A simple way to understand this protocol and to write scripts and programs that use it is to take a look at the (not useful) example FvwmScript-ComExample and fvwm-script-ComExample.pl (that can found in the fvwm data directory). Moreover, this implementation of the protocol solves questions as: What to do if the script exits for a bad reason? What to do if the program exits for a bad reason? …etc.

注意事项

FvwmScript crashes if widgets are accessed that have not been defined.

FvwmScript must be spawned by Fvwm. It will not work from the command line.

参考文献

man 1 FvwmScript, Version 2.8-1+b1
Documentation/FvwmScript