Skip to content

Commit

Permalink
a bit useful status
Browse files Browse the repository at this point in the history
  • Loading branch information
source-c committed Jan 19, 2014
1 parent 1dc43ad commit 184581f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
4 changes: 4 additions & 0 deletions status/TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- add actions for mouse events^
* lock
* sleep
* refresh
2 changes: 1 addition & 1 deletion status/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11
# flags
CPPFLAGS = -DVERSION=\"${VERSION}\"
#CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
CFLAGS = -std=gnu99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
#LDFLAGS = -g ${LIBS}
LDFLAGS = -s ${LIBS}

Expand Down
114 changes: 101 additions & 13 deletions status/yawmstatus.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
** Basic dwm status with just time HH:MM and battery
** Compile with:
*/
#include <stdio.h>
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>

#define __USE_POSIX

#include <string.h>
#include <stdio.h>
#include <getopt.h>
#include <time.h>
#include <X11/Xlib.h>

Expand Down Expand Up @@ -48,46 +48,130 @@ gettime_basic(void){


int
getbattery() {
getbattery(char *batn) {
#define DEF_BNAME "BAT0"
FILE *fd;
int energy_now, energy_full, voltage_now;
char *filename;

if (asprintf(&filename, "/sys/class/power_supply/%s/energy_now",
(batn)?batn:DEF_BNAME ) == -1)
return -1;

fd = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
fd = fopen(filename, "r");
if(fd == NULL) {
fprintf(stderr, "Error opening energy_now.\n");
free(filename);
return -1;
}
fscanf(fd, "%d", &energy_now);
fclose(fd);
free(filename);


if (asprintf(&filename, "/sys/class/power_supply/%s/energy_full",
(batn)?batn:DEF_BNAME ) == -1)
return -1;

fd = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
fd = fopen(filename, "r");
if(fd == NULL) {
fprintf(stderr, "Error opening energy_full.\n");
free(filename);
return -1;
}
fscanf(fd, "%d", &energy_full);
fclose(fd);
free(filename);

if (asprintf(&filename, "/sys/class/power_supply/%s/voltage_now",
(batn)?batn:DEF_BNAME ) == -1)
return -1;

fd = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
fd = fopen(filename, "r");
if(fd == NULL) {
fprintf(stderr, "Error opening voltage_now.\n");
free(filename);
return -1;
}
fscanf(fd, "%d", &voltage_now);
fclose(fd);

free(filename);

return ((float)energy_now * 1000 / (float)voltage_now) * 100 / ((float)energy_full * 1000 / (float)voltage_now);
}

char*
get_iface_state(const char* ifname){
#define DEF_IFNAME "wlan0"
FILE *fd;
char state[5] = {0};
char *ret , *filename;

if (NULL == ifname)
ifname = DEF_IFNAME;


if (asprintf(&filename, "/sys/class/net/%s/operstate", ifname) == -1)
return NULL;

fd = fopen(filename, "r");
if(fd == NULL) {
fprintf(stderr, "Error opening interface state.\n");
free(filename);
return NULL;
}

fscanf(fd, "%4s", state);
fclose(fd);
free(filename);

if (strncmp(state,"up",2) == 0){
asprintf(&ret, "(%s)", (strncmp(ifname,"wlan",4))?"E":"W");
} else if (strncmp(state,"down",4) == 0) {
asprintf(&ret, "(%s)", "-");
} else {
asprintf(&ret, "(%s)", "?");
}
return ret;
}

int
main(int cn, char** cv) {
char *status;
char *datetime;
int bat0 = 0;
int count = 0;
int interval = 0;

char *iface = NULL, *ifstate = NULL;
char *batn = NULL;
int ch;

while ((ch = getopt (cn, cv, "i:b:t:h")) > 0){
switch(ch){
case 'i': /* interface name */
iface = optarg;
break;
case 't': /* timeout */
interval = atoi(optarg);
break;
case 'h': /* print usage */
printf("\tUsage: %s [options]\n"
"\t\t-i network interface name\n"
"\t\t-b battery class name\n"
"\t\t-t time interval between updates = 1-60 sec\n"
, cv[0]);
return 0;
break;
case 'b': /* battery class name */
batn = optarg;
break;
default:
return 0;
}
}
if (interval < 1 || interval > 60)
interval = 12;


if (!(dpy = XOpenDisplay(NULL))) {
Expand All @@ -97,21 +181,25 @@ main(int cn, char** cv) {

if((status = malloc(200)) == NULL)
exit(1);



for (;;count++) {
datetime = gettime_basic();
if ( count % 12 == 0 ){
bat0 = getbattery();
if ( count % interval == 0 ){
bat0 = getbattery(batn);
if (ifstate) free(ifstate);
ifstate = get_iface_state(iface);
}
snprintf(status, 14, "%d%% | %s", bat0, datetime);
snprintf(status, 14, "%d%% %s %s", bat0, ifstate,datetime);

free(datetime);
setstatus(status);

sleep (1);
}

if (ifstate) free(ifstate);

free(status);
XCloseDisplay(dpy);

Expand Down
2 changes: 1 addition & 1 deletion wm/config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static const unsigned int snap = 10; /* snap pixel */
static const Bool showbar = True; /* False means no bar */
static const Bool topbar = True; /* False means bottom bar */
static const Bool autoLayout = True; /* False means don't drop layout while switching */
/* runtime configured apperance */
/* runtime configured appearance */
static Bool autoFocus = True; /* False means no autofocusing windows by mouse pointer */

/* Systray */
Expand Down

0 comments on commit 184581f

Please sign in to comment.