Modify AppCoreUiThreadBase class
[platform/core/appfw/app-core.git] / tizen-cpp / app-core-ui-cpp / wayland_handler_private.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Ecore_Wl2.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include "app-core-ui-cpp/wayland_handler_private.hh"
22 #include "common/log_private.hh"
23
24 namespace tizen_cpp {
25
26 WaylandHandler::WaylandHandler() {
27 }
28
29 WaylandHandler::~WaylandHandler() {
30   Fini();
31 }
32
33 int WaylandHandler::Init() {
34   _D("INIT");
35   if (__builtin_expect(initialized_, 1))
36     return 0;
37
38   auto* wl2_dsp = ecore_wl2_connected_display_get(nullptr);
39   if (wl2_dsp == nullptr)
40     wl2_dsp = ecore_wl2_display_connect(nullptr);
41
42   dsp_ = ecore_wl2_display_get(wl2_dsp);
43   if (dsp_ == nullptr) {
44     _E("wl_display_connect() is failed");
45     return -1;
46   }
47
48   reg_ = wl_display_get_registry(dsp_);
49   if (reg_ == nullptr) {
50     _E("wl_display_get_registry() is failed");
51     Fini();
52     return -1;
53   }
54
55   wl_registry_add_listener(reg_, &reg_listener_, this);
56   wl_display_roundtrip(dsp_);
57
58   if (tz_policy_ == nullptr) {
59     _E("Failed to get tizen policy interface");
60     Fini();
61     return -1;
62   }
63
64   initialized_ = true;
65   return 0;
66 }
67
68 void WaylandHandler::Fini() {
69   if (!initialized_)
70     return;
71
72   if (tz_policy_) {
73     tizen_policy_destroy(tz_policy_);
74     tz_policy_ = nullptr;
75   }
76
77   if (reg_) {
78     wl_registry_destroy(reg_);
79     reg_ = nullptr;
80   }
81
82   dsp_ = nullptr;
83   initialized_ = false;
84 }
85
86 void WaylandHandler::SetBgState() {
87   if (!initialized_)
88     return;
89
90   tizen_policy_set_background_state(tz_policy_, getpid());
91   wl_display_roundtrip(dsp_);
92   _D("Set Background State");
93 }
94
95 void WaylandHandler::UnsetBgState() {
96   if (!initialized_)
97     return;
98
99   tizen_policy_unset_background_state(tz_policy_, getpid());
100   wl_display_roundtrip(dsp_);
101   _D("Unset Background State");
102 }
103
104 void WaylandHandler::SetAppId(const std::string& app_id) {
105   if (!initialized_)
106     return;
107
108   tizen_policy_set_appid(tz_policy_, getpid(), app_id.c_str());
109   wl_display_roundtrip(dsp_);
110   _D("Set AppId: %s", app_id.c_str());
111 }
112
113 void WaylandHandler::WlListenerCb(void* data, struct wl_registry* reg,
114     uint32_t id, const char* interface, uint32_t ver) {
115   if (interface && !strcmp(interface, "tizen_policy")) {
116     auto* handle = static_cast<WaylandHandler*>(data);
117     if (handle->tz_policy_ == nullptr) {
118       handle->tz_policy_ = reinterpret_cast<struct tizen_policy*>(
119           wl_registry_bind(reinterpret_cast<struct wl_registry*>(handle->reg_),
120             id, &tizen_policy_interface, 7));
121     }
122   }
123 }
124
125 void WaylandHandler::WlListenerRemoveCb(void* data, struct wl_registry* reg,
126     unsigned int id) {
127 }
128
129 }  // namespace tizen_cpp