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

« back to all changes in this revision

Viewing changes to user_binary/powersave.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 <getopt.h>
2
24
#include <stdio.h>
3
25
#include <string.h>
4
26
#include <stdlib.h>
5
27
#include <errno.h>
6
28
 
7
 
 
8
29
#include "powerlib.h"
9
 
#include "powersave_daemonlib.h"
10
 
#include "../config.h"
11
 
 
 
30
#include "config.h"
 
31
 
 
32
#include "powersave_dbus.h"
 
33
 
 
34
#include <string>
 
35
#include <iostream>
12
36
 
13
37
#define MAX_OPT_LEN 20
14
38
#define MAX_PARAM_LENGTH 50
15
39
 
 
40
using namespace std;
 
41
 
16
42
static int option = -1;
17
43
static int verbose = 0;
18
44
static char schemeName[MAX_PARAM_LENGTH + 1] = "";
19
45
 
 
46
static char dpmDeviceClass[MAX_PARAM_LENGTH + 1] = "";
 
47
 
20
48
// toDo: set modes to specification ...
21
49
static int BATT_INFO = 0;
22
 
static int BATT_INFO_D = 0;
23
50
static int BAT_STATE = 0;
24
51
static int THROT_INFO = 0;
25
52
static int SET_THROT_PERC = 0;
42
69
 
43
70
static int LIST_SCHEMES = 0;
44
71
static int SET_ACTIVE_SCHEME = 0;
45
 
 
46
 
void general_error(){
47
 
        fprintf(stderr, "An error occured, use -v to increase verbose level and have a look into syslog.\n");
 
72
static int GET_SCHEME_DESCRIPTION = 0;
 
73
 
 
74
static int GET_BRIGHTNESS = 0;
 
75
static int GET_BRIGHTNESS_LEVELS = 0;
 
76
static int SET_BRIGHTNESS = 0;
 
77
 
 
78
static int DPM_SUSPEND = 0;
 
79
static int DPM_RESUME = 0;
 
80
static int DPM_NUMBER = 0;
 
81
static int DPM_GET_DEVICES = 0;
 
82
static int DPM_GET_CLASSES = 0;
 
83
 
 
84
void general_error()
 
85
{
 
86
        fprintf(stderr, "An error occured. Make sure that the powersave daemon is\n"
 
87
                "running and use -v to increase verbose level and have a look into syslog.\n");
48
88
}
49
89
 
50
 
void no_connect_no_rights_error(){
 
90
void no_connect_no_rights_error()
 
91
{
51
92
        fprintf(stderr, "Could not connect to powersaved because of missing permissions."
52
93
                " Are you priviledged to connect to the powersaved?\n");
53
94
}
54
95
 
55
 
void no_connect_no_daemon_error(){
 
96
void no_connect_no_daemon_error()
 
97
{
56
98
        fprintf(stderr, "Could not connect to powersaved. Is the powersaved running?\n");
57
99
}
58
100
 
59
 
void no_cpufreq_error(){
 
101
void no_cpufreq_error()
 
102
{
60
103
        fprintf(stderr, "Speedstepping is not supported.\n");
61
104
}
62
105
 
63
 
void usage(){
64
 
        fprintf(stderr, "Usage: powersave {-f|-l|-A} {-u|-U|-m} -[crbBsSaTFVtx] [-p percent] [-v level] [-e scheme_name]\n"
65
 
                "-U set machine into suspend-to-disk (ACPI S4/APM suspend) sleeping state\n"
66
 
                "-u set machine into suspend-to-ram  (ACPI S3/APM suspend) sleeping state\n"
67
 
                "-m set machine into standby         (ACPI S1/APM standby) sleeping state\n\n"
68
 
                "-f set cpufreq to performance mode\n"
69
 
                "-l set cpufreq to powersave mode\n"
70
 
                "-A set cpufreq to dynamic mode\n"
71
 
                "-c print out the current cpufreq policy\n\n"
72
 
                "-s print out the current battery state\n\n"
73
 
                "-p X throttles processors of machine by X percent\n"
74
 
                "-t prints out current throttling state\n"
75
 
                "-S prints out whether APM or ACPI is supported\n"
76
 
                "-b general battery info\n"
77
 
                "-B info of each battery\n"
78
 
                "-d get battery info from daemon\n"
79
 
                "-a power supply info (AC/Battery)\n"
80
 
                "-T thermal Info\n"
81
 
                "-F fan Info\n"
82
 
                "-x show all available schemes\n"
83
 
                "-e X switch currently active scheme\n"
84
 
                "-r current speed, does not work on all machines properly (manpage)\n"
85
 
                "-v X Verbose level (see manpage for values)\n"
86
 
                "-V current compiled version\n\n"
87
 
                );
 
106
void usage()
 
107
{
 
108
        fprintf(stderr,
 
109
                "Usage: powersave {-f|-l|-A} {-u|-U|-m} -[crbBsSaTFVtxKL] [-p percent] [-v level] [-e scheme_name] -k [brightness level]\n"
 
110
                "                 [-h|--help] -i [device class] -I [device class] -J [device class] -j [device number]\n\n"
 
111
                " Suspend/Standby:\n"
 
112
                "   -U, --suspend-to-disk                  set machine into suspend-to-disk (ACPI S4/APM suspend)\n"
 
113
                "   -u, --suspend-to-ram                   set machine into suspend-to-ram  (ACPI S3/APM suspend)\n"
 
114
                "   -m, --standby                          set machine into standby         (ACPI S1/APM standby)\n\n"
 
115
                " CPUFreq modes:\n"
 
116
                "   -f, --performance-speed                set cpufreq to performance mode\n"
 
117
                "   -l, --powersave-speed                  set cpufreq to powersave mode\n"
 
118
                "   -A, --dynamic-speed                    set cpufreq to dynamic mode\n"
 
119
                "   -c, --cpufreq-state-info               print out the current cpufreq policy\n\n"
 
120
                " Throttling:\n"
 
121
                "   -p <x>, --set-throttling-percent <x>   throttles processors of machine by X percent\n"
 
122
                "   -t, --throttling-info                  prints out current throttling state\n\n"
 
123
                " Schemes:\n"
 
124
                "   -x, --list-schemes                     show all available schemes\n"
 
125
                "   -X, --get-scheme-description           show the description of scheme\n"
 
126
                "   -e <x>, --set-active-scheme <x>        switch currently active scheme\n\n"
 
127
                " Brightness:\n"
 
128
                "   -k, --set-brightness <x>               set current brightness level\n"
 
129
                "   -K, --get-brightness                   get current brightness level\n"
 
130
                "   -L, --get-brightness-levels            get number of brightness levels\n\n"
 
131
                " Runtime powermanagement: (experimental)\n"
 
132
                "   -i --dpm-suspend <device class>       set devices of a device class into D3 power save mode\n"
 
133
                "   -I --dpm-resume <device class>        set devices of a device class back to D0 power mode\n"
 
134
                "   -j --dpm-device-number <x>            only suspend/resume device number <x>\n"
 
135
                "   -J --dpm-get-devices <device class>   List devices from a specific device class\n"
 
136
                "   -o --dpm-get-classes                  List available device classes\n\n"
 
137
                " Print information:\n"
 
138
                "   -s, --battery-state-info               print out the current battery state\n"
 
139
                "   -b, --battery-info                     general battery info\n"
 
140
                "   -B, --detailed-battery-info            info of each battery\n"
 
141
                "   -S, --apm-acpi                         prints out whether APM or ACPI is supported\n"
 
142
                "   -a, --ac-status-info                   power supply info (AC/Battery)\n"
 
143
                "   -T, --get-thermal-info                 thermal Info\n"
 
144
                "   -F, --get-fan-info                     fan Info\n"
 
145
                "   -r, --calc-CPU-frequency               current speed (see manpage)\n"
 
146
                "   -v <x>, --verbose <x>                  Verbose level (see manpage for values)\n"
 
147
                "   -V, --version                          current compiled version\n\n");
88
148
        exit(EXIT_FAILURE);
89
149
}
90
150
 
91
 
void checkArgs(){
92
 
        int info = BATT_INFO + BAT_STATE + THROT_INFO + SET_THROT_PERC + AC_INFO + APM_ACPI + CPUFREQ_INFO + REAL_CPU_SPEED + EXT_BATT_INFO + THERMAL_INFO + FAN_INFO + BATT_INFO_D;
 
151
void checkArgs()
 
152
{
 
153
        int info =
 
154
            BATT_INFO + BAT_STATE + THROT_INFO + SET_THROT_PERC + AC_INFO + APM_ACPI + CPUFREQ_INFO + REAL_CPU_SPEED +
 
155
            EXT_BATT_INFO + THERMAL_INFO + FAN_INFO + GET_BRIGHTNESS + GET_BRIGHTNESS_LEVELS;
93
156
        int cpufreq = PERFORMANCE_SPEED + POWERSAVE_SPEED + DYNAMIC_SPEED;
94
157
        int sleep = SUSPEND_TO_DISK + SUSPEND_TO_RAM;
95
158
 
96
 
        if (sleep > 1 || cpufreq > 1){
 
159
        if (sleep > 1 || cpufreq > 1) {
97
160
                usage();
98
161
                exit(EXIT_FAILURE);
99
162
        }
100
 
        if (sleep == 1 && (info > 0 || cpufreq > 0)){
 
163
        if (sleep == 1 && (info > 0 || cpufreq > 0)) {
101
164
                usage();
102
165
                exit(EXIT_FAILURE);
103
166
        }
104
167
        return;
105
168
}
106
169
 
107
 
void getArgs(int argc, char** argv){
108
 
 
109
 
        int option_index=0;
110
 
        char *endptr[MAX_PARAM_LENGTH +1];
111
 
        
112
 
        struct option opts[] =
113
 
                {
114
 
                        {"performance-speed", 0, 0, 'f'},
115
 
                        {"powersave-speed", 0, 0, 'l'},
116
 
                        {"dynamic-speed", 0, 0, 'A'},
117
 
                        {"cpufreq-state-info", 0, 0, 'c'},
118
 
                        {"calc-CPU-frequency", 0, 0, 'r'},
119
 
                        {"apm-acpi", 0, 0, 'S'},
120
 
                        {"throttling-info", 0, 0, 't'},
121
 
                        {"set-throttling-percent", 1, 0, 'p'},
122
 
                        {"suspend-to-disk", 0, 0, 'U'},
123
 
                        {"suspend-to-ram", 0, 0, 'u'},
124
 
                        {"standby", 0, 0, 'm'},
125
 
                        {"get-thermal-info", 0, 0, 'T'},
126
 
                        {"get-fan-info", 0, 0, 'F'},
127
 
                        {"battery-info", 0, 0, 'b'},
128
 
                        {"daemon-battery-info", 0, 0, 'd'},
129
 
                        {"detailed-battery-info", 0, 0, 'B'},
130
 
                        {"battery-state-info", 0, 0, 's'},
131
 
                        {"ac-status-info", 0, 0, 'a'},
132
 
                        {"list-schemes", 0, 0, 'x'},
133
 
                        {"set-active-scheme", 0, 0, 'e'},
134
 
                        {"verbose", 0, 0, 'v'},
135
 
                        {"version", 0, 0, 'V'},
136
 
                        {"help", 0, 0, 'h'},
137
 
                        {NULL, 0, 0, 0},
138
 
                };
139
 
        while (1){
140
 
                int i = getopt_long(argc, argv, "v:p:e:crbdBsSaTFVxuUmhtlfA", opts, &option_index);
141
 
                if (i == -1){
142
 
                        break;
143
 
                }
144
 
                if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
 
170
void getArgs(int argc, char **argv)
 
171
{
 
172
 
 
173
        int option_index = 0;
 
174
        char *endptr[MAX_PARAM_LENGTH + 1];
 
175
 
 
176
        struct option opts[] = {
 
177
                {"performance-speed", 0, 0, 'f'},
 
178
                {"powersave-speed", 0, 0, 'l'},
 
179
                {"dynamic-speed", 0, 0, 'A'},
 
180
                {"cpufreq-state-info", 0, 0, 'c'},
 
181
                {"calc-CPU-frequency", 0, 0, 'r'},
 
182
                {"apm-acpi", 0, 0, 'S'},
 
183
                {"throttling-info", 0, 0, 't'},
 
184
                {"set-throttling-percent", 1, 0, 'p'},
 
185
                {"suspend-to-disk", 0, 0, 'U'},
 
186
                {"suspend-to-ram", 0, 0, 'u'},
 
187
                {"standby", 0, 0, 'm'},
 
188
                {"get-thermal-info", 0, 0, 'T'},
 
189
                {"get-fan-info", 0, 0, 'F'},
 
190
                {"battery-info", 0, 0, 'b'},
 
191
                {"detailed-battery-info", 0, 0, 'B'},
 
192
                {"battery-state-info", 0, 0, 's'},
 
193
                {"ac-status-info", 0, 0, 'a'},
 
194
                {"list-schemes", 0, 0, 'x'},
 
195
                {"set-active-scheme", 1, 0, 'e'},
 
196
                {"get-scheme-description", 1, 0, 'X'},
 
197
                {"set-brightness", 1, 0, 'k'},
 
198
                {"get-brightness", 0, 0, 'K'},
 
199
                {"get-brightness-levels", 0, 0, 'L'},
 
200
                {"dpm-suspend", 1, 0, 'i'},
 
201
                {"dpm-resume", 1, 0, 'I'},
 
202
                {"dpm-device-number", 1, 0, 'j'},
 
203
                {"dpm-get-devices", 1, 0, 'J'},
 
204
                {"dpm-available-classes", 0, 0, 'o'},
 
205
                {"verbose", 0, 0, 'v'},
 
206
                {"version", 0, 0, 'V'},
 
207
                {"help", 0, 0, 'h'},
 
208
                {NULL, 0, 0, 0},
 
209
        };
 
210
        while (1) {
 
211
                int i = getopt_long(argc, argv, "v:p:e:crbBsSaTFVxuUmhtlfAk:KLi:I:j:J:oX:", opts, &option_index);
 
212
                if (argc <= 1) {
 
213
                        usage();
 
214
                        exit(0);
 
215
                        break;
 
216
                }
 
217
                if (i == -1) {
 
218
                        break;
 
219
                }
 
220
                if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH) {
145
221
                        fprintf(stderr, "Too long parameter\n");
146
222
                        exit(EXIT_FAILURE);
147
223
                }
148
 
                switch (i){
 
224
                switch (i) {
149
225
                case 0:
150
226
                        /*
151
 
                        if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
152
 
                                fprintf(stderr, "Too long parameter\n");
153
 
                                exit(EXIT_FAILURE);
154
 
                        }
155
 
                        */
 
227
                           if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
 
228
                           fprintf(stderr, "Too long parameter\n");
 
229
                           exit(EXIT_FAILURE);
 
230
                           }
 
231
                         */
156
232
                        break;
157
 
                        
 
233
 
158
234
                case 'c':
159
235
                        CPUFREQ_INFO = 1;
160
236
                        break;
161
237
                case 'b':
162
238
                        BATT_INFO = 1;
163
239
                        break;
164
 
                case 'd':
165
 
                        BATT_INFO_D = 1;
166
 
                        break;
167
240
                case 'B':
168
241
                        EXT_BATT_INFO = 1;
169
242
                        break;
202
275
                        break;
203
276
                case 'e':
204
277
                        SET_ACTIVE_SCHEME = 1;
205
 
                        strcpy (schemeName, optarg);
 
278
                        strcpy(schemeName, optarg);
206
279
                        break;
207
280
                case 'x':
208
281
                        LIST_SCHEMES = 1;
209
282
                        break;
 
283
                case 'X':
 
284
                        GET_SCHEME_DESCRIPTION = 1;
 
285
                        strcpy(schemeName, optarg);
 
286
                        break;
210
287
                case 'F':
211
288
                        FAN_INFO = 1;
212
289
                        break;
213
290
                case 'p':
214
291
                        SET_THROT_PERC = 1;
215
292
                        option = strtol(optarg, endptr, 10);
216
 
                        if (**endptr != '\0'|| option < 0 || option > 100){
 
293
                        if (**endptr != '\0' || option < 0 || option > 100) {
217
294
                                printf("Wrong throttling in percent value,"
218
295
                                       " must be between 0-100: %s\n", strerror(errno));
219
 
                                exit (EXIT_FAILURE);
 
296
                                exit(EXIT_FAILURE);
220
297
                        }
221
298
                        break;
222
299
                case 's':
223
300
                        BAT_STATE = 1;
224
301
                        break;
 
302
                case 'k':
 
303
                        SET_BRIGHTNESS = 1;
 
304
                        if (*optarg == 'u')
 
305
                                option = -1;    // up
 
306
                        else if (*optarg == 'd')
 
307
                                option = -2;    // down
 
308
                        else
 
309
                                option = strtol(optarg, endptr, 10);
 
310
                        break;
 
311
                case 'K':
 
312
                        GET_BRIGHTNESS = 1;
 
313
                        break;
 
314
                case 'L':
 
315
                        GET_BRIGHTNESS_LEVELS = 1;
 
316
                        break;
 
317
                case 'J':
 
318
                        DPM_GET_DEVICES = 1;
 
319
                        strcpy(dpmDeviceClass, optarg);
 
320
                        break;
 
321
                case 'j':
 
322
                        DPM_NUMBER = 1;
 
323
                        option = strtol(optarg, endptr, 10);
 
324
                        break;
 
325
                case 'i':
 
326
                        DPM_SUSPEND = 1;
 
327
                        strcpy(dpmDeviceClass, optarg);
 
328
                        break;
 
329
                case 'I':
 
330
                        DPM_RESUME = 1;
 
331
                        strcpy(dpmDeviceClass, optarg);
 
332
                        break;
 
333
                case 'o':
 
334
                        DPM_GET_CLASSES = 1;
 
335
                        break;
225
336
                case 'v':
226
337
                        verbose = strtol(optarg, endptr, 10);
227
 
                        if (**endptr != '\0' || verbose < 0 || verbose > 36){
 
338
                        if (**endptr != '\0' || verbose < 0 || verbose > 36) {
228
339
                                printf("Wrong verbose(-v, debug) paramater: %s\n", optarg);
229
 
                                exit (EXIT_FAILURE);
230
 
                        }
231
 
                        else {
 
340
                                exit(EXIT_FAILURE);
 
341
                        } else {
232
342
                                setDebugLevel(verbose);
233
343
                                //fprintf(stderr, "Debug Level set to %d\n", verbose);
234
344
                        }
235
345
                        break;
236
346
                case 'V':
237
 
                        VERSION_INFO=1;
 
347
                        VERSION_INFO = 1;
238
348
                        break;
239
349
                case 'h':
240
350
                        usage();
241
 
                        exit (0);
 
351
                        exit(0);
242
352
                        break;
243
353
                }
244
354
        }
245
 
        
 
355
 
246
356
}
247
357
 
248
 
int get_bat_info(int ext) {
249
 
        BatteryDetailed bd;
 
358
int get_bat_info(int ext)
 
359
{
250
360
        BatteryGeneral bg;
251
 
        int x, bat_num, full_capacity, remPerc, hours, mins, ac_state = 0;
 
361
        int x, bat_num = 0, ac_state = 0;
252
362
 
253
363
        // test of general battery information:
254
 
        if (getBatteriesInfo(&bg) < 0){
 
364
        if (getBatteriesInfo(&bg) < 0) {
255
365
                fprintf(stderr, "Could not get battery information.");
256
366
                return -1;
257
 
        }
258
 
        else {
 
367
        } else {
259
368
                ac_state = getACAdapterStatus();
260
 
                hours = bg.remMins/60;
261
 
                mins  = bg.remMins%60;
262
 
                if (bg.remPerc >= 0 && bg.remMins >= 0 && ac_state == AC_OFFLINE) 
263
 
                        fprintf(stdout, "Battery: %d %% %d minutes\n", bg.remPerc, bg.remMins);
264
 
                else if(bg.remPerc >= 0)
265
 
                        fprintf(stdout, "Battery: %d %%\n", bg.remPerc);
266
 
                else
267
 
                        fprintf(stdout, "Battery: error.\n");
 
369
                if (bg.remaining_percent >= 0) {
 
370
                        fprintf(stdout, "Battery: %d %%", bg.remaining_percent);
 
371
 
 
372
                        if (ac_state == AC_ONLINE && bg.remaining_minutes > 0) {
 
373
                                fprintf(stdout, ", %d minutes until fully charged", bg.remaining_minutes);
 
374
                        } else if (ac_state == AC_OFFLINE && bg.remaining_minutes > 0) {
 
375
                                fprintf(stdout, ", %d minutes remaining", bg.remaining_minutes);
 
376
                        }
 
377
 
 
378
                        fprintf(stdout, "\n");
 
379
                }               
 
380
 
268
381
                if (ac_state == AC_ONLINE)
269
 
                        fprintf (stdout, "AC is online.\n");
270
 
                else{
271
 
                
272
 
                }
 
382
                        fprintf(stdout, "AC is online.\n");
273
383
        }
274
384
        // Test of detailed battery information
275
 
        if (ext){
276
 
                if (checkACPI() == ACPI){
277
 
                        for (x = 0; (getBatteryDetailedInfo(x, &bd) == 0) && x < MAX_BATTERIES ; x++){
278
 
                                if (bd.present == PRESENT_YES){
279
 
                                        bat_num++;
280
 
                                        // compare with library implementation
281
 
                                        full_capacity = (bd.remaining_capacity > bd.last_full_capacity) ? bd.remaining_capacity : bd.last_full_capacity;
282
 
                                        if (full_capacity > 0 &&  bd.remaining_capacity > 0){
283
 
                                                remPerc = (int)((float)bd.remaining_capacity / (float)(full_capacity) * 100.0);
284
 
                                        }
285
 
                                        else if (full_capacity == 0 || bd.remaining_capacity == 0){
286
 
                                                remPerc = 0;
287
 
                                        }
288
 
                                        else 
289
 
                                                remPerc = -1;
290
 
                                        fprintf(stdout, "Battery%d: %d\n", x, remPerc);
 
385
        if (ext) {
 
386
                if (checkACPI() == ACPI) {                      
 
387
                        for (x = 0; x < numBatteries(); x++) {
 
388
 
 
389
                                int ret = getBatteryInfo(x, &bg);
 
390
 
 
391
                                if (ret < 0) {
 
392
                                        fprintf(stderr, "Battery error. Maybe hal is not running?\n");
 
393
                                        exit(EXIT_FAILURE);
 
394
                                }
 
395
 
 
396
                                bat_num++;
 
397
 
 
398
                                if (!ret) {
 
399
                                        fprintf(stdout, "Battery%d: not present\n", bat_num);
 
400
                                }
 
401
                                else {
 
402
                                        fprintf(stdout, "Battery%d: %d %%", bat_num, bg.remaining_percent);
 
403
                                        if (bg.remaining_minutes >= 0 ) {
 
404
                                                fprintf(stdout, ", %d minutes", bg.remaining_minutes);
 
405
                                        }
 
406
                                        fprintf(stdout,"\n");
291
407
                                }
292
408
                        }
293
 
                }
294
 
                else 
 
409
                } else
295
410
                        fprintf(stdout, "No acpi system\n");
296
411
        }
297
412
        return 1;
298
413
}
299
414
 
300
 
/* errors:
301
 
   returns -1 for parameter specific error
302
 
   returns -2 if user has no rights to connect to daemon
303
 
   returns -3 if socket conn could not be established (no daemon...)
 
415
/** @brief main function
 
416
 *
 
417
 * errors:
 
418
 *     - -1 for parameter specific error
 
419
 *     - -2 if user has no rights to connect to daemon
 
420
 *     - -3 if socket conn could not be established (no daemon...)
304
421
*/
305
 
 
306
 
int main(int argc, char** argv)
 
422
int main(int argc, char **argv)
307
423
{
308
424
        int ret = 0;
309
425
        int states, current, err_code;
313
429
        min = max = 0;
314
430
        getArgs(argc, argv);
315
431
        checkArgs();
316
 
        int y,x = 0;
317
 
        SchemeList* schemes = NULL;
318
 
        SchemeNode* scheme_node = NULL;
319
 
 
320
 
        int64_t daemon_system_states = 0;
321
 
        int64_t supported_sleep_states = 0;
322
 
 
323
 
        /* request daemon system states if one of these are set */
324
 
 
325
 
        if (SUSPEND_TO_RAM || SUSPEND_TO_DISK || STANDBY)
326
 
                supported_sleep_states = getSupportedSleepStates();
327
 
 
328
 
        if (CPUFREQ_INFO || BAT_STATE || BATT_INFO_D){
329
 
                daemon_system_states = send_Action_get_States();
330
 
                if (daemon_system_states < 0){
331
 
                        switch (daemon_system_states){
332
 
                        case POWERSAVED_NO_RIGHTS:
333
 
                                no_connect_no_rights_error();
334
 
                                ret=-2;                 
335
 
                                break;
336
 
                        case POWERSAVED_ERROR_SOCKET_ERROR:
337
 
                                no_connect_no_daemon_error();
338
 
                                ret=-3;
339
 
                                break;
340
 
                        default:
341
 
                                printf ("Could not connect to daemon, unknown error.\n");
342
 
                        }
343
 
                        CPUFREQ_INFO = 0;
344
 
                        BAT_STATE = 0;
345
 
                }
346
 
        }
347
 
 
348
 
        if (SUSPEND_TO_RAM){
349
 
                if (!((supported_sleep_states & APM_SUSPEND) || 
350
 
                      (supported_sleep_states & ACPI_S3) || 
351
 
                      (supported_sleep_states & ACPI_S3_BIOS)))
352
 
                        fprintf(stderr, "Suspend-To-Ram is not supported by BIOS.\n");
353
 
                else{
354
 
                        err_code = send_Action(POWERSAVED_ACTION_SUSPEND_TO_RAM);
355
 
                        switch (err_code){
356
 
                        case POWERSAVED_ERROR_NOERROR:
357
 
                                break;
358
 
                        case POWERSAVED_ERROR_SUSPEND_TO_RAM_DISABLED:
359
 
                                fprintf(stderr, "Suspend-To-Ram has been disabled by administrator.\n");
360
 
                                ret=-1;                 
361
 
                                break;
362
 
                        case POWERSAVED_NO_RIGHTS:
363
 
                                no_connect_no_rights_error();
364
 
                                ret=-2;                 
365
 
                                break;
366
 
                        case POWERSAVED_ERROR_SOCKET_ERROR:
367
 
                                no_connect_no_daemon_error();
368
 
                                ret=-3;
369
 
                                break;
370
 
                        default:
371
 
                                general_error();
372
 
                                ret=-1;
373
 
                        }
374
 
                }
375
 
        }
376
 
        if (STANDBY){
377
 
                if (!((supported_sleep_states & APM_STANDBY) || 
378
 
                      (supported_sleep_states & ACPI_S1)))
379
 
                        fprintf(stderr, "Standby is not supported by BIOS.\n");
380
 
                else{
381
 
                        err_code = send_Action(POWERSAVED_ACTION_STNDBY);
382
 
                        switch (err_code){
383
 
                        case POWERSAVED_ERROR_NOERROR:
384
 
                                break;
385
 
                        case POWERSAVED_ERROR_STNDBY_DISABLED:
386
 
                                fprintf(stderr, "Standby has been disabled by administrator.\n");
387
 
                                ret=-1;                 
388
 
                                break;
389
 
                        case POWERSAVED_NO_RIGHTS:
390
 
                                no_connect_no_rights_error();
391
 
                                ret=-2;                 
392
 
                                break;
393
 
                        case POWERSAVED_ERROR_SOCKET_ERROR:
394
 
                                no_connect_no_daemon_error();
395
 
                                ret=-3;
396
 
                                break;
397
 
                        default:
398
 
                                general_error();
399
 
                                ret=-1;
400
 
                        }
401
 
                }
402
 
        }
403
 
        else if (SUSPEND_TO_DISK){
404
 
                if (!((supported_sleep_states & APM_SUSPEND) || 
405
 
                      (supported_sleep_states & ACPI_S4) || 
406
 
                      (supported_sleep_states & ACPI_S4_BIOS)))
407
 
                        fprintf(stderr, "Suspend-To-Disk is not supported by BIOS.\n");
408
 
                else {
409
 
                        err_code = send_Action(POWERSAVED_ACTION_SUSPEND_TO_DISK);
410
 
                        switch(err_code){
411
 
                        case POWERSAVED_ERROR_NOERROR:
412
 
                                break;
413
 
                        case POWERSAVED_ERROR_SUSPEND_TO_DISK_DISABLED:
414
 
                                fprintf(stderr, "Suspend-To-Disk has been disabled by administrator.\n");
415
 
                                ret=-1;
416
 
                                break;
417
 
                        case POWERSAVED_NO_RIGHTS:
418
 
                                no_connect_no_rights_error();
419
 
                                ret=-2;                 
420
 
                                break;
421
 
                        case POWERSAVED_ERROR_SOCKET_ERROR:
422
 
                                no_connect_no_daemon_error();
423
 
                                ret=-3;
424
 
                                break;
425
 
                        default:
426
 
                                general_error();
427
 
                                ret=-1;
428
 
                        }
429
 
                }
430
 
        }
431
 
        if (PERFORMANCE_SPEED){
432
 
                err_code = send_Action(POWERSAVED_ACTION_CPUFREQ_PERFORMANCE);
433
 
                switch(err_code){
434
 
                case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
 
432
        int y, x = 0;
 
433
 
 
434
        DBusMessage *reply;
 
435
        DBusError error;
 
436
        dbus_error_init(&error);
 
437
 
 
438
        string result;
 
439
        char *dummy = "";
 
440
 
 
441
        if (SUSPEND_TO_RAM) {
 
442
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "SuspendToRam");
 
443
                switch (err_code) {
 
444
                case REPLY_SUCCESS:
 
445
                        break;
 
446
                case REPLY_DISABLED:
 
447
                        fprintf(stderr, "Suspend-To-RAM has been disabled by administrator.\n");
 
448
                        ret = -1;
 
449
                        break;
 
450
                case REPLY_HW_NOT_SUPPORTED:
 
451
                        fprintf(stderr, "Suspend-To-RAM is not supported.\n");
 
452
                        ret = -1;
 
453
                        break;
 
454
                case REPLY_DBUS_ERROR:
 
455
                        fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
 
456
                                DBus_Error_Array[err_code]);
 
457
                        break;
 
458
                default:
 
459
                        fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
 
460
                        ret = -1;
 
461
                }
 
462
        }
 
463
        if (STANDBY) {
 
464
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "Standby");
 
465
                switch (err_code) {
 
466
                case REPLY_SUCCESS:
 
467
                        break;
 
468
                case REPLY_DISABLED:
 
469
                        fprintf(stderr, "Standby has been disabled by administrator.\n");
 
470
                        ret = -1;
 
471
                        break;
 
472
                case REPLY_HW_NOT_SUPPORTED:
 
473
                        fprintf(stderr, "Standby is not supported.\n");
 
474
                        ret = -1;
 
475
                        break;
 
476
                case REPLY_DBUS_ERROR:
 
477
                        fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
 
478
                                DBus_Error_Array[err_code]);
 
479
                        break;
 
480
                default:
 
481
                        fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
 
482
                        ret = -1;
 
483
                }
 
484
        } else if (SUSPEND_TO_DISK) {
 
485
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "SuspendToDisk");
 
486
                switch (err_code) {
 
487
                case REPLY_SUCCESS:
 
488
                        break;
 
489
                case REPLY_DISABLED:
 
490
                        fprintf(stderr, "Suspend-To-disk has been disabled by administrator.\n");
 
491
                        ret = -1;
 
492
                        break;
 
493
                case REPLY_HW_NOT_SUPPORTED:
 
494
                        fprintf(stderr, "Suspend-To-disk is not supported.\n");
 
495
                        ret = -1;
 
496
                        break;
 
497
                case REPLY_DBUS_ERROR:
 
498
                        fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
 
499
                                DBus_Error_Array[err_code]);
 
500
                        break;
 
501
                default:
 
502
                        fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
 
503
                        ret = -1;
 
504
                }
 
505
        }
 
506
        if (PERFORMANCE_SPEED) {
 
507
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "CpufreqPerformance");
 
508
                switch (err_code) {
 
509
                case REPLY_HW_NOT_SUPPORTED:
435
510
                        no_cpufreq_error();
436
 
                        ret=-1;
 
511
                        ret = -1;
437
512
                        break;
438
 
                case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
 
513
                case REPLY_ALREADY_SET:
439
514
                        fprintf(stderr, "Speed already set to maximum \n");
440
515
                        //noerror
441
516
                        break;
442
 
                case POWERSAVED_ERROR_NOERROR:
 
517
                case REPLY_SUCCESS:
443
518
                        fprintf(stderr, "Speed set to maximum \n");
444
519
                        break;
445
 
                case POWERSAVED_NO_RIGHTS:
 
520
                case REPLY_NO_RIGHTS:
446
521
                        no_connect_no_rights_error();
447
 
                        ret=-2;                 
448
 
                        break;
449
 
                case POWERSAVED_ERROR_SOCKET_ERROR:
450
 
                        no_connect_no_daemon_error();
451
 
                        ret=-3;
 
522
                        ret = -2;
452
523
                        break;
453
524
                default:
454
525
                        general_error();
455
 
                        ret=-1;
 
526
                        ret = -1;
456
527
                }
457
 
        }
458
 
        else if (POWERSAVE_SPEED){
459
 
                err_code = send_Action(POWERSAVED_ACTION_CPUFREQ_POWERSAVE);
 
528
        } else if (POWERSAVE_SPEED) {
 
529
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "CpufreqPowersave");
460
530
                switch (err_code) {
461
 
                case POWERSAVED_ERROR_NOERROR:
 
531
                case REPLY_HW_NOT_SUPPORTED:
 
532
                        no_cpufreq_error();
 
533
                        ret = -1;
 
534
                        break;
 
535
                case REPLY_ALREADY_SET:
 
536
                        fprintf(stderr, "Speed already set to minimum.\n");
 
537
                        //noerror
 
538
                        break;
 
539
                case REPLY_SUCCESS:
462
540
                        fprintf(stderr, "Speed set to minimum.\n");
463
541
                        break;
464
 
                case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
465
 
                        no_cpufreq_error();
466
 
                        ret=-1;
467
 
                        break;
468
 
                case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
469
 
                        fprintf(stderr, "Speed already set to minimum.\n");
470
 
                        break;
471
 
                case POWERSAVED_NO_RIGHTS:
 
542
                case REPLY_NO_RIGHTS:
472
543
                        no_connect_no_rights_error();
473
 
                        ret=-2;                 
474
 
                        break;
475
 
                case POWERSAVED_ERROR_SOCKET_ERROR:
476
 
                        no_connect_no_daemon_error();
477
 
                        ret=-3;
 
544
                        ret = -2;
478
545
                        break;
479
546
                default:
480
547
                        general_error();
481
 
                        ret=-1;
 
548
                        ret = -1;
482
549
                }
483
 
        }
484
 
        else if (DYNAMIC_SPEED){
485
 
                err_code = send_Action(POWERSAVED_ACTION_CPUFREQ_DYNAMIC);
 
550
        } else if (DYNAMIC_SPEED) {
 
551
                err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "CpufreqDynamic");
486
552
                switch (err_code) {
487
 
                case POWERSAVED_ERROR_NOERROR:
488
 
                        fprintf(stderr, "Speed policy set to to dynamic.\n");
489
 
                        break;
490
 
                case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
 
553
                case REPLY_HW_NOT_SUPPORTED:
491
554
                        no_cpufreq_error();
492
 
                        ret=-1;
 
555
                        ret = -1;
493
556
                        break;
494
 
                case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
 
557
                case REPLY_ALREADY_SET:
495
558
                        fprintf(stderr, "Speed already set to dynamic.\n");
496
 
                        break;
497
 
                case POWERSAVED_NO_RIGHTS:
 
559
                        //noerror
 
560
                        break;
 
561
                case REPLY_SUCCESS:
 
562
                        fprintf(stderr, "Speed set to dynamic.\n");
 
563
                        break;
 
564
                case REPLY_NO_RIGHTS:
498
565
                        no_connect_no_rights_error();
499
 
                        ret=-2;                 
500
 
                        break;
501
 
                case POWERSAVED_ERROR_SOCKET_ERROR:
502
 
                        no_connect_no_daemon_error();
503
 
                        ret=-3;
 
566
                        ret = -2;
504
567
                        break;
505
568
                default:
506
569
                        general_error();
507
 
                        ret=-1;
 
570
                        ret = -1;
508
571
                }
509
572
        }
510
 
        if (CPUFREQ_INFO){
511
 
                //err_code = send_Action(POWERSAVED_REQUEST_CPUFREQ_STATE_INFO);
512
 
                err_code = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_CPUFREQ_STATE_INFO);
513
 
                switch(err_code){
514
 
                case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
 
573
        if (CPUFREQ_INFO) {
 
574
                err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "CpufreqPolicy");
 
575
                switch (err_code) {
 
576
                case REPLY_HW_NOT_SUPPORTED:
515
577
                        no_cpufreq_error();
516
 
                        ret=-1;
517
 
                        break;
518
 
                case POWERSAVED_CPUFREQ_STATE_PERFORMANCE:
519
 
                        fprintf(stdout, "PERFORMANCE\n");
520
 
                        ret = 1;
521
 
                        break;
522
 
                case POWERSAVED_CPUFREQ_STATE_POWERSAVE:
523
 
                        fprintf(stdout, "POWERSAVE\n");
524
 
                        ret = 2;
525
 
                        break;
526
 
                case POWERSAVED_CPUFREQ_STATE_DYNAMIC:
527
 
                        fprintf(stdout, "DYNAMIC\n");
528
 
                        ret = 3;
529
 
                        break;
530
 
                case POWERSAVED_NO_RIGHTS:
531
 
                        no_connect_no_rights_error();
532
 
                        ret=-2;                 
533
 
                        break;
534
 
                case POWERSAVED_ERROR_SOCKET_ERROR:
535
 
                        no_connect_no_daemon_error();
536
 
                        ret=-3;
537
 
                        break;
538
 
                default:
539
 
                        general_error();
540
 
                        ret=-1;
541
 
                }
542
 
        }
543
 
        if  (REAL_CPU_SPEED){
544
 
                ret_freq = getRealProcessorSpeed();
545
 
                if (ret_freq == -2){
546
 
                        fprintf(stderr, "0\nMachine's architecture is not supported or feature has"
547
 
                                " not been compiled for this machine's architecture(see manpage).\n");
548
 
                        ret=-1;
549
 
                }
550
 
                else if (ret_freq < 0){
551
 
                        fprintf(stderr, "0\nCould not calculate CPU frequency\n");
552
 
                        ret=-1;
553
 
                }
554
 
                else
555
 
                        fprintf(stdout, "%5f MHz\n", ret_freq);
556
 
        }
557
 
        if (BATT_INFO_D){
558
 
                err_code = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_REM_PERCENT_BATTERY);
559
 
                current  = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_REM_MINUTES_BATTERY);
560
 
                fprintf(stdout, "Battery: %d %% %d minutes.\n", err_code, current);
561
 
        }
562
 
        if (BAT_STATE){
563
 
                //err_code = send_Action(POWERSAVED_REQUEST_BATTERY_STATE_INFO);
564
 
                err_code = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_BATTERY_STATE_INFO);
565
 
                switch (err_code){
566
 
                case POWERSAVED_BATTERY_STATE_NORMAL:
567
 
                        fprintf(stdout, "NORMAL\n");
568
 
                        ret = 1;
569
 
                        break;
570
 
                case POWERSAVED_BATTERY_STATE_WARNING:
571
 
                        fprintf(stdout, "WARNING\n");
572
 
                        ret = 2;
573
 
                        break;
574
 
                case POWERSAVED_BATTERY_STATE_LOW:
575
 
                        fprintf(stdout, "LOW\n");
576
 
                        ret = 3;
577
 
                        break;
578
 
                case POWERSAVED_BATTERY_STATE_CRITICAL:
579
 
                        fprintf(stdout, "CRITICAL\n");
580
 
                        ret = 4;
581
 
                        break;
582
 
                case POWERSAVED_NO_RIGHTS:
583
 
                        no_connect_no_rights_error();
584
 
                        ret=-2;                 
585
 
                        break;
586
 
                case POWERSAVED_ERROR_SOCKET_ERROR:
587
 
                        no_connect_no_daemon_error();
588
 
                        ret=-3;
589
 
                        break;
590
 
                default:
591
 
                        general_error();
592
 
                        ret=-1;
593
 
                }
594
 
        }
595
 
        if (APM_ACPI){
596
 
                switch (checkACPI()){
 
578
                        ret = -1;
 
579
                        break;
 
580
                case REPLY_SUCCESS:
 
581
                        if (!dbusGetMessageString(reply, &dummy, 0) && dummy != NULL) {
 
582
                                result = dummy; //string(parsed_reply_message._strings[0]);
 
583
                                if (result == "performance") {
 
584
                                        ret = 1;
 
585
                                        cout << "PERFORMANCE" << endl;
 
586
                                } else if (result == "powersave") {
 
587
                                        ret = 2;
 
588
                                        cout << "POWERSAVE" << endl;
 
589
                                } else if (result == "dynamic") {
 
590
                                        ret = 3;
 
591
                                        cout << "DYNAMIC" << endl;
 
592
                                } else {
 
593
                                        ret = -1;
 
594
                                        cout << "Error: unknown reply '" << result << "' from daemon." << endl;
 
595
                                }
 
596
                        } else {
 
597
                                ret = -1;
 
598
                                general_error();
 
599
                        }
 
600
                        dbus_message_unref(reply);
 
601
                        break;
 
602
                case REPLY_NO_RIGHTS:
 
603
                        no_connect_no_rights_error();
 
604
                        ret = -2;
 
605
                        break;
 
606
                default:
 
607
                        printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
 
608
                        ret = -1;
 
609
                }
 
610
        }
 
611
        if (BAT_STATE) {
 
612
                err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BatteryState");
 
613
                switch (err_code) {
 
614
                case REPLY_HW_NOT_SUPPORTED:
 
615
                        cerr << "No Battery." << endl;
 
616
                        ret = -1;
 
617
                        break;
 
618
                case REPLY_SUCCESS:
 
619
                        if (!dbusGetMessageString(reply, &dummy, 0) && dummy != NULL) {
 
620
                                result = dummy;
 
621
                                if (result == "normal") {
 
622
                                        ret = 1;
 
623
                                        cout << "NORMAL" << endl;
 
624
                                } else if (result == "warning") {
 
625
                                        ret = 2;
 
626
                                        cout << "WARNING" << endl;
 
627
                                } else if (result == "low") {
 
628
                                        ret = 3;
 
629
                                        cout << "LOW" << endl;
 
630
                                } else if (result == "critical") {
 
631
                                        ret = 4;
 
632
                                        cout << "CRITICAL" << endl;
 
633
                                }
 
634
                        } else {
 
635
                                ret = -1;
 
636
                                general_error();
 
637
                        }
 
638
                        dbus_message_unref(reply);
 
639
                        break;
 
640
                default:
 
641
                        cerr << "unknown reply code to 'BatteryState' request" << endl;
 
642
                        ret = -1;
 
643
                }
 
644
        }
 
645
        if (SET_ACTIVE_SCHEME) {
 
646
                if (strlen(schemeName) != 0) {
 
647
                        dummy = schemeName;
 
648
                        err_code = dbusSendMessage(ACTION_MESSAGE,
 
649
                                                   "SchemesSet", DBUS_TYPE_STRING, &dummy, DBUS_TYPE_INVALID);
 
650
                        switch (err_code) {
 
651
                        case REPLY_SUCCESS:
 
652
                                printf("Scheme %s successfully activated\n", schemeName);
 
653
                                break;
 
654
                        case REPLY_ALREADY_SET:
 
655
                                printf("Scheme %s already active\n", schemeName);
 
656
                                break;
 
657
                        case REPLY_INVALID_PARAM:
 
658
                                printf("Scheme %s does not exist\n", schemeName);
 
659
                                ret = -1;
 
660
                                break;
 
661
                        default:
 
662
                                printf("Error: %s\n", DBus_Error_Array[err_code]);
 
663
                                general_error();
 
664
                        }
 
665
                }
 
666
        }
 
667
        if (LIST_SCHEMES) {
 
668
                char *_schemes;
 
669
                int active;
 
670
                int battery;
 
671
                int ac_power;
 
672
 
 
673
                err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "SchemesGet");
 
674
                if (err_code == REPLY_SUCCESS) {
 
675
                        if (!dbusGetMessageInteger(reply, &active, 0)
 
676
                            && !dbusGetMessageInteger(reply, &battery, 1)
 
677
                            && !dbusGetMessageInteger(reply, &ac_power, 2))
 
678
                                if (err_code < 0) {
 
679
                                        fprintf(stderr, "Could not connect with daemon\n");
 
680
                                } else {
 
681
                                        for (int x = 0;
 
682
                                             !dbusGetMessageString(reply, &_schemes, x) && dummy != NULL; x++) {
 
683
                                                printf("%s%s%s%s\n",
 
684
                                                       _schemes,
 
685
                                                       ((active == x) ? "\tactive" : ""),
 
686
                                                       ((battery == x) ? "\tbattery_default_scheme" : ""),
 
687
                                                       ((ac_power == x) ? "\tAC_default_scheme" : ""));
 
688
                                        }
 
689
                        } else {
 
690
                                fprintf(stderr, "Could not assign schemes to current/battery/ac_power\n");
 
691
                                ret = -1;
 
692
                        }
 
693
                } else {
 
694
                        general_error();
 
695
                        ret = -1;
 
696
                }
 
697
        }
 
698
        if (GET_SCHEME_DESCRIPTION) {
 
699
                dummy = schemeName;
 
700
                char *description;
 
701
 
 
702
                err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "SchemesGetDescription",
 
703
                                           DBUS_TYPE_STRING, &dummy, DBUS_TYPE_INVALID);
 
704
 
 
705
                switch (err_code) {
 
706
                case REPLY_INVALID_PARAM:
 
707
                        printf("Error: Scheme %s does not exist\n", schemeName);
 
708
                        ret = -1;
 
709
                        break;
 
710
                case REPLY_SUCCESS:
 
711
                        if (!dbusGetMessageString(reply, &description, 0)) {
 
712
                                printf("%s\n", description);
 
713
                        }
 
714
                        else {
 
715
                                general_error();
 
716
                        }
 
717
                        break;
 
718
                default:
 
719
                        printf("Error: %s\n", DBus_Error_Array[err_code]);
 
720
                        general_error();
 
721
                }
 
722
        }
 
723
        if (APM_ACPI) {
 
724
                switch (checkACPI()) {
597
725
                case APM:
598
726
                        fprintf(stdout, "APM\n");
599
727
                        ret = 2;
607
735
                        ret = -1;
608
736
                }
609
737
        }
610
 
        if (BATT_INFO){
611
 
                if (get_bat_info(0) < 0){
612
 
                        fprintf(stderr, "Could not read out battery info properly\n");
613
 
                        ret = -2;
614
 
                }
615
 
        }
616
 
        if (EXT_BATT_INFO){
617
 
                if (get_bat_info(1) < 0){
618
 
                        fprintf(stderr, "Could not read out battery info properly\n");
619
 
                        ret = -2;
620
 
                }
621
 
        }
622
 
        if (AC_INFO){
 
738
        if (BATT_INFO) {
 
739
                if (get_bat_info(0) < 0) {
 
740
                        fprintf(stderr, "Could not read out battery info properly\n");
 
741
                        ret = -2;
 
742
                }
 
743
        }
 
744
        if (EXT_BATT_INFO) {
 
745
                if (get_bat_info(1) < 0) {
 
746
                        fprintf(stderr, "Could not read out battery info properly\n");
 
747
                        ret = -2;
 
748
                }
 
749
        }
 
750
        if (AC_INFO) {
623
751
                states = getACAdapterStatus();
624
 
                if (states == AC_ONLINE){
 
752
                if (states == AC_ONLINE) {
625
753
                        fprintf(stdout, "ONLINE\n");
626
754
                        ret = 2;
627
 
                }
628
 
                else if (states == AC_OFFLINE){
 
755
                } else if (states == AC_OFFLINE) {
629
756
                        fprintf(stdout, "OFFLINE\n");
630
757
                        ret = 1;
631
 
                }
632
 
                else {
 
758
                } else {
633
759
                        fprintf(stderr, "AC state is unknown, assume ONLINE\n");
634
760
                        ret = -1;
635
761
                }
636
762
        }
637
 
        if (THROT_INFO){
638
 
                for (x=0; getThrottlingInfoCPU(x, &states, &current) > 0; x++){
 
763
        if (THROT_INFO) {
 
764
                for (x = 0; getThrottlingInfoCPU(x, &states, &current) > 0; x++) {
639
765
                        fprintf(stdout, "CPU: %d\nThrottling states: %d\nCurrent state: %d\n", x, states, current);
640
766
                }
641
767
                if (x == 0)
642
768
                        fprintf(stderr, "Throttling not supported \n");
643
769
        }
644
 
        if (SET_THROT_PERC){
645
 
                if (getThrottlingInfo(&states, &current) < 0){
 
770
        if (SET_THROT_PERC) {
 
771
                if (getThrottlingInfo(&states, &current) < 0) {
646
772
                        fprintf(stderr, "Throttling not supported\n");
647
773
                        ret = -1;
648
 
                }
649
 
                else {
650
 
                        for (x=0; getThrottlingInfoCPU(x, &states, &current) > 0;x++){
651
 
                                fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", x, states, current);
652
 
                                if (setThrottlingPercentCPU(x, option) < 0){
653
 
                                        fprintf(stderr, "Cannot set throttling state to %d percent for cpu %d", option, x);
 
774
                } else {
 
775
                        for (x = 0; getThrottlingInfoCPU(x, &states, &current) > 0; x++) {
 
776
                                fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", x, states,
 
777
                                        current);
 
778
                                if (setThrottlingPercentCPU(x, option) < 0) {
 
779
                                        fprintf(stderr, "Cannot set throttling state to %d percent for cpu %d", option,
 
780
                                                x);
654
781
                                        ret = -1;
655
782
                                        break;
656
783
                                }
657
784
                        }
658
785
                        usleep(100);
659
786
                        fprintf(stdout, "Changed to:\n");
660
 
                        for (y=0; getThrottlingInfoCPU(y, &states, &current) > 0;y++){
661
 
                                fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", y, states, current);
 
787
                        for (y = 0; getThrottlingInfoCPU(y, &states, &current) > 0; y++) {
 
788
                                fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", y, states,
 
789
                                        current);
662
790
                        }
663
791
                }
664
792
        }
665
 
        if (THERMAL_INFO){
 
793
        if (THERMAL_INFO) {
666
794
                err_code = getThermalZonesNum();
667
 
                if (err_code < 0){
 
795
                if (err_code < 0) {
668
796
                        fprintf(stderr, "No thermal devices available: %d\n", err_code);
669
 
                        ret=-1;
 
797
                        ret = -1;
670
798
                }
671
 
                thermal_dev = (ThermalDev*)malloc(sizeof(ThermalDev));
672
 
                for (x=0; x < err_code && !(getThermalZone(x, thermal_dev) < 0); x++){
 
799
                thermal_dev = (ThermalDev *) malloc(sizeof(ThermalDev));
 
800
                for (x = 0; x < err_code && !(getThermalZone(x, thermal_dev) < 0); x++) {
673
801
                        fprintf(stdout, "Thermal Device no. %d:\n", x);
674
802
                        if (thermal_dev->temperature > 0)
675
803
                                fprintf(stdout, "Temperature: %d\n", thermal_dev->temperature);
676
 
                        if (thermal_dev->state > 0){
 
804
                        if (thermal_dev->state > 0) {
677
805
                                fprintf(stdout, "State: ");
678
 
                                switch (thermal_dev->state){
 
806
                                switch (thermal_dev->state) {
679
807
                                case HOT:
680
808
                                        fprintf(stdout, "HOT\n");
681
809
                                        break;
698
826
                                fprintf(stdout, "Hot: %d\n", thermal_dev->hot);
699
827
                        if (thermal_dev->passive > 0)
700
828
                                fprintf(stdout, "Passive: %d\n", thermal_dev->passive);
701
 
                        for (y = 0;y < MAX_THERMAL_ACTIVE; y++){
 
829
                        for (y = 0; y < MAX_THERMAL_ACTIVE; y++) {
702
830
                                if (thermal_dev->active[y] > 0)
703
831
                                        fprintf(stdout, "Active %d: %d\n", y, thermal_dev->active[y]);
704
832
                        }
705
833
                }
706
834
                free(thermal_dev);
707
835
        }
708
 
        if (FAN_INFO){
 
836
        if (FAN_INFO) {
709
837
                err_code = getFanNum();
710
 
                if (err_code <= 0){
 
838
                if (err_code <= 0) {
711
839
                        fprintf(stderr, "No fan devices available.\n");
712
 
                        ret=-1;
713
 
                }
714
 
                else{
715
 
                        for (x=0; x < err_code && !((current = getFanStatus(x)) < 0); x++)
 
840
                        ret = -1;
 
841
                } else {
 
842
                        for (x = 0; x < err_code && !((current = getFanStatus(x)) < 0); x++)
716
843
                                printf("Fan no %d: %s\n", x, current ? "off" : "on");
717
844
                }
718
845
        }
719
 
        if (LIST_SCHEMES){
720
 
                schemes = (SchemeList*) malloc(sizeof(SchemeList));
721
 
                schemes->beginning = NULL;
722
 
                schemes->current_scheme = NULL;
723
 
                schemes->ac_scheme = NULL;
724
 
                schemes->battery_scheme = NULL;
725
 
                err_code = send_Action_get_all_Schemes(schemes);
726
 
                                
727
 
                if (err_code < 0){
728
 
                        fprintf(stderr, "Could not connect with daemon\n");
729
 
                }
730
 
                else{
731
 
                        if (schemes == NULL || ret < 0)
732
 
                                fprintf(stderr, "No schemes available\n");
733
 
                        else {
734
 
                                for (scheme_node = schemes->beginning; scheme_node != NULL; scheme_node = scheme_node->next){
735
 
                                        printf ("%s%s%s%s\n", 
736
 
                                                scheme_node->name, 
737
 
                                                ((schemes->current_scheme == scheme_node) ? "\tactive" : ""),
738
 
                                                ((schemes->battery_scheme == scheme_node) ? "\tbattery_default_scheme" : ""),
739
 
                                                ((schemes->ac_scheme == scheme_node) ? "\tAC_default_scheme" : ""));
740
 
                                }
741
 
                        }
742
 
                        if (schemes){
743
 
                                destroySchemeList(schemes);
744
 
                                free (schemes);
745
 
                        }
746
 
                }
747
 
        }
748
 
        if (SET_ACTIVE_SCHEME){
749
 
                if (strlen(schemeName) != 0){
750
 
                        err_code = send_Action_ActivateScheme(schemeName);
751
 
                        switch (err_code){
752
 
                        case POWERSAVED_ERROR:
753
 
                                printf("Unknown error, probably wrong packet protocol, this is a bug\n");
754
 
                                ret = -1;
755
 
                                break;
756
 
                        case POWERSAVED_NO_RIGHTS:
757
 
                                no_connect_no_rights_error();
758
 
                                ret = -1;
759
 
                                break;
760
 
                        case POWERSAVED_SCHEME_NOT_SET:
761
 
                                printf("Scheme %s does not exist\n", schemeName);
762
 
                                ret = -1;
763
 
                                break;
764
 
                        case POWERSAVED_SCHEME_ALREADY_SET:
765
 
                                printf("Scheme %s already active\n", schemeName);
766
 
                                /* no error */
767
 
                                break;
768
 
                        case POWERSAVED_ERROR_NOERROR:
769
 
                                printf("Scheme %s successfully activated\n", schemeName);
770
 
                                break;
771
 
                        case POWERSAVED_ERROR_SOCKET_ERROR:
772
 
                                no_connect_no_daemon_error();
773
 
                                ret=-3;
774
 
                                break;
775
 
                        default:
776
 
                                printf("Unknown return value for scheme activation\n");
777
 
                                ret = -1;
778
 
                                break;
779
 
                        }
780
 
                }
781
 
        }
782
 
        if (VERSION_INFO){
 
846
        if (REAL_CPU_SPEED) {
 
847
                ret_freq = getRealProcessorSpeed();
 
848
                if (ret_freq == -2) {
 
849
                        fprintf(stderr, "0\nMachine's architecture is not supported or feature has"
 
850
                                " not been compiled for this machine's architecture(see manpage).\n");
 
851
                        ret = -1;
 
852
                } else if (ret_freq < 0) {
 
853
                        fprintf(stderr, "0\nCould not calculate CPU frequency\n");
 
854
                        ret = -1;
 
855
                } else
 
856
                        fprintf(stdout, "%5f MHz\n", ret_freq);
 
857
        }
 
858
        if (SET_BRIGHTNESS) {
 
859
                char *method = "BrightnessSet";
 
860
                switch (option) {
 
861
                case -1:
 
862
                        method = "BrightnessUp";
 
863
                        break;
 
864
                case -2:
 
865
                        method = "BrightnessDown";
 
866
                        break;
 
867
                default:
 
868
                        break;
 
869
                }
 
870
                err_code = dbusSendMessage(ACTION_MESSAGE, method, DBUS_TYPE_INT32, &option, DBUS_TYPE_INVALID);
 
871
 
 
872
                switch (err_code) {
 
873
                case REPLY_HW_NOT_SUPPORTED:
 
874
                        fprintf(stderr, "Setting of brightness not supported.\n");
 
875
                        ret = -1;
 
876
                        break;
 
877
                case REPLY_ALREADY_SET:
 
878
                        fprintf(stderr, "Brightness level already set.\n");
 
879
                        //noerror
 
880
                        break;
 
881
                case REPLY_SUCCESS:
 
882
                        fprintf(stderr, "Brightness level set to %d.\n", option);
 
883
                        break;
 
884
                case REPLY_NO_RIGHTS:
 
885
                        no_connect_no_rights_error();
 
886
                        ret = -2;
 
887
                        break;
 
888
                default:
 
889
                        general_error();
 
890
                        ret = -1;
 
891
                }
 
892
        }
 
893
        if (GET_BRIGHTNESS) {
 
894
                int level = -1;
 
895
                err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BrightnessGet");
 
896
                switch (err_code) {
 
897
                case REPLY_HW_NOT_SUPPORTED:
 
898
                        fprintf(stderr, "Brightness not supported.\n");
 
899
                        ret = -1;
 
900
                        break;
 
901
                case REPLY_SUCCESS:
 
902
                        if (!dbusGetMessageInteger(reply, &level, 0)) {
 
903
                                printf("Current brightness level: %d.\n", level);
 
904
                        } else {
 
905
                                ret = -1;
 
906
                                general_error();
 
907
                        }
 
908
                        dbus_message_unref(reply);
 
909
                        break;
 
910
                case REPLY_NO_RIGHTS:
 
911
                        no_connect_no_rights_error();
 
912
                        ret = -2;
 
913
                        break;
 
914
                default:
 
915
                        printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
 
916
                        ret = -1;
 
917
                }
 
918
        }
 
919
        if (GET_BRIGHTNESS_LEVELS) {
 
920
                int levels = -1;
 
921
                err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BrightnessLevelsGet");
 
922
                switch (err_code) {
 
923
                case REPLY_HW_NOT_SUPPORTED:
 
924
                        fprintf(stderr, "Brightness not supported.\n");
 
925
                        ret = -1;
 
926
                        break;
 
927
                case REPLY_SUCCESS:
 
928
                        if (!dbusGetMessageInteger(reply, &levels, 0)) {
 
929
                                printf("Available brightness levels: %d.\n", levels);
 
930
                        } else {
 
931
                                ret = -1;
 
932
                                general_error();
 
933
                        }
 
934
                        dbus_message_unref(reply);
 
935
                        break;
 
936
                case REPLY_NO_RIGHTS:
 
937
                        no_connect_no_rights_error();
 
938
                        ret = -2;
 
939
                        break;
 
940
                default:
 
941
                        printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
 
942
                        ret = -1;
 
943
                }
 
944
        }
 
945
        if (DPM_GET_DEVICES) {
 
946
                dummy = dpmDeviceClass;
 
947
 
 
948
                err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "DpmDevicesGet",
 
949
                                                    DBUS_TYPE_STRING, &dummy,
 
950
                                                    DBUS_TYPE_INVALID);
 
951
 
 
952
                if (err_code != REPLY_SUCCESS) {
 
953
                        switch (err_code) {
 
954
                        case REPLY_DISABLED:
 
955
                                fprintf(stderr, "Device power management has been disabled by administrator.\n");
 
956
                                break;
 
957
                        default:
 
958
                                general_error();
 
959
                        }
 
960
                        ret = -1;
 
961
                }
 
962
 
 
963
                char *device;
 
964
                int state = -1;
 
965
                int i = 0;
 
966
                while (!dbusGetMessageString(reply, &device, i)
 
967
                       && !dbusGetMessageInteger(reply, &state, i)) {
 
968
                        printf("Device [%d]: %s\n"
 
969
                               "            power state: %d\n", i, device, state);
 
970
                        i++;
 
971
                }
 
972
        
 
973
        }
 
974
        if (DPM_GET_CLASSES) {
 
975
                err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "DpmClassesGet",
 
976
                                                    DBUS_TYPE_INVALID);
 
977
 
 
978
                if (err_code != REPLY_SUCCESS) {
 
979
                        switch (err_code) {
 
980
                        case REPLY_DISABLED:
 
981
                                fprintf(stderr, "Device power management has been disabled by administrator.\n");
 
982
                                break;
 
983
                        default:
 
984
                                general_error();
 
985
                        }
 
986
                        ret = -1;
 
987
                }
 
988
                
 
989
                char *c;
 
990
                int i = 0;
 
991
                while (!dbusGetMessageString(reply, &c, i)) {
 
992
                        printf("%s\n", c);
 
993
                        i++;
 
994
                }
 
995
        }
 
996
        if (DPM_SUSPEND) {
 
997
                dummy = dpmDeviceClass;
 
998
 
 
999
                err_code = dbusSendMessage(ACTION_MESSAGE, "DpmDevicesSuspend",
 
1000
                                           DBUS_TYPE_STRING, &dummy,
 
1001
                                           DBUS_TYPE_INT32, &option,
 
1002
                                           DBUS_TYPE_INVALID);
 
1003
 
 
1004
 
 
1005
                switch (err_code) {
 
1006
                case REPLY_HW_NOT_SUPPORTED:
 
1007
                        fprintf(stderr, "Runtime powermanagement not supported.\n");
 
1008
                        ret = -1;
 
1009
                        break;
 
1010
                case REPLY_ALREADY_SET:
 
1011
                        fprintf(stderr, "Already suspended.\n");
 
1012
                        //noerror
 
1013
                        break;
 
1014
                case REPLY_SUCCESS:
 
1015
                        fprintf(stderr, "Suspended %s.\n", dpmDeviceClass);
 
1016
                        break;
 
1017
                case REPLY_NO_RIGHTS:
 
1018
                        no_connect_no_rights_error();
 
1019
                        ret = -2;
 
1020
                        break;
 
1021
                case REPLY_DISABLED:
 
1022
                        fprintf(stderr, "Device power management has been disabled by administrator.\n");
 
1023
                        break;
 
1024
                default:
 
1025
                        general_error();
 
1026
                        ret = -1;
 
1027
                }
 
1028
        }
 
1029
        if (DPM_RESUME) {
 
1030
                dummy = dpmDeviceClass;
 
1031
                err_code = dbusSendMessage(ACTION_MESSAGE, "DpmDevicesResume",
 
1032
                                           DBUS_TYPE_STRING, &dummy,
 
1033
                                           DBUS_TYPE_INT32, &option,
 
1034
                                           DBUS_TYPE_INVALID);
 
1035
 
 
1036
                switch (err_code) {
 
1037
                case REPLY_HW_NOT_SUPPORTED:
 
1038
                        fprintf(stderr, "Runtime powermanagement not supported.\n");
 
1039
                        ret = -1;
 
1040
                        break;
 
1041
                case REPLY_ALREADY_SET:
 
1042
                        fprintf(stderr, "Already resumed.\n");
 
1043
                        //noerror
 
1044
                        break;
 
1045
                case REPLY_SUCCESS:
 
1046
                        fprintf(stderr, "Resumed %s.\n", dpmDeviceClass);
 
1047
                        break;
 
1048
                case REPLY_NO_RIGHTS:
 
1049
                        no_connect_no_rights_error();
 
1050
                        ret = -2;
 
1051
                        break;
 
1052
                case REPLY_DISABLED:
 
1053
                        fprintf(stderr, "Device power management has been disabled by administrator.\n");
 
1054
                        break;
 
1055
                default:
 
1056
                        general_error();
 
1057
                        ret = -1;
 
1058
                }
 
1059
        }
 
1060
        if (VERSION_INFO) {
783
1061
#ifdef VERSION
784
 
                printf ("Powersave package version: %s\n", VERSION);
 
1062
                printf("Powersave package version: %s\n", VERSION);
785
1063
#else
786
 
                printf ("Version has not been defined by VERSION at compile time.\n");
 
1064
                printf("Version has not been defined by VERSION at compile time.\n");
787
1065
#endif
788
1066
        }
789
1067
        if (ret == -1)