r4289 - in trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi: . src

rob at sita.openmoko.org rob at sita.openmoko.org
Tue Apr 1 12:06:08 CEST 2008


Author: rob
Date: 2008-04-01 12:06:07 +0200 (Tue, 01 Apr 2008)
New Revision: 4289

Modified:
   trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/ChangeLog
   trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/src/openmoko-panel-wifi.c
Log:
2008-04-01  Rob Bradford  <rob at openedhand.com>

	* src/openmoko-panel-wifi.c: (wifi_applet_is_wireless_available),
	(wifi_applet_update_visibility), (wifi_applet_weak_notify_cb),
	(wifi_applet_timeout_cb), (mb_panel_applet_create):
	Poll every 5 seconds for the state of the wifi radio. It would be
	nicer to be notified rather than needing to poll. This uses the 
	SIOCGIWTXPOW ioctl().



Modified: trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/ChangeLog
===================================================================
--- trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/ChangeLog	2008-04-01 05:35:32 UTC (rev 4288)
+++ trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/ChangeLog	2008-04-01 10:06:07 UTC (rev 4289)
@@ -1,3 +1,12 @@
+2008-04-01  Rob Bradford  <rob at openedhand.com>
+
+	* src/openmoko-panel-wifi.c: (wifi_applet_is_wireless_available),
+	(wifi_applet_update_visibility), (wifi_applet_weak_notify_cb),
+	(wifi_applet_timeout_cb), (mb_panel_applet_create):
+	Poll every 5 seconds for the state of the wifi radio. It would be
+	nicer to be notified rather than needing to poll. This uses the 
+	SIOCGIWTXPOW ioctl().
+
 2008-03-28  Rob Bradford  <rob at openedhand.com>
 
 	* Initial commit

Modified: trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/src/openmoko-panel-wifi.c
===================================================================
--- trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/src/openmoko-panel-wifi.c	2008-04-01 05:35:32 UTC (rev 4288)
+++ trunk/src/target/OM-2007.2/panel-plugins/openmoko-panel-wifi/src/openmoko-panel-wifi.c	2008-04-01 10:06:07 UTC (rev 4289)
@@ -16,36 +16,119 @@
  *
  */
 
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <math.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>		/* gethostbyname, getnetbyname */
+#include <net/ethernet.h>	/* struct ether_addr */
+#include <sys/time.h>		/* struct timeval */
+#include <unistd.h>
+
+#include <linux/if.h>
+#include <linux/wireless.h>
+
 #include <libmokopanelui2/moko-panel-applet.h>
 #include <gtk/gtk.h>
 
+#define IFACE "eth0"
+#define QUERY_FREQ 5 
+
+typedef struct 
+{
+  GtkWidget *applet;
+  int sock; /* socket for doing query on */
+  gchar *iface;
+  guint timeout_id;
+} WifiAppletData;
+
 static gboolean
-wifi_applet_is_wireless_available ()
+wifi_applet_is_wireless_available (WifiAppletData *applet)
 {
-  /* Not implemented yet */
-  return TRUE;
+  struct iwreq wrq;
+
+  /* Clear our request and set the interface name */
+  memset (&wrq, 0, sizeof (struct iwreq));
+  strncpy ((char *)&wrq.ifr_name, applet->iface, IFNAMSIZ);
+
+  /* Feel the power, uhh, do the ioctl() */
+  if (ioctl (applet->sock, SIOCGIWTXPOW, &wrq) != 0)
+  {
+    g_warning ("Error performing ioctl: %s", g_strerror (errno));
+    return FALSE;
+  }
+
+  return !wrq.u.txpower.disabled;
 }
 
 static void
-wifi_applet_update_visibility (GtkWidget *applet)
+wifi_applet_update_visibility (WifiAppletData *applet)
 {
-  if (wifi_applet_is_wireless_available ())
-    gtk_widget_show_all (applet);
+  if (wifi_applet_is_wireless_available (applet))
+    gtk_widget_show_all (applet->applet);
   else
-    gtk_widget_hide_all (applet);
+    gtk_widget_hide_all (applet->applet);
 }
 
+/* Tidy up callback. Don't want stray timeouts. */
+static void
+wifi_applet_weak_notify_cb (gpointer data, GObject *dead_object)
+{
+  WifiAppletData *applet = (WifiAppletData *)data;
+
+  g_source_remove (applet->timeout_id);
+  close (applet->sock);
+  g_free (applet);
+}
+
+static gboolean
+wifi_applet_timeout_cb (gpointer data)
+{
+  wifi_applet_update_visibility ((WifiAppletData *)data);
+
+  return TRUE;
+}
+
 G_MODULE_EXPORT GtkWidget * 
 mb_panel_applet_create (const gchar *id, GtkOrientation orientation)
 {
-    GtkWidget *applet;
+  WifiAppletData *applet;
 
-    applet = moko_panel_applet_new ();
+  applet = g_new0 (WifiAppletData, 1);
 
-    moko_panel_applet_set_icon (MOKO_PANEL_APPLET (applet),
-        PKGDATADIR "/" "Wifi.png");
+  /* Open socket to perform ioctl() on */
+  applet->sock = socket (AF_INET, SOCK_DGRAM, 0);
 
-    wifi_applet_update_visibility (applet);
+  if (!applet->sock)
+  {
+    g_warning ("Unable to open socket: %s", g_strerror (errno));
+    return FALSE;
+  }
 
-    return applet;
+  /* Our interface name */
+  applet->iface = IFACE;
+
+  applet->applet = moko_panel_applet_new ();
+
+  /* 
+   * Weak reference so we can find out when the applet is destroyed to get
+   * rid of the timeout and tidy up
+   */
+  g_object_weak_ref ((GObject *)applet->applet, wifi_applet_weak_notify_cb, applet);
+
+  moko_panel_applet_set_icon (MOKO_PANEL_APPLET (applet->applet),
+      PKGDATADIR "/" "Wifi.png");
+
+  wifi_applet_update_visibility (applet);
+
+  applet->timeout_id = g_timeout_add_seconds (QUERY_FREQ, wifi_applet_timeout_cb, 
+      applet);
+
+  return applet->applet;
 }





More information about the commitlog mailing list