From a063cda5cff1e3ec56dabddd8ddcd42060a57fd4 Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Tue, 3 Aug 2010 18:31:31 +0200 Subject: [PATCH] Split up functionality of plugin in separate modules Prepared network device class --- Makefile | 2 +- hal.c | 164 ++++++++++++++++++++++++++++++++++ hal.h | 34 +++++++ net.c | 51 +++++++++++ net.h | 53 +++++++++++ plugin.c | 163 ++++++++++++++++++++++++++++++++++ plugin.h | 55 ++++++++++++ status.c | 297 -------------------------------------------------------------- status.h | 53 ----------- util.c | 72 +++++++++++++++ util.h | 30 +++++++ 11 files changed, 623 insertions(+), 351 deletions(-) create mode 100644 hal.c create mode 100644 hal.h create mode 100644 net.c create mode 100644 net.h create mode 100644 plugin.c create mode 100644 plugin.h delete mode 100644 status.c delete mode 100644 status.h create mode 100644 util.c create mode 100644 util.h diff --git a/Makefile b/Makefile index d66ae7e..a3082f3 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ all: mtetherd mtetherd-plugin.so mtetherd: mtetherd.o device.o $(CC) $(LDFLAGS) $(LIBS_DBUS) -o $@ $^ -mtetherd-plugin.so: status.o +mtetherd-plugin.so: plugin.o hal.o net.o util.o $(CC) $(LDFLAGS) $(LIBS_HILDON) -shared -o $@ $^ %PHONY: clean install uninstall diff --git a/hal.c b/hal.c new file mode 100644 index 0000000..b2edb1b --- /dev/null +++ b/hal.c @@ -0,0 +1,164 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "hal.h" + +static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc"; +static const char *USBNET_MODULE = "g_ether"; + +static void mtetherd_hal_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) { + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx)); + + if (plugin) { + g_message("Got HAL condition %s on %s: %s", condition, udi, detail); + //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail); + if (strcmp("ButtonPressed", condition) == 0) { + if (strcmp(USBDEV_PATH, udi) == 0) { + mtetherd_status_plugin_usb_plugged_show(plugin); + } + } + } +} + +static void mtetherd_hal_device_added(LibHalContext *ctx, const char *udi) { + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx)); + + if (plugin) { + g_message("Got HAL device added on %s", udi); + if (strcmp(USBDEV_PATH, udi) == 0) { + plugin->priv->net_on = TRUE; + enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); + } + } +} + +static void mtetherd_hal_device_removed(LibHalContext *ctx, const char *udi) { + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx)); + + if (plugin) { + g_message("Got HAL device added on %s", udi); + if (strcmp(USBDEV_PATH, udi) == 0) { + plugin->priv->net_on = FALSE; + enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); + } + } +} + +gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin) { + if (plugin) { + plugin->devices = NULL; + + GError *err = NULL; + plugin->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err); + if (err) { + g_warning("Can't open DBUS connection: %s", err->message); + g_error_free(err); + err = NULL; + return FALSE; + } else { + g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection); + } + if (plugin->dbus_connection) { + plugin->hal_context = libhal_ctx_new(); + if (plugin->hal_context) { + if (libhal_ctx_set_dbus_connection(plugin->hal_context, dbus_g_connection_get_connection(plugin->dbus_connection))) { + if (!libhal_ctx_set_user_data(plugin->hal_context, plugin)) { + g_warning("Can't set user data of HAL context"); + } + if (!libhal_ctx_set_device_condition(plugin->hal_context, mtetherd_hal_device_condition)) { + g_warning("Error assigning device condition callback"); + } + if (!libhal_ctx_set_device_added(plugin->hal_context, mtetherd_hal_device_added)) { + g_warning("Error assigning device added callback"); + } + if (!libhal_ctx_set_device_removed(plugin->hal_context, mtetherd_hal_device_removed)) { + g_warning("Error assigning device removed callback"); + } + DBusError derr; + dbus_error_init(&derr); + if (!libhal_ctx_init(plugin->hal_context, &derr)) { + if (dbus_error_is_set(&derr)) { + g_warning("Error initializing HAL context (%s): %s", derr.name, derr.message); + dbus_error_free(&derr); + } else { + g_warning("Error initializing HAL context: unknown error"); + } + libhal_ctx_free(plugin->priv->hal_context); + plugin->hal_context = NULL; + return FALSE; + } + } else { + g_warning("Can't set DBUS connection of HAL context"); + libhal_ctx_free(plugin->hal_context); + plugin->hal_context = NULL; + return FALSE; + } + } else { + g_warning("Can't allocate HAL context"); + return FALSE; + } + } + return TRUE; + + } + return FALSE; +} + +void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin) { + if (plugin) { + if (plugin->hal_context) { + libhal_ctx_shutdown(plugin->hal_context, NULL); + libhal_ctx_free(plugin->hal_context); + } + if (plugin->dbus_connection) { + dbus_g_connection_unref(plugin->dbus_connection); + } + } +} + +void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin) { + if (plugin && plugin->hal_context) { + DBusError derr; + dbus_error_init(&derr); + int ndevices = 0; + char **udis = libhal_find_device_by_capability(plugin->hal_context, "net", &ndevices, &derr); + if (dbus_error_is_set(&derr)) { + g_warning("Error loading list of network devices (%s): %s", derr.name, derr.message); + dbus_error_free(&derr); + } + if (udis) { + int i; + for (i = 0; i < ndevices; i++) { + char *device = libhal_device_get_property_string(plugin->hal_context, udis[i], "net.interface", &derr); + if (dbus_error_is_set(&derr)) { + g_warning("Error getting interface name of %s (%s): %s", udis[i], derr.name, derr.message); + dbus_error_free(&derr); + } + if (device) { + // Check if this one of the supported devices + plugin->devices = g_list_prepend(plugin->devices, udis[i]); + libhal_free_string(device); + } + } + libhal_free_string_array(udis); + } + } +} + diff --git a/hal.h b/hal.h new file mode 100644 index 0000000..3ebccc1 --- /dev/null +++ b/hal.h @@ -0,0 +1,34 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _MTETHERD_HAL_H +#define _MTETHERD_HAL_H + +#include +#include +#include +#include "plugin.h" + +gboolean mtetherd_hal_init(MTetherDStatusPlugin *plugin); +void mtetherd_hal_finalize(MTetherDStatusPlugin *plugin); +void mtetherd_hal_device_scan(MTetherDStatusPlugin *plugin); + +#endif //_MTETHERD_HAL_H + diff --git a/net.c b/net.c new file mode 100644 index 0000000..422794e --- /dev/null +++ b/net.c @@ -0,0 +1,51 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "net.h" + +#define MTETHERD_DEVICE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MTETHERD_DEVICE_TYPE_BAR, MTetherDDevicePrivate)) + +struct _MTetherDDevicePrivate { + char *interface; + char *udi; + struct in_addr addr; + struct in_addr netmask; + struct in_addr dhcp_start; + struct in_addr dhcp_end; +}; + +static void mtetherd_device_bar_finalize(MTetherDDevice *self) { + self->priv = MTETHERD_DEVICE_GET_PRIVATE(self); +} + +static void mtetherd_device_class_init(MTetherDDeviceClass *klass) { + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = mtetherd_device_bar_finalize; + g_type_class_add_private(klass, sizeof(MTetherDDevicePrivate)); +} + +static void mtetherd_device_bar_init(MTetherDDevice *self) { + self->priv = MTETHERD_DEVICE_GET_PRIVATE(self); + + +} + diff --git a/net.h b/net.h new file mode 100644 index 0000000..0bb9d2d --- /dev/null +++ b/net.h @@ -0,0 +1,53 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _MTETHERD_NET_H +#define _MTETHERD_NET_H + +#include + +G_BEGIN_DECLS + +#define TYPE_MTETHERD_DEVICE (mtetherd_device_get_type()) +#define MTETHERD_DEVICE(object) (G_TYPE_CHECK_INSTANCE_CAST((object), TYPE_MTETHERD_DEVICE, MTetherDDevice)) +#define MTETHERD_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_MTETHERD_DEVICE, MTetherDDeviceClass)) +#define IS_MTETHERD_DEVICE(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), TYPE_MTETHERD_DEVICE)) +#define IS_MTETHERD_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_MTETHERD_DEVICE)) +#define MTETHERD_DEVICE_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), TYPE_MTETHERD_DEVICE, MTetherDDeviceClass)) + +typedef struct _MTetherDDeviceClass MTetherDDeviceClass; +typedef struct _MTetherDDevice MTetherDDevice; +typedef struct _MTetherDDevicePrivate MTetherDDevicePrivate; + +struct _MTetherDDeviceClass { + GObjectClass parent; +}; + +struct _MTetherDDevice { + GObject parent; + MTetherDDevicePrivate *priv; +}; + +GType mtetherd_device_get_type(); + +G_END_DECLS + +#endif //_MTETHERD_NET_H + diff --git a/plugin.c b/plugin.c new file mode 100644 index 0000000..e432e0e --- /dev/null +++ b/plugin.c @@ -0,0 +1,163 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "plugin.h" + +#define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate)) + +struct _MTetherDStatusPluginPrivate { + GtkWidget *enable_button; + gboolean usb_on; + gboolean net_on; +}; + +#ifndef IMAGE_DIR +#define IMAGE_DIR "/usr/share/pixmaps" +#endif +#ifndef SBIN_DIR +#define SBIN_DIR "/usr/sbin" +#endif + +HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM); + +static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { } + +static void mtetherd_status_plugin_finalize(GObject *object) { + g_message("Destroying mtetherd status plugin"); + + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object); + + if (plugin) { + mtetherd_hal_finalize(plugin); + } +} + +static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) { + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = mtetherd_status_plugin_finalize; + g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate)); +} + +static void enable_button_set_text(GtkWidget *button, gboolean enabled) { + if (enabled) { + hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Enabled"); + } else { + hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Disabled"); + } +} + +static void enable_button_clicked(GtkWidget *button, gpointer data) { + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data); + + if (plugin && plugin->priv && button == plugin->priv->enable_button) { + if (plugin->priv->net_on) { + if (!launch_usbnet_script(FALSE)) { + g_error("Error starting USB networking"); + } + } else { + if (!launch_usbnet_script(TRUE)) { + g_error("Error starting USB networking"); + } + } + } +} + +static void mtetherd_status_plugin_usb_plugged_show(MTetherDStatusPlugin *plugin) { + if (plugin) { + if (plugin->priv && plugin->priv->hal_context) { + DBusError derr; + dbus_error_init(&derr); + dbus_bool_t plugged = libhal_device_get_property_bool(plugin->priv->hal_context, USBDEV_PATH, "button.state.value", &derr); + if (dbus_error_is_set(&derr)) { + g_warning("Error getting USB plugged status (%s): %s", derr.name, derr.message); + hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message); + dbus_error_free(&derr); + } else { + plugin->priv->usb_on = plugged; + if (plugin->priv->usb_on) { + gtk_widget_show(GTK_WIDGET(plugin)); + } else { + gtk_widget_hide(GTK_WIDGET(plugin)); + } + } + } else { + // DEBUG + //gtk_widget_show(GTK_WIDGET(plugin)); + } + } +} + +static void mtetherd_status_plugin_mapped(GtkWidget *widget, gpointer data) { + hildon_banner_show_informationf(widget, NULL, "Plugin mapped"); + MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(widget); + + if (plugin && plugin->priv) { + plugin->priv->net_on = get_usbnet_enabled(plugin); + if (plugin->priv->enable_button) { + enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); + } + } +} + +static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) { + plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin); + + plugin->priv->usb_on = FALSE; + plugin->priv->net_on = FALSE; + + plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL); + if (plugin->priv->enable_button) { + GError *err = NULL; + GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err); + if (err) { + g_warning("Can't load mtetherd icon: %s", err->message); + g_error_free(err); + err = NULL; + } + if (icon) { + GtkWidget *image = gtk_image_new_from_pixbuf(icon); + hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image); + hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT); + g_object_unref(icon); + } + gboolean enabled = get_usbnet_enabled(plugin); + enable_button_set_text(plugin->priv->enable_button, enabled); + g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(enable_button_clicked), plugin); + gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button); + gtk_widget_show_all(plugin->priv->enable_button); + } + + mtetherd_hal_init(plugin); + + mtetherd_status_plugin_usb_plugged_show(plugin); + + //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin"); +} + diff --git a/plugin.h b/plugin.h new file mode 100644 index 0000000..ec81da2 --- /dev/null +++ b/plugin.h @@ -0,0 +1,55 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _MTETHERD_STATUS_H +#define _MTETHERD_STATUS_H + +#include + +G_BEGIN_DECLS + +#define TYPE_MTETHERD_STATUS_PLUGIN (mtetherd_status_plugin_get_type()) +#define MTETHERD_STATUS_PLUGIN(object) (G_TYPE_CHECK_INSTANCE_CAST((object), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPlugin)) +#define MTETHERD_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginClass)) +#define IS_MTETHERD_STATUS_PLUGIN(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), TYPE_MTETHERD_STATUS_PLUGIN)) +#define IS_MTETHERD_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_MTETHERD_STATUS_PLUGIN)) +#define MTETHERD_STATUS_PLUGIN_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginClass)) + +typedef struct _MTetherDStatusPluginClass MTetherDStatusPluginClass; +typedef struct _MTetherDStatusPlugin MTetherDStatusPlugin; +typedef struct _MTetherDStatusPluginPrivate MTetherDStatusPluginPrivate; + +struct _MTetherDStatusPluginClass { + HDStatusMenuItemClass parent; +}; + +struct _MTetherDStatusPlugin { + HDStatusMenuItem parent; + MTetherDStatusPluginPrivate *priv; + DBusGConnection *dbus_connection; + LibHalContext *hal_context; +}; + +GType mtetherd_status_plugin_get_type(); + +G_END_DECLS + +#endif //_MTETHERD_STATUS_H + diff --git a/status.c b/status.c deleted file mode 100644 index 4d7ec06..0000000 --- a/status.c +++ /dev/null @@ -1,297 +0,0 @@ -/* - mtetherd - (c) 2010 Gregor Riepl - - Tethering utility for Maemo - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "status.h" - -#define MTETHERD_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginPrivate)) - -struct _MTetherDStatusPluginPrivate { - GtkWidget *enable_button; - gboolean usb_on; - gboolean net_on; - DBusGConnection *dbus_connection; - LibHalContext *hal_context; -}; - -static const char *USBDEV_PATH = "/org/freedesktop/Hal/devices/usb_device_1d6b_2_musb_hdrc"; -static const char *USBNET_MODULE = "g_ether"; - -#ifndef IMAGE_DIR -#define IMAGE_DIR "/usr/share/pixmaps" -#endif -#ifndef SBIN_DIR -#define SBIN_DIR "/usr/sbin" -#endif - -HD_DEFINE_PLUGIN_MODULE(MTetherDStatusPlugin, mtetherd_status_plugin, HD_TYPE_STATUS_MENU_ITEM); - -static void mtetherd_status_plugin_class_finalize(MTetherDStatusPluginClass *klass) { } - -static void mtetherd_status_plugin_finalize(GObject *object) { - g_message("Destroying mtetherd status plugin"); - - MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(object); - - if (plugin && plugin->priv) { - if (plugin->priv->hal_context) { - libhal_ctx_shutdown(plugin->priv->hal_context, NULL); - libhal_ctx_free(plugin->priv->hal_context); - } - if (plugin->priv->dbus_connection) { - dbus_g_connection_unref(plugin->priv->dbus_connection); - } - } -} - -static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) { - GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - - gobject_class->finalize = mtetherd_status_plugin_finalize; - g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate)); -} - -static gboolean get_usbnet_enabled(MTetherDStatusPlugin *plugin) { - g_message("Scanning /proc/modules"); - //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Scanning /proc/modules"); - - FILE *fp = fopen("/proc/modules", "r"); - if (!fp) { - return FALSE; - } - gboolean found = FALSE; - char *line = NULL; - while (!found && getline(&line, NULL, fp) != -1) { - g_message("Checking if '%s' contains g_ether...", line); - if (strncmp(line, USBNET_MODULE, sizeof(USBNET_MODULE) - 1) == 0) { - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Found g_ether"); - found = TRUE; - } - free(line); - line = NULL; - } - fclose(fp); - return found; -} - -static gboolean launch_usbnet_script(gboolean enable) { - const char *arg; - if (enable) { - arg = SBIN_DIR "mtetherd-usbnet-enable.sh"; - } else { - arg = SBIN_DIR "mtetherd-usbnet-disable.sh"; - } - g_debug("Launching %s", arg); - const char *command[] = { "/usr/bin/sudo", arg, NULL }; - pid_t pid = fork(); - if (pid == 0) { - if (execv(command[0], (char **const) command) == -1) { - exit(1); - } - } else if (pid == -1) { - // error forking - return FALSE; - } - // launch ok - return TRUE; -} - -static void enable_button_set_text(GtkWidget *button, gboolean enabled) { - if (enabled) { - hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Enabled"); - } else { - hildon_button_set_text(HILDON_BUTTON(button), "Toggle USB Networking", "Disabled"); - } -} - -static void enable_button_clicked(GtkWidget *button, gpointer data) { - MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(data); - - if (plugin && plugin->priv && button == plugin->priv->enable_button) { - if (plugin->priv->net_on) { - if (!launch_usbnet_script(FALSE)) { - g_error("Error starting USB networking"); - } - } else { - if (!launch_usbnet_script(TRUE)) { - g_error("Error starting USB networking"); - } - } - } -} - -static void mtetherd_status_plugin_usb_plugged_show(MTetherDStatusPlugin *plugin) { - if (plugin) { - if (plugin->priv && plugin->priv->hal_context) { - DBusError derr; - dbus_error_init(&derr); - dbus_bool_t plugged = libhal_device_get_property_bool(plugin->priv->hal_context, USBDEV_PATH, "button.state.value", &derr); - if (dbus_error_is_set(&derr)) { - g_warning("Error getting USB plugged status (%s): %s", derr.name, derr.message); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error getting USB plugged status (%s): %s", derr.name, derr.message); - dbus_error_free(&derr); - } else { - plugin->priv->usb_on = plugged; - if (plugin->priv->usb_on) { - gtk_widget_show(GTK_WIDGET(plugin)); - } else { - gtk_widget_hide(GTK_WIDGET(plugin)); - } - } - } else { - // DEBUG - //gtk_widget_show(GTK_WIDGET(plugin)); - } - } -} - -static void mtetherd_status_plugin_mapped(GtkWidget *widget, gpointer data) { - hildon_banner_show_informationf(widget, NULL, "Plugin mapped"); - MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(widget); - - if (plugin && plugin->priv) { - plugin->priv->net_on = get_usbnet_enabled(plugin); - if (plugin->priv->enable_button) { - enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); - } - } -} - -static gboolean mtetherd_status_plugin_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) { - g_message("Got event %d", event->type); - return FALSE; -} - -static void mtetherd_status_plugin_device_condition(LibHalContext *ctx, const char *udi, const char *condition, const char *detail) { - MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(libhal_ctx_get_user_data(ctx)); - - if (plugin) { - g_message("Got HAL condition %s on %s: %s", condition, udi, detail); - //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Got HAL condition %s on %s: %s", condition, udi, detail); - if (strcmp(USBDEV_CONDITION, "ButtonPressed") == 0) { - if (strcmp(USBDEV_PATH, udi) == 0) { - mtetherd_status_plugin_usb_plugged_show(plugin); - } - } else if (strcmp("DeviceAdded", condition) == 0) { - if (strcmp(USBDEV_PATH, detail) == 0) { - plugin->priv->net_on = TRUE; - enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); - } - } else if (strcmp("DeviceRemoved", condition) == 0) { - if (strcmp(USBDEV_PATH, detail) == 0) { - plugin->priv->net_on = FALSE; - enable_button_set_text(plugin->priv->enable_button, plugin->priv->net_on); - } - } - } -} - -static void mtetherd_status_plugin_init(MTetherDStatusPlugin *plugin) { - plugin->priv = MTETHERD_STATUS_PLUGIN_GET_PRIVATE(plugin); - - plugin->priv->usb_on = FALSE; - plugin->priv->net_on = FALSE; - - plugin->priv->enable_button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL); - if (plugin->priv->enable_button) { - GError *err = NULL; - GdkPixbuf *icon = gdk_pixbuf_new_from_file(IMAGE_DIR "/mtetherd-net-icon.png", &err); - if (err) { - g_warning("Can't load mtetherd icon: %s", err->message); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't load mtetherd icon: %s", err->message); - g_error_free(err); - err = NULL; - } - if (icon) { - GtkWidget *image = gtk_image_new_from_pixbuf(icon); - hildon_button_set_image(HILDON_BUTTON(plugin->priv->enable_button), image); - hildon_button_set_image_position(HILDON_BUTTON(plugin->priv->enable_button), GTK_POS_LEFT); - g_object_unref(icon); - } - gboolean enabled = get_usbnet_enabled(plugin); - enable_button_set_text(plugin->priv->enable_button, enabled); - g_signal_connect(plugin->priv->enable_button, "clicked", G_CALLBACK(enable_button_clicked), plugin); - gtk_container_add(GTK_CONTAINER(plugin), plugin->priv->enable_button); - gtk_widget_show_all(plugin->priv->enable_button); - } - //g_signal_connect(plugin, "expose-event", G_CALLBACK(mtetherd_status_plugin_event), NULL); - - //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Initialized mtetherd status plugin"); - GError *err = NULL; - plugin->priv->dbus_connection = hd_status_plugin_item_get_dbus_g_connection(HD_STATUS_PLUGIN_ITEM(plugin), DBUS_BUS_SYSTEM, &err); - if (err) { - g_warning("Can't open DBUS connection: %s", err->message); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error opening DBUS connection: %s", err->message); - g_error_free(err); - err = NULL; - } else { - g_message("Got DBUS Glib connection: %p", plugin->priv->dbus_connection); - } - if (plugin->priv->dbus_connection) { - plugin->priv->hal_context = libhal_ctx_new(); - if (plugin->priv->hal_context) { - if (libhal_ctx_set_dbus_connection(plugin->priv->hal_context, dbus_g_connection_get_connection(plugin->priv->dbus_connection))) { - if (!libhal_ctx_set_user_data(plugin->priv->hal_context, plugin)) { - g_warning("Can't set user data of HAL context"); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't set user data of HAL context"); - } - if (!libhal_ctx_set_device_condition(plugin->priv->hal_context, mtetherd_status_plugin_device_condition)) { - g_warning("Error assigning device condition callback"); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error assigning device condition callback"); - } - DBusError derr; - dbus_error_init(&derr); - if (!libhal_ctx_init(plugin->priv->hal_context, &derr)) { - if (dbus_error_is_set(&derr)) { - g_warning("Error initializing HAL context (%s): %s", derr.name, derr.message); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error initializing HAL context (%s): %s", derr.name, derr.message); - dbus_error_free(&derr); - } else { - g_warning("Error initializing HAL context: unknown error"); - //hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Error initializing HAL context: unknown error"); - } - libhal_ctx_free(plugin->priv->hal_context); - plugin->priv->hal_context = NULL; - } - } else { - g_warning("Can't set DBUS connection of HAL context"); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't set DBUS connection of HAL context"); - libhal_ctx_free(plugin->priv->hal_context); - plugin->priv->hal_context = NULL; - } - } else { - g_warning("Can't allocate HAL context"); - hildon_banner_show_informationf(GTK_WIDGET(plugin), NULL, "Can't allocate HAL context"); - } - } - - mtetherd_status_plugin_usb_plugged_show(plugin); -} - diff --git a/status.h b/status.h deleted file mode 100644 index 3cdae45..0000000 --- a/status.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - mtetherd - (c) 2010 Gregor Riepl - - Tethering utility for Maemo - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef _MTETHERD_STATUS_H -#define _MTETHERD_STATUS_H - -#include - -G_BEGIN_DECLS - -#define TYPE_MTETHERD_STATUS_PLUGIN (mtetherd_status_plugin_get_type()) -#define MTETHERD_STATUS_PLUGIN(object) (G_TYPE_CHECK_INSTANCE_CAST((object), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPlugin)) -#define MTETHERD_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginClass)) -#define IS_MTETHERD_STATUS_PLUGIN(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), TYPE_MTETHERD_STATUS_PLUGIN)) -#define IS_MTETHERD_STATUS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_MTETHERD_STATUS_PLUGIN)) -#define MTETHERD_STATUS_PLUGIN_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), TYPE_MTETHERD_STATUS_PLUGIN, MTetherDStatusPluginClass)) - -typedef struct _MTetherDStatusPluginClass MTetherDStatusPluginClass; -typedef struct _MTetherDStatusPlugin MTetherDStatusPlugin; -typedef struct _MTetherDStatusPluginPrivate MTetherDStatusPluginPrivate; - -struct _MTetherDStatusPluginClass { - HDStatusMenuItemClass parent; -}; - -struct _MTetherDStatusPlugin { - HDStatusMenuItem parent; - MTetherDStatusPluginPrivate *priv; -}; - -GType mtetherd_status_plugin_get_type(); - -G_END_DECLS - -#endif //_MTETHERD_STATUS_H - diff --git a/util.c b/util.c new file mode 100644 index 0000000..cc3966d --- /dev/null +++ b/util.c @@ -0,0 +1,72 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include + +static const char *MODULE_LIST = "/proc/modules"; + +gboolean mtetherd_scan_modules(const char *module) { + g_message("Scanning %s", MODULE_LIST); + + FILE *fp = fopen(MODULE_LIST, "r"); + if (!fp) { + return FALSE; + } + gboolean found = FALSE; + char *line = NULL; + while (!found && getline(&line, NULL, fp) != -1) { + g_debug("Checking if '%s' starts with %s...", line, module); + if (strcmp(line, module, strlen(module)) == 0) { + g_message("Found %s", module); + found = TRUE; + } + free(line); + line = NULL; + } + fclose(fp); + return found; +} + +gboolean mtetherd_launch_script(const char *command[]) { +/* + const char *arg; + if (enable) { + arg = SBIN_DIR "mtetherd-usbnet-enable.sh"; + } else { + arg = SBIN_DIR "mtetherd-usbnet-disable.sh"; + } + g_debug("Launching %s", arg); + const char *command[] = { "/usr/bin/sudo", arg, NULL }; +*/ + pid_t pid = fork(); + if (pid == 0) { + if (execv(command[0], (char **const) command) == -1) { + exit(1); + } + } else if (pid == -1) { + // error forking + return FALSE; + } + // launch ok + return TRUE; +} + diff --git a/util.h b/util.h new file mode 100644 index 0000000..118a5d1 --- /dev/null +++ b/util.h @@ -0,0 +1,30 @@ +/* + mtetherd + (c) 2010 Gregor Riepl + + Tethering utility for Maemo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _MTETHERD_UTIL_H +#define _MTETHERD_UTIL_H + +#include + +gboolean mtetherd_scan_modules(const char *module); +gboolean mtetherd_launch_script(const char *command[]); + +#endif //_MTETHERD_UTIL_H + -- 1.7.9.5