3.3. Timeouts

In our previous example we displayed the text and then removed it from the display when the program finished. In this section we will cover how to remove an XOSD window from the display without destroying it or stopping the program. We begin by explicitly hiding the display (Section 3.3.1, “Explicitly Hiding a Display”) before covering the XOSD timeout mechanism (Section 3.3.3, “Using Timeouts”).

The xosd_hide is used to explicitly make an XOSD window invisible (unmapped in X11 terminology). It is a simple function to call, as it only takes the XOSD window to act on as an argument. For example we could replace the last two function calls in our previous example with the following code that hides the window, prints out some text, and waits for three seconds before exiting.

  xosd_hide (osd);
  printf ("Up to here!\n");
  sleep (3);
  xosd_destroy (osd);
  exit (0);

To redisplay a window that has been hidden we use the xosd_show function. To illustrate how xosd_show and xosd_hide can work together we will create a function that makes an XOSD window blink (Example 3.2). The function takes three arguments: the XOSD window, the number of times to blink, and the delay between blinks. The body of the function goes through a for-loop showing and hiding the display before exiting.

For the function listed in Example 3.2 to work correctly xosd_display must be called first. It is left as an exercise to the reader to rewrite the function so it takes the string to display as an argument and calls xosd_display on its own accord.

XOSD windows can automatically hide themselves, rather than the controlling program explicitly calling the xosd_hide function. To do this, set the windows timeout value by calling the xosd_set_timeout function. For example, the following causes the display to be hidden two seconds before the sleep function returns.

  xosd_set_timeout(osd, 6);
  xosd_display (osd, 0, XOSD_string, "You have been R00ted");
  sleep (8);

You may want to display some information, perform a few functions, then ensure the XOSD window is hidden before carrying on. The xosd_wait_until_no_display function can be used for just this purpose. It causes execution of the calling program to be suspended until the window has been removed from the display. For example, the following code displays some text, waits for two seconds, prints some data out, and then waits for the display to become hidden.

  xosd_set_timeout(osd, 6);
  xosd_display (osd, 0, XOSD_string, "R00ting Machine");
  sleep(2);
  fprintf(stderr, "Hacking away...\n");
  fprintf(stderr, "Just assume this can take a\n");
  fprintf(stderr, "variable amount of time.\n");
  xosd_wait_until_no_display (osd);