~ubuntu-branches/ubuntu/vivid/mate-applets/vivid-proposed

« back to all changes in this revision

Viewing changes to cpufreq/src/cpufreq-monitor.c

  • Committer: Package Import Robot
  • Author(s): Mike Gabriel
  • Date: 2014-04-21 13:57:07 UTC
  • Revision ID: package-import@ubuntu.com-20140421135707-hh4arlnzglq9xxfo
Tags: upstream-1.8.0+dfsg1
ImportĀ upstreamĀ versionĀ 1.8.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * MATE CPUFreq Applet
 
3
 * Copyright (C) 2004 Carlos Garcia Campos <carlosgc@gnome.org>
 
4
 *
 
5
 *  This library is free software; you can redistribute it and/or
 
6
 *  modify it under the terms of the GNU General Public
 
7
 *  License as published by the Free Software Foundation; either
 
8
 *  version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 *  This library is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 *  General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public
 
16
 *  License along with this library; if not, write to the Free
 
17
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 *
 
19
 * Authors : Carlos Garcļæ½a Campos <carlosgc@gnome.org>
 
20
 */
 
21
 
 
22
#include "cpufreq-monitor.h"
 
23
 
 
24
#define CPUFREQ_MONITOR_GET_PRIVATE(obj) \
 
25
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), CPUFREQ_TYPE_MONITOR, CPUFreqMonitorPrivate))
 
26
 
 
27
#define CPUFREQ_MONITOR_INTERVAL 1
 
28
 
 
29
/* Properties */
 
30
enum {
 
31
        PROP_0,
 
32
        PROP_CPU,
 
33
        PROP_ONLINE,
 
34
        PROP_FREQUENCY,
 
35
        PROP_MAX_FREQUENCY,
 
36
        PROP_GOVERNOR
 
37
};
 
38
 
 
39
/* Signals */
 
40
enum {
 
41
        SIGNAL_CHANGED,
 
42
        N_SIGNALS
 
43
};
 
44
 
 
45
struct _CPUFreqMonitorPrivate {
 
46
        guint    cpu;
 
47
        gboolean online;
 
48
        gint     cur_freq;
 
49
        gint     max_freq;
 
50
        gchar   *governor;
 
51
        GList   *available_freqs;
 
52
        GList   *available_govs;
 
53
        guint    timeout_handler;
 
54
 
 
55
        gboolean changed;
 
56
};
 
57
 
 
58
static void   cpufreq_monitor_init         (CPUFreqMonitor      *monitor);
 
59
static void   cpufreq_monitor_class_init   (CPUFreqMonitorClass *klass);
 
60
static void   cpufreq_monitor_finalize     (GObject             *object);
 
61
 
 
62
static void   cpufreq_monitor_set_property (GObject             *object,
 
63
                                            guint                prop_id,
 
64
                                            const GValue        *value,
 
65
                                            GParamSpec          *spec);
 
66
static void   cpufreq_monitor_get_property (GObject             *object,
 
67
                                            guint                prop_id,
 
68
                                            GValue              *value,
 
69
                                            GParamSpec          *spec);
 
70
 
 
71
static guint signals[N_SIGNALS];
 
72
 
 
73
G_DEFINE_ABSTRACT_TYPE (CPUFreqMonitor, cpufreq_monitor, G_TYPE_OBJECT)
 
74
 
 
75
static void
 
76
cpufreq_monitor_init (CPUFreqMonitor *monitor)
 
77
{
 
78
        monitor->priv = CPUFREQ_MONITOR_GET_PRIVATE (monitor);
 
79
 
 
80
        monitor->priv->governor = NULL;
 
81
        monitor->priv->available_freqs = NULL;
 
82
        monitor->priv->available_govs = NULL;
 
83
        monitor->priv->timeout_handler = 0;
 
84
 
 
85
        monitor->priv->changed = FALSE;
 
86
}
 
87
 
 
88
static void
 
89
cpufreq_monitor_class_init (CPUFreqMonitorClass *klass)
 
90
{
 
91
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
92
 
 
93
        object_class->set_property = cpufreq_monitor_set_property;
 
94
        object_class->get_property = cpufreq_monitor_get_property;
 
95
 
 
96
        /* Public virtual methods */
 
97
        klass->run = NULL;
 
98
        klass->get_available_frequencies = NULL;
 
99
        klass->get_available_governors = NULL;
 
100
 
 
101
        g_type_class_add_private (klass, sizeof (CPUFreqMonitorPrivate));
 
102
        
 
103
        /* Porperties */
 
104
        g_object_class_install_property (object_class,
 
105
                                         PROP_CPU,
 
106
                                         g_param_spec_uint ("cpu",
 
107
                                                            "CPU",
 
108
                                                            "The cpu to monitor",
 
109
                                                            0, 
 
110
                                                            G_MAXUINT,
 
111
                                                            0, 
 
112
                                                            G_PARAM_CONSTRUCT |
 
113
                                                            G_PARAM_READWRITE));
 
114
        g_object_class_install_property (object_class,
 
115
                                         PROP_ONLINE,
 
116
                                         g_param_spec_boolean ("online",
 
117
                                                               "Online",
 
118
                                                               "Whether cpu is online",
 
119
                                                               TRUE,
 
120
                                                               G_PARAM_READWRITE));
 
121
        g_object_class_install_property (object_class,
 
122
                                         PROP_FREQUENCY,
 
123
                                         g_param_spec_int ("frequency",
 
124
                                                           "Frequency",
 
125
                                                           "The current cpu frequency",
 
126
                                                           0, 
 
127
                                                           G_MAXINT,
 
128
                                                           0,
 
129
                                                           G_PARAM_READWRITE));
 
130
        g_object_class_install_property (object_class,
 
131
                                         PROP_MAX_FREQUENCY,
 
132
                                         g_param_spec_int ("max-frequency",
 
133
                                                           "MaxFrequency",
 
134
                                                           "The max cpu frequency",
 
135
                                                           0,
 
136
                                                           G_MAXINT,
 
137
                                                           0,
 
138
                                                           G_PARAM_READWRITE));
 
139
        g_object_class_install_property (object_class,
 
140
                                         PROP_GOVERNOR,
 
141
                                         g_param_spec_string ("governor",
 
142
                                                              "Governor",
 
143
                                                              "The current cpufreq governor",
 
144
                                                              NULL,
 
145
                                                              G_PARAM_READWRITE));
 
146
 
 
147
        /* Signals */
 
148
        signals[SIGNAL_CHANGED] =
 
149
                g_signal_new ("changed",
 
150
                              G_TYPE_FROM_CLASS (klass),
 
151
                              G_SIGNAL_RUN_LAST,
 
152
                              G_STRUCT_OFFSET (CPUFreqMonitorClass, changed),
 
153
                              NULL, NULL,
 
154
                              g_cclosure_marshal_VOID__VOID,
 
155
                              G_TYPE_NONE, 0);
 
156
           
 
157
        object_class->finalize = cpufreq_monitor_finalize;
 
158
}
 
159
 
 
160
static void
 
161
cpufreq_monitor_finalize (GObject *object)
 
162
{
 
163
        CPUFreqMonitor *monitor = CPUFREQ_MONITOR (object);
 
164
 
 
165
        monitor->priv->online = FALSE;
 
166
        
 
167
        if (monitor->priv->timeout_handler > 0) {
 
168
                g_source_remove (monitor->priv->timeout_handler);
 
169
                monitor->priv->timeout_handler = 0;
 
170
        }
 
171
 
 
172
        if (monitor->priv->governor) {
 
173
                g_free (monitor->priv->governor);
 
174
                monitor->priv->governor = NULL;
 
175
        }
 
176
 
 
177
        if (monitor->priv->available_freqs) {
 
178
                g_list_foreach (monitor->priv->available_freqs,
 
179
                                (GFunc) g_free,
 
180
                                NULL);
 
181
                g_list_free (monitor->priv->available_freqs);
 
182
                monitor->priv->available_freqs = NULL;
 
183
        }
 
184
 
 
185
        if (monitor->priv->available_govs) {
 
186
                g_list_foreach (monitor->priv->available_govs,
 
187
                                (GFunc) g_free,
 
188
                                NULL);
 
189
                g_list_free (monitor->priv->available_govs);
 
190
                monitor->priv->available_govs = NULL;
 
191
        }
 
192
 
 
193
        G_OBJECT_CLASS (cpufreq_monitor_parent_class)->finalize (object);
 
194
}
 
195
 
 
196
static void
 
197
cpufreq_monitor_set_property (GObject      *object,
 
198
                              guint         prop_id,
 
199
                              const GValue *value,
 
200
                              GParamSpec   *spec)
 
201
{
 
202
        CPUFreqMonitor *monitor;
 
203
 
 
204
        monitor = CPUFREQ_MONITOR (object);
 
205
 
 
206
        switch (prop_id) {
 
207
        case PROP_CPU: {
 
208
                guint cpu = g_value_get_uint (value);
 
209
 
 
210
                if (cpu != monitor->priv->cpu) {
 
211
                        monitor->priv->cpu = cpu;
 
212
                        monitor->priv->changed = TRUE;
 
213
                }
 
214
        }
 
215
                break;
 
216
        case PROP_ONLINE:
 
217
                monitor->priv->online = g_value_get_boolean (value);
 
218
 
 
219
                break;
 
220
        case PROP_FREQUENCY: {
 
221
                gint freq = g_value_get_int (value);
 
222
 
 
223
                if (freq != monitor->priv->cur_freq) {
 
224
                        monitor->priv->cur_freq = freq;
 
225
                        monitor->priv->changed = TRUE;
 
226
                }
 
227
        }
 
228
                break;
 
229
        case PROP_MAX_FREQUENCY: {
 
230
                gint freq = g_value_get_int (value);
 
231
 
 
232
                if (freq != monitor->priv->max_freq) {
 
233
                        monitor->priv->max_freq = freq;
 
234
                        monitor->priv->changed = TRUE;
 
235
                }
 
236
        }
 
237
                break;
 
238
        case PROP_GOVERNOR: {
 
239
                const gchar *gov = g_value_get_string (value);
 
240
 
 
241
                if (monitor->priv->governor) {
 
242
                        if (g_ascii_strcasecmp (gov, monitor->priv->governor) != 0) {
 
243
                                g_free (monitor->priv->governor);
 
244
                                monitor->priv->governor = gov ? g_strdup (gov) : NULL;
 
245
                                monitor->priv->changed = TRUE;
 
246
                        }
 
247
                } else {
 
248
                        monitor->priv->governor = gov ? g_strdup (gov) : NULL;
 
249
                        monitor->priv->changed = TRUE;
 
250
                }
 
251
        }
 
252
                break;
 
253
        default:
 
254
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
 
255
                break;
 
256
        }
 
257
}
 
258
 
 
259
static void
 
260
cpufreq_monitor_get_property (GObject    *object,
 
261
                              guint       prop_id,
 
262
                              GValue     *value,
 
263
                              GParamSpec *spec)
 
264
{
 
265
        CPUFreqMonitor *monitor;
 
266
 
 
267
        monitor = CPUFREQ_MONITOR (object);
 
268
 
 
269
        switch (prop_id) {
 
270
        case PROP_CPU:
 
271
                g_value_set_uint (value, monitor->priv->cpu);
 
272
                break;
 
273
        case PROP_ONLINE:
 
274
                g_value_set_boolean (value, monitor->priv->online);
 
275
                break;
 
276
        case PROP_FREQUENCY:
 
277
                g_value_set_int (value, monitor->priv->cur_freq);
 
278
                break;
 
279
        case PROP_MAX_FREQUENCY:
 
280
                g_value_set_int (value, monitor->priv->max_freq);
 
281
                break;
 
282
        case PROP_GOVERNOR:
 
283
                g_value_set_string (value, monitor->priv->governor);
 
284
                break;
 
285
        default:
 
286
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
 
287
                break;
 
288
        }
 
289
}
 
290
 
 
291
static gboolean
 
292
cpufreq_monitor_run_cb (CPUFreqMonitor *monitor)
 
293
{
 
294
        CPUFreqMonitorClass *class;
 
295
        gboolean             retval = FALSE;
 
296
 
 
297
        class = CPUFREQ_MONITOR_GET_CLASS (monitor);
 
298
        
 
299
        if (class->run)
 
300
                retval = class->run (monitor);
 
301
 
 
302
        if (monitor->priv->changed) {
 
303
                g_signal_emit (monitor, signals[SIGNAL_CHANGED], 0);
 
304
                monitor->priv->changed = FALSE;
 
305
        }
 
306
 
 
307
        return retval;
 
308
}
 
309
 
 
310
void
 
311
cpufreq_monitor_run (CPUFreqMonitor *monitor)
 
312
{
 
313
        g_return_if_fail (CPUFREQ_IS_MONITOR (monitor));
 
314
 
 
315
        if (monitor->priv->timeout_handler > 0)
 
316
                return;
 
317
 
 
318
        monitor->priv->timeout_handler =
 
319
                g_timeout_add_seconds (CPUFREQ_MONITOR_INTERVAL,
 
320
                               (GSourceFunc) cpufreq_monitor_run_cb,
 
321
                               (gpointer) monitor);
 
322
}
 
323
 
 
324
GList *
 
325
cpufreq_monitor_get_available_frequencies (CPUFreqMonitor *monitor)
 
326
{
 
327
        CPUFreqMonitorClass *class;
 
328
        
 
329
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
 
330
 
 
331
        if (!monitor->priv->online)
 
332
                return NULL;
 
333
        
 
334
        if (monitor->priv->available_freqs)
 
335
                return monitor->priv->available_freqs;
 
336
 
 
337
        class = CPUFREQ_MONITOR_GET_CLASS (monitor);
 
338
        
 
339
        if (class->get_available_frequencies) {
 
340
                monitor->priv->available_freqs = class->get_available_frequencies (monitor);
 
341
        }
 
342
 
 
343
        return monitor->priv->available_freqs;
 
344
}
 
345
 
 
346
GList *
 
347
cpufreq_monitor_get_available_governors (CPUFreqMonitor *monitor)
 
348
{
 
349
        CPUFreqMonitorClass *class;
 
350
        
 
351
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
 
352
 
 
353
        if (!monitor->priv->online)
 
354
                return NULL;
 
355
        
 
356
        if (monitor->priv->available_govs)
 
357
                return monitor->priv->available_govs;
 
358
 
 
359
        class = CPUFREQ_MONITOR_GET_CLASS (monitor);
 
360
        
 
361
        if (class->get_available_governors) {
 
362
                monitor->priv->available_govs = class->get_available_governors (monitor);
 
363
        }
 
364
 
 
365
        return monitor->priv->available_govs;
 
366
}
 
367
 
 
368
guint
 
369
cpufreq_monitor_get_cpu (CPUFreqMonitor *monitor)
 
370
{
 
371
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), 0);
 
372
 
 
373
        return monitor->priv->cpu;
 
374
}
 
375
 
 
376
void
 
377
cpufreq_monitor_set_cpu (CPUFreqMonitor *monitor, guint cpu)
 
378
{
 
379
        g_return_if_fail (CPUFREQ_IS_MONITOR (monitor));
 
380
 
 
381
        g_object_set (G_OBJECT (monitor),
 
382
                      "cpu", cpu, NULL);
 
383
}
 
384
 
 
385
gint
 
386
cpufreq_monitor_get_frequency (CPUFreqMonitor *monitor)
 
387
{
 
388
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), -1);
 
389
 
 
390
        return monitor->priv->cur_freq;
 
391
}
 
392
 
 
393
const gchar *
 
394
cpufreq_monitor_get_governor (CPUFreqMonitor *monitor)
 
395
{
 
396
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), NULL);
 
397
 
 
398
        return monitor->priv->governor;
 
399
}
 
400
 
 
401
gint
 
402
cpufreq_monitor_get_percentage (CPUFreqMonitor *monitor)
 
403
{
 
404
        g_return_val_if_fail (CPUFREQ_IS_MONITOR (monitor), -1);
 
405
 
 
406
        if (monitor->priv->max_freq > 0) {
 
407
                return ((monitor->priv->cur_freq * 100) / monitor->priv->max_freq);
 
408
        }
 
409
 
 
410
        return -1;
 
411
}