Coverage Report

Created: 2025-12-31 06:32

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