Coverage Report

Created: 2026-02-26 06:23

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