aboutsummaryrefslogtreecommitdiff
path: root/src/mm-sleep-context.c
blob: 69a9da22aebc14009ef8e83ad51066db05e358e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details:
 *
 * Copyright 2025 Dan Williams <dan@ioncontrol.co>
 */

#include <gio/gio.h>

#include "mm-utils.h"

#include "mm-sleep-context.h"

static void log_object_iface_init (MMLogObjectInterface *iface);

G_DEFINE_TYPE_EXTENDED (MMSleepContext, mm_sleep_context, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (MM_TYPE_LOG_OBJECT, log_object_iface_init))

struct _MMSleepContextPrivate {
    GTask *task;
    guint  timeout_id;
};

enum {
    DONE,
    LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = { 0 };

/*****************************************************************************/

static gchar *
log_object_build_id (MMLogObject *_self)
{
    return g_strdup_printf ("sleep-context");
}

/*****************************************************************************/

static void
timeout_cleanup (MMSleepContext *self)
{
    if (self->priv->timeout_id)
        g_source_remove (self->priv->timeout_id);
    self->priv->timeout_id = 0;
}

void
mm_sleep_context_complete (MMSleepContext *self,
                           GError         *error)
{
    timeout_cleanup (self);

    if (error) {
        mm_obj_dbg (self, "completing with error '%s'", error->message);
        g_task_return_error (self->priv->task, error);
    } else {
        mm_obj_dbg (self, "completing with success");
        g_task_return_boolean (self->priv->task, TRUE);
    }
    g_clear_object (&self->priv->task);
}

static gboolean
operation_timeout (MMSleepContext *self)
{
    mm_obj_dbg (self, "timeout");
    mm_sleep_context_complete (self,
                               g_error_new_literal (G_IO_ERROR,
                                                    G_IO_ERROR_TIMED_OUT,
                                                    "timeout waiting for sleep preparation to complete"));
    return G_SOURCE_REMOVE;
}

static void
operation_ready (MMSleepContext *self,
                 GAsyncResult   *res)
{
    g_autoptr(GError) error = NULL;
    gboolean          success;

    mm_obj_dbg (self, "notifying listeners task is done");

    success = g_task_propagate_boolean (G_TASK (res), &error);
    g_assert (success || error);
    g_signal_emit (self, signals[DONE], 0, error);
}

/*****************************************************************************/

MMSleepContext *
mm_sleep_context_new (guint timeout_seconds)
{
    MMSleepContext *self;

    self = MM_SLEEP_CONTEXT (g_object_new (MM_TYPE_SLEEP_CONTEXT, NULL));
    self->priv->task = g_task_new (self,
                                   NULL,
                                   (GAsyncReadyCallback)operation_ready,
                                   NULL);
    self->priv->timeout_id = g_timeout_add_seconds (timeout_seconds,
                                                    (GSourceFunc)operation_timeout,
                                                    self);
    mm_obj_dbg (self, "new context with %d second timeout", timeout_seconds);

    return self;
}

static void
log_object_iface_init (MMLogObjectInterface *iface)
{
    iface->build_id = log_object_build_id;
}

static void
mm_sleep_context_init (MMSleepContext *self)
{
    /* Initialize opaque pointer to private data */
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                              MM_TYPE_SLEEP_CONTEXT,
                                              MMSleepContextPrivate);
}

static void
dispose (GObject *object)
{
    MMSleepContext *self = MM_SLEEP_CONTEXT (object);

    timeout_cleanup (self);
    /* Task must always be completed and cleared before disposal */
    g_assert (self->priv->task == NULL);

    G_OBJECT_CLASS (mm_sleep_context_parent_class)->dispose (object);
}

static void
mm_sleep_context_class_init (MMSleepContextClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMSleepContextPrivate));

    /* Virtual methods */
    object_class->dispose = dispose;

    signals[DONE] = g_signal_new (MM_SLEEP_CONTEXT_DONE,
                                  G_OBJECT_CLASS_TYPE (object_class),
                                  G_SIGNAL_RUN_FIRST,
                                  G_STRUCT_OFFSET (MMSleepContextClass, done),
                                  NULL, /* accumulator      */
                                  NULL, /* accumulator data */
                                  g_cclosure_marshal_generic,
                                  G_TYPE_NONE,
                                  1,
                                  G_TYPE_ERROR);
}