/src/glib/gobject/gsignal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 2000-2001 Red Hat, Inc. |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General |
17 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * this code is based on the original GtkSignal implementation |
20 | | * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu> |
21 | | */ |
22 | | |
23 | | /* |
24 | | * MT safe |
25 | | */ |
26 | | |
27 | | #include "config.h" |
28 | | |
29 | | #include <string.h> |
30 | | #include <signal.h> |
31 | | |
32 | | #include "gsignal.h" |
33 | | #include "gtype-private.h" |
34 | | #include "gbsearcharray.h" |
35 | | #include "gvaluecollector.h" |
36 | | #include "gvaluetypes.h" |
37 | | #include "gobject.h" |
38 | | #include "genums.h" |
39 | | #include "gobject_trace.h" |
40 | | |
41 | | |
42 | | /** |
43 | | * SECTION:signals |
44 | | * @short_description: A means for customization of object behaviour |
45 | | * and a general purpose notification mechanism |
46 | | * @title: Signals |
47 | | * |
48 | | * The basic concept of the signal system is that of the emission |
49 | | * of a signal. Signals are introduced per-type and are identified |
50 | | * through strings. Signals introduced for a parent type are available |
51 | | * in derived types as well, so basically they are a per-type facility |
52 | | * that is inherited. |
53 | | * |
54 | | * A signal emission mainly involves invocation of a certain set of |
55 | | * callbacks in precisely defined manner. There are two main categories |
56 | | * of such callbacks, per-object ones and user provided ones. |
57 | | * (Although signals can deal with any kind of instantiatable type, I'm |
58 | | * referring to those types as "object types" in the following, simply |
59 | | * because that is the context most users will encounter signals in.) |
60 | | * The per-object callbacks are most often referred to as "object method |
61 | | * handler" or "default (signal) handler", while user provided callbacks are |
62 | | * usually just called "signal handler". |
63 | | * |
64 | | * The object method handler is provided at signal creation time (this most |
65 | | * frequently happens at the end of an object class' creation), while user |
66 | | * provided handlers are frequently connected and disconnected to/from a |
67 | | * certain signal on certain object instances. |
68 | | * |
69 | | * A signal emission consists of five stages, unless prematurely stopped: |
70 | | * |
71 | | * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals |
72 | | * |
73 | | * 2. Invocation of normal user-provided signal handlers (where the @after |
74 | | * flag is not set) |
75 | | * |
76 | | * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals |
77 | | * |
78 | | * 4. Invocation of user provided signal handlers (where the @after flag is set) |
79 | | * |
80 | | * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals |
81 | | * |
82 | | * The user-provided signal handlers are called in the order they were |
83 | | * connected in. |
84 | | * |
85 | | * All handlers may prematurely stop a signal emission, and any number of |
86 | | * handlers may be connected, disconnected, blocked or unblocked during |
87 | | * a signal emission. |
88 | | * |
89 | | * There are certain criteria for skipping user handlers in stages 2 and 4 |
90 | | * of a signal emission. |
91 | | * |
92 | | * First, user handlers may be blocked. Blocked handlers are omitted during |
93 | | * callback invocation, to return from the blocked state, a handler has to |
94 | | * get unblocked exactly the same amount of times it has been blocked before. |
95 | | * |
96 | | * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional |
97 | | * @detail argument passed in to g_signal_emit() has to match the detail |
98 | | * argument of the signal handler currently subject to invocation. |
99 | | * Specification of no detail argument for signal handlers (omission of the |
100 | | * detail part of the signal specification upon connection) serves as a |
101 | | * wildcard and matches any detail argument passed in to emission. |
102 | | * |
103 | | * While the @detail argument is typically used to pass an object property name |
104 | | * (as with #GObject::notify), no specific format is mandated for the detail |
105 | | * string, other than that it must be non-empty. |
106 | | * |
107 | | * ## Memory management of signal handlers # {#signal-memory-management} |
108 | | * |
109 | | * If you are connecting handlers to signals and using a #GObject instance as |
110 | | * your signal handler user data, you should remember to pair calls to |
111 | | * g_signal_connect() with calls to g_signal_handler_disconnect() or |
112 | | * g_signal_handlers_disconnect_by_func(). While signal handlers are |
113 | | * automatically disconnected when the object emitting the signal is finalised, |
114 | | * they are not automatically disconnected when the signal handler user data is |
115 | | * destroyed. If this user data is a #GObject instance, using it from a |
116 | | * signal handler after it has been finalised is an error. |
117 | | * |
118 | | * There are two strategies for managing such user data. The first is to |
119 | | * disconnect the signal handler (using g_signal_handler_disconnect() or |
120 | | * g_signal_handlers_disconnect_by_func()) when the user data (object) is |
121 | | * finalised; this has to be implemented manually. For non-threaded programs, |
122 | | * g_signal_connect_object() can be used to implement this automatically. |
123 | | * Currently, however, it is unsafe to use in threaded programs. |
124 | | * |
125 | | * The second is to hold a strong reference on the user data until after the |
126 | | * signal is disconnected for other reasons. This can be implemented |
127 | | * automatically using g_signal_connect_data(). |
128 | | * |
129 | | * The first approach is recommended, as the second approach can result in |
130 | | * effective memory leaks of the user data if the signal handler is never |
131 | | * disconnected for some reason. |
132 | | */ |
133 | | |
134 | | |
135 | | #define REPORT_BUG "please report occurrence circumstances to https://gitlab.gnome.org/GNOME/glib/issues/new" |
136 | | |
137 | | /* --- typedefs --- */ |
138 | | typedef struct _SignalNode SignalNode; |
139 | | typedef struct _SignalKey SignalKey; |
140 | | typedef struct _Emission Emission; |
141 | | typedef struct _Handler Handler; |
142 | | typedef struct _HandlerList HandlerList; |
143 | | typedef struct _HandlerMatch HandlerMatch; |
144 | | typedef enum |
145 | | { |
146 | | EMISSION_STOP, |
147 | | EMISSION_RUN, |
148 | | EMISSION_HOOK, |
149 | | EMISSION_RESTART |
150 | | } EmissionState; |
151 | | |
152 | | |
153 | | /* --- prototypes --- */ |
154 | | static inline guint signal_id_lookup (const gchar *name, |
155 | | GType itype); |
156 | | static void signal_destroy_R (SignalNode *signal_node); |
157 | | static inline HandlerList* handler_list_ensure (guint signal_id, |
158 | | gpointer instance); |
159 | | static inline HandlerList* handler_list_lookup (guint signal_id, |
160 | | gpointer instance); |
161 | | static inline Handler* handler_new (guint signal_id, |
162 | | gpointer instance, |
163 | | gboolean after); |
164 | | static void handler_insert (guint signal_id, |
165 | | gpointer instance, |
166 | | Handler *handler); |
167 | | static Handler* handler_lookup (gpointer instance, |
168 | | gulong handler_id, |
169 | | GClosure *closure, |
170 | | guint *signal_id_p); |
171 | | static inline HandlerMatch* handler_match_prepend (HandlerMatch *list, |
172 | | Handler *handler, |
173 | | guint signal_id); |
174 | | static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node, |
175 | | gpointer instance); |
176 | | static HandlerMatch* handlers_find (gpointer instance, |
177 | | GSignalMatchType mask, |
178 | | guint signal_id, |
179 | | GQuark detail, |
180 | | GClosure *closure, |
181 | | gpointer func, |
182 | | gpointer data, |
183 | | gboolean one_and_only); |
184 | | static inline void handler_ref (Handler *handler); |
185 | | static inline void handler_unref_R (guint signal_id, |
186 | | gpointer instance, |
187 | | Handler *handler); |
188 | | static gint handler_lists_cmp (gconstpointer node1, |
189 | | gconstpointer node2); |
190 | | static inline void emission_push (Emission *emission); |
191 | | static inline void emission_pop (Emission *emission); |
192 | | static inline Emission* emission_find (guint signal_id, |
193 | | GQuark detail, |
194 | | gpointer instance); |
195 | | static gint class_closures_cmp (gconstpointer node1, |
196 | | gconstpointer node2); |
197 | | static gint signal_key_cmp (gconstpointer node1, |
198 | | gconstpointer node2); |
199 | | static gboolean signal_emit_unlocked_R (SignalNode *node, |
200 | | GQuark detail, |
201 | | gpointer instance, |
202 | | GValue *return_value, |
203 | | const GValue *instance_and_params); |
204 | | static void add_invalid_closure_notify (Handler *handler, |
205 | | gpointer instance); |
206 | | static void remove_invalid_closure_notify (Handler *handler, |
207 | | gpointer instance); |
208 | | static void invalid_closure_notify (gpointer data, |
209 | | GClosure *closure); |
210 | | static const gchar * type_debug_name (GType type); |
211 | | static void node_check_deprecated (const SignalNode *node); |
212 | | static void node_update_single_va_closure (SignalNode *node); |
213 | | |
214 | | |
215 | | /* --- structures --- */ |
216 | | typedef struct |
217 | | { |
218 | | GSignalAccumulator func; |
219 | | gpointer data; |
220 | | } SignalAccumulator; |
221 | | typedef struct |
222 | | { |
223 | | GHook hook; |
224 | | GQuark detail; |
225 | | } SignalHook; |
226 | 0 | #define SIGNAL_HOOK(hook) ((SignalHook*) (hook)) |
227 | | |
228 | | struct _SignalNode |
229 | | { |
230 | | /* permanent portion */ |
231 | | guint signal_id; |
232 | | GType itype; |
233 | | const gchar *name; |
234 | | guint destroyed : 1; |
235 | | |
236 | | /* reinitializable portion */ |
237 | | guint flags : 9; |
238 | | guint n_params : 8; |
239 | | guint single_va_closure_is_valid : 1; |
240 | | guint single_va_closure_is_after : 1; |
241 | | GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */ |
242 | | GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */ |
243 | | GBSearchArray *class_closure_bsa; |
244 | | SignalAccumulator *accumulator; |
245 | | GSignalCMarshaller c_marshaller; |
246 | | GSignalCVaMarshaller va_marshaller; |
247 | | GHookList *emission_hooks; |
248 | | |
249 | | GClosure *single_va_closure; |
250 | | }; |
251 | | |
252 | 0 | #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */ |
253 | | |
254 | | struct _SignalKey |
255 | | { |
256 | | GType itype; |
257 | | GQuark quark; |
258 | | guint signal_id; |
259 | | }; |
260 | | |
261 | | struct _Emission |
262 | | { |
263 | | Emission *next; |
264 | | gpointer instance; |
265 | | GSignalInvocationHint ihint; |
266 | | EmissionState state; |
267 | | GType chain_type; |
268 | | }; |
269 | | |
270 | | struct _HandlerList |
271 | | { |
272 | | guint signal_id; |
273 | | Handler *handlers; |
274 | | Handler *tail_before; /* normal signal handlers are appended here */ |
275 | | Handler *tail_after; /* CONNECT_AFTER handlers are appended here */ |
276 | | }; |
277 | | |
278 | | struct _Handler |
279 | | { |
280 | | gulong sequential_number; |
281 | | Handler *next; |
282 | | Handler *prev; |
283 | | GQuark detail; |
284 | | guint signal_id; |
285 | | guint ref_count; |
286 | | guint block_count : 16; |
287 | 0 | #define HANDLER_MAX_BLOCK_COUNT (1 << 16) |
288 | | guint after : 1; |
289 | | guint has_invalid_closure_notify : 1; |
290 | | GClosure *closure; |
291 | | gpointer instance; |
292 | | }; |
293 | | struct _HandlerMatch |
294 | | { |
295 | | Handler *handler; |
296 | | HandlerMatch *next; |
297 | | guint signal_id; |
298 | | }; |
299 | | |
300 | | typedef struct |
301 | | { |
302 | | GType instance_type; /* 0 for default closure */ |
303 | | GClosure *closure; |
304 | | } ClassClosure; |
305 | | |
306 | | |
307 | | /* --- variables --- */ |
308 | | static GBSearchArray *g_signal_key_bsa = NULL; |
309 | | static const GBSearchConfig g_signal_key_bconfig = { |
310 | | sizeof (SignalKey), |
311 | | signal_key_cmp, |
312 | | G_BSEARCH_ARRAY_ALIGN_POWER2, |
313 | | }; |
314 | | static GBSearchConfig g_signal_hlbsa_bconfig = { |
315 | | sizeof (HandlerList), |
316 | | handler_lists_cmp, |
317 | | 0, |
318 | | }; |
319 | | static GBSearchConfig g_class_closure_bconfig = { |
320 | | sizeof (ClassClosure), |
321 | | class_closures_cmp, |
322 | | 0, |
323 | | }; |
324 | | static GHashTable *g_handler_list_bsa_ht = NULL; |
325 | | static Emission *g_emissions = NULL; |
326 | | static gulong g_handler_sequential_number = 1; |
327 | | static GHashTable *g_handlers = NULL; |
328 | | |
329 | | G_LOCK_DEFINE_STATIC (g_signal_mutex); |
330 | 4.01k | #define SIGNAL_LOCK() G_LOCK (g_signal_mutex) |
331 | 4.01k | #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex) |
332 | | |
333 | | |
334 | | /* --- signal nodes --- */ |
335 | | static guint g_n_signal_nodes = 0; |
336 | | static SignalNode **g_signal_nodes = NULL; |
337 | | |
338 | | static inline SignalNode* |
339 | | LOOKUP_SIGNAL_NODE (guint signal_id) |
340 | 3 | { |
341 | 3 | if (signal_id < g_n_signal_nodes) |
342 | 3 | return g_signal_nodes[signal_id]; |
343 | 0 | else |
344 | 0 | return NULL; |
345 | 3 | } |
346 | | |
347 | | |
348 | | /* --- functions --- */ |
349 | | /* @key must have already been validated with is_valid() |
350 | | * Modifies @key in place. */ |
351 | | static void |
352 | | canonicalize_key (gchar *key) |
353 | 0 | { |
354 | 0 | gchar *p; |
355 | |
|
356 | 0 | for (p = key; *p != 0; p++) |
357 | 0 | { |
358 | 0 | gchar c = *p; |
359 | |
|
360 | 0 | if (c == '_') |
361 | 0 | *p = '-'; |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | /* @key must have already been validated with is_valid() */ |
366 | | static gboolean |
367 | | is_canonical (const gchar *key) |
368 | 6 | { |
369 | 6 | return (strchr (key, '_') == NULL); |
370 | 6 | } |
371 | | |
372 | | /** |
373 | | * g_signal_is_valid_name: |
374 | | * @name: the canonical name of the signal |
375 | | * |
376 | | * Validate a signal name. This can be useful for dynamically-generated signals |
377 | | * which need to be validated at run-time before actually trying to create them. |
378 | | * |
379 | | * See [canonical parameter names][canonical-parameter-names] for details of |
380 | | * the rules for valid names. The rules for signal names are the same as those |
381 | | * for property names. |
382 | | * |
383 | | * Returns: %TRUE if @name is a valid signal name, %FALSE otherwise. |
384 | | * Since: 2.66 |
385 | | */ |
386 | | gboolean |
387 | | g_signal_is_valid_name (const gchar *name) |
388 | 3 | { |
389 | | /* FIXME: We allow this, against our own documentation (the leading `-` is |
390 | | * invalid), because GTK has historically used this. */ |
391 | 3 | if (g_str_equal (name, "-gtk-private-changed")) |
392 | 0 | return TRUE; |
393 | | |
394 | 3 | return g_param_spec_is_valid_name (name); |
395 | 3 | } |
396 | | |
397 | | static inline guint |
398 | | signal_id_lookup (const gchar *name, |
399 | | GType itype) |
400 | 3 | { |
401 | 3 | GQuark quark; |
402 | 3 | GType *ifaces, type = itype; |
403 | 3 | SignalKey key; |
404 | 3 | guint n_ifaces; |
405 | | |
406 | 3 | quark = g_quark_try_string (name); |
407 | 3 | key.quark = quark; |
408 | | |
409 | | /* try looking up signals for this type and its ancestors */ |
410 | 3 | do |
411 | 4 | { |
412 | 4 | SignalKey *signal_key; |
413 | | |
414 | 4 | key.itype = type; |
415 | 4 | signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key); |
416 | | |
417 | 4 | if (signal_key) |
418 | 0 | return signal_key->signal_id; |
419 | | |
420 | 4 | type = g_type_parent (type); |
421 | 4 | } |
422 | 4 | while (type); |
423 | | |
424 | | /* no luck, try interfaces it exports */ |
425 | 3 | ifaces = g_type_interfaces (itype, &n_ifaces); |
426 | 3 | while (n_ifaces--) |
427 | 0 | { |
428 | 0 | SignalKey *signal_key; |
429 | |
|
430 | 0 | key.itype = ifaces[n_ifaces]; |
431 | 0 | signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key); |
432 | |
|
433 | 0 | if (signal_key) |
434 | 0 | { |
435 | 0 | g_free (ifaces); |
436 | 0 | return signal_key->signal_id; |
437 | 0 | } |
438 | 0 | } |
439 | 3 | g_free (ifaces); |
440 | | |
441 | | /* If the @name is non-canonical, try again. This is the slow path — people |
442 | | * should use canonical names in their queries if they want performance. */ |
443 | 3 | if (!is_canonical (name)) |
444 | 0 | { |
445 | 0 | guint signal_id; |
446 | 0 | gchar *name_copy = g_strdup (name); |
447 | 0 | canonicalize_key (name_copy); |
448 | |
|
449 | 0 | signal_id = signal_id_lookup (name_copy, itype); |
450 | |
|
451 | 0 | g_free (name_copy); |
452 | |
|
453 | 0 | return signal_id; |
454 | 0 | } |
455 | | |
456 | 3 | return 0; |
457 | 3 | } |
458 | | |
459 | | static gint |
460 | | class_closures_cmp (gconstpointer node1, |
461 | | gconstpointer node2) |
462 | 0 | { |
463 | 0 | const ClassClosure *c1 = node1, *c2 = node2; |
464 | | |
465 | 0 | return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type); |
466 | 0 | } |
467 | | |
468 | | static gint |
469 | | handler_lists_cmp (gconstpointer node1, |
470 | | gconstpointer node2) |
471 | 0 | { |
472 | 0 | const HandlerList *hlist1 = node1, *hlist2 = node2; |
473 | | |
474 | 0 | return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id); |
475 | 0 | } |
476 | | |
477 | | static inline HandlerList* |
478 | | handler_list_ensure (guint signal_id, |
479 | | gpointer instance) |
480 | 0 | { |
481 | 0 | GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); |
482 | 0 | HandlerList key; |
483 | | |
484 | 0 | key.signal_id = signal_id; |
485 | 0 | key.handlers = NULL; |
486 | 0 | key.tail_before = NULL; |
487 | 0 | key.tail_after = NULL; |
488 | 0 | if (!hlbsa) |
489 | 0 | { |
490 | 0 | hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig); |
491 | 0 | hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key); |
492 | 0 | g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa); |
493 | 0 | } |
494 | 0 | else |
495 | 0 | { |
496 | 0 | GBSearchArray *o = hlbsa; |
497 | |
|
498 | 0 | hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key); |
499 | 0 | if (hlbsa != o) |
500 | 0 | g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa); |
501 | 0 | } |
502 | 0 | return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key); |
503 | 0 | } |
504 | | |
505 | | static inline HandlerList* |
506 | | handler_list_lookup (guint signal_id, |
507 | | gpointer instance) |
508 | 0 | { |
509 | 0 | GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); |
510 | 0 | HandlerList key; |
511 | | |
512 | 0 | key.signal_id = signal_id; |
513 | | |
514 | 0 | return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL; |
515 | 0 | } |
516 | | |
517 | | static guint |
518 | | handler_hash (gconstpointer key) |
519 | 0 | { |
520 | 0 | return (guint)((Handler*)key)->sequential_number; |
521 | 0 | } |
522 | | |
523 | | static gboolean |
524 | | handler_equal (gconstpointer a, gconstpointer b) |
525 | 0 | { |
526 | 0 | Handler *ha = (Handler *)a; |
527 | 0 | Handler *hb = (Handler *)b; |
528 | 0 | return (ha->sequential_number == hb->sequential_number) && |
529 | 0 | (ha->instance == hb->instance); |
530 | 0 | } |
531 | | |
532 | | static Handler* |
533 | | handler_lookup (gpointer instance, |
534 | | gulong handler_id, |
535 | | GClosure *closure, |
536 | | guint *signal_id_p) |
537 | 0 | { |
538 | 0 | GBSearchArray *hlbsa; |
539 | |
|
540 | 0 | if (handler_id) |
541 | 0 | { |
542 | 0 | Handler key; |
543 | 0 | key.sequential_number = handler_id; |
544 | 0 | key.instance = instance; |
545 | 0 | return g_hash_table_lookup (g_handlers, &key); |
546 | |
|
547 | 0 | } |
548 | | |
549 | 0 | hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); |
550 | | |
551 | 0 | if (hlbsa) |
552 | 0 | { |
553 | 0 | guint i; |
554 | | |
555 | 0 | for (i = 0; i < hlbsa->n_nodes; i++) |
556 | 0 | { |
557 | 0 | HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); |
558 | 0 | Handler *handler; |
559 | | |
560 | 0 | for (handler = hlist->handlers; handler; handler = handler->next) |
561 | 0 | if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id)) |
562 | 0 | { |
563 | 0 | if (signal_id_p) |
564 | 0 | *signal_id_p = hlist->signal_id; |
565 | |
|
566 | 0 | return handler; |
567 | 0 | } |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | 0 | return NULL; |
572 | 0 | } |
573 | | |
574 | | static inline HandlerMatch* |
575 | | handler_match_prepend (HandlerMatch *list, |
576 | | Handler *handler, |
577 | | guint signal_id) |
578 | 0 | { |
579 | 0 | HandlerMatch *node; |
580 | | |
581 | 0 | node = g_slice_new (HandlerMatch); |
582 | 0 | node->handler = handler; |
583 | 0 | node->next = list; |
584 | 0 | node->signal_id = signal_id; |
585 | 0 | handler_ref (handler); |
586 | | |
587 | 0 | return node; |
588 | 0 | } |
589 | | static inline HandlerMatch* |
590 | | handler_match_free1_R (HandlerMatch *node, |
591 | | gpointer instance) |
592 | 0 | { |
593 | 0 | HandlerMatch *next = node->next; |
594 | | |
595 | 0 | handler_unref_R (node->signal_id, instance, node->handler); |
596 | 0 | g_slice_free (HandlerMatch, node); |
597 | | |
598 | 0 | return next; |
599 | 0 | } |
600 | | |
601 | | static HandlerMatch* |
602 | | handlers_find (gpointer instance, |
603 | | GSignalMatchType mask, |
604 | | guint signal_id, |
605 | | GQuark detail, |
606 | | GClosure *closure, |
607 | | gpointer func, |
608 | | gpointer data, |
609 | | gboolean one_and_only) |
610 | 0 | { |
611 | 0 | HandlerMatch *mlist = NULL; |
612 | | |
613 | 0 | if (mask & G_SIGNAL_MATCH_ID) |
614 | 0 | { |
615 | 0 | HandlerList *hlist = handler_list_lookup (signal_id, instance); |
616 | 0 | Handler *handler; |
617 | 0 | SignalNode *node = NULL; |
618 | | |
619 | 0 | if (mask & G_SIGNAL_MATCH_FUNC) |
620 | 0 | { |
621 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
622 | 0 | if (!node || !node->c_marshaller) |
623 | 0 | return NULL; |
624 | 0 | } |
625 | | |
626 | 0 | mask = ~mask; |
627 | 0 | for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next) |
628 | 0 | if (handler->sequential_number && |
629 | 0 | ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) && |
630 | 0 | ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) && |
631 | 0 | ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) && |
632 | 0 | ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) && |
633 | 0 | ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller && |
634 | 0 | G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL && |
635 | 0 | ((GCClosure*) handler->closure)->callback == func))) |
636 | 0 | { |
637 | 0 | mlist = handler_match_prepend (mlist, handler, signal_id); |
638 | 0 | if (one_and_only) |
639 | 0 | return mlist; |
640 | 0 | } |
641 | 0 | } |
642 | 0 | else |
643 | 0 | { |
644 | 0 | GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); |
645 | | |
646 | 0 | mask = ~mask; |
647 | 0 | if (hlbsa) |
648 | 0 | { |
649 | 0 | guint i; |
650 | | |
651 | 0 | for (i = 0; i < hlbsa->n_nodes; i++) |
652 | 0 | { |
653 | 0 | HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); |
654 | 0 | SignalNode *node = NULL; |
655 | 0 | Handler *handler; |
656 | | |
657 | 0 | if (!(mask & G_SIGNAL_MATCH_FUNC)) |
658 | 0 | { |
659 | 0 | node = LOOKUP_SIGNAL_NODE (hlist->signal_id); |
660 | 0 | if (!node->c_marshaller) |
661 | 0 | continue; |
662 | 0 | } |
663 | | |
664 | 0 | for (handler = hlist->handlers; handler; handler = handler->next) |
665 | 0 | if (handler->sequential_number && |
666 | 0 | ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) && |
667 | 0 | ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) && |
668 | 0 | ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) && |
669 | 0 | ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) && |
670 | 0 | ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller && |
671 | 0 | G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL && |
672 | 0 | ((GCClosure*) handler->closure)->callback == func))) |
673 | 0 | { |
674 | 0 | mlist = handler_match_prepend (mlist, handler, hlist->signal_id); |
675 | 0 | if (one_and_only) |
676 | 0 | return mlist; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | } |
680 | 0 | } |
681 | | |
682 | 0 | return mlist; |
683 | 0 | } |
684 | | |
685 | | static inline Handler* |
686 | | handler_new (guint signal_id, gpointer instance, gboolean after) |
687 | 0 | { |
688 | 0 | Handler *handler = g_slice_new (Handler); |
689 | 0 | #ifndef G_DISABLE_CHECKS |
690 | 0 | if (g_handler_sequential_number < 1) |
691 | 0 | g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG); |
692 | 0 | #endif |
693 | | |
694 | 0 | handler->sequential_number = g_handler_sequential_number++; |
695 | 0 | handler->prev = NULL; |
696 | 0 | handler->next = NULL; |
697 | 0 | handler->detail = 0; |
698 | 0 | handler->signal_id = signal_id; |
699 | 0 | handler->instance = instance; |
700 | 0 | handler->ref_count = 1; |
701 | 0 | handler->block_count = 0; |
702 | 0 | handler->after = after != FALSE; |
703 | 0 | handler->closure = NULL; |
704 | 0 | handler->has_invalid_closure_notify = 0; |
705 | |
|
706 | 0 | g_hash_table_add (g_handlers, handler); |
707 | | |
708 | 0 | return handler; |
709 | 0 | } |
710 | | |
711 | | static inline void |
712 | | handler_ref (Handler *handler) |
713 | 0 | { |
714 | 0 | g_return_if_fail (handler->ref_count > 0); |
715 | | |
716 | 0 | handler->ref_count++; |
717 | 0 | } |
718 | | |
719 | | static inline void |
720 | | handler_unref_R (guint signal_id, |
721 | | gpointer instance, |
722 | | Handler *handler) |
723 | 0 | { |
724 | 0 | g_return_if_fail (handler->ref_count > 0); |
725 | | |
726 | 0 | handler->ref_count--; |
727 | |
|
728 | 0 | if (G_UNLIKELY (handler->ref_count == 0)) |
729 | 0 | { |
730 | 0 | HandlerList *hlist = NULL; |
731 | |
|
732 | 0 | if (handler->next) |
733 | 0 | handler->next->prev = handler->prev; |
734 | 0 | if (handler->prev) /* watch out for g_signal_handlers_destroy()! */ |
735 | 0 | handler->prev->next = handler->next; |
736 | 0 | else |
737 | 0 | { |
738 | 0 | hlist = handler_list_lookup (signal_id, instance); |
739 | 0 | g_assert (hlist != NULL); |
740 | 0 | hlist->handlers = handler->next; |
741 | 0 | } |
742 | | |
743 | 0 | if (instance) |
744 | 0 | { |
745 | | /* check if we are removing the handler pointed to by tail_before */ |
746 | 0 | if (!handler->after && (!handler->next || handler->next->after)) |
747 | 0 | { |
748 | 0 | if (!hlist) |
749 | 0 | hlist = handler_list_lookup (signal_id, instance); |
750 | 0 | if (hlist) |
751 | 0 | { |
752 | 0 | g_assert (hlist->tail_before == handler); /* paranoid */ |
753 | 0 | hlist->tail_before = handler->prev; |
754 | 0 | } |
755 | 0 | } |
756 | | |
757 | | /* check if we are removing the handler pointed to by tail_after */ |
758 | 0 | if (!handler->next) |
759 | 0 | { |
760 | 0 | if (!hlist) |
761 | 0 | hlist = handler_list_lookup (signal_id, instance); |
762 | 0 | if (hlist) |
763 | 0 | { |
764 | 0 | g_assert (hlist->tail_after == handler); /* paranoid */ |
765 | 0 | hlist->tail_after = handler->prev; |
766 | 0 | } |
767 | 0 | } |
768 | 0 | } |
769 | | |
770 | 0 | SIGNAL_UNLOCK (); |
771 | 0 | g_closure_unref (handler->closure); |
772 | 0 | SIGNAL_LOCK (); |
773 | 0 | g_slice_free (Handler, handler); |
774 | 0 | } |
775 | 0 | } |
776 | | |
777 | | static void |
778 | | handler_insert (guint signal_id, |
779 | | gpointer instance, |
780 | | Handler *handler) |
781 | 0 | { |
782 | 0 | HandlerList *hlist; |
783 | | |
784 | 0 | g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */ |
785 | | |
786 | 0 | hlist = handler_list_ensure (signal_id, instance); |
787 | 0 | if (!hlist->handlers) |
788 | 0 | { |
789 | 0 | hlist->handlers = handler; |
790 | 0 | if (!handler->after) |
791 | 0 | hlist->tail_before = handler; |
792 | 0 | } |
793 | 0 | else if (handler->after) |
794 | 0 | { |
795 | 0 | handler->prev = hlist->tail_after; |
796 | 0 | hlist->tail_after->next = handler; |
797 | 0 | } |
798 | 0 | else |
799 | 0 | { |
800 | 0 | if (hlist->tail_before) |
801 | 0 | { |
802 | 0 | handler->next = hlist->tail_before->next; |
803 | 0 | if (handler->next) |
804 | 0 | handler->next->prev = handler; |
805 | 0 | handler->prev = hlist->tail_before; |
806 | 0 | hlist->tail_before->next = handler; |
807 | 0 | } |
808 | 0 | else /* insert !after handler into a list of only after handlers */ |
809 | 0 | { |
810 | 0 | handler->next = hlist->handlers; |
811 | 0 | if (handler->next) |
812 | 0 | handler->next->prev = handler; |
813 | 0 | hlist->handlers = handler; |
814 | 0 | } |
815 | 0 | hlist->tail_before = handler; |
816 | 0 | } |
817 | |
|
818 | 0 | if (!handler->next) |
819 | 0 | hlist->tail_after = handler; |
820 | 0 | } |
821 | | |
822 | | static void |
823 | | node_update_single_va_closure (SignalNode *node) |
824 | 0 | { |
825 | 0 | GClosure *closure = NULL; |
826 | 0 | gboolean is_after = FALSE; |
827 | | |
828 | | /* Fast path single-handler without boxing the arguments in GValues */ |
829 | 0 | if (G_TYPE_IS_OBJECT (node->itype) && |
830 | 0 | (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 && |
831 | 0 | (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL)) |
832 | 0 | { |
833 | 0 | GSignalFlags run_type; |
834 | 0 | ClassClosure * cc; |
835 | 0 | GBSearchArray *bsa = node->class_closure_bsa; |
836 | |
|
837 | 0 | if (bsa == NULL || bsa->n_nodes == 0) |
838 | 0 | closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC; |
839 | 0 | else if (bsa->n_nodes == 1) |
840 | 0 | { |
841 | | /* Look for default class closure (can't support non-default as it |
842 | | chains up using GValues */ |
843 | 0 | cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0); |
844 | 0 | if (cc->instance_type == 0) |
845 | 0 | { |
846 | 0 | run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP); |
847 | | /* Only support *one* of run-first or run-last, not multiple or cleanup */ |
848 | 0 | if (run_type == G_SIGNAL_RUN_FIRST || |
849 | 0 | run_type == G_SIGNAL_RUN_LAST) |
850 | 0 | { |
851 | 0 | closure = cc->closure; |
852 | 0 | is_after = (run_type == G_SIGNAL_RUN_LAST); |
853 | 0 | } |
854 | 0 | } |
855 | 0 | } |
856 | 0 | } |
857 | |
|
858 | 0 | node->single_va_closure_is_valid = TRUE; |
859 | 0 | node->single_va_closure = closure; |
860 | 0 | node->single_va_closure_is_after = is_after; |
861 | 0 | } |
862 | | |
863 | | static inline void |
864 | | emission_push (Emission *emission) |
865 | 0 | { |
866 | 0 | emission->next = g_emissions; |
867 | 0 | g_emissions = emission; |
868 | 0 | } |
869 | | |
870 | | static inline void |
871 | | emission_pop (Emission *emission) |
872 | 0 | { |
873 | 0 | Emission *node, *last = NULL; |
874 | |
|
875 | 0 | for (node = g_emissions; node; last = node, node = last->next) |
876 | 0 | if (node == emission) |
877 | 0 | { |
878 | 0 | if (last) |
879 | 0 | last->next = node->next; |
880 | 0 | else |
881 | 0 | g_emissions = node->next; |
882 | 0 | return; |
883 | 0 | } |
884 | 0 | g_assert_not_reached (); |
885 | 0 | } |
886 | | |
887 | | static inline Emission* |
888 | | emission_find (guint signal_id, |
889 | | GQuark detail, |
890 | | gpointer instance) |
891 | 0 | { |
892 | 0 | Emission *emission; |
893 | | |
894 | 0 | for (emission = g_emissions; emission; emission = emission->next) |
895 | 0 | if (emission->instance == instance && |
896 | 0 | emission->ihint.signal_id == signal_id && |
897 | 0 | emission->ihint.detail == detail) |
898 | 0 | return emission; |
899 | 0 | return NULL; |
900 | 0 | } |
901 | | |
902 | | static inline Emission* |
903 | | emission_find_innermost (gpointer instance) |
904 | 0 | { |
905 | 0 | Emission *emission; |
906 | | |
907 | 0 | for (emission = g_emissions; emission; emission = emission->next) |
908 | 0 | if (emission->instance == instance) |
909 | 0 | return emission; |
910 | | |
911 | 0 | return NULL; |
912 | 0 | } |
913 | | |
914 | | static gint |
915 | | signal_key_cmp (gconstpointer node1, |
916 | | gconstpointer node2) |
917 | 3 | { |
918 | 3 | const SignalKey *key1 = node1, *key2 = node2; |
919 | | |
920 | 3 | if (key1->itype == key2->itype) |
921 | 1 | return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark); |
922 | 2 | else |
923 | 2 | return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype); |
924 | 3 | } |
925 | | |
926 | | void |
927 | | _g_signal_init (void) |
928 | 4 | { |
929 | 4 | SIGNAL_LOCK (); |
930 | 4 | if (!g_n_signal_nodes) |
931 | 4 | { |
932 | | /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */ |
933 | 4 | g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL); |
934 | 4 | g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig); |
935 | | |
936 | | /* invalid (0) signal_id */ |
937 | 4 | g_n_signal_nodes = 1; |
938 | 4 | g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes); |
939 | 4 | g_signal_nodes[0] = NULL; |
940 | 4 | g_handlers = g_hash_table_new (handler_hash, handler_equal); |
941 | 4 | } |
942 | 4 | SIGNAL_UNLOCK (); |
943 | 4 | } |
944 | | |
945 | | void |
946 | | _g_signals_destroy (GType itype) |
947 | 0 | { |
948 | 0 | guint i; |
949 | | |
950 | 0 | SIGNAL_LOCK (); |
951 | 0 | for (i = 1; i < g_n_signal_nodes; i++) |
952 | 0 | { |
953 | 0 | SignalNode *node = g_signal_nodes[i]; |
954 | | |
955 | 0 | if (node->itype == itype) |
956 | 0 | { |
957 | 0 | if (node->destroyed) |
958 | 0 | g_critical (G_STRLOC ": signal \"%s\" of type '%s' already destroyed", |
959 | 0 | node->name, |
960 | 0 | type_debug_name (node->itype)); |
961 | 0 | else |
962 | 0 | signal_destroy_R (node); |
963 | 0 | } |
964 | 0 | } |
965 | 0 | SIGNAL_UNLOCK (); |
966 | 0 | } |
967 | | |
968 | | /** |
969 | | * g_signal_stop_emission: |
970 | | * @instance: (type GObject.Object): the object whose signal handlers you wish to stop. |
971 | | * @signal_id: the signal identifier, as returned by g_signal_lookup(). |
972 | | * @detail: the detail which the signal was emitted with. |
973 | | * |
974 | | * Stops a signal's current emission. |
975 | | * |
976 | | * This will prevent the default method from running, if the signal was |
977 | | * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after" |
978 | | * flag). |
979 | | * |
980 | | * Prints a warning if used on a signal which isn't being emitted. |
981 | | */ |
982 | | void |
983 | | g_signal_stop_emission (gpointer instance, |
984 | | guint signal_id, |
985 | | GQuark detail) |
986 | 0 | { |
987 | 0 | SignalNode *node; |
988 | | |
989 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
990 | 0 | g_return_if_fail (signal_id > 0); |
991 | | |
992 | 0 | SIGNAL_LOCK (); |
993 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
994 | 0 | if (node && detail && !(node->flags & G_SIGNAL_DETAILED)) |
995 | 0 | { |
996 | 0 | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
997 | 0 | SIGNAL_UNLOCK (); |
998 | 0 | return; |
999 | 0 | } |
1000 | 0 | if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) |
1001 | 0 | { |
1002 | 0 | Emission *emission = emission_find (signal_id, detail, instance); |
1003 | | |
1004 | 0 | if (emission) |
1005 | 0 | { |
1006 | 0 | if (emission->state == EMISSION_HOOK) |
1007 | 0 | g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook", |
1008 | 0 | node->name, instance); |
1009 | 0 | else if (emission->state == EMISSION_RUN) |
1010 | 0 | emission->state = EMISSION_STOP; |
1011 | 0 | } |
1012 | 0 | else |
1013 | 0 | g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'", |
1014 | 0 | node->name, instance); |
1015 | 0 | } |
1016 | 0 | else |
1017 | 0 | g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance); |
1018 | 0 | SIGNAL_UNLOCK (); |
1019 | 0 | } |
1020 | | |
1021 | | static void |
1022 | | signal_finalize_hook (GHookList *hook_list, |
1023 | | GHook *hook) |
1024 | 0 | { |
1025 | 0 | GDestroyNotify destroy = hook->destroy; |
1026 | |
|
1027 | 0 | if (destroy) |
1028 | 0 | { |
1029 | 0 | hook->destroy = NULL; |
1030 | 0 | SIGNAL_UNLOCK (); |
1031 | 0 | destroy (hook->data); |
1032 | 0 | SIGNAL_LOCK (); |
1033 | 0 | } |
1034 | 0 | } |
1035 | | |
1036 | | /** |
1037 | | * g_signal_add_emission_hook: |
1038 | | * @signal_id: the signal identifier, as returned by g_signal_lookup(). |
1039 | | * @detail: the detail on which to call the hook. |
1040 | | * @hook_func: (not nullable): a #GSignalEmissionHook function. |
1041 | | * @hook_data: (nullable) (closure hook_func): user data for @hook_func. |
1042 | | * @data_destroy: (nullable) (destroy hook_data): a #GDestroyNotify for @hook_data. |
1043 | | * |
1044 | | * Adds an emission hook for a signal, which will get called for any emission |
1045 | | * of that signal, independent of the instance. This is possible only |
1046 | | * for signals which don't have %G_SIGNAL_NO_HOOKS flag set. |
1047 | | * |
1048 | | * Returns: the hook id, for later use with g_signal_remove_emission_hook(). |
1049 | | */ |
1050 | | gulong |
1051 | | g_signal_add_emission_hook (guint signal_id, |
1052 | | GQuark detail, |
1053 | | GSignalEmissionHook hook_func, |
1054 | | gpointer hook_data, |
1055 | | GDestroyNotify data_destroy) |
1056 | 0 | { |
1057 | 0 | static gulong seq_hook_id = 1; |
1058 | 0 | SignalNode *node; |
1059 | 0 | GHook *hook; |
1060 | 0 | SignalHook *signal_hook; |
1061 | |
|
1062 | 0 | g_return_val_if_fail (signal_id > 0, 0); |
1063 | 0 | g_return_val_if_fail (hook_func != NULL, 0); |
1064 | | |
1065 | 0 | SIGNAL_LOCK (); |
1066 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1067 | 0 | if (!node || node->destroyed) |
1068 | 0 | { |
1069 | 0 | g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id); |
1070 | 0 | SIGNAL_UNLOCK (); |
1071 | 0 | return 0; |
1072 | 0 | } |
1073 | 0 | if (node->flags & G_SIGNAL_NO_HOOKS) |
1074 | 0 | { |
1075 | 0 | g_critical ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id); |
1076 | 0 | SIGNAL_UNLOCK (); |
1077 | 0 | return 0; |
1078 | 0 | } |
1079 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
1080 | 0 | { |
1081 | 0 | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
1082 | 0 | SIGNAL_UNLOCK (); |
1083 | 0 | return 0; |
1084 | 0 | } |
1085 | 0 | node->single_va_closure_is_valid = FALSE; |
1086 | 0 | if (!node->emission_hooks) |
1087 | 0 | { |
1088 | 0 | node->emission_hooks = g_new (GHookList, 1); |
1089 | 0 | g_hook_list_init (node->emission_hooks, sizeof (SignalHook)); |
1090 | 0 | node->emission_hooks->finalize_hook = signal_finalize_hook; |
1091 | 0 | } |
1092 | |
|
1093 | 0 | node_check_deprecated (node); |
1094 | |
|
1095 | 0 | hook = g_hook_alloc (node->emission_hooks); |
1096 | 0 | hook->data = hook_data; |
1097 | 0 | hook->func = (gpointer) hook_func; |
1098 | 0 | hook->destroy = data_destroy; |
1099 | 0 | signal_hook = SIGNAL_HOOK (hook); |
1100 | 0 | signal_hook->detail = detail; |
1101 | 0 | node->emission_hooks->seq_id = seq_hook_id; |
1102 | 0 | g_hook_append (node->emission_hooks, hook); |
1103 | 0 | seq_hook_id = node->emission_hooks->seq_id; |
1104 | |
|
1105 | 0 | SIGNAL_UNLOCK (); |
1106 | |
|
1107 | 0 | return hook->hook_id; |
1108 | 0 | } |
1109 | | |
1110 | | /** |
1111 | | * g_signal_remove_emission_hook: |
1112 | | * @signal_id: the id of the signal |
1113 | | * @hook_id: the id of the emission hook, as returned by |
1114 | | * g_signal_add_emission_hook() |
1115 | | * |
1116 | | * Deletes an emission hook. |
1117 | | */ |
1118 | | void |
1119 | | g_signal_remove_emission_hook (guint signal_id, |
1120 | | gulong hook_id) |
1121 | 0 | { |
1122 | 0 | SignalNode *node; |
1123 | |
|
1124 | 0 | g_return_if_fail (signal_id > 0); |
1125 | 0 | g_return_if_fail (hook_id > 0); |
1126 | | |
1127 | 0 | SIGNAL_LOCK (); |
1128 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1129 | 0 | if (!node || node->destroyed) |
1130 | 0 | { |
1131 | 0 | g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id); |
1132 | 0 | goto out; |
1133 | 0 | } |
1134 | 0 | else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id)) |
1135 | 0 | g_critical ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id); |
1136 | | |
1137 | 0 | node->single_va_closure_is_valid = FALSE; |
1138 | |
|
1139 | 0 | out: |
1140 | 0 | SIGNAL_UNLOCK (); |
1141 | 0 | } |
1142 | | |
1143 | | static inline guint |
1144 | | signal_parse_name (const gchar *name, |
1145 | | GType itype, |
1146 | | GQuark *detail_p, |
1147 | | gboolean force_quark) |
1148 | 0 | { |
1149 | 0 | const gchar *colon = strchr (name, ':'); |
1150 | 0 | guint signal_id; |
1151 | | |
1152 | 0 | if (!colon) |
1153 | 0 | { |
1154 | 0 | signal_id = signal_id_lookup (name, itype); |
1155 | 0 | if (signal_id && detail_p) |
1156 | 0 | *detail_p = 0; |
1157 | 0 | } |
1158 | 0 | else if (colon[1] == ':') |
1159 | 0 | { |
1160 | 0 | gchar buffer[32]; |
1161 | 0 | guint l = colon - name; |
1162 | | |
1163 | 0 | if (colon[2] == '\0') |
1164 | 0 | return 0; |
1165 | | |
1166 | 0 | if (l < 32) |
1167 | 0 | { |
1168 | 0 | memcpy (buffer, name, l); |
1169 | 0 | buffer[l] = 0; |
1170 | 0 | signal_id = signal_id_lookup (buffer, itype); |
1171 | 0 | } |
1172 | 0 | else |
1173 | 0 | { |
1174 | 0 | gchar *signal = g_new (gchar, l + 1); |
1175 | | |
1176 | 0 | memcpy (signal, name, l); |
1177 | 0 | signal[l] = 0; |
1178 | 0 | signal_id = signal_id_lookup (signal, itype); |
1179 | 0 | g_free (signal); |
1180 | 0 | } |
1181 | | |
1182 | 0 | if (signal_id && detail_p) |
1183 | 0 | *detail_p = (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2); |
1184 | 0 | } |
1185 | 0 | else |
1186 | 0 | signal_id = 0; |
1187 | 0 | return signal_id; |
1188 | 0 | } |
1189 | | |
1190 | | /** |
1191 | | * g_signal_parse_name: |
1192 | | * @detailed_signal: a string of the form "signal-name::detail". |
1193 | | * @itype: The interface/instance type that introduced "signal-name". |
1194 | | * @signal_id_p: (out): Location to store the signal id. |
1195 | | * @detail_p: (out): Location to store the detail quark. |
1196 | | * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail. |
1197 | | * |
1198 | | * Internal function to parse a signal name into its @signal_id |
1199 | | * and @detail quark. |
1200 | | * |
1201 | | * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values. |
1202 | | */ |
1203 | | gboolean |
1204 | | g_signal_parse_name (const gchar *detailed_signal, |
1205 | | GType itype, |
1206 | | guint *signal_id_p, |
1207 | | GQuark *detail_p, |
1208 | | gboolean force_detail_quark) |
1209 | 0 | { |
1210 | 0 | SignalNode *node; |
1211 | 0 | GQuark detail = 0; |
1212 | 0 | guint signal_id; |
1213 | | |
1214 | 0 | g_return_val_if_fail (detailed_signal != NULL, FALSE); |
1215 | 0 | g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE); |
1216 | | |
1217 | 0 | SIGNAL_LOCK (); |
1218 | 0 | signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark); |
1219 | |
|
1220 | 0 | node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL; |
1221 | |
|
1222 | 0 | if (!node || node->destroyed || |
1223 | 0 | (detail && !(node->flags & G_SIGNAL_DETAILED))) |
1224 | 0 | { |
1225 | 0 | SIGNAL_UNLOCK (); |
1226 | 0 | return FALSE; |
1227 | 0 | } |
1228 | | |
1229 | 0 | SIGNAL_UNLOCK (); |
1230 | |
|
1231 | 0 | if (signal_id_p) |
1232 | 0 | *signal_id_p = signal_id; |
1233 | 0 | if (detail_p) |
1234 | 0 | *detail_p = detail; |
1235 | | |
1236 | 0 | return TRUE; |
1237 | 0 | } |
1238 | | |
1239 | | /** |
1240 | | * g_signal_stop_emission_by_name: |
1241 | | * @instance: (type GObject.Object): the object whose signal handlers you wish to stop. |
1242 | | * @detailed_signal: a string of the form "signal-name::detail". |
1243 | | * |
1244 | | * Stops a signal's current emission. |
1245 | | * |
1246 | | * This is just like g_signal_stop_emission() except it will look up the |
1247 | | * signal id for you. |
1248 | | */ |
1249 | | void |
1250 | | g_signal_stop_emission_by_name (gpointer instance, |
1251 | | const gchar *detailed_signal) |
1252 | 0 | { |
1253 | 0 | guint signal_id; |
1254 | 0 | GQuark detail = 0; |
1255 | 0 | GType itype; |
1256 | | |
1257 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
1258 | 0 | g_return_if_fail (detailed_signal != NULL); |
1259 | | |
1260 | 0 | SIGNAL_LOCK (); |
1261 | 0 | itype = G_TYPE_FROM_INSTANCE (instance); |
1262 | 0 | signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); |
1263 | 0 | if (signal_id) |
1264 | 0 | { |
1265 | 0 | SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); |
1266 | | |
1267 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
1268 | 0 | g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal); |
1269 | 0 | else if (!g_type_is_a (itype, node->itype)) |
1270 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
1271 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
1272 | 0 | else |
1273 | 0 | { |
1274 | 0 | Emission *emission = emission_find (signal_id, detail, instance); |
1275 | | |
1276 | 0 | if (emission) |
1277 | 0 | { |
1278 | 0 | if (emission->state == EMISSION_HOOK) |
1279 | 0 | g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook", |
1280 | 0 | node->name, instance); |
1281 | 0 | else if (emission->state == EMISSION_RUN) |
1282 | 0 | emission->state = EMISSION_STOP; |
1283 | 0 | } |
1284 | 0 | else |
1285 | 0 | g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'", |
1286 | 0 | node->name, instance); |
1287 | 0 | } |
1288 | 0 | } |
1289 | 0 | else |
1290 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
1291 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
1292 | 0 | SIGNAL_UNLOCK (); |
1293 | 0 | } |
1294 | | |
1295 | | /** |
1296 | | * g_signal_lookup: |
1297 | | * @name: the signal's name. |
1298 | | * @itype: the type that the signal operates on. |
1299 | | * |
1300 | | * Given the name of the signal and the type of object it connects to, gets |
1301 | | * the signal's identifying integer. Emitting the signal by number is |
1302 | | * somewhat faster than using the name each time. |
1303 | | * |
1304 | | * Also tries the ancestors of the given type. |
1305 | | * |
1306 | | * The type class passed as @itype must already have been instantiated (for |
1307 | | * example, using g_type_class_ref()) for this function to work, as signals are |
1308 | | * always installed during class initialization. |
1309 | | * |
1310 | | * See g_signal_new() for details on allowed signal names. |
1311 | | * |
1312 | | * Returns: the signal's identifying number, or 0 if no signal was found. |
1313 | | */ |
1314 | | guint |
1315 | | g_signal_lookup (const gchar *name, |
1316 | | GType itype) |
1317 | 0 | { |
1318 | 0 | guint signal_id; |
1319 | 0 | g_return_val_if_fail (name != NULL, 0); |
1320 | 0 | g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0); |
1321 | | |
1322 | 0 | SIGNAL_LOCK (); |
1323 | 0 | signal_id = signal_id_lookup (name, itype); |
1324 | 0 | SIGNAL_UNLOCK (); |
1325 | 0 | if (!signal_id) |
1326 | 0 | { |
1327 | | /* give elaborate warnings */ |
1328 | 0 | if (!g_type_name (itype)) |
1329 | 0 | g_critical (G_STRLOC ": unable to look up signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'", |
1330 | 0 | name, itype); |
1331 | 0 | else if (!g_signal_is_valid_name (name)) |
1332 | 0 | g_critical (G_STRLOC ": unable to look up invalid signal name \"%s\" on type '%s'", |
1333 | 0 | name, g_type_name (itype)); |
1334 | 0 | } |
1335 | | |
1336 | 0 | return signal_id; |
1337 | 0 | } |
1338 | | |
1339 | | /** |
1340 | | * g_signal_list_ids: |
1341 | | * @itype: Instance or interface type. |
1342 | | * @n_ids: Location to store the number of signal ids for @itype. |
1343 | | * |
1344 | | * Lists the signals by id that a certain instance or interface type |
1345 | | * created. Further information about the signals can be acquired through |
1346 | | * g_signal_query(). |
1347 | | * |
1348 | | * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs. |
1349 | | */ |
1350 | | guint* |
1351 | | g_signal_list_ids (GType itype, |
1352 | | guint *n_ids) |
1353 | 0 | { |
1354 | 0 | SignalKey *keys; |
1355 | 0 | GArray *result; |
1356 | 0 | guint n_nodes; |
1357 | 0 | guint i; |
1358 | | |
1359 | 0 | g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL); |
1360 | 0 | g_return_val_if_fail (n_ids != NULL, NULL); |
1361 | | |
1362 | 0 | SIGNAL_LOCK (); |
1363 | 0 | keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0); |
1364 | 0 | n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa); |
1365 | 0 | result = g_array_new (FALSE, FALSE, sizeof (guint)); |
1366 | | |
1367 | 0 | for (i = 0; i < n_nodes; i++) |
1368 | 0 | if (keys[i].itype == itype) |
1369 | 0 | { |
1370 | 0 | g_array_append_val (result, keys[i].signal_id); |
1371 | 0 | } |
1372 | 0 | *n_ids = result->len; |
1373 | 0 | SIGNAL_UNLOCK (); |
1374 | 0 | if (!n_nodes) |
1375 | 0 | { |
1376 | | /* give elaborate warnings */ |
1377 | 0 | if (!g_type_name (itype)) |
1378 | 0 | g_critical (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'", |
1379 | 0 | itype); |
1380 | 0 | else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype)) |
1381 | 0 | g_critical (G_STRLOC ": unable to list signals of non instantiatable type '%s'", |
1382 | 0 | g_type_name (itype)); |
1383 | 0 | else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype)) |
1384 | 0 | g_critical (G_STRLOC ": unable to list signals of unloaded type '%s'", |
1385 | 0 | g_type_name (itype)); |
1386 | 0 | } |
1387 | | |
1388 | 0 | return (guint*) g_array_free (result, FALSE); |
1389 | 0 | } |
1390 | | |
1391 | | /** |
1392 | | * g_signal_name: |
1393 | | * @signal_id: the signal's identifying number. |
1394 | | * |
1395 | | * Given the signal's identifier, finds its name. |
1396 | | * |
1397 | | * Two different signals may have the same name, if they have differing types. |
1398 | | * |
1399 | | * Returns: (nullable): the signal name, or %NULL if the signal number was invalid. |
1400 | | */ |
1401 | | const gchar * |
1402 | | g_signal_name (guint signal_id) |
1403 | 0 | { |
1404 | 0 | SignalNode *node; |
1405 | 0 | const gchar *name; |
1406 | | |
1407 | 0 | SIGNAL_LOCK (); |
1408 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1409 | 0 | name = node ? node->name : NULL; |
1410 | 0 | SIGNAL_UNLOCK (); |
1411 | | |
1412 | 0 | return (char*) name; |
1413 | 0 | } |
1414 | | |
1415 | | /** |
1416 | | * g_signal_query: |
1417 | | * @signal_id: The signal id of the signal to query information for. |
1418 | | * @query: (out caller-allocates) (not optional): A user provided structure that is |
1419 | | * filled in with constant values upon success. |
1420 | | * |
1421 | | * Queries the signal system for in-depth information about a |
1422 | | * specific signal. This function will fill in a user-provided |
1423 | | * structure to hold signal-specific information. If an invalid |
1424 | | * signal id is passed in, the @signal_id member of the #GSignalQuery |
1425 | | * is 0. All members filled into the #GSignalQuery structure should |
1426 | | * be considered constant and have to be left untouched. |
1427 | | */ |
1428 | | void |
1429 | | g_signal_query (guint signal_id, |
1430 | | GSignalQuery *query) |
1431 | 0 | { |
1432 | 0 | SignalNode *node; |
1433 | | |
1434 | 0 | g_return_if_fail (query != NULL); |
1435 | | |
1436 | 0 | SIGNAL_LOCK (); |
1437 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1438 | 0 | if (!node || node->destroyed) |
1439 | 0 | query->signal_id = 0; |
1440 | 0 | else |
1441 | 0 | { |
1442 | 0 | query->signal_id = node->signal_id; |
1443 | 0 | query->signal_name = node->name; |
1444 | 0 | query->itype = node->itype; |
1445 | 0 | query->signal_flags = node->flags; |
1446 | 0 | query->return_type = node->return_type; |
1447 | 0 | query->n_params = node->n_params; |
1448 | 0 | query->param_types = node->param_types; |
1449 | 0 | } |
1450 | 0 | SIGNAL_UNLOCK (); |
1451 | 0 | } |
1452 | | |
1453 | | /** |
1454 | | * g_signal_new: |
1455 | | * @signal_name: the name for the signal |
1456 | | * @itype: the type this signal pertains to. It will also pertain to |
1457 | | * types which are derived from this type. |
1458 | | * @signal_flags: a combination of #GSignalFlags specifying detail of when |
1459 | | * the default handler is to be invoked. You should at least specify |
1460 | | * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. |
1461 | | * @class_offset: The offset of the function pointer in the class structure |
1462 | | * for this type. Used to invoke a class method generically. Pass 0 to |
1463 | | * not associate a class method slot with this signal. |
1464 | | * @accumulator: (nullable): the accumulator for this signal; may be %NULL. |
1465 | | * @accu_data: (nullable) (closure accumulator): user data for the @accumulator. |
1466 | | * @c_marshaller: (nullable): the function to translate arrays of parameter |
1467 | | * values to signal emissions into C language callback invocations or %NULL. |
1468 | | * @return_type: the type of return value, or %G_TYPE_NONE for a signal |
1469 | | * without a return value. |
1470 | | * @n_params: the number of parameter types to follow. |
1471 | | * @...: a list of types, one for each parameter. |
1472 | | * |
1473 | | * Creates a new signal. (This is usually done in the class initializer.) |
1474 | | * |
1475 | | * A signal name consists of segments consisting of ASCII letters and |
1476 | | * digits, separated by either the `-` or `_` character. The first |
1477 | | * character of a signal name must be a letter. Names which violate these |
1478 | | * rules lead to undefined behaviour. These are the same rules as for property |
1479 | | * naming (see g_param_spec_internal()). |
1480 | | * |
1481 | | * When registering a signal and looking up a signal, either separator can |
1482 | | * be used, but they cannot be mixed. Using `-` is considerably more efficient. |
1483 | | * Using `_` is discouraged. |
1484 | | * |
1485 | | * If 0 is used for @class_offset subclasses cannot override the class handler |
1486 | | * in their class_init method by doing super_class->signal_handler = my_signal_handler. |
1487 | | * Instead they will have to use g_signal_override_class_handler(). |
1488 | | * |
1489 | | * If @c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as |
1490 | | * the marshaller for this signal. In some simple cases, g_signal_new() |
1491 | | * will use a more optimized c_marshaller and va_marshaller for the signal |
1492 | | * instead of g_cclosure_marshal_generic(). |
1493 | | * |
1494 | | * If @c_marshaller is non-%NULL, you need to also specify a va_marshaller |
1495 | | * using g_signal_set_va_marshaller() or the generic va_marshaller will |
1496 | | * be used. |
1497 | | * |
1498 | | * Returns: the signal id |
1499 | | */ |
1500 | | guint |
1501 | | g_signal_new (const gchar *signal_name, |
1502 | | GType itype, |
1503 | | GSignalFlags signal_flags, |
1504 | | guint class_offset, |
1505 | | GSignalAccumulator accumulator, |
1506 | | gpointer accu_data, |
1507 | | GSignalCMarshaller c_marshaller, |
1508 | | GType return_type, |
1509 | | guint n_params, |
1510 | | ...) |
1511 | 3 | { |
1512 | 3 | va_list args; |
1513 | 3 | guint signal_id; |
1514 | | |
1515 | 3 | g_return_val_if_fail (signal_name != NULL, 0); |
1516 | | |
1517 | 3 | va_start (args, n_params); |
1518 | | |
1519 | 3 | signal_id = g_signal_new_valist (signal_name, itype, signal_flags, |
1520 | 3 | class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL, |
1521 | 3 | accumulator, accu_data, c_marshaller, |
1522 | 3 | return_type, n_params, args); |
1523 | | |
1524 | 3 | va_end (args); |
1525 | | |
1526 | 3 | return signal_id; |
1527 | 3 | } |
1528 | | |
1529 | | /** |
1530 | | * g_signal_new_class_handler: |
1531 | | * @signal_name: the name for the signal |
1532 | | * @itype: the type this signal pertains to. It will also pertain to |
1533 | | * types which are derived from this type. |
1534 | | * @signal_flags: a combination of #GSignalFlags specifying detail of when |
1535 | | * the default handler is to be invoked. You should at least specify |
1536 | | * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. |
1537 | | * @class_handler: (nullable): a #GCallback which acts as class implementation of |
1538 | | * this signal. Used to invoke a class method generically. Pass %NULL to |
1539 | | * not associate a class method with this signal. |
1540 | | * @accumulator: (nullable): the accumulator for this signal; may be %NULL. |
1541 | | * @accu_data: (nullable) (closure accumulator): user data for the @accumulator. |
1542 | | * @c_marshaller: (nullable): the function to translate arrays of parameter |
1543 | | * values to signal emissions into C language callback invocations or %NULL. |
1544 | | * @return_type: the type of return value, or %G_TYPE_NONE for a signal |
1545 | | * without a return value. |
1546 | | * @n_params: the number of parameter types to follow. |
1547 | | * @...: a list of types, one for each parameter. |
1548 | | * |
1549 | | * Creates a new signal. (This is usually done in the class initializer.) |
1550 | | * |
1551 | | * This is a variant of g_signal_new() that takes a C callback instead |
1552 | | * of a class offset for the signal's class handler. This function |
1553 | | * doesn't need a function pointer exposed in the class structure of |
1554 | | * an object definition, instead the function pointer is passed |
1555 | | * directly and can be overridden by derived classes with |
1556 | | * g_signal_override_class_closure() or |
1557 | | * g_signal_override_class_handler() and chained to with |
1558 | | * g_signal_chain_from_overridden() or |
1559 | | * g_signal_chain_from_overridden_handler(). |
1560 | | * |
1561 | | * See g_signal_new() for information about signal names. |
1562 | | * |
1563 | | * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as |
1564 | | * the marshaller for this signal. |
1565 | | * |
1566 | | * Returns: the signal id |
1567 | | * |
1568 | | * Since: 2.18 |
1569 | | */ |
1570 | | guint |
1571 | | g_signal_new_class_handler (const gchar *signal_name, |
1572 | | GType itype, |
1573 | | GSignalFlags signal_flags, |
1574 | | GCallback class_handler, |
1575 | | GSignalAccumulator accumulator, |
1576 | | gpointer accu_data, |
1577 | | GSignalCMarshaller c_marshaller, |
1578 | | GType return_type, |
1579 | | guint n_params, |
1580 | | ...) |
1581 | 0 | { |
1582 | 0 | va_list args; |
1583 | 0 | guint signal_id; |
1584 | |
|
1585 | 0 | g_return_val_if_fail (signal_name != NULL, 0); |
1586 | | |
1587 | 0 | va_start (args, n_params); |
1588 | |
|
1589 | 0 | signal_id = g_signal_new_valist (signal_name, itype, signal_flags, |
1590 | 0 | class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL, |
1591 | 0 | accumulator, accu_data, c_marshaller, |
1592 | 0 | return_type, n_params, args); |
1593 | |
|
1594 | 0 | va_end (args); |
1595 | |
|
1596 | 0 | return signal_id; |
1597 | 0 | } |
1598 | | |
1599 | | static inline ClassClosure* |
1600 | | signal_find_class_closure (SignalNode *node, |
1601 | | GType itype) |
1602 | 0 | { |
1603 | 0 | GBSearchArray *bsa = node->class_closure_bsa; |
1604 | 0 | ClassClosure *cc; |
1605 | |
|
1606 | 0 | if (bsa) |
1607 | 0 | { |
1608 | 0 | ClassClosure key; |
1609 | | |
1610 | | /* cc->instance_type is 0 for default closure */ |
1611 | |
|
1612 | 0 | if (g_bsearch_array_get_n_nodes (bsa) == 1) |
1613 | 0 | { |
1614 | 0 | cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0); |
1615 | 0 | if (cc && cc->instance_type == 0) /* check for default closure */ |
1616 | 0 | return cc; |
1617 | 0 | } |
1618 | | |
1619 | 0 | key.instance_type = itype; |
1620 | 0 | cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key); |
1621 | 0 | while (!cc && key.instance_type) |
1622 | 0 | { |
1623 | 0 | key.instance_type = g_type_parent (key.instance_type); |
1624 | 0 | cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key); |
1625 | 0 | } |
1626 | 0 | } |
1627 | 0 | else |
1628 | 0 | cc = NULL; |
1629 | 0 | return cc; |
1630 | 0 | } |
1631 | | |
1632 | | static inline GClosure* |
1633 | | signal_lookup_closure (SignalNode *node, |
1634 | | GTypeInstance *instance) |
1635 | 0 | { |
1636 | 0 | ClassClosure *cc; |
1637 | |
|
1638 | 0 | cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance)); |
1639 | 0 | return cc ? cc->closure : NULL; |
1640 | 0 | } |
1641 | | |
1642 | | static void |
1643 | | signal_add_class_closure (SignalNode *node, |
1644 | | GType itype, |
1645 | | GClosure *closure) |
1646 | 3 | { |
1647 | 3 | ClassClosure key; |
1648 | | |
1649 | 3 | node->single_va_closure_is_valid = FALSE; |
1650 | | |
1651 | 3 | if (!node->class_closure_bsa) |
1652 | 3 | node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig); |
1653 | 3 | key.instance_type = itype; |
1654 | 3 | key.closure = g_closure_ref (closure); |
1655 | 3 | node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa, |
1656 | 3 | &g_class_closure_bconfig, |
1657 | 3 | &key); |
1658 | 3 | g_closure_sink (closure); |
1659 | 3 | if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure)) |
1660 | 3 | { |
1661 | 3 | g_closure_set_marshal (closure, node->c_marshaller); |
1662 | 3 | if (node->va_marshaller) |
1663 | 3 | _g_closure_set_va_marshal (closure, node->va_marshaller); |
1664 | 3 | } |
1665 | 3 | } |
1666 | | |
1667 | | /** |
1668 | | * g_signal_newv: |
1669 | | * @signal_name: the name for the signal |
1670 | | * @itype: the type this signal pertains to. It will also pertain to |
1671 | | * types which are derived from this type |
1672 | | * @signal_flags: a combination of #GSignalFlags specifying detail of when |
1673 | | * the default handler is to be invoked. You should at least specify |
1674 | | * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST |
1675 | | * @class_closure: (nullable): The closure to invoke on signal emission; |
1676 | | * may be %NULL |
1677 | | * @accumulator: (nullable): the accumulator for this signal; may be %NULL |
1678 | | * @accu_data: (nullable) (closure accumulator): user data for the @accumulator |
1679 | | * @c_marshaller: (nullable): the function to translate arrays of |
1680 | | * parameter values to signal emissions into C language callback |
1681 | | * invocations or %NULL |
1682 | | * @return_type: the type of return value, or %G_TYPE_NONE for a signal |
1683 | | * without a return value |
1684 | | * @n_params: the length of @param_types |
1685 | | * @param_types: (array length=n_params) (nullable): an array of types, one for |
1686 | | * each parameter (may be %NULL if @n_params is zero) |
1687 | | * |
1688 | | * Creates a new signal. (This is usually done in the class initializer.) |
1689 | | * |
1690 | | * See g_signal_new() for details on allowed signal names. |
1691 | | * |
1692 | | * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as |
1693 | | * the marshaller for this signal. |
1694 | | * |
1695 | | * Returns: the signal id |
1696 | | */ |
1697 | | guint |
1698 | | g_signal_newv (const gchar *signal_name, |
1699 | | GType itype, |
1700 | | GSignalFlags signal_flags, |
1701 | | GClosure *class_closure, |
1702 | | GSignalAccumulator accumulator, |
1703 | | gpointer accu_data, |
1704 | | GSignalCMarshaller c_marshaller, |
1705 | | GType return_type, |
1706 | | guint n_params, |
1707 | | GType *param_types) |
1708 | 3 | { |
1709 | 3 | const gchar *name; |
1710 | 3 | gchar *signal_name_copy = NULL; |
1711 | 3 | guint signal_id, i; |
1712 | 3 | SignalNode *node; |
1713 | 3 | GSignalCMarshaller builtin_c_marshaller; |
1714 | 3 | GSignalCVaMarshaller builtin_va_marshaller; |
1715 | 3 | GSignalCVaMarshaller va_marshaller; |
1716 | | |
1717 | 3 | g_return_val_if_fail (signal_name != NULL, 0); |
1718 | 3 | g_return_val_if_fail (g_signal_is_valid_name (signal_name), 0); |
1719 | 3 | g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0); |
1720 | 3 | if (n_params) |
1721 | 3 | g_return_val_if_fail (param_types != NULL, 0); |
1722 | 3 | g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0); |
1723 | 3 | if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE)) |
1724 | 3 | g_return_val_if_fail (accumulator == NULL, 0); |
1725 | 3 | if (!accumulator) |
1726 | 3 | g_return_val_if_fail (accu_data == NULL, 0); |
1727 | 3 | g_return_val_if_fail ((signal_flags & G_SIGNAL_ACCUMULATOR_FIRST_RUN) == 0, 0); |
1728 | | |
1729 | 3 | if (!is_canonical (signal_name)) |
1730 | 0 | { |
1731 | 0 | signal_name_copy = g_strdup (signal_name); |
1732 | 0 | canonicalize_key (signal_name_copy); |
1733 | 0 | name = signal_name_copy; |
1734 | 0 | } |
1735 | 3 | else |
1736 | 3 | { |
1737 | 3 | name = signal_name; |
1738 | 3 | } |
1739 | | |
1740 | 3 | SIGNAL_LOCK (); |
1741 | | |
1742 | 3 | signal_id = signal_id_lookup (name, itype); |
1743 | 3 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1744 | 3 | if (node && !node->destroyed) |
1745 | 0 | { |
1746 | 0 | g_critical (G_STRLOC ": signal \"%s\" already exists in the '%s' %s", |
1747 | 0 | name, |
1748 | 0 | type_debug_name (node->itype), |
1749 | 0 | G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry"); |
1750 | 0 | g_free (signal_name_copy); |
1751 | 0 | SIGNAL_UNLOCK (); |
1752 | 0 | return 0; |
1753 | 0 | } |
1754 | 3 | if (node && node->itype != itype) |
1755 | 0 | { |
1756 | 0 | g_critical (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'", |
1757 | 0 | name, |
1758 | 0 | type_debug_name (itype), |
1759 | 0 | type_debug_name (node->itype)); |
1760 | 0 | g_free (signal_name_copy); |
1761 | 0 | SIGNAL_UNLOCK (); |
1762 | 0 | return 0; |
1763 | 0 | } |
1764 | 7 | for (i = 0; i < n_params; i++) |
1765 | 4 | if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE)) |
1766 | 0 | { |
1767 | 0 | g_critical (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type", |
1768 | 0 | i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name); |
1769 | 0 | g_free (signal_name_copy); |
1770 | 0 | SIGNAL_UNLOCK (); |
1771 | 0 | return 0; |
1772 | 0 | } |
1773 | 3 | if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE)) |
1774 | 0 | { |
1775 | 0 | g_critical (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type", |
1776 | 0 | type_debug_name (return_type), type_debug_name (itype), name); |
1777 | 0 | g_free (signal_name_copy); |
1778 | 0 | SIGNAL_UNLOCK (); |
1779 | 0 | return 0; |
1780 | 0 | } |
1781 | | |
1782 | | /* setup permanent portion of signal node */ |
1783 | 3 | if (!node) |
1784 | 3 | { |
1785 | 3 | SignalKey key; |
1786 | | |
1787 | 3 | signal_id = g_n_signal_nodes++; |
1788 | 3 | node = g_new (SignalNode, 1); |
1789 | 3 | node->signal_id = signal_id; |
1790 | 3 | g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes); |
1791 | 3 | g_signal_nodes[signal_id] = node; |
1792 | 3 | node->itype = itype; |
1793 | 3 | key.itype = itype; |
1794 | 3 | key.signal_id = signal_id; |
1795 | 3 | node->name = g_intern_string (name); |
1796 | 3 | key.quark = g_quark_from_string (name); |
1797 | 3 | g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key); |
1798 | | |
1799 | 3 | TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype)); |
1800 | 3 | } |
1801 | 3 | node->destroyed = FALSE; |
1802 | | |
1803 | | /* setup reinitializable portion */ |
1804 | 3 | node->single_va_closure_is_valid = FALSE; |
1805 | 3 | node->flags = signal_flags & G_SIGNAL_FLAGS_MASK; |
1806 | 3 | node->n_params = n_params; |
1807 | 3 | node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params); |
1808 | 3 | node->return_type = return_type; |
1809 | 3 | node->class_closure_bsa = NULL; |
1810 | 3 | if (accumulator) |
1811 | 0 | { |
1812 | 0 | node->accumulator = g_new (SignalAccumulator, 1); |
1813 | 0 | node->accumulator->func = accumulator; |
1814 | 0 | node->accumulator->data = accu_data; |
1815 | 0 | } |
1816 | 3 | else |
1817 | 3 | node->accumulator = NULL; |
1818 | | |
1819 | 3 | builtin_c_marshaller = NULL; |
1820 | 3 | builtin_va_marshaller = NULL; |
1821 | | |
1822 | | /* Pick up built-in va marshallers for standard types, and |
1823 | | instead of generic marshaller if no marshaller specified */ |
1824 | 3 | if (n_params == 0 && return_type == G_TYPE_NONE) |
1825 | 0 | { |
1826 | 0 | builtin_c_marshaller = g_cclosure_marshal_VOID__VOID; |
1827 | 0 | builtin_va_marshaller = g_cclosure_marshal_VOID__VOIDv; |
1828 | 0 | } |
1829 | 3 | else if (n_params == 1 && return_type == G_TYPE_NONE) |
1830 | 2 | { |
1831 | 2 | #define ADD_CHECK(__type__) \ |
1832 | 28 | else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \ |
1833 | 26 | { \ |
1834 | 2 | builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \ |
1835 | 2 | builtin_va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \ |
1836 | 2 | } |
1837 | | |
1838 | 2 | if (0) {} |
1839 | 2 | ADD_CHECK (BOOLEAN) |
1840 | 2 | ADD_CHECK (CHAR) |
1841 | 2 | ADD_CHECK (UCHAR) |
1842 | 2 | ADD_CHECK (INT) |
1843 | 2 | ADD_CHECK (UINT) |
1844 | 2 | ADD_CHECK (LONG) |
1845 | 2 | ADD_CHECK (ULONG) |
1846 | 2 | ADD_CHECK (ENUM) |
1847 | 2 | ADD_CHECK (FLAGS) |
1848 | 2 | ADD_CHECK (FLOAT) |
1849 | 2 | ADD_CHECK (DOUBLE) |
1850 | 2 | ADD_CHECK (STRING) |
1851 | 2 | ADD_CHECK (PARAM) |
1852 | 2 | ADD_CHECK (BOXED) |
1853 | 0 | ADD_CHECK (POINTER) |
1854 | 0 | ADD_CHECK (OBJECT) |
1855 | 0 | ADD_CHECK (VARIANT) |
1856 | 2 | } |
1857 | | |
1858 | 3 | if (c_marshaller == NULL) |
1859 | 3 | { |
1860 | 3 | if (builtin_c_marshaller) |
1861 | 2 | { |
1862 | 2 | c_marshaller = builtin_c_marshaller; |
1863 | 2 | va_marshaller = builtin_va_marshaller; |
1864 | 2 | } |
1865 | 1 | else |
1866 | 1 | { |
1867 | 1 | c_marshaller = g_cclosure_marshal_generic; |
1868 | 1 | va_marshaller = g_cclosure_marshal_generic_va; |
1869 | 1 | } |
1870 | 3 | } |
1871 | 0 | else |
1872 | 0 | va_marshaller = NULL; |
1873 | | |
1874 | 3 | node->c_marshaller = c_marshaller; |
1875 | 3 | node->va_marshaller = va_marshaller; |
1876 | 3 | node->emission_hooks = NULL; |
1877 | 3 | if (class_closure) |
1878 | 3 | signal_add_class_closure (node, 0, class_closure); |
1879 | | |
1880 | 3 | SIGNAL_UNLOCK (); |
1881 | | |
1882 | 3 | g_free (signal_name_copy); |
1883 | | |
1884 | 3 | return signal_id; |
1885 | 3 | } |
1886 | | |
1887 | | /** |
1888 | | * g_signal_set_va_marshaller: |
1889 | | * @signal_id: the signal id |
1890 | | * @instance_type: the instance type on which to set the marshaller. |
1891 | | * @va_marshaller: the marshaller to set. |
1892 | | * |
1893 | | * Change the #GSignalCVaMarshaller used for a given signal. This is a |
1894 | | * specialised form of the marshaller that can often be used for the |
1895 | | * common case of a single connected signal handler and avoids the |
1896 | | * overhead of #GValue. Its use is optional. |
1897 | | * |
1898 | | * Since: 2.32 |
1899 | | */ |
1900 | | void |
1901 | | g_signal_set_va_marshaller (guint signal_id, |
1902 | | GType instance_type, |
1903 | | GSignalCVaMarshaller va_marshaller) |
1904 | 0 | { |
1905 | 0 | SignalNode *node; |
1906 | | |
1907 | 0 | g_return_if_fail (signal_id > 0); |
1908 | 0 | g_return_if_fail (va_marshaller != NULL); |
1909 | | |
1910 | 0 | SIGNAL_LOCK (); |
1911 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
1912 | 0 | if (node) |
1913 | 0 | { |
1914 | 0 | node->va_marshaller = va_marshaller; |
1915 | 0 | if (node->class_closure_bsa) |
1916 | 0 | { |
1917 | 0 | ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0); |
1918 | 0 | if (cc->closure->marshal == node->c_marshaller) |
1919 | 0 | _g_closure_set_va_marshal (cc->closure, va_marshaller); |
1920 | 0 | } |
1921 | |
|
1922 | 0 | node->single_va_closure_is_valid = FALSE; |
1923 | 0 | } |
1924 | |
|
1925 | 0 | SIGNAL_UNLOCK (); |
1926 | 0 | } |
1927 | | |
1928 | | |
1929 | | /** |
1930 | | * g_signal_new_valist: |
1931 | | * @signal_name: the name for the signal |
1932 | | * @itype: the type this signal pertains to. It will also pertain to |
1933 | | * types which are derived from this type. |
1934 | | * @signal_flags: a combination of #GSignalFlags specifying detail of when |
1935 | | * the default handler is to be invoked. You should at least specify |
1936 | | * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. |
1937 | | * @class_closure: (nullable): The closure to invoke on signal emission; may be %NULL. |
1938 | | * @accumulator: (nullable): the accumulator for this signal; may be %NULL. |
1939 | | * @accu_data: (nullable) (closure accumulator): user data for the @accumulator. |
1940 | | * @c_marshaller: (nullable): the function to translate arrays of parameter |
1941 | | * values to signal emissions into C language callback invocations or %NULL. |
1942 | | * @return_type: the type of return value, or %G_TYPE_NONE for a signal |
1943 | | * without a return value. |
1944 | | * @n_params: the number of parameter types in @args. |
1945 | | * @args: va_list of #GType, one for each parameter. |
1946 | | * |
1947 | | * Creates a new signal. (This is usually done in the class initializer.) |
1948 | | * |
1949 | | * See g_signal_new() for details on allowed signal names. |
1950 | | * |
1951 | | * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as |
1952 | | * the marshaller for this signal. |
1953 | | * |
1954 | | * Returns: the signal id |
1955 | | */ |
1956 | | guint |
1957 | | g_signal_new_valist (const gchar *signal_name, |
1958 | | GType itype, |
1959 | | GSignalFlags signal_flags, |
1960 | | GClosure *class_closure, |
1961 | | GSignalAccumulator accumulator, |
1962 | | gpointer accu_data, |
1963 | | GSignalCMarshaller c_marshaller, |
1964 | | GType return_type, |
1965 | | guint n_params, |
1966 | | va_list args) |
1967 | 3 | { |
1968 | | /* Somewhat arbitrarily reserve 200 bytes. That should cover the majority |
1969 | | * of cases where n_params is small and still be small enough for what we |
1970 | | * want to put on the stack. */ |
1971 | 3 | GType param_types_stack[200 / sizeof (GType)]; |
1972 | 3 | GType *param_types_heap = NULL; |
1973 | 3 | GType *param_types; |
1974 | 3 | guint i; |
1975 | 3 | guint signal_id; |
1976 | | |
1977 | 3 | param_types = param_types_stack; |
1978 | 3 | if (n_params > 0) |
1979 | 3 | { |
1980 | 3 | if (G_UNLIKELY (n_params > G_N_ELEMENTS (param_types_stack))) |
1981 | 0 | { |
1982 | 0 | param_types_heap = g_new (GType, n_params); |
1983 | 0 | param_types = param_types_heap; |
1984 | 0 | } |
1985 | | |
1986 | 7 | for (i = 0; i < n_params; i++) |
1987 | 4 | param_types[i] = va_arg (args, GType); |
1988 | 3 | } |
1989 | | |
1990 | 3 | signal_id = g_signal_newv (signal_name, itype, signal_flags, |
1991 | 3 | class_closure, accumulator, accu_data, c_marshaller, |
1992 | 3 | return_type, n_params, param_types); |
1993 | 3 | g_free (param_types_heap); |
1994 | | |
1995 | 3 | return signal_id; |
1996 | 3 | } |
1997 | | |
1998 | | static void |
1999 | | signal_destroy_R (SignalNode *signal_node) |
2000 | 0 | { |
2001 | 0 | SignalNode node = *signal_node; |
2002 | |
|
2003 | 0 | signal_node->destroyed = TRUE; |
2004 | | |
2005 | | /* reentrancy caution, zero out real contents first */ |
2006 | 0 | signal_node->single_va_closure_is_valid = FALSE; |
2007 | 0 | signal_node->n_params = 0; |
2008 | 0 | signal_node->param_types = NULL; |
2009 | 0 | signal_node->return_type = 0; |
2010 | 0 | signal_node->class_closure_bsa = NULL; |
2011 | 0 | signal_node->accumulator = NULL; |
2012 | 0 | signal_node->c_marshaller = NULL; |
2013 | 0 | signal_node->va_marshaller = NULL; |
2014 | 0 | signal_node->emission_hooks = NULL; |
2015 | | |
2016 | | #ifdef G_ENABLE_DEBUG |
2017 | | /* check current emissions */ |
2018 | | { |
2019 | | Emission *emission; |
2020 | | |
2021 | | for (emission = g_emissions; emission; emission = emission->next) |
2022 | | if (emission->ihint.signal_id == node.signal_id) |
2023 | | g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')", |
2024 | | node.name, emission->instance); |
2025 | | } |
2026 | | #endif |
2027 | | |
2028 | | /* free contents that need to |
2029 | | */ |
2030 | 0 | SIGNAL_UNLOCK (); |
2031 | 0 | g_free (node.param_types); |
2032 | 0 | if (node.class_closure_bsa) |
2033 | 0 | { |
2034 | 0 | guint i; |
2035 | |
|
2036 | 0 | for (i = 0; i < node.class_closure_bsa->n_nodes; i++) |
2037 | 0 | { |
2038 | 0 | ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i); |
2039 | |
|
2040 | 0 | g_closure_unref (cc->closure); |
2041 | 0 | } |
2042 | 0 | g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig); |
2043 | 0 | } |
2044 | 0 | g_free (node.accumulator); |
2045 | 0 | if (node.emission_hooks) |
2046 | 0 | { |
2047 | 0 | g_hook_list_clear (node.emission_hooks); |
2048 | 0 | g_free (node.emission_hooks); |
2049 | 0 | } |
2050 | 0 | SIGNAL_LOCK (); |
2051 | 0 | } |
2052 | | |
2053 | | /** |
2054 | | * g_signal_override_class_closure: |
2055 | | * @signal_id: the signal id |
2056 | | * @instance_type: the instance type on which to override the class closure |
2057 | | * for the signal. |
2058 | | * @class_closure: the closure. |
2059 | | * |
2060 | | * Overrides the class closure (i.e. the default handler) for the given signal |
2061 | | * for emissions on instances of @instance_type. @instance_type must be derived |
2062 | | * from the type to which the signal belongs. |
2063 | | * |
2064 | | * See g_signal_chain_from_overridden() and |
2065 | | * g_signal_chain_from_overridden_handler() for how to chain up to the |
2066 | | * parent class closure from inside the overridden one. |
2067 | | */ |
2068 | | void |
2069 | | g_signal_override_class_closure (guint signal_id, |
2070 | | GType instance_type, |
2071 | | GClosure *class_closure) |
2072 | 0 | { |
2073 | 0 | SignalNode *node; |
2074 | | |
2075 | 0 | g_return_if_fail (signal_id > 0); |
2076 | 0 | g_return_if_fail (class_closure != NULL); |
2077 | | |
2078 | 0 | SIGNAL_LOCK (); |
2079 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
2080 | 0 | node_check_deprecated (node); |
2081 | 0 | if (!g_type_is_a (instance_type, node->itype)) |
2082 | 0 | g_critical ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id); |
2083 | 0 | else |
2084 | 0 | { |
2085 | 0 | ClassClosure *cc = signal_find_class_closure (node, instance_type); |
2086 | | |
2087 | 0 | if (cc && cc->instance_type == instance_type) |
2088 | 0 | g_critical ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id); |
2089 | 0 | else |
2090 | 0 | signal_add_class_closure (node, instance_type, class_closure); |
2091 | 0 | } |
2092 | 0 | SIGNAL_UNLOCK (); |
2093 | 0 | } |
2094 | | |
2095 | | /** |
2096 | | * g_signal_override_class_handler: |
2097 | | * @signal_name: the name for the signal |
2098 | | * @instance_type: the instance type on which to override the class handler |
2099 | | * for the signal. |
2100 | | * @class_handler: the handler. |
2101 | | * |
2102 | | * Overrides the class closure (i.e. the default handler) for the |
2103 | | * given signal for emissions on instances of @instance_type with |
2104 | | * callback @class_handler. @instance_type must be derived from the |
2105 | | * type to which the signal belongs. |
2106 | | * |
2107 | | * See g_signal_chain_from_overridden() and |
2108 | | * g_signal_chain_from_overridden_handler() for how to chain up to the |
2109 | | * parent class closure from inside the overridden one. |
2110 | | * |
2111 | | * Since: 2.18 |
2112 | | */ |
2113 | | void |
2114 | | g_signal_override_class_handler (const gchar *signal_name, |
2115 | | GType instance_type, |
2116 | | GCallback class_handler) |
2117 | 0 | { |
2118 | 0 | guint signal_id; |
2119 | |
|
2120 | 0 | g_return_if_fail (signal_name != NULL); |
2121 | 0 | g_return_if_fail (instance_type != G_TYPE_NONE); |
2122 | 0 | g_return_if_fail (class_handler != NULL); |
2123 | | |
2124 | 0 | signal_id = g_signal_lookup (signal_name, instance_type); |
2125 | |
|
2126 | 0 | if (signal_id) |
2127 | 0 | g_signal_override_class_closure (signal_id, instance_type, |
2128 | 0 | g_cclosure_new (class_handler, NULL, NULL)); |
2129 | 0 | else |
2130 | 0 | g_critical ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'", |
2131 | 0 | G_STRLOC, signal_name, instance_type); |
2132 | |
|
2133 | 0 | } |
2134 | | |
2135 | | /** |
2136 | | * g_signal_chain_from_overridden: |
2137 | | * @instance_and_params: (array) the argument list of the signal emission. |
2138 | | * The first element in the array is a #GValue for the instance the signal |
2139 | | * is being emitted on. The rest are any arguments to be passed to the signal. |
2140 | | * @return_value: Location for the return value. |
2141 | | * |
2142 | | * Calls the original class closure of a signal. This function should only |
2143 | | * be called from an overridden class closure; see |
2144 | | * g_signal_override_class_closure() and |
2145 | | * g_signal_override_class_handler(). |
2146 | | */ |
2147 | | void |
2148 | | g_signal_chain_from_overridden (const GValue *instance_and_params, |
2149 | | GValue *return_value) |
2150 | 0 | { |
2151 | 0 | GType chain_type = 0, restore_type = 0; |
2152 | 0 | Emission *emission = NULL; |
2153 | 0 | GClosure *closure = NULL; |
2154 | 0 | guint n_params = 0; |
2155 | 0 | gpointer instance; |
2156 | | |
2157 | 0 | g_return_if_fail (instance_and_params != NULL); |
2158 | 0 | instance = g_value_peek_pointer (instance_and_params); |
2159 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2160 | | |
2161 | 0 | SIGNAL_LOCK (); |
2162 | 0 | emission = emission_find_innermost (instance); |
2163 | 0 | if (emission) |
2164 | 0 | { |
2165 | 0 | SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id); |
2166 | | |
2167 | 0 | g_assert (node != NULL); /* paranoid */ |
2168 | | |
2169 | | /* we should probably do the same parameter checks as g_signal_emit() here. |
2170 | | */ |
2171 | 0 | if (emission->chain_type != G_TYPE_NONE) |
2172 | 0 | { |
2173 | 0 | ClassClosure *cc = signal_find_class_closure (node, emission->chain_type); |
2174 | | |
2175 | 0 | g_assert (cc != NULL); /* closure currently in call stack */ |
2176 | | |
2177 | 0 | n_params = node->n_params; |
2178 | 0 | restore_type = cc->instance_type; |
2179 | 0 | cc = signal_find_class_closure (node, g_type_parent (cc->instance_type)); |
2180 | 0 | if (cc && cc->instance_type != restore_type) |
2181 | 0 | { |
2182 | 0 | closure = cc->closure; |
2183 | 0 | chain_type = cc->instance_type; |
2184 | 0 | } |
2185 | 0 | } |
2186 | 0 | else |
2187 | 0 | g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance); |
2188 | 0 | } |
2189 | 0 | else |
2190 | 0 | g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance); |
2191 | | |
2192 | 0 | if (closure) |
2193 | 0 | { |
2194 | 0 | emission->chain_type = chain_type; |
2195 | 0 | SIGNAL_UNLOCK (); |
2196 | 0 | g_closure_invoke (closure, |
2197 | 0 | return_value, |
2198 | 0 | n_params + 1, |
2199 | 0 | instance_and_params, |
2200 | 0 | &emission->ihint); |
2201 | 0 | SIGNAL_LOCK (); |
2202 | 0 | emission->chain_type = restore_type; |
2203 | 0 | } |
2204 | 0 | SIGNAL_UNLOCK (); |
2205 | 0 | } |
2206 | | |
2207 | | /** |
2208 | | * g_signal_chain_from_overridden_handler: (skip) |
2209 | | * @instance: (type GObject.TypeInstance): the instance the signal is being |
2210 | | * emitted on. |
2211 | | * @...: parameters to be passed to the parent class closure, followed by a |
2212 | | * location for the return value. If the return type of the signal |
2213 | | * is %G_TYPE_NONE, the return value location can be omitted. |
2214 | | * |
2215 | | * Calls the original class closure of a signal. This function should |
2216 | | * only be called from an overridden class closure; see |
2217 | | * g_signal_override_class_closure() and |
2218 | | * g_signal_override_class_handler(). |
2219 | | * |
2220 | | * Since: 2.18 |
2221 | | */ |
2222 | | void |
2223 | | g_signal_chain_from_overridden_handler (gpointer instance, |
2224 | | ...) |
2225 | 0 | { |
2226 | 0 | GType chain_type = 0, restore_type = 0; |
2227 | 0 | Emission *emission = NULL; |
2228 | 0 | GClosure *closure = NULL; |
2229 | 0 | SignalNode *node = NULL; |
2230 | 0 | guint n_params = 0; |
2231 | |
|
2232 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2233 | | |
2234 | 0 | SIGNAL_LOCK (); |
2235 | 0 | emission = emission_find_innermost (instance); |
2236 | 0 | if (emission) |
2237 | 0 | { |
2238 | 0 | node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id); |
2239 | |
|
2240 | 0 | g_assert (node != NULL); /* paranoid */ |
2241 | | |
2242 | | /* we should probably do the same parameter checks as g_signal_emit() here. |
2243 | | */ |
2244 | 0 | if (emission->chain_type != G_TYPE_NONE) |
2245 | 0 | { |
2246 | 0 | ClassClosure *cc = signal_find_class_closure (node, emission->chain_type); |
2247 | |
|
2248 | 0 | g_assert (cc != NULL); /* closure currently in call stack */ |
2249 | | |
2250 | 0 | n_params = node->n_params; |
2251 | 0 | restore_type = cc->instance_type; |
2252 | 0 | cc = signal_find_class_closure (node, g_type_parent (cc->instance_type)); |
2253 | 0 | if (cc && cc->instance_type != restore_type) |
2254 | 0 | { |
2255 | 0 | closure = cc->closure; |
2256 | 0 | chain_type = cc->instance_type; |
2257 | 0 | } |
2258 | 0 | } |
2259 | 0 | else |
2260 | 0 | g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance); |
2261 | 0 | } |
2262 | 0 | else |
2263 | 0 | g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance); |
2264 | | |
2265 | 0 | if (closure) |
2266 | 0 | { |
2267 | 0 | GValue *instance_and_params; |
2268 | 0 | GType signal_return_type; |
2269 | 0 | GValue *param_values; |
2270 | 0 | va_list var_args; |
2271 | 0 | guint i; |
2272 | |
|
2273 | 0 | va_start (var_args, instance); |
2274 | |
|
2275 | 0 | signal_return_type = node->return_type; |
2276 | 0 | instance_and_params = g_newa0 (GValue, n_params + 1); |
2277 | 0 | param_values = instance_and_params + 1; |
2278 | |
|
2279 | 0 | for (i = 0; i < node->n_params; i++) |
2280 | 0 | { |
2281 | 0 | gchar *error; |
2282 | 0 | GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
2283 | 0 | gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE; |
2284 | |
|
2285 | 0 | SIGNAL_UNLOCK (); |
2286 | 0 | G_VALUE_COLLECT_INIT (param_values + i, ptype, |
2287 | 0 | var_args, |
2288 | 0 | static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, |
2289 | 0 | &error); |
2290 | 0 | if (error) |
2291 | 0 | { |
2292 | 0 | g_critical ("%s: %s", G_STRLOC, error); |
2293 | 0 | g_free (error); |
2294 | | |
2295 | | /* we purposely leak the value here, it might not be |
2296 | | * in a correct state if an error condition occurred |
2297 | | */ |
2298 | 0 | while (i--) |
2299 | 0 | g_value_unset (param_values + i); |
2300 | |
|
2301 | 0 | va_end (var_args); |
2302 | 0 | return; |
2303 | 0 | } |
2304 | 0 | SIGNAL_LOCK (); |
2305 | 0 | } |
2306 | | |
2307 | 0 | SIGNAL_UNLOCK (); |
2308 | 0 | g_value_init_from_instance (instance_and_params, instance); |
2309 | 0 | SIGNAL_LOCK (); |
2310 | |
|
2311 | 0 | emission->chain_type = chain_type; |
2312 | 0 | SIGNAL_UNLOCK (); |
2313 | |
|
2314 | 0 | if (signal_return_type == G_TYPE_NONE) |
2315 | 0 | { |
2316 | 0 | g_closure_invoke (closure, |
2317 | 0 | NULL, |
2318 | 0 | n_params + 1, |
2319 | 0 | instance_and_params, |
2320 | 0 | &emission->ihint); |
2321 | 0 | } |
2322 | 0 | else |
2323 | 0 | { |
2324 | 0 | GValue return_value = G_VALUE_INIT; |
2325 | 0 | gchar *error = NULL; |
2326 | 0 | GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
2327 | 0 | gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE; |
2328 | |
|
2329 | 0 | g_value_init (&return_value, rtype); |
2330 | |
|
2331 | 0 | g_closure_invoke (closure, |
2332 | 0 | &return_value, |
2333 | 0 | n_params + 1, |
2334 | 0 | instance_and_params, |
2335 | 0 | &emission->ihint); |
2336 | |
|
2337 | 0 | G_VALUE_LCOPY (&return_value, |
2338 | 0 | var_args, |
2339 | 0 | static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, |
2340 | 0 | &error); |
2341 | 0 | if (!error) |
2342 | 0 | { |
2343 | 0 | g_value_unset (&return_value); |
2344 | 0 | } |
2345 | 0 | else |
2346 | 0 | { |
2347 | 0 | g_critical ("%s: %s", G_STRLOC, error); |
2348 | 0 | g_free (error); |
2349 | | |
2350 | | /* we purposely leak the value here, it might not be |
2351 | | * in a correct state if an error condition occurred |
2352 | | */ |
2353 | 0 | } |
2354 | 0 | } |
2355 | | |
2356 | 0 | for (i = 0; i < n_params; i++) |
2357 | 0 | g_value_unset (param_values + i); |
2358 | 0 | g_value_unset (instance_and_params); |
2359 | |
|
2360 | 0 | va_end (var_args); |
2361 | |
|
2362 | 0 | SIGNAL_LOCK (); |
2363 | 0 | emission->chain_type = restore_type; |
2364 | 0 | } |
2365 | 0 | SIGNAL_UNLOCK (); |
2366 | 0 | } |
2367 | | |
2368 | | /** |
2369 | | * g_signal_get_invocation_hint: |
2370 | | * @instance: (type GObject.Object): the instance to query |
2371 | | * |
2372 | | * Returns the invocation hint of the innermost signal emission of instance. |
2373 | | * |
2374 | | * Returns: (transfer none) (nullable): the invocation hint of the innermost |
2375 | | * signal emission, or %NULL if not found. |
2376 | | */ |
2377 | | GSignalInvocationHint* |
2378 | | g_signal_get_invocation_hint (gpointer instance) |
2379 | 0 | { |
2380 | 0 | Emission *emission = NULL; |
2381 | | |
2382 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL); |
2383 | | |
2384 | 0 | SIGNAL_LOCK (); |
2385 | 0 | emission = emission_find_innermost (instance); |
2386 | 0 | SIGNAL_UNLOCK (); |
2387 | | |
2388 | 0 | return emission ? &emission->ihint : NULL; |
2389 | 0 | } |
2390 | | |
2391 | | /** |
2392 | | * g_signal_connect_closure_by_id: |
2393 | | * @instance: (type GObject.Object): the instance to connect to. |
2394 | | * @signal_id: the id of the signal. |
2395 | | * @detail: the detail. |
2396 | | * @closure: (not nullable): the closure to connect. |
2397 | | * @after: whether the handler should be called before or after the |
2398 | | * default handler of the signal. |
2399 | | * |
2400 | | * Connects a closure to a signal for a particular object. |
2401 | | * |
2402 | | * If @closure is a floating reference (see g_closure_sink()), this function |
2403 | | * takes ownership of @closure. |
2404 | | * |
2405 | | * Returns: the handler ID (always greater than 0 for successful connections) |
2406 | | */ |
2407 | | gulong |
2408 | | g_signal_connect_closure_by_id (gpointer instance, |
2409 | | guint signal_id, |
2410 | | GQuark detail, |
2411 | | GClosure *closure, |
2412 | | gboolean after) |
2413 | 0 | { |
2414 | 0 | SignalNode *node; |
2415 | 0 | gulong handler_seq_no = 0; |
2416 | | |
2417 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
2418 | 0 | g_return_val_if_fail (signal_id > 0, 0); |
2419 | 0 | g_return_val_if_fail (closure != NULL, 0); |
2420 | | |
2421 | 0 | SIGNAL_LOCK (); |
2422 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
2423 | 0 | if (node) |
2424 | 0 | { |
2425 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
2426 | 0 | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
2427 | 0 | else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) |
2428 | 0 | g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance); |
2429 | 0 | else |
2430 | 0 | { |
2431 | 0 | Handler *handler = handler_new (signal_id, instance, after); |
2432 | |
|
2433 | 0 | if (G_TYPE_IS_OBJECT (node->itype)) |
2434 | 0 | _g_object_set_has_signal_handler ((GObject *) instance, signal_id); |
2435 | |
|
2436 | 0 | handler_seq_no = handler->sequential_number; |
2437 | 0 | handler->detail = detail; |
2438 | 0 | handler->closure = g_closure_ref (closure); |
2439 | 0 | g_closure_sink (closure); |
2440 | 0 | add_invalid_closure_notify (handler, instance); |
2441 | 0 | handler_insert (signal_id, instance, handler); |
2442 | 0 | if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure)) |
2443 | 0 | { |
2444 | 0 | g_closure_set_marshal (closure, node->c_marshaller); |
2445 | 0 | if (node->va_marshaller) |
2446 | 0 | _g_closure_set_va_marshal (closure, node->va_marshaller); |
2447 | 0 | } |
2448 | 0 | } |
2449 | 0 | } |
2450 | 0 | else |
2451 | 0 | g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance); |
2452 | 0 | SIGNAL_UNLOCK (); |
2453 | | |
2454 | 0 | return handler_seq_no; |
2455 | 0 | } |
2456 | | |
2457 | | /** |
2458 | | * g_signal_connect_closure: |
2459 | | * @instance: (type GObject.Object): the instance to connect to. |
2460 | | * @detailed_signal: a string of the form "signal-name::detail". |
2461 | | * @closure: (not nullable): the closure to connect. |
2462 | | * @after: whether the handler should be called before or after the |
2463 | | * default handler of the signal. |
2464 | | * |
2465 | | * Connects a closure to a signal for a particular object. |
2466 | | * |
2467 | | * If @closure is a floating reference (see g_closure_sink()), this function |
2468 | | * takes ownership of @closure. |
2469 | | * |
2470 | | * Returns: the handler ID (always greater than 0 for successful connections) |
2471 | | */ |
2472 | | gulong |
2473 | | g_signal_connect_closure (gpointer instance, |
2474 | | const gchar *detailed_signal, |
2475 | | GClosure *closure, |
2476 | | gboolean after) |
2477 | 0 | { |
2478 | 0 | guint signal_id; |
2479 | 0 | gulong handler_seq_no = 0; |
2480 | 0 | GQuark detail = 0; |
2481 | 0 | GType itype; |
2482 | |
|
2483 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
2484 | 0 | g_return_val_if_fail (detailed_signal != NULL, 0); |
2485 | 0 | g_return_val_if_fail (closure != NULL, 0); |
2486 | | |
2487 | 0 | SIGNAL_LOCK (); |
2488 | 0 | itype = G_TYPE_FROM_INSTANCE (instance); |
2489 | 0 | signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); |
2490 | 0 | if (signal_id) |
2491 | 0 | { |
2492 | 0 | SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); |
2493 | |
|
2494 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
2495 | 0 | g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal); |
2496 | 0 | else if (!g_type_is_a (itype, node->itype)) |
2497 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
2498 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
2499 | 0 | else |
2500 | 0 | { |
2501 | 0 | Handler *handler = handler_new (signal_id, instance, after); |
2502 | |
|
2503 | 0 | if (G_TYPE_IS_OBJECT (node->itype)) |
2504 | 0 | _g_object_set_has_signal_handler ((GObject *) instance, signal_id); |
2505 | |
|
2506 | 0 | handler_seq_no = handler->sequential_number; |
2507 | 0 | handler->detail = detail; |
2508 | 0 | handler->closure = g_closure_ref (closure); |
2509 | 0 | g_closure_sink (closure); |
2510 | 0 | add_invalid_closure_notify (handler, instance); |
2511 | 0 | handler_insert (signal_id, instance, handler); |
2512 | 0 | if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure)) |
2513 | 0 | { |
2514 | 0 | g_closure_set_marshal (handler->closure, node->c_marshaller); |
2515 | 0 | if (node->va_marshaller) |
2516 | 0 | _g_closure_set_va_marshal (handler->closure, node->va_marshaller); |
2517 | 0 | } |
2518 | 0 | } |
2519 | 0 | } |
2520 | 0 | else |
2521 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
2522 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
2523 | 0 | SIGNAL_UNLOCK (); |
2524 | |
|
2525 | 0 | return handler_seq_no; |
2526 | 0 | } |
2527 | | |
2528 | | static void |
2529 | | node_check_deprecated (const SignalNode *node) |
2530 | 0 | { |
2531 | 0 | static const gchar * g_enable_diagnostic = NULL; |
2532 | |
|
2533 | 0 | if (G_UNLIKELY (!g_enable_diagnostic)) |
2534 | 0 | { |
2535 | 0 | g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC"); |
2536 | 0 | if (!g_enable_diagnostic) |
2537 | 0 | g_enable_diagnostic = "0"; |
2538 | 0 | } |
2539 | |
|
2540 | 0 | if (g_enable_diagnostic[0] == '1') |
2541 | 0 | { |
2542 | 0 | if (node->flags & G_SIGNAL_DEPRECATED) |
2543 | 0 | { |
2544 | 0 | g_warning ("The signal %s::%s is deprecated and shouldn't be used " |
2545 | 0 | "anymore. It will be removed in a future version.", |
2546 | 0 | type_debug_name (node->itype), node->name); |
2547 | 0 | } |
2548 | 0 | } |
2549 | 0 | } |
2550 | | |
2551 | | /** |
2552 | | * g_signal_connect_data: |
2553 | | * @instance: (type GObject.Object): the instance to connect to. |
2554 | | * @detailed_signal: a string of the form "signal-name::detail". |
2555 | | * @c_handler: (not nullable): the #GCallback to connect. |
2556 | | * @data: (nullable) (closure c_handler): data to pass to @c_handler calls. |
2557 | | * @destroy_data: (nullable) (destroy data): a #GClosureNotify for @data. |
2558 | | * @connect_flags: a combination of #GConnectFlags. |
2559 | | * |
2560 | | * Connects a #GCallback function to a signal for a particular object. Similar |
2561 | | * to g_signal_connect(), but allows to provide a #GClosureNotify for the data |
2562 | | * which will be called when the signal handler is disconnected and no longer |
2563 | | * used. Specify @connect_flags if you need `..._after()` or |
2564 | | * `..._swapped()` variants of this function. |
2565 | | * |
2566 | | * Returns: the handler ID (always greater than 0 for successful connections) |
2567 | | */ |
2568 | | gulong |
2569 | | g_signal_connect_data (gpointer instance, |
2570 | | const gchar *detailed_signal, |
2571 | | GCallback c_handler, |
2572 | | gpointer data, |
2573 | | GClosureNotify destroy_data, |
2574 | | GConnectFlags connect_flags) |
2575 | 0 | { |
2576 | 0 | guint signal_id; |
2577 | 0 | gulong handler_seq_no = 0; |
2578 | 0 | GQuark detail = 0; |
2579 | 0 | GType itype; |
2580 | 0 | gboolean swapped, after; |
2581 | | |
2582 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
2583 | 0 | g_return_val_if_fail (detailed_signal != NULL, 0); |
2584 | 0 | g_return_val_if_fail (c_handler != NULL, 0); |
2585 | | |
2586 | 0 | swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE; |
2587 | 0 | after = (connect_flags & G_CONNECT_AFTER) != FALSE; |
2588 | |
|
2589 | 0 | SIGNAL_LOCK (); |
2590 | 0 | itype = G_TYPE_FROM_INSTANCE (instance); |
2591 | 0 | signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); |
2592 | 0 | if (signal_id) |
2593 | 0 | { |
2594 | 0 | SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); |
2595 | |
|
2596 | 0 | node_check_deprecated (node); |
2597 | |
|
2598 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
2599 | 0 | g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal); |
2600 | 0 | else if (!g_type_is_a (itype, node->itype)) |
2601 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
2602 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
2603 | 0 | else |
2604 | 0 | { |
2605 | 0 | Handler *handler = handler_new (signal_id, instance, after); |
2606 | |
|
2607 | 0 | if (G_TYPE_IS_OBJECT (node->itype)) |
2608 | 0 | _g_object_set_has_signal_handler ((GObject *) instance, signal_id); |
2609 | |
|
2610 | 0 | handler_seq_no = handler->sequential_number; |
2611 | 0 | handler->detail = detail; |
2612 | 0 | handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data)); |
2613 | 0 | g_closure_sink (handler->closure); |
2614 | 0 | handler_insert (signal_id, instance, handler); |
2615 | 0 | if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure)) |
2616 | 0 | { |
2617 | 0 | g_closure_set_marshal (handler->closure, node->c_marshaller); |
2618 | 0 | if (node->va_marshaller) |
2619 | 0 | _g_closure_set_va_marshal (handler->closure, node->va_marshaller); |
2620 | 0 | } |
2621 | 0 | } |
2622 | 0 | } |
2623 | 0 | else |
2624 | 0 | g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'", |
2625 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
2626 | 0 | SIGNAL_UNLOCK (); |
2627 | |
|
2628 | 0 | return handler_seq_no; |
2629 | 0 | } |
2630 | | |
2631 | | static void |
2632 | | signal_handler_block_unlocked (gpointer instance, |
2633 | | gulong handler_id); |
2634 | | |
2635 | | /** |
2636 | | * g_signal_handler_block: |
2637 | | * @instance: (type GObject.Object): The instance to block the signal handler of. |
2638 | | * @handler_id: Handler id of the handler to be blocked. |
2639 | | * |
2640 | | * Blocks a handler of an instance so it will not be called during any |
2641 | | * signal emissions unless it is unblocked again. Thus "blocking" a |
2642 | | * signal handler means to temporarily deactivate it, a signal handler |
2643 | | * has to be unblocked exactly the same amount of times it has been |
2644 | | * blocked before to become active again. |
2645 | | * |
2646 | | * The @handler_id has to be a valid signal handler id, connected to a |
2647 | | * signal of @instance. |
2648 | | */ |
2649 | | void |
2650 | | g_signal_handler_block (gpointer instance, |
2651 | | gulong handler_id) |
2652 | 0 | { |
2653 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2654 | 0 | g_return_if_fail (handler_id > 0); |
2655 | | |
2656 | 0 | SIGNAL_LOCK (); |
2657 | 0 | signal_handler_block_unlocked (instance, handler_id); |
2658 | 0 | SIGNAL_UNLOCK (); |
2659 | 0 | } |
2660 | | |
2661 | | static void |
2662 | | signal_handler_block_unlocked (gpointer instance, |
2663 | | gulong handler_id) |
2664 | 0 | { |
2665 | 0 | Handler *handler; |
2666 | |
|
2667 | 0 | handler = handler_lookup (instance, handler_id, NULL, NULL); |
2668 | 0 | if (handler) |
2669 | 0 | { |
2670 | 0 | #ifndef G_DISABLE_CHECKS |
2671 | 0 | if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1) |
2672 | 0 | g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG); |
2673 | 0 | #endif |
2674 | 0 | handler->block_count += 1; |
2675 | 0 | } |
2676 | 0 | else |
2677 | 0 | g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id); |
2678 | 0 | } |
2679 | | |
2680 | | static void |
2681 | | signal_handler_unblock_unlocked (gpointer instance, |
2682 | | gulong handler_id); |
2683 | | |
2684 | | /** |
2685 | | * g_signal_handler_unblock: |
2686 | | * @instance: (type GObject.Object): The instance to unblock the signal handler of. |
2687 | | * @handler_id: Handler id of the handler to be unblocked. |
2688 | | * |
2689 | | * Undoes the effect of a previous g_signal_handler_block() call. A |
2690 | | * blocked handler is skipped during signal emissions and will not be |
2691 | | * invoked, unblocking it (for exactly the amount of times it has been |
2692 | | * blocked before) reverts its "blocked" state, so the handler will be |
2693 | | * recognized by the signal system and is called upon future or |
2694 | | * currently ongoing signal emissions (since the order in which |
2695 | | * handlers are called during signal emissions is deterministic, |
2696 | | * whether the unblocked handler in question is called as part of a |
2697 | | * currently ongoing emission depends on how far that emission has |
2698 | | * proceeded yet). |
2699 | | * |
2700 | | * The @handler_id has to be a valid id of a signal handler that is |
2701 | | * connected to a signal of @instance and is currently blocked. |
2702 | | */ |
2703 | | void |
2704 | | g_signal_handler_unblock (gpointer instance, |
2705 | | gulong handler_id) |
2706 | 0 | { |
2707 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2708 | 0 | g_return_if_fail (handler_id > 0); |
2709 | | |
2710 | 0 | SIGNAL_LOCK (); |
2711 | 0 | signal_handler_unblock_unlocked (instance, handler_id); |
2712 | 0 | SIGNAL_UNLOCK (); |
2713 | 0 | } |
2714 | | |
2715 | | static void |
2716 | | signal_handler_unblock_unlocked (gpointer instance, |
2717 | | gulong handler_id) |
2718 | 0 | { |
2719 | 0 | Handler *handler; |
2720 | |
|
2721 | 0 | handler = handler_lookup (instance, handler_id, NULL, NULL); |
2722 | 0 | if (handler) |
2723 | 0 | { |
2724 | 0 | if (handler->block_count) |
2725 | 0 | handler->block_count -= 1; |
2726 | 0 | else |
2727 | 0 | g_critical (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance); |
2728 | 0 | } |
2729 | 0 | else |
2730 | 0 | g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id); |
2731 | 0 | } |
2732 | | |
2733 | | static void |
2734 | | signal_handler_disconnect_unlocked (gpointer instance, |
2735 | | gulong handler_id); |
2736 | | |
2737 | | /** |
2738 | | * g_signal_handler_disconnect: |
2739 | | * @instance: (type GObject.Object): The instance to remove the signal handler from. |
2740 | | * @handler_id: Handler id of the handler to be disconnected. |
2741 | | * |
2742 | | * Disconnects a handler from an instance so it will not be called during |
2743 | | * any future or currently ongoing emissions of the signal it has been |
2744 | | * connected to. The @handler_id becomes invalid and may be reused. |
2745 | | * |
2746 | | * The @handler_id has to be a valid signal handler id, connected to a |
2747 | | * signal of @instance. |
2748 | | */ |
2749 | | void |
2750 | | g_signal_handler_disconnect (gpointer instance, |
2751 | | gulong handler_id) |
2752 | 0 | { |
2753 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2754 | 0 | g_return_if_fail (handler_id > 0); |
2755 | | |
2756 | 0 | SIGNAL_LOCK (); |
2757 | 0 | signal_handler_disconnect_unlocked (instance, handler_id); |
2758 | 0 | SIGNAL_UNLOCK (); |
2759 | 0 | } |
2760 | | |
2761 | | static void |
2762 | | signal_handler_disconnect_unlocked (gpointer instance, |
2763 | | gulong handler_id) |
2764 | 0 | { |
2765 | 0 | Handler *handler; |
2766 | |
|
2767 | 0 | handler = handler_lookup (instance, handler_id, 0, 0); |
2768 | 0 | if (handler) |
2769 | 0 | { |
2770 | 0 | g_hash_table_remove (g_handlers, handler); |
2771 | 0 | handler->sequential_number = 0; |
2772 | 0 | handler->block_count = 1; |
2773 | 0 | remove_invalid_closure_notify (handler, instance); |
2774 | 0 | handler_unref_R (handler->signal_id, instance, handler); |
2775 | 0 | } |
2776 | 0 | else |
2777 | 0 | g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id); |
2778 | 0 | } |
2779 | | |
2780 | | /** |
2781 | | * g_signal_handler_is_connected: |
2782 | | * @instance: (type GObject.Object): The instance where a signal handler is sought. |
2783 | | * @handler_id: the handler ID. |
2784 | | * |
2785 | | * Returns whether @handler_id is the ID of a handler connected to @instance. |
2786 | | * |
2787 | | * Returns: whether @handler_id identifies a handler connected to @instance. |
2788 | | */ |
2789 | | gboolean |
2790 | | g_signal_handler_is_connected (gpointer instance, |
2791 | | gulong handler_id) |
2792 | 0 | { |
2793 | 0 | Handler *handler; |
2794 | 0 | gboolean connected; |
2795 | |
|
2796 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE); |
2797 | | |
2798 | 0 | SIGNAL_LOCK (); |
2799 | 0 | handler = handler_lookup (instance, handler_id, NULL, NULL); |
2800 | 0 | connected = handler != NULL; |
2801 | 0 | SIGNAL_UNLOCK (); |
2802 | |
|
2803 | 0 | return connected; |
2804 | 0 | } |
2805 | | |
2806 | | /** |
2807 | | * g_signal_handlers_destroy: |
2808 | | * @instance: (type GObject.Object): The instance whose signal handlers are destroyed |
2809 | | * |
2810 | | * Destroy all signal handlers of a type instance. This function is |
2811 | | * an implementation detail of the #GObject dispose implementation, |
2812 | | * and should not be used outside of the type system. |
2813 | | */ |
2814 | | void |
2815 | | g_signal_handlers_destroy (gpointer instance) |
2816 | 4.00k | { |
2817 | 4.00k | GBSearchArray *hlbsa; |
2818 | | |
2819 | 4.00k | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
2820 | | |
2821 | 4.00k | SIGNAL_LOCK (); |
2822 | 4.00k | hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); |
2823 | 4.00k | if (hlbsa) |
2824 | 0 | { |
2825 | 0 | guint i; |
2826 | | |
2827 | | /* reentrancy caution, delete instance trace first */ |
2828 | 0 | g_hash_table_remove (g_handler_list_bsa_ht, instance); |
2829 | | |
2830 | 0 | for (i = 0; i < hlbsa->n_nodes; i++) |
2831 | 0 | { |
2832 | 0 | HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); |
2833 | 0 | Handler *handler = hlist->handlers; |
2834 | | |
2835 | 0 | while (handler) |
2836 | 0 | { |
2837 | 0 | Handler *tmp = handler; |
2838 | | |
2839 | 0 | handler = tmp->next; |
2840 | 0 | tmp->block_count = 1; |
2841 | | /* cruel unlink, this works because _all_ handlers vanish */ |
2842 | 0 | tmp->next = NULL; |
2843 | 0 | tmp->prev = tmp; |
2844 | 0 | if (tmp->sequential_number) |
2845 | 0 | { |
2846 | 0 | g_hash_table_remove (g_handlers, tmp); |
2847 | 0 | remove_invalid_closure_notify (tmp, instance); |
2848 | 0 | tmp->sequential_number = 0; |
2849 | 0 | handler_unref_R (0, NULL, tmp); |
2850 | 0 | } |
2851 | 0 | } |
2852 | 0 | } |
2853 | 0 | g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig); |
2854 | 0 | } |
2855 | 4.00k | SIGNAL_UNLOCK (); |
2856 | 4.00k | } |
2857 | | |
2858 | | /** |
2859 | | * g_signal_handler_find: |
2860 | | * @instance: (type GObject.Object): The instance owning the signal handler to be found. |
2861 | | * @mask: Mask indicating which of @signal_id, @detail, @closure, @func |
2862 | | * and/or @data the handler has to match. |
2863 | | * @signal_id: Signal the handler has to be connected to. |
2864 | | * @detail: Signal detail the handler has to be connected to. |
2865 | | * @closure: (nullable): The closure the handler will invoke. |
2866 | | * @func: The C closure callback of the handler (useless for non-C closures). |
2867 | | * @data: (nullable) (closure closure): The closure data of the handler's closure. |
2868 | | * |
2869 | | * Finds the first signal handler that matches certain selection criteria. |
2870 | | * The criteria mask is passed as an OR-ed combination of #GSignalMatchType |
2871 | | * flags, and the criteria values are passed as arguments. |
2872 | | * The match @mask has to be non-0 for successful matches. |
2873 | | * If no handler was found, 0 is returned. |
2874 | | * |
2875 | | * Returns: A valid non-0 signal handler id for a successful match. |
2876 | | */ |
2877 | | gulong |
2878 | | g_signal_handler_find (gpointer instance, |
2879 | | GSignalMatchType mask, |
2880 | | guint signal_id, |
2881 | | GQuark detail, |
2882 | | GClosure *closure, |
2883 | | gpointer func, |
2884 | | gpointer data) |
2885 | 0 | { |
2886 | 0 | gulong handler_seq_no = 0; |
2887 | | |
2888 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
2889 | 0 | g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); |
2890 | | |
2891 | 0 | if (mask & G_SIGNAL_MATCH_MASK) |
2892 | 0 | { |
2893 | 0 | HandlerMatch *mlist; |
2894 | | |
2895 | 0 | SIGNAL_LOCK (); |
2896 | 0 | mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE); |
2897 | 0 | if (mlist) |
2898 | 0 | { |
2899 | 0 | handler_seq_no = mlist->handler->sequential_number; |
2900 | 0 | handler_match_free1_R (mlist, instance); |
2901 | 0 | } |
2902 | 0 | SIGNAL_UNLOCK (); |
2903 | 0 | } |
2904 | | |
2905 | 0 | return handler_seq_no; |
2906 | 0 | } |
2907 | | |
2908 | | typedef void (*CallbackHandlerFunc) (gpointer instance, gulong handler_seq_no); |
2909 | | |
2910 | | static guint |
2911 | | signal_handlers_foreach_matched_unlocked_R (gpointer instance, |
2912 | | GSignalMatchType mask, |
2913 | | guint signal_id, |
2914 | | GQuark detail, |
2915 | | GClosure *closure, |
2916 | | gpointer func, |
2917 | | gpointer data, |
2918 | | CallbackHandlerFunc callback) |
2919 | 0 | { |
2920 | 0 | HandlerMatch *mlist; |
2921 | 0 | guint n_handlers = 0; |
2922 | |
|
2923 | 0 | mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE); |
2924 | 0 | while (mlist) |
2925 | 0 | { |
2926 | 0 | n_handlers++; |
2927 | 0 | if (mlist->handler->sequential_number) |
2928 | 0 | callback (instance, mlist->handler->sequential_number); |
2929 | |
|
2930 | 0 | mlist = handler_match_free1_R (mlist, instance); |
2931 | 0 | } |
2932 | |
|
2933 | 0 | return n_handlers; |
2934 | 0 | } |
2935 | | |
2936 | | /** |
2937 | | * g_signal_handlers_block_matched: |
2938 | | * @instance: (type GObject.Object): The instance to block handlers from. |
2939 | | * @mask: Mask indicating which of @signal_id, @detail, @closure, @func |
2940 | | * and/or @data the handlers have to match. |
2941 | | * @signal_id: Signal the handlers have to be connected to. |
2942 | | * @detail: Signal detail the handlers have to be connected to. |
2943 | | * @closure: (nullable): The closure the handlers will invoke. |
2944 | | * @func: The C closure callback of the handlers (useless for non-C closures). |
2945 | | * @data: (nullable) (closure closure): The closure data of the handlers' closures. |
2946 | | * |
2947 | | * Blocks all handlers on an instance that match a certain selection criteria. |
2948 | | * |
2949 | | * The criteria mask is passed as a combination of #GSignalMatchType flags, and |
2950 | | * the criteria values are passed as arguments. A handler must match on all |
2951 | | * flags set in @mask to be blocked (i.e. the match is conjunctive). |
2952 | | * |
2953 | | * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE, |
2954 | | * %G_SIGNAL_MATCH_FUNC |
2955 | | * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches. |
2956 | | * If no handlers were found, 0 is returned, the number of blocked handlers |
2957 | | * otherwise. |
2958 | | * |
2959 | | * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78. |
2960 | | * |
2961 | | * Returns: The number of handlers that matched. |
2962 | | */ |
2963 | | guint |
2964 | | g_signal_handlers_block_matched (gpointer instance, |
2965 | | GSignalMatchType mask, |
2966 | | guint signal_id, |
2967 | | GQuark detail, |
2968 | | GClosure *closure, |
2969 | | gpointer func, |
2970 | | gpointer data) |
2971 | 0 | { |
2972 | 0 | guint n_handlers = 0; |
2973 | | |
2974 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
2975 | 0 | g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); |
2976 | | |
2977 | 0 | if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) |
2978 | 0 | { |
2979 | 0 | SIGNAL_LOCK (); |
2980 | 0 | n_handlers = |
2981 | 0 | signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail, |
2982 | 0 | closure, func, data, |
2983 | 0 | signal_handler_block_unlocked); |
2984 | 0 | SIGNAL_UNLOCK (); |
2985 | 0 | } |
2986 | | |
2987 | 0 | return n_handlers; |
2988 | 0 | } |
2989 | | |
2990 | | /** |
2991 | | * g_signal_handlers_unblock_matched: |
2992 | | * @instance: (type GObject.Object): The instance to unblock handlers from. |
2993 | | * @mask: Mask indicating which of @signal_id, @detail, @closure, @func |
2994 | | * and/or @data the handlers have to match. |
2995 | | * @signal_id: Signal the handlers have to be connected to. |
2996 | | * @detail: Signal detail the handlers have to be connected to. |
2997 | | * @closure: (nullable): The closure the handlers will invoke. |
2998 | | * @func: The C closure callback of the handlers (useless for non-C closures). |
2999 | | * @data: (nullable) (closure closure): The closure data of the handlers' closures. |
3000 | | * |
3001 | | * Unblocks all handlers on an instance that match a certain selection |
3002 | | * criteria. |
3003 | | * |
3004 | | * The criteria mask is passed as a combination of #GSignalMatchType flags, and |
3005 | | * the criteria values are passed as arguments. A handler must match on all |
3006 | | * flags set in @mask to be unblocked (i.e. the match is conjunctive). |
3007 | | * |
3008 | | * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE, |
3009 | | * %G_SIGNAL_MATCH_FUNC |
3010 | | * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches. |
3011 | | * If no handlers were found, 0 is returned, the number of unblocked handlers |
3012 | | * otherwise. The match criteria should not apply to any handlers that are |
3013 | | * not currently blocked. |
3014 | | * |
3015 | | * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78. |
3016 | | * |
3017 | | * Returns: The number of handlers that matched. |
3018 | | */ |
3019 | | guint |
3020 | | g_signal_handlers_unblock_matched (gpointer instance, |
3021 | | GSignalMatchType mask, |
3022 | | guint signal_id, |
3023 | | GQuark detail, |
3024 | | GClosure *closure, |
3025 | | gpointer func, |
3026 | | gpointer data) |
3027 | 0 | { |
3028 | 0 | guint n_handlers = 0; |
3029 | | |
3030 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
3031 | 0 | g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); |
3032 | | |
3033 | 0 | if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) |
3034 | 0 | { |
3035 | 0 | SIGNAL_LOCK (); |
3036 | 0 | n_handlers = |
3037 | 0 | signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail, |
3038 | 0 | closure, func, data, |
3039 | 0 | signal_handler_unblock_unlocked); |
3040 | 0 | SIGNAL_UNLOCK (); |
3041 | 0 | } |
3042 | | |
3043 | 0 | return n_handlers; |
3044 | 0 | } |
3045 | | |
3046 | | /** |
3047 | | * g_signal_handlers_disconnect_matched: |
3048 | | * @instance: (type GObject.Object): The instance to remove handlers from. |
3049 | | * @mask: Mask indicating which of @signal_id, @detail, @closure, @func |
3050 | | * and/or @data the handlers have to match. |
3051 | | * @signal_id: Signal the handlers have to be connected to. |
3052 | | * @detail: Signal detail the handlers have to be connected to. |
3053 | | * @closure: (nullable): The closure the handlers will invoke. |
3054 | | * @func: The C closure callback of the handlers (useless for non-C closures). |
3055 | | * @data: (nullable) (closure closure): The closure data of the handlers' closures. |
3056 | | * |
3057 | | * Disconnects all handlers on an instance that match a certain |
3058 | | * selection criteria. |
3059 | | * |
3060 | | * The criteria mask is passed as a combination of #GSignalMatchType flags, and |
3061 | | * the criteria values are passed as arguments. A handler must match on all |
3062 | | * flags set in @mask to be disconnected (i.e. the match is conjunctive). |
3063 | | * |
3064 | | * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE, |
3065 | | * %G_SIGNAL_MATCH_FUNC or |
3066 | | * %G_SIGNAL_MATCH_DATA match flags is required for successful |
3067 | | * matches. If no handlers were found, 0 is returned, the number of |
3068 | | * disconnected handlers otherwise. |
3069 | | * |
3070 | | * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78. |
3071 | | * |
3072 | | * Returns: The number of handlers that matched. |
3073 | | */ |
3074 | | guint |
3075 | | g_signal_handlers_disconnect_matched (gpointer instance, |
3076 | | GSignalMatchType mask, |
3077 | | guint signal_id, |
3078 | | GQuark detail, |
3079 | | GClosure *closure, |
3080 | | gpointer func, |
3081 | | gpointer data) |
3082 | 0 | { |
3083 | 0 | guint n_handlers = 0; |
3084 | | |
3085 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
3086 | 0 | g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); |
3087 | | |
3088 | 0 | if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) |
3089 | 0 | { |
3090 | 0 | SIGNAL_LOCK (); |
3091 | 0 | n_handlers = |
3092 | 0 | signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail, |
3093 | 0 | closure, func, data, |
3094 | 0 | signal_handler_disconnect_unlocked); |
3095 | 0 | SIGNAL_UNLOCK (); |
3096 | 0 | } |
3097 | | |
3098 | 0 | return n_handlers; |
3099 | 0 | } |
3100 | | |
3101 | | /** |
3102 | | * g_signal_has_handler_pending: |
3103 | | * @instance: (type GObject.Object): the object whose signal handlers are sought. |
3104 | | * @signal_id: the signal id. |
3105 | | * @detail: the detail. |
3106 | | * @may_be_blocked: whether blocked handlers should count as match. |
3107 | | * |
3108 | | * Returns whether there are any handlers connected to @instance for the |
3109 | | * given signal id and detail. |
3110 | | * |
3111 | | * If @detail is 0 then it will only match handlers that were connected |
3112 | | * without detail. If @detail is non-zero then it will match handlers |
3113 | | * connected both without detail and with the given detail. This is |
3114 | | * consistent with how a signal emitted with @detail would be delivered |
3115 | | * to those handlers. |
3116 | | * |
3117 | | * Since 2.46 this also checks for a non-default class closure being |
3118 | | * installed, as this is basically always what you want. |
3119 | | * |
3120 | | * One example of when you might use this is when the arguments to the |
3121 | | * signal are difficult to compute. A class implementor may opt to not |
3122 | | * emit the signal if no one is attached anyway, thus saving the cost |
3123 | | * of building the arguments. |
3124 | | * |
3125 | | * Returns: %TRUE if a handler is connected to the signal, %FALSE |
3126 | | * otherwise. |
3127 | | */ |
3128 | | gboolean |
3129 | | g_signal_has_handler_pending (gpointer instance, |
3130 | | guint signal_id, |
3131 | | GQuark detail, |
3132 | | gboolean may_be_blocked) |
3133 | 0 | { |
3134 | 0 | HandlerMatch *mlist; |
3135 | 0 | gboolean has_pending; |
3136 | 0 | SignalNode *node; |
3137 | | |
3138 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE); |
3139 | 0 | g_return_val_if_fail (signal_id > 0, FALSE); |
3140 | | |
3141 | 0 | SIGNAL_LOCK (); |
3142 | |
|
3143 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
3144 | 0 | if (detail) |
3145 | 0 | { |
3146 | 0 | if (!(node->flags & G_SIGNAL_DETAILED)) |
3147 | 0 | { |
3148 | 0 | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
3149 | 0 | SIGNAL_UNLOCK (); |
3150 | 0 | return FALSE; |
3151 | 0 | } |
3152 | 0 | } |
3153 | 0 | mlist = handlers_find (instance, |
3154 | 0 | (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)), |
3155 | 0 | signal_id, detail, NULL, NULL, NULL, TRUE); |
3156 | 0 | if (mlist) |
3157 | 0 | { |
3158 | 0 | has_pending = TRUE; |
3159 | 0 | handler_match_free1_R (mlist, instance); |
3160 | 0 | } |
3161 | 0 | else |
3162 | 0 | { |
3163 | 0 | ClassClosure *class_closure = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance)); |
3164 | 0 | if (class_closure != NULL && class_closure->instance_type != 0) |
3165 | 0 | has_pending = TRUE; |
3166 | 0 | else |
3167 | 0 | has_pending = FALSE; |
3168 | 0 | } |
3169 | 0 | SIGNAL_UNLOCK (); |
3170 | |
|
3171 | 0 | return has_pending; |
3172 | 0 | } |
3173 | | |
3174 | | static void |
3175 | | signal_emitv_unlocked (const GValue *instance_and_params, |
3176 | | guint signal_id, |
3177 | | GQuark detail, |
3178 | | GValue *return_value); |
3179 | | |
3180 | | /** |
3181 | | * g_signal_emitv: |
3182 | | * @instance_and_params: (array): argument list for the signal emission. |
3183 | | * The first element in the array is a #GValue for the instance the signal |
3184 | | * is being emitted on. The rest are any arguments to be passed to the signal. |
3185 | | * @signal_id: the signal id |
3186 | | * @detail: the detail |
3187 | | * @return_value: (inout) (optional): Location to |
3188 | | * store the return value of the signal emission. This must be provided if the |
3189 | | * specified signal returns a value, but may be ignored otherwise. |
3190 | | * |
3191 | | * Emits a signal. Signal emission is done synchronously. |
3192 | | * The method will only return control after all handlers are called or signal emission was stopped. |
3193 | | * |
3194 | | * Note that g_signal_emitv() doesn't change @return_value if no handlers are |
3195 | | * connected, in contrast to g_signal_emit() and g_signal_emit_valist(). |
3196 | | */ |
3197 | | void |
3198 | | g_signal_emitv (const GValue *instance_and_params, |
3199 | | guint signal_id, |
3200 | | GQuark detail, |
3201 | | GValue *return_value) |
3202 | 0 | { |
3203 | 0 | SIGNAL_LOCK (); |
3204 | 0 | signal_emitv_unlocked (instance_and_params, signal_id, detail, return_value); |
3205 | 0 | SIGNAL_UNLOCK (); |
3206 | 0 | } |
3207 | | |
3208 | | static void |
3209 | | signal_emitv_unlocked (const GValue *instance_and_params, |
3210 | | guint signal_id, |
3211 | | GQuark detail, |
3212 | | GValue *return_value) |
3213 | 0 | { |
3214 | 0 | gpointer instance; |
3215 | 0 | SignalNode *node; |
3216 | | #ifdef G_ENABLE_DEBUG |
3217 | | const GValue *param_values; |
3218 | | guint i; |
3219 | | #endif |
3220 | | |
3221 | 0 | g_return_if_fail (instance_and_params != NULL); |
3222 | 0 | instance = g_value_peek_pointer (instance_and_params); |
3223 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
3224 | 0 | g_return_if_fail (signal_id > 0); |
3225 | | |
3226 | | #ifdef G_ENABLE_DEBUG |
3227 | | param_values = instance_and_params + 1; |
3228 | | #endif |
3229 | | |
3230 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
3231 | 0 | if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) |
3232 | 0 | { |
3233 | 0 | g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance); |
3234 | 0 | return; |
3235 | 0 | } |
3236 | | #ifdef G_ENABLE_DEBUG |
3237 | | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
3238 | | { |
3239 | | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
3240 | | return; |
3241 | | } |
3242 | | for (i = 0; i < node->n_params; i++) |
3243 | | if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE)) |
3244 | | { |
3245 | | g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'", |
3246 | | G_STRLOC, |
3247 | | type_debug_name (node->param_types[i]), |
3248 | | i, |
3249 | | node->name, |
3250 | | G_VALUE_TYPE_NAME (param_values + i)); |
3251 | | return; |
3252 | | } |
3253 | | if (node->return_type != G_TYPE_NONE) |
3254 | | { |
3255 | | if (!return_value) |
3256 | | { |
3257 | | g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)", |
3258 | | G_STRLOC, |
3259 | | type_debug_name (node->return_type), |
3260 | | node->name); |
3261 | | return; |
3262 | | } |
3263 | | else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE)) |
3264 | | { |
3265 | | g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'", |
3266 | | G_STRLOC, |
3267 | | type_debug_name (node->return_type), |
3268 | | node->name, |
3269 | | G_VALUE_TYPE_NAME (return_value)); |
3270 | | return; |
3271 | | } |
3272 | | } |
3273 | | else |
3274 | | return_value = NULL; |
3275 | | #endif /* G_ENABLE_DEBUG */ |
3276 | | |
3277 | | /* optimize NOP emissions */ |
3278 | 0 | if (!node->single_va_closure_is_valid) |
3279 | 0 | node_update_single_va_closure (node); |
3280 | |
|
3281 | 0 | if (node->single_va_closure != NULL && |
3282 | 0 | (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC || |
3283 | 0 | _g_closure_is_void (node->single_va_closure, instance))) |
3284 | 0 | { |
3285 | 0 | HandlerList* hlist; |
3286 | | |
3287 | | /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */ |
3288 | 0 | if (_g_object_has_signal_handler ((GObject *)instance)) |
3289 | 0 | hlist = handler_list_lookup (node->signal_id, instance); |
3290 | 0 | else |
3291 | 0 | hlist = NULL; |
3292 | |
|
3293 | 0 | if (hlist == NULL || hlist->handlers == NULL) |
3294 | 0 | { |
3295 | | /* nothing to do to emit this signal */ |
3296 | | /* g_printerr ("omitting emission of \"%s\"\n", node->name); */ |
3297 | 0 | return; |
3298 | 0 | } |
3299 | 0 | } |
3300 | | |
3301 | | /* Pass a stable node pointer, whose address can't change even if the |
3302 | | * g_signal_nodes array gets reallocated. */ |
3303 | 0 | SignalNode node_copy = *node; |
3304 | 0 | signal_emit_unlocked_R (&node_copy, detail, instance, return_value, instance_and_params); |
3305 | 0 | } |
3306 | | |
3307 | | static inline gboolean |
3308 | | accumulate (GSignalInvocationHint *ihint, |
3309 | | GValue *return_accu, |
3310 | | GValue *handler_return, |
3311 | | SignalAccumulator *accumulator) |
3312 | 0 | { |
3313 | 0 | gboolean continue_emission; |
3314 | |
|
3315 | 0 | if (!accumulator) |
3316 | 0 | return TRUE; |
3317 | | |
3318 | 0 | continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data); |
3319 | 0 | g_value_reset (handler_return); |
3320 | |
|
3321 | 0 | ihint->run_type &= ~G_SIGNAL_ACCUMULATOR_FIRST_RUN; |
3322 | |
|
3323 | 0 | return continue_emission; |
3324 | 0 | } |
3325 | | |
3326 | | static gboolean |
3327 | | signal_emit_valist_unlocked (gpointer instance, |
3328 | | guint signal_id, |
3329 | | GQuark detail, |
3330 | | va_list var_args); |
3331 | | |
3332 | | /** |
3333 | | * g_signal_emit_valist: (skip) |
3334 | | * @instance: (type GObject.TypeInstance): the instance the signal is being |
3335 | | * emitted on. |
3336 | | * @signal_id: the signal id |
3337 | | * @detail: the detail |
3338 | | * @var_args: a list of parameters to be passed to the signal, followed by a |
3339 | | * location for the return value. If the return type of the signal |
3340 | | * is %G_TYPE_NONE, the return value location can be omitted. |
3341 | | * |
3342 | | * Emits a signal. Signal emission is done synchronously. |
3343 | | * The method will only return control after all handlers are called or signal emission was stopped. |
3344 | | * |
3345 | | * Note that g_signal_emit_valist() resets the return value to the default |
3346 | | * if no handlers are connected, in contrast to g_signal_emitv(). |
3347 | | */ |
3348 | | void |
3349 | | g_signal_emit_valist (gpointer instance, |
3350 | | guint signal_id, |
3351 | | GQuark detail, |
3352 | | va_list var_args) |
3353 | 0 | { |
3354 | 0 | SIGNAL_LOCK (); |
3355 | 0 | if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args)) |
3356 | 0 | SIGNAL_UNLOCK (); |
3357 | 0 | } |
3358 | | |
3359 | | /*<private> |
3360 | | * signal_emit_valist_unlocked: |
3361 | | * @instance: The instance to emit from |
3362 | | * @signal_id: Signal id to emit |
3363 | | * @detail: Signal detail |
3364 | | * @var_args: Call arguments |
3365 | | * |
3366 | | * Returns: %TRUE if the signal mutex has been left locked |
3367 | | */ |
3368 | | static gboolean |
3369 | | signal_emit_valist_unlocked (gpointer instance, |
3370 | | guint signal_id, |
3371 | | GQuark detail, |
3372 | | va_list var_args) |
3373 | 0 | { |
3374 | 0 | GValue *instance_and_params; |
3375 | 0 | GValue *param_values; |
3376 | 0 | SignalNode *node; |
3377 | 0 | guint i; |
3378 | |
|
3379 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), TRUE); |
3380 | 0 | g_return_val_if_fail (signal_id > 0, TRUE); |
3381 | | |
3382 | 0 | node = LOOKUP_SIGNAL_NODE (signal_id); |
3383 | 0 | if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) |
3384 | 0 | { |
3385 | 0 | g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance); |
3386 | 0 | return TRUE; |
3387 | 0 | } |
3388 | 0 | #ifndef G_DISABLE_CHECKS |
3389 | 0 | if (detail && !(node->flags & G_SIGNAL_DETAILED)) |
3390 | 0 | { |
3391 | 0 | g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail); |
3392 | 0 | return TRUE; |
3393 | 0 | } |
3394 | 0 | #endif /* !G_DISABLE_CHECKS */ |
3395 | | |
3396 | 0 | if (!node->single_va_closure_is_valid) |
3397 | 0 | node_update_single_va_closure (node); |
3398 | | |
3399 | | /* There's no need to deep copy this, because a SignalNode instance won't |
3400 | | * ever be destroyed, given that _g_signals_destroy() is not called in any |
3401 | | * real program, however the SignalNode pointer could change, so just store |
3402 | | * the struct contents references, so that we won't try to deference a |
3403 | | * potentially invalid (or changed) pointer; |
3404 | | */ |
3405 | 0 | SignalNode node_copy = *node; |
3406 | |
|
3407 | 0 | if (node->single_va_closure != NULL) |
3408 | 0 | { |
3409 | 0 | HandlerList* hlist; |
3410 | 0 | Handler *fastpath_handler = NULL; |
3411 | 0 | Handler *l; |
3412 | 0 | GClosure *closure = NULL; |
3413 | 0 | gboolean fastpath = TRUE; |
3414 | 0 | GSignalFlags run_type = G_SIGNAL_RUN_FIRST; |
3415 | |
|
3416 | 0 | if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC && |
3417 | 0 | !_g_closure_is_void (node->single_va_closure, instance)) |
3418 | 0 | { |
3419 | 0 | if (_g_closure_supports_invoke_va (node->single_va_closure)) |
3420 | 0 | { |
3421 | 0 | closure = node->single_va_closure; |
3422 | 0 | if (node->single_va_closure_is_after) |
3423 | 0 | run_type = G_SIGNAL_RUN_LAST; |
3424 | 0 | else |
3425 | 0 | run_type = G_SIGNAL_RUN_FIRST; |
3426 | 0 | } |
3427 | 0 | else |
3428 | 0 | fastpath = FALSE; |
3429 | 0 | } |
3430 | | |
3431 | | /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */ |
3432 | 0 | if (_g_object_has_signal_handler ((GObject *)instance)) |
3433 | 0 | hlist = handler_list_lookup (node->signal_id, instance); |
3434 | 0 | else |
3435 | 0 | hlist = NULL; |
3436 | |
|
3437 | 0 | for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next) |
3438 | 0 | { |
3439 | 0 | if (!l->block_count && |
3440 | 0 | (!l->detail || l->detail == detail)) |
3441 | 0 | { |
3442 | 0 | if (closure != NULL || !_g_closure_supports_invoke_va (l->closure)) |
3443 | 0 | { |
3444 | 0 | fastpath = FALSE; |
3445 | 0 | break; |
3446 | 0 | } |
3447 | 0 | else |
3448 | 0 | { |
3449 | 0 | fastpath_handler = l; |
3450 | 0 | closure = l->closure; |
3451 | 0 | if (l->after) |
3452 | 0 | run_type = G_SIGNAL_RUN_LAST; |
3453 | 0 | else |
3454 | 0 | run_type = G_SIGNAL_RUN_FIRST; |
3455 | 0 | } |
3456 | 0 | } |
3457 | 0 | } |
3458 | |
|
3459 | 0 | if (fastpath && closure == NULL && node_copy.return_type == G_TYPE_NONE) |
3460 | 0 | return TRUE; |
3461 | | |
3462 | | /* Don't allow no-recurse emission as we might have to restart, which means |
3463 | | we will run multiple handlers and thus must ref all arguments */ |
3464 | 0 | if (closure != NULL && (node_copy.flags & (G_SIGNAL_NO_RECURSE)) != 0) |
3465 | 0 | fastpath = FALSE; |
3466 | | |
3467 | 0 | if (fastpath) |
3468 | 0 | { |
3469 | 0 | Emission emission; |
3470 | 0 | GValue *return_accu, accu = G_VALUE_INIT; |
3471 | 0 | GType instance_type = G_TYPE_FROM_INSTANCE (instance); |
3472 | 0 | GValue emission_return = G_VALUE_INIT; |
3473 | 0 | GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
3474 | 0 | gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE; |
3475 | |
|
3476 | 0 | if (rtype == G_TYPE_NONE) |
3477 | 0 | return_accu = NULL; |
3478 | 0 | else if (node_copy.accumulator) |
3479 | 0 | return_accu = &accu; |
3480 | 0 | else |
3481 | 0 | return_accu = &emission_return; |
3482 | |
|
3483 | 0 | emission.instance = instance; |
3484 | 0 | emission.ihint.signal_id = signal_id; |
3485 | 0 | emission.ihint.detail = detail; |
3486 | 0 | emission.ihint.run_type = run_type | G_SIGNAL_ACCUMULATOR_FIRST_RUN; |
3487 | 0 | emission.state = EMISSION_RUN; |
3488 | 0 | emission.chain_type = instance_type; |
3489 | 0 | emission_push (&emission); |
3490 | |
|
3491 | 0 | if (fastpath_handler) |
3492 | 0 | handler_ref (fastpath_handler); |
3493 | |
|
3494 | 0 | if (closure != NULL) |
3495 | 0 | { |
3496 | 0 | TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type)); |
3497 | |
|
3498 | 0 | SIGNAL_UNLOCK (); |
3499 | |
|
3500 | 0 | if (rtype != G_TYPE_NONE) |
3501 | 0 | g_value_init (&emission_return, rtype); |
3502 | |
|
3503 | 0 | if (node_copy.accumulator) |
3504 | 0 | g_value_init (&accu, rtype); |
3505 | | |
3506 | | /* |
3507 | | * Coverity doesn’t understand the paired ref/unref here and seems |
3508 | | * to ignore the ref, thus reports every call to g_signal_emit() |
3509 | | * as causing a double-free. That’s incorrect, but I can’t get a |
3510 | | * model file to work for avoiding the false positives, so instead |
3511 | | * comment out the ref/unref when doing static analysis. |
3512 | | */ |
3513 | 0 | #ifndef __COVERITY__ |
3514 | 0 | g_object_ref (instance); |
3515 | 0 | #endif |
3516 | 0 | _g_closure_invoke_va (closure, |
3517 | 0 | return_accu, |
3518 | 0 | instance, |
3519 | 0 | var_args, |
3520 | 0 | node_copy.n_params, |
3521 | 0 | node_copy.param_types); |
3522 | 0 | accumulate (&emission.ihint, &emission_return, &accu, node_copy.accumulator); |
3523 | |
|
3524 | 0 | if (node_copy.accumulator) |
3525 | 0 | g_value_unset (&accu); |
3526 | |
|
3527 | 0 | SIGNAL_LOCK (); |
3528 | 0 | } |
3529 | |
|
3530 | 0 | emission.chain_type = G_TYPE_NONE; |
3531 | 0 | emission_pop (&emission); |
3532 | |
|
3533 | 0 | if (fastpath_handler) |
3534 | 0 | handler_unref_R (signal_id, instance, fastpath_handler); |
3535 | |
|
3536 | 0 | SIGNAL_UNLOCK (); |
3537 | |
|
3538 | 0 | if (rtype != G_TYPE_NONE) |
3539 | 0 | { |
3540 | 0 | gchar *error = NULL; |
3541 | 0 | for (i = 0; i < node_copy.n_params; i++) |
3542 | 0 | { |
3543 | 0 | GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
3544 | 0 | G_VALUE_COLLECT_SKIP (ptype, var_args); |
3545 | 0 | } |
3546 | | |
3547 | 0 | if (closure == NULL) |
3548 | 0 | g_value_init (&emission_return, rtype); |
3549 | |
|
3550 | 0 | G_VALUE_LCOPY (&emission_return, |
3551 | 0 | var_args, |
3552 | 0 | static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, |
3553 | 0 | &error); |
3554 | 0 | if (!error) |
3555 | 0 | g_value_unset (&emission_return); |
3556 | 0 | else |
3557 | 0 | { |
3558 | 0 | g_critical ("%s: %s", G_STRLOC, error); |
3559 | 0 | g_free (error); |
3560 | | /* we purposely leak the value here, it might not be |
3561 | | * in a correct state if an error condition occurred |
3562 | | */ |
3563 | 0 | } |
3564 | 0 | } |
3565 | | |
3566 | 0 | TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type)); |
3567 | | |
3568 | | /* See comment above paired ref above */ |
3569 | 0 | #ifndef __COVERITY__ |
3570 | 0 | if (closure != NULL) |
3571 | 0 | g_object_unref (instance); |
3572 | 0 | #endif |
3573 | |
|
3574 | 0 | return FALSE; |
3575 | 0 | } |
3576 | 0 | } |
3577 | | |
3578 | 0 | SIGNAL_UNLOCK (); |
3579 | |
|
3580 | 0 | instance_and_params = g_newa0 (GValue, node_copy.n_params + 1); |
3581 | 0 | param_values = instance_and_params + 1; |
3582 | |
|
3583 | 0 | for (i = 0; i < node_copy.n_params; i++) |
3584 | 0 | { |
3585 | 0 | gchar *error; |
3586 | 0 | GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
3587 | 0 | gboolean static_scope = node_copy.param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE; |
3588 | |
|
3589 | 0 | G_VALUE_COLLECT_INIT (param_values + i, ptype, |
3590 | 0 | var_args, |
3591 | 0 | static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, |
3592 | 0 | &error); |
3593 | 0 | if (error) |
3594 | 0 | { |
3595 | 0 | g_critical ("%s: %s", G_STRLOC, error); |
3596 | 0 | g_free (error); |
3597 | | |
3598 | | /* we purposely leak the value here, it might not be |
3599 | | * in a correct state if an error condition occurred |
3600 | | */ |
3601 | 0 | while (i--) |
3602 | 0 | g_value_unset (param_values + i); |
3603 | |
|
3604 | 0 | return FALSE; |
3605 | 0 | } |
3606 | 0 | } |
3607 | | |
3608 | 0 | g_value_init_from_instance (instance_and_params, instance); |
3609 | 0 | if (node_copy.return_type == G_TYPE_NONE) |
3610 | 0 | { |
3611 | 0 | SIGNAL_LOCK (); |
3612 | 0 | signal_emit_unlocked_R (&node_copy, detail, instance, NULL, instance_and_params); |
3613 | 0 | SIGNAL_UNLOCK (); |
3614 | 0 | } |
3615 | 0 | else |
3616 | 0 | { |
3617 | 0 | GValue return_value = G_VALUE_INIT; |
3618 | 0 | gchar *error = NULL; |
3619 | 0 | GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE; |
3620 | 0 | gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE; |
3621 | | |
3622 | 0 | g_value_init (&return_value, rtype); |
3623 | |
|
3624 | 0 | SIGNAL_LOCK (); |
3625 | 0 | signal_emit_unlocked_R (&node_copy, detail, instance, &return_value, instance_and_params); |
3626 | 0 | SIGNAL_UNLOCK (); |
3627 | |
|
3628 | 0 | G_VALUE_LCOPY (&return_value, |
3629 | 0 | var_args, |
3630 | 0 | static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, |
3631 | 0 | &error); |
3632 | 0 | if (!error) |
3633 | 0 | g_value_unset (&return_value); |
3634 | 0 | else |
3635 | 0 | { |
3636 | 0 | g_critical ("%s: %s", G_STRLOC, error); |
3637 | 0 | g_free (error); |
3638 | | |
3639 | | /* we purposely leak the value here, it might not be |
3640 | | * in a correct state if an error condition occurred |
3641 | | */ |
3642 | 0 | } |
3643 | 0 | } |
3644 | 0 | for (i = 0; i < node_copy.n_params; i++) |
3645 | 0 | g_value_unset (param_values + i); |
3646 | 0 | g_value_unset (instance_and_params); |
3647 | |
|
3648 | 0 | return FALSE; |
3649 | 0 | } |
3650 | | |
3651 | | /** |
3652 | | * g_signal_emit: |
3653 | | * @instance: (type GObject.Object): the instance the signal is being emitted on. |
3654 | | * @signal_id: the signal id |
3655 | | * @detail: the detail |
3656 | | * @...: parameters to be passed to the signal, followed by a |
3657 | | * location for the return value. If the return type of the signal |
3658 | | * is %G_TYPE_NONE, the return value location can be omitted. |
3659 | | * |
3660 | | * Emits a signal. Signal emission is done synchronously. |
3661 | | * The method will only return control after all handlers are called or signal emission was stopped. |
3662 | | * |
3663 | | * Note that g_signal_emit() resets the return value to the default |
3664 | | * if no handlers are connected, in contrast to g_signal_emitv(). |
3665 | | */ |
3666 | | void |
3667 | | g_signal_emit (gpointer instance, |
3668 | | guint signal_id, |
3669 | | GQuark detail, |
3670 | | ...) |
3671 | 0 | { |
3672 | 0 | va_list var_args; |
3673 | |
|
3674 | 0 | va_start (var_args, detail); |
3675 | 0 | g_signal_emit_valist (instance, signal_id, detail, var_args); |
3676 | 0 | va_end (var_args); |
3677 | 0 | } |
3678 | | |
3679 | | /** |
3680 | | * g_signal_emit_by_name: |
3681 | | * @instance: (type GObject.Object): the instance the signal is being emitted on. |
3682 | | * @detailed_signal: a string of the form "signal-name::detail". |
3683 | | * @...: parameters to be passed to the signal, followed by a |
3684 | | * location for the return value. If the return type of the signal |
3685 | | * is %G_TYPE_NONE, the return value location can be omitted. The |
3686 | | * number of parameters to pass to this function is defined when creating the signal. |
3687 | | * |
3688 | | * Emits a signal. Signal emission is done synchronously. |
3689 | | * The method will only return control after all handlers are called or signal emission was stopped. |
3690 | | * |
3691 | | * Note that g_signal_emit_by_name() resets the return value to the default |
3692 | | * if no handlers are connected, in contrast to g_signal_emitv(). |
3693 | | */ |
3694 | | void |
3695 | | g_signal_emit_by_name (gpointer instance, |
3696 | | const gchar *detailed_signal, |
3697 | | ...) |
3698 | 0 | { |
3699 | 0 | GQuark detail = 0; |
3700 | 0 | guint signal_id; |
3701 | 0 | GType itype; |
3702 | |
|
3703 | 0 | g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); |
3704 | 0 | g_return_if_fail (detailed_signal != NULL); |
3705 | | |
3706 | 0 | itype = G_TYPE_FROM_INSTANCE (instance); |
3707 | |
|
3708 | 0 | SIGNAL_LOCK (); |
3709 | 0 | signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); |
3710 | |
|
3711 | 0 | if (signal_id) |
3712 | 0 | { |
3713 | 0 | va_list var_args; |
3714 | |
|
3715 | 0 | va_start (var_args, detailed_signal); |
3716 | 0 | if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args)) |
3717 | 0 | SIGNAL_UNLOCK (); |
3718 | 0 | va_end (var_args); |
3719 | 0 | } |
3720 | 0 | else |
3721 | 0 | { |
3722 | 0 | SIGNAL_UNLOCK (); |
3723 | |
|
3724 | 0 | g_critical ("%s: signal name '%s' is invalid for instance '%p' of type '%s'", |
3725 | 0 | G_STRLOC, detailed_signal, instance, g_type_name (itype)); |
3726 | 0 | } |
3727 | 0 | } |
3728 | | |
3729 | | G_ALWAYS_INLINE static inline GValue * |
3730 | | maybe_init_accumulator_unlocked (SignalNode *node, |
3731 | | GValue *emission_return, |
3732 | | GValue *accumulator_value) |
3733 | 0 | { |
3734 | 0 | if (node->accumulator) |
3735 | 0 | { |
3736 | 0 | if (accumulator_value->g_type) |
3737 | 0 | return accumulator_value; |
3738 | | |
3739 | 0 | g_value_init (accumulator_value, |
3740 | 0 | node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE); |
3741 | 0 | return accumulator_value; |
3742 | 0 | } |
3743 | | |
3744 | 0 | return emission_return; |
3745 | 0 | } |
3746 | | |
3747 | | static gboolean |
3748 | | signal_emit_unlocked_R (SignalNode *node, |
3749 | | GQuark detail, |
3750 | | gpointer instance, |
3751 | | GValue *emission_return, |
3752 | | const GValue *instance_and_params) |
3753 | 0 | { |
3754 | 0 | SignalAccumulator *accumulator; |
3755 | 0 | Emission emission; |
3756 | 0 | GClosure *class_closure; |
3757 | 0 | HandlerList *hlist; |
3758 | 0 | Handler *handler_list = NULL; |
3759 | 0 | GValue *return_accu, accu = G_VALUE_INIT; |
3760 | 0 | guint signal_id; |
3761 | 0 | gulong max_sequential_handler_number; |
3762 | 0 | gboolean return_value_altered = FALSE; |
3763 | 0 | guint n_params; |
3764 | |
|
3765 | 0 | TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance))); |
3766 | | |
3767 | | /* We expect this function to be called with a stable SignalNode pointer |
3768 | | * that cannot change location, so accessing its stable members should |
3769 | | * always work even after a lock/unlock. |
3770 | | */ |
3771 | 0 | signal_id = node->signal_id; |
3772 | 0 | n_params = node->n_params + 1; |
3773 | |
|
3774 | 0 | if (node->flags & G_SIGNAL_NO_RECURSE) |
3775 | 0 | { |
3776 | 0 | Emission *emission_node = emission_find (signal_id, detail, instance); |
3777 | |
|
3778 | 0 | if (emission_node) |
3779 | 0 | { |
3780 | 0 | emission_node->state = EMISSION_RESTART; |
3781 | 0 | return return_value_altered; |
3782 | 0 | } |
3783 | 0 | } |
3784 | 0 | accumulator = node->accumulator; |
3785 | 0 | emission.instance = instance; |
3786 | 0 | emission.ihint.signal_id = node->signal_id; |
3787 | 0 | emission.ihint.detail = detail; |
3788 | 0 | emission.ihint.run_type = 0; |
3789 | 0 | emission.state = 0; |
3790 | 0 | emission.chain_type = G_TYPE_NONE; |
3791 | 0 | emission_push (&emission); |
3792 | 0 | class_closure = signal_lookup_closure (node, instance); |
3793 | | |
3794 | 0 | EMIT_RESTART: |
3795 | | |
3796 | 0 | if (handler_list) |
3797 | 0 | handler_unref_R (signal_id, instance, handler_list); |
3798 | 0 | max_sequential_handler_number = g_handler_sequential_number; |
3799 | 0 | hlist = handler_list_lookup (signal_id, instance); |
3800 | 0 | handler_list = hlist ? hlist->handlers : NULL; |
3801 | 0 | if (handler_list) |
3802 | 0 | handler_ref (handler_list); |
3803 | | |
3804 | 0 | emission.ihint.run_type = G_SIGNAL_RUN_FIRST | G_SIGNAL_ACCUMULATOR_FIRST_RUN; |
3805 | | |
3806 | 0 | if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure) |
3807 | 0 | { |
3808 | 0 | emission.state = EMISSION_RUN; |
3809 | |
|
3810 | 0 | emission.chain_type = G_TYPE_FROM_INSTANCE (instance); |
3811 | 0 | SIGNAL_UNLOCK (); |
3812 | 0 | return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu); |
3813 | 0 | g_closure_invoke (class_closure, |
3814 | 0 | return_accu, |
3815 | 0 | n_params, |
3816 | 0 | instance_and_params, |
3817 | 0 | &emission.ihint); |
3818 | 0 | if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && |
3819 | 0 | emission.state == EMISSION_RUN) |
3820 | 0 | emission.state = EMISSION_STOP; |
3821 | 0 | SIGNAL_LOCK (); |
3822 | 0 | emission.chain_type = G_TYPE_NONE; |
3823 | 0 | return_value_altered = TRUE; |
3824 | | |
3825 | 0 | if (emission.state == EMISSION_STOP) |
3826 | 0 | goto EMIT_CLEANUP; |
3827 | 0 | else if (emission.state == EMISSION_RESTART) |
3828 | 0 | goto EMIT_RESTART; |
3829 | 0 | } |
3830 | | |
3831 | 0 | if (node->emission_hooks) |
3832 | 0 | { |
3833 | 0 | GHook *hook; |
3834 | 0 | GHook *static_emission_hooks[3]; |
3835 | 0 | size_t n_emission_hooks = 0; |
3836 | 0 | const gboolean may_recurse = TRUE; |
3837 | 0 | guint i; |
3838 | |
|
3839 | 0 | emission.state = EMISSION_HOOK; |
3840 | | |
3841 | | /* Quick check to determine whether any hooks match this emission, |
3842 | | * before committing to the more complex work of calling those hooks. |
3843 | | * We save a few of them into a static array, to try to avoid further |
3844 | | * allocations. |
3845 | | */ |
3846 | 0 | hook = g_hook_first_valid (node->emission_hooks, may_recurse); |
3847 | 0 | while (hook) |
3848 | 0 | { |
3849 | 0 | SignalHook *signal_hook = SIGNAL_HOOK (hook); |
3850 | |
|
3851 | 0 | if (!signal_hook->detail || signal_hook->detail == detail) |
3852 | 0 | { |
3853 | 0 | if (n_emission_hooks < G_N_ELEMENTS (static_emission_hooks)) |
3854 | 0 | { |
3855 | 0 | static_emission_hooks[n_emission_hooks] = |
3856 | 0 | g_hook_ref (node->emission_hooks, hook); |
3857 | 0 | } |
3858 | |
|
3859 | 0 | n_emission_hooks += 1; |
3860 | 0 | } |
3861 | |
|
3862 | 0 | hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse); |
3863 | 0 | } |
3864 | | |
3865 | | /* Re-iterate back through the matching hooks and copy them into |
3866 | | * an array which won’t change when we unlock to call the |
3867 | | * user-provided hook functions. |
3868 | | * These functions may change hook configuration for this signal, |
3869 | | * add / remove signal handlers, etc. |
3870 | | */ |
3871 | 0 | if G_UNLIKELY (n_emission_hooks > 0) |
3872 | 0 | { |
3873 | 0 | guint8 static_hook_returns[G_N_ELEMENTS (static_emission_hooks)]; |
3874 | 0 | GHook **emission_hooks = NULL; |
3875 | 0 | guint8 *hook_returns = NULL; |
3876 | |
|
3877 | 0 | if G_LIKELY (n_emission_hooks <= G_N_ELEMENTS (static_emission_hooks)) |
3878 | 0 | { |
3879 | 0 | emission_hooks = static_emission_hooks; |
3880 | 0 | hook_returns = static_hook_returns; |
3881 | 0 | } |
3882 | 0 | else |
3883 | 0 | { |
3884 | 0 | emission_hooks = g_newa (GHook *, n_emission_hooks); |
3885 | 0 | hook_returns = g_newa (guint8, n_emission_hooks); |
3886 | | |
3887 | | /* We can't just memcpy the ones we have in the static array, |
3888 | | * to the alloca()'d one because otherwise we'd get an invalid |
3889 | | * ID assertion during unref |
3890 | | */ |
3891 | 0 | i = 0; |
3892 | 0 | for (hook = g_hook_first_valid (node->emission_hooks, may_recurse); |
3893 | 0 | hook != NULL; |
3894 | 0 | hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse)) |
3895 | 0 | { |
3896 | 0 | SignalHook *signal_hook = SIGNAL_HOOK (hook); |
3897 | |
|
3898 | 0 | if (!signal_hook->detail || signal_hook->detail == detail) |
3899 | 0 | { |
3900 | 0 | if (i < G_N_ELEMENTS (static_emission_hooks)) |
3901 | 0 | { |
3902 | 0 | emission_hooks[i] = g_steal_pointer (&static_emission_hooks[i]); |
3903 | 0 | g_assert (emission_hooks[i] == hook); |
3904 | 0 | } |
3905 | 0 | else |
3906 | 0 | { |
3907 | 0 | emission_hooks[i] = g_hook_ref (node->emission_hooks, hook); |
3908 | 0 | } |
3909 | | |
3910 | 0 | i += 1; |
3911 | 0 | } |
3912 | 0 | } |
3913 | | |
3914 | 0 | g_assert (i == n_emission_hooks); |
3915 | 0 | } |
3916 | | |
3917 | 0 | SIGNAL_UNLOCK (); |
3918 | |
|
3919 | 0 | for (i = 0; i < n_emission_hooks; ++i) |
3920 | 0 | { |
3921 | 0 | GSignalEmissionHook hook_func; |
3922 | 0 | gboolean need_destroy; |
3923 | 0 | guint old_flags; |
3924 | |
|
3925 | 0 | hook = emission_hooks[i]; |
3926 | 0 | hook_func = (GSignalEmissionHook) hook->func; |
3927 | |
|
3928 | 0 | old_flags = g_atomic_int_or (&hook->flags, G_HOOK_FLAG_IN_CALL); |
3929 | 0 | need_destroy = !hook_func (&emission.ihint, n_params, |
3930 | 0 | instance_and_params, hook->data); |
3931 | |
|
3932 | 0 | if (!(old_flags & G_HOOK_FLAG_IN_CALL)) |
3933 | 0 | { |
3934 | 0 | g_atomic_int_compare_and_exchange (&hook->flags, |
3935 | 0 | old_flags | G_HOOK_FLAG_IN_CALL, |
3936 | 0 | old_flags); |
3937 | 0 | } |
3938 | |
|
3939 | 0 | hook_returns[i] = !!need_destroy; |
3940 | 0 | } |
3941 | |
|
3942 | 0 | SIGNAL_LOCK (); |
3943 | |
|
3944 | 0 | for (i = 0; i < n_emission_hooks; i++) |
3945 | 0 | { |
3946 | 0 | hook = emission_hooks[i]; |
3947 | |
|
3948 | 0 | g_hook_unref (node->emission_hooks, hook); |
3949 | |
|
3950 | 0 | if (hook_returns[i]) |
3951 | 0 | g_hook_destroy_link (node->emission_hooks, hook); |
3952 | 0 | } |
3953 | 0 | } |
3954 | | |
3955 | 0 | if (emission.state == EMISSION_RESTART) |
3956 | 0 | goto EMIT_RESTART; |
3957 | 0 | } |
3958 | | |
3959 | 0 | if (handler_list) |
3960 | 0 | { |
3961 | 0 | Handler *handler = handler_list; |
3962 | | |
3963 | 0 | emission.state = EMISSION_RUN; |
3964 | 0 | handler_ref (handler); |
3965 | 0 | do |
3966 | 0 | { |
3967 | 0 | Handler *tmp; |
3968 | | |
3969 | 0 | if (handler->after) |
3970 | 0 | { |
3971 | 0 | handler_unref_R (signal_id, instance, handler_list); |
3972 | 0 | handler_list = handler; |
3973 | 0 | break; |
3974 | 0 | } |
3975 | 0 | else if (!handler->block_count && (!handler->detail || handler->detail == detail) && |
3976 | 0 | handler->sequential_number < max_sequential_handler_number) |
3977 | 0 | { |
3978 | 0 | SIGNAL_UNLOCK (); |
3979 | 0 | return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu); |
3980 | 0 | g_closure_invoke (handler->closure, |
3981 | 0 | return_accu, |
3982 | 0 | n_params, |
3983 | 0 | instance_and_params, |
3984 | 0 | &emission.ihint); |
3985 | 0 | if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && |
3986 | 0 | emission.state == EMISSION_RUN) |
3987 | 0 | emission.state = EMISSION_STOP; |
3988 | 0 | SIGNAL_LOCK (); |
3989 | 0 | return_value_altered = TRUE; |
3990 | | |
3991 | 0 | tmp = emission.state == EMISSION_RUN ? handler->next : NULL; |
3992 | 0 | } |
3993 | 0 | else |
3994 | 0 | tmp = handler->next; |
3995 | | |
3996 | 0 | if (tmp) |
3997 | 0 | handler_ref (tmp); |
3998 | 0 | handler_unref_R (signal_id, instance, handler_list); |
3999 | 0 | handler_list = handler; |
4000 | 0 | handler = tmp; |
4001 | 0 | } |
4002 | 0 | while (handler); |
4003 | | |
4004 | 0 | if (emission.state == EMISSION_STOP) |
4005 | 0 | goto EMIT_CLEANUP; |
4006 | 0 | else if (emission.state == EMISSION_RESTART) |
4007 | 0 | goto EMIT_RESTART; |
4008 | 0 | } |
4009 | | |
4010 | 0 | emission.ihint.run_type &= ~G_SIGNAL_RUN_FIRST; |
4011 | 0 | emission.ihint.run_type |= G_SIGNAL_RUN_LAST; |
4012 | | |
4013 | 0 | if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure) |
4014 | 0 | { |
4015 | 0 | emission.state = EMISSION_RUN; |
4016 | | |
4017 | 0 | emission.chain_type = G_TYPE_FROM_INSTANCE (instance); |
4018 | 0 | SIGNAL_UNLOCK (); |
4019 | 0 | return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu); |
4020 | 0 | g_closure_invoke (class_closure, |
4021 | 0 | return_accu, |
4022 | 0 | n_params, |
4023 | 0 | instance_and_params, |
4024 | 0 | &emission.ihint); |
4025 | 0 | if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && |
4026 | 0 | emission.state == EMISSION_RUN) |
4027 | 0 | emission.state = EMISSION_STOP; |
4028 | 0 | SIGNAL_LOCK (); |
4029 | 0 | emission.chain_type = G_TYPE_NONE; |
4030 | 0 | return_value_altered = TRUE; |
4031 | | |
4032 | 0 | if (emission.state == EMISSION_STOP) |
4033 | 0 | goto EMIT_CLEANUP; |
4034 | 0 | else if (emission.state == EMISSION_RESTART) |
4035 | 0 | goto EMIT_RESTART; |
4036 | 0 | } |
4037 | | |
4038 | 0 | if (handler_list) |
4039 | 0 | { |
4040 | 0 | Handler *handler = handler_list; |
4041 | | |
4042 | 0 | emission.state = EMISSION_RUN; |
4043 | 0 | handler_ref (handler); |
4044 | 0 | do |
4045 | 0 | { |
4046 | 0 | Handler *tmp; |
4047 | | |
4048 | 0 | if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) && |
4049 | 0 | handler->sequential_number < max_sequential_handler_number) |
4050 | 0 | { |
4051 | 0 | SIGNAL_UNLOCK (); |
4052 | 0 | return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu); |
4053 | 0 | g_closure_invoke (handler->closure, |
4054 | 0 | return_accu, |
4055 | 0 | n_params, |
4056 | 0 | instance_and_params, |
4057 | 0 | &emission.ihint); |
4058 | 0 | if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && |
4059 | 0 | emission.state == EMISSION_RUN) |
4060 | 0 | emission.state = EMISSION_STOP; |
4061 | 0 | SIGNAL_LOCK (); |
4062 | 0 | return_value_altered = TRUE; |
4063 | | |
4064 | 0 | tmp = emission.state == EMISSION_RUN ? handler->next : NULL; |
4065 | 0 | } |
4066 | 0 | else |
4067 | 0 | tmp = handler->next; |
4068 | | |
4069 | 0 | if (tmp) |
4070 | 0 | handler_ref (tmp); |
4071 | 0 | handler_unref_R (signal_id, instance, handler); |
4072 | 0 | handler = tmp; |
4073 | 0 | } |
4074 | 0 | while (handler); |
4075 | | |
4076 | 0 | if (emission.state == EMISSION_STOP) |
4077 | 0 | goto EMIT_CLEANUP; |
4078 | 0 | else if (emission.state == EMISSION_RESTART) |
4079 | 0 | goto EMIT_RESTART; |
4080 | 0 | } |
4081 | | |
4082 | 0 | EMIT_CLEANUP: |
4083 | | |
4084 | 0 | emission.ihint.run_type &= ~G_SIGNAL_RUN_LAST; |
4085 | 0 | emission.ihint.run_type |= G_SIGNAL_RUN_CLEANUP; |
4086 | | |
4087 | 0 | if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure) |
4088 | 0 | { |
4089 | 0 | gboolean need_unset = FALSE; |
4090 | | |
4091 | 0 | emission.state = EMISSION_STOP; |
4092 | | |
4093 | 0 | emission.chain_type = G_TYPE_FROM_INSTANCE (instance); |
4094 | 0 | SIGNAL_UNLOCK (); |
4095 | 0 | if (node->return_type != G_TYPE_NONE && !accumulator) |
4096 | 0 | { |
4097 | 0 | g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE); |
4098 | 0 | need_unset = TRUE; |
4099 | 0 | } |
4100 | 0 | g_closure_invoke (class_closure, |
4101 | 0 | node->return_type != G_TYPE_NONE ? &accu : NULL, |
4102 | 0 | n_params, |
4103 | 0 | instance_and_params, |
4104 | 0 | &emission.ihint); |
4105 | 0 | if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && |
4106 | 0 | emission.state == EMISSION_RUN) |
4107 | 0 | emission.state = EMISSION_STOP; |
4108 | 0 | if (need_unset) |
4109 | 0 | g_value_unset (&accu); |
4110 | 0 | SIGNAL_LOCK (); |
4111 | 0 | return_value_altered = TRUE; |
4112 | |
|
4113 | 0 | emission.chain_type = G_TYPE_NONE; |
4114 | | |
4115 | 0 | if (emission.state == EMISSION_RESTART) |
4116 | 0 | goto EMIT_RESTART; |
4117 | 0 | } |
4118 | | |
4119 | 0 | if (handler_list) |
4120 | 0 | handler_unref_R (signal_id, instance, handler_list); |
4121 | | |
4122 | 0 | emission_pop (&emission); |
4123 | 0 | if (accumulator) |
4124 | 0 | g_value_unset (&accu); |
4125 | |
|
4126 | 0 | TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance))); |
4127 | |
|
4128 | 0 | return return_value_altered; |
4129 | 0 | } |
4130 | | |
4131 | | static void |
4132 | | add_invalid_closure_notify (Handler *handler, |
4133 | | gpointer instance) |
4134 | 0 | { |
4135 | 0 | g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify); |
4136 | 0 | handler->has_invalid_closure_notify = 1; |
4137 | 0 | } |
4138 | | |
4139 | | static void |
4140 | | remove_invalid_closure_notify (Handler *handler, |
4141 | | gpointer instance) |
4142 | 0 | { |
4143 | 0 | if (handler->has_invalid_closure_notify) |
4144 | 0 | { |
4145 | 0 | g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify); |
4146 | 0 | handler->has_invalid_closure_notify = 0; |
4147 | 0 | } |
4148 | 0 | } |
4149 | | |
4150 | | static void |
4151 | | invalid_closure_notify (gpointer instance, |
4152 | | GClosure *closure) |
4153 | 0 | { |
4154 | 0 | Handler *handler; |
4155 | 0 | guint signal_id; |
4156 | |
|
4157 | 0 | SIGNAL_LOCK (); |
4158 | |
|
4159 | 0 | handler = handler_lookup (instance, 0, closure, &signal_id); |
4160 | | /* See https://bugzilla.gnome.org/show_bug.cgi?id=730296 for discussion about this... */ |
4161 | 0 | g_assert (handler != NULL); |
4162 | 0 | g_assert (handler->closure == closure); |
4163 | | |
4164 | 0 | g_hash_table_remove (g_handlers, handler); |
4165 | 0 | handler->sequential_number = 0; |
4166 | 0 | handler->block_count = 1; |
4167 | 0 | handler_unref_R (signal_id, instance, handler); |
4168 | |
|
4169 | 0 | SIGNAL_UNLOCK (); |
4170 | 0 | } |
4171 | | |
4172 | | static const gchar* |
4173 | | type_debug_name (GType type) |
4174 | 0 | { |
4175 | 0 | if (type) |
4176 | 0 | { |
4177 | 0 | const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE); |
4178 | 0 | return name ? name : "<unknown>"; |
4179 | 0 | } |
4180 | 0 | else |
4181 | 0 | return "<invalid>"; |
4182 | 0 | } |
4183 | | |
4184 | | /** |
4185 | | * g_signal_accumulator_true_handled: |
4186 | | * @ihint: standard #GSignalAccumulator parameter |
4187 | | * @return_accu: standard #GSignalAccumulator parameter |
4188 | | * @handler_return: standard #GSignalAccumulator parameter |
4189 | | * @dummy: standard #GSignalAccumulator parameter |
4190 | | * |
4191 | | * A predefined #GSignalAccumulator for signals that return a |
4192 | | * boolean values. The behavior that this accumulator gives is |
4193 | | * that a return of %TRUE stops the signal emission: no further |
4194 | | * callbacks will be invoked, while a return of %FALSE allows |
4195 | | * the emission to continue. The idea here is that a %TRUE return |
4196 | | * indicates that the callback handled the signal, and no further |
4197 | | * handling is needed. |
4198 | | * |
4199 | | * Since: 2.4 |
4200 | | * |
4201 | | * Returns: standard #GSignalAccumulator result |
4202 | | */ |
4203 | | gboolean |
4204 | | g_signal_accumulator_true_handled (GSignalInvocationHint *ihint, |
4205 | | GValue *return_accu, |
4206 | | const GValue *handler_return, |
4207 | | gpointer dummy) |
4208 | 0 | { |
4209 | 0 | gboolean continue_emission; |
4210 | 0 | gboolean signal_handled; |
4211 | | |
4212 | 0 | signal_handled = g_value_get_boolean (handler_return); |
4213 | 0 | g_value_set_boolean (return_accu, signal_handled); |
4214 | 0 | continue_emission = !signal_handled; |
4215 | | |
4216 | 0 | return continue_emission; |
4217 | 0 | } |
4218 | | |
4219 | | /** |
4220 | | * g_signal_accumulator_first_wins: |
4221 | | * @ihint: standard #GSignalAccumulator parameter |
4222 | | * @return_accu: standard #GSignalAccumulator parameter |
4223 | | * @handler_return: standard #GSignalAccumulator parameter |
4224 | | * @dummy: standard #GSignalAccumulator parameter |
4225 | | * |
4226 | | * A predefined #GSignalAccumulator for signals intended to be used as a |
4227 | | * hook for application code to provide a particular value. Usually |
4228 | | * only one such value is desired and multiple handlers for the same |
4229 | | * signal don't make much sense (except for the case of the default |
4230 | | * handler defined in the class structure, in which case you will |
4231 | | * usually want the signal connection to override the class handler). |
4232 | | * |
4233 | | * This accumulator will use the return value from the first signal |
4234 | | * handler that is run as the return value for the signal and not run |
4235 | | * any further handlers (ie: the first handler "wins"). |
4236 | | * |
4237 | | * Returns: standard #GSignalAccumulator result |
4238 | | * |
4239 | | * Since: 2.28 |
4240 | | **/ |
4241 | | gboolean |
4242 | | g_signal_accumulator_first_wins (GSignalInvocationHint *ihint, |
4243 | | GValue *return_accu, |
4244 | | const GValue *handler_return, |
4245 | | gpointer dummy) |
4246 | 0 | { |
4247 | 0 | g_value_copy (handler_return, return_accu); |
4248 | 0 | return FALSE; |
4249 | 0 | } |
4250 | | |
4251 | | /** |
4252 | | * g_clear_signal_handler: |
4253 | | * @handler_id_ptr: A pointer to a handler ID (of type #gulong) of the handler to be disconnected. |
4254 | | * @instance: (type GObject.Object): The instance to remove the signal handler from. |
4255 | | * This pointer may be %NULL or invalid, if the handler ID is zero. |
4256 | | * |
4257 | | * Disconnects a handler from @instance so it will not be called during |
4258 | | * any future or currently ongoing emissions of the signal it has been |
4259 | | * connected to. The @handler_id_ptr is then set to zero, which is never a valid handler ID value (see g_signal_connect()). |
4260 | | * |
4261 | | * If the handler ID is 0 then this function does nothing. |
4262 | | * |
4263 | | * There is also a macro version of this function so that the code |
4264 | | * will be inlined. |
4265 | | * |
4266 | | * Since: 2.62 |
4267 | | */ |
4268 | | void |
4269 | | (g_clear_signal_handler) (gulong *handler_id_ptr, |
4270 | | gpointer instance) |
4271 | 0 | { |
4272 | 0 | g_return_if_fail (handler_id_ptr != NULL); |
4273 | | |
4274 | | #ifndef g_clear_signal_handler |
4275 | | #error g_clear_signal_handler() macro is not defined |
4276 | | #endif |
4277 | | |
4278 | 0 | g_clear_signal_handler (handler_id_ptr, instance); |
4279 | 0 | } |