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