[gnome-applets] cpufreq: port to GDBus



commit b24ea7511461bc9ab1f97ff77c45963364cb44db
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sun Mar 8 23:32:52 2020 +0200

    cpufreq: port to GDBus
    
    This also removes ability to use cpufreq-selector as standalone
    application to change cpu frequency and/or governor.
    
    https://gitlab.gnome.org/GNOME/gnome-applets/issues/2

 configure.ac                                       |   4 +-
 cpufreq/src/cpufreq-selector/Makefile.am           |  15 +-
 .../cpufreq-selector/cpufreq-selector-service.c    | 792 ++++++++++-----------
 .../cpufreq-selector/cpufreq-selector-service.h    |  68 +-
 .../cpufreq-selector/cpufreq-selector-service.xml  |  23 -
 cpufreq/src/cpufreq-selector/main.c                | 208 +-----
 6 files changed, 432 insertions(+), 678 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index bb29e2d93..e304c9224 100644
--- a/configure.ac
+++ b/configure.ac
@@ -67,7 +67,6 @@ LIBGTOP_REQUIRED=2.11.92
 LIBWNCK_REQUIRED=3.14.1
 LIBNOTIFY_REQUIRED=0.7
 UPOWER_REQUIRED=0.99.8
-DBUS_REQUIRED=1.1.2
 DBUS_GLIB_REQUIRED=0.74
 ADWAITA_ICON_THEME_REQUIRED=3.14.0
 LIBXML_REQUIRED=2.5.0
@@ -164,8 +163,7 @@ AC_SUBST(LIBNOTIFY_CFLAGS)
 AC_SUBST(LIBNOTIFY_LIBS)
 
 PKG_CHECK_MODULES([CPUFREQ_SELECTOR], [
-  dbus-1 >= $DBUS_REQUIRED
-  dbus-glib-1 >= $DBUS_GLIB_REQUIRED
+  gio-unix-2.0 >= $GIO_REQUIRED)
   glib-2.0 >= $GLIB_REQUIRED
   gobject-2.0 >= $GLIB_REQUIRED
   polkit-gobject-1 >= $POLKIT_REQUIRED
diff --git a/cpufreq/src/cpufreq-selector/Makefile.am b/cpufreq/src/cpufreq-selector/Makefile.am
index 369c39292..b32055a0a 100644
--- a/cpufreq/src/cpufreq-selector/Makefile.am
+++ b/cpufreq/src/cpufreq-selector/Makefile.am
@@ -37,12 +37,16 @@ dbus_servicesdir = $(datadir)/dbus-1/system-services
 dbus_confdir = $(sysconfdir)/dbus-1/system.d
 polkitdir = $(datadir)/polkit-1/actions
 
-BUILT_SOURCES = cpufreq-selector-service-glue.h
+cpufreq-selector-gen.h:
+cpufreq-selector-gen.c: ../org.gnome.CPUFreqSelector.xml
+       $(AM_V_GEN) $(GDBUS_CODEGEN) --c-namespace CPUFreq \
+               --generate-c-code cpufreq-selector-gen \
+               $(srcdir)/../org.gnome.CPUFreqSelector.xml
 
-cpufreq-selector-service-glue.h: $(srcdir)/cpufreq-selector-service.xml
-       dbus-binding-tool --prefix=cpufreq_selector_service --mode=glib-server  \
-                         --output=cpufreq-selector-service-glue.h              \
-                         $(srcdir)/cpufreq-selector-service.xml
+BUILT_SOURCES = \
+       cpufreq-selector-gen.c \
+       cpufreq-selector-gen.h \
+       $(NULL)
 
 dbus_services_DATA = $(dbus_services_in_files:.service.in=.service)
 $(dbus_services_DATA): $(dbus_services_in_files)
@@ -53,7 +57,6 @@ dbus_conf_DATA = org.gnome.CPUFreqSelector.conf
 polkit_DATA = $(polkit_in_files:.policy.in=.policy)
 
 EXTRA_DIST = \
-       cpufreq-selector-service.xml    \
        $(dbus_services_in_files)       \
        org.gnome.CPUFreqSelector.conf  \
        $(polkit_in_files)
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c 
b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
index 11a64f22b..20bb21aab 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
@@ -1,484 +1,452 @@
 /*
- * GNOME CPUFreq Applet
- * Copyright (C) 2008 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright (C) 2008 Carlos Garcia Campos
+ * Copyright (C) 2020 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/>.
  */
 
+#include "config.h"
+#include "cpufreq-selector-service.h"
+
 #include <polkit/polkit.h>
-#include <dbus/dbus-glib-lowlevel.h>
 
 #include "cpufreq-selector.h"
-#include "cpufreq-selector-service.h"
-#include "cpufreq-selector-service-glue.h"
+#include "cpufreq-selector-gen.h"
 
-#define MAX_CPUS 255
+struct _CPUFreqSelectorService
+{
+  GObject             parent;
 
-struct _CPUFreqSelectorService {
-        GObject parent;
+  PolkitAuthority    *authority;
 
-       CPUFreqSelector *selectors[MAX_CPUS];
-       gint             selectors_max;
+  CPUFreqSelectorGen *selector;
+  guint               bus_name_id;
 
-       DBusGConnection *system_bus;
-       
-       /* PolicyKit */
-       PolkitAuthority   *authority;
+  guint               quit_timeout_id;
 
-       guint              timer_id;
+  GHashTable         *selectors;
 };
 
-struct _CPUFreqSelectorServiceClass {
-        GObjectClass parent_class;
+enum
+{
+  SIGNAL_QUIT,
+
+  LAST_SIGNAL
 };
 
-G_DEFINE_TYPE (CPUFreqSelectorService, cpufreq_selector_service, G_TYPE_OBJECT)
+static guint signals[LAST_SIGNAL] = { 0 };
 
-#define BUS_NAME "org.gnome.CPUFreqSelector"
+G_DEFINE_TYPE (CPUFreqSelectorService, cpufreq_selector_service, G_TYPE_OBJECT)
 
-GType
-cpufreq_selector_service_error_get_type (void)
+static gboolean
+quit_timeout_cb (gpointer user_data)
 {
-       static GType etype = 0;
-
-       if (G_UNLIKELY (etype == 0)) {
-               static const GEnumValue values[] = {
-                       { SERVICE_ERROR_GENERAL,            "SERVICE_ERROR_GENERAL",            
"GeneralError" },
-                       { SERVICE_ERROR_DBUS,               "SERVICE_ERROR_DBUS",               "DBUSError" },
-                       { SERVICE_ERROR_ALREADY_REGISTERED, "SERVICE_ERROR_ALREADY_REGISTERED", 
"AlreadyRegistered" },
-                       { SERVICE_ERROR_NOT_AUTHORIZED,     "SERVICE_ERROR_NOT_AUTHORIZED",     
"NotAuthorized"},
-                       { 0, NULL, NULL}
-               };
-               
-               etype = g_enum_register_static ("CPUFreqSelectorServiceError", values);
-       }
-
-       return etype;
-}
+  CPUFreqSelectorService *self;
 
-GQuark
-cpufreq_selector_service_error_quark (void)
-{
-       static GQuark error_quark = 0;
+       self = CPUFREQ_SELECTOR_SERVICE (user_data);
+       self->quit_timeout_id = 0;
+
+  g_signal_emit (self, signals[SIGNAL_QUIT], 0);
 
-       if (G_UNLIKELY (error_quark == 0))
-               error_quark =
-                       g_quark_from_static_string ("cpufreq-selector-service-error-quark");
-       
-       return error_quark;
+  return G_SOURCE_REMOVE;
 }
 
 static void
-cpufreq_selector_service_finalize (GObject *object)
+reset_quit_timeout (CPUFreqSelectorService *self)
 {
-       CPUFreqSelectorService *service = CPUFREQ_SELECTOR_SERVICE (object);
-       gint i;
+  if (self->quit_timeout_id != 0)
+    g_source_remove (self->quit_timeout_id);
 
-       service->system_bus = NULL;
+  self->quit_timeout_id = g_timeout_add_seconds (30, quit_timeout_cb, self);
 
-       if (service->timer_id != 0) {
-               g_source_remove (service->timer_id);
-               service->timer_id = 0;
-       }
+  g_source_set_name_by_id (self->quit_timeout_id,
+                           "[gnome-applets] quit_timeout_cb");
+}
+
+static CPUFreqSelector *
+get_selector_for_cpu (CPUFreqSelectorService *self,
+                      guint                   cpu)
+{
+  CPUFreqSelector *selector;
 
-       if (service->selectors_max >= 0) {
-               for (i = 0; i < service->selectors_max; i++) {
-                       if (service->selectors[i]) {
-                               g_object_unref (service->selectors[i]);
-                               service->selectors[i] = NULL;
-                       }
-               }
+  selector = g_hash_table_lookup (self->selectors, GUINT_TO_POINTER (cpu));
 
-               service->selectors_max = -1;
-       }
+  if (selector == NULL)
+    {
+      selector = cpufreq_selector_new (cpu);
 
-       if (service->authority) {
-               g_object_unref (service->authority);
-               service->authority = NULL;
-       }
+      g_hash_table_insert (self->selectors, GUINT_TO_POINTER (cpu), selector);
+    }
 
-       G_OBJECT_CLASS (cpufreq_selector_service_parent_class)->finalize (object);
+  return selector;
 }
 
-static void
-cpufreq_selector_service_class_init (CPUFreqSelectorServiceClass *klass)
+static gboolean
+service_check_policy (CPUFreqSelectorService  *self,
+                      GDBusMethodInvocation   *invocation,
+                      GError                 **error)
+{
+  const char *sender;
+  PolkitSubject *subject;
+  PolkitAuthorizationResult *result;
+  gboolean is_authorized;
+
+  sender = g_dbus_method_invocation_get_sender (invocation);
+  subject = polkit_system_bus_name_new (sender);
+
+  result = polkit_authority_check_authorization_sync (self->authority,
+                                                      subject,
+                                                      "org.gnome.cpufreqselector",
+                                                      NULL,
+                                                      
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
+                                                      NULL,
+                                                      error);
+
+  g_object_unref (subject);
+
+  if (*error != NULL)
+    {
+      g_warning ("Check policy: %s", (*error)->message);
+      g_object_unref (result);
+
+      return FALSE;
+    }
+
+  is_authorized = polkit_authorization_result_get_is_authorized (result);
+  g_object_unref (result);
+
+  if (!is_authorized)
+    {
+      g_set_error (error,
+                   G_DBUS_ERROR,
+                   G_DBUS_ERROR_ACCESS_DENIED,
+                   "Caller is not authorized");
+    }
+
+  return is_authorized;
+}
+
+static gboolean
+handle_can_set_cb (CPUFreqSelectorGen    *object,
+                   GDBusMethodInvocation *invocation,
+                   gpointer               user_data)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  CPUFreqSelectorService *self;
+  const char *sender;
+  PolkitSubject *subject;
+  GError *error;
+  PolkitAuthorizationResult *result;
+  gboolean can_set;
+
+  self = CPUFREQ_SELECTOR_SERVICE (user_data);
+
+  reset_quit_timeout (self);
+
+  sender = g_dbus_method_invocation_get_sender (invocation);
+  subject = polkit_system_bus_name_new (sender);
+
+  error = NULL;
+  result = polkit_authority_check_authorization_sync (self->authority,
+                                                      subject,
+                                                      "org.gnome.cpufreqselector",
+                                                      NULL,
+                                                      0,
+                                                      NULL,
+                                                      &error);
+
+  g_object_unref (subject);
+
+  if (error != NULL)
+    {
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  can_set = FALSE;
+  if (polkit_authorization_result_get_is_authorized (result) ||
+      polkit_authorization_result_get_is_challenge (result))
+    can_set = TRUE;
 
-       object_class->finalize = cpufreq_selector_service_finalize;
+  g_object_unref (result);
+
+  cpufreq_selector_gen_complete_can_set (object, invocation, can_set);
+
+  return TRUE;
 }
 
-static void
-cpufreq_selector_service_init (CPUFreqSelectorService *service)
+static gboolean
+handle_set_frequency_cb (CPUFreqSelectorGen    *object,
+                         GDBusMethodInvocation *invocation,
+                         guint                  cpu,
+                         guint                  frequency,
+                         gpointer               user_data)
 {
-       service->selectors_max = -1;
+  CPUFreqSelectorService *self;
+  GError *error;
+  CPUFreqSelector *selector;
+
+  self = CPUFREQ_SELECTOR_SERVICE (user_data);
+
+  reset_quit_timeout (self);
+
+  error = NULL;
+  if (!service_check_policy (self, invocation, &error))
+    {
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  selector = get_selector_for_cpu (self, cpu);
+
+  if (selector == NULL)
+    {
+      error = g_error_new (G_DBUS_ERROR,
+                           G_DBUS_ERROR_FAILED,
+                           "Error setting frequency on cpu %d: No cpufreq support",
+                           cpu);
+
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  cpufreq_selector_set_frequency (selector, frequency, &error);
+
+  if (error != NULL)
+    {
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  cpufreq_selector_gen_complete_set_frequency (object, invocation);
+
+  return TRUE;
 }
 
 static gboolean
-service_shutdown (gpointer user_data)
+handle_set_governor_cb (CPUFreqSelectorGen    *object,
+                        GDBusMethodInvocation *invocation,
+                        guint                  cpu,
+                        const gchar           *governor,
+                        gpointer               user_data)
 {
-       CPUFreqSelectorService *service;
+  CPUFreqSelectorService *self;
+  GError *error;
+  CPUFreqSelector *selector;
+
+  self = CPUFREQ_SELECTOR_SERVICE (user_data);
+
+  reset_quit_timeout (self);
+
+  error = NULL;
+  if (!service_check_policy (self, invocation, &error))
+    {
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
 
-       service = CPUFREQ_SELECTOR_SERVICE (user_data);
-       service->timer_id = 0;
+      return FALSE;
+    }
 
-       g_object_unref (service);
+  selector = get_selector_for_cpu (self, cpu);
 
-       return G_SOURCE_REMOVE;
+  if (selector == NULL)
+    {
+      error = g_error_new (G_DBUS_ERROR,
+                           G_DBUS_ERROR_FAILED,
+                           "Error setting governor on cpu %d: No cpufreq support",
+                           cpu);
+
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  cpufreq_selector_set_governor (selector, governor, &error);
+
+  if (error != NULL)
+    {
+      g_dbus_method_invocation_return_gerror (invocation, error);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  cpufreq_selector_gen_complete_set_governor (object, invocation);
+
+  return TRUE;
 }
 
 static void
-reset_killtimer (CPUFreqSelectorService *service)
+bus_acquired_cb (GDBusConnection *connection,
+                 const char      *name,
+                 gpointer         user_data)
 {
-       if (service->timer_id != 0)
-               g_source_remove (service->timer_id);
-
-       service->timer_id = g_timeout_add_seconds (30, service_shutdown, service);
+  CPUFreqSelectorService *self;
+  GDBusInterfaceSkeleton *skeleton;
+  GError *error;
+  gboolean exported;
+
+  self = CPUFREQ_SELECTOR_SERVICE (user_data);
+  skeleton = G_DBUS_INTERFACE_SKELETON (self->selector);
+
+  error = NULL;
+  exported = g_dbus_interface_skeleton_export (skeleton,
+                                               connection,
+                                               "/org/gnome/cpufreq_selector/selector",
+                                               &error);
+
+  if (!exported)
+    {
+      g_warning ("Failed to export interface: %s", error->message);
+      g_error_free (error);
+
+      return;
+    }
+
+  g_signal_connect (self->selector,
+                    "handle-can-set",
+                    G_CALLBACK (handle_can_set_cb),
+                    self);
+
+  g_signal_connect (self->selector,
+                    "handle-set-frequency",
+                    G_CALLBACK (handle_set_frequency_cb),
+                    self);
+
+  g_signal_connect (self->selector,
+                    "handle-set-governor",
+                    G_CALLBACK (handle_set_governor_cb),
+                    self);
 }
 
-gboolean
-cpufreq_selector_service_register (CPUFreqSelectorService *service,
-                                  GError                **error)
+static void
+name_acquired_cb (GDBusConnection *connection,
+                  const char      *name,
+                  gpointer         user_data)
 {
-       DBusGConnection *connection;
-       DBusGProxy      *bus_proxy;
-       gboolean         res;
-       guint            result;
-       GError          *err = NULL;
-
-       if (service->system_bus) {
-               g_set_error (error,
-                            CPUFREQ_SELECTOR_SERVICE_ERROR,
-                            SERVICE_ERROR_ALREADY_REGISTERED,
-                            "Service %s already registered", BUS_NAME);
-               return FALSE;
-       }
-
-       connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
-       if (!connection) {
-               g_set_error (error,
-                            CPUFREQ_SELECTOR_SERVICE_ERROR,
-                            SERVICE_ERROR_DBUS,
-                            "Couldn't connect to system bus: %s",
-                            err->message);
-               g_error_free (err);
-
-               return FALSE;
-       }
-
-       bus_proxy = dbus_g_proxy_new_for_name (connection,
-                                              DBUS_SERVICE_DBUS,
-                                              DBUS_PATH_DBUS,
-                                              DBUS_INTERFACE_DBUS);
-       if (!bus_proxy) {
-               g_set_error (error,
-                            CPUFREQ_SELECTOR_SERVICE_ERROR,
-                            SERVICE_ERROR_DBUS,
-                            "Could not construct bus_proxy object");
-               return FALSE;
-       }
-
-       res = dbus_g_proxy_call (bus_proxy,
-                                "RequestName",
-                                &err,
-                                G_TYPE_STRING, BUS_NAME,
-                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
-                                G_TYPE_INVALID,
-                                G_TYPE_UINT, &result,
-                                G_TYPE_INVALID);
-       g_object_unref (bus_proxy);
-       
-       if (!res) {
-               if (err) {
-                       g_set_error (error,
-                                    CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                    SERVICE_ERROR_DBUS,
-                                    "Failed to acquire %s: %s",
-                                    BUS_NAME, err->message);
-                       g_error_free (err);
-               } else {
-                       g_set_error (error,
-                                    CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                    SERVICE_ERROR_DBUS,
-                                    "Failed to acquire %s", BUS_NAME);
-               }
-
-               return FALSE;
-       }
-
-       if (result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
-               g_set_error (error,
-                            CPUFREQ_SELECTOR_SERVICE_ERROR,
-                            SERVICE_ERROR_ALREADY_REGISTERED,
-                            "Service %s already registered", BUS_NAME);
-               return FALSE;
-       }
-
-       service->authority = polkit_authority_get_sync (NULL, error);
-
-       if (!service->authority)
-         return FALSE;
-
-       service->system_bus = connection;
-
-       dbus_g_object_type_install_info (CPUFREQ_TYPE_SELECTOR_SERVICE,
-                                        &dbus_glib_cpufreq_selector_service_object_info);
-       dbus_g_connection_register_g_object (connection,
-                                            "/org/gnome/cpufreq_selector/selector",
-                                            G_OBJECT (service));
-       dbus_g_error_domain_register (CPUFREQ_SELECTOR_SERVICE_ERROR, NULL,
-                                     CPUFREQ_TYPE_SELECTOR_SERVICE_ERROR);
-
-       reset_killtimer (service);
-
-       return TRUE;
 }
 
-static CPUFreqSelector *
-get_selector_for_cpu (CPUFreqSelectorService *service,
-                     guint                   cpu)
+static void
+name_lost_cb (GDBusConnection *connection,
+              const char      *name,
+              gpointer         user_data)
 {
-       if (!service->selectors[cpu]) {
-               service->selectors[cpu] = cpufreq_selector_new (cpu);
-               if (!service->selectors[cpu])
-                       return NULL;
-               
-               if (service->selectors_max < cpu)
-                       service->selectors_max = cpu;
-       }
-       
-       return service->selectors[cpu];
 }
 
-/* PolicyKit */
-static gboolean
-cpufreq_selector_service_check_policy (CPUFreqSelectorService *service,
-                                      DBusGMethodInvocation  *context,
-                                      GError                **error)
+static void
+cpufreq_selector_service_dispose (GObject *object)
 {
-       PolkitSubject             *subject;
-       PolkitAuthorizationResult *result;
-       gchar                     *sender;
-       gboolean                   ret;
-
-       sender = dbus_g_method_get_sender (context);
-       subject = polkit_system_bus_name_new (sender);
-       g_free (sender);
-
-       result = polkit_authority_check_authorization_sync (service->authority,
-                                                            subject,
-                                                            "org.gnome.cpufreqselector",
-                                                            NULL,
-                                                            
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
-                                                            NULL, error);
-        g_object_unref (subject);
-
-        if (*error) {
-               g_warning ("Check policy: %s", (*error)->message);
-               g_object_unref (result);
-
-               return FALSE;
-       }
-
-       ret = polkit_authorization_result_get_is_authorized (result);
-       if (!ret) {
-               g_set_error (error,
-                            CPUFREQ_SELECTOR_SERVICE_ERROR,
-                            SERVICE_ERROR_NOT_AUTHORIZED,
-                            "Caller is not authorized");
-       }
-
-       g_object_unref (result);
-
-       return ret;
+  CPUFreqSelectorService *self;
+
+  self = CPUFREQ_SELECTOR_SERVICE (object);
+
+  if (self->bus_name_id != 0)
+    {
+      g_bus_unown_name (self->bus_name_id);
+      self->bus_name_id = 0;
+    }
+
+  if (self->selector)
+    {
+      GDBusInterfaceSkeleton *skeleton;
+
+      skeleton = G_DBUS_INTERFACE_SKELETON (self->selector);
+
+      g_dbus_interface_skeleton_unexport (skeleton);
+      g_clear_object (&self->selector);
+    }
+
+  if (self->quit_timeout_id != 0)
+    {
+      g_source_remove (self->quit_timeout_id);
+      self->quit_timeout_id = 0;
+    }
+
+  g_clear_object (&self->authority);
+
+  g_clear_pointer (&self->selectors, g_hash_table_destroy);
+
+  G_OBJECT_CLASS (cpufreq_selector_service_parent_class)->dispose (object);
 }
 
-/* D-BUS interface */
-gboolean
-cpufreq_selector_service_set_frequency (CPUFreqSelectorService *service,
-                                       guint                   cpu,
-                                       guint                   frequency,
-                                       DBusGMethodInvocation  *context)
+static void
+install_signals (void)
 {
-       CPUFreqSelector *selector;
-       GError          *error = NULL;
-
-       reset_killtimer (service);
-
-       if (!cpufreq_selector_service_check_policy (service, context, &error)) {
-               dbus_g_method_return_error (context, error);
-               g_error_free (error);
-
-               return FALSE;
-       }
-
-       if (cpu > MAX_CPUS) {
-               GError *err;
-               
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting frequency on cpu %d: Invalid cpu",
-                                  cpu);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-
-               return FALSE;
-       }
-
-       selector = get_selector_for_cpu (service, cpu);
-       if (!selector) {
-               GError *err;
-
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting frequency on cpu %d: No cpufreq support",
-                                  cpu);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-
-               return FALSE;
-       }
-
-       cpufreq_selector_set_frequency (selector, frequency, &error);
-       if (error) {
-               GError *err;
-
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting frequency %d on cpu %d: %s",
-                                  frequency, cpu, error->message);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-               g_error_free (error);
-
-               return FALSE;
-       }
-       
-       dbus_g_method_return (context);
-       
-       return TRUE;
+  signals[SIGNAL_QUIT] =
+    g_signal_new ("quit",
+                  CPUFREQ_TYPE_SELECTOR_SERVICE,
+                  G_SIGNAL_RUN_LAST,
+                  0,
+                  NULL,
+                  NULL,
+                  NULL,
+                  G_TYPE_NONE,
+                  0);
 }
 
-gboolean
-cpufreq_selector_service_set_governor (CPUFreqSelectorService *service,
-                                      guint                   cpu,
-                                      const gchar            *governor,
-                                      DBusGMethodInvocation  *context)
+static void
+cpufreq_selector_service_class_init (CPUFreqSelectorServiceClass *self_class)
 {
-       CPUFreqSelector *selector;
-       GError          *error = NULL;
-
-       reset_killtimer (service);
-
-       if (!cpufreq_selector_service_check_policy (service, context, &error)) {
-               dbus_g_method_return_error (context, error);
-               g_error_free (error);
-
-               return FALSE;
-       }
-       
-       if (cpu > MAX_CPUS) {
-               GError *err;
-
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting governor on cpu %d: Invalid cpu",
-                                  cpu);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-
-               return FALSE;
-       }
-
-       selector = get_selector_for_cpu (service, cpu);
-       if (!selector) {
-               GError *err;
-
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting governor on cpu %d: No cpufreq support",
-                                  cpu);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-
-               return FALSE;
-       }
-
-       cpufreq_selector_set_governor (selector, governor, &error);
-       if (error) {
-               GError *err;
-
-               err = g_error_new (CPUFREQ_SELECTOR_SERVICE_ERROR,
-                                  SERVICE_ERROR_DBUS,
-                                  "Error setting governor %s on cpu %d: %s",
-                                  governor, cpu, error->message);
-               dbus_g_method_return_error (context, err);
-               g_error_free (err);
-               g_error_free (error);
-
-               return FALSE;
-       }
-
-       dbus_g_method_return (context);
-       
-       return TRUE;
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (self_class);
+
+  object_class->dispose = cpufreq_selector_service_dispose;
+
+  install_signals ();
 }
 
+static void
+cpufreq_selector_service_init (CPUFreqSelectorService *self)
+{
+  GError *error;
+
+  error = NULL;
+  self->authority = polkit_authority_get_sync (NULL, &error);
+
+  if (error != NULL)
+    {
+      g_printerr ("%s\n", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  self->selector = cpufreq_selector_gen_skeleton_new ();
+  self->bus_name_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
+                                      "org.gnome.CPUFreqSelector",
+                                      G_BUS_NAME_OWNER_FLAGS_NONE,
+                                      bus_acquired_cb,
+                                      name_acquired_cb,
+                                      name_lost_cb,
+                                      self,
+                                      NULL);
+
+  self->selectors = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref);
+
+  reset_quit_timeout (self);
+}
 
-gboolean
-cpufreq_selector_service_can_set (CPUFreqSelectorService *service,
-                                 DBusGMethodInvocation  *context)
+CPUFreqSelectorService *
+cpufreq_selector_service_new (void)
 {
-       PolkitSubject             *subject;
-       PolkitAuthorizationResult *result;
-       gchar                     *sender;
-       gboolean                   ret;
-        GError                    *error = NULL;
-
-       reset_killtimer (service);
-
-       sender = dbus_g_method_get_sender (context);
-       subject = polkit_system_bus_name_new (sender);
-       g_free (sender);
-
-       result = polkit_authority_check_authorization_sync (service->authority,
-                                                            subject,
-                                                            "org.gnome.cpufreqselector",
-                                                            NULL,
-                                                            0,
-                                                            NULL,
-                                                           &error);
-        g_object_unref (subject);
-
-       if (error) {
-               dbus_g_method_return_error (context, error);
-               g_error_free (error);
-
-               return FALSE;
-       }
-
-        if (polkit_authorization_result_get_is_authorized (result)) {
-               ret = TRUE;
-       } else if (polkit_authorization_result_get_is_challenge (result)) {
-               ret = TRUE;
-       } else {
-               ret = FALSE;
-       }
-
-       g_object_unref (result);
-
-        dbus_g_method_return (context, ret);
-
-       return TRUE;
+  return g_object_new (CPUFREQ_TYPE_SELECTOR_SERVICE, NULL);
 }
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h 
b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
index 785bce6e8..8effcfb9a 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
@@ -1,67 +1,33 @@
 /*
- * GNOME CPUFreq Applet
- * Copyright (C) 2008 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright (C) 2020 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/>.
  */
 
-#ifndef __CPUFREQ_SELECTOR_SERVICE_H__
-#define __CPUFREQ_SELECTOR_SERVICE_H__
+#ifndef CPUFREQ_SELECTOR_SERVICE_H
+#define CPUFREQ_SELECTOR_SERVICE_H
 
 #include <glib-object.h>
-#include <dbus/dbus-glib.h>
 
 G_BEGIN_DECLS
 
-#define CPUFREQ_TYPE_SELECTOR_SERVICE            (cpufreq_selector_service_get_type ())
-#define CPUFREQ_SELECTOR_SERVICE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
CPUFREQ_TYPE_SELECTOR_SERVICE, CPUFreqSelectorService))
-#define CPUFREQ_SELECTOR_SERVICE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), 
CPUFREQ_TYPE_SELECTOR_SERVICE, CPUFreqSelectorServiceClass))
-#define CPUFREQ_IS_SELECTOR_SERVICE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
CPUFREQ_TYPE_SELECTOR_SERVICE))
-#define CPUFREQ_IS_SELECTOR_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), 
CPUFREQ_TYPE_SELECTOR_SERVICE))
-#define CPUFREQ_SELECTOR_SERVICE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), 
CPUFREQ_TYPE_SELECTOR_SERVICE, CPUFreqSelectorServiceClass))
+#define CPUFREQ_TYPE_SELECTOR_SERVICE (cpufreq_selector_service_get_type ())
+G_DECLARE_FINAL_TYPE (CPUFreqSelectorService, cpufreq_selector_service,
+                      CPUFREQ, SELECTOR_SERVICE, GObject)
 
-#define CPUFREQ_SELECTOR_SERVICE_ERROR           (cpufreq_selector_service_error_quark ())
-#define CPUFREQ_TYPE_SELECTOR_SERVICE_ERROR      (cpufreq_selector_service_error_get_type ())
-
-enum {
-       SERVICE_ERROR_GENERAL,
-       SERVICE_ERROR_DBUS,
-       SERVICE_ERROR_ALREADY_REGISTERED,
-       SERVICE_ERROR_NOT_AUTHORIZED
-};
-
-typedef struct _CPUFreqSelectorService        CPUFreqSelectorService;
-typedef struct _CPUFreqSelectorServiceClass   CPUFreqSelectorServiceClass;
-
-GType                   cpufreq_selector_service_get_type       (void) G_GNUC_CONST;
-GType                   cpufreq_selector_service_error_get_type (void) G_GNUC_CONST;
-GQuark                  cpufreq_selector_service_error_quark    (void) G_GNUC_CONST;
-gboolean                cpufreq_selector_service_register       (CPUFreqSelectorService *service,
-                                                                GError                **error);
-
-gboolean                cpufreq_selector_service_set_frequency  (CPUFreqSelectorService *service,
-                                                                guint                   cpu,
-                                                                guint                   frequency,
-                                                                DBusGMethodInvocation  *context);
-gboolean                cpufreq_selector_service_set_governor   (CPUFreqSelectorService *service,
-                                                                guint                   cpu,
-                                                                const gchar            *governor,
-                                                                DBusGMethodInvocation  *context);
-gboolean               cpufreq_selector_service_can_set         (CPUFreqSelectorService *service,
-                                                                DBusGMethodInvocation  *context);
+CPUFreqSelectorService *cpufreq_selector_service_new (void);
 
 G_END_DECLS
 
-#endif /* __CPUFREQ_SELECTOR_SERVICE_H__ */
-
+#endif
diff --git a/cpufreq/src/cpufreq-selector/main.c b/cpufreq/src/cpufreq-selector/main.c
index 394278fbd..d584a1111 100644
--- a/cpufreq/src/cpufreq-selector/main.c
+++ b/cpufreq/src/cpufreq-selector/main.c
@@ -1,205 +1,47 @@
 /*
- * GNOME CPUFreq Applet
- * Copyright (C) 2004 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright (C) 2004 Carlos Garcia Campos
+ * Copyright (C) 2020 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/>.
- *
- * Authors : Carlos García Campos <carlosgc gnome org>
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 
-#include <glib.h>
-#include <glib-object.h>
-
 #include "cpufreq-selector-service.h"
-#include "cpufreq-selector.h"
-
-static gint    cpu = 0;
-static gchar  *governor = NULL;
-static gulong  frequency = 0;
-
-static const GOptionEntry options[] = {
-       { "cpu",       'c', 0, G_OPTION_ARG_INT,    &cpu,       "CPU Number",       NULL },
-       { "governor",  'g', 0, G_OPTION_ARG_STRING, &governor,  "Governor",         NULL },
-       { "frequency", 'f', 0, G_OPTION_ARG_INT,    &frequency, "Frequency in KHz", NULL },
-       { NULL }
-};
-
-static void
-do_exit (GMainLoop *loop,
-        GObject   *object)
-{
-       if (g_main_loop_is_running (loop))
-               g_main_loop_quit (loop);
-}
-
-static void
-cpufreq_selector_set_values_dbus (void)
-{
-       DBusGConnection *connection;
-       DBusGProxy      *proxy;
-       gboolean         res;
-       GError          *error = NULL;
-
-       connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
-       if (!connection) {
-               g_printerr ("Couldn't connect to system bus: %s\n",
-                           error->message);
-               g_error_free (error);
-
-               return;
-       }
-
-       proxy = dbus_g_proxy_new_for_name (connection,
-                                          "org.gnome.CPUFreqSelector",
-                                          "/org/gnome/cpufreq_selector/selector",
-                                          "org.gnome.CPUFreqSelector");
-       if (!proxy) {
-               g_printerr ("Could not construct proxy object\n");
-
-               return;
-       }
-
-       if (governor) {
-               res = dbus_g_proxy_call (proxy, "SetGovernor", &error,
-                                        G_TYPE_UINT, cpu,
-                                        G_TYPE_STRING, governor,
-                                        G_TYPE_INVALID,
-                                        G_TYPE_INVALID);
-               if (!res) {
-                       if (error) {
-                               g_printerr ("Error calling SetGovernor: %s\n", error->message);
-                               g_error_free (error);
-                       } else {
-                               g_printerr ("Error calling SetGovernor\n");
-                       }
-                       
-                       g_object_unref (proxy);
-                       
-                       return;
-               }
-       }
-
-       if (frequency != 0) {
-               res = dbus_g_proxy_call (proxy, "SetFrequency", &error,
-                                        G_TYPE_UINT, cpu,
-                                        G_TYPE_UINT, frequency,
-                                        G_TYPE_INVALID,
-                                        G_TYPE_INVALID);
-               if (!res) {
-                       if (error) {
-                               g_printerr ("Error calling SetFrequency: %s\n", error->message);
-                               g_error_free (error);
-                       } else {
-                               g_printerr ("Error calling SetFrequency\n");
-                       }
-                       
-                       g_object_unref (proxy);
-                       
-                       return;
-               }
-       }
-
-       g_object_unref (proxy);
-}
 
 static void
-cpufreq_selector_set_values (void)
+quit_cb (CPUFreqSelectorService *self,
+         GMainLoop              *loop)
 {
-       CPUFreqSelector *selector;
-       GError          *error = NULL;
-
-       selector = cpufreq_selector_new (cpu);
-       if (!selector) {
-               g_printerr ("No cpufreq support\n");
-
-               return;
-       }
-
-       if (governor) {
-               cpufreq_selector_set_governor (selector, governor, &error);
-
-               if (error) {
-                       g_printerr ("%s\n", error->message);
-                       g_error_free (error);
-                       error = NULL;
-               }
-       }
-
-       if (frequency != 0) {
-               cpufreq_selector_set_frequency (selector, frequency, &error);
-
-               if (error) {
-                       g_printerr ("%s\n", error->message);
-                       g_error_free (error);
-                       error = NULL;
-               }
-       }
-
-       g_object_unref (selector);
+  if (g_main_loop_is_running (loop))
+    g_main_loop_quit (loop);
 }
 
-gint
-main (gint argc, gchar **argv)
+int
+main (void)
 {
-       GMainLoop      *loop;
-       GOptionContext *context;
-       GError         *error = NULL;
-       CPUFreqSelectorService *selector_service;
-
-       context = g_option_context_new ("- CPUFreq Selector");
-       g_option_context_add_main_entries (context, options, NULL);
-       
-       if (!g_option_context_parse (context, &argc, &argv, &error)) {
-               if (error) {
-                       g_printerr ("%s\n", error->message);
-                       g_error_free (error);
-               } 
-
-               g_option_context_free (context);
-               
-               return 1;
-       }
-       
-       g_option_context_free (context);
-
-       selector_service = g_object_new (CPUFREQ_TYPE_SELECTOR_SERVICE, NULL);
-
-       if (!cpufreq_selector_service_register (selector_service, &error)) {
-               if (governor || frequency != 0) {
-                       cpufreq_selector_set_values_dbus ();
-
-                       return 0;
-               }
-
-               g_printerr ("%s\n", error->message);
-               g_error_free (error);
+  GMainLoop *loop;
+  CPUFreqSelectorService *service;
 
-               return 1;
-       }
+  loop = g_main_loop_new (NULL, FALSE);
+  service = cpufreq_selector_service_new ();
 
-       cpufreq_selector_set_values ();
+  g_signal_connect (service, "quit", G_CALLBACK (quit_cb), loop);
 
-       loop = g_main_loop_new (NULL, FALSE);
-       g_object_weak_ref (G_OBJECT (selector_service),
-                          (GWeakNotify) do_exit,
-                          loop);
-               
-       g_main_loop_run (loop);
+  g_main_loop_run (loop);
 
-       g_main_loop_unref (loop);
+  g_main_loop_unref (loop);
 
-        return 0;
+  return 0;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]