~cmiller/ubuntu/trusty/icewm/translations-cause-crash-lp447883

« back to all changes in this revision

Viewing changes to src/aapm.cc

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2009-01-26 00:18:14 UTC
  • mfrom: (1.3.1 upstream) (5.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090126001814-ea5ceoy4uroruz72
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
#define BAT_UNKNOWN     0
60
60
#define BAT_CHARGING    1
61
61
#define BAT_DISCHARGING 2
 
62
#define BAT_FULL        3
62
63
 
63
64
 
64
65
void ApmStr(char *s, bool Tool) {
171
172
 
172
173
 
173
174
 
174
 
    if (ACstatus == 0x1)
 
175
    if (ACstatus == 0x1) {
175
176
        if (Tool)
176
177
            strcat(s, _(" - Power"));
177
178
        else
178
179
            strcat(s, _("P"));
179
 
    if ((BATflag & 8))
 
180
    }
 
181
    if ((BATflag & 8)) {
180
182
        if (Tool)
181
183
            strcat(s, _(" - Charging"));
182
184
        else
183
185
            strcat(s, _("C"));
 
186
    }
184
187
}
185
188
 
186
189
int ignore_directory_entry(struct dirent *de) {
212
215
int YApm::ignore_directory_bat_entry(struct dirent *de) {
213
216
    return
214
217
        ignore_directory_entry(de) || \
 
218
        strstr("AC", de->d_name) || \
 
219
        (acpiIgnoreBatteries &&
 
220
         strstr(acpiIgnoreBatteries, de->d_name));
 
221
}
 
222
 
 
223
int YApm::ignore_directory_ac_entry(struct dirent *de) {
 
224
    return
 
225
        ignore_directory_entry(de) || \
 
226
        strstr("BAT", de->d_name) || \
215
227
        (acpiIgnoreBatteries &&
216
228
         strstr(acpiIgnoreBatteries, de->d_name));
217
229
}
418
430
 
419
431
}
420
432
 
 
433
void YApm::SysStr(char *s, bool Tool) {
 
434
    char buf[80], bat_info[250];
 
435
    FILE *fd;
 
436
    //name of the battery
 
437
    char *BATname;
 
438
    //battery is present or absent
 
439
    int BATpresent;
 
440
    //battery status charging/discharging/unknown
 
441
    int BATstatus;
 
442
    //maximal battery capacity (mWh)
 
443
    int BATcapacity_full;
 
444
    //design capacity (mAh)
 
445
    int BATcapacity_design;
 
446
    //current battery capacity (mWh)
 
447
    int BATcapacity_remain;
 
448
    //how much energy is used atm (mW)
 
449
    int BATrate;
 
450
    //time until the battery is discharged (min)
 
451
    int BATtime_remain;
 
452
    //status of ac-adapter online/offline
 
453
    int ACstatus;
 
454
    int i;
 
455
 
 
456
    *s='\0';
 
457
 
 
458
    //assign some default values, in case
 
459
    //the file in /sys/class/power_supply will contain unexpected values
 
460
    ACstatus = -1;
 
461
    if (acpiACName && acpiACName[0] != 0) {
 
462
        strcat3(buf, "/sys/class/power_supply/", acpiACName, "/online", sizeof(buf));
 
463
        fd = fopen(buf, "r");
 
464
        if (fd != NULL) {
 
465
            while (fgets(buf, sizeof(buf), fd)) {
 
466
                    if (strncmp(buf, "1", 1) == 0) {
 
467
                        ACstatus = AC_ONLINE;
 
468
                    }
 
469
                    else if (strncmp(buf, "0", 1) == 0) {
 
470
                        ACstatus = AC_OFFLINE;
 
471
                    }
 
472
                    else {
 
473
                        ACstatus = AC_UNKNOWN;
 
474
                    }
 
475
            }
 
476
            fclose(fd);
 
477
        }
 
478
    }
 
479
 
 
480
    int n = 0;
 
481
    for (i = 0; i < batteryNum; i++) {
 
482
        BATname = acpiBatteries[i]->name;
 
483
        //assign some default values, in case
 
484
        //the files in /sys/class/power_supply will contain unexpected values
 
485
        BATpresent = -1;
 
486
        BATstatus = -1;
 
487
        BATcapacity_full = -1;
 
488
        BATcapacity_design = -1;
 
489
        BATcapacity_remain = -1;
 
490
        BATrate = -1;
 
491
        BATtime_remain = -1;
 
492
 
 
493
        strcat3(buf, "/sys/class/power_supply/", BATname, "/status", sizeof(buf));
 
494
        fd = fopen(buf, "r");
 
495
        if (fd != NULL && fgets(buf, sizeof(buf), fd)) {
 
496
                if (strncasecmp(buf, "charging", 8) == 0) {
 
497
                        BATstatus = BAT_CHARGING;
 
498
                    }
 
499
                    else if (strncasecmp(buf, "discharging", 11) == 0) {
 
500
                        BATstatus = BAT_DISCHARGING;
 
501
                    }
 
502
                    else if (strncasecmp(buf, "full", 4) == 0) {
 
503
                        BATstatus = BAT_FULL;
 
504
                    }
 
505
                    else {
 
506
                        BATstatus = BAT_UNKNOWN;
 
507
                    }
 
508
                fclose(fd);
 
509
        }
 
510
        strcat3(buf, "/sys/class/power_supply/", BATname, "/current_now", sizeof(buf));
 
511
        fd = fopen(buf, "r");
 
512
        if (fd != NULL && fgets(buf, sizeof(buf), fd)) {
 
513
                //In case it contains non-numeric value
 
514
                if (sscanf(buf,"%d", &BATrate) <= 0) {
 
515
                    BATrate = -1;
 
516
                }
 
517
                fclose(fd);
 
518
        }
 
519
        strcat3(buf, "/sys/class/power_supply/", BATname, "/energy_now", sizeof(buf));
 
520
        fd = fopen(buf, "r");
 
521
        if (fd == NULL) {
 
522
            strcat3(buf, "/sys/class/power_supply/", BATname, "/charge_now", sizeof(buf));
 
523
            fd = fopen(buf, "r");
 
524
        }
 
525
        if (fd != NULL && fgets(buf, sizeof(buf), fd)) {
 
526
                //In case it contains non-numeric value
 
527
                if (sscanf(buf,"%d", &BATcapacity_remain) <= 0) {
 
528
                    BATcapacity_remain = -1;
 
529
                }
 
530
                fclose(fd);
 
531
        }
 
532
        strcat3(buf, "/sys/class/power_supply/", BATname, "/present", sizeof(buf));
 
533
        fd = fopen(buf, "r");
 
534
        if (fd != NULL && fgets(buf, sizeof(buf), fd)) {
 
535
                if (strncmp(buf, "1", 1) == 0) {
 
536
                    BATpresent = BAT_PRESENT;
 
537
                }
 
538
                else {
 
539
                    BATpresent = BAT_ABSENT;
 
540
                }
 
541
                fclose(fd);
 
542
        }
 
543
 
 
544
        if (BATpresent == BAT_PRESENT) { //battery is present now
 
545
            if (acpiBatteries[i]->present == BAT_ABSENT) { //and previously was absent
 
546
                //read full-capacity value
 
547
                strcat3(buf, "/sys/class/power_supply/", BATname, "/energy_full_design", sizeof(buf));
 
548
                fd = fopen(buf, "r");
 
549
                if (fd == NULL) {
 
550
                    strcat3(buf, "/sys/class/power_supply/", BATname, "/charge_full_design", sizeof(buf));
 
551
                    fd = fopen(buf, "r");
 
552
                }
 
553
                if (fd != NULL) {
 
554
                    if (fgets(buf, sizeof(buf), fd)) {
 
555
                            //in case it contains non-numeric value
 
556
                            if (sscanf(buf, "%d", &BATcapacity_design)<=0) {
 
557
                                BATcapacity_design = -1;
 
558
                            }
 
559
                    }
 
560
                    fclose(fd);
 
561
                }
 
562
                strcat3(buf, "/sys/class/power_supply/", BATname, "/energy_full", sizeof(buf));
 
563
                fd = fopen(buf, "r");
 
564
                if (fd == NULL) {
 
565
                    strcat3(buf, "/sys/class/power_supply/", BATname, "/charge_full", sizeof(buf));
 
566
                    fd = fopen(buf, "r");
 
567
                }
 
568
                if (fd != NULL) {
 
569
                    if (fgets(buf, sizeof(buf), fd)) {
 
570
                            //in case it contains non-numeric value
 
571
                            if (sscanf(buf, "%d", &BATcapacity_full)<=0) {
 
572
                                BATcapacity_full = -1;
 
573
                            }
 
574
                    }
 
575
                    fclose(fd);
 
576
                }
 
577
                if (BATcapacity_remain > BATcapacity_full && BATcapacity_design > 0)
 
578
                        BATcapacity_full = BATcapacity_design;
 
579
                acpiBatteries[i]->capacity_full = BATcapacity_full;
 
580
            }
 
581
            else {
 
582
                BATcapacity_full = acpiBatteries[i]->capacity_full;
 
583
            }
 
584
        }
 
585
        acpiBatteries[i]->present = BATpresent;
 
586
 
 
587
        if (!Tool &&
 
588
            taskBarShowApmTime &&
 
589
            BATpresent == BAT_PRESENT &&
 
590
            //bios calculates remaining time, only while discharging
 
591
            BATstatus == BAT_DISCHARGING &&
 
592
            //did we parse the needed values successfully?
 
593
            BATcapacity_full >= 0 && BATcapacity_remain >= 0 && BATrate > 0) {
 
594
            BATtime_remain = (int) (60 * (double)(BATcapacity_remain) / BATrate);
 
595
            sprintf(bat_info, "%d:%02d", BATtime_remain / 60, BATtime_remain % 60);
 
596
        }
 
597
        else if (BATpresent == BAT_PRESENT &&
 
598
                 //did we parse the needed values successfully?
 
599
                 BATcapacity_remain >= 0 && BATcapacity_full >= 0)
 
600
        {
 
601
            sprintf(bat_info, "%3.0f%%",
 
602
                    100 * (double)BATcapacity_remain / BATcapacity_full);
 
603
        }
 
604
        else {
 
605
            //battery is absent or we didn't parse some needed values
 
606
            bat_info[0] = 0;
 
607
        }
 
608
 
 
609
        if (BATstatus == BAT_CHARGING) {
 
610
            if (Tool)
 
611
                strcat(bat_info, _(" - Charging"));
 
612
            else
 
613
                strcat(bat_info, _("C"));
 
614
        }
 
615
 
 
616
        if ((n > 0) && (*bat_info)) {
 
617
            if (Tool)
 
618
                strcat(s, " / ");
 
619
            else
 
620
                strcat(s, "/");
 
621
        }
 
622
        n++;
 
623
        strcat(s, bat_info);
 
624
    }
 
625
 
 
626
    if (ACstatus == AC_ONLINE) {
 
627
        if (Tool)
 
628
            strcat(s,_(" - Power"));
 
629
        else {
 
630
///            if (!prettyClock) strcat(s, " ");
 
631
            strcat(s,_("P"));
 
632
        }
 
633
    }
 
634
 
 
635
}
 
636
 
421
637
void YApm::PmuStr(char *s, const bool tool_tip)
422
638
{
423
639
   #warning Why the string s is "  "?
523
739
    struct dirent **de;
524
740
    int n, i;
525
741
    FILE *pmu_info;
 
742
                    char buf[80];
 
743
                    FILE *fd;
526
744
 
527
745
    batteryNum = 0;
528
746
    acpiACName = 0;
529
747
    fCurrentState = 0;
530
748
 
531
749
    //search for acpi info first
532
 
    n = scandir("/proc/acpi/battery", &de, 0, alphasort);
 
750
    n = scandir("/sys/class/power_supply", &de, 0, alphasort);
 
751
    if (n < 0) {
 
752
        n = scandir("/proc/acpi/battery", &de, 0, alphasort);
 
753
        //use sysfs info
 
754
        if (n > 0) mode = ACPI;
 
755
        }
 
756
    //use acpi info
 
757
    else mode = SYSFS;
533
758
    if (n > 0) {
534
 
        //use acpi info
535
 
        mode = ACPI;
536
 
 
537
759
        //scan for batteries
538
760
        i = 0;
539
761
        while (i < n && batteryNum < MAX_ACPI_BATTERY_NUM) {
 
762
            if (mode == SYSFS) 
 
763
            {
 
764
                    strcat3(buf, "/sys/class/power_supply/", de[i]->d_name, "/online", sizeof(buf));
 
765
                    fd = fopen(buf, "r");
 
766
                    if (fd != NULL) {
 
767
                         fclose(fd);
 
768
                         free(de[i]);
 
769
                         i++;
 
770
                         continue;
 
771
                    }
 
772
            }
540
773
            if (!ignore_directory_bat_entry(de[i])) {
541
774
                //found a battery
542
775
                acpiBatteries[batteryNum] =
556
789
        free(de);
557
790
 
558
791
        //scan for first ac_adapter
559
 
        n = scandir("/proc/acpi/ac_adapter", &de, 0, alphasort);
 
792
        if (mode == ACPI) 
 
793
            n = scandir("/proc/acpi/ac_adapter", &de, 0, alphasort);
 
794
        else if (mode == SYSFS) 
 
795
            n = scandir("/sys/class/power_supply", &de, 0, alphasort);
560
796
        if (n > 0) {
561
797
            i = 0;
562
798
            while (i < n) {
563
 
                if (!ignore_directory_entry(de[i])) {
564
 
                    //found an ac_adapter
565
 
                    acpiACName = (char*)calloc(strlen(de[i]->d_name) + 1, sizeof(char));
566
 
                    strcpy(acpiACName, de[i]->d_name);
567
 
                    break;
 
799
                if (mode == SYSFS) {
 
800
 
 
801
                    strcat3(buf, "/sys/class/power_supply/", de[i]->d_name, "/online", sizeof(buf));
 
802
                    fd = fopen(buf, "r");
 
803
                    if (fd != NULL) {
 
804
                        acpiACName = (char*)calloc(strlen(de[i]->d_name) + 1, sizeof(char));
 
805
                        strcpy(acpiACName, de[i]->d_name);
 
806
                        fclose(fd);
 
807
                        break;
 
808
                    }
 
809
                } else {
 
810
                    if (!ignore_directory_ac_entry(de[i])) {
 
811
                        //found an ac_adapter
 
812
                        acpiACName = (char*)calloc(strlen(de[i]->d_name) + 1, sizeof(char));
 
813
                        strcpy(acpiACName, de[i]->d_name);
 
814
                        break;
 
815
                    }
568
816
                }
569
817
                free(de[i]);
570
818
                i++;
610
858
YApm::~YApm() {
611
859
    int i;
612
860
    delete apmTimer; apmTimer = 0;
613
 
    if (ACPI == mode) {
 
861
    if (ACPI == mode || mode == SYSFS) {
614
862
        for (i=0; i<batteryNum; i++) {
615
863
            free(acpiBatteries[i]->name);
616
864
            free(acpiBatteries[i]);
632
880
 
633
881
    //estimate applet's size
634
882
    for (i = 0; i < batteryNum; i++) {
635
 
        if (mode == ACPI && acpiBatteries[i]->present == BAT_ABSENT)
 
883
        if ((mode == ACPI || mode == SYSFS) && acpiBatteries[i]->present == BAT_ABSENT)
636
884
            continue;
637
885
        if (taskBarShowApmTime)
638
886
            strcat(buf, "0:00");
656
904
    case ACPI:
657
905
        AcpiStr(s,0);
658
906
        break;
 
907
    case SYSFS:
 
908
        SysStr(s,0);
 
909
        break;
659
910
    case APM:
660
911
        ApmStr(s,0);
661
912
        break;