From 923f6fb7b25dabbe83d846acea09bb68e021fc1a Mon Sep 17 00:00:00 2001 From: Vincent Untz Date: Fri, 18 Feb 2011 18:11:24 +0100 Subject: doc: Add some high-level documentation This includes hints as to when to use the library, common pitfalls and two simple examples. https://bugzilla.gnome.org/show_bug.cgi?id=344137 --- doc/Makefile.am | 2 +- doc/example-force-update.c | 27 ++++++++++ doc/example-lazy-initialization.c | 51 ++++++++++++++++++ doc/libwnck-docs.sgml | 106 ++++++++++++++++++++++++++++++++------ 4 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 doc/example-force-update.c create mode 100644 doc/example-lazy-initialization.c (limited to 'doc') diff --git a/doc/Makefile.am b/doc/Makefile.am index 54cf3d0..57992bf 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -35,7 +35,7 @@ EXTRA_HFILES = HTML_IMAGES = # Extra SGML files that are included by $(DOC_MAIN_SGML_FILE) -content_files = +content_files = example-force-update.c example-lazy-initialization.c # Other files to distribute extra_files = diff --git a/doc/example-force-update.c b/doc/example-force-update.c new file mode 100644 index 0000000..2d54d14 --- /dev/null +++ b/doc/example-force-update.c @@ -0,0 +1,27 @@ +#include + +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; +} diff --git a/doc/example-lazy-initialization.c b/doc/example-lazy-initialization.c new file mode 100644 index 0000000..dca0598 --- /dev/null +++ b/doc/example-lazy-initialization.c @@ -0,0 +1,51 @@ +#include + +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; +} diff --git a/doc/libwnck-docs.sgml b/doc/libwnck-docs.sgml index 07aa26b..daf1c50 100644 --- a/doc/libwnck-docs.sgml +++ b/doc/libwnck-docs.sgml @@ -7,12 +7,13 @@ Libwnck Reference Manual - + Libwnck 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: @@ -32,21 +33,94 @@ - + + + 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 termes, 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. + + + + 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. + + + + + 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 Support -- cgit v1.2.1