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

« back to all changes in this revision

Viewing changes to daemon/config_pm.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) 2004,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
 
1
23
#include "config_pm.h"
2
 
 
3
 
 
4
 
string PS_Config::scheme_dir="/etc/sysconfig/powersave/";
5
 
 
6
 
PS_Config::PS_Config(){
7
 
 
8
 
        pDebug (DBG_DEBUG, "Init power management interface!\n");
 
24
#include "config.h"
 
25
#include "stringutil.h"
 
26
#include <fstream>
 
27
#include <iostream>
 
28
#include <map>
 
29
 
 
30
string PS_Config::scheme_dir = CONFIG_DIR;
 
31
 
 
32
PS_Config::PS_Config()
 
33
{
 
34
 
 
35
        pDebug(DBG_DEBUG, "Init power management interface");
9
36
        /*****************************************
10
37
         Initialise variables will(should) be overridden in readConfFiles()
11
38
         upper case letters: are used in config files...
12
39
        */
13
 
        
14
 
        DYNAMIC_CPU_LOW_LIMIT = 25;
15
 
        CPU_IDLE_TIMEOUT = 0;
16
 
        CPU_HIGH_LIMIT = 75;
17
 
        JUMP_CPU_FREQ_MAX_LIMIT = 0;
 
40
 
 
41
        CPUFREQ_CONTROL = CPUFREQ_KERNEL;
 
42
 
 
43
        CPU_IDLE_LIMIT = 25;
 
44
        CPU_IDLE_TIMEOUT = 10;
 
45
        CPU_HIGH_LIMIT = 80;
 
46
        JUMP_CPU_FREQ_MAX_LIMIT = 50;
18
47
        CPUFREQ_HYSTERESIS = 5;
19
48
        CONSIDER_NICE = 0;
20
 
        POLL_INTERVAL = 2000;
21
 
 
22
 
        BAT_WARN_LIMIT = 15;
23
 
        BAT_LOW_LIMIT = 8;
24
 
        BAT_CRIT_LIMIT = 2;
25
 
        FORCE_BATTERY_POLLING = 0;
26
 
        BATTERY_POLLING_MULTIPLICATOR = 5;
27
 
 
28
 
        ENABLE_THERMAL_MANAGEMENT = KERNEL;
29
 
 
30
 
        PM_GROUP = "powersave";
 
49
        POLL_INTERVAL = 333;
 
50
        CPUFREQ_ENABLED = true;
 
51
 
 
52
        BAT_WARN_LIMIT = 15;
 
53
        BAT_LOW_LIMIT = 8;
 
54
        BAT_CRIT_LIMIT = 2;
 
55
 
 
56
        ENABLE_THERMAL_MANAGEMENT = KERNEL_PASSIVE;
 
57
 
 
58
        DISPLAY_BRIGHTNESS = -1;        // -1 == off / don't care
 
59
 
31
60
        USER_STANDBY_DISABLED = 1;
32
 
        USER_SUSPEND2DISK_DISABLED = 1;
 
61
        USER_SUSPEND2DISK_DISABLED = 0;
33
62
        USER_SUSPEND2RAM_DISABLED = 1;
 
63
        DPM_ENABLED = 1;
34
64
        POWER_BUTTON_DELAY = 0;
35
65
 
36
66
        SUSPEND2DISK_DELAY = 0;
37
67
        SUSPEND2RAM_DELAY = 0;
38
68
        STANDBY_DELAY = 0;
39
69
 
40
 
        ALLOW_THROTTLING = 0;
 
70
        ALLOW_THROTTLING = false;
41
71
        MAX_CPU_THROTTLING = 0;
42
 
        ALWAYS_THROTTLE = 0;
 
72
        ALWAYS_THROTTLE = false;
43
73
        CPUFREQUENCY = _DYNAMIC;
44
 
        USER_INPUT_TIMEOUT1 = 300;
45
 
        USER_INPUT_TIMEOUT2 = 1800;
46
74
        COOLING_POLICY = COOLING_MODE_ACTIVE;
47
 
        DISABLE_DISPLAY_SETTINGS = 0;
48
 
        DISABLE_SCREEN_SAVER = 0;
49
 
        DISABLE_DPMS = 0;
50
 
        DPMS_STANDBY = 10;
51
 
        DPMS_SUSPEND = 20;
52
 
        DPMS_OFF = 30;
53
75
        SCHEME_NAME = "Default";
54
 
        
 
76
 
 
77
        AC_SCHEME = "performance";
 
78
        BATTERY_SCHEME = "powersave";
 
79
 
55
80
        /*****************************************/
56
81
 
57
 
 
58
82
        /* Initialise temperature structures with BIOS/kernel values */
59
83
        int x, ret = 0;
60
84
        for (x = 0; x < MAX_THERMAL_ZONES; x++)
61
85
                thermal_zones[x].present = 0;
62
 
        for (x = 0, ret = 1; x < MAX_THERMAL_ZONES; x++){
 
86
        for (x = 0, ret = 1; x < MAX_THERMAL_ZONES; x++) {
63
87
                ret = getThermalZone(x, &thermal_zones[x]);
64
 
                if (ret < 0){
 
88
                if (ret < 0) {
65
89
                        thermal_zones[x].present = 0;
66
90
                        break;
67
91
                }
69
93
        }
70
94
}
71
95
 
72
 
PS_Config::~PS_Config(){
 
96
PS_Config::~PS_Config()
 
97
{
73
98
 
74
99
};
75
100
 
76
 
int PS_Config::readConfigFile(string file){
77
 
 
78
 
        FILE *fp;
79
 
        char buf[MAX_LINE_SIZE + 1] = "";
80
 
 
81
 
        int line = 0;
82
 
        char COMMENT = '#';
 
101
int PS_Config::readConfigFile(const std::string & file)
 
102
{
 
103
 
 
104
        int lineno = 0;
 
105
        std::string line;
 
106
 
 
107
        std::ifstream input(file.c_str());
 
108
        if (!input)
 
109
                return -1;
 
110
 
83
111
        EventIter i;
84
 
        int int_val;
85
 
        char *endptr[MAX_LINE_SIZE + 1];
86
 
        char _event[MAX_LINE_SIZE + 1] = "", _value[MAX_LINE_SIZE + 1] = "";
87
 
 
88
 
        fp = fopen(file.c_str(), "r");
89
 
        if (!fp)
90
 
                return -1;
91
 
 
92
 
        while (fgets(buf, sizeof(buf)-1, fp) != NULL) {
93
 
                line++;
94
 
 
95
 
                int x = 0;
96
 
                for (; buf[x] == ' ' ; x++);
97
 
                if (buf[x] == COMMENT || buf[x] == '\n' || buf[0] == '\0')
98
 
                        continue;
99
 
 
100
 
                //parse = string(buf);
101
 
                // hack to also allow quoted variable assignements
102
 
                if (sscanf(buf, "%[^=]=\"%[^\"]", _event, _value) < 2){
103
 
                        pDebug(DBG_DIAG, "in config file %s in line %d or empty value", 
104
 
                               file.c_str(), line);
105
 
                        continue;
106
 
                }
107
 
                if (!strncmp(_event, "POWERSAVE_EVENT_", 16)){
108
 
                        /* convert e.g. POWERSAVE_EVENT_TEMPERATURE_HOT to 
 
112
 
 
113
        while (getline(input, line)) {
 
114
                lineno++;
 
115
                line = stripTrailingWS(stripLeadingWS(stripLineComment(line)));
 
116
                if (line == "")
 
117
                        continue;
 
118
 
 
119
                std::string::size_type pos = line.find('=');
 
120
                if (pos == std::string::npos) {
 
121
                        pDebug(DBG_DIAG, "no = found in file %s line %d", file.c_str(), lineno);
 
122
                        continue;
 
123
                }
 
124
 
 
125
                std::string key = stripTrailingWS(line.substr(0, pos));
 
126
                std::string value = stripLeadingWS(line.substr(pos + 1));
 
127
                value = stripApostrophe(value);
 
128
 
 
129
                if (key == "") {        // this is a seriously broken config file
 
130
                        pDebug(DBG_WARN, "invalid line %d in file %s", lineno, file.c_str());
 
131
                        continue;
 
132
                }
 
133
 
 
134
                if (value == "") {
 
135
                        pDebug(DBG_INFO, "Empty value in file %s line %d, key '%s'", file.c_str(), lineno, key.c_str());
 
136
                        continue;
 
137
                }
 
138
                // Comment out this line to see all read config values:
 
139
                // printf ("Read value: %s - key: %s\n", value.c_str(), key.c_str());
 
140
#if 1
 
141
                /* We are going to shorten and de-obfuscate the configuration variables.
 
142
                   The POWERSAVE_/POWERSAVED_-prefixes are going away. Some variables
 
143
                   will be renamed:
 
144
                   POWERSAVE_EVENT_PROCESSOR_DYNAMIC_HIGH => EVENT_PROCESSOR_BUSY
 
145
                   POWERSAVE_EVENT_PROCESSOR_DYNAMIC_LOW  => EVENT_PROCESSOR_IDLE
 
146
                   POWERSAVED_DYNAMIC_LOW_CPU_LIMIT       => CPU_IDLE_LIMIT
 
147
                   the event stuff is handled at other places.
 
148
                   For a smooth transition, both config versions are accepted for a
 
149
                   limited time. This is why this ugly hack is here
 
150
                 */
 
151
                // renamed variables
 
152
                if (key == "POWERSAVE_EVENT_PROCESSOR_DYNAMIC_HIGH")
 
153
                        key = "EVENT_PROCESSOR_BUSY";
 
154
                if (key == "POWERSAVE_EVENT_PROCESSOR_DYNAMIC_LOW")
 
155
                        key = "EVENT_PROCESSOR_IDLE";
 
156
                if (key == "POWERSAVED_DYNAMIC_LOW_CPU_LIMIT")
 
157
                        key = "PROCESSOR_IDLE_LIMIT";
 
158
 
 
159
                // remove leading POWERSAVE_
 
160
                if (key.find("POWERSAVE_") == 0)
 
161
                        key = key.substr(10);
 
162
                // remove leading POWERSAVED_
 
163
                if (key.find("POWERSAVED_") == 0)
 
164
                        key = key.substr(11);
 
165
#endif
 
166
 
 
167
                if (key.find("EVENT_") == 0) {
 
168
                        /* convert e.g. EVENT_TEMPERATURE_HOT to 
109
169
                           temperature.hot to search for the event object */
110
 
                        int y = 16;
111
 
                        for (; _event[y] != '\0' && y < MAX_LINE_SIZE; y++){
112
 
                                if (_event[y] == '_')
113
 
                                        _event[y-16] = '.';
114
 
                                else
115
 
                                        _event[y-16] = tolower(_event[y]);
116
 
                        }
117
 
                        _event[y-16] = '\0';
118
 
                        i = eventMap.find(string(_event));
119
 
                        if (i != eventMap.end()){
120
 
                                i->second.setActions2Execute(_value);
121
 
                        }
122
 
                        else{
123
 
                                pDebug(DBG_WARN, "Found actions variable for non existing event: %s", _event);
124
 
                        }
125
 
                }
126
 
                else if (!strcmp(_event, "POWERSAVED_CPU_HIGH_LIMIT")){
127
 
                        if (_value[0] == '\0')
128
 
                                continue;
129
 
                        int_val = strtol(_value, endptr, 10);
130
 
                        if (**endptr != '\0'){
131
 
                                strtointErr(_value, line, file.c_str());
132
 
                                continue;
133
 
                        }
134
 
                        if (int_val < 0 || int_val > 100)
135
 
                                pDebug(DBG_ERR, "POWERSAVED_CPU_HIGH_LIMIT must between 0 and 100. Default value used.")
136
 
                                else
137
 
                                        CPU_HIGH_LIMIT = int_val;
138
 
                }
139
 
                else if (!strcmp(_event, "POWERSAVED_BATTERY_WARNING")){
140
 
                        if (_value[0] == '\0')
141
 
                                continue;
142
 
                        int_val = strtol(_value, endptr, 10);
143
 
                        if (**endptr != '\0'){
144
 
                                strtointErr(_value, line, file.c_str());
145
 
                                continue;
146
 
                        }
147
 
                        if (int_val < 0 || int_val > 100)
148
 
                                pDebug(DBG_ERR, "POWERSAVED_BATTERY_WARNING must between 0 and 100. Default value used.")
149
 
                                else
150
 
                                        BAT_WARN_LIMIT = int_val;
151
 
                }
152
 
                else if (!strcmp(_event, "POWERSAVED_BATTERY_LOW")){
153
 
                        if (_value[0] == '\0')
154
 
                                continue;
155
 
                        int_val = strtol(_value, endptr, 10);
156
 
                        if (**endptr != '\0'){
157
 
                                strtointErr(_value, line, file.c_str());
158
 
                                continue;
159
 
                        }
160
 
                        if (int_val < 0 || int_val > 100)
161
 
                                pDebug(DBG_ERR, "POWERSAVED_BATTERY_LOW must between 0 and 100. Default value used.")
162
 
                                else
163
 
                                        BAT_LOW_LIMIT = int_val;
164
 
                }
165
 
                else if (!strcmp(_event, "POWERSAVED_BATTERY_CRITICAL")){
166
 
                        if (_value[0] == '\0')
167
 
                                continue;
168
 
                        int_val = strtol(_value, endptr, 10);
169
 
                        if (**endptr != '\0'){
170
 
                                strtointErr(_value, line, file.c_str());
171
 
                                continue;
172
 
                        }
173
 
                        BAT_CRIT_LIMIT = int_val;
174
 
                }
175
 
                else if (!strcmp(_event, "POWERSAVED_CPU_IDLE_TIMEOUT")){
176
 
                        if (_value[0] == '\0')
177
 
                                continue;
178
 
                        int_val = strtol(_value, endptr, 10);
179
 
                        if (**endptr != '\0'){
180
 
                                strtointErr(_value, line, file.c_str());
181
 
                                continue;
182
 
                        }
183
 
                        if (int_val < 0)
184
 
                                pDebug(DBG_ERR, "POWERSAVED_CPU_IDLE_TIMEOUT must greater than 0. Default value used.")
185
 
                                else
186
 
                                        CPU_IDLE_TIMEOUT = int_val * 1000;
187
 
                }
188
 
                else if (!strcmp(_event, "POWERSAVED_BATTERY_POLLING_MULTIPLICATOR")){
189
 
                        if (_value[0] == '\0')
190
 
                                continue;
191
 
                        int_val = strtol(_value, endptr, 10);
192
 
                        if (**endptr != '\0'){
193
 
                                strtointErr(_value, line, file.c_str());
194
 
                                continue;
195
 
                        }
196
 
                        BATTERY_POLLING_MULTIPLICATOR = int_val;
197
 
                }
198
 
                else if (!strcmp(_event, "POWERSAVED_POLLING_INTERVAL")){
199
 
                        if (_value[0] == '\0')
200
 
                                continue;
201
 
                        int_val = strtol(_value, endptr, 10);
202
 
                        if (**endptr != '\0'){
203
 
                                strtointErr(_value, line, file.c_str());
204
 
                                continue;
205
 
                        }
206
 
                        POLL_INTERVAL = int_val;
207
 
                }
208
 
                else if (!strcmp(_event, "POWERSAVED_DYNAMIC_LOW_CPU_LIMIT")){
209
 
                        if (_value[0] == '\0')
210
 
                                continue;
211
 
                        int_val = strtol(_value, endptr, 10);
212
 
                        if (**endptr != '\0'){
213
 
                                strtointErr(_value, line, file.c_str());
214
 
                                continue;
215
 
                        }
216
 
                        if (int_val < 0 || int_val > 100)
217
 
                                pDebug(DBG_ERR, "POWERSAVED_DYNAMIC_LOW_CPU_LIMIT must between 0 and 100. Default value used.")
218
 
                                else
219
 
                                       DYNAMIC_CPU_LOW_LIMIT = int_val;
220
 
                }
221
 
                else if (!strcmp(_event, "POWERSAVED_POWERBTN_DELAY")){
222
 
                        if (_value[0] == '\0')
223
 
                                continue;
224
 
                        int_val = strtol(_value, endptr, 10);
225
 
                        if (**endptr != '\0'){
226
 
                                strtointErr(_value, line, file.c_str());
227
 
                                continue;
228
 
                        }
229
 
                        POWER_BUTTON_DELAY = int_val;
230
 
                }
231
 
                else if (!strcmp(_event, "POWERSAVED_PM_GROUP")){
232
 
                        PM_GROUP = _value;
233
 
                }
234
 
                else if (!strcmp(_event, "POWERSAVED_FORCE_BATTERY_POLLING")){
235
 
                        if(!strncasecmp(_value,"yes", 3))
236
 
                                FORCE_BATTERY_POLLING = 1;
237
 
                        else
238
 
                                FORCE_BATTERY_POLLING = 0;
239
 
                }
240
 
                else if (!strcmp(_event, "POWERSAVED_DISABLE_USER_SUSPEND2DISK")){
241
 
                        if(!strncasecmp(_value,"yes", 3))
242
 
                                USER_SUSPEND2DISK_DISABLED = 1;
243
 
                        else
244
 
                                USER_SUSPEND2DISK_DISABLED = 0;
245
 
                }
246
 
                else if (!strcmp(_event, "POWERSAVED_DISABLE_USER_SUSPEND2RAM")){
247
 
                        if(!strncasecmp(_value,"yes", 3))
248
 
                                USER_SUSPEND2RAM_DISABLED = 1;
249
 
                        else
250
 
                                USER_SUSPEND2RAM_DISABLED = 0;
251
 
                }
252
 
                else if (!strcmp(_event, "POWERSAVED_DISABLE_USER_STANDBY")){
253
 
                        if(!strncasecmp(_value,"yes", 3))
254
 
                                USER_STANDBY_DISABLED = 1;
255
 
                        else
256
 
                                USER_STANDBY_DISABLED = 0;
257
 
                }
258
 
                else if (!strcmp(_event, "POWERSAVED_CONSIDER_NICE")){
259
 
                        if(!strncasecmp(_value,"yes", 3))
260
 
                                CONSIDER_NICE = 1;
261
 
                        else
262
 
                                CONSIDER_NICE = 0;
263
 
                }
264
 
                else if (!strcmp(_event, "POWERSAVED_JUMP_CPU_FREQ_MAX_LIMIT")){
265
 
                        int_val = atoi(_value);
266
 
                        if (int_val > 0 && int_val < 99)
267
 
                                JUMP_CPU_FREQ_MAX_LIMIT = int_val;
268
 
                }
269
 
                else if (!strcmp(_event, "POWERSAVED_CPU_HYSTERESIS")){
270
 
                        int_val = atoi(_value);
271
 
                        if (int_val > 0 && int_val < 99)
272
 
                                CPUFREQ_HYSTERESIS = int_val;
273
 
                }
274
 
                else if (!strcmp(_event, "POWERSAVE_ENABLE_THERMAL_MANAGEMENT")){
275
 
                        if(!strncasecmp(_value,"kernel", 6))
276
 
                                ENABLE_THERMAL_MANAGEMENT = KERNEL;
277
 
                        /*
278
 
                          else if(!strncasecmp(_value,"userspace", 9))
279
 
                          ENABLE_THERMAL_MANAGEMENT = USERSPACE;
280
 
                          else if(!strncasecmp(_value,"both", 4))
281
 
                          ENABLE_THERMAL_MANAGEMENT = BOTH;
282
 
                        */
283
 
                        else
284
 
                                ENABLE_THERMAL_MANAGEMENT = OFF;
285
 
                }
286
 
                else if (!strcmp(_event, "POWERSAVE_SUSPEND2DISK_DELAY")){
287
 
                        if (_value[0] == '\0')
288
 
                                continue;
289
 
                        int_val = strtol(_value, endptr, 10);
290
 
                        if (**endptr != '\0'){
291
 
                                strtointErr(_value, line, file.c_str());
292
 
                                continue;
293
 
                        }
294
 
                        SUSPEND2DISK_DELAY = int_val;
295
 
                }
296
 
                else if (!strcmp(_event, "POWERSAVE_SUSPEND2RAM_DELAY")){
297
 
                        if (_value[0] == '\0')
298
 
                                continue;
299
 
                        int_val = strtol(_value, endptr, 10);
300
 
                        if (**endptr != '\0'){
301
 
                                strtointErr(_value, line, file.c_str());
302
 
                                continue;
303
 
                        }
304
 
                        SUSPEND2RAM_DELAY = int_val;
305
 
                }
306
 
                else if (!strcmp(_event, "POWERSAVE_STANDBY_DELAY")){
307
 
                        if (_value[0] == '\0')
308
 
                                continue;
309
 
                        int_val = strtol(_value, endptr, 10);
310
 
                        if (**endptr != '\0'){
311
 
                                strtointErr(_value, line, file.c_str());
312
 
                                continue;
313
 
                        }
314
 
                        STANDBY_DELAY = int_val;
315
 
                }
316
 
                else if (!strcmp(_event, "POWERSAVE_AC_SCHEME"))
317
 
                        AC_SCHEME=_value;
318
 
                else if (!strcmp(_event, "POWERSAVE_BATTERY_SCHEME"))
319
 
                        BATTERY_SCHEME=_value;
320
 
                else if (!strcmp(_event, "POWERSAVE_MAX_THROTTLING")){
321
 
                        if (_value[0] == '\0')
322
 
                                continue;
323
 
                        int_val = strtol(_value, endptr, 10);
324
 
                        if (**endptr != '\0'){
325
 
                                strtointErr(_value, line, file.c_str());
326
 
                                continue;
327
 
                        }
328
 
                        if (int_val < 0 || int_val > 100)
329
 
                                pDebug(DBG_ERR, "POWERSAVED_CPU_HIGH_LIMIT must between 0 and 100."
330
 
                                       " Default value used.")
331
 
                                else
332
 
                                        MAX_CPU_THROTTLING = int_val;
333
 
                }
334
 
                else if (!strcmp(_event, "POWERSAVE_ALLOW_THROTTLING")){
335
 
                        if(!strncasecmp(_value,"yes", 3))
336
 
                                ALLOW_THROTTLING = 1;
337
 
                        else
338
 
                                ALLOW_THROTTLING = 0;
339
 
                }
340
 
                else if (!strcmp(_event, "POWERSAVE_ALWAYS_THROTTLE")){
341
 
                        if(!strncasecmp(_value,"yes", 3))
342
 
                                ALWAYS_THROTTLE = 1;
343
 
                        else
344
 
                                ALWAYS_THROTTLE = 0;
345
 
                }
346
 
                else if (!strcmp(_event, "POWERSAVE_SCHEME_NAME")){
347
 
                        SCHEME_NAME = _value;
348
 
                }
349
 
                else if (!strcmp(_event, "POWERSAVE_CPUFREQUENCY")){
350
 
                        if(!strncasecmp(_value,"performance", 11))
351
 
                                CPUFREQUENCY = _PERFORMANCE;
352
 
                        else if(!strncasecmp(_value,"dynamic", 7))
353
 
                                CPUFREQUENCY = _DYNAMIC;
354
 
                        else if(!strncasecmp(_value,"powersave", 9))
355
 
                                CPUFREQUENCY = _POWERSAVE;
356
 
                        else
357
 
                                pDebug(DBG_WARN, "Error in line %d of config file %s."
358
 
                                       " CPU frequency mode %s does not exits.\n", line, file.c_str(), _value);
359
 
                }
360
 
                else if (!strcmp(_event, "POWERSAVE_COOLING_POLICY")){
361
 
                        if(!strncasecmp(_value,"active", 6))
362
 
                                COOLING_POLICY = COOLING_MODE_ACTIVE;
363
 
                        if(!strncasecmp(_value,"passive", 7))
364
 
                                COOLING_POLICY = COOLING_MODE_PASSIVE;
365
 
                }
366
 
                else if (!strcmp(_event, "POWERSAVE_DISABLE_SCREEN_SAVER")){
367
 
                        if(!strncasecmp(_value,"yes", 3))
368
 
                                DISABLE_SCREEN_SAVER = 1;
369
 
                        else
370
 
                                DISABLE_SCREEN_SAVER = 0;
371
 
                }
372
 
                else if (!strcmp(_event, "POWERSAVE_DISABLE_DISPLAY_SETTINGS")){
373
 
                        if(!strncasecmp(_value,"yes", 3))
374
 
                                DISABLE_DISPLAY_SETTINGS = 1;
375
 
                        else
376
 
                                DISABLE_DISPLAY_SETTINGS = 0;
377
 
                }
378
 
                else if (!strcmp(_event, "POWERSAVE_DISABLE_DPMS")){
379
 
                        if(!strncasecmp(_value,"yes", 3))
380
 
                                DISABLE_DPMS = 1;
381
 
                        else
382
 
                                DISABLE_DPMS = 0;
383
 
                }
384
 
                else if (!strcmp(_event, "POWERSAVE_DPMS_STANDBY")){
385
 
                        if (_value[0] == '\0')
386
 
                                continue;
387
 
                        int_val = strtol(_value, endptr, 10);
388
 
                        if (**endptr != '\0'){
389
 
                                strtointErr(_value, line, file.c_str());
390
 
                                continue;
391
 
                        }
392
 
                        if ((int_val < 0) || (int_val > 500))
393
 
                                pDebug(DBG_ERR, "POWERSAVE_DPMS_STANDBY must between 0 and 500."
394
 
                                       " Default value %d used.", DPMS_STANDBY)
395
 
                                else
396
 
                                        DPMS_STANDBY = int_val;
397
 
                }
398
 
                else if (!strcmp(_event, "POWERSAVE_DPMS_SUSPEND")){
399
 
                        if (_value[0] == '\0')
400
 
                                continue;
401
 
                        int_val = strtol(_value, endptr, 10);
402
 
                        if (**endptr != '\0'){
403
 
                                strtointErr(_value, line, file.c_str());
404
 
                                continue;
405
 
                        }
406
 
                        if ((int_val < 0) || (int_val > 500))
407
 
                                pDebug(DBG_ERR, "POWERSAVE_DPMS_SUSPEND must between 0 and 500."
408
 
                                       " Default value %d used.", DPMS_SUSPEND)
409
 
                                else
410
 
                                        DPMS_SUSPEND = int_val;
411
 
                }
412
 
                else if (!strcmp(_event, "POWERSAVE_DPMS_OFF")){
413
 
                        if (_value[0] == '\0')
414
 
                                continue;
415
 
                        int_val = strtol(_value, endptr, 10);
416
 
                        if (**endptr != '\0'){
417
 
                                strtointErr(_value, line, file.c_str());
418
 
                                continue;
419
 
                        }
420
 
                        if ((int_val < 0) || (int_val > 500))
421
 
                                pDebug(DBG_ERR, "POWERSAVE_DPMS_OFF must between 0 and 500."
422
 
                                       " Default value %d used.", DPMS_OFF)
423
 
                                else
424
 
                                        DPMS_OFF = int_val;
425
 
                }
426
 
                else if (!strncmp(_event, "POWERSAVE_THERMAL_CRITICAL", 26)){
427
 
                        int x = -1;
428
 
                        int  error = 0;
429
 
                        int_val = strtol(_value, endptr, 10);
430
 
                        if (strlen(_event) == 28 && !(int_val < 0) && !(int_val > 200)){
431
 
                                x = _event[27]-'0';
432
 
                                if (x < 0 || x > MAX_THERMAL_ZONES)
433
 
                                        error = 1;
434
 
                        }
435
 
                        else
436
 
                                error = 1;
437
 
                        if (error || thermal_zones[x].present == 0)
438
 
                                error = 1;
439
 
                        else{
440
 
                                /* value of 0 -> do not override BIOS settings */
441
 
                                if (int_val > 0)
442
 
                                        thermal_zones[x].critical = int_val;
443
 
                        }
444
 
                        if (error)
445
 
                                pDebug (DBG_WARN, "Wrong syntax or not existing thermal_zone(%d),"
446
 
                                        " in line %d of config file %s",
447
 
                                        x, line, file.c_str());
448
 
                }
449
 
                else if (!strncmp(_event, "POWERSAVE_THERMAL_HOT", 21)){
450
 
                        int x = -1;
451
 
                        int  error = 0;
452
 
                        int_val = strtol(_value, endptr, 10);
453
 
                        if (strlen(_event) == 23 && !(int_val < 0) && !(int_val > 200)){
454
 
                                x = _event[22]-'0';
455
 
                                if (x < 0 || x > MAX_THERMAL_ZONES)
456
 
                                        error = 1;
457
 
                        }
458
 
                        else
459
 
                                error = 1;
460
 
                        if (error || thermal_zones[x].present == 0)
461
 
                                error = 1;
462
 
                        else{
463
 
                                /* value of 0 -> do not override BIOS settings */
464
 
                                if (int_val > 0)
465
 
                                        thermal_zones[x].hot = int_val;
466
 
                        }
467
 
                        if (error)
468
 
                                pDebug (DBG_WARN, "Wrong syntax or not existing thermal_zone(%d),"
469
 
                                        " in line %d of config file %s",
470
 
                                        x, line, file.c_str());
471
 
                }
472
 
                else if (!strncmp(_event, "POWERSAVE_THERMAL_PASSIVE", 25)){
473
 
                        int x = -1;
474
 
                        int  error = 0;
475
 
                        int_val = strtol(_value, endptr, 10);
476
 
                        if (strlen(_event) == 27 && !(int_val < 0) && !(int_val > 200)){
477
 
                                x = _event[26]-'0';
478
 
                                if (x < 0 || x > MAX_THERMAL_ZONES || thermal_zones[x].present == 0)
479
 
                                        error = 1;
480
 
                        }
481
 
                        else
482
 
                                error = 1;
483
 
                                /* value of 0 -> do not override BIOS settings */
484
 
                        if (int_val > 0 && !error)
485
 
                            thermal_zones[x].passive = int_val;
486
 
                        if (error)
487
 
                                pDebug (DBG_WARN, "Wrong syntax or not existing thermal_zone(%d),"
488
 
                                        " in line %d of config file %s",
489
 
                                        x, line, file.c_str());
490
 
                }
491
 
                /* format: POWERSAVE_THERMAL_ACTIVE_1_2="45"
 
170
                        key = lowercase(key.substr(6));
 
171
 
 
172
                        while ((pos = key.find("_")) != string::npos)
 
173
                                key[pos] = '.';
 
174
 
 
175
                        i = eventMap.find(key);
 
176
                        if (i != eventMap.end()) {
 
177
                                list< string > actions;
 
178
                                splitLine(value, actions);
 
179
                                i->second.setActions2Execute(actions);
 
180
                        } else {
 
181
                                pDebug(DBG_WARN, "Found actions variable for non existing event: '%s'", key.c_str());
 
182
                        }
 
183
                } else
 
184
                        data[key] = value;
 
185
        }
 
186
        return 0;
 
187
}
 
188
 
 
189
void PS_Config::assignConfigEntries()
 
190
{
 
191
 
 
192
        std::string s, t;
 
193
        int i;
 
194
 
 
195
        CPU_HIGH_LIMIT = checkValue(CPU_HIGH_LIMIT, "CPU_HIGH_LIMIT", 0, 100);
 
196
        BAT_WARN_LIMIT = checkValue(BAT_WARN_LIMIT, "BATTERY_WARNING", 0, 100);
 
197
        BAT_LOW_LIMIT = checkValue(BAT_LOW_LIMIT, "BATTERY_LOW", 0, 100);
 
198
        BAT_CRIT_LIMIT = checkValue(BAT_CRIT_LIMIT, "BATTERY_CRITICAL", 0, 100);
 
199
 
 
200
        CPU_IDLE_TIMEOUT = checkValue(CPU_IDLE_TIMEOUT, "CPU_IDLE_TIMEOUT", 0);
 
201
 
 
202
        POLL_INTERVAL = checkValue(POLL_INTERVAL, "POLLING_INTERVAL", 0);
 
203
 
 
204
        CPU_IDLE_LIMIT = checkValue(CPU_IDLE_LIMIT, "CPU_IDLE_LIMIT", 0, 100);
 
205
        POWER_BUTTON_DELAY = checkValue(POWER_BUTTON_DELAY, "POWERBTN_DELAY", 0);
 
206
 
 
207
        USER_SUSPEND2DISK_DISABLED = checkYes(USER_SUSPEND2DISK_DISABLED, "DISABLE_USER_SUSPEND2DISK");
 
208
        USER_SUSPEND2RAM_DISABLED = checkYes(USER_SUSPEND2RAM_DISABLED, "DISABLE_USER_SUSPEND2RAM");
 
209
        USER_STANDBY_DISABLED = checkYes(USER_STANDBY_DISABLED, "DISABLE_USER_STANDBY");
 
210
        DPM_ENABLED = checkYes(DPM_ENABLED, "ENABLE_DPM");
 
211
 
 
212
        CONSIDER_NICE = checkYes(CONSIDER_NICE, "CONSIDER_NICE");
 
213
 
 
214
        JUMP_CPU_FREQ_MAX_LIMIT = checkValue(JUMP_CPU_FREQ_MAX_LIMIT, "JUMP_CPU_FREQ_MAX_LIMIT", 0, 99);
 
215
 
 
216
        CPUFREQ_HYSTERESIS = checkValue(CPUFREQ_HYSTERESIS, "CPU_HYSTERESIS", 0, 99);
 
217
 
 
218
        s = data["ENABLE_THERMAL_MANAGEMENT"];
 
219
        if (s == "" || s == "kernel_passive") {
 
220
                // this is the default, even with s == ""
 
221
                ENABLE_THERMAL_MANAGEMENT = KERNEL_PASSIVE;
 
222
        } else if (s == "off") {
 
223
                ENABLE_THERMAL_MANAGEMENT = OFF;
 
224
        } else if (s == "kernel") {
 
225
                ENABLE_THERMAL_MANAGEMENT = KERNEL;
 
226
        } else if (s == "both" || s == "userspace") {
 
227
                pDebug(DBG_WARN, "Userspace/both thermal management mode not supported yet");
 
228
                ENABLE_THERMAL_MANAGEMENT = OFF;
 
229
        } else {
 
230
                pDebug(DBG_WARN, "Wrong value for  ENABLE_THERMAL_MANAGEMENT: %s", s.c_str());
 
231
        }
 
232
        /*
 
233
           else if (s == "userspace")
 
234
           ENABLE_THERMAL_MANAGEMENT = USERSPACE;
 
235
           else if (s == "both")
 
236
           ENABLE_THERMAL_MANAGEMENT = BOTH;
 
237
         */
 
238
 
 
239
        SUSPEND2DISK_DELAY = checkValue(SUSPEND2DISK_DELAY, "SUSPEND2DISK_DELAY", 0);
 
240
        SUSPEND2RAM_DELAY = checkValue(SUSPEND2RAM_DELAY, "SUSPEND2RAM_DELAY", 0);
 
241
        STANDBY_DELAY = checkValue(STANDBY_DELAY, "STANDBY_DELAY", 0);
 
242
 
 
243
        s = data["AC_SCHEME"];
 
244
        if (s != "")
 
245
                AC_SCHEME = s.c_str();
 
246
 
 
247
        s = data["BATTERY_SCHEME"];
 
248
        if (s != "")
 
249
                BATTERY_SCHEME = s.c_str();
 
250
 
 
251
        s = data["CPUFREQ_ENABLED"];
 
252
        if (s == "yes")
 
253
                CPUFREQ_ENABLED = true;
 
254
        else if (s == "no")
 
255
                CPUFREQ_ENABLED = false;
 
256
        else if ( s != "" )
 
257
                pDebug(DBG_WARN, "wrong setting for CPUFREQ_ENABLED in cpufreq config file '%s'",
 
258
                       s.c_str());
 
259
                
 
260
        MAX_CPU_THROTTLING = checkValue(MAX_CPU_THROTTLING, "MAX_THROTTLING", 0, 100);
 
261
 
 
262
        ALLOW_THROTTLING = checkYes(ALLOW_THROTTLING, "ALLOW_THROTTLING");
 
263
        ALWAYS_THROTTLE = checkYes(ALWAYS_THROTTLE, "ALWAYS_THROTTLE");
 
264
 
 
265
        s = data["SCHEME_NAME"];
 
266
        if (s != "")
 
267
                SCHEME_NAME = s.c_str();
 
268
 
 
269
        s = data["SCHEME_DESCRIPTION"];
 
270
        if (s != "")
 
271
                SCHEME_DESCRIPTION = s.c_str();
 
272
 
 
273
        s = data["CPUFREQUENCY"];
 
274
        if (s == "performance")
 
275
                CPUFREQUENCY = _PERFORMANCE;
 
276
        else if (s == "dynamic")
 
277
                CPUFREQUENCY = _DYNAMIC;
 
278
        else if (s == "powersave")
 
279
                CPUFREQUENCY = _POWERSAVE;
 
280
        else if (s != "")
 
281
                pDebug(DBG_WARN, "cpufreq mode '%s' does not exist.", s.c_str());
 
282
 
 
283
        s = data["CPUFREQ_CONTROL"];
 
284
        if (s == "userspace")
 
285
                CPUFREQ_CONTROL = CPUFREQ_USERSPACE;
 
286
        else if (s == "kernel")
 
287
                CPUFREQ_CONTROL = CPUFREQ_KERNEL;
 
288
        else if (s != "")
 
289
                pDebug(DBG_ERR, "cpufreq control '%s' does not exist.", s.c_str());
 
290
 
 
291
        s = data["COOLING_POLICY"];
 
292
        if (s == "active")
 
293
                COOLING_POLICY = COOLING_MODE_ACTIVE;
 
294
        else if (s == "passive")
 
295
                COOLING_POLICY = COOLING_MODE_PASSIVE;
 
296
        else if (s != "")
 
297
                pDebug(DBG_WARN, "cooling mode '%s' does not exist.\n", s.c_str());
 
298
 
 
299
        s = data["DISPLAY_BRIGHTNESS"];
 
300
        if (s == "ignore")
 
301
                DISPLAY_BRIGHTNESS = -1;
 
302
        else if (s == "min")
 
303
                DISPLAY_BRIGHTNESS = -2;
 
304
        else if (s == "med")
 
305
                DISPLAY_BRIGHTNESS = -3;
 
306
        else if (s == "max")
 
307
                DISPLAY_BRIGHTNESS = -4;
 
308
        else
 
309
                DISPLAY_BRIGHTNESS = checkValue(DISPLAY_BRIGHTNESS, "DISPLAY_BRIGHTNESS", 0);
 
310
 
 
311
 
 
312
        /* map class names used in config file to our internal device
 
313
         * classes for runtime powermanagement */
 
314
        list< string > classes;
 
315
        splitLine(data["DPM_DEVICES"], classes);
 
316
 
 
317
        for (list< string >::iterator it = classes.begin();
 
318
             it != classes.end(); ++it) {
 
319
 
 
320
                int i = 0;
 
321
                while (i != DPM_CLASS_MAX) {
 
322
                        if ((*it) == DPM_CLASS_NAMES[i]) {
 
323
                                dpmClasses.push_back((DPM_DEVICE_CLASS)i);
 
324
                        }
 
325
                        i++;
 
326
                }
 
327
        }
 
328
 
 
329
        for (int x = 0; x < MAX_THERMAL_ZONES; x++) {
 
330
                if (thermal_zones[x].present == 0)
 
331
                        continue;
 
332
 
 
333
                int error = 0;
 
334
 
 
335
                s = "THERMAL_CRITICAL_" + toString(x);
 
336
                i = checkValue(s, 0, 200);
 
337
                if (i < -1)     // -1 is no error, but unconfigured; -2 is error
 
338
                        error = 1;
 
339
                else if (i > 0) {
 
340
                        thermal_zones[x].critical = i;
 
341
                        pDebug(DBG_INFO, "%s = %d", s.c_str(), i);
 
342
                }
 
343
 
 
344
                s = "THERMAL_HOT_" + toString(x);
 
345
                i = checkValue(s, 0, 200);
 
346
                if (i < -1)     // -1 is no error, but unconfigured; -2 is error
 
347
                        error = 1;
 
348
                else if (i > 0) {
 
349
                        thermal_zones[x].hot = i;
 
350
                        pDebug(DBG_INFO, "%s = %d", s.c_str(), i);
 
351
                }
 
352
 
 
353
                s = "THERMAL_PASSIVE_" + toString(x);
 
354
                i = checkValue(s, 0, 200);
 
355
                if (i < -1)     // -1 is no error, but unconfigured; -2 is error
 
356
                        error = 1;
 
357
                else if (i > 0) {
 
358
                        thermal_zones[x].passive = i;
 
359
                        pDebug(DBG_INFO, "%s = %d", s.c_str(), i);
 
360
                }
 
361
 
 
362
                /* format: THERMAL_ACTIVE_1_2="45"
492
363
                   first  _1: which thermal zone
493
364
                   second _2: which active device
494
 
                   in this case: 
495
 
                   set limit of active device 2 (active[2]) of first thermal zone to 45 degree Celcius */
496
 
                else if (!strncmp(_event, "POWERSAVE_THERMAL_ACTIVE", 24)){
497
 
                        int x = -1;
498
 
                        int y = -1;
499
 
                        int  error = 0;
500
 
                        int_val = strtol(_value, endptr, 10);
501
 
                        /* which thermal zone is affected? */
502
 
                        if (strlen(_event) == 26 && !(int_val < 0) && !(int_val > 200)){
503
 
                                x = _event[25]-'0';
504
 
                                if (x < 0 || x > MAX_THERMAL_ZONES || thermal_zones[x].present == 0)
505
 
                                        error = 1;
506
 
                        }
507
 
                        /* which active trip point is affected? */
508
 
                        if (strlen(_event) == 28 && !(int_val < 0) && !(int_val > 200)){
509
 
                                y = _event[27]-'0';
510
 
                                if (y < 0 || y > 9)
511
 
                                        error = 1;
512
 
                        }
513
 
                        else
 
365
                   in this case: set limit of active device 2 (active[2])
 
366
                   of first thermal zone to 45 degree Celsius */
 
367
                t = "THERMAL_ACTIVE_" + toString(x) + "_";
 
368
                for (int y = 0; y < ACPI_THERMAL_MAX_ACTIVE; y++) {
 
369
                        s = t + toString(y);
 
370
                        i = checkValue(s, 0, 200);
 
371
                        if (i < -1)     // -1 is no error, but unconfigured; -2 is error
514
372
                                error = 1;
515
 
                        /* value of 0 -> do not override BIOS settings */
516
 
                        if (int_val > 0 && !error)
517
 
                                        thermal_zones[x].active[y] = int_val;
518
 
                        if (error)
519
 
                                pDebug (DBG_WARN, "Wrong syntax or not existing thermal_zone(%d),"
520
 
                                        " in line %d of config file %s",
521
 
                                        x, line, file.c_str());
522
 
                }
523
 
                        // these configs are used in the powersave_proxy and need not
524
 
                        // not to be considered at this place, maybe later ...
525
 
                /* ignore most variables which are used by scripts */
526
 
                else if (!strcmp(_event, "POWERSAVED_DEBUG") || 
527
 
                         !strcmp(_event, "POWERSAVED_SECURITY") ||
528
 
                         !strcmp(_event, "POWERSAVED_START_ACPID") ||
529
 
                         !strcmp(_event, "POWERSAVE_UNLOAD_MODULES_BEFORE_SUSPEND2DISK") ||
530
 
                         !strcmp(_event, "POWERSAVE_UNLOAD_MODULES_BEFORE_SUSPEND2RAM") ||
531
 
                         !strcmp(_event, "POWERSAVE_UNLOAD_MODULES_BEFORE_STANDBY") ||
532
 
                         !strcmp(_event, "POWERSAVE_NOTIFY_METHOD") ||
533
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2DISK_RESTART_SERVICES") ||
534
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2RAM_RESTART_SERVICES") ||
535
 
                         !strcmp(_event, "POWERSAVE_STANDBY_RESTART_SERVICES") ||
536
 
                         !strcmp(_event, "POWERSAVE_ACPI_MODULES") ||
537
 
                         !strcmp(_event, "POWERSAVE_ACPI_MODULES_NOT_TO_UNLOAD") ||
538
 
                         !strcmp(_event, "POWERSAVE_CPUFREQD_MODULE") ||
539
 
                         !strcmp(_event, "POWERSAVE_CPUFREQD_MODULE_OPTS") ||
540
 
                         !strcmp(_event, "POWERSAVE_BOOT_LOADER") ||
541
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2DISK_SHUTDOWN_MODE") ||
542
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2DISK_RESTORE_CLOCK") ||
543
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2RAM_RESTORE_CLOCK") ||
544
 
                         !strcmp(_event, "POWERSAVE_STANDBY_RESTORE_CLOCK") ||
545
 
                         !strcmp(_event, "POWERSAVE_DISK_STANDBY_MODE") ||
546
 
                         !strcmp(_event, "POWERSAVE_DISK_ACOUSTIC") ||
547
 
                         !strcmp(_event, "POWERSAVE_SUSPEND2DISK_IGNORE_KERNEL_MISMATCH") ||
548
 
                         !strcmp(_event, "POWERSAVE_SCHEME_DESCRIPTION") ||
549
 
                         !strcmp(_event, "POWERSAVE_SCREENSAVER_DPMS_OFF") ||
550
 
                         !strcmp(_event, "POWERSAVE_SCREENSAVER_BLANKONLY") ||
551
 
                         !strcmp(_event, "POWERSAVE_ACTION_USER_INPUT_TIMEOUT") ||
552
 
                         !strcmp(_event, "POWERSAVE_USER_INPUT_TIMEOUT")) {}
553
 
                
554
 
                else{
555
 
                        pDebug(DBG_INFO, "Error in line %d of config file %s."
556
 
                               " Event or setting %s does not exist, ignored.\n", line, file.c_str(), _event);
557
 
                }
 
373
                        else if (i > 0) {
 
374
                                thermal_zones[x].active[y] = i;
 
375
                                pDebug(DBG_INFO, "%s = %d", s.c_str(), i);
 
376
                        }
 
377
                }
 
378
                if (error)
 
379
                        pDebug(DBG_WARN, "Wrong value for thermal zone %d.", x);
558
380
        }
559
 
        fclose(fp);
560
 
        return 0;
561
 
}
562
 
 
563
 
Event& PS_Config::getEvent( string eventName ){
 
381
}
 
382
 
 
383
int PS_Config::checkValue(const std::string &what, int min, int max)
 
384
{
 
385
        std::string s;
 
386
        s = data[what];
 
387
        if (s == "")
 
388
                return -1;
 
389
        int val = toPosInt(s);
 
390
        if (val < min || val > max) {
 
391
                pDebug(DBG_ERR, "%s must be between %d and %d, was %d, raw '%s'. "
 
392
                       "Default used.", what.c_str(), min, max, val, s.c_str());
 
393
                return -2;
 
394
        } else
 
395
                return val;
 
396
}
 
397
 
 
398
int PS_Config::checkValue(const std::string &what, int min)
 
399
{
 
400
        std::string s;
 
401
        s = data[what];
 
402
        if (s == "")
 
403
                return -1;
 
404
        int val = toPosInt(s);
 
405
        if (val < min) {
 
406
                pDebug(DBG_ERR, "%s must be >= %d, was %d, raw '%s'. "
 
407
                       "Default used.", what.c_str(), min, val, s.c_str());
 
408
                return -2;
 
409
        } else
 
410
                return val;
 
411
}
 
412
 
 
413
int PS_Config::checkValue(int def, const std::string &what, int min, int max)
 
414
{
 
415
        std::string s;
 
416
        s = data[what];
 
417
        if (s == "")
 
418
                return def;
 
419
        int val = toPosInt(s);
 
420
        if (val < min || val > max) {
 
421
                pDebug(DBG_ERR, "%s must be between %d and %d, was %d, raw '%s'. "
 
422
                       "Default used.", what.c_str(), min, max, val, s.c_str());
 
423
                return def;
 
424
        } else
 
425
                return val;
 
426
}
 
427
 
 
428
int PS_Config::checkValue(int def, const std::string &what, int min)
 
429
{
 
430
        std::string s;
 
431
        s = data[what];
 
432
        if (s == "")
 
433
                return def;
 
434
        int val = toPosInt(s);
 
435
        if (val < min) {
 
436
                pDebug(DBG_ERR, "%s must be >= %d, was %d, raw '%s'. "
 
437
                       "Default used.", what.c_str(), min, val, s.c_str());
 
438
                return def;
 
439
        } else
 
440
                return val;
 
441
}
 
442
 
 
443
int PS_Config::checkYes(const std::string &what)
 
444
{
 
445
        std::string s;
 
446
        s = data[what];
 
447
        if (s == "")
 
448
                return -1;
 
449
        return (lowercase(s) == "yes") ? 1 : 0;
 
450
}
 
451
 
 
452
int PS_Config::checkYes(int def, const std::string &what)
 
453
{
 
454
        std::string s;
 
455
        s = data[what];
 
456
        if (s == "")
 
457
                return def;
 
458
        return (lowercase(s) == "yes") ? 1 : 0;
 
459
}
 
460
 
 
461
Event & PS_Config::getEvent(const string &eventName)
 
462
{
564
463
        EventIter i = eventMap.find(eventName);
565
464
 
566
465
        /*
567
 
        if (i == eventMap.end()){
568
 
                pDebug (DBG_ERR, "Event has not been initialised or does not exist! This is an error!!\n");
569
 
        }
570
 
        else {
571
 
        */
572
 
                return i->second;
573
 
//      }
574
 
}
575
 
 
 
466
           if (i == eventMap.end()){
 
467
           pDebug (DBG_ERR, "Event has not been initialised or does not exist! This is an error!!\n");
 
468
           }
 
469
           else {
 
470
         */
 
471
        return i->second;
 
472
//      }
 
473
}
 
474
 
 
475
void PS_Config::strtointErr(char *str, int line, const char *file)
 
476
{
 
477
        pDebug(DBG_ERR, "Could not convert string '%s' to int in"
 
478
               " line %d; config file: %s", str, line, file);
 
479
}
 
480
 
 
481
 
 
482
void PS_Config::splitLine(string line, std::list< std::string > &strings)
 
483
{
 
484
        strings.clear();
 
485
        string action;
 
486
        int x = line.size();
 
487
        int y = 0;
 
488
        if (line == "")
 
489
                return;
 
490
 
 
491
        while (x > 0 && isspace(line[x - 1]))
 
492
                x--;
 
493
        y = x - 1;
 
494
        do {
 
495
                while (y > 0 && !isspace(line[y])) {
 
496
                        y--;
 
497
                }
 
498
                if (y != 0)
 
499
                        y++;
 
500
                if (x > y) {
 
501
                        action = line.substr(y, x - y);
 
502
                        strings.push_front(action);
 
503
                } else
 
504
                        break;
 
505
                y--;
 
506
                while (y > 0 && isspace(line[y]))
 
507
                        y--;
 
508
                x = y + 1;
 
509
        } while (y > 0);
 
510
 
 
511
}