-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindicator-netspeed.c
302 lines (241 loc) · 9.35 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This is an Ubuntu appindicator that displays the current network traffic.
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>
#include <pango/pango.h>
#include <gio/gio.h>
#include <stdbool.h>
#define TRACE(...) if (trace) { printf( __VA_ARGS__); fflush(stdout); } else {}
bool trace = false;
/* update period in seconds */
int period = 1;
gboolean first_run = TRUE;
GSettings *settings;
AppIndicator *indicator;
GtkWidget *indicator_menu;
GtkWidget *interfaces_menu;
gchar* selected_if_name = NULL;
GtkWidget *if_chosen;
GtkWidget *net_down_item;
GtkWidget *net_up_item;
GtkWidget *quit_item;
gchar* format_net_label(int data, bool padding)
{
gchar *string;
/*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
{
string = g_strdup_printf("%.1f KiB/s", data/1024.0);
}
else
{
string = g_strdup_printf("%.2f MiB/s", data/1048576.0);
}
//will someone have 1 gb/s ? maybe...
if(padding)
{
//render string and get its pixel width
int width = 0;
static int maxWidth = 12; //max width for label in pixels
//TODO: should be determined from current panel font type and size
int spaceWidth = 4; //width of one space char in pixels,
PangoContext* context = gtk_widget_get_pango_context(indicator_menu);
PangoLayout* layout = pango_layout_new(context);
pango_layout_set_text(layout, string, strlen(string));
pango_layout_get_pixel_size(layout, &width, NULL);
// frees the layout object, do not use after this point
g_object_unref(layout);
//push max size up as needed
if (width > maxWidth) maxWidth = width + spaceWidth;
gchar *old_string = string;
//fill up with spaces
string = g_strdup_printf("%*s%s", (int)((maxWidth-width)/spaceWidth), " ", string);
g_free(old_string);
}
return string;
}
void get_net(int traffic[2])
{
TRACE("\n")
TRACE("getting net traffic...\n")
TRACE("selected interface is %s\n", selected_if_name)
static int bytes_in_old = 0;
static int bytes_out_old = 0;
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++)
{
TRACE("\n")
TRACE("processing interface %s\n", interfaces[i])
if (strcmp("lo", interfaces[i]) == 0)
{
TRACE("skipping loopback interface\n")
continue;
}
if(strcmp("all", selected_if_name) == 0 || strcmp(selected_if_name, interfaces[i]) == 0) {
glibtop_get_netload(&netload, interfaces[i]);
TRACE("in: %ld, out: %ld\n", netload.bytes_in, netload.bytes_out)
bytes_in += netload.bytes_in;
bytes_out += netload.bytes_out;
}
else
{
TRACE("skipping\n")
}
}
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;
}
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);
g_settings_set_value (settings, "if-name", g_variant_new_string(selected_if_name));
first_run = TRUE;
update();
}
void add_netifs() {
TRACE("populating list of interfaces\n")
//populate list of interfaces
//TODO: make this refresh when interfaces change
glibtop_netlist netlist;
gchar **interfaces = glibtop_get_netlist(&netlist);
GtkWidget *if_item;
for(int i = 0; i < netlist.number; i++) {
if (strcmp("lo", interfaces[i]) == 0)
{
TRACE("skipping loopback interface\n")
continue;
}
TRACE("adding interface %s\n", interfaces[i]);
if_item = gtk_menu_item_new_with_label(interfaces[i]);
gtk_menu_shell_append(GTK_MENU_SHELL(interfaces_menu), if_item);
g_signal_connect (G_OBJECT(if_item), "activate", G_CALLBACK(if_signal_select), NULL);
}
if_item = gtk_menu_item_new_with_label("all"); //can name and id be different?
gtk_menu_shell_append(GTK_MENU_SHELL(interfaces_menu), if_item);
g_signal_connect (G_OBJECT(if_item), "activate", G_CALLBACK(if_signal_select), NULL);
//set previously selected value
gtk_menu_item_set_label(GTK_MENU_ITEM(if_chosen), selected_if_name);
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)
{
trace = true;
argc--;
argv++;
printf("Tracing is on\n");
fflush(stdout);
}
gtk_init (&argc, &argv);
settings = g_settings_new("apps.indicators.netspeed");
selected_if_name = g_variant_get_string (g_settings_get_value(settings, "if-name"), NULL);
indicator_menu = gtk_menu_new();
//add interfaces menu
interfaces_menu = gtk_menu_new();
//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);
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);
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);
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), net_up_item);
//separator
sep = gtk_separator_menu_item_new();
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), sep);
//quit item
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);
//create app indicator
indicator = app_indicator_new ("netspeed", "network-idle", 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));
//set indicator position. default: all the way left
//TODO: make this optional so placement can be automatic
guint32 ordering_index = g_variant_get_int32(g_settings_get_value(settings, "ordering-index"));
app_indicator_set_ordering_index(indicator, ordering_index);
update();
/* update period in milliseconds */
g_timeout_add(1000*period, update, NULL);
gtk_main ();
return 0;
}