File: is-dynamic-plugin.c

package info (click to toggle)
indicator-sensors 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: ansic: 8,898; sh: 571; makefile: 394; xml: 41
file content (399 lines) | stat: -rw-r--r-- 11,462 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (C) 2014 Alex Murray <murray.alex@gmail.com>
 *
 * indicator-sensors 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 3 of the License, or
 * (at your option) any later version.
 *
 * indicator-sensors 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with indicator-sensors.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "is-dynamic-plugin.h"
#include <stdlib.h>
#include <math.h>
#include <inttypes.h>
#include <indicator-sensors/is-application.h>
#include <indicator-sensors/is-manager.h>
#include <indicator-sensors/is-log.h>
#include <glib/gi18n.h>

static void peas_activatable_iface_init(PeasActivatableInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED(IsDynamicPlugin,
                               is_dynamic_plugin,
                               PEAS_TYPE_EXTENSION_BASE,
                               0,
                               G_IMPLEMENT_INTERFACE_DYNAMIC(PEAS_TYPE_ACTIVATABLE,
                                                             peas_activatable_iface_init));

#define DYNAMIC_RATE_DATA_KEY "dynamic-rate-data"

#define DYNAMIC_SENSOR_PATH "virtual/dynamic"

#define EWMA_ALPHA 0.2

enum
{
  PROP_OBJECT = 1,
};

struct _IsDynamicPluginPrivate
{
  IsApplication *application;
  IsSensor *sensor;
  IsSensor *max;
  gdouble max_rate;
};

typedef struct _RateData
{
  gdouble rate;
  gdouble last_value;
  gint64 last_time;
} RateData;

static void is_dynamic_plugin_finalize(GObject *object);

static void
is_dynamic_plugin_set_property(GObject *object,
                               guint prop_id,
                               const GValue *value,
                               GParamSpec *pspec)
{
  IsDynamicPlugin *plugin = IS_DYNAMIC_PLUGIN(object);

  switch (prop_id)
  {
    case PROP_OBJECT:
      plugin->priv->application = IS_APPLICATION(g_value_dup_object(value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
      break;
  }
}

static void
is_dynamic_plugin_get_property(GObject *object,
                               guint prop_id,
                               GValue *value,
                               GParamSpec *pspec)
{
  IsDynamicPlugin *plugin = IS_DYNAMIC_PLUGIN(object);

  switch (prop_id)
  {
    case PROP_OBJECT:
      g_value_set_object(value, plugin->priv->application);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
      break;
  }
}

static void
is_dynamic_plugin_init(IsDynamicPlugin *self)
{
  IsDynamicPluginPrivate *priv =
    G_TYPE_INSTANCE_GET_PRIVATE(self, IS_TYPE_DYNAMIC_PLUGIN,
                                IsDynamicPluginPrivate);

  self->priv = priv;
}

static void
is_dynamic_plugin_finalize(GObject *object)
{
  IsDynamicPlugin *self = (IsDynamicPlugin *)object;
  IsDynamicPluginPrivate *priv = self->priv;

  (void)priv;

  G_OBJECT_CLASS(is_dynamic_plugin_parent_class)->finalize(object);
}

static void
update_sensor_from_max(IsDynamicPlugin *self)
{
  IsDynamicPluginPrivate *priv;
  gchar *label;

  priv = self->priv;

  label = g_strdup_printf("Δ%s", is_sensor_get_label(priv->max));
  is_sensor_set_label(priv->sensor, label);
  is_sensor_set_icon(priv->sensor, is_sensor_get_icon(priv->max));
  is_sensor_set_value(priv->sensor, is_sensor_get_value(priv->max));
  is_sensor_set_units(priv->sensor, is_sensor_get_units(priv->max));
  is_sensor_set_digits(priv->sensor, is_sensor_get_digits(priv->max));
  g_free(label);
}

static void
on_sensor_value_notify(IsSensor *sensor,
                       GParamSpec *pspec,
                       gpointer user_data)
{
  IsDynamicPlugin *self;
  IsDynamicPluginPrivate *priv;
  RateData *data;
  gdouble value, dv, dt, rate;
  gint64 now;

  self = IS_DYNAMIC_PLUGIN(user_data);
  priv = self->priv;

  value = is_sensor_get_value(sensor);

  if (value - IS_SENSOR_VALUE_UNSET <= DBL_EPSILON)
  {
    is_debug("dynamic", "sensor value for sensor %s is unset - ignoring",
             is_sensor_get_label(sensor));
    goto exit;
  }

  now = g_get_monotonic_time();

  data = g_object_get_data(G_OBJECT(sensor), DYNAMIC_RATE_DATA_KEY);
  if (data == NULL)
  {
    is_debug("dynamic", "Creating new dynamic rate data for sensor: %s",
             is_sensor_get_label(sensor));

    // allocate data
    data = g_malloc0(sizeof(*data));
    data->rate = 0.0f;
    data->last_value = value;
    data->last_time = now;
    g_object_set_data_full(G_OBJECT(sensor), DYNAMIC_RATE_DATA_KEY,
                           data, g_free);
    goto exit;
  }

  is_debug("dynamic", "Got existing rate data for sensor: %s - rate: %f, last_value %f, last_time %"PRId64"",
           is_sensor_get_label(sensor),
           data->rate,
           data->last_value,
           data->last_time);
  dv = value - data->last_value;
  dt = ((double)(now - data->last_time) /
        (double)G_USEC_PER_SEC);

  // convert rate to units per second
  rate = fabs(dv / dt);
  is_debug("dynamic", "abs rate of change of sensor %s: %f (t0: %f, t-1: %f, dv: %f, dt: %f)",
           is_sensor_get_label(sensor), rate, value, data->last_value,
           dv, dt);

  // calculate exponentially weighted moving average of rate
  rate = (EWMA_ALPHA * rate) + ((1 - EWMA_ALPHA) * data->rate);
  data->rate = rate;
  data->last_value = value;
  data->last_time = now;
  is_debug("dynamic", "EWMA abs rate of change of sensor %s: %f",
           is_sensor_get_label(sensor), rate);

  if (rate > priv->max_rate && sensor != priv->max)
  {
    // let's see if we can get away without taking a reference on sensor
    priv->max = sensor;

    is_message("dynamic", "New highest EWMA rate sensor: %s (rate %f)",
               is_sensor_get_label(sensor), rate);
  }

  if (sensor == priv->max)
  {
    priv->max_rate = rate;

    update_sensor_from_max(self);
  }

exit:
  return;
}

static void
on_sensor_enabled(IsManager *manager,
                  IsSensor *sensor,
                  gint index,
                  gpointer data)
{
  IsDynamicPlugin *self = (IsDynamicPlugin *)data;

  // don't bother monitoring non-temperature sensors
  if (IS_IS_TEMPERATURE_SENSOR(sensor))
  {
    is_debug("dynamic", "sensor enabled: %s", is_sensor_get_label(sensor));
    on_sensor_value_notify(sensor, NULL, self);
    g_signal_connect(sensor, "notify::value",
                     G_CALLBACK(on_sensor_value_notify), self);
  }
}

static void
on_sensor_disabled(IsManager *manager,
                   IsSensor *sensor,
                   gpointer data)
{
  IsDynamicPlugin *self = (IsDynamicPlugin *)data;
  IsDynamicPluginPrivate *priv = self->priv;

  // don't bother monitoring non-temperature sensors
  if (IS_IS_TEMPERATURE_SENSOR(sensor))
  {
    is_debug("dynamic", "sensor disabled: %s", is_sensor_get_label(sensor));
    g_signal_handlers_disconnect_by_func(sensor,
                                         G_CALLBACK(on_sensor_value_notify),
                                         self);
    if (priv->max == sensor)
    {
      // get all sensors and find the one with the maximum rate and switch to
      // this
      GSList *sensors, *_list;

      priv->max = NULL;
      priv->max_rate = 0.0;

      is_sensor_set_label(priv->sensor, "Δ");
      is_sensor_set_icon(priv->sensor, IS_STOCK_CHIP);
      is_sensor_set_value(priv->sensor, 0.0);
      is_sensor_set_units(priv->sensor, "");
      is_sensor_set_digits(priv->sensor, 1);

      sensors = is_manager_get_enabled_sensors_list(manager);
      for (_list = sensors;
           _list != NULL;
           _list = _list->next)
      {
        if (IS_IS_TEMPERATURE_SENSOR(_list->data))
        {
          on_sensor_value_notify(IS_SENSOR(_list->data), NULL, self);
        }
      }
    }

  }
}

static void
is_dynamic_plugin_activate(PeasActivatable *activatable)
{
  IsDynamicPlugin *self = IS_DYNAMIC_PLUGIN(activatable);
  IsDynamicPluginPrivate *priv = self->priv;
  IsManager *manager;
  GSList *sensors, *_list;
  int i = 0;

  manager = is_application_get_manager(priv->application);

  // create our virtual sensor which mimics the current highest rate of change
  // sensor's value and label
  is_debug("dynamic", "creating virtual sensor");
  priv->sensor = is_sensor_new(DYNAMIC_SENSOR_PATH);
  is_sensor_set_label(priv->sensor, "Δ");
  is_sensor_set_icon(priv->sensor, IS_STOCK_CHIP);
  is_sensor_set_value(priv->sensor, 0.0);
  is_sensor_set_units(priv->sensor, "");
  is_sensor_set_digits(priv->sensor, 1);
  is_manager_add_sensor(manager, priv->sensor);

  is_debug("dynamic", "attaching to signals");
  sensors = is_manager_get_enabled_sensors_list(manager);
  for (_list = sensors;
       _list != NULL;
       _list = _list->next)
  {
    IsSensor *sensor = IS_SENSOR(_list->data);
    on_sensor_enabled(manager, sensor, i, self);
    g_object_unref(sensor);
    i++;
  }
  g_slist_free(sensors);
  g_signal_connect(manager, "sensor-enabled",
                   G_CALLBACK(on_sensor_enabled), self);
  g_signal_connect(manager, "sensor-disabled",
                   G_CALLBACK(on_sensor_disabled), self);

}

static void
is_dynamic_plugin_deactivate(PeasActivatable *activatable)
{
  IsDynamicPlugin *self = IS_DYNAMIC_PLUGIN(activatable);
  IsDynamicPluginPrivate *priv = self->priv;
  IsManager *manager;
  GSList *sensors, *_list;

  is_debug("dynamic", "dettaching from signals");

  manager = is_application_get_manager(priv->application);

  is_manager_remove_path(manager, DYNAMIC_SENSOR_PATH);
  sensors = is_manager_get_enabled_sensors_list(manager);
  for (_list = sensors;
       _list != NULL;
       _list = _list->next)
  {
    IsSensor *sensor = IS_SENSOR(_list->data);
    on_sensor_disabled(manager, sensor, self);
    g_object_unref(sensor);
  }
  g_slist_free(sensors);
  g_signal_handlers_disconnect_by_func(manager,
                                       G_CALLBACK(on_sensor_enabled), self);
  g_signal_handlers_disconnect_by_func(manager,
                                       G_CALLBACK(on_sensor_disabled), self);

}

static void
is_dynamic_plugin_class_init(IsDynamicPluginClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS(klass);

  g_type_class_add_private(klass, sizeof(IsDynamicPluginPrivate));

  gobject_class->get_property = is_dynamic_plugin_get_property;
  gobject_class->set_property = is_dynamic_plugin_set_property;
  gobject_class->finalize = is_dynamic_plugin_finalize;

  g_object_class_override_property(gobject_class, PROP_OBJECT, "object");
}

static void
peas_activatable_iface_init(PeasActivatableInterface *iface)
{
  iface->activate = is_dynamic_plugin_activate;
  iface->deactivate = is_dynamic_plugin_deactivate;
}

static void
is_dynamic_plugin_class_finalize(IsDynamicPluginClass *klass)
{
  /* nothing to do */
}

G_MODULE_EXPORT void
peas_register_types(PeasObjectModule *module)
{
  is_dynamic_plugin_register_type(G_TYPE_MODULE(module));

  peas_object_module_register_extension_type(module,
                                             PEAS_TYPE_ACTIVATABLE,
                                             IS_TYPE_DYNAMIC_PLUGIN);
}