2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018 Konsulko Group
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
30 void rectangle::fit(unsigned long to_width, unsigned long to_height)
32 // fit rect within (to_width x to_height)
34 if (to_width <= (unsigned long)width()) {
36 set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
37 set_right(left() + to_width - 1);
39 // scale to fit height
40 set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
41 set_bottom(top() + to_height - 1);
45 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
47 long inner_w = width();
48 long inner_h = height();
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);
56 void rectangle::set_aspect(double ratio)
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)
62 // width / height = ratio
63 // width * height = area
65 // width = sqrt(ratio * area)
66 // height = width / ratio
68 long orig_w = width();
69 long orig_h = height();
74 set_right(left() + orig_w - 1);
75 set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
77 set_bottom(top() + orig_h - 1);
78 set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
82 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
84 const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
94 clock_gettime(CLOCK_REALTIME, &tp);
95 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
99 if (log == NULL || vasprintf(&message, log, args) < 0)
101 fprintf(stderr, "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
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)
117 clock_gettime(CLOCK_REALTIME, &tp);
118 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
122 if (log == NULL || vasprintf(&message, log, args) < 0)
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);
129 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
131 const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
132 if (log_level < level)
139 if (log == NULL || vasprintf(&message, log, args) < 0)
141 fprintf(stderr, "%s \n", message);
146 std::string get_file_path(const char *file_name, const char *root_path)
148 char const *default_base_path = root_path;
149 std::string path("");
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");
161 if (default_base_path) {
162 path = default_base_path;
163 path.append("/etc/");
164 path.append(file_name);
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);
172 path = override_path;
176 HMI_INFO("Using %s", path.c_str());