From 495ce3edfb9e13065421ddddbf93f86008f701fe Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Fri, 5 Aug 2011 21:53:33 -0400 Subject: [PATCH] Add ObConfigValue types for a keyboard and mouse button (with modifiers). Make the copy process return FALSE if the value cannot be copied successfully. Make the enum, key, and mouse functions return FALSE if the ObConfigValue isn't valid. --- openbox/config_value.c | 62 +++++++++++++++++++++++++++++++----------- openbox/config_value.h | 24 ++++++++++------ 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/openbox/config_value.c b/openbox/config_value.c index b871ec0c..99ce4bfe 100644 --- a/openbox/config_value.c +++ b/openbox/config_value.c @@ -19,6 +19,9 @@ #include "config_value.h" #include "action_list.h" #include "geom.h" +#include "keyboard.h" +#include "mouse.h" +#include "translate.h" #include "stdlib.h" @@ -81,11 +84,13 @@ gboolean config_value_is_action_list(const ObConfigValue *v) /**************************** pointer functions ****************************/ -void config_value_copy_ptr(ObConfigValue *v, - ObConfigValueDataType type, - ObConfigValueDataPtr p, - const ObConfigValueEnum e[]) +gboolean config_value_copy_ptr(ObConfigValue *v, + ObConfigValueDataType type, + ObConfigValueDataPtr p, + const ObConfigValueEnum e[]) { + gboolean ok = TRUE; + switch (type) { case OB_CONFIG_VALUE_STRING: *p.string = config_value_string(v); @@ -97,7 +102,7 @@ void config_value_copy_ptr(ObConfigValue *v, *p.integer = config_value_int(v); break; case OB_CONFIG_VALUE_ENUM: - *p.enumeration = config_value_enum(v, e); + ok = config_value_enum(v, e, p.enumeration); break; case OB_CONFIG_VALUE_FRACTION: { gint n, d; @@ -107,11 +112,15 @@ void config_value_copy_ptr(ObConfigValue *v, break; } case OB_CONFIG_VALUE_GRAVITY_COORD: { - GravityCoord c; - config_value_gravity_coord(v, &c); - *p.coord = c; + config_value_gravity_coord(v, p.coord); break; } + case OB_CONFIG_VALUE_KEY: + ok = config_value_key(v, p.key); + break; + case OB_CONFIG_VALUE_BUTTON: + ok = config_value_button(v, p.button); + break; case OB_CONFIG_VALUE_STRING_LIST: { *p.list = config_value_string_list(v); break; @@ -120,9 +129,9 @@ void config_value_copy_ptr(ObConfigValue *v, *p.actions = config_value_action_list(v); break; case NUM_OB_CONFIG_VALUE_TYPES: - default: g_assert_not_reached(); } + return ok; } @@ -149,18 +158,21 @@ guint config_value_int(ObConfigValue *v) s = v->v.string; return strtol(s, &s, 10); } -guint config_value_enum(ObConfigValue *v, const ObConfigValueEnum choices[]) +gboolean config_value_enum(ObConfigValue *v, const ObConfigValueEnum choices[], + guint *out) { const ObConfigValueEnum *e; - g_return_val_if_fail(v != NULL, (guint)-1); - g_return_val_if_fail(config_value_is_string(v), (guint)-1); - g_return_val_if_fail(choices != NULL, (guint)-1); + g_return_val_if_fail(v != NULL, FALSE); + g_return_val_if_fail(config_value_is_string(v), FALSE); + g_return_val_if_fail(choices != NULL, FALSE); for (e = choices; e->name; ++e) - if (g_strcasecmp(v->v.string, e->name) == 0) - return e->value; - return (guint)-1; + if (g_strcasecmp(v->v.string, e->name) == 0) { + *out = e->value; + return TRUE; + } + return FALSE; } void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom) { @@ -208,6 +220,24 @@ void config_value_gravity_coord(ObConfigValue *v, GravityCoord *c) c->denom = atoi(s+1); } } +gboolean config_value_key(ObConfigValue *v, struct _ObKeyboardKey *k) +{ + k->modifiers = 0; + k->keycode = 0; + + g_return_val_if_fail(v != NULL, FALSE); + g_return_val_if_fail(config_value_is_string(v), FALSE); + return translate_key(v->v.string, &k->modifiers, &k->keycode); +} +gboolean config_value_button(ObConfigValue *v, struct _ObMouseButton *b) +{ + b->modifiers = 0; + b->button = 0; + + g_return_val_if_fail(v != NULL, FALSE); + g_return_val_if_fail(config_value_is_string(v), FALSE); + return translate_button(v->v.string, &b->modifiers, &b->button); +} const gchar *const* config_value_string_list(ObConfigValue *v) { g_return_val_if_fail(v != NULL, NULL); diff --git a/openbox/config_value.h b/openbox/config_value.h index d1123341..f4ffd6e6 100644 --- a/openbox/config_value.h +++ b/openbox/config_value.h @@ -16,8 +16,6 @@ See the COPYING file for a copy of the GNU General Public License. */ -#include "geom.h" - #include struct _GravityCoord; @@ -38,7 +36,10 @@ typedef enum { OB_CONFIG_VALUE_INTEGER, OB_CONFIG_VALUE_FRACTION, OB_CONFIG_VALUE_GRAVITY_COORD, + OB_CONFIG_VALUE_KEY, + OB_CONFIG_VALUE_BUTTON, OB_CONFIG_VALUE_STRING_LIST, + OB_CONFIG_VALUE_LIST, OB_CONFIG_VALUE_ACTIONLIST, NUM_OB_CONFIG_VALUE_TYPES } ObConfigValueDataType; @@ -55,6 +56,8 @@ typedef union _ObConfigValueDataPtr { guint denom; } *fraction; struct _GravityCoord *coord; + struct _ObKeyboardKey *key; + struct _ObMouseButton *button; const gchar *const**list; struct _ObActionList **actions; } ObConfigValueDataPtr; @@ -83,20 +86,25 @@ gboolean config_value_is_action_list(const ObConfigValue *v); /*! Copies the data inside @v to the destination that the pointer @p is pointing to. */ -void config_value_copy_ptr(ObConfigValue *v, - ObConfigValueDataType type, - ObConfigValueDataPtr p, - const ObConfigValueEnum e[]); +gboolean config_value_copy_ptr(ObConfigValue *v, + ObConfigValueDataType type, + ObConfigValueDataPtr p, + const ObConfigValueEnum e[]); /* These ones are valid on a string value */ const gchar* config_value_string(ObConfigValue *v); gboolean config_value_bool(ObConfigValue *v); guint config_value_int(ObConfigValue *v); -/*! returns (guint)-1 if an error */ -guint config_value_enum(ObConfigValue *v, const ObConfigValueEnum e[]); +/*! returns FALSE if the value isn't in the enumeration */ +gboolean config_value_enum(ObConfigValue *v, const ObConfigValueEnum e[], + guint *out); void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom); void config_value_gravity_coord(ObConfigValue *v, struct _GravityCoord *c); +/*! returns FALSE if the key string isn't valid */ +gboolean config_value_key(ObConfigValue *v, struct _ObKeyboardKey *k); +/*! returns FALSE if the button string isn't valid */ +gboolean config_value_button(ObConfigValue *v, struct _ObMouseButton *b); /* These ones are valid on a string list value */ -- 2.34.1