typedef struct <PREFIX>_<NAME>_s {
<PREFIX>_<NAME>_callback_s callback;
event_handler_h event_handler;
+ char *sender_appid;
+ bool is_system;
GRecMutex mutex;
void *user_data;
} <PREFIX>_<NAME>_t;
return b;
}
-static const char *__get_appid()
+static char *__get_event_name(const char *appid, bool is_system, const char *iface_name)
{
- static char *str = nullptr;
-
- if (str)
- return str;
-
- char *id = nullptr;
- int ret = app_get_id(&id);
- if (ret != APP_ERROR_NONE)
- id = strdup("system");
-
- str = id;
- return str;
-}
+ size_t size;
+ char *buf;
+
+ if (is_system) {
+ size = strlen("tizen.system.event.tidl_iface_") + strlen(iface_name) + 1;
+ buf = calloc(1, size);
+ if (!buf)
+ return nullptr;
+ snprintf(buf, size, "tizen.system.event.tidl_iface_%s", iface_name);
+ return buf;
+ }
-static char *__get_event_name(const char *appid, const char *iface_name)
-{
- size_t size = strlen(appid) + strlen(iface_name) + 19;
- char *buf = calloc(1, size);
+ size = strlen(appid) + strlen(iface_name) + 19;
+ buf = calloc(1, size);
if (!buf)
return nullptr;
snprintf(buf, size, "event.%s.tidl_iface_%s", appid, iface_name);
rpc_port_parcel_destroy(p);
}
-int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, <PREFIX>_<NAME>_h *h)
+int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, bool is_system, <PREFIX>_<NAME>_h *h)
{
<PREFIX>_<NAME>_t *handle;
int ret;
return RPC_PORT_ERROR_OUT_OF_MEMORY;
}
- g_rec_mutex_init(&handle->mutex);
- handle->callback = *callback;
- handle->user_data = user_data;
-
- ev_name = __get_event_name(sender_appid, "<NAME>");
- if (ev_name == nullptr)
+ ev_name = __get_event_name(sender_appid, is_system, "<NAME>");
+ if (ev_name == nullptr) {
+ free(handle);
return RPC_PORT_ERROR_OUT_OF_MEMORY;
+ }
ret = event_add_event_handler(ev_name, __<NAME>_event_system_cb, handle, &handle->event_handler);
free(ev_name);
if (ret != EVENT_ERROR_NONE) {
_E("Failed to register events. result(%d)", ret);
+ free(handle);
return RPC_PORT_ERROR_INVALID_PARAMETER;
}
+ handle->sender_appid = strdup(sender_appid);
+ if (handle->sender_appid == nullptr) {
+ _E("Out of memory");
+ event_remove_event_handler(handle->event_handler);
+ free(handle);
+ return RPC_PORT_ERROR_OUT_OF_MEMORY;
+ }
+
+ g_rec_mutex_init(&handle->mutex);
+ handle->callback = *callback;
+ handle->user_data = user_data;
+ handle->is_system = is_system;
+
*h = handle;
return RPC_PORT_ERROR_NONE;
}
-int <PREFIX>_<NAME>_create_for_sender(<PREFIX>_<NAME>_h *h)
+int <PREFIX>_<NAME>_create_for_sender(const char *sender_appid, bool is_system, <PREFIX>_<NAME>_h *h)
{
<PREFIX>_<NAME>_t *handle;
- if (h == nullptr) {
+ if (h == nullptr || sender_appid == nullptr) {
_E("Invalid parameter");
return RPC_PORT_ERROR_INVALID_PARAMETER;
}
return RPC_PORT_ERROR_OUT_OF_MEMORY;
}
+ handle->sender_appid = strdup(sender_appid);
+ if (handle->sender_appid == nullptr) {
+ _E("Out of memory");
+ free(handle);
+ return RPC_PORT_ERROR_OUT_OF_MEMORY;
+ }
+
g_rec_mutex_init(&handle->mutex);
+ handle->is_system = is_system;
*h = handle;
return RPC_PORT_ERROR_NONE;
if (h->event_handler)
event_remove_event_handler(h->event_handler);
+ if (h->sender_appid)
+ free(h->sender_appid);
free(h);
return;
}
- int ret = event_publish_app_event(__get_event_name(__get_appid(), "<NAME>"), b);
+ int ret = event_publish_app_event(__get_event_name(h->sender_appid, h->is_system, "<NAME>"), b);
if (ret != EVENT_ERROR_NONE) {
_E("Failed to publish event. result(%d)", ret);
bundle_free(b);
constexpr const char CB_INTERFACE_EXTRA_HEADER[] =
R"__c_cb(
-#include <app_common.h>
#include <app_event.h>
)__c_cb";
* @param[in] sender_appid ID of app sending events
* @param[in] callback The set of callback functions to handle events
* @param[in] user_data The user data to be passed to the callback function
+ * @param[in] is_system True on system events
* @param[out] h The <PREFIX>_<NAME> handle
* @return @c 0 on success,
* otherwise a negative error value
* @see <PREFIX>_<NAME>_destroy()
* @see #<PREFIX>_<NAME>_callback_s
*/
-int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, <PREFIX>_<NAME>_h *h);
+int <PREFIX>_<NAME>_create(const char *sender_appid, <PREFIX>_<NAME>_callback_s *callback, void *user_data, bool is_system, <PREFIX>_<NAME>_h *h);
/**
* @brief Creates a <PREFIX>_<NAME> handle for sending events.
* @remarks The @a h handle should be released using
* the <PREFIX>_<NAME>_destroy() if it's no longer needed.
*
+ * @param[in] sender_appid ID of app sending events
+ * @param[in] is_system True on system events
* @param[out] h The <PREFIX>_<NAME> handle
* @return @c 0 on success,
* otherwise a negative error value
* @see <PREFIX>_<NAME>_destroy()
*/
-int <PREFIX>_<NAME>_create_for_sender(<PREFIX>_<NAME>_h *h);
+int <PREFIX>_<NAME>_create_for_sender(const char *sender_appid, bool is_system, <PREFIX>_<NAME>_h *h);
/**
* @brief Destroys the <PREFIX>_<NAME> handle.
const char CB_INTERFACE_CTOR[] =
R"__cpp_cb(
-<CLS_NAME>::<CLS_NAME>() {
-}
-
-<CLS_NAME>::<CLS_NAME>(const std::string& sender_appid) {
- Subscribe(sender_appid);
+<CLS_NAME>::<CLS_NAME>(std::string sender_appid, bool is_system)
+ : sender_appid_(std::move(sender_appid)), is_system_(is_system) {
+ Subscribe();
}
)__cpp_cb";
const char CB_GROUP_HELPER_METHODS[] =
R"__cpp_cb(
-void <CLS_NAME>::Subscribe(const std::string& sender_appid) {
+void <CLS_NAME>::Subscribe() {
std::lock_guard<std::recursive_mutex> lock(mutex_);
- int ret = event_add_event_handler(GetEventName(sender_appid).c_str(), EventCb, this, &event_handler_);
+ int ret = event_add_event_handler(GetEventName().c_str(), EventCb, this, &event_handler_);
if (ret != EVENT_ERROR_NONE) {
_E("Failed to register events. result(%d)", ret);
throw InvalidIOException();
return p;
}
-std::string <CLS_NAME>::GetAppId() {
- static std::string str;
-
- if (!str.empty())
- return str;
-
- char* id;
- int ret = app_get_id(&id);
- if (ret != APP_ERROR_NONE)
- return "system";
-
- str = id;
- free(id);
- return str;
-}
-
std::string <CLS_NAME>::GetEventName() {
- return "event." + GetAppId() + ".tidl_iface_<CLS_NAME>";
-}
-
-std::string <CLS_NAME>::GetEventName(const std::string& sender_appid) {
- return "event." + sender_appid + ".tidl_iface_<CLS_NAME>";
+ if (is_system_)
+ return "tizen.system.event.tidl_iface_<CLS_NAME>";
+ return "event." + sender_appid_ + ".tidl_iface_<CLS_NAME>";
}
void <CLS_NAME>::EventCb(const char* event_name, bundle* event_data, void* user_data) {
R"__cpp_cb(
class <CLS_NAME> {
public:
- <CLS_NAME>();
- <CLS_NAME>(const std::string& sender_appid);
+ <CLS_NAME>(std::string sender_appid, bool is_system = false);
virtual ~<CLS_NAME>();
<PUBLIC_MEMBERS>
bundle* GetBundleFromParcel(rpc_port_parcel_h p, bundle* b);
rpc_port_parcel_h GetParcelFromBundle(bundle* b);
- std::string GetAppId();
std::string GetEventName();
- std::string GetEventName(const std::string& sender_appid);
- void Subscribe(const std::string& sender_appid);
+ void Subscribe();
void Unsubscribe();
static void EventCb(const char* event_name, bundle* event_data, void* user_data);
private:
event_handler_h event_handler_ = nullptr;
+ std::string sender_appid_;
std::recursive_mutex mutex_;
+ bool is_system_;
};
)__cpp_cb";
R"__cpp_cb(
#pragma once
-#include <app_common.h>
#include <app_event.h>
#include <bundle.h>
#include <rpc-port-parcel.h>
private EventReceiver _eventReceiver;
private Object _lock = new Object();
private bool _disposedValue = false;
+ private string _senderAppid;
private Bundle GetBundleFromParcel(Parcel p)
{
public <CLS_NAME>(string senderAppid)
{
string eventName = GetEventName(senderAppid, "<CLS_NAME>");
+ _senderAppid = senderAppid;
_eventReceiver = new EventReceiver(eventName);
_eventReceiver.Received += (obj, args) =>
{
};
}
- public <CLS_NAME>()
- {
- }
-
~<CLS_NAME>()
{
Dispose(false);
p.WriteInt((int)MethodId.<METHOD_ID>);
<PARAM_SERIALIZER>
var bundle = GetBundleFromParcel(p);
- string eventName = GetEventName(Application.Current.ApplicationInfo.ApplicationId, "<CLS_NAME>");
+ string eventName = GetEventName(_senderAppid, "<CLS_NAME>");
ApplicationEventManager.Publish(eventName, bundle);
}
}