diff options
author | Richard Hughes <richard@hughsie.com> | 2010-03-26 12:49:01 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2010-03-26 12:49:01 +0000 |
commit | 6d607a406cef0484cab383e36a5e216a19320ec6 (patch) | |
tree | 90288937a72be8114bc6f289aa39b0f2b7f7d972 /src | |
parent | daa2d5f3b745e52e9b2a9117d8020e7d4378369a (diff) | |
download | upower-6d607a406cef0484cab383e36a5e216a19320ec6.tar.gz |
Convert the daemon to using objects from libupower-glib, not devkit-power-gobject
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/up-device.c | 24 | ||||
-rw-r--r-- | src/up-history.c | 218 | ||||
-rw-r--r-- | src/up-qos.c | 108 | ||||
-rw-r--r-- | src/up-wakeups.c | 111 |
5 files changed, 242 insertions, 227 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 74fa3b7..cbdc065 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,9 +14,8 @@ INCLUDES = \ -D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT \ -DG_UDEV_API_IS_SUBJECT_TO_CHANGE \ -DUP_COMPILATION \ - -DUP_DISABLE_DEPRECATED \ + -DUP_DISABLE_DEPRECATED \ -I$(top_srcdir)/libupower-glib \ - -I$(top_srcdir)/devkit-power-gobject \ -I$(top_srcdir) \ $(GIO_CFLAGS) \ $(DBUS_GLIB_CFLAGS) \ @@ -24,7 +23,6 @@ INCLUDES = \ $(POLKIT_CFLAGS) \ $(GLIB_CFLAGS) -DEVKIT_POWER_LIBS = $(top_builddir)/devkit-power-gobject/libdevkit-power-gobject.la UPOWER_LIBS = $(top_builddir)/libupower-glib/libupower-glib.la BUILT_SOURCES = \ @@ -95,8 +93,7 @@ upowerd_LDADD = \ $(GIO_LIBS) \ $(DBUS_GLIB_LIBS) \ $(POLKIT_LIBS) \ - $(UPOWER_LIBS) \ - $(DEVKIT_POWER_LIBS) + $(UPOWER_LIBS) if BACKEND_TYPE_DUMMY upowerd_LDADD += \ @@ -152,7 +149,6 @@ up_self_test_LDADD = \ $(GLIB_LIBS) \ $(GIO_CFLAGS) \ $(DBUS_GLIB_LIBS) \ - $(DEVKIT_POWER_LIBS) \ $(POLKIT_LIBS) \ $(UPOWER_LIBS) diff --git a/src/up-device.c b/src/up-device.c index 326de3f..41662bb 100644 --- a/src/up-device.c +++ b/src/up-device.c @@ -37,8 +37,8 @@ #include "up-native.h" #include "up-device.h" #include "up-history.h" -#include "up-history-obj.h" -#include "up-stats-obj.h" +#include "up-history-item.h" +#include "up-stats-item.h" #include "up-marshal.h" #include "up-device-glue.h" @@ -596,7 +596,7 @@ up_device_get_statistics (UpDevice *device, const gchar *type, DBusGMethodInvoca GError *error; GPtrArray *array = NULL; GPtrArray *complex; - const UpStatsObj *obj; + UpStatsItem *item; GValue *value; guint i; @@ -633,11 +633,13 @@ up_device_get_statistics (UpDevice *device, const gchar *type, DBusGMethodInvoca /* copy data to dbus struct */ complex = g_ptr_array_sized_new (array->len); for (i=0; i<array->len; i++) { - obj = (const UpStatsObj *) g_ptr_array_index (array, i); + item = (UpStatsItem *) g_ptr_array_index (array, i); value = g_new0 (GValue, 1); g_value_init (value, UP_DBUS_STRUCT_DOUBLE_DOUBLE); g_value_take_boxed (value, dbus_g_type_specialized_construct (UP_DBUS_STRUCT_DOUBLE_DOUBLE)); - dbus_g_type_struct_set (value, 0, obj->value, 1, obj->accuracy, -1); + dbus_g_type_struct_set (value, + 0, up_stats_item_get_value (item), + 1, up_stats_item_get_accuracy (item), -1); g_ptr_array_add (complex, g_value_get_boxed (value)); g_free (value); } @@ -658,7 +660,7 @@ up_device_get_history (UpDevice *device, const gchar *type_string, guint timespa GError *error; GPtrArray *array = NULL; GPtrArray *complex; - const UpHistoryObj *obj; + UpHistoryItem *item; GValue *value; guint i; UpHistoryType type = UP_HISTORY_TYPE_UNKNOWN; @@ -697,20 +699,22 @@ up_device_get_history (UpDevice *device, const gchar *type_string, guint timespa /* copy data to dbus struct */ complex = g_ptr_array_sized_new (array->len); for (i=0; i<array->len; i++) { - obj = (const UpHistoryObj *) g_ptr_array_index (array, i); + item = (UpHistoryItem *) g_ptr_array_index (array, i); value = g_new0 (GValue, 1); g_value_init (value, UP_DBUS_STRUCT_UINT_DOUBLE_UINT); g_value_take_boxed (value, dbus_g_type_specialized_construct (UP_DBUS_STRUCT_UINT_DOUBLE_UINT)); - dbus_g_type_struct_set (value, 0, obj->time, 1, obj->value, 2, obj->state, -1); + dbus_g_type_struct_set (value, + 0, up_history_item_get_time (item), + 1, up_history_item_get_value (item), + 2, up_history_item_get_state (item), -1); g_ptr_array_add (complex, g_value_get_boxed (value)); g_free (value); } dbus_g_method_return (context, complex); out: - if (array != NULL) { + if (array != NULL) g_ptr_array_unref (array); - } return TRUE; } diff --git a/src/up-history.c b/src/up-history.c index 19c263b..48497e1 100644 --- a/src/up-history.c +++ b/src/up-history.c @@ -29,8 +29,8 @@ #include "egg-debug.h" #include "up-history.h" -#include "up-stats-obj.h" -#include "up-history-obj.h" +#include "up-stats-item.h" +#include "up-history-item.h" static void up_history_finalize (GObject *object); @@ -65,9 +65,9 @@ G_DEFINE_TYPE (UpHistory, up_history, G_TYPE_OBJECT) * up_history_array_copy_cb: **/ static void -up_history_array_copy_cb (const UpHistoryObj *obj, GPtrArray *dest) +up_history_array_copy_cb (UpHistoryItem *item, GPtrArray *dest) { - g_ptr_array_add (dest, up_history_obj_copy (obj)); + g_ptr_array_add (dest, g_object_ref (item)); } /** @@ -106,8 +106,8 @@ up_history_array_copy_cb (const UpHistoryObj *obj, GPtrArray *dest) static GPtrArray * up_history_array_limit_resolution (GPtrArray *array, guint max_num) { - const UpHistoryObj *obj; - UpHistoryObj *nobj; + UpHistoryItem *item; + UpHistoryItem *item_new; gfloat division; guint length; gint i; @@ -121,7 +121,7 @@ up_history_array_limit_resolution (GPtrArray *array, guint max_num) guint step = 1; gfloat preset; - new = g_ptr_array_new_with_free_func ((GDestroyNotify) up_history_obj_free); + new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); egg_debug ("length of array (before) %i", array->len); /* check length */ @@ -135,10 +135,10 @@ up_history_array_limit_resolution (GPtrArray *array, guint max_num) } /* last element */ - obj = (const UpHistoryObj *) g_ptr_array_index (array, length-1); - last = obj->time; - obj = (const UpHistoryObj *) g_ptr_array_index (array, 0); - first = obj->time; + item = (UpHistoryItem *) g_ptr_array_index (array, length-1); + last = up_history_item_get_time (item); + item = (UpHistoryItem *) g_ptr_array_index (array, 0); + first = up_history_item_get_time (item); division = (first - last) / (gfloat) max_num; egg_debug ("Using a x division of %f (first=%i,last=%i)", division, first, last); @@ -147,36 +147,38 @@ up_history_array_limit_resolution (GPtrArray *array, guint max_num) * division algorithm so we don't keep diluting the previous * data with a conventional 1-in-x type algorithm. */ for (i=length-1; i>=0; i--) { - obj = (const UpHistoryObj *) g_ptr_array_index (array, i); + item = (UpHistoryItem *) g_ptr_array_index (array, i); preset = last + (division * (gfloat) step); /* if state changed or we went over the preset do a new point */ - if (count > 0 && (obj->time > preset || obj->state != state)) { - nobj = up_history_obj_new (); - nobj->time = time_s / count; - nobj->value = value / count; - nobj->state = state; - g_ptr_array_add (new, nobj); + if (count > 0 && + (up_history_item_get_time (item) > preset || + up_history_item_get_state (item) != state)) { + item_new = up_history_item_new (); + up_history_item_set_time (item_new, time_s / count); + up_history_item_set_value (item_new, value / count); + up_history_item_set_state (item_new, state); + g_ptr_array_add (new, item_new); step++; - time_s = obj->time; - value = obj->value; - state = obj->state; + time_s = up_history_item_get_time (item); + value = up_history_item_get_value (item); + state = up_history_item_get_state (item); count = 1; } else { count++; - time_s += obj->time; - value += obj->value; + time_s += up_history_item_get_time (item); + value += up_history_item_get_value (item); } } /* only add if nonzero */ if (count > 0) { - nobj = up_history_obj_new (); - nobj->time = time_s / count; - nobj->value = value / count; - nobj->state = state; - g_ptr_array_add (new, nobj); + item_new = up_history_item_new (); + up_history_item_set_time (item_new, time_s / count); + up_history_item_set_value (item_new, value / count); + up_history_item_set_state (item_new, state); + g_ptr_array_add (new, item_new); } /* check length */ @@ -192,7 +194,7 @@ static GPtrArray * up_history_copy_array_timespan (const GPtrArray *array, guint timespan) { guint i; - const UpHistoryObj *obj; + UpHistoryItem *item; GPtrArray *array_new; GTimeVal timeval; @@ -203,13 +205,14 @@ up_history_copy_array_timespan (const GPtrArray *array, guint timespan) /* new data */ array_new = g_ptr_array_new (); g_get_current_time (&timeval); + egg_debug ("limiting data to last %i seconds", timespan); /* treat the timespan like a range, and search backwards */ timespan *= 0.95f; for (i=array->len-1; i>0; i--) { - obj = (const UpHistoryObj *) g_ptr_array_index (array, i); - if (timeval.tv_sec - obj->time < timespan) - g_ptr_array_add (array_new, up_history_obj_copy (obj)); + item = (UpHistoryItem *) g_ptr_array_index (array, i); + if (timeval.tv_sec - up_history_item_get_time (item) < timespan) + g_ptr_array_add (array_new, g_object_ref (item)); } return array_new; @@ -266,10 +269,10 @@ up_history_get_profile_data (UpHistory *history, gboolean charging) gfloat average = 0.0f; guint bin; guint oldbin = 999; - const UpHistoryObj *obj_last = NULL; - const UpHistoryObj *obj; - const UpHistoryObj *obj_old = NULL; - UpStatsObj *stats; + UpHistoryItem *item_last = NULL; + UpHistoryItem *item; + UpHistoryItem *item_old = NULL; + UpStatsItem *stats; GPtrArray *array; GPtrArray *data; guint time_s; @@ -281,20 +284,21 @@ up_history_get_profile_data (UpHistory *history, gboolean charging) /* create 100 item list and set to zero */ data = g_ptr_array_new (); for (i=0; i<101; i++) { - stats = up_stats_obj_create (0.0f, 0.0f); + stats = up_stats_item_new (); g_ptr_array_add (data, stats); } array = history->priv->data_charge; for (i=0; i<array->len; i++) { - obj = (const UpHistoryObj *) g_ptr_array_index (array, i); - if (obj_last == NULL || obj->state != obj_last->state) { - obj_old = NULL; + item = (UpHistoryItem *) g_ptr_array_index (array, i); + if (item_last == NULL || + up_history_item_get_state (item) != up_history_item_get_state (item_last)) { + item_old = NULL; goto cont; } /* round to the nearest int */ - bin = rint (obj->value); + bin = rint (up_history_item_get_value (item)); /* ensure bin is in range */ if (bin >= data->len) @@ -303,45 +307,45 @@ up_history_get_profile_data (UpHistory *history, gboolean charging) /* different */ if (oldbin != bin) { oldbin = bin; - if (obj_old != NULL) { + if (item_old != NULL) { /* not enough or too much difference */ - value = fabs (obj->value - obj_old->value); + value = fabs (up_history_item_get_value (item) - up_history_item_get_value (item_old)); if (value < 0.01f) { - obj_old = NULL; + item_old = NULL; goto cont; } if (value > 3.0f) { - obj_old = NULL; + item_old = NULL; goto cont; } - time_s = obj->time - obj_old->time; + time_s = up_history_item_get_time (item) - up_history_item_get_time (item_old); /* use the accuracy field as a counter for now */ - if ((charging && obj->state == UP_DEVICE_STATE_CHARGING) || - (!charging && obj->state == UP_DEVICE_STATE_DISCHARGING)) { - stats = (UpStatsObj *) g_ptr_array_index (data, bin); - stats->value += time_s; - stats->accuracy++; + if ((charging && up_history_item_get_state (item) == UP_DEVICE_STATE_CHARGING) || + (!charging && up_history_item_get_state (item) == UP_DEVICE_STATE_DISCHARGING)) { + stats = (UpStatsItem *) g_ptr_array_index (data, bin); + up_stats_item_set_value (stats, up_stats_item_get_value (stats) + time_s); + up_stats_item_set_accuracy (stats, up_stats_item_get_accuracy (stats) + 1); } } - obj_old = obj; + item_old = item; } cont: - obj_last = obj; + item_last = item; } /* divide the value by the number of samples to make the average */ for (i=0; i<101; i++) { - stats = (UpStatsObj *) g_ptr_array_index (data, i); - if (stats->accuracy != 0) - stats->value = stats->value / stats->accuracy; + stats = (UpStatsItem *) g_ptr_array_index (data, i); + if (up_stats_item_get_accuracy (stats) != 0) + up_stats_item_set_value (stats, up_stats_item_get_value (stats) / up_stats_item_get_accuracy (stats)); } /* find non-zero accuracy values for the average */ for (i=0; i<101; i++) { - stats = (UpStatsObj *) g_ptr_array_index (data, i); - if (stats->accuracy > 0) { - total_value += stats->value; + stats = (UpStatsItem *) g_ptr_array_index (data, i); + if (up_stats_item_get_accuracy (stats) > 0) { + total_value += up_stats_item_get_value (stats); non_zero_accuracy++; } } @@ -354,19 +358,17 @@ cont: /* make the values a factor of 0, so that 1.0 is twice the * average, and -1.0 is half the average */ for (i=0; i<101; i++) { - stats = (UpStatsObj *) g_ptr_array_index (data, i); - if (stats->accuracy > 0) - stats->value = (stats->value - average) / average; + stats = (UpStatsItem *) g_ptr_array_index (data, i); + if (up_stats_item_get_accuracy (stats) > 0) + up_stats_item_set_value (stats, (up_stats_item_get_value (stats) - average) / average); else - stats->value = 0.0f; + up_stats_item_set_value (stats, 0.0f); } /* accuracy is a percentage scale, where each cycle = 20% */ for (i=0; i<101; i++) { - stats = (UpStatsObj *) g_ptr_array_index (data, i); - stats->accuracy *= 20; - if (stats->accuracy > 100.0f) - stats->accuracy = 100.0f; + stats = (UpStatsItem *) g_ptr_array_index (data, i); + up_stats_item_set_accuracy (stats, up_stats_item_get_accuracy (stats) * 20.0f); } return data; @@ -398,7 +400,7 @@ static gboolean up_history_array_to_file (GPtrArray *list, const gchar *filename) { guint i; - const UpHistoryObj *obj; + UpHistoryItem *item; gchar *part; GString *string; gboolean ret = TRUE; @@ -407,8 +409,8 @@ up_history_array_to_file (GPtrArray *list, const gchar *filename) /* generate data */ string = g_string_new (""); for (i=0; i<list->len; i++) { - obj = g_ptr_array_index (list, i); - part = up_history_obj_to_string (obj); + item = g_ptr_array_index (list, i); + part = up_history_item_to_string (item); if (part == NULL) { ret = FALSE; break; @@ -454,7 +456,7 @@ up_history_array_from_file (GPtrArray *list, const gchar *filename) gchar **parts = NULL; guint i; guint length; - UpHistoryObj *obj; + UpHistoryItem *item; /* do we exist */ ret = g_file_test (filename, G_FILE_TEST_EXISTS); @@ -482,9 +484,10 @@ up_history_array_from_file (GPtrArray *list, const gchar *filename) /* add valid entries */ egg_debug ("loading %i items of data from %s", length, filename); for (i=0; i<length-1; i++) { - obj = up_history_obj_from_string (parts[i]); - if (obj != NULL) - g_ptr_array_add (list, obj); + item = up_history_item_new (); + ret = up_history_item_set_from_string (item, parts[i]); + if (ret) + g_ptr_array_add (list, item); } out: @@ -548,7 +551,7 @@ static gboolean up_history_is_low_power (UpHistory *history) { guint length; - const UpHistoryObj *obj; + UpHistoryItem *item; /* current status is always up to date */ if (history->priv->state != UP_DEVICE_STATE_DISCHARGING) @@ -560,12 +563,12 @@ up_history_is_low_power (UpHistory *history) return FALSE; /* get the last saved charge object */ - obj = (const UpHistoryObj *) g_ptr_array_index (history->priv->data_charge, length-1); - if (obj->state != UP_DEVICE_STATE_DISCHARGING) + item = (UpHistoryItem *) g_ptr_array_index (history->priv->data_charge, length-1); + if (up_history_item_get_state (item) != UP_DEVICE_STATE_DISCHARGING) return FALSE; /* high enough */ - if (obj->value > 10) + if (up_history_item_get_value (item) > 10) return FALSE; /* we are low power */ @@ -609,7 +612,7 @@ static gboolean up_history_load_data (UpHistory *history) { gchar *filename; - UpHistoryObj *obj; + UpHistoryItem *item; /* load rate history from disk */ filename = up_history_get_filename (history, "rate"); @@ -632,12 +635,13 @@ up_history_load_data (UpHistory *history) g_free (filename); /* save a marker so we don't use incomplete percentages */ - obj = up_history_obj_create (0, UP_DEVICE_STATE_UNKNOWN); - g_ptr_array_add (history->priv->data_rate, up_history_obj_copy (obj)); - g_ptr_array_add (history->priv->data_charge, up_history_obj_copy (obj)); - g_ptr_array_add (history->priv->data_time_full, up_history_obj_copy (obj)); - g_ptr_array_add (history->priv->data_time_empty, up_history_obj_copy (obj)); - up_history_obj_free (obj); + item = up_history_item_new (); + up_history_item_set_time_to_present (item); + g_ptr_array_add (history->priv->data_rate, g_object_ref (item)); + g_ptr_array_add (history->priv->data_charge, g_object_ref (item)); + g_ptr_array_add (history->priv->data_time_full, g_object_ref (item)); + g_ptr_array_add (history->priv->data_time_empty, g_object_ref (item)); + g_object_unref (item); up_history_schedule_save (history); return TRUE; @@ -685,7 +689,7 @@ up_history_set_state (UpHistory *history, UpDeviceState state) gboolean up_history_set_charge_data (UpHistory *history, gdouble percentage) { - UpHistoryObj *obj; + UpHistoryItem *item; g_return_val_if_fail (UP_IS_HISTORY (history), FALSE); @@ -697,8 +701,11 @@ up_history_set_charge_data (UpHistory *history, gdouble percentage) return FALSE; /* add to array and schedule save file */ - obj = up_history_obj_create (percentage, history->priv->state); - g_ptr_array_add (history->priv->data_charge, obj); + item = up_history_item_new (); + up_history_item_set_time_to_present (item); + up_history_item_set_value (item, percentage); + up_history_item_set_state (item, history->priv->state); + g_ptr_array_add (history->priv->data_charge, item); up_history_schedule_save (history); /* save last value */ @@ -713,7 +720,7 @@ up_history_set_charge_data (UpHistory *history, gdouble percentage) gboolean up_history_set_rate_data (UpHistory *history, gdouble rate) { - UpHistoryObj *obj; + UpHistoryItem *item; g_return_val_if_fail (UP_IS_HISTORY (history), FALSE); @@ -725,8 +732,11 @@ up_history_set_rate_data (UpHistory *history, gdouble rate) return FALSE; /* add to array and schedule save file */ - obj = up_history_obj_create (rate, history->priv->state); - g_ptr_array_add (history->priv->data_rate, obj); + item = up_history_item_new (); + up_history_item_set_time_to_present (item); + up_history_item_set_value (item, rate); + up_history_item_set_state (item, history->priv->state); + g_ptr_array_add (history->priv->data_rate, item); up_history_schedule_save (history); /* save last value */ @@ -741,7 +751,7 @@ up_history_set_rate_data (UpHistory *history, gdouble rate) gboolean up_history_set_time_full_data (UpHistory *history, gint64 time_s) { - UpHistoryObj *obj; + UpHistoryItem *item; g_return_val_if_fail (UP_IS_HISTORY (history), FALSE); @@ -755,8 +765,11 @@ up_history_set_time_full_data (UpHistory *history, gint64 time_s) return FALSE; /* add to array and schedule save file */ - obj = up_history_obj_create ((gdouble) time_s, history->priv->state); - g_ptr_array_add (history->priv->data_time_full, obj); + item = up_history_item_new (); + up_history_item_set_time_to_present (item); + up_history_item_set_value (item, (gdouble) time_s); + up_history_item_set_state (item, history->priv->state); + g_ptr_array_add (history->priv->data_time_full, item); up_history_schedule_save (history); /* save last value */ @@ -771,7 +784,7 @@ up_history_set_time_full_data (UpHistory *history, gint64 time_s) gboolean up_history_set_time_empty_data (UpHistory *history, gint64 time_s) { - UpHistoryObj *obj; + UpHistoryItem *item; g_return_val_if_fail (UP_IS_HISTORY (history), FALSE); @@ -785,8 +798,11 @@ up_history_set_time_empty_data (UpHistory *history, gint64 time_s) return FALSE; /* add to array and schedule save file */ - obj = up_history_obj_create ((gdouble) time_s, history->priv->state); - g_ptr_array_add (history->priv->data_time_empty, obj); + item = up_history_item_new (); + up_history_item_set_time_to_present (item); + up_history_item_set_value (item, (gdouble) time_s); + up_history_item_set_state (item, history->priv->state); + g_ptr_array_add (history->priv->data_time_empty, item); up_history_schedule_save (history); /* save last value */ @@ -819,10 +835,10 @@ up_history_init (UpHistory *history) history->priv->rate_last = 0; history->priv->percentage_last = 0; history->priv->state = UP_DEVICE_STATE_UNKNOWN; - history->priv->data_rate = g_ptr_array_new_with_free_func ((GDestroyNotify) up_history_obj_free); - history->priv->data_charge = g_ptr_array_new_with_free_func ((GDestroyNotify) up_history_obj_free); - history->priv->data_time_full = g_ptr_array_new_with_free_func ((GDestroyNotify) up_history_obj_free); - history->priv->data_time_empty = g_ptr_array_new_with_free_func ((GDestroyNotify) up_history_obj_free); + history->priv->data_rate = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + history->priv->data_charge = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + history->priv->data_time_full = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + history->priv->data_time_empty = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); history->priv->save_id = 0; } diff --git a/src/up-qos.c b/src/up-qos.c index d4d610e..81fbce9 100644 --- a/src/up-qos.c +++ b/src/up-qos.c @@ -38,7 +38,7 @@ #include "up-marshal.h" #include "up-daemon.h" #include "up-polkit.h" -#include "up-qos-obj.h" +#include "up-qos-item.h" #include "up-qos-glue.h" #include "up-types.h" @@ -81,19 +81,19 @@ G_DEFINE_TYPE (UpQos, up_qos, G_TYPE_OBJECT) /** * up_qos_find_from_cookie: **/ -static UpQosObj * +static UpQosItem * up_qos_find_from_cookie (UpQos *qos, guint32 cookie) { guint i; GPtrArray *data; - UpQosObj *obj; + UpQosItem *item; /* search list */ data = qos->priv->data; for (i=0; i<data->len; i++) { - obj = g_ptr_array_index (data, i); - if (obj->cookie == cookie) - return obj; + item = g_ptr_array_index (data, i); + if (up_qos_item_get_cookie (item) == cookie) + return item; } /* nothing found */ @@ -127,14 +127,16 @@ up_qos_get_lowest (UpQos *qos, UpQosKind type) guint i; gint lowest = G_MAXINT; GPtrArray *data; - UpQosObj *obj; + UpQosItem *item; /* find lowest */ data = qos->priv->data; for (i=0; i<data->len; i++) { - obj = g_ptr_array_index (data, i); - if (obj->type == type && obj->value > 0 && obj->value < lowest) - lowest = obj->value; + item = g_ptr_array_index (data, i); + if (up_qos_item_get_kind (item) == type && + up_qos_item_get_value (item) > 0 && + up_qos_item_get_value (item) < lowest) + lowest = up_qos_item_get_value (item); } /* over-ride */ @@ -244,7 +246,7 @@ out: void up_qos_request_latency (UpQos *qos, const gchar *type_text, gint value, gboolean persistent, DBusGMethodInvocation *context) { - UpQosObj *obj; + UpQosItem *item; gchar *sender = NULL; const gchar *auth; gchar *cmdline = NULL; @@ -309,25 +311,28 @@ up_qos_request_latency (UpQos *qos, const gchar *type_text, gint value, gboolean } /* seems okay, add to list */ - obj = g_new (UpQosObj, 1); - obj->cookie = up_qos_generate_cookie (qos); - obj->sender = g_strdup (sender); - obj->value = value; - obj->uid = uid; - obj->pid = pid; - obj->cmdline = g_strdup (cmdline); - obj->persistent = persistent; - obj->type = type; - g_ptr_array_add (qos->priv->data, obj); + item = up_qos_item_new (); + up_qos_item_set_cookie (item, up_qos_generate_cookie (qos)); + up_qos_item_set_sender (item, sender); + up_qos_item_set_value (item, value); + up_qos_item_set_uid (item, uid); + up_qos_item_set_pid (item, pid); + up_qos_item_set_cmdline (item, cmdline); + up_qos_item_set_persistent (item, persistent); + up_qos_item_set_kind (item, type); + g_ptr_array_add (qos->priv->data, item); egg_debug ("Recieved Qos from '%s' (%i:%i)' saving as #%i", - obj->sender, obj->value, obj->persistent, obj->cookie); + up_qos_item_get_sender (item), + up_qos_item_get_value (item), + up_qos_item_get_persistent (item), + up_qos_item_get_cookie (item)); /* TODO: if persistent add to datadase */ /* only emit event on the first one */ up_qos_latency_perhaps_changed (qos, type); - dbus_g_method_return (context, obj->cookie); + dbus_g_method_return (context, up_qos_item_get_cookie (item)); out: if (subject != NULL) g_object_unref (subject); @@ -336,32 +341,21 @@ out: } /** - * up_qos_free_data_obj: - **/ -static void -up_qos_free_data_obj (UpQosObj *obj) -{ - g_free (obj->cmdline); - g_free (obj->sender); - g_free (obj); -} - -/** * up_qos_cancel_request: * - * Removes a cookie and associated data from the UpQosObj struct. + * Removes a cookie and associated data from the UpQosItem struct. **/ void up_qos_cancel_request (UpQos *qos, guint cookie, DBusGMethodInvocation *context) { - UpQosObj *obj; + UpQosItem *item; GError *error; gchar *sender = NULL; PolkitSubject *subject = NULL; /* find the correct cookie */ - obj = up_qos_find_from_cookie (qos, cookie); - if (obj == NULL) { + item = up_qos_find_from_cookie (qos, cookie); + if (item == NULL) { error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "Cannot find request for #%i", cookie); dbus_g_method_return_error (context, error); @@ -377,7 +371,7 @@ up_qos_cancel_request (UpQos *qos, guint cookie, DBusGMethodInvocation *context) } /* are we not the sender? */ - if (g_strcmp0 (sender, obj->sender) != 0) { + if (g_strcmp0 (sender, up_qos_item_get_sender (item)) != 0) { subject = up_polkit_get_subject (qos->priv->polkit, context); if (subject == NULL) goto out; @@ -388,8 +382,8 @@ up_qos_cancel_request (UpQos *qos, guint cookie, DBusGMethodInvocation *context) egg_debug ("Clear #%i", cookie); /* remove object from list */ - g_ptr_array_remove (qos->priv->data, obj); - up_qos_latency_perhaps_changed (qos, obj->type); + g_ptr_array_remove (qos->priv->data, item); + up_qos_latency_perhaps_changed (qos, up_qos_item_get_kind (item)); /* TODO: if persistent remove from datadase */ @@ -455,25 +449,25 @@ up_qos_get_latency_requests (UpQos *qos, GPtrArray **requests, GError **error) { guint i; GPtrArray *data; - UpQosObj *obj; + UpQosItem *item; *requests = g_ptr_array_new (); data = qos->priv->data; for (i=0; i<data->len; i++) { GValue elem = {0}; - obj = g_ptr_array_index (data, i); + item = g_ptr_array_index (data, i); g_value_init (&elem, UP_QOS_REQUESTS_STRUCT_TYPE); g_value_take_boxed (&elem, dbus_g_type_specialized_construct (UP_QOS_REQUESTS_STRUCT_TYPE)); dbus_g_type_struct_set (&elem, - 0, obj->cookie, - 1, obj->uid, - 2, obj->pid, - 3, obj->cmdline, - 4, 0, //obj->timespec, - 5, obj->persistent, - 6, up_qos_kind_to_string (obj->type), - 7, obj->value, + 0, up_qos_item_get_cookie (item), + 1, up_qos_item_get_uid (item), + 2, up_qos_item_get_pid (item), + 3, up_qos_item_get_cmdline (item), + 4, 0, //up_qos_item_get_timespec (item), + 5, up_qos_item_get_persistent (item), + 6, up_qos_kind_to_string (up_qos_item_get_kind (item)), + 7, up_qos_item_get_value (item), G_MAXUINT); g_ptr_array_add (*requests, g_value_get_boxed (&elem)); } @@ -494,16 +488,16 @@ up_qos_remove_dbus (UpQos *qos, const gchar *sender) { guint i; GPtrArray *data; - UpQosObj *obj; + UpQosItem *item; /* remove *any* senders that match the sender */ data = qos->priv->data; for (i=0; i<data->len; i++) { - obj = g_ptr_array_index (data, i); - if (strcmp (obj->sender, sender) == 0) { + item = g_ptr_array_index (data, i); + if (strcmp (up_qos_item_get_sender (item), sender) == 0) { egg_debug ("Auto-revoked idle qos on %s", sender); - g_ptr_array_remove (qos->priv->data, obj); - up_qos_latency_perhaps_changed (qos, obj->type); + g_ptr_array_remove (qos->priv->data, item); + up_qos_latency_perhaps_changed (qos, up_qos_item_get_kind (item)); } } } @@ -557,7 +551,7 @@ up_qos_init (UpQos *qos) qos->priv = UP_QOS_GET_PRIVATE (qos); qos->priv->polkit = up_polkit_new (); - qos->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) up_qos_free_data_obj); + qos->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); /* TODO: need to load persistent values */ /* setup lowest */ diff --git a/src/up-wakeups.c b/src/up-wakeups.c index a675810..df0e807 100644 --- a/src/up-wakeups.c +++ b/src/up-wakeups.c @@ -33,7 +33,7 @@ #include "up-daemon.h" #include "up-marshal.h" #include "up-wakeups-glue.h" -#include "up-wakeups-obj.h" +#include "up-wakeup-item.h" static void up_wakeups_finalize (GObject *object); static gboolean up_wakeups_timerstats_enable (UpWakeups *wakeups); @@ -109,14 +109,18 @@ out: } /** - * up_wakeups_data_obj_compare: + * up_wakeups_data_item_compare: **/ static gint -up_wakeups_data_obj_compare (const UpWakeupsObj **obj1, const UpWakeupsObj **obj2) +up_wakeups_data_item_compare (UpWakeupItem **item1, UpWakeupItem **item2) { - if ((*obj1)->value > (*obj2)->value) + gdouble val1; + gdouble val2; + val1 = up_wakeup_item_get_value (*item1); + val2 = up_wakeup_item_get_value (*item2); + if (val1 > val2) return -1; - if ((*obj1)->value < (*obj2)->value) + if (val1 < val2) return 1; return -0; } @@ -124,22 +128,22 @@ up_wakeups_data_obj_compare (const UpWakeupsObj **obj1, const UpWakeupsObj **obj /** * up_wakeups_data_get_or_create: **/ -static UpWakeupsObj * +static UpWakeupItem * up_wakeups_data_get_or_create (UpWakeups *wakeups, guint id) { guint i; - UpWakeupsObj *obj; + UpWakeupItem *item; for (i=0; i<wakeups->priv->data->len; i++) { - obj = g_ptr_array_index (wakeups->priv->data, i); - if (obj->id == id) + item = g_ptr_array_index (wakeups->priv->data, i); + if (up_wakeup_item_get_id (item) == id) goto out; } - obj = up_wakeups_obj_new (); - obj->id = id; - g_ptr_array_add (wakeups->priv->data, obj); + item = up_wakeup_item_new (); + up_wakeup_item_set_id (item, id); + g_ptr_array_add (wakeups->priv->data, item); out: - return obj; + return item; } /** @@ -150,11 +154,11 @@ up_wakeups_data_get_total (UpWakeups *wakeups) { guint i; gfloat total = 0; - UpWakeupsObj *obj; + UpWakeupItem *item; for (i=0; i<wakeups->priv->data->len; i++) { - obj = g_ptr_array_index (wakeups->priv->data, i); - total += obj->value; + item = g_ptr_array_index (wakeups->priv->data, i); + total += up_wakeup_item_get_value (item); } return (guint) total; } @@ -197,7 +201,7 @@ up_wakeups_get_data (UpWakeups *wakeups, GPtrArray **data, GError **error) { guint i; GPtrArray *array; - UpWakeupsObj *obj; + UpWakeupItem *item; /* no capability */ if (!wakeups->priv->has_capability) { @@ -209,24 +213,24 @@ up_wakeups_get_data (UpWakeups *wakeups, GPtrArray **data, GError **error) up_wakeups_timerstats_enable (wakeups); /* sort data */ - g_ptr_array_sort (wakeups->priv->data, (GCompareFunc) up_wakeups_data_obj_compare); + g_ptr_array_sort (wakeups->priv->data, (GCompareFunc) up_wakeups_data_item_compare); *data = g_ptr_array_new (); array = wakeups->priv->data; for (i=0; i<array->len; i++) { GValue elem = {0}; - obj = g_ptr_array_index (array, i); - if (obj->value < UP_WAKEUPS_SMALLEST_VALUE) + item = g_ptr_array_index (array, i); + if (up_wakeup_item_get_value (item) < UP_WAKEUPS_SMALLEST_VALUE) continue; g_value_init (&elem, UP_WAKEUPS_REQUESTS_STRUCT_TYPE); g_value_take_boxed (&elem, dbus_g_type_specialized_construct (UP_WAKEUPS_REQUESTS_STRUCT_TYPE)); dbus_g_type_struct_set (&elem, - 0, obj->is_userspace, - 1, obj->id, - 2, obj->value, - 3, obj->cmdline, - 4, obj->details, + 0, up_wakeup_item_get_is_userspace (item), + 1, up_wakeup_item_get_id (item), + 2, up_wakeup_item_get_value (item), + 3, up_wakeup_item_get_cmdline (item), + 4, up_wakeup_item_get_details (item), G_MAXUINT); g_ptr_array_add (*data, g_value_get_boxed (&elem)); } @@ -341,15 +345,15 @@ up_wakeups_poll_kernel_cb (UpWakeups *wakeups) guint irq; guint interrupts; GPtrArray *sections; - UpWakeupsObj *obj; + UpWakeupItem *item; egg_debug ("event"); /* set all kernel data objs to zero */ for (i=0; i<wakeups->priv->data->len; i++) { - obj = g_ptr_array_index (wakeups->priv->data, i); - if (!obj->is_userspace) - obj->value = 0.0f; + item = g_ptr_array_index (wakeups->priv->data, i); + if (!up_wakeup_item_get_is_userspace (item)) + up_wakeup_item_set_value (item, 0.0f); } /* get the data */ @@ -415,8 +419,8 @@ up_wakeups_poll_kernel_cb (UpWakeups *wakeups) found = g_ptr_array_index (sections, cpus+1); /* save in database */ - obj = up_wakeups_data_get_or_create (wakeups, irq); - if (obj->details == NULL) { + item = up_wakeups_data_get_or_create (wakeups, irq); + if (up_wakeup_item_get_details (item) == NULL) { /* remove the interrupt type */ found2 = strstr (found, "IO-APIC-fasteoi"); @@ -425,19 +429,19 @@ up_wakeups_poll_kernel_cb (UpWakeups *wakeups) found2 = strstr (found, "IO-APIC-edge"); if (found2 != NULL) found = g_strchug ((gchar*)found2+14); - obj->details = g_strdup (found); + up_wakeup_item_set_details (item, found); /* we special */ if (special_ipi) - obj->cmdline = g_strdup ("kernel-ipi"); + up_wakeup_item_set_cmdline (item, "kernel-ipi"); else - obj->cmdline = g_strdup ("interrupt"); - obj->is_userspace = FALSE; + up_wakeup_item_set_cmdline (item, "interrupt"); + up_wakeup_item_set_is_userspace (item, FALSE); } /* we report this in minutes, not seconds */ - if (obj->old > 0) - obj->value = (interrupts - obj->old) / (gfloat) UP_WAKEUPS_POLL_INTERVAL_KERNEL; - obj->old = interrupts; + if (up_wakeup_item_get_old (item) > 0) + up_wakeup_item_set_value (item, (interrupts - up_wakeup_item_get_old (item)) / (gfloat) UP_WAKEUPS_POLL_INTERVAL_KERNEL); + up_wakeup_item_set_old (item, interrupts); skip: g_ptr_array_unref (sections); } @@ -462,7 +466,7 @@ up_wakeups_poll_userspace_cb (UpWakeups *wakeups) gchar *data = NULL; gchar **lines = NULL; const gchar *string; - UpWakeupsObj *obj; + UpWakeupItem *item; GPtrArray *sections; guint pid; guint interrupts; @@ -472,9 +476,9 @@ up_wakeups_poll_userspace_cb (UpWakeups *wakeups) /* set all userspace data objs to zero */ for (i=0; i<wakeups->priv->data->len; i++) { - obj = g_ptr_array_index (wakeups->priv->data, i); - if (obj->is_userspace) - obj->value = 0.0f; + item = g_ptr_array_index (wakeups->priv->data, i); + if (up_wakeup_item_get_is_userspace (item)) + up_wakeup_item_set_value (item, 0.0f); } /* get the data */ @@ -533,27 +537,28 @@ up_wakeups_poll_userspace_cb (UpWakeups *wakeups) /* get details */ /* save in database */ - obj = up_wakeups_data_get_or_create (wakeups, pid); - if (obj->details == NULL) { + item = up_wakeups_data_get_or_create (wakeups, pid); + if (up_wakeup_item_get_details (item) == NULL) { /* get process name (truncated) */ string = g_ptr_array_index (sections, 2); if (strcmp (string, "insmod") == 0 || strcmp (string, "modprobe") == 0 || strcmp (string, "swapper") == 0) { - obj->cmdline = g_strdup (string); - obj->is_userspace = FALSE; + up_wakeup_item_set_cmdline (item, string); + up_wakeup_item_set_is_userspace (item, FALSE); } else { /* try to get a better command line */ - obj->cmdline = up_wakeups_get_cmdline (pid); - if (obj->cmdline == NULL || obj->cmdline[0] == '\0') - obj->cmdline = g_strdup (string); - obj->is_userspace = TRUE; + up_wakeup_item_set_cmdline (item, up_wakeups_get_cmdline (pid)); + if (up_wakeup_item_get_cmdline (item) == NULL || + up_wakeup_item_get_cmdline (item)[0] == '\0') + up_wakeup_item_set_cmdline (item, string); + up_wakeup_item_set_is_userspace (item, TRUE); } string = g_ptr_array_index (sections, 3); - obj->details = g_strdup (string); + up_wakeup_item_set_details (item, string); } /* we report this in minutes, not seconds */ - obj->value = (gfloat) interrupts / interval; + up_wakeup_item_set_value (item, (gfloat) interrupts / interval); skip: g_ptr_array_unref (sections); @@ -717,7 +722,7 @@ up_wakeups_init (UpWakeups *wakeups) GError *error = NULL; wakeups->priv = UP_WAKEUPS_GET_PRIVATE (wakeups); - wakeups->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) up_wakeups_obj_free); + wakeups->priv->data = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); wakeups->priv->total_old = 0; wakeups->priv->total_ave = 0; wakeups->priv->poll_userspace_id = 0; |