1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Session Management
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="Gnome Application Basics" href=
"cha-startup.html">
<link rel="PREVIOUS" title="Saving Configuration Information"
href="z79.html">
<link rel="NEXT" title="The Main Window: GnomeApp" href=
"cha-main.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z79.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="cha-main.html"><font color="#0000ff" size="2">
<b>Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1">
<a name="SEC-SESSIONMANAGEMENT">Session Management</a>
</h1>
<p>
The term <i class="FIRSTTERM">session</i> refers to a
snapshot of the state of a user's desktop: what
applications are open, where their windows are located,
what windows each application has open, what size those
windows are, what documents are open, current cursor
position, and so on. Users should be able to save their
session before logging out, and have it automatically
restored as closely as possible next time they log in. For
this to work, applications must cooperate by having the
ability to record and restore those aspects of their state
not controlled by the window manager.
</p>
<p>
A special program called the <i class="FIRSTTERM">session
manager</i> notifies applications when they should save
their state. The Gnome desktop environment comes with a
session manager called <tt class="APPLICATION">
gnome-session</tt>, but Gnome uses the X session management
specification, which is several years old. CDE uses the
same specification, and at press time, KDE was planning to
adopt it as well; an application that implements session
management via the Gnome interfaces should work on any
session-managed desktop. Gnome does implement some
extensions to the basic specification (notably, startup
"priorities") but these should not break other session
managers and will likely be implemented in KDE as well.
</p>
<p>
It's worthwhile to read the session management
documentation that comes with X; it's a good introduction
to what's going on "behind the scenes." The Gnome libraries
also come with a useful document, called <tt class=
"FILENAME">session-management.txt</tt>; have a look at it,
and the heavily-commented <tt class="FILENAME">
gnome-client.h</tt> header file, for additional details not
covered in this section.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z86">Using the <span class="STRUCTNAME">
GnomeClient</span> Object</a>
</h2>
<p>
Gnome shields you from the raw session management
interface that comes with X. This is done via a <span
class="STRUCTNAME">GtkObject</span> called <span class=
"STRUCTNAME">GnomeClient</span>. <span class=
"STRUCTNAME">GnomeClient</span> represents your
application's connection to the session manager.
</p>
<p>
Gnome manages most of the details of session management.
For most applications, you only have to respond to two
requests.
</p>
<ul>
<li>
<p>
When a session is saved, the session manager will ask
each client to save enough information to restore its
state the next time the user logs in. Your
application should save as much interesting state as
possible: the current open documents, cursor
position, command histories, and so on. Applications
should <i class="EMPHASIS">not</i> save their current
window geometries; the window manager is responsible
for that.
</p>
</li>
<li>
<p>
Sometimes the session manager will ask your client to
shut down and exit (typically when the user logs
out). When you receive this request you should do
whatever is necessary to exit the application.
</p>
</li>
</ul>
<p>
When the session manager requests action from your
application, a the <span class="STRUCTNAME">
GnomeClient</span> object emits an appropriate signal.
The two important signals are <span class="SYMBOL">
"save_yourself"</span> and <span class="SYMBOL">
"die"</span>. <span class="SYMBOL">"save_yourself"</span>
is emitted when an application should save its state, and
<span class="SYMBOL">"die"</span> is emitted when an
application should exit. A <span class="SYMBOL">
"save_yourself"</span> callback is fairly complex and has
quite a few arguments; a <span class="SYMBOL">
"die"</span> callback is trivial.
</p>
<p>
GnomeHello obtains a pointer to the <span class=
"STRUCTNAME">GnomeClient</span> object and connects to
its signals as follows:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
client = gnome_master_client ();
gtk_signal_connect (GTK_OBJECT (client), "save_yourself",
GTK_SIGNAL_FUNC (save_session), argv[0]);
gtk_signal_connect (GTK_OBJECT (client), "die",
GTK_SIGNAL_FUNC (session_die), NULL);
</pre>
</td>
</tr>
</table>
<p>
<span class="STRUCTNAME">argv[0]</span> will be used in
the <span class="SYMBOL">"save_yourself"</span> callback.
</p>
<p>
First, here's the <span class="SYMBOL">"die"</span>
callback from GnomeHello:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
session_die(GnomeClient* client, gpointer client_data)
{
gtk_main_quit ();
}
</pre>
</td>
</tr>
</table>
<p>
Straightforward; the application just exits.
</p>
<p>
Now the <span class="SYMBOL">"save_yourself"</span>
callback:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static gint
save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style,
gint is_shutdown, GnomeInteractStyle interact_style,
gint is_fast, gpointer client_data)
{
gchar** argv;
guint argc;
/* allocate 0-filled, so it will be NULL-terminated */
argv = g_malloc0(sizeof(gchar*)*4);
argc = 1;
argv[0] = client_data;
if (message)
{
argv[1] = "--message";
argv[2] = message;
argc = 3;
}
gnome_client_set_clone_command (client, argc, argv);
gnome_client_set_restart_command (client, argc, argv);
return TRUE;
}
</pre>
</td>
</tr>
</table>
<p>
This is a bit more complex. A <span class="SYMBOL">
"save_yourself"</span> must tell the session manager how
to restart and "clone" (create a new instance of) the
application. The restarted application should remember as
much state as possible; in GnomeHello's case, it will
remember the message being displayed. The simplest way to
store application state is to generate a command line, as
GnomeHello does. It's also possible to ask <span class=
"STRUCTNAME">GnomeClient</span> for a prefix to be used
with the gnome-config API; you can then save information
to a per-session configuration file. Applications with
significant state will need to use this method.
</p>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z79.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="cha-main.html"><font color="#0000ff" size="2">
<b>Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Saving Configuration
Information</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>The Main Window: <tt
class="CLASSNAME">GnomeApp</tt></b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|