7 #include "e_dbus_private.h"
9 static E_DBus_Interface *introspectable_interface = NULL;
10 static E_DBus_Interface *properties_interface = NULL;
12 typedef struct E_DBus_Method E_DBus_Method;
13 typedef struct E_DBus_Signal E_DBus_Signal;
15 Eina_Strbuf * e_dbus_object_introspect(E_DBus_Object *obj);
17 static void e_dbus_object_unregister(DBusConnection *conn, void *user_data);
18 static DBusHandlerResult e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data);
20 static void e_dbus_interface_free(E_DBus_Interface *iface);
22 static E_DBus_Method *e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func);
23 static void e_dbus_object_method_free(E_DBus_Method *m);
25 static E_DBus_Signal *e_dbus_signal_new(const char *name, const char *signature);
26 static void e_dbus_object_signal_free(E_DBus_Signal *s);
28 static void _introspect_indent_append(Eina_Strbuf *buf, int level);
29 static void _introspect_interface_append(Eina_Strbuf *buf, E_DBus_Interface *iface, int level);
30 static void _introspect_method_append(Eina_Strbuf *buf, E_DBus_Method *method, int level);
31 static void _introspect_signal_append(Eina_Strbuf *buf, E_DBus_Signal *signal, int level);
32 static void _introspect_arg_append(Eina_Strbuf *buf, const char *type, const char *direction, int level);
35 //static Eina_List *standard_methods = NULL;
38 static DBusObjectPathVTable vtable = {
39 e_dbus_object_unregister,
40 e_dbus_object_handler,
49 E_DBus_Connection *conn;
51 Eina_List *interfaces;
52 char *introspection_data;
53 int introspection_dirty;
55 E_DBus_Object_Property_Get_Cb cb_property_get;
56 E_DBus_Object_Property_Set_Cb cb_property_set;
61 struct E_DBus_Interface
73 char *reply_signature;
74 E_DBus_Method_Cb func;
84 cb_introspect(E_DBus_Object *obj, DBusMessage *msg)
89 if (obj->introspection_dirty || !obj->introspection_data)
91 buf = e_dbus_object_introspect(obj);
94 ret = dbus_message_new_error(msg, "org.enlightenment.NotIntrospectable", "This object does not provide introspection data");
98 if (obj->introspection_data) free(obj->introspection_data);
99 obj->introspection_data = strdup(eina_strbuf_string_get(buf));
100 eina_strbuf_free(buf);
102 //printf("XML: \n\n%s\n\n", obj->introspection_data);
103 ret = dbus_message_new_method_return(msg);
104 dbus_message_append_args(ret, DBUS_TYPE_STRING, &(obj->introspection_data), DBUS_TYPE_INVALID);
110 cb_properties_get(E_DBus_Object *obj, DBusMessage *msg)
113 DBusMessageIter iter, sub;
119 dbus_error_init(&err);
120 dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
122 if (dbus_error_is_set(&err))
124 return dbus_message_new_error(msg, err.name, err.message);
127 obj->cb_property_get(obj, property, &type, &value);
128 if (type == DBUS_TYPE_INVALID)
130 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
133 if (dbus_type_is_basic(type))
135 reply = dbus_message_new_method_return(msg);
136 dbus_message_iter_init_append(msg, &iter);
137 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, e_dbus_basic_type_as_string(type), &sub);
138 dbus_message_iter_append_basic(&sub, type, &value);
139 dbus_message_iter_close_container(&iter, &sub);
144 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
149 cb_properties_set(E_DBus_Object *obj, DBusMessage *msg)
151 DBusMessageIter iter, sub;
156 dbus_message_iter_init(msg, &iter);
157 dbus_message_iter_get_basic(&iter, &property);
158 dbus_message_iter_recurse(&iter, &sub);
159 type = dbus_message_iter_get_arg_type(&sub);
160 if (dbus_type_is_basic(type))
162 dbus_message_iter_get_basic(&sub, &value);
163 if (obj->cb_property_set(obj, property, type, value))
165 return dbus_message_new_method_return(msg);
169 return dbus_message_new_error_printf(msg, "org.enlightenment.DBus.InvalidProperty", "The property '%s' does not exist on this object.", property);
174 return dbus_message_new_error(msg, "org.enlightenment.DBus.UnsupportedType", "E_DBus currently only supports properties of a basic type.");
180 e_dbus_object_init(void)
182 introspectable_interface = e_dbus_interface_new("org.freedesktop.DBus.Introspectable");
183 properties_interface = e_dbus_interface_new(E_DBUS_FDO_INTERFACE_PROPERTIES);
184 if (!introspectable_interface || !properties_interface)
186 if (introspectable_interface) e_dbus_interface_unref(introspectable_interface);
187 introspectable_interface = NULL;
188 if (properties_interface) e_dbus_interface_unref(properties_interface);
189 properties_interface = NULL;
193 e_dbus_interface_method_add(introspectable_interface, "Introspect", "", "s", cb_introspect);
194 e_dbus_interface_method_add(properties_interface, "Get", "s", "v", cb_properties_get);
195 e_dbus_interface_method_add(properties_interface, "Set", "sv", "", cb_properties_set);
200 e_dbus_object_shutdown(void)
202 e_dbus_interface_unref(introspectable_interface);
203 introspectable_interface = NULL;
205 e_dbus_interface_unref(properties_interface);
206 properties_interface = NULL;
210 e_dbus_object_add(E_DBus_Connection *conn, const char *object_path, void *data)
214 EINA_SAFETY_ON_NULL_RETURN_VAL(conn, NULL);
215 EINA_SAFETY_ON_NULL_RETURN_VAL(object_path, NULL);
217 obj = calloc(1, sizeof(E_DBus_Object));
218 if (!obj) return NULL;
220 if (!dbus_connection_register_object_path(conn->conn, object_path, &vtable, obj))
227 e_dbus_connection_ref(conn);
228 obj->path = strdup(object_path);
230 obj->interfaces = NULL;
232 e_dbus_object_interface_attach(obj, introspectable_interface);
238 e_dbus_object_free(E_DBus_Object *obj)
240 E_DBus_Interface *iface;
244 DBG("e_dbus_object_free (%s)", obj->path);
245 dbus_connection_unregister_object_path(obj->conn->conn, obj->path);
246 e_dbus_connection_close(obj->conn);
248 if (obj->path) free(obj->path);
249 EINA_LIST_FREE(obj->interfaces, iface)
250 e_dbus_interface_unref(iface);
252 if (obj->introspection_data) free(obj->introspection_data);
258 e_dbus_object_data_get(E_DBus_Object *obj)
263 EAPI E_DBus_Connection *
264 e_dbus_object_conn_get(E_DBus_Object *obj)
270 e_dbus_object_path_get(E_DBus_Object *obj)
275 EAPI const Eina_List *
276 e_dbus_object_interfaces_get(E_DBus_Object *obj)
278 return obj->interfaces;
282 e_dbus_object_property_get_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Get_Cb func)
284 obj->cb_property_get = func;
288 e_dbus_object_property_set_cb_set(E_DBus_Object *obj, E_DBus_Object_Property_Set_Cb func)
290 obj->cb_property_set = func;
294 e_dbus_object_interface_attach(E_DBus_Object *obj, E_DBus_Interface *iface)
296 EINA_SAFETY_ON_NULL_RETURN(obj);
297 EINA_SAFETY_ON_NULL_RETURN(iface);
299 e_dbus_interface_ref(iface);
300 obj->interfaces = eina_list_append(obj->interfaces, iface);
301 obj->introspection_dirty = 1;
302 DBG("e_dbus_object_interface_attach (%s, %s) ", obj->path, iface->name);
306 e_dbus_object_interface_detach(E_DBus_Object *obj, E_DBus_Interface *iface)
308 E_DBus_Interface *found;
310 DBG("e_dbus_object_interface_detach (%s, %s) ", obj->path, iface->name);
311 found = eina_list_data_find(obj->interfaces, iface);
314 obj->interfaces = eina_list_remove(obj->interfaces, iface);
315 obj->introspection_dirty = 1;
316 e_dbus_interface_unref(iface);
320 e_dbus_interface_ref(E_DBus_Interface *iface)
323 DBG("e_dbus_interface_ref (%s) = %d", iface->name, iface->refcount);
327 e_dbus_interface_unref(E_DBus_Interface *iface)
329 DBG("e_dbus_interface_unref (%s) = %d", iface->name, iface->refcount - 1);
330 if (--(iface->refcount) == 0)
331 e_dbus_interface_free(iface);
335 e_dbus_interface_free(E_DBus_Interface *iface)
340 if (iface->name) free(iface->name);
341 EINA_LIST_FREE(iface->methods, m)
342 e_dbus_object_method_free(m);
343 EINA_LIST_FREE(iface->signals, s)
344 e_dbus_object_signal_free(s);
350 e_dbus_interface_method_add(E_DBus_Interface *iface, const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
354 m = e_dbus_method_new(member, signature, reply_signature, func);
355 DBG("E-dbus: Add method %s: %p", member, m);
358 iface->methods = eina_list_append(iface->methods, m);
363 e_dbus_interface_signal_add(E_DBus_Interface *iface, const char *name, const char *signature)
367 s = e_dbus_signal_new(name, signature);
368 DBG("E-dbus: Add signal %s: %p", name, s);
371 iface->signals = eina_list_append(iface->signals, s);
375 EAPI E_DBus_Interface *
376 e_dbus_interface_new(const char *interface)
378 E_DBus_Interface *iface;
380 if (!interface) return NULL;
382 iface = calloc(1, sizeof(E_DBus_Interface));
383 if (!iface) return NULL;
386 iface->name = strdup(interface);
387 iface->methods = NULL;
388 iface->signals = NULL;
393 static E_DBus_Method *
394 e_dbus_method_new(const char *member, const char *signature, const char *reply_signature, E_DBus_Method_Cb func)
398 if (!member || !func) return NULL;
400 if (signature && !dbus_signature_validate(signature, NULL)) return NULL;
401 if (reply_signature && !dbus_signature_validate(reply_signature, NULL)) return NULL;
402 m = calloc(1, sizeof(E_DBus_Method));
405 m->member = strdup(member);
407 m->signature = strdup(signature);
409 m->reply_signature = strdup(reply_signature);
416 e_dbus_object_method_free(E_DBus_Method *m)
419 if (m->member) free(m->member);
420 if (m->signature) free(m->signature);
421 if (m->reply_signature) free(m->reply_signature);
426 static E_DBus_Signal *
427 e_dbus_signal_new(const char *name, const char *signature)
431 if (!name) return NULL;
433 if (signature && !dbus_signature_validate(signature, NULL)) return NULL;
434 s = calloc(1, sizeof(E_DBus_Signal));
437 s->name = strdup(name);
439 s->signature = strdup(signature);
445 e_dbus_object_signal_free(E_DBus_Signal *s)
448 if (s->name) free(s->name);
449 if (s->signature) free(s->signature);
453 static E_DBus_Method *
454 e_dbus_object_method_find(E_DBus_Object *obj, const char *interface, const char *member)
457 E_DBus_Interface *iface;
460 if (!obj || !member) return NULL;
462 EINA_LIST_FOREACH(obj->interfaces, l, iface)
464 if (strcmp(interface, iface->name)) continue;
465 EINA_LIST_FOREACH(iface->methods, ll, m)
467 if (!strcmp(member, m->member))
474 static DBusHandlerResult
475 e_dbus_object_handler(DBusConnection *conn, DBusMessage *message, void *user_data)
480 dbus_uint32_t serial;
484 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
486 m = e_dbus_object_method_find(obj, dbus_message_get_interface(message), dbus_message_get_member(message));
488 /* XXX should this send an 'invalid method' error instead? */
490 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
492 if (m->signature && !dbus_message_has_signature(message, m->signature))
493 reply = dbus_message_new_error_printf(message, "org.enlightenment.InvalidSignature", "Expected signature: %s", m->signature);
495 reply = m->func(obj, message);
497 /* user can choose reply later */
499 return DBUS_HANDLER_RESULT_HANDLED;
501 dbus_connection_send(conn, reply, &serial);
502 dbus_message_unref(reply);
504 return DBUS_HANDLER_RESULT_HANDLED;
508 e_dbus_object_unregister(DBusConnection *conn __UNUSED__, void *user_data __UNUSED__)
510 /* free up the object struct? */
514 e_dbus_object_introspect(E_DBus_Object *obj)
518 E_DBus_Interface *iface;
521 buf = eina_strbuf_new();
524 eina_strbuf_append(buf, "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n");
526 eina_strbuf_append(buf, "<node name=\"");
527 eina_strbuf_append(buf, obj->path);
528 eina_strbuf_append(buf, "\">\n");
531 EINA_LIST_FOREACH(obj->interfaces, l, iface)
532 _introspect_interface_append(buf, iface, level);
534 eina_strbuf_append(buf, "</node>\n");
539 _introspect_indent_append(Eina_Strbuf *buf, int level)
541 /* XXX optimize this? */
544 eina_strbuf_append_char(buf, ' ');
547 _introspect_interface_append(Eina_Strbuf *buf, E_DBus_Interface *iface, int level)
553 _introspect_indent_append(buf, level);
554 eina_strbuf_append(buf, "<interface name=\"");
555 eina_strbuf_append(buf, iface->name);
556 eina_strbuf_append(buf, "\">\n");
559 DBG("introspect iface: %s", iface->name);
560 EINA_LIST_FOREACH(iface->methods, l, m)
561 _introspect_method_append(buf, m, level);
562 EINA_LIST_FOREACH(iface->signals, l, s)
563 _introspect_signal_append(buf, s, level);
566 _introspect_indent_append(buf, level);
567 eina_strbuf_append(buf, "</interface>\n");
570 _introspect_method_append(Eina_Strbuf *buf, E_DBus_Method *method, int level)
572 DBusSignatureIter iter;
575 _introspect_indent_append(buf, level);
576 DBG("introspect method: %s\n", method->member);
577 eina_strbuf_append(buf, "<method name=\"");
578 eina_strbuf_append(buf, method->member);
579 eina_strbuf_append(buf, "\">\n");
583 if (method->signature &&
584 method->signature[0] &&
585 dbus_signature_validate(method->signature, NULL))
587 dbus_signature_iter_init(&iter, method->signature);
588 while ((type = dbus_signature_iter_get_signature(&iter)))
590 _introspect_arg_append(buf, type, "in", level);
593 if (!dbus_signature_iter_next(&iter)) break;
597 /* append reply args */
598 if (method->reply_signature &&
599 method->reply_signature[0] &&
600 dbus_signature_validate(method->reply_signature, NULL))
602 dbus_signature_iter_init(&iter, method->reply_signature);
603 while ((type = dbus_signature_iter_get_signature(&iter)))
605 _introspect_arg_append(buf, type, "out", level);
608 if (!dbus_signature_iter_next(&iter)) break;
613 _introspect_indent_append(buf, level);
614 eina_strbuf_append(buf, "</method>\n");
618 _introspect_signal_append(Eina_Strbuf *buf, E_DBus_Signal *s, int level)
620 DBusSignatureIter iter;
623 _introspect_indent_append(buf, level);
624 DBG("introspect signal: %s", s->name);
625 eina_strbuf_append(buf, "<signal name=\"");
626 eina_strbuf_append(buf, s->name);
627 eina_strbuf_append(buf, "\">\n");
633 dbus_signature_validate(s->signature, NULL))
635 dbus_signature_iter_init(&iter, s->signature);
636 while ((type = dbus_signature_iter_get_signature(&iter)))
638 _introspect_arg_append(buf, type, NULL, level);
641 if (!dbus_signature_iter_next(&iter)) break;
646 _introspect_indent_append(buf, level);
647 eina_strbuf_append(buf, "</signal>\n");
651 _introspect_arg_append(Eina_Strbuf *buf, const char *type, const char *direction, int level)
653 _introspect_indent_append(buf, level);
654 eina_strbuf_append(buf, "<arg type=\"");
655 eina_strbuf_append(buf, type);
658 eina_strbuf_append(buf, "\" direction=\"");
659 eina_strbuf_append(buf, direction);
661 eina_strbuf_append(buf, "\"/>\n");
666 DBusConnection *e_dbus_conn_object_get(E_DBus_Connection *conn)