* Added libgtop2 support
* Changed comment to GPL * Better organize of code (init and close function) (Old svn revision: 286)master
parent
fb8d345119
commit
39c7c55760
|
@ -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@
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,32 +1,43 @@
|
|||
/*
|
||||
* Copyright (c) 2003 Riccardo Persichetti <ricpersi@libero.it>
|
||||
/* XFce 4 - Netload Plugin
|
||||
* 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:
|
||||
* 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.
|
||||
*
|
||||
* 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 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.
|
||||
*
|
||||
* 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 <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_ */
|
||||
|
|
|
@ -1,28 +1,22 @@
|
|||
/*
|
||||
/* XFce 4 - Netload Plugin
|
||||
* 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:
|
||||
* 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.
|
||||
*
|
||||
* 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 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.
|
||||
*
|
||||
* 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 <config.h>
|
||||
#endif
|
||||
|
@ -32,17 +26,13 @@
|
|||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <net/if.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"
|
||||
|
||||
|
@ -54,55 +44,61 @@
|
|||
* 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 <ronald@cs.vu.nl>
|
||||
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++) {
|
||||
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));
|
||||
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) {
|
||||
if ( strncmp(ifmd.ifmd_name, device, strlen(device)) == 0 )
|
||||
{
|
||||
cur_ibytes = ifmd.ifmd_data.ifi_ibytes;
|
||||
cur_obytes = ifmd.ifmd_data.ifi_obytes;
|
||||
break;
|
||||
|
@ -114,24 +110,32 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
|
|||
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 */
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -1,27 +1,19 @@
|
|||
|
||||
/*
|
||||
/* XFce 4 - Netload Plugin
|
||||
* 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:
|
||||
* 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.
|
||||
*
|
||||
* 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 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.
|
||||
*
|
||||
* 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 <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"
|
||||
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
@ -67,15 +62,6 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
|
|||
|
||||
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
|
||||
|
@ -84,10 +70,12 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
|
|||
|
||||
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 == ' ')
|
||||
{
|
||||
|
@ -98,15 +86,17 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
|
|||
{
|
||||
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(
|
||||
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)
|
||||
&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;
|
||||
}
|
||||
|
@ -120,24 +110,32 @@ void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *
|
|||
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 */
|
||||
}
|
||||
|
||||
|
|
|
@ -1,26 +1,19 @@
|
|||
/*
|
||||
* Copyright (c) 2003 Benedikt Meurer <benedikt.meurer@unix-ag.uni-siegen.de>
|
||||
/* XFce 4 - Netload Plugin
|
||||
* 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:
|
||||
* 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.
|
||||
*
|
||||
* 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 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.
|
||||
*
|
||||
* 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);
|
||||
|
||||
|
@ -236,6 +229,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)
|
||||
|
|
Loading…
Reference in New Issue