1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/***************************************************************************
* *
* Powersave Daemon *
* *
* Copyright (C) 2005 SUSE Linux Products GmbH *
* *
* Author(s): Holger Macht <hmacht@suse.de> *
* *
* 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 you *
* option) any later version. *
* *
* 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, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include <signal.h>
#include <sched.h>
#include <errno.h>
#include <string.h>
#include "main_loop.h"
#include "powerlib.h"
#include "globals.h"
#include "cpufreq_management.h"
#include "event_management.h"
#include "apm.h"
using namespace Powersave::Globals;
/**** class Callback ****/
Callback::Callback() : interval(-1), function(NULL), id(-1)
{
}
Callback::Callback(int interval_, gboolean (*function_)(gpointer data)) :
interval(interval_), function(function_), id(-1)
{
}
/**** class MainLoop ****/
MainLoop::MainLoop() {
/* set up a new main loop with glib and the default context */
_gmain = g_main_loop_new(NULL, FALSE);
DBusConnection *dbus_connection = _dbus_server.openSystemConnection();
if ( dbus_connection == NULL ) {
pDebug(DBG_ERR,
"Error setting up connection to DBus system bus. Aborting...");
exit(1);
} else {
dbus_connection_setup_with_g_main(dbus_connection, NULL);
}
if (!hwEvent_connect()) {
pDebug(DBG_ERR, "Cannot access acpi/apm event file\n");
/* only try to reconnect if we have acpi. If /dev/apm_bios
is not there on startup, it won't appear later */
APM_Interface *p = dynamic_cast< APM_Interface* >(pm);
if (p == NULL) {
addCallback(3000, (GSourceFunc) hwEvent_reconnect);
}
}
// wait about 10 minutes for hal to appear
unsigned int waiting = 0;
while (!DBus_Server::serviceHasOwner("org.freedesktop.Hal")) {
/* give other processes a better chance to evolve */
if (sched_yield() != 0) {
pDebug(DBG_ERR, "Error while yielding scheduler: %s", strerror(errno));
}
sleep(3);
waiting++;
if (waiting > 200) {
pDebug(DBG_ERR, "Haldaemon did not appear. Aborting...");
kill(getpid(), SIGTERM);
}
}
setupCallbacks();
}
void MainLoop::run()
{
pDebug(DBG_INFO, "Starting main loop");
g_main_loop_run(_gmain);
}
void MainLoop::quit()
{
g_main_loop_quit(_gmain);
}
gboolean MainLoop::hwEvent_callback(GIOChannel *io_channel,
GIOCondition io_condition,
gpointer data)
{
int io_channel_fd = g_io_channel_unix_get_fd(io_channel);
if (io_condition & (G_IO_ERR | G_IO_HUP)) {
pDebug(DBG_ERR, "hw event socket broke away trying to reconnect...");
close(io_channel_fd);
main_loop->addCallback(3000, (GSourceFunc) hwEvent_reconnect);
return false;
}
pm->handleHWEventRequest(io_channel_fd);
return true;
}
gboolean MainLoop::hwEvent_reconnect(gpointer data)
{
if (hwEvent_connect()) {
pDebug(DBG_ERR, "Successfully reconnected to acpi/apm event file\n");
return false;
}
return true;
}
gboolean MainLoop::hwEvent_connect()
{
int hwEvent_fd = -1;
GIOChannel *hwEvent_channel;
int hwEvent_watch = -1;
/* set up the event interface. /proc/acpi/event or /dev/apm_bios */
if ((hwEvent_fd = pm->openHWEventFD()) < 0) {
return false;
} else {
hwEvent_channel = g_io_channel_unix_new(hwEvent_fd);
hwEvent_watch = g_io_add_watch(hwEvent_channel,
GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP),
hwEvent_callback, NULL);
return true;
}
}
gboolean MainLoop::checkThrottling_callback(gpointer data)
{
if (config_obj->current_scheme->ALLOW_THROTTLING && !cpufreq->isSupported()) {
pm->checkThrottling();
return true;
}
return false;
}
gboolean MainLoop::checkEventTimeouts_callback(gpointer data)
{
return pm->_eM->checkEventTimeouts();
}
gboolean MainLoop::adjustSpeeds_callback(gpointer data)
{
if (cpufreq->hasControlMode(CPUFREQ_USERSPACE)) {
cpufreq->adjustSpeeds();
return true;
}
return false;
}
void MainLoop::setupCallbacks()
{
_callback_functions.push_back(
Callback(config_obj->current_scheme->POLL_INTERVAL,
checkThrottling_callback));
_callback_functions.push_back(
Callback(3000 ,checkEventTimeouts_callback));
_callback_functions.push_back(
Callback(config_obj->current_scheme->POLL_INTERVAL,
adjustSpeeds_callback));
updateCallbacks();
}
void MainLoop::updateCallbacks()
{
for (std::list<Callback>::iterator it = _callback_functions.begin();
it != _callback_functions.end(); ++it) {
if (g_main_context_find_source_by_id(NULL, it->id) == NULL) {
it->id = g_timeout_add(it->interval, it->function, NULL);
}
}
}
int MainLoop::addCallback(int interval, gboolean (*function)(gpointer data))
{
return g_timeout_add(interval, function, NULL);
}
|