wacom: Clean up CcWacomButtonRow

We now only care of pad buttons, so this code can be quite simplified.
This commit is contained in:
Carlos Garnacho 2016-06-23 16:48:34 +02:00
parent f06203a5b2
commit d118070a73
2 changed files with 41 additions and 222 deletions

View file

@ -25,10 +25,8 @@
G_DEFINE_TYPE (CcWacomButtonRow, cc_wacom_button_row, GTK_TYPE_LIST_BOX_ROW) G_DEFINE_TYPE (CcWacomButtonRow, cc_wacom_button_row, GTK_TYPE_LIST_BOX_ROW)
#define ACTION_TYPE_KEY "action-type" #define ACTION_KEY "action"
#define CUSTOM_ACTION_KEY "custom-action" #define KEYBINDING_KEY "keybinding"
#define KEY_CUSTOM_ELEVATOR_ACTION "custom-elevator-action"
#define OLED_LABEL "oled-label"
#define WACOM_C(x) g_dpgettext2(NULL, "Wacom action-type", x) #define WACOM_C(x) g_dpgettext2(NULL, "Wacom action-type", x)
@ -39,14 +37,15 @@ enum {
}; };
struct _CcWacomButtonRowPrivate { struct _CcWacomButtonRowPrivate {
GsdWacomTabletButton *button; guint button;
GSettings *settings;
GtkDirectionType direction; GtkDirectionType direction;
GtkComboBox *action_combo; GtkComboBox *action_combo;
GsdWacomKeyShortcutButton *key_shortcut_button; GsdWacomKeyShortcutButton *key_shortcut_button;
}; };
static GtkWidget * static GtkWidget *
create_actions_combo (GsdWacomTabletButtonType type) create_actions_combo (void)
{ {
GtkListStore *model; GtkListStore *model;
GtkTreeIter iter; GtkTreeIter iter;
@ -58,12 +57,6 @@ create_actions_combo (GsdWacomTabletButtonType type)
for (i = 0; i < G_N_ELEMENTS (action_table); i++) for (i = 0; i < G_N_ELEMENTS (action_table); i++)
{ {
if ((type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
type == WACOM_TABLET_BUTTON_TYPE_RING) &&
action_table[i].action_type != GSD_WACOM_ACTION_TYPE_NONE &&
action_table[i].action_type != GSD_WACOM_ACTION_TYPE_CUSTOM)
continue;
gtk_list_store_append (model, &iter); gtk_list_store_append (model, &iter);
gtk_list_store_set (model, &iter, gtk_list_store_set (model, &iter,
ACTION_NAME_COLUMN, WACOM_C(action_table[i].action_name), ACTION_NAME_COLUMN, WACOM_C(action_table[i].action_name),
@ -82,50 +75,19 @@ create_actions_combo (GsdWacomTabletButtonType type)
} }
static void static void
cc_wacom_button_row_update_shortcut (CcWacomButtonRow *row, cc_wacom_button_row_update_shortcut (CcWacomButtonRow *row,
GsdWacomActionType action_type) GDesktopPadButtonAction action_type)
{ {
CcWacomButtonRowPrivate *priv; CcWacomButtonRowPrivate *priv;
GsdWacomTabletButton *button;
GtkDirectionType dir;
guint keyval; guint keyval;
GdkModifierType mask; GdkModifierType mask;
char *shortcut; char *shortcut;
if (action_type != GSD_WACOM_ACTION_TYPE_CUSTOM) if (action_type != G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING)
return; return;
priv = row->priv; priv = row->priv;
shortcut = g_settings_get_string (row->priv->settings, KEYBINDING_KEY);
button = priv->button;
dir = priv->direction;
if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
button->type == WACOM_TABLET_BUTTON_TYPE_RING)
{
char *str;
char **strv;
strv = g_settings_get_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION);
if (strv != NULL)
{
if (dir == GTK_DIR_UP)
str = strv[0];
else
str = strv[1];
shortcut = g_strdup (str);
g_strfreev (strv);
}
else
{
shortcut = NULL;
}
}
else
shortcut = g_settings_get_string (button->settings, CUSTOM_ACTION_KEY);
if (shortcut != NULL) if (shortcut != NULL)
{ {
@ -140,38 +102,14 @@ cc_wacom_button_row_update_shortcut (CcWacomButtonRow *row,
} }
} }
static gchar *
get_tablet_dir_button_shortcut (GsdWacomTabletButton *button,
GtkDirectionType dir)
{
char *str, **strv;
char *shortcut = NULL;
strv = g_settings_get_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION);
if (strv != NULL)
{
if (dir == GTK_DIR_UP)
str = strv[0];
else
str = strv[1];
shortcut = g_strdup (str);
g_strfreev (strv);
}
return shortcut;
}
static void static void
cc_wacom_button_row_update_action (CcWacomButtonRow *row, cc_wacom_button_row_update_action (CcWacomButtonRow *row,
GsdWacomActionType action_type) GDesktopPadButtonAction action_type)
{ {
CcWacomButtonRowPrivate *priv; CcWacomButtonRowPrivate *priv;
GtkTreeIter iter; GtkTreeIter iter;
gboolean iter_valid; gboolean iter_valid;
GsdWacomActionType current_action_type, real_action_type; GDesktopPadButtonAction current_action_type, real_action_type;
GtkTreeModel *model; GtkTreeModel *model;
priv = row->priv; priv = row->priv;
@ -179,19 +117,6 @@ cc_wacom_button_row_update_action (CcWacomButtonRow *row,
model = gtk_combo_box_get_model (priv->action_combo); model = gtk_combo_box_get_model (priv->action_combo);
real_action_type = action_type; real_action_type = action_type;
if (priv->button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
priv->button->type == WACOM_TABLET_BUTTON_TYPE_RING)
{
char *shortcut;
shortcut = get_tablet_dir_button_shortcut (priv->button, priv->direction);
if (shortcut == NULL || g_strcmp0 (shortcut, "") == 0)
real_action_type = GSD_WACOM_ACTION_TYPE_NONE;
g_free (shortcut);
}
for (iter_valid = gtk_tree_model_get_iter_first (model, &iter); iter_valid; for (iter_valid = gtk_tree_model_get_iter_first (model, &iter); iter_valid;
iter_valid = gtk_tree_model_iter_next (model, &iter)) iter_valid = gtk_tree_model_iter_next (model, &iter))
{ {
@ -211,123 +136,38 @@ static void
cc_wacom_button_row_update (CcWacomButtonRow *row) cc_wacom_button_row_update (CcWacomButtonRow *row)
{ {
CcWacomButtonRowPrivate *priv; CcWacomButtonRowPrivate *priv;
GsdWacomTabletButton *button; GDesktopPadButtonAction current_action_type;
GsdWacomActionType current_action_type;
priv = row->priv; priv = row->priv;
button = priv->button; current_action_type = g_settings_get_enum (priv->settings, ACTION_KEY);
current_action_type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
cc_wacom_button_row_update_shortcut (row, current_action_type); cc_wacom_button_row_update_shortcut (row, current_action_type);
cc_wacom_button_row_update_action (row, current_action_type); cc_wacom_button_row_update_action (row, current_action_type);
gtk_widget_set_sensitive (GTK_WIDGET (row->priv->key_shortcut_button), gtk_widget_set_sensitive (GTK_WIDGET (row->priv->key_shortcut_button),
current_action_type == GSD_WACOM_ACTION_TYPE_CUSTOM); current_action_type == G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
} }
static void static void
assign_custom_key_to_dir_button (CcWacomButtonRow *row, change_button_action_type (CcWacomButtonRow *row,
gchar *custom_key) GDesktopPadButtonAction type)
{ {
GsdWacomTabletButton *button; g_settings_set_enum (row->priv->settings, ACTION_KEY, type);
GtkDirectionType dir;
char *strs[3];
char **strv;
button = row->priv->button;
dir = row->priv->direction;
strs[2] = NULL;
strs[0] = strs[1] = "";
strv = g_settings_get_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION);
if (strv != NULL)
{
if (g_strv_length (strv) >= 1)
strs[0] = strv[0];
if (g_strv_length (strv) >= 2)
strs[1] = strv[1];
}
if (dir == GTK_DIR_UP)
strs[0] = custom_key;
else
strs[1] = custom_key;
g_settings_set_strv (button->settings,
KEY_CUSTOM_ELEVATOR_ACTION,
(const gchar * const*) strs);
g_clear_pointer (&strv, g_strfreev);
}
static void
change_button_action_type (CcWacomButtonRow *row,
GsdWacomActionType type)
{
GsdWacomTabletButton *button;
GsdWacomActionType current_type;
button = row->priv->button;
if (button == NULL)
return;
current_type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
button->type == WACOM_TABLET_BUTTON_TYPE_RING)
{
if (type == GSD_WACOM_ACTION_TYPE_NONE)
assign_custom_key_to_dir_button (row, "");
else if (type == GSD_WACOM_ACTION_TYPE_CUSTOM)
{
guint keyval;
GdkModifierType mask;
char *custom_key;
g_object_get (row->priv->key_shortcut_button,
"key-value", &keyval,
"key-mods", &mask,
NULL);
mask &= ~GDK_LOCK_MASK;
custom_key = gtk_accelerator_name (keyval, mask);
assign_custom_key_to_dir_button (row, custom_key);
g_settings_set_enum (button->settings, ACTION_TYPE_KEY, type);
g_free (custom_key);
}
}
else if (current_type != type)
{
g_settings_set_enum (button->settings, ACTION_TYPE_KEY, type);
}
gtk_widget_set_sensitive (GTK_WIDGET (row->priv->key_shortcut_button), gtk_widget_set_sensitive (GTK_WIDGET (row->priv->key_shortcut_button),
type == GSD_WACOM_ACTION_TYPE_CUSTOM); type == G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
} }
static void static void
on_key_shortcut_edited (GsdWacomKeyShortcutButton *shortcut_button, on_key_shortcut_edited (GsdWacomKeyShortcutButton *shortcut_button,
CcWacomButtonRow *row) CcWacomButtonRow *row)
{ {
GsdWacomTabletButton *button;
char *custom_key; char *custom_key;
guint keyval; guint keyval;
GdkModifierType mask; GdkModifierType mask;
button = row->priv->button; change_button_action_type (row, G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING);
if (button == NULL)
return;
change_button_action_type (row, GSD_WACOM_ACTION_TYPE_CUSTOM);
g_object_get (row->priv->key_shortcut_button, g_object_get (row->priv->key_shortcut_button,
"key-value", &keyval, "key-value", &keyval,
@ -338,13 +178,7 @@ on_key_shortcut_edited (GsdWacomKeyShortcutButton *shortcut_button,
custom_key = gtk_accelerator_name (keyval, mask); custom_key = gtk_accelerator_name (keyval, mask);
if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP || g_settings_set_string (row->priv->settings, KEYBINDING_KEY, custom_key);
button->type == WACOM_TABLET_BUTTON_TYPE_RING)
{
assign_custom_key_to_dir_button (row, custom_key);
}
else
g_settings_set_string (button->settings, CUSTOM_ACTION_KEY, custom_key);
g_free (custom_key); g_free (custom_key);
} }
@ -353,15 +187,15 @@ static void
on_key_shortcut_cleared (GsdWacomKeyShortcutButton *key_shortcut_button, on_key_shortcut_cleared (GsdWacomKeyShortcutButton *key_shortcut_button,
CcWacomButtonRow *row) CcWacomButtonRow *row)
{ {
change_button_action_type (row, GSD_WACOM_ACTION_TYPE_NONE); change_button_action_type (row, G_DESKTOP_PAD_BUTTON_ACTION_NONE);
cc_wacom_button_row_update_action (row, GSD_WACOM_ACTION_TYPE_NONE); cc_wacom_button_row_update_action (row, G_DESKTOP_PAD_BUTTON_ACTION_NONE);
} }
static void static void
on_row_action_combo_box_changed (GtkComboBox *combo, on_row_action_combo_box_changed (GtkComboBox *combo,
CcWacomButtonRow *row) CcWacomButtonRow *row)
{ {
GsdWacomActionType type; GDesktopPadButtonAction type;
GtkTreeModel *model; GtkTreeModel *model;
GtkListBox *list_box; GtkListBox *list_box;
GtkTreeIter iter; GtkTreeIter iter;
@ -410,46 +244,32 @@ cc_wacom_button_row_init (CcWacomButtonRow *button_row)
} }
GtkWidget * GtkWidget *
cc_wacom_button_row_new (GsdWacomTabletButton *button, cc_wacom_button_row_new (guint button,
GtkDirectionType dir) GSettings *settings)
{ {
GtkWidget *row; GtkWidget *row;
GtkWidget *grid, *combo, *label, *shortcut_button; GtkWidget *grid, *combo, *label, *shortcut_button;
CcWacomButtonRowPrivate *priv; CcWacomButtonRowPrivate *priv;
char *dir_name = NULL; char *name = NULL;
row = g_object_new (CC_WACOM_TYPE_BUTTON_ROW, NULL); row = g_object_new (CC_WACOM_TYPE_BUTTON_ROW, NULL);
priv = CC_WACOM_BUTTON_ROW (row)->priv; priv = CC_WACOM_BUTTON_ROW (row)->priv;
priv->button = button; priv->button = button;
priv->direction = dir; priv->settings = g_object_ref (settings);
grid = gtk_grid_new (); grid = gtk_grid_new ();
gtk_widget_show (grid); gtk_widget_show (grid);
gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE); gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE); gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
if (dir == GTK_DIR_UP || dir == GTK_DIR_DOWN) name = g_strdup_printf (_("Button %d"), button);
{ label = gtk_label_new (name);
if (button->type == WACOM_TABLET_BUTTON_TYPE_RING)
{
dir_name = g_strdup_printf ("%s (%s)",
button->name,
dir == GTK_DIR_UP ? "" : "");
} else {
dir_name = g_strdup_printf ("%s (%s)",
button->name,
dir == GTK_DIR_UP ? C_("Wacom tablet button", "Up") :
C_("Wacom tablet button", "Down"));
}
}
label = gtk_label_new (dir_name ? dir_name : button->name);
g_object_set (label, "halign", GTK_ALIGN_START, NULL); g_object_set (label, "halign", GTK_ALIGN_START, NULL);
gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1); gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
gtk_widget_show (label); gtk_widget_show (label);
combo = create_actions_combo (button->type); combo = create_actions_combo ();
gtk_grid_attach (GTK_GRID (grid), combo, 1, 0, 1, 1); gtk_grid_attach (GTK_GRID (grid), combo, 1, 0, 1, 1);
gtk_widget_show (combo); gtk_widget_show (combo);
priv->action_combo = GTK_COMBO_BOX (combo); priv->action_combo = GTK_COMBO_BOX (combo);
@ -475,7 +295,7 @@ cc_wacom_button_row_new (GsdWacomTabletButton *button,
cc_wacom_button_row_update (CC_WACOM_BUTTON_ROW (row)); cc_wacom_button_row_update (CC_WACOM_BUTTON_ROW (row));
g_free (dir_name); g_free (name);
return row; return row;
} }

View file

@ -21,8 +21,7 @@
#define __CC_WACOM_BUTTON_ROW_H__ #define __CC_WACOM_BUTTON_ROW_H__
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gdesktop-enums.h>
#include "gsd-wacom-device.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -48,19 +47,19 @@ struct _CcWacomButtonRowClass {
}; };
static struct { static struct {
GsdWacomActionType action_type; GDesktopPadButtonAction action_type;
const gchar *action_name; const gchar *action_name;
} action_table[] = { } action_table[] = {
{ GSD_WACOM_ACTION_TYPE_NONE, NC_("Wacom action-type", "None") }, { G_DESKTOP_PAD_BUTTON_ACTION_NONE, NC_("Wacom action-type", "Application defined") },
{ GSD_WACOM_ACTION_TYPE_CUSTOM, NC_("Wacom action-type", "Send Keystroke") }, { G_DESKTOP_PAD_BUTTON_ACTION_KEYBINDING, NC_("Wacom action-type", "Send Keystroke") },
{ GSD_WACOM_ACTION_TYPE_SWITCH_MONITOR, NC_("Wacom action-type", "Switch Monitor") }, { G_DESKTOP_PAD_BUTTON_ACTION_SWITCH_MONITOR, NC_("Wacom action-type", "Switch Monitor") },
{ GSD_WACOM_ACTION_TYPE_HELP, NC_("Wacom action-type", "Show On-Screen Help") } { G_DESKTOP_PAD_BUTTON_ACTION_HELP, NC_("Wacom action-type", "Show On-Screen Help") }
}; };
GType cc_wacom_button_row_get_type (void); GType cc_wacom_button_row_get_type (void);
GtkWidget * cc_wacom_button_row_new (GsdWacomTabletButton *button, GtkWidget * cc_wacom_button_row_new (guint button,
GtkDirectionType dir); GSettings *settings);
G_END_DECLS G_END_DECLS