From 06f1e490056dd7df7948a031c64e9ccf87d9a231 Mon Sep 17 00:00:00 2001
From: Yuta Doi <yuta-d@witz-inc.co.jp>
Date: Wed, 20 Dec 2017 13:19:49 +0900
Subject: Add APIs which can get information about the display and the surface
 area

getDisplayInfo() can get the display information as follows:
 - width[pixel]
 - height[pixel]
 - width[mm]
 - height[mm]

  NOTE:
    It uses wl_output::geometry() for getting physical width[mm] and height[mm] of the display,
    but the value is different with measured value.

    value from wl_output::geometry(): width:320 height:520
    measured value                  : width:193 height:343

getAreaInfo() can get the information of area drawn by the application as follows:
 - x-coordinate
 - y-coordinate
 - width
 - height

The details are described in doc/ApplicationGuide.md in project apps/agl-service-windowmanager-2017.

Bug-AGL: SPEC-1087

Change-Id: Icee00af4ab210b056cc67c2868ef2c594da1dbe1
Signed-off-by: Yuta Doi <yuta-d@witz-inc.co.jp>
---
 src/libwindowmanager.cpp | 196 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/libwindowmanager.h   |   5 ++
 2 files changed, 200 insertions(+), 1 deletion(-)

diff --git a/src/libwindowmanager.cpp b/src/libwindowmanager.cpp
index 9b9c555..dfb8631 100644
--- a/src/libwindowmanager.cpp
+++ b/src/libwindowmanager.cpp
@@ -72,6 +72,9 @@ class LibWindowmanager::Impl {
     int deactivateSurface(json_object *object);
     int endDraw(json_object *object);
 
+    int getDisplayInfo(json_object *object);
+    int getAreaInfo(json_object *in_obj, json_object *out_obj);
+
     void set_event_handler(enum EventType et, handler_fun func);
 
     Impl();
@@ -371,6 +374,181 @@ int LibWindowmanager::Impl::endDraw(json_object *object) {
     });
 }
 
+int LibWindowmanager::Impl::getDisplayInfo(json_object *object) {
+    TRACE();
+    HMI_DEBUG("libwm", "called");
+
+    if (nullptr == object) {
+        HMI_ERROR("libwm", "Argment is NULL!");
+        return -EINVAL;
+    }
+
+    if ((nullptr == this->loop) || (nullptr == this->wsj1)) {
+        HMI_ERROR("libwm", "LibWindowmanager is not initialized!");
+        return -EPERM;
+    }
+
+    int w_px, h_px, w_mm, h_mm;
+    int rc = -1;
+    /* send the request */
+    int rc2 =
+        this->api_call("GetDisplayInfo", nullptr,
+                       [&rc, &w_px, &h_px, &w_mm, &h_mm](bool ok, json_object *j) {
+            if (ok) {
+                json_object *val;
+                if (json_object_object_get_ex(j, "response", &val)) {
+                    HMI_DEBUG("libwm", "responce:%s", json_object_get_string(val));
+
+                    json_object *j_w_px = nullptr;
+                    if (!json_object_object_get_ex(val, "width_pixel", &j_w_px)) {
+                        HMI_DEBUG("libwm", "Not found key \"width_pixel\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    w_px = json_object_get_int(j_w_px);
+
+                    json_object *j_h_px = nullptr;
+                    if (!json_object_object_get_ex(val, "height_pixel", &j_h_px)) {
+                        HMI_DEBUG("libwm", "Not found key \"height_pixel\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    h_px = json_object_get_int(j_h_px);
+
+                    json_object *j_w_mm = nullptr;
+                    if (!json_object_object_get_ex(val, "width_mm", &j_w_mm)) {
+                        HMI_DEBUG("libwm", "Not found key \"width_mm\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    w_mm = json_object_get_int(j_w_mm);
+
+                    json_object *j_h_mm = nullptr;
+                    if (!json_object_object_get_ex(val, "height_mm", &j_h_mm)) {
+                        HMI_DEBUG("libwm", "Not found key \"height_mm\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    h_mm = json_object_get_int(j_h_mm);
+                    rc = 0;
+                }
+                else {
+                    HMI_ERROR("libwm", "Not found key \"response\"");
+                    rc = -EINVAL;
+                    return;
+                }
+            } else {
+                HMI_ERROR("libwm", "Windowmanager-service is not initialized: %s",
+                        j != nullptr ? json_object_to_json_string_ext(
+                                j, JSON_C_TO_STRING_PRETTY)
+                          : "no-info");
+                rc = -EPERM;
+            }
+        });
+
+    if (0 > rc2) {
+        HMI_ERROR("libwm", "api_call() failed");
+        rc = rc2;
+    }
+
+    if (0 == rc) {
+      json_object_object_add(object, "width_pixel", json_object_new_int(w_px));
+      json_object_object_add(object, "height_pixel", json_object_new_int(h_px));
+      json_object_object_add(object, "width_mm", json_object_new_int(w_mm));
+      json_object_object_add(object, "height_mm", json_object_new_int(h_mm));
+    }
+
+    return rc;
+}
+
+int LibWindowmanager::Impl::getAreaInfo(json_object *in_obj, json_object *out_obj) {
+    TRACE();
+    HMI_DEBUG("libwm", "called");
+
+    if (nullptr == in_obj) {
+        HMI_ERROR("libwm", "Argment is NULL!");
+        return -EINVAL;
+    }
+
+    if ((nullptr == this->loop) || (nullptr == this->wsj1)) {
+        HMI_ERROR("libwm", "LibWindowmanager is not initialized!");
+        return -EPERM;
+    }
+
+    int x, y, w, h;
+    int rc = -1;
+    /* send the request */
+    int rc2 =
+      this->api_call("GetAreaInfo", in_obj,
+                     [&rc, &x, &y, &w, &h](bool ok, json_object *j) {
+            if (ok) {
+                json_object *val;
+                HMI_DEBUG("libwm", "j:%s", json_object_get_string(j));
+                if (json_object_object_get_ex(j, "response", &val)) {
+                    json_object *j_x = nullptr;
+                    if (!json_object_object_get_ex(val, "x", &j_x)) {
+                        HMI_DEBUG("libwm", "Not found key \"x\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    x = json_object_get_int(j_x);
+
+                    json_object *j_y = nullptr;
+                    if (!json_object_object_get_ex(val, "y", &j_y)) {
+                        HMI_DEBUG("libwm", "Not found key \"y\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    y = json_object_get_int(j_y);
+
+                    json_object *j_w = nullptr;
+                    if (!json_object_object_get_ex(val, "width", &j_w)) {
+                        HMI_DEBUG("libwm", "Not found key \"width\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    w = json_object_get_int(j_w);
+
+                    json_object *j_h = nullptr;
+                    if (!json_object_object_get_ex(val, "height", &j_h)) {
+                        HMI_DEBUG("libwm", "Not found key \"height\"");
+                        rc = -EINVAL;
+                        return;
+                    }
+                    h = json_object_get_int(j_h);
+                    rc = 0;
+
+                    HMI_DEBUG("libwm", "responce:%s", json_object_get_string(val));
+                }
+                else {
+                    HMI_ERROR("libwm", "Not found key \"response\"");
+                    rc = -EINVAL;
+                    return;
+                }
+            } else {
+                HMI_ERROR("libwm", "Could not get area rect: %s",
+                        j != nullptr ? json_object_to_json_string_ext(
+                                j, JSON_C_TO_STRING_PRETTY)
+                          : "no-info");
+                rc = -EINVAL;
+            }
+        });
+
+    if (0 > rc2) {
+        HMI_ERROR("libwm", "api_call() failed");
+        rc = rc2;
+    }
+
+    if (0 == rc) {
+      json_object_object_add(out_obj, "x", json_object_new_int(x));
+      json_object_object_add(out_obj, "y", json_object_new_int(y));
+      json_object_object_add(out_obj, "width", json_object_new_int(w));
+      json_object_object_add(out_obj, "height", json_object_new_int(h));
+    }
+
+    return rc;
+}
+
 static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
 {
 }
@@ -430,7 +608,9 @@ int LibWindowmanager::Impl::api_call(
     TRACE();
 
     int rc = 0;
-    if (0 == strcmp("RequestSurface", verb)) {
+    if ((0 == strcmp("RequestSurface", verb)) ||
+        (0 == strcmp("GetDisplayInfo", verb)) ||
+        (0 == strcmp("GetAreaInfo", verb))) {
         // We need to wrap the actual onReply call once in order to
         // *look* like a normal functions pointer (std::functions<>
         // with captures cannot convert to function pointers).
@@ -567,6 +747,20 @@ int LibWindowmanager::endDraw(json_object *object) {
     return this->d->endDraw(object);
 }
 
+int LibWindowmanager::getDisplayInfo(json_object *object) {
+    return this->d->getDisplayInfo(object);
+}
+
+int LibWindowmanager::getAreaInfo(json_object *in_obj, json_object *out_obj) {
+    return this->d->getAreaInfo(in_obj, out_obj);
+}
+
+int LibWindowmanager::getAreaInfo(const char *label, json_object *out_obj) {
+    json_object *object = json_object_new_object();
+    json_object_object_add(object, this->kKeyDrawingName, json_object_new_string(label));
+    return this->d->getAreaInfo(object, out_obj);
+}
+
 void LibWindowmanager::set_event_handler(enum EventType et, handler_fun f) {
     return this->d->set_event_handler(et, std::move(f));
 }
diff --git a/src/libwindowmanager.h b/src/libwindowmanager.h
index ec14617..b2b9496 100644
--- a/src/libwindowmanager.h
+++ b/src/libwindowmanager.h
@@ -40,6 +40,7 @@ public:
     /* Key for json obejct */
     const char *kKeyDrawingName = "drawing_name";
     const char *kKeyDrawingArea = "drawing_area";
+    const char *kKeyDrawingRect = "drawing_rect";
     const char *kKeyIviId = "ivi_id";
 
     enum EventType {
@@ -61,6 +62,10 @@ public:
     int activateSurface(json_object *object);
     int deactivateSurface(json_object *object);
     int endDraw(json_object *object);
+    int getDisplayInfo(json_object *object);
+    int getAreaInfo(json_object *in_obj, json_object *out_obj);
+
+    int getAreaInfo(const char *label, json_object *out_obj);
 
     void set_event_handler(enum EventType et, handler_fun f);
 
-- 
cgit