summaryrefslogtreecommitdiff
path: root/statusbar.c
blob: 81e37bf20e33007246c11bf836c444467b886178 (plain)
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
/*
 * Fast and easy way to gather the necessary information for dwm status bar
 *
 * Needs libacpi-dev and libcpufreq-dev
 *
 * Copyright (c) 2007-2009 T. Klauser <tklauser@distanz.ch>
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#include <sys/sysinfo.h>
#include <libacpi.h>
#include <cpufreq.h>

#define DATE_STR "%Y-%m-%d %H:%M"
#define HOUR 3600			/* sec per hour */
#define MINUTE 60			/* sec per min */
#define LOADS_SCALE 65536.0

int main(int argc, char **argv)
{
	time_t today;
	struct tm tim;
	char str[25];
	int temp;
	long i, n_cpu, up, uph, upm;
	struct sysinfo info;
	global_t g;

	if (check_acpi_support() == NOT_SUPPORTED)
		return -1;
	if (!(init_acpi_batt(&g) == SUCCESS))
		return -1;
	if (!(init_acpi_acadapt(&g) == SUCCESS))
		return -1;
	if (!(init_acpi_thermal(&g) == SUCCESS))
		return -1;

	/* 1) Date */
	time(&today);
	tim = *localtime(&today);
	strftime(str, 25, DATE_STR, &tim);
	str[25] = '\0';
	printf("%s | ", str);

	/* 2) Battery percentage (only the first battery matters for now) */
	read_acpi_batt(0);
	if (batteries[0].present)
		printf("%d%% %s| ", batteries[0].percentage,
				g.adapt.ac_state == P_AC ? "(ac) " : "");
	else
		printf("n/a%% | ");

	/* TODO: Use sysfs! */
	/* 3) Temperature */
	temp = g.temperature;
	if (temp < 0 || temp == NOT_SUPPORTED)
		printf("n/a C | ");
	else
		printf("%d C | ", temp);

	/* 4) Current CPU frequency for each core */
	n_cpu = sysconf(_SC_NPROCESSORS_CONF);
	for (i = 0; i < n_cpu; i++) {
		unsigned long freq = cpufreq_get_freq_kernel(i);

		if (freq) {
			unsigned long tmp_freq;
			/* Taken from cpufrequtils */
				if (freq > 1000000) {
				tmp_freq = freq % 10000;
				if (tmp_freq >= 5000)
					freq += 10000;
				printf("%u.%02u G", ((unsigned int) freq / 1000000),
						((unsigned int) (freq % 1000000) / 10000));
			} else if (freq > 100000) {
				tmp_freq = freq % 1000;
				if (tmp_freq >= 500)
					freq += 1000;
				printf("%u M", ((unsigned int) freq / 1000));
			} else if (freq > 1000) {
				tmp_freq = freq % 100;
				if (tmp_freq >= 50)
					freq += 100;
				printf("%u.%01u M", ((unsigned int) freq / 1000),
						((unsigned int) (freq % 1000) / 100));
			} else
				printf("%lu k", freq);

			printf("Hz | ");
		} else
			printf("n/a | ");
	}

	/* 5) Uptime and load average */
	if (sysinfo(&info) == 0) {
		up = info.uptime;
		uph = up / HOUR;
		upm = (up % HOUR) / MINUTE;
		printf("%lu:%.02lu | %.02f %.02f %.02f\n", uph, upm,
				info.loads[0] / LOADS_SCALE,
				info.loads[1] / LOADS_SCALE,
				info.loads[2] / LOADS_SCALE);
	} else
		printf("n/a | n/a\n");

	return 0;
}