From 39c7c55760aa1118ad900f3e7aff9861f2a7b3bd Mon Sep 17 00:00:00 2001 From: Bernhard Walle Date: Sun, 17 Aug 2003 18:55:46 +0000 Subject: [PATCH] * Added libgtop2 support * Changed comment to GPL * Better organize of code (init and close function) (Old svn revision: 286) --- panel-plugin/Makefile.am | 2 + panel-plugin/Makefile.in | 16 ++++ panel-plugin/net.h | 61 ++++++++------ panel-plugin/net_freebsd.c | 160 ++++++++++++++++++------------------ panel-plugin/net_libgtop2.c | 104 +++++++++++++++++++++++ panel-plugin/net_linux.c | 134 +++++++++++++++--------------- panel-plugin/netload.c | 41 +++++---- 7 files changed, 325 insertions(+), 193 deletions(-) create mode 100644 panel-plugin/net_libgtop2.c diff --git a/panel-plugin/Makefile.am b/panel-plugin/Makefile.am index 46b09c0..00fc3e1 100644 --- a/panel-plugin/Makefile.am +++ b/panel-plugin/Makefile.am @@ -15,7 +15,9 @@ libnetload_la_SOURCES = \ libnetload_la_CFLAGS = \ -DPACKAGE_LOCALE_DIR=\"$(localedir)\" \ + @LIBGTOP_INCS@ \ @XFCE4_PANEL_CFLAGS@ libnetload_la_LIBADD = \ + @LIBGTOP_LIBS@ \ @XFCE4_PANEL_LIBS@ diff --git a/panel-plugin/Makefile.in b/panel-plugin/Makefile.in index 97e8abc..1cab11a 100644 --- a/panel-plugin/Makefile.in +++ b/panel-plugin/Makefile.in @@ -40,6 +40,7 @@ ACLOCAL = @ACLOCAL@ AMDEP_FALSE = @AMDEP_FALSE@ AMDEP_TRUE = @AMDEP_TRUE@ AMTAR = @AMTAR@ +AR = @AR@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ @@ -51,6 +52,10 @@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ CPP = @CPP@ CPPFLAGS = @CPPFLAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ CYGPATH_W = @CYGPATH_W@ DATADIRNAME = @DATADIRNAME@ DEFS = @DEFS@ @@ -61,6 +66,8 @@ ECHO_N = @ECHO_N@ ECHO_T = @ECHO_T@ EGREP = @EGREP@ EXEEXT = @EXEEXT@ +F77 = @F77@ +FFLAGS = @FFLAGS@ GETTEXT_PACKAGE = @GETTEXT_PACKAGE@ GMOFILES = @GMOFILES@ GMSGFMT = @GMSGFMT@ @@ -71,6 +78,8 @@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ INSTOBJEXT = @INSTOBJEXT@ INTLLIBS = @INTLLIBS@ LDFLAGS = @LDFLAGS@ +LIBGTOP_INCS = @LIBGTOP_INCS@ +LIBGTOP_LIBS = @LIBGTOP_LIBS@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ LIBTOOL = @LIBTOOL@ @@ -107,11 +116,16 @@ XFCE4_PANEL_LIBS = @XFCE4_PANEL_LIBS@ XFCE4_PANEL_PLUGINSDIR = @XFCE4_PANEL_PLUGINSDIR@ XFCE4_PANEL_REQUIRED_VERSION = @XFCE4_PANEL_REQUIRED_VERSION@ XGETTEXT = @XGETTEXT@ +ac_ct_AR = @ac_ct_AR@ ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_F77 = @ac_ct_F77@ ac_ct_RANLIB = @ac_ct_RANLIB@ ac_ct_STRIP = @ac_ct_STRIP@ am__fastdepCC_FALSE = @am__fastdepCC_FALSE@ am__fastdepCC_TRUE = @am__fastdepCC_TRUE@ +am__fastdepCXX_FALSE = @am__fastdepCXX_FALSE@ +am__fastdepCXX_TRUE = @am__fastdepCXX_TRUE@ am__include = @am__include@ am__leading_dot = @am__leading_dot@ am__quote = @am__quote@ @@ -163,10 +177,12 @@ libnetload_la_SOURCES = \ libnetload_la_CFLAGS = \ -DPACKAGE_LOCALE_DIR=\"$(localedir)\" \ + @LIBGTOP_INCS@ \ @XFCE4_PANEL_CFLAGS@ libnetload_la_LIBADD = \ + @LIBGTOP_LIBS@ \ @XFCE4_PANEL_LIBS@ subdir = panel-plugin diff --git a/panel-plugin/net.h b/panel-plugin/net.h index f98b9d9..c8f13ff 100644 --- a/panel-plugin/net.h +++ b/panel-plugin/net.h @@ -1,32 +1,43 @@ -/* - * Copyright (c) 2003 Riccardo Persichetti +/* XFce 4 - Netload Plugin + * Copyright (c) 2003 Bernhard Walle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#ifndef __NET_H -#define __NET_H + +#ifndef _NET_H_ +#define _NET_H_ #include -void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *tot); +/** + * Should be called to initialize. + */ +void init_netload(); -#endif /* ndef __NET_H */ +/** + * Gets the current netload. You must call init_netload() once before you use this function! + * @param device A device string like "ippp0" or "eth0". + * @param in Input load in byte/s. + * @param out Output load in byte/s. + * @param tot Total load in byte/s. + */ +void get_current_netload(const gchar* device, gint64 *in, gint64 *out, gint64 *tot); + +/** + * Should be called to do cleanup work. + */ +void close_netload(); + +#endif /* _NET_H_ */ diff --git a/panel-plugin/net_freebsd.c b/panel-plugin/net_freebsd.c index 40855a5..6fe7e18 100644 --- a/panel-plugin/net_freebsd.c +++ b/panel-plugin/net_freebsd.c @@ -1,28 +1,22 @@ -/* - * Copyright (c) 2003 Bernhard Walle +/* XFce 4 - Netload Plugin + * Copyright (c) 2003 Bernhard Walle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + #ifdef HAVE_CONFIG_H #include #endif @@ -32,17 +26,13 @@ #include #include #include +#include +#include + #include #include #include #include -#include -#include - -#include -#include -#include -#include #include "net.h" @@ -54,84 +44,98 @@ * Converted from C++ to C (using glib data types) by Bernhard Walle */ -void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *tot) + +static guint64 prev_ibytes, cur_ibytes, offset_ibytes; +static guint64 prev_obytes, cur_obytes, offset_obytes; +static struct timeval prev_time; + +void init_netload() +{ + prev_ibytes = cur_ibytes = offset_ibytes = 0; + prev_obytes = cur_obytes = offset_obytes = 0; + gettimeofday(&prev_time, NULL); +} + + +void get_current_netload(const gchar* device, gint64 *in, gint64 *out, gint64 *tot) { - static guint64 prev_ibytes = 0, cur_ibytes = 0, offset_ibytes = 0; - static guint64 prev_obytes = 0, cur_obytes = 0, offset_obytes = 0; - static struct timeval prev_time; static gboolean first = TRUE; struct timeval curr_time; gdouble delta_t; gint64 ni, no; struct ifmibdata ifmd; - size_t ifmd_size=sizeof ifmd; + size_t ifmd_size = sizeof(ifmd); gint nr_network_devs; - size_t int_size=sizeof nr_network_devs; - gint name[6]; + size_t int_size = sizeof(nr_network_devs); + gint name[] = { + CTL_NET, /* 0 */ + PF_LINK, /* 1 */ + NETLINK_GENERIC, /* 2 */ + IFMIB_IFDATA, /* 3 */ + 0, /* 4 */ + IFDATA_GENERAL /* 5 */ + }; gint i; - if (in != NULL && out != NULL && tot != NULL) - { - *in = *out = *tot = 0; - } - - if (first) - { - gettimeofday(&prev_time, NULL); - } gettimeofday(&curr_time, NULL); delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; - // FreeBSD code by Ronald Klop - name[0] = CTL_NET; - name[1] = PF_LINK; - name[2] = NETLINK_GENERIC; - name[3] = IFMIB_IFDATA; - name[5] = IFDATA_GENERAL; - - if(sysctlbyname("net.link.generic.system.ifcount",&nr_network_devs, - &int_size,(void*)0,0) == -1) { - g_printf("%s@%d: %s\n",__FILE__,__LINE__,strerror(errno)); - } else { - for(i=1;i<=nr_network_devs;i++) { - name[4] = i; /* row of the ifmib table */ - - if(sysctl(name, 6, &ifmd, &ifmd_size, (void *)0, 0) == -1) { - printf(_("%s@%d: %s\n"),__FILE__,__LINE__,strerror(errno)); - continue; - } - if (strncmp(ifmd.ifmd_name, device, strlen(device)) == 0) { - cur_ibytes = ifmd.ifmd_data.ifi_ibytes; - cur_obytes = ifmd.ifmd_data.ifi_obytes; - break; - } - } - } + if(sysctlbyname("net.link.generic.system.ifcount", &nr_network_devs, &int_size, NULL, 0) == -1) + { + g_printf("%s@%d: %s\n", __FILE__, __LINE__, strerror(errno)); + } + else + { + for( i = 1; i <= nr_network_devs; i++ ) + { + name[4] = i; /* row of the ifmib table */ + + if( sysctl(name, 6, &ifmd, &ifmd_size, (void *)0, 0) == -1 ) + { + printf("%s@%d: %s\n" ,__FILE__ ,__LINE__, strerror(errno)); + continue; + } + if ( strncmp(ifmd.ifmd_name, device, strlen(device)) == 0 ) + { + cur_ibytes = ifmd.ifmd_data.ifi_ibytes; + cur_obytes = ifmd.ifmd_data.ifi_obytes; + break; + } + } + } cur_ibytes += offset_ibytes; cur_obytes += offset_obytes; if (cur_ibytes < prev_ibytes) - // har, har, overflow. Use the recent prev_ibytes value as offset this time + { + /* har, har, overflow. Use the recent prev_ibytes value as offset this time */ cur_ibytes = offset_ibytes = prev_ibytes; + } if (cur_obytes < prev_obytes) - // har, har, overflow. Use the recent prev_obytes value as offset this time + { + /* har, har, overflow. Use the recent prev_obytes value as offset this time */ cur_obytes = offset_obytes = prev_obytes; - + } ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t); no = (gint64)((cur_obytes - prev_obytes) / delta_t); - if (in != NULL && out != NULL && tot != NULL) + if (in != NULL && out != NULL && tot != NULL && !first) { *in = ni; *out = no; *tot = *in + *out; } + else + { + *in = *out = *tot = 0; + first = FALSE; + } prev_time.tv_sec = curr_time.tv_sec; prev_time.tv_usec = curr_time.tv_usec; @@ -139,10 +143,10 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 * prev_ibytes = cur_ibytes; prev_obytes = cur_obytes; - if (first) - { - *in = *out = *tot = 0; - first = FALSE; - } +} + +void close_netload() +{ + /* Do nothing here */ } diff --git a/panel-plugin/net_libgtop2.c b/panel-plugin/net_libgtop2.c new file mode 100644 index 0000000..336a0ef --- /dev/null +++ b/panel-plugin/net_libgtop2.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2003 Bernhard Walle + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "net.h" + +static glibtop_netload netload; +static struct timeval prev_time; +static guint64 prev_ibytes, cur_ibytes; +static guint64 prev_obytes, cur_obytes; +static guint64 prev_tbytes, cur_tbytes; + +void init_netload() +{ + glibtop_init(); + prev_ibytes = cur_ibytes = 0; + prev_obytes = cur_obytes = 0; + prev_tbytes = cur_tbytes = 0; + gettimeofday(&prev_time, NULL); +} + +void get_current_netload(const gchar* device, gint64 *in, gint64 *out, gint64 *tot) +{ + static gboolean first = TRUE; + struct timeval curr_time; + gdouble delta_t; + + if (in != NULL && out != NULL && tot != NULL) + { + *in = *out = *tot = 0; + } + + gettimeofday(&curr_time, NULL); + delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L + + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; + + glibtop_get_netload (&netload, device); + + cur_ibytes = (gint64) netload.bytes_in; + cur_obytes = (gint64) netload.bytes_out; + cur_tbytes = (gint64) netload.bytes_total; + + if (in != NULL && out != NULL && tot != NULL && !first) + { + *in = (gint64)((cur_ibytes - prev_ibytes) / delta_t); + *out = (gint64)((cur_obytes - prev_obytes) / delta_t); + *tot = (gint64)((cur_tbytes - prev_tbytes) / delta_t); + } + else + { + *in = *out = *tot = 0; + first = FALSE; + } + + prev_time.tv_sec = curr_time.tv_sec; + prev_time.tv_usec = curr_time.tv_usec; + + prev_ibytes = cur_ibytes; + prev_obytes = cur_obytes; + prev_tbytes = cur_tbytes; + +} + +void close_netload() +{ + glibtop_close(); +} + diff --git a/panel-plugin/net_linux.c b/panel-plugin/net_linux.c index 4acb3d2..8eb852e 100644 --- a/panel-plugin/net_linux.c +++ b/panel-plugin/net_linux.c @@ -1,27 +1,19 @@ - -/* - * Copyright (c) 2003 Bernhard Walle +/* XFce 4 - Netload Plugin + * Copyright (c) 2003 Bernhard Walle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H @@ -35,14 +27,8 @@ #include #include -#include -#include -#include -#include - #include "net.h" - /* * Basic code taken from IceWM * (c) Marko Macek and others, GNU General Public License @@ -50,11 +36,20 @@ * Converted from C++ to C (using glib data types) by Bernhard Walle */ -void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *tot) + +static guint64 prev_ibytes, cur_ibytes, offset_ibytes; +static guint64 prev_obytes, cur_obytes, offset_obytes; +static struct timeval prev_time; + +void init_netload() +{ + prev_ibytes = cur_ibytes = offset_ibytes = 0; + prev_obytes = cur_obytes = offset_obytes = 0; + gettimeofday(&prev_time, NULL); +} + +void get_current_netload(const gchar* device, gint64 *in, gint64 *out, gint64 *tot) { - static guint64 prev_ibytes = 0, cur_ibytes = 0, offset_ibytes = 0; - static guint64 prev_obytes = 0, cur_obytes = 0, offset_obytes = 0; - static struct timeval prev_time; static gboolean first = TRUE; struct timeval curr_time; gdouble delta_t; @@ -64,80 +59,83 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 * gchar *p; gint64 ipackets, opackets, ierrs, oerrs, idrop, odrop, ififo, ofifo, iframe, ocolls, ocarrier, icomp, ocomp, imcast; - + FILE *fp = NULL; - - if (in != NULL && out != NULL && tot != NULL) - { - *in = *out = *tot = 0; - } - - if (first) - { - gettimeofday(&prev_time, NULL); - } + gettimeofday(&curr_time, NULL); delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; - + if ( (fp = fopen("/proc/net/dev", "r")) == NULL) { + *in = *out = *tot = 0; return; } - - while ( fgets(buf, BUFSIZ, fp) != NULL ) { + + while ( fgets(buf, BUFSIZ, fp) != NULL ) + { p = buf; while (*p == ' ') { p++; } - + if (strncmp(p, device, strlen(device)) == 0 && p[strlen(device)] == ':') { p = strchr(p, ':') + 1; - - if (sscanf(p, "%llu %lld %lld %lld %lld %lld %lld %lld" " %llu %lld %lld %lld %lld %lld %lld %lld", - &cur_ibytes, &ipackets, &ierrs, &idrop, &ififo, &iframe, &icomp, &imcast, - &cur_obytes, &opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier, &ocomp) != 16) + + if (sscanf( + p, + "%llu %lld %lld %lld %lld %lld %lld %lld" " %llu %lld %lld %lld %lld %lld %lld %lld", + &cur_ibytes, &ipackets, &ierrs, &idrop, &ififo, &iframe, &icomp, &imcast, + &cur_obytes, &opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier, &ocomp) != 16 ) { ipackets = opackets = 0; sscanf(p, "%lld %lld %lld %lld %lld" " %lld %lld %lld %lld %lld %lld", &ipackets, &ierrs, &idrop, &ififo, &iframe, &opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier); - // for linux<2.0 fake packets as bytes (we only need relative values anyway) + /* for linux<2.0 fake packets as bytes (we only need relative values anyway) */ cur_ibytes = ipackets; cur_obytes = opackets; } - + break; } } fclose(fp); - + cur_ibytes += offset_ibytes; cur_obytes += offset_obytes; - + if (cur_ibytes < prev_ibytes) - // har, har, overflow. Use the recent prev_ibytes value as offset this time + { + /* har, har, overflow. Use the recent prev_ibytes value as offset this time */ cur_ibytes = offset_ibytes = prev_ibytes; - + } + if (cur_obytes < prev_obytes) - // har, har, overflow. Use the recent prev_obytes value as offset this time + { + /* har, har, overflow. Use the recent prev_obytes value as offset this time */ cur_obytes = offset_obytes = prev_obytes; - - - + } + + ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t); no = (gint64)((cur_obytes - prev_obytes) / delta_t); - - if (in != NULL && out != NULL && tot != NULL) + + if (in != NULL && out != NULL && tot != NULL && !first) { *in = ni; *out = no; *tot = *in + *out; } + else + { + *in = *out = *tot = 0; + first = FALSE; + } prev_time.tv_sec = curr_time.tv_sec; prev_time.tv_usec = curr_time.tv_usec; @@ -145,10 +143,10 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 * prev_ibytes = cur_ibytes; prev_obytes = cur_obytes; - if (first) - { - *in = *out = *tot = 0; - first = FALSE; - } +} + +void close_netload() +{ + /* Do nothing here */ } diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index 8fcf8f0..7b8d405 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -1,26 +1,19 @@ -/* - * Copyright (c) 2003 Benedikt Meurer +/* XFce 4 - Netload Plugin + * Copyright (c) 2003 Bernhard Walle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H @@ -146,7 +139,7 @@ static gint update_monitors(t_global_monitor *global) } g_snprintf(caption, sizeof(caption), - _("Incoming: %lld bit/s\nOutgoing: %lld bit/s\nTotal: %lld bit/s"), + _("Incoming: %lld byte/s\nOutgoing: %lld byte/s\nTotal: %lld byte/s"), net[IN], net[OUT], net[TOT]); gtk_tooltips_set_tip(tooltips, GTK_WIDGET(global->monitor->ebox), caption, NULL); @@ -235,6 +228,8 @@ static t_global_monitor * monitor_new(void) gtk_container_add(GTK_CONTAINER(global->ebox), GTK_WIDGET(global->box)); + + init_netload(); return global; } @@ -375,6 +370,8 @@ monitor_free(Control *ctrl) g_free(global->monitor->options.label_text); } g_free(global); + + close_netload(); } static void setup_monitor(t_global_monitor *global)