Coverage Report

Created: 2026-01-22 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/glib-2.86.3/gobject/gsignalgroup.c
Line
Count
Source
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 *
3
 * Copyright (C) 2015-2022 Christian Hergert <christian@hergert.me>
4
 * Copyright (C) 2015 Garrett Regier <garrettregier@gmail.com>
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
 * SPDX-License-Identifier: LGPL-2.1-or-later
20
 */
21
22
#include "config.h"
23
#include "glib.h"
24
#include "glibintl.h"
25
26
#include "gparamspecs.h"
27
#include "gsignalgroup.h"
28
#include "gvaluetypes.h"
29
30
/**
31
 * GSignalGroup:
32
 *
33
 * `GSignalGroup` manages a collection of signals on a `GObject`.
34
 *
35
 * `GSignalGroup` simplifies the process of connecting  many signals to a `GObject`
36
 * as a group. As such there is no API to disconnect a signal from the group.
37
 *
38
 * In particular, this allows you to:
39
 *
40
 *  - Change the target instance, which automatically causes disconnection
41
 *    of the signals from the old instance and connecting to the new instance.
42
 *  - Block and unblock signals as a group
43
 *  - Ensuring that blocked state transfers across target instances.
44
 *
45
 * One place you might want to use such a structure is with `GtkTextView` and
46
 * `GtkTextBuffer`. Often times, you'll need to connect to many signals on
47
 * `GtkTextBuffer` from a `GtkTextView` subclass. This allows you to create a
48
 * signal group during instance construction, simply bind the
49
 * `GtkTextView:buffer` property to `GSignalGroup:target` and connect
50
 * all the signals you need. When the `GtkTextView:buffer` property changes
51
 * all of the signals will be transitioned correctly.
52
 *
53
 * Since: 2.72
54
 */
55
56
struct _GSignalGroup
57
{
58
  GObject     parent_instance;
59
60
  GWeakRef    target_ref;
61
  GRecMutex   mutex;
62
  GPtrArray  *handlers;
63
  GType       target_type;
64
  gssize      block_count;
65
66
  guint       has_bound_at_least_once : 1;
67
};
68
69
typedef struct _GSignalGroupClass
70
{
71
  GObjectClass parent_class;
72
73
  void (*bind) (GSignalGroup *self,
74
                GObject      *target);
75
} GSignalGroupClass;
76
77
typedef struct
78
{
79
  GSignalGroup *group;
80
  gulong             handler_id;
81
  GClosure          *closure;
82
  guint              signal_id;
83
  GQuark             signal_detail;
84
  guint              connect_after : 1;
85
} SignalHandler;
86
87
0
G_DEFINE_TYPE (GSignalGroup, g_signal_group, G_TYPE_OBJECT)
88
0
89
0
typedef enum
90
0
{
91
0
  PROP_TARGET = 1,
92
0
  PROP_TARGET_TYPE,
93
0
  LAST_PROP
94
0
} GSignalGroupProperty;
95
0
96
0
enum
97
0
{
98
0
  BIND,
99
0
  UNBIND,
100
0
  LAST_SIGNAL
101
0
};
102
0
103
0
static GParamSpec *properties[LAST_PROP];
104
0
static guint signals[LAST_SIGNAL];
105
0
106
0
static void
107
0
g_signal_group_set_target_type (GSignalGroup *self,
108
0
                                GType         target_type)
109
0
{
110
0
  g_assert (G_IS_SIGNAL_GROUP (self));
111
0
  g_assert (g_type_is_a (target_type, G_TYPE_OBJECT));
112
113
0
  self->target_type = target_type;
114
115
  /* The class must be created at least once for the signals
116
   * to be registered, otherwise g_signal_parse_name() will fail
117
   */
118
0
  if (G_TYPE_IS_INTERFACE (target_type))
119
0
    {
120
0
      if (g_type_default_interface_peek (target_type) == NULL)
121
0
        g_type_default_interface_unref (g_type_default_interface_ref (target_type));
122
0
    }
123
0
  else
124
0
    {
125
0
      if (g_type_class_peek (target_type) == NULL)
126
0
        g_type_class_unref (g_type_class_ref (target_type));
127
0
    }
128
0
}
129
130
static void
131
g_signal_group_gc_handlers (GSignalGroup *self)
132
0
{
133
0
  guint i;
134
135
0
  g_assert (G_IS_SIGNAL_GROUP (self));
136
137
  /*
138
   * Remove any handlers for which the closures have become invalid. We do
139
   * this cleanup lazily to avoid situations where we could have disposal
140
   * active on both the signal group and the peer object.
141
   */
142
143
0
  for (i = self->handlers->len; i > 0; i--)
144
0
    {
145
0
      const SignalHandler *handler = g_ptr_array_index (self->handlers, i - 1);
146
147
0
      g_assert (handler != NULL);
148
0
      g_assert (handler->closure != NULL);
149
150
0
      if (handler->closure->is_invalid)
151
0
        g_ptr_array_remove_index (self->handlers, i - 1);
152
0
    }
153
0
}
154
155
static void
156
g_signal_group__target_weak_notify (gpointer  data,
157
                                    GObject  *where_object_was)
158
0
{
159
0
  GSignalGroup *self = data;
160
0
  guint i;
161
162
0
  g_assert (G_IS_SIGNAL_GROUP (self));
163
0
  g_assert (where_object_was != NULL);
164
165
0
  g_rec_mutex_lock (&self->mutex);
166
167
0
  g_weak_ref_set (&self->target_ref, NULL);
168
169
0
  for (i = 0; i < self->handlers->len; i++)
170
0
    {
171
0
      SignalHandler *handler = g_ptr_array_index (self->handlers, i);
172
173
0
      handler->handler_id = 0;
174
0
    }
175
176
0
  g_signal_emit (self, signals[UNBIND], 0);
177
0
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TARGET]);
178
179
0
  g_rec_mutex_unlock (&self->mutex);
180
0
}
181
182
static void
183
g_signal_group_bind_handler (GSignalGroup  *self,
184
                             SignalHandler *handler,
185
                             GObject       *target)
186
0
{
187
0
  gssize i;
188
189
0
  g_assert (self != NULL);
190
0
  g_assert (G_IS_OBJECT (target));
191
0
  g_assert (handler != NULL);
192
0
  g_assert (handler->signal_id != 0);
193
0
  g_assert (handler->closure != NULL);
194
0
  g_assert (handler->closure->is_invalid == 0);
195
0
  g_assert (handler->handler_id == 0);
196
197
0
  handler->handler_id = g_signal_connect_closure_by_id (target,
198
0
                                                        handler->signal_id,
199
0
                                                        handler->signal_detail,
200
0
                                                        handler->closure,
201
0
                                                        handler->connect_after);
202
203
0
  g_assert (handler->handler_id != 0);
204
205
0
  for (i = 0; i < self->block_count; i++)
206
0
    g_signal_handler_block (target, handler->handler_id);
207
0
}
208
209
static void
210
g_signal_group_bind (GSignalGroup *self,
211
                     GObject      *target)
212
0
{
213
0
  GObject *hold;
214
0
  guint i;
215
216
0
  g_assert (G_IS_SIGNAL_GROUP (self));
217
0
  g_assert (!target || G_IS_OBJECT (target));
218
219
0
  if (target == NULL)
220
0
    return;
221
222
0
  self->has_bound_at_least_once = TRUE;
223
224
0
  hold = g_object_ref (target);
225
226
0
  g_weak_ref_set (&self->target_ref, hold);
227
0
  g_object_weak_ref (hold, g_signal_group__target_weak_notify, self);
228
229
0
  g_signal_group_gc_handlers (self);
230
231
0
  for (i = 0; i < self->handlers->len; i++)
232
0
    {
233
0
      SignalHandler *handler = g_ptr_array_index (self->handlers, i);
234
235
0
      g_signal_group_bind_handler (self, handler, hold);
236
0
    }
237
238
0
  g_signal_emit (self, signals [BIND], 0, hold);
239
240
0
  g_object_unref (hold);
241
0
}
242
243
static void
244
g_signal_group_unbind (GSignalGroup *self)
245
0
{
246
0
  GObject *target;
247
0
  guint i;
248
249
0
  g_return_if_fail (G_IS_SIGNAL_GROUP (self));
250
251
0
  target = g_weak_ref_get (&self->target_ref);
252
253
  /*
254
   * Target may be NULL by this point, as we got notified of its destruction.
255
   * However, if we're early enough, we may get a full reference back and can
256
   * cleanly disconnect our connections.
257
   */
258
259
0
  if (target != NULL)
260
0
    {
261
0
      g_weak_ref_set (&self->target_ref, NULL);
262
263
      /*
264
       * Let go of our weak reference now that we have a full reference
265
       * for the life of this function.
266
       */
267
0
      g_object_weak_unref (target,
268
0
                           g_signal_group__target_weak_notify,
269
0
                           self);
270
0
    }
271
272
0
  g_signal_group_gc_handlers (self);
273
274
0
  for (i = 0; i < self->handlers->len; i++)
275
0
    {
276
0
      SignalHandler *handler;
277
0
      gulong handler_id;
278
279
0
      handler = g_ptr_array_index (self->handlers, i);
280
281
0
      g_assert (handler != NULL);
282
0
      g_assert (handler->signal_id != 0);
283
0
      g_assert (handler->closure != NULL);
284
285
0
      handler_id = handler->handler_id;
286
0
      handler->handler_id = 0;
287
288
      /*
289
       * If @target is NULL, we lost a race to cleanup the weak
290
       * instance and the signal connections have already been
291
       * finalized and therefore nothing to do.
292
       */
293
294
0
      if (target != NULL && handler_id != 0)
295
0
        g_signal_handler_disconnect (target, handler_id);
296
0
    }
297
298
0
  g_signal_emit (self, signals [UNBIND], 0);
299
300
0
  g_clear_object (&target);
301
0
}
302
303
static gboolean
304
g_signal_group_check_target_type (GSignalGroup *self,
305
                                  gpointer      target)
306
0
{
307
0
  if ((target != NULL) &&
308
0
      !g_type_is_a (G_OBJECT_TYPE (target), self->target_type))
309
0
    {
310
0
      g_critical ("Failed to set GSignalGroup of target type %s "
311
0
                  "using target %p of type %s",
312
0
                  g_type_name (self->target_type),
313
0
                  target, G_OBJECT_TYPE_NAME (target));
314
0
      return FALSE;
315
0
    }
316
317
0
  return TRUE;
318
0
}
319
320
/**
321
 * g_signal_group_block:
322
 * @self: the #GSignalGroup
323
 *
324
 * Blocks all signal handlers managed by @self so they will not
325
 * be called during any signal emissions. Must be unblocked exactly
326
 * the same number of times it has been blocked to become active again.
327
 *
328
 * This blocked state will be kept across changes of the target instance.
329
 *
330
 * Since: 2.72
331
 */
332
void
333
g_signal_group_block (GSignalGroup *self)
334
0
{
335
0
  GObject *target;
336
0
  guint i;
337
338
0
  g_return_if_fail (G_IS_SIGNAL_GROUP (self));
339
0
  g_return_if_fail (self->block_count >= 0);
340
341
0
  g_rec_mutex_lock (&self->mutex);
342
343
0
  self->block_count++;
344
345
0
  target = g_weak_ref_get (&self->target_ref);
346
347
0
  if (target == NULL)
348
0
    goto unlock;
349
350
0
  for (i = 0; i < self->handlers->len; i++)
351
0
    {
352
0
      const SignalHandler *handler = g_ptr_array_index (self->handlers, i);
353
354
0
      g_assert (handler != NULL);
355
0
      g_assert (handler->signal_id != 0);
356
0
      g_assert (handler->closure != NULL);
357
0
      g_assert (handler->handler_id != 0);
358
359
0
      g_signal_handler_block (target, handler->handler_id);
360
0
    }
361
362
0
  g_object_unref (target);
363
364
0
unlock:
365
0
  g_rec_mutex_unlock (&self->mutex);
366
0
}
367
368
/**
369
 * g_signal_group_unblock:
370
 * @self: the #GSignalGroup
371
 *
372
 * Unblocks all signal handlers managed by @self so they will be
373
 * called again during any signal emissions unless it is blocked
374
 * again. Must be unblocked exactly the same number of times it
375
 * has been blocked to become active again.
376
 *
377
 * Since: 2.72
378
 */
379
void
380
g_signal_group_unblock (GSignalGroup *self)
381
0
{
382
0
  GObject *target;
383
0
  guint i;
384
385
0
  g_return_if_fail (G_IS_SIGNAL_GROUP (self));
386
0
  g_return_if_fail (self->block_count > 0);
387
388
0
  g_rec_mutex_lock (&self->mutex);
389
390
0
  self->block_count--;
391
392
0
  target = g_weak_ref_get (&self->target_ref);
393
0
  if (target == NULL)
394
0
    goto unlock;
395
396
0
  for (i = 0; i < self->handlers->len; i++)
397
0
    {
398
0
      const SignalHandler *handler = g_ptr_array_index (self->handlers, i);
399
400
0
      g_assert (handler != NULL);
401
0
      g_assert (handler->signal_id != 0);
402
0
      g_assert (handler->closure != NULL);
403
0
      g_assert (handler->handler_id != 0);
404
405
0
      g_signal_handler_unblock (target, handler->handler_id);
406
0
    }
407
408
0
  g_object_unref (target);
409
410
0
unlock:
411
0
  g_rec_mutex_unlock (&self->mutex);
412
0
}
413
414
/**
415
 * g_signal_group_dup_target:
416
 * @self: the #GSignalGroup
417
 *
418
 * Gets the target instance used when connecting signals.
419
 *
420
 * Returns: (nullable) (transfer full) (type GObject): The target instance
421
 *
422
 * Since: 2.72
423
 */
424
gpointer
425
g_signal_group_dup_target (GSignalGroup *self)
426
0
{
427
0
  GObject *target;
428
429
0
  g_return_val_if_fail (G_IS_SIGNAL_GROUP (self), NULL);
430
431
0
  g_rec_mutex_lock (&self->mutex);
432
0
  target = g_weak_ref_get (&self->target_ref);
433
0
  g_rec_mutex_unlock (&self->mutex);
434
435
0
  return target;
436
0
}
437
438
/**
439
 * g_signal_group_set_target:
440
 * @self: the #GSignalGroup.
441
 * @target: (nullable) (type GObject) (transfer none): The target instance used
442
 *     when connecting signals.
443
 *
444
 * Sets the target instance used when connecting signals. Any signal
445
 * that has been registered with g_signal_group_connect_object() or
446
 * similar functions will be connected to this object.
447
 *
448
 * If the target instance was previously set, signals will be
449
 * disconnected from that object prior to connecting to @target.
450
 *
451
 * Since: 2.72
452
 */
453
void
454
g_signal_group_set_target (GSignalGroup *self,
455
                           gpointer      target)
456
0
{
457
0
  GObject *object;
458
459
0
  g_return_if_fail (G_IS_SIGNAL_GROUP (self));
460
461
0
  g_rec_mutex_lock (&self->mutex);
462
463
0
  object = g_weak_ref_get (&self->target_ref);
464
465
0
  if (object == (GObject *)target)
466
0
    goto cleanup;
467
468
0
  if (!g_signal_group_check_target_type (self, target))
469
0
    goto cleanup;
470
471
  /* Only emit unbind if we've ever called bind */
472
0
  if (self->has_bound_at_least_once)
473
0
    g_signal_group_unbind (self);
474
475
0
  g_signal_group_bind (self, target);
476
477
0
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TARGET]);
478
479
0
cleanup:
480
0
  g_clear_object (&object);
481
0
  g_rec_mutex_unlock (&self->mutex);
482
0
}
483
484
static void
485
signal_handler_free (gpointer data)
486
0
{
487
0
  SignalHandler *handler = data;
488
489
0
  if (handler->closure != NULL)
490
0
    g_closure_invalidate (handler->closure);
491
492
0
  handler->handler_id = 0;
493
0
  handler->signal_id = 0;
494
0
  handler->signal_detail = 0;
495
0
  g_clear_pointer (&handler->closure, g_closure_unref);
496
0
  g_slice_free (SignalHandler, handler);
497
0
}
498
499
static void
500
g_signal_group_constructed (GObject *object)
501
0
{
502
0
  GSignalGroup *self = (GSignalGroup *)object;
503
0
  GObject *target;
504
505
0
  g_rec_mutex_lock (&self->mutex);
506
507
0
  target = g_weak_ref_get (&self->target_ref);
508
0
  if (!g_signal_group_check_target_type (self, target))
509
0
    g_signal_group_set_target (self, NULL);
510
511
0
  G_OBJECT_CLASS (g_signal_group_parent_class)->constructed (object);
512
513
0
  g_clear_object (&target);
514
515
0
  g_rec_mutex_unlock (&self->mutex);
516
0
}
517
518
static void
519
g_signal_group_dispose (GObject *object)
520
0
{
521
0
  GSignalGroup *self = (GSignalGroup *)object;
522
523
0
  g_rec_mutex_lock (&self->mutex);
524
525
0
  g_signal_group_gc_handlers (self);
526
527
0
  if (self->has_bound_at_least_once)
528
0
    g_signal_group_unbind (self);
529
530
0
  g_ptr_array_set_size (self->handlers, 0);
531
532
0
  g_rec_mutex_unlock (&self->mutex);
533
534
0
  G_OBJECT_CLASS (g_signal_group_parent_class)->dispose (object);
535
0
}
536
537
static void
538
g_signal_group_finalize (GObject *object)
539
0
{
540
0
  GSignalGroup *self = (GSignalGroup *)object;
541
542
0
  g_weak_ref_clear (&self->target_ref);
543
0
  g_rec_mutex_clear (&self->mutex);
544
0
  g_ptr_array_unref (self->handlers);
545
546
0
  G_OBJECT_CLASS (g_signal_group_parent_class)->finalize (object);
547
0
}
548
549
static void
550
g_signal_group_get_property (GObject    *object,
551
                             guint       prop_id,
552
                             GValue     *value,
553
                             GParamSpec *pspec)
554
0
{
555
0
  GSignalGroup *self = G_SIGNAL_GROUP (object);
556
557
0
  switch ((GSignalGroupProperty) prop_id)
558
0
    {
559
0
    case PROP_TARGET:
560
0
      g_value_take_object (value, g_signal_group_dup_target (self));
561
0
      break;
562
563
0
    case PROP_TARGET_TYPE:
564
0
      g_value_set_gtype (value, self->target_type);
565
0
      break;
566
567
0
    default:
568
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569
0
    }
570
0
}
571
572
static void
573
g_signal_group_set_property (GObject      *object,
574
                             guint         prop_id,
575
                             const GValue *value,
576
                             GParamSpec   *pspec)
577
0
{
578
0
  GSignalGroup *self = G_SIGNAL_GROUP (object);
579
580
0
  switch ((GSignalGroupProperty) prop_id)
581
0
    {
582
0
    case PROP_TARGET:
583
0
      g_signal_group_set_target (self, g_value_get_object (value));
584
0
      break;
585
586
0
    case PROP_TARGET_TYPE:
587
0
      g_signal_group_set_target_type (self, g_value_get_gtype (value));
588
0
      break;
589
590
0
    default:
591
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
592
0
    }
593
0
}
594
595
static void
596
g_signal_group_class_init (GSignalGroupClass *klass)
597
0
{
598
0
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
599
600
0
  object_class->constructed = g_signal_group_constructed;
601
0
  object_class->dispose = g_signal_group_dispose;
602
0
  object_class->finalize = g_signal_group_finalize;
603
0
  object_class->get_property = g_signal_group_get_property;
604
0
  object_class->set_property = g_signal_group_set_property;
605
606
  /**
607
   * GSignalGroup:target
608
   *
609
   * The target instance used when connecting signals.
610
   *
611
   * Since: 2.72
612
   */
613
0
  properties[PROP_TARGET] =
614
0
      g_param_spec_object ("target", NULL, NULL,
615
0
                           G_TYPE_OBJECT,
616
0
                           (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
617
618
  /**
619
   * GSignalGroup:target-type
620
   *
621
   * The #GType of the target property.
622
   *
623
   * Since: 2.72
624
   */
625
0
  properties[PROP_TARGET_TYPE] =
626
0
      g_param_spec_gtype ("target-type", NULL, NULL,
627
0
                          G_TYPE_OBJECT,
628
0
                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
629
630
0
  g_object_class_install_properties (object_class, LAST_PROP, properties);
631
632
  /**
633
   * GSignalGroup::bind:
634
   * @self: the #GSignalGroup
635
   * @instance: a #GObject containing the new value for #GSignalGroup:target
636
   *
637
   * This signal is emitted when #GSignalGroup:target is set to a new value
638
   * other than %NULL. It is similar to #GObject::notify on `target` except it
639
   * will not emit when #GSignalGroup:target is %NULL and also allows for
640
   * receiving the #GObject without a data-race.
641
   *
642
   * Since: 2.72
643
   */
644
0
  signals[BIND] =
645
0
      g_signal_new ("bind",
646
0
                    G_TYPE_FROM_CLASS (klass),
647
0
                    G_SIGNAL_RUN_LAST,
648
0
                    0,
649
0
                    NULL, NULL, NULL,
650
0
                    G_TYPE_NONE,
651
0
                    1,
652
0
                    G_TYPE_OBJECT);
653
654
  /**
655
   * GSignalGroup::unbind:
656
   * @self: a #GSignalGroup
657
   *
658
   * This signal is emitted when the target instance of @self is set to a
659
   * new #GObject.
660
   *
661
   * This signal will only be emitted if the previous target of @self is
662
   * non-%NULL.
663
   *
664
   * Since: 2.72
665
   */
666
0
  signals[UNBIND] =
667
0
      g_signal_new ("unbind",
668
0
                    G_TYPE_FROM_CLASS (klass),
669
0
                    G_SIGNAL_RUN_LAST,
670
0
                    0,
671
0
                    NULL, NULL, NULL,
672
0
                    G_TYPE_NONE,
673
0
                    0);
674
0
}
675
676
static void
677
g_signal_group_init (GSignalGroup *self)
678
0
{
679
0
  g_rec_mutex_init (&self->mutex);
680
0
  self->handlers = g_ptr_array_new_with_free_func (signal_handler_free);
681
0
  self->target_type = G_TYPE_OBJECT;
682
0
}
683
684
/**
685
 * g_signal_group_new:
686
 * @target_type: the #GType of the target instance.
687
 *
688
 * Creates a new #GSignalGroup for target instances of @target_type.
689
 *
690
 * Returns: (transfer full): a new #GSignalGroup
691
 *
692
 * Since: 2.72
693
 */
694
GSignalGroup *
695
g_signal_group_new (GType target_type)
696
0
{
697
0
  g_return_val_if_fail (g_type_is_a (target_type, G_TYPE_OBJECT), NULL);
698
699
0
  return g_object_new (G_TYPE_SIGNAL_GROUP,
700
0
                       "target-type", target_type,
701
0
                       NULL);
702
0
}
703
704
static gboolean
705
g_signal_group_connect_closure_ (GSignalGroup   *self,
706
                                 const gchar    *detailed_signal,
707
                                 GClosure       *closure,
708
                                 gboolean        after)
709
0
{
710
0
  GObject *target;
711
0
  SignalHandler *handler;
712
0
  guint signal_id;
713
0
  GQuark signal_detail;
714
715
0
  g_return_val_if_fail (G_IS_SIGNAL_GROUP (self), FALSE);
716
0
  g_return_val_if_fail (detailed_signal != NULL, FALSE);
717
0
  g_return_val_if_fail (closure != NULL, FALSE);
718
719
0
  if (!g_signal_parse_name (detailed_signal, self->target_type,
720
0
                            &signal_id, &signal_detail, TRUE))
721
0
    {
722
0
      g_critical ("Invalid signal name ā€œ%sā€", detailed_signal);
723
0
      return FALSE;
724
0
    }
725
726
0
  g_rec_mutex_lock (&self->mutex);
727
728
0
  if (self->has_bound_at_least_once)
729
0
    {
730
0
      g_critical ("Cannot add signals after setting target");
731
0
      g_rec_mutex_unlock (&self->mutex);
732
0
      return FALSE;
733
0
    }
734
735
0
  handler = g_slice_new0 (SignalHandler);
736
0
  handler->group = self;
737
0
  handler->signal_id = signal_id;
738
0
  handler->signal_detail = signal_detail;
739
0
  handler->closure = g_closure_ref (closure);
740
0
  handler->connect_after = (guint) after;
741
742
0
  g_closure_sink (closure);
743
744
0
  g_ptr_array_add (self->handlers, handler);
745
746
0
  target = g_weak_ref_get (&self->target_ref);
747
748
0
  if (target != NULL)
749
0
    {
750
0
      g_signal_group_bind_handler (self, handler, target);
751
0
      g_object_unref (target);
752
0
    }
753
754
  /* Lazily remove any old handlers on connect */
755
0
  g_signal_group_gc_handlers (self);
756
757
0
  g_rec_mutex_unlock (&self->mutex);
758
0
  return TRUE;
759
0
}
760
761
/**
762
 * g_signal_group_connect_closure:
763
 * @self: a #GSignalGroup
764
 * @detailed_signal: a string of the form `signal-name` with optional `::signal-detail`
765
 * @closure: (not nullable): the closure to connect.
766
 * @after: whether the handler should be called before or after the
767
 *  default handler of the signal.
768
 *
769
 * Connects @closure to the signal @detailed_signal on #GSignalGroup:target.
770
 *
771
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
772
 *
773
 * Since: 2.74
774
 */
775
void
776
g_signal_group_connect_closure (GSignalGroup   *self,
777
                                const gchar    *detailed_signal,
778
                                GClosure       *closure,
779
                                gboolean        after)
780
0
{
781
0
  g_signal_group_connect_closure_ (self, detailed_signal, closure, after);
782
0
}
783
784
static void
785
g_signal_group_connect_full (GSignalGroup   *self,
786
                             const gchar    *detailed_signal,
787
                             GCallback       c_handler,
788
                             gpointer        data,
789
                             GClosureNotify  notify,
790
                             GConnectFlags   flags,
791
                             gboolean        is_object)
792
0
{
793
0
  GClosure *closure;
794
795
0
  g_return_if_fail (c_handler != NULL);
796
0
  g_return_if_fail (!is_object || G_IS_OBJECT (data));
797
798
0
  if ((flags & G_CONNECT_SWAPPED) != 0)
799
0
    closure = g_cclosure_new_swap (c_handler, data, notify);
800
0
  else
801
0
    closure = g_cclosure_new (c_handler, data, notify);
802
803
0
  if (is_object)
804
0
    {
805
      /* Set closure->is_invalid when data is disposed. We only track this to avoid
806
       * reconnecting in the future. However, we do a round of cleanup when ever we
807
       * connect a new object or the target changes to GC the old handlers.
808
       */
809
0
      g_object_watch_closure (data, closure);
810
0
    }
811
812
0
  if (!g_signal_group_connect_closure_ (self,
813
0
                                        detailed_signal,
814
0
                                        closure,
815
0
                                        (flags & G_CONNECT_AFTER) != 0))
816
0
    g_closure_unref (closure);
817
0
}
818
819
/**
820
 * g_signal_group_connect_object: (skip)
821
 * @self: a #GSignalGroup
822
 * @detailed_signal: a string of the form `signal-name` with optional `::signal-detail`
823
 * @c_handler: (scope notified): the #GCallback to connect
824
 * @object: (not nullable) (transfer none): the #GObject to pass as data to @c_handler calls
825
 * @flags: #GConnectFlags for the signal connection
826
 *
827
 * Connects @c_handler to the signal @detailed_signal on #GSignalGroup:target.
828
 *
829
 * Ensures that the @object stays alive during the call to @c_handler
830
 * by temporarily adding a reference count. When the @object is destroyed
831
 * the signal handler will automatically be removed.
832
 *
833
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
834
 *
835
 * Since: 2.72
836
 */
837
void
838
g_signal_group_connect_object (GSignalGroup  *self,
839
                               const gchar   *detailed_signal,
840
                               GCallback      c_handler,
841
                               gpointer       object,
842
                               GConnectFlags  flags)
843
0
{
844
0
  g_return_if_fail (G_IS_OBJECT (object));
845
846
0
  g_signal_group_connect_full (self, detailed_signal, c_handler, object, NULL,
847
0
                               flags, TRUE);
848
0
}
849
850
/**
851
 * g_signal_group_connect_data:
852
 * @self: a #GSignalGroup
853
 * @detailed_signal: a string of the form "signal-name::detail"
854
 * @c_handler: (scope notified) (closure data) (destroy notify): the #GCallback to connect
855
 * @data: the data to pass to @c_handler calls
856
 * @notify: function to be called when disposing of @self
857
 * @flags: the flags used to create the signal connection
858
 *
859
 * Connects @c_handler to the signal @detailed_signal
860
 * on the target instance of @self.
861
 *
862
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
863
 *
864
 * Since: 2.72
865
 */
866
void
867
g_signal_group_connect_data (GSignalGroup   *self,
868
                             const gchar    *detailed_signal,
869
                             GCallback       c_handler,
870
                             gpointer        data,
871
                             GClosureNotify  notify,
872
                             GConnectFlags   flags)
873
0
{
874
0
  g_signal_group_connect_full (self, detailed_signal, c_handler, data, notify,
875
0
                               flags, FALSE);
876
0
}
877
878
/**
879
 * g_signal_group_connect: (skip)
880
 * @self: a #GSignalGroup
881
 * @detailed_signal: a string of the form "signal-name::detail"
882
 * @c_handler: (scope notified): the #GCallback to connect
883
 * @data: the data to pass to @c_handler calls
884
 *
885
 * Connects @c_handler to the signal @detailed_signal
886
 * on the target instance of @self.
887
 *
888
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
889
 *
890
 * Since: 2.72
891
 */
892
void
893
g_signal_group_connect (GSignalGroup *self,
894
                        const gchar  *detailed_signal,
895
                        GCallback     c_handler,
896
                        gpointer      data)
897
0
{
898
0
  g_signal_group_connect_full (self, detailed_signal, c_handler, data, NULL,
899
0
                               0, FALSE);
900
0
}
901
902
/**
903
 * g_signal_group_connect_after: (skip)
904
 * @self: a #GSignalGroup
905
 * @detailed_signal: a string of the form "signal-name::detail"
906
 * @c_handler: (scope notified): the #GCallback to connect
907
 * @data: the data to pass to @c_handler calls
908
 *
909
 * Connects @c_handler to the signal @detailed_signal
910
 * on the target instance of @self.
911
 *
912
 * The @c_handler will be called after the default handler of the signal.
913
 *
914
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
915
 *
916
 * Since: 2.72
917
 */
918
void
919
g_signal_group_connect_after (GSignalGroup *self,
920
                              const gchar  *detailed_signal,
921
                              GCallback     c_handler,
922
                              gpointer      data)
923
0
{
924
0
  g_signal_group_connect_full (self, detailed_signal, c_handler,
925
0
                               data, NULL, G_CONNECT_AFTER, FALSE);
926
0
}
927
928
/**
929
 * g_signal_group_connect_swapped:
930
 * @self: a #GSignalGroup
931
 * @detailed_signal: a string of the form "signal-name::detail"
932
 * @c_handler: (scope async): the #GCallback to connect
933
 * @data: the data to pass to @c_handler calls
934
 *
935
 * Connects @c_handler to the signal @detailed_signal
936
 * on the target instance of @self.
937
 *
938
 * The instance on which the signal is emitted and @data
939
 * will be swapped when calling @c_handler.
940
 *
941
 * You cannot connect a signal handler after #GSignalGroup:target has been set.
942
 *
943
 * Since: 2.72
944
 */
945
void
946
g_signal_group_connect_swapped (GSignalGroup *self,
947
                                const gchar  *detailed_signal,
948
                                GCallback     c_handler,
949
                                gpointer      data)
950
0
{
951
0
  g_signal_group_connect_full (self, detailed_signal, c_handler, data, NULL,
952
0
                               G_CONNECT_SWAPPED, FALSE);
953
0
}