2f17845d604d75f09e1ce7594a207f059c2ee475
[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 <vector>
23
24 #include <sys/poll.h>
25
26 #ifndef DO_NOT_USE_AFB
27 extern "C"
28 {
29 #include <afb/afb-binding.h>
30 };
31 #endif
32
33 #define CONCAT_(X, Y) X##Y
34 #define CONCAT(X, Y) CONCAT_(X, Y)
35
36 #ifdef __GNUC__
37 #define ATTR_FORMAT(stringindex, firsttocheck) \
38     __attribute__((format(printf, stringindex, firsttocheck)))
39 #define ATTR_NORETURN __attribute__((noreturn))
40 #else
41 #define ATTR_FORMAT(stringindex, firsttocheck)
42 #define ATTR_NORETURN
43 #endif
44
45 #ifdef AFB_BINDING_VERSION
46 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
47 #define logerror(...) AFB_ERROR(__VA_ARGS__)
48 #define fatal(...)              \
49     do                          \
50     {                           \
51         AFB_ERROR(__VA_ARGS__); \
52         abort();                \
53     } while (0)
54 #else
55 #define lognotice(...)
56 #define logerror(...)
57 #define fatal(...) \
58     do             \
59     {              \
60         abort();   \
61     } while (0)
62 #endif
63
64 #ifdef DEBUG_OUTPUT
65 #ifdef AFB_BINDING_VERSION
66 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
67 #else
68 #define logdebug(...)
69 #endif
70 #else
71 #define logdebug(...)
72 #endif
73
74 #ifndef SCOPE_TRACING
75 #define ST()
76 #define STN(N)
77 #else
78 #define ST() \
79     ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
80 #define STN(N) \
81     ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
82
83 struct ScopeTrace
84 {
85     thread_local static int indent;
86     char const *f{};
87     explicit ScopeTrace(char const *func);
88     ~ScopeTrace();
89 };
90 #endif
91
92 /**
93  * @struct unique_fd
94  */
95 struct unique_fd
96 {
97     int fd{-1};
98     unique_fd() = default;
99     explicit unique_fd(int f) : fd{f} {}
100     operator int() const { return fd; }
101     ~unique_fd();
102     unique_fd(unique_fd const &) = delete;
103     unique_fd &operator=(unique_fd const &) = delete;
104     unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
105     unique_fd &operator=(unique_fd &&o)
106     {
107         std::swap(this->fd, o.fd);
108         return *this;
109     }
110 };
111
112 class rectangle
113 {
114   public:
115     explicit rectangle(long wd, long ht) : _right(wd - 1), _bottom(ht - 1) {};
116
117     void set_left(long l) {
118         _left = l;
119     }
120     long left() const { return _left; };
121
122     void set_right(long r) {
123         _right = r;
124     }
125     long right() const { return _right; };
126
127     void set_top(long t) {
128         _top = t;
129     }
130     long top() const { return _top; };
131
132     void set_bottom(long b) {
133         _bottom = b;
134     }
135     long bottom() const { return _bottom; }
136
137     long width() const {
138         if (is_valid())
139             return 0;
140         else
141             return _right - _left + 1;
142     }
143
144     long height() const {
145         if (is_valid())
146             return 0;
147         else
148             return _bottom - _top + 1;
149     }
150
151     void set_aspect(double ratio);
152     void fit(unsigned long to_width, unsigned long to_height);
153     void center(unsigned long outer_w, unsigned long outer_h);
154
155   private:
156     bool is_valid() const {
157         return (_top > _bottom || _left > _right);
158     }
159
160     long _left = 0;
161     long _top = 0;
162     long _right;
163     long _bottom;
164 };
165
166 #endif // !WM_UTIL_HPP