From 9e6b36460a6e9d3643b709c0770f1f162890287f Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 30 Nov 2018 22:47:10 -0500 Subject: [PATCH 3/7] Remove deprecated /proc/acpi support in favor of sysfs. --- panel-plugin/libacpi.c | 364 +---------------------------------------- 1 file changed, 7 insertions(+), 357 deletions(-) diff --git a/panel-plugin/libacpi.c b/panel-plugin/libacpi.c index efb5c9c..df05185 100644 --- a/panel-plugin/libacpi.c +++ b/panel-plugin/libacpi.c @@ -58,9 +58,6 @@ static int acpifd; #include "libacpi.h" -#define ACBASE "/proc/acpi/ac_adapter" - - static char batteries[MAXBATT][128]; static char battinfo[MAXBATT][128]; /* path to AC adapter because not all AC adapter are listed @@ -207,7 +204,7 @@ int check_acpi_sysfs(void) if (sysfs == 0) { #ifdef DEBUG - printf("DBG:No acpi support for sysfs. Trying procfs...\n"); + printf("DBG:No acpi support for sysfs.\n"); #endif return 2; } @@ -245,7 +242,7 @@ int check_acpi_sysfs(void) if ( batt_count == 0 ) { #ifdef DEBUG - printf("DBG:No acpi support for sysfs. Trying procfs...\n"); + printf("DBG:No acpi support for sysfs.\n"); #endif acpi_sysfs = 0; return 2; @@ -277,63 +274,7 @@ fopen_glob(const char *name, const char *mode) int check_acpi(void) { #ifdef __linux__ - if ( check_acpi_sysfs() == 0 ) - return 0; -#endif - DIR *battdir; - struct dirent *batt; - char *name; -#ifdef __linux__ - FILE *acpi; - - if (!(acpi = fopen ("/proc/acpi/info", "r")) - && !(acpi = fopen ("/sys/module/acpi/parameters/acpica_version", "r"))) - { -#ifdef DEBUG - printf("DBG:no acpi: /proc/acpi/info or " - "/sys/module/acpi/parameters/acpica_version not found!\n"); -#endif - return 1; - } - - /* yep, all good */ - fclose (acpi); - - /* now enumerate batteries */ - batt_count = 0; - battdir = opendir ("/proc/acpi/battery"); - if (battdir == 0) - { -#ifdef DEBUG - printf("DBG:No battery. /proc/acpi/battery not found!\n"); -#endif - return 2; - } - while ((batt = readdir (battdir))) - { - name = batt->d_name; - - /* skip . and .. */ - if (!strncmp (".", name, 1) || !strncmp ("..", name, 2)) continue; - - sprintf (batteries[batt_count], "/proc/acpi/battery/%s/state", name); - if (!(acpi = fopen (batteries[batt_count], "r"))) { - sprintf (batteries[batt_count], "/proc/acpi/battery/%s/status", name); - } - else fclose (acpi); - - sprintf (battinfo[batt_count], "/proc/acpi/battery/%s/info", name); -#ifdef DEBUG - printf("DBG:battery number %d at:\n",batt_count); - printf("DBG:info->%s\n",battinfo[batt_count]); - printf("DBG:state->%s\n",batteries[batt_count]); - printf("DBG:------------------------\n"); -#endif - - batt_count++; - } - closedir (battdir); - return 0; + return check_acpi_sysfs(); #else #ifdef HAVE_SYSCTL { @@ -434,66 +375,7 @@ int read_acad_state_sysfs(void) int read_acad_state(void) { #ifdef __linux__ - if (acpi_sysfs) - return read_acad_state_sysfs(); - FILE *acpi; - char *ptr; - char stat; - - char acpath[64]; - char *name; - DIR *acdir; - struct dirent *ac; - - if (!(acdir=opendir(ACBASE))){ - return -1; - } - while ((ac = readdir (acdir))){ - name = ac->d_name; - - /* skip . and .. */ - if (name[0] == '.') - continue; - - sprintf (acpath, "%s/%s/state", ACBASE, name); - if (access(acpath,R_OK)){ - sprintf (acpath, "%s/%s/status", ACBASE, name); - if (access(acpath,R_OK)){ - return -1; - } - } - break; //only one ac adapter supported - } - closedir(acdir); - acpi = fopen (acpath, "r"); - - fread (buf, 512, 1, acpi); - fclose (acpi); - if (!acadstate) acadstate=(ACADstate *)malloc(sizeof(ACADstate)); - - if ( (ptr = strstr(buf, "state:")) ) - { - stat = *(ptr + 26); - if (stat == 'n') acadstate->state = 1; - if (stat == 'f') - { - acadstate->state = 0; - return 0; - } - } - - if ( (ptr = strstr (buf, "Status:")) ) - { - stat = *(ptr + 26); - if (stat == 'n') acadstate->state = 1; - if (stat == 'f') - { - acadstate->state = 0; - return 0; - } - } - - return 1; + return acpi_sysfs ? read_acad_state_sysfs() : 0; #else #ifdef HAVE_SYSCTL static char buf[BUFSIZ]; @@ -594,121 +476,7 @@ int read_acpi_info(int battery) return 0; } - if (acpi_sysfs) - return read_acpi_info_sysfs(battery); - - FILE *acpi; - char *ptr; - char stat; - int temp; - - if (!(acpi = fopen (battinfo[battery], "r"))) { -#ifdef DEBUG - printf("DBG:cannot open %s for read!\n",battinfo[battery]); -#endif - return 0; - } - -#ifdef DEBUG - { - int jj= fread (buf, 1,512, acpi); - printf("DBG:%d characters read from %s\n",jj,battinfo[battery]); - } -#else - fread (buf, 1,512, acpi); -#endif - fclose (acpi); - - if (!acpiinfo) acpiinfo=(ACPIinfo *)malloc(sizeof(ACPIinfo)); - - if ((ptr = strstr (buf, "present:")) || (ptr = strstr (buf, "Present:"))) - { -#ifdef DEBUG - printf("DBG:Battery present... and its called %s\n",battinfo[battery]); -#endif - stat = *(ptr + 25); - if (stat == 'y') - { - acpiinfo->present = 1; - if ((ptr = strstr (buf, "design capacity:")) || (ptr = strstr (buf, "Design Capacity:"))) - { - ptr += 25; - sscanf (ptr, "%d", &temp); - acpiinfo->design_capacity = temp; -#ifdef DEBUG - printf("DBG:design capacity:%d\n",temp); -#endif - } - if ((ptr = strstr (buf, "last full capacity:")) || (ptr = strstr (buf, "Last Full Capacity:"))) - { - ptr += 25; - sscanf (ptr, "%d", &temp); - acpiinfo->last_full_capacity = temp; -#ifdef DEBUG - printf("DBG:last full capacity:%d\n",temp); -#endif - } - if ((ptr = strstr (buf, "battery technology:")) || (ptr = strstr (buf, "Battery Technology:"))) - { - stat = *(ptr + 25); - switch (stat) - { - case 'n': - acpiinfo->battery_technology = 1; - break; - case 'r': - acpiinfo->battery_technology = 0; - break; - } - } - if ((ptr = strstr (buf, "design voltage:")) || (ptr = strstr (buf, "Design Voltage:"))) - { - ptr += 25; - sscanf (ptr, "%d", &temp); - acpiinfo->design_voltage = temp; -#ifdef DEBUG - printf("DBG:design voltage:%d\n",temp); -#endif - } - if ((ptr = strstr (buf, "design capacity warning:")) || (ptr = strstr (buf, "Design Capacity Warning:"))) - { - ptr += 25; - sscanf (ptr, "%d", &temp); - acpiinfo->design_capacity_warning = temp; -#ifdef DEBUG - printf("DBG:design capacity warning:%d\n",temp); -#endif - } - if ((ptr = strstr (buf, "design capacity low:")) || (ptr = strstr (buf, "Design Capacity Low:"))) - { - ptr += 25; - sscanf (ptr, "%d", &temp); - acpiinfo->design_capacity_low = temp; -#ifdef DEBUG - printf("DBG:design capacity low:%d\n",temp); -#endif - } -#ifdef DEBUG - printf("DBG:ALL Battery information read...\n"); -#endif - } - else /* Battery not present */ - { -#ifdef DEBUG - printf("DBG:Battery not present!... and its called %s\n",battinfo[battery]); -#endif - acpiinfo->present = 0; - acpiinfo->design_capacity = 0; - acpiinfo->last_full_capacity = 0; - acpiinfo->battery_technology = 0; - acpiinfo->design_voltage = 0; - acpiinfo->design_capacity_warning = 0; - acpiinfo->design_capacity_low = 0; - return 0; - } - } - - return 1; + return acpi_sysfs ? read_acpi_info_sysfs(battery) : 0; #else #ifdef HAVE_SYSCTL static char buf[BUFSIZ]; @@ -837,112 +605,7 @@ int read_acpi_state_sysfs(int battery) int read_acpi_state(int battery) { #ifdef __linux__ - if (acpi_sysfs) - return read_acpi_state_sysfs(battery); - - FILE *acpi; - char *ptr; - char stat; - - int percent = 100; /* battery percentage */ - int ptemp, rate, rtime = 0; - - if (!(acpi = fopen (batteries[battery], "r"))) { -#ifdef DEBUG - printf("DBG:Could not open %s (%d)\n",batteries[battery],battery); -#endif - return 0; - } - - fread (buf, 512, 1, acpi); - fclose (acpi); - if (!acpistate) acpistate=(ACPIstate *)malloc(sizeof(ACPIstate)); - - if ((ptr = strstr (buf, "present:")) || (ptr = strstr (buf, "Present:"))) - { -#ifdef DEBUG - printf("DBG:Battery state present...\n"); -#endif - stat = *(ptr + 25); - if (stat == 'y') - { - acpistate->present = 1; - if ((ptr = strstr (buf, "charging state:")) || (ptr = strstr (buf, "State:"))) - { - stat = *(ptr + 25); - switch (stat) - { - case 'd': - acpistate->state = 1; - break; - case 'c': - if (*(ptr + 33) == '/') - acpistate->state = 0; - else - acpistate->state = 2; - break; - case 'u': - acpistate->state = 3; - break; - } - } - /* This section of the code will calculate "percentage remaining" - * using battery capacity, and the following formula - * (acpi spec 3.9.2): - * - * percentage = (current_capacity / last_full_capacity) * 100; - * - */ - if ((ptr = strstr (buf, "remaining capacity:")) || (ptr = strstr (buf, "Remaining Capacity:"))) - { - ptr += 25; - sscanf (ptr, "%d", &ptemp); - acpistate->rcapacity = ptemp; - percent = (float) ((float) ptemp / (float) acpiinfo->last_full_capacity) * 100; - acpistate->percentage = percent; -#ifdef DEBUG - printf("DBG:remaining capacity:100 * %d/%d = %d\n", - ptemp,acpiinfo->last_full_capacity,acpistate->percentage); -#endif - } - if ((ptr = strstr (buf, "present rate:")) || (ptr = strstr (buf, "Present Rate:"))) - { - ptr += 25; - sscanf (ptr, "%d", &rate); - - /* if something wrong */ - if (rate <= 0) rate = 0; - - acpistate->prate = rate; - - /* time remaining in minutes */ - rtime = ((float) ((float) acpistate->rcapacity / - (float) acpistate->prate)) * 60; - if (rtime <= 0) rtime = 0; - - acpistate->rtime = rtime; - } - if ((ptr = strstr (buf, "present voltage:")) || (ptr = strstr (buf, "Battery Voltage:"))) - { - ptr += 25; - sscanf (ptr, "%d", &ptemp); - acpistate->pvoltage = ptemp; - } - } - else /* Battery not present */ - { - acpistate->present = 0; - acpistate->state = UNKNOW; - acpistate->prate = 0; - acpistate->rcapacity = 0; - acpistate->pvoltage = 0; - acpistate->rtime = 0; - acpistate->percentage = 0; - return 0; - } - } - - return 1; + return acpi_sysfs ? read_acpi_state_sysfs(battery) : 0; #else #ifdef HAVE_SYSCTL char *string; @@ -1052,23 +715,10 @@ const char *get_temperature(void) { #ifdef __linux__ FILE *fp; - char *proc_temperature="/proc/acpi/thermal_zone/*/temperature"; char *sys_temperature="/sys/class/thermal/thermal_zone*/temp"; static char *p,*p2,line[256]; - if ( (fp=fopen_glob(proc_temperature, "r")) != NULL ) - { - fgets(line,255,fp); - fclose(fp); - p=strtok(line," "); - if (!p) return NULL; - p=p+strlen(p)+1; - while (p && *p ==' ') p++; - if (*p==0) return NULL; - if (strchr(p,'\n')) p=strtok(p,"\n"); - return (const char *)p; - } - else if ( (fp=fopen_glob(sys_temperature, "r")) != NULL ) + if ( (fp=fopen_glob(sys_temperature, "r")) != NULL ) { fgets(line,255,fp); fclose(fp); -- 2.19.1