ivi-main-loop
exampleGLibApp.cpp
1 /**
2  * This example shows how to provide a dispatcher reference to library functions, in an application which is based
3  * on a glib main loop.
4  * Note that a small variation of this example could be made if the actual main loop was based on Qt. It would just be a matter
5  * of getting the GMainContext* reference from Qt, via some private Qt methods.
6  */
7 
8 #include <stdio.h>
9 #include <glib.h>
10 
11 #include "ivi-main-loop.h"
12 #include "ivi-main-loop-log.h"
13 
14 #include "exampleLibrary.h"
15 
16 using namespace ivi;
17 
18 gboolean glibCallback(gpointer data) {
19  printf("glib callback called\n");
20  return TRUE;
21 }
22 
23 int main(int argc, const char * *argv) {
24 
25  // Create our context
26  GMainContext* context = g_main_context_new();
27 
28  // Create a source using the glib API
29  GSource * glibSource = g_timeout_source_new(1500);
30  g_source_set_callback(glibSource, glibCallback, nullptr, nullptr);
31  g_source_attach(glibSource, context);
32 
33  // Create our dispatcher object which wraps the glib context
34  GLibEventDispatcher dispatcher(context);
35 
36  // Now we have a "GENIVI-compatible" dispatcher reference, which we can pass to GENIVI components, under certain conditions
37 
38  // Call a function which accepts a parameter of type DefaultEventDispatcherType. If the DefaultEventDispatcherType is not set to GLibEventDispatcher, this should cause a compile error
39  typicalGeniviLibraryFunction(dispatcher);
40 
41  // Call a function which accepts a parameter of type Glib. For sure, this call will not fail here
42  libraryFunctionGLibOnly(dispatcher);
43 
44  // Create the main loop object
45  GMainLoop* main_loop = g_main_loop_new(context, FALSE);
46 
47  log_debug() << "entering main loop";
48  g_main_loop_run(main_loop);
49 
50  return 0;
51 }
That class implements the EventDispatcher interface using glib's main loop functions.