aboutsummaryrefslogtreecommitdiffstats
path: root/status/batt.c
blob: f6e36b14876109e172289b27e5821f486eb14390 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#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;
}