* Added libgtop2 support

* Changed comment to GPL
* Better organize of code (init and close function)


(Old svn revision: 286)
master
Bernhard Walle 2003-08-17 18:55:46 +00:00
parent fb8d345119
commit 39c7c55760
7 changed files with 325 additions and 193 deletions

View File

@ -15,7 +15,9 @@ libnetload_la_SOURCES = \
libnetload_la_CFLAGS = \ libnetload_la_CFLAGS = \
-DPACKAGE_LOCALE_DIR=\"$(localedir)\" \ -DPACKAGE_LOCALE_DIR=\"$(localedir)\" \
@LIBGTOP_INCS@ \
@XFCE4_PANEL_CFLAGS@ @XFCE4_PANEL_CFLAGS@
libnetload_la_LIBADD = \ libnetload_la_LIBADD = \
@LIBGTOP_LIBS@ \
@XFCE4_PANEL_LIBS@ @XFCE4_PANEL_LIBS@

View File

@ -40,6 +40,7 @@ ACLOCAL = @ACLOCAL@
AMDEP_FALSE = @AMDEP_FALSE@ AMDEP_FALSE = @AMDEP_FALSE@
AMDEP_TRUE = @AMDEP_TRUE@ AMDEP_TRUE = @AMDEP_TRUE@
AMTAR = @AMTAR@ AMTAR = @AMTAR@
AR = @AR@
AUTOCONF = @AUTOCONF@ AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@ AUTOHEADER = @AUTOHEADER@
AUTOMAKE = @AUTOMAKE@ AUTOMAKE = @AUTOMAKE@
@ -51,6 +52,10 @@ CCDEPMODE = @CCDEPMODE@
CFLAGS = @CFLAGS@ CFLAGS = @CFLAGS@
CPP = @CPP@ CPP = @CPP@
CPPFLAGS = @CPPFLAGS@ CPPFLAGS = @CPPFLAGS@
CXX = @CXX@
CXXCPP = @CXXCPP@
CXXDEPMODE = @CXXDEPMODE@
CXXFLAGS = @CXXFLAGS@
CYGPATH_W = @CYGPATH_W@ CYGPATH_W = @CYGPATH_W@
DATADIRNAME = @DATADIRNAME@ DATADIRNAME = @DATADIRNAME@
DEFS = @DEFS@ DEFS = @DEFS@
@ -61,6 +66,8 @@ ECHO_N = @ECHO_N@
ECHO_T = @ECHO_T@ ECHO_T = @ECHO_T@
EGREP = @EGREP@ EGREP = @EGREP@
EXEEXT = @EXEEXT@ EXEEXT = @EXEEXT@
F77 = @F77@
FFLAGS = @FFLAGS@
GETTEXT_PACKAGE = @GETTEXT_PACKAGE@ GETTEXT_PACKAGE = @GETTEXT_PACKAGE@
GMOFILES = @GMOFILES@ GMOFILES = @GMOFILES@
GMSGFMT = @GMSGFMT@ GMSGFMT = @GMSGFMT@
@ -71,6 +78,8 @@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
INSTOBJEXT = @INSTOBJEXT@ INSTOBJEXT = @INSTOBJEXT@
INTLLIBS = @INTLLIBS@ INTLLIBS = @INTLLIBS@
LDFLAGS = @LDFLAGS@ LDFLAGS = @LDFLAGS@
LIBGTOP_INCS = @LIBGTOP_INCS@
LIBGTOP_LIBS = @LIBGTOP_LIBS@
LIBOBJS = @LIBOBJS@ LIBOBJS = @LIBOBJS@
LIBS = @LIBS@ LIBS = @LIBS@
LIBTOOL = @LIBTOOL@ LIBTOOL = @LIBTOOL@
@ -107,11 +116,16 @@ XFCE4_PANEL_LIBS = @XFCE4_PANEL_LIBS@
XFCE4_PANEL_PLUGINSDIR = @XFCE4_PANEL_PLUGINSDIR@ XFCE4_PANEL_PLUGINSDIR = @XFCE4_PANEL_PLUGINSDIR@
XFCE4_PANEL_REQUIRED_VERSION = @XFCE4_PANEL_REQUIRED_VERSION@ XFCE4_PANEL_REQUIRED_VERSION = @XFCE4_PANEL_REQUIRED_VERSION@
XGETTEXT = @XGETTEXT@ XGETTEXT = @XGETTEXT@
ac_ct_AR = @ac_ct_AR@
ac_ct_CC = @ac_ct_CC@ 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_RANLIB = @ac_ct_RANLIB@
ac_ct_STRIP = @ac_ct_STRIP@ ac_ct_STRIP = @ac_ct_STRIP@
am__fastdepCC_FALSE = @am__fastdepCC_FALSE@ am__fastdepCC_FALSE = @am__fastdepCC_FALSE@
am__fastdepCC_TRUE = @am__fastdepCC_TRUE@ am__fastdepCC_TRUE = @am__fastdepCC_TRUE@
am__fastdepCXX_FALSE = @am__fastdepCXX_FALSE@
am__fastdepCXX_TRUE = @am__fastdepCXX_TRUE@
am__include = @am__include@ am__include = @am__include@
am__leading_dot = @am__leading_dot@ am__leading_dot = @am__leading_dot@
am__quote = @am__quote@ am__quote = @am__quote@
@ -163,10 +177,12 @@ libnetload_la_SOURCES = \
libnetload_la_CFLAGS = \ libnetload_la_CFLAGS = \
-DPACKAGE_LOCALE_DIR=\"$(localedir)\" \ -DPACKAGE_LOCALE_DIR=\"$(localedir)\" \
@LIBGTOP_INCS@ \
@XFCE4_PANEL_CFLAGS@ @XFCE4_PANEL_CFLAGS@
libnetload_la_LIBADD = \ libnetload_la_LIBADD = \
@LIBGTOP_LIBS@ \
@XFCE4_PANEL_LIBS@ @XFCE4_PANEL_LIBS@
subdir = panel-plugin subdir = panel-plugin

View File

@ -1,32 +1,43 @@
/* /* XFce 4 - Netload Plugin
* Copyright (c) 2003 Riccardo Persichetti <ricpersi@libero.it> * Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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 * This program is distributed in the hope that it will be useful,
* modification, are permitted provided that the following conditions * but WITHOUT ANY WARRANTY; without even the implied warranty of
* are met: * 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 * You should have received a copy of the GNU General Public License
* notice, this list of conditions and the following disclaimer. * along with this program; if not, write to the Free Software
* 2. Redistributions in binary form must reproduce the above copyright * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 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.
*/ */
#ifndef __NET_H
#define __NET_H #ifndef _NET_H_
#define _NET_H_
#include <gtk/gtk.h> #include <gtk/gtk.h>
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_ */

View File

@ -1,28 +1,22 @@
/* /* XFce 4 - Netload Plugin
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de> * Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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 * This program is distributed in the hope that it will be useful,
* modification, are permitted provided that the following conditions * but WITHOUT ANY WARRANTY; without even the implied warranty of
* are met: * 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 * You should have received a copy of the GNU General Public License
* notice, this list of conditions and the following disclaimer. * along with this program; if not, write to the Free Software
* 2. Redistributions in binary form must reproduce the above copyright * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 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 #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
@ -32,17 +26,13 @@
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <net/if.h> #include <net/if.h>
#include <net/if_mib.h> #include <net/if_mib.h>
#include <time.h>
#include <sys/time.h>
#include <libxfce4util/i18n.h>
#include <libxfcegui4/dialogs.h>
#include <panel/plugins.h>
#include <panel/xfce.h>
#include "net.h" #include "net.h"
@ -54,84 +44,98 @@
* Converted from C++ to C (using glib data types) by Bernhard Walle * 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; static gboolean first = TRUE;
struct timeval curr_time; struct timeval curr_time;
gdouble delta_t; gdouble delta_t;
gint64 ni, no; gint64 ni, no;
struct ifmibdata ifmd; struct ifmibdata ifmd;
size_t ifmd_size=sizeof ifmd; size_t ifmd_size = sizeof(ifmd);
gint nr_network_devs; gint nr_network_devs;
size_t int_size=sizeof nr_network_devs; size_t int_size = sizeof(nr_network_devs);
gint name[6]; gint name[] = {
CTL_NET, /* 0 */
PF_LINK, /* 1 */
NETLINK_GENERIC, /* 2 */
IFMIB_IFDATA, /* 3 */
0, /* 4 */
IFDATA_GENERAL /* 5 */
};
gint i; gint i;
if (in != NULL && out != NULL && tot != NULL)
{
*in = *out = *tot = 0;
}
if (first)
{
gettimeofday(&prev_time, NULL);
}
gettimeofday(&curr_time, NULL); gettimeofday(&curr_time, NULL);
delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L
+ (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0;
// FreeBSD code by Ronald Klop <ronald@cs.vu.nl> if(sysctlbyname("net.link.generic.system.ifcount", &nr_network_devs, &int_size, NULL, 0) == -1)
name[0] = CTL_NET; {
name[1] = PF_LINK; g_printf("%s@%d: %s\n", __FILE__, __LINE__, strerror(errno));
name[2] = NETLINK_GENERIC; }
name[3] = IFMIB_IFDATA; else
name[5] = IFDATA_GENERAL; {
for( i = 1; i <= nr_network_devs; i++ )
if(sysctlbyname("net.link.generic.system.ifcount",&nr_network_devs, {
&int_size,(void*)0,0) == -1) { name[4] = i; /* row of the ifmib table */
g_printf("%s@%d: %s\n",__FILE__,__LINE__,strerror(errno));
} else { if( sysctl(name, 6, &ifmd, &ifmd_size, (void *)0, 0) == -1 )
for(i=1;i<=nr_network_devs;i++) { {
name[4] = i; /* row of the ifmib table */ printf("%s@%d: %s\n" ,__FILE__ ,__LINE__, strerror(errno));
continue;
if(sysctl(name, 6, &ifmd, &ifmd_size, (void *)0, 0) == -1) { }
printf(_("%s@%d: %s\n"),__FILE__,__LINE__,strerror(errno)); if ( strncmp(ifmd.ifmd_name, device, strlen(device)) == 0 )
continue; {
} cur_ibytes = ifmd.ifmd_data.ifi_ibytes;
if (strncmp(ifmd.ifmd_name, device, strlen(device)) == 0) { cur_obytes = ifmd.ifmd_data.ifi_obytes;
cur_ibytes = ifmd.ifmd_data.ifi_ibytes; break;
cur_obytes = ifmd.ifmd_data.ifi_obytes; }
break; }
} }
}
}
cur_ibytes += offset_ibytes; cur_ibytes += offset_ibytes;
cur_obytes += offset_obytes; cur_obytes += offset_obytes;
if (cur_ibytes < prev_ibytes) 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; cur_ibytes = offset_ibytes = prev_ibytes;
}
if (cur_obytes < prev_obytes) 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; cur_obytes = offset_obytes = prev_obytes;
}
ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t); ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t);
no = (gint64)((cur_obytes - prev_obytes) / 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; *in = ni;
*out = no; *out = no;
*tot = *in + *out; *tot = *in + *out;
} }
else
{
*in = *out = *tot = 0;
first = FALSE;
}
prev_time.tv_sec = curr_time.tv_sec; prev_time.tv_sec = curr_time.tv_sec;
prev_time.tv_usec = curr_time.tv_usec; 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_ibytes = cur_ibytes;
prev_obytes = cur_obytes; prev_obytes = cur_obytes;
if (first) }
{
*in = *out = *tot = 0; void close_netload()
first = FALSE; {
} /* Do nothing here */
} }

104
panel-plugin/net_libgtop2.c Normal file
View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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 <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <time.h>
#include <sys/time.h>
#include <glibtop.h>
#include <glibtop/close.h>
#include <glibtop/netload.h>
#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();
}

View File

@ -1,27 +1,19 @@
/* XFce 4 - Netload Plugin
/* * Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de> *
* 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 * This program is distributed in the hope that it will be useful,
* modification, are permitted provided that the following conditions * but WITHOUT ANY WARRANTY; without even the implied warranty of
* are met: * 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 * You should have received a copy of the GNU General Public License
* notice, this list of conditions and the following disclaimer. * along with this program; if not, write to the Free Software
* 2. Redistributions in binary form must reproduce the above copyright * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 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 #ifdef HAVE_CONFIG_H
@ -35,14 +27,8 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <libxfce4util/i18n.h>
#include <libxfcegui4/dialogs.h>
#include <panel/plugins.h>
#include <panel/xfce.h>
#include "net.h" #include "net.h"
/* /*
* Basic code taken from IceWM * Basic code taken from IceWM
* (c) Marko Macek and others, GNU General Public License * (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 * 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; static gboolean first = TRUE;
struct timeval curr_time; struct timeval curr_time;
gdouble delta_t; gdouble delta_t;
@ -64,80 +59,83 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
gchar *p; gchar *p;
gint64 ipackets, opackets, ierrs, oerrs, idrop, odrop, ififo, ofifo, gint64 ipackets, opackets, ierrs, oerrs, idrop, odrop, ififo, ofifo,
iframe, ocolls, ocarrier, icomp, ocomp, imcast; iframe, ocolls, ocarrier, icomp, ocomp, imcast;
FILE *fp = NULL; FILE *fp = NULL;
if (in != NULL && out != NULL && tot != NULL)
{
*in = *out = *tot = 0;
}
if (first)
{
gettimeofday(&prev_time, NULL);
}
gettimeofday(&curr_time, NULL); gettimeofday(&curr_time, NULL);
delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L
+ (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0;
if ( (fp = fopen("/proc/net/dev", "r")) == NULL) if ( (fp = fopen("/proc/net/dev", "r")) == NULL)
{ {
*in = *out = *tot = 0;
return; return;
} }
while ( fgets(buf, BUFSIZ, fp) != NULL ) { while ( fgets(buf, BUFSIZ, fp) != NULL )
{
p = buf; p = buf;
while (*p == ' ') while (*p == ' ')
{ {
p++; p++;
} }
if (strncmp(p, device, strlen(device)) == 0 && p[strlen(device)] == ':') if (strncmp(p, device, strlen(device)) == 0 && p[strlen(device)] == ':')
{ {
p = strchr(p, ':') + 1; p = strchr(p, ':') + 1;
if (sscanf(p, "%llu %lld %lld %lld %lld %lld %lld %lld" " %llu %lld %lld %lld %lld %lld %lld %lld", if (sscanf(
&cur_ibytes, &ipackets, &ierrs, &idrop, &ififo, &iframe, &icomp, &imcast, p,
&cur_obytes, &opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier, &ocomp) != 16) "%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; ipackets = opackets = 0;
sscanf(p, "%lld %lld %lld %lld %lld" " %lld %lld %lld %lld %lld %lld", sscanf(p, "%lld %lld %lld %lld %lld" " %lld %lld %lld %lld %lld %lld",
&ipackets, &ierrs, &idrop, &ififo, &iframe, &ipackets, &ierrs, &idrop, &ififo, &iframe,
&opackets, &oerrs, &odrop, &ofifo, &ocolls, &ocarrier); &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_ibytes = ipackets;
cur_obytes = opackets; cur_obytes = opackets;
} }
break; break;
} }
} }
fclose(fp); fclose(fp);
cur_ibytes += offset_ibytes; cur_ibytes += offset_ibytes;
cur_obytes += offset_obytes; cur_obytes += offset_obytes;
if (cur_ibytes < prev_ibytes) 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; cur_ibytes = offset_ibytes = prev_ibytes;
}
if (cur_obytes < prev_obytes) 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; cur_obytes = offset_obytes = prev_obytes;
}
ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t); ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t);
no = (gint64)((cur_obytes - prev_obytes) / 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; *in = ni;
*out = no; *out = no;
*tot = *in + *out; *tot = *in + *out;
} }
else
{
*in = *out = *tot = 0;
first = FALSE;
}
prev_time.tv_sec = curr_time.tv_sec; prev_time.tv_sec = curr_time.tv_sec;
prev_time.tv_usec = curr_time.tv_usec; 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_ibytes = cur_ibytes;
prev_obytes = cur_obytes; prev_obytes = cur_obytes;
if (first) }
{
*in = *out = *tot = 0; void close_netload()
first = FALSE; {
} /* Do nothing here */
} }

View File

@ -1,26 +1,19 @@
/* /* XFce 4 - Netload Plugin
* Copyright (c) 2003 Benedikt Meurer <benedikt.meurer@unix-ag.uni-siegen.de> * Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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 * This program is distributed in the hope that it will be useful,
* modification, are permitted provided that the following conditions * but WITHOUT ANY WARRANTY; without even the implied warranty of
* are met: * 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 * You should have received a copy of the GNU General Public License
* notice, this list of conditions and the following disclaimer. * along with this program; if not, write to the Free Software
* 2. Redistributions in binary form must reproduce the above copyright * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 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 #ifdef HAVE_CONFIG_H
@ -146,7 +139,7 @@ static gint update_monitors(t_global_monitor *global)
} }
g_snprintf(caption, sizeof(caption), 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]); net[IN], net[OUT], net[TOT]);
gtk_tooltips_set_tip(tooltips, GTK_WIDGET(global->monitor->ebox), caption, NULL); 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)); gtk_container_add(GTK_CONTAINER(global->ebox), GTK_WIDGET(global->box));
init_netload();
return global; return global;
} }
@ -375,6 +370,8 @@ monitor_free(Control *ctrl)
g_free(global->monitor->options.label_text); g_free(global->monitor->options.label_text);
} }
g_free(global); g_free(global);
close_netload();
} }
static void setup_monitor(t_global_monitor *global) static void setup_monitor(t_global_monitor *global)