diff options
author | Jasper St. Pierre <jstpierre@mecheye.net> | 2013-05-27 17:23:11 -0400 |
---|---|---|
committer | Jasper St. Pierre <jstpierre@mecheye.net> | 2013-06-12 16:19:28 -0400 |
commit | 5203a8059eabe36921944520412d0a6b277daf0b (patch) | |
tree | 72bc808a85637b4855f34f6f5955b859273de7d6 | |
parent | f86eafd49701ef46b1e6d3a3d88b893068135604 (diff) | |
download | gnome-settings-daemon-wip/aggregate-menu.tar.gz |
power: Add support for rfkill switches in g-s-d's power pluginwip/aggregate-menu
This will be used by gnome-shell and gnome-control-center, instead
of copy/paste rfkill code.
-rw-r--r-- | plugins/power/Makefile.am | 6 | ||||
-rw-r--r-- | plugins/power/gsd-power-manager.c | 113 | ||||
-rw-r--r-- | plugins/power/rfkill-glib.c | 299 | ||||
-rw-r--r-- | plugins/power/rfkill-glib.h | 65 | ||||
-rw-r--r-- | plugins/power/rfkill.h | 107 |
5 files changed, 590 insertions, 0 deletions
diff --git a/plugins/power/Makefile.am b/plugins/power/Makefile.am index 42ab1265..b5eaf52f 100644 --- a/plugins/power/Makefile.am +++ b/plugins/power/Makefile.am @@ -8,6 +8,9 @@ plugin_LTLIBRARIES = \ libpower_la_SOURCES = \ gpm-common.c \ gpm-common.h \ + rfkill-glib.c \ + rfkill-glib.h \ + rfkill.h \ gsd-backlight-linux.c \ gsd-backlight-linux.h \ gsd-power-manager.c \ @@ -66,6 +69,9 @@ noinst_PROGRAMS = gsd-test-power gsd_test_power_SOURCES = \ gpm-common.c \ gpm-common.h \ + rfkill-glib.c \ + rfkill-glib.h \ + rfkill.h \ gsd-backlight-linux.c \ gsd-backlight-linux.h \ gsd-power-manager.c \ diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c index b5fba32c..573a3106 100644 --- a/plugins/power/gsd-power-manager.c +++ b/plugins/power/gsd-power-manager.c @@ -50,6 +50,7 @@ #include "gnome-settings-session.h" #include "gsd-enums.h" #include "gsd-power-manager.h" +#include "rfkill-glib.h" #define GNOME_SESSION_DBUS_NAME "org.gnome.SessionManager" #define GNOME_SESSION_DBUS_PATH_PRESENCE "/org/gnome/SessionManager/Presence" @@ -95,6 +96,7 @@ static const gchar introspection_xml[] = " <property name='Icon' type='s' access='read'/>" " <property name='Tooltip' type='s' access='read'/>" " <property name='Percentage' type='d' access='read'/>" +" <property name='AirplaneMode' type='b' access='readwrite'/>" " <method name='GetPrimaryDevice'>" " <arg name='device' type='(susdut)' direction='out' />" " </method>" @@ -214,6 +216,10 @@ struct GsdPowerManagerPrivate GsdPowerIdleMode previous_idle_mode; guint xscreensaver_watchdog_timer_id; + + /* Killswitch stuff */ + CcRfkillGlib *rfkill; + GHashTable *killswitches; }; enum { @@ -227,6 +233,7 @@ static void engine_update_composite_device (GsdPowerManager *manager); static GIcon *engine_get_icon (GsdPowerManager *manager); static gchar *engine_get_summary (GsdPowerManager *manager); static gdouble engine_get_percentage (GsdPowerManager *manager); +static gboolean engine_get_airplane_mode (GsdPowerManager *manager); static void do_power_action_type (GsdPowerManager *manager, GsdPowerActionType action_type); static void do_lid_closed_action (GsdPowerManager *manager); static void uninhibit_lid_switch (GsdPowerManager *manager); @@ -3270,6 +3277,73 @@ logind_proxy_signal_cb (GDBusProxy *proxy, } } +static gboolean +engine_get_airplane_mode (GsdPowerManager *manager) +{ + gboolean enabled; + GHashTableIter iter; + gpointer key, value; + + enabled = TRUE; + + g_hash_table_iter_init (&iter, manager->priv->killswitches); + while (g_hash_table_iter_next (&iter, &key, &value)) { + int idx, state; + + idx = GPOINTER_TO_INT (key); + state = GPOINTER_TO_INT (value); + g_debug ("Killswitch %d is %s", idx, state ? "enabled" : "disabled"); + + /* A single device that's enabled? airplane mode is off */ + if (state == FALSE) { + enabled = FALSE; + break; + } + } + + return enabled; +} + +static void +rfkill_changed (CcRfkillGlib *rfkill, + GList *events, + GsdPowerManager *manager) +{ + GList *l; + GVariant *params; + + for (l = events; l != NULL; l = l->next) { + struct rfkill_event *event = l->data; + + switch (event->op) { + case RFKILL_OP_ADD: + case RFKILL_OP_CHANGE: + g_hash_table_insert (manager->priv->killswitches, + GINT_TO_POINTER (event->idx), + GINT_TO_POINTER (event->soft || event->hard)); + break; + case RFKILL_OP_DEL: + g_hash_table_remove (manager->priv->killswitches, + GINT_TO_POINTER (event->idx)); + break; + } + } + + /* not yet connected to the bus */ + if (manager->priv->connection == NULL) + return; + + params = g_variant_new_parsed ("('" GSD_POWER_DBUS_NAME "', [{'AirplaneMode', %v}], @as [])", + g_variant_new_boolean (engine_get_airplane_mode (manager))); + + g_dbus_connection_emit_signal (manager->priv->connection, + NULL, + GSD_POWER_DBUS_PATH, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + params, NULL); +} + gboolean gsd_power_manager_start (GsdPowerManager *manager, GError **error) @@ -3432,6 +3506,12 @@ gsd_power_manager_start (GsdPowerManager *manager, /* don't blank inside a VM */ manager->priv->is_virtual_machine = gsd_power_is_hardware_a_vm (); + manager->priv->killswitches = g_hash_table_new (g_direct_hash, g_direct_equal); + manager->priv->rfkill = cc_rfkill_glib_new (); + g_signal_connect (G_OBJECT (manager->priv->rfkill), "changed", + G_CALLBACK (rfkill_changed), manager); + cc_rfkill_glib_open (manager->priv->rfkill); + gnome_settings_profile_end (NULL); return TRUE; } @@ -3755,6 +3835,10 @@ handle_get_property_main (GsdPowerManager *manager, percentage = engine_get_percentage (manager); if (percentage >= 0) retval = g_variant_new_double (percentage); + } else if (g_strcmp0 (property_name, "AirplaneMode") == 0) { + gboolean airplane_mode; + airplane_mode = engine_get_airplane_mode (manager); + retval = g_variant_new_boolean (airplane_mode); } return retval; @@ -3805,6 +3889,33 @@ handle_get_property (GDBusConnection *connection, } static gboolean +engine_set_airplane_mode (GsdPowerManager *manager, + gboolean enable) +{ + struct rfkill_event event; + + memset (&event, 0, sizeof(event)); + event.op = RFKILL_OP_CHANGE_ALL; + event.type = RFKILL_TYPE_ALL; + event.soft = enable ? 1 : 0; + return cc_rfkill_glib_send_event (manager->priv->rfkill, &event) >= 0; +} + +static gboolean +handle_set_property_main (GsdPowerManager *manager, + const gchar *property_name, + GVariant *value) +{ + if (g_strcmp0 (property_name, "AirplaneMode") == 0) { + gboolean airplane_mode; + g_variant_get (value, "b", &airplane_mode); + return engine_set_airplane_mode (manager, airplane_mode); + } + + return FALSE; +} + +static gboolean handle_set_property_screen (GsdPowerManager *manager, const gchar *property_name, GVariant *value) @@ -3838,6 +3949,8 @@ handle_set_property (GDBusConnection *connection, if (g_strcmp0 (interface_name, GSD_POWER_DBUS_INTERFACE_SCREEN) == 0) { return handle_set_property_screen (manager, property_name, value); + } else if (g_strcmp0 (interface_name, GSD_POWER_DBUS_INTERFACE) == 0) { + return handle_set_property_main (manager, property_name, value); } else { g_warning ("not recognised interface: %s", interface_name); return FALSE; diff --git a/plugins/power/rfkill-glib.c b/plugins/power/rfkill-glib.c new file mode 100644 index 00000000..7c9ee3cd --- /dev/null +++ b/plugins/power/rfkill-glib.c @@ -0,0 +1,299 @@ +/* + * + * gnome-bluetooth - Bluetooth integration for GNOME + * + * Copyright (C) 2012 Bastien Nocera <hadess@hadess.net> + * + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <errno.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> + +#include <glib.h> + +#include "rfkill-glib.h" + +enum { + CHANGED, + LAST_SIGNAL +}; + +static int signals[LAST_SIGNAL] = { 0 }; + +#define CC_RFKILL_GLIB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ + CC_RFKILL_TYPE_GLIB, CcRfkillGlibPrivate)) + +struct CcRfkillGlibPrivate { + int fd; + GIOChannel *channel; + guint watch_id; +}; + +G_DEFINE_TYPE(CcRfkillGlib, cc_rfkill_glib, G_TYPE_OBJECT) + +int +cc_rfkill_glib_send_event (CcRfkillGlib *rfkill, struct rfkill_event *event) +{ + g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1); + g_return_val_if_fail (rfkill->priv->fd > 0, -1); + + return write (rfkill->priv->fd, event, sizeof(struct rfkill_event)); +} + +static const char * +type_to_string (unsigned int type) +{ + switch (type) { + case RFKILL_TYPE_ALL: + return "ALL"; + case RFKILL_TYPE_WLAN: + return "WLAN"; + case RFKILL_TYPE_BLUETOOTH: + return "RFKILL"; + case RFKILL_TYPE_UWB: + return "UWB"; + case RFKILL_TYPE_WIMAX: + return "WIMAX"; + case RFKILL_TYPE_WWAN: + return "WWAN"; + default: + return "UNKNOWN"; + } +} + +static const char * +op_to_string (unsigned int op) +{ + switch (op) { + case RFKILL_OP_ADD: + return "ADD"; + case RFKILL_OP_DEL: + return "DEL"; + case RFKILL_OP_CHANGE: + return "CHANGE"; + case RFKILL_OP_CHANGE_ALL: + return "CHANGE_ALL"; + default: + g_assert_not_reached (); + } +} + +static void +print_event (struct rfkill_event *event) +{ + g_debug ("RFKILL event: idx %u type %u (%s) op %u (%s) soft %u hard %u", + event->idx, + event->type, type_to_string (event->type), + event->op, op_to_string (event->op), + event->soft, event->hard); +} + +static void +emit_changed_signal_and_free (CcRfkillGlib *rfkill, + GList *events) +{ + if (events == NULL) + return; + + g_signal_emit (G_OBJECT (rfkill), + signals[CHANGED], + 0, events); + g_list_free_full (events, g_free); +} + +static gboolean +event_cb (GIOChannel *source, + GIOCondition condition, + CcRfkillGlib *rfkill) +{ + GList *events; + + events = NULL; + + if (condition & G_IO_IN) { + GIOStatus status; + struct rfkill_event event; + gsize read; + + status = g_io_channel_read_chars (source, + (char *) &event, + sizeof(event), + &read, + NULL); + + while (status == G_IO_STATUS_NORMAL && read == sizeof(event)) { + struct rfkill_event *event_ptr; + + print_event (&event); + + event_ptr = g_memdup (&event, sizeof(event)); + events = g_list_prepend (events, event_ptr); + + status = g_io_channel_read_chars (source, + (char *) &event, + sizeof(event), + &read, + NULL); + } + events = g_list_reverse (events); + } else { + g_debug ("something else happened"); + return FALSE; + } + + emit_changed_signal_and_free (rfkill, events); + + return TRUE; +} + +static void +cc_rfkill_glib_init (CcRfkillGlib *rfkill) +{ + CcRfkillGlibPrivate *priv; + + priv = CC_RFKILL_GLIB_GET_PRIVATE (rfkill); + rfkill->priv = priv; + rfkill->priv->fd = -1; +} + +int +cc_rfkill_glib_open (CcRfkillGlib *rfkill) +{ + CcRfkillGlibPrivate *priv; + int fd; + int ret; + GList *events; + + g_return_val_if_fail (RFKILL_IS_GLIB (rfkill), -1); + g_return_val_if_fail (rfkill->priv->fd == -1, -1); + + priv = rfkill->priv; + + fd = open("/dev/rfkill", O_RDWR); + if (fd < 0) { + if (errno == EACCES) + g_warning ("Could not open RFKILL control device, please verify your installation"); + return fd; + } + + ret = fcntl(fd, F_SETFL, O_NONBLOCK); + if (ret < 0) { + g_debug ("Can't set RFKILL control device to non-blocking"); + close(fd); + return ret; + } + + events = NULL; + + while (1) { + struct rfkill_event event; + struct rfkill_event *event_ptr; + ssize_t len; + + len = read(fd, &event, sizeof(event)); + if (len < 0) { + if (errno == EAGAIN) + break; + g_debug ("Reading of RFKILL events failed"); + break; + } + + if (len != RFKILL_EVENT_SIZE_V1) { + g_warning ("Wrong size of RFKILL event\n"); + continue; + } + + if (event.op != RFKILL_OP_ADD) + continue; + + g_debug ("Read killswitch of type '%s' (idx=%d): soft %d hard %d", + type_to_string (event.type), + event.idx, event.soft, event.hard); + + event_ptr = g_memdup (&event, sizeof(event)); + events = g_list_prepend (events, event_ptr); + } + + /* Setup monitoring */ + priv->fd = fd; + priv->channel = g_io_channel_unix_new (priv->fd); + priv->watch_id = g_io_add_watch (priv->channel, + G_IO_IN | G_IO_HUP | G_IO_ERR, + (GIOFunc) event_cb, + rfkill); + + events = g_list_reverse (events); + emit_changed_signal_and_free (rfkill, events); + + return fd; +} + +static void +cc_rfkill_glib_finalize (GObject *object) +{ + CcRfkillGlib *rfkill; + CcRfkillGlibPrivate *priv; + + rfkill = CC_RFKILL_GLIB (object); + priv = rfkill->priv; + + /* cleanup monitoring */ + if (priv->watch_id > 0) { + g_source_remove (priv->watch_id); + priv->watch_id = 0; + g_io_channel_shutdown (priv->channel, FALSE, NULL); + g_io_channel_unref (priv->channel); + } + close(priv->fd); + priv->fd = -1; + + G_OBJECT_CLASS(cc_rfkill_glib_parent_class)->finalize(object); +} + +static void +cc_rfkill_glib_class_init(CcRfkillGlibClass *klass) +{ + GObjectClass *object_class = (GObjectClass *) klass; + + g_type_class_add_private(klass, sizeof(CcRfkillGlibPrivate)); + object_class->finalize = cc_rfkill_glib_finalize; + + signals[CHANGED] = + g_signal_new ("changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (CcRfkillGlibClass, changed), + NULL, NULL, + NULL, + G_TYPE_NONE, 1, G_TYPE_POINTER); + +} + +CcRfkillGlib * +cc_rfkill_glib_new (void) +{ + return CC_RFKILL_GLIB (g_object_new (CC_RFKILL_TYPE_GLIB, NULL)); +} diff --git a/plugins/power/rfkill-glib.h b/plugins/power/rfkill-glib.h new file mode 100644 index 00000000..c2834d44 --- /dev/null +++ b/plugins/power/rfkill-glib.h @@ -0,0 +1,65 @@ +/* + * + * gnome-bluetooth - Bluetooth integration for GNOME + * + * Copyright (C) 2012 Bastien Nocera <hadess@hadess.net> + * + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CC_RFKILL_GLIB_H +#define __CC_RFKILL_GLIB_H + +#include <glib-object.h> +#include "rfkill.h" + +G_BEGIN_DECLS + +#define CC_RFKILL_TYPE_GLIB (cc_rfkill_glib_get_type()) +#define CC_RFKILL_GLIB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + CC_RFKILL_TYPE_GLIB, CcRfkillGlib)) +#define CC_RFKILL_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), \ + CC_RFKILL_TYPE_GLIB, CcRfkillGlibClass)) +#define RFKILL_IS_GLIB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + CC_RFKILL_TYPE_GLIB)) +#define RFKILL_IS_GLIB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), \ + CC_RFKILL_TYPE_GLIB)) +#define RFKILL_GET_GLIB_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \ + CC_RFKILL_TYPE_GLIB, CcRfkillGlibClass)) + +typedef struct CcRfkillGlibPrivate CcRfkillGlibPrivate; + +typedef struct _CcRfkillGlib { + GObject parent; + CcRfkillGlibPrivate *priv; +} CcRfkillGlib; + +typedef struct _CcRfkillGlibClass { + GObjectClass parent_class; + + void (*changed) (CcRfkillGlib *rfkill, GList *events); +} CcRfkillGlibClass; + +GType cc_rfkill_glib_get_type(void); + +CcRfkillGlib *cc_rfkill_glib_new (void); +int cc_rfkill_glib_open (CcRfkillGlib *rfkill); +int cc_rfkill_glib_send_event (CcRfkillGlib *rfkill, struct rfkill_event *event); + +G_END_DECLS + +#endif /* __CC_RFKILL_GLIB_H */ diff --git a/plugins/power/rfkill.h b/plugins/power/rfkill.h new file mode 100644 index 00000000..abb2c661 --- /dev/null +++ b/plugins/power/rfkill.h @@ -0,0 +1,107 @@ +#ifndef __RFKILL_H +#define __RFKILL_H + +/* + * Copyright (C) 2006 - 2007 Ivo van Doorn + * Copyright (C) 2007 Dmitry Torokhov + * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <linux/types.h> + +/* define userspace visible states */ +#define RFKILL_STATE_SOFT_BLOCKED 0 +#define RFKILL_STATE_UNBLOCKED 1 +#define RFKILL_STATE_HARD_BLOCKED 2 + +/** + * enum rfkill_type - type of rfkill switch. + * + * @RFKILL_TYPE_ALL: toggles all switches (requests only - not a switch type) + * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device. + * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device. + * @RFKILL_TYPE_UWB: switch is on a ultra wideband device. + * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device. + * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device. + * @RFKILL_TYPE_GPS: switch is on a GPS device. + * @RFKILL_TYPE_FM: switch is on a FM radio device. + * @NUM_RFKILL_TYPES: number of defined rfkill types + */ +enum rfkill_type { + RFKILL_TYPE_ALL = 0, + RFKILL_TYPE_WLAN, + RFKILL_TYPE_BLUETOOTH, + RFKILL_TYPE_UWB, + RFKILL_TYPE_WIMAX, + RFKILL_TYPE_WWAN, + RFKILL_TYPE_GPS, + RFKILL_TYPE_FM, + NUM_RFKILL_TYPES, +}; + +/** + * enum rfkill_operation - operation types + * @RFKILL_OP_ADD: a device was added + * @RFKILL_OP_DEL: a device was removed + * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device + * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all) + */ +enum rfkill_operation { + RFKILL_OP_ADD = 0, + RFKILL_OP_DEL, + RFKILL_OP_CHANGE, + RFKILL_OP_CHANGE_ALL, +}; + +/** + * struct rfkill_event - events for userspace on /dev/rfkill + * @idx: index of dev rfkill + * @type: type of the rfkill struct + * @op: operation code + * @hard: hard state (0/1) + * @soft: soft state (0/1) + * + * Structure used for userspace communication on /dev/rfkill, + * used for events from the kernel and control to the kernel. + */ +struct rfkill_event { + __u32 idx; + __u8 type; + __u8 op; + __u8 soft, hard; +} __attribute__((packed)); + +/* + * We are planning to be backward and forward compatible with changes + * to the event struct, by adding new, optional, members at the end. + * When reading an event (whether the kernel from userspace or vice + * versa) we need to accept anything that's at least as large as the + * version 1 event size, but might be able to accept other sizes in + * the future. + * + * One exception is the kernel -- we already have two event sizes in + * that we've made the 'hard' member optional since our only option + * is to ignore it anyway. + */ +#define RFKILL_EVENT_SIZE_V1 8 + +/* ioctl for turning off rfkill-input (if present) */ +#define RFKILL_IOC_MAGIC 'R' +#define RFKILL_IOC_NOINPUT 1 +#define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT) + +/* and that's all userspace gets */ + +#endif /* RFKILL_H */ |