aboutsummaryrefslogtreecommitdiffstats
path: root/status/cpu.c
blob: 5f203eed53a10222a2ed22071055daf49bb15d4a (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
#include "kconfig.h"
#include "dwmstatus.h"

static int prev_total = 0;
static int prev_idle  = 0;

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


	int str_size=0;
	int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
	int diff_idle, diff_total, diff_usage;

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

	FILE *f = fopen( "/proc/stat", "r" );
	ASSERT(f != NULL );

	fscanf( f, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle );
	fclose( f );

	ASSERT( curr_user > 0 );
	ASSERT( curr_nice > 0 );
	ASSERT( curr_system > 0 );
	ASSERT( curr_idle > 0 );

	//printf("%d %d %d %d\n", curr_user, curr_nice, curr_system, curr_idle );

	curr_total = curr_user + curr_nice + curr_system + curr_idle;
	diff_idle  = curr_idle - prev_idle;
	diff_total = curr_total - prev_total;
	diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
	prev_total = curr_total;
	prev_idle  = curr_idle;

#ifdef CONFIG_STATUS_UTF8
	//chip f64f, //ef ac 99
	buf->str[0] = 0xef;
	buf->str[1] = 0xac;
	buf->str[2] = 0x99;
	str_size = 3;
	printf("asd1 [%s]\n", buf->str);
#endif

#ifdef CONFIG_STATUS_FMT_PANGO
	//seperate by colors 
	// COLD - BLUE - < 20%
	// WARM - GREEN 20-80%
	// HOT - RED >80%
	if ( diff_usage < 20 )
	{
		snprintf( buf->str, buf->size-1, "CPU:<span color=\"#3366ff\">%02d%%</span> ", diff_usage );
	} else if( diff_usage > 80 )
	{
		snprintf( buf->str, buf->size-1, "CPU:<span color=\"#ff6633\">%02d%%</span> ", diff_usage );
	} else
	{
		snprintf( buf->str, buf->size-1, "CPU:<span color=\"#33ff66\">%02d%%</span> ", diff_usage );
	}
#else
	#ifdef CONFIG_STATUS_UTF8
		snprintf( &buf->str[str_size], buf->size-1-str_size, "%02d%% ", diff_usage );
	#else
		snprintf( &buf->str[str_size], buf->size-1-str_size, "CPU:%02d%% ", diff_usage );
	#endif

#endif

	return 0;
}