Commit 00847b34 authored by Christian Kellner's avatar Christian Kellner Committed by Christian Kellner
Browse files

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.
parent c01ac22b
......@@ -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, "")
......
......@@ -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 \
......
/* -*- 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;
}
#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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment