Libwnck Reference ManualLibwnck Overview
libwnck is the Window Navigator Construction Kit, a library for use in writing pagers, tasklists, and more generally applications that are dealing with window management. It tries hard to respect the Extended Window Manager Hints specification (EWMH). The Inter-Client Communication Conventions Manual (ICCCM) is also a useful resource.
libwnck depends on the following libraries:
GTK+
The GTK+ GUI library.
libstartupnotification
A library which allows providing visual feedback to the user that an application or operation is being launched (typically through a "busy" cursor).
Getting Started with libwnckUse 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.
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.
Libwnck Core Window Management SupportLibwnck WidgetsLibwnck UtilitiesIndex of symbolsIndex of all symbolsIndex of deprecated symbolsIndex of new symbols in 2.2Index of new symbols in 2.4Index of new symbols in 2.6Index of new symbols in 2.8Index of new symbols in 2.10Index of new symbols in 2.12Index of new symbols in 2.14Index of new symbols in 2.16Index of new symbols in 2.20Index of new symbols in 2.22Index of new symbols in 3.0Index of new symbols in 3.4