File: main_loop.h

package info (click to toggle)
powersave 0.14.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,764 kB
  • ctags: 999
  • sloc: sh: 11,357; cpp: 8,103; ansic: 2,631; makefile: 388
file content (195 lines) | stat: -rw-r--r-- 6,388 bytes parent folder | download
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
/***************************************************************************
 *                                                                         *
 *                         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                  *
 *                                                                         *
 ***************************************************************************/

#ifndef POWERSAVE_MAIN_LOOP_H
#define POWERSAVE_MAIN_LOOP_H

#include <list>
#include "dbus_server.h"

class PM_interface;

/** @brief class representing a callback function inside the main loop
 *
 * Every callback has a unique id, an interval on which it is executed and
 * the callback function
*/
class Callback {
public:
	/** @brief contructor */
	Callback();

	/** @brief constructing a new callback object from the given values 
	 * 
	 * @param interval the interval on which the callback is executed
	 * @param function the callback function which will be called
	 */
	Callback(int interval, gboolean (*function)(gpointer data));

	/** @brief interval in which the callback function is executed */
	int interval;

	/** @brief the callback function */
	gboolean (*function)(gpointer data);

	/** @brief the id of the event source assigned by glib */
	int id;
};


/** @brief class implementing a main loop with glib
 *
 * Cares about callbacks and executes appropriate functions from
 * PM_Interface
 */
class MainLoop {
public:
	/** @brief constructor initializing the MainLoop object
	 *
	 * Constructor setting up the dbus connection and the gmain loop.
	 * For running the main loop, you have to call run().
	 */
	MainLoop();

	/** @brief runs the main loop
	 *
	 * Runs the main loop and blocks until quit() is called.
	 */
	void run();

	/** @brief quits the main loop */
	void quit();

 	/** @brief updates all callbacks within the main loop
	 *
	 * Updates all callbacks within the main loop. If a callback
	 * already exists, do nothing. This function should be called
	 * whenever there is a config/scheme/hardware change. It needs
	 * less ressources, so it is no problem to call it as often as
	 * needed.
	 */
	void updateCallbacks();

	/** @brief adds a callback to the main loop
	 *
	 * adds a callback function with the given interval to the gmain loop
	 *
	 * @param interval interval on which the function should be called
	 * @param function the function pointer serving as callback
	 */
	int addCallback(int interval, gboolean (*function)(gpointer data));

protected:

private:
	/** @brief callback for events on acpi/apm event file
	 *
	 * Function is called whenever there is an event on the hw event
	 * filedescriptor. Defined static to be used as gmain loop
	 * callback
	 *
	 * @param io_channel the io channel used by glib
	 * @param io_condition conditions on what we listen
	 * @param data user data
	 *
	 * @return true on success, false otherwise. If false, the
	 * callback is not called again until readded
	 */
	static gboolean hwEvent_callback(GIOChannel *io_channel,
					 GIOCondition io_condition,
					 gpointer data);

	/** @brief callback for reconnection to the hwEvent channel
	 *
	 * Function is called successively when hw event channel broke
	 * away until it is back again.. Defined static to be used as
	 * gmain loop callback
	 *
	 * @param data user data
	 *
	 * @return true on success, false otherwise. If false, the
	 * callback is never called again
	 */
	static gboolean hwEvent_reconnect(gpointer data);

	/** @brief connects to the hw event file (apm/acpi)
	 *
	 * Function is called successively when hw event channel broke
	 * away. Defined static to be used inside static callback
	 * functions.
	 *
	 * @return true on success, false otherwise.
	 */
	static gboolean hwEvent_connect();

	/** @brief callback for updating the cpu state inside PM_interface
	 *
	 * calls pm->updateCPUState();
	 *
	 * @return true on success, false otherwise. If false, the
	 * callback is never called again.
	 */
	static gboolean checkThrottling_callback(gpointer data);

	/** @brief callback for checking pending powersave events
	 *
	 * calls pm->checkEventTimeouts();
	 *
	 * @param data user data
	 *
	 * @return true on success, false otherwise. If false, the
	 * callback is never called again.
	 */
	static gboolean checkEventTimeouts_callback(gpointer data);

	/** @brief callback for adjust cpu speed in case of cpufreq
	 * userspace
	 *
	 * calls CPUFreq::adjustSpeeds
	 *
	 * If cpu, function returns false is not called again
	 *
	 * @param data user data
	 *
	 * @return true on success, false otherwise. If false, the
	 * callback is never called again.
	 */
	static gboolean adjustSpeeds_callback(gpointer data);

	/** @brief adds callbacks to the main loop
	 *
	 * Adds the default callbacks to the main loop.
	 */
	void setupCallbacks();

	/** @brief gmain loop object */
	GMainLoop *_gmain;

	/** @brief list storing all added callbacks */
	std::list<Callback> _callback_functions;

	DBus_Server _dbus_server;
};

#endif // POWERSAVE_MAIN_LOOP_H