#include "menu.h"
DBusGProxy *dbus = NULL;
-GList *interfaces = NULL;
+GSList *interfaces = NULL;
static GtkStatusIcon *status_icon;
-static gint ani_timer;
+static int ani_timer;
static int ani_counter;
-static gboolean online;
-static gboolean carrier;
+static bool online;
+static bool carrier;
static char **interface_order;
static NotifyNotification *nn;
"REBOOT",
"IPV4LL",
"INFORM",
+ "STATIC",
"TIMEOUT",
NULL
};
NULL
};
-static gboolean
+static bool
ignore_if_msg(const struct if_msg *ifm)
{
if (g_strcmp0(ifm->reason, "STOP") == 0 ||
g_strcmp0(ifm->reason, "RELEASE") == 0)
- return TRUE;
- return FALSE;
+ return true;
+ return false;
}
static struct if_msg *
find_if_msg(const char *iface)
{
- GList *gl;
+ GSList *gl;
struct if_msg *ifm;
for (gl = interfaces; gl; gl = gl->next) {
ifm = (struct if_msg *)gl->data;
- if (g_strcmp0(ifm->name, iface) == 0)
+ if (g_strcmp0(ifm->ifname, iface) == 0)
return ifm;
}
return NULL;
static void
free_if_ap(struct if_ap *ifa)
{
+ g_free(ifa->ifname);
g_free(ifa->bssid);
g_free(ifa->flags);
g_free(ifa->ssid);
{
GSList *gl;
- g_free(ifm->name);
+ g_free(ifm->ifname);
g_free(ifm->reason);
g_free(ifm->ssid);
for (gl = ifm->scan_results; gl; gl = gl->next)
if (error) {
g_critical("%s: %s", msg, error->message);
- dialog = gtk_message_dialog_new(NULL,
- 0,
- GTK_MESSAGE_ERROR,
- GTK_BUTTONS_CLOSE,
- "%s: %s",
- msg,
- error->message);
+ dialog = gtk_message_dialog_new(NULL, 0,
+ GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
+ "%s: %s", msg, error->message);
} else {
g_critical("%s", msg);
- dialog = gtk_message_dialog_new(NULL,
- 0,
- GTK_MESSAGE_ERROR,
- GTK_BUTTONS_CLOSE,
- "%s",
- msg);
+ dialog = gtk_message_dialog_new(NULL, 0,
+ GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", msg);
}
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
otype = dbus_g_type_get_collection("GPtrArray", otype);
- if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
- G_TYPE_STRING, ifm->name, G_TYPE_INVALID,
- otype, &array, G_TYPE_INVALID))
- error_exit(_("GetScanResults"), error);
+ error = NULL;
+ if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
+ G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
+ otype, &array, G_TYPE_INVALID))
+ error_exit(_("ScanResults"), error);
for (i = 0; i < array->len; i++) {
config = g_ptr_array_index(array, i);
if (val == NULL)
continue;
ifa = g_malloc0(sizeof(*ifa));
+ ifa->ifname = g_strdup(ifm->ifname);
ifa->bssid = g_strdup(g_value_get_string(val));
val = g_hash_table_lookup(config, "Frequency");
if (val)
if (val == NULL)
return NULL;
ifm = g_malloc0(sizeof(*ifm));
- ifm->name = g_strdup(g_value_get_string(val));
+ ifm->ifname = g_strdup(g_value_get_string(val));
val = g_hash_table_lookup(config, "Reason");
if (val)
ifm->reason = g_strdup(g_value_get_string(val));
return ifm;
}
-static gboolean
+static bool
if_up(const struct if_msg *ifm)
{
const char *const *r;
for (r = up_reasons; *r; r++)
if (g_strcmp0(*r, ifm->reason) == 0)
- return TRUE;
- return FALSE;
+ return true;
+ return false;
}
static char *
char *msg, *p;
const char *reason = NULL;
size_t len;
- gboolean showip, showssid;
+ bool showip, showssid;
- showip = TRUE;
- showssid = FALSE;
+ showip = true;
+ showssid = false;
if (if_up(ifm))
reason = N_("Acquired address");
else {
if (ifm->wireless) {
reason = N_("Asssociated with");
if (ifm->ssid != NULL)
- showssid = TRUE;
+ showssid = true;
} else
reason = N_("Cable plugged in");
- showip = FALSE;
+ showip = false;
} else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
if (ifm->wireless) {
if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
reason = N_("Disassociated from");
- showssid = TRUE;
+ showssid = true;
} else
- reason = N_("Not associated");
+ reason = N_("Not associated");
} else
reason = N_("Cable unplugged");
- showip = FALSE;
+ showip = false;
}
}
if (reason == NULL)
reason = ifm->reason;
- len = strlen(ifm->name) + 3;
+ len = strlen(ifm->ifname) + 3;
len += strlen(reason) + 1;
if (ifm->ip.s_addr != 0) {
len += 16; /* 000. * 4 */
if (showssid)
len += strlen(ifm->ssid) + 1;
msg = p = g_malloc(len);
- p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
+ p += g_snprintf(msg, len, "%s: %s", ifm->ifname, reason);
if (showssid)
p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
if (ifm->ip.s_addr != 0 && showip) {
return msg;
}
-static gint
+static int
if_msg_comparer(gconstpointer a, gconstpointer b)
{
const struct if_msg *ifa, *ifb;
ifa = (const struct if_msg *)a;
ifb = (const struct if_msg *)b;
for (order = (const char *const *)interface_order; *order; order++) {
- if (g_strcmp0(*order, ifa->name) == 0)
+ if (g_strcmp0(*order, ifa->ifname) == 0)
return -1;
- if (g_strcmp0(*order, ifb->name) == 0)
+ if (g_strcmp0(*order, ifb->ifname) == 0)
return 1;
}
return 0;
static gboolean
animate_carrier(_unused gpointer data)
{
+ const char *icon;
+
if (ani_timer == 0)
- return FALSE;
+ return false;
switch(ani_counter++) {
case 0:
- gtk_status_icon_set_from_icon_name(status_icon, "network-transmit");
+ icon = "network-transmit";
break;
case 1:
- gtk_status_icon_set_from_icon_name(status_icon, "network-receive");
+ icon = "network-receive";
break;
default:
- gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
+ icon = "network-idle";
ani_counter = 0;
break;
}
- return TRUE;
+ gtk_status_icon_set_from_icon_name(status_icon, icon);
+ return true;
}
static gboolean
animate_online(_unused gpointer data)
{
+ const char *icon;
+
if (ani_timer == 0)
- return FALSE;
+ return false;
if (ani_counter++ > 6) {
ani_timer = 0;
ani_counter = 0;
- return FALSE;
+ return false;
}
if (ani_counter % 2 == 0)
- gtk_status_icon_set_from_icon_name(status_icon, "network-idle");
+ icon = "network-idle";
else
- gtk_status_icon_set_from_icon_name(status_icon, "network-transmit-receive");
- return TRUE;
+ icon = "network-transmit-receive";
+ gtk_status_icon_set_from_icon_name(status_icon, icon);
+ return true;
}
static void
-update_online(char **buffer)
+update_online(void)
{
- gboolean ison, iscarrier;
+ bool ison, iscarrier;
char *msg, *msgs, *tmp;
- const GList *gl;
+ const GSList *gl;
const struct if_msg *ifm;
- ison = iscarrier = FALSE;
+ ison = iscarrier = false;
msgs = NULL;
for (gl = interfaces; gl; gl = gl->next) {
ifm = (const struct if_msg *)gl->data;
if (if_up(ifm))
- ison = iscarrier = TRUE;
+ ison = iscarrier = true;
if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
- iscarrier = TRUE;
+ iscarrier = true;
msg = print_if_msg(ifm);
if (msgs) {
tmp = g_strconcat(msgs, "\n", msg, NULL);
ani_timer = g_timeout_add(500, animate_carrier, NULL);
} else {
gtk_status_icon_set_from_icon_name(status_icon,
- "network-offline");
+ "network-offline");
}
}
gtk_status_icon_set_tooltip(status_icon, msgs);
- if (buffer)
- *buffer = msgs;
- else
- g_free(msgs);
+ g_free(msgs);
}
void
notify_notification_close(nn, NULL);
if (gtk_status_icon_get_visible(status_icon))
nn = notify_notification_new_with_status_icon(title,
- msg,
- icon,
- status_icon);
+ msg, icon, status_icon);
else
nn = notify_notification_new(title, msg, icon, NULL);
notify_notification_set_timeout(nn, 5000);
dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
{
struct if_msg *ifm, *ifp;
- gboolean rem;
- GList *gl;
+ bool rem;
+ GSList *gl;
char *msg, *title;
const char *act, *net;
const char *const *r;
ifp = NULL;
for (gl = interfaces; gl; gl = gl->next) {
ifp = (struct if_msg *)gl->data;
- if (g_strcmp0(ifp->name, ifm->name) == 0) {
+ if (g_strcmp0(ifp->ifname, ifm->ifname) == 0) {
ifm->scan_results = ifp->scan_results;
ifp->scan_results = NULL;
free_if_msg(ifp);
if (rem)
- interfaces = g_list_delete_link(interfaces, gl);
+ interfaces =
+ g_slist_delete_link(interfaces, gl);
else
gl->data = ifm;
break;
}
}
if (ifp == NULL && !rem)
- interfaces = g_list_prepend(interfaces, ifm);
- interfaces = g_list_sort(interfaces, if_msg_comparer);
- update_online(NULL);
+ interfaces = g_slist_prepend(interfaces, ifm);
+ interfaces = g_slist_sort(interfaces, if_msg_comparer);
+ update_online();
/* We should ignore renew and stop so we don't annoy the user */
if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
if (ignore_if_msg(ifm))
g_free(ifm);
else if (ifm)
- interfaces = g_list_prepend(interfaces, ifm);
+ interfaces = g_slist_prepend(interfaces, ifm);
}
static void
GHashTable *ifs;
GError *error = NULL;
GType otype;
- char *msg;
- GList *gl;
- GSList *gsl;
+ GSList *gl, *gsl;
GPtrArray *array;
struct if_msg *ifm;
otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
- G_TYPE_INVALID,
- otype, &ifs, G_TYPE_INVALID))
+ G_TYPE_INVALID,
+ otype, &ifs, G_TYPE_INVALID))
error_exit("GetInterfaces", error);
g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
g_hash_table_unref(ifs);
g_strfreev(interface_order);
interface_order = NULL;
if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
- G_TYPE_INVALID,
- G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
+ G_TYPE_INVALID,
+ G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
error_exit("ListInterfaces", error);
- interfaces = g_list_sort(interfaces, if_msg_comparer);
+ interfaces = g_slist_sort(interfaces, if_msg_comparer);
otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
otype = dbus_g_type_get_collection("GPtrArray", otype);
ifm = (struct if_msg *)gl->data;
if (!ifm->wireless)
continue;
- if (!dbus_g_proxy_call(dbus, "GetScanResults", &error,
- G_TYPE_STRING, ifm->name, G_TYPE_INVALID,
- otype, &array, G_TYPE_INVALID))
+ if (!dbus_g_proxy_call(dbus, "ScanResults", &error,
+ G_TYPE_STRING, ifm->ifname, G_TYPE_INVALID,
+ otype, &array, G_TYPE_INVALID))
{
- g_message("GetScanResults: %s", error->message);
+ g_message("ScanResults: %s", error->message);
g_clear_error(&error);
continue;
}
ifm->scan_results = get_scan_results(ifm);
}
- msg = NULL;
- update_online(&msg);
- // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
- if (msg != NULL) {
- notify("Interface status", msg, GTK_STOCK_NETWORK);
- g_free(msg);
- }
+ update_online();
}
static void
check_status(const char *status)
{
static char *last = NULL;
- GList *gl;
+ GSList *gl;
char *version;
const char *msg;
- gboolean refresh;
+ bool refresh;
GError *error = NULL;
g_message("Status changed to %s", status);
if (g_strcmp0(status, "down") == 0) {
for (gl = interfaces; gl; gl = gl->next)
free_if_msg((struct if_msg *)gl->data);
- g_list_free(interfaces);
+ g_slist_free(interfaces);
interfaces = NULL;
- update_online(NULL);
- msg = N_(last? "Connection to dhcpcd lost" : "dhcpcd not running");
+ update_online();
+ msg = N_(last ?
+ "Connection to dhcpcd lost" : "dhcpcd not running");
gtk_status_icon_set_tooltip(status_icon, msg);
notify(_("No network"), msg, GTK_STOCK_NETWORK);
}
- refresh = FALSE;
+ refresh = false;
if (last == NULL) {
if (g_strcmp0(status, "down") != 0)
- refresh = TRUE;
+ refresh = true;
} else {
if (g_strcmp0(status, last) == 0)
return;
if (g_strcmp0(last, "down") == 0)
- refresh = TRUE;
+ refresh = true;
g_free(last);
}
last = g_strdup(status);
if (!refresh)
return;
if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
- G_TYPE_INVALID,
- G_TYPE_STRING, &version, G_TYPE_INVALID))
+ G_TYPE_INVALID,
+ G_TYPE_STRING, &version, G_TYPE_INVALID))
error_exit(_("GetDhcpcdVersion"), error);
g_message(_("Connected to %s-%s"), "dhcpcd", version);
g_free(version);
}
static void
-dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
+dhcpcd_status(_unused DBusGProxy *proxy, const char *status,
+ _unused void *data)
{
check_status(status);
}
static void
-dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface, _unused void *data)
+dhcpcd_scan_results(_unused DBusGProxy *proxy, const char *iface,
+ _unused void *data)
{
struct if_msg *ifm;
- GSList *gl;
+ struct if_ap *ifa, *ifa2;
+ GSList *gl, *aps, *l;
+ char *txt, *ntxt;
ifm = find_if_msg(iface);
if (ifm == NULL)
return;
- g_message(_("%s: Received scan results"), ifm->name);
+ g_message(_("%s: Received scan results"), ifm->ifname);
+ aps = get_scan_results(ifm);
+ txt = NULL;
+ for (gl = aps; gl; gl = gl->next) {
+ ifa = (struct if_ap *)gl->data;
+ for (l = ifm->scan_results; l; l = l->next) {
+ ifa2 = (struct if_ap *)l->data;
+ if (g_strcmp0(ifa->ssid, ifa2->ssid) == 0)
+ break;
+ }
+ if (l == NULL) {
+ if (txt == NULL)
+ txt = g_strdup(ifa->ssid);
+ else {
+ ntxt = g_strconcat(txt, "\n", ifa->ssid, NULL);
+ g_free(txt);
+ txt = ntxt;
+ }
+ }
+ }
for (gl = ifm->scan_results; gl; gl = gl->next)
free_if_ap((struct if_ap *)gl->data);
g_slist_free(ifm->scan_results);
- ifm->scan_results = get_scan_results(ifm);
+ ifm->scan_results = aps;
+ if (txt != NULL) {
+ notify(N_("Found new AP"), txt, GTK_STOCK_NETWORK);
+ g_free(txt);
+ }
}
int
char *version = NULL;
GType otype;
int tries = 5;
-
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, NULL);
g_set_application_name("dhcpcd Monitor");
status_icon = gtk_status_icon_new_from_icon_name("network-offline");
if (status_icon == NULL)
- status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
+ status_icon =
+ gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
//network_offline = gtk_status_icon_get_pixbuf(status_icon);
- gtk_status_icon_set_tooltip(status_icon, _("Connecting to dhcpcd ..."));
- gtk_status_icon_set_visible(status_icon, TRUE);
+ gtk_status_icon_set_tooltip(status_icon,
+ _("Connecting to dhcpcd ..."));
+ gtk_status_icon_set_visible(status_icon, true);
notify_init(PACKAGE);
if (bus == NULL || error != NULL)
error_exit(_("Could not connect to system bus"), error);
dbus = dbus_g_proxy_new_for_name(bus,
- DHCPCD_SERVICE,
- DHCPCD_PATH,
- DHCPCD_SERVICE);
+ DHCPCD_SERVICE,
+ DHCPCD_PATH,
+ DHCPCD_SERVICE);
g_message(_("Connecting to dhcpcd-dbus ..."));
while (--tries > 0) {
g_clear_error(&error);
- if (dbus_g_proxy_call_with_timeout(dbus,
- "GetVersion",
- 500,
- &error,
- G_TYPE_INVALID,
- G_TYPE_STRING,
- &version,
- G_TYPE_INVALID))
+ if (dbus_g_proxy_call_with_timeout(dbus, "GetVersion", 500,
+ &error, G_TYPE_INVALID,
+ G_TYPE_STRING, &version, G_TYPE_INVALID))
break;
}
if (tries == 0)
g_free(version);
gtk_status_icon_set_tooltip(status_icon, _("Triggering dhcpcd ..."));
- online = FALSE;
+ online = false;
menu_init(status_icon);
if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
- G_TYPE_INVALID,
- G_TYPE_STRING, &version, G_TYPE_INVALID))
+ G_TYPE_INVALID,
+ G_TYPE_STRING, &version, G_TYPE_INVALID))
error_exit(_("GetStatus"), error);
check_status(version);
g_free(version);
otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
dbus_g_proxy_add_signal(dbus, "Event",
- otype, G_TYPE_INVALID);
+ otype, G_TYPE_INVALID);
dbus_g_proxy_connect_signal(dbus, "Event",
- G_CALLBACK(dhcpcd_event),
- bus, NULL);
+ G_CALLBACK(dhcpcd_event), bus, NULL);
dbus_g_proxy_add_signal(dbus, "StatusChanged",
- G_TYPE_STRING, G_TYPE_INVALID);
+ G_TYPE_STRING, G_TYPE_INVALID);
dbus_g_proxy_connect_signal(dbus, "StatusChanged",
- G_CALLBACK(dhcpcd_status),
- bus, NULL);
+ G_CALLBACK(dhcpcd_status), bus, NULL);
dbus_g_proxy_add_signal(dbus, "ScanResults",
- G_TYPE_STRING, G_TYPE_INVALID);
+ G_TYPE_STRING, G_TYPE_INVALID);
dbus_g_proxy_connect_signal(dbus, "ScanResults",
- G_CALLBACK(dhcpcd_scan_results),
- bus, NULL);
+ G_CALLBACK(dhcpcd_scan_results), bus, NULL);
gtk_main();
return 0;