Directory: | ./ |
---|---|
File: | server/keyboard_pool.cpp |
Date: | 2024-01-22 17:25:27 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 104 | 108 | 96.3% |
Branches: | 49 | 68 | 72.1% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | SPDX-FileCopyrightText: 2020 Roman Gilg <subdiff@gmail.com> | ||
3 | SPDX-FileCopyrightText: 2021 Francesco Sorrentino <francesco.sorr@gmail.com> | ||
4 | |||
5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only | ||
6 | */ | ||
7 | #include "keyboard_pool.h" | ||
8 | #include "display.h" | ||
9 | #include "keyboard.h" | ||
10 | #include "keyboard_p.h" | ||
11 | #include "seat.h" | ||
12 | #include "seat_p.h" | ||
13 | #include "utils.h" | ||
14 | |||
15 | #include <algorithm> | ||
16 | |||
17 | namespace Wrapland::Server | ||
18 | { | ||
19 | |||
20 | 561 | keyboard_pool::keyboard_pool(Seat* seat) | |
21 | 187 | : seat{seat} | |
22 | { | ||
23 | 187 | } | |
24 | |||
25 | 374 | keyboard_pool::~keyboard_pool() | |
26 | { | ||
27 |
1/2✓ Branch 0 taken 374 times.
✗ Branch 1 not taken.
|
374 | QObject::disconnect(focus.surface_lost_notifier); |
28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 374 times.
|
374 | for (auto dev : devices) { |
29 | ✗ | QObject::disconnect(dev, nullptr, seat, nullptr); | |
30 | } | ||
31 | 374 | } | |
32 | |||
33 | 177 | keyboard_focus const& keyboard_pool::get_focus() const | |
34 | { | ||
35 | 177 | return focus; | |
36 | } | ||
37 | |||
38 | 58 | keyboard_modifiers const& keyboard_pool::get_modifiers() const | |
39 | { | ||
40 | 58 | return modifiers; | |
41 | } | ||
42 | |||
43 | 6 | keyboard_repeat_info const& keyboard_pool::get_repeat_info() const | |
44 | { | ||
45 | 6 | return keyRepeat; | |
46 | } | ||
47 | |||
48 | 49 | void keyboard_pool::create_device(Client* client, uint32_t version, uint32_t id) | |
49 | { | ||
50 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | auto keyboard = new Keyboard(client, version, id, seat); |
51 | 49 | keyboard->repeatInfo(keyRepeat.rate, keyRepeat.delay); | |
52 | 49 | devices.push_back(keyboard); | |
53 | |||
54 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
49 | if (focus.surface && focus.surface->client() == keyboard->client()) { |
55 | // this is a keyboard for the currently focused keyboard surface | ||
56 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (keymap) { |
57 | ✗ | keyboard->setKeymap(keymap); | |
58 | } | ||
59 | 4 | focus.devices.push_back(keyboard); | |
60 | 4 | keyboard->setFocusedSurface(focus.serial, focus.surface); | |
61 | 4 | } | |
62 | |||
63 | 98 | QObject::connect(keyboard, &Keyboard::resourceDestroyed, seat, [keyboard, this] { | |
64 | 49 | remove_one(devices, keyboard); | |
65 | 49 | remove_one(focus.devices, keyboard); | |
66 | |||
67 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | assert(!contains(devices, keyboard)); |
68 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | assert(!contains(focus.devices, keyboard)); |
69 | 49 | }); | |
70 | |||
71 | 49 | Q_EMIT seat->keyboardCreated(keyboard); | |
72 | 49 | } | |
73 | |||
74 | 14 | bool keyboard_pool::update_key(uint32_t key, key_state state) | |
75 | { | ||
76 | 14 | auto it = states.find(key); | |
77 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
|
14 | if (it != states.end() && it->second == state) { |
78 | 2 | return false; | |
79 | } | ||
80 | 12 | states[key] = state; | |
81 | 12 | return true; | |
82 | 14 | } | |
83 | |||
84 | 14 | void keyboard_pool::key(uint32_t key, key_state state) | |
85 | { | ||
86 | 14 | lastStateSerial = seat->d_ptr->display()->handle->nextSerial(); | |
87 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
|
14 | if (!update_key(key, state)) { |
88 | 2 | return; | |
89 | } | ||
90 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (focus.surface) { |
91 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
|
22 | for (auto kbd : focus.devices) { |
92 | 10 | kbd->key(lastStateSerial, key, state); | |
93 | } | ||
94 | 12 | } | |
95 | 14 | } | |
96 | 187 | ||
97 | 2 | void keyboard_pool::update_modifiers(uint32_t depressed, | |
98 | uint32_t latched, | ||
99 | uint32_t locked, | ||
100 | uint32_t group) | ||
101 | { | ||
102 | 2 | auto mods = keyboard_modifiers{depressed, latched, locked, group, modifiers.serial}; | |
103 | 187 | ||
104 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (modifiers == mods) { |
105 | ✗ | return; | |
106 | } | ||
107 | |||
108 | 2 | modifiers = mods; | |
109 | |||
110 | 2 | auto const serial = seat->d_ptr->display()->handle->nextSerial(); | |
111 | 2 | modifiers.serial = serial; | |
112 | |||
113 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (focus.surface) { |
114 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (auto& keyboard : focus.devices) { |
115 | 1 | keyboard->updateModifiers(serial, depressed, latched, locked, group); | |
116 | } | ||
117 | 2 | } | |
118 | 2 | } | |
119 | |||
120 | 113 | void keyboard_pool::set_focused_surface(Surface* surface) | |
121 | { | ||
122 | 113 | auto const serial = seat->d_ptr->display()->handle->nextSerial(); | |
123 | |||
124 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 113 times.
|
124 | for (auto kbd : focus.devices) { |
125 | 11 | kbd->setFocusedSurface(serial, nullptr); | |
126 | } | ||
127 | |||
128 |
2/2✓ Branch 0 taken 95 times.
✓ Branch 1 taken 18 times.
|
113 | if (focus.surface) { |
129 | 18 | QObject::disconnect(focus.surface_lost_notifier); | |
130 | 18 | } | |
131 | |||
132 |
1/2✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
|
113 | focus = {}; |
133 | 113 | focus.devices = interfacesForSurface(surface, devices); | |
134 | |||
135 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 102 times.
|
113 | if (surface) { |
136 | 102 | focus.surface = surface; | |
137 | 102 | focus.serial = serial; | |
138 | 204 | focus.surface_lost_notifier | |
139 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
288 | = QObject::connect(surface, &Surface::resourceDestroyed, seat, [this] { focus = {}; }); |
140 | 102 | } | |
141 | |||
142 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 113 times.
|
167 | for (auto kbd : focus.devices) { |
143 |
3/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 1 times.
|
54 | if (kbd->d_ptr->needs_keymap_update && keymap) { |
144 | 1 | kbd->setKeymap(keymap); | |
145 | 1 | } | |
146 | 54 | kbd->setFocusedSurface(serial, surface); | |
147 | } | ||
148 | 113 | } | |
149 | |||
150 | 2 | void keyboard_pool::set_keymap(char const* keymap) | |
151 | { | ||
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (this->keymap == keymap) { |
153 | ✗ | return; | |
154 | } | ||
155 | |||
156 | 2 | this->keymap = keymap; | |
157 | |||
158 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (auto device : devices) { |
159 | 1 | device->d_ptr->needs_keymap_update = true; | |
160 | } | ||
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (keymap) { |
162 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (auto device : focus.devices) { |
163 | 1 | device->setKeymap(keymap); | |
164 | } | ||
165 | 2 | } | |
166 | 2 | } | |
167 | |||
168 | 5 | void keyboard_pool::set_repeat_info(int32_t charactersPerSecond, int32_t delay) | |
169 | { | ||
170 | 5 | keyRepeat.rate = qMax(charactersPerSecond, 0); | |
171 | 5 | keyRepeat.delay = qMax(delay, 0); | |
172 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | for (auto device : devices) { |
173 | 1 | device->repeatInfo(keyRepeat.rate, keyRepeat.delay); | |
174 | } | ||
175 | 5 | } | |
176 | |||
177 | 58 | std::vector<uint32_t> keyboard_pool::pressed_keys() const | |
178 | { | ||
179 | 58 | std::vector<uint32_t> keys; | |
180 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 16 times.
|
74 | for (auto const& [serial, state] : states) { |
181 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
|
16 | if (state == key_state::pressed) { |
182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | keys.push_back(serial); |
183 | 3 | } | |
184 | } | ||
185 | 58 | return keys; | |
186 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | } |
187 | |||
188 | } | ||
189 |