Skip to content

Commit c3a883d

Browse files
committed
some cleanup
1 parent 9d7d9be commit c3a883d

File tree

11 files changed

+132
-63
lines changed

11 files changed

+132
-63
lines changed

src/wayland/input_method/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ qt_add_library(quickshell-wayland-input-method STATIC
66
manager.cpp
77
qml.cpp
88
qml_helpers.cpp
9+
c_helpers.cpp
910
)
1011

1112
qt_add_qml_module(quickshell-wayland-input-method
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "c_helpers.hpp"
2+
3+
#include <qlogging.h>
4+
5+
namespace qs::wayland::input_method::impl {
6+
7+
void FreeDeleter::operator()(const char* p) const {
8+
std::free(const_cast<std::remove_const_t<char>*>(p)); // NOLINT
9+
}
10+
11+
SharedMemory::SharedMemory(const char* shmName, int oFlag, size_t size)
12+
: mShmName(shmName)
13+
, mSize(size)
14+
, fd(shm_open(this->mShmName, oFlag, 0))
15+
, map(nullptr) {
16+
if (this->fd == -1) {
17+
perror("");
18+
qDebug() << "Virtual keyboard failed to open shared memory";
19+
return;
20+
}
21+
if (ftruncate(this->fd, static_cast<int>(size)) == -1) {
22+
this->fd = -1;
23+
perror("");
24+
qDebug() << "Virtual keyboard failed to resize shared memory to" << size;
25+
return;
26+
}
27+
this->map = static_cast<char*>(mmap(nullptr, this->mSize, PROT_WRITE, MAP_SHARED, this->fd, 0));
28+
if (this->map == MAP_FAILED) {
29+
perror("");
30+
qDebug() << "Virtual keyboard failed to open shared memory";
31+
return;
32+
}
33+
}
34+
SharedMemory::~SharedMemory() {
35+
if (this->fd != -1) {
36+
close(this->fd);
37+
shm_unlink(this->mShmName);
38+
}
39+
if (this->map != nullptr) {
40+
munmap(this->map, this->mSize);
41+
}
42+
}
43+
SharedMemory::SharedMemory(SharedMemory&& other) noexcept
44+
: mShmName(std::exchange(other.mShmName, nullptr))
45+
, mSize(std::exchange(other.mSize, 0))
46+
, fd(std::exchange(other.fd, -1))
47+
, map(std::exchange(other.map, nullptr)) {}
48+
SharedMemory& SharedMemory::operator=(SharedMemory&& other) noexcept {
49+
this->mShmName = std::exchange(other.mShmName, nullptr);
50+
this->mSize = std::exchange(other.mSize, 0);
51+
this->fd = std::exchange(other.fd, -1);
52+
this->map = std::exchange(other.map, nullptr);
53+
return *this;
54+
}
55+
56+
SharedMemory::operator bool() const { return fd != -1 && map != MAP_FAILED; }
57+
[[nodiscard]] int SharedMemory::get() const { return fd; }
58+
59+
void SharedMemory::write(const char* string) {
60+
if (!this->map) return;
61+
strcpy(this->map, string);
62+
}
63+
64+
} // namespace qs::wayland::input_method::impl
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <sys/mman.h>
4+
5+
namespace qs::wayland::input_method::impl {
6+
7+
struct FreeDeleter {
8+
void operator()(const char* p) const;
9+
};
10+
// Dont use this for literals, only c strings that were allocated
11+
using uniqueCString = std::unique_ptr<const char, FreeDeleter>;
12+
13+
class SharedMemory {
14+
public:
15+
SharedMemory(const char* shmName, int oFlag, size_t size);
16+
~SharedMemory();
17+
SharedMemory(const SharedMemory&) = delete;
18+
SharedMemory(SharedMemory&& other) noexcept;
19+
SharedMemory& operator=(const SharedMemory&) = delete;
20+
SharedMemory& operator=(SharedMemory&& other) noexcept;
21+
22+
[[nodiscard]] operator bool() const;
23+
[[nodiscard]] int get() const;
24+
25+
void write(const char* string);
26+
27+
private:
28+
const char* mShmName;
29+
size_t mSize;
30+
int fd;
31+
char* map;
32+
};
33+
34+
} // namespace qs::wayland::input_method::impl

src/wayland/input_method/input_method.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "input_method.hpp"
22

3+
#include <qlogging.h>
34
#include <qtmetamacros.h>
45
#include <wayland-input-method-unstable-v2-client-protocol.h>
56

@@ -30,6 +31,7 @@ void InputMethodHandle::deleteText(int before, int after) {
3031
}
3132
void InputMethodHandle::commit() { zwp_input_method_v2::commit(this->serial++); }
3233

34+
bool InputMethodHandle::hasKeyboard() const { return this->keyboard != nullptr; }
3335
QPointer<InputMethodKeyboardGrab> InputMethodHandle::grabKeyboard() {
3436
if (this->keyboard) return this->keyboard;
3537

src/wayland/input_method/input_method.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class InputMethodHandle
2525
void deleteText(int before, int after);
2626
void commit();
2727

28+
[[nodiscard]] bool hasKeyboard() const;
2829
QPointer<InputMethodKeyboardGrab> grabKeyboard();
2930
void releaseKeyboard();
3031

src/wayland/input_method/key_map_state.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <wayland-client-protocol.h>
77
#include <xkbcommon/xkbcommon.h>
88

9-
#include "types.hpp"
9+
#include "c_helpers.hpp"
1010

1111
namespace qs::wayland::input_method::impl {
1212

src/wayland/input_method/keyboard_grab.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ void InputMethodKeyboardGrab::zwp_input_method_keyboard_grab_v2_key(
9191
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) emit returnPress();
9292
return;
9393
}
94+
95+
// Skip adding the control keys because we've consumed them
96+
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
97+
this->mKeyState[key - this->mKeyMapState.minKeycode()] = KeyState::PRESSED;
98+
} else if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
99+
this->mKeyState[key - this->mKeyMapState.minKeycode()] = KeyState::RELEASED;
100+
}
101+
94102
if (sym == XKB_KEY_Up) {
95103
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) emit directionPress(DirectionKey::UP);
96104
return;
@@ -116,17 +124,12 @@ void InputMethodKeyboardGrab::zwp_input_method_keyboard_grab_v2_key(
116124
return;
117125
}
118126

119-
// Skip adding the control keys because we've consumed them
120-
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
121-
this->mKeyState[key - this->mKeyMapState.minKeycode()] = KeyState::PRESSED;
122-
} else if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
123-
this->mKeyState[key - this->mKeyMapState.minKeycode()] = KeyState::RELEASED;
124-
}
125-
126127
QChar character = this->mKeyMapState.getChar(key);
127128
if (character != '\0') {
128129
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) emit keyPress(character);
129130
return;
131+
} else {
132+
this->mVirturalKeyboard->sendKey(key, static_cast<wl_keyboard_key_state>(state));
130133
}
131134
}
132135

src/wayland/input_method/qml.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ void InputMethod::deleteText(int before, int after) {
3434

3535
bool InputMethod::isActive() const { return this->hasInput() && this->handle->isActive(); }
3636

37-
bool InputMethod::keyboardOnActive() const { return this->mKeyboardOnActive; }
38-
void InputMethod::setKeyboardOnActive(bool value) {
39-
if (this->mKeyboardOnActive == value) return;
40-
this->mKeyboardOnActive = value;
41-
emit keyboardOnActiveChanged();
42-
}
43-
4437
QQmlComponent* InputMethod::keyboardComponent() const { return this->mKeyboardComponent; }
4538
void InputMethod::setKeyboardComponent(QQmlComponent* keyboardComponent) {
4639
if (this->mKeyboardComponent == keyboardComponent) return;
@@ -86,11 +79,18 @@ void InputMethod::releaseInput() {
8679

8780
bool InputMethod::hasKeyboard() const {
8881
// The lifetime of keyboard should be less than handle's
82+
if (this->keyboard) {
83+
assert(this->handle->hasKeyboard());
84+
}
8985
return this->keyboard;
9086
}
9187

9288
void InputMethod::grabKeyboard() {
9389
if (this->hasKeyboard()) return;
90+
if (this->handle->hasKeyboard()) {
91+
qmlDebug(this) << "Only one input method can grad a keyboard at any one time";
92+
return;
93+
}
9494
auto* instanceObj =
9595
this->mKeyboardComponent->create(QQmlEngine::contextForObject(this->mKeyboardComponent));
9696
auto* instance = qobject_cast<Keyboard*>(instanceObj);
@@ -121,9 +121,7 @@ void InputMethod::releaseKeyboard() {
121121

122122
void InputMethod::handleKeyboardActive() {
123123
if (!this->mKeyboardComponent) return;
124-
if (this->isActive() && this->mKeyboardOnActive) {
125-
this->grabKeyboard();
126-
} else if (this->keyboard) {
124+
if (this->keyboard) {
127125
this->releaseKeyboard();
128126
}
129127
}
@@ -184,6 +182,7 @@ QString KeyboardDirectionKey::toString(Enum direction) {
184182
case LEFT: return "LEFT";
185183
case RIGHT: return "RIGHT";
186184
}
185+
return "UNKNOWN";
187186
}
188187
KeyboardDirectionKey::Enum KeyboardDirectionKey::fromDirection(impl::DirectionKey direction) {
189188
return static_cast<KeyboardDirectionKey::Enum>(direction);

src/wayland/input_method/qml.hpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

33
#include <qobject.h>
4+
#include <qqmlcomponent.h>
45
#include <qqmlintegration.h>
6+
#include <qtclasshelpermacros.h>
57
#include <qtmetamacros.h>
68

79
#include "types.hpp"
@@ -65,10 +67,6 @@ class InputMethod: public QObject {
6567
Q_PROPERTY(bool active READ isActive NOTIFY activeChanged);
6668
Q_PROPERTY(bool hasInput READ hasInput NOTIFY hasInputChanged);
6769
Q_PROPERTY(bool hasKeyboard READ hasKeyboard NOTIFY hasKeyboardChanged);
68-
Q_PROPERTY(
69-
bool keyboardOnActive MEMBER mKeyboardOnActive READ keyboardOnActive WRITE setKeyboardOnActive
70-
NOTIFY keyboardOnActiveChanged
71-
);
7270
Q_PROPERTY(
7371
bool clearPreeditOnKeyboardRelease MEMBER mClearPreeditOnKeyboardRelease NOTIFY
7472
clearPreeditOnKeyboardReleaseChanged
@@ -91,9 +89,6 @@ class InputMethod: public QObject {
9189

9290
Q_INVOKABLE [[nodiscard]] bool isActive() const;
9391

94-
Q_INVOKABLE [[nodiscard]] bool keyboardOnActive() const;
95-
Q_INVOKABLE void setKeyboardOnActive(bool value);
96-
9792
Q_INVOKABLE [[nodiscard]] QQmlComponent* keyboardComponent() const;
9893
Q_INVOKABLE void setKeyboardComponent(QQmlComponent* keyboardComponent);
9994

@@ -109,7 +104,6 @@ class InputMethod: public QObject {
109104
void activeChanged();
110105
void hasInputChanged();
111106
void hasKeyboardChanged();
112-
void keyboardOnActiveChanged();
113107
void clearPreeditOnKeyboardReleaseChanged();
114108
void keyboardComponentChanged();
115109

@@ -123,7 +117,6 @@ private slots:
123117
Keyboard* keyboard = nullptr;
124118
QQmlComponent* mKeyboardComponent = nullptr;
125119

126-
bool mKeyboardOnActive = false;
127120
bool mClearPreeditOnKeyboardRelease = true;
128121
};
129122

src/wayland/input_method/types.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
#pragma once
22

3-
#include <qobject.h>
4-
#include <qtclasshelpermacros.h>
5-
63
namespace qs::wayland::input_method::impl {
74
enum class DirectionKey : uint8_t { UP, DOWN, LEFT, RIGHT };
8-
9-
struct FreeDeleter {
10-
void operator()(const char* p) const {
11-
std::free(const_cast<std::remove_const_t<char>*>(p)); // NOLINT
12-
}
13-
};
14-
// Dont use this for literals, only c strings that were allocated
15-
using uniqueCString = std::unique_ptr<const char, FreeDeleter>;
165
} // namespace qs::wayland::input_method::impl

0 commit comments

Comments
 (0)