Add gitlab issue/merge request templates
[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 <time.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <fstream>
25
26 #include <unistd.h>
27
28 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
29
30 void rectangle::fit(unsigned long to_width, unsigned long to_height)
31 {
32     // fit rect within (to_width x to_height)
33
34     if (to_width <= (unsigned long)width()) {
35         // scale to fit with
36         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
37         set_right(left() + to_width - 1);
38     } else {
39         // scale to fit height
40         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
41         set_bottom(top() + to_height - 1);
42     }
43 }
44
45 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
46 {
47     long inner_w = width();
48     long inner_h = height();
49
50     set_left((outer_w - inner_w) / 2);
51     set_right(left() + inner_w - 1);
52     set_top((outer_h - inner_h) / 2);
53     set_bottom(top() + inner_h - 1);
54 }
55
56 void rectangle::set_aspect(double ratio)
57 {
58     // aspect ratio is width:height (= width/height)
59     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
60     //      Portrait of HD's ratio is 9:16 (= 0.5625)
61     //
62     // width / height = ratio
63     // width * height = area
64     //
65     // width = sqrt(ratio * area)
66     // height = width / ratio
67
68     long orig_w = width();
69     long orig_h = height();
70
71     if (ratio >= 1) {
72         // width >= height
73         // try to keep width
74         set_right(left() + orig_w - 1);
75         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
76     } else {
77         set_bottom(top() + orig_h - 1);
78         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
79     }
80 }
81
82 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
83 {
84     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
85     if(log_level < level)
86     {
87         return;
88     }
89
90     char *message;
91     struct timespec tp;
92     unsigned int time;
93
94     clock_gettime(CLOCK_REALTIME, &tp);
95     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
96
97     va_list args;
98     va_start(args, log);
99     if (log == NULL || vasprintf(&message, log, args) < 0)
100         message = NULL;
101     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
102     va_end(args);
103     free(message);
104 }
105
106 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
107     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
108     if(log_level < level)
109     {
110         return;
111     }
112
113     char *message;
114     struct timespec tp;
115     unsigned int time;
116
117     clock_gettime(CLOCK_REALTIME, &tp);
118         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
119
120         va_list args;
121         va_start(args, log);
122         if (log == NULL || vasprintf(&message, log, args) < 0)
123         message = NULL;
124     fprintf(stderr,  "[%10.3f] [wm %s] [%s, %s(), Line:%d] >>> req %d: %s \n", time / 1000.0, ERROR_FLAG[level], file, func, line, seq_num, message);
125     va_end(args);
126         free(message);
127 }
128
129 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
130 {
131     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
132     if (log_level < level)
133     {
134         return;
135     }
136     char *message;
137     va_list args;
138     va_start(args, log);
139     if (log == NULL || vasprintf(&message, log, args) < 0)
140         message = NULL;
141     fprintf(stderr, "%s \n", message);
142     va_end(args);
143     free(message);
144 }
145
146 std::string get_file_path(const char *file_name, const char *root_path)
147 {
148     char const *default_base_path = root_path;
149     std::string path("");
150
151     if(!file_name) {
152         return path;
153     }
154
155     if (!default_base_path) {
156         default_base_path = getenv("AFM_APP_INSTALL_DIR");
157         if (!default_base_path) {
158             HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
159         }
160     }
161     if (default_base_path) {
162         path = default_base_path;
163         path.append("/etc/");
164         path.append(file_name);
165     }
166
167     // Check for over-ride in /etc/xdg/windowmanager
168     std::string override_path("/etc/xdg/windowmanager/");
169     override_path.append(file_name);
170     std::ifstream i(override_path);
171     if (i.good()) {
172         path = override_path;
173     }
174     i.close();
175
176     HMI_INFO("Using %s", path.c_str());
177     return path;
178 }
179
180 void ChangeAreaReq::dump()
181 {
182     DUMP("=== change request dump ===");
183     DUMP("request from : %s", this->appname.c_str());
184     DUMP("save : %s", this->save ? "true" : "false");
185     for(const auto& req : this->area_req)
186     {
187         DUMP("area change req : %s", req.first.c_str());
188         DUMP("  x:%d y:%d w:%d h:%d",req.second.x, req.second.y, req.second.w, req.second.h);
189     }
190     for(const auto& req : this->update_app2area)
191     {
192         DUMP("update change app : %s, area:%s", req.first.c_str(), req.second.c_str());
193     }
194     DUMP("======== dump end =========");
195 }