[compiz] Include option type in gconf schema

Mike Dransfield mike at blueroot.co.uk
Thu Nov 16 09:42:18 PST 2006


David Reveman wrote:
> On Wed, 2006-11-15 at 02:16 +0000, Ben Reeves wrote:
>   
>> David, is there any chance that the gconf (or dbus) changes will be
>> introduced into the git? because i would really like to get my
>> configurator released, and i can't do it until the option type is
>> available.
>>     
>
> Sure, I'll look at Mike's dbus patch as soon as he thinks it's ready to
> land in head.
>   

Here it is, I think it looks good enough to be accepted.

I have changed a few things for the api to make it
clearer and more intuitive.

get and set were renamed to getValue and setValue
I felt that get and set were a bit generic and seemed
like they would return a reference to the option
rather than the value.  I have included get and set
still and I will remove them after version 0.5.0.  I
doubt anyone is actually using them yet, but we
should start as we mean to go along.

I changed getOptionMetadata to getMetadata since
the path is given at the option level.

There is an additional function to get a string from
an option type.  This seemed better to be in the core,
feel free to move it back into the dbus plugin, but I
feel it could be useful for other plugins too.

There is also a change to the action function to make
the method return true or false.  Previously the error
messages were very misleading and methods should
return something.

I have also added a quick typo fix to the instructions.

While I was testing the get and set, I found that if you
try to get an action option value it crashes.  The
problem was related to the edge, dbus seems very
particular about how it is passed strings.  I could not
get to the bottom of the problem so I have just left it.
Maybe other people do not see the crash?

-------------- next part --------------
diff --git a/include/compiz.h b/include/compiz.h
index cfb2942..1a46277 100644
--- a/include/compiz.h
+++ b/include/compiz.h
@@ -495,6 +495,8 @@ stringToColor (const char     *color,
 char *
 colorToString (unsigned short *rgba);
 
+char *
+optionTypeToString (CompOptionType type);
 
 /* display.c */
 
-------------- next part --------------
--- ../../clean/plugins/dbus.c	2006-11-15 20:22:38.961551784 +0000
+++ dbus.c	2006-11-16 16:45:32.182596168 +0000
@@ -32,11 +32,15 @@
 
 #include <compiz.h>
 
-#define COMPIZ_DBUS_SERVICE_NAME	   "org.freedesktop.compiz"
-#define COMPIZ_DBUS_ACTIVATE_MEMBER_NAME   "activate"
-#define COMPIZ_DBUS_DEACTIVATE_MEMBER_NAME "deactivate"
-#define COMPIZ_DBUS_SET_MEMBER_NAME        "set"
-#define COMPIZ_DBUS_GET_MEMBER_NAME        "get"
+#define COMPIZ_DBUS_SERVICE_NAME	             "org.freedesktop.compiz"
+#define COMPIZ_DBUS_ACTIVATE_MEMBER_NAME             "activate"
+#define COMPIZ_DBUS_DEACTIVATE_MEMBER_NAME           "deactivate"
+#define COMPIZ_DBUS_SET_MEMBER_NAME_DEP              "set"
+#define COMPIZ_DBUS_GET_MEMBER_NAME_DEP              "get"
+#define COMPIZ_DBUS_SET_MEMBER_NAME                  "setValue"
+#define COMPIZ_DBUS_GET_MEMBER_NAME                  "getValue"
+#define COMPIZ_DBUS_GET_OPTIONS_MEMBER_NAME          "getOptions"
+#define COMPIZ_DBUS_GET_OPTION_META_MEMBER_NAME      "getMetadata"
 
 typedef enum {
     DbusActionIndexKeyBinding    = 0,
@@ -119,6 +123,23 @@
     return NULL;
 }
 
+void
+dbusReturnBool (DBusConnection *connection,
+		DBusMessage *message,
+		Bool value)
+{
+    DBusMessage *reply;
+    Bool    msgBool = TRUE;
+    reply = dbus_message_new_method_return (message);
+    dbus_message_append_args (reply,
+				  DBUS_TYPE_BOOLEAN, &msgBool,
+				  DBUS_TYPE_INVALID);
+    dbus_connection_send (connection, reply, NULL);
+    dbus_connection_flush (connection);
+    dbus_message_unref (reply);
+}
+
+
 /*
  * Activate can be used to trigger any existing action. Arguments
  * should be a pair of { string, bool|int32|double|string }.
@@ -140,15 +161,14 @@
  * /org/freedesktop/compiz/cube/allscreens/unfold	      \
  * org.freedesktop.compiz.activate			      \
  * string:'root'					      \
- * int32:`xwininfo -root | grep id: | awk '{ print $4 }'`     \
- * string:'face' int32:1
+ * int32:`xwininfo -root | grep id: | awk '{ print $4 }'`
  *
  * dbus-send --type=method_call --dest=org.freedesktop.compiz \
  * /org/freedesktop/compiz/cube/allscreens/unfold	      \
  * org.freedesktop.compiz.deactivate			      \
  * string:'root'					      \
- * int32:`xwininfo -root | grep id: | awk '{ print $4 }'`     \
- * string:'face' int32:1
+ * int32:`xwininfo -root | grep id: | awk '{ print $4 }'`
+ *
  */
 static Bool
 dbusHandleActionMessage (DBusConnection *connection,
@@ -702,13 +722,152 @@
     return FALSE;
 }
 
+static Bool
+dbusHandleGetOptionsMessage (DBusConnection *connection,
+			     DBusMessage    *message,
+			     CompDisplay	   *d,
+			     char	   **path)
+{
+    CompScreen *s;
+    CompOption *option;
+    int	       nOption;
+
+    option = dbusGetOptionsFromPath (d, path, &s, &nOption);
+    if (!option)
+	return FALSE;
+
+    DBusMessage *reply;
+
+    reply = dbus_message_new_method_return (message);
+
+    while (nOption--)
+    {
+        dbus_message_append_args (reply,
+				  DBUS_TYPE_STRING, &option->name,
+				  DBUS_TYPE_INVALID);
+	option++;
+    }
+    dbus_connection_send (connection, reply, NULL);
+    dbus_connection_flush (connection);
+
+    dbus_message_unref (reply);
+
+    return TRUE;
+
+}
+
+static Bool
+dbusHandleGetOptionMetadataMessage (DBusConnection *connection,
+			            DBusMessage    *message,
+			            CompDisplay	   *d,
+			            char	   **path)
+{
+    CompScreen	*s;
+    CompOption	*option;
+    int		nOption;
+    Bool	handled = FALSE;
+    int i;
+
+    double min;
+    double max;
+    double precision;
+
+    char *sPossible;
+
+    char *datatype;
+    char *listtype;
+
+    DBusMessage *reply;
+
+    reply = dbus_message_new_method_return (message);
+
+
+    option = dbusGetOptionsFromPath (d, path, &s, &nOption);
+    if (!option)
+	return FALSE;
+
+    while (nOption--)
+    {
+	if (strcmp (option->name, path[2]) == 0)
+	{
+
+	    datatype = optionTypeToString (option->type);
+
+            dbus_message_append_args (reply,
+				      DBUS_TYPE_STRING, &option->shortDesc,
+				      DBUS_TYPE_STRING, &option->longDesc,
+				      DBUS_TYPE_STRING, &datatype,
+				      DBUS_TYPE_INVALID);
+
+	    if (option->type == CompOptionTypeList)
+	    {
+	        listtype = optionTypeToString (option->value.list.type);
+                dbus_message_append_args (reply,
+				          DBUS_TYPE_STRING, &listtype,
+				          DBUS_TYPE_INVALID);
+	    }
+
+	    if (option->type == CompOptionTypeInt ||
+		( (option->type == CompOptionTypeList) &&
+                  (option->value.list.type == CompOptionTypeInt) ) )
+		dbus_message_append_args (reply,
+				          DBUS_TYPE_INT32, &option->rest.i.min,
+				          DBUS_TYPE_INT32, &option->rest.i.max,
+				          DBUS_TYPE_INVALID);
+
+	    else if (option->type == CompOptionTypeFloat ||
+		     ( (option->type == CompOptionTypeList) &&
+                       (option->value.list.type == CompOptionTypeFloat) ) )
+	    {
+		min = option->rest.f.min;
+		max = option->rest.f.max;
+		precision = option->rest.f.precision;
+		dbus_message_append_args (reply,
+				          DBUS_TYPE_DOUBLE, &min,
+				          DBUS_TYPE_DOUBLE, &max,
+				          DBUS_TYPE_DOUBLE, &precision,
+				          DBUS_TYPE_INVALID);
+	    }
+	    else if (option->type == CompOptionTypeString ||
+		     ( (option->type == CompOptionTypeList) &&
+                       (option->value.list.type == CompOptionTypeString) ) )
+	    {
+    		if (option->rest.s.nString)
+    		{
+		    for ( i=0; i < option->rest.s.nString; i++ )
+		    {
+			sPossible = option->rest.s.string[i];
+			dbus_message_append_args (reply,
+				         	  DBUS_TYPE_STRING, &sPossible,
+				          	  DBUS_TYPE_INVALID);
+		    }
+		}
+	    }
+	    handled = TRUE;
+	}
+	option++;
+    }
+
+    if (!handled)
+	return FALSE;
+
+    dbus_connection_send (connection, reply, NULL);
+    dbus_connection_flush (connection);
+
+    dbus_message_unref (reply);
+
+    return TRUE;
+
+}
+
 static DBusHandlerResult
 dbusHandleMessage (DBusConnection *connection,
 		   DBusMessage    *message,
 		   void           *userData)
 {
     CompDisplay *d = (CompDisplay *) userData;
-    Bool	status = FALSE;
+    Bool	status  = FALSE;
+    Bool	handled = FALSE;
     char	**path;
     const char  *service, *interface, *member;
 
@@ -728,7 +887,7 @@
     if (!dbus_message_get_path_decomposed (message, &path))
 	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-    if (!path[0] || !path[1] || !path[2] || !path[3] || !path[4] || !path[5])
+    if (!path[0] || !path[1] || !path[2] || !path[3] || !path[4])
 	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
     if (strcmp (path[0], "org")	       ||
@@ -736,32 +895,70 @@
 	strcmp (path[2], "compiz"))
 	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-    if (dbus_message_has_member (message, COMPIZ_DBUS_ACTIVATE_MEMBER_NAME))
-    {
-	status = dbusHandleActionMessage (connection, message, d, &path[3],
-					  TRUE);
-    }
-    else if (dbus_message_has_member (message,
-				      COMPIZ_DBUS_DEACTIVATE_MEMBER_NAME))
-    {
-	status = dbusHandleActionMessage (connection, message, d, &path[3],
-					  FALSE);
-    }
-    else if (dbus_message_has_member (message, COMPIZ_DBUS_SET_MEMBER_NAME))
+    if (path[5])  /* option level */
     {
-	status = dbusHandleSetOptionMessage (connection, message, d, &path[3]);
+	/* actions */
+        if (dbus_message_has_member (message, COMPIZ_DBUS_ACTIVATE_MEMBER_NAME))
+        {
+	    status = dbusHandleActionMessage (connection, message, d, &path[3],
+					      TRUE);
+	    handled = TRUE;
+        }
+        else if (dbus_message_has_member (message,
+				          COMPIZ_DBUS_DEACTIVATE_MEMBER_NAME))
+        {
+	    status = dbusHandleActionMessage (connection, message, d, &path[3],
+					      FALSE);
+	    handled = TRUE;
+        }
+	/* get/set option values */
+        else if (dbus_message_has_member (message,
+					  COMPIZ_DBUS_SET_MEMBER_NAME) ||
+		dbus_message_has_member (message,
+					 COMPIZ_DBUS_SET_MEMBER_NAME_DEP))
+        {
+	    status = dbusHandleSetOptionMessage (connection, message, d,
+						 &path[3]);
+	    handled = TRUE;
+        }
+        else if (dbus_message_has_member (message,
+					  COMPIZ_DBUS_GET_MEMBER_NAME) ||
+		dbus_message_has_member (message,
+					 COMPIZ_DBUS_GET_MEMBER_NAME_DEP))
+        {
+	    status = dbusHandleGetOptionMessage (connection, message, d,
+						 &path[3]);
+	    handled = TRUE;
+        }
+        else if (dbus_message_has_member (message,
+                                          COMPIZ_DBUS_GET_OPTION_META_MEMBER_NAME))
+        {
+	    status = dbusHandleGetOptionMetadataMessage (connection, message, d,
+						         &path[3]);
+	    handled = TRUE;
+        }
     }
-    else if (dbus_message_has_member (message, COMPIZ_DBUS_GET_MEMBER_NAME))
+    else if (path[4])  /* plugin screen/display level */
     {
-	status = dbusHandleGetOptionMessage (connection, message, d, &path[3]);
+
+        if (dbus_message_has_member (message,
+				          COMPIZ_DBUS_GET_OPTIONS_MEMBER_NAME))
+        {
+	    status = dbusHandleGetOptionsMessage (connection, message, d,
+						  &path[3]);
+	    handled = TRUE;
+        }
     }
 
     dbus_free_string_array (path);
 
-    if (status)
-	return DBUS_HANDLER_RESULT_HANDLED;
+    if (handled)
+	dbusReturnBool (connection, message, status);
+    else
+	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+    return DBUS_HANDLER_RESULT_HANDLED;
 
-    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 static Bool
-------------- next part --------------
diff --git a/src/option.c b/src/option.c
index 0b61592..eb6e689 100644
--- a/src/option.c
+++ b/src/option.c
@@ -53,6 +53,20 @@ struct _Modifier {
 
 #define N_MODIFIERS (sizeof (modifiers) / sizeof (struct _Modifier))
 
+struct _Datatype {
+    char *name;
+    int  type;
+} datatypes[] = {
+    { "Action",         CompOptionTypeAction	},
+    { "Bool",    	CompOptionTypeBool	},
+    { "Int", 		CompOptionTypeInt	},
+    { "Float",	        CompOptionTypeFloat	},
+    { "String",		CompOptionTypeString	},
+    { "Color",	        CompOptionTypeColor	},
+    { "List",	        CompOptionTypeList	},
+};
+#define N_DATATYPES (sizeof (datatypes) / sizeof (struct _Datatype))
+
 static char *edgeName[] = {
     N_("Left"),
     N_("Right"),
@@ -667,3 +681,21 @@ colorToString (unsigned short *rgba)
 
     return strdup (tmp);
 }
+
+char *
+optionTypeToString (CompOptionType type)
+{
+    char *datatype = NULL;
+    int  i;
+
+    for (i = 0; i < N_DATATYPES; i++)
+    {
+	if (type == datatypes[i].type)
+	{
+	    datatype = stringAppend (datatype, datatypes[i].name);
+	    return datatype;
+	}
+    }
+
+    return datatype;
+}


More information about the compiz mailing list