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