prepares event propagation
[src/app-framework-binder.git] / plugins / afm-main-plugin / afm-main-plugin.c
index efa83ba..d7ef198 100644 (file)
 #define _GNU_SOURCE         /* See feature_test_macros(7) */
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 #include <json.h>
 
 #include "afb-plugin.h"
-#include "afb-req-itf.h"
-#include "afb-poll-itf.h"
 
 #include "utils-sbus.h"
 #include "utils-jbus.h"
 
 static const char _auto_[]      = "auto";
 static const char _continue_[]  = "continue";
+static const char _changed_[]   = "changed";
 static const char _detail_[]    = "detail";
 static const char _id_[]        = "id";
 static const char _install_[]   = "install";
@@ -46,9 +46,15 @@ static const char _uninstall_[] = "uninstall";
 static const char _uri_[]       = "uri";
 
 static const struct AFB_interface *interface;
+static struct afb_evmgr evmgr;
 
 static struct jbus *jbus;
 
+static void application_list_changed(const char *data, void *closure)
+{
+       afb_evmgr_push(evmgr, "application-list-changed", NULL);
+}
+
 static struct json_object *embed(const char *tag, struct json_object *obj)
 {
        struct json_object *result;
@@ -75,7 +81,8 @@ static void embed_call_void(struct afb_req request, const char *method)
 {
        struct json_object *obj = jbus_call_sj_sync(jbus, method, "true");
        if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method, obj ? json_object_to_json_string(obj) : "NULL");
+               fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method,
+                       obj ? json_object_to_json_string(obj) : "NULL");
        if (obj == NULL) {
                afb_req_fail(request, "failed", "framework daemon failure");
                return;
@@ -103,7 +110,8 @@ static void call_appid(struct afb_req request, const char *method)
        }
        obj = jbus_call_sj_sync(jbus, method, sid);
        if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid, obj ? json_object_to_json_string(obj) : "NULL");
+               fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid,
+                       obj ? json_object_to_json_string(obj) : "NULL");
        free(sid);
        if (obj == NULL) {
                afb_req_fail(request, "failed", "framework daemon failure");
@@ -173,7 +181,8 @@ static void start(struct afb_req request)
        /* calls the service */
        obj = jbus_call_sj_sync(jbus, _start_, query);
        if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
+               fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query,
+                       obj ? json_object_to_json_string(obj) : "NULL");
        free(query);
 
        /* check status */
@@ -236,7 +245,8 @@ static void install(struct afb_req request)
 
        obj = jbus_call_sj_sync(jbus, _install_, query);
        if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
+               fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
+                       obj ? json_object_to_json_string(obj) : "NULL");
        free(query);
 
        /* check status */
@@ -281,29 +291,43 @@ static struct sbus_itf sbusitf;
 
 const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
 {
+       int rc;
+       struct afb_pollmgr pollmgr;
        struct sbus *sbus;
 
-       if (interface != NULL)
-               return NULL;
-
+       /* records the interface */
+       assert (interface == NULL);
        interface = itf;
-       sbusitf.wait = itf->pollitf->wait;
-       sbusitf.open = itf->pollitf->open;
-       sbusitf.on_readable = itf->pollitf->on_readable;
-       sbusitf.on_writable = itf->pollitf->on_writable;
-       sbusitf.on_hangup = itf->pollitf->on_hangup;
-       sbusitf.close = itf->pollitf->close;
-
-       sbus = sbus_session(&sbusitf, itf->pollclosure);
+       evmgr = afb_daemon_get_evmgr(itf->daemon);
+
+       /* creates the sbus for session */
+       pollmgr = afb_daemon_get_pollmgr(itf->daemon);
+       sbusitf.wait = pollmgr.itf->wait;
+       sbusitf.open = pollmgr.itf->open;
+       sbusitf.on_readable = pollmgr.itf->on_readable;
+       sbusitf.on_writable = pollmgr.itf->on_writable;
+       sbusitf.on_hangup = pollmgr.itf->on_hangup;
+       sbusitf.close = pollmgr.itf->close;
+       sbus = sbus_session(&sbusitf, pollmgr.closure);
        if (sbus == NULL) {
                fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
                return NULL;
        }
 
+       /* creates the jbus for accessing afm-user-daemon */
        jbus = create_jbus(sbus, "/org/AGL/afm/user");
-        if (jbus)
-               return &plug_desc;
-       sbus_unref(sbus);
-       return NULL;
+        if (jbus == NULL) {
+               sbus_unref(sbus);
+               return NULL;
+       }
+
+       /* records the signal handler */
+       rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
+       if (rc < 0) {
+               jbus_unref(jbus);
+               return NULL;
+       }
+
+       return &plug_desc;
 }