barrier/src/lib/server/Config.h

546 lines
15 KiB
C
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Bolton Software Ltd.
* Copyright (C) 2002 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 COPYING that should have accompanied this file.
*
* 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/>.
*/
#pragma once
#include "server/InputFilter.h"
#include "synergy/option_types.h"
#include "synergy/protocol_types.h"
#include "synergy/IPlatformScreen.h"
#include "net/NetworkAddress.h"
#include "base/String.h"
#include "base/XBase.h"
#include "common/stdmap.h"
#include "common/stdset.h"
2012-06-10 16:50:54 +00:00
#include <iosfwd>
2014-11-11 13:51:47 +00:00
class Config;
class ConfigReadContext;
class IEventQueue;
2012-06-10 16:50:54 +00:00
namespace std {
template <>
2014-11-11 13:51:47 +00:00
struct iterator_traits<Config> {
typedef String value_type;
2012-06-10 16:50:54 +00:00
typedef ptrdiff_t difference_type;
typedef bidirectional_iterator_tag iterator_category;
2014-11-11 13:51:47 +00:00
typedef String* pointer;
typedef String& reference;
2012-06-10 16:50:54 +00:00
};
};
//! Server configuration
/*!
This class holds server configuration information. That includes
the names of screens and their aliases, the links between them,
and network addresses.
Note that case is preserved in screen names but is ignored when
comparing names. Screen names and their aliases share a
namespace and must be unique.
*/
2014-11-11 13:51:47 +00:00
class Config {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
typedef std::map<OptionID, OptionValue> ScreenOptions;
typedef std::pair<float, float> Interval;
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
class CellEdge {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
CellEdge(EDirection side, float position);
CellEdge(EDirection side, const Interval&);
CellEdge(const String& name, EDirection side, const Interval&);
~CellEdge();
Interval getInterval() const;
void setName(const String& newName);
String getName() const;
2012-06-10 16:50:54 +00:00
EDirection getSide() const;
2014-11-11 13:51:47 +00:00
bool overlaps(const CellEdge&) const;
2012-06-10 16:50:54 +00:00
bool isInside(float x) const;
// transform position to [0,1]
float transform(float x) const;
// transform [0,1] to position
float inverseTransform(float x) const;
// compares side and start of interval
2014-11-11 13:51:47 +00:00
bool operator<(const CellEdge&) const;
2012-06-10 16:50:54 +00:00
// compares side and interval
2014-11-11 13:51:47 +00:00
bool operator==(const CellEdge&) const;
bool operator!=(const CellEdge&) const;
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
void init(const String& name, EDirection side,
const Interval&);
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
String m_name;
2012-06-10 16:50:54 +00:00
EDirection m_side;
2014-11-11 13:51:47 +00:00
Interval m_interval;
2012-06-10 16:50:54 +00:00
};
private:
2014-11-11 13:51:47 +00:00
class Name {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
Name(Config*, const String& name);
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
bool operator==(const String& name) const;
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
Config* m_config;
String m_name;
2012-06-10 16:50:54 +00:00
};
2014-11-11 13:51:47 +00:00
class Cell {
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
typedef std::map<CellEdge, CellEdge> EdgeLinks;
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
typedef EdgeLinks::const_iterator const_iterator;
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
bool add(const CellEdge& src, const CellEdge& dst);
2012-06-10 16:50:54 +00:00
void remove(EDirection side);
void remove(EDirection side, float position);
2014-11-11 13:51:47 +00:00
void remove(const Name& destinationName);
void rename(const Name& oldName, const String& newName);
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
bool hasEdge(const CellEdge&) const;
bool overlaps(const CellEdge&) const;
2012-06-10 16:50:54 +00:00
bool getLink(EDirection side, float position,
2014-11-11 13:51:47 +00:00
const CellEdge*& src, const CellEdge*& dst) const;
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
bool operator==(const Cell&) const;
bool operator!=(const Cell&) const;
2012-06-10 16:50:54 +00:00
const_iterator begin() const;
const_iterator end() const;
private:
2014-11-11 13:51:47 +00:00
EdgeLinks m_neighbors;
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
ScreenOptions m_options;
2012-06-10 16:50:54 +00:00
};
2014-11-11 13:51:47 +00:00
typedef std::map<String, Cell, synergy::string::CaselessCmp> CellMap;
typedef std::map<String, String, synergy::string::CaselessCmp> NameMap;
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
typedef Cell::const_iterator link_const_iterator;
typedef CellMap::const_iterator internal_const_iterator;
typedef NameMap::const_iterator all_const_iterator;
class const_iterator : std::iterator_traits<Config> {
2012-06-10 16:50:54 +00:00
public:
explicit const_iterator() : m_i() { }
explicit const_iterator(const internal_const_iterator& i) : m_i(i) { }
const_iterator& operator=(const const_iterator& i) {
m_i = i.m_i;
return *this;
}
2014-11-11 13:51:47 +00:00
String operator*() { return m_i->first; }
const String* operator->() { return &(m_i->first); }
2012-06-10 16:50:54 +00:00
const_iterator& operator++() { ++m_i; return *this; }
const_iterator operator++(int) { return const_iterator(m_i++); }
const_iterator& operator--() { --m_i; return *this; }
const_iterator operator--(int) { return const_iterator(m_i--); }
bool operator==(const const_iterator& i) const {
return (m_i == i.m_i);
}
bool operator!=(const const_iterator& i) const {
return (m_i != i.m_i);
}
private:
internal_const_iterator m_i;
};
2014-11-11 13:51:47 +00:00
Config(IEventQueue* events);
virtual ~Config();
2012-06-10 16:50:54 +00:00
#ifdef TEST_ENV
2014-11-11 13:51:47 +00:00
Config() : m_inputFilter(NULL) { }
#endif
2012-06-10 16:50:54 +00:00
//! @name manipulators
//@{
//! Add screen
/*!
Adds a screen, returning true iff successful. If a screen or
alias with the given name exists then it fails.
*/
2014-11-11 13:51:47 +00:00
bool addScreen(const String& name);
2012-06-10 16:50:54 +00:00
//! Rename screen
/*!
Renames a screen. All references to the name are updated.
Returns true iff successful.
*/
2014-11-11 13:51:47 +00:00
bool renameScreen(const String& oldName,
const String& newName);
2012-06-10 16:50:54 +00:00
//! Remove screen
/*!
Removes a screen. This also removes aliases for the screen and
disconnects any connections to the screen. \c name may be an
alias.
*/
2014-11-11 13:51:47 +00:00
void removeScreen(const String& name);
2012-06-10 16:50:54 +00:00
//! Remove all screens
/*!
Removes all screens, aliases, and connections.
*/
void removeAllScreens();
//! Add alias
/*!
Adds an alias for a screen name. An alias can be used
any place the canonical screen name can (except addScreen()).
Returns false if the alias name already exists or the canonical
name is unknown, otherwise returns true.
*/
2014-11-11 13:51:47 +00:00
bool addAlias(const String& canonical,
const String& alias);
2012-06-10 16:50:54 +00:00
//! Remove alias
/*!
Removes an alias for a screen name. It returns false if the
alias is unknown or a canonical name, otherwise returns true.
*/
2014-11-11 13:51:47 +00:00
bool removeAlias(const String& alias);
2012-06-10 16:50:54 +00:00
//! Remove aliases
/*!
Removes all aliases for a canonical screen name. It returns false
if the canonical name is unknown, otherwise returns true.
*/
2014-11-11 13:51:47 +00:00
bool removeAliases(const String& canonical);
2012-06-10 16:50:54 +00:00
//! Remove all aliases
/*!
This removes all aliases but not the screens.
*/
void removeAllAliases();
//! Connect screens
/*!
Establishes a one-way connection between portions of opposite edges
of two screens. Each portion is described by an interval defined
by two numbers, the start and end of the interval half-open on the
end. The numbers range from 0 to 1, inclusive, for the left/top
to the right/bottom. The user will be able to jump from the
\c srcStart to \c srcSend interval of \c srcSide of screen
\c srcName to the opposite side of screen \c dstName in the interval
\c dstStart and \c dstEnd when both screens are connected to the
server and the user isn't locked to a screen. Returns false if
\c srcName is unknown. \c srcStart must be less than or equal to
\c srcEnd and \c dstStart must be less then or equal to \c dstEnd
and all of \c srcStart, \c srcEnd, \c dstStart, or \c dstEnd must
be inside the range [0,1].
*/
2014-11-11 13:51:47 +00:00
bool connect(const String& srcName,
2012-06-10 16:50:54 +00:00
EDirection srcSide,
float srcStart, float srcEnd,
2014-11-11 13:51:47 +00:00
const String& dstName,
2012-06-10 16:50:54 +00:00
float dstStart, float dstEnd);
//! Disconnect screens
/*!
Removes all connections created by connect() on side \c srcSide.
Returns false if \c srcName is unknown.
*/
2014-11-11 13:51:47 +00:00
bool disconnect(const String& srcName,
2012-06-10 16:50:54 +00:00
EDirection srcSide);
//! Disconnect screens
/*!
Removes the connections created by connect() on side \c srcSide
covering position \c position. Returns false if \c srcName is
unknown.
*/
2014-11-11 13:51:47 +00:00
bool disconnect(const String& srcName,
2012-06-10 16:50:54 +00:00
EDirection srcSide, float position);
//! Set server address
/*!
Set the synergy listen addresses. There is no default address so
this must be called to run a server using this configuration.
*/
2014-11-11 13:51:47 +00:00
void setSynergyAddress(const NetworkAddress&);
2012-06-10 16:50:54 +00:00
//! Add a screen option
/*!
Adds an option and its value to the named screen. Replaces the
existing option's value if there is one. Returns true iff \c name
is a known screen.
*/
2014-11-11 13:51:47 +00:00
bool addOption(const String& name,
2012-06-10 16:50:54 +00:00
OptionID option, OptionValue value);
//! Remove a screen option
/*!
Removes an option and its value from the named screen. Does
nothing if the option doesn't exist on the screen. Returns true
iff \c name is a known screen.
*/
2014-11-11 13:51:47 +00:00
bool removeOption(const String& name, OptionID option);
2012-06-10 16:50:54 +00:00
//! Remove a screen options
/*!
Removes all options and values from the named screen. Returns true
iff \c name is a known screen.
*/
2014-11-11 13:51:47 +00:00
bool removeOptions(const String& name);
2012-06-10 16:50:54 +00:00
//! Get the hot key input filter
/*!
Returns the hot key input filter. Clients can modify hotkeys using
that object.
*/
2014-11-11 13:51:47 +00:00
virtual InputFilter*
getInputFilter();
2012-06-10 16:50:54 +00:00
//@}
//! @name accessors
//@{
//! Test screen name validity
/*!
Returns true iff \c name is a valid screen name.
*/
2014-11-11 13:51:47 +00:00
bool isValidScreenName(const String& name) const;
2012-06-10 16:50:54 +00:00
//! Get beginning (canonical) screen name iterator
const_iterator begin() const;
//! Get ending (canonical) screen name iterator
const_iterator end() const;
//! Get beginning screen name iterator
all_const_iterator beginAll() const;
//! Get ending screen name iterator
all_const_iterator endAll() const;
//! Test for screen name
/*!
Returns true iff \c name names a screen.
*/
2014-11-11 13:51:47 +00:00
virtual bool isScreen(const String& name) const;
2012-06-10 16:50:54 +00:00
//! Test for canonical screen name
/*!
Returns true iff \c name is the canonical name of a screen.
*/
2014-11-11 13:51:47 +00:00
bool isCanonicalName(const String& name) const;
2012-06-10 16:50:54 +00:00
//! Get canonical name
/*!
Returns the canonical name of a screen or the empty string if
the name is unknown. Returns the canonical name if one is given.
*/
2014-11-11 13:51:47 +00:00
String getCanonicalName(const String& name) const;
2012-06-10 16:50:54 +00:00
//! Get neighbor
/*!
Returns the canonical screen name of the neighbor in the given
direction (set through connect()) at position \c position. Returns
the empty string if there is no neighbor in that direction, otherwise
saves the position on the neighbor in \c positionOut if it's not
\c NULL.
*/
2014-11-11 13:51:47 +00:00
String getNeighbor(const String&, EDirection,
2012-06-10 16:50:54 +00:00
float position, float* positionOut) const;
//! Check for neighbor
/*!
Returns \c true if the screen has a neighbor anywhere along the edge
given by the direction.
*/
2014-11-11 13:51:47 +00:00
bool hasNeighbor(const String&, EDirection) const;
2012-06-10 16:50:54 +00:00
//! Check for neighbor
/*!
Returns \c true if the screen has a neighbor in the given range along
the edge given by the direction.
*/
2014-11-11 13:51:47 +00:00
bool hasNeighbor(const String&, EDirection,
2012-06-10 16:50:54 +00:00
float start, float end) const;
//! Get beginning neighbor iterator
2014-11-11 13:51:47 +00:00
link_const_iterator beginNeighbor(const String&) const;
2012-06-10 16:50:54 +00:00
//! Get ending neighbor iterator
2014-11-11 13:51:47 +00:00
link_const_iterator endNeighbor(const String&) const;
2012-06-10 16:50:54 +00:00
//! Get the server address
2014-11-11 13:51:47 +00:00
const NetworkAddress& getSynergyAddress() const;
2012-06-10 16:50:54 +00:00
//! Get the screen options
/*!
Returns all the added options for the named screen. Returns NULL
if the screen is unknown and an empty collection if there are no
options.
*/
2014-11-11 13:51:47 +00:00
const ScreenOptions* getOptions(const String& name) const;
2012-06-10 16:50:54 +00:00
//! Check for lock to screen action
/*!
Returns \c true if this configuration has a lock to screen action.
This is for backwards compatible support of ScrollLock locking.
*/
bool hasLockToScreenAction() const;
//! Compare configurations
2014-11-11 13:51:47 +00:00
bool operator==(const Config&) const;
2012-06-10 16:50:54 +00:00
//! Compare configurations
2014-11-11 13:51:47 +00:00
bool operator!=(const Config&) const;
2012-06-10 16:50:54 +00:00
//! Read configuration
/*!
Reads a configuration from a context. Throws XConfigRead on error
and context is unchanged.
*/
2014-11-11 13:51:47 +00:00
void read(ConfigReadContext& context);
2012-06-10 16:50:54 +00:00
//! Read configuration
/*!
Reads a configuration from a stream. Throws XConfigRead on error.
*/
2014-11-11 13:51:47 +00:00
friend std::istream& operator>>(std::istream&, Config&);
2012-06-10 16:50:54 +00:00
//! Write configuration
/*!
Writes a configuration to a stream.
*/
2014-11-11 13:51:47 +00:00
friend std::ostream& operator<<(std::ostream&, const Config&);
2012-06-10 16:50:54 +00:00
//! Get direction name
/*!
Returns the name of a direction (for debugging).
*/
static const char* dirName(EDirection);
//! Get interval as string
/*!
Returns an interval as a parseable string.
*/
2014-11-11 13:51:47 +00:00
static String formatInterval(const Interval&);
2012-06-10 16:50:54 +00:00
//@}
private:
2014-11-11 13:51:47 +00:00
void readSection(ConfigReadContext&);
void readSectionOptions(ConfigReadContext&);
void readSectionScreens(ConfigReadContext&);
void readSectionLinks(ConfigReadContext&);
void readSectionAliases(ConfigReadContext&);
InputFilter::Condition*
parseCondition(ConfigReadContext&,
const String& condition,
const std::vector<String>& args);
void parseAction(ConfigReadContext&,
const String& action,
const std::vector<String>& args,
InputFilter::Rule&, bool activate);
void parseScreens(ConfigReadContext&, const String&,
std::set<String>& screens) const;
2012-06-10 16:50:54 +00:00
static const char* getOptionName(OptionID);
2014-11-11 13:51:47 +00:00
static String getOptionValue(OptionID, OptionValue);
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
CellMap m_map;
NameMap m_nameToCanonicalName;
NetworkAddress m_synergyAddress;
ScreenOptions m_globalOptions;
InputFilter m_inputFilter;
2012-06-10 16:50:54 +00:00
bool m_hasLockToScreenAction;
IEventQueue* m_events;
2012-06-10 16:50:54 +00:00
};
//! Configuration read context
/*!
Maintains a context when reading a configuration from a stream.
*/
2014-11-11 13:51:47 +00:00
class ConfigReadContext {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
typedef std::vector<String> ArgList;
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
ConfigReadContext(std::istream&, SInt32 firstLine = 1);
~ConfigReadContext();
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
bool readLine(String&);
2012-06-10 16:50:54 +00:00
UInt32 getLineNumber() const;
bool operator!() const;
2014-11-11 13:51:47 +00:00
OptionValue parseBoolean(const String&) const;
OptionValue parseInt(const String&) const;
OptionValue parseModifierKey(const String&) const;
OptionValue parseCorner(const String&) const;
OptionValue parseCorners(const String&) const;
Config::Interval
2012-06-10 16:50:54 +00:00
parseInterval(const ArgList& args) const;
void parseNameWithArgs(
2014-11-11 13:51:47 +00:00
const String& type, const String& line,
const String& delim, String::size_type& index,
String& name, ArgList& args) const;
IPlatformScreen::KeyInfo*
parseKeystroke(const String& keystroke) const;
IPlatformScreen::KeyInfo*
parseKeystroke(const String& keystroke,
const std::set<String>& screens) const;
IPlatformScreen::ButtonInfo*
parseMouse(const String& mouse) const;
KeyModifierMask parseModifier(const String& modifiers) const;
2014-01-24 17:53:03 +00:00
std::istream& getStream() const { return m_stream; };
2012-06-10 16:50:54 +00:00
private:
// not implemented
2014-11-11 13:51:47 +00:00
ConfigReadContext& operator=(const ConfigReadContext&);
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
static String concatArgs(const ArgList& args);
2012-06-10 16:50:54 +00:00
private:
std::istream& m_stream;
SInt32 m_line;
};
//! Configuration stream read exception
/*!
Thrown when a configuration stream cannot be parsed.
*/
class XConfigRead : public XBase {
public:
2014-11-11 13:51:47 +00:00
XConfigRead(const ConfigReadContext& context, const String&);
XConfigRead(const ConfigReadContext& context,
const char* errorFmt, const String& arg);
virtual ~XConfigRead() _NOEXCEPT;
2012-06-10 16:50:54 +00:00
protected:
// XBase overrides
2014-11-11 13:51:47 +00:00
virtual String getWhat() const throw();
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
String m_error;
2012-06-10 16:50:54 +00:00
};