VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gxeventloop.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 gxeventloop.h
19 ///
20 
21 #ifndef GX_EVENT_LOOP_H
22 #define GX_EVENT_LOOP_H
23 
24 #include "gdef.h"
25 #include "gxdef.h"
26 #include "gxwindowmap.h"
27 #include <time.h> // timeval
28 #include <sys/time.h> // gettimeofday()
29 
30 namespace GX
31 {
32  class EventLoop ;
33  class Display ;
34 }
35 
36 namespace GX
37 {
38  struct Timeval /// A thin wrapper for 'struct timeval' providing relational operators etc.
39  {
40  Timeval() { ::gettimeofday(&m_timeval,nullptr) ; }
41  explicit Timeval( int ) { reset() ; }
42  explicit Timeval( const struct timeval & tv ) : m_timeval(tv) {}
43  void reset() { m_timeval.tv_sec = 0 ; m_timeval.tv_usec = 0 ; }
44  bool is_set() const { return m_timeval.tv_sec != 0 ; }
45  bool operator<( const Timeval & other ) const
46  {
47  return
48  m_timeval.tv_sec < other.m_timeval.tv_sec ||
49  ( m_timeval.tv_sec == other.m_timeval.tv_sec && m_timeval.tv_usec < other.m_timeval.tv_usec ) ;
50  }
51  bool operator==( const Timeval & other ) const
52  {
53  return m_timeval.tv_sec == other.m_timeval.tv_sec && m_timeval.tv_usec == other.m_timeval.tv_usec ;
54  }
55  bool operator>( const Timeval & other ) const
56  {
57  return !((*this)==other) && other < (*this) ;
58  }
59  struct timeval m_timeval ;
60  } ;
61  inline Timeval operator-( const Timeval & big , const Timeval & small )
62  {
63  Timeval result ;
64  const bool carry = small.m_timeval.tv_usec > big.m_timeval.tv_usec ;
65  result.m_timeval.tv_usec = (carry?1000000L:0L) + big.m_timeval.tv_usec ;
66  result.m_timeval.tv_usec -= small.m_timeval.tv_usec ;
67  result.m_timeval.tv_sec = big.m_timeval.tv_sec - small.m_timeval.tv_sec - (carry?1:0) ;
68  return result ;
69  }
70  inline Timeval operator+( const Timeval & base , unsigned long usec )
71  {
72  Timeval result = base ;
73  result.m_timeval.tv_usec += ( usec % 1000000UL ) ;
74  result.m_timeval.tv_sec += ( usec / 1000000UL ) ;
75  bool carry = result.m_timeval.tv_usec > 1000000L ;
76  if( carry )
77  {
78  result.m_timeval.tv_usec -= 1000000L ;
79  result.m_timeval.tv_sec++ ;
80  }
81  return result ;
82  }
83 }
84 
85 /// \class GX::EventLoop
86 /// An event-loop class that delivers Xlib 'Display' events to GX::Window
87 /// objects via their GX::EventHandler interface. Note that GX::Window
88 /// objects register themselves with the GX::WindowMap singleton.
89 ///
91 {
92 public:
93  explicit EventLoop( Display & ) ;
94  ///< Constructor. The display reference is kept.
95 
96  void run() ;
97  ///< Runs the event loop.
98 
99  void runUntil( int event_type ) ;
100  ///< Runs the event loop until the given event is received.
101 
102  void runOnce() ;
103  ///< Waits for one event and processes it.
104 
105  void runToEmpty() ;
106  ///< Processes all events in the queue and then returns.
107 
108  void runToTimeout() ;
109  ///< Processes all events until the timer goes off.
110 
111  void startTimer( unsigned int milliseconds ) ;
112  ///< Starts a timer for runToTimeout().
113 
114  void handlePendingEvents() ;
115  ///< Handles all pending events. This is to allow the GX::EventLoop
116  ///< to be subservient to another event loop; the main event loop
117  ///< calls this method when it detects a read event on the display's
118  ///< file descriptor.
119 
120 private:
121  EventLoop( const EventLoop & ) ;
122  void operator=( const EventLoop & ) ;
123  void handle( ::XEvent & ) ;
124  void handleImp( ::XEvent & ) ;
125  GX::Window & w( ::Window ) ;
126  Display & display() ;
127 
128 private:
129  GX::Display & m_display ;
130  GX::WindowMap m_window_map ;
131  Timeval m_timeout ;
132 } ;
133 
134 #endif
A window class that is-a GX::Drawable and a GX::EventHandler.
Definition: gxwindow.h:47
void handlePendingEvents()
Handles all pending events.
An event-loop class that delivers Xlib 'Display' events to GX::Window objects via their GX::EventHand...
Definition: gxeventloop.h:90
An Xlib Display wrapper.
Definition: gxdisplay.h:38
void startTimer(unsigned int milliseconds)
Starts a timer for runToTimeout().
void runToEmpty()
Processes all events in the queue and then returns.
Definition: gxeventloop.cpp:57
void runOnce()
Waits for one event and processes it.
void runUntil(int event_type)
Runs the event loop until the given event is received.
void run()
Runs the event loop.
Definition: gxeventloop.cpp:48
A class that can locate a GX::Window object based on a Xlib window handle.
Definition: gxwindowmap.h:39
void runToTimeout()
Processes all events until the timer goes off.
Definition: gxeventloop.cpp:68
EventLoop(Display &)
Constructor. The display reference is kept.
Definition: gxeventloop.cpp:37
A thin wrapper for 'struct timeval' providing relational operators etc.
Definition: gxeventloop.h:38