5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
14 #include <sys/types.h>
17 #include <sys/ioctl.h>
22 #include <sys/socket.h>
27 #include <linux/omapfb.h>
31 #define SCREEN_WIDTH 800
32 #define SCREEN_HEIGHT 480
35 #define MEM_SIZE (((WIDTH * HEIGHT * 2 * 2) + 0xfff) & ~0xfff)
38 static struct fb_var_screeninfo g_vi;
39 static uint16_t *g_screen_base, *g_screen;
40 static unsigned int g_old_mem;
42 static int g_exit, g_hide;
43 static pthread_cond_t g_cond;
45 #define IS_START(buf, str) \
46 !strncmp(buf, str, sizeof(str) - 1)
48 static void clear_bytes(void *mem, int bytes);
50 static int setup_layer(int fd)
52 struct omapfb_plane_info pi;
53 struct omapfb_mem_info mi;
54 struct omapfb_color_key key;
57 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
59 perror("ioctl OMAPFB_QUERY_PLANE");
63 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
65 perror("ioctl OMAPFB_QUERY_MEM");
69 /* must disable when changing stuff */
72 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
74 perror("ioctl OMAPFB_SETUP_PLANE (d)");
80 if (mi.size < MEM_SIZE) {
82 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
84 perror("ioctl SETUP_MEM");
89 pi.pos_x = SCREEN_WIDTH - WIDTH;
92 pi.out_height = HEIGHT;
93 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
95 perror("ioctl OMAPFB_SETUP_PLANE (e1)");
99 ret = ioctl(fd, FBIOGET_VSCREENINFO, &g_vi);
101 perror("ioctl FBIOGET_VSCREENINFO");
105 g_vi.xres = g_vi.xres_virtual = WIDTH;
107 g_vi.yres_virtual = HEIGHT * 2;
108 g_vi.bits_per_pixel = 16;
110 ret = ioctl(fd, FBIOPUT_VSCREENINFO, &g_vi);
112 perror("ioctl FBIOPUT_VSCREENINFO");
117 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
119 perror("ioctl OMAPFB_SETUP_PLANE (e2)");
123 memset(&key, 0, sizeof(key));
124 key.key_type = OMAPFB_COLOR_KEY_VID_SRC;
125 key.trans_key = 0x07e0;
127 ret = ioctl(fd, OMAPFB_SET_COLOR_KEY, &key);
129 perror("ioctl OMAPFB_SET_COLOR_KEY");
133 g_screen_base = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE,
135 if (g_screen_base == MAP_FAILED) {
137 g_screen = g_screen_base = NULL;
140 clear_bytes(g_screen_base, MEM_SIZE);
141 g_screen = g_screen_base;
147 static int check_layer(int fd)
149 struct omapfb_plane_info pi;
152 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
154 perror("ioctl OMAPFB_QUERY_PLANE");
159 printf("something disabled the layer (tv-out?), quitting\n");
166 static void remove_layer(int fd)
168 struct omapfb_plane_info pi;
169 struct omapfb_mem_info mi;
172 if (g_screen_base != NULL) {
173 munmap(g_screen_base, MEM_SIZE);
174 g_screen = g_screen_base = NULL;
177 memset(&pi, 0, sizeof(pi));
179 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
181 perror("ioctl OMAPFB_SETUP_PLANE (c)");
183 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
185 perror("ioctl OMAPFB_QUERY_MEM");
189 if (mi.size != g_old_mem) {
191 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
193 perror("ioctl SETUP_MEM");
201 static void flip_fb(int fd)
203 g_vi.yoffset = g_flip_id * HEIGHT;
204 ioctl(fd, FBIOPAN_DISPLAY, &g_vi);
206 g_screen = g_screen_base + g_flip_id * WIDTH * HEIGHT;
209 static void handle_term(int num)
212 pthread_cond_signal(&g_cond);
215 static void handle_usr1(int num)
218 pthread_cond_signal(&g_cond);
221 #define s_printf(x, y, fmt, ...) \
222 basic_text_out16(g_screen, WIDTH, x, y, fmt, ##__VA_ARGS__);
224 static int read_int_file(const char *fn, int *val)
233 ret = fscanf(f, "%d", val);
241 static void clear_bytes(void *mem, int bytes)
246 for (; l >= 4; l -= 4, p += 4)
247 p[0] = p[1] = p[2] = p[3] = 0x07e007e0;
248 for (; l > 0; l--, p++)
252 static void clear(int h)
254 clear_bytes(g_screen, WIDTH * h * 2);
258 unsigned long idlesum;
265 struct proc_stat last_st;
275 static void collect_proc_stat(void)
281 unsigned long iowait;
285 hz = sysconf(_SC_CLK_TCK);
286 printf("hz: %ld\n", hz);
289 f = fopen("/proc/stat", "r");
293 if (fscanf(f, "cpu %*s %*s %lu %lu %lu",
294 &st.sys, &wait, &iowait) != 3)
297 if (fgets(buf, sizeof(buf), f) == NULL)
299 } while (fscanf(f, "intr %lu", &st.irqs) != 1);
301 if (fgets(buf, sizeof(buf), f) == NULL)
303 } while (fscanf(f, "ctxt %lu", &st.ctxt) != 1);
305 st.idlesum = wait + iowait;
307 stats.cpu_usage = hz - (st.idlesum - stats.last_st.idlesum);
308 stats.cpu_sys = st.sys - stats.last_st.sys;
310 stats.cpu_usage = stats.cpu_usage * 100 / hz;
311 stats.cpu_sys = stats.cpu_sys * 100 / hz;
313 stats.irqs = st.irqs - stats.last_st.irqs;
314 stats.ctxt = st.ctxt - stats.last_st.ctxt;
321 static void collect_mem_stat(void)
323 static const char fn[] = "/proc/meminfo";
324 uint32_t total = 0, free = 0, buffers = 0, cached = 0;
325 uint32_t anonhuge = 0, hugetot = 0, hugefree = 0, hugepgsz = 0;
326 uint32_t free_swap = 0;
334 while (fgets(buf, sizeof(buf), f)) {
335 if (total == 0 && sscanf(buf, "MemTotal: %u", &total) == 1)
337 if (free == 0 && sscanf(buf, "MemFree: %u", &free) == 1)
339 if (buffers == 0 && sscanf(buf, "Buffers: %u", &buffers) == 1)
341 if (cached == 0 && sscanf(buf, "Cached: %u", &cached) == 1)
343 if (free_swap == 0 && sscanf(buf, "SwapFree: %u", &free_swap) == 1)
345 if (anonhuge == 0 && sscanf(buf, "AnonHugePages: %u", &anonhuge) == 1)
347 if (hugetot == 0 && sscanf(buf, "HugePages_Total: %u", &hugetot) == 1)
349 if (hugefree == 0 && sscanf(buf, "HugePages_Free: %u", &hugefree) == 1)
351 if (hugepgsz == 0 && sscanf(buf, "Hugepagesize: %u", &hugepgsz) == 1)
357 hugetot = (hugetot - hugefree) * hugepgsz;
358 free += buffers + cached;
359 stats.mem_free = free >> 10;
360 stats.mem_huge = hugetot >> 10;
361 stats.mem_free_swap = free_swap >> 10;
370 struct io_stat last_st;
375 static void collect_io_stat(void)
377 unsigned long sec_r, sec_w;
378 struct io_stat st = { 0, };
383 for (i = 0; i < 2; i++) {
384 snprintf(buf, sizeof(buf), "/sys/block/mmcblk%d/stat", i);
389 if (fscanf(f, "%*s %*s %lu %*s %*s %*s %lu",
390 &sec_r, &sec_w) == 2)
398 io_stats.read = st.sec_r - io_stats.last_st.sec_r;
399 io_stats.write = st.sec_w - io_stats.last_st.sec_w;
400 // assuming 512 byte sectors..
402 io_stats.write >>= 1;
404 io_stats.last_st = st;
413 struct net_stat last_st;
421 static void collect_net_stat(void)
423 struct net_stat st = { 0, };
424 unsigned long rx, tx;
429 net_stats.down = net_stats.up =
430 net_stats.have_wlan = net_stats.link = net_stats.level = 0;
432 f = fopen("/proc/net/dev", "r");
437 static const char fmt[] =
438 "%255s %lu %*s %*s %*s %*s %*s %*s %lu";
439 if (fscanf(f, fmt, buf, &rx, &tx) != 3)
441 if (IS_START(buf, "lo"))
450 net_stats.down = st.rx - net_stats.last_st.rx;
451 net_stats.up = st.tx - net_stats.last_st.tx;
453 net_stats.last_st = st;
455 f = fopen("/proc/net/wireless", "r");
461 if (fscanf(f, "%255s %*x %d. %d.", buf, &lnk, &lvl) != 3)
463 if (IS_START(buf, "lo"))
465 // first one should be enough
466 net_stats.have_wlan = 1;
467 net_stats.link = lnk;
468 net_stats.level = lvl;
474 static void collect_stats(void)
482 static char cpu_reg_path[64];
484 static void init_stats(void)
491 // find the CPU regulator
492 for (i = found = 0; ; i++) {
493 snprintf(buf, sizeof(buf),
494 "/sys/class/regulator/regulator.%d/name", i);
498 fgets(buf, sizeof(buf), f);
500 if (IS_START(buf, "vdd_mpu")) {
507 snprintf(cpu_reg_path, sizeof(cpu_reg_path),
508 "/sys/class/regulator/regulator.%d/microvolts", i);
510 // do collect so differential stats get sane values
514 static void get_time(int x, int y)
520 gettimeofday(&tv, NULL);
521 t = localtime(&tv.tv_sec);
522 strftime(buf, sizeof(buf), "%T", t);
523 s_printf(x, y, " %s", buf);
526 static void get_cpu(int x, int y)
528 s_printf(x, y, "CPU:%5d%%", stats.cpu_usage);
531 static void get_sys(int x, int y)
533 s_printf(x, y, "SYS:%5d%%", stats.cpu_sys);
536 static void get_irq(int x, int y)
538 s_printf(x, y, "IRQ:%5u", stats.irqs);
541 static void get_cs(int x, int y)
543 s_printf(x, y, "CS:%6u", stats.ctxt);
546 static void get_cpuf(int x, int y)
548 static const char fn[] =
549 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq";
552 if (read_int_file(fn, &f))
554 s_printf(x, y, "f:%7dM", f / 1000);
557 static void get_cpuv(int x, int y)
561 if (!cpu_reg_path[0] || read_int_file(cpu_reg_path, &v))
563 s_printf(x, y, "V:%7.3fV", (float)v / 1000000.0f);
566 static void get_memfree(int x, int y)
568 s_printf(x, y, "M:%7dM", stats.mem_free);
571 static void get_memhuge(int x, int y)
573 s_printf(x, y, "H:%7dM", stats.mem_huge);
576 static void get_memswap(int x, int y)
578 s_printf(x, y, "S:%7dM", stats.mem_free_swap);
581 #define PS_BASE "/sys/class/power_supply/bq27500-0/"
583 static void get_bcap(int x, int y)
585 static const char fnc[] = PS_BASE "capacity";
588 if (read_int_file(fnc, &c))
590 s_printf(x, y, "C:%7d%%", c);
593 static void get_btime(int x, int y)
595 static const char fne[] = PS_BASE "time_to_empty_now";
596 static const char fnf[] = PS_BASE "time_to_full_now";
599 if (read_int_file(fne, &t) || t == 0) {
600 if (read_int_file(fnf, &t))
603 s_printf(x, y, "t:%4d:%02d", t / 3600, t / 60 % 60);
606 static void get_bvolts(int x, int y)
608 static const char fnv[] = PS_BASE "voltage_now";
611 if (read_int_file(fnv, &volt))
614 s_printf(x, y, "V:%7.3fV", (float)volt / 1000000.0f);
617 static void get_bwatts(int x, int y)
619 static const char fnc[] = PS_BASE "current_now";
620 static const char fnv[] = PS_BASE "voltage_now";
621 int w, cur = 0, volt = 0;
623 if (read_int_file(fnc, &cur))
625 if (read_int_file(fnv, &volt))
628 w = (cur / 1000) * (volt / 1000);
629 s_printf(x, y, "P:%7.3fW", (float)w / 1000000.0f);
632 static void get_btemp(int x, int y)
634 static const char fnt[] = PS_BASE "temp";
637 if (read_int_file(fnt, &t))
639 s_printf(x, y, "T:%7.1fC", (float)t / 10.0f);
642 static void get_ioread(int x, int y)
644 s_printf(x, y, "R:%5.1fM/s", (float)io_stats.read / 1000.0f);
647 static void get_iowrite(int x, int y)
649 s_printf(x, y, "W:%5.1fM/s", (float)io_stats.write / 1000.0f);
652 static void get_netdown(int x, int y)
654 s_printf(x, y, "D:%5uK/s", net_stats.down / 1000);
657 static void get_netup(int x, int y)
659 s_printf(x, y, "U:%5uK/s", net_stats.up / 1000);
662 static void get_netsgnl(int x, int y)
664 s_printf(x, y, "S:%5ddBm", net_stats.level);
667 int main(int argc, char *argv[])
669 static const char socket_name[] = "\0liveinfo";
670 static const char fbname[] = "/dev/fb2";
671 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
672 pthread_condattr_t condattr;
673 struct sockaddr_un sun;
678 int y = 0, y_max = 0;
683 // look for other instance
684 sock = socket(PF_UNIX, SOCK_STREAM, 0);
686 perror("socket PF_UNIX");
690 memset(&sun, 0, sizeof(sun));
691 sun.sun_family = AF_UNIX;
692 memcpy(sun.sun_path, socket_name, sizeof(socket_name));
694 ret = connect(sock, (struct sockaddr *)&sun, sizeof(sun));
696 printf("other instance detected, sending poke command\n");
697 ret = send(sock, "poke", 5, 0);
701 return ret == 5 ? 0 : 1;
704 fd = open(fbname, O_RDWR);
706 fprintf(stderr, "open %s: ", fbname);
713 // no need for privileges any more
714 p = getenv("SUDO_UID");
724 // bind/listen for commands
725 ret = bind(sock, (struct sockaddr *)&sun, sizeof(sun));
732 ret = listen(sock, 1);
740 pfd.events = POLLIN | POLLPRI;
743 ret = pthread_condattr_init(&condattr);
744 ret |= pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
745 ret |= pthread_cond_init(&g_cond, &condattr);
746 pthread_condattr_destroy(&condattr);
748 fprintf(stderr, "cond init failed\n");
752 ret = setup_layer(fd);
756 // clean up on kill too
757 signal(SIGTERM, handle_term);
758 signal(SIGUSR1, handle_usr1);
760 pthread_mutex_lock(&mutex);
761 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
763 perror("clock_gettime CLOCK_MONOTONIC");
779 else if (is_hidden) {
780 ret = setup_layer(fd);
794 get_time (0, y += Y_STEP);
796 get_cpu (0, y += Y_STEP);
797 get_sys (0, y += Y_STEP);
798 get_irq (0, y += Y_STEP);
799 get_cs (0, y += Y_STEP);
800 get_cpuf (0, y += Y_STEP);
801 get_cpuv (0, y += Y_STEP);
803 s_printf(0, y += Y_STEP, "mem");
804 get_memfree(0, y += Y_STEP);
805 get_memswap(0, y += Y_STEP);
807 get_memhuge(0, y += Y_STEP);
809 s_printf(0, y += Y_STEP, "i/o");
810 get_ioread (0, y += Y_STEP);
811 get_iowrite(0, y += Y_STEP);
813 s_printf(0, y += Y_STEP, "net");
814 get_netdown(0, y += Y_STEP);
815 get_netup (0, y += Y_STEP);
816 if (net_stats.have_wlan)
817 get_netsgnl(0, y += Y_STEP);
819 s_printf(0, y += Y_STEP, "batt");
820 get_bcap (0, y += Y_STEP);
821 get_btime (0, y += Y_STEP);
822 get_bvolts (0, y += Y_STEP);
823 get_bwatts (0, y += Y_STEP);
824 get_btemp (0, y += Y_STEP);
828 if (check_layer(fd) != 0)
831 // anything on unix socket?
832 ret = poll(&pfd, 1, 0);
834 printf("poll returned %d\n", ret);
840 pthread_cond_timedwait(&g_cond, &mutex, &ts);
850 // vim:expandtab:sw=2:ts=2