aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/protocols/GlobalShortcuts.cpp
blob: b376cae338aa3ba29d400879bce92dc37291a3be (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "GlobalShortcuts.hpp"
#include "../Compositor.hpp"

#define GLOBAL_SHORTCUTS_VERSION 1

static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) {
    g_pProtocolManager->m_pGlobalShortcutsProtocolManager->bindManager(client, data, version, id);
}

static void handleDisplayDestroy(struct wl_listener* listener, void* data) {
    g_pProtocolManager->m_pGlobalShortcutsProtocolManager->displayDestroy();
}

void CGlobalShortcutsProtocolManager::displayDestroy() {
    wl_global_destroy(m_pGlobal);
}

CGlobalShortcutsProtocolManager::CGlobalShortcutsProtocolManager() {
    m_pGlobal = wl_global_create(g_pCompositor->m_sWLDisplay, &hyprland_global_shortcuts_manager_v1_interface, GLOBAL_SHORTCUTS_VERSION, this, bindManagerInt);

    if (!m_pGlobal) {
        Debug::log(ERR, "GlobalShortcutsManager could not start!");
        return;
    }

    m_liDisplayDestroy.notify = handleDisplayDestroy;
    wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy);

    Debug::log(LOG, "GlobalShortcutsManager started successfully!");
}

static void handleRegisterShortcut(wl_client* client, wl_resource* resource, uint32_t shortcut, const char* id, const char* app_id, const char* description,
                                   const char* trigger_description) {
    g_pProtocolManager->m_pGlobalShortcutsProtocolManager->registerShortcut(client, resource, shortcut, id, app_id, description, trigger_description);
}

static void handleDestroy(wl_client* client, wl_resource* resource) {
    wl_resource_destroy(resource);
}

static const struct hyprland_global_shortcuts_manager_v1_interface globalShortcutsManagerImpl = {
    .register_shortcut = handleRegisterShortcut,
    .destroy           = handleDestroy,
};

static const struct hyprland_global_shortcut_v1_interface shortcutImpl = {
    .destroy = handleDestroy,
};

void CGlobalShortcutsProtocolManager::bindManager(wl_client* client, void* data, uint32_t version, uint32_t id) {
    const auto RESOURCE = wl_resource_create(client, &hyprland_global_shortcuts_manager_v1_interface, version, id);
    wl_resource_set_implementation(RESOURCE, &globalShortcutsManagerImpl, this, nullptr);

    Debug::log(LOG, "GlobalShortcutsManager bound successfully!");

    m_vClients.emplace_back(std::make_unique<SShortcutClient>(client));
}

SShortcutClient* CGlobalShortcutsProtocolManager::clientFromWlClient(wl_client* client) {
    for (auto& c : m_vClients) {
        if (c->client == client) {
            return c.get();
        }
    }

    return nullptr;
}

static void onShortcutDestroy(wl_resource* pResource) {
    g_pProtocolManager->m_pGlobalShortcutsProtocolManager->destroyShortcut(pResource);
}

void CGlobalShortcutsProtocolManager::registerShortcut(wl_client* client, wl_resource* resource, uint32_t shortcut, const char* id, const char* app_id, const char* description,
                                                       const char* trigger_description) {
    const auto PCLIENT = clientFromWlClient(client);

    if (!PCLIENT) {
        Debug::log(ERR, "Error at global shortcuts: no client in register?");
        return;
    }

    for (auto& c : m_vClients) {
        for (auto& sh : c->shortcuts) {
            if (sh->appid == app_id && sh->id == id) {
                wl_resource_post_error(resource, HYPRLAND_GLOBAL_SHORTCUTS_MANAGER_V1_ERROR_ALREADY_TAKEN, "Combination is taken");
                return;
            }
        }
    }

    const auto PSHORTCUT   = PCLIENT->shortcuts.emplace_back(std::make_unique<SShortcut>()).get();
    PSHORTCUT->id          = id;
    PSHORTCUT->description = description;
    PSHORTCUT->appid       = app_id;
    PSHORTCUT->shortcut    = shortcut;

    PSHORTCUT->resource = wl_resource_create(client, &hyprland_global_shortcut_v1_interface, 1, shortcut);
    if (!PSHORTCUT->resource) {
        wl_client_post_no_memory(client);
        std::erase_if(PCLIENT->shortcuts, [&](const auto& other) { return other.get() == PSHORTCUT; });
        return;
    }

    wl_resource_set_implementation(PSHORTCUT->resource, &shortcutImpl, this, &onShortcutDestroy);
}

bool CGlobalShortcutsProtocolManager::globalShortcutExists(std::string appid, std::string trigger) {
    for (auto& c : m_vClients) {
        for (auto& sh : c->shortcuts) {
            if (sh->appid == appid && sh->id == trigger) {
                return true;
            }
        }
    }

    return false;
}

void CGlobalShortcutsProtocolManager::sendGlobalShortcutEvent(std::string appid, std::string trigger, bool pressed) {
    for (auto& c : m_vClients) {
        for (auto& sh : c->shortcuts) {
            if (sh->appid == appid && sh->id == trigger) {
                timespec now;
                clock_gettime(CLOCK_MONOTONIC, &now);
                uint32_t tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0;
                uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF;
                if (pressed)
                    hyprland_global_shortcut_v1_send_pressed(sh->resource, tvSecHi, tvSecLo, now.tv_nsec);
                else
                    hyprland_global_shortcut_v1_send_released(sh->resource, tvSecHi, tvSecLo, now.tv_nsec);
            }
        }
    }
}

std::vector<SShortcut> CGlobalShortcutsProtocolManager::getAllShortcuts() {
    std::vector<SShortcut> copy;
    for (auto& c : m_vClients) {
        for (auto& sh : c->shortcuts) {
            copy.push_back(*sh);
        }
    }

    return copy;
}

void CGlobalShortcutsProtocolManager::destroyShortcut(wl_resource* resource) {
    for (auto& c : m_vClients) {
        std::erase_if(c->shortcuts, [&](const auto& other) { return other->resource == resource; });
    }
}