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