add source for ces2019
[apps/agl-service-windowmanager-2017.git] / src / util.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 "util.hpp"
18
19 #include <sstream>
20 #include <time.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24
25 #include <unistd.h>
26
27 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
28
29 void rectangle::fit(unsigned long to_width, unsigned long to_height)
30 {
31     // fit rect within (to_width x to_height)
32
33     if (to_width <= width()) {
34         // scale to fit with
35         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
36         set_right(left() + to_width - 1);
37     } else {
38         // scale to fit height
39         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
40         set_bottom(top() + to_height - 1);
41     }
42 }
43
44 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
45 {
46     long inner_w = width();
47     long inner_h = height();
48
49     set_left((outer_w - inner_w) / 2);
50     set_right(left() + inner_w - 1);
51     set_top((outer_h - inner_h) / 2);
52     set_bottom(top() + inner_h - 1);
53 }
54
55 void rectangle::set_aspect(double ratio)
56 {
57     // aspect ratio is width:height (= width/height)
58     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
59     //      Portrait of HD's ratio is 9:16 (= 0.5625)
60     //
61     // width / height = ratio
62     // width * height = area
63     //
64     // width = sqrt(ratio * area)
65     // height = width / ratio
66
67     long orig_w = width();
68     long orig_h = height();
69
70     if (ratio >= 1) {
71         // width >= height
72         // try to keep width
73         set_right(left() + orig_w - 1);
74         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
75     } else {
76         set_bottom(top() + orig_h - 1);
77         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
78     }
79 }
80
81 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
82 {
83     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
84     if(log_level < level)
85     {
86         return;
87     }
88
89     char *message;
90     struct timespec tp;
91     unsigned int time;
92
93     clock_gettime(CLOCK_REALTIME, &tp);
94     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
95
96     va_list args;
97     va_start(args, log);
98     if (log == NULL || vasprintf(&message, log, args) < 0)
99         message = NULL;
100     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
101     va_end(args);
102     free(message);
103 }
104
105 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
106     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
107     if(log_level < level)
108     {
109         return;
110     }
111
112     char *message;
113     struct timespec tp;
114     unsigned int time;
115
116     clock_gettime(CLOCK_REALTIME, &tp);
117         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
118
119         va_list args;
120         va_start(args, log);
121         if (log == NULL || vasprintf(&message, log, args) < 0)
122         message = NULL;
123     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);
124     va_end(args);
125         free(message);
126 }
127
128 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
129 {
130     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
131     if (log_level < level)
132     {
133         return;
134     }
135     char *message;
136     va_list args;
137     va_start(args, log);
138     if (log == NULL || vasprintf(&message, log, args) < 0)
139         message = NULL;
140     fprintf(stderr, "%s \n", message);
141     va_end(args);
142     free(message);
143 }
144
145 std::vector<std::string> parseString(std::string str, char delimiter)
146 {
147     // Parse string by delimiter
148     std::vector<std::string> vct;
149     std::stringstream ss{str};
150     std::string buf;
151     while (std::getline(ss, buf, delimiter))
152     {
153         if (!buf.empty())
154         {
155             // Delete space and push back to vector
156             vct.push_back(deleteSpace(buf));
157         }
158     }
159     return vct;
160 }
161
162 std::string deleteSpace(std::string str)
163 {
164     std::string ret = str;
165     size_t pos;
166     while ((pos = ret.find_first_of(" ")) != std::string::npos)
167     {
168         ret.erase(pos, 1);
169     }
170     return ret;
171 }
172