Enable scaling to fit various screen resolutions
[apps/agl-service-windowmanager.git] / src / util.cpp
index 2ae856f..1fb6ac8 100644 (file)
@@ -37,3 +37,30 @@ unique_fd::~unique_fd() {
       close(this->fd);
    }
 }
+
+void rectangle::scale(int to_w, int to_h, bool keep_aspect)
+{
+  if (!keep_aspect) {
+    w = to_w;
+    h = to_h;
+    return;
+  }
+
+  int64_t resized_w = int64_t(to_h) * int64_t(w) / int64_t(h);
+
+  if (resized_w <= to_w) {
+    /* use aspect by height */
+    w = resized_w;
+    h = to_h;
+  } else {
+    /* use aspect by width */
+    w = to_h;
+    h = int64_t(to_w) * int64_t(h) / int64_t(w);
+  }
+}
+
+void rectangle::center(int outer_w, int outer_h)
+{
+  x = (outer_w - w) / 2;
+  y = (outer_h - h) / 2;
+}