[gnome-applets] cpufreq: simplify CPUFreqSelector
- From: Alberts Muktupāvels <muktupavels src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-applets] cpufreq: simplify CPUFreqSelector
- Date: Tue, 6 Dec 2016 20:28:22 +0000 (UTC)
commit 56d0d6baf6041c89d618ebf0c1529020a53dc000
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date: Tue Dec 6 21:08:15 2016 +0200
cpufreq: simplify CPUFreqSelector
cpufreq/src/cpufreq-selector/Makefile.am | 1 -
.../cpufreq-selector/cpufreq-selector-libcpufreq.c | 193 ------------
.../cpufreq-selector/cpufreq-selector-libcpufreq.h | 60 ----
.../cpufreq-selector/cpufreq-selector-service.c | 4 +-
cpufreq/src/cpufreq-selector/cpufreq-selector.c | 329 +++++++++++++-------
cpufreq/src/cpufreq-selector/cpufreq-selector.h | 85 ++----
cpufreq/src/cpufreq-selector/main.c | 4 +-
7 files changed, 241 insertions(+), 435 deletions(-)
---
diff --git a/cpufreq/src/cpufreq-selector/Makefile.am b/cpufreq/src/cpufreq-selector/Makefile.am
index 3e5611a..bcec21a 100644
--- a/cpufreq/src/cpufreq-selector/Makefile.am
+++ b/cpufreq/src/cpufreq-selector/Makefile.am
@@ -27,7 +27,6 @@ endif
cpufreq_selector_SOURCES = \
cpufreq-selector.c cpufreq-selector.h \
- cpufreq-selector-libcpufreq.c cpufreq-selector-libcpufreq.h \
$(service_files) \
$(BUILT_SOURCES) \
main.c
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
index a20a436..7645063 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
@@ -20,9 +20,7 @@
#include <dbus/dbus-glib-lowlevel.h>
#include "cpufreq-selector.h"
-#include "cpufreq-selector-libcpufreq.h"
#include "cpufreq-selector-service.h"
-
#include "cpufreq-selector-service-glue.h"
#define MAX_CPUS 255
@@ -252,7 +250,7 @@ get_selector_for_cpu (CPUFreqSelectorService *service,
guint cpu)
{
if (!service->selectors[cpu]) {
- service->selectors[cpu] = cpufreq_selector_libcpufreq_new (cpu);
+ service->selectors[cpu] = cpufreq_selector_new (cpu);
if (!service->selectors[cpu])
return NULL;
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector.c b/cpufreq/src/cpufreq-selector/cpufreq-selector.c
index d7da183..68e3e3c 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector.c
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector.c
@@ -1,169 +1,258 @@
/*
- * GNOME CPUFreq Applet
- * Copyright (C) 2004 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright (C) 2004 Carlos Garcia Campos
+ * Copyright (C) 2016 Alberts Muktupāvels
*
- * This library 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 2 of the License, or (at your option) any later version.
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
*
- * This library 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.
+ * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * Authors : Carlos García Campos <carlosgc gnome org>
+ * Authors:
+ * Alberts Muktupāvels <alberts muktupavels gmail com>
+ * Carlos García Campos <carlosgc gnome org>
*/
-#include <glib.h>
+#include "config.h"
+
+#include <cpufreq.h>
+#include <stdlib.h>
+
#include "cpufreq-selector.h"
-#define CPUFREQ_SELECTOR_GET_PRIVATE(obj) \
- (G_TYPE_INSTANCE_GET_PRIVATE((obj), CPUFREQ_TYPE_SELECTOR, CPUFreqSelectorPrivate))
+typedef enum
+{
+ CPUFREQ_SELECTOR_ERROR_INVALID_CPU,
+ CPUFREQ_SELECTOR_ERROR_INVALID_GOVERNOR,
+ CPUFREQ_SELECTOR_ERROR_SET_FREQUENCY
+} CPUFreqSelectorError;
-enum {
- PROP_0,
- PROP_CPU
-};
+#define CPUFREQ_SELECTOR_ERROR cpufreq_selector_error_quark ()
-struct _CPUFreqSelectorPrivate {
- guint cpu;
+typedef struct cpufreq_policy CPUFreqPolicy;
+typedef struct cpufreq_available_frequencies CPUFreqFrequencyList;
+typedef struct cpufreq_available_governors CPUFreqGovernorList;
+
+struct _CPUFreqSelector
+{
+ GObject parent;
+
+ guint cpu;
};
-static void cpufreq_selector_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *spec);
-static void cpufreq_selector_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *spec);
+enum
+{
+ PROP_0,
+ PROP_CPU
+};
-G_DEFINE_ABSTRACT_TYPE (CPUFreqSelector, cpufreq_selector, G_TYPE_OBJECT)
+G_DEFINE_TYPE (CPUFreqSelector, cpufreq_selector, G_TYPE_OBJECT)
-GQuark
+static GQuark
cpufreq_selector_error_quark (void)
{
- static GQuark error_quark = 0;
+ static GQuark error_quark = 0;
- if (error_quark == 0)
- error_quark =
- g_quark_from_static_string ("cpufreq-selector-error-quark");
+ if (error_quark == 0)
+ error_quark = g_quark_from_static_string ("cpufreq-selector-error-quark");
- return error_quark;
+ return error_quark;
}
-static void
-cpufreq_selector_init (CPUFreqSelector *selector)
+static guint
+cpufreq_selector_get_valid_frequency (CPUFreqSelector *selector,
+ guint frequency)
+{
+ gint dist;
+ guint retval;
+ CPUFreqFrequencyList *freqs;
+ CPUFreqFrequencyList *freq;
+
+ dist = G_MAXINT;
+ retval = 0;
+
+ freqs = cpufreq_get_available_frequencies (selector->cpu);
+
+ if (!freqs)
+ return 0;
+
+ for (freq = freqs; freq; freq = freq->next)
+ {
+ gint current_dist;
+
+ if (freq->frequency == frequency)
+ {
+ cpufreq_put_available_frequencies (freqs);
+
+ return frequency;
+ }
+
+ current_dist = abs (freq->frequency - frequency);
+
+ if (current_dist < dist)
+ {
+ dist = current_dist;
+ retval = freq->frequency;
+ }
+ }
+
+ return retval;
+}
+
+static gboolean
+cpufreq_selector_validate_governor (CPUFreqSelector *selector,
+ const gchar *governor)
{
+ CPUFreqGovernorList *govs;
+ CPUFreqGovernorList *gov;
- selector->priv = CPUFREQ_SELECTOR_GET_PRIVATE (selector);
+ govs = cpufreq_get_available_governors (selector->cpu);
+ if (!govs)
+ return FALSE;
- selector->priv->cpu = 0;
+ for (gov = govs; gov; gov = gov->next)
+ {
+ if (g_ascii_strcasecmp (gov->governor, governor) == 0)
+ {
+ cpufreq_put_available_governors (govs);
+
+ return TRUE;
+ }
+ }
+
+ cpufreq_put_available_governors (govs);
+
+ return FALSE;
}
static void
-cpufreq_selector_class_init (CPUFreqSelectorClass *klass)
+cpufreq_selector_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *spec)
{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
- g_type_class_add_private (klass, sizeof (CPUFreqSelectorPrivate));
-
- object_class->set_property = cpufreq_selector_set_property;
- object_class->get_property = cpufreq_selector_get_property;
-
- /* Public virtual methods */
- klass->set_frequency = NULL;
- klass->set_governor = NULL;
-
- /* Porperties */
- g_object_class_install_property (object_class,
- PROP_CPU,
- g_param_spec_uint ("cpu",
- NULL,
- NULL,
- 0,
- G_MAXUINT,
- 0,
- G_PARAM_CONSTRUCT_ONLY |
- G_PARAM_READWRITE));
+ CPUFreqSelector *selector;
+
+ selector = CPUFREQ_SELECTOR (object);
+
+ switch (prop_id)
+ {
+ case PROP_CPU:
+ g_value_set_uint (value, selector->cpu);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
+ }
}
static void
cpufreq_selector_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *spec)
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *spec)
{
- CPUFreqSelector *selector = CPUFREQ_SELECTOR (object);
-
- switch (prop_id) {
- case PROP_CPU:
- selector->priv->cpu = g_value_get_uint (value);
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
- break;
- }
+ CPUFreqSelector *selector;
+
+ selector = CPUFREQ_SELECTOR (object);
+
+ switch (prop_id)
+ {
+ case PROP_CPU:
+ selector->cpu = g_value_get_uint (value);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
+ break;
+ }
}
static void
-cpufreq_selector_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *spec)
+cpufreq_selector_class_init (CPUFreqSelectorClass *selector_class)
{
- CPUFreqSelector *selector = CPUFREQ_SELECTOR (object);
-
- switch (prop_id) {
- case PROP_CPU:
- g_value_set_uint (value, selector->priv->cpu);
- break;
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
- break;
- }
+ GObjectClass *object_class;
+
+ object_class = G_OBJECT_CLASS (selector_class);
+
+ object_class->get_property = cpufreq_selector_get_property;
+ object_class->set_property = cpufreq_selector_set_property;
+
+ g_object_class_install_property (object_class, PROP_CPU,
+ g_param_spec_uint ("cpu", NULL, NULL,
+ 0, G_MAXUINT, 0,
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE));
}
-gboolean
-cpufreq_selector_set_frequency (CPUFreqSelector *selector,
- guint frequency,
- GError **error)
+static void
+cpufreq_selector_init (CPUFreqSelector *selector)
{
- CPUFreqSelectorClass *class;
-
- g_return_val_if_fail (CPUFREQ_IS_SELECTOR (selector), FALSE);
- g_return_val_if_fail (frequency > 0, FALSE);
-
- class = CPUFREQ_SELECTOR_GET_CLASS (selector);
-
- if (class->set_frequency) {
- return class->set_frequency (selector, frequency, error);
- }
-
- return FALSE;
+}
+
+CPUFreqSelector *
+cpufreq_selector_new (guint cpu)
+{
+ return g_object_new (CPUFREQ_TYPE_SELECTOR, "cpu", cpu, NULL);
}
gboolean
-cpufreq_selector_set_governor (CPUFreqSelector *selector,
- const gchar *governor,
- GError **error)
+cpufreq_selector_set_frequency (CPUFreqSelector *selector,
+ guint frequency,
+ GError **error)
{
- CPUFreqSelectorClass *class;
-
- g_return_val_if_fail (CPUFREQ_IS_SELECTOR (selector), FALSE);
- g_return_val_if_fail (governor != NULL, FALSE);
-
- class = CPUFREQ_SELECTOR_GET_CLASS (selector);
-
- if (class->set_governor) {
- return class->set_governor (selector, governor, error);
- }
+ guint freq;
+
+ g_return_val_if_fail (CPUFREQ_IS_SELECTOR (selector), FALSE);
+ g_return_val_if_fail (frequency > 0, FALSE);
+
+ freq = cpufreq_selector_get_valid_frequency (selector, frequency);
+
+ if (cpufreq_set_frequency (selector->cpu, freq) != 0)
+ {
+ g_set_error (error, CPUFREQ_SELECTOR_ERROR,
+ CPUFREQ_SELECTOR_ERROR_SET_FREQUENCY,
+ "Cannot set frequency '%d'", frequency);
+
+ return FALSE;
+ }
- return FALSE;
+ return TRUE;
}
+gboolean
+cpufreq_selector_set_governor (CPUFreqSelector *selector,
+ const gchar *governor,
+ GError **error)
+{
+ g_return_val_if_fail (CPUFREQ_IS_SELECTOR (selector), FALSE);
+ g_return_val_if_fail (governor != NULL, FALSE);
+
+ if (!cpufreq_selector_validate_governor (selector, governor))
+ {
+ g_set_error (error, CPUFREQ_SELECTOR_ERROR,
+ CPUFREQ_SELECTOR_ERROR_INVALID_GOVERNOR,
+ "Invalid governor '%s'", governor);
+
+ return FALSE;
+ }
+ if (cpufreq_modify_policy_governor (selector->cpu, (gchar *) governor) != 0)
+ {
+ g_set_error (error, CPUFREQ_SELECTOR_ERROR,
+ CPUFREQ_SELECTOR_ERROR_INVALID_GOVERNOR,
+ "Invalid governor '%s'", governor);
+
+ return FALSE;
+ }
+
+ return FALSE;
+}
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector.h b/cpufreq/src/cpufreq-selector/cpufreq-selector.h
index 1b49852..f9d6e4e 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector.h
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector.h
@@ -1,73 +1,46 @@
/*
- * GNOME CPUFreq Applet
- * Copyright (C) 2004 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright (C) 2004 Carlos Garcia Campos
+ * Copyright (C) 2016 Alberts Muktupāvels
*
- * This library 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 2 of the License, or (at your option) any later version.
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
*
- * This library 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.
+ * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * Authors : Carlos García Campos <carlosgc gnome org>
+ * Authors:
+ * Alberts Muktupāvels <alberts muktupavels gmail com>
+ * Carlos García Campos <carlosgc gnome org>
*/
-#ifndef __CPUFREQ_SELECTOR_H__
-#define __CPUFREQ_SELECTOR_H__
+#ifndef CPUFREQ_SELECTOR_H
+#define CPUFREQ_SELECTOR_H
#include <glib-object.h>
-#define CPUFREQ_TYPE_SELECTOR (cpufreq_selector_get_type ())
-#define CPUFREQ_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CPUFREQ_TYPE_SELECTOR,
CPUFreqSelector))
-#define CPUFREQ_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), CPUFREQ_TYPE_SELECTOR,
CPUFreqSelectorClass))
-#define CPUFREQ_IS_SELECTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CPUFREQ_TYPE_SELECTOR))
-#define CPUFREQ_IS_SELECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CPUFREQ_TYPE_SELECTOR))
-#define CPUFREQ_SELECTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CPUFREQ_TYPE_SELECTOR,
CPUFreqSelectorClass))
+G_BEGIN_DECLS
-#define CPUFREQ_SELECTOR_ERROR (cpufreq_selector_error_quark ())
+#define CPUFREQ_TYPE_SELECTOR cpufreq_selector_get_type ()
+G_DECLARE_FINAL_TYPE (CPUFreqSelector, cpufreq_selector,
+ CPUFREQ, SELECTOR, GObject)
-enum {
- SELECTOR_ERROR_INVALID_CPU,
- SELECTOR_ERROR_INVALID_GOVERNOR,
- SELECTOR_ERROR_SET_FREQUENCY
-};
+CPUFreqSelector *cpufreq_selector_new (guint cpu);
-typedef struct _CPUFreqSelector CPUFreqSelector;
-typedef struct _CPUFreqSelectorClass CPUFreqSelectorClass;
-typedef struct _CPUFreqSelectorPrivate CPUFreqSelectorPrivate;
+gboolean cpufreq_selector_set_frequency (CPUFreqSelector *selector,
+ guint frequency,
+ GError **error);
-struct _CPUFreqSelector {
- GObject parent;
+gboolean cpufreq_selector_set_governor (CPUFreqSelector *selector,
+ const gchar *governor,
+ GError **error);
- CPUFreqSelectorPrivate *priv;
-};
+G_END_DECLS
-struct _CPUFreqSelectorClass {
- GObjectClass parent_class;
-
- gboolean (* set_frequency) (CPUFreqSelector *selector,
- guint frequency,
- GError **error);
- gboolean (* set_governor) (CPUFreqSelector *selector,
- const gchar *governor,
- GError **error);
-};
-
-
-GType cpufreq_selector_get_type (void) G_GNUC_CONST;
-GQuark cpufreq_selector_error_quark (void) G_GNUC_CONST;
-
-gboolean cpufreq_selector_set_frequency (CPUFreqSelector *selector,
- guint frequency,
- GError **error);
-gboolean cpufreq_selector_set_governor (CPUFreqSelector *selector,
- const gchar *governor,
- GError **error);
-
-#endif /* __CPUFREQ_SELECTOR_H__ */
+#endif
diff --git a/cpufreq/src/cpufreq-selector/main.c b/cpufreq/src/cpufreq-selector/main.c
index 8c1d0d0..5e47b16 100644
--- a/cpufreq/src/cpufreq-selector/main.c
+++ b/cpufreq/src/cpufreq-selector/main.c
@@ -28,7 +28,7 @@
#ifdef HAVE_POLKIT
#include "cpufreq-selector-service.h"
#endif
-#include "cpufreq-selector-libcpufreq.h"
+#include "cpufreq-selector.h"
static gint cpu = 0;
static gchar *governor = NULL;
@@ -127,7 +127,7 @@ cpufreq_selector_set_values (void)
CPUFreqSelector *selector;
GError *error = NULL;
- selector = cpufreq_selector_libcpufreq_new (cpu);
+ selector = cpufreq_selector_new (cpu);
if (!selector) {
g_printerr ("No cpufreq support\n");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]