2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2004 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "synergy/IKeyState.h"
|
|
|
|
|
|
2022-12-08 09:16:44 +00:00
|
|
|
#include <cstdint>
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <cstdlib>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <cstring>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// IKeyState
|
|
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IKeyState::IKeyState(IEventQueue *events) {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// IKeyState::KeyInfo
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(KeyID id, KeyModifierMask mask,
|
|
|
|
|
KeyButton button, SInt32 count) {
|
|
|
|
|
KeyInfo *info = (KeyInfo *)malloc(sizeof(KeyInfo));
|
|
|
|
|
info->m_key = id;
|
|
|
|
|
info->m_mask = mask;
|
|
|
|
|
info->m_button = button;
|
|
|
|
|
info->m_count = count;
|
|
|
|
|
info->m_screens = NULL;
|
|
|
|
|
info->m_screensBuffer[0] = '\0';
|
|
|
|
|
return info;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IKeyState::KeyInfo *
|
|
|
|
|
IKeyState::KeyInfo::alloc(KeyID id, KeyModifierMask mask, KeyButton button,
|
|
|
|
|
SInt32 count, const std::set<String> &destinations) {
|
|
|
|
|
String screens = join(destinations);
|
2024-07-16 08:30:35 +00:00
|
|
|
const char *buffer = screens.c_str();
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// build structure
|
|
|
|
|
KeyInfo *info = (KeyInfo *)malloc(sizeof(KeyInfo) + screens.size());
|
|
|
|
|
info->m_key = id;
|
|
|
|
|
info->m_mask = mask;
|
|
|
|
|
info->m_button = button;
|
|
|
|
|
info->m_count = count;
|
|
|
|
|
info->m_screens = info->m_screensBuffer;
|
2024-07-16 08:30:35 +00:00
|
|
|
std::copy(buffer, buffer + screens.size() + 1, info->m_screensBuffer);
|
2024-07-02 19:07:06 +00:00
|
|
|
return info;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(const KeyInfo &x) {
|
|
|
|
|
auto bufferLen = strnlen(x.m_screensBuffer, SIZE_MAX);
|
|
|
|
|
auto info = (KeyInfo *)malloc(sizeof(KeyInfo) + bufferLen);
|
|
|
|
|
info->m_key = x.m_key;
|
|
|
|
|
info->m_mask = x.m_mask;
|
|
|
|
|
info->m_button = x.m_button;
|
|
|
|
|
info->m_count = x.m_count;
|
|
|
|
|
info->m_screens = x.m_screens ? info->m_screensBuffer : NULL;
|
|
|
|
|
memcpy(info->m_screensBuffer, x.m_screensBuffer, bufferLen + 1);
|
|
|
|
|
return info;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool IKeyState::KeyInfo::isDefault(const char *screens) {
|
|
|
|
|
return (screens == NULL || screens[0] == '\0');
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool IKeyState::KeyInfo::contains(const char *screens, const String &name) {
|
|
|
|
|
// special cases
|
|
|
|
|
if (isDefault(screens)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (screens[0] == '*') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// search
|
|
|
|
|
String match;
|
|
|
|
|
match.reserve(name.size() + 2);
|
|
|
|
|
match += ":";
|
|
|
|
|
match += name;
|
|
|
|
|
match += ":";
|
|
|
|
|
return (strstr(screens, match.c_str()) != NULL);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool IKeyState::KeyInfo::equal(const KeyInfo *a, const KeyInfo *b) {
|
|
|
|
|
return (a->m_key == b->m_key && a->m_mask == b->m_mask &&
|
|
|
|
|
a->m_button == b->m_button && a->m_count == b->m_count &&
|
|
|
|
|
strcmp(a->m_screensBuffer, b->m_screensBuffer) == 0);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
String IKeyState::KeyInfo::join(const std::set<String> &destinations) {
|
|
|
|
|
// collect destinations into a string. names are surrounded by ':'
|
|
|
|
|
// which makes searching easy. the string is empty if there are no
|
|
|
|
|
// destinations and "*" means all destinations.
|
|
|
|
|
String screens;
|
2024-07-16 08:30:35 +00:00
|
|
|
for (auto i = destinations.begin(); i != destinations.end(); ++i) {
|
2024-07-02 19:07:06 +00:00
|
|
|
if (*i == "*") {
|
|
|
|
|
screens = "*";
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
if (screens.empty()) {
|
|
|
|
|
screens = ":";
|
|
|
|
|
}
|
|
|
|
|
screens += *i;
|
|
|
|
|
screens += ":";
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
return screens;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void IKeyState::KeyInfo::split(const char *screens, std::set<String> &dst) {
|
|
|
|
|
dst.clear();
|
|
|
|
|
if (isDefault(screens)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (screens[0] == '*') {
|
|
|
|
|
dst.insert("*");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *i = screens + 1;
|
|
|
|
|
while (*i != '\0') {
|
|
|
|
|
const char *j = strchr(i, ':');
|
|
|
|
|
dst.insert(String(i, j - i));
|
|
|
|
|
i = j + 1;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|