43
70
static int LIST_SCHEMES = 0;
44
71
static int SET_ACTIVE_SCHEME = 0;
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;
74
static int GET_BRIGHTNESS = 0;
75
static int GET_BRIGHTNESS_LEVELS = 0;
76
static int SET_BRIGHTNESS = 0;
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;
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");
50
void no_connect_no_rights_error(){
90
void no_connect_no_rights_error()
51
92
fprintf(stderr, "Could not connect to powersaved because of missing permissions."
52
93
" Are you priviledged to connect to the powersaved?\n");
55
void no_connect_no_daemon_error(){
96
void no_connect_no_daemon_error()
56
98
fprintf(stderr, "Could not connect to powersaved. Is the powersaved running?\n");
59
void no_cpufreq_error(){
101
void no_cpufreq_error()
60
103
fprintf(stderr, "Speedstepping is not supported.\n");
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"
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"
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"
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"
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"
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"
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);
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;
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;
96
if (sleep > 1 || cpufreq > 1){
159
if (sleep > 1 || cpufreq > 1) {
98
161
exit(EXIT_FAILURE);
100
if (sleep == 1 && (info > 0 || cpufreq > 0)){
163
if (sleep == 1 && (info > 0 || cpufreq > 0)) {
102
165
exit(EXIT_FAILURE);
107
void getArgs(int argc, char** argv){
110
char *endptr[MAX_PARAM_LENGTH +1];
112
struct option opts[] =
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'},
140
int i = getopt_long(argc, argv, "v:p:e:crbdBsSaTFVxuUmhtlfA", opts, &option_index);
144
if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
170
void getArgs(int argc, char **argv)
173
int option_index = 0;
174
char *endptr[MAX_PARAM_LENGTH + 1];
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'},
211
int i = getopt_long(argc, argv, "v:p:e:crbBsSaTFVxuUmhtlfAk:KLi:I:j:J:oX:", opts, &option_index);
220
if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH) {
145
221
fprintf(stderr, "Too long parameter\n");
146
222
exit(EXIT_FAILURE);
151
if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
152
fprintf(stderr, "Too long parameter\n");
227
if (optarg && strlen(optarg) >= MAX_PARAM_LENGTH){
228
fprintf(stderr, "Too long parameter\n");
159
235
CPUFREQ_INFO = 1;
168
241
EXT_BATT_INFO = 1;
204
277
SET_ACTIVE_SCHEME = 1;
205
strcpy (schemeName, optarg);
278
strcpy(schemeName, optarg);
208
281
LIST_SCHEMES = 1;
284
GET_SCHEME_DESCRIPTION = 1;
285
strcpy(schemeName, optarg);
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));
306
else if (*optarg == 'd')
309
option = strtol(optarg, endptr, 10);
315
GET_BRIGHTNESS_LEVELS = 1;
319
strcpy(dpmDeviceClass, optarg);
323
option = strtol(optarg, endptr, 10);
327
strcpy(dpmDeviceClass, optarg);
331
strcpy(dpmDeviceClass, optarg);
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);
232
342
setDebugLevel(verbose);
233
343
//fprintf(stderr, "Debug Level set to %d\n", verbose);
248
int get_bat_info(int ext) {
358
int get_bat_info(int ext)
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;
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.");
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);
267
fprintf(stdout, "Battery: error.\n");
369
if (bg.remaining_percent >= 0) {
370
fprintf(stdout, "Battery: %d %%", bg.remaining_percent);
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);
378
fprintf(stdout, "\n");
268
381
if (ac_state == AC_ONLINE)
269
fprintf (stdout, "AC is online.\n");
382
fprintf(stdout, "AC is online.\n");
274
384
// Test of detailed battery information
276
if (checkACPI() == ACPI){
277
for (x = 0; (getBatteryDetailedInfo(x, &bd) == 0) && x < MAX_BATTERIES ; x++){
278
if (bd.present == PRESENT_YES){
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);
285
else if (full_capacity == 0 || bd.remaining_capacity == 0){
290
fprintf(stdout, "Battery%d: %d\n", x, remPerc);
386
if (checkACPI() == ACPI) {
387
for (x = 0; x < numBatteries(); x++) {
389
int ret = getBatteryInfo(x, &bg);
392
fprintf(stderr, "Battery error. Maybe hal is not running?\n");
399
fprintf(stdout, "Battery%d: not present\n", bat_num);
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);
406
fprintf(stdout,"\n");
295
410
fprintf(stdout, "No acpi system\n");
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
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...)
306
int main(int argc, char** argv)
422
int main(int argc, char **argv)
309
425
int states, current, err_code;
314
430
getArgs(argc, argv);
317
SchemeList* schemes = NULL;
318
SchemeNode* scheme_node = NULL;
320
int64_t daemon_system_states = 0;
321
int64_t supported_sleep_states = 0;
323
/* request daemon system states if one of these are set */
325
if (SUSPEND_TO_RAM || SUSPEND_TO_DISK || STANDBY)
326
supported_sleep_states = getSupportedSleepStates();
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();
336
case POWERSAVED_ERROR_SOCKET_ERROR:
337
no_connect_no_daemon_error();
341
printf ("Could not connect to daemon, unknown error.\n");
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");
354
err_code = send_Action(POWERSAVED_ACTION_SUSPEND_TO_RAM);
356
case POWERSAVED_ERROR_NOERROR:
358
case POWERSAVED_ERROR_SUSPEND_TO_RAM_DISABLED:
359
fprintf(stderr, "Suspend-To-Ram has been disabled by administrator.\n");
362
case POWERSAVED_NO_RIGHTS:
363
no_connect_no_rights_error();
366
case POWERSAVED_ERROR_SOCKET_ERROR:
367
no_connect_no_daemon_error();
377
if (!((supported_sleep_states & APM_STANDBY) ||
378
(supported_sleep_states & ACPI_S1)))
379
fprintf(stderr, "Standby is not supported by BIOS.\n");
381
err_code = send_Action(POWERSAVED_ACTION_STNDBY);
383
case POWERSAVED_ERROR_NOERROR:
385
case POWERSAVED_ERROR_STNDBY_DISABLED:
386
fprintf(stderr, "Standby has been disabled by administrator.\n");
389
case POWERSAVED_NO_RIGHTS:
390
no_connect_no_rights_error();
393
case POWERSAVED_ERROR_SOCKET_ERROR:
394
no_connect_no_daemon_error();
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");
409
err_code = send_Action(POWERSAVED_ACTION_SUSPEND_TO_DISK);
411
case POWERSAVED_ERROR_NOERROR:
413
case POWERSAVED_ERROR_SUSPEND_TO_DISK_DISABLED:
414
fprintf(stderr, "Suspend-To-Disk has been disabled by administrator.\n");
417
case POWERSAVED_NO_RIGHTS:
418
no_connect_no_rights_error();
421
case POWERSAVED_ERROR_SOCKET_ERROR:
422
no_connect_no_daemon_error();
431
if (PERFORMANCE_SPEED){
432
err_code = send_Action(POWERSAVED_ACTION_CPUFREQ_PERFORMANCE);
434
case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
436
dbus_error_init(&error);
441
if (SUSPEND_TO_RAM) {
442
err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "SuspendToRam");
447
fprintf(stderr, "Suspend-To-RAM has been disabled by administrator.\n");
450
case REPLY_HW_NOT_SUPPORTED:
451
fprintf(stderr, "Suspend-To-RAM is not supported.\n");
454
case REPLY_DBUS_ERROR:
455
fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
456
DBus_Error_Array[err_code]);
459
fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
464
err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "Standby");
469
fprintf(stderr, "Standby has been disabled by administrator.\n");
472
case REPLY_HW_NOT_SUPPORTED:
473
fprintf(stderr, "Standby is not supported.\n");
476
case REPLY_DBUS_ERROR:
477
fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
478
DBus_Error_Array[err_code]);
481
fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
484
} else if (SUSPEND_TO_DISK) {
485
err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "SuspendToDisk");
490
fprintf(stderr, "Suspend-To-disk has been disabled by administrator.\n");
493
case REPLY_HW_NOT_SUPPORTED:
494
fprintf(stderr, "Suspend-To-disk is not supported.\n");
497
case REPLY_DBUS_ERROR:
498
fprintf(stderr, "Cannot connect to powersaved. Is the daemon running? (%s)\n",
499
DBus_Error_Array[err_code]);
502
fprintf(stderr, "Error(%d): %s\n", err_code, DBus_Error_Array[err_code]);
506
if (PERFORMANCE_SPEED) {
507
err_code = dbusSendSimpleMessage(ACTION_MESSAGE, "CpufreqPerformance");
509
case REPLY_HW_NOT_SUPPORTED:
435
510
no_cpufreq_error();
438
case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
513
case REPLY_ALREADY_SET:
439
514
fprintf(stderr, "Speed already set to maximum \n");
442
case POWERSAVED_ERROR_NOERROR:
443
518
fprintf(stderr, "Speed set to maximum \n");
445
case POWERSAVED_NO_RIGHTS:
520
case REPLY_NO_RIGHTS:
446
521
no_connect_no_rights_error();
449
case POWERSAVED_ERROR_SOCKET_ERROR:
450
no_connect_no_daemon_error();
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:
535
case REPLY_ALREADY_SET:
536
fprintf(stderr, "Speed already set to minimum.\n");
462
540
fprintf(stderr, "Speed set to minimum.\n");
464
case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
468
case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
469
fprintf(stderr, "Speed already set to minimum.\n");
471
case POWERSAVED_NO_RIGHTS:
542
case REPLY_NO_RIGHTS:
472
543
no_connect_no_rights_error();
475
case POWERSAVED_ERROR_SOCKET_ERROR:
476
no_connect_no_daemon_error();
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");
490
case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
553
case REPLY_HW_NOT_SUPPORTED:
491
554
no_cpufreq_error();
494
case POWERSAVED_CPUFREQ_MODE_ALREADY_SET:
557
case REPLY_ALREADY_SET:
495
558
fprintf(stderr, "Speed already set to dynamic.\n");
497
case POWERSAVED_NO_RIGHTS:
562
fprintf(stderr, "Speed set to dynamic.\n");
564
case REPLY_NO_RIGHTS:
498
565
no_connect_no_rights_error();
501
case POWERSAVED_ERROR_SOCKET_ERROR:
502
no_connect_no_daemon_error();
511
//err_code = send_Action(POWERSAVED_REQUEST_CPUFREQ_STATE_INFO);
512
err_code = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_CPUFREQ_STATE_INFO);
514
case POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED:
574
err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "CpufreqPolicy");
576
case REPLY_HW_NOT_SUPPORTED:
515
577
no_cpufreq_error();
518
case POWERSAVED_CPUFREQ_STATE_PERFORMANCE:
519
fprintf(stdout, "PERFORMANCE\n");
522
case POWERSAVED_CPUFREQ_STATE_POWERSAVE:
523
fprintf(stdout, "POWERSAVE\n");
526
case POWERSAVED_CPUFREQ_STATE_DYNAMIC:
527
fprintf(stdout, "DYNAMIC\n");
530
case POWERSAVED_NO_RIGHTS:
531
no_connect_no_rights_error();
534
case POWERSAVED_ERROR_SOCKET_ERROR:
535
no_connect_no_daemon_error();
544
ret_freq = getRealProcessorSpeed();
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");
550
else if (ret_freq < 0){
551
fprintf(stderr, "0\nCould not calculate CPU frequency\n");
555
fprintf(stdout, "%5f MHz\n", ret_freq);
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);
563
//err_code = send_Action(POWERSAVED_REQUEST_BATTERY_STATE_INFO);
564
err_code = evaluate_States(daemon_system_states, POWERSAVED_REQUEST_BATTERY_STATE_INFO);
566
case POWERSAVED_BATTERY_STATE_NORMAL:
567
fprintf(stdout, "NORMAL\n");
570
case POWERSAVED_BATTERY_STATE_WARNING:
571
fprintf(stdout, "WARNING\n");
574
case POWERSAVED_BATTERY_STATE_LOW:
575
fprintf(stdout, "LOW\n");
578
case POWERSAVED_BATTERY_STATE_CRITICAL:
579
fprintf(stdout, "CRITICAL\n");
582
case POWERSAVED_NO_RIGHTS:
583
no_connect_no_rights_error();
586
case POWERSAVED_ERROR_SOCKET_ERROR:
587
no_connect_no_daemon_error();
596
switch (checkACPI()){
581
if (!dbusGetMessageString(reply, &dummy, 0) && dummy != NULL) {
582
result = dummy; //string(parsed_reply_message._strings[0]);
583
if (result == "performance") {
585
cout << "PERFORMANCE" << endl;
586
} else if (result == "powersave") {
588
cout << "POWERSAVE" << endl;
589
} else if (result == "dynamic") {
591
cout << "DYNAMIC" << endl;
594
cout << "Error: unknown reply '" << result << "' from daemon." << endl;
600
dbus_message_unref(reply);
602
case REPLY_NO_RIGHTS:
603
no_connect_no_rights_error();
607
printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
612
err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BatteryState");
614
case REPLY_HW_NOT_SUPPORTED:
615
cerr << "No Battery." << endl;
619
if (!dbusGetMessageString(reply, &dummy, 0) && dummy != NULL) {
621
if (result == "normal") {
623
cout << "NORMAL" << endl;
624
} else if (result == "warning") {
626
cout << "WARNING" << endl;
627
} else if (result == "low") {
629
cout << "LOW" << endl;
630
} else if (result == "critical") {
632
cout << "CRITICAL" << endl;
638
dbus_message_unref(reply);
641
cerr << "unknown reply code to 'BatteryState' request" << endl;
645
if (SET_ACTIVE_SCHEME) {
646
if (strlen(schemeName) != 0) {
648
err_code = dbusSendMessage(ACTION_MESSAGE,
649
"SchemesSet", DBUS_TYPE_STRING, &dummy, DBUS_TYPE_INVALID);
652
printf("Scheme %s successfully activated\n", schemeName);
654
case REPLY_ALREADY_SET:
655
printf("Scheme %s already active\n", schemeName);
657
case REPLY_INVALID_PARAM:
658
printf("Scheme %s does not exist\n", schemeName);
662
printf("Error: %s\n", DBus_Error_Array[err_code]);
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))
679
fprintf(stderr, "Could not connect with daemon\n");
682
!dbusGetMessageString(reply, &_schemes, x) && dummy != NULL; x++) {
685
((active == x) ? "\tactive" : ""),
686
((battery == x) ? "\tbattery_default_scheme" : ""),
687
((ac_power == x) ? "\tAC_default_scheme" : ""));
690
fprintf(stderr, "Could not assign schemes to current/battery/ac_power\n");
698
if (GET_SCHEME_DESCRIPTION) {
702
err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "SchemesGetDescription",
703
DBUS_TYPE_STRING, &dummy, DBUS_TYPE_INVALID);
706
case REPLY_INVALID_PARAM:
707
printf("Error: Scheme %s does not exist\n", schemeName);
711
if (!dbusGetMessageString(reply, &description, 0)) {
712
printf("%s\n", description);
719
printf("Error: %s\n", DBus_Error_Array[err_code]);
724
switch (checkACPI()) {
598
726
fprintf(stdout, "APM\n");
611
if (get_bat_info(0) < 0){
612
fprintf(stderr, "Could not read out battery info properly\n");
617
if (get_bat_info(1) < 0){
618
fprintf(stderr, "Could not read out battery info properly\n");
739
if (get_bat_info(0) < 0) {
740
fprintf(stderr, "Could not read out battery info properly\n");
745
if (get_bat_info(1) < 0) {
746
fprintf(stderr, "Could not read out battery info properly\n");
623
751
states = getACAdapterStatus();
624
if (states == AC_ONLINE){
752
if (states == AC_ONLINE) {
625
753
fprintf(stdout, "ONLINE\n");
628
else if (states == AC_OFFLINE){
755
} else if (states == AC_OFFLINE) {
629
756
fprintf(stdout, "OFFLINE\n");
633
759
fprintf(stderr, "AC state is unknown, assume ONLINE\n");
638
for (x=0; getThrottlingInfoCPU(x, &states, ¤t) > 0; x++){
764
for (x = 0; getThrottlingInfoCPU(x, &states, ¤t) > 0; x++) {
639
765
fprintf(stdout, "CPU: %d\nThrottling states: %d\nCurrent state: %d\n", x, states, current);
642
768
fprintf(stderr, "Throttling not supported \n");
645
if (getThrottlingInfo(&states, ¤t) < 0){
770
if (SET_THROT_PERC) {
771
if (getThrottlingInfo(&states, ¤t) < 0) {
646
772
fprintf(stderr, "Throttling not supported\n");
650
for (x=0; getThrottlingInfoCPU(x, &states, ¤t) > 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);
775
for (x = 0; getThrottlingInfoCPU(x, &states, ¤t) > 0; x++) {
776
fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", x, states,
778
if (setThrottlingPercentCPU(x, option) < 0) {
779
fprintf(stderr, "Cannot set throttling state to %d percent for cpu %d", option,
659
786
fprintf(stdout, "Changed to:\n");
660
for (y=0; getThrottlingInfoCPU(y, &states, ¤t) > 0;y++){
661
fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", y, states, current);
787
for (y = 0; getThrottlingInfoCPU(y, &states, ¤t) > 0; y++) {
788
fprintf(stdout, "CPU %d:\nThrottling states : %d\nCurrent state: %d\n", y, states,
666
794
err_code = getThermalZonesNum();
668
796
fprintf(stderr, "No thermal devices available: %d\n", err_code);
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) {
680
808
fprintf(stdout, "HOT\n");
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]);
706
834
free(thermal_dev);
709
837
err_code = getFanNum();
711
839
fprintf(stderr, "No fan devices available.\n");
715
for (x=0; x < err_code && !((current = getFanStatus(x)) < 0); x++)
842
for (x = 0; x < err_code && !((current = getFanStatus(x)) < 0); x++)
716
843
printf("Fan no %d: %s\n", x, current ? "off" : "on");
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);
728
fprintf(stderr, "Could not connect with daemon\n");
731
if (schemes == NULL || ret < 0)
732
fprintf(stderr, "No schemes available\n");
734
for (scheme_node = schemes->beginning; scheme_node != NULL; scheme_node = scheme_node->next){
735
printf ("%s%s%s%s\n",
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" : ""));
743
destroySchemeList(schemes);
748
if (SET_ACTIVE_SCHEME){
749
if (strlen(schemeName) != 0){
750
err_code = send_Action_ActivateScheme(schemeName);
752
case POWERSAVED_ERROR:
753
printf("Unknown error, probably wrong packet protocol, this is a bug\n");
756
case POWERSAVED_NO_RIGHTS:
757
no_connect_no_rights_error();
760
case POWERSAVED_SCHEME_NOT_SET:
761
printf("Scheme %s does not exist\n", schemeName);
764
case POWERSAVED_SCHEME_ALREADY_SET:
765
printf("Scheme %s already active\n", schemeName);
768
case POWERSAVED_ERROR_NOERROR:
769
printf("Scheme %s successfully activated\n", schemeName);
771
case POWERSAVED_ERROR_SOCKET_ERROR:
772
no_connect_no_daemon_error();
776
printf("Unknown return value for scheme activation\n");
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");
852
} else if (ret_freq < 0) {
853
fprintf(stderr, "0\nCould not calculate CPU frequency\n");
856
fprintf(stdout, "%5f MHz\n", ret_freq);
858
if (SET_BRIGHTNESS) {
859
char *method = "BrightnessSet";
862
method = "BrightnessUp";
865
method = "BrightnessDown";
870
err_code = dbusSendMessage(ACTION_MESSAGE, method, DBUS_TYPE_INT32, &option, DBUS_TYPE_INVALID);
873
case REPLY_HW_NOT_SUPPORTED:
874
fprintf(stderr, "Setting of brightness not supported.\n");
877
case REPLY_ALREADY_SET:
878
fprintf(stderr, "Brightness level already set.\n");
882
fprintf(stderr, "Brightness level set to %d.\n", option);
884
case REPLY_NO_RIGHTS:
885
no_connect_no_rights_error();
893
if (GET_BRIGHTNESS) {
895
err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BrightnessGet");
897
case REPLY_HW_NOT_SUPPORTED:
898
fprintf(stderr, "Brightness not supported.\n");
902
if (!dbusGetMessageInteger(reply, &level, 0)) {
903
printf("Current brightness level: %d.\n", level);
908
dbus_message_unref(reply);
910
case REPLY_NO_RIGHTS:
911
no_connect_no_rights_error();
915
printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
919
if (GET_BRIGHTNESS_LEVELS) {
921
err_code = dbusSendSimpleMessageWithReply(REQUEST_MESSAGE, &reply, "BrightnessLevelsGet");
923
case REPLY_HW_NOT_SUPPORTED:
924
fprintf(stderr, "Brightness not supported.\n");
928
if (!dbusGetMessageInteger(reply, &levels, 0)) {
929
printf("Available brightness levels: %d.\n", levels);
934
dbus_message_unref(reply);
936
case REPLY_NO_RIGHTS:
937
no_connect_no_rights_error();
941
printf("Error: %d - %s\n", err_code, DBus_Error_Array[err_code]);
945
if (DPM_GET_DEVICES) {
946
dummy = dpmDeviceClass;
948
err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "DpmDevicesGet",
949
DBUS_TYPE_STRING, &dummy,
952
if (err_code != REPLY_SUCCESS) {
955
fprintf(stderr, "Device power management has been disabled by administrator.\n");
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);
974
if (DPM_GET_CLASSES) {
975
err_code = dbusSendMessageWithReply(REQUEST_MESSAGE, &reply, "DpmClassesGet",
978
if (err_code != REPLY_SUCCESS) {
981
fprintf(stderr, "Device power management has been disabled by administrator.\n");
991
while (!dbusGetMessageString(reply, &c, i)) {
997
dummy = dpmDeviceClass;
999
err_code = dbusSendMessage(ACTION_MESSAGE, "DpmDevicesSuspend",
1000
DBUS_TYPE_STRING, &dummy,
1001
DBUS_TYPE_INT32, &option,
1006
case REPLY_HW_NOT_SUPPORTED:
1007
fprintf(stderr, "Runtime powermanagement not supported.\n");
1010
case REPLY_ALREADY_SET:
1011
fprintf(stderr, "Already suspended.\n");
1015
fprintf(stderr, "Suspended %s.\n", dpmDeviceClass);
1017
case REPLY_NO_RIGHTS:
1018
no_connect_no_rights_error();
1021
case REPLY_DISABLED:
1022
fprintf(stderr, "Device power management has been disabled by administrator.\n");
1030
dummy = dpmDeviceClass;
1031
err_code = dbusSendMessage(ACTION_MESSAGE, "DpmDevicesResume",
1032
DBUS_TYPE_STRING, &dummy,
1033
DBUS_TYPE_INT32, &option,
1037
case REPLY_HW_NOT_SUPPORTED:
1038
fprintf(stderr, "Runtime powermanagement not supported.\n");
1041
case REPLY_ALREADY_SET:
1042
fprintf(stderr, "Already resumed.\n");
1046
fprintf(stderr, "Resumed %s.\n", dpmDeviceClass);
1048
case REPLY_NO_RIGHTS:
1049
no_connect_no_rights_error();
1052
case REPLY_DISABLED:
1053
fprintf(stderr, "Device power management has been disabled by administrator.\n");
784
printf ("Powersave package version: %s\n", VERSION);
1062
printf("Powersave package version: %s\n", VERSION);
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");