-#include <stdio.h>
-#include <stdlib.h>
-#include <glib.h>
-#include <vconf.h>
-#include <bundle.h>
-#include <eventsystem.h>
-
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "vconf_event_handler.hh"
+
+#include <memory>
#include <log.hh>
+#include <eventsystem.h>
-namespace {
-
-/* table item : sent system-event by esd */
-static GHashTable *esd_sent_table;
-
-typedef struct __esd_sent_table_item {
- char *event_name;
- bundle *event_data;
-} esd_sent_item;
-
-struct esd_vconf_handler {
- const char *key;
- void (*esd_vconfcb_fn) (keynode_t *node, void *user_data);
-};
-
-int __esd_event_data_compare(bundle *b1, bundle *b2, const char *key) {
- int ret = 0;
- int tmp1 = 0;
- int tmp2 = 0;
- char *str1 = NULL;
- char *str2 = NULL;
-
- if (bundle_get_count(b1) == bundle_get_count(b2)) {
- tmp1 = bundle_get_str(b1, key, &str1);
- tmp2 = bundle_get_str(b2, key, &str2);
- if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) {
- if (strcmp(str1, str2) != 0) {
- _D("new event_data : value check");
- ret = 1;
- }
- }
- } else {
- _D("new event_data : bundle_count check");
- ret = 1;
- }
-
- if (ret == 0)
- _D("same event_data");
-
- return ret;
-}
-
-int __esd_send_system_event(const char *event_name, bundle *b, const char *key) {
- int ret = ES_R_OK;
- esd_sent_item *item =
- (esd_sent_item *)g_hash_table_lookup(esd_sent_table, event_name);
-
- if (item && __esd_event_data_compare(item->event_data, b, key) == 0) {
- _D("skip send: same with previous data");
- } else {
- ret = eventsystem_send_system_event(event_name, b);
- if (ret != ES_R_OK) {
- _E("failed to send event");
- goto out;
- }
-
- if (item) {
- bundle_free(item->event_data);
- item->event_data = bundle_dup(b);
- } else {
- item = (esd_sent_item *)calloc(1, sizeof(esd_sent_item));
- if (item == NULL) {
- _E("memory alloc failed");
- ret = ES_R_ERROR;
- goto out;
- }
- item->event_name = strdup(event_name);
- if (item->event_name == NULL) {
- _E("out of memory");
- free(item);
- item = nullptr;
- ret = ES_R_ERROR;
- goto out;
- }
- item->event_data = bundle_dup(b);
- }
-
- g_hash_table_insert(esd_sent_table, item->event_name, item);
- }
+namespace esd::module {
-out:
- return ret;
-}
-
-void __esd_vconfcb_location_use_mylocation(keynode_t *node, void *user_data) {
+void VconfEventHandler::LocationUseMyLocationCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_LOCATION_ENABLE_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_LOCATION_ENABLE_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_location_enabled(keynode_t *node, void *user_data) {
+void VconfEventHandler::LocationEnabledCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_GPS_ENABLE_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_GPS_ENABLE_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_location_network_enabled(keynode_t *node, void *user_data) {
+void VconfEventHandler::LocationNetworkEnabledCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_NPS_ENABLE_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_NPS_ENABLE_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_language_set(keynode_t *node, void *user_data) {
+void VconfEventHandler::LangsetCb(keynode_t *node, void *user_data) {
char *str = 0;
bundle *b = NULL;
const char *key = NULL;
b = bundle_create();
bundle_add_str(b, key, str);
- if (__esd_send_system_event(SYS_EVENT_LANGUAGE_SET, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_LANGUAGE_SET), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
if (str)
free(str);
-}
+};
-void __esd_vconfcb_hour_format(keynode_t *node, void *user_data) {
+void VconfEventHandler::RegionFormatHourCb(keynode_t *node, void *user_data) {
int ret = 0;
int hours = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_HOUR_FORMAT, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_HOUR_FORMAT), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_region_format(keynode_t *node, void *user_data) {
+void VconfEventHandler::RegionFormatCb(keynode_t *node, void *user_data) {
char *str = 0;
bundle *b = NULL;
const char *key = NULL;
b = bundle_create();
bundle_add_str(b, key, str);
- if (__esd_send_system_event(SYS_EVENT_REGION_FORMAT, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_REGION_FORMAT), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
if (str)
free(str);
-}
+};
-void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data) {
+void VconfEventHandler::VibrationStatusCb(keynode_t *node, void *user_data) {
int ret = 0;
int vibration_on = 0;
int sound_on = 0;
val = EVT_VAL_VIBRATION_ON;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
val = EVT_VAL_SILENTMODE_OFF;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
val = EVT_VAL_VIBRATION_OFF;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
val = EVT_VAL_SILENTMODE_ON;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
}
}
-}
+};
-void __esd_vconfcb_sound_status(keynode_t *node, void *user_data) {
+void VconfEventHandler::SoundStatusCb(keynode_t *node, void *user_data) {
int ret = 0;
int vibration_on = 0;
int sound_on = 0;
val = EVT_VAL_VIBRATION_OFF;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_VIBRATION_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
val = EVT_VAL_SILENTMODE_OFF;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
val = EVT_VAL_SILENTMODE_ON;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_SILENT_MODE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
}
}
-}
+};
-void __esd_vconfcb_auto_rotate(keynode_t *node, void *user_data) {
+void VconfEventHandler::AutoRotateCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_SCREEN_AUTOROTATE_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_SCREEN_AUTOROTATE_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_mobiledata_state(keynode_t *node, void *user_data) {
+void VconfEventHandler::MobileDataStateCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_MOBILE_DATA_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_MOBILE_DATA_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_roaming_state(keynode_t *node, void *user_data) {
+void VconfEventHandler::RoamingStateCb(keynode_t *node, void *user_data) {
int ret = 0;
int enabled = 0;
bundle *b = NULL;
b = bundle_create();
bundle_add_str(b, key, val);
- if (__esd_send_system_event(SYS_EVENT_DATA_ROAMING_STATE, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_DATA_ROAMING_STATE), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
-}
+};
-void __esd_vconfcb_font_set(keynode_t *node, void *user_data) {
+void VconfEventHandler::FontSetCb(keynode_t *node, void *user_data) {
char *str = 0;
bundle *b = NULL;
const char *key = NULL;
b = bundle_create();
bundle_add_str(b, key, str);
- if (__esd_send_system_event(SYS_EVENT_FONT_SET, b, key) != ES_R_OK)
+ if (SendSytemEvent(std::string(SYS_EVENT_FONT_SET), b,
+ std::string(key)) != ES_R_OK)
_E("failed to send event");
if (b)
bundle_free(b);
if (str)
free(str);
+};
+
+void VconfEventHandler::SetDefaultEvents() {
+ vconf_notify_key_changed(VCONFKEY_LOCATION_USE_MY_LOCATION, LocationUseMyLocationCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_LOCATION_ENABLED, LocationEnabledCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_LOCATION_NETWORK_ENABLED, LocationNetworkEnabledCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_LANGSET, LangsetCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, RegionFormatHourCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, RegionFormatCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, VibrationStatusCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, SoundStatusCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, AutoRotateCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_3G_ENABLE, MobileDataStateCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, RoamingStateCb, NULL);
+ vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, FontSetCb, NULL);
}
-struct esd_vconf_handler vconf_callbacks[] = {
- {VCONFKEY_LOCATION_USE_MY_LOCATION, __esd_vconfcb_location_use_mylocation},
- {VCONFKEY_LOCATION_ENABLED, __esd_vconfcb_location_enabled},
- {VCONFKEY_LOCATION_NETWORK_ENABLED, __esd_vconfcb_location_network_enabled},
- {VCONFKEY_LANGSET, __esd_vconfcb_language_set},
- {VCONFKEY_REGIONFORMAT_TIME1224, __esd_vconfcb_hour_format},
- {VCONFKEY_REGIONFORMAT, __esd_vconfcb_region_format},
- {VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __esd_vconfcb_vibration_status},
- {VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __esd_vconfcb_sound_status},
- {VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __esd_vconfcb_auto_rotate},
- {VCONFKEY_3G_ENABLE, __esd_vconfcb_mobiledata_state},
- {VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, __esd_vconfcb_roaming_state},
- {VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, __esd_vconfcb_font_set},
-};
+std::map<std::string, bundle*> VconfEventHandler::event_table_;
+
+int VconfEventHandler::SendSytemEvent(std::string event_name, bundle *b, std::string key) {
+ int ret = ES_R_OK;
+ bundle* data = nullptr;
+ const auto& it = event_table_.find(event_name);
+ if (it != event_table_.end())
+ data = it->second;
+
+ if (data && EventDataCompare(data, b, key) == 0) {
+ _D("skip send: same with previous data");
+ } else {
+ ret = eventsystem_send_system_event(event_name.c_str(), b);
+ if (ret != ES_R_OK) {
+ _E("failed to send event");
+ return ret;
+ }
-int vconfcbs_size = sizeof(vconf_callbacks)/sizeof(struct esd_vconf_handler);
+ if (data)
+ bundle_free(data);
-} // namespace
+ event_table_[event_name] = bundle_dup(b);
+ }
+
+ return ret;
+}
-int __esd_register_vconf_callbacks() {
- int i = 0;
+int VconfEventHandler::EventDataCompare(bundle* b1, bundle* b2, std::string key) {
int ret = 0;
- int result = ES_R_OK;
-
- esd_sent_table = g_hash_table_new(g_str_hash, g_str_equal);
-
- _D("vconf callbacks size(%d)", vconfcbs_size);
- for (i = 0; i < vconfcbs_size; i++) {
- ret = vconf_notify_key_changed(vconf_callbacks[i].key,
- vconf_callbacks[i].esd_vconfcb_fn, NULL);
- if (ret != VCONF_OK) {
- _E("failed to register vconf callback (%s)", vconf_callbacks[i].key);
- result = ES_R_ERROR;
- break;
+ int tmp1 = 0;
+ int tmp2 = 0;
+ char *str1 = NULL;
+ char *str2 = NULL;
+
+ if (bundle_get_count(b1) == bundle_get_count(b2)) {
+ tmp1 = bundle_get_str(b1, key.c_str(), &str1);
+ tmp2 = bundle_get_str(b2, key.c_str(), &str2);
+ if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) {
+ if (strcmp(str1, str2) != 0) {
+ _D("new event_data : value check");
+ ret = 1;
+ }
}
+ } else {
+ _D("new event_data : bundle_count check");
+ ret = 1;
}
- return result;
+ if (ret == 0)
+ _D("same event_data");
+
+ return ret;
}
+
+} // namespace
\ No newline at end of file