#include <iwlib.h>
#endif
+#include <dbus/dbus.h>
+
struct sysfs {
int fd;
int arg;
On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_*
*/
+/*on n900 with power kernel: (non power kernel could use "hal-device bme" and do something else)
+/sys/class/power_supply/bq27200-0/uevent
+PHYSDEVPATH=/class/i2c-adapter/i2c-2/2-0055
+PHYSDEVBUS=i2c
+PHYSDEVDRIVER=bq27200-battery
+POWER_SUPPLY_NAME=bq27200-0
+POWER_SUPPLY_TYPE=Battery
+POWER_SUPPLY_PRESENT=1 << this is always 1, it means the battery is inserted
+POWER_SUPPLY_VOLTAGE_NOW=4039 << this keeps updating during charging, both here and in dbus
+POWER_SUPPLY_CURRENT_NOW=1960 << this goes negative when charging!
+POWER_SUPPLY_CAPACITY=98 << supposed to be the %, it keeps updating while charging, unlike the dbus vals that freeze
+POWER_SUPPLY_TEMP=39 << only temperature sensor in n900 :(
+*/
+
#define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply"
-#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
-#define APM_PATH "/proc/apm"
-#define MAX_BATTERY_COUNT 4
+#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" //not for n900
+#define APM_PATH "/proc/apm" //not for n900
+#define MAX_BATTERY_COUNT 4 //more like 1
static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
-static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
-static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+//static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+//static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
static int batteries_initialized = 0;
static char batteries[MAX_BATTERY_COUNT][32];
-static int acpi_last_full[MAX_BATTERY_COUNT];
-static int acpi_design_capacity[MAX_BATTERY_COUNT];
+//static int acpi_last_full[MAX_BATTERY_COUNT];
+//static int acpi_design_capacity[MAX_BATTERY_COUNT];
+
+//eg 4100
+static int last_battery_volts[MAX_BATTERY_COUNT];
+
+//eg 78
+static unsigned char last_cell_radio_dbm;
+
+//eg 100
+static unsigned char last_cell_radio_percent;
+
+//eg 'full' 'on' 'off'
+static char last_batt_charge_status[16];
+
+//eg 35
+static int last_battery_temp[MAX_BATTERY_COUNT];
/* e.g. "charging 75%" */
static char last_battery_str[MAX_BATTERY_COUNT][64];
int get_battery_idx(const char *bat)
{
- int idx;
+// int idx;
+//
+// for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
+// if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
+// break;
+// }
+// }
+//
+// /* if not found, enter a new entry */
+// if (!strlen(batteries[idx])) {
+// snprintf(batteries[idx], 31, "%s", bat);
+// }
- for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
- if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
- break;
- }
+ return 0;
+}
+
+static int dbus_queue = 0;
+
+void set_dbus_retval(char *buffer, unsigned int n, int item);
+
+DBusConnection *connection;
+DBusError error;
+DBusMessage *message;
+DBusMessageIter iter;
+DBusBusType type;
+int message_type;
+DBusMessage *reply;
+
+void dbus_exit_app_view()
+{
+ //dbus-send --type=signal --session /com/nokia/hildon_desktop .
+
+ type = DBUS_BUS_SESSION;
+ //message_type = DBUS_MESSAGE_TYPE_SIGNAL;
+ dbus_error_init (&error);
+ connection = dbus_bus_get (type, &error);
+ message = dbus_message_new_signal ("/com/nokia/hildon_desktop", "com.nokia.hildon_desktop", "exit_app_view");
+ // send the message and flush the connection
+ if (!dbus_connection_send(connection, message, NULL)) {
+ fprintf(stderr, "Out Of Memory!\n");
+ exit(1);
+ }
+ dbus_connection_flush(connection);
+
+ // free the message
+ dbus_message_unref(message);
+
+}
+
+void get_dbus_stuff(char *buffer,unsigned int intMax_length, int item)
+{
+ char method[128];
+ char path[128];
+ char dest[128];
+ char *args = "";
+ if (dbus_queue > 10)
+ {
+ fprintf (stderr, "too much dbus queuing\n");
+ exit(1);
+// set_dbus_retval(buffer, intMax_length, item);
+// return;
+ }
+ dbus_queue++;//prevent a queue from forming on these requests...
+//fetch data from dbus, store in here as last_cell_radio_dbm
+//return into buffer
+
+ type = DBUS_BUS_SYSTEM;
+ message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
+// print_reply = TRUE;
+// print_reply_literal = FALSE;
+ int reply_timeout_ms = 5000;
+ dbus_error_init (&error);
+ connection = dbus_bus_get (type, &error);
+ if (connection == NULL)
+ {
+ fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
+ (type == DBUS_BUS_SYSTEM) ? "system" : "session",
+ error.message);
+ dbus_error_free (&error);
+ exit (1);
+ }
+ switch(item){
+ case DBUS_CELL_DBM:
+ snprintf(method,127,"get_signal_strength");
+ snprintf(path,127,"/com/nokia/phone/net");
+ snprintf(dest,127,"com.nokia.phone.net");
+ message = dbus_message_new_method_call (dest,path,"Phone.Net",method);
+ dbus_message_set_auto_start (message, TRUE);
+ break;
+ case DBUS_CELL_PERCENT:
+ snprintf(method,127,"get_signal_strength");
+ snprintf(path,127,"/com/nokia/phone/net");
+ snprintf(dest,127,"com.nokia.phone.net");
+ message = dbus_message_new_method_call (dest,path,"Phone.Net",method);
+ dbus_message_set_auto_start (message, TRUE);
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ // 'full' 'on' 'off'
+ snprintf(method,127,"GetProperty");
+ args = "maemo.rechargeable.charging_status";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ // '96'
+ snprintf(method,127,"GetProperty");
+ args = "battery.charge_level.percentage";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ // '3600' - '4200'
+ snprintf(method,127,"GetProperty");
+ args = "battery.voltage.current";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ default:
+ fprintf (stderr, "invalid item type in get_dbus_stuff");
+ break;
+ }
+ if (message == NULL)
+ {
+ fprintf (stderr, "Couldn't allocate D-Bus message\n");
+ exit (1);
+ }
+ if (!dbus_message_set_destination (message, dest))
+ {
+ fprintf (stderr, "Not enough memory\n");
+ exit (1);
}
- /* if not found, enter a new entry */
- if (!strlen(batteries[idx])) {
- snprintf(batteries[idx], 31, "%s", bat);
+ dbus_error_init (&error);
+ reply = dbus_connection_send_with_reply_and_block (connection, message, reply_timeout_ms, &error);
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "Error %s: %s\n",error.name,error.message);
+ //exit (1);//if we set timeout to 30s or something i guess it's okay to exit on "no reply" cuz something is fu*ked;
}
+ if (reply)
+ {
+ DBusMessageIter iter;
+ dbus_message_iter_init (reply, &iter);
+ //int type = dbus_message_iter_get_arg_type(&iter);
+ int current_fieldnumber = 0;
+ while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
+ {
+ //fprintf (stderr,"dbus-monitor too dumb to decipher arg type '%c'\n", type);
+ current_fieldnumber++;
+ switch(item){
+ case DBUS_CELL_DBM:
+ if (current_fieldnumber == 2)
+ {
+ unsigned char val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_cell_radio_dbm = val;
+ }
+ break;
+ case DBUS_CELL_PERCENT:
+ if (current_fieldnumber == 1)
+ {
+ unsigned char val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_cell_radio_percent = val;
+ }
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ if (current_fieldnumber == 1)
+ {
+ char *val;
+ dbus_message_iter_get_basic(&iter, &val);
+ snprintf(last_batt_charge_status,16,"%s",val);
+ }
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ if (current_fieldnumber == 1)
+ {
+ dbus_uint32_t val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_battery_perct[0] = val;
+ }
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ if (current_fieldnumber == 1)
+ {
+ dbus_uint32_t val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_battery_volts[0] = val;
+ }
+ break;
+ default:
+ fprintf (stderr, "invalid item type in get_dbus_stuff reply loop");
+ break;
+ }
+ dbus_message_iter_next (&iter);
+ }
+ dbus_message_unref (reply);
+ }
+ set_dbus_retval(buffer, intMax_length, item);
+ dbus_message_unref (message);
+ dbus_connection_unref (connection);
+ dbus_queue = 0;//reset to zero now that complete
+}
- return idx;
+void set_dbus_retval(char *buffer, unsigned int intMax_length, int item)
+{
+ switch (item) {
+ case DBUS_CELL_DBM:
+ snprintf(buffer, intMax_length, "%d", last_cell_radio_dbm);
+ break;
+ case DBUS_CELL_PERCENT:
+ snprintf(buffer, intMax_length, "%d", last_cell_radio_percent);
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ snprintf(buffer, intMax_length, "%s", last_batt_charge_status);
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ snprintf(buffer, intMax_length, "%i", last_battery_perct[0]);
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ snprintf(buffer, intMax_length, "%i", last_battery_volts[0]);
+ break;
+ default:
+ fprintf (stderr, "invalid item type in set_dbus_retval");
+ break;
+ }
}
void set_return_value(char *buffer, unsigned int n, int item, int idx);
void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
{
- static int idx, rep = 0, rep1 = 0, rep2 = 0;
+ static int idx, rep = 0;
char acpi_path[128];
char sysfs_path[128];
memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
- /* first try SYSFS if that fails try ACPI */
+ /* first try SYSFS if that fails try DBUS */
- if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
+ if (sysfs_bat_fp[idx] == NULL) {
sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
}
- if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
- acpi_bat_fp[idx] = open_file(acpi_path, &rep1);
- }
-
if (sysfs_bat_fp[idx] != NULL) {
/* SYSFS */
- int present_rate = -1;
- int remaining_capacity = -1;
- char charging_state[64];
+ int present_rate = -9999; //we will put "current now" into this. negative when charging!
+ int remaining_capacity = -1; //in %
+ char charging_state[64];//can't get this without hal bme
+ int voltage = -1;
+ int temp = 9999;
char present[4];
-
+ strcpy(present, "no");
strcpy(charging_state, "unknown");
while (!feof(sysfs_bat_fp[idx])) {
+ //we are looping through the data here
char buf[256];
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
break;
-
- /* let's just hope units are ok */
- if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
- strcpy(present, "yes");
- else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0)
- strcpy(present, "no");
- else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0)
- sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state);
- /* present_rate is not the same as the
- current flowing now but it is the same value
- which was used in the past. so we continue
- the tradition! */
+ if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)//does current row match this?
+ strcpy(present, "yes");//n900 always yes
+ else if (strncmp(buf, "POWER_SUPPLY_VOLTAGE_NOW=", 25) == 0)//keep checking possible matches
+ sscanf(buf, "POWER_SUPPLY_VOLTAGE_NOW=%d", &voltage);
else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0)
sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate);
- else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0)
- sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
- else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0)
- sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]);
- else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0)
- sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
- else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
- sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
+ else if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0)
+ sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
+ else if (strncmp(buf, "POWER_SUPPLY_TEMP=", 18) == 0)
+ sscanf(buf, "POWER_SUPPLY_TEMP=%d", &temp);
}
-
fclose(sysfs_bat_fp[idx]);
sysfs_bat_fp[idx] = NULL;
- /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
- if (remaining_capacity > acpi_last_full[idx])
- acpi_last_full[idx] = remaining_capacity; /* normalize to 100% */
+ last_battery_volts[idx] = voltage;
+ last_battery_temp[idx] = temp;
- /* not present */
- if (strcmp(present, "No") == 0) {
- strncpy(last_battery_str[idx], "not present", 64);
- }
/* charging */
- else if (strcmp(charging_state, "Charging") == 0) {
- if (acpi_last_full[idx] != 0 && present_rate > 0) {
+ if (present_rate <= 0) {
/* e.g. charging 75% */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
- (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
- /* e.g. 2h 37m */
- format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
- (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
- } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1);
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- }
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
+ snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
+ }
/* discharging */
- else if (strncmp(charging_state, "Discharging", 64) == 0) {
- if (present_rate > 0) {
+ else if (present_rate > 0) {
/* e.g. discharging 35% */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
- (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
- /* e.g. 1h 12m */
- format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
- (long) (((float) remaining_capacity / present_rate) * 3600));
- } else if (present_rate == 0) { /* Thanks to Nexox for this one */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
- "discharging %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
+ snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
}
/* charged */
- /* thanks to Lukas Zapletal <lzap@seznam.cz> */
- else if (strncmp(charging_state, "Charged", 64) == 0 || strncmp(charging_state, "Full", 64) == 0) {
- /* Below happens with the second battery on my X40,
- * when the second one is empty and the first one
- * being charged. */
- if (remaining_capacity == 0)
- strcpy(last_battery_str[idx], "empty");
- else
- strcpy(last_battery_str[idx], "charged");
+ if (remaining_capacity == 100)
+ strcpy(last_battery_str[idx], "charged 100%");
+ } else {
+ //if don't have power kernel, use HAL / dbus
+ int remaining_capacity = -1; //in %
+ int temp = -1;
+ remaining_capacity = get_battery_perct(bat);
+ get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_VOLTS_CURRENT);
+ get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_CHRG_STATUS);
+ if (strncmp(buffer, "on", 2) == 0) {
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
}
- /* unknown, probably full / AC */
- else {
- if (acpi_last_full[idx] != 0
- && remaining_capacity != acpi_last_full[idx])
- snprintf(last_battery_str[idx], 64, "unknown %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- else
- strncpy(last_battery_str[idx], "AC", 64);
+ else if (strncmp(buffer, "off", 3) == 0) {
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
}
- } else if (acpi_bat_fp[idx] != NULL) {
- /* ACPI */
- int present_rate = -1;
- int remaining_capacity = -1;
- char charging_state[64];
- char present[4];
-
- /* read last full capacity if it's zero */
- if (acpi_last_full[idx] == 0) {
- static int rep3 = 0;
- char path[128];
- FILE *fp;
-
- snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
- fp = open_file(path, &rep3);
- if (fp != NULL) {
- while (!feof(fp)) {
- char b[256];
-
- if (fgets(b, 256, fp) == NULL) {
- break;
- }
- if (sscanf(b, "last full capacity: %d",
- &acpi_last_full[idx]) != 0) {
- break;
- }
- }
-
- fclose(fp);
- }
- }
-
- fseek(acpi_bat_fp[idx], 0, SEEK_SET);
-
- strcpy(charging_state, "unknown");
-
- while (!feof(acpi_bat_fp[idx])) {
- char buf[256];
-
- if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
- break;
- }
-
- /* let's just hope units are ok */
- if (strncmp(buf, "present:", 8) == 0) {
- sscanf(buf, "present: %4s", present);
- } else if (strncmp(buf, "charging state:", 15) == 0) {
- sscanf(buf, "charging state: %63s", charging_state);
- } else if (strncmp(buf, "present rate:", 13) == 0) {
- sscanf(buf, "present rate: %d", &present_rate);
- } else if (strncmp(buf, "remaining capacity:", 19) == 0) {
- sscanf(buf, "remaining capacity: %d", &remaining_capacity);
- }
- }
- /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
- if (remaining_capacity > acpi_last_full[idx]) {
- /* normalize to 100% */
- acpi_last_full[idx] = remaining_capacity;
- }
-
- /* not present */
- if (strcmp(present, "no") == 0) {
- strncpy(last_battery_str[idx], "not present", 64);
- /* charging */
- } else if (strcmp(charging_state, "charging") == 0) {
- if (acpi_last_full[idx] != 0 && present_rate > 0) {
- /* e.g. charging 75% */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "charging %i%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- /* e.g. 2h 37m */
- format_seconds(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1,
- (long) (((acpi_last_full[idx] - remaining_capacity) *
- 3600) / present_rate));
- } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "charging %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- strncpy(last_battery_str[idx], "charging",
- sizeof(last_battery_str[idx]) - 1);
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- /* discharging */
- } else if (strncmp(charging_state, "discharging", 64) == 0) {
- if (present_rate > 0) {
- /* e.g. discharging 35% */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- /* e.g. 1h 12m */
- format_seconds(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1,
- (long) ((remaining_capacity * 3600) / present_rate));
- } else if (present_rate == 0) { /* Thanks to Nexox for this one */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "full");
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- /* charged */
- } else if (strncmp(charging_state, "charged", 64) == 0) {
- /* thanks to Lukas Zapletal <lzap@seznam.cz> */
- /* Below happens with the second battery on my X40,
- * when the second one is empty and the first one being charged. */
- if (remaining_capacity == 0) {
- strcpy(last_battery_str[idx], "empty");
- } else {
- strcpy(last_battery_str[idx], "charged");
- }
- /* unknown, probably full / AC */
- } else {
- if (strncmp(charging_state, "Full", 64) == 0) {
- strncpy(last_battery_str[idx], "full", 64);
- } else if (acpi_last_full[idx] != 0
- && remaining_capacity != acpi_last_full[idx]) {
- snprintf(last_battery_str[idx], 64, "unknown %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- } else {
- strncpy(last_battery_str[idx], "AC", 64);
- }
- }
- fclose(acpi_bat_fp[idx]);
- acpi_bat_fp[idx] = NULL;
- } else {
- /* APM */
- if (apm_bat_fp[idx] == NULL) {
- apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
- }
-
- if (apm_bat_fp[idx] != NULL) {
- unsigned int ac, status, flag;
- int life;
-
- fscanf(apm_bat_fp[idx], "%*s %*s %*x %x %x %x %d%%",
- &ac, &status, &flag, &life);
-
- if (life == -1) {
- /* could check now that there is ac */
- snprintf(last_battery_str[idx], 64, "AC");
-
- /* could check that status == 3 here? */
- } else if (ac && life != 100) {
- snprintf(last_battery_str[idx], 64, "charging %d%%", life);
- } else {
- snprintf(last_battery_str[idx], 64, "%d%%", life);
- }
-
- /* it seemed to buffer it so file must be closed (or could use
- * syscalls directly but I don't feel like coding it now) */
- fclose(apm_bat_fp[idx]);
- apm_bat_fp[idx] = NULL;
- }
+ else if (strncmp(buffer, "full", 4) == 0) {//no, it won't always be 100%. stupid dbus.
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charged %i%%", remaining_capacity);
+ }
+ last_battery_temp[idx] = temp;
}
set_return_value(buffer, n, item, idx);
}
case BATTERY_TIME:
snprintf(buffer, n, "%s", last_battery_time_str[idx]);
break;
+ case BATTERY_VOLTS:
+ snprintf(buffer, n, "%i", last_battery_volts[idx]); // voltage
+ break;
+ case BATTERY_TEMP:
+ snprintf(buffer, n, "%i", last_battery_temp[idx]); // temperature
+ break;
default:
+ fprintf (stderr, "invalid item type in set_return_value");
break;
}
}
} else if (0 != strncmp("AC", buffer, 2)) {
buffer[0] = 'U';
memmove(buffer + 1, buffer + 11, n - 11);
- }
+ } else fprintf (stderr, "invalid input buffer in get_battery_short_status");
}
int get_battery_perct(const char *bat)
{
static int rep = 0;
int idx;
- char acpi_path[128];
char sysfs_path[128];
int remaining_capacity = -1;
-
- snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
init_batteries();
-
idx = get_battery_idx(bat);
/* don't update battery too often */
- if (current_update_time - last_battery_perct_time[idx] < 30) {
+ if (current_update_time - last_battery_perct_time[idx] < 29.5) {
return last_battery_perct[idx];
}
last_battery_perct_time[idx] = current_update_time;
- /* Only check for SYSFS or ACPI */
-
- if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
- sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
- rep = 0;
- }
-
- if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
- acpi_bat_fp[idx] = open_file(acpi_path, &rep);
- }
-
+ /* try SYSFS first */
+ sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
if (sysfs_bat_fp[idx] != NULL) {
/* SYSFS */
while (!feof(sysfs_bat_fp[idx])) {
char buf[256];
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
break;
-
- if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
- sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
- } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
- sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
- } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
- sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
- } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
- sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
+ if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) {
+ sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
+
+// if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
+// sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
+// } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
+// sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
+// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
+// sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
+// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
+// sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
+// }
}
}
-
fclose(sysfs_bat_fp[idx]);
sysfs_bat_fp[idx] = NULL;
+ last_battery_perct[idx] = remaining_capacity;
- } else if (acpi_bat_fp[idx] != NULL) {
- /* ACPI */
- /* read last full capacity if it's zero */
- if (acpi_design_capacity[idx] == 0) {
- static int rep2;
- char path[128];
- FILE *fp;
-
- snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
- fp = open_file(path, &rep2);
- if (fp != NULL) {
- while (!feof(fp)) {
- char b[256];
-
- if (fgets(b, 256, fp) == NULL) {
- break;
- }
- if (sscanf(b, "last full capacity: %d",
- &acpi_design_capacity[idx]) != 0) {
- break;
- }
- }
- fclose(fp);
- }
- }
-
- fseek(acpi_bat_fp[idx], 0, SEEK_SET);
-
- while (!feof(acpi_bat_fp[idx])) {
- char buf[256];
-
- if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
- break;
- }
-
- if (buf[0] == 'r') {
- sscanf(buf, "remaining capacity: %d", &remaining_capacity);
- }
- }
- }
- if (remaining_capacity < 0) {
- return 0;
}
+ else
+ {
+ char *useless;
+ useless = malloc(128 * sizeof(char));
+ get_dbus_stuff(useless,128,DBUS_HAL_BATTERY_PERCENT);
+ //i told you it's useless...
+ //snprintf(last_battery_perct[idx], 127, "%i", useless);
+ //guess what? dbus battery % doesn't update while charging
+// get_dbus_stuff(useless,n,DBUS_HAL_BATTERY_CHRG_STATUS);
+// if (strncmp(useless, "full", 4) == 0)
+// {
+//
+// }
+ }
+// else if (acpi_bat_fp[idx] != NULL) {
+// /* ACPI */
+// /* read last full capacity if it's zero */
+// if (acpi_design_capacity[idx] == 0) {
+// static int rep2;
+// char path[128];
+// FILE *fp;
+//
+// snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
+// fp = open_file(path, &rep2);
+// if (fp != NULL) {
+// while (!feof(fp)) {
+// char b[256];
+//
+// if (fgets(b, 256, fp) == NULL) {
+// break;
+// }
+// if (sscanf(b, "last full capacity: %d",
+// &acpi_design_capacity[idx]) != 0) {
+// break;
+// }
+// }
+// fclose(fp);
+// }
+// }
+//
+// fseek(acpi_bat_fp[idx], 0, SEEK_SET);
+//
+// while (!feof(acpi_bat_fp[idx])) {
+// char buf[256];
+//
+// if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
+// break;
+// }
+//
+// if (buf[0] == 'r') {
+// sscanf(buf, "remaining capacity: %d", &remaining_capacity);
+// }
+// }
+// }
+
/* compute the battery percentage */
- last_battery_perct[idx] =
- (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
- if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
+
+ //(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
+ //if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
return last_battery_perct[idx];
}
-int get_battery_perct_bar(const char *bar)
+int get_battery_perct_bar(const char *bat)
{
int idx;
- get_battery_perct(bar);
- idx = get_battery_idx(bar);
+ get_battery_perct(bat);
+ idx = get_battery_idx(bat);
return (int) (last_battery_perct[idx] * 2.56 - 1);
}