VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvviewerevent.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvviewerevent.h
19 ///
20 
21 #ifndef GV_VIEWEREVENT__H
22 #define GV_VIEWEREVENT__H
23 
24 #include "gdef.h"
25 #include "gpublisher.h"
26 #include <string>
27 #include <iostream>
28 
29 namespace Gv
30 {
31  class ViewerEvent ;
32  class ViewerEventMixin ;
33  class ViewerEventMixinImp ;
34 }
35 
36 /// \class Gv::ViewerEvent
37 /// A class for receiving and handling events published from a viewer process.
38 ///
40 {
41 public:
42  enum Type { Up , Down , Drag , Move , Invalid } ;
43  struct Info /// A structure representing an interaction event received from a viewer process.
44  {
45  Type type ;
46  int x ;
47  int y ;
48  int dx ;
49  int dy ;
50  int x_down ;
51  int y_down ;
52  bool shift ;
53  bool control ;
54  Info() ;
55  std::string str() const ;
56  } ;
57 
58  explicit ViewerEvent( const std::string & channel_name = std::string() ) ;
59  ///< Constructor.
60 
61  Info apply( const std::string & ) ;
62  ///< Parses the event string and maintains an internal button state.
63 
64  Info apply( const std::vector<char> & ) ;
65  ///< Overload taking a vector.
66 
67  bool init() ;
68  ///< Tries to subscribe to the viewer's event channel.
69  ///< Returns true if newly subscribed.
70 
71  int fd() const ;
72  ///< Returns the subsription file descriptor, or minus one if
73  ///< not init()isalised.
74 
75  bool receive( std::vector<char> & , std::string * type_p = nullptr ) ;
76  ///< Receives the payload for an event on fd().
77  /// \see G::PublisherSubscriber::receive().
78 
79  std::string channelName() const ;
80  ///< Returns the channel name, as passed to the ctor.
81 
82 private:
83  ViewerEvent( const ViewerEvent & ) ;
84  void operator=( const ViewerEvent & ) ;
85  static int parse( const std::string & event , const std::string & key , int default_ = -1 ) ;
86 
87 private:
88  std::string m_name ;
89  G::PublisherSubscription m_channel ;
90  bool m_button_down ;
91 } ;
92 
93 /// \class Gv::ViewerEventMixin
94 /// A mixin base-class containing a ViewerEvent object that integrates
95 /// with the GNet::EventLoop.
96 ///
98 {
99 public:
100  explicit ViewerEventMixin( const std::string & channel ) ;
101  ///< Constructor.
102 
103  virtual ~ViewerEventMixin() ;
104  ///< Destructor.
105 
106  virtual void onViewerEvent( ViewerEvent::Info ) = 0 ;
107  ///< Callback that delivers an event from the viewer.
108 
109 private:
111  void operator=( const ViewerEventMixin & ) ;
112 
113 private:
114  ViewerEventMixinImp * m_imp ;
115 } ;
116 
117 namespace Gv
118 {
119  inline
120  std::ostream & operator<<( std::ostream & stream , const ViewerEvent::Info & event )
121  {
122  return stream << event.str() ;
123  }
124 }
125 
126 #endif
A structure representing an interaction event received from a viewer process.
Definition: gvviewerevent.h:43
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.
Definition: gvviewerevent.h:39
bool init()
Tries to subscribe to the viewer's event channel.
ViewerEvent(const std::string &channel_name=std::string())
Constructor.
ViewerEventMixin(const std::string &channel)
Constructor.
An easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
Definition: gpublisher.h:229
A pimple-pattern implementation class for Gv::ViewerEventMixin.
virtual void onViewerEvent(ViewerEvent::Info)=0
Callback that delivers an event from the viewer.
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 mixin base-class containing a ViewerEvent object that integrates with the GNet::EventLoop.
Definition: gvviewerevent.h:97