ivi-main-loop
ivi-main-loop-glib.h
1  /* Copyright (C) 2015 Jacques Guillou */
2  /* Copyright (C) 2015 Pelagicore AB <info@pelagicore.com> */
3 
4  /* This library is free software; you can redistribute it and/or */
5  /* modify it under the terms of the GNU Library General Public */
6  /* License as published by the Free Software Foundation; either */
7  /* version 2 of the License, or (at your option) any later version. */
8 
9  /* This library 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 GNU */
12  /* Library General Public License for more details. */
13 
14  /* A copy of the GNU Library General Public License is included in the */
15  /* "LICENSE" file. */
16 
17 #pragma once
18 
19 #include "ivi-main-loop-common.h"
20 #include "glib.h"
21 
22 namespace ivi {
23 
24 static constexpr int UNREGISTERED_SOURCE = -1;
25 
26 class GLibEventDispatcher;
27 
29  public IdleEventSource
30 {
31 
32 public:
33  GLibIdleEventSource(GLibEventDispatcher &mainLoop, CallBackFunction callBackFunction) :
34  IdleEventSource(callBackFunction),
35  m_mainLoop(mainLoop)
36  {
37  m_func = callBackFunction;
38  }
39 
41 
42  void enable() override;
43 
44  void disable() override;
45 
46  bool isEnabled() const override;
47 
48 private:
49  static gboolean onGLibCallback(gpointer data);
50 
51  GSource *m_source = nullptr;
52  GLibEventDispatcher &m_mainLoop;
53 
54 };
55 
56 
58  public TimeOutEventSource
59 {
60 public:
61  GLibTimeOutEventSource(GLibEventDispatcher &mainLoop, CallBackFunction callBackFunction, DurationInMilliseconds duration) :
62  TimeOutEventSource(duration, callBackFunction), m_mainLoop(mainLoop)
63  {
64  }
65 
66 public:
68 
69  void setDuration(DurationInMilliseconds duration) override;
70 
71  void enable() override;
72 
73  void disable() override;
74 
75  bool isEnabled() const override;
76 
77 private:
78  static gboolean onTimerCallback(gpointer data);
79 
80  GLibEventDispatcher &m_mainLoop;
81  GSource *m_source = nullptr;
82 };
83 
86 {
87 public:
88  GLibChannelWatchEventSource(GLibEventDispatcher &mainLoop, CallBackFunction callBackFunction, FileDescriptor fileDescriptor,
89  Event events);
90 
92 
93  void disable() override;
94 
95  void enable() override;
96 
97  bool isEnabled() const {
98  return (inputSourceID!=UNREGISTERED_SOURCE);
99  }
100 
101 private:
102  static gboolean onSocketDataAvailableGLibCallback(GIOChannel *gio, GIOCondition condition, gpointer data);
103 
104  static Event toEventSource(const GIOCondition condition);
105 
106  static GIOCondition toGIOCondition(const Event event);
107 
108  gint inputSourceID = UNREGISTERED_SOURCE;
109 
110  GIOChannel *m_channel = nullptr;
111  GLibEventDispatcher &m_mainLoop;
112  Event m_events;
113 
114 };
115 
116 /**
117  * That class implements the EventDispatcher interface using glib's main loop functions
118  */
120  public EventDispatcher
121 {
122 public:
126 
127  /**
128  * Construct an instance using GLib's default main context if we do not have any instance of GLibEventDispatcher using that
129  * context yet, otherwise we create a dedicated context
130  */
132 
133  /**
134  * Construct a glib event dispatcher using the given context
135  */
136  GLibEventDispatcher(GMainContext* context);
137 
138  IdleEventSource *newIdleEventSource(const IdleEventSource::CallBackFunction &callBackFunction) final override;
139 
140  TimeOutEventSource *newTimeOutEventSource(const TimeOutEventSource::CallBackFunction &callBackFunction,
141  DurationInMilliseconds duration) final override;
142 
143  ChannelWatchEventSource *newChannelWatchEventSource(const ChannelWatchEventSource::CallBackFunction &callBackFunction,
144  FileDescriptor filedescriptor,
145  ChannelWatchEventSource::Event events) final override;
146 
147  void run() final override;
148 
149  void quit() final override;
150 
151  /**
152  * Return the glib main context reference
153  */
154  GMainContext *getGMainContext()
155  {
156  return m_context;
157  }
158 
159 private:
160  GMainContext *m_context = nullptr;
161  GMainLoop *m_mainLoop = nullptr;
162 
163  static bool s_bDefaultContextAlreadyUsed;
164 
165 };
166 
167 }
IdleEventSource * newIdleEventSource(const IdleEventSource::CallBackFunction &callBackFunction) finaloverride
Create a new idle event source.
TimeOutEventSource * newTimeOutEventSource(const TimeOutEventSource::CallBackFunction &callBackFunction, DurationInMilliseconds duration) finaloverride
Create a new timeout event source.
void disable() override
Disables the source.
A timeout source is triggered after a certain amount of time.
This kind of source can be used to be notified whenever an event has occurred concerning a channel...
bool isEnabled() const override
Returns true if the source is currently enabled, false otherwise.
ChannelWatchEventSource * newChannelWatchEventSource(const ChannelWatchEventSource::CallBackFunction &callBackFunction, FileDescriptor filedescriptor, ChannelWatchEventSource::Event events) finaloverride
Create a new channel watch event source.
GLibEventDispatcher()
Construct an instance using GLib's default main context if we do not have any instance of GLibEventDi...
An idle event source can be used to be notified whenever a dispatcher has no non-idle to trigger...
That class implements the EventDispatcher interface using glib's main loop functions.
void enable() override
Enables the source.
Event dispatcher interface.
bool isEnabled() const override
Returns true if the source is currently enabled, false otherwise.
void quit() finaloverride
Stops the main loop.
GMainContext * getGMainContext()
Return the glib main context reference.
void disable() override
Disables the source.
bool isEnabled() const
Returns true if the source is currently enabled, false otherwise.
void disable() override
Disables the source.
void enable() override
Enables the source.
void run() finaloverride
Runs the main loop.
void enable() override
Enables the source.