barrier/src/lib/synergy/IKeyState.cpp

162 lines
3.7 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Synergy Si Ltd.
* Copyright (C) 2004 Chris Schoeneman
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
* found in the file LICENSE that should have accompanied this file.
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/>.
*/
#include "synergy/IKeyState.h"
#include "base/EventQueue.h"
2012-06-10 16:50:54 +00:00
#include <cstring>
#include <cstdlib>
//
// IKeyState
//
2014-02-25 15:03:43 +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
//
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo*
IKeyState::KeyInfo::alloc(KeyID id,
2012-06-10 16:50:54 +00:00
KeyModifierMask mask, KeyButton button, SInt32 count)
{
2014-11-11 13:51:47 +00:00
KeyInfo* info = (KeyInfo*)malloc(sizeof(KeyInfo));
2012-06-10 16:50:54 +00:00
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;
}
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo*
IKeyState::KeyInfo::alloc(KeyID id,
2012-06-10 16:50:54 +00:00
KeyModifierMask mask, KeyButton button, SInt32 count,
2014-11-11 13:51:47 +00:00
const std::set<String>& destinations)
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
String screens = join(destinations);
2012-06-10 16:50:54 +00:00
// build structure
2014-11-11 13:51:47 +00:00
KeyInfo* info = (KeyInfo*)malloc(sizeof(KeyInfo) + screens.size());
2012-06-10 16:50:54 +00:00
info->m_key = id;
info->m_mask = mask;
info->m_button = button;
info->m_count = count;
info->m_screens = info->m_screensBuffer;
strcpy(info->m_screensBuffer, screens.c_str());
return info;
}
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo*
IKeyState::KeyInfo::alloc(const KeyInfo& x)
2012-06-10 16:50:54 +00:00
{
2014-11-11 13:51:47 +00:00
KeyInfo* info = (KeyInfo*)malloc(sizeof(KeyInfo) +
2012-06-10 16:50:54 +00:00
strlen(x.m_screensBuffer));
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;
strcpy(info->m_screensBuffer, x.m_screensBuffer);
return info;
}
bool
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo::isDefault(const char* screens)
2012-06-10 16:50:54 +00:00
{
return (screens == NULL || screens[0] == '\0');
}
bool
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo::contains(const char* screens, const String& name)
2012-06-10 16:50:54 +00:00
{
// special cases
if (isDefault(screens)) {
return false;
}
if (screens[0] == '*') {
return true;
}
// search
2014-11-11 13:51:47 +00:00
String match;
2012-06-10 16:50:54 +00:00
match.reserve(name.size() + 2);
match += ":";
match += name;
match += ":";
return (strstr(screens, match.c_str()) != NULL);
}
bool
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo::equal(const KeyInfo* a, const KeyInfo* b)
2012-06-10 16:50:54 +00:00
{
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);
}
2014-11-11 13:51:47 +00:00
String
IKeyState::KeyInfo::join(const std::set<String>& destinations)
2012-06-10 16:50:54 +00:00
{
// 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.
2014-11-11 13:51:47 +00:00
String screens;
for (std::set<String>::const_iterator i = destinations.begin();
2012-06-10 16:50:54 +00:00
i != destinations.end(); ++i) {
if (*i == "*") {
screens = "*";
break;
}
else {
if (screens.empty()) {
screens = ":";
}
screens += *i;
screens += ":";
}
}
return screens;
}
void
2014-11-11 13:51:47 +00:00
IKeyState::KeyInfo::split(const char* screens, std::set<String>& dst)
2012-06-10 16:50:54 +00:00
{
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, ':');
2014-11-11 13:51:47 +00:00
dst.insert(String(i, j - i));
2012-06-10 16:50:54 +00:00
i = j + 1;
}
}