all: clang-format
[staging/windowmanager.git] / src / util.hpp
1 #ifndef WM_UTIL_HPP
2 #define WM_UTIL_HPP
3
4 #include <functional>
5 #include <sys/poll.h>
6 #include <vector>
7
8 #ifdef __GNUC__
9 #define ATTR_FORMAT(stringindex, firsttocheck) \
10    __attribute__((format(printf, stringindex, firsttocheck)))
11 #else
12 #define ATTR_FORMAT(stringindex, firsttocheck)
13 #endif
14
15 void lognotice(char const *fmt, ...) ATTR_FORMAT(1, 2);
16 void logerror(char const *fmt, ...) ATTR_FORMAT(1, 2);
17 void fatal(char const *fmt, ...) ATTR_FORMAT(1, 2);
18
19 #ifdef DEBUG_OUTPUT
20 void logdebug(char const *fmt, ...) ATTR_FORMAT(1, 2);
21 #else
22 static inline void logdebug(char const *fmt, ...) ATTR_FORMAT(1, 2);
23 static inline void logdebug(char const *fmt, ...) {}
24 #endif
25
26 //      _                   _                 _                       __     _
27 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
28 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
29 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
30 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
31 //                                                 |_|         |_____|
32 struct unique_fd {
33    int fd{-1};
34    unique_fd() = default;
35    explicit unique_fd(int f) : fd{f} {}
36    operator int() const { return fd; }
37    ~unique_fd();
38    unique_fd(unique_fd const &) = delete;
39    unique_fd &operator=(unique_fd const &) = delete;
40    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
41    unique_fd &operator=(unique_fd &&o) {
42       std::swap(this->fd, o.fd);
43       return *this;
44    }
45 };
46
47 //      _                   _     ____       _ _
48 //  ___| |_ _ __ _   _  ___| |_  |  _ \ ___ | | | ___ _ __
49 // / __| __| '__| | | |/ __| __| | |_) / _ \| | |/ _ \ '__|
50 // \__ \ |_| |  | |_| | (__| |_  |  __/ (_) | | |  __/ |
51 // |___/\__|_|   \__,_|\___|\__| |_|   \___/|_|_|\___|_|
52 //
53 struct Poller {
54    std::vector<std::function<int(int)>> handlers;
55    std::vector<struct pollfd> pfds;
56
57    Poller() = default;
58    void add_fd(int fd, std::function<int(int)> handler);
59    int check_events();
60 };
61
62 #endif  // !WM_UTIL_HPP