CConfig.h

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #ifndef CCONFIG_H
00016 #define CCONFIG_H
00017 
00018 #include "OptionTypes.h"
00019 #include "ProtocolTypes.h"
00020 #include "CNetworkAddress.h"
00021 #include "CStringUtil.h"
00022 #include "CInputFilter.h"
00023 #include "XBase.h"
00024 #include "stdmap.h"
00025 #include "stdset.h"
00026 #include "IPlatformScreen.h"
00027 #include <iosfwd>
00028 
00029 class CConfig;
00030 class CConfigReadContext;
00031 
00032 namespace std {
00033 template <>
00034 struct iterator_traits<CConfig> {
00035     typedef CString                     value_type;
00036     typedef ptrdiff_t                   difference_type;
00037     typedef bidirectional_iterator_tag  iterator_category;
00038     typedef CString*                    pointer;
00039     typedef CString&                    reference;
00040 };
00041 };
00042 
00044 
00053 class CConfig {
00054 public:
00055     typedef std::map<OptionID, OptionValue> CScreenOptions;
00056     typedef std::pair<float, float> CInterval;
00057 
00058     class CCellEdge {
00059     public:
00060         CCellEdge(EDirection side, float position);
00061         CCellEdge(EDirection side, const CInterval&);
00062         CCellEdge(const CString& name, EDirection side, const CInterval&);
00063         ~CCellEdge();
00064 
00065         CInterval       getInterval() const;
00066         void            setName(const CString& newName);
00067         CString         getName() const;
00068         EDirection      getSide() const;
00069         bool            overlaps(const CCellEdge&) const;
00070         bool            isInside(float x) const;
00071 
00072         // transform position to [0,1]
00073         float           transform(float x) const;
00074 
00075         // transform [0,1] to position
00076         float           inverseTransform(float x) const;
00077 
00078         // compares side and start of interval
00079         bool            operator<(const CCellEdge&) const;
00080 
00081         // compares side and interval
00082         bool            operator==(const CCellEdge&) const;
00083         bool            operator!=(const CCellEdge&) const;
00084 
00085     private:
00086         void            init(const CString& name, EDirection side,
00087                             const CInterval&);
00088 
00089     private:
00090         CString         m_name;
00091         EDirection      m_side;
00092         CInterval       m_interval;
00093     };
00094 
00095 private:
00096     class CName {
00097     public:
00098         CName(CConfig*, const CString& name);
00099 
00100         bool            operator==(const CString& name) const;
00101 
00102     private:
00103         CConfig*        m_config;
00104         CString         m_name;
00105     };
00106 
00107     class CCell {
00108     private:
00109         typedef std::map<CCellEdge, CCellEdge> CEdgeLinks;
00110 
00111     public:
00112         typedef CEdgeLinks::const_iterator const_iterator;
00113 
00114         bool            add(const CCellEdge& src, const CCellEdge& dst);
00115         void            remove(EDirection side);
00116         void            remove(EDirection side, float position);
00117         void            remove(const CName& destinationName);
00118         void            rename(const CName& oldName, const CString& newName);
00119 
00120         bool            hasEdge(const CCellEdge&) const;
00121         bool            overlaps(const CCellEdge&) const;
00122 
00123         bool            getLink(EDirection side, float position,
00124                             const CCellEdge*& src, const CCellEdge*& dst) const;
00125 
00126         bool            operator==(const CCell&) const;
00127         bool            operator!=(const CCell&) const;
00128 
00129         const_iterator  begin() const;
00130         const_iterator  end() const;
00131 
00132     private:
00133         CEdgeLinks      m_neighbors;
00134 
00135     public:
00136         CScreenOptions  m_options;
00137     };
00138     typedef std::map<CString, CCell, CStringUtil::CaselessCmp> CCellMap;
00139     typedef std::map<CString, CString, CStringUtil::CaselessCmp> CNameMap;
00140 
00141 public:
00142     typedef CCell::const_iterator link_const_iterator;
00143     typedef CCellMap::const_iterator internal_const_iterator;
00144     typedef CNameMap::const_iterator all_const_iterator;
00145     class const_iterator : std::iterator_traits<CConfig> {
00146     public:
00147         explicit const_iterator() : m_i() { }
00148         explicit const_iterator(const internal_const_iterator& i) : m_i(i) { }
00149 
00150         const_iterator& operator=(const const_iterator& i) {
00151             m_i = i.m_i;
00152             return *this;
00153         }
00154         CString         operator*() { return m_i->first; }
00155         const CString*  operator->() { return &(m_i->first); }
00156         const_iterator& operator++() { ++m_i;  return *this; }
00157         const_iterator  operator++(int) { return const_iterator(m_i++); }
00158         const_iterator& operator--() { --m_i;  return *this; }
00159         const_iterator  operator--(int) { return const_iterator(m_i--); }
00160         bool            operator==(const const_iterator& i) const {
00161             return (m_i == i.m_i);
00162         }
00163         bool            operator!=(const const_iterator& i) const {
00164             return (m_i != i.m_i);
00165         }
00166 
00167     private:
00168         internal_const_iterator m_i;
00169     };
00170 
00171     CConfig();
00172     virtual ~CConfig();
00173 
00175 
00176 
00178 
00182     bool                addScreen(const CString& name);
00183 
00185 
00189     bool                renameScreen(const CString& oldName,
00190                             const CString& newName);
00191 
00193 
00198     void                removeScreen(const CString& name);
00199 
00201 
00204     void                removeAllScreens();
00205 
00207 
00213     bool                addAlias(const CString& canonical,
00214                             const CString& alias);
00215 
00217 
00221     bool                removeAlias(const CString& alias);
00222 
00224 
00228     bool                removeAliases(const CString& canonical);
00229 
00231 
00234     void                removeAllAliases();
00235 
00237 
00252     bool                connect(const CString& srcName,
00253                             EDirection srcSide,
00254                             float srcStart, float srcEnd,
00255                             const CString& dstName,
00256                             float dstStart, float dstEnd);
00257 
00259 
00263     bool                disconnect(const CString& srcName,
00264                             EDirection srcSide);
00265 
00267 
00272     bool                disconnect(const CString& srcName,
00273                             EDirection srcSide, float position);
00274 
00276 
00280     void                setSynergyAddress(const CNetworkAddress&);
00281 
00283 
00288     bool                addOption(const CString& name,
00289                             OptionID option, OptionValue value);
00290 
00292 
00297     bool                removeOption(const CString& name, OptionID option);
00298 
00300 
00304     bool                removeOptions(const CString& name);
00305 
00307 
00311     CInputFilter*       getInputFilter();
00312 
00314 
00315 
00316 
00318 
00321     bool                isValidScreenName(const CString& name) const;
00322 
00324     const_iterator      begin() const;
00326     const_iterator      end() const;
00327 
00329     all_const_iterator  beginAll() const;
00331     all_const_iterator  endAll() const;
00332 
00334 
00337     bool                isScreen(const CString& name) const;
00338 
00340 
00343     bool                isCanonicalName(const CString& name) const;
00344 
00346 
00350     CString             getCanonicalName(const CString& name) const;
00351 
00353 
00360     CString             getNeighbor(const CString&, EDirection,
00361                             float position, float* positionOut) const;
00362 
00364 
00368     bool                hasNeighbor(const CString&, EDirection) const;
00369 
00371 
00375     bool                hasNeighbor(const CString&, EDirection,
00376                             float start, float end) const;
00377 
00379     link_const_iterator beginNeighbor(const CString&) const;
00381     link_const_iterator endNeighbor(const CString&) const;
00382 
00384     const CNetworkAddress&  getSynergyAddress() const;
00385 
00387 
00392     const CScreenOptions* getOptions(const CString& name) const;
00393 
00395 
00399     bool                    hasLockToScreenAction() const;
00400 
00402     bool                operator==(const CConfig&) const;
00404     bool                operator!=(const CConfig&) const;
00405 
00407 
00411     void                    read(CConfigReadContext& context);
00412 
00414 
00417     friend std::istream&    operator>>(std::istream&, CConfig&);
00418 
00420 
00423     friend std::ostream&    operator<<(std::ostream&, const CConfig&);
00424 
00426 
00429     static const char*  dirName(EDirection);
00430 
00432 
00435     static CString      formatInterval(const CInterval&);
00436 
00438 
00439 private:
00440     void                readSection(CConfigReadContext&);
00441     void                readSectionOptions(CConfigReadContext&);
00442     void                readSectionScreens(CConfigReadContext&);
00443     void                readSectionLinks(CConfigReadContext&);
00444     void                readSectionAliases(CConfigReadContext&);
00445 
00446     CInputFilter::CCondition*
00447                         parseCondition(CConfigReadContext&,
00448                             const CString& condition,
00449                             const std::vector<CString>& args);
00450     void                parseAction(CConfigReadContext&,
00451                             const CString& action,
00452                             const std::vector<CString>& args,
00453                             CInputFilter::CRule&, bool activate);
00454 
00455     void                parseScreens(CConfigReadContext&, const CString&,
00456                             std::set<CString>& screens) const;
00457     static const char*  getOptionName(OptionID);
00458     static CString      getOptionValue(OptionID, OptionValue);
00459 
00460 private:
00461     CCellMap            m_map;
00462     CNameMap            m_nameToCanonicalName;
00463     CNetworkAddress     m_synergyAddress;
00464     CScreenOptions      m_globalOptions;
00465     CInputFilter        m_inputFilter;
00466     bool                m_hasLockToScreenAction;
00467 };
00468 
00470 
00473 class CConfigReadContext {
00474 public:
00475     typedef std::vector<CString> ArgList;
00476 
00477     CConfigReadContext(std::istream&, SInt32 firstLine = 1);
00478     ~CConfigReadContext();
00479 
00480     bool            readLine(CString&);
00481     UInt32          getLineNumber() const;
00482 
00483     operator void*() const;
00484     bool            operator!() const;
00485 
00486     OptionValue     parseBoolean(const CString&) const;
00487     OptionValue     parseInt(const CString&) const;
00488     OptionValue     parseModifierKey(const CString&) const;
00489     OptionValue     parseCorner(const CString&) const;
00490     OptionValue     parseCorners(const CString&) const;
00491     CConfig::CInterval
00492                     parseInterval(const ArgList& args) const;
00493     void            parseNameWithArgs(
00494                         const CString& type, const CString& line,
00495                         const CString& delim, CString::size_type& index,
00496                         CString& name, ArgList& args) const;
00497     IPlatformScreen::CKeyInfo*
00498                     parseKeystroke(const CString& keystroke) const;
00499     IPlatformScreen::CKeyInfo*
00500                     parseKeystroke(const CString& keystroke,
00501                         const std::set<CString>& screens) const;
00502     IPlatformScreen::CButtonInfo*
00503                     parseMouse(const CString& mouse) const;
00504     KeyModifierMask parseModifier(const CString& modifiers) const;
00505 
00506 private:
00507     // not implemented
00508     CConfigReadContext& operator=(const CConfigReadContext&);
00509 
00510     static CString  concatArgs(const ArgList& args);
00511 
00512 private:
00513     std::istream&   m_stream;
00514     SInt32          m_line;
00515 };
00516 
00518 
00521 class XConfigRead : public XBase {
00522 public:
00523     XConfigRead(const CConfigReadContext& context, const CString&);
00524     XConfigRead(const CConfigReadContext& context,
00525                             const char* errorFmt, const CString& arg);
00526     ~XConfigRead();
00527 
00528 protected:
00529     // XBase overrides
00530     virtual CString     getWhat() const throw();
00531 
00532 private:
00533     CString             m_error;
00534 };
00535 
00536 #endif

Generated on Fri Nov 6 00:18:44 2009 for synergy-plus by  doxygen 1.4.7