Add APIs which can get information of display and area
[apps/agl-service-windowmanager.git] / src / afb_binding_api.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 "app.hpp"
18 #include "json_helper.hpp"
19
20 #include <csignal>
21
22 #include <json.hpp>
23
24 using json = nlohmann::json;
25
26 #include <json-c/json.h>
27
28 namespace wm {
29 /**
30  * binding_api impl
31  */
32 binding_api::result_type binding_api::requestsurface(
33    char const *drawing_name) {
34    auto r = this->app->api_request_surface(drawing_name);
35    if (r.is_err()) {
36       return Err<json_object *>(r.unwrap_err());
37    }
38    return Ok(json_object_new_int(r.unwrap()));
39 }
40
41 binding_api::result_type binding_api::activatesurface(
42    char const *drawing_name, char const *drawing_area) {
43   HMI_DEBUG("wm", "%s drawing_name %s, drawing_area %s", __func__, drawing_name, drawing_area);
44   auto r = this->app->api_activate_surface(drawing_name, drawing_area);
45    if (r != nullptr) {
46       HMI_DEBUG("wm", "%s failed with error: %s", __func__, r);
47       return Err<json_object *>(r);
48    }
49    return Ok(json_object_new_object());
50 }
51
52 binding_api::result_type binding_api::deactivatesurface(char const* drawing_name) {
53    HMI_DEBUG("wm", "%s drawing_name %s", __func__, drawing_name);
54    auto r = this->app->api_deactivate_surface(drawing_name);
55    if (r != nullptr) {
56       HMI_DEBUG("wm", "%s failed with error: %s", __func__, r);
57       return Err<json_object *>(r);
58    }
59    return Ok(json_object_new_object());
60 }
61
62 binding_api::result_type binding_api::enddraw(char const* drawing_name) {
63    HMI_DEBUG("wm", "%s drawing_name %s", __func__, drawing_name);
64    auto r = this->app->api_enddraw(drawing_name);
65    if (r != nullptr) {
66       HMI_DEBUG("wm", "%s failed with error: %s", __func__, r);
67       return Err<json_object *>(r);
68    }
69    return Ok(json_object_new_object());
70 }
71
72 binding_api::result_type binding_api::getdisplayinfo() {
73    auto r = this->app->api_get_display_info();
74    if (r.is_err()) {
75       return Err<json_object *>(r.unwrap_err());
76    }
77    return Ok(r.unwrap());
78 }
79
80 binding_api::result_type binding_api::getareainfo(
81    char const *drawing_name) {
82    auto r = this->app->api_get_area_info(drawing_name);
83    if (r.is_err()) {
84       return Err<json_object *>(r.unwrap_err());
85    }
86    return Ok(r.unwrap());
87 }
88
89 binding_api::result_type binding_api::list_drawing_names() {
90    HMI_DEBUG("wm", "%s", __func__);
91    json j = this->app->id_alloc.name2id;
92    return Ok(json_tokener_parse(j.dump().c_str()));
93 }
94
95 binding_api::result_type binding_api::debug_layers() {
96    HMI_DEBUG("wm", "%s", __func__);
97    return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
98 }
99
100 binding_api::result_type binding_api::debug_surfaces() {
101    HMI_DEBUG("wm", "%s", __func__);
102    return Ok(to_json(this->app->controller->sprops));
103 }
104
105 binding_api::result_type binding_api::debug_status() {
106    HMI_DEBUG("wm", "%s", __func__);
107    json_object *jr = json_object_new_object();
108    json_object_object_add(jr, "surfaces",
109                           to_json(this->app->controller->sprops));
110    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
111    return Ok(jr);
112 }
113
114 binding_api::result_type binding_api::debug_terminate() {
115    HMI_DEBUG("wm", "%s", __func__);
116    if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr) {
117       raise(SIGKILL);  // afb-daemon kills it's pgroup using TERM, which
118                        // doesn't play well with perf
119    }
120    return Ok(json_object_new_object());
121 }
122
123 binding_api::result_type binding_api::ping() {
124    this->app->api_ping();
125    return Ok(json_object_new_object());
126 }
127
128 }  // namespace wm