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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <glib.h>
27 #include <glib-object.h>
28 
29 #ifdef HAVE_POLKIT
30 #include "cpufreq-selector-service.h"
31 #else
32 #include <unistd.h>
33 #endif
34 #include "cpufreq-selector-factory.h"
35 
36 
37 static gint    cpu = 0;
38 static gchar  *governor = NULL;
39 static gulong  frequency = 0;
40 
41 static const GOptionEntry options[] = {
42     { "cpu",       'c', 0, G_OPTION_ARG_INT,    &cpu,       "CPU Number",       NULL },
43     { "governor",  'g', 0, G_OPTION_ARG_STRING, &governor,  "Governor",         NULL },
44     { "frequency", 'f', 0, G_OPTION_ARG_INT,    &frequency, "Frequency in KHz", NULL },
45     { NULL }
46 };
47 
48 #ifdef HAVE_POLKIT
49 static void
do_exit(GMainLoop * loop,GObject * object)50 do_exit (GMainLoop *loop,
51          GObject   *object)
52 {
53     if (g_main_loop_is_running (loop))
54         g_main_loop_quit (loop);
55 }
56 
57 static void
cpufreq_selector_set_values_dbus(void)58 cpufreq_selector_set_values_dbus (void)
59 {
60     DBusGConnection *connection;
61     DBusGProxy      *proxy;
62     gboolean         res;
63     GError          *error = NULL;
64 
65     connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
66     if (!connection) {
67         g_printerr ("Couldn't connect to system bus: %s\n",
68                     error->message);
69         g_error_free (error);
70 
71         return;
72     }
73 
74     proxy = dbus_g_proxy_new_for_name (connection,
75                                        "org.mate.CPUFreqSelector",
76                                        "/org/mate/cpufreq_selector/selector",
77                                        "org.mate.CPUFreqSelector");
78     if (!proxy) {
79         g_printerr ("Could not construct proxy object\n");
80 
81         return;
82     }
83 
84     if (governor) {
85         res = dbus_g_proxy_call (proxy, "SetGovernor", &error,
86                                  G_TYPE_UINT, cpu,
87                                  G_TYPE_STRING, governor,
88                                  G_TYPE_INVALID,
89                                  G_TYPE_INVALID);
90         if (!res) {
91             if (error) {
92                 g_printerr ("Error calling SetGovernor: %s\n", error->message);
93                 g_error_free (error);
94             } else {
95                 g_printerr ("Error calling SetGovernor\n");
96             }
97 
98             g_object_unref (proxy);
99 
100             return;
101         }
102     }
103 
104     if (frequency != 0) {
105         res = dbus_g_proxy_call (proxy, "SetFrequency", &error,
106                                  G_TYPE_UINT, cpu,
107                                  G_TYPE_UINT, frequency,
108                                  G_TYPE_INVALID,
109                                  G_TYPE_INVALID);
110         if (!res) {
111             if (error) {
112                 g_printerr ("Error calling SetFrequency: %s\n", error->message);
113                 g_error_free (error);
114             } else {
115                 g_printerr ("Error calling SetFrequency\n");
116             }
117 
118             g_object_unref (proxy);
119 
120             return;
121         }
122     }
123 
124     g_object_unref (proxy);
125 }
126 #endif /* HAVE_POLKIT */
127 
128 static void
cpufreq_selector_set_values(void)129 cpufreq_selector_set_values (void)
130 {
131     CPUFreqSelector *selector;
132     GError          *error = NULL;
133 
134     selector = cpufreq_selector_factory_create_selector (cpu);
135     if (!selector) {
136         g_printerr ("No cpufreq support\n");
137 
138         return;
139     }
140 
141     if (governor) {
142         cpufreq_selector_set_governor (selector, governor, &error);
143 
144         if (error) {
145             g_printerr ("%s\n", error->message);
146             g_error_free (error);
147             error = NULL;
148         }
149     }
150 
151     if (frequency != 0) {
152         cpufreq_selector_set_frequency (selector, frequency, &error);
153 
154         if (error) {
155             g_printerr ("%s\n", error->message);
156             g_error_free (error);
157             error = NULL;
158         }
159     }
160 
161     g_object_unref (selector);
162 }
163 
164 gint
main(gint argc,gchar ** argv)165 main (gint argc, gchar **argv)
166 {
167 #ifdef HAVE_POLKIT
168     GMainLoop      *loop;
169 #endif
170     GOptionContext *context;
171     GError         *error = NULL;
172 
173 #ifndef HAVE_POLKIT
174     if (geteuid () != 0) {
175         g_printerr ("You must be root\n");
176 
177         return 1;
178     }
179 
180     if (argc < 2) {
181         g_printerr ("Missing operand after `cpufreq-selector'\n");
182         g_printerr ("Try `cpufreq-selector --help' for more information.\n");
183 
184         return 1;
185     }
186 #endif
187 
188     context = g_option_context_new ("- CPUFreq Selector");
189     g_option_context_add_main_entries (context, options, NULL);
190 
191     if (!g_option_context_parse (context, &argc, &argv, &error)) {
192         if (error) {
193             g_printerr ("%s\n", error->message);
194             g_error_free (error);
195         }
196 
197         g_option_context_free (context);
198 
199         return 1;
200     }
201 
202     g_option_context_free (context);
203 
204 #ifdef HAVE_POLKIT
205     if (!cpufreq_selector_service_register (SELECTOR_SERVICE, &error)) {
206         if (governor || frequency != 0) {
207             cpufreq_selector_set_values_dbus ();
208 
209             return 0;
210         }
211 
212         g_printerr ("%s\n", error->message);
213         g_error_free (error);
214 
215         return 1;
216     }
217 
218     cpufreq_selector_set_values ();
219 
220     loop = g_main_loop_new (NULL, FALSE);
221     g_object_weak_ref (G_OBJECT (SELECTOR_SERVICE),
222                        (GWeakNotify) do_exit,
223                        loop);
224 
225     g_main_loop_run (loop);
226 
227     g_main_loop_unref (loop);
228 #else /* !HAVE_POLKIT */
229     cpufreq_selector_set_values ();
230 #endif /* HAVE_POLKIT */
231 
232         return 0;
233 }
234