#include <memory>
#include <string>
#include <thread>
+#include <utility>
#include "app-core-cpp/app_core_base.hh"
+#include "app-core-rotation-cpp/app_core_rotation.hh"
#include "app-core-ui-cpp/api/app_core_ui_base.h"
#include "app-core-ui-cpp/app_core_task_base.hh"
#include "app-core-ui-cpp/app_core_ui_delegator_private.hh"
#include "app-core-ui-cpp/app_core_ui_plugin_private.hh"
+#include "app-core-ui-cpp/app_core_ui_thread_base.hh"
#include "app-core-ui-cpp/wayland_handler_private.hh"
+#include "app-core-ui-cpp/window_position_private.hh"
#include "common/ecore_handler.hh"
#include "common/glib_private.hh"
#include "common/log_private.hh"
+#include "common/log_tracer.hh"
namespace tizen_cpp {
constexpr const char K_SERVICE_THREAD[] = "__K_SERVICE_THREAD";
+constexpr const char K_HINT_RECENT_SCREEN_POSITION[] =
+ "__K_HINT_RECENT_SCREEN_POSITION";
+const int APPID_MAX = 256;
-void SetComm(const std::string& name) {
- pid_t tid = syscall(__NR_gettid);
- std::string path = "/proc/" + std::to_string(tid) + "/comm";
- int fd = open(path.c_str(), O_WRONLY);
- if (fd < 0) {
- _E("open(%s) is failed. errno(%d)", path.c_str(), errno);
- return;
- }
-
- ssize_t bytes_written = write(fd, name.c_str(), name.length() + 1);
- if (bytes_written < 0)
- _E("write(%d) is failed. errno(%d)", fd, errno);
-
- close(fd);
-}
-
-class AppCoreUiBase::Impl {
+class AppCoreUiBase::Impl : public AppCoreRotation::IEventListener {
public:
Impl(AppCoreUiBase* parent, unsigned int hint)
: parent_(parent),
hint_(hint),
handler_(std::make_shared<EcoreHandler>(parent)),
- wl_handler_(new WaylandHandler()) {
+ wl_handler_(new WaylandHandler()),
+ wp_manager_(new WindowPositionManager()) {
}
private:
int FiniWl();
void PluginInit(int argc, char** argv);
void PluginFini();
+ void SetWindowPosition(const tizen_base::Bundle& b);
+ void SetWindowPosition();
+ void SetWindow(unsigned int wid);
+ Ecore_Wl2_Window* GetWindow() const;
void Run(int argc, char** argv);
void Exit();
+ void RotationInit();
+ void RotationShutdown();
+ void OnRotationChanged(int degree) override;
std::list<std::shared_ptr<WinNode>> winnode_list_;
unsigned int hint_;
std::unique_ptr<AppCoreUiDelegator> plugin_delegator_;
std::unique_ptr<AppCoreUiPlugin> plugin_;
std::unique_ptr<AppCoreTaskBase> service_;
- GMainContext* context_ = nullptr;
- std::thread thread_;
+ std::unique_ptr<WindowPosition> position_;
+ Ecore_Wl2_Window* window_ = nullptr;
+ std::unique_ptr<WindowPositionManager> wp_manager_;
+ std::unique_ptr<AppCoreUiThreadBase> thread_;
+ std::unique_ptr<AppCoreRotation> rotation_;
};
AppCoreUiBase::AppCoreUiBase(unsigned int hint)
return false;
}
+ SetWindow(win);
winnode_list_.emplace_back(new WinNode(win, surf));
return true;
}
return false;
}
+ SetWindow(0);
winnode_list_.remove_if(
[win](std::shared_ptr<struct AppCoreUiBase::Impl::WinNode> node) {
return node->win_ == win;
if (vis != VT_NONE)
node->vis_ = vis;
+ if (GetWindow() == nullptr)
+ SetWindow(win);
+
+ SetWindowPosition();
return true;
}
}
void AppCoreUiBase::Impl::PluginInit(int argc, char** argv) {
+ LogTracer tracer("AppCoreUiBase::Impl::PluginInit()");
plugin_.reset(AppCoreUiPlugin::Load());
if (plugin_ == nullptr)
return;
plugin_->Fini(parent_);
}
+void AppCoreUiBase::Impl::SetWindowPosition(const tizen_base::Bundle& b) {
+ position_ = wp_manager_->ReadFromBundle(b);
+ if (position_) {
+ _D("x: %d, y: %d, w: %d, h: %d",
+ position_->GetPositionX(), position_->GetPositionY(),
+ position_->GetScreenWidth(), position_->GetScreenHeight());
+ }
+
+ auto recent_screen_position = b.GetString(K_HINT_RECENT_SCREEN_POSITION);
+ if (!recent_screen_position.empty()) {
+ auto position = wp_manager_->ReadFromFile();
+ if (position) {
+ _D("x: %d, y: %d, w: %d, h: %d",
+ position->GetPositionX(), position->GetPositionY(),
+ position->GetScreenWidth(), position->GetScreenHeight());
+ position_ = std::move(position);
+ } else {
+ _D("There is no window position");
+ }
+ }
+
+ if (position_)
+ wp_manager_->WriteToFile(position_);
+}
+
+void AppCoreUiBase::Impl::SetWindowPosition() {
+ if (!position_)
+ return;
+
+ if (GetWindow() == nullptr) {
+ _E("window is nullptr");
+ return;
+ }
+
+ int x = 0;
+ int y = 0;
+ int w = 0;
+ int h = 0;
+ ecore_wl2_window_geometry_get(GetWindow(), &x, &y, &w, &h);
+
+ _W("x: %d, y: %d, w: %d, h: %d", w, y, w, h);
+ if (position_->GetPositionX() != x ||
+ position_->GetPositionY() != y ||
+ position_->GetScreenWidth() != w ||
+ position_->GetScreenHeight() != h) {
+ position_.reset(new WindowPosition(x, y, w, h));
+ wp_manager_->WriteToFile(position_);
+ }
+}
+
+void AppCoreUiBase::Impl::SetWindow(unsigned int wid) {
+ if (wid == 0) {
+ window_ = nullptr;
+ return;
+ }
+
+ window_ = ecore_wl2_window_find(wid);
+ if (window_ == nullptr) {
+ _E("ecore_wl2_window_find() is failed. wid(%u)", wid);
+ return;
+ }
+
+ _D("wid(%u), window(%p)", wid, window_);
+}
+
+Ecore_Wl2_Window* AppCoreUiBase::Impl::GetWindow() const {
+ return window_;
+}
+
void AppCoreUiBase::DoRun(int argc, char** argv) {
SetCoreDelegator(nullptr);
SetLoopDelegator(nullptr);
impl_->handler_->Init();
impl_->PluginInit(argc, argv);
- char appid[PATH_MAX] = {0, };
+ char appid[APPID_MAX] = {0, };
int ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
if (ret != 0)
_E("Fail to get appid. pid(%d)", getpid());
impl_->w_status_ = Impl::WS_NONE;
impl_->appid_ = std::string(appid);
LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", appid);
- if (impl_->hint_ & HINT_BG_LAUNCH_CONTROL) {
- tizen_base::Bundle b(bundle_import_from_argv(argc, argv), false, true);
- if (!b.IsEmpty()) {
+
+ tizen_base::Bundle b(bundle_import_from_argv(argc, argv), false, true);
+ if (!b.IsEmpty()) {
+ if (impl_->hint_ & HINT_BG_LAUNCH_CONTROL) {
std::string bg_launch = b.GetString(AUL_SVC_K_BG_LAUNCH);
if (bg_launch.compare("enable") == 0)
impl_->ApplyBgState(true);
}
+
+ impl_->SetWindowPosition(b);
}
if (impl_->hint_ & HINT_WINDOW_ID_CONTROL)
void AppCoreUiBase::Impl::Run(int argc, char** argv) {
if (hint_ & HINT_DUAL_THREAD) {
service_ = parent_->CreateTask();
- context_ = g_main_context_new();
- std::string env = std::to_string(
- reinterpret_cast<unsigned long int>(context_));
- setenv("TIZEN_GLIB_CONTEXT", env.c_str(), 1);
-
- thread_ = std::thread([&] {
- SetComm("UIThread+");
- parent_->DoRun(argc, argv);
- });
+ auto* thread_context = AppCoreUiThreadBase::GetContext();
+ if (thread_context == nullptr) {
+ thread_.reset(new AppCoreUiThreadBase());
+ thread_->Run(argc, argv);
+ thread_context = AppCoreUiThreadBase::GetContext();
+ }
+ thread_context->Post([&]() { parent_->DoRun(argc, argv); });
service_->Run(argc, argv);
-
- if (thread_.joinable())
- thread_.join();
-
- setenv("TIZEN_GLIB_CONTEXT", "0", 1);
- g_main_context_unref(context_);
- context_ = nullptr;
return;
}
void AppCoreUiBase::Impl::Exit() {
if (hint_ & HINT_DUAL_THREAD) {
- GLib::IdleAdd(context_, [](gpointer user_data) {
- auto* impl = static_cast<AppCoreUiBase::Impl*>(user_data);
- impl->parent_->DoExit();
- return G_SOURCE_REMOVE;
- }, this);
+ auto* thread_context = AppCoreUiThreadBase::GetContext();
+ if (thread_context != nullptr) {
+ thread_context->Post([&]() { parent_->DoExit(); });
+ thread_context->Exit();
+ }
service_->Exit();
return;
parent_->DoExit();
}
+void AppCoreUiBase::Impl::RotationInit() {
+ if (!rotation_) rotation_.reset(new AppCoreRotation(this));
+
+ rotation_->Init();
+}
+
+void AppCoreUiBase::Impl::RotationShutdown() {
+ if (rotation_) rotation_->Dispose();
+}
+
+void AppCoreUiBase::Impl::OnRotationChanged(int degree) {
+ AppCoreBase::SetRotationState(
+ static_cast<AppCoreBase::RotationState>(degree));
+ parent_->RaiseEvent(degree, IEvent::Type::DEVICE_ORIENTATION_CHANGED);
+}
+
void AppCoreUiBase::Run(int argc, char** argv) {
impl_->Run(argc, argv);
}
return;
}
+ std::string rpc_port = b.GetString(AUL_K_RPC_PORT);
if (parent_->GetHint() & HINT_BG_LAUNCH_CONTROL) {
std::string bg_launch = b.GetString(AUL_SVC_K_BG_LAUNCH);
if (!bg_launch.empty() && bg_launch.compare("enable") == 0) {
if (!bg_state_ && state_ != AS_RUNNING)
ApplyBgState(true);
} else {
- if (bg_state_)
+ if (bg_state_ && rpc_port.empty())
ApplyBgState(false);
}
}
if (parent_->GetHint() & HINT_WINDOW_AUTO_CONTROL) {
- if (!bg_state_) {
- std::string rpc_port = b.GetString(AUL_K_RPC_PORT);
- if (rpc_port.empty())
- RaiseWin();
- }
+ if (!bg_state_ && rpc_port.empty())
+ RaiseWin();
}
}
else
impl_->UpdateWin(win, surf, Impl::VT_NONE);
- if (surf != 0)
+ if (surf != 0) {
GroupAdd();
+ }
}
bool AppCoreUiBase::Impl::CheckVisible() {
impl_->resource_reclaiming_ = enable;
}
+int AppCoreUiBase::GetWindowPosition(int* x, int* y, int* w, int* h) {
+ if (!impl_->position_ ||
+ x == nullptr ||
+ y == nullptr ||
+ w == nullptr ||
+ h == nullptr)
+ return -1;
+
+ *x = impl_->position_->GetPositionX();
+ *y = impl_->position_->GetPositionY();
+ *w = impl_->position_->GetScreenWidth();
+ *h = impl_->position_->GetScreenHeight();
+
+ return 0;
+}
+
+int AppCoreUiBase::OnSetEvent(IEvent::Type event) {
+ AppCoreBase::OnSetEvent(event);
+
+ if (event == IEvent::Type::DEVICE_ORIENTATION_CHANGED)
+ impl_->RotationInit();
+
+ return 0;
+}
+
+int AppCoreUiBase::OnUnsetEvent(IEvent::Type event) {
+ AppCoreBase::OnUnsetEvent(event);
+
+ if (event == IEvent::Type::DEVICE_ORIENTATION_CHANGED)
+ impl_->RotationShutdown();
+
+ return 0;
+}
+
} // namespace tizen_cpp