~ubuntu-branches/ubuntu/karmic/powersave/karmic

« back to all changes in this revision

Viewing changes to daemon/main_loop.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2006-01-13 21:38:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060113213852-lqnirx6tfj6q76jv
Tags: 0.11.2-1
* New upstream release.
* Removed patches shebang_fix.diff, awk_path_fix.diff and
  wttyhx_fixes.diff, all merged upstream.
* hal and dbus are now mandatory. Added the corresponding dependencies to
  debian/control.
* Added powersaved.postinst, dbus needs to be reloaded after powersaved has
  been installed. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                                                         *
 
3
 *                         Powersave Daemon                                *
 
4
 *                                                                         *
 
5
 *            Copyright (C) 2005 SUSE Linux Products GmbH                  *
 
6
 *                                                                         *
 
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.                                              *
 
11
 *                                                                         *
 
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.                                *
 
16
 *                                                                         *
 
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                  *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
#include "main_loop.h"
 
24
#include "powerlib.h"
 
25
#include "globals.h"
 
26
#include "cpufreq.h"
 
27
#include "cpufreq_management.h"
 
28
#include "event_management.h"
 
29
 
 
30
using namespace Powersave::Globals;
 
31
 
 
32
/**** class Callback ****/
 
33
Callback::Callback()
 
34
{
 
35
        id = -1;
 
36
        interval = -1;
 
37
        function = NULL;
 
38
}
 
39
 
 
40
Callback::Callback(int interval_, gboolean (*function_)(gpointer data))
 
41
{
 
42
        id = -1;
 
43
        interval = interval_;
 
44
        function = function_;
 
45
}
 
46
 
 
47
/**** class MainLoop ****/
 
48
 
 
49
MainLoop::MainLoop() {
 
50
        /* set up a new main loop with glib and the default context */
 
51
        _gmain = g_main_loop_new(NULL, FALSE);
 
52
 
 
53
        DBusConnection *dbus_connection = _dbus_server.openSystemConnection();
 
54
 
 
55
        if ( dbus_connection == NULL ) {
 
56
                pDebug(DBG_ERR,
 
57
                       "Error setting up connection to DBus system bus. Aborting...");
 
58
                exit(1);
 
59
        } else {
 
60
                dbus_connection_setup_with_g_main(dbus_connection, NULL);
 
61
        }
 
62
 
 
63
        if (!hwEvent_connect()) {
 
64
                addCallback(3000, (GSourceFunc) hwEvent_reconnect);
 
65
        }
 
66
 
 
67
        setupCallbacks();
 
68
}
 
69
 
 
70
void MainLoop::run()
 
71
{
 
72
        pDebug(DBG_INFO, "Starting main loop");
 
73
        g_main_loop_run(_gmain);
 
74
}
 
75
 
 
76
void MainLoop::quit()
 
77
{
 
78
        g_main_loop_quit(_gmain);
 
79
}
 
80
 
 
81
gboolean MainLoop::hwEvent_callback(GIOChannel *io_channel,
 
82
                                    GIOCondition io_condition,
 
83
                                    gpointer data)
 
84
{
 
85
        int io_channel_fd = g_io_channel_unix_get_fd(io_channel);
 
86
 
 
87
        if (io_condition & (G_IO_ERR | G_IO_HUP)) {
 
88
                pDebug(DBG_ERR, "hw event socket broke away trying to reconnect...");
 
89
 
 
90
                close(io_channel_fd);
 
91
 
 
92
                main_loop->addCallback(3000, (GSourceFunc) hwEvent_reconnect);
 
93
 
 
94
                return false;
 
95
        }
 
96
 
 
97
        pm->handleHWEventRequest(io_channel_fd);
 
98
 
 
99
        return true;
 
100
}
 
101
 
 
102
gboolean MainLoop::hwEvent_reconnect(gpointer data)
 
103
{
 
104
        if (hwEvent_connect()) {
 
105
                return false;
 
106
        }
 
107
        
 
108
        return true;
 
109
}
 
110
 
 
111
gboolean MainLoop::hwEvent_connect()
 
112
{
 
113
        int hwEvent_fd = -1;
 
114
        GIOChannel *hwEvent_channel;
 
115
        int hwEvent_watch = -1;
 
116
 
 
117
        /* set up the event interface. /proc/acpi/event or /dev/apm_bios */
 
118
        if ((hwEvent_fd = pm->openHWEventFD()) < 0) {
 
119
                return false;
 
120
        } else {
 
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);
 
125
                return true;
 
126
        }
 
127
}
 
128
 
 
129
gboolean MainLoop::checkThrottling_callback(gpointer data)
 
130
{
 
131
        pm->checkThrottling();
 
132
 
 
133
        return true;
 
134
}
 
135
 
 
136
gboolean MainLoop::checkEventTimeouts_callback(gpointer data)
 
137
{
 
138
        pm->_eM->checkEventTimeouts();
 
139
 
 
140
        return true;
 
141
}
 
142
 
 
143
gboolean MainLoop::adjustSpeeds_callback(gpointer data)
 
144
{
 
145
        if (config_obj->current_scheme->CPUFREQ_CONTROL == CPUFREQ_USERSPACE) {
 
146
                cpufreq->adjustSpeeds();
 
147
                return true;
 
148
        }
 
149
 
 
150
        return false;
 
151
}
 
152
 
 
153
void MainLoop::setupCallbacks()
 
154
{
 
155
        _callback_functions.push_back(
 
156
                Callback(config_obj->current_scheme->POLL_INTERVAL,
 
157
                         checkThrottling_callback));
 
158
 
 
159
        _callback_functions.push_back(
 
160
                Callback(3000 ,checkEventTimeouts_callback));
 
161
 
 
162
        _callback_functions.push_back(
 
163
                Callback(config_obj->current_scheme->POLL_INTERVAL,
 
164
                         adjustSpeeds_callback));
 
165
 
 
166
        updateCallbacks();
 
167
}
 
168
 
 
169
void MainLoop::updateCallbacks()
 
170
{
 
171
        for (std::list<Callback>::iterator it = _callback_functions.begin();
 
172
             it != _callback_functions.end(); ++it) {
 
173
 
 
174
                if (g_main_context_find_source_by_id(NULL, it->id) == NULL) {
 
175
                        it->id = g_timeout_add(it->interval, it->function, NULL);
 
176
                }
 
177
        }
 
178
}
 
179
 
 
180
int MainLoop::addCallback(int interval, gboolean (*function)(gpointer data))
 
181
{
 
182
        Callback cb(interval, function);
 
183
 
 
184
        cb.id = g_timeout_add(cb.interval, cb.function, NULL);
 
185
        _callback_functions.push_back(cb);
 
186
 
 
187
        return cb.id;
 
188
}