1
/***************************************************************************
5
* Copyright (C) 2005 SUSE Linux Products GmbH *
7
* This program is free software; you can redistribute it and/or modify it *
8
* under the terms of the GNU General Public License as published by the *
9
* Free Software Foundation; either version 2 of the License, or (at you *
10
* option) any later version. *
12
* This program is distributed in the hope that it will be useful, but *
13
* WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15
* General Public License for more details. *
17
* You should have received a copy of the GNU General Public License along *
18
* with this program; if not, write to the Free Software Foundation, Inc., *
19
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21
***************************************************************************/
23
#include "main_loop.h"
27
#include "cpufreq_management.h"
28
#include "event_management.h"
30
using namespace Powersave::Globals;
32
/**** class Callback ****/
40
Callback::Callback(int interval_, gboolean (*function_)(gpointer data))
47
/**** class MainLoop ****/
49
MainLoop::MainLoop() {
50
/* set up a new main loop with glib and the default context */
51
_gmain = g_main_loop_new(NULL, FALSE);
53
DBusConnection *dbus_connection = _dbus_server.openSystemConnection();
55
if ( dbus_connection == NULL ) {
57
"Error setting up connection to DBus system bus. Aborting...");
60
dbus_connection_setup_with_g_main(dbus_connection, NULL);
63
if (!hwEvent_connect()) {
64
addCallback(3000, (GSourceFunc) hwEvent_reconnect);
72
pDebug(DBG_INFO, "Starting main loop");
73
g_main_loop_run(_gmain);
78
g_main_loop_quit(_gmain);
81
gboolean MainLoop::hwEvent_callback(GIOChannel *io_channel,
82
GIOCondition io_condition,
85
int io_channel_fd = g_io_channel_unix_get_fd(io_channel);
87
if (io_condition & (G_IO_ERR | G_IO_HUP)) {
88
pDebug(DBG_ERR, "hw event socket broke away trying to reconnect...");
92
main_loop->addCallback(3000, (GSourceFunc) hwEvent_reconnect);
97
pm->handleHWEventRequest(io_channel_fd);
102
gboolean MainLoop::hwEvent_reconnect(gpointer data)
104
if (hwEvent_connect()) {
111
gboolean MainLoop::hwEvent_connect()
114
GIOChannel *hwEvent_channel;
115
int hwEvent_watch = -1;
117
/* set up the event interface. /proc/acpi/event or /dev/apm_bios */
118
if ((hwEvent_fd = pm->openHWEventFD()) < 0) {
121
hwEvent_channel = g_io_channel_unix_new(hwEvent_fd);
122
hwEvent_watch = g_io_add_watch(hwEvent_channel,
123
GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP),
124
hwEvent_callback, NULL);
129
gboolean MainLoop::checkThrottling_callback(gpointer data)
131
pm->checkThrottling();
136
gboolean MainLoop::checkEventTimeouts_callback(gpointer data)
138
pm->_eM->checkEventTimeouts();
143
gboolean MainLoop::adjustSpeeds_callback(gpointer data)
145
if (config_obj->current_scheme->CPUFREQ_CONTROL == CPUFREQ_USERSPACE) {
146
cpufreq->adjustSpeeds();
153
void MainLoop::setupCallbacks()
155
_callback_functions.push_back(
156
Callback(config_obj->current_scheme->POLL_INTERVAL,
157
checkThrottling_callback));
159
_callback_functions.push_back(
160
Callback(3000 ,checkEventTimeouts_callback));
162
_callback_functions.push_back(
163
Callback(config_obj->current_scheme->POLL_INTERVAL,
164
adjustSpeeds_callback));
169
void MainLoop::updateCallbacks()
171
for (std::list<Callback>::iterator it = _callback_functions.begin();
172
it != _callback_functions.end(); ++it) {
174
if (g_main_context_find_source_by_id(NULL, it->id) == NULL) {
175
it->id = g_timeout_add(it->interval, it->function, NULL);
180
int MainLoop::addCallback(int interval, gboolean (*function)(gpointer data))
182
Callback cb(interval, function);
184
cb.id = g_timeout_add(cb.interval, cb.function, NULL);
185
_callback_functions.push_back(cb);