Part II. Getting Started with libwnck

Use Cases

Most users of libwnck should be tools that deal heavily with window management in one way or another: tasklists and pagers are obvious examples, but tools to automatically organize windows, to track resources of windows, or to inspect what is happening on a display can also be built with this library.

Applications that just need to do some management on their own windows (like positioning one of their windows on a specific workspace) should likely not use libwnck, as the use of this library is relatively expensive in terms of resources. The internals of libwnck make sure that the library always tracks everything that is occurring on the display, mirroring various information from the X server and actively using resources to update the cached information as it changes. In concrete terms, every time something changes on the display, every application using libwnck will wake up. An application that is not dealing specifically with window management should not do this. While wnck_shutdown() can be used to mitigate the expensiveness of libwnck, it is generally not considered a proper solution.

When considering the use of libwnck, it makes sense to keep in mind the cost of the library. For example, it is possible to share this cost between various tools all dealing in one way or another with window management, by grouping them in the same process, even if from a UI perspective they all look like different applications.

Common Pitfalls

While the API provided by libwnck should be mostly straight-forward in general, a few pitfalls are often hit by users of the library.

Explicit fetching of information

At its creation, a WnckScreen object will not have fetched information from the X server. If queried immediately after its creation (via wnck_screen_get_windows() or wnck_screen_get_workspaces(), for example), the WnckScreen object will look like there are no workspaces nor windows on the screen. This information is fetched in the main event loop with an idle source, to avoid an expensive synchronous operation on startup. If no main event loop is used, or if the information is needed as soon as possible after the creation of the object, wnck_screen_force_update() can be used to explicitly fetch the information.

Lazy initialization of WnckScreen objects and signals

As mentioned above, a WnckScreen object will have no information at its creation: it is lazily initialized during a main event loop. This lazy initialization will lead to the emission of many signals by the WnckScreen object: for instance, the "window-opened" signal will be emitted for all WnckWindow objects representing existing windows during the lazy initialization. This is actually a feature that enables you to easily initialize the state of your application, with the same code you will use to update its state when new windows get opened; there is an example showing this.

Memory management

All objects provided by the Core Window Management Support are owned by libwnck and should not be referenced or unreferenced by the user. Those objects are tied to X resources, and it makes no sense to keep the objects alive when the X resources are gone; doing so could lead to errors. Therefore it is important that, when keeping in memory a pointer to such an object, the life of this object is tracked to make sure the pointer is always valid.

This memory management model is important to keep in mind for users of wnck_shutdown(), and especially for users of libwnck through introspection. With introspection, all variables pointing to objects owned by libwnck must be cleared before wnck_shutdown() as the introspection support can add some references to the objects.

For instance, the following would work in Python:

from gi.repository import Wnck

screen = Wnck.Screen.get_default()
[...]
screen = None
Wnck.Shutdown()
       

Source indication

Window management actions that are performed with libwnck are generally implemented as requests to the window manager. In order to not disturb the workflow of the user, the window manager may choose to put restrictions on various requests sent from applications. However, if those requests represent direct actions from the user, then the window manager will obey them. To indicate that the requests are the result of actions from the user, the application should set the source indication in the requests, as defined in the EWMH. The wnck_set_client_type() can be used to define the source indication.

GDK initialization

Internally, libwnck uses GDK. This means that before any call to libwnck API, GDK needs to be initialized. This can be achieved with gdk_init(), or indirectly via gtk_init().

Examples

This first example is a small utility listing all windows on the current screen. As this is all done synchronously, without using a main event loop, we use wnck_screen_force_update() to explicitly fetch the information needed for the WnckScreen object.

       #include <libwnck/libwnck.h>

int
main (int    argc,
      char **argv)
{
  WnckScreen *screen;
  WnckWindow *active_window;
  GList *window_l;

  gdk_init (&argc, &argv);

  screen = wnck_screen_get_default ();

  wnck_screen_force_update (screen);

  active_window = wnck_screen_get_active_window (screen);

  for (window_l = wnck_screen_get_windows (screen); window_l != NULL; window_l = window_l->next)
    {
      WnckWindow *window = WNCK_WINDOW (window_l->data);
      g_print ("%s%s\n", wnck_window_get_name (window),
                         window == active_window ? " (active)" : "");
    }

  return 0;
}

      

The second example is similar, except that we use a main event loop. We connect to the "window-opened" signal to print information about new WnckScreen objects. Here, we use the fact that the "window-opened" signal is emitted for all existing windows during the lazy initialization of the WnckScreen object, in order to achieve an output similar to the previous example. However, during the lazy initialization, the active window is not necessarily known yet and we cannot tell whether the opened window is the currently active one. We connect to the "active-window-changed" signal to determine the active window when this information becomes available.

       #include <libwnck/libwnck.h>

static void
on_window_opened (WnckScreen *screen,
                  WnckWindow *window,
                  gpointer    data)
{
  /* Note: when this event is emitted while screen is initialized, there is no
   * active window yet. */

  g_print ("%s\n", wnck_window_get_name (window));
}

static void
on_active_window_changed (WnckScreen *screen,
                          WnckWindow *previously_active_window,
                          gpointer    data)
{
  WnckWindow *active_window;

  active_window = wnck_screen_get_active_window (screen);

  if (active_window)
    g_print ("active: %s\n", wnck_window_get_name (active_window));
  else
    g_print ("no active window\n");
}

int
main (int    argc,
      char **argv)
{
  GMainLoop *loop;
  WnckScreen *screen;

  gdk_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);
  screen = wnck_screen_get_default ();

  g_signal_connect (screen, "window-opened",
                    G_CALLBACK (on_window_opened), NULL);
  g_signal_connect (screen, "active-window-changed",
                    G_CALLBACK (on_active_window_changed), NULL);

  g_main_loop_run (loop);

  g_main_loop_unref (loop);

  return 0;
}