d3cab089cebe4c84d50b34f93d4319c546fdabb5
[apps/agl-service-windowmanager.git] / src / util.hpp
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 #ifndef WM_UTIL_HPP
19 #define WM_UTIL_HPP
20
21 #include <functional>
22 #include <thread>
23 #include <sys/poll.h>
24 #include <string.h>
25
26 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
27
28 #define HMI_ERROR(args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__,"wm",args, ##__VA_ARGS__)
29 #define HMI_WARNING(args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
30 #define HMI_NOTICE(args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
31 #define HMI_INFO(args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
32 #define HMI_DEBUG(args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
33
34 #define HMI_SEQ_ERROR(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
35 #define HMI_SEQ_WARNING(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
36 #define HMI_SEQ_NOTICE(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
37 #define HMI_SEQ_INFO(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
38 #define HMI_SEQ_DEBUG(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
39
40 #define DUMP(args, ...) _DUMP(LOG_LEVEL_DEBUG, args, ##__VA_ARGS__)
41
42 enum LOG_LEVEL{
43     LOG_LEVEL_NONE = 0,
44     LOG_LEVEL_ERROR,
45     LOG_LEVEL_WARNING,
46     LOG_LEVEL_NOTICE,
47     LOG_LEVEL_INFO,
48     LOG_LEVEL_DEBUG,
49     LOG_LEVEL_MAX = LOG_LEVEL_DEBUG
50 };
51
52 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...);
53 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...);
54 void _DUMP(enum LOG_LEVEL level, const char *log, ...);
55
56 /**
57  * @struct unique_fd
58  */
59 struct unique_fd
60 {
61     int fd{-1};
62     unique_fd() = default;
63     explicit unique_fd(int f) : fd{f} {}
64     operator int() const { return fd; }
65     ~unique_fd();
66     unique_fd(unique_fd const &) = delete;
67     unique_fd &operator=(unique_fd const &) = delete;
68     unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
69     unique_fd &operator=(unique_fd &&o)
70     {
71         std::swap(this->fd, o.fd);
72         return *this;
73     }
74 };
75
76 class rectangle
77 {
78   public:
79     explicit rectangle(long wd, long ht) : _right(wd - 1), _bottom(ht - 1) {};
80
81     void set_left(long l) {
82         _left = l;
83     }
84     long left() const { return _left; };
85
86     void set_right(long r) {
87         _right = r;
88     }
89     long right() const { return _right; };
90
91     void set_top(long t) {
92         _top = t;
93     }
94     long top() const { return _top; };
95
96     void set_bottom(long b) {
97         _bottom = b;
98     }
99     long bottom() const { return _bottom; }
100
101     long width() const {
102         if (is_valid())
103             return 0;
104         else
105             return _right - _left + 1;
106     }
107
108     long height() const {
109         if (is_valid())
110             return 0;
111         else
112             return _bottom - _top + 1;
113     }
114
115     void set_aspect(double ratio);
116     void fit(unsigned long to_width, unsigned long to_height);
117     void center(unsigned long outer_w, unsigned long outer_h);
118
119   private:
120     bool is_valid() const {
121         return (_top > _bottom || _left > _right);
122     }
123
124     long _left = 0;
125     long _top = 0;
126     long _right;
127     long _bottom;
128 };
129
130 // Configuration file path helper
131 std::string get_file_path(const char *file_name, const char *log_category = "wm");
132
133 #endif // !WM_UTIL_HPP