barrier: Add GInitable based constructor method

This one does proper error reporting. Via Javascript, barriers are
constructed directly via GObject construction, which currently can't
handle error reporting, but when calling from C we can. However, if we
initialize using GInitable, and use that in our constructor method, once
gjs gains support for construction using GInitable, including the error
reporting, we'll automatically get proper error reporting to Javascript.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2442>
This commit is contained in:
Jonas Ådahl 2022-03-18 17:31:25 +01:00 committed by Marge Bot
parent 27b75d3c96
commit 3c6c1af0a0
2 changed files with 61 additions and 1 deletions

View File

@ -35,7 +35,13 @@ typedef struct _MetaBarrierPrivate
MetaBarrierImpl *impl; MetaBarrierImpl *impl;
} MetaBarrierPrivate; } MetaBarrierPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (MetaBarrier, meta_barrier, G_TYPE_OBJECT) static void initable_iface_init (GInitableIface *initable_iface);
G_DEFINE_TYPE_WITH_CODE (MetaBarrier, meta_barrier, G_TYPE_OBJECT,
G_ADD_PRIVATE (MetaBarrier)
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
initable_iface_init))
G_DEFINE_TYPE (MetaBarrierImpl, meta_barrier_impl, G_TYPE_OBJECT) G_DEFINE_TYPE (MetaBarrierImpl, meta_barrier_impl, G_TYPE_OBJECT)
G_STATIC_ASSERT ((int) META_BARRIER_DIRECTION_POSITIVE_X == G_STATIC_ASSERT ((int) META_BARRIER_DIRECTION_POSITIVE_X ==
@ -225,6 +231,31 @@ meta_barrier_release (MetaBarrier *barrier,
META_BARRIER_IMPL_GET_CLASS (impl)->release (impl, event); META_BARRIER_IMPL_GET_CLASS (impl)->release (impl, event);
} }
static gboolean
meta_barrier_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
MetaBarrier *barrier = META_BARRIER (initable);
MetaBarrierPrivate *priv = meta_barrier_get_instance_private (barrier);
priv = meta_barrier_get_instance_private (barrier);
if (!priv->impl)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to create barrier impl");
return FALSE;
}
return TRUE;
}
static void
initable_iface_init (GInitableIface *initable_iface)
{
initable_iface->init = meta_barrier_initable_init;
}
static void static void
init_barrier_impl (MetaBarrier *barrier) init_barrier_impl (MetaBarrier *barrier)
{ {
@ -393,6 +424,26 @@ meta_barrier_init (MetaBarrier *barrier)
{ {
} }
MetaBarrier *
meta_barrier_new (MetaBackend *backend,
int x1,
int y1,
int x2,
int y2,
MetaBarrierDirection directions,
GError **error)
{
return g_initable_new (META_TYPE_BARRIER,
NULL, error,
"backend", backend,
"x1", x1,
"y1", y1,
"x2", x2,
"y2", y2,
"directions", directions,
NULL);
}
void void
meta_barrier_emit_hit_signal (MetaBarrier *barrier, meta_barrier_emit_hit_signal (MetaBarrier *barrier,
MetaBarrierEvent *event) MetaBarrierEvent *event)

View File

@ -33,6 +33,15 @@ G_DECLARE_FINAL_TYPE (MetaBarrier, meta_barrier,
typedef struct _MetaBarrierEvent MetaBarrierEvent; typedef struct _MetaBarrierEvent MetaBarrierEvent;
META_EXPORT
MetaBarrier * meta_barrier_new (MetaBackend *backend,
int x1,
int y1,
int x2,
int y2,
MetaBarrierDirection directions,
GError **error);
META_EXPORT META_EXPORT
gboolean meta_barrier_is_active (MetaBarrier *barrier); gboolean meta_barrier_is_active (MetaBarrier *barrier);