Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CC=gcc
CFLAGS=-g -Wall -Wfatal-errors -std=c99 $(shell pkg-config --cflags --libs gtk+-3.0 appindicator3-0.1 libgtop-2.0)
PREFIX=~/.local

all: indicator-netspeed

Expand All @@ -10,11 +11,11 @@ clean:
rm -f *.o indicator-netspeed

install:
install --mode=755 indicator-netspeed /usr/bin/
install indicator-netspeed.gschema.xml /usr/share/glib-2.0/schemas/
glib-compile-schemas /usr/share/glib-2.0/schemas/
install --mode=755 indicator-netspeed ${PREFIX}/bin/
install indicator-netspeed.gschema.xml ${PREFIX}/share/glib-2.0/schemas/
glib-compile-schemas ${PREFIX}/share/glib-2.0/schemas/

uninstall:
rm /usr/bin/indicator-netspeed
rm /usr/share/glib-2.0/schemas/indicator-netspeed.gschema.xml
glib-compile-schemas /usr/share/glib-2.0/schemas/
rm ${PREFIX}/bin/indicator-netspeed
rm ${PREFIX}/share/glib-2.0/schemas/indicator-netspeed.gschema.xml
glib-compile-schemas ${PREFIX}/share/glib-2.0/schemas/
126 changes: 68 additions & 58 deletions indicator-netspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,35 @@ GtkWidget *net_down_item;
GtkWidget *net_up_item;
GtkWidget *quit_item;

gchar* format_net_label(int data, bool padding)
gchar* format_net_label(int data, char* emoji, bool padding)
{
gchar *string;

double speed = data/1024.0;
if (!data)
{
return NULL;
}

/*if(data < 1000)
{
string = g_strdup_printf("%d B/s", data);
}
else*/ if(data < 1000000) //should be < 1 MiB and not 1 MB, but this keeps width smaller
else*/ if(speed < 1000000) //should be < 1 MiB and not 1 MB, but this keeps width smaller
{
string = g_strdup_printf("%.1f KiB/s", data/1024.0);
string = g_strdup_printf("%.1f KiB/s", speed);
}
else
{
string = g_strdup_printf("%.2f MiB/s", data/1048576.0);
string = g_strdup_printf("%.2f MiB/s", speed/1024);
}
//will someone have 1 gb/s ? maybe...

if (emoji)
{
string = g_strdup_printf ("%s %s", emoji, string);
}

if(padding)
{
//render string and get its pixel width
Expand Down Expand Up @@ -136,18 +148,63 @@ void get_net(int traffic[2])
bytes_out_old = bytes_out;
}

gboolean update() {
//get sum of up and down net traffic and separate values
//and refresh labels of current interface
int net_traffic[2] = {0, 0};
get_net(net_traffic);
int net_down = net_traffic[0];
int net_up = net_traffic[1];
int net_total = net_down + net_up;

gchar *indicator_label = format_net_label(net_total, NULL, true);
if (indicator_label)
{
gchar *label_guide = "10000.00 MiB/s"; //maximum length label text, doesn't really work atm
app_indicator_set_label(indicator, indicator_label, label_guide);
g_free(indicator_label);
} else {
app_indicator_set_label(indicator, "", "");
}

gchar *net_down_label = format_net_label(net_down, "🠋", false);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_down_item), net_down_label);
g_free(net_down_label);

gchar *net_up_label = format_net_label(net_up, "🠉", false);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_up_item), net_up_label);
g_free(net_up_label);

if (net_down && net_up)
{
app_indicator_set_icon(indicator, "network-transmit-receive");
}
else if (net_down)
{
app_indicator_set_icon(indicator, "network-receive");
}
else if (net_up)
{
app_indicator_set_icon(indicator, "network-transmit");
}
else {
app_indicator_set_icon(indicator, "network-idle");
}

return TRUE;
}

void if_signal_select(GtkMenuItem *menu_item, gpointer user_data) {
//set currently selected interface from user selection
gchar *old_if_name = selected_if_name;


// We're allocating a new string here, but we don't need to free the previous
// one because the call to gtk_menu_item_set_label below does that already.
selected_if_name = g_strdup(gtk_menu_item_get_label(menu_item));
TRACE("Selected interface %s\n", selected_if_name);
// frees the previous selected_if_name
gtk_menu_item_set_label(if_chosen, selected_if_name);
gtk_menu_item_set_label(GTK_MENU_ITEM(if_chosen), selected_if_name);
g_settings_set_value (settings, "if-name", g_variant_new_string(selected_if_name));


first_run = TRUE;
update();
Expand Down Expand Up @@ -184,47 +241,6 @@ void add_netifs() {
g_strfreev(interfaces);
}

gboolean update() {
//get sum of up and down net traffic and separate values
//and refresh labels of current interface
int net_traffic[2] = {0, 0};
get_net(net_traffic);
int net_down = net_traffic[0];
int net_up = net_traffic[1];
int net_total = net_down + net_up;

gchar *indicator_label = format_net_label(net_total, true);
gchar *label_guide = "10000.00 MiB/s"; //maximum length label text, doesn't really work atm
app_indicator_set_label(indicator, indicator_label, label_guide);
g_free(indicator_label);

gchar *net_down_label = format_net_label(net_down, false);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_down_item), net_down_label);
g_free(net_down_label);

gchar *net_up_label = format_net_label(net_up, false);
gtk_menu_item_set_label(GTK_MENU_ITEM(net_up_item), net_up_label);
g_free(net_up_label);

if (net_down && net_up)
{
app_indicator_set_icon(indicator, "network-transmit-receive");
}
else if (net_down)
{
app_indicator_set_icon(indicator, "network-receive");
}
else if (net_up)
{
app_indicator_set_icon(indicator, "network-transmit");
}
else {
app_indicator_set_icon(indicator, "network-idle");
}

return TRUE;
}

int main (int argc, char **argv)
{
if (argc > 1 && strcmp("--trace", argv[1]) == 0)
Expand All @@ -249,24 +265,18 @@ int main (int argc, char **argv)
//add interface names
if_chosen = gtk_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), if_chosen);
gtk_menu_item_set_submenu (if_chosen, interfaces_menu);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(if_chosen), interfaces_menu);
add_netifs();

//separator
GtkWidget *sep = gtk_separator_menu_item_new();
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), sep);

//add menu entries for up and down speed
net_down_item = gtk_image_menu_item_new_with_label("");
GtkWidget *net_down_icon = gtk_image_new_from_icon_name("network-receive", GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(net_down_item), net_down_icon);
gtk_image_menu_item_set_always_show_image(GTK_IMAGE_MENU_ITEM(net_down_item), TRUE);
net_down_item = gtk_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), net_down_item);

net_up_item = gtk_image_menu_item_new_with_label("");
GtkWidget *net_up_icon = gtk_image_new_from_icon_name("network-transmit", GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(net_up_item), net_up_icon);
gtk_image_menu_item_set_always_show_image(GTK_IMAGE_MENU_ITEM(net_up_item), TRUE);
net_up_item = gtk_menu_item_new_with_label("");
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), net_up_item);

//separator
Expand Down