Commit 897519c2 authored by Guido Gunther's avatar Guido Gunther Committed by Guido Gunther
Browse files

Switch keyboard-events to GAction



This allows to register global keyboard events from different
parts of the shell easily and moves it's handling out of
the overcrowded settings.
Signed-off-by: Guido Gunther's avatarGuido Günther <guido.gunther@puri.sm>
parent 009177fa
......@@ -19,41 +19,34 @@
* @Title: PhoshKeyboardEvents
*/
enum {
SIGNAL_ACCELERATOR_ACTIVATED,
SIGNAL_ACCELERATOR_GRABBED,
N_SIGNALS,
};
static guint signals[N_SIGNALS] = { 0 };
struct _PhoshKeyboardEvents {
GObject parent;
GSimpleActionGroup parent;
struct phosh_private_keyboard_event *kbevent;
GHashTable *accelerators;
};
static void initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (PhoshKeyboardEvents, phosh_keyboard_events, G_TYPE_OBJECT,
G_DEFINE_TYPE_WITH_CODE (PhoshKeyboardEvents, phosh_keyboard_events, G_TYPE_SIMPLE_ACTION_GROUP,
G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, initable_iface_init));
static void
handle_accelerator_activated_event (void *data,
struct phosh_private_keyboard_event *kbevent,
uint32_t action_id,
uint32_t timestamp)
{
const gchar *action;
PhoshKeyboardEvents *self = PHOSH_KEYBOARD_EVENTS (data);
g_debug ("incoming action! %d", action_id);
g_signal_emit (self,
signals[SIGNAL_ACCELERATOR_ACTIVATED],
0,
action_id,
timestamp);
action = g_hash_table_lookup (self->accelerators, GINT_TO_POINTER (action_id));
g_return_if_fail (action);
g_debug ("Accelerator %d activated: %s", action_id, action);
g_return_if_fail (g_action_group_has_action (G_ACTION_GROUP (self), action));
g_action_group_activate_action (G_ACTION_GROUP (self), action, NULL);
}
......@@ -85,11 +78,8 @@ handle_grab_success_event (void *data,
uint32_t action_id)
{
PhoshKeyboardEvents *self = PHOSH_KEYBOARD_EVENTS (data);
g_signal_emit (self,
signals[SIGNAL_ACCELERATOR_GRABBED],
0,
accelerator,
action_id);
g_hash_table_insert (self->accelerators, GINT_TO_POINTER (action_id), g_strdup (accelerator));
}
static const struct phosh_private_keyboard_event_listener keyboard_event_listener = {
......@@ -99,6 +89,25 @@ static const struct phosh_private_keyboard_event_listener keyboard_event_listene
};
static void
on_action_added (PhoshKeyboardEvents *self,
gchar *action_name,
GActionGroup *action_group)
{
g_debug ("Grabbing accelerator %s", action_name);
phosh_private_keyboard_event_grab_accelerator_request (self->kbevent, action_name);
}
static void
on_action_removed (PhoshKeyboardEvents *self,
gchar *action_name,
GActionGroup *action_group)
{
g_debug ("Should ungrab accelerator %s", action_name);
}
static gboolean
initable_init (GInitable *initable,
GCancellable *cancelable,
......@@ -137,6 +146,16 @@ initable_init (GInitable *initable,
phosh_private_keyboard_event_add_listener (self->kbevent, &keyboard_event_listener, self);
g_signal_connect (self,
"action-added",
G_CALLBACK (on_action_added),
NULL);
g_signal_connect (self,
"action-removed",
G_CALLBACK (on_action_removed),
NULL);
return TRUE;
}
......@@ -152,65 +171,42 @@ static void
phosh_keyboard_events_dispose (GObject *object)
{
PhoshKeyboardEvents *self = PHOSH_KEYBOARD_EVENTS (object);
g_clear_pointer(&self->kbevent, phosh_private_keyboard_event_destroy);
g_clear_pointer (&self->kbevent, phosh_private_keyboard_event_destroy);
G_OBJECT_CLASS (phosh_keyboard_events_parent_class)->dispose (object);
}
static void
phosh_keyboard_events_class_init (PhoshKeyboardEventsClass *klass)
phosh_keyboard_events_finalize (GObject *object)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = phosh_keyboard_events_dispose;
PhoshKeyboardEvents *self = PHOSH_KEYBOARD_EVENTS (object);
/**
* PhoshKeyboardEvents::keypress:
* @kbevent: The #PhoshKeyboardEvents emitting the signal.
* @action_id: The id of the forwarded action
* @timestamp: The timestamp when the key has been pressed
*
* Emitted whenever a subscribed accelerator/action has been received
*/
signals[SIGNAL_ACCELERATOR_ACTIVATED] = g_signal_new (
"accelerator-activated",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
/**
* PhoshKeyboardEvents::accelerator-grabbed:
* @kbevent: The #PhoshKeyboardEvents emitting the signal.
* @accelerator: The accelerator which has been grabbed
* @action_id: The assigned id of the accelerator
*
* Emitted whenever an accelerator subscription has been successfull
*/
signals[SIGNAL_ACCELERATOR_GRABBED] = g_signal_new (
"accelerator-grabbed",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
NULL, G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_UINT);
g_clear_pointer (&self->accelerators, g_hash_table_unref);
G_OBJECT_CLASS (phosh_keyboard_events_parent_class)->finalize (object);
}
void
phosh_keyboard_events_register_keys (PhoshKeyboardEvents *self,
char **accelerators,
size_t len)
static void
phosh_keyboard_events_class_init (PhoshKeyboardEventsClass *klass)
{
g_return_if_fail (self->kbevent);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
for (size_t i = 0; i < len; ++i) {
phosh_private_keyboard_event_grab_accelerator_request (self->kbevent, accelerators[i]);
}
object_class->dispose = phosh_keyboard_events_dispose;
object_class->finalize = phosh_keyboard_events_finalize;
}
static void
phosh_keyboard_events_init (PhoshKeyboardEvents *self)
{
self->accelerators = g_hash_table_new_full (g_direct_hash,
g_direct_equal,
NULL,
g_free);
}
......
......@@ -17,9 +17,7 @@ G_DECLARE_FINAL_TYPE (PhoshKeyboardEvents,
phosh_keyboard_events,
PHOSH,
KEYBOARD_EVENTS,
GObject)
GSimpleActionGroup)
PhoshKeyboardEvents *phosh_keyboard_events_new (void);
void phosh_keyboard_events_register_keys (PhoshKeyboardEvents *self,
char **keys,
size_t len);
......@@ -70,15 +70,18 @@ typedef struct _PhoshSettings
GtkWidget *list_notifications;
GtkWidget *sw_notifications;
LfbEvent *notify_event;
/* KeyboardEvents */
PhoshKeyboardEvents *keyboard_events;
GHashTable *accelerator_callbacks;
} PhoshSettings;
G_DEFINE_TYPE (PhoshSettings, phosh_settings, GTK_TYPE_BIN)
static void raise_volume (GSimpleAction *action, GVariant *param, gpointer self);
static void lower_volume (GSimpleAction *action, GVariant *param, gpointer self);
const GActionEntry action_entries[] = {
{ "XF86AudioLowerVolume", lower_volume, },
{ "XF86AudioRaiseVolume", raise_volume, },
};
static void
brightness_value_changed_cb (GtkScale *scale_brightness, gpointer *unused)
......@@ -186,46 +189,22 @@ change_volume (PhoshSettings *self,
}
static void
lower_volume (PhoshSettings *self)
{
change_volume (self, -1);
}
static void
raise_volume (PhoshSettings *self)
lower_volume (GSimpleAction *action, GVariant *param, gpointer self)
{
change_volume (self, 1);
}
g_return_if_fail (PHOSH_IS_SETTINGS (self));
static void
accelerator_grabbed_cb (PhoshSettings *self,
const char *accelerator,
uint32_t action_id)
{
guint64 action = action_id;
if (g_strcmp0 (accelerator, "XF86AudioLowerVolume") == 0) {
g_hash_table_insert (self->accelerator_callbacks, (gpointer) action, (gpointer) lower_volume);
} else if (g_strcmp0 (accelerator, "XF86AudioRaiseVolume") == 0) {
g_hash_table_insert (self->accelerator_callbacks, (gpointer) action, (gpointer) raise_volume);
}
change_volume (PHOSH_SETTINGS (self), -1);
}
static void
accelerator_activated_cb (PhoshSettings *self,
uint32_t action_id,
uint32_t timestamp)
raise_volume (GSimpleAction *action, GVariant *param, gpointer self)
{
void (*callback)(PhoshSettings *);
guint64 action = action_id;
g_return_if_fail (PHOSH_IS_SETTINGS (self));
callback = g_hash_table_lookup (self->accelerator_callbacks, (gpointer) action);
if (callback == NULL) {
g_warning ("No callback for action %d", action_id);
return;
}
callback (self);
change_volume (PHOSH_SETTINGS (self), 1);
}
static void
update_output_vol_bar (PhoshSettings *self)
{
......@@ -460,6 +439,11 @@ phosh_settings_constructed (GObject *object)
on_notifcation_items_changed (self, -1, -1, -1,
G_LIST_MODEL (phosh_notify_manager_get_list (manager)));
phosh_shell_add_global_keyboard_action_entries (phosh_shell_get_default (),
action_entries,
G_N_ELEMENTS (action_entries),
self);
G_OBJECT_CLASS (phosh_settings_parent_class)->constructed (object);
}
......@@ -476,12 +460,6 @@ phosh_settings_dispose (GObject *object)
g_clear_object (&self->notify_event);
}
if (self->accelerator_callbacks != NULL) {
g_hash_table_remove_all (self->accelerator_callbacks);
g_hash_table_unref (self->accelerator_callbacks);
self->accelerator_callbacks = NULL;
}
G_OBJECT_CLASS (phosh_settings_parent_class)->dispose (object);
}
......@@ -543,37 +521,12 @@ phosh_settings_class_init (PhoshSettingsClass *klass)
static void
phosh_settings_init (PhoshSettings *self)
{
char *subscribe_accelerators[] = {
"XF86AudioLowerVolume",
"XF86AudioRaiseVolume",
"XF86AudioMute",
};
self->notify_event = lfb_event_new ("message-missed-notification");
gtk_widget_init_template (GTK_WIDGET (self));
self->keyboard_events = phosh_keyboard_events_new ();
if (!self->keyboard_events)
return;
self->accelerator_callbacks = g_hash_table_new (g_direct_hash, g_direct_equal);
phosh_keyboard_events_register_keys (self->keyboard_events,
subscribe_accelerators,
G_N_ELEMENTS (subscribe_accelerators));
g_signal_connect_swapped (self->keyboard_events,
"accelerator-activated",
G_CALLBACK (accelerator_activated_cb),
self);
g_signal_connect_swapped (self->keyboard_events,
"accelerator-grabbed",
G_CALLBACK (accelerator_grabbed_cb),
self);
}
GtkWidget *
phosh_settings_new (void)
{
......
......@@ -33,6 +33,7 @@
#include "feedback-manager.h"
#include "home.h"
#include "idle-manager.h"
#include "keyboard-events.h"
#include "lockscreen-manager.h"
#include "monitor-manager.h"
#include "monitor/monitor.h"
......@@ -99,6 +100,7 @@ typedef struct
PhoshBtManager *bt_manager;
PhoshWWan *wwan;
PhoshTorchManager *torch_manager;
PhoshKeyboardEvents *keyboard_events;
/* sensors */
PhoshSensorProxyManager *sensor_proxy_manager;
......@@ -329,6 +331,7 @@ phosh_shell_dispose (GObject *object)
g_clear_object (&priv->notification_banner);
g_clear_object (&priv->keyboard_events);
/* dispose managers in opposite order of declaration */
g_clear_object (&priv->torch_manager);
g_clear_object (&priv->wwan);
......@@ -617,6 +620,8 @@ phosh_shell_constructed (GObject *object)
self);
}
priv->keyboard_events = phosh_keyboard_events_new ();
g_idle_add ((GSourceFunc) setup_idle_cb, self);
}
......@@ -1077,3 +1082,22 @@ phosh_shell_is_startup_finished(PhoshShell *self)
return priv->startup_finished;
}
void
phosh_shell_add_global_keyboard_action_entries (PhoshShell *self,
const GActionEntry *entries,
gint n_entries,
gpointer user_data)
{
PhoshShellPrivate *priv;
g_return_if_fail (PHOSH_IS_SHELL (self));
priv = phosh_shell_get_instance_private (self);
g_return_if_fail (priv->keyboard_events);
g_action_map_add_action_entries (G_ACTION_MAP (priv->keyboard_events),
entries,
n_entries,
user_data);
}
......@@ -58,5 +58,9 @@ void phosh_shell_fade_out (PhoshShell *self, guint timeout);
void phosh_shell_enable_power_save (PhoshShell *self, gboolean enable);
gboolean phosh_shell_started_by_display_manager(PhoshShell *self);
gboolean phosh_shell_is_startup_finished (PhoshShell *self);
void phosh_shell_add_global_keyboard_action_entries (PhoshShell *self,
const GActionEntry *actions,
gint n_entries,
gpointer user_data);
G_END_DECLS
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment