32 m_channel(channel_name,true) ,
39 return m_channel.name() ;
44 return m_channel.open() ;
49 return m_channel.fd() ;
54 return m_channel.receive( buffer , type_p ) ;
57 int Gv::ViewerEvent::parse(
const std::string & event ,
const std::string & key ,
int default_ )
59 typedef std::string::size_type pos_t ;
60 const pos_t npos = std::string::npos ;
62 pos_t pos1 =
event.find(
"'"+key+
"':" ) ;
63 if( pos1 == npos )
return default_ ;
64 pos_t pos2 = pos1 + key.length() + 3U ;
65 pos_t pos3 =
event.find(
"," , pos2 ) ;
66 if( pos3 == npos )
return default_ ;
67 std::string value =
event.substr( pos2 , pos3-pos2 ) ;
75 return apply( std::string(&event[0],event.size()) ) ;
82 info.x = parse( event ,
"x" ) ;
83 info.y = parse( event ,
"y" ) ;
84 info.x_down = parse( event ,
"x0" ) ;
85 info.y_down = parse( event ,
"y0" ) ;
86 info.dx = parse( event ,
"dx" ) ;
87 info.dy = parse( event ,
"dy" ) ;
88 info.shift = parse( event ,
"shift" ) ;
89 info.control = parse( event ,
"control" ) ;
96 if( event.find(
"'button': 'down'") != std::string::npos )
100 m_button_down = true ;
104 if( event.find(
"'event': 'move'") != std::string::npos )
106 info.type = m_button_down ? Drag : Move ;
108 if( event.find(
"'button': 'up'") != std::string::npos )
112 m_button_down = false ;
131 virtual void readEvent()
override ;
132 virtual void onException( std::exception & )
override ;
142 Gv::ViewerEventMixinImp::ViewerEventMixinImp(
ViewerEventMixin & outer ,
const std::string & channel ) :
144 m_viewer_event(channel) ,
148 if( !channel.empty() )
149 m_timer.startTimer( 1 ) ;
152 Gv::ViewerEventMixinImp::~ViewerEventMixinImp()
158 void Gv::ViewerEventMixinImp::onTimeout()
160 if( m_viewer_event.init() )
162 G_ASSERT( m_fd == -1 ) ;
165 m_fd = m_viewer_event.fd() ;
171 G_DEBUG(
"Gv::ViewerEventMixinImp::onTimeout: viewer event channel not ready: " << m_viewer_event.channelName() ) ;
172 m_timer.startTimer( 1 ) ;
176 void Gv::ViewerEventMixinImp::readEvent()
178 std::vector<char> buffer ;
179 if( !m_viewer_event.receive( buffer ) )
180 throw std::runtime_error(
"viewer has gone away" ) ;
182 m_outer.onViewerEvent( m_viewer_event.apply(buffer) ) ;
185 void Gv::ViewerEventMixinImp::onException( std::exception & )
204 Gv::ViewerEvent::Info::Info() :
217 std::string Gv::ViewerEvent::Info::str()
const
219 std::ostringstream ss ;
222 << x <<
"," << y <<
","
223 << dx <<
"," << dy <<
","
224 << x_down <<
"," << y_down <<
","
225 << shift <<
"," << control ;
A structure representing an interaction event received from a viewer process.
bool receive(std::vector< char > &, std::string *type_p=nullptr)
Receives the payload for an event on fd().
A class for receiving and handling events published from a viewer process.
static int toInt(const std::string &s)
Converts string 's' to an int.
bool init()
Tries to subscribe to the viewer's event channel.
ViewerEvent(const std::string &channel_name=std::string())
Constructor.
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
ViewerEventMixin(const std::string &channel)
Constructor.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
A pimple-pattern implementation class for Gv::ViewerEventMixin.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
static bool isInt(const std::string &s)
Returns true if the string can be converted into an integer without throwing an exception.
A base class for classes that handle asynchronous events from the event loop.
virtual ~ViewerEventMixin()
Destructor.
std::string channelName() const
Returns the channel name, as passed to the ctor.
int fd() const
Returns the subsription file descriptor, or minus one if not init()isalised.
Info apply(const std::string &)
Parses the event string and maintains an internal button state.
A timer class template in which the timeout is delivered to the specified method. ...
A mixin base-class containing a ViewerEvent object that integrates with the GNet::EventLoop.
static std::string ws()
A convenience function returning standard whitespace characters.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.