POI: AGL LifeCycle Management
[apps/agl-service-windowmanager.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 <time.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include <unistd.h>
25
26 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
27
28 void rectangle::fit(unsigned long to_width, unsigned long to_height)
29 {
30     // fit rect within (to_width x to_height)
31
32     if (to_width <= (unsigned long)width()) {
33         // scale to fit with
34         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
35         set_right(left() + to_width - 1);
36     } else {
37         // scale to fit height
38         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
39         set_bottom(top() + to_height - 1);
40     }
41 }
42
43 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
44 {
45     long inner_w = width();
46     long inner_h = height();
47
48     set_left((outer_w - inner_w) / 2);
49     set_right(left() + inner_w - 1);
50     set_top((outer_h - inner_h) / 2);
51     set_bottom(top() + inner_h - 1);
52 }
53
54 void rectangle::set_aspect(double ratio)
55 {
56     // aspect ratio is width:height (= width/height)
57     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
58     //      Portrait of HD's ratio is 9:16 (= 0.5625)
59     //
60     // width / height = ratio
61     // width * height = area
62     //
63     // width = sqrt(ratio * area)
64     // height = width / ratio
65
66     long orig_w = width();
67     long orig_h = height();
68
69     if (ratio >= 1) {
70         // width >= height
71         // try to keep width
72         set_right(left() + orig_w - 1);
73         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
74     } else {
75         set_bottom(top() + orig_h - 1);
76         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
77     }
78 }
79
80 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
81 {
82     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
83     if(log_level < level)
84     {
85         return;
86     }
87
88     char *message;
89     struct timespec tp;
90     unsigned int time;
91
92     clock_gettime(CLOCK_REALTIME, &tp);
93     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
94
95     va_list args;
96     va_start(args, log);
97     if (log == NULL || vasprintf(&message, log, args) < 0)
98         message = NULL;
99     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
100     va_end(args);
101     free(message);
102 }
103
104 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
105     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
106     if(log_level < level)
107     {
108         return;
109     }
110
111     char *message;
112     struct timespec tp;
113     unsigned int time;
114
115     clock_gettime(CLOCK_REALTIME, &tp);
116         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
117
118         va_list args;
119         va_start(args, log);
120         if (log == NULL || vasprintf(&message, log, args) < 0)
121         message = NULL;
122     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);
123     va_end(args);
124         free(message);
125 }
126
127 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
128 {
129     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
130     if (log_level < level)
131     {
132         return;
133     }
134     char *message;
135     va_list args;
136     va_start(args, log);
137     if (log == NULL || vasprintf(&message, log, args) < 0)
138         message = NULL;
139     fprintf(stderr, "%s \n", message);
140     va_end(args);
141     free(message);
142 }