ivi-main-loop
exampleLibrary.h
1 #pragma once
2 
3 #include "ivi-main-loop.h"
4 #include "ivi-main-loop-log.h"
5 
6 /**
7  * Example of a GENIVI library function which registers a new source in the given dispatcher.
8  * GENIVI code should normally use the DefaultEventDispatcherType type, which is a typedef to either
9  * a GLibEventDispatcher, or other GLib-free class
10  */
11 inline void typicalGeniviLibraryFunction(ivi::DefaultEventDispatcherType& dispatcher) {
12  auto src = new ivi::DefaultEventDispatcherType::TimeOutEventSourceType(dispatcher, [&] () {
13  log_debug() << "Timeout libraryFunction";
14  return ivi::DefaultEventDispatcherType::TimeOutEventSourceType::ReportStatus::KEEP_ENABLED;
15  }, 2000);
16  src->enable();
17 }
18 
19 inline gboolean glibCallbackLib(gpointer data) {
20  printf("glibCallbackLib called\n");
21  return TRUE;
22 }
23 
24 /**
25  * Example of a library function which only accepts a GLib kind of mainloop reference.
26  * That would typically be the case if the function was using some OSS software which only support GLib as
27  * a main loop (e.g. PulseAudio glib main loop integration)
28  */
29 inline void libraryFunctionGLibOnly(ivi::GLibEventDispatcher& dispatcher) {
30 
31  // Get the GLib GMainContext reference
32  auto context = dispatcher.getGMainContext();
33 
34  // Create our source using the GLib API directly
35  GSource * glibSource = g_timeout_source_new(1800);
36  g_source_set_callback(glibSource, glibCallbackLib, nullptr, nullptr);
37  g_source_attach(glibSource, context);
38 
39 }
That class implements the EventDispatcher interface using glib's main loop functions.
GMainContext * getGMainContext()
Return the glib main context reference.