aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/protocols/WaylandProtocol.hpp
blob: 056322ac81a0f380eff70caf63ab8be7579b653a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

#include "../defines.hpp"

#include <functional>

#define RESOURCE_OR_BAIL(resname)                                                                                                                                                  \
    const auto resname = (CWaylandResource*)wl_resource_get_user_data(resource);                                                                                                   \
    if (!resname)                                                                                                                                                                  \
        return;

#define PROTO NProtocols

#define EXTRACT_CLASS_NAME()                                                                                                                                                       \
    []() constexpr -> std::string_view {                                                                                                                                           \
        constexpr std::string_view prettyFunction = __PRETTY_FUNCTION__;                                                                                                           \
        constexpr size_t           colons         = prettyFunction.find("::");                                                                                                     \
        if (colons != std::string_view::npos) {                                                                                                                                    \
            constexpr size_t begin = prettyFunction.substr(0, colons).rfind(' ') + 1;                                                                                              \
            constexpr size_t end   = colons - begin;                                                                                                                               \
            return prettyFunction.substr(begin, end);                                                                                                                              \
        } else {                                                                                                                                                                   \
            return "Global";                                                                                                                                                       \
        }                                                                                                                                                                          \
    }()

#define LOGM(level, ...)                                                                                                                                                           \
    do {                                                                                                                                                                           \
        std::ostringstream oss;                                                                                                                                                    \
        if (level == WARN || level == ERR || level == CRIT) {                                                                                                                      \
            oss << "[" << __FILE__ << ":" << __LINE__ << "] ";                                                                                                                     \
        } else if (level == LOG || level == INFO || level == TRACE) {                                                                                                              \
            oss << "[" << EXTRACT_CLASS_NAME() << "] ";                                                                                                                            \
        }                                                                                                                                                                          \
        if constexpr (std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value == 1 && std::is_same_v<decltype(__VA_ARGS__), std::string>) {                                 \
            oss << __VA_ARGS__;                                                                                                                                                    \
            Debug::log(level, oss.str());                                                                                                                                          \
        } else {                                                                                                                                                                   \
            Debug::log(level, std::format("{}{}", oss.str(), std::format(__VA_ARGS__)));                                                                                           \
        }                                                                                                                                                                          \
    } while (0)

class IWaylandProtocol;
struct IWaylandProtocolDestroyWrapper {
    wl_listener       listener;
    IWaylandProtocol* parent = nullptr;
};

class IWaylandProtocol {
  public:
    IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name);
    virtual ~IWaylandProtocol();

    virtual void                   onDisplayDestroy();
    virtual void                   removeGlobal();
    virtual wl_global*             getGlobal();

    virtual void                   bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) = 0;

    IWaylandProtocolDestroyWrapper m_liDisplayDestroy;

  private:
    std::string m_szName;
    wl_global*  m_pGlobal = nullptr;
};