📄 xpppoem.c
字号:
/* based on: $XConsortium: xload.c,v 1.37 94/04/17 20:43:44 converse Exp $ *//*Copyright (c) 1989 X ConsortiumPermission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OROTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shallnot be used in advertising or otherwise to promote the sale, use orother dealings in this Software without prior written authorizationfrom the X Consortium.*//* xpppoem monitor a DSL connection over pppoe by Thomas Schmitt (scdbackup@gmx.net) Published under BSD license which essentially says the same than the license terms above. Derived from Mario Klebsch's (mario@klebsch.de) xpppload (which he derived from xisdnload which is derived from xload) Found as http://www.klebsch.de/xpppload-0.4a.tar.gz 23 Oct 2001 All PPP specific code was changed to use pppoem which reads the files /proc/net/pppoe and /proc/net/dev and also prints statistics to stdout and stderr. Some features were added like colors indicating throughput and adjustable unit for the histogram. Some others were removed like the "trying" color or the "activate"/"deactivate" triggers. Give xpppload a try if xpppoem does not find the /proc files. It uses a completely different approach via sockets and ioctls. If i had known xpppload before, pppoem would not have been made. They are different enough to coexist, though.*/#include <stdlib.h>#include <unistd.h>#include <stdio.h>#include <errno.h>#include <time.h>#include <sys/time.h>#include <sys/types.h>#include <signal.h>#include <X11/Intrinsic.h>#include <X11/Xatom.h>#include <X11/StringDefs.h>#include <X11/Shell.h>#include <X11/Xaw/Cardinals.h>#include <X11/Xaw/Label.h>#include <X11/Xaw/Paned.h>#include <X11/Xaw/StripChart.h>#include <X11/Xmu/SysUtil.h>#include "xpppload.bit"char *ProgramName;static void quit();/* * Definition of the Application resources structure. */typedef struct _XPppoemResources { Boolean show_label; Boolean show_graph; char *online_color; char *cps_range; char *show; char *busy_color; char *busy_samples; char *offline_color; char *unit; char *dev_name; char *pretend; char *listen_stdin; char *report;} XPppoemResources;/* * Command line options table. Only resources are entered here...there is a * pass over the remaining options after XtParseCommand is let loose. */static XrmOptionDescRec options[] = { {"-scale", "*load.minScale", XrmoptionSepArg, NULL}, {"-update", "*load.update", XrmoptionSepArg, NULL}, {"-hl", "*load.highlight", XrmoptionSepArg, NULL}, {"-highlight", "*load.highlight", XrmoptionSepArg, NULL}, {"-nolabel", "*showLabel", XrmoptionNoArg, "False"}, {"-nograph", "*showGraph", XrmoptionNoArg, "False"}, {"-jumpscroll", "*load.jumpScroll", XrmoptionSepArg, NULL}, {"-online", "*onlineColor", XrmoptionSepArg, NULL}, {"-cps_range", "*cpsRange", XrmoptionSepArg, NULL}, {"-show", "*showSpans", XrmoptionSepArg, NULL}, {"-busy", "*busyColor", XrmoptionSepArg, NULL}, {"-busy_samples", "*busySamples", XrmoptionSepArg, NULL}, {"-offline", "*offlineColor", XrmoptionSepArg, NULL}, {"-unit", "*unitScale", XrmoptionSepArg, NULL}, {"-dev_name", "*devName", XrmoptionSepArg, NULL}, {"-pretend", "*pretendOpen", XrmoptionSepArg, NULL}, {"-listen_stdin", "*listenStdin", XrmoptionSepArg, NULL}, {"-report", "*reportText", XrmoptionSepArg, NULL}};/* * The structure containing the resource information for the * Xpppload application resources. */#define Offset(field) (XtOffsetOf(XPppoemResources, field))static XtResource my_resources[] = { {"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean), Offset(show_label), XtRImmediate, (XtPointer) TRUE}, {"showGraph", XtCBoolean, XtRBoolean, sizeof(Boolean), Offset(show_graph), XtRImmediate, (XtPointer) TRUE}, {"onlineColor", "OnlineColor", XtRString, sizeof(char *), Offset(online_color), XtRString, NULL}, {"cpsRange", "CpsRange", XtRString, sizeof(char *), Offset(cps_range), XtRString, NULL}, {"showSpans", "ShowSpans", XtRString, sizeof(char *), Offset(show), XtRString, NULL}, {"busyColor", "BusyColor", XtRString, sizeof(char *), Offset(busy_color), XtRString, NULL}, {"busySamples", "BusySamples", XtRString, sizeof(char *), Offset(busy_samples), XtRString, NULL}, {"offlineColor", "OfflineColor", XtRString, sizeof(char *), Offset(offline_color), XtRString, NULL}, {"unitScale", "unitScale", XtRString, sizeof(char *), Offset(unit), XtRString, NULL}, {"devName", "DevName", XtRString, sizeof(char *), Offset(dev_name), XtRString, NULL}, {"pretendOpen", "PretendOpen", XtRString, sizeof(char *), Offset(pretend), XtRString, NULL}, {"listenStdin", "ListenStdin", XtRString, sizeof(char *), Offset(listen_stdin), XtRString, NULL}, {"reportText", "ReportText", XtRString, sizeof(char *), Offset(report), XtRString, NULL},};#undef Offsetstatic XPppoemResources resources;static XtActionsRec xpppoem_actions[] = { { "quit", quit },};static Atom wm_delete_window;static Pixel onlinecolor, offlinecolor, bgcolor;static Widget label_wid;#include <sys/types.h>#include <sys/stat.h>#include <string.h>#include <wait.h>#include <unistd.h>#define Pppoem_use_own_cleanuP#define Pppoem_use_own_sfilE#define Pppoem_no_system_includeS#include "../pppoem.h"static struct PppoeM *PppoemM= NULL;#include "../pppoem.c"static int Xpppoem_do_show_heaT= 0;static int Xpppoem_busy_sampleS= 15;static int Xpppoem_heat_rangE= 90000.0;static double Xpppoem_uniT= 20000.0;static int Xpppoem_show_labeL= 1;static int Xpppoem_show_grapH= 1;/* * Exit with message describing command line format. */void usage(flag)int flag;/* bit0= short form*/{ fprintf (stderr, "usage: %s [-options ...]\n", ProgramName); fprintf (stderr, "\n"); fprintf (stderr, "xload lookalike program displaying a histogram of DSL throughput.\n"); if(!(flag&1)) fprintf (stderr, "\n"); fprintf (stderr, "where options include:\n"); fprintf (stderr, " -display dpy X server on which to display\n"); fprintf (stderr, " -geometry geom size and location of window\n"); fprintf (stderr, " -nolabel do not show a headline over histogram\n"); fprintf (stderr, " -nograph do not show histogram but only a color pad\n"); fprintf (stderr, " -fn font font to use in label\n"); fprintf (stderr, " -unit number data rate representing one scale unit\n"); if(!(flag&1)) fprintf (stderr, " the default value is 20000\n"); fprintf (stderr, " -scale number minimum number of scale lines\n"); fprintf (stderr, " -update seconds interval between updates\n"); fprintf (stderr, " -bg color background color of text label\n"); fprintf (stderr, " -fg color graph color\n"); fprintf (stderr, " -hl color scale and text color\n"); fprintf (stderr, " -online color background color when online\n"); fprintf (stderr, " -busy color|off indicate throughput by changing colors\n"); if(!(flag&1)) { fprintf (stderr, " based on -busy_samples average. 0 cps will\n"); fprintf (stderr, " show the -online color. The value given\n"); fprintf (stderr, " with -cps_range shows the -busy color.\n"); fprintf (stderr, " Color \"off\" disables color changing.\n"); } fprintf (stderr, " -busy_samples value number of samples for changing colors\n"); if(!(flag&1)) { fprintf (stderr, " (default: 15)\n"); fprintf (stderr, " -cps_range value upper cps limit of a color scale defined\n"); fprintf (stderr, " with -online and -busy (default: 90000)\n"); fprintf (stderr, " Note: if this value is larger than the\n"); fprintf (stderr, " largest \"-show\" number, the eternal average\n"); fprintf (stderr, " of the connection determines the color.\n"); } else fprintf (stderr, " -cps_range value upper cps limit of a -busy color scale\n"); fprintf (stderr, " -offline color background color when offline\n"); fprintf (stderr, " -jumpscroll value number of pixels to scroll on overflow\n"); if((flag&1)) fprintf (stderr, " -help print a longer help text on stderr\n"); if(!(flag&1)) { fprintf (stderr, "\n"); Pppoem__help_stderr(1); } fprintf (stderr, "\n"); exit(1);}void Xpppoem_init(){ int ret; ret= Pppoem_new(&PppoemM,"-",1.0,1,15,300,1|2); if(ret<=0) exit(1);}int Xpppoem_start(mon,flag)struct PppoeM *mon;int flag;{ /* to get a message on exit */ Cleanup_set_handlers(0); Pppoem_report(mon,"PPPOEM S",1|2); if(! Pppoem_has_headlines_on(mon,0)) Pppoem_show_stat(mon,2); /* one headline */ return(1);}unsigned Xpppoem_pixel_to_rgb(w,pixel,flag)Widget w;Pixel pixel;int flag;{ XColor color; unsigned rgb,red,green,blue; color.pixel= onlinecolor; XQueryColor(XtDisplay(w), DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w))), &color); red= (color.red>>8)&255; green= (color.green>>8)&255; blue= (color.blue>>8)&255; rgb= (red<<16) | (green<<8) | blue; return(rgb);}unsigned Xpppoem_colorname_to_rgb(w,colorname,flag)Widget w;char *colorname;int flag;{ XColor color; unsigned rgb,red,green,blue; XParseColor(XtDisplay(w), DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w))), colorname, &color); red= (color.red>>8)&255; green= (color.green>>8)&255; blue= (color.blue>>8)&255; rgb= (red<<16) | (green<<8) | blue; return(rgb);}int Xpppoem_change_color(w,colorname,flag)Widget w;char *colorname;int flag;{ XrmValue namein, pixelout; Arg args[1]; namein.addr= colorname; namein.size= strlen(colorname); XtConvert(w,XtRString, &namein, XtRPixel, &pixelout); if (pixelout.addr) {/* fprintf(stderr,"\n new pixel value = %X\n",*(Pixel*)(pixelout.addr));i*/ XtSetArg(args[0], XtNbackground, *(Pixel*)(pixelout.addr)); XtSetValues(w, args, 1); } return(1);}int Xpppoem_show_heat(w,heat,flag)Widget w;double heat; /* 0 to 1 */int flag;/* bit0 - bit3 = mode 0 = compute and eventually show color 1 = set colors[0] to 24bit RGB Value in heat 2 = compute color scale from colors[0] and 24bit RGB Value in heat bit4= ignore last_idx*/{ struct stat stbuf; int ret=1,idx,colorbase,colorscale,i,rgb,mode,debug= 0; int red_low,green_low,blue_low,red_high,green_high,blue_high; static char colors[][16]= { "#a0a0a0", "#ae8e82", "#b2867e", "#b87e7a", "#c07876", "#c87272", "#d06c6c", "#d86666", "#e06060", "#e85a5a", "#f05454", "#ff4848", "#ff3838", "#ff2424", "#ff1212", "#ff0000", }; static int last_idx= -1,count= 0; if(stat("xpppoem_debug_color_change",&stbuf)!=-1) debug= 1; if(debug) printf("Xpppoem_show_heat: count=%ld flag=%ld heat=%lf last_idx=%ld\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -