⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitor.c

📁 EGui是一个开源的图形系统软件,类似于QT/Embedded、GTK-FB、MicroWindow。目标是嵌入式平台整合解 决方案。基于Linux Framebuffer 设备驱动上实现。有完
💻 C
字号:
/******************************************************** * Egui code,LGPL * Function : Test Linux framebuffer  * Author: asmcos@mimios.com * Data : 2005-08-09 * $Id: monitor.c,v 1.10 2006/04/14 00:53:28 hjs Exp $ ********************************************************/#include <stdlib.h>#include <stdio.h>#include <time.h>#include <unistd.h>#include <ctype.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <Egui.h>#define MAX 100int value[MAX];EGui_FBinfo Efbinfo;Ecolor fcolor;Ecolor lcolor;Ecolor bcolor;#define NCPUSTATES 4static char *cpustatenames[NCPUSTATES+1] ={    "user", "nice", "system", "idle",    NULL};/*=SYSTEM STATE INFO====================================================*/    /* these are for calculating cpu state percentages */static long cp_time[NCPUSTATES];static long cp_old[NCPUSTATES];static long cp_diff[NCPUSTATES];static int cpu_states[NCPUSTATES];static inline char *skip_ws(const char *p){       while (isspace(*p)) p++;    return (char *)p;}       static inline char *skip_token(const char *p){       while (isspace(*p)) p++;     while (*p && !isspace(*p)) p++;    return (char *)p;}long percentages(cnt, out, new, old, diffs)int cnt;int *out;register long *new;register long *old;long *diffs;{    register int i;    register long change;    register long total_change;    register long *dp;    long half_total;        /* initialization */    total_change = 0;    dp = diffs;    /* calculate changes for each state and the overall change */    for (i = 0; i < cnt; i++)    {        if ((change = *new - *old) < 0)        {            /* this only happens when the counter wraps */            change = (int)                ((unsigned long)*new-(unsigned long)*old);        }        total_change += (*dp++ = change);        *old++ = *new++;    }    /* avoid divide by zero potential */    if (total_change == 0)    {        total_change = 1;    }    /* calculate percentages based on overall change, rounding up */    half_total = total_change / 2l;    for (i = 0; i < cnt; i++)    {        *out = (int)((*diffs * 1000 + half_total) / total_change);      out ++;      diffs ++;    }    /* return the total in case the caller wants to use it */    return(total_change);}int get_system_info (){  int fd;  int len;  char buffer[4096+1];  char *p;  fd = open("/proc/stat", O_RDONLY);  len = read(fd, buffer, sizeof(buffer)-1);  close(fd);  buffer[len] = '\0';    p = skip_token(buffer);                 /* "cpu" */  cp_time[0] = strtoul(p, &p, 0);  cp_time[1] = strtoul(p, &p, 0);  cp_time[2] = strtoul(p, &p, 0);  cp_time[3] = strtoul(p, &p, 0);    /* convert cp_time counts to percentages */  percentages(4, cpu_states, cp_time, cp_old, cp_diff);  //  printf ("IDLE=%d\n",cpu_states[3]/10);  return cpu_states[3] / 10;  }voiddisplay_recall (unsigned int * para){  int i,height,width;  char cpuinfo[10];  Efont *font;  EGui_Window * ewindow;  ewindow = ( EGui_Window *) (para);    for (i=1;i<MAX;i++)    value[i-1] = value[i];  /* get_system_info = IDLE value */  width = ewindow->frame_w;  value[MAX-1] = get_system_info();  for (i=width;i<(MAX+width);i++){    height = ewindow->frame_w + ewindow->title_h;    Egui_vline (i,height,i,100 + height-1,&bcolor,ewindow);    Egui_vline (i,value[i-width]+height-1,i,100+height-1,&lcolor,ewindow);  }  sprintf(cpuinfo,"cpu:%02d%%",100-value[MAX-1]);  Egui_drawstring(30,40,cpuinfo,font,&fcolor,ewindow);  Egui_timer_insert(1000,display_recall,(unsigned int *)ewindow);}intmain (){  int i;  int height;  EGui_Window * ewindow;  Ecolor * color;  Egui_open (&Efbinfo);  for (i=0;i<MAX;i++)    value[i] = 100;  get_system_info ();  color = malloc (sizeof ( Ecolor));  if (color == NULL)    {      printf ("malloc color ERROR\n");      return -1;    }  color->r = color->g = color->b = 0xcc;  /* create window will initial color's pixel.    */  ewindow = Egui_CreateWindow (&Efbinfo,50,50,100,100,color,EGUI_WINDOW_TOP);  if (ewindow == NULL)    {      printf ("New windows failed\n");      Egui_close ();      return 1;    }   Egui_drawwindow(ewindow);  fcolor.r = fcolor.g = 0;  fcolor.b = 0xFF;  color_from_rgb (ewindow,&fcolor);  lcolor.r = lcolor.b = 0;  lcolor.g = 0xFF;  color_from_rgb (ewindow,&lcolor);  bcolor.r = bcolor.b = 0;  bcolor.g = 0;  color_from_rgb (ewindow,&bcolor);  Egui_timer_insert(1000,display_recall,(unsigned int *)ewindow);  egui_loop ();    Egui_CloseWindow (ewindow);  free(color);  Egui_close ();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -