aboutsummaryrefslogblamecommitdiffstats
path: root/status/batt.c
blob: f6e36b14876109e172289b27e5821f486eb14390 (plain) (tree)















































































































































                                                                                                                               
#include "kconfig.h"
#include "dwmstatus.h"
#include "debug/debug.h"


#define BAT_PATH "/sys/class/power_supply/BAT0"
#define AC_PATH "/sys/class/power_supply/AC"

/*
char utf8_batt_charging[]    = { 0xef, 0x88, 0x91 };
char utf8_batt_charged_0[]   = { 0xef, 0x88, 0x92 };
char utf8_batt_charged_20[]  = { 0xef, 0x88, 0x95 };
char utf8_batt_charged_70[]  = { 0xef, 0x88, 0x94 };
char utf8_batt_charged_100[] = { 0xef, 0x88, 0x93 };
*/

char utf8_batt_charging[]    = { 0xef, 0x96, 0x83 }; //ef 96 83

char utf8_batt_charged_0[]   = { 0xef, 0x89, 0x84 }; //ef 89 84
char utf8_batt_charged_20[]  = { 0xef, 0x89, 0x83 }; //ef 89 83
char utf8_batt_charged_70[]  = { 0xef, 0x89, 0x8e }; //ef 89 8e
char utf8_batt_charged_100[] = { 0xef, 0x95, 0xba }; //ef 95 ba


int print_batt( bbuf *buf )
{
	ASSERT( buf != NULL );

	if ( buf->size < 1 )
	{
		buf->size = 1024;
		buf->str = malloc( buf->size );
	}

	int c1_s=0; //CALC OFFSET INSIDE STATUS_UTF8

	int ac_state=-1;
	FILE *fac = fopen( AC_PATH "/online", "r" );

	ASSERT(fac != NULL );
	if ( fac != NULL )
	{
		//printf("1\n");
		fscanf( fac, "%d", &ac_state );
		//printf("1\n");
		fclose( fac );
	} else
	{
		printf("%s\n",strerror(errno));
	}

	//printf("1\n");
	//printf("%d\n",ac_state);

	int bat_cap=-1;
	FILE *fbat = fopen( BAT_PATH "/capacity", "r" );
	ASSERT( fbat != NULL );

	if ( fbat != NULL )
	{
		fscanf( fbat, "%d", &bat_cap );
		fclose( fbat );
	} else
	{
		printf("%s\n", strerror(errno));
	}

	//we expect that everything is nice
	ASSERT(bat_cap >= 0);
	ASSERT(bat_cap <= 100);
	ASSERT(ac_state <= 1);
	ASSERT(ac_state >= 0);

#ifdef CONFIG_STATUS_UTF8
	if ( ac_state )
	{
		c1_s = sizeof( utf8_batt_charging );
		memcpy( buf->str, utf8_batt_charging, c1_s );
		snprintf( buf->str+c1_s, buf->size-1-c1_s, " %02d ", bat_cap );
	} else
	{
		if ( bat_cap < 20)
		{
			c1_s = sizeof( utf8_batt_charged_0 );
			memcpy( buf->str, utf8_batt_charged_0, c1_s );
		} else if ( bat_cap < 70 )
		{		
			c1_s = sizeof( utf8_batt_charged_20 );
			memcpy( buf->str, utf8_batt_charged_20, c1_s );
		} else if ( bat_cap < 98 )
		{
			c1_s = sizeof( utf8_batt_charged_70 );
			memcpy( buf->str, utf8_batt_charged_70, c1_s );
		}
		else
		{	
			c1_s = sizeof( utf8_batt_charged_100 );
			memcpy( buf->str, utf8_batt_charged_100, c1_s );
		}
		
	}
#else
	
#endif

//PUT COLORING INTO WORK
#ifdef CONFIG_STATUS_FMT_PANGO
	//seperate by colors 
	// RED - few secs left - <10%
	// ORANGE - better to prepare - 10-30%
	// GREEN - looks ok - >30%
	if ( bat_cap < 10 && bat_cap >= 0 )
	{
		#ifdef CONFIG_STATUS_UTF8
			snprintf(buf->str+c1_s, buf->size-1-c1_s, " <span color=\"#ff6633\">%02d</span> ", bat_cap );
		#else
			snprintf( buf->str, buf->size-1, "AC:%d BAT:<span color=\"#ff6633\">%02d</span> ", ac_state, bat_cap );
		#endif
	} else if ( bat_cap < 30 )
	{
		#ifdef CONFIG_STATUS_UTF8
			snprintf(buf->str+c1_s, buf->size-1-c1_s, " <span color=\"#ffcc33\">%02d</span> ", bat_cap );
		#else
			snprintf( buf->str, buf->size-1, "AC:%d BAT:<span color=\"#ffcc33\">%02d</span> ", ac_state, bat_cap );
		#endif
	} else if ( bat_cap < 100 )
	{
		#ifdef CONFIG_STATUS_UTF8
			snprintf(buf->str+c1_s, buf->size-1-c1_s, " <span color=\"#33ff66\">%02d</span> ", bat_cap );
		#else
			snprintf( buf->str, buf->size-1, "AC:%d BAT:<span color=\"#33ff66\">%02d</span> ", ac_state, bat_cap );
		#endif
	}
#else
	#ifdef CONFIG_STATUS_UTF8
		snprintf(buf->str+c1_s, buf->size-1-c1_s, " !%02d ", bat_cap );
	#else
		snprintf( buf->str, buf->size-1, "AC:%d BAT:%02d ", ac_state, bat_cap );
	#endif
#endif

	return 0;
}