summaryrefstree
path: root/usr/share/gtk-doc/html/gobject/signal.html
blob: e16803db79b28ced4f86cb9e2594903895021bb8 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Signals</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="index.html" title="GObject Reference Manual">
<link rel="up" href="chapter-signal.html" title="The GObject messaging system">
<link rel="prev" href="chapter-signal.html" title="The GObject messaging system">
<link rel="next" href="rn01.html" title="API Reference">
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="chapter-signal.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="chapter-signal.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GObject Reference Manual</th>
<td><a accesskey="n" href="rn01.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="signal"></a>Signals</h2></div></div></div>
<p>
      GObject's signals have nothing to do with standard UNIX signals: they connect 
      arbitrary application-specific events with any number of listeners.
      For example, in GTK+, every user event (keystroke or mouse move) is received
      from the X server and generates a GTK+ event under the form of a signal emission
      on a given object instance.
    </p>
<p>
      Each signal is registered in the type system together with the type on which
      it can be emitted: users of the type are said to <span class="emphasis"><em>connect</em></span>
      to the signal on a given type instance when they register a closure to be 
      invoked upon the signal emission. Users can also emit the signal by themselves 
      or stop the emission of the signal from within one of the closures connected 
      to the signal.
    </p>
<p>
      When a signal is emitted on a given type instance, all the closures
      connected to this signal on this type instance will be invoked. All the closures
      connected to such a signal represent callbacks whose signature looks like:
</p>
<pre class="programlisting">
return_type function_callback (gpointer instance, ... , gpointer user_data);
</pre>
<p>
    </p>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="signal-registration"></a>Signal registration</h3></div></div></div>
<p>
		To register a new signal on an existing type, we can use any of <code class="function"><a class="link" href="gobject-Signals.html#g-signal-newv" title="g_signal_newv ()">g_signal_newv</a></code>,
		<code class="function"><a class="link" href="gobject-Signals.html#g-signal-new-valist" title="g_signal_new_valist ()">g_signal_new_valist</a></code> or <code class="function"><a class="link" href="gobject-Signals.html#g-signal-new" title="g_signal_new ()">g_signal_new</a></code> functions:
</p>
<pre class="programlisting">
guint g_signal_newv (const gchar        *signal_name,
                     GType               itype,
                     GSignalFlags        signal_flags,
                     GClosure           *class_closure,
                     GSignalAccumulator  accumulator,
                     gpointer            accu_data,
                     GSignalCMarshaller  c_marshaller,
                     GType               return_type,
                     guint               n_params,
                     GType              *param_types);
</pre>
<p>
		The number of parameters to these functions is a bit intimidating but they are relatively
		simple:
		</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
			  signal_name: is a string which can be used to uniquely identify a given signal.
			</p></li>
<li class="listitem"><p>
			  itype: is the instance type on which this signal can be emitted.
			</p></li>
<li class="listitem"><p>
			  signal_flags: partly defines the order in which closures which were connected to the
			  signal are invoked.
			</p></li>
<li class="listitem"><p>
			  class_closure: this is the default closure for the signal: if it is not NULL upon
			  the signal emission, it will be invoked upon this emission of the signal. The 
			  moment where this closure is invoked compared to other closures connected to that 
			  signal depends partly on the signal_flags.
			</p></li>
<li class="listitem"><p>
			  accumulator: this is a function pointer which is invoked after each closure
			  has been invoked. If it returns FALSE, signal emission is stopped. If it returns
			  TRUE, signal emission proceeds normally. It is also used to compute the return
			  value of the signal based on the return value of all the invoked closures.
			</p></li>
<li class="listitem"><p>
			  accumulator_data: this pointer will be passed down to each invocation of the
			  accumulator during emission.
			</p></li>
<li class="listitem"><p>
			  c_marshaller: this is the default C marshaller for any closure which is connected to
			this signal.
			</p></li>
<li class="listitem"><p>
			  return_type: this is the type of the return value of the signal.
			</p></li>
<li class="listitem"><p>
			  n_params: this is the number of parameters this signal takes.
			</p></li>
<li class="listitem"><p>
			  param_types: this is an array of GTypes which indicate the type of each parameter
			  of the signal. The length of this array is indicated by n_params.
			</p></li>
</ul></div>
<p>
      </p>
<p>
		As you can see from the above definition, a signal is basically a description
		of the closures which can be connected to this signal and a description of the
		order in which the closures connected to this signal will be invoked.
	  </p>
</div>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="signal-connection"></a>Signal connection</h3></div></div></div>
<p>
		If you want to connect to a signal with a closure, you have three possibilities:
		</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
		  You can register a class closure at signal registration: this is a
		  system-wide operation. i.e.: the class_closure will be invoked during each emission
		  of a given signal on all the instances of the type which supports that signal.
			</p></li>
<li class="listitem"><p>
		  You can use <code class="function"><a class="link" href="gobject-Signals.html#g-signal-override-class-closure" title="g_signal_override_class_closure ()">g_signal_override_class_closure</a></code> which
		  overrides the class_closure of a given type. It is possible to call this function
		  only on a derived type of the type on which the signal was registered.
		  This function is of use only to language bindings.
			</p></li>
<li class="listitem"><p>
		  You can register a closure with the <code class="function"><a class="link" href="gobject-Signals.html#g-signal-connect" title="g_signal_connect()">g_signal_connect</a></code>
		  family of functions. This is an instance-specific operation: the closure
		  will be invoked only during emission of a given signal on a given instance.
			</p></li>
</ul></div>
<p>
		It is also possible to connect a different kind of callback on a given signal: 
		emission hooks are invoked whenever a given signal is emitted whatever the instance on 
		which it is emitted. Emission hooks are used for example to get all mouse_clicked
		emissions in an application to be able to emit the small mouse click sound.
		Emission hooks are connected with <code class="function"><a class="link" href="gobject-Signals.html#g-signal-add-emission-hook" title="g_signal_add_emission_hook ()">g_signal_add_emission_hook</a></code>
		and removed with <code class="function"><a class="link" href="gobject-Signals.html#g-signal-remove-emission-hook" title="g_signal_remove_emission_hook ()">g_signal_remove_emission_hook</a></code>.
	  </p>
</div>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="signal-emission"></a>Signal emission</h3></div></div></div>
<p>
		Signal emission is done through the use of the <code class="function"><a class="link" href="gobject-Signals.html#g-signal-emit" title="g_signal_emit ()">g_signal_emit</a></code> family 
		of functions.
</p>
<pre class="programlisting">
void g_signal_emitv (const GValue *instance_and_params,
                     guint         signal_id,
                     GQuark        detail,
                     GValue       *return_value);
</pre>
<p>
		</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
			The instance_and_params array of GValues contains the list of input
			parameters to the signal. The first element of the array is the 
			instance pointer on which to invoke the signal. The following elements of
			the array contain the list of parameters to the signal.
			</p></li>
<li class="listitem"><p>
			signal_id identifies the signal to invoke.
			</p></li>
<li class="listitem"><p>
			detail identifies the specific detail of the signal to invoke. A detail is a kind of 
			magic token/argument which is passed around during signal emission and which is used
			by closures connected to the signal to filter out unwanted signal emissions. In most 
			cases, you can safely set this value to zero. See <a class="xref" href="signal.html#signal-detail" title="The detail argument">the section called “The <span class="emphasis"><em>detail</em></span> argument”</a> for
			more details about this parameter.
			</p></li>
<li class="listitem"><p>
			return_value holds the return value of the last closure invoked during emission if
			no accumulator was specified. If an accumulator was specified during signal creation,
			this accumulator is used to calculate the return_value as a function of the return
			values of all the closures invoked during emission. 
			<a href="#ftn.id-1.3.5.3.7.2.3.4.1.1" class="footnote" name="id-1.3.5.3.7.2.3.4.1.1"><sup class="footnote">[8]</sup></a>
			If no closure is invoked during
			emission, the return_value is nonetheless initialized to zero/null.
			</p></li>
</ul></div>
<p>
		</p>
<p>
		Internally, the GValue array is passed to the emission function proper, 
		<code class="function">signal_emit_unlocked_R</code> (implemented in <code class="filename">gsignal.c</code>).
		Signal emission can be decomposed in 5 steps:
		</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
			<span class="emphasis"><em>RUN_FIRST</em></span>: if the G_SIGNAL_RUN_FIRST flag was used
			during signal registration and if there exist a class_closure for this signal,
			the class_closure is invoked. Jump to <span class="emphasis"><em>EMISSION_HOOK</em></span> state.
			</p></li>
<li class="listitem"><p>
			<span class="emphasis"><em>EMISSION_HOOK</em></span>: if any emission hook was added to
			the signal, they are invoked from first to last added. Accumulate return values
			and jump to <span class="emphasis"><em>HANDLER_RUN_FIRST</em></span> state. 
			</p></li>
<li class="listitem"><p>
			<span class="emphasis"><em>HANDLER_RUN_FIRST</em></span>: if any closure were connected
			with the <code class="function"><a class="link" href="gobject-Signals.html#g-signal-connect" title="g_signal_connect()">g_signal_connect</a></code> family of 
			functions, and if they are not blocked (with the <code class="function"><a class="link" href="gobject-Signals.html#g-signal-handler-block" title="g_signal_handler_block ()">g_signal_handler_block</a></code>
			family of functions) they are run here, from first to last connected.
			Jump to <span class="emphasis"><em>RUN_LAST</em></span> state.
			</p></li>
<li class="listitem"><p>
			<span class="emphasis"><em>RUN_LAST</em></span>: if the G_SIGNAL_RUN_LAST
			flag was set during registration and if a class_closure
			was set, it is invoked here. Jump to 
			<span class="emphasis"><em>HANDLER_RUN_LAST</em></span> state.
			</p></li>
<li class="listitem"><p>
			<span class="emphasis"><em>HANDLER_RUN_LAST</em></span>: if any closure were connected
			with the <code class="function">g_signal_connect_after</code> family of 
			functions, if they were not invoked during HANDLER_RUN_FIRST and if they 
			are not blocked, they are run here, from first to last connected.
			Jump to  <span class="emphasis"><em>RUN_CLEANUP</em></span> state.
			</p></li>
<li class="listitem"><p>
			<span class="emphasis"><em>RUN_CLEANUP</em></span>: if the G_SIGNAL_RUN_CLEANUP flag
			was set during registration and if a class_closure was set,
			it is invoked here. Signal emission is completed here.
			</p></li>
</ul></div>
<p>      
	  </p>
<p>
		If, at any point during emission (except in RUN_CLEANUP state), one of the
		closures or emission hook stops the signal emission with
		<code class="function"><a class="link" href="gobject-Signals.html#g-signal-stop-emission" title="g_signal_stop_emission ()">g_signal_stop_emission</a></code>,
		emission jumps to CLEANUP state.
	  </p>
<p>
		If, at any point during emission, one of the closures or emission hook 
		emits the same signal on the same instance, emission is restarted from
		the RUN_FIRST state.
	  </p>
<p>
		The accumulator function is invoked in all states, after invocation
		of each closure (except in EMISSION_HOOK and CLEANUP). It accumulates
		the closure return value into the signal return value and returns TRUE or
		FALSE. If, at any point, it does not return TRUE, emission jumps to CLEANUP state.
	  </p>
<p>
		If no accumulator function was provided, the value returned by the last handler
		run will be returned by <code class="function"><a class="link" href="gobject-Signals.html#g-signal-emit" title="g_signal_emit ()">g_signal_emit</a></code>.
	  </p>
</div>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="signal-detail"></a>The <span class="emphasis"><em>detail</em></span> argument</h3></div></div></div>
<p>All the functions related to signal emission or signal connection have a parameter
		named the <span class="emphasis"><em>detail</em></span>. Sometimes, this parameter is hidden by the API
		but it is always there, under one form or another. 
	  </p>
<p>
	    Of the three main connection functions,
		only one has an explicit detail parameter as a <a href="http://library.gnome.org/devel/glib/unstable/glib-Quarks.html#GQuark"><span class="type">GQuark</span></a>
		<a href="#ftn.id-1.3.5.3.8.3.2" class="footnote" name="id-1.3.5.3.8.3.2"><sup class="footnote">[9]</sup></a>:
</p>
<pre class="programlisting">
gulong     g_signal_connect_closure_by_id          (gpointer          instance,
                           guint          signal_id,
                           GQuark          detail,
                           GClosure         *closure,
                           gboolean          after);
</pre>
<p>
        The two other functions hide the detail parameter in the signal name identification:
</p>
<pre class="programlisting">
gulong     g_signal_connect_closure          (gpointer          instance,
                           const gchar       *detailed_signal,
                           GClosure         *closure,
                           gboolean          after);
gulong     g_signal_connect_data              (gpointer          instance,
                           const gchar     *detailed_signal,
                           GCallback      c_handler,
                           gpointer          data,
                           GClosureNotify      destroy_data,
                           GConnectFlags      connect_flags);
</pre>
<p>
		Their detailed_signal parameter is a string which identifies the name of the signal
		to connect to. However, the format of this string is structured to look like 
		<span class="emphasis"><em>signal_name::detail_name</em></span>. Connecting to the signal
		named <span class="emphasis"><em>notify::cursor_position</em></span> will actually connect to the signal
		named <span class="emphasis"><em>notify</em></span> with the <span class="emphasis"><em>cursor_position</em></span> name.
		Internally, the detail string is transformed to a GQuark if it is present.
	  </p>
<p>
		Of the four main signal emission functions, three have an explicit detail parameter as a 
		<a href="http://library.gnome.org/devel/glib/unstable/glib-Quarks.html#GQuark"><span class="type">GQuark</span></a> again:
</p>
<pre class="programlisting">
void                  g_signal_emitv        (const GValue       *instance_and_params,
                         guint               signal_id,
                         GQuark              detail,
                         GValue             *return_value);
void                  g_signal_emit_valist  (gpointer            instance,
                         guint               signal_id,
                         GQuark              detail,
                         va_list             var_args);
void                  g_signal_emit         (gpointer            instance,
                         guint               signal_id,
                         GQuark              detail,
                         ...);
</pre>
<p>
        The fourth function hides it in its signal name parameter:
</p>
<pre class="programlisting">
void                  g_signal_emit_by_name (gpointer            instance,
                         const gchar        *detailed_signal,
                         ...);
</pre>
<p>
        The format of the detailed_signal parameter is exactly the same as the format used by
        the <code class="function"><a class="link" href="gobject-Signals.html#g-signal-connect" title="g_signal_connect()">g_signal_connect</a></code> functions: <span class="emphasis"><em>signal_name::detail_name</em></span>.
	  </p>
<p>
        If a detail is provided by the user to the emission function, it is used during emission to match
        against the closures which also provide a detail.
        If the closures' detail does not match the detail provided by the user, they will not be invoked
        (even though they are connected to a signal which is being emitted).
	  </p>
<p>
		This completely optional filtering mechanism is mainly used as an optimization for signals
		which are often emitted for many different reasons: the clients can filter out which events they are
		interested in before the closure's marshalling code runs. For example, this is used extensively
		by the <span class="emphasis"><em>notify</em></span> signal of GObject: whenever a property is modified on a GObject,
		instead of just emitting the <span class="emphasis"><em>notify</em></span> signal, GObject associates as a detail to this
		signal emission the name of the property modified. This allows clients who wish to be notified of changes
		to only one property to filter most events before receiving them.
	  </p>
<p>
		As a simple rule, users can and should set the detail parameter to zero: this will disable completely
        this optional filtering.
	  </p>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.id-1.3.5.3.7.2.3.4.1.1" class="footnote"><p><a href="#id-1.3.5.3.7.2.3.4.1.1" class="para"><sup class="para">[8] </sup></a>
			  James (again!!) gives a few non-trivial examples of accumulators:
			  <span class="quote">“<span class="quote">
				For instance, you may have an accumulator that ignores NULL returns from 
				closures, and only accumulates the non-NULL ones. Another accumulator may try
				to return the list of values returned by the closures.
			  </span>”</span>
			</p></div>
<div id="ftn.id-1.3.5.3.8.3.2" class="footnote"><p><a href="#id-1.3.5.3.8.3.2" class="para"><sup class="para">[9] </sup></a>A GQuark is an integer which uniquely represents a string. It is possible to transform
		   back and forth between the integer and string representations with the functions 
		   <code class="function"><a href="http://library.gnome.org/devel/glib/unstable/glib-Quarks.html#g-quark-from-string">g_quark_from_string</a></code> and <code class="function"><a href="http://library.gnome.org/devel/glib/unstable/glib-Quarks.html#g-quark-to-string">g_quark_to_string</a></code>.
		  </p></div>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.19</div>
</body>
</html>