/* * Copyright 2026 Joel Klinghed * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #include "xcb_atoms.hh" #include "xcb_connection.hh" #include "xcb_event.hh" #include #include #include #include #include #include #include #include namespace xcb { namespace { class AtomsImpl : public Atoms { public: explicit AtomsImpl(shared_conn conn) : conn_(std::move(conn)), storage_(std::make_shared()) {} Reference get(std::string atom) override { auto it = index_.find(atom); size_t index; if (it == index_.end()) { index = cookie_.size(); cookie_.push_back( xcb_intern_atom(conn_.get(), 0, atom.size(), atom.c_str())); index_.emplace(std::move(atom), index); } else { index = it->second; } return {storage_, index}; } bool sync() override { std::vector atoms; atoms.reserve(cookie_.size()); for (auto const& cookie : cookie_) { xcb::reply reply( xcb_intern_atom_reply(conn_.get(), cookie, nullptr)); if (!reply) return false; atoms.push_back(reply->atom); } storage_->set(std::move(atoms)); return true; } private: class StorageImpl : public Storage { public: [[nodiscard]] xcb_atom_t get(size_t id) const override { assert(id < resolved_.size()); return resolved_[id]; } void set(std::vector resolved) { resolved_ = std::move(resolved); } private: std::vector resolved_; }; shared_conn conn_; std::map index_; std::vector cookie_; std::shared_ptr storage_; }; } // namespace std::unique_ptr Atoms::create(shared_conn conn) { return std::make_unique(std::move(conn)); } } // namespace xcb