ubuntuusers.de

Anhang: indicator-netspeed.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*

This is an Ubuntu appindicator that displays the current network traffic.

Build dependencies:
apt-get install libgtop2-dev libgtk-3-dev libappindicator3-dev

Compile with:
gcc -Wall `pkg-config --cflags --libs gtk+-3.0 appindicator-0.1 libgtop-2.0` -o indicator-netspeed ./indicator-netspeed.c

Based on indicator-netspeed.c from https://gist.github.com/982939

License: this software is in the public domain.

*/

#include <gtk/gtk.h>
#include <libappindicator/app-indicator.h>
#include <glibtop.h>
#include <glibtop/netload.h>
#include <glibtop/netlist.h>

/* update period in seconds */
int period = 1;

AppIndicator *indicator;
GtkWidget *indicator_menu;

GtkWidget *net_down_item;
GtkWidget *net_up_item;
GtkWidget *quit_item;

gchar* format_net_label(int data)
{
    gchar *string;
    if(data < 1000)
    {
        string = g_strdup_printf("%d B/s", data);
    }
    else if(data < 1000000)
    {
        string = g_strdup_printf("%.1f KiB/s", data/1000.0);
    }
    else
    {
        string = g_strdup_printf("%.2f MiB/s", data/1000000.0);
    }
    return string;
}

void get_net(int traffic[2])
{
    static int bytes_in_old = 0;
    static int bytes_out_old = 0;
    static gboolean first_run = TRUE;
    glibtop_netload netload;
    glibtop_netlist netlist;
    int bytes_in = 0;
    int bytes_out = 0;
    int i = 0;
    
    gchar **interfaces = glibtop_get_netlist(&netlist);
    
    for(i = 0; i < netlist.number; i++)
    {
        if (strcmp("lo", interfaces[i]) == 0)
        {
            continue;
        }
        glibtop_get_netload(&netload, interfaces[i]);
        bytes_in += netload.bytes_in;
        bytes_out += netload.bytes_out;
    }
    g_strfreev(interfaces);
    
    if(first_run)
    {
        bytes_in_old = bytes_in;
        bytes_out_old = bytes_out;
        first_run = FALSE;
    }

    traffic[0] = (bytes_in - bytes_in_old) / period;
    traffic[1] = (bytes_out - bytes_out_old) / period;
    
    bytes_in_old = bytes_in;
    bytes_out_old = bytes_out;
}

gboolean update()
{
    int net_traffic[2] = {0, 0};
    get_net(net_traffic);
    int net_down = net_traffic[0];
    int net_up = net_traffic[1];

    
    gchar *net_down_label = format_net_label(net_down);
    gchar *net_up_label = format_net_label(net_up);
    gchar *indicator_label = g_strdup_printf("%s ↓  %s ↑", net_down_label, net_up_label);
    g_free(net_up_label);
    g_free(net_down_label);
    gchar *label_guide = "Net: 10000.00 MiB/s"; /* I wish... */
    app_indicator_set_label(indicator, indicator_label, label_guide);
    g_free(indicator_label);

    
    return TRUE;
}

int main (int argc, char **argv)
{
    gtk_init (&argc, &argv);

    indicator_menu = gtk_menu_new();
       

    quit_item = gtk_menu_item_new_with_label("Quit");
    gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), quit_item);
    g_signal_connect(quit_item, "activate", G_CALLBACK (gtk_main_quit), NULL);

    gtk_widget_show_all(indicator_menu);

    indicator = app_indicator_new ("netspeed", "network-transmit-receive", APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
    app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_label(indicator, "netspeed", "netspeed");
    app_indicator_set_menu(indicator, GTK_MENU (indicator_menu));
 
    update();
    
    /* update period in milliseconds */
    g_timeout_add(1000*period, update, NULL);

    gtk_main ();

    return 0;
}
Anhang herunterladen

Diese Revision wurde am 22. Mai 2017 18:30 von Heinrich_Schwietering erstellt.