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.
xosd_set_font (osd, "fixed");
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 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.
As XOSD is mainly developed in New Zealand, where English is spoken and written, the API uses English, rather than American. Therefore colour is written correctly, with a u.
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 final visual aspect of the window that we can change is the outline colour of the display, so the border of the text or graphics is different to the rest of the display. This final tweak makes the display easier to read, if you get the colours right! Generally, the outline colour is black, while the fill-colour is light. Place the following example code to your program just after the call to xosd_set_colour. The call to xosd_set_outline_offset sets the outline to be two pixels wide, while the xosd_set_outline_colour call sets the outline colour to black.
xosd_set_outline_offset(osd, 2); xosd_set_outline_colour(osd, "black");
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.
Table 3.1. Vertical Positions Declared in the xosd_pos Enumeration
XOSD_top | The top of the display. |
XOSD_middle | The middle of the display. |
XOSD_bottom | The bottom of the display |
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);
Often it looks better if text is aligned to the right of the display, rather than being shown on the left. To do this we use the xosd_set_align function to align the XOSD window to the left, centre, or right of the display. The enumeration xosd_align declares the three constants used to specify the horizontal alignment of the window (Table 3.2).
Table 3.2. Horizontal Positions Declared in the xosd_align Enumeration
XOSD_left | The left of the display. |
XOSD_center | The center of the display. |
XOSD_right | The right of the display. |
Fine control of horizontal placement is performed using the xosd_set_horizontal_offset function, which alters the number of pixels the display is offset from the left or right of the display (depending which is closer).
Add the following code to our small program, just after the call to the xosd_set_vertical_offset function, and before the call to the xosd_display function. It will move the XOSD window to the right, with a gap of 48 pixels.
xosd_set_align (osd, XOSD_right); xosd_set_horizontal_offset (osd, 48);
That is about all the functions you need to alter the appearance and location of an XOSD window. Your example C file will look similar to Example 3.1.
Example 3.1. A Simple XOSD Program
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <xosd.h> int main (int argc, char *argv[]) { xosd *osd; osd = xosd_create (2); if (osd == NULL) { perror ("Could not create \"osd\""); exit (1); } xosd_set_font (osd, "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-iso8859-1"); xosd_set_shadow_offset (osd, 2); xosd_set_colour (osd, "#494066"); xosd_set_outline_offset(osd, 2); xosd_set_outline_colour(osd, "black"); xosd_set_pos (osd, XOSD_bottom); xosd_set_vertical_offset (osd, 48); xosd_set_align (osd, XOSD_right); xosd_set_horizontal_offset (osd, 48); xosd_display (osd, 0, XOSD_string, "You have been R00ted"); sleep (8); xosd_destroy (osd); exit (0); }
[3] Serifed fonts are more readable than sans-serif fonts, so serifed fonts should be used for a long body of text (such as this paragraph) while sans-serif should be used for short bodies of text (such as a heading) because it is easier to dsitinguish each letter.