docs: add yaml book
[apps/agl-service-windowmanager.git] / src / util.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "util.hpp"
19
20 #include <cerrno>
21 #include <cstdarg>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <ctime>
25 #include <fstream>
26
27 #include <unistd.h>
28
29 #include "hmi-debug.h"
30
31 #ifdef SCOPE_TRACING
32 thread_local int ScopeTrace::indent = 0;
33 ScopeTrace::ScopeTrace(char const *func) : f(func)
34 {
35     fprintf(stderr, "%lu %*s%s -->\n", pthread_self(), 2 * indent++, "", this->f);
36 }
37 ScopeTrace::~ScopeTrace() { fprintf(stderr, "%lu %*s%s <--\n", pthread_self(), 2 * --indent, "", this->f); }
38 #endif
39
40 unique_fd::~unique_fd()
41 {
42     if (this->fd != -1)
43     {
44         close(this->fd);
45     }
46 }
47
48 void rectangle::fit(unsigned long to_width, unsigned long to_height)
49 {
50     // fit rect within (to_width x to_height)
51
52     if (to_width <= width()) {
53         // scale to fit with
54         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
55         set_right(left() + to_width - 1);
56     } else {
57         // scale to fit height
58         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
59         set_bottom(top() + to_height - 1);
60     }
61 }
62
63 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
64 {
65     long inner_w = width();
66     long inner_h = height();
67
68     set_left((outer_w - inner_w) / 2);
69     set_right(left() + inner_w - 1);
70     set_top((outer_h - inner_h) / 2);
71     set_bottom(top() + inner_h - 1);
72 }
73
74 void rectangle::set_aspect(double ratio)
75 {
76     // aspect ratio is width:height (= width/height)
77     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
78     //      Portrait of HD's ratio is 9:16 (= 0.5625)
79     //
80     // width / height = ratio
81     // width * height = area
82     //
83     // width = sqrt(ratio * area)
84     // height = width / ratio
85
86     long orig_w = width();
87     long orig_h = height();
88
89     if (ratio >= 1) {
90         // width >= height
91         // try to keep width
92         set_right(left() + orig_w - 1);
93         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
94     } else {
95         set_bottom(top() + orig_h - 1);
96         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
97     }
98 }
99
100 std::string get_file_path(const char *file_name, const char *log_category)
101 {
102     char const *default_base_path = getenv("AFM_APP_INSTALL_DIR");
103     std::string path("");
104
105     if(!file_name) {
106         return path;
107     }
108
109     if (!default_base_path)
110     {
111         HMI_ERROR(log_category, "AFM_APP_INSTALL_DIR is not defined");
112     }
113     else
114     {
115         path = default_base_path;
116         path.append("/etc/");
117         path.append(file_name);
118     }
119
120     // Check for over-ride in /etc/xdg/windowmanager
121     std::string override_path("/etc/xdg/windowmanager/");
122     override_path.append(file_name);
123     std::ifstream i(override_path);
124     if (i.good())
125     {
126         path = override_path;
127     }
128     i.close();
129
130     HMI_INFO(log_category, "Using %s", path.c_str());
131     return path;
132 }