File indexing completed on 2024-05-26 05:38:22
0001 /* 0002 SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org> 0003 SPDX-FileCopyrightText: 2024 Fushan Wen <qydwhotmail@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "keystate.h" 0009 0010 #include <KModifierKeyInfo> 0011 0012 #include "keyboardindicator_debug.h" 0013 0014 namespace 0015 { 0016 [[nodiscard]] std::shared_ptr<KModifierKeyInfo> keyInfo() 0017 { 0018 static std::weak_ptr<KModifierKeyInfo> s_backend; 0019 if (s_backend.expired()) { 0020 std::shared_ptr<KModifierKeyInfo> ptr{new KModifierKeyInfo}; 0021 s_backend = ptr; 0022 return ptr; 0023 } 0024 return s_backend.lock(); 0025 } 0026 } 0027 0028 KeyState::KeyState(QObject *parent) 0029 : QObject(parent) 0030 { 0031 } 0032 0033 KeyState::~KeyState() 0034 { 0035 } 0036 0037 Qt::Key KeyState::key() const 0038 { 0039 return m_key; 0040 } 0041 0042 void KeyState::setKey(Qt::Key newKey) 0043 { 0044 if (m_key == newKey) [[unlikely]] { 0045 return; 0046 } 0047 0048 m_key = newKey; 0049 Q_EMIT keyChanged(); 0050 0051 if (m_keyInfo) { 0052 if (!m_keyInfo->knowsKey(newKey)) [[unlikely]] { 0053 // Ignore unknown keys 0054 qCWarning(KEYBOARDINDICATOR_DEBUG) << "Unknown key" << newKey; 0055 m_keyInfo->disconnect(this); 0056 m_keyInfo.reset(); 0057 return; 0058 } 0059 } else { 0060 m_keyInfo = keyInfo(); 0061 if (!m_keyInfo->knowsKey(newKey)) [[unlikely]] { 0062 qCWarning(KEYBOARDINDICATOR_DEBUG) << "Unknown key" << newKey; 0063 m_keyInfo.reset(); 0064 return; 0065 } 0066 } 0067 0068 m_pressed = m_keyInfo->isKeyPressed(m_key); 0069 m_latched = m_keyInfo->isKeyLatched(m_key); 0070 m_locked = m_keyInfo->isKeyLocked(m_key); 0071 0072 connect(m_keyInfo.get(), &KModifierKeyInfo::keyPressed, this, &KeyState::onKeyPressed); 0073 connect(m_keyInfo.get(), &KModifierKeyInfo::keyLatched, this, &KeyState::onKeyLatched); 0074 connect(m_keyInfo.get(), &KModifierKeyInfo::keyLocked, this, &KeyState::onKeyLocked); 0075 connect(m_keyInfo.get(), &KModifierKeyInfo::keyAdded, this, &KeyState::onKeyAdded); 0076 connect(m_keyInfo.get(), &KModifierKeyInfo::keyRemoved, this, &KeyState::onKeyRemoved); 0077 } 0078 0079 bool KeyState::pressed() const 0080 { 0081 return m_pressed; 0082 } 0083 0084 bool KeyState::latched() const 0085 { 0086 return m_latched; 0087 } 0088 0089 bool KeyState::locked() const 0090 { 0091 return m_locked; 0092 } 0093 0094 void KeyState::lock(bool lock) 0095 { 0096 if (m_keyInfo) [[likely]] { 0097 m_keyInfo->setKeyLocked(m_key, lock); 0098 } 0099 } 0100 0101 void KeyState::latch(bool latch) 0102 { 0103 if (m_keyInfo) [[likely]] { 0104 m_keyInfo->setKeyLatched(m_key, latch); 0105 } 0106 } 0107 0108 void KeyState::onKeyPressed(Qt::Key key, bool state) 0109 { 0110 if (key == m_key) { 0111 m_pressed = state; 0112 } 0113 } 0114 0115 void KeyState::onKeyLatched(Qt::Key key, bool state) 0116 { 0117 if (key == m_key) { 0118 m_latched = state; 0119 } 0120 } 0121 0122 void KeyState::onKeyLocked(Qt::Key key, bool state) 0123 { 0124 if (key == m_key) { 0125 m_locked = state; 0126 } 0127 } 0128 0129 void KeyState::onKeyAdded(Qt::Key key) 0130 { 0131 if (key == m_key) { 0132 m_pressed = m_keyInfo->isKeyPressed(m_key); 0133 m_latched = m_keyInfo->isKeyLatched(m_key); 0134 m_locked = m_keyInfo->isKeyLocked(m_key); 0135 } 0136 } 0137 0138 void KeyState::onKeyRemoved(Qt::Key key) 0139 { 0140 if (key == m_key) { 0141 m_pressed = false; 0142 m_latched = false; 0143 m_locked = false; 0144 } 0145 } 0146 0147 MouseButtonState::MouseButtonState(QObject *parent) 0148 : QObject(parent) 0149 { 0150 } 0151 0152 MouseButtonState::~MouseButtonState() 0153 { 0154 } 0155 0156 Qt::MouseButton MouseButtonState::button() const 0157 { 0158 return m_button; 0159 } 0160 0161 void MouseButtonState::setButton(Qt::MouseButton newButton) 0162 { 0163 if (m_button == newButton) [[unlikely]] { 0164 return; 0165 } 0166 0167 m_button = newButton; 0168 Q_EMIT buttonChanged(); 0169 0170 if (m_button <= Qt::NoButton || m_button > Qt::MaxMouseButton) { 0171 if (m_keyInfo) { 0172 qCWarning(KEYBOARDINDICATOR_DEBUG) << "Unknown mouse button" << newButton; 0173 m_keyInfo->disconnect(this); 0174 m_keyInfo.reset(); 0175 } 0176 return; 0177 } 0178 0179 if (!m_keyInfo) { 0180 m_keyInfo = keyInfo(); 0181 } 0182 0183 m_pressed = m_keyInfo->isButtonPressed(m_button); 0184 0185 connect(m_keyInfo.get(), &KModifierKeyInfo::buttonPressed, this, &MouseButtonState::onMouseButtonPressed); 0186 } 0187 0188 bool MouseButtonState::pressed() const 0189 { 0190 return m_pressed; 0191 } 0192 0193 void MouseButtonState::onMouseButtonPressed(Qt::MouseButton mouseButton, bool state) 0194 { 0195 if (mouseButton == m_button) { 0196 m_pressed = state; 0197 } 0198 } 0199 0200 #include "moc_keystate.cpp"