c++-gtk-utils
prog_present.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 and 2011 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 */
24 
25 #ifndef CGU_PROG_PRESENT_H
26 #define CGU_PROG_PRESENT_H
27 
28 #include <glib.h>
29 
31 
32 namespace Cgu {
33 
34 /**
35  * @defgroup prog_present prog_present
36  * @anchor prog_presenterAnchor
37  *
38  * \#include <c++-gtk-utils/prog_present.h>
39  *
40  * The c++-gtk-utils library provides functions to make a single
41  * instance program. The functionality for a single instance is
42  * provided by gio (if glib >= 2.26 is installed) or dbus-glib, but
43  * the interface in this file comprises only two functions,
44  * register_prog() and present_instance(). register_prog() returns
45  * 'true' if it is the first program instance to run in a particular
46  * user session, otherwise 'false'. If false is returned,
47  * present_instance() is then called to bring up the program instance
48  * already running. register_prog() is passed a function to carry out
49  * the program presentation.
50  *
51  * Typical usage would be, for example:
52  *
53  * @code
54  * GtkWidget* window;
55  * gboolean present_func(void* data, const char** args) {
56  * gdk_x11_window_move_to_current_desktop(gtk_widget_get_window(window));
57  * gtk_window_present(GTK_WINDOW(window));
58  * return TRUE;
59  * }
60  *
61  * using namespace Cgu;
62  *
63  * int main(int argc, char* argv[]) {
64  * gtk_init(&argc, &argv);
65  * if (register_prog("my_prog", present_func)) {
66  * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
67  * ... go through normal business of creating GTK+ program interface ...
68  * gtk_main();
69  * }
70  * else {
71  * if (!present_instance()) gdk_notify_startup_complete();
72  * else {
73  * ... carry out a dbus failure strategy, which may mean starting
74  * ... the program anyway with appropriate warnings to the user
75  * ... that there may be multiple instances running, or may mean
76  * ... displaying a GtkMessageDialog object to the user suggesting
77  * ... that the user should check that the session message bus
78  * ... daemon is running, and then terminating
79  * }
80  * }
81  * return 0;
82  * }
83  * @endcode
84  *
85  * @b NOTE
86  *
87  * These functions are only compiled into the library if either
88  * dbus-glib >= 0.70, or glib >= 2.26 (for gio's dbus implementation),
89  * is installed. If both are installed, gio's dbus implementation is
90  * used.
91  */
92 
93 
94 
95 /**
96  * Function pointer type that present_instance() will execute via dbus
97  * and intended to cause eg the program window to show itself. It must
98  * return TRUE if successful, FALSE if not. Returning FALSE will
99  * cause present_instance() to return 2. However, generally any
100  * GDK/GTK+ errors encountered by the function represented by this
101  * function pointer are best reported by that function bringing up a
102  * dialog diagnostic rather than by returning FALSE (in other words,
103  * in most cases you will want to return TRUE, even if there has been
104  * a problem). It should not throw, as the dbus and dbus-glib
105  * implementation has C linkage and cannot handle exceptions.
106  * @param object_data NULL or persistent data, as determined by the
107  * call to register_prog(). By default it is NULL.
108  * @param instance_args NULL or a NULL terminated array of strings to
109  * be passed to the PresentFunc function on a particular invocation,
110  * as determined by present_instance(). By default it is NULL.
111  * @return TRUE if successful, FALSE if not and it is important to
112  * notify the caller of the problem.
113  * @ingroup prog_present
114  */
115 typedef gboolean(*PresentFunc)(void* object_data, const char** instance_args);
116 
117 /**
118  * register_prog() returns 'true' if this is the first instance of the
119  * program to register itself, otherwise 'false'. false will also be
120  * returned, and an error logged, if a dbus connection cannot be
121  * obtained in order to carry out program registration (in which case
122  * the subsequent call to present_instance() will return 1,
123  * representing failure). The first argument (prog_name) can be
124  * anything that contains valid xml name characters (provided that for
125  * any one program it is always the same for that program), but it is
126  * best to pass the name of the program to be invoked. The object
127  * data argument is persistent data passed to each invocation of
128  * PresentFunc for that program while it is running, so the data
129  * passed needs to exist throughout program execution (eg, allocate it
130  * on the heap or have it in the same scope as that in which main()
131  * returns). By default a NULL value is passed. It does not throw.
132  * @param prog_name An identifier name comprising valid xml
133  * characters.
134  * @param func A function pointer representing the function to be
135  * executed by present_instance() if the program is already running.
136  * @param object_data NULL or persistent data to be passed to each
137  * invocation of func. By default it is NULL.
138  * @return 'true' if this is the first instance of the program to
139  * register itself, otherwise 'false'. false will also be returned,
140  * and an error logged, if a dbus connection cannot be obtained in
141  * order to carry out program registration (in which case the
142  * subsequent call to present_instance() will return 1, representing
143  * failure).
144  * @ingroup prog_present
145  */
146 bool register_prog(const char* prog_name, PresentFunc func, void* object_data = 0);
147 
148 /**
149  * present_instance() is to be called if register_prog() returns
150  * false. Returns 0 on success, 1 if there has been a dbus error or 2
151  * if the PresentFunc callback registered by register_prog() has
152  * returned FALSE. The instance_args argument is passed to
153  * PresentFunc on this invocation, and is either NULL (the default
154  * argument), or a NULL terminated array of strings, and in most cases
155  * you won't need it so NULL is appropriate. It is a blocking call
156  * (it will wait for the PresentFunc function to complete). It does
157  * not throw.
158  * @param instance_args NULL or a NULL terminated array of strings to
159  * be passed to the PresentFunc function on this invocation. By
160  * default it is NULL.
161  * @return 0 on success, 1 on failure (say, because a dbus connection
162  * could not be obtained in order to carry out program registration)
163  * or 2 if the PresentFunc callback registered by register_prog()
164  * returns FALSE.
165  * @ingroup prog_present
166  */
167 int present_instance(const char** instance_args = 0);
168 
169 } // namespace Cgu
170 
171 #endif
Cgu
Definition: application.h:44
Cgu::register_prog
bool register_prog(const char *prog_name, PresentFunc func, void *object_data=0)
Cgu::present_instance
int present_instance(const char **instance_args=0)
Cgu::PresentFunc
gboolean(* PresentFunc)(void *object_data, const char **instance_args)
Definition: prog_present.h:115
cgu_config.h