~xavi-garcia-mena/ubuntu/vivid/upower/percentages-power-off

« back to all changes in this revision

Viewing changes to src/dummy/up-backend.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-02-16 10:16:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100216101624-2cmwqsr1ndftdd87
Tags: upstream-0.9.0+git20100216.72bb2
ImportĀ upstreamĀ versionĀ 0.9.0+git20100216.72bb2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <math.h>
 
27
#include <glib/gi18n.h>
 
28
#include <gio/gio.h>
 
29
 
 
30
#include "egg-debug.h"
 
31
 
 
32
#include "up-backend.h"
 
33
#include "up-daemon.h"
 
34
#include "up-marshal.h"
 
35
#include "up-device.h"
 
36
 
 
37
static void     up_backend_class_init   (UpBackendClass *klass);
 
38
static void     up_backend_init (UpBackend              *backend);
 
39
static void     up_backend_finalize     (GObject                *object);
 
40
 
 
41
#define UP_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_BACKEND, UpBackendPrivate))
 
42
 
 
43
struct UpBackendPrivate
 
44
{
 
45
        UpDaemon                *daemon;
 
46
        UpDevice                *device;
 
47
        UpDeviceList            *device_list; /* unused */
 
48
        GObject                 *native;
 
49
};
 
50
 
 
51
enum {
 
52
        SIGNAL_DEVICE_ADDED,
 
53
        SIGNAL_DEVICE_REMOVED,
 
54
        SIGNAL_LAST
 
55
};
 
56
 
 
57
static guint signals [SIGNAL_LAST] = { 0 };
 
58
 
 
59
G_DEFINE_TYPE (UpBackend, up_backend, G_TYPE_OBJECT)
 
60
 
 
61
/**
 
62
 * up_backend_changed_time_cb:
 
63
 **/
 
64
static gboolean
 
65
up_backend_changed_time_cb (UpBackend *backend)
 
66
{
 
67
        UpDevice *device;
 
68
        GTimeVal timeval;
 
69
 
 
70
        //FIXME!
 
71
        device = NULL;
 
72
 
 
73
        /* reset time */
 
74
        g_get_current_time (&timeval);
 
75
        g_object_set (device, "update-time", (guint64) timeval.tv_sec, NULL);
 
76
        return TRUE;
 
77
}
 
78
 
 
79
/**
 
80
 * up_backend_add_cb:
 
81
 **/
 
82
static gboolean
 
83
up_backend_add_cb (UpBackend *backend)
 
84
{
 
85
        gboolean ret;
 
86
 
 
87
        /* coldplug */
 
88
        ret = up_device_coldplug (backend->priv->device, backend->priv->daemon, backend->priv->native);
 
89
        if (!ret) {
 
90
                egg_warning ("failed to coldplug");
 
91
                goto out;
 
92
        }
 
93
 
 
94
        /* emit */
 
95
        g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, backend->priv->native, backend->priv->device);
 
96
 
 
97
        /* setup poll */
 
98
        g_timeout_add_seconds (2, (GSourceFunc) up_backend_changed_time_cb, backend);
 
99
out:
 
100
        return FALSE;
 
101
}
 
102
 
 
103
/**
 
104
 * up_backend_coldplug:
 
105
 * @backend: The %UpBackend class instance
 
106
 * @daemon: The %UpDaemon controlling instance
 
107
 *
 
108
 * Finds all the devices already plugged in, and emits device-add signals for
 
109
 * each of them.
 
110
 *
 
111
 * Return value: %TRUE for success
 
112
 **/
 
113
gboolean
 
114
up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
 
115
{
 
116
        backend->priv->daemon = g_object_ref (daemon);
 
117
        backend->priv->device_list = up_daemon_get_device_list (daemon);
 
118
 
 
119
        /* small delay until first device is added */
 
120
        g_timeout_add_seconds (1, (GSourceFunc) up_backend_add_cb, backend);
 
121
 
 
122
        return TRUE;
 
123
}
 
124
 
 
125
/**
 
126
 * up_backend_class_init:
 
127
 * @klass: The UpBackendClass
 
128
 **/
 
129
static void
 
130
up_backend_class_init (UpBackendClass *klass)
 
131
{
 
132
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
133
        object_class->finalize = up_backend_finalize;
 
134
 
 
135
        signals [SIGNAL_DEVICE_ADDED] =
 
136
                g_signal_new ("device-added",
 
137
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
 
138
                              G_STRUCT_OFFSET (UpBackendClass, device_added),
 
139
                              NULL, NULL, up_marshal_VOID__POINTER_POINTER,
 
140
                              G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
 
141
        signals [SIGNAL_DEVICE_REMOVED] =
 
142
                g_signal_new ("device-removed",
 
143
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
 
144
                              G_STRUCT_OFFSET (UpBackendClass, device_removed),
 
145
                              NULL, NULL, up_marshal_VOID__POINTER_POINTER,
 
146
                              G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
 
147
 
 
148
        g_type_class_add_private (klass, sizeof (UpBackendPrivate));
 
149
}
 
150
 
 
151
/**
 
152
 * up_backend_init:
 
153
 **/
 
154
static void
 
155
up_backend_init (UpBackend *backend)
 
156
{
 
157
        backend->priv = UP_BACKEND_GET_PRIVATE (backend);
 
158
        backend->priv->daemon = NULL;
 
159
        backend->priv->device_list = NULL;
 
160
        backend->priv->native = g_object_new (UP_TYPE_DEVICE, NULL);
 
161
        backend->priv->device = up_device_new ();
 
162
 
 
163
        /* setup dummy */
 
164
        g_object_set (backend->priv->device,
 
165
                      "native-path", "/hal/blows/goats",
 
166
                      "vendor", "hughsie",
 
167
                      "model", "BAT1",
 
168
                      "serial", "0001",
 
169
                      "type", UP_DEVICE_KIND_BATTERY,
 
170
                      "online", FALSE,
 
171
                      "power-supply", TRUE,
 
172
                      "is-present", TRUE,
 
173
                      "is-rechargeable", TRUE,
 
174
                      "has-history", FALSE,
 
175
                      "has-statistics", FALSE,
 
176
                      "state", UP_DEVICE_STATE_DISCHARGING,
 
177
                      "energy", 0.0f,
 
178
                      "energy-empty", 0.0f,
 
179
                      "energy-full", 10.0f,
 
180
                      "energy-full-design", 10.0f,
 
181
                      "energy-rate", 5.0f,
 
182
                      "percentage", 50.0f,
 
183
                      NULL);
 
184
}
 
185
 
 
186
/**
 
187
 * up_backend_finalize:
 
188
 **/
 
189
static void
 
190
up_backend_finalize (GObject *object)
 
191
{
 
192
        UpBackend *backend;
 
193
 
 
194
        g_return_if_fail (UP_IS_BACKEND (object));
 
195
 
 
196
        backend = UP_BACKEND (object);
 
197
 
 
198
        if (backend->priv->daemon != NULL)
 
199
                g_object_unref (backend->priv->daemon);
 
200
        if (backend->priv->device_list != NULL)
 
201
                g_object_unref (backend->priv->device_list);
 
202
 
 
203
        g_object_unref (backend->priv->native);
 
204
        g_object_unref (backend->priv->device);
 
205
 
 
206
        G_OBJECT_CLASS (up_backend_parent_class)->finalize (object);
 
207
}
 
208
 
 
209
/**
 
210
 * up_backend_new:
 
211
 *
 
212
 * Return value: a new %UpBackend object.
 
213
 **/
 
214
UpBackend *
 
215
up_backend_new (void)
 
216
{
 
217
        UpBackend *backend;
 
218
        backend = g_object_new (UP_TYPE_BACKEND, NULL);
 
219
        return UP_BACKEND (backend);
 
220
}
 
221
 
 
222
/***************************************************************************
 
223
 ***                          MAKE CHECK TESTS                           ***
 
224
 ***************************************************************************/
 
225
#ifdef EGG_TEST
 
226
#include "egg-test.h"
 
227
 
 
228
void
 
229
up_backend_test (gpointer user_data)
 
230
{
 
231
        EggTest *test = (EggTest *) user_data;
 
232
        UpBackend *backend;
 
233
 
 
234
        if (!egg_test_start (test, "UpBackend"))
 
235
                return;
 
236
 
 
237
        /************************************************************/
 
238
        egg_test_title (test, "get instance");
 
239
        backend = up_backend_new ();
 
240
        egg_test_assert (test, backend != NULL);
 
241
 
 
242
        /* unref */
 
243
        g_object_unref (backend);
 
244
 
 
245
        egg_test_end (test);
 
246
}
 
247
#endif
 
248