Table of Contents
This guide will give you a brief tour of the internals of the XOSD library. It will look at how XOSD is designed and the coding standards that the main authors use. To understand how XOSD works internally you will need a good knowledge of POSIX threading (pthread), and a general knowlege of how X11 works. Almost all the other libaries used in XOSD are part of ANSI-C.
All of the XOSD library is implemented in a single C-file: xosd.c — roughly 40 functions, two type definitions, and a struct. You can think of the struct, xosd, as an object that has all its atributes as private, and all the functions in xosd.c as methods; The functions that begin with xosd_ are public methods, while the other functions are private.
If you look at the xosd structure (Figure 4.1) you will see that the first five fields are concerned with threading. Each XOSD window consists of two threads. The first, event_thread, runs the function event_loop that recieves X11 events, and the second, timeout_thread, runs the function timeout_loop that hides the window when timeouts expire.[4] A mutex is used so only one thread ever modifies the fields in the xosd structure. To use the mutex, each function in xosd.c has a call to pthread_mutex_lock near the top of the function and call pthread_mutex_unlock near the end. Any code you add to xosd.c will also have to use the mutex, otherwise race conditions can occour.
The next ten fields in xosd store information about how the XOSD window is displayed. XOSD does not use any GUI toolkits, such as GTK+ or Qt, instead it calls the basic X11 functions for drawing text and graphics. The following eleven fields (from width to bar_length) store information about the window that are set by the various xosd_set_ functions.
The two boolean fields, mapped and done, have quite different functions. The mapped field is set to true if the display is shown. On the other hand, the done field is only set to true when xosd_destroy is called as it causes the event and timeout threads to exit.
Colours are controlled by the three fields: pixel, colour and Colourmap. The astute reader will now realise why xosd_set_colour changes the colour of all the text and graphics displayed in the window, and why lines in the same window cannot have different colours.
The content of the display is contained in the lines field, which is allocated when the window is created. There are five fields associated with each line:
Not all of these fields are used at any one time, as a line can only display text, a percentage bar, or a slider.
The final two filds are used to control the timeout values. The number of seconds untill the window is unmapped from the display is controlled by the timeout field, while timeout_time is to set the date when the window will be unmapped (as required by pthread_cond_timedwait in the timeout_loop thread).
[4] There are at least three threads in any program that uses libxosd as the main-program also runs in a seperate thread.