As Linux has become more popular, distributors have tried to put a more friendly face on the underlying complexity of the operating system. Most vendors offer configuration tools that guide users through the process of editing config files so they don't have to do it by hand. Mandrake Linux has a reputation for providing a number of easy-to-use configuration tools that make crossing over to Linux from Windows or MacOS relatively easy.
An interesting feature of the Mandrake configuration tools (also known as drak tools) is that many of them can be used in either console mode or in X (the Linux GUI). One of the most useful drak tools is libDrakx, which is the Mandrake add-on to perl-GTK. Part of the drakxtools-newt package, libDrakx offers some custom commands and behaves correctly in console or X. It includes both the core set of Mandrake's configuration tools and the includes you need to build a new tool. I will walk you through how you can use libDrakx to create your own user interfaces.
Overview of libDrakx
A standard Mandrake install includes drakxtools-newt, which offers a good portion of the underlying code needed to create the dual interface. A number of common routines used by the Mandrake developers come from perl-MDK-Common. The X GUI functionality is provided by perl-GTK, while the console display is handled by libnewt. If you've been involved with Linux for any length of time and remember the old Slackware dialog-based installer, you'll find that the newt interface is similar. The Mandrake installer uses this same toolset to offer both a GUI and text-based installer.
A simple example
Let's start with something basic. The application in Listing A displays a prompt and an OK button, which closes the program. As you can see in Figure A and Figure B, the user is presented with essentially the same interface, whether at the console or using the GUI.
Looks simple enough. But how does this work? Let's walk through the code, line by line.
- · Line 1: This is a standard Perl script header.
- · Line 3: We tell Perl to add /usr/lib/libDrakx to the library path.
- · Line 5: interactive pulls in the core of the drak toolkit. We'll cover this in more detail later.
- · Line 6: Again, this is standard Perl; strict just enforces strict variable, reference, and subroutine rules when your code is passed to the Perl interpreter.
- · Line 8: We're creating a new user interface.
- · Line 10: ask_warn is a simple message with an OK button. The first argument is the window title, and the second is the displayed message. The "_(" format is not required, but MandrakeSoft uses it to facilitate integrating language translation files so the text is displayed in the user's native language based on the system locale configuration.
- · Line 12: This just closes out the UI.
The $DISPLAY variable in the user’s environment determines the graphics mode. If you were to execute the above code while in X, with DISPLAY=:0, you would see the GUI version. If you unset the DISPLAY variable (unset DISPLAY), you would see the text-based version. The decision on how to proceed takes place in /usr/lib/libDrakx/interactive.pm, as shown in Listing B.
You'll see other code in interactive.pm that checks to see whether the app requires root user privileges and if so, asks for the root password. The code in Listing B illustrates how either interactive_gtk or interactive_newt will be used, based on the $DISPLAY variable and a test for the X environment.
That's all there is to it. If you want to display more text than a simple phrase, say, the contents of a file, you can use the Mandrake toolkit's simple function cat_. To use it, you'll want to include common.pm by adding use common to your code. If you take a look at /usr/lib/libDrakx/common.pm, you'll see a number of useful shortcut functions that will save you some coding work. This time, instead of passing the literal strings to ask_warn, I'll try variables. Look at Listing C.
You can use a number of other standard widgets beyond a simple OK box. First, let's allow the user to make a decision without changing the code base. Now, the application will display OK or Cancel and proceed based on the user's choice. Figure C shows the GUI version, and Figure D presents the console version. To see the code, check out Listing D.
A more complex configuration tool: The list
Lists are also useful for configuration tools, and these too can be done quite easily. The ask_from_list function expects a title, a prompt, a list of selections, and the default value. Listing E shows how you can provide a popup ToolTip. (The ToolTip doesn't work in text mode.)
A more elaborate list might involve a tree or outline type selection. Listing F illustrates this technique.
In Listing F, you declare the pipe symbol [|] as the separator in argument three of ask_from_treelist, and you declare Fruit|Apple as the default, as well as a None option. In the GUI display, the pipe symbol parses the tree display. On the console, the string is displayed as is, without the visual tree effect, as shown in Figures E and F, respectively.
GUI or text interface?
Combinations of the above widgets, along with the other interfaces in interactive.pm, should be enough to allow you to create a user interface to cover most tasks. The text interface is not quite as sophisticated as the GUI. If you can keep the interface simple, being able to run in both the console and GUI is a definite plus. MandrakeSoft's toolkit is licensed under the GPL, and you are free to use and adapt the code even if you don't use Mandrake Linux.