Coverage Report

Created: 2025-07-17 06:56

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