git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/LoadMonitor/main.c

      1 
      2 /*=========================================================================
      3  *
      4  * fbload, a load monitor for FBUI (in-kernel framebuffer UI)
      5  * Copyright (C) 2004 Zachary T Smith, fbui@comcast.net
      6  *
      7  * This module is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 2 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * This module is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this module; if not, write to the Free Software
     19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  *
     21  * (See the file COPYING in the main directory of this archive for
     22  * more details.)
     23  *
     24  *=======================================================================*/
     25 
     26 
     27 /* Changes
     28  *
     29  * Sept 26, 2004: responds to geo changes.
     30  */
     31 
     32 #include <stdio.h>
     33 #include <time.h>
     34 #include <unistd.h>
     35 
     36 #include "libfbui.h"
     37 
     38 
     39 
     40 float getload ()
     41 {
     42 	float a,b,c;
     43 	FILE *f = fopen ("/proc/loadavg", "r");
     44 	if (3 != fscanf (f, "%f %f %f", &a, &b, &c))
     45 		a = 0.0;
     46 	fclose (f);
     47 	return a;
     48 }
     49 
     50 
     51 int
     52 main(int argc, char** argv)
     53 {
     54 	int i;
     55 	Display *dpy;
     56 	Window *win;
     57 	int seconds_per = 5;
     58 
     59 	if (argc >= 2 && isdigit(*argv[1]) && '0'!=isdigit(*argv[1]))
     60 		seconds_per = atoi (argv[1]);
     61 
     62 	short w = 50;
     63 	short h = 120;
     64 	float *values;
     65 	unsigned long fgcolor = RGB_GREEN;
     66 	unsigned long bgcolor = 0x2000;
     67 
     68 	dpy = fbui_display_open ();
     69 	if (!dpy)
     70 		FATAL ("cannot open display");
     71 
     72 	win = fbui_window_open (dpy, w,h, &w,&h, 9999,9999, -1, -1, 
     73 		&fgcolor, &bgcolor, "fbload", "", 
     74 		FBUI_PROGTYPE_TOOL,false,false, -1,false, 
     75 		false, false, argc,argv);
     76 	if (!win) 
     77 		FATAL ("cannot create window");
     78 
     79 	int last_x = w - 1;
     80 	int last_y = h - 1;
     81 
     82 	values = (float*) malloc (w * sizeof(float));
     83 	if (!values)
     84 		FATAL("out of memory");
     85 
     86 	for (i=0; i<w; i++)
     87 		values[i] = 0.0;
     88 
     89 	unsigned long barcolor = fgcolor;
     90 
     91 	int range=0;
     92 	time_t t0 = time(NULL);
     93 	while(1) {
     94 		time_t t;
     95 		int need=0;
     96 		int need_redraw=0;
     97 		usleep (50000);
     98 
     99 		t = time(NULL);
    100 		int tdiff = t - t0;
    101 		if (tdiff >= seconds_per) {
    102 			need=1;
    103 			t0 = t;
    104 		}
    105 
    106 		if (!need) {
    107 			Event ev;
    108 			int err;
    109 			if (err = fbui_poll_event (dpy, &ev,
    110 					     FBUI_EVENTMASK_ALL & ~FBUI_EVENTMASK_KEY))
    111 			{
    112 				if (err != FBUI_ERR_NOEVENT)
    113 					fbui_print_error (err);
    114 				continue;
    115 			}
    116 printf ("%s got event %s\n", argv[0], fbui_get_event_name (ev.type));
    117 
    118 			int num = ev.type;
    119 			char key = ev.key;
    120 
    121 			if (ev.win != win)
    122 				FATAL ("event for wrong window");
    123 
    124 			if (num == FBUI_EVENT_EXPOSE)
    125 				need_redraw = need = 1;
    126 			else if (num == FBUI_EVENT_MOVE_RESIZE) {
    127 				w = ev.width;
    128 				h = ev.height;
    129 				need = 1;
    130 			}
    131 			if (num == FBUI_EVENT_ENTER) {
    132 				printf ("fbload got Enter\n");
    133 				continue;
    134 			}
    135 			if (num == FBUI_EVENT_LEAVE) {
    136 				printf ("fbload got Leave\n");
    137 				continue;
    138 			}
    139 
    140 		}
    141 
    142 		if (!need) 
    143 			continue;
    144 
    145 		/* shift values to left */
    146 		for (i=1; i<w; i++)
    147 			values[i-1] = values[i];
    148 
    149 		int nu = values[w-1] = getload();
    150 		nu++;
    151 		if (nu > range)
    152 			need_redraw = 1;
    153 
    154 		/* get max value */
    155 		float max = 0;
    156 		for (i=0; i<w; i++) {
    157 			if (max < values[i])
    158 				max = values[i];
    159 		}
    160 
    161 		range = max;
    162 		range++;
    163 		if (!range)
    164 			range = 1;
    165 
    166 		fbui_clear (dpy, win);
    167 		for (i=0; i<w; i++)
    168 			fbui_draw_vline (dpy, win, i, 
    169 				h - ((float)h/range) * values[i], h, barcolor);
    170 
    171 		if (range > 1) {
    172 			for (i=0; i<range; i++) {
    173 				int y = h/range;
    174 				y *= i;
    175 				y = h - y;
    176 				fbui_draw_hline (dpy, win, 0, last_x, y, RGB_YELLOW);
    177 			}
    178 		}
    179 		fbui_flush(dpy, win);
    180 	}
    181 
    182 	fbui_window_close (dpy, win);
    183 	fbui_display_close (dpy);
    184 	return 0;
    185 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.