[gnome-battery-bench] First draft of new power supply infrastructure



commit 00847b34949213cf49ac39e7485343e6a50c5901
Author: Christian Kellner <christian kellner me>
Date:   Thu Mar 16 19:55:40 2017 +0100

    First draft of new power supply infrastructure
    
    Plan is to have own GbbBattery and GbbAdapter classes that can
    then be used by GbbPowerMonitor and also GbbSystemInfo. New
    code will be using GUdev.

 configure.ac       |    5 +-
 src/Makefile.am    |    2 +
 src/power-supply.c |  177 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/power-supply.h |   11 +++
 4 files changed, 193 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2b460e6..5ff4a65 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,13 +17,14 @@ GLIB_GSETTINGS
 
 base_packages="libevdev glib-2.0 >= 2.44.0 gio-unix-2.0"
 x_packages="x11 xi xtst"
+app_packages="json-glib-1.0 gudev-1.0"
 
 AC_PROG_CC
 AM_PROG_CC_C_O
 
 PKG_CHECK_MODULES([HELPER], [$base_packages polkit-gobject-1])
-PKG_CHECK_MODULES([COMMANDLINE], [$base_packages $x_packages json-glib-1.0])
-PKG_CHECK_MODULES([APPLICATION], [$base_packages $x_packages gtk+-3.0 json-glib-1.0])
+PKG_CHECK_MODULES([COMMANDLINE], [$base_packages $x_packages $app_packages])
+PKG_CHECK_MODULES([APPLICATION], [$base_packages $x_packages $app_packages gtk+-3.0])
 
 AC_PATH_PROG(ASCIIDOC, asciidoc, "")
 AC_PATH_PROG(XMLTO, xmlto, "")
diff --git a/src/Makefile.am b/src/Makefile.am
index 1cabc03..5e550bc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,6 +25,8 @@ client_sources =                              \
        event-recorder.h                        \
        power-monitor.c                         \
        power-monitor.h                         \
+       power-supply.h                          \
+       power-supply.c                          \
        system-info.h                           \
        system-info.c                           \
        system-state.c                          \
diff --git a/src/power-supply.c b/src/power-supply.c
new file mode 100644
index 0000000..018c57d
--- /dev/null
+++ b/src/power-supply.c
@@ -0,0 +1,177 @@
+/* -*- mode: C; c-file-style: "stroustrup"; indent-tabs-mode: nil; -*- */
+
+#include <glib.h>
+#include <gudev/gudev.h>
+
+#include "power-supply.h"
+
+struct _GbbBattery {
+    GObject parent;
+
+    GUdevDevice *udevice;
+    char *vendor;
+    char *model;
+};
+
+enum {
+    PROP_BAT_0,
+    PROP_UDEV_DEVICE,
+
+    PROP_VENDOR,
+    PROP_MODEL,
+
+    PROP_BAT_LAST
+};
+
+static GParamSpec *battery_props[PROP_BAT_LAST] = { NULL, };
+
+G_DEFINE_TYPE(GbbBattery, gbb_battery, G_TYPE_OBJECT);
+
+static void
+gbb_battery_finalize(GObject *obj)
+{
+    GbbBattery *bat = GBB_BATTERY(obj);
+
+    g_free(bat->vendor);
+    g_free(bat->model);
+
+    g_clear_object(&bat->udevice);
+
+    G_OBJECT_CLASS(gbb_battery_parent_class)->finalize(obj);
+}
+
+static void
+gbb_battery_get_property(GObject    *object,
+                         guint       prop_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
+{
+    GbbBattery *bat = GBB_BATTERY(object);
+
+    switch (prop_id) {
+    case PROP_UDEV_DEVICE:
+        g_value_set_object(value, bat->udevice);
+        break;
+
+    case PROP_VENDOR:
+        g_value_set_string(value, bat->vendor);
+        break;
+
+    case PROP_MODEL:
+        g_value_set_string(value, bat->model);
+        break;
+    }
+}
+
+static void
+gbb_battery_set_property(GObject     *object,
+                         guint        prop_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+    GbbBattery *bat = GBB_BATTERY(object);
+
+    switch (prop_id) {
+    case PROP_UDEV_DEVICE:
+        bat->udevice = g_value_dup_object(value);
+        break;
+
+    default:
+        g_assert_not_reached();
+    }
+}
+
+static void
+gbb_battery_constructed(GObject *obj)
+{
+    GbbBattery *bat = GBB_BATTERY(obj);
+    GUdevDevice *device = bat->udevice;
+    const gchar *value;
+
+    value = g_udev_device_get_sysfs_attr(device, "manufacturer");
+    bat->vendor = g_strdup(value);
+
+    value = g_udev_device_get_sysfs_attr(device, "model_name");
+    bat->model = g_strdup(value);
+
+    G_OBJECT_CLASS(gbb_battery_parent_class)->constructed(obj);
+}
+
+static void
+gbb_battery_init(GbbBattery *bat)
+{
+}
+
+static void
+gbb_battery_class_init(GbbBatteryClass *klass)
+{
+    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+
+    gobject_class->finalize     = gbb_battery_finalize;
+    gobject_class->get_property = gbb_battery_get_property;
+    gobject_class->set_property = gbb_battery_set_property;
+    gobject_class->constructed  = gbb_battery_constructed;
+
+    battery_props[PROP_UDEV_DEVICE] =
+        g_param_spec_object("udev-device",
+                            NULL, NULL,
+                            G_UDEV_TYPE_DEVICE,
+                            G_PARAM_READWRITE |
+                            G_PARAM_CONSTRUCT_ONLY |
+                            G_PARAM_STATIC_NAME);
+
+    battery_props[PROP_VENDOR] =
+        g_param_spec_string("vendor",
+                            NULL, NULL,
+                            "",
+                            G_PARAM_READABLE |
+                            G_PARAM_STATIC_NAME);
+
+    battery_props[PROP_MODEL] =
+        g_param_spec_string("model",
+                            NULL, NULL,
+                            "",
+                            G_PARAM_READABLE |
+                            G_PARAM_STATIC_NAME);
+
+    g_object_class_install_properties(gobject_class,
+                                      PROP_BAT_LAST,
+                                      battery_props);
+}
+
+
+
+GList *
+gbb_battery_discover()
+{
+    GUdevClient *client;
+    GList *devices;
+    GList *l;
+    GList *supplies = NULL;
+
+    client = g_udev_client_new(NULL);
+
+    devices = g_udev_client_query_by_subsystem(client, "power_supply");
+
+    for (l = devices; l != NULL; l = l->next) {
+        GUdevDevice *device = l->data;
+        const gchar *dev_type;
+
+        dev_type = g_udev_device_get_sysfs_attr(device,
+                                                "type");
+        if (dev_type == NULL) {
+            continue;
+        }
+
+        g_print("Type: %s\n", dev_type);
+        if (g_str_equal(dev_type, "Battery")) {
+            GObject *bat = g_object_new(GBB_TYPE_BATTERY,
+                                        "udev-device", device,
+                                        NULL);
+            supplies = g_list_prepend(supplies, bat);
+        }
+    }
+
+    g_list_free_full(devices, (GDestroyNotify) g_object_unref);
+    return supplies;
+}
diff --git a/src/power-supply.h b/src/power-supply.h
new file mode 100644
index 0000000..70f4d75
--- /dev/null
+++ b/src/power-supply.h
@@ -0,0 +1,11 @@
+#ifndef __POWER_SUPPLY__
+#define __POWER_SUPPLY__
+
+#include <glib-object.h>
+
+#define GBB_TYPE_BATTERY gbb_battery_get_type()
+G_DECLARE_FINAL_TYPE(GbbBattery, gbb_battery, GBB, BATTERY, GObject)
+
+GList* gbb_battery_discover(void);
+
+#endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]