Computer Laboratory

What are X resources ?

Every utility which opens a separate window under X has adjustable parameters known as resources which control such properties as size, colour etc. They can be set permanently by putting the required settings (in a particular well-specified syntax) into a file and then merging those parameters into a per-user resource database. The resources put into the file .Xresources in your home directory will be merged automatically when you start an X session. You can also merge resources using the xrdb command. You can see the current state of your resource database with the command

	xrdb -query

Resources can be set either by hand or by using a utility such as editres.

Altering by hand

You can find out about the resources for a given utility by looking at its man page. For example, the manual page for xclock contains a section called X DEFAULTS which contains the entry:

       analog (class Boolean)
               Specifies whether or not an analog clock should be
               used instead of a digital  one.   The  default  is
               True.

which means that there is a resource of the xclock utilty called analog which determines whether xclock displays a normal clock face or the time in words.

Resources are ordered in a hierarchy, so the next thing to do is to determine where the particular resource we are interested in fits into that hierarchy. Each object in the hierarchy is identified by a class as well as an instance name. At the top level of the hierarchy is the class and instance name of the application itself. There is a convention (which is not strictly adhered to) that the class name of the application is the same as the program name, but with the first letter capitalized (e.g. Bitmap or Emacs) although some programs that begin with the letter X also capitalize the second letter for historical reasons (e.g. XTerm, XClock).

Again, the place to look is the manual page. In the case of xclock there is a section called WIDGETS (Widgets being a common piece of X terminology meaning class in the object-oriented programming sense) which contains the entry:

In order to specify resources, it is useful to know the hierarchy of the widgets which compose xclock. In the notation below, indentation indicates hierarchical structure. The widget class name is given first, followed by the widget instance name. XClock xclock Clock clock

which means that all the resources of xclock are subclasses of the class XClock (or xclock), and that the clock resources themselves are subcomponents of the Clock (or clock) subclass. There is another, unmentioned, subclass of XClock called shellext which is not interesting. Some of this information could also be found using the xprop utility - look for the class information:

	xprop -name xclock|grep -i class

returns

	WM_CLASS(STRING) = "xclock", "XClock"

Thus the resource mentioned above, analog, may be specified as

.xclock.clock.analog

Wildcards can be used in the specification or resources, which is useful as tracing the exact hierarchy of every single resource would otherwise be daunting. If you use a . (period) to separate the classnames, you get a tight binding, which means that one class is directly above or below the other in the class hierarchy. This also has the highest precedence of the specification methods. If you use a * (asterisk), you get a so-called loose binding, and you will skip any number of classes in the hierarchy, and it will attempt to match the next possible class defined. Thus we can also specify the analog resource as

.xclock*analog

We now need to give the resource a value. For fonts or colours see Fonts and Colours. For positions and sizes (aka geometry) it is useful to find the current default size and position of an application - this can be done using xprop (use is fairly self-explanatory).

The man page above pointed out that analog is a Boolean, and that the default is "True". If we wish to make it false we put the following into our .Xresources file:

.xclock*analog: false

When we next start an X session then whenever the xclock utility is started we will get a digital version. Alternatively we could put that line into some other file, eg foo and merge it into the resources database using xrdb:

	xrdb -merge foo

and henceforth whenever the xclock utility is started we will get a digital version - until we restart our X session at which point it will revert to the default (ie it is only permanent if it is in .Xresources)

editres

Configuration of resources by hand is clearly fiddly and time consuming. There has to be an easier way - and fortunately there is - editres. Note that this may not be installed by default on your system - you may have to install it yourself - see Software installation under Unix. The best way to demonstrate its use is by example. First of all start up the utility you are interested in (eg xclock again) and then start editres:

      xclock &
      editres &

You will probably see a simple clock, and the mostly-blank window of editres. The main window is showing us the resource tree of any given window on our desktop, and upon program launch, there is none. To give it one hold down the left button on the Commands menu, and select Get Tree. In the top of the window will appear Click the mouse pointer on any toolkit client. This message is more than it seems, and it is a hint to us that not all X applications are toolkit clients (though most of the basic ones are, and the program will usually tell us if it is not in the manual page). The mouse cursor will turn to a crosshair, and wait for you to click on another window.

For this example, first click on the xclock. You will see a couple of things appear in the editres window. These are the configurable branches that the toolkit sees. Click on the bottom right one (clock). It should invert. Now select Show Resource Box from the Commands menu. You will see another window pop up, with the heading .xclock.clock.unknown.

From here you can toy with the configuration options of the main xclock window. First, click on Set Save File at the bottom, and put in a filename such as /homes/yourname/resources, to indicate that you're testing some of the resources here. Now click on foreground and put in blue next to the Enter Resource Value: prompt. Then hit Apply at the bottom. The minute notches around the clock should turn blue. Go to background and enter navajowhite (or whatever colour you prefer - see section Colours). Then apply that. As you can see, we can configure the whole window this way. But if you were to try launching another xclock, it would appear just as this one appeared before any changes - the changes have to be saved.

Click Save and then Popdown Resource Box. If you view the file you just created you will see all those resources spelled out, in correct X Toolkit format, for your convenience. But if you try launching another xclock it will be unmodified. You need to merge the changed resources into your resource database:

      xrdb -merge /homes/yourname/resources

This command merges the resources just for the current X session. To load these resources automatically on X startup you will have to add them to your .Xresources file.

Fonts

There are a number of commands which may be used to gather information about the fonts available on your display:

xfontsel
The xfontsel application provides a simple way to display the fonts known to your X server, examine samples of each, and retrieve the X Logical Font Description ("XLFD") full name for a font. Use is fairly self-explanatory.
        xfontsel -print
is particularly useful as once you have picked out a font you like the look of and hit the Quit button it will print that font name on Standard Output, from whence you can use cut and paste to insert that name into a file such as .Xresources.
xfd
The xfd utility creates a window containing the name of the font being displayed, a row of command buttons, several lines of text for displaying character metrics, and a grid containing one glyph per cell. The characters are shown in increasing order from left to right, top to bottom.
        xfd -fn fontname
where fontname is for example the name returned by xfontsel -print.
xlsfonts
This lists the fonts that match a given pattern. The wildcard character * may be used to match any sequence of characters. If no pattern is given it will tell you about all the fonts it knows. xlsfonts -l fontname will give you more information - Note that typing xlsfonts -l without a fontname will probably cause your X server to grind to a halt as it tries to gather information about all the possible fonts - don't try it !
xset +fp path
The xset utility can be used to set a number of parameters, with the +fp or fp= options it can be used to add locations to the default font search path, should you wish to add your own. Use with care ! If you are doing this you should also read the man page for mkfontdir.

Colours

Technically, under X colours are specified through a numerical definition. This will be something like
rgb:{red}/{green}/{blue} , where {colour} is a 1 to 4 digit hexadecimal number.

Examples:
red is rgb:ffff/0/0
green is rgb:0/ffff/0
blue is rgb:0/0/ffff
yellow is rgb:ffff/ffff/0
white is rgb:ffff/ffff/ffff
black is rgb:0/0/0

The rgb at the start may be missed off and the syntax may vary, eg red may be #ff0000 or #ffff00000000 or 255 0 0.

Fortunately it is more usual to use text names for colours, some of which are distinctly imaginative, eg PapayaWhip, SaddleBrown or MistyRose. There is a database listing all the colours with their numerical representation, the contents of which can be listed with the showrgb utility.