2 * Copyright (c) 2017 - 2021 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <bundle_cpp.h>
27 #include "app_request.h"
30 #include "include/aul.h"
31 #include "include/aul_cmd.h"
32 #include "include/aul_window.h"
36 using namespace aul::internal;
40 constexpr const char kWmBusName[] = "org.enlightenment.wm";
41 constexpr const char kWmObjectPath[] = "/org/enlightenment/wm";
42 constexpr const char kWmInterfaceName[] = "org.enlightenment.wm.proc";
43 constexpr const char kWmMethodNameInfo[] = "GetVisibleWinInfo";
44 constexpr const char kWmMethodNameFocus[] = "GetFocusProc";
45 constexpr const unsigned int kWmDbusTimeout = 5000;
47 GDBusConnection* system_conn;
49 GDBusConnection* GetConn() {
50 if (system_conn == nullptr) {
51 GError* error = nullptr;
52 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
53 if (system_conn == nullptr) {
54 _E("g_bus_get_sync() is failed. error(%s)",
55 error ? error->message : "Unknown");
56 g_clear_error(&error);
66 WindowInfo(unsigned int rid, int x, int y, int w, int h,
67 bool alpha, int visibility, bool focused, int pid, int ppid,
68 int apid, int noti_level, bool opaque)
69 : rid_(rid), x_(x), y_(y), w_(w), h_(h), alpha_(alpha),
70 visibility_(visibility), focused_(focused), pid_(pid),
71 ppid_(ppid), apid_(apid), noti_level_(noti_level), opaque_(opaque) {
74 unsigned int GetResourceId() const {
78 int GetPositionX() const {
82 int GetPositionY() const {
86 int GetWidth() const {
90 int GetHeight() const {
94 bool HasAlpha() const {
98 int GetVisibility() const {
102 bool IsFocused() const {
110 int GetParentPid() const {
114 int GetAncestorPid() const {
118 int GetNotificationLevel() const {
122 bool IsOpaque() const {
142 void WindowInfoDestroyFunc(gpointer data) {
143 auto* info = static_cast<WindowInfo*>(data);
152 extern "C" API int aul_window_stack_get(aul_window_stack_h* handle) {
153 if (handle == nullptr) {
154 _E("Invalid parameter");
158 GDBusConnection* conn = GetConn();
162 auto* msg = g_dbus_message_new_method_call(kWmBusName,
163 kWmObjectPath, kWmInterfaceName, kWmMethodNameInfo);
164 if (msg == nullptr) {
165 _E("g_dbus_message_new_method_call() is faield");
168 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_auto(
169 msg, g_object_unref);
171 GError* error = nullptr;
172 auto* reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
173 G_DBUS_SEND_MESSAGE_FLAGS_NONE, kWmDbusTimeout, nullptr, nullptr, &error);
174 if (reply == nullptr || error != nullptr) {
175 _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
176 error ? error->message : "Unknown");
177 g_clear_error(&error);
180 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_auto(
181 reply, g_object_unref);
183 auto* body = g_dbus_message_get_body(reply);
184 if (body == nullptr) {
185 _E("g_dbus_message_get_body() is failed");
189 unsigned int rid = 0;
194 gboolean alpha = FALSE;
196 gboolean focused = FALSE;
201 gboolean opaque = FALSE;
203 GVariantIter* iter = nullptr;
204 g_variant_get(body, "(a(uiiiibibiiiib))", &iter);
208 std::unique_ptr<GVariantIter, decltype(g_variant_iter_free)*> iter_auto(
209 iter, g_variant_iter_free);
211 GList* list = nullptr;
212 while (g_variant_iter_loop(iter, "(uiiiibibiiiib)",
226 auto* info = new (std::nothrow) WindowInfo(rid, x, y, w, h,
227 alpha ? true : false, visibility, focused ? true : false,
228 pid, ppid, apid, noti_level, opaque ? true : false);
229 if (info == nullptr) {
231 g_list_free_full(list, WindowInfoDestroyFunc);
235 list = g_list_append(list, info);
238 *handle = static_cast<aul_window_stack_h>(list);
242 extern "C" API int aul_window_stack_del(aul_window_stack_h handle) {
243 if (handle == nullptr) {
244 _E("Invalid parameter");
248 auto* list = static_cast<GList*>(handle);
249 g_list_free_full(list, WindowInfoDestroyFunc);
253 extern "C" API int aul_window_stack_foreach(aul_window_stack_h handle,
254 void (*iter_cb)(aul_window_info_h info, void* data), void* data) {
255 if (handle == nullptr || iter_cb == nullptr) {
256 _E("Invalid parameter");
260 auto* iter = static_cast<GList*>(handle);
262 iter_cb(static_cast<aul_window_info_h>(iter->data), data);
263 iter = g_list_next(iter);
269 extern "C" API int aul_window_stack_info_get_resource_id(aul_window_info_h info,
271 if (info == nullptr || rid == nullptr) {
272 _E("Invalid parameter");
276 auto* handle = static_cast<WindowInfo*>(info);
277 *rid = handle->GetResourceId();
281 extern "C" API int aul_window_info_get_pid(aul_window_info_h info, int* pid) {
282 if (info == nullptr || pid == nullptr) {
283 _E("Invalid parameter");
287 auto* handle = static_cast<WindowInfo*>(info);
288 *pid = handle->GetPid();
292 extern "C" API int aul_window_info_get_parent_pid(aul_window_info_h info,
294 if (info == nullptr || ppid == nullptr) {
295 _E("Invalid parameter");
299 auto* handle = static_cast<WindowInfo*>(info);
300 *ppid = handle->GetParentPid();
304 extern "C" API int aul_window_info_get_ancestor_pid(aul_window_info_h info,
306 if (info == nullptr || apid == nullptr) {
307 _E("Invalid parameter");
311 auto* handle = static_cast<WindowInfo*>(info);
312 *apid = handle->GetAncestorPid();
316 extern "C" API int aul_window_info_get_visibility(aul_window_info_h info,
318 if (info == nullptr || visibility == nullptr) {
319 _E("Invalid parameter");
323 auto* handle = static_cast<WindowInfo*>(info);
324 *visibility = handle->GetVisibility();
328 extern "C" API int aul_window_info_has_alpha(aul_window_info_h info,
330 if (info == nullptr || alpha == nullptr) {
331 _E("Invalid parameter");
335 auto* handle = static_cast<WindowInfo*>(info);
336 *alpha = handle->HasAlpha();
340 extern "C" API int aul_window_info_is_focused(aul_window_info_h info,
342 if (info == nullptr || focused == nullptr) {
343 _E("Invalid parameter");
347 auto* handle = static_cast<WindowInfo*>(info);
348 *focused = handle->IsFocused();
352 extern "C" API int aul_window_info_get_geometry(aul_window_info_h info,
353 int* x, int* y, int* w, int* h) {
354 if (info == nullptr ||
359 _E("Invalid parameter");
363 auto* handle = static_cast<WindowInfo*>(info);
364 *x = handle->GetPositionX();
365 *y = handle->GetPositionY();
366 *w = handle->GetWidth();
367 *h = handle->GetHeight();
371 extern "C" API int aul_window_info_get_notification_level(
372 aul_window_info_h info, aul_window_notification_level_e* level) {
373 if (info == nullptr || level == nullptr) {
374 _E("Invalid parameter");
378 auto* handle = static_cast<WindowInfo*>(info);
379 *level = static_cast<aul_window_notification_level_e>(
380 handle->GetNotificationLevel());
384 extern "C" API int aul_window_info_get_opaque(aul_window_info_h info,
386 if (info == nullptr || opaque == nullptr) {
387 _E("Invalid parameter");
391 auto* handle = static_cast<WindowInfo*>(info);
392 *opaque = handle->IsOpaque();
396 extern "C" API int aul_window_get_focused_pid(pid_t* pid) {
397 if (pid == nullptr) {
398 _E("Invalid parameter");
402 GDBusConnection* conn = GetConn();
406 auto* msg = g_dbus_message_new_method_call(kWmBusName,
407 kWmObjectPath, kWmInterfaceName, kWmMethodNameFocus);
408 if (msg == nullptr) {
409 _E("g_dbus_message_new_method_call() is failed");
412 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_auto(
413 msg, g_object_unref);
415 GError* error = nullptr;
416 auto* reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
417 G_DBUS_SEND_MESSAGE_FLAGS_NONE, kWmDbusTimeout, nullptr, nullptr,
419 if (reply == nullptr || error != nullptr) {
420 _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
421 error ? error->message : "Unknown");
422 g_clear_error(&error);
425 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_auto(
426 reply, g_object_unref);
428 auto* body = g_dbus_message_get_body(reply);
429 if (body == nullptr) {
430 _E("g_dbus_message_get_body() is failed");
434 gint focused_pid = -1;
435 g_variant_get(body, "(i)", &focused_pid);
436 *pid = static_cast<pid_t>(focused_pid);
437 _W("Result = %d", focused_pid);
441 extern "C" API int aul_window_attach(const char* parent_appid,
442 const char* child_appid) {
443 if (parent_appid == nullptr || child_appid == nullptr) {
444 _E("Invalid parameter");
448 tizen_base::Bundle b {
449 { AUL_K_PARENT_APPID, parent_appid },
450 { AUL_K_CHILD_APPID, child_appid }
452 int ret = AppRequest(APP_WINDOW_ATTACH, getuid())
456 _E("Failed to send request. error(%d)", ret);
463 extern "C" API int aul_window_detach(const char* child_appid) {
464 if (child_appid == nullptr) {
465 _E("Invalid parameter");
469 tizen_base::Bundle b {{ AUL_K_CHILD_APPID, child_appid }};
470 int ret = AppRequest(APP_WINDOW_DETACH, getuid())
474 _E("Failed to send request. error(%d)", ret);
481 extern "C" API int aul_window_attach_below(const char* parent_appid,
482 const char* child_appid) {
483 if (parent_appid == nullptr || child_appid == nullptr) {
484 _E("Invalid parameter");
488 tizen_base::Bundle b {
489 { AUL_K_PARENT_APPID, parent_appid },
490 { AUL_K_CHILD_APPID, child_appid }
492 int ret = AppRequest(APP_WINDOW_ATTACH_BELOW, getuid())
496 _E("Failed to send request. error(%d)", ret);