📄 linux.c
字号:
| capacity granularity 2: 1480 mWh | model number: 6000 | serial number: 1 | battery type: 4 | OEM info: XXXX */ if (sscanf(buf, "design capacity: %d", &bf->full_cap) == 1) if (_GK.debug_level & DEBUG_BATTERY) printf("%s: %d <- %s", bf->info, bf->full_cap, buf); if (sscanf(buf, "last full capacity: %d", &bf->full_cap) == 1) if (_GK.debug_level & DEBUG_BATTERY) printf("%s: %d <- %s", bf->info, bf->full_cap, buf); } fclose(f); } if (bf->full_cap == 0) { bf->full_cap = gkrellm_battery_full_cap_fallback(); bf->full_cap_bug = TRUE; } on_line = FALSE; if ( acpi_ac_state_file && (f = fopen(acpi_ac_state_file, "r")) != NULL ) { while (fgets_lower_case(buf, sizeof(buf), f)) { /* state: {on-line|off-line} */ if ( ( sscanf (buf, "state: %31s", s1) == 1 || sscanf (buf, "status: %31s", s1) == 1 ) && !strcmp(s1, "on-line") ) on_line = TRUE; } fclose(f); } if ((f = fopen(bf->state, "r")) == NULL) return FALSE; charging = FALSE; available = FALSE; have_charging_state = FALSE; while (fgets_lower_case(buf, sizeof(buf), f)) { /* present: {yes|no} | capacity state: ok | charging state: {charging|discharging|unknown} | present rate: 15000 mW | remaining capacity: 31282 mWh | present voltage: 16652 mV */ if (sscanf(buf, "charging state: %31s", s1) == 1) { have_charging_state = TRUE; if ( (!strcmp(s1, "unknown") && on_line) || !strcmp(s1, "charging") ) charging = TRUE; continue; } if (sscanf(buf, "remaining capacity: %d", &cur_cap) == 1) { if (_GK.debug_level & DEBUG_BATTERY) printf("%s: %d <- %s", bf->state, cur_cap, buf); continue; } if (sscanf(buf, "present rate: %d", &cur_rate) == 1) continue; if ( sscanf(buf, "present: %31s", s1) == 1 && !strcmp(s1, "yes") ) available = TRUE; } fclose(f); if (_GK.debug_level & DEBUG_BATTERY) printf( "Battery: on_line=%d charging=%d have_charging=%d state_file=%d\n", on_line, charging, have_charging_state, acpi_ac_state_file ? TRUE : FALSE); if (!acpi_ac_state_file && charging) /* Assumption for buggy ACPI */ on_line = TRUE; if (!have_charging_state && on_line) /* Another buggy ACPI */ charging = TRUE; if (charging) bf->got_full_cap = FALSE; /* reread full_cap */ percent = cur_cap * 100 / bf->full_cap; if (percent > 100) { percent = 100; if (bf->full_cap_bug) bf->full_cap = cur_cap; else bf->got_full_cap = FALSE; /* reread full_cap */ } if (cur_rate > 0) { if (charging) time_left = 60 * (bf->full_cap - cur_cap) / cur_rate; else time_left = 60 * cur_cap / cur_rate; } if (_GK.debug_level & DEBUG_BATTERY) { printf("Battery %d: percent=%d time_left=%d cur_cap=%d full_cap=%d\n", bf->id, percent, time_left, cur_cap, bf->full_cap); printf(" available=%d on_line=%d charging=%d fc_bug=%d\n", available, on_line, charging, bf->full_cap_bug); } gkrellm_battery_assign_data(bf->id, available, on_line, charging, percent, time_left); return TRUE; }/* ---------------------------- *//* APM battery interface */#define PROC_APM_FILE "/proc/apm"/* From: arch/i386/kernel/apm.c|| 0) Linux driver version (this will change if format changes)| 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.| 2) APM flags from APM Installation Check (0x00):| bit 0: APM_16_BIT_SUPPORT| bit 1: APM_32_BIT_SUPPORT| bit 2: APM_IDLE_SLOWS_CLOCK| bit 3: APM_BIOS_DISABLED| bit 4: APM_BIOS_DISENGAGED| 3) AC line status| 0x00: Off-line| 0x01: On-line| 0x02: On backup power (BIOS >= 1.1 only)| 0xff: Unknown| 4) Battery status| 0x00: High| 0x01: Low| 0x02: Critical| 0x03: Charging| 0x04: Selected battery not present (BIOS >= 1.2 only)| 0xff: Unknown| 5) Battery flag| bit 0: High| bit 1: Low| bit 2: Critical| bit 3: Charging| bit 7: No system battery| 0xff: Unknown| 6) Remaining battery life (percentage of charge):| 0-100: valid| -1: Unknown| 7) Remaining battery life (time units):| Number of remaining minutes or seconds| -1: Unknown| 8) min = minutes; sec = seconds*/#define APM_BIOS_VERSION(major, minor) \ ( bios_major > (major) \ || (bios_major == (major) && bios_minor >= (minor)) \ ) /* AC line status values */#define APM_ON_LINE 1 /* Battery status values */#define APM_CHARGING 3#define APM_NOT_PRESENT 4 /* Battery flag bits */#define APM_NO_BATTERY 0x80#define APM_UNKNOWN 0xFFstatic voidapm_battery_assign(gint id, gint bios_major, gint bios_minor, gint ac_line_status, gint battery_status, gint battery_flag, gint percent, gint time_left, gchar *units) { gboolean available, on_line, charging; if ( (battery_flag != APM_UNKNOWN && (battery_flag & APM_NO_BATTERY)) || (battery_status == APM_UNKNOWN && ac_line_status == APM_UNKNOWN) || (battery_status == APM_NOT_PRESENT && APM_BIOS_VERSION(1,2)) ) available = FALSE; else available = TRUE; if (ac_line_status != APM_UNKNOWN) on_line = (ac_line_status == APM_ON_LINE) ? TRUE : FALSE; else on_line = (battery_status == APM_CHARGING) ? FALSE : TRUE; if (battery_status != APM_UNKNOWN) charging= (battery_status == APM_CHARGING) ? TRUE : FALSE; else charging = (ac_line_status == APM_ON_LINE) ? TRUE : FALSE; if (!strcmp(units, "sec") && time_left > 0) time_left /= 60; gkrellm_battery_assign_data(id, available, on_line, charging, percent, time_left); }static gbooleanapm_battery_data(void) { FILE *f; gchar buf[128], units[32]; gint percent = 0, time_left = 0; gint ac_line_status, battery_status, battery_flag; gint bios_major, bios_minor; gint id, n_batteries = 1; if ((f = fopen(PROC_APM_FILE, "r")) == NULL) return FALSE; fgets(buf, sizeof(buf), f); sscanf(buf, "%*s %d.%d %*x %x %x %x %d%% %d %31s\n", &bios_major, &bios_minor, &ac_line_status, &battery_status, &battery_flag, &percent, &time_left, units); /* If have APM dual battery patch, next line will be number of batteries. */ if (fgets(buf, sizeof(buf), f)) sscanf(buf, "%d\n", &n_batteries); if (n_batteries < 2) apm_battery_assign(0, bios_major, bios_minor, ac_line_status, battery_status, battery_flag, percent, time_left, units); else { apm_battery_assign(GKRELLM_BATTERY_COMPOSITE_ID, bios_major, bios_minor, ac_line_status, battery_status, battery_flag, percent, time_left, units); while (n_batteries-- > 0 && fgets(buf, sizeof(buf), f)) { sscanf(buf, "%d %x %x %d%% %d %31s\n", &id, &battery_status, &battery_flag, &percent, &time_left, units); apm_battery_assign(id - 1, bios_major, bios_minor, ac_line_status, battery_status, battery_flag, percent, time_left, units); } } fclose(f); return TRUE; }voidgkrellm_sys_battery_read_data(void) { GList *list; if (acpi_battery_list) for (list = acpi_battery_list; list; list = list->next) acpi_battery_data((BatteryFile *)(list->data)); else apm_battery_data(); }gbooleangkrellm_sys_battery_init() { acpi_setup(); return TRUE; }/* ===================================================================== *//* Uptime monitor interface *//* Calculating an uptime based on system time has a fuzzy meaning for| laptops since /proc/uptime does not include time system has been| sleeping. So, read /proc/uptime always.*/time_tgkrellm_sys_uptime_read_uptime(void) { FILE *f; gulong l = 0; if ((f = fopen("/proc/uptime", "r")) != NULL) { fscanf(f, "%lu", &l); fclose(f); } return (time_t) l; }gbooleangkrellm_sys_uptime_init(void) { return TRUE; }/* ===================================================================== *//* Sensor monitor interface *//* ------- Linux ------------------------------------------------------- */#define THERMAL_ZONE_DIR "/proc/acpi/thermal_zone"#define THERMAL_DIR "/proc/acpi/thermal"#define SENSORS_DIR "/proc/sys/dev/sensors"#define SYSFS_I2C_DIR "/sys/bus/i2c/devices"#define UNINORTH_DIR "/sys/devices/temperatures" /* mbmon and hddtemp sensor interfaces are handled in sensors-common.c */#define LM_SENSOR_INTERFACE 0#define THERMAL_INTERFACE 1#define THERMAL_ZONE_INTERFACE 2#define NVIDIA_SETTINGS_INTERFACE 3#define IBM_ACPI_INTERFACE 4#define UNINORTH_INTERFACE 5#define IBM_ACPI_FAN_FILE "/proc/acpi/ibm/fan"#define IBM_ACPI_THERMAL "/proc/acpi/ibm/thermal"#define IBM_ACPI_CPU_TEMP 0#define IBM_ACPI_PCI_TEMP 1#define IBM_ACPI_HDD_TEMP 2#define IBM_ACPI_GPU_TEMP 3#define IBM_ACPI_BAT1_TEMP 4#define IBM_ACPI_NA1_TEMP 5#define IBM_ACPI_BAT2_TEMP 6#define IBM_ACPI_NA2_TEMP 7#define IBM_ACPI_FAN 8typedef struct { gchar *name; gfloat factor; gfloat offset; gchar *vref; } VoltDefault; /* Tables of voltage correction factors and offsets derived from the | compute lines in sensors.conf. See the README file. */ /* "lm78-*" "lm78-j-*" "lm79-*" "w83781d-*" "sis5595-*" "as99127f-*" */ /* Values from LM78/LM79 data sheets */static VoltDefault voltdefault0[] = { { "Vcor1", 1.0, 0, NULL }, { "Vcor2", 1.0, 0, NULL }, { "+3.3V", 1.0, 0, NULL }, { "+5V", 1.68, 0, NULL }, /* in3 ((6.8/10)+1)*@ */ { "+12V", 4.0, 0, NULL }, /* in4 ((30/10)+1)*@ */ { "-12V", -4.0, 0, NULL }, /* in5 -(240/60)*@ */ { "-5V", -1.667, 0, NULL } /* in6 -(100/60)*@ */ }; /* "w83782d-*" "w83783s-*" "w83627hf-*" */static VoltDefault voltdefault1[] = { { "Vcor1", 1.0, 0, NULL }, { "Vcor2", 1.0, 0, NULL }, { "+3.3V", 1.0, 0, NULL }, { "+5V", 1.68, 0, NULL }, /* in3 ((6.8/10)+1)*@ */ { "+12V", 3.80, 0, NULL }, /* in4 ((28/10)+1)*@ */ { "-12V", 5.14 , -14.91, NULL }, /* in5 (5.14 * @) - 14.91 */ { "-5V", 3.14 , -7.71, NULL }, /* in6 (3.14 * @) - 7.71 */ { "V5SB", 1.68, 0, NULL }, /* in7 ((6.8/10)+1)*@ */ { "VBat", 1.0, 0, NULL } }; /* lm80-* */static VoltDefault voltdefault2[] = { { "+5V", 2.633, 0, NULL }, /* in0 (24/14.7 + 1) * @ */ { "VTT", 1.0, 0, NULL }, { "+3.3V", 1.737, 0, NULL }, /* in2 (22.1/30 + 1) * @ */ { "Vcore", 1.474, 0, NULL }, /* in3 (2.8/1.9) * @ */ { "+12V", 6.316, 0, NULL }, /* in4 (160/30.1 + 1) * @ */ { "-12V", 5.482, -4.482, "in0" }, /* in5 (160/35.7)*(@ - in0) + @ */ { "-5V", 3.222, -2.222, "in0" } /* in6 (36/16.2)*(@ - in0) + @ */ }; /* gl518sm-* */static VoltDefault voltdefault3[] = { { "+5V", 1.0, 0, NULL }, /* vdd */ { "+3.3V", 1.0, 0, NULL }, /* vin1 */ { "+12V", 4.192, 0, NULL }, /* vin2 (197/47)*@ */ { "Vcore", 1.0, 0, NULL } /* vin3 */ }; /* gl520sm-* */static VoltDefault voltdefault4[] = { { "+5V", 1.0, 0, NULL }, /* vdd */ { "+3.3V", 1.0, 0, NULL }, /* vin1 */ { "+12V", 4.192, 0, NULL }, /* vin2 (197/47)*@ */ { "Vcore", 1.0, 0, NULL }, /* vin3 */ { "-12V", 5.0, -4.0, "vdd" } /* vin4 (5*@)-(4*vdd) */ }; /* via686a-* */static VoltDefault voltdefault5[] = { { "Vcor", 1.02, 0, NULL }, /* in0 */ { "+2.5V", 1.0, 0, NULL }, /* in1 */ { "I/O", 1.02, 0, NULL }, /* in2 */ { "+5V", 1.009, 0, NULL }, /* in3 */ { "+12V", 1.04, 0, NULL } /* in4 */ }; /* mtp008-* */static VoltDefault voltdefault6[] = { { "Vcor1", 1.0, 0, NULL }, /* in0 */ { "+3.3V", 1.0, 0, NULL }, /* in1 */ { "+12V", 3.8, 0, NULL }, /* in2 @ * 38 / 10*/ { "Vcor2", 1.0, 0, NULL }, /* in3 */ { "?", 1.0, 0, NULL }, /* in4 */ { "-12V", 5.143, -16.944, NULL }, /* in5 (@ * 36 - 118.61) / 7*/ { "Vtt", 1.0, 0, NULL } /* in6 */ }; /* adm1025-* adm9240-* lm87-* lm81-* ds1780-* */static VoltDefault voltdefault7[] = { { "2.5V", 1.0, 0, NULL }, /* in0 */ { "Vccp", 1.0, 0, NULL }, /* in1 */ { "3.3V", 1.0, 0, NULL }, /* in2 */ { "5V", 1.0, 0, NULL }, /* in3 */ { "12V", 1.0, 0, NULL }, /* in4 */ { "Vcc", 1.0, 0, NULL } /* in5 */ }; /* it87-* it8705-* it8712 */static VoltDefault voltdefault8[] = { { "Vcor1", 1.0, 0, NULL }, { "Vcor2", 1.0, 0, NULL }, { "+3.3V", 2.0, 0, NULL }, /* in2 (1 + 1)*@ */ { "+5V", 1.68, 0, NULL }, /* in3 ((6.8/10)+1)*@ */ { "+12V", 4.0, 0, NULL }, /* in4 ((30/10)+1)*@ */ { "-12V", 7.67, -27.36, NULL }, /* in5 (7.67 * @) - 27.36 */ { "-5V", 4.33, -13.64, NULL }, /* in6 (4.33 * @) - 13.64 */ { "Stby", 1.68, 0, NULL }, /* in7 ((6.8/10)+1)*@ */ { "Vbat", 1.0, 0, NULL } /* in8 */ }; /* fscpos */static VoltDefault voltdefault9[] = { { "+12V", 1.0, 0, NULL }, { "+5V", 1.0, 0, NULL }, { "+3.3V", 1.0, 0, NULL } };gbooleangkrellm_sys_sensors_get_temperature(gchar *sensor_path, gint id, gint iodev, gint interface, gfloat *temp) { FILE *f; gchar buf[128], units[32]; gint n; gfloat T, t[5], ibm_acpi_temp[8]; gboolean result = FALSE; if ( interface == THERMAL_INTERFACE || interface == THERMAL_ZONE_INTERFACE ) { f = fopen(sensor_path, "r"); if (f) { while (fgets(buf, sizeof(buf), f) != NULL) { if (need_locale_fix) locale_fix(buf); if ((n = sscanf(buf, "temperature: %f %31s", &T, units)) > 0) { if (n == 2 && !strcmp(units, "dK")) T = T / 10.0 - 273.0; *temp = T; result = TRUE; } } fclose(f); } return result; } if (interface == IBM_ACPI_INTERFACE) { f = fopen(sensor_path, "r"); if (f) { fgets(buf, sizeof(buf), f); sscanf(buf, "temperatures: %f %f %f %f %f %f %f %f", &ibm_acpi_temp[IBM_ACPI_CPU_TEMP], &ibm_acpi_temp[IBM_ACPI_PCI_TEMP], &ibm_acpi_temp[IBM_ACPI_HDD_TEMP], &ibm_acpi_temp[IBM_ACPI_GPU_TEMP], &ibm_acpi_temp[IBM_ACPI_BAT1_TEMP], &ibm_acpi_temp[IBM_ACPI_NA1_TEMP], &ibm_acpi_temp[IBM_ACPI_BAT2_TEMP], &ibm_acpi_temp[IBM_ACPI_NA2_TEMP]); *temp = ibm_acpi_temp[iodev]; result = TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -