• Main Page
  • Classes
  • Files
  • File List

IKeyState.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "IKeyState.h"
00020 #include "CEventQueue.h"
00021 #include <cstring>
00022 #include <cstdlib>
00023 
00024 //
00025 // IKeyState
00026 //
00027 
00028 CEvent::Type        IKeyState::s_keyDownEvent   = CEvent::kUnknown;
00029 CEvent::Type        IKeyState::s_keyUpEvent     = CEvent::kUnknown;
00030 CEvent::Type        IKeyState::s_keyRepeatEvent = CEvent::kUnknown;
00031 
00032 IKeyState::IKeyState() :
00033     m_eventQueue(*EVENTQUEUE)
00034 {
00035 }
00036 
00037 IKeyState::IKeyState(IEventQueue& eventQueue) :
00038     m_eventQueue(eventQueue)
00039 {
00040 }
00041 
00042 CEvent::Type
00043 IKeyState::getKeyDownEvent(IEventQueue& eventQueue)
00044 {
00045     return eventQueue.registerTypeOnce(s_keyDownEvent,
00046                             "IKeyState::keyDown");
00047 }
00048 
00049 CEvent::Type
00050 IKeyState::getKeyUpEvent(IEventQueue& eventQueue)
00051 {
00052     return eventQueue.registerTypeOnce(s_keyUpEvent,
00053                             "IKeyState::keyUp");
00054 }
00055 
00056 CEvent::Type
00057 IKeyState::getKeyRepeatEvent(IEventQueue& eventQueue)
00058 {
00059     return eventQueue.registerTypeOnce(s_keyRepeatEvent,
00060                             "IKeyState::keyRepeat");
00061 }
00062 
00063 //
00064 // IKeyState::CKeyInfo
00065 //
00066 
00067 IKeyState::CKeyInfo*
00068 IKeyState::CKeyInfo::alloc(KeyID id,
00069                 KeyModifierMask mask, KeyButton button, SInt32 count)
00070 {
00071     CKeyInfo* info           = (CKeyInfo*)malloc(sizeof(CKeyInfo));
00072     info->m_key              = id;
00073     info->m_mask             = mask;
00074     info->m_button           = button;
00075     info->m_count            = count;
00076     info->m_screens          = NULL;
00077     info->m_screensBuffer[0] = '\0';
00078     return info;
00079 }
00080 
00081 IKeyState::CKeyInfo*
00082 IKeyState::CKeyInfo::alloc(KeyID id,
00083                 KeyModifierMask mask, KeyButton button, SInt32 count,
00084                 const std::set<CString>& destinations)
00085 {
00086     CString screens = join(destinations);
00087 
00088     // build structure
00089     CKeyInfo* info  = (CKeyInfo*)malloc(sizeof(CKeyInfo) + screens.size());
00090     info->m_key     = id;
00091     info->m_mask    = mask;
00092     info->m_button  = button;
00093     info->m_count   = count;
00094     info->m_screens = info->m_screensBuffer;
00095     strcpy(info->m_screensBuffer, screens.c_str());
00096     return info;
00097 }
00098 
00099 IKeyState::CKeyInfo*
00100 IKeyState::CKeyInfo::alloc(const CKeyInfo& x)
00101 {
00102     CKeyInfo* info  = (CKeyInfo*)malloc(sizeof(CKeyInfo) +
00103                                         strlen(x.m_screensBuffer));
00104     info->m_key     = x.m_key;
00105     info->m_mask    = x.m_mask;
00106     info->m_button  = x.m_button;
00107     info->m_count   = x.m_count;
00108     info->m_screens = x.m_screens ? info->m_screensBuffer : NULL;
00109     strcpy(info->m_screensBuffer, x.m_screensBuffer);
00110     return info;
00111 }
00112 
00113 bool
00114 IKeyState::CKeyInfo::isDefault(const char* screens)
00115 {
00116     return (screens == NULL || screens[0] == '\0');
00117 }
00118 
00119 bool
00120 IKeyState::CKeyInfo::contains(const char* screens, const CString& name)
00121 {
00122     // special cases
00123     if (isDefault(screens)) {
00124         return false;
00125     }
00126     if (screens[0] == '*') {
00127         return true;
00128     }
00129 
00130     // search
00131     CString match;
00132     match.reserve(name.size() + 2);
00133     match += ":";
00134     match += name;
00135     match += ":";
00136     return (strstr(screens, match.c_str()) != NULL);
00137 }
00138 
00139 bool
00140 IKeyState::CKeyInfo::equal(const CKeyInfo* a, const CKeyInfo* b)
00141 {
00142     return (a->m_key    == b->m_key &&
00143             a->m_mask   == b->m_mask &&
00144             a->m_button == b->m_button &&
00145             a->m_count  == b->m_count &&
00146             strcmp(a->m_screensBuffer, b->m_screensBuffer) == 0);
00147 }
00148 
00149 CString
00150 IKeyState::CKeyInfo::join(const std::set<CString>& destinations)
00151 {
00152     // collect destinations into a string.  names are surrounded by ':'
00153     // which makes searching easy.  the string is empty if there are no
00154     // destinations and "*" means all destinations.
00155     CString screens;
00156     for (std::set<CString>::const_iterator i = destinations.begin();
00157                                 i != destinations.end(); ++i) {
00158         if (*i == "*") {
00159             screens = "*";
00160             break;
00161         }
00162         else {
00163             if (screens.empty()) {
00164                 screens = ":";
00165             }
00166             screens += *i;
00167             screens += ":";
00168         }
00169     }
00170     return screens;
00171 }
00172 
00173 void
00174 IKeyState::CKeyInfo::split(const char* screens, std::set<CString>& dst)
00175 {
00176     dst.clear();
00177     if (isDefault(screens)) {
00178         return;
00179     }
00180     if (screens[0] == '*') {
00181         dst.insert("*");
00182         return;
00183     }
00184 
00185     const char* i = screens + 1;
00186     while (*i != '\0') {
00187         const char* j = strchr(i, ':');
00188         dst.insert(CString(i, j - i));
00189         i = j + 1;
00190     }
00191 }

Generated on Tue May 21 2013 00:00:06 for Synergy by  doxygen 1.7.1