summaryrefslogtreecommitdiff
path: root/src/status
diff options
context:
space:
mode:
Diffstat (limited to 'src/status')
-rw-r--r--src/status/battery.c30
-rw-r--r--src/status/cpuusage.c33
-rw-r--r--src/status/datetime.c61
-rw-r--r--src/status/fan.c25
-rw-r--r--src/status/memusage.c18
-rw-r--r--src/status/netif.c27
-rw-r--r--src/status/temp.c29
-rw-r--r--src/status/uptime.c83
-rw-r--r--src/status/volume_alsa.c32
9 files changed, 206 insertions, 132 deletions
diff --git a/src/status/battery.c b/src/status/battery.c
index 34bfcc0..65fdd1b 100644
--- a/src/status/battery.c
+++ b/src/status/battery.c
@@ -3,7 +3,7 @@
#include <unistd.h>
#include <string.h>
-#include "status/battery.h"
+#include "common.h"
#include "tools.h"
#ifndef POWER_BASEDIR
@@ -11,8 +11,11 @@
#endif
-void status_battery(char *batname)
+void status_battery(GlobalData *g, char *batname)
{
+ StatusItem s;
+ char text[32] = { 0 };
+
char batpath[256];
int batpathlen;
@@ -26,6 +29,9 @@ void status_battery(char *batname)
float battTime = -1;
+ statusitem_init(&s);
+ s.text = text;
+
/* Prepare path */
batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
@@ -73,21 +79,21 @@ void status_battery(char *batname)
if (chargePercent <= 40) {
if (chargePercent <= 25) {
if (chargePercent <= 10) {
- fputs("^fg(red)", stdout);
+ s.color = "red";
} else {
// 11-25%
- fputs("^fg(orange)", stdout);
+ s.color = "orange";
}
} else {
// 26-40%
- fputs("^fg(yellow)", stdout);
+ s.color = "yellow";
}
} else {
if (chargePercent > 70) {
- fputs("^fg(white)", stdout);
+ s.color = "white";
} else {
// 41-70%
- fputs("^fg(green)", stdout);
+ s.color = "green";
}
}
@@ -95,10 +101,12 @@ void status_battery(char *batname)
if (battW == 0) {
// fully charged and not in use
- printf(" %s: %d%% _ _ ",
- batname, chargePercent);
+ snprintf(text, sizeof(text), "%s: %d%% _ _",
+ batname, chargePercent);
} else {
- printf(" %s: %d%% %.1fh %.1fW ",
- batname, chargePercent, battTime, (float)battW / 1000000.0);
+ snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
+ batname, chargePercent, battTime, (float)battW / 1000000.0);
}
+
+ line_append_item(g, &s);
}
diff --git a/src/status/cpuusage.c b/src/status/cpuusage.c
index 1bb34ae..a3b1d5d 100644
--- a/src/status/cpuusage.c
+++ b/src/status/cpuusage.c
@@ -3,7 +3,7 @@
#include <unistd.h>
#include <stdlib.h>
-#include "status/cpuusage.h"
+#include "common.h"
#include "config.h"
#ifndef NUM_CPUS
@@ -21,8 +21,11 @@ unsigned long last_cpu_used = 0;
unsigned long last_cpu_total = 0;
float cpu_history[CPU_HISTORY_SIZE]; // don't care about init values
-void status_cpuusage()
+void status_cpuusage(GlobalData *g)
{
+ StatusItem s;
+ char text[32] = { 0 };
+
double loadavg[3] = { -13.37, -13.37, -13.37 } ;
char *stline = NULL;
@@ -38,8 +41,11 @@ void status_cpuusage()
int i;
+ statusitem_init(&s);
+ s.text = text;
+
// Error signaling color
- fputs("^fg(yellow)", stdout);
+ s.color = "yellow";
stfile = fopen("/proc/stat", "r");
if (stfile != NULL) {
@@ -65,13 +71,11 @@ void status_cpuusage()
if (cpu_history[0] < 0.4) {
// CPU idling OK
- fputs("^fg(#0077FF)", stdout);
+ s.color = "#0077FF";
} else {
// CPU busy
- fputs("^fg(#FF00FF)", stdout);
+ s.color = "#FF00FF";
}
-
- //printf(" CPU: %.0f%% ", cpu_history[0] * 100);
}
free(stline);
@@ -79,13 +83,14 @@ void status_cpuusage()
if (getloadavg(loadavg, 3) > 0) {
- printf(" %s%.0f,%.0f,%.0f,%.0f ",
- cpu_history[0] < 0.1000 ? " " : "",
- cpu_history[0] * 100,
- loadavg[0] * (100 / 1),
- loadavg[1] * (100 / 1), // (100 / NUM_CPUS)
- loadavg[2] * (100 / 1));
+ snprintf(text, sizeof(text),
+ "%s%.0f,%.0f,%.0f,%.0f",
+ cpu_history[0] < 0.1000 ? " " : "",
+ cpu_history[0] * 100,
+ loadavg[0] * (100 / 1),
+ loadavg[1] * (100 / 1), // (100 / NUM_CPUS)
+ loadavg[2] * (100 / 1));
}
- //fputs(" CPU usage ", stdout);
+ line_append_item(g, &s);
}
diff --git a/src/status/datetime.c b/src/status/datetime.c
index a25d411..d7d9d0a 100644
--- a/src/status/datetime.c
+++ b/src/status/datetime.c
@@ -1,36 +1,53 @@
#include <stdio.h>
#include <time.h>
-#include "status/datetime.h"
+#include "common.h"
#include "config.h"
-void status_datetime()
+void status_datetime(GlobalData *g)
{
+ StatusItem s;
+ char text[32] = { 0 };
+
time_t nows = 0;
struct tm *nowtm;
+
+ statusitem_init(&s);
+ s.text = text;
+
nows = time(NULL);
if (nows == ((time_t) -1)) {
- printf(" ^fg(red)ERROR: DATETIME");
- return;
- }
+ s.color = "red";
+ s.text = "ERROR: DATETIME";
- nowtm = localtime(&nows);
-
- printf(" ^fg(#666666)%d.%d.%d ^fg(grey)%d:%.2d"
- #ifdef SHOW_SECONDS
- ":%.2d"
- #endif
- " "
- ,nowtm -> tm_mday,
- (nowtm -> tm_mon) + 1,
- (nowtm -> tm_year) + 1900,
- nowtm -> tm_hour,
- nowtm -> tm_min
-
- #ifdef SHOW_SECONDS
- ,nowtm -> tm_sec
- #endif
- );
+ line_append_item(g, &s);
+ } else {
+ nowtm = localtime(&nows);
+
+ s.color = "#666666";
+ snprintf(text, sizeof(text),
+ "%d.%d.%d",
+ nowtm->tm_mday,
+ (nowtm->tm_mon) + 1,
+ (nowtm->tm_year) + 1900
+ );
+ line_append_item(g, &s);
+
+ s.color = "grey";
+ snprintf(text, sizeof(text),
+ "%d:%.2d"
+ #ifdef SHOW_SECONDS
+ ":%.2d"
+ #endif
+ ,nowtm->tm_hour,
+ nowtm->tm_min
+
+ #ifdef SHOW_SECONDS
+ ,nowtm -> tm_sec
+ #endif
+ );
+ line_append_item(g, &s);
+ }
}
diff --git a/src/status/fan.c b/src/status/fan.c
index 0e7cfdf..6b26678 100644
--- a/src/status/fan.c
+++ b/src/status/fan.c
@@ -2,15 +2,22 @@
#include <fcntl.h>
#include <unistd.h>
-#include "status/fan.h"
+#include "common.h"
#include "tools.h"
-void status_fan(char *title, char *sysfile)
+void status_fan(GlobalData *g, char *title, char *sysfile)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
char stline[16];
ssize_t stlen;
+
+ statusitem_init(&s);
+ s.text = text;
+
stlen = fileRead(stline, sizeof(stline), sysfile);
if (stlen <= 0) {
return;
@@ -18,12 +25,14 @@ void status_fan(char *title, char *sysfile)
// Read a valid value? Sometimes we get garbage from sysfs...
if (stlen > 5) {
- printf(" ^fg(red)%sERROR ", title);
- return;
+ s.color = "red";
+ snprintf(text, sizeof(text), "%sERROR", title);
+ } else {
+ stline[stlen - 1] = '\0';
+
+ s.color = "#CCCCCC";
+ snprintf(text, sizeof(text), "%s%s rpm", title, stline);
}
- fputs(" ^fg(#CCCCCC)", stdout);
- fputs(title, stdout);
- fwrite(stline, 1, stlen - 1, stdout);
- fputs(" rpm ", stdout);
+ line_append_item(g, &s);
}
diff --git a/src/status/memusage.c b/src/status/memusage.c
index 6a6cb21..f69c027 100644
--- a/src/status/memusage.c
+++ b/src/status/memusage.c
@@ -1,12 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
-#include "status/memusage.h"
+#include "common.h"
#include "tools.h"
-void status_memusage()
+void status_memusage(GlobalData *g)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
char *stline = NULL;
size_t stlen;
FILE *stfile;
@@ -18,6 +21,9 @@ void status_memusage()
int memcached = 0;
+ statusitem_init(&s);
+ s.text = text;
+
stfile = fopen("/proc/meminfo", "r");
if (stfile != NULL) {
stlen = getline(&stline, &stlen, stfile);
@@ -41,11 +47,13 @@ void status_memusage()
/* Change color based on % of RAM used */
if ((float)memused / (float)memtotal < 0.85) {
- fputs("^fg(green)", stdout);
+ s.color = "green";
} else {
- fputs("^fg(red)", stdout);
+ s.color = "red";
}
- printf(" Mem: %d M ", memused);
+ snprintf(text, sizeof(text), "Mem: %d M", memused);
+
+ line_append_item(g, &s);
}
}
diff --git a/src/status/netif.c b/src/status/netif.c
index 23e6520..3c0fef0 100644
--- a/src/status/netif.c
+++ b/src/status/netif.c
@@ -3,7 +3,7 @@
#include <unistd.h>
#include <string.h>
-#include "status/netif.h"
+#include "common.h"
#include "tools.h"
#include "config.h"
@@ -12,8 +12,11 @@
#endif
-void status_netif(char *ifname)
+void status_netif(GlobalData *g, char *ifname)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
char ifpath[256];
int ifpathlen;
@@ -24,6 +27,9 @@ void status_netif(char *ifname)
int ifsumpower;
+ statusitem_init(&s);
+ s.text = text;
+
/* Prepare path */
ifpathlen = sizeof(NETIF_BASEDIR) - 1 + strlen(ifname);
if (ifpathlen + 1 + sizeof("/statistics/rx_bytes") >= sizeof(ifpath)) {
@@ -38,7 +44,7 @@ void status_netif(char *ifname)
/* Is the interface up? */
if (access(ifpath, F_OK)) {
- //printf(" ^fg(grey)[%s] ", ifname);
+ //s.color = "grey";
return;
}
@@ -47,9 +53,9 @@ void status_netif(char *ifname)
stlen = fileRead(stline, sizeof(stline), ifpath);
if (stlen > 0) {
if (stline[0] == '1') {
- fputs("^fg(yellow)", stdout);
+ s.color = "yellow";
} else {
- //fputs("^fg(red)", stdout);
+ //s.color = "red";
return;
}
} else {
@@ -73,8 +79,11 @@ void status_netif(char *ifname)
ifsum = ifsum / 1024;
}
- printf(" %s: %.*f %c ", ifname,
- ifsumpower ? ifsumpower - 1 : ifsumpower,
- ifsum,
- powerToChar(ifsumpower));
+ snprintf(text, sizeof(text), "%s: %.*f %c",
+ ifname,
+ ifsumpower ? ifsumpower - 1 : ifsumpower,
+ ifsum,
+ powerToChar(ifsumpower));
+
+ line_append_item(g, &s);
}
diff --git a/src/status/temp.c b/src/status/temp.c
index e6763d0..c102fcd 100644
--- a/src/status/temp.c
+++ b/src/status/temp.c
@@ -2,15 +2,22 @@
#include <fcntl.h>
#include <unistd.h>
-#include "status/temp.h"
+#include "common.h"
#include "tools.h"
-void status_temp(char *title, char *sysfile)
+void status_temp(GlobalData *g, char *title, char *sysfile)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
char stline[16];
ssize_t stlen;
+
+ statusitem_init(&s);
+ s.text = text;
+
stlen = fileRead(stline, sizeof(stline), sysfile);
if (stlen <= 0) {
return;
@@ -21,16 +28,14 @@ void status_temp(char *title, char *sysfile)
* Sometimes we get garbage from sysfs...
*/
if (stlen < 6 || stlen > 7) {
- printf(" ^fg(red)%sERROR ", title);
- return;
+ s.color = "red";
+ snprintf(text, sizeof(text), "%sERROR", title);
+ } else {
+ stline[stlen - 4] = '\0';
+
+ s.color = "#FF33FF";
+ snprintf(text, sizeof(text), "%s%s°C", title, stline);
}
- fputs(" ^fg(#FF33FF)", stdout);
- fputs(title, stdout);
- fwrite(stline, 1, stlen - 4, stdout);
- /*
- fputs(".", stdout);
- fwrite(&stline[stlen - 3], 1, 1, stdout);
- */
- fputs("°C ", stdout);
+ line_append_item(g, &s);
}
diff --git a/src/status/uptime.c b/src/status/uptime.c
index 106051c..ef5b2e5 100644
--- a/src/status/uptime.c
+++ b/src/status/uptime.c
@@ -3,57 +3,70 @@
#include <unistd.h>
#include <stdlib.h>
-#include "status/uptime.h"
+#include "common.h"
#include "tools.h"
#include "config.h"
-void status_uptime()
+void status_uptime(GlobalData *g)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
char stline[16];
ssize_t stlen;
int i;
int upts, uptm, upth, uptd;
- fputs(" ^fg(#AAAAAA)up: ", stdout);
+
+ statusitem_init(&s);
+ s.text = text;
stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
if (stlen < 0) {
- fputs(" ^fg(red)ERROR ", stdout);
- return;
- }
+ s.color = "red";
+ s.text = "up: ERROR";
+ } else {
+ unsigned textlen = 0;
- /* Cut first element */
- for(i = 0; i < stlen; i++) {
- if (stline[i] == ' ') {
- stline[i] = '\0';
- break;
+ /* Cut first element */
+ for(i = 0; i < stlen; i++) {
+ if (stline[i] == ' ') {
+ stline[i] = '\0';
+ break;
+ }
}
- }
- // Split time into days, hours, mins, secs
- upts = atoi(stline);
- uptd = upts / (24 * 60 * 60);
- upts -= uptd * (24 * 60 * 60);
- upth = upts / (60 * 60);
- upts -= upth * (60 * 60);
- uptm = upts / (60);
- upts -= uptm * (60);
-
- if (uptd > 0) {
- printf("%dd ", uptd);
- }
+ // Split time into days, hours, mins, secs
+ upts = atoi(stline);
+ uptd = upts / (24 * 60 * 60);
+ upts -= uptd * (24 * 60 * 60);
+ upth = upts / (60 * 60);
+ upts -= upth * (60 * 60);
+ uptm = upts / (60);
+ upts -= uptm * (60);
- printf("%d:%.2d"
- #ifdef SHOW_SECONDS
- ":%.2d"
- #endif
+ s.color = "#AAAAAA";
+ textlen = snprintf(text, sizeof(text), "up: ");
+ if (uptd > 0) {
+ textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
+ }
+
+
+ snprintf(&text[textlen], sizeof(text) - textlen,
+ "%d:%.2d"
+ #ifdef SHOW_SECONDS
+ ":%.2d"
+ #endif
- " "
- ,upth
- ,uptm
+ ""
+ ,upth
+ ,uptm
- #ifdef SHOW_SECONDS
- ,upts
- #endif
- );
+ #ifdef SHOW_SECONDS
+ ,upts
+ #endif
+ );
+
+ line_append_item(g, &s);
+ }
}
diff --git a/src/status/volume_alsa.c b/src/status/volume_alsa.c
index 41f61b8..54fe44e 100644
--- a/src/status/volume_alsa.c
+++ b/src/status/volume_alsa.c
@@ -1,10 +1,16 @@
#include <alsa/asoundlib.h>
-#include "status/volume_alsa.h"
+#include "common.h"
-int status_volume_alsa(char *cardname, char *mixername, snd_mixer_selem_channel_id_t channel)
+int status_volume_alsa(GlobalData *g,
+ char *cardname,
+ char *mixername,
+ snd_mixer_selem_channel_id_t channel)
{
+ StatusItem s;
+ char text[16] = { 0 };
+
snd_mixer_t *handle = NULL;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
@@ -14,6 +20,9 @@ int status_volume_alsa(char *cardname, char *mixername, snd_mixer_selem_channel_
int on_off;
+ statusitem_init(&s);
+ s.text = text;
+
snd_mixer_selem_id_alloca(&sid);
if (snd_mixer_open(&handle, 0) < 0) {
@@ -42,26 +51,17 @@ int status_volume_alsa(char *cardname, char *mixername, snd_mixer_selem_channel_
if (snd_mixer_selem_has_playback_volume(elem)
&& snd_mixer_selem_has_playback_channel(elem, channel)) {
snd_mixer_selem_get_playback_switch(elem, channel, &on_off);
- if (on_off) {
- fputs("^fg(#22FF22)", stdout);
- } else {
- fputs("^fg(red)", stdout);
- }
-
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
-
snd_mixer_selem_get_playback_volume(elem, channel, &volume);
- fputs("^ca(1, amixer sset Master toggle)", stdout);
- fputs("^ca(4, amixer sset Master 2+ unmute)", stdout);
- fputs("^ca(5, amixer sset Master 2- unmute)", stdout);
- printf(" Vol: %d ", (int)volume);
- fputs("^ca()", stdout);
- fputs("^ca()", stdout);
- fputs("^ca()", stdout);
+
+ s.color = on_off ? "#22FF22" : "red";
+ snprintf(text, sizeof(text), "Vol: %d", (int)volume);
}
snd_mixer_close(handle);
+ line_append_item(g, &s);
+
return 0;
ERROR: