From: Youngjae Shin Date: Wed, 4 Mar 2020 03:11:30 +0000 (+0900) Subject: revise vconf changed callbacks X-Git-Tag: submit/tizen/20200406.072014~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b7a2c7e52092c75d7e9b13072fb2697cccb45886;p=platform%2Fcore%2Fsystem%2Fmodes-plugins.git revise vconf changed callbacks --- diff --git a/CMakeLists.txt b/CMakeLists.txt index fd4247e..cf8ccb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ IF(NOT DEFINED MODES_PLUGIN_DEFAULT_DIR) SET(MODES_PLUGIN_DEFAULT_DIR "/usr/lib/modes-plugins/") ENDIF(NOT DEFINED MODES_PLUGIN_DEFAULT_DIR) +SET(VCONF_COMMON_SRC ${CMAKE_SOURCE_DIR}/common/VconfCbHandler.cpp) + ADD_SUBDIRECTORY(wifi) ADD_SUBDIRECTORY(vconf) ADD_SUBDIRECTORY(app) diff --git a/common/VconfCbHandler.cpp b/common/VconfCbHandler.cpp new file mode 100644 index 0000000..e4577ca --- /dev/null +++ b/common/VconfCbHandler.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "VconfCbHandler.h" + +#include +#include +#include +#include +#include "plugin-log.h" +#include "VconfChangeAction.h" + +MODES_NAMESPACE_USE; + +std::map VconfCbHandler::callbackMap; + +int VconfCbHandler::handleSubscription(const std::string &key) +{ + auto found = callbackMap.find(key); + if (callbackMap.end() != found) { + WARN("Acition(%s) already exist", key.c_str()); + found->second->vconfChangedCB(); + + callbackMap.erase(found); + int ret = vconf_ignore_key_changed(key.c_str(), vconfCallback); + if (0 != ret) { + ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret); + return MODES_ERROR_SYSTEM; + } + } + return MODES_ERROR_NONE; +} + +int VconfCbHandler::setChangedCB(const std::string &key, VconfChangeAction *action) +{ + RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); + + int ret = vconf_notify_key_changed(key.c_str(), vconfCallback, NULL); + if (0 != ret) { + ERR("vconf_notify_key_changed(%s) Fail(%d)", key.c_str(), ret); + return MODES_ERROR_SYSTEM; + } + + callbackMap[key] = action; + + return MODES_ERROR_NONE; +} + +void VconfCbHandler::unSetChangedCB(const std::string &key) +{ + auto found = callbackMap.find(key); + if (callbackMap.end() == found) { + ERR("No Changed Callback(%s)", key.c_str()); + return; + } + + callbackMap.erase(found); + int ret = vconf_ignore_key_changed(key.c_str(), vconfCallback); + if (0 != ret) { + ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret); + return; + } +} + +void VconfCbHandler::vconfCallback(keynode_t *node, void *userData) +{ + auto found = callbackMap.find(node->keyname); + if (callbackMap.end() == found) { + ERR("No VconfAction(%s)", node->keyname); + return; + } + + found->second->vconfChangedCB(node); + DBG("Action(%s) is Changed", node->keyname); +} diff --git a/display/CMakeLists.txt b/display/CMakeLists.txt index 85bf7cc..e6add36 100644 --- a/display/CMakeLists.txt +++ b/display/CMakeLists.txt @@ -7,7 +7,7 @@ PKG_CHECK_MODULES(DISPLAY_pkgs REQUIRED modes dlog capi-base-common capi-system- INCLUDE_DIRECTORIES(${DISPLAY_pkgs_INCLUDE_DIRS}) LINK_DIRECTORIES(${DISPLAY_pkgs_LIBRARY_DIRS}) -ADD_LIBRARY(${DISPLAY_PLUGIN} SHARED ${DISPLAY_SRCS}) +ADD_LIBRARY(${DISPLAY_PLUGIN} SHARED ${DISPLAY_SRCS} ${VCONF_COMMON_SRC}) TARGET_LINK_LIBRARIES(${DISPLAY_PLUGIN} ${DISPLAY_pkgs_LIBRARIES}) SET_TARGET_PROPERTIES(${DISPLAY_PLUGIN} PROPERTIES NO_SONAME 1 ) INSTALL(TARGETS ${DISPLAY_PLUGIN} DESTINATION ${MODES_PLUGIN_DEFAULT_DIR}) diff --git a/display/DisplayActVconf.cpp b/display/DisplayActVconf.cpp new file mode 100644 index 0000000..a9e8d07 --- /dev/null +++ b/display/DisplayActVconf.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "DisplayActVconf.h" + +#include +#include +#include +#include +#include "plugin-log.h" +#include "VconfCbHandler.h" + +MODES_NAMESPACE_USE; + +// It should align with actionKey in DisplayFactory +const std::string DisplayActVconf::NAME[2] = { + "autoBrightness", + "timeout", +}; +const char* const DisplayActVconf::KEY[2] = { + VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, + VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, +}; + +DisplayActVconf::DisplayActVconf(int type) + :DisplayAction(NAME[type]), keyType(type), + requestVal(-1), oldVal(-1), cb(nullptr), cbData(nullptr) +{ +} + +int DisplayActVconf::set(int val) +{ + requestVal = val; + + int ret = cbHandler.handleSubscription(KEY[keyType]); + if (MODES_ERROR_NONE != ret) { + ERR("handleChange(%s) Fail(%d)", KEY[keyType], ret); + return ret; + } + + ret = vconf_get_int(KEY[keyType], &oldVal); + if (0 != ret) { + ERR("vconf_get_int(%s) Fail(%d)", KEY[keyType], ret); + return MODES_ERROR_SYSTEM; + } + + if (oldVal == val) { + INFO("%s already set(%d)", NAME[keyType].c_str(), val); + } else { + ret = vconf_set_int(KEY[keyType], val); + if (0 != ret) { + ERR("vconf_set_int(%s, %d) Fail(%d)", KEY[keyType], val, ret); + return MODES_ERROR_SYSTEM; + } + } + + return MODES_ERROR_NONE; +} + +void DisplayActVconf::undo() +{ + RET_IF(-1 == oldVal); + + int ret = vconf_set_int(KEY[keyType], oldVal); + if (0 != ret) + ERR("vconf_set_int(%s, %d) Fail(%d)", KEY[keyType], oldVal, ret); +} + +std::string DisplayActVconf::serialize() +{ + std::ostringstream ostr; + + ostr << oldVal; + return ostr.str(); +} + +int DisplayActVconf::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> oldVal; + + return MODES_ERROR_NONE; +} + +int DisplayActVconf::setChangedCallback(valueChangedCB callback, void *userData) +{ + RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER); + + int ret = cbHandler.setChangedCB(KEY[keyType], this); + if (MODES_ERROR_NONE != ret) { + ERR("setChangedCB(%s) Fail(%d)", KEY[keyType], ret); + return ret; + } + + cb = callback; + cbData = userData; + + return MODES_ERROR_NONE; +} + +void DisplayActVconf::unSetChangedCallback(valueChangedCB callback, void *userData) +{ + RET_IF(nullptr == callback); + + cbHandler.unSetChangedCB(KEY[keyType]); + + cbData = nullptr; + cb = nullptr; +} + +void DisplayActVconf::vconfChangedCB() +{ + //The value will be changed by Modes. That's why the callback is always called. + if (cb) + cb(cbData); +} + +void DisplayActVconf::vconfChangedCB(keynode_t *node) +{ + const char *vconfKey = vconf_keynode_get_name(node); + if (vconfKey != KEY[keyType]) { + WARN("Unknown vconf(%s) notification", vconfKey); + return; + } + + int val = vconf_keynode_get_int(node); + + if (cb && (val != requestVal)) + cb(cbData); +} diff --git a/display/DisplayActVconf.h b/display/DisplayActVconf.h new file mode 100644 index 0000000..2d2d7cc --- /dev/null +++ b/display/DisplayActVconf.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +#pragma once + +#include +#include "plugin-def.h" +#include "DisplayAction.h" +#include "VconfCbHandler.h" + +MODES_NAMESPACE_BEGIN + +class DisplayActVconf : public DisplayAction, public VconfChangeAction { +public: + DisplayActVconf(int type); + + int set(int val) override; + void undo() override; + std::string serialize() override; + int parse(const std::string &archive) override; + int setChangedCallback(valueChangedCB callback, void *userData) override; + void unSetChangedCallback(valueChangedCB callback, void *userData) override; + + void vconfChangedCB() override; + void vconfChangedCB(keynode_t *node) override; + + static const std::string NAME[2]; + static const char* const KEY[2]; +private: + int keyType; + int requestVal; + int oldVal; + VconfCbHandler cbHandler; + valueChangedCB cb; + void *cbData; +}; + +MODES_NAMESPACE_END diff --git a/display/DisplayAutoBrightness.cpp b/display/DisplayAutoBrightness.cpp deleted file mode 100644 index cb24efb..0000000 --- a/display/DisplayAutoBrightness.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 "DisplayAutoBrightness.h" -#include -#include -#include -#include -#include "plugin-log.h" - -MODES_NAMESPACE_USE; - -const std::string DisplayAutoBrightness::NAME = "autoBrightness"; -const char* const DisplayAutoBrightness::key = VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT; - -DisplayAutoBrightness::DisplayAutoBrightness() - :DisplayAction(NAME), oldVal(-1), cb(nullptr), cbData(nullptr) -{ -} - -DisplayAutoBrightness::~DisplayAutoBrightness() -{ -} - -int DisplayAutoBrightness::set(int val) -{ - int ret = vconf_get_int(key, &oldVal); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", key, ret); - return MODES_ERROR_SYSTEM; - } - - if (oldVal == val) { - INFO("Auto brightness already set(%d)", val); - return MODES_ERROR_NONE; - } - - ret = vconf_set_int(key, val); - if (0 != ret) { - ERR("vconf_set_int(%s, %d) Fail(%d)", key, val, ret); - return MODES_ERROR_SYSTEM; - } - - return MODES_ERROR_NONE; -} - -void DisplayAutoBrightness::undo() -{ - RET_IF(-1 == oldVal); - - int ret = vconf_set_int(key, oldVal); - if (0 != ret) - ERR("vconf_set_int(%s, %d) Fail(%d)", key, oldVal, ret); -} - -std::string DisplayAutoBrightness::serialize() -{ - std::ostringstream ostr; - - ostr << oldVal; - return ostr.str(); -} - -int DisplayAutoBrightness::parse(const std::string &archive) -{ - std::istringstream iss(archive); - iss >> oldVal; - - return MODES_ERROR_NONE; -} - -int DisplayAutoBrightness::setChangedCallback(valueChangedCB callback, void *userData) -{ - cbData = userData; - cb = callback; - int ret = vconf_notify_key_changed(key, autoBrightnessChangedCB, this); - if (0 != ret) { - ERR("vconf_notify_key_changed(%s) Fail(%d)", key, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; -} - -void DisplayAutoBrightness::unSetChangedCallback(valueChangedCB callback, void *userData) -{ - int ret = vconf_ignore_key_changed(key, autoBrightnessChangedCB); - if (0 != ret) - ERR("vconf_ignore_key_changed(%s) Fail(%d)", key, ret); - - cbData = NULL; - cb = NULL; -} - -void DisplayAutoBrightness::autoBrightnessChangedCB(keynode_t *node, void *userData) -{ - DisplayAutoBrightness *action = (DisplayAutoBrightness*)userData; - - RET_IF(NULL == userData); - - int val = 0; - int ret = vconf_get_int(key, &val); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", key, ret); - return; - } - - if (action->cb && (val != action->oldVal)) - action->cb(action->cbData); -} diff --git a/display/DisplayAutoBrightness.h b/display/DisplayAutoBrightness.h deleted file mode 100644 index 528664e..0000000 --- a/display/DisplayAutoBrightness.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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. - */ -#pragma once - -#include -#include -#include "plugin-def.h" -#include "DisplayAction.h" - -MODES_NAMESPACE_BEGIN - -class DisplayAutoBrightness : public DisplayAction { -public: - DisplayAutoBrightness(); - ~DisplayAutoBrightness(); - - int set(int val) override; - void undo() override; - std::string serialize() override; - int parse(const std::string &archive) override; - - int setChangedCallback(valueChangedCB callback, void *userData) override; - void unSetChangedCallback(valueChangedCB callback, void *userData) override; - - static const std::string NAME; - -private: - static void autoBrightnessChangedCB(keynode_t *node, void *userData); - - static const char* const key; - int oldVal; - valueChangedCB cb; - void *cbData; -}; - -MODES_NAMESPACE_END diff --git a/display/DisplayFactory.cpp b/display/DisplayFactory.cpp index b3e2dda..8497b8a 100644 --- a/display/DisplayFactory.cpp +++ b/display/DisplayFactory.cpp @@ -17,18 +17,17 @@ #include #include #include "plugin-log.h" +#include "DisplayActVconf.h" #include "DisplayBrightness.h" -#include "DisplayAutoBrightness.h" -#include "DisplayTimeout.h" #include "DisplayAllowPalmTouch.h" MODES_NAMESPACE_USE; DisplayFactory::DisplayFactory() { + actionMap[DisplayActVconf::NAME[AUTO_BRIGHTNESS_VCONF]] = AUTO_BRIGHTNESS_VCONF; + actionMap[DisplayActVconf::NAME[TIMEOUT_VCONF]] = TIMEOUT_VCONF; actionMap[DisplayBrightness::NAME] = BRIGHTNESS; - actionMap[DisplayAutoBrightness::NAME] = AUTO_BRIGHTNESS; - actionMap[DisplayTimeout::NAME] = TIMEOUT; actionMap[DisplayAllowPalmTouch::NAME] = ALLOW_PALM_TOUCH; } @@ -42,14 +41,14 @@ DisplayAction* DisplayFactory::createAction(const std::string &key) DisplayAction *action; switch (search->second) { - case BRIGHTNESS: - action = new DisplayBrightness(); + case AUTO_BRIGHTNESS_VCONF: + action = new DisplayActVconf(AUTO_BRIGHTNESS_VCONF); break; - case AUTO_BRIGHTNESS: - action = new DisplayAutoBrightness(); + case TIMEOUT_VCONF: + action = new DisplayActVconf(TIMEOUT_VCONF); break; - case TIMEOUT: - action = new DisplayTimeout(); + case BRIGHTNESS: + action = new DisplayBrightness(); break; case ALLOW_PALM_TOUCH: action = new DisplayAllowPalmTouch(); diff --git a/display/DisplayFactory.h b/display/DisplayFactory.h index a440e3d..f0602a9 100644 --- a/display/DisplayFactory.h +++ b/display/DisplayFactory.h @@ -30,9 +30,9 @@ public: void destroyAction(DisplayAction *action); private: enum actionKey{ + AUTO_BRIGHTNESS_VCONF, + TIMEOUT_VCONF, BRIGHTNESS, - AUTO_BRIGHTNESS, - TIMEOUT, ALLOW_PALM_TOUCH, }; diff --git a/display/DisplayTimeout.cpp b/display/DisplayTimeout.cpp deleted file mode 100644 index d68f0e9..0000000 --- a/display/DisplayTimeout.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 "DisplayTimeout.h" -#include -#include -#include -#include -#include "plugin-log.h" - -MODES_NAMESPACE_USE; - -const std::string DisplayTimeout::NAME = "timeout"; -DisplayTimeout::DisplayTimeout() - :DisplayAction(NAME), key(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL), oldDisplayTimeout(-1) -{ -} - -DisplayTimeout::~DisplayTimeout() -{ -} - -int DisplayTimeout::set(int val) -{ - int ret = vconf_get_int(key, &oldDisplayTimeout); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", key, ret); - return MODES_ERROR_SYSTEM; - } - ret = vconf_set_int(key, val); - if (0 != ret) { - ERR("vconf_set_int(%s, %d) Fail(%d)", key, val, ret); - return MODES_ERROR_SYSTEM; - } - - return MODES_ERROR_NONE; -} - -void DisplayTimeout::undo() -{ - DBG("undo Display Timeout(%s, %d)", key, oldDisplayTimeout); - int ret = vconf_set_int(key, oldDisplayTimeout); - if (0 != ret) - ERR("vconf_set_int(%s, %d) Fail(%d)", key, oldDisplayTimeout, ret); -} - -std::string DisplayTimeout::serialize() -{ - std::ostringstream ostr; - - ostr << oldDisplayTimeout; - return ostr.str(); -} - -int DisplayTimeout::parse(const std::string &archive) -{ - std::istringstream iss(archive); - iss >> oldDisplayTimeout; - - return MODES_ERROR_NONE; -} diff --git a/display/DisplayTimeout.h b/display/DisplayTimeout.h deleted file mode 100644 index c908e15..0000000 --- a/display/DisplayTimeout.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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. - */ -#pragma once - -#include -#include "plugin-def.h" -#include "DisplayAction.h" - -MODES_NAMESPACE_BEGIN - -class DisplayTimeout : public DisplayAction { -public: - DisplayTimeout(); - ~DisplayTimeout(); - - int set(int val) override; - void undo() override; - std::string serialize() override; - int parse(const std::string &archive) override; - - static const std::string NAME; -private: - const char* const key; - int oldDisplayTimeout; -}; - -MODES_NAMESPACE_END diff --git a/include/VconfCbHandler.h b/include/VconfCbHandler.h new file mode 100644 index 0000000..7e0d379 --- /dev/null +++ b/include/VconfCbHandler.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +#pragma once + +#include +#include +#include +#include "plugin-def.h" +#include "VconfChangeAction.h" + +MODES_NAMESPACE_BEGIN + +class VconfCbHandler { +public: + int handleSubscription(const std::string &key); + int setChangedCB(const std::string &key, VconfChangeAction *action); + void unSetChangedCB(const std::string &key); +private: + static void vconfCallback(keynode_t *node, void *userData); + static std::map callbackMap; +}; + +MODES_NAMESPACE_END + diff --git a/include/VconfChangeAction.h b/include/VconfChangeAction.h new file mode 100644 index 0000000..108a08b --- /dev/null +++ b/include/VconfChangeAction.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +#pragma once + +#include +#include "plugin-def.h" + +MODES_NAMESPACE_BEGIN + +class VconfChangeAction { +public: + virtual void vconfChangedCB() = 0; + virtual void vconfChangedCB(keynode_t *node) = 0; +}; + +MODES_NAMESPACE_END diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index 65d9a7f..48927dd 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -60,7 +60,7 @@ SET(VCONF_PLUGIN_TEST "modes-plugintest-vconf") FILE(GLOB VCONF_SRCS ${VCONF_SRC_DIR}/*.cpp) SET(VCONF_SRCS ${VCONF_SRCS} "mdsp_test_vconf.cpp") -ADD_EXECUTABLE(${VCONF_PLUGIN_TEST} ${VCONF_SRCS}) +ADD_EXECUTABLE(${VCONF_PLUGIN_TEST} ${VCONF_SRCS} ${VCONF_COMMON_SRC}) TARGET_LINK_LIBRARIES(${VCONF_PLUGIN_TEST} ${test_pkgs_LIBRARIES}) INSTALL(TARGETS ${VCONF_PLUGIN_TEST} DESTINATION ${TEST_INSTALL_DIR}) #===================================================================# @@ -78,6 +78,6 @@ SET(DISPLAY_PLUGIN_TEST "modes-plugintest-display") FILE(GLOB DISPLAY_SRCS ${DISPLAY_SRC_DIR}/*.cpp) SET(DISPLAY_SRCS ${DISPLAY_SRCS} "mdsp_test_display.cpp") -ADD_EXECUTABLE(${DISPLAY_PLUGIN_TEST} ${DISPLAY_SRCS}) +ADD_EXECUTABLE(${DISPLAY_PLUGIN_TEST} ${DISPLAY_SRCS} ${VCONF_COMMON_SRC}) TARGET_LINK_LIBRARIES(${DISPLAY_PLUGIN_TEST} ${test_pkgs_LIBRARIES}) INSTALL(TARGETS ${DISPLAY_PLUGIN_TEST} DESTINATION ${TEST_INSTALL_DIR}) diff --git a/unittests/mdsp_test_vconf.cpp b/unittests/mdsp_test_vconf.cpp index e364e11..7feb4f1 100644 --- a/unittests/mdsp_test_vconf.cpp +++ b/unittests/mdsp_test_vconf.cpp @@ -71,7 +71,7 @@ protected: static gboolean changedCallbackidler(gpointer data) { - DBG("start change the value db/setting/mode"); + DBG("start change the value db/setting/psmode"); vconf_set_int("db/setting/psmode", 7); return G_SOURCE_REMOVE; } @@ -96,17 +96,11 @@ TEST_F(VconfPluginTest, VconfInt) ret = plugin->set(key, 0, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - int val = plugin->getInt(key); - EXPECT_EQ(val, 0); - PluginAction *action; ret = plugin->set(key, 1, &action); EXPECT_EQ(ret, MODES_ERROR_NONE); plugin->undo(action); - - val = plugin->getInt(key); - EXPECT_EQ(val, 0); } TEST_F(VconfPluginTest, VconfDouble) @@ -117,17 +111,11 @@ TEST_F(VconfPluginTest, VconfDouble) ret = plugin->set(key, 0.0, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - double val = plugin->getDouble(key); - EXPECT_EQ(val, 0.0); - PluginAction *action; ret = plugin->set(key, 1.0, &action); EXPECT_EQ(ret, MODES_ERROR_NONE); plugin->undo(action); - - val = plugin->getDouble(key); - EXPECT_EQ(val, 0.0); } TEST_F(VconfPluginTest, VconfBool) @@ -138,17 +126,11 @@ TEST_F(VconfPluginTest, VconfBool) ret = plugin->set(key, true, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - double val = plugin->getBool(key); - EXPECT_EQ(val, true); - PluginAction *action; ret = plugin->set(key, false, &action); EXPECT_EQ(ret, MODES_ERROR_NONE); plugin->undo(action); - - val = plugin->getBool(key); - EXPECT_EQ(val, true); } TEST_F(VconfPluginTest, VconfStr) @@ -161,17 +143,11 @@ TEST_F(VconfPluginTest, VconfStr) ret = plugin->set(key, devName, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - std::string val = plugin->getString(key); - EXPECT_STREQ(val.c_str(), devName.c_str()); - PluginAction *action; ret = plugin->set(key, tmpName, &action); EXPECT_EQ(ret, MODES_ERROR_NONE); plugin->undo(action); - - val = plugin->getString(key); - EXPECT_STREQ(val.c_str(), devName.c_str()); } TEST_F(VconfPluginTest, callbackPluginVconf) diff --git a/vconf/CMakeLists.txt b/vconf/CMakeLists.txt index 6a83822..cb97410 100644 --- a/vconf/CMakeLists.txt +++ b/vconf/CMakeLists.txt @@ -2,19 +2,11 @@ SET(VCONF_PLUGIN "modes-plugin-vconf") FILE(GLOB VCONF_SRCS *.cpp) -pkg_check_modules(vconf_pkgs REQUIRED modes dlog capi-base-common vconf) +PKG_CHECK_MODULES(vconf_pkgs REQUIRED modes dlog capi-base-common vconf) INCLUDE_DIRECTORIES(${vconf_pkgs_INCLUDE_DIRS}) LINK_DIRECTORIES(${vconf_pkgs_LIBRARY_DIRS}) -# ==== WifiErrorMessage Header genertor ==== -#ADD_CUSTOM_TARGET( -# genWifiErrorMessage -# COMMAND echo "Run WifiErrorMessage Generator" -# COMMAND tool/WifiErrorMessageGenerator.sh -#) -#ADD_DEPENDENCIES(${VCONF_PLUGIN} genWifiErrorMessage) - -ADD_LIBRARY(${VCONF_PLUGIN} SHARED ${VCONF_SRCS}) +ADD_LIBRARY(${VCONF_PLUGIN} SHARED ${VCONF_SRCS} ${VCONF_COMMON_SRC}) TARGET_LINK_LIBRARIES(${VCONF_PLUGIN} ${vconf_pkgs_LIBRARIES}) SET_TARGET_PROPERTIES(${VCONF_PLUGIN} PROPERTIES NO_SONAME 1 ) INSTALL(TARGETS ${VCONF_PLUGIN} DESTINATION ${MODES_PLUGIN_DEFAULT_DIR}) diff --git a/vconf/VconfAction.cpp b/vconf/VconfAction.cpp deleted file mode 100644 index 32a551f..0000000 --- a/vconf/VconfAction.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 "VconfAction.h" - -#include -#include -#include -#include "plugin-log.h" - -MODES_NAMESPACE_USE; - -std::map VconfAction::callbackMap; - -VconfAction::VconfAction(const std::string &name, const std::string &vconfKey) - : PluginAction(name), key(vconfKey) -{ -} - -int VconfAction::setChangedCallback(valueChangedCB callback, void *userData) -{ - callbackMap[key].userData = userData; - callbackMap[key].callback = callback; - - int ret = vconf_notify_key_changed(key.c_str(), vconfChangedCallback, &callbackMap[key]); - if (0 != ret) { - ERR("vconf_notify_key_changed(%s) Fail(%d)", key.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - - DBG("setChangedCallback(%s) Success", key.c_str()); - return MODES_ERROR_NONE; -} - -void VconfAction::unSetChangedCallback(valueChangedCB callback, void *userData) -{ - auto found = callbackMap.find(key); - if (found == callbackMap.end()) { - ERR("No Changed Callback(%s)", key.c_str()); - return; - } - - callbackMap.erase(found); - int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback); - if (0 != ret) { - ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret); - return; - } - - DBG("unSetChangedCallback(%s) Success", key.c_str()); -} - -int VconfAction::handleChange() -{ - auto found = callbackMap.find(key); - if (callbackMap.end() != found) { - DBG("Acition(%s) already exist", key.c_str()); - found->second.callback(found->second.userData); - - callbackMap.erase(found); - int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback); - if (0 != ret) { - ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - } - return MODES_ERROR_NONE; -} - -void VconfAction::vconfChangedCallback(keynode_t *node, void *userData) -{ - CallbackData *cbData = (CallbackData*)userData; - - RET_IF(NULL == userData); - - auto found = callbackMap.find(node->keyname); - if (&(found->second) != cbData) { - ERR("Unknown callbackData(%s)", node->keyname); - return; - } - - cbData->callback(cbData->userData); - DBG("Action(%s) is Changed", node->keyname); -} diff --git a/vconf/VconfAction.h b/vconf/VconfAction.h deleted file mode 100644 index 5bc0af3..0000000 --- a/vconf/VconfAction.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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. - */ -#pragma once - -#include -#include -#include -#include -#include "plugin-def.h" - -MODES_NAMESPACE_BEGIN - -class VconfAction : public PluginAction { -public: - VconfAction(const std::string &name, const std::string &vconfKey); - virtual ~VconfAction() = default; - - int setChangedCallback(valueChangedCB callback, void *userData) override; - void unSetChangedCallback(valueChangedCB callback, void *userData) override; -protected: - int handleChange(); - std::string key; -private: - struct CallbackData { - void *userData; - valueChangedCB callback; - }; - static void vconfChangedCallback(keynode_t *node, void *userData); - static std::map callbackMap; -}; - -MODES_NAMESPACE_END - diff --git a/vconf/VconfActionBool.cpp b/vconf/VconfActionBool.cpp index c58b06e..23209ad 100644 --- a/vconf/VconfActionBool.cpp +++ b/vconf/VconfActionBool.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ #include "VconfActionBool.h" + #include #include #include @@ -21,17 +22,16 @@ MODES_NAMESPACE_USE; -VconfActionBool::VconfActionBool(const std::string &key) - : VconfAction("boolVconf", key), oldVal(false) +VconfActionBool::VconfActionBool(const std::string &vconfKey) + : PluginAction("boolVconf"), key(vconfKey), requestVal(false), oldVal(false), cb(nullptr), cbData(nullptr) { } int VconfActionBool::set(bool val) { - int ret; - DBG("setBool(%s, %d)", key.c_str(), val); + requestVal = val; - ret = handleChange(); + int ret = cbHandler.handleSubscription(key); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", key.c_str(), ret); return ret; @@ -45,32 +45,22 @@ int VconfActionBool::set(bool val) } oldVal = prev; - ret = vconf_set_bool(key.c_str(), val); - if (0 != ret) { - ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; -} - -bool VconfActionBool::get() -{ - int value = 0; - int ret = vconf_get_bool(key.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_bool(%s) Fail(%d)", key.c_str(), ret); - return false; + if (oldVal == val) { + INFO("vconf(%s) already set(%d)", key.c_str(), val); + } else { + ret = vconf_set_bool(key.c_str(), val); + if (0 != ret) { + ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), val, ret); + return MODES_ERROR_SYSTEM; + } } - return value ? true : false; + return MODES_ERROR_NONE; } void VconfActionBool::undo() { - int ret; - DBG("undoBool(%s, %d)", key.c_str(), oldVal); - - ret = vconf_set_bool(key.c_str(), oldVal ? 1 : 0); + int ret = vconf_set_bool(key.c_str(), oldVal ? 1 : 0); if (0 != ret) ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), oldVal, ret); } @@ -90,3 +80,49 @@ int VconfActionBool::parse(const std::string &archive) return MODES_ERROR_NONE; } + +int VconfActionBool::setChangedCallback(valueChangedCB callback, void *userData) +{ + RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER); + + int ret = cbHandler.setChangedCB(key, this); + if (MODES_ERROR_NONE != ret) { + ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret); + return ret; + } + + cb = callback; + cbData = userData; + + return MODES_ERROR_NONE; +} + +void VconfActionBool::unSetChangedCallback(valueChangedCB callback, void *userData) +{ + RET_IF(NULL == callback); + + cbHandler.unSetChangedCB(key); + + cbData = NULL; + cb = NULL; +} + +void VconfActionBool::vconfChangedCB() +{ + //The value will be changed by Modes. That's why the callback is always called. + if (cb) + cb(cbData); +} + +void VconfActionBool::vconfChangedCB(keynode_t *node) +{ + const char *vconfKey = vconf_keynode_get_name(node); + if (vconfKey != key) { + WARN("Unknown vconf(%s) notification", vconfKey); + return; + } + + bool val = vconf_keynode_get_bool(node); + if (cb && (val != requestVal)) + cb(cbData); +} diff --git a/vconf/VconfActionBool.h b/vconf/VconfActionBool.h index 90644ae..2e8c989 100644 --- a/vconf/VconfActionBool.h +++ b/vconf/VconfActionBool.h @@ -16,24 +16,32 @@ #pragma once #include -#include "VconfAction.h" +#include #include "plugin-def.h" +#include "VconfCbHandler.h" MODES_NAMESPACE_BEGIN -class VconfActionBool : public VconfAction { +class VconfActionBool : public PluginAction, public VconfChangeAction { public: - VconfActionBool(const std::string &key); - virtual ~VconfActionBool() = default; + VconfActionBool(const std::string &vconfKey); int set(bool val); - bool get(); void undo() override; std::string serialize() override; int parse(const std::string &archive) override; + int setChangedCallback(valueChangedCB callback, void *userData) override; + void unSetChangedCallback(valueChangedCB callback, void *userData) override; + + void vconfChangedCB() override; + void vconfChangedCB(keynode_t *node) override; private: + std::string key; + bool requestVal; bool oldVal; + VconfCbHandler cbHandler; + valueChangedCB cb; + void *cbData; }; MODES_NAMESPACE_END - diff --git a/vconf/VconfActionDbl.cpp b/vconf/VconfActionDbl.cpp index e5765f0..15bca08 100644 --- a/vconf/VconfActionDbl.cpp +++ b/vconf/VconfActionDbl.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ #include "VconfActionDbl.h" + #include #include #include @@ -21,17 +22,16 @@ MODES_NAMESPACE_USE; -VconfActionDbl::VconfActionDbl(const std::string &key) - : VconfAction("dblVconf", key), oldVal(0.0) +VconfActionDbl::VconfActionDbl(const std::string &vconfKey) + : PluginAction("dblVconf"), key(vconfKey), requestVal(0.0), oldVal(0.0), cb(nullptr), cbData(nullptr) { } int VconfActionDbl::set(double val) { - int ret; - DBG("setDbl(%s, %f)", key.c_str(), val); + requestVal = val; - ret = handleChange(); + int ret = cbHandler.handleSubscription(key); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", key.c_str(), ret); return ret; @@ -43,32 +43,22 @@ int VconfActionDbl::set(double val) return MODES_ERROR_SYSTEM; } - ret = vconf_set_dbl(key.c_str(), val); - if (0 != ret) { - ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; -} - -double VconfActionDbl::get() -{ - double value = 0.0; - int ret = vconf_get_dbl(key.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_dbl(%s) Fail(%d)", key.c_str(), ret); - return 0.0; + if (oldVal == val) { + INFO("vconf(%s) already set(%f)", key.c_str(), val); + } else { + ret = vconf_set_dbl(key.c_str(), val); + if (0 != ret) { + ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), val, ret); + return MODES_ERROR_SYSTEM; + } } - return value; + return MODES_ERROR_NONE; } void VconfActionDbl::undo() { - int ret; - DBG("undoDbl(%s, %f)", key.c_str(), oldVal); - - ret = vconf_set_dbl(key.c_str(), oldVal); + int ret = vconf_set_dbl(key.c_str(), oldVal); if (0 != ret) ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), oldVal, ret); } @@ -88,3 +78,49 @@ int VconfActionDbl::parse(const std::string &archive) return MODES_ERROR_NONE; } + +int VconfActionDbl::setChangedCallback(valueChangedCB callback, void *userData) +{ + RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER); + + int ret = cbHandler.setChangedCB(key, this); + if (MODES_ERROR_NONE != ret) { + ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret); + return ret; + } + + cb = callback; + cbData = userData; + + return MODES_ERROR_NONE; +} + +void VconfActionDbl::unSetChangedCallback(valueChangedCB callback, void *userData) +{ + RET_IF(NULL == callback); + + cbHandler.unSetChangedCB(key); + + cbData = NULL; + cb = NULL; +} + +void VconfActionDbl::vconfChangedCB() +{ + //The value will be changed by Modes. That's why the callback is always called. + if (cb) + cb(cbData); +} + +void VconfActionDbl::vconfChangedCB(keynode_t *node) +{ + const char *vconfKey = vconf_keynode_get_name(node); + if (vconfKey != key) { + WARN("Unknown vconf(%s) notification", vconfKey); + return; + } + + double val = vconf_keynode_get_dbl(node); + if (cb && (val != requestVal)) + cb(cbData); +} diff --git a/vconf/VconfActionDbl.h b/vconf/VconfActionDbl.h index 0fd0bdf..3a98ebe 100644 --- a/vconf/VconfActionDbl.h +++ b/vconf/VconfActionDbl.h @@ -16,24 +16,32 @@ #pragma once #include -#include "VconfAction.h" +#include #include "plugin-def.h" +#include "VconfCbHandler.h" MODES_NAMESPACE_BEGIN -class VconfActionDbl : public VconfAction { +class VconfActionDbl : public PluginAction, public VconfChangeAction { public: - VconfActionDbl(const std::string &key); - virtual ~VconfActionDbl() = default; + VconfActionDbl(const std::string &vconfKey); int set(double val); - double get(); void undo() override; std::string serialize() override; int parse(const std::string &archive) override; + int setChangedCallback(valueChangedCB callback, void *userData) override; + void unSetChangedCallback(valueChangedCB callback, void *userData) override; + + void vconfChangedCB() override; + void vconfChangedCB(keynode_t *node) override; private: + std::string key; + double requestVal; double oldVal; + VconfCbHandler cbHandler; + valueChangedCB cb; + void *cbData; }; MODES_NAMESPACE_END - diff --git a/vconf/VconfActionInt.cpp b/vconf/VconfActionInt.cpp index b6e2bb8..08504f5 100644 --- a/vconf/VconfActionInt.cpp +++ b/vconf/VconfActionInt.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ #include "VconfActionInt.h" + #include #include #include @@ -21,17 +22,16 @@ MODES_NAMESPACE_USE; -VconfActionInt::VconfActionInt(const std::string &key) - : VconfAction("intVconf", key), oldVal(0) +VconfActionInt::VconfActionInt(const std::string &vconfKey) + : PluginAction("intVconf"), key(vconfKey), requestVal(0), oldVal(0), cb(nullptr), cbData(nullptr) { } int VconfActionInt::set(int val) { - int ret; - DBG("setInt(%s, %d)", key.c_str(), val); + requestVal = val; - ret = handleChange(); + int ret = cbHandler.handleSubscription(key); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", key.c_str(), ret); return ret; @@ -43,32 +43,22 @@ int VconfActionInt::set(int val) return MODES_ERROR_SYSTEM; } - ret = vconf_set_int(key.c_str(), val); - if (0 != ret) { - ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; -} - -int VconfActionInt::get() -{ - int value = 0; - int ret = vconf_get_int(key.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", key.c_str(), ret); - return 0; + if (oldVal == val) { + INFO("vconf(%s) already set(%d)", key.c_str(), val); + } else { + ret = vconf_set_int(key.c_str(), val); + if (0 != ret) { + ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), val, ret); + return MODES_ERROR_SYSTEM; + } } - return value; + return MODES_ERROR_NONE; } void VconfActionInt::undo() { - int ret; - DBG("undoInt(%s, %d)", key.c_str(), oldVal); - - ret = vconf_set_int(key.c_str(), oldVal); + int ret = vconf_set_int(key.c_str(), oldVal); if (0 != ret) ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), oldVal, ret); } @@ -88,3 +78,49 @@ int VconfActionInt::parse(const std::string &archive) return MODES_ERROR_NONE; } + +int VconfActionInt::setChangedCallback(valueChangedCB callback, void *userData) +{ + RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER); + + int ret = cbHandler.setChangedCB(key, this); + if (MODES_ERROR_NONE != ret) { + ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret); + return ret; + } + + cb = callback; + cbData = userData; + + return MODES_ERROR_NONE; +} + +void VconfActionInt::unSetChangedCallback(valueChangedCB callback, void *userData) +{ + RET_IF(NULL == callback); + + cbHandler.unSetChangedCB(key); + + cbData = NULL; + cb = NULL; +} + +void VconfActionInt::vconfChangedCB() +{ + //The value will be changed by Modes. That's why the callback is always called. + if (cb) + cb(cbData); +} + +void VconfActionInt::vconfChangedCB(keynode_t *node) +{ + const char *vconfKey = vconf_keynode_get_name(node); + if (vconfKey != key) { + WARN("Unknown vconf(%s) notification", vconfKey); + return; + } + + int val = vconf_keynode_get_int(node); + if (cb && (val != requestVal)) + cb(cbData); +} diff --git a/vconf/VconfActionInt.h b/vconf/VconfActionInt.h index 086665d..49108e8 100644 --- a/vconf/VconfActionInt.h +++ b/vconf/VconfActionInt.h @@ -16,24 +16,32 @@ #pragma once #include -#include "VconfAction.h" +#include #include "plugin-def.h" +#include "VconfCbHandler.h" MODES_NAMESPACE_BEGIN -class VconfActionInt : public VconfAction { +class VconfActionInt : public PluginAction, public VconfChangeAction { public: - VconfActionInt(const std::string &key); - virtual ~VconfActionInt() = default; + VconfActionInt(const std::string &vconfKey); int set(int val); - int get(); void undo() override; std::string serialize() override; int parse(const std::string &archive) override; + int setChangedCallback(valueChangedCB callback, void *userData) override; + void unSetChangedCallback(valueChangedCB callback, void *userData) override; + + void vconfChangedCB() override; + void vconfChangedCB(keynode_t *node) override; private: + std::string key; + int requestVal; int oldVal; + VconfCbHandler cbHandler; + valueChangedCB cb; + void *cbData; }; MODES_NAMESPACE_END - diff --git a/vconf/VconfActionStr.cpp b/vconf/VconfActionStr.cpp index 024e128..b483261 100644 --- a/vconf/VconfActionStr.cpp +++ b/vconf/VconfActionStr.cpp @@ -14,23 +14,23 @@ * limitations under the License. */ #include "VconfActionStr.h" + #include #include #include "plugin-log.h" MODES_NAMESPACE_USE; -VconfActionStr::VconfActionStr(const std::string &key) - : VconfAction("strVconf", key) +VconfActionStr::VconfActionStr(const std::string &vconfKey) + : PluginAction("strVconf"), key(vconfKey), cb(nullptr), cbData(nullptr) { } int VconfActionStr::set(const std::string &val) { - int ret; - DBG("setStr(%s, %s)", key.c_str(), val.c_str()); + requestVal = val; - ret = handleChange(); + int ret = cbHandler.handleSubscription(key); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", key.c_str(), ret); return ret; @@ -44,34 +44,22 @@ int VconfActionStr::set(const std::string &val) oldVal = prev; free(prev); - ret = vconf_set_str(key.c_str(), val.c_str()); - if (0 != ret) { - ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), val.c_str(), ret); - return MODES_ERROR_SYSTEM; + if (oldVal == val) { + INFO("vconf(%s) already set(%s)", key.c_str(), val.c_str()); + } else { + ret = vconf_set_str(key.c_str(), val.c_str()); + if (0 != ret) { + ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), val.c_str(), ret); + return MODES_ERROR_SYSTEM; + } } - return MODES_ERROR_NONE; -} - -std::string VconfActionStr::get() -{ - char *value = vconf_get_str(key.c_str()); - if (NULL == value) { - ERR("vconf_get_str(%s) Fail()", key.c_str()); - return std::string(); - } - - std::string retStr = value; - free(value); - return retStr; + return MODES_ERROR_NONE; } void VconfActionStr::undo() { - int ret; - DBG("undoStr(%s, %s)", key.c_str(), oldVal.c_str()); - - ret = vconf_set_str(key.c_str(), oldVal.c_str()); + int ret = vconf_set_str(key.c_str(), oldVal.c_str()); if (0 != ret) ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), oldVal.c_str(), ret); } @@ -86,3 +74,49 @@ int VconfActionStr::parse(const std::string &archive) oldVal = archive; return MODES_ERROR_NONE; } + +int VconfActionStr::setChangedCallback(valueChangedCB callback, void *userData) +{ + RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER); + + int ret = cbHandler.setChangedCB(key, this); + if (MODES_ERROR_NONE != ret) { + ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret); + return ret; + } + + cb = callback; + cbData = userData; + + return MODES_ERROR_NONE; +} + +void VconfActionStr::unSetChangedCallback(valueChangedCB callback, void *userData) +{ + RET_IF(nullptr == callback); + + cbHandler.unSetChangedCB(key); + + cbData = nullptr; + cb = nullptr; +} + +void VconfActionStr::vconfChangedCB() +{ + //The value will be changed by Modes. That's why the callback is always called. + if (cb) + cb(cbData); +} + +void VconfActionStr::vconfChangedCB(keynode_t *node) +{ + const char *vconfKey = vconf_keynode_get_name(node); + if (vconfKey != key) { + WARN("Unknown vconf(%s) notification", vconfKey); + return; + } + + char *val = vconf_keynode_get_str(node); + if (cb && (val != requestVal)) + cb(cbData); +} diff --git a/vconf/VconfActionStr.h b/vconf/VconfActionStr.h index 38af35c..ad84154 100644 --- a/vconf/VconfActionStr.h +++ b/vconf/VconfActionStr.h @@ -16,23 +16,32 @@ #pragma once #include -#include "VconfAction.h" +#include #include "plugin-def.h" +#include "VconfCbHandler.h" MODES_NAMESPACE_BEGIN -class VconfActionStr : public VconfAction { +class VconfActionStr : public PluginAction, public VconfChangeAction { public: - VconfActionStr(const std::string &key); - ~VconfActionStr() = default; + VconfActionStr(const std::string &vconfKey); int set(const std::string &val); - std::string get(); void undo() override; std::string serialize() override; int parse(const std::string &archive) override; + int setChangedCallback(valueChangedCB callback, void *userData) override; + void unSetChangedCallback(valueChangedCB callback, void *userData) override; + + void vconfChangedCB() override; + void vconfChangedCB(keynode_t *node) override; private: + std::string key; + std::string requestVal; std::string oldVal; + VconfCbHandler cbHandler; + valueChangedCB cb; + void *cbData; }; MODES_NAMESPACE_END diff --git a/vconf/VconfPlugin.cpp b/vconf/VconfPlugin.cpp index e0739e4..c748893 100644 --- a/vconf/VconfPlugin.cpp +++ b/vconf/VconfPlugin.cpp @@ -37,11 +37,6 @@ public: int set(const std::string &key, const std::string &val, PluginAction **piAction) override; PluginAction* getUndoAction(const std::string &key, const std::string &info) override; std::string serializeAction(PluginAction *piAction) override; - - int getInt(const std::string &key) override; - double getDouble(const std::string &key) override; - bool getBool(const std::string &key) override; - std::string getString(const std::string &key) override; }; extern "C" API Plugin *objectCreate(void) @@ -166,51 +161,3 @@ std::string VconfPlugin::serializeAction(PluginAction *piAction) std::string rawStr = piAction->serialize(); return piAction->getName().front() + rawStr; } - -int VconfPlugin::getInt(const std::string &key) -{ - DBG("getInt(%s)", key.c_str()); - - std::string newKey(key); - std::replace(newKey.begin(), newKey.end(), '.', '/'); - - VconfActionInt action(newKey); - - return action.get(); -} - -double VconfPlugin::getDouble(const std::string &key) -{ - DBG("getDouble(%s)", key.c_str()); - - std::string newKey(key); - std::replace(newKey.begin(), newKey.end(), '.', '/'); - - VconfActionDbl action(newKey); - - return action.get(); -} - -bool VconfPlugin::getBool(const std::string &key) -{ - DBG("getBool(%s)", key.c_str()); - - std::string newKey(key); - std::replace(newKey.begin(), newKey.end(), '.', '/'); - - VconfActionBool action(newKey); - - return action.get(); -} - -std::string VconfPlugin::getString(const std::string &key) -{ - DBG("getString(%s)", key.c_str()); - - std::string newKey(key); - std::replace(newKey.begin(), newKey.end(), '.', '/'); - - VconfActionStr action(newKey); - - return action.get(); -}