prop_dictionary_t prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top) { prop_object_iterator_t i; prop_dictionary_t d; prop_object_t ko, o; prop_dictionary_keysym_t k; const char *key; d = prop_dictionary_copy_mutable(bottom); i = prop_dictionary_iterator(top); while ((ko = prop_object_iterator_next(i)) != NULL) { k = (prop_dictionary_keysym_t)ko; key = prop_dictionary_keysym_cstring_nocopy(k); o = prop_dictionary_get_keysym(top, k); if (o == NULL || !prop_dictionary_set(d, key, o)) { prop_object_release((prop_object_t)d); d = NULL; break; } } prop_object_iterator_release(i); prop_dictionary_make_immutable(d); return d; }
static const char * acpi_debug_getkey(prop_dictionary_t dict, uint32_t arg) { prop_object_iterator_t i; prop_object_t obj, val; const char *key; uint32_t num; i = prop_dictionary_iterator(dict); while ((obj = prop_object_iterator_next(i)) != NULL) { key = prop_dictionary_keysym_cstring_nocopy(obj); val = prop_dictionary_get(dict, key); num = prop_number_unsigned_integer_value(val); if (arg == num) return key; } return "UNKNOWN"; }
/* * Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or * the user provided path) and returns the temperature in degree celcius. * */ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) { char *outwalk = buffer; #ifdef THERMAL_ZONE const char *walk; bool colorful_output = false; char *thermal_zone; if (path == NULL) asprintf(&thermal_zone, THERMAL_ZONE, zone); else { static glob_t globbuf; if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) die("glob() failed\n"); if (globbuf.gl_pathc == 0) { /* No glob matches, the specified path does not contain a wildcard. */ asprintf(&thermal_zone, path, zone); } else { /* glob matched, we take the first match and ignore the others */ asprintf(&thermal_zone, "%s", globbuf.gl_pathv[0]); } globfree(&globbuf); } INSTANCE(thermal_zone); for (walk = format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; continue; } if (BEGINS_WITH(walk + 1, "degrees")) { #if defined(LINUX) static char buf[16]; long int temp; if (!slurp(thermal_zone, buf, sizeof(buf))) goto error; temp = strtol(buf, NULL, 10); if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0) *(outwalk++) = '?'; else { if ((temp / 1000) >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } outwalk += sprintf(outwalk, "%ld", (temp / 1000)); if (colorful_output) { END_COLOR; colorful_output = false; } } #elif defined(__DragonFly__) struct sensor th_sensor; size_t th_sensorlen; th_sensorlen = sizeof(th_sensor); if (sysctlbyname(thermal_zone, &th_sensor, &th_sensorlen, NULL, 0) == -1) { perror("sysctlbyname"); goto error; } if (MUKTOC(th_sensor.value) >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } outwalk += sprintf(outwalk, "%.2f", MUKTOC(th_sensor.value)); if (colorful_output) { END_COLOR; colorful_output = false; } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) int sysctl_rslt; size_t sysctl_size = sizeof(sysctl_rslt); if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0)) goto error; if (TZ_AVG(sysctl_rslt) >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt)); if (colorful_output) { END_COLOR; colorful_output = false; } #elif defined(__OpenBSD__) struct sensordev sensordev; struct sensor sensor; size_t sdlen, slen; int dev, numt, mib[5] = {CTL_HW, HW_SENSORS, 0, 0, 0}; sdlen = sizeof(sensordev); slen = sizeof(sensor); for (dev = 0;; dev++) { mib[2] = dev; if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) { if (errno == ENXIO) continue; if (errno == ENOENT) break; goto error; } /* 'path' is the node within the full path (defaults to acpitz0). */ if (BEGINS_WITH(sensordev.xname, thermal_zone)) { mib[3] = SENSOR_TEMP; /* Limit to temo0, but should retrieve from a full path... */ for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) { mib[4] = numt; if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) { if (errno != ENOENT) { warn("sysctl"); continue; } } if ((int)MUKTOC(sensor.value) >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value)); if (colorful_output) { END_COLOR; colorful_output = false; } } } } #elif defined(__NetBSD__) int fd, rval; bool err = false; prop_dictionary_t dict; prop_array_t array; prop_object_iterator_t iter; prop_object_iterator_t iter2; prop_object_t obj, obj2, obj3; fd = open("/dev/sysmon", O_RDONLY); if (fd == -1) goto error; rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict); if (rval == -1) { err = true; goto error_netbsd1; } /* No drivers registered? */ if (prop_dictionary_count(dict) == 0) { err = true; goto error_netbsd2; } iter = prop_dictionary_iterator(dict); if (iter == NULL) { err = true; goto error_netbsd2; } /* iterate over the dictionary returned by the kernel */ while ((obj = prop_object_iterator_next(iter)) != NULL) { /* skip this dict if it's not what we're looking for */ if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) != strlen(thermal_zone)) || (strncmp(thermal_zone, prop_dictionary_keysym_cstring_nocopy(obj), strlen(thermal_zone)) != 0)) continue; array = prop_dictionary_get_keysym(dict, obj); if (prop_object_type(array) != PROP_TYPE_ARRAY) { err = true; goto error_netbsd3; } iter2 = prop_array_iterator(array); if (!iter2) { err = true; goto error_netbsd3; } /* iterate over array of dicts specific to target sensor */ while ((obj2 = prop_object_iterator_next(iter2)) != NULL) { obj3 = prop_dictionary_get(obj2, "cur-value"); float temp = MUKTOC(prop_number_integer_value(obj3)); if ((int)temp >= max_threshold) { START_COLOR("color_bad"); colorful_output = true; } outwalk += sprintf(outwalk, "%.2f", temp); if (colorful_output) { END_COLOR; colorful_output = false; } break; } prop_object_iterator_release(iter2); } error_netbsd3: prop_object_iterator_release(iter); error_netbsd2: prop_object_release(dict); error_netbsd1: close(fd); if (err) goto error; #endif walk += strlen("degrees"); } } free(thermal_zone); OUTPUT_FULL_TEXT(buffer); return; error: free(thermal_zone); #endif OUTPUT_FULL_TEXT("can't read temp"); (void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr); }
static int swsensor_init(void *arg) { int error, val = 0; const char *key, *str; prop_dictionary_t pd = (prop_dictionary_t)arg; prop_object_t po, obj; prop_object_iterator_t iter; prop_type_t type; const struct sme_descr_entry *descr; swsensor_sme = sysmon_envsys_create(); if (swsensor_sme == NULL) return ENOTTY; swsensor_sme->sme_name = "swsensor"; swsensor_sme->sme_cookie = &swsensor_edata; swsensor_sme->sme_refresh = swsensor_refresh; swsensor_sme->sme_set_limits = NULL; swsensor_sme->sme_get_limits = NULL; /* Set defaults in case no prop dictionary given */ swsensor_edata.units = ENVSYS_INTEGER; swsensor_edata.flags = 0; sw_sensor_mode = 0; sw_sensor_value = 0; sw_sensor_limit = 0; /* Iterate over the provided dictionary, if any */ if (pd != NULL) { iter = prop_dictionary_iterator(pd); if (iter == NULL) return ENOMEM; while ((obj = prop_object_iterator_next(iter)) != NULL) { key = prop_dictionary_keysym_cstring_nocopy(obj); po = prop_dictionary_get_keysym(pd, obj); type = prop_object_type(po); if (type == PROP_TYPE_NUMBER) val = prop_number_integer_value(po); /* Sensor type/units */ if (strcmp(key, "type") == 0) { if (type == PROP_TYPE_NUMBER) { descr = sme_find_table_entry( SME_DESC_UNITS, val); if (descr == NULL) return EINVAL; swsensor_edata.units = descr->type; continue; } if (type != PROP_TYPE_STRING) return EINVAL; str = prop_string_cstring_nocopy(po); descr = sme_find_table_desc(SME_DESC_UNITS, str); if (descr == NULL) return EINVAL; swsensor_edata.units = descr->type; continue; } /* Sensor flags */ if (strcmp(key, "flags") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; swsensor_edata.flags = val; continue; } /* Sensor limit behavior * 0 - simple sensor, no hw limits * 1 - simple sensor, hw provides initial limit * 2 - complex sensor, hw provides settable * limits and does its own limit checking */ if (strcmp(key, "mode") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; sw_sensor_mode = val; if (sw_sensor_mode > 2) sw_sensor_mode = 2; else if (sw_sensor_mode < 0) sw_sensor_mode = 0; continue; } /* Grab any limit that might be specified */ if (strcmp(key, "limit") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; sw_sensor_limit = val; continue; } /* Grab the initial value */ if (strcmp(key, "value") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; sw_sensor_value = val; continue; } /* Grab value_min and value_max */ if (strcmp(key, "value_min") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; swsensor_edata.value_min = val; swsensor_edata.flags |= ENVSYS_FVALID_MIN; continue; } if (strcmp(key, "value_max") == 0) { if (type != PROP_TYPE_NUMBER) return EINVAL; swsensor_edata.value_max = val; swsensor_edata.flags |= ENVSYS_FVALID_MAX; continue; } /* See if sensor reports percentages vs raw values */ if (strcmp(key, "percentage") == 0) { if (type != PROP_TYPE_BOOL) return EINVAL; if (prop_bool_true(po)) swsensor_edata.flags |= ENVSYS_FPERCENT; continue; } /* Unrecognized dicttionary object */ #ifdef DEBUG printf("%s: unknown attribute %s\n", __func__, key); #endif return EINVAL; } /* while */ prop_object_iterator_release(iter); } /* Initialize limit processing */ if (sw_sensor_mode >= 1) swsensor_sme->sme_get_limits = swsensor_get_limits; if (sw_sensor_mode == 2) swsensor_sme->sme_set_limits = swsensor_set_limits; if (sw_sensor_mode != 0) { swsensor_edata.flags |= ENVSYS_FMONLIMITS; swsensor_get_limits(swsensor_sme, &swsensor_edata, &sw_sensor_deflims, &sw_sensor_defprops); } strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN); /* Wait for refresh to validate the sensor value */ swsensor_edata.state = ENVSYS_SINVALID; sw_sensor_state = ENVSYS_SVALID; error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata); if (error != 0) { aprint_error("sysmon_envsys_sensor_attach failed: %d\n", error); return error; } error = sysmon_envsys_register(swsensor_sme); if (error != 0) { aprint_error("sysmon_envsys_register failed: %d\n", error); return error; } sysctl_swsensor_setup(); aprint_normal("swsensor: initialized\n"); return 0; }
static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) { char *outwalk = buffer; #if defined(LINUX) char buf[1024]; memset(buf, 0, 1024); const char *walk, *last; bool watt_as_unit = false; int voltage = -1; char batpath[512]; sprintf(batpath, path, number); INSTANCE(batpath); if (!slurp(batpath, buf, sizeof(buf))) { OUTPUT_FULL_TEXT(format_down); return false; } for (walk = buf, last = buf; (walk - buf) < 1024; walk++) { if (*walk == '\n') { last = walk + 1; continue; } if (*walk != '=') continue; if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) { watt_as_unit = true; batt_info->remaining = atoi(walk + 1); batt_info->percentage_remaining = -1; } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) { watt_as_unit = false; batt_info->remaining = atoi(walk + 1); batt_info->percentage_remaining = -1; } else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) { batt_info->percentage_remaining = atoi(walk + 1); } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW=")) batt_info->present_rate = abs(atoi(walk + 1)); else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW=")) voltage = abs(atoi(walk + 1)); /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as * unit instead of μAh. We will calculate it as we need it * later. */ else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW=")) batt_info->present_rate = abs(atoi(walk + 1)); else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging")) batt_info->status = CS_CHARGING; else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) batt_info->status = CS_FULL; else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging")) batt_info->status = CS_DISCHARGING; else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=")) batt_info->status = CS_UNKNOWN; else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") || BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN=")) batt_info->full_design = atoi(walk + 1); else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") || BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL=")) batt_info->full_last = atoi(walk + 1); } /* the difference between POWER_SUPPLY_ENERGY_NOW and * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is * given in mWh, the charge in mAh. So calculate every value given in * ampere to watt */ if (!watt_as_unit && voltage >= 0) { if (batt_info->present_rate > 0) { batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0)); } if (batt_info->remaining > 0) { batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0)); } if (batt_info->full_design > 0) { batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0)); } if (batt_info->full_last > 0) { batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0)); } } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) int state; int sysctl_rslt; size_t sysctl_size = sizeof(sysctl_rslt); if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) { OUTPUT_FULL_TEXT(format_down); return false; } batt_info->percentage_remaining = sysctl_rslt; if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) { OUTPUT_FULL_TEXT(format_down); return false; } batt_info->seconds_remaining = sysctl_rslt * 60; if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) { OUTPUT_FULL_TEXT(format_down); return false; } state = sysctl_rslt; if (state == 0 && batt_info->percentage_remaining == 100) batt_info->status = CS_FULL; else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100) batt_info->status = CS_CHARGING; else batt_info->status = CS_DISCHARGING; #elif defined(__OpenBSD__) /* * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and * probing acpi(4) devices. */ struct apm_power_info apm_info; int apm_fd; apm_fd = open("/dev/apm", O_RDONLY); if (apm_fd < 0) { OUTPUT_FULL_TEXT("can't open /dev/apm"); return false; } if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0) OUTPUT_FULL_TEXT("can't read power info"); close(apm_fd); /* Don't bother to go further if there's no battery present. */ if ((apm_info.battery_state == APM_BATTERY_ABSENT) || (apm_info.battery_state == APM_BATT_UNKNOWN)) { OUTPUT_FULL_TEXT(format_down); return false; } switch (apm_info.ac_state) { case APM_AC_OFF: batt_info->status = CS_DISCHARGING; break; case APM_AC_ON: batt_info->status = CS_CHARGING; break; default: /* If we don't know what's going on, just assume we're discharging. */ batt_info->status = CS_DISCHARGING; break; } batt_info->percentage_remaining = apm_info.battery_life; /* Can't give a meaningful value for remaining minutes if we're charging. */ if (batt_info->status != CS_CHARGING) { batt_info->seconds_remaining = apm_info.minutes_left * 60; } #elif defined(__NetBSD__) /* * Using envsys(4) via sysmon(4). */ int fd, rval; bool is_found = false; char sensor_desc[16]; prop_dictionary_t dict; prop_array_t array; prop_object_iterator_t iter; prop_object_iterator_t iter2; prop_object_t obj, obj2, obj3, obj4, obj5; if (number >= 0) (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number); fd = open("/dev/sysmon", O_RDONLY); if (fd < 0) { OUTPUT_FULL_TEXT("can't open /dev/sysmon"); return false; } rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict); if (rval == -1) { close(fd); return false; } if (prop_dictionary_count(dict) == 0) { prop_object_release(dict); close(fd); return false; } iter = prop_dictionary_iterator(dict); if (iter == NULL) { prop_object_release(dict); close(fd); } /* iterate over the dictionary returned by the kernel */ while ((obj = prop_object_iterator_next(iter)) != NULL) { /* skip this dict if it's not what we're looking for */ if (number < 0) { /* we want all batteries */ if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj), "acpibat")) continue; } else { /* we want a specific battery */ if (strcmp(sensor_desc, prop_dictionary_keysym_cstring_nocopy(obj)) != 0) continue; } is_found = true; array = prop_dictionary_get_keysym(dict, obj); if (prop_object_type(array) != PROP_TYPE_ARRAY) { prop_object_iterator_release(iter); prop_object_release(dict); close(fd); return false; } iter2 = prop_array_iterator(array); if (!iter2) { prop_object_iterator_release(iter); prop_object_release(dict); close(fd); return false; } struct battery_info batt_buf = { .full_design = 0, .full_last = 0, .remaining = 0, .present_rate = 0, .status = CS_UNKNOWN, }; int voltage = -1; bool watt_as_unit = false; /* iterate over array of dicts specific to target battery */ while ((obj2 = prop_object_iterator_next(iter2)) != NULL) { obj3 = prop_dictionary_get(obj2, "description"); if (obj3 == NULL) continue; if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); if (prop_number_integer_value(obj3)) batt_buf.status = CS_CHARGING; else batt_buf.status = CS_DISCHARGING; } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); obj4 = prop_dictionary_get(obj2, "max-value"); obj5 = prop_dictionary_get(obj2, "type"); batt_buf.remaining = prop_number_integer_value(obj3); batt_buf.full_design = prop_number_integer_value(obj4); if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0) watt_as_unit = false; else watt_as_unit = true; } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); batt_buf.present_rate = prop_number_integer_value(obj3); } else if (strcmp("charge rate", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); batt_info->present_rate = prop_number_integer_value(obj3); } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); batt_buf.full_last = prop_number_integer_value(obj3); } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) { obj3 = prop_dictionary_get(obj2, "cur-value"); voltage = prop_number_integer_value(obj3); } } prop_object_iterator_release(iter2); if (!watt_as_unit && voltage != -1) { batt_buf.present_rate = (((float)voltage / 1000.0) * ((float)batt_buf.present_rate / 1000.0)); batt_buf.remaining = (((float)voltage / 1000.0) * ((float)batt_buf.remaining / 1000.0)); batt_buf.full_design = (((float)voltage / 1000.0) * ((float)batt_buf.full_design / 1000.0)); batt_buf.full_last = (((float)voltage / 1000.0) * ((float)batt_buf.full_last / 1000.0)); } if (batt_buf.remaining == batt_buf.full_design) batt_buf.status = CS_FULL; add_battery_info(batt_info, &batt_buf); } prop_object_iterator_release(iter); prop_object_release(dict); close(fd); if (!is_found) { OUTPUT_FULL_TEXT(format_down); return false; } batt_info->present_rate = abs(batt_info->present_rate); #endif return true; } /* * Populate batt_info with aggregate information about all batteries. * Returns false on error, and an error message will have been written. */ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) { #if defined(LINUX) char *outwalk = buffer; bool is_found = false; char *placeholder; char *globpath = sstrdup(path); if ((placeholder = strstr(path, "%d")) != NULL) { char *globplaceholder = globpath + (placeholder - path); *globplaceholder = '*'; strcpy(globplaceholder + 1, placeholder + 2); } if (!strcmp(globpath, path)) { OUTPUT_FULL_TEXT("no '%d' in battery path"); return false; } glob_t globbuf; if (glob(globpath, 0, NULL, &globbuf) == 0) { for (size_t i = 0; i < globbuf.gl_pathc; i++) { /* Probe to see if there is such a battery. */ struct battery_info batt_buf = { .full_design = 0, .full_last = 0, .remaining = 0, .present_rate = 0, .status = CS_UNKNOWN, }; if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) return false; is_found = true; add_battery_info(batt_info, &batt_buf); } } globfree(&globbuf); free(globpath); if (!is_found) { OUTPUT_FULL_TEXT(format_down); return false; } batt_info->present_rate = abs(batt_info->present_rate); #else /* FreeBSD and OpenBSD only report aggregates. NetBSD always * iterates through all batteries, so it's more efficient to * aggregate in slurp_battery_info. */ return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down); #endif return true; } void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) { const char *walk; char *outwalk = buffer; struct battery_info batt_info = { .full_design = -1, .full_last = -1, .remaining = -1, .present_rate = -1, .seconds_remaining = -1, .percentage_remaining = -1, .status = CS_UNKNOWN, }; bool colorful_output = false; #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery stats in whole percent. */ integer_battery_capacity = true; #endif #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery time in minutes. */ hide_seconds = true; #endif if (number < 0) { if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down)) return; } else { if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down)) return; } // *Choose* a measure of the 'full' battery. It is whichever is better of // the battery's (hardware-given) design capacity (batt_info.full_design) // and the battery's last known good charge (batt_info.full_last). // We prefer the design capacity, but use the last capacity if we don't have it, // or if we are asked to (last_full_capacity == true); but similarly we use // the design capacity if we don't have the last capacity. // If we don't have either then both full_design and full_last <= 0, // which implies full <= 0, which bails out on the following line. int full = batt_info.full_design; if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) { full = batt_info.full_last; } if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) { /* We have no physical measurements and no estimates. Nothing * much we can report, then. */ OUTPUT_FULL_TEXT(format_down); return; } if (batt_info.percentage_remaining < 0) { batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100); /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully * charged, even though that’s plainly wrong. For people who chose to see * the percentage calculated based on the last full capacity, we clamp the * value to 100%, as that makes more sense. * See http://bugs.debian.org/785398 */ if (last_full_capacity && batt_info.percentage_remaining > 100) { batt_info.percentage_remaining = 100; } } if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) { if (batt_info.status == CS_CHARGING) batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate; else if (batt_info.status == CS_DISCHARGING) batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate; else batt_info.seconds_remaining = 0; } if (batt_info.status == CS_DISCHARGING && low_threshold > 0) { if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) { START_COLOR("color_bad"); colorful_output = true; } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) { START_COLOR("color_bad"); colorful_output = true; } } #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \ do { \ if (outwalk == prevoutwalk) { \ if (outwalk > buffer && isspace((int)outwalk[-1])) \ outwalk--; \ else if (isspace((int)*(walk + 1))) \ walk++; \ } \ } while (0) for (walk = format; *walk != '\0'; walk++) { char *prevoutwalk = outwalk; if (*walk != '%') { *(outwalk++) = *walk; continue; } if (BEGINS_WITH(walk + 1, "status")) { const char *statusstr; switch (batt_info.status) { case CS_CHARGING: statusstr = status_chr; break; case CS_DISCHARGING: statusstr = status_bat; break; case CS_FULL: statusstr = status_full; break; default: statusstr = status_unk; } outwalk += sprintf(outwalk, "%s", statusstr); walk += strlen("status"); } else if (BEGINS_WITH(walk + 1, "percentage")) { if (integer_battery_capacity) { outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark); } else { outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark); } walk += strlen("percentage"); } else if (BEGINS_WITH(walk + 1, "remaining")) { if (batt_info.seconds_remaining >= 0) { int seconds, hours, minutes; hours = batt_info.seconds_remaining / 3600; seconds = batt_info.seconds_remaining - (hours * 3600); minutes = seconds / 60; seconds -= (minutes * 60); if (hide_seconds) outwalk += sprintf(outwalk, "%02d:%02d", max(hours, 0), max(minutes, 0)); else outwalk += sprintf(outwalk, "%02d:%02d:%02d", max(hours, 0), max(minutes, 0), max(seconds, 0)); } walk += strlen("remaining"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "emptytime")) { if (batt_info.seconds_remaining >= 0) { time_t empty_time = time(NULL) + batt_info.seconds_remaining; set_timezone(NULL); /* Use local time. */ struct tm *empty_tm = localtime(&empty_time); if (hide_seconds) outwalk += sprintf(outwalk, "%02d:%02d", max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0)); else outwalk += sprintf(outwalk, "%02d:%02d:%02d", max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0)); } walk += strlen("emptytime"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } else if (BEGINS_WITH(walk + 1, "consumption")) { if (batt_info.present_rate >= 0) outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6); walk += strlen("consumption"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); } } if (colorful_output) END_COLOR; OUTPUT_FULL_TEXT(buffer); }