3.2. Changing Attributes

In this section we will look at how to change the appearance and location of an XOSD window. We will start by changing the font so the text is more readable, before adding a shadow and changing the colour. Finally we will look at how to move the window to a new position.

The default values for an XOSD object are close to unreadable. The reason for this is that the only font that can be guaranteed to be present in X-Windows is fixed, which is close to unreadable. Chances are that you have a myriad of fonts installed on your system. To change the font the xosd_set_font function is used. We could explicitly change the font to fixed in our example program by adding the following line before the call to xosd_display.

To change the font to something other than fixed we need to know the XLFD (X Logical Font Descriptor) of the font we wish to use. A program such as xfontsel(1) becomes very useful at this point. Start xfontsel and select a font. Click the select button and paste the text into the call to the xosd_set_font function call you added before. Your call to xosd_set_font should now look something like the following.

  xosd_set_font (osd, "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-iso8859-1");

The above code makes XOSD use Helvetica Bold at 32 points, which you probably have on your system. Chose a bold sans-serif font (such as Futura, Helvetica, Arial, or Avant Garde) whenever possible as the sans-serif fonts are more legible than their serifed counterparts (such as Times, Palatino, or Lucida Bright).[3]

After making the modifications to change the font, compile and run the example program again. The text will be much larger and easier to read.

To increase readability (and because is looks good) a shadow can be added to the text. Figure 3.1 shows the example text without a shadow, while Figure 3.2 shows the same text with a shadow.

The xosd_set_shadow_offset function is used to change the number of pixels the shadow is offset to the bottom-right of the main display. (Initially the shadow-offset is zero.) A value between one and four pixels looks good. The following code adds a four-pixel shadow to our example program; place the code just after the call to xosd_set_font.

  xosd_set_shadow_offset (osd, 4);

Once again, compile and run the example program. Experiment with the size of the shadow.

The next the visual attributes that we can change is the colour of the display, which is green by default. To do this we use the xosd_set_colour function. Colours can be specified by name, or hexadecimal colour specifier.

The valid X11® colour-names are stored in the file rgb.txt; from the command line type the following to find where rgb.txt is stored.

bash$ locate rgb.txt

The colour name is passed as a string to the xosd_set_colour For example, the following code makes the text displayed in the XOSD window lawngreen, which is close to the standard colour for television on-screen displays.

  xosd_set_colour (osd, "lawngreen");

Add the above code to your program, putting it before the call to xosd_display and see the difference.

Hexadecimal colour specifiers are similar to those used in HTML. They are made up of three pairs of characters (a hex triplet) with a # at the start. Rather than explain how hexadecimal colours work, it is easier to use a GUI tool to create the triplet for you.

Start the GIMP drawing program, and double-click on the colour-swab at the bottom of the Toolbox window. The colour selector window will appear. Change the colour, using whatever method you like, select the Hex Triplet, and paste it into you code, replacing the colour name. Put a # at the start of the number and you are done! Your code will look something like the following, which makes the XOSD window use the light-purple which is often used in GNOME icons.

  xosd_set_colour (osd, "#494066");

The XOSD window can be placed in three and vertical three horizontal positions, with minor adjustments to fine-tune the placement.

There are three possible vertical positions for an XOSD window (Table 3.1). By default the window is placed at the top of the display, but xosd_set_pos function can be called to change this.

The xosd_set_pos function takes two arguments: the window to act on, and a position. The position is specified using one of the values from the xosd_pos enumeration, which are listed in Table 3.1. For example, the following code places the XOSD window at the bottom of the display.

  xosd_set_pos (osd, XOSD_bottom);

Most desktop systems have a panel at the bottom of the display. When the XOSD display covers the panel the text in the XOSD window can be hard to read. To overcome this we can move the window up slightly by calling the xosd_set_vertical_offset function that moves the window by a small number of pixels. The direction that the xosd_set_vertical_offset moves the XOSD window depends on its position: if the window is positioned at the top of the display (XOSD_top) then the window is moved down, while the window is moved up if it positioned at the bottom of the display (XOSD_bottom). In our example we will move the XOSD window up by 48 pixels, which should be enough to avoid most panels.

  xosd_set_vertical_offset (osd, 48);