aboutsummaryrefslogtreecommitdiff
path: root/src/getbat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/getbat.c')
-rw-r--r--src/getbat.c153
1 files changed, 77 insertions, 76 deletions
diff --git a/src/getbat.c b/src/getbat.c
index a9dd997..2e08cd3 100644
--- a/src/getbat.c
+++ b/src/getbat.c
@@ -58,147 +58,148 @@ static gint get_int(char* line)
static gint get_int_value_from_file(const gchar* path, const gchar* label, int label_size)
{
- FILE* fd;
- gchar buff[100];
- gint value;
+ FILE* fd;
+ gchar buff[100];
+ gint value;
- if(!(fd = fopen(path, "r")))
- return -1;
+ if(!(fd = fopen(path, "r")))
+ return -1;
- while(fgets(buff, 100, fd)!= NULL)
- {
- if(strncmp(label, buff, label_size) == 0)
+ while(fgets(buff, 100, fd)!= NULL)
{
- value = get_int(buff);
- break;
+ if(strncmp(label, buff, label_size) == 0)
+ {
+ value = get_int(buff);
+ break;
+ }
}
- }
- fclose(fd);
- return value;
+ fclose(fd);
+ return value;
}
static gboolean file_has_line(const gchar* path, const gchar* line)
{
- FILE* fd;
- gchar buff[100];
+ FILE* fd;
+ gchar buff[100];
- if(!(fd = fopen(path, "r")))
- return FALSE;
+ if(!(fd = fopen(path, "r")))
+ return FALSE;
- while(fgets(buff, 100, fd) != NULL)
- {
- if(strstr(buff, line) != NULL)
+ while(fgets(buff, 100, fd) != NULL)
{
- fclose(fd);
- return TRUE;
+ if(strstr(buff, line) != NULL)
+ {
+ fclose(fd);
+ return TRUE;
+ }
}
- }
- fclose(fd);
- return FALSE;
+ fclose(fd);
+ return FALSE;
}
gboolean gb_init()
{
- BAT_NUM = get_bat_num();
- sprintf(INFO_PATH, "/proc/acpi/battery/BAT%i/info", gb_number());
- sprintf(STATE_PATH, "/proc/acpi/battery/BAT%i/state", gb_number());
-
- FILE* fd;
- gchar buff[100];
+ BAT_NUM = get_bat_num();
+ sprintf(INFO_PATH, "/proc/acpi/battery/BAT%i/info", gb_number());
+ sprintf(STATE_PATH, "/proc/acpi/battery/BAT%i/state", gb_number());
- if(!(fd = fopen(INFO_PATH, "r")))
- return FALSE;
+ FILE* fd;
+ gchar buff[100];
- while(fgets(buff, 100, fd)!= NULL)
- {
- if(strncmp("last full capacity:", buff, 19) == 0)
- MAX_CAPACITY = get_int(buff);
- else if(strncmp("design capacity warning:", buff, 24) == 0)
- WARN_CAPACITY = get_int(buff);
- else if(strncmp("design capacity low:", buff, 20) == 0)
- LOW_CAPACITY = get_int(buff);
- else if(strncmp("present: no",buff, 27) == 0)
+ if(!(fd = fopen(INFO_PATH, "r")))
return FALSE;
- }
- fclose(fd);
- return TRUE;
+
+ while(fgets(buff, 100, fd)!= NULL)
+ {
+ if(strncmp("last full capacity:", buff, 19) == 0)
+ MAX_CAPACITY = get_int(buff);
+ else if(strncmp("design capacity warning:", buff, 24) == 0)
+ WARN_CAPACITY = get_int(buff);
+ else if(strncmp("design capacity low:", buff, 20) == 0)
+ LOW_CAPACITY = get_int(buff);
+ else if(strncmp("present: no",buff, 27) == 0)
+ return FALSE;
+ }
+ fclose(fd);
+ return TRUE;
}
+/* To do: replace with '#define's */
+
gint gb_current_capacity()
{
- return get_int_value_from_file(STATE_PATH, "remaining capacity:", 19);
+ return get_int_value_from_file(STATE_PATH, "remaining capacity:", 19);
}
gint gb_current_rate()
{
- return get_int_value_from_file(STATE_PATH, "present rate:", 13);
+ return get_int_value_from_file(STATE_PATH, "present rate:", 13);
}
gboolean gb_discharging()
{
- return file_has_line(STATE_PATH, "discharging");
+ return file_has_line(STATE_PATH, "discharging");
}
gboolean gb_charged()
{
- return file_has_line(STATE_PATH, "charged");
+ return file_has_line(STATE_PATH, "charged");
}
gboolean gb_charging()
{
- if(!file_has_line(STATE_PATH, "discharging") && !file_has_line(STATE_PATH, "charged"))
- return TRUE;
- return FALSE;
+ if(!file_has_line(STATE_PATH, "discharging") && !file_has_line(STATE_PATH, "charged"))
+ return TRUE;
+ return FALSE;
}
gint gb_number()
{
- return BAT_NUM;
+ return BAT_NUM;
}
gint gb_max_capacity()
{
- return MAX_CAPACITY;
+ return MAX_CAPACITY;
}
gint gb_warn_capacity()
{
- return WARN_CAPACITY;
+ return WARN_CAPACITY;
}
gint gb_low_capacity()
{
- return LOW_CAPACITY;
+ return LOW_CAPACITY;
}
gint gb_percent()
{
- int percent, max_capacity;
- if ( (max_capacity = gb_max_capacity()) == 0 )
+ int percent, max_capacity;
+ if ( (max_capacity = gb_max_capacity()) == 0 )
return 0;
- percent = (gb_current_capacity() * 100) / max_capacity;
- if (percent > 100)
- percent = 100;
- return percent;
+ percent = (gb_current_capacity() * 100) / max_capacity;
+ if (percent > 100)
+ percent = 100;
+ return percent;
}
void gb_discharge_time(gchar* time)
{
- float ftime = (float)gb_current_capacity() / (float)gb_current_rate();
- float minutes = ((float)ftime - (int)ftime) * (float)60;
- if(minutes < 10)
- sprintf(time, "%i:0%i", (int)ftime, (int)minutes);
- else
- sprintf(time, "%i:%i", (int)ftime, (int)minutes);
+ float ftime = (float)gb_current_capacity() / (float)gb_current_rate();
+ float minutes = ((float)ftime - (int)ftime) * (float)60;
+ if(minutes < 10)
+ sprintf(time, "%i:0%i", (int)ftime, (int)minutes);
+ else
+ sprintf(time, "%i:%i", (int)ftime, (int)minutes);
}
void gb_charge_time(gchar* time)
{
- float ftime = ((float)gb_max_capacity() - (float)gb_current_capacity()) / (float)gb_current_rate();
- float minutes = ((float)ftime - (int)ftime) * (float)60;
- if(minutes < 10)
- sprintf(time, "%i:0%i", (int)ftime, (int)minutes);
- else
- sprintf(time, "%i:%i", (int)ftime, (int)minutes);
-}
-
+ float ftime = ((float)gb_max_capacity() - (float)gb_current_capacity()) / (float)gb_current_rate();
+ float minutes = ((float)ftime - (int)ftime) * (float)60;
+ if(minutes < 10)
+ sprintf(time, "%i:0%i", (int)ftime, (int)minutes);
+ else
+ sprintf(time, "%i:%i", (int)ftime, (int)minutes);
+} \ No newline at end of file