If we have a wireless interface, use the signal strength icon in the
authorRoy Marples <roy@marples.name>
Thu, 4 Dec 2014 13:23:01 +0000 (13:23 +0000)
committerRoy Marples <roy@marples.name>
Thu, 4 Dec 2014 13:23:01 +0000 (13:23 +0000)
system tray instead of the wired connection.

src/dhcpcd-gtk/dhcpcd-gtk.h
src/dhcpcd-gtk/main.c
src/dhcpcd-gtk/menu.c
src/dhcpcd-qt/dhcpcd-qt.cpp
src/dhcpcd-qt/dhcpcd-qt.h
src/dhcpcd-qt/dhcpcd-ssidmenuwidget.cpp

index e343dcb15a1e2798d886f1cc33734f8f36fdf3b5..2ad61f0c4a5494e4a9e37fd45a534231e0580cb0 100644 (file)
@@ -71,6 +71,7 @@ typedef TAILQ_HEAD(wi_scan_head, wi_scan) WI_SCANS;
 extern WI_SCANS wi_scans;
 
 WI_SCAN * wi_scan_find(DHCPCD_WI_SCAN *);
+const char *get_strength_icon_name(int strength);
 
 void menu_init(GtkStatusIcon *, DHCPCD_CONNECTION *);
 void menu_update_scans(WI_SCAN *, DHCPCD_WI_SCAN *);
index 35fe4b26bc00107011405140dbc0ab045206d259..1081dafa238f8e3c777f3a5d628ba8004ef69506 100644 (file)
@@ -75,25 +75,82 @@ wi_scan_find(DHCPCD_WI_SCAN *scan)
        return NULL;
 }
 
+const char *
+get_strength_icon_name(int strength)
+{
+
+       if (strength > 80)
+               return "network-wireless-connected-100";
+       else if (strength > 55)
+               return "network-wireless-connected-75";
+       else if (strength > 30)
+               return "network-wireless-connected-50";
+       else if (strength > 5)
+               return "network-wireless-connected-25";
+       else
+               return "network-wireless-connected-00";
+}
+
+static DHCPCD_WI_SCAN *
+get_strongest_scan()
+{
+       WI_SCAN *w;
+       DHCPCD_WI_SCAN *scan, *s;
+
+       scan = NULL;
+       TAILQ_FOREACH(w, &wi_scans, next) {
+               for (s = w->scans; s; s = s->next) {
+                       if (dhcpcd_wi_associated(w->interface, s) &&
+                           (scan == NULL ||
+                           s->strength.value > scan->strength.value))
+                               scan = s;
+               }
+       }
+       return scan;
+}
+
 static gboolean
 animate_carrier(_unused gpointer data)
 {
        const char *icon;
+       DHCPCD_WI_SCAN *scan;
 
        if (ani_timer == 0)
                return false;
 
-       switch(ani_counter++) {
-       case 0:
-               icon = "network-transmit";
-               break;
-       case 1:
-               icon = "network-receive";
-               break;
-       default:
-               icon = "network-idle";
-               ani_counter = 0;
-               break;
+       scan = get_strongest_scan();
+       if (scan) {
+               switch(ani_counter++) {
+               case 0:
+                       icon = "network-wireless-connected-00";
+                       break;
+               case 1:
+                       icon = "network-wireless-connected-25";
+                       break;
+               case 2:
+                       icon = "network-wireless-connected-50";
+                       break;
+               case 3:
+                       icon = "network-wireless-connected-75";
+                       break;
+               default:
+                       icon = "network-wireless-connected-100";
+                       ani_counter = 0;
+               }
+
+       } else {
+               switch(ani_counter++) {
+               case 0:
+                       icon = "network-transmit";
+                       break;
+               case 1:
+                       icon = "network-receive";
+                       break;
+               default:
+                       icon = "network-idle";
+                       ani_counter = 0;
+                       break;
+               }
        }
        gtk_status_icon_set_from_icon_name(status_icon, icon);
        return true;
@@ -103,6 +160,7 @@ static gboolean
 animate_online(_unused gpointer data)
 {
        const char *icon;
+       DHCPCD_WI_SCAN *scan;
 
        if (ani_timer == 0)
                return false;
@@ -113,10 +171,13 @@ animate_online(_unused gpointer data)
                return false;
        }
 
+       scan = get_strongest_scan();
        if (ani_counter % 2 == 0)
-               icon = "network-idle";
+               icon = scan ? "network-wireless-connected-00" :
+                   "network-idle";
        else
-               icon = "network-transmit-receive";
+               icon = scan ? get_strength_icon_name(scan->strength.value) :
+                   "network-transmit-receive";
        gtk_status_icon_set_from_icon_name(status_icon, icon);
        return true;
 }
index e13353685ff1a81203eb2f9948e118a8b33366f0..8f4a9cda0a21409c251fd9893dd926d5adc22694 100644 (file)
@@ -95,22 +95,6 @@ on_about(_unused GtkMenuItem *item)
        gtk_widget_hide(GTK_WIDGET(about));
 }
 
-static const char *
-get_strength_icon_name(int strength)
-{
-
-       if (strength > 80)
-               return "network-wireless-connected-100";
-       else if (strength > 55)
-               return "network-wireless-connected-75";
-       else if (strength > 30)
-               return "network-wireless-connected-50";
-       else if (strength > 5)
-               return "network-wireless-connected-25";
-       else
-               return "network-wireless-connected-00";
-}
-
 static bool
 is_associated(WI_SCAN *wi, DHCPCD_WI_SCAN *scan)
 {
index 1b612e82361a5c5c47a7224f3e748918a26d7808..92c714576e6ee9ab30980f7af626a3b8c58019e9 100644 (file)
@@ -110,9 +110,48 @@ QList<DhcpcdWi *> *DhcpcdQt::getWis()
        return wis;
 }
 
+const char * DhcpcdQt::signalStrengthIcon(DHCPCD_WI_SCAN *scan)
+{
+
+       if (scan->strength.value > 80)
+               return "network-wireless-connected-100";
+       else if (scan->strength.value > 55)
+               return "network-wireless-connected-75";
+       else if (scan->strength.value > 30)
+               return "network-wireless-connected-50";
+       else if (scan->strength.value > 5)
+               return "network-wireless-connected-25";
+       else
+               return "network-wireless-connected-00";
+}
+
+DHCPCD_WI_SCAN * DhcpcdQt::getStrongestSignal()
+{
+       DHCPCD_WI_SCAN *scan, *scans, *s;
+       DHCPCD_WPA *wpa;
+       DHCPCD_IF *i;
+
+       scan = NULL;
+       for (auto &wi : *wis) {
+               wpa = wi->getWpa();
+               i = dhcpcd_wpa_if(wpa);
+               scans = wi->getScans();
+               for (s = scans; s; s = s->next) {
+                       if (dhcpcd_wi_associated(i, s) &&
+                           (scan == NULL ||
+                           s->strength.value > scan->strength.value))
+                               scan = s;
+               }
+       }
+       return scan;
+}
+
 void DhcpcdQt::animate()
 {
        const char *icon;
+       DHCPCD_WI_SCAN *scan;
+
+       scan = getStrongestSignal();
 
        if (onLine) {
                if (aniCounter++ > 6) {
@@ -122,20 +161,42 @@ void DhcpcdQt::animate()
                }
 
                if (aniCounter % 2 == 0)
-                       icon = "network-idle";
+                       icon = scan ? "network-wireless-connected-00" :
+                           "network-idle";
                else
-                       icon = "network-transmit-receive";
+                       icon = scan ? DhcpcdQt::signalStrengthIcon(scan) :
+                           "network-transmit-receive";
        } else {
-               switch(aniCounter++) {
-               case 0:
-                       icon = "network-transmit";
-                       break;
-               case 1:
-                       icon = "network-receive";
-                       break;
-               default:
-                       icon = "network-idle";
-                       aniCounter = 0;
+               if (scan) {
+                       switch(aniCounter++) {
+                       case 0:
+                               icon = "network-wireless-connected-00";
+                               break;
+                       case 1:
+                               icon = "network-wireless-connected-25";
+                               break;
+                       case 2:
+                               icon = "network-wireless-connected-50";
+                               break;
+                       case 3:
+                               icon = "network-wireless-connected-75";
+                               break;
+                       default:
+                               icon = "network-wireless-connected-100";
+                               aniCounter = 0;
+                       }
+               } else {
+                       switch(aniCounter++) {
+                       case 0:
+                               icon = "network-transmit";
+                               break;
+                       case 1:
+                               icon = "network-receive";
+                               break;
+                       default:
+                               icon = "network-idle";
+                               aniCounter = 0;
+                       }
                }
        }
 
@@ -369,6 +430,13 @@ void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
        } else
                processScans(wi, scans);
 
+       if (!aniTimer->isActive()) {
+               DHCPCD_WI_SCAN *scan;
+
+               scan = getStrongestSignal();
+               if (scan)
+                       setIcon("status", DhcpcdQt::signalStrengthIcon(scan));
+       }
 }
 
 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
index 1bd66fc8efb9e3944a465bfa03692d6a9d0ce8bb..a0d1586176f3027741e55ff937e7183e66a01b9f 100644 (file)
@@ -69,6 +69,7 @@ public:
            void *d);
        void wpaStatusCallback(DHCPCD_WPA *wpa, const char *status);
 
+       static const char * signalStrengthIcon(DHCPCD_WI_SCAN *scan);
        static QIcon getIcon(QString category, QString name);
        QList<DhcpcdWi *> *getWis();
 
@@ -104,6 +105,7 @@ private:
        QDialog *about;
        QDialog *preferences;
 
+       DHCPCD_WI_SCAN *getStrongestSignal();
        void processScans(DhcpcdWi *wi, DHCPCD_WI_SCAN *scans);
        void addSsidMenu(QMenu *&menu, DHCPCD_IF *ifp, DhcpcdWi *&wi);
        void createSsidMenu();
index 0c3a39aeeb0185de1e84f5dbe52e80c9f6a63053..5101f7ed8a44750a5fb867e3ed6e2f80b2c73421 100644 (file)
@@ -129,21 +129,8 @@ void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
                picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
        encicon->setPixmap(picon);
 
-       if (scan->strength.value > 80)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-100");
-       else if (scan->strength.value > 55)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-75");
-       else if (scan->strength.value > 30)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-50");
-       else if (scan->strength.value > 5)
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-25");
-       else
-               icon = DhcpcdQt::getIcon("status",
-                   "network-wireless-connected-00");
+       icon = DhcpcdQt::getIcon("status",
+           DhcpcdQt::signalStrengthIcon(scan));
        picon = icon.pixmap(ICON_SIZE);
        if (picon.height() != ICON_SIZE)
                picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);