spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

spoon.c (2131B)


      1 #include <err.h>
      2 #include <limits.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <unistd.h>
      6 
      7 #include <X11/keysym.h>
      8 #include <X11/Xlib.h>
      9 
     10 #include "arg.h"
     11 #include "util.h"
     12 
     13 int battread(void *, char *, size_t);
     14 int cpuread(void *, char *, size_t);
     15 int dateread(void *, char *, size_t);
     16 int dummyread(void *, char *, size_t);
     17 int loadread(void *, char *, size_t);
     18 int mixread(void *, char *, size_t);
     19 int mpdread(void *, char *, size_t);
     20 int tempread(void *, char *, size_t);
     21 int wifiread(void *, char *, size_t);
     22 int xkblayoutread(void *, char *, size_t);
     23 int fileread(void *, char *, size_t);
     24 int keyread(void *, char *, size_t);
     25 int netspeedread(void *, char *, size_t);
     26 int brightnessread(void *, char *, size_t);
     27 int countread(void *, char *, size_t);
     28 
     29 struct ent {
     30 	char *fmt;
     31 	int (*read)(void *, char *, size_t);
     32 	void *arg;
     33 };
     34 
     35 #include "types.h"
     36 #include "config.h"
     37 
     38 char *argv0;
     39 int single;
     40 int tty;
     41 
     42 int
     43 dummyread(void *arg, char *buf, size_t len)
     44 {
     45 	buf[0] = '\0';
     46 	return 0;
     47 }
     48 
     49 void
     50 entcat(char *line, size_t len)
     51 {
     52 	char buf[LINE_MAX];
     53 	char *s, *e;
     54 	struct ent *ent;
     55 	int ret, i;
     56 
     57 	s = line;
     58 	e = line + len;
     59 	for (i = 0; i < LEN(ents); i++) {
     60 		ent = &ents[i];
     61 		ret = ent->read(ent->arg, buf, sizeof(buf));
     62 		if (ret == 0 && s < e)
     63 			s += snprintf(s, e - s, ent->fmt, buf);
     64 	}
     65 }
     66 
     67 void
     68 xloop(void)
     69 {
     70 	char line[LINE_MAX];
     71 	Display *dpy;
     72 
     73 	dpy = XOpenDisplay(NULL);
     74 	if (dpy == NULL)
     75 		errx(1, "cannot open display");
     76 	for (;;) {
     77 		entcat(line, sizeof(line));
     78 		XStoreName(dpy, DefaultRootWindow(dpy), line);
     79 		XSync(dpy, False);
     80 		if (single)
     81 			break;
     82 		sleep(delay);
     83 	}
     84 }
     85 
     86 void
     87 ttyloop(void)
     88 {
     89 	char line[LINE_MAX];
     90 
     91 	for (;;) {
     92 		entcat(line, sizeof(line));
     93 		puts(line);
     94 		fflush(stdout);
     95 		if (single)
     96 			break;
     97 		sleep(delay);
     98 	}
     99 }
    100 
    101 void
    102 usage(void)
    103 {
    104 	fprintf(stderr, "%s: [-st]\n", argv0);
    105 	fprintf(stderr, "  -s	One shot mode\n");
    106 	fprintf(stderr, "  -t	Use TTY backend\n");
    107 	exit(1);
    108 }
    109 
    110 int
    111 main(int argc, char *argv[])
    112 {
    113 	char *end;
    114 
    115 	ARGBEGIN {
    116 	case 's':
    117 		single = 1;
    118 		break;
    119 	case 't':
    120 		tty = 1;
    121 		break;
    122 	default:
    123 		usage();
    124 	} ARGEND
    125 
    126 	if (tty)
    127 		ttyloop();
    128 	else
    129 		xloop();
    130 	return 0;
    131 }