summaryrefslogtreecommitdiff
path: root/src/option.cpp
diff options
context:
space:
mode:
authorDennis Kasprzyk <onestone@opencompositing.org>2008-08-20 01:43:20 +0200
committerDennis kasprzyk <onestone@opencompositing.org>2008-08-20 01:43:20 +0200
commit94de9e6dcc21bf9cd747138da2ba7029e7045716 (patch)
treed757557ff77ef9052fa5dee4eea626bf955c167d /src/option.cpp
parent79d78d3f60e683411a075fe44fe4ab862d0fadc6 (diff)
downloadunity-window-decorator-94de9e6dcc21bf9cd747138da2ba7029e7045716.tar.gz
unity-window-decorator-94de9e6dcc21bf9cd747138da2ba7029e7045716.tar.bz2
Port of CompOption and CompAction to C++.
Diffstat (limited to 'src/option.cpp')
-rw-r--r--src/option.cpp1336
1 files changed, 622 insertions, 714 deletions
diff --git a/src/option.cpp b/src/option.cpp
index 87bbb3c..bfda243 100644
--- a/src/option.cpp
+++ b/src/option.cpp
@@ -29,937 +29,845 @@
#include <ctype.h>
#include <math.h>
+#include <boost/foreach.hpp>
+#define foreach BOOST_FOREACH
+
#include <compiz-core.h>
+#include <compoption.h>
+#include "privateoption.h"
-struct _Modifier {
- char *name;
- int modifier;
-} modifiers[] = {
- { "<Shift>", ShiftMask },
- { "<Control>", ControlMask },
- { "<Mod1>", Mod1Mask },
- { "<Mod2>", Mod2Mask },
- { "<Mod3>", Mod3Mask },
- { "<Mod4>", Mod4Mask },
- { "<Mod5>", Mod5Mask },
- { "<Alt>", CompAltMask },
- { "<Meta>", CompMetaMask },
- { "<Super>", CompSuperMask },
- { "<Hyper>", CompHyperMask },
- { "<ModeSwitch>", CompModeSwitchMask }
-};
-
-#define N_MODIFIERS (sizeof (modifiers) / sizeof (struct _Modifier))
-
-struct _Edge {
- char *name;
- char *modifierName;
-} edges[] = {
- { "Left", "<LeftEdge>" },
- { "Right", "<RightEdge>" },
- { "Top", "<TopEdge>" },
- { "Bottom", "<BottomEdge>" },
- { "TopLeft", "<TopLeftEdge>" },
- { "TopRight", "<TopRightEdge>" },
- { "BottomLeft", "<BottomLeftEdge>" },
- { "BottomRight", "<BottomRightEdge>" }
-};
+CompOption::Vector noOptions (0);
-void
-compInitOptionValue (CompOptionValue *v)
+CompOption::Value::Value () :
+ priv (new PrivateValue ())
{
- memset (v, 0, sizeof (CompOptionValue));
}
-void
-compFiniOptionValue (CompOptionValue *v,
- CompOptionType type)
+CompOption::Value::Value (const Value &v) :
+ priv (new PrivateValue (*v.priv))
{
- int i;
-
- switch (type) {
- case CompOptionTypeString:
- if (v->s)
- free (v->s);
- break;
- case CompOptionTypeMatch:
- delete (v->match);
- v->match = NULL;
- break;
- case CompOptionTypeList:
- for (i = 0; i < v->list.nValue; i++)
- compFiniOptionValue (&v->list.value[i], v->list.type);
-
- if (v->list.value)
- free (v->list.value);
- break;
- default:
- break;
- }
}
-void
-compInitOption (CompOption *o)
+CompOption::Value::~Value ()
{
- memset (o, 0, sizeof (CompOption));
+ delete priv;
}
-void
-compFiniOption (CompOption *o)
+CompOption::Value::Value (const bool b) :
+ priv (new PrivateValue ())
{
- compFiniOptionValue (&o->value, o->type);
+ set (b);
}
-CompOption *
-compFindOption (CompOption *option,
- int nOption,
- const char *name,
- int *index)
+CompOption::Value::Value (const int i) :
+ priv (new PrivateValue ())
{
- int i;
-
- for (i = 0; i < nOption; i++)
- {
- if (strcmp (option[i].name, name) == 0)
- {
- if (index)
- *index = i;
+ set (i);
+}
- return &option[i];
- }
- }
+CompOption::Value::Value (const float f) :
+ priv (new PrivateValue ())
+{
+ set (f);
+}
- return 0;
+CompOption::Value::Value (const unsigned short *color) :
+ priv (new PrivateValue ())
+{
+ set (color);
}
-Bool
-compSetBoolOption (CompOption *option,
- CompOptionValue *value)
+CompOption::Value::Value (const CompString& s) :
+ priv (new PrivateValue ())
{
- int b;
+ set (s);
+}
- b = (value->b) ? TRUE : FALSE;
+CompOption::Value::Value (const char *s) :
+ priv (new PrivateValue ())
+{
+ set (s);
+}
- if (option->value.b == b)
- return FALSE;
- option->value.b = b;
+CompOption::Value::Value (const CompMatch& m) :
+ priv (new PrivateValue ())
+{
+ set (m);
+}
- return TRUE;
+CompOption::Value::Value (const CompAction& a) :
+ priv (new PrivateValue ())
+{
+ set (a);
}
-Bool
-compSetIntOption (CompOption *option,
- CompOptionValue *value)
+CompOption::Value::Value (CompOption::Type type, const Vector& l) :
+ priv (new PrivateValue ())
{
- if (value->i < option->rest.i.min ||
- value->i > option->rest.i.max ||
- value->i == option->value.i)
- return FALSE;
+ set (type, l);
+}
- option->value.i = value->i;
- return TRUE;
+void
+CompOption::Value::set (const bool b)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeBool;
+ priv->value.b = b;
}
-Bool
-compSetFloatOption (CompOption *option,
- CompOptionValue *value)
+void
+CompOption::Value::set (const int i)
{
- float v, p;
-
- /* Workaround for float rounding errors */
- static float equalRange = 1e-5;
-
- int sign = (value->f < 0 ? -1 : 1);
+ priv->reset ();
+ priv->type = CompOption::TypeInt;
+ priv->value.i = i;
+}
- p = 1.0f / option->rest.f.precision;
- v = ((int) (value->f * p + sign * 0.5f)) / p;
+void
+CompOption::Value::set (const float f)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeFloat;
+ priv->value.f = f;
+}
- if (v < (option->rest.f.min - equalRange) ||
- v > (option->rest.f.max + equalRange))
- return FALSE;
+void
+CompOption::Value::set (const unsigned short *color)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeColor;
+ priv->value.c[0] = color[0];
+ priv->value.c[1] = color[1];
+ priv->value.c[2] = color[2];
+ priv->value.c[3] = color[3];
+}
- if (v > (option->value.f - equalRange) &&
- v < (option->value.f + equalRange))
- return FALSE;
+void
+CompOption::Value::set (const CompString& s)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeString;
+ priv->string = s;
+}
- option->value.f = v;
+void
+CompOption::Value::set (const char *s)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeString;
+ priv->string = CompString (s);
+}
- return TRUE;
+void
+CompOption::Value::set (const CompMatch& m)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeMatch;
+ priv->match = m;
}
-Bool
-compSetStringOption (CompOption *option,
- CompOptionValue *value)
+void
+CompOption::Value::set (const CompAction& a)
{
- char *s;
+ priv->reset ();
+ priv->type = CompOption::TypeAction;
+ priv->action = a;
+}
- s = value->s;
- if (!s)
- s = "";
+void
+CompOption::Value::set (CompOption::Type type, const Vector& l)
+{
+ priv->reset ();
+ priv->type = CompOption::TypeList;
+ priv->list = l;
+ priv->listType = type;
+}
- if (option->value.s == s)
- return FALSE;
- if (option->value.s && s)
- {
- if (strcmp (option->value.s, s) == 0)
- return FALSE;
- }
+bool
+CompOption::Value::b ()
+{
+ if (priv->type != CompOption::TypeBool)
+ return false;
+ return priv->value.b;
+}
- if (option->value.s)
- free (option->value.s);
+int
+CompOption::Value::i ()
+{
+ if (priv->type != CompOption::TypeInt)
+ return 0;
+ return priv->value.i;
+}
- option->value.s = strdup (s);
+float
+CompOption::Value::f ()
+{
+ if (priv->type != CompOption::TypeFloat)
+ return 0.0;
+ return priv->value.f;
+}
- return TRUE;
+unsigned short*
+CompOption::Value::c ()
+{
+ if (priv->type != CompOption::TypeColor)
+ return reinterpret_cast<unsigned short *> (&defaultColor);
+ return priv->value.c;
}
-Bool
-compSetColorOption (CompOption *option,
- CompOptionValue *value)
+CompString
+CompOption::Value::s ()
{
- if (memcmp (value->c, option->value.c, sizeof (value->c)) == 0)
- return FALSE;
+ return priv->string;
+}
- memcpy (option->value.c, value->c, sizeof (value->c));
+CompMatch &
+CompOption::Value::match ()
+{
+ return priv->match;
+}
- return TRUE;
+CompAction &
+CompOption::Value::action ()
+{
+ return priv->action;
}
-Bool
-compSetActionOption (CompOption *option,
- CompOptionValue *value)
+CompOption::Type
+CompOption::Value::listType ()
{
- CompAction *action = &option->value.action;
- CompOptionValue v = *value;
+ return priv->listType;
+}
- /* initiate, terminate, priv and state should never be changed */
- v.action.initiate = action->initiate;
- v.action.terminate = action->terminate;
- v.action.state = action->state;
- v.action.priv = action->priv;
+CompOption::Value::Vector &
+CompOption::Value::list ()
+{
+ return priv->list;
+}
- if (action->type == v.action.type)
+bool
+CompOption::Value::operator== (const CompOption::Value &val)
+{
+ if (priv->type != val.priv->type)
+ return false;
+ switch (priv->type)
{
- switch (option->type) {
- case CompOptionTypeKey:
- if (!(action->type & CompBindingTypeKey))
- return FALSE;
-
- if (action->key.keycode == v.action.key.keycode &&
- action->key.modifiers == v.action.key.modifiers)
- return FALSE;
+ case CompOption::TypeBool:
+ return priv->value.b == val.priv->value.b;
+ break;
+ case CompOption::TypeInt:
+ return priv->value.i == val.priv->value.i;
+ break;
+ case CompOption::TypeFloat:
+ return priv->value.f == val.priv->value.f;
break;
- case CompOptionTypeButton:
- if (!(action->type & (CompBindingTypeButton |
- CompBindingTypeEdgeButton)))
- return FALSE;
-
- if (action->type & CompBindingTypeEdgeButton)
- {
- if (action->button.button == v.action.button.button &&
- action->button.modifiers == v.action.button.modifiers &&
- action->edgeMask == v.action.edgeMask)
- return FALSE;
- }
- else if (action->type & CompBindingTypeButton)
- {
- if (action->button.button == v.action.button.button &&
- action->button.modifiers == v.action.button.modifiers)
- return FALSE;
- }
+ case CompOption::TypeColor:
+ return (priv->value.c[0] == val.priv->value.c[0]) &&
+ (priv->value.c[1] == val.priv->value.c[1]) &&
+ (priv->value.c[2] == val.priv->value.c[2]) &&
+ (priv->value.c[3] == val.priv->value.c[3]);
break;
- case CompOptionTypeEdge:
- if (v.action.edgeMask == action->edgeMask)
- return FALSE;
+ case CompOption::TypeString:
+ return priv->string.compare (val.priv->string) == 0;
break;
- case CompOptionTypeBell:
- if (v.action.bell == action->bell)
- return FALSE;
+ case CompOption::TypeMatch:
+ return priv->match == val.priv->match;
+ break;
+ case CompOption::TypeAction:
+ return priv->action == val.priv->action;
+ break;
+ case CompOption::TypeList:
+ if (priv->listType != val.priv->listType)
+ return false;
+ if (priv->list.size () != val.priv->list.size ())
+ return false;
+ for (unsigned int i = 0; i < priv->list.size (); i++)
+ if (priv->list[i] != val.priv->list[i])
+ return false;
+ return true;
break;
default:
- return FALSE;
- }
+ break;
}
- *action = v.action;
-
- return TRUE;
+ return true;
}
-Bool
-compSetMatchOption (CompOption *option,
- CompOptionValue *value)
+bool
+CompOption::Value::operator!= (const CompOption::Value &val)
{
- CompDisplay *display = option->value.match->display ();
- CompMatch match;
-
- if (*option->value.match == *value->match)
- return FALSE;
-
- match = *value->match;
-
- delete option->value.match;
-
- option->value.match = new CompMatch (*value->match);
-
- if (display)
- option->value.match->update (display);
-
- return TRUE;
+ return !(*this == val);
}
-Bool
-compSetOptionList (CompOption *option,
- CompOptionValue *value)
+CompOption::Value &
+CompOption::Value::operator= (const CompOption::Value &val)
{
- CompOption o;
- Bool status = FALSE;
- int i, min;
-
- if (value->list.nValue != option->value.list.nValue)
- {
- CompOptionValue *v;
-
- v = (CompOptionValue *) malloc (sizeof (CompOptionValue) * value->list.nValue);
- if (!v)
- return FALSE;
-
- min = MIN (value->list.nValue, option->value.list.nValue);
-
- for (i = min; i < option->value.list.nValue; i++)
- {
- switch (option->value.list.type) {
- case CompOptionTypeString:
- if (option->value.list.value[i].s)
- free (option->value.list.value[i].s);
- break;
- case CompOptionTypeMatch:
- delete option->value.list.value[i].match;
- option->value.list.value[i].match = NULL;
- default:
- break;
- }
- }
-
- memset (v, 0, sizeof (CompOptionValue) * value->list.nValue);
-
- if (min)
- memcpy (v, option->value.list.value,
- sizeof (CompOptionValue) * min);
-
- if (option->value.list.value)
- free (option->value.list.value);
-
- option->value.list.value = v;
- option->value.list.nValue = value->list.nValue;
-
- status = TRUE;
- }
+ delete priv;
+ priv = new PrivateValue (*val.priv);
+ return *this;
+}
- o = *option;
- o.type = option->value.list.type;
+PrivateValue::PrivateValue () :
+ type (CompOption::TypeBool),
+ string (""),
+ action (),
+ match (),
+ listType (CompOption::TypeBool),
+ list ()
+{
+ memset (&value, 0, sizeof (ValueUnion));
+}
- for (i = 0; i < value->list.nValue; i++)
- {
- o.value = option->value.list.value[i];
+PrivateValue::PrivateValue (const PrivateValue& p) :
+ type (p.type),
+ string (p.string),
+ action (p.action),
+ match (p.match),
+ listType (p.listType),
+ list (p.list)
+{
+ memcpy (&value, &p.value, sizeof (ValueUnion));
+}
- switch (o.type) {
- case CompOptionTypeBool:
- status |= compSetBoolOption (&o, &value->list.value[i]);
- break;
- case CompOptionTypeInt:
- status |= compSetIntOption (&o, &value->list.value[i]);
+void
+PrivateValue::reset ()
+{
+ switch (type) {
+ case CompOption::TypeString:
+ string = "";
break;
- case CompOptionTypeFloat:
- status |= compSetFloatOption (&o, &value->list.value[i]);
+ case CompOption::TypeMatch:
+ match = CompMatch ();
break;
- case CompOptionTypeString:
- status |= compSetStringOption (&o, &value->list.value[i]);
+ case CompOption::TypeAction:
+ action = CompAction ();
break;
- case CompOptionTypeColor:
- status |= compSetColorOption (&o, &value->list.value[i]);
+ case CompOption::TypeList:
+ list.clear ();
+ listType = CompOption::TypeBool;
break;
- case CompOptionTypeMatch:
- status |= compSetMatchOption (&o, &value->list.value[i]);
default:
break;
- }
-
- option->value.list.value[i] = o.value;
}
-
- return status;
-}
-
-Bool
-compSetOption (CompOption *option,
- CompOptionValue *value)
-{
- switch (option->type) {
- case CompOptionTypeBool:
- return compSetBoolOption (option, value);
- case CompOptionTypeInt:
- return compSetIntOption (option, value);
- case CompOptionTypeFloat:
- return compSetFloatOption (option, value);
- case CompOptionTypeString:
- return compSetStringOption (option, value);
- case CompOptionTypeColor:
- return compSetColorOption (option, value);
- case CompOptionTypeMatch:
- return compSetMatchOption (option, value);
- case CompOptionTypeAction:
- case CompOptionTypeKey:
- case CompOptionTypeButton:
- case CompOptionTypeEdge:
- case CompOptionTypeBell:
- return compSetActionOption (option, value);
- case CompOptionTypeList:
- return compSetOptionList (option, value);
- }
-
- return FALSE;
+ type = CompOption::TypeBool;
}
-Bool
-getBoolOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- Bool defaultValue)
+CompOption::Restriction::Restriction () :
+ priv (new PrivateRestriction ())
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeBool)
- if (strcmp (option->name, name) == 0)
- return option->value.b;
+}
- option++;
- }
+CompOption::Restriction::Restriction (const CompOption::Restriction::Restriction &r) :
+ priv (new PrivateRestriction (*r.priv))
+{
+}
- return defaultValue;
+CompOption::Restriction::~Restriction ()
+{
+ delete priv;
}
int
-getIntOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- int defaultValue)
+CompOption::Restriction::iMin ()
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeInt)
- if (strcmp (option->name, name) == 0)
- return option->value.i;
-
- option++;
- }
+ if (priv->type == CompOption::TypeInt)
+ return priv->rest.i.min;
+ return MINSHORT;
+}
- return defaultValue;
+int
+CompOption::Restriction::iMax ()
+{
+ if (priv->type == CompOption::TypeInt)
+ return priv->rest.i.max;
+ return MAXSHORT;
}
float
-getFloatOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- float defaultValue)
+CompOption::Restriction::fMin ()
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeFloat)
- if (strcmp (option->name, name) == 0)
- return option->value.f;
-
- option++;
- }
-
- return defaultValue;
+ if (priv->type == CompOption::TypeFloat)
+ return priv->rest.f.min;
+ return MINSHORT;
}
-char *
-getStringOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- char *defaultValue)
+float
+CompOption::Restriction::fMax ()
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeString)
- if (strcmp (option->name, name) == 0)
- return option->value.s;
-
- option++;
- }
-
- return defaultValue;
+ if (priv->type == CompOption::TypeFloat)
+ return priv->rest.f.min;
+ return MINSHORT;
}
-unsigned short *
-getColorOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- unsigned short *defaultValue)
+float
+CompOption::Restriction::fPrecision ()
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeColor)
- if (strcmp (option->name, name) == 0)
- return option->value.c;
+ if (priv->type == CompOption::TypeFloat)
+ return priv->rest.f.precision;
+ return 0.1f;
+}
- option++;
- }
- return defaultValue;
+void
+CompOption::Restriction::set (int min, int max)
+{
+ priv->type = CompOption::TypeInt;
+ priv->rest.i.min = min;
+ priv->rest.i.max = max;
}
-CompMatch *
-getMatchOptionNamed (CompOption *option,
- int nOption,
- const char *name,
- CompMatch *defaultValue)
+void
+CompOption::Restriction::set (float min, float max, float precision)
{
- while (nOption--)
- {
- if (option->type == CompOptionTypeMatch)
- if (strcmp (option->name, name) == 0)
- return option->value.match;
+ priv->type = CompOption::TypeFloat;
+ priv->rest.f.min = min;
+ priv->rest.f.max = max;
+ priv->rest.f.precision = precision;
+}
- option++;
- }
+bool
+CompOption::Restriction::inRange (int i)
+{
+ if (priv->type != CompOption::TypeInt)
+ return true;
+ if (i < priv->rest.i.min)
+ return false;
+ if (i > priv->rest.i.max)
+ return false;
+ return true;
+}
- return defaultValue;
+bool
+CompOption::Restriction::inRange (float f)
+{
+ if (priv->type != CompOption::TypeFloat)
+ return true;
+ if (f < priv->rest.f.min)
+ return false;
+ if (f > priv->rest.f.max)
+ return false;
+ return true;
}
-static char *
-stringAppend (char *s,
- const char *a)
+CompOption::Restriction &
+CompOption::Restriction::operator= (const CompOption::Restriction &rest)
{
- char *r;
- int len;
+ delete priv;
+ priv = new PrivateRestriction (*rest.priv);
+ return *this;
+}
- len = strlen (a);
- if (s)
- len += strlen (s);
+CompOption *
+CompOption::findOption (CompOption::Vector &options,
+ CompString name,
+ unsigned int *index)
+{
+ unsigned int i;
- r = (char *) malloc (len + 1);
- if (r)
+ for (i = 0; i < options.size (); i++)
{
- if (s)
+ if (options[i].priv->name.compare(name) == 0)
{
- sprintf (r, "%s%s", s, a);
- free (s);
- }
- else
- {
- sprintf (r, "%s", a);
- }
+ if (index)
+ *index = i;
- s = r;
+ return &options[i];
+ }
}
- return s;
+ return NULL;
}
-static char *
-modifiersToString (CompDisplay *d,
- unsigned int modMask)
+CompOption::CompOption () :
+ priv (new PrivateOption ())
{
- char *binding = NULL;
- int i;
-
- for (i = 0; i < N_MODIFIERS; i++)
- {
- if (modMask & modifiers[i].modifier)
- binding = stringAppend (binding, modifiers[i].name);
- }
-
- return binding;
}
-static char *
-edgeMaskToBindingString (CompDisplay *d,
- unsigned int edgeMask)
+CompOption::CompOption (const CompOption &o) :
+ priv (new PrivateOption (*o.priv))
{
- char *binding = NULL;
- int i;
-
- for (i = 0; i < SCREEN_EDGE_NUM; i++)
- if (edgeMask & (1 << i))
- binding = stringAppend (binding, edges[i].modifierName);
-
- return binding;
}
-char *
-keyBindingToString (CompDisplay *d,
- CompKeyBinding *key)
+CompOption::CompOption (CompString name, CompOption::Type type) :
+ priv (new PrivateOption ())
{
- char *binding;
-
- binding = modifiersToString (d, key->modifiers);
-
- if (key->keycode != 0)
- {
- KeySym keysym;
- char *keyname;
-
- keysym = XKeycodeToKeysym (d->dpy (), key->keycode, 0);
- keyname = XKeysymToString (keysym);
-
- if (keyname)
- {
- binding = stringAppend (binding, keyname);
- }
- else
- {
- char keyCodeStr[256];
-
- snprintf (keyCodeStr, 256, "0x%x", key->keycode);
- binding = stringAppend (binding, keyCodeStr);
- }
- }
-
- return binding;
+ setName (name, type);
}
-char *
-buttonBindingToString (CompDisplay *d,
- CompButtonBinding *button)
+CompOption::~CompOption ()
{
- char *binding;
- char buttonStr[256];
-
- binding = modifiersToString (d, button->modifiers);
-
- snprintf (buttonStr, 256, "Button%d", button->button);
- binding = stringAppend (binding, buttonStr);
+ delete priv;
+}
- return binding;
+void
+CompOption::setName (CompString name, CompOption::Type type)
+{
+ priv->name = name;
+ priv->type = type;
+}
+
+CompString
+CompOption::name ()
+{
+ return priv->name;
}
-char *
-keyActionToString (CompDisplay *d,
- CompAction *action)
+CompOption::Type
+CompOption::type ()
{
- char *binding;
+ return priv->type;
+}
- binding = keyBindingToString (d, &action->key);
- if (!binding)
- return strdup ("Disabled");
+CompOption::Value &
+CompOption::value ()
+{
+ return priv->value;
+}
- return binding;
+CompOption::Restriction &
+CompOption::rest ()
+{
+ return priv->rest;
}
-char *
-buttonActionToString (CompDisplay *d,
- CompAction *action)
+bool
+CompOption::set (CompOption::Value &val)
{
- char *binding, *edge;
- char buttonStr[256];
+ if (priv->type == CompOption::TypeKey ||
+ priv->type == CompOption::TypeButton ||
+ priv->type == CompOption::TypeEdge ||
+ priv->type == CompOption::TypeBell)
+ val.action ().copyState (priv->value.action ());
- binding = modifiersToString (d, action->button.modifiers);
- edge = edgeMaskToBindingString (d, action->edgeMask);
+ if (priv->value == val)
+ return false;
- if (edge)
+ switch (priv->type)
{
- binding = stringAppend (binding, edge);
- free (edge);
- }
+ case CompOption::TypeInt:
+ if (!priv->rest.inRange (val.i ()))
+ return false;
+ break;
+ case CompOption::TypeFloat:
+ {
+ float v, p;
+ int sign = (val.f () < 0 ? -1 : 1);
- snprintf (buttonStr, 256, "Button%d", action->button.button);
- binding = stringAppend (binding, buttonStr);
+ if (!priv->rest.inRange (val.f ()))
+ return false;
- if (!binding)
- return strdup ("Disabled");
+ p = 1.0f / priv->rest.fPrecision ();
+ v = ((int) (val.f () * p + sign * 0.5f)) / p;
- return binding;
+ priv->value.set (v);
+ return true;
+ }
+ case CompOption::TypeAction:
+ return false;
+ case CompOption::TypeKey:
+ if (!(val.action ().type () & CompAction::BindingTypeKey))
+ return false;
+ break;
+ case CompOption::TypeButton:
+ if (!(val.action ().type () & (CompAction::BindingTypeButton |
+ CompAction::BindingTypeEdgeButton)))
+ return false;
+ break;
+ default:
+ break;
+ }
+ priv->value = val;
+ return true;
}
-static unsigned int
-stringToModifiers (CompDisplay *d,
- const char *binding)
+bool
+CompOption::isAction ()
{
- unsigned int mods = 0;
- int i;
-
- for (i = 0; i < N_MODIFIERS; i++)
- {
- if (strstr (binding, modifiers[i].name))
- mods |= modifiers[i].modifier;
+ switch (priv->type) {
+ case CompOption::TypeAction:
+ case CompOption::TypeKey:
+ case CompOption::TypeButton:
+ case CompOption::TypeEdge:
+ case CompOption::TypeBell:
+ return true;
+ default:
+ break;
}
- return mods;
+ return false;
}
-static unsigned int
-bindingStringToEdgeMask (CompDisplay *d,
- const char *binding)
+CompOption &
+CompOption::operator= (const CompOption &option)
{
- unsigned int edgeMask = 0;
- int i;
-
- for (i = 0; i < SCREEN_EDGE_NUM; i++)
- if (strstr (binding, edges[i].modifierName))
- edgeMask |= 1 << i;
-
- return edgeMask;
+ delete priv;
+ priv = new PrivateOption (*option.priv);
+ return *this;
}
-Bool
-stringToKeyBinding (CompDisplay *d,
- const char *binding,
- CompKeyBinding *key)
+
+bool
+CompOption::getBoolOptionNamed (Vector &options, CompString name,
+ bool defaultValue)
{
- char *ptr;
- unsigned int mods;
- KeySym keysym;
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeBool &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.b ();
- mods = stringToModifiers (d, binding);
+ return defaultValue;
+}
- ptr = strrchr (binding, '>');
- if (ptr)
- binding = ptr + 1;
+int
+CompOption::getIntOptionNamed (Vector &options, CompString name,
+ int defaultValue)
+{
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeInt &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.i ();
- while (*binding && !isalnum (*binding))
- binding++;
+ return defaultValue;
+}
- if (!*binding)
- {
- if (mods)
- {
- key->keycode = 0;
- key->modifiers = mods;
- return TRUE;
- }
+float
+CompOption::getFloatOptionNamed (Vector &options, CompString name,
+ float defaultValue)
+{
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeFloat &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.f ();
- return FALSE;
- }
+ return defaultValue;
+}
- keysym = XStringToKeysym (binding);
- if (keysym != NoSymbol)
- {
- KeyCode keycode;
- keycode = XKeysymToKeycode (d->dpy (), keysym);
- if (keycode)
- {
- key->keycode = keycode;
- key->modifiers = mods;
+CompString
+CompOption::getStringOptionNamed (Vector &options, CompString name,
+ CompString defaultValue)
+{
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeString &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.s ();
- return TRUE;
- }
- }
+ return defaultValue;
+}
- if (strncmp (binding, "0x", 2) == 0)
- {
- key->keycode = strtol (binding, NULL, 0);
- key->modifiers = mods;
- return TRUE;
- }
+unsigned short *
+CompOption::getColorOptionNamed (Vector &options, CompString name,
+ unsigned short *defaultValue)
+{
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeColor &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.c ();
- return FALSE;
+ return defaultValue;
}
-Bool
-stringToButtonBinding (CompDisplay *d,
- const char *binding,
- CompButtonBinding *button)
+
+CompMatch &
+CompOption::getMatchOptionNamed (Vector &options, CompString name,
+ CompMatch &defaultValue)
{
- char *ptr;
- unsigned int mods;
+ foreach (CompOption &o, options)
+ if (o.priv->type == CompOption::TypeMatch &&
+ o.priv->name.compare(name) == 0)
+ return o.priv->value.match ();
- mods = stringToModifiers (d, binding);
+ return defaultValue;
+}
- ptr = strrchr (binding, '>');
- if (ptr)
- binding = ptr + 1;
- while (*binding && !isalnum (*binding))
- binding++;
- if (strncmp (binding, "Button", strlen ("Button")) == 0)
- {
- int buttonNum;
+bool
+CompOption::stringToColor (CompString color,
+ unsigned short *rgba)
+{
+ int c[4];
- if (sscanf (binding + strlen ("Button"), "%d", &buttonNum) == 1)
- {
- button->button = buttonNum;
- button->modifiers = mods;
+ if (sscanf (color.c_str (), "#%2x%2x%2x%2x",
+ &c[0], &c[1], &c[2], &c[3]) == 4)
+ {
+ rgba[0] = c[0] << 8 | c[0];
+ rgba[1] = c[1] << 8 | c[1];
+ rgba[2] = c[2] << 8 | c[2];
+ rgba[3] = c[3] << 8 | c[3];
- return TRUE;
- }
+ return true;
}
- return FALSE;
+ return false;
}
-void
-stringToKeyAction (CompDisplay *d,
- const char *binding,
- CompAction *action)
+CompString
+CompOption::colorToString (unsigned short *rgba)
{
- if (stringToKeyBinding (d, binding, &action->key))
- action->type = CompBindingTypeKey;
- else
- action->type = CompBindingTypeNone;
+ return compPrintf ("#%.2x%.2x%.2x%.2x", rgba[0] / 256, rgba[1] / 256,
+ rgba[2] / 256, rgba[3] / 256);
}
-void
-stringToButtonAction (CompDisplay *d,
- const char *binding,
- CompAction *action)
+CompString
+CompOption::typeToString (CompOption::Type type)
{
- if (stringToButtonBinding (d, binding, &action->button))
- {
- action->edgeMask = bindingStringToEdgeMask (d, binding);
- if (action->edgeMask)
- action->type = CompBindingTypeEdgeButton;
- else
- action->type = CompBindingTypeButton;
- }
- else
- {
- action->type = CompBindingTypeNone;
+ switch (type) {
+ case CompOption::TypeBool:
+ return "bool";
+ case CompOption::TypeInt:
+ return "int";
+ case CompOption::TypeFloat:
+ return "float";
+ case CompOption::TypeString:
+ return "string";
+ case CompOption::TypeColor:
+ return "color";
+ case CompOption::TypeAction:
+ return "action";
+ case CompOption::TypeKey:
+ return "key";
+ case CompOption::TypeButton:
+ return "button";
+ case CompOption::TypeEdge:
+ return "edge";
+ case CompOption::TypeBell:
+ return "bell";
+ case CompOption::TypeMatch:
+ return "match";
+ case CompOption::TypeList:
+ return "list";
}
+
+ return "unknown";
}
-const char *
-edgeToString (unsigned int edge)
+bool
+CompOption::setScreenOption (CompScreen *s,
+ CompOption &o,
+ CompOption::Value &value)
{
- return edges[edge].name;
+ return o.set (value);
}
-unsigned int
-stringToEdgeMask (const char *edge)
+bool
+CompOption::setDisplayOption (CompDisplay *d,
+ CompOption &o,
+ CompOption::Value &value)
{
- unsigned int edgeMask = 0;
- char *needle;
- int i;
-
- for (i = 0; i < SCREEN_EDGE_NUM; i++)
+ if (o.isAction () &&
+ o.value ().action ().state () & CompAction::StateAutoGrab)
{
- needle = strstr (edge, edgeToString (i));
- if (needle)
- {
- if (needle != edge && isalnum (*(needle - 1)))
- continue;
+ CompScreen *s;
- needle += strlen (edgeToString (i));
+ for (s = d->screens (); s; s = s->next)
+ if (!s->addAction (&value.action ()))
+ break;
- if (*needle && isalnum (*needle))
- continue;
+ if (s)
+ {
+ CompScreen *failed = s;
+
+ for (s = d->screens (); s && s != failed; s = s->next)
+ s->removeAction (&value.action ());
- edgeMask |= 1 << i;
+ return false;
+ }
+ else
+ {
+ for (s = d->screens (); s; s = s->next)
+ s->removeAction (&o.value ().action ());
}
+
+ return o.set (value);
}
- return edgeMask;
+ return o.set (value);
}
-char *
-edgeMaskToString (unsigned int edgeMask)
+static void
+finiScreenOptionValue (CompScreen *s,
+ CompOption::Value &v,
+ CompOption::Type type)
{
- char *edge = NULL;
- int i;
-
- for (i = 0; i < SCREEN_EDGE_NUM; i++)
- {
- if (edgeMask & (1 << i))
- {
- if (edge)
- edge = stringAppend (edge, " | ");
-
- edge = stringAppend (edge, edgeToString (i));
- }
+ switch (type) {
+ case CompOption::TypeAction:
+ case CompOption::TypeKey:
+ case CompOption::TypeButton:
+ case CompOption::TypeEdge:
+ case CompOption::TypeBell:
+ if (v.action ().state () & CompAction::StateAutoGrab)
+ s->removeAction (&v.action ());
+ break;
+ case CompOption::TypeList:
+ foreach (CompOption::Value &val, v.list ())
+ finiScreenOptionValue (s, val, v.listType ());
+ default:
+ break;
}
-
- if (!edge)
- return strdup ("");
-
- return edge;
}
-Bool
-stringToColor (const char *color,
- unsigned short *rgba)
+static void
+finiDisplayOptionValue (CompDisplay *d,
+ CompOption::Value &v,
+ CompOption::Type type)
{
- int c[4];
-
- if (sscanf (color, "#%2x%2x%2x%2x", &c[0], &c[1], &c[2], &c[3]) == 4)
- {
- rgba[0] = c[0] << 8 | c[0];
- rgba[1] = c[1] << 8 | c[1];
- rgba[2] = c[2] << 8 | c[2];
- rgba[3] = c[3] << 8 | c[3];
+ CompScreen *s;
- return TRUE;
+ switch (type) {
+ case CompOption::TypeAction:
+ case CompOption::TypeKey:
+ case CompOption::TypeButton:
+ case CompOption::TypeEdge:
+ case CompOption::TypeBell:
+ if (v.action ().state () & CompAction::StateAutoGrab)
+ for (s = d->screens (); s; s = s->next)
+ s->removeAction (&v.action ());
+ break;
+ case CompOption::TypeList:
+ foreach (CompOption::Value &val, v.list ())
+ finiDisplayOptionValue (d, val, v.listType ());
+ default:
+ break;
}
-
- return FALSE;
}
-char *
-colorToString (unsigned short *rgba)
-{
- char tmp[256];
-
- snprintf (tmp, 256, "#%.2x%.2x%.2x%.2x",
- rgba[0] / 256, rgba[1] / 256, rgba[2] / 256, rgba[3] / 256);
- return strdup (tmp);
+void
+CompOption::finiScreenOptions (CompScreen *s,
+ CompOption::Vector &options)
+{
+ foreach (CompOption &o, options)
+ finiScreenOptionValue (s, o.value (), o.type ());
}
-const char *
-optionTypeToString (CompOptionType type)
+void
+CompOption::finiDisplayOptions (CompDisplay *d,
+ CompOption::Vector &options)
{
- switch (type) {
- case CompOptionTypeBool:
- return "bool";
- case CompOptionTypeInt:
- return "int";
- case CompOptionTypeFloat:
- return "float";
- case CompOptionTypeString:
- return "string";
- case CompOptionTypeColor:
- return "color";
- case CompOptionTypeAction:
- return "action";
- case CompOptionTypeKey:
- return "key";
- case CompOptionTypeButton:
- return "button";
- case CompOptionTypeEdge:
- return "edge";
- case CompOptionTypeBell:
- return "bell";
- case CompOptionTypeMatch:
- return "match";
- case CompOptionTypeList:
- return "list";
- }
-
- return "unknown";
+ foreach (CompOption &o, options)
+ finiDisplayOptionValue (d, o.value (), o.type ());
}
-Bool
-isActionOption (CompOption *option)
+PrivateOption::PrivateOption () :
+ name (""),
+ type (CompOption::TypeBool),
+ value (),
+ rest ()
{
- switch (option->type) {
- case CompOptionTypeAction:
- case CompOptionTypeKey:
- case CompOptionTypeButton:
- case CompOptionTypeEdge:
- case CompOptionTypeBell:
- return TRUE;
- default:
- break;
- }
+}
- return FALSE;
+PrivateOption::PrivateOption (const PrivateOption &p) :
+ name (p.name),
+ type (p.type),
+ value (p.value),
+ rest (p.rest)
+{
}
+