Skip to content

Commit 6a913ee

Browse files
committed
make label show which interface is shown; priority to eth0, then wireless, then ipv6
1 parent 5626292 commit 6a913ee

1 file changed

Lines changed: 136 additions & 159 deletions

File tree

showip.c

Lines changed: 136 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ If both are unconnected, will display blank.
1010
1111
$> make
1212
13-
or
13+
or something like:
1414
1515
$> gcc -Wall `pkg-config --cflags gtk+-2.0 lxpanel` -shared -fPIC showip.c toolbox.c toolbox-char-array.c toolbox-line-parser.c toolbox-text-buffer-reader.c -o showip.so `pkg-config --libs lxpanel`
1616
@@ -52,195 +52,172 @@ typedef struct
5252

5353
static void update_display(ShowIp *pPlugin)
5454
{
55-
GdkColor color;
56-
//gchar *separator;
57-
58-
char ip[256]; /*
59-
char mac[256];
60-
char subnet[256];
61-
char broadcast[256];
62-
char RX_packets[256];
63-
char RX_errors[256];
64-
char RX_bytes[256];
65-
char TX_packets[256];
66-
char TX_errors[256];
67-
char TX_bytes[256];
68-
char collisions[256];
69-
char ifconfig[20000] = "";
70-
printf("=eth0:");*/
71-
//C_GetNetworkInformation(ip, subnet, broadcast, mac, RX_packets, RX_errors, RX_bytes, TX_packets, TX_errors, TX_bytes, collisions, ifconfig, sizeof(ifconfig), NULL);
72-
73-
struct ifaddrs * ifAddrStruct=NULL;
74-
struct ifaddrs * ifa=NULL;
75-
void * tmpAddrPtr=NULL;
76-
77-
getifaddrs(&ifAddrStruct);
78-
79-
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
80-
{
81-
if (!ifa->ifa_addr)
82-
{
83-
continue;
84-
}
85-
if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4
86-
{
87-
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
88-
char addressBuffer[INET_ADDRSTRLEN];
89-
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
90-
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
91-
if (strcmp(ifa->ifa_name, "lo") != 0)
92-
{
93-
strncpy(ip, addressBuffer, sizeof(ip)-1);
94-
}
95-
}
96-
}
55+
GdkColor color;
56+
57+
char ip[256];
58+
char which_interface[256] = "";
59+
struct ifaddrs * ifAddrStruct=NULL;
60+
struct ifaddrs * ifa=NULL;
61+
void * tmpAddrPtr=NULL;
62+
63+
getifaddrs(&ifAddrStruct);
64+
65+
// get eth0 first
66+
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
67+
{
68+
if (!ifa->ifa_addr)
69+
{
70+
continue;
71+
}
72+
73+
if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4
74+
{
75+
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
76+
char addressBuffer[INET_ADDRSTRLEN];
77+
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
78+
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
79+
if (strcmp(ifa->ifa_name, "lo") != 0)
80+
{
81+
strncpy(ip, addressBuffer, sizeof(ip)-1);
82+
strcpy(which_interface, ifa->ifa_name);
83+
}
84+
85+
if (strcmp(ifa->ifa_name, "eth0") == 0)
86+
{
87+
break;
88+
}
89+
}
90+
}
91+
92+
// get eth0 first
93+
if (ip[0] == '\0') for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
94+
{
95+
if (!ifa->ifa_addr)
96+
{
97+
continue;
98+
}
99+
if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4
100+
{
101+
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
102+
char addressBuffer[INET_ADDRSTRLEN];
103+
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
104+
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
105+
if (ifa->ifa_name[0] == 'w')
106+
{
107+
strncpy(ip, addressBuffer, sizeof(ip)-1); // get wireless lans
108+
strcpy(which_interface, ifa->ifa_name);
109+
}
110+
}
111+
}
97112

98-
// loop ipv6 if we couldnt find ipv4 eth0
99-
if (ip[0] == '\0') for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
100-
{
101-
if (!ifa->ifa_addr)
102-
{
103-
continue;
104-
}
105-
106-
if (ifa->ifa_addr->sa_family == AF_INET6)
107-
{
108-
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
109-
char addressBuffer[INET6_ADDRSTRLEN];
110-
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
111-
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
112-
if (strcmp(ifa->ifa_name, "lo") != 0)
113-
{
114-
strncpy(ip, addressBuffer, sizeof(ip)-1);
115-
}
116-
}
117-
}
113+
// loop ipv6 if we couldnt find ipv4 eth0
114+
if (ip[0] == '\0') for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
115+
{
116+
if (!ifa->ifa_addr)
117+
{
118+
continue;
119+
}
120+
121+
if (ifa->ifa_addr->sa_family == AF_INET6)
122+
{
123+
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
124+
char addressBuffer[INET6_ADDRSTRLEN];
125+
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
126+
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
127+
if (strcmp(ifa->ifa_name, "lo") != 0)
128+
{
129+
strncpy(ip, addressBuffer, sizeof(ip)-1);
130+
strcpy(which_interface, ifa->ifa_name);
131+
break;
132+
}
133+
}
134+
}
118135

119-
if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
136+
if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
120137

121-
printf("ip(%s)\n",ip);/*
122-
printf("subnet(%s)\n",subnet);
123-
printf("broadcast(%s)\n",broadcast);
124-
printf("mac(%s)\n",mac);
125-
printf("RX_packets(%s)\n",RX_packets);
126-
printf("RX_errors(%s)\n",RX_errors);
127-
printf("RX_bytes(%s)\n",RX_bytes);
128-
printf("TX_packets(%s)\n",TX_packets);
129-
printf("TX_errors(%s)\n",TX_errors);
130-
printf("TX_bytes(%s)\n",TX_bytes);
131-
printf("collisions(%s)\n",collisions);*/
132-
/*
133-
if (ip[0] == '\0')
134-
{
135-
printf("=wlan0:");
136-
iret = C_GetNetworkInformation(ip, subnet, broadcast, mac, RX_packets, RX_errors, RX_bytes, TX_packets, TX_errors, TX_bytes, collisions, ifconfig, sizeof(ifconfig), "wlan0");
137-
if (iret <= 0)
138-
{
139-
printf("no wlan0, iret(%d)\n", iret);
140-
}
141-
else
142-
{
143-
printf("ip(%s)\n",ip);
144-
printf("subnet(%s)\n",subnet);
145-
printf("broadcast(%s)\n",broadcast);
146-
printf("mac(%s)\n",mac);
147-
printf("RX_packets(%s)\n",RX_packets);
148-
printf("RX_errors(%s)\n",RX_errors);
149-
printf("RX_bytes(%s)\n",RX_bytes);
150-
printf("TX_packets(%s)\n",TX_packets);
151-
printf("TX_errors(%s)\n",TX_errors);
152-
printf("TX_bytes(%s)\n",TX_bytes);
153-
printf("collisions(%s)\n",collisions);
154-
}
155-
}
156-
*/
157-
if (ip[0] == '\0')
158-
{
159-
strcpy(ip, "No IP found");
160-
}
161-
//lxpanel_draw_label_text(pPlugin->panel, pPlugin->gLabel, ip, TRUE, 2, TRUE);
162-
163-
gdk_color_parse("#FF80FF", &color);
164-
color.pixel = 1;
165-
color.green = 255;
166-
color.blue = 155;
167-
color.red = 100;
168-
lxpanel_draw_label_text_with_color(pPlugin->panel, pPlugin->gLabel, ip, TRUE, 1.3, &color);
169-
/*
170-
snprintf(buffer, sizeof(buffer), "<span color=\"#%06x\"><b>%02d</b></span>",
171-
gcolor2rgb24(&color), temp);
172-
gtk_label_set_markup (GTK_LABEL(th->namew), buffer) ;
173-
*/
174-
gtk_widget_set_tooltip_text(pPlugin->gLabel, "tooltip"); // FIXME write which interface
138+
printf("ip(%s)\n",ip);
139+
140+
if (ip[0] == '\0')
141+
{
142+
strcpy(ip, "No IP found");
143+
}
144+
145+
//lxpanel_draw_label_text(pPlugin->panel, pPlugin->gLabel, ip, TRUE, 2, TRUE);
146+
gdk_color_parse("#FF80FF", &color);
147+
color.pixel = 1;
148+
color.green = 255;
149+
color.blue = 155;
150+
color.red = 100;
151+
lxpanel_draw_label_text_with_color(pPlugin->panel, pPlugin->gLabel, ip, TRUE, 1.3, &color);
152+
gtk_widget_set_tooltip_text(pPlugin->gLabel, which_interface);
175153
}
176154

177155
static gboolean update_display_timeout(gpointer user_data)
178156
{
179-
if (g_source_is_destroyed(g_main_current_source()))
180-
return FALSE;
181-
update_display(user_data);
182-
return TRUE; /* repeat later */
157+
if (g_source_is_destroyed(g_main_current_source()))
158+
return FALSE;
159+
update_display(user_data);
160+
return TRUE; /* repeat later */
183161
}
184162

185163
GtkWidget *showip_constructor(LXPanel *panel, config_setting_t *settings)
186164
{
187-
/* panel is a pointer to the panel and
188-
settings is a pointer to the configuration data. */
189-
(void)settings;
165+
/* panel is a pointer to the panel and
166+
settings is a pointer to the configuration data. */
167+
(void)settings;
190168

191-
// allocate our private structure instance
192-
ShowIp *pPlugin = g_new0(ShowIp, 1);
169+
// allocate our private structure instance
170+
ShowIp *pPlugin = g_new0(ShowIp, 1);
193171

194-
// update the instance count
195-
pPlugin->iMyId = ++iInstanceCount;
196-
pPlugin->panel = panel;
172+
// update the instance count
173+
pPlugin->iMyId = ++iInstanceCount;
174+
pPlugin->panel = panel;
197175

198-
// make a label out of the ID
199-
char cIdBuf[20];
200-
cIdBuf[sizeof(cIdBuf)-1] = '\0';
201-
snprintf(cIdBuf, sizeof(cIdBuf)-1, "IP %s", "127.0.0.1");
176+
// make a label out of the ID
177+
char cIdBuf[20];
178+
cIdBuf[sizeof(cIdBuf)-1] = '\0';
179+
snprintf(cIdBuf, sizeof(cIdBuf)-1, "IP %s", "127.0.0.1");
202180

203-
// create a label widget instance
204-
pPlugin->gLabel = gtk_label_new(cIdBuf);
181+
// create a label widget instance
182+
pPlugin->gLabel = gtk_label_new(cIdBuf);
205183

206-
update_display(pPlugin);
184+
update_display(pPlugin);
207185

208-
// set the label to be visible
209-
gtk_widget_show(pPlugin->gLabel);
186+
// set the label to be visible
187+
gtk_widget_show(pPlugin->gLabel);
210188

211-
// need to create a container to be able to set a border
212-
GtkWidget *p = gtk_event_box_new();
189+
// need to create a container to be able to set a border
190+
GtkWidget *p = gtk_event_box_new();
213191

214-
// our widget doesn't have a window...
215-
// it is usually illegal to call gtk_widget_set_has_window() from application but for GtkEventBox it doesn't hurt
216-
gtk_widget_set_has_window(p, FALSE);
192+
// our widget doesn't have a window...
193+
// it is usually illegal to call gtk_widget_set_has_window() from application but for GtkEventBox it doesn't hurt
194+
gtk_widget_set_has_window(p, FALSE);
217195

218-
// bind private structure to the widget assuming it should be freed using g_free()
219-
lxpanel_plugin_set_data(p, pPlugin, g_free);
196+
// bind private structure to the widget assuming it should be freed using g_free()
197+
lxpanel_plugin_set_data(p, pPlugin, g_free);
220198

221-
// set border width
222-
gtk_container_set_border_width(GTK_CONTAINER(p), 1);
199+
// set border width
200+
gtk_container_set_border_width(GTK_CONTAINER(p), 1);
223201

224-
// add the label to the container
225-
gtk_container_add(GTK_CONTAINER(p), pPlugin->gLabel);
202+
// add the label to the container
203+
gtk_container_add(GTK_CONTAINER(p), pPlugin->gLabel);
226204

227-
// set the size we want
228-
gtk_widget_set_size_request(p, 140, 25);
205+
// set the size we want
206+
gtk_widget_set_size_request(p, 140, 25);
229207

230-
pPlugin->timer = g_timeout_add_seconds(4, (GSourceFunc) update_display_timeout, (gpointer)pPlugin);
208+
pPlugin->timer = g_timeout_add_seconds(4, (GSourceFunc) update_display_timeout, (gpointer)pPlugin);
231209

232-
// success!!!
233-
return p;
210+
return p;
234211
}
235212

236213
FM_DEFINE_MODULE(lxpanel_gtk, showip)
237214

238215
/* Plugin descriptor. */
239216
LXPanelPluginInit fm_module_init_lxpanel_gtk = {
240-
.name = "ShowIP",
241-
.description = "Display current IP address.",
217+
.name = "ShowIP",
218+
.description = "Display current IP address.",
242219

243-
// assigning our functions to provided pointers.
244-
.new_instance = showip_constructor
220+
// assigning our functions to provided pointers.
221+
.new_instance = showip_constructor
245222
};
246223

0 commit comments

Comments
 (0)