summaryrefslogtreecommitdiff
path: root/src/xcb_atoms.cc
blob: 92802402970dda1c9d6796023eaee72991d180c6 (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
/*
 * 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 <cassert>
#include <cstddef>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <xcb/xproto.h>

namespace xcb {

namespace {

class AtomsImpl : public Atoms {
 public:
  explicit AtomsImpl(shared_conn conn)
      : conn_(std::move(conn)), storage_(std::make_shared<StorageImpl>()) {}

  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<xcb_atom_t> atoms;
    atoms.reserve(cookie_.size());
    for (auto const& cookie : cookie_) {
      xcb::reply<xcb_intern_atom_reply_t> 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<xcb_atom_t> resolved) {
      resolved_ = std::move(resolved);
    }

   private:
    std::vector<xcb_atom_t> resolved_;
  };

  shared_conn conn_;
  std::map<std::string, size_t> index_;
  std::vector<xcb_intern_atom_cookie_t> cookie_;
  std::shared_ptr<StorageImpl> storage_;
};

}  // namespace

std::unique_ptr<Atoms> Atoms::create(shared_conn conn) {
  return std::make_unique<AtomsImpl>(std::move(conn));
}

}  // namespace xcb