|
1 | 1 | #include "virtual_keyboard.hpp" |
2 | 2 | #include <chrono> |
3 | 3 |
|
4 | | -#include <sys/mman.h> |
5 | 4 | #include <fcntl.h> |
| 5 | +#include <sys/mman.h> |
6 | 6 |
|
7 | 7 | namespace qs::wayland::input_method::impl { |
8 | 8 |
|
9 | 9 | namespace { |
10 | 10 | using namespace std::chrono; |
11 | 11 | uint32_t now() { |
12 | | - return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 12 | + return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
13 | 13 | } |
14 | 14 | } // namespace |
15 | 15 |
|
16 | | -VirtualKeyboardHandle::VirtualKeyboardHandle(::zwp_virtual_keyboard_v1* keyboard, const KeyMapState& keymap) |
| 16 | +VirtualKeyboardHandle::VirtualKeyboardHandle( |
| 17 | + ::zwp_virtual_keyboard_v1* keyboard, |
| 18 | + const KeyMapState& keymap |
| 19 | +) |
17 | 20 | : QtWayland::zwp_virtual_keyboard_v1(keyboard) { |
18 | | - setKeymapState(keymap); |
| 21 | + setKeymapState(keymap); |
19 | 22 | } |
20 | 23 |
|
21 | 24 | VirtualKeyboardHandle::~VirtualKeyboardHandle() { this->destroy(); } |
22 | 25 |
|
23 | 26 | void VirtualKeyboardHandle::setKeymapState(const KeyMapState& keymap) { |
24 | | - if (this->mKeyMapState == keymap) return; |
25 | | - if (!keymap) return; |
26 | | - this->mKeyMapState = keymap; |
| 27 | + if (this->mKeyMapState == keymap) return; |
| 28 | + if (!keymap) return; |
| 29 | + this->mKeyMapState = keymap; |
27 | 30 |
|
28 | | - static const char* shmName = "/quickshellvirtualkeyboardformat"; |
29 | | - if(shm_unlink(shmName) == -1 && errno != ENOENT){ |
30 | | - perror(""); |
31 | | - qDebug() << "Virtual keyboard failed to unlink shared memory"; |
32 | | - } |
33 | | - int fd = shm_open(shmName, O_CREAT | O_RDWR, 0); |
34 | | - if(fd == -1) { |
35 | | - perror(""); |
36 | | - qDebug() << "Virtual keyboard failed to open shared memory"; |
37 | | - return; |
38 | | - } |
39 | | - auto keymapString = keymap.keyMapAsString(); |
40 | | - size_t size = strlen(keymapString.get()) + 1; |
41 | | - if(ftruncate(fd, static_cast<int>(size)) == -1) { |
42 | | - perror(""); |
43 | | - qDebug() << "Virtual keyboard failed to resize shared memory to" << size; |
44 | | - return; |
45 | | - } |
| 31 | + static const char* shmName = "/quickshellvirtualkeyboardformat"; |
| 32 | + if (shm_unlink(shmName) == -1 && errno != ENOENT) { |
| 33 | + perror(""); |
| 34 | + qDebug() << "Virtual keyboard failed to unlink shared memory"; |
| 35 | + } |
| 36 | + int fd = shm_open(shmName, O_CREAT | O_RDWR, 0); |
| 37 | + if (fd == -1) { |
| 38 | + perror(""); |
| 39 | + qDebug() << "Virtual keyboard failed to open shared memory"; |
| 40 | + return; |
| 41 | + } |
| 42 | + auto keymapString = keymap.keyMapAsString(); |
| 43 | + size_t size = strlen(keymapString.get()) + 1; |
| 44 | + if (ftruncate(fd, static_cast<int>(size)) == -1) { |
| 45 | + perror(""); |
| 46 | + qDebug() << "Virtual keyboard failed to resize shared memory to" << size; |
| 47 | + return; |
| 48 | + } |
46 | 49 |
|
47 | | - char* mapShm = static_cast<char*>(mmap(nullptr, size, PROT_WRITE, MAP_SHARED, fd, 0)); |
48 | | - if(mapShm == MAP_FAILED) { |
49 | | - perror(""); |
50 | | - qDebug() << "Virtual keyboard failed to open shared memory"; |
51 | | - return; |
52 | | - } |
53 | | - strcpy(mapShm, keymapString.get()); |
| 50 | + char* mapShm = static_cast<char*>(mmap(nullptr, size, PROT_WRITE, MAP_SHARED, fd, 0)); |
| 51 | + if (mapShm == MAP_FAILED) { |
| 52 | + perror(""); |
| 53 | + qDebug() << "Virtual keyboard failed to open shared memory"; |
| 54 | + return; |
| 55 | + } |
| 56 | + strcpy(mapShm, keymapString.get()); |
54 | 57 |
|
55 | | - this->zwp_virtual_keyboard_v1::keymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fd, size); |
| 58 | + this->zwp_virtual_keyboard_v1::keymap(WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fd, size); |
56 | 59 |
|
57 | | - good = true; |
| 60 | + good = true; |
58 | 61 | } |
59 | 62 |
|
60 | | - |
61 | 63 | void VirtualKeyboardHandle::sendKey(xkb_keycode_t keycode, wl_keyboard_key_state state) { |
62 | | - if (!good) return; |
63 | | - if(keycode == XKB_KEYCODE_INVALID) return; |
| 64 | + if (!good) return; |
| 65 | + if (keycode == XKB_KEYCODE_INVALID) return; |
| 66 | + |
| 67 | + this->zwp_virtual_keyboard_v1::key(now(), keycode - 8, state); |
| 68 | +} |
64 | 69 |
|
65 | | - this->zwp_virtual_keyboard_v1::key(now(), keycode - 8, state); |
| 70 | +void VirtualKeyboardHandle::sendModifiers() { |
| 71 | + auto mods = mKeyMapState.serialiseMods(); |
| 72 | + modifiers(mods.depressed, mods.latched, mods.locked, mods.depressedLayout); |
66 | 73 | } |
67 | 74 |
|
68 | 75 | bool VirtualKeyboardHandle::getGood() const { return good; } |
|
0 commit comments