

This example shows how to setup, and explains how an Ecore\+\_\+\+Poller is called. You can \mbox{\hyperlink{ecore_poller_example_8c-example}{see the full source code here}}.

In this example we store the initial time of the program just to use as comparison to the time when the poller callbacks are called. It will be stored in {\ttfamily \+\_\+initial\+\_\+time} \+:


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o ecore\_poller\_example ecore\_poller\_example.c `pkg-config --libs --cflags ecore eo`}

\textcolor{preprocessor}{#include <Ecore.h>}
\textcolor{comment}{//#include <Ecore\_Eo.h>}
\textcolor{preprocessor}{#include <unistd.h>}

\textcolor{keyword}{static} \textcolor{keywordtype}{double} \_initial\_time = 0;

\end{DoxyCodeInclude}


Then next step is to define the poller callback. This callback assumes that a {\ttfamily data} pointer is passed to it on creation, and is a string just used to identify the poller. The callback prints this string and the time since the program started, and returns \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{E\+C\+O\+R\+E\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+R\+E\+N\+EW}} to keep being called.


\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_poller\_print\_cb(\textcolor{keywordtype}{void} *data)
\{
   \textcolor{keywordtype}{char} *str = data;
   printf(\textcolor{stringliteral}{"Ecore Poller '%s' callback called after %0.3f seconds.\(\backslash\)n"},
          str, \mbox{\hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}}() - \_initial\_time);

   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};
\}

\end{DoxyCodeInclude}


Now in the main function we initialize Ecore, and save the initial time of the program, so we can compare it later with the time that the pollers are being called\+:


\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_poller\_quit\_cb(\textcolor{keywordtype}{void} *data \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})
\{

   \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \textcolor{keywordtype}{double} interval = 0.3; \textcolor{comment}{// tick each 0.3 seconds}
   Ecore\_Poller *poller1, *poller2, *poller3;
   \textcolor{keywordtype}{char} *str1 = \textcolor{stringliteral}{"poller1"};
   \textcolor{keywordtype}{char} *str2 = \textcolor{stringliteral}{"poller2"};
   \textcolor{keywordtype}{char} *str3 = \textcolor{stringliteral}{"poller3"};

   \textcolor{keywordflow}{if} (!\mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}())
     \{
        printf(\textcolor{stringliteral}{"ERROR: Cannot init Ecore!\(\backslash\)n"});
        \textcolor{keywordflow}{return} -1;
     \}

   \_initial\_time = \mbox{\hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}}();

\end{DoxyCodeInclude}


Then we change the poll interval to 0.\+3 seconds (the default is 0.\+125 seconds) just to show the A\+PI usage.

Finally, we create two pollers, one that will be called every 4 ticks, and another one that will be called every 8 ticks. This means the the first poller interval will be around 1.\+2 seconds, and the second one will be around 2.\+4 seconds. But the most important point is\+: since the second poller interval is a multiple of the first one, they will be always synchronized. Ecore calls pollers that are in the \char`\"{}same tick\char`\"{} together. It doesn\textquotesingle{}t go back to the main loop and check if there\textquotesingle{}s another poller to execute at this time, but instead it calls all the pollers registered to this \char`\"{}tick\char`\"{} at the same time. See the description of \mbox{\hyperlink{group__Ecore__Poller__Group_gadd8ee7135864da4313bc88faa0a2d7c9}{ecore\+\_\+poller\+\_\+add()}} for more details. This is easy to see in the time printed by both of them.

If instead of two synchronized pollers, we were using two different timers, one with interval of 1.\+2 seconds and another one with an interval of 2.\+4 seconds, there would be no guarantee that they would be totally in sync. Some delay in the execution of another task, or even in the task called in the callback, could make them get out of sync, forcing Ecore\textquotesingle{}s main loop to wake up more than necessary.

Well, this is the code that create these two pollers and set the poll interval, then starts the main loop\+:


\begin{DoxyCodeInclude}

   \mbox{\hyperlink{group__Ecore__Poller__Group_ga466dff0c7fb7eb0f5b4e5a3825fa10ac}{ecore\_poller\_poll\_interval\_set}}(
      \mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}}, interval);

   poller1 = \mbox{\hyperlink{group__Ecore__Poller__Group_gadd8ee7135864da4313bc88faa0a2d7c9}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}}, 4, \_poller\_print\_cb, str1
      );
   poller2 = \mbox{\hyperlink{group__Ecore__Poller__Group_gadd8ee7135864da4313bc88faa0a2d7c9}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}}, 8, \_poller\_print\_cb, str2
      );
   poller3 = \mbox{\hyperlink{group__Ecore__Poller__Group_gadd8ee7135864da4313bc88faa0a2d7c9}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}}, 30, \_poller\_quit\_cb, str3
      );

\textcolor{comment}{//   poller1 = efl\_add\_ref(ECORE\_POLLER\_CLASS, NULL, //         ecore\_poller\_constructor(efl\_added,
       ECORE\_POLLER\_CORE, 4, \_poller\_print\_cb, str1));}
\textcolor{comment}{//   poller2 = efl\_add\_ref(ECORE\_POLLER\_CLASS, NULL, //         ecore\_poller\_constructor(efl\_added,
       ECORE\_POLLER\_CORE, 8, \_poller\_print\_cb, str2));}
\textcolor{comment}{//   poller3 = efl\_add\_ref(ECORE\_POLLER\_CLASS, NULL, //         ecore\_poller\_constructor(efl\_added,
       ECORE\_POLLER\_CORE, 20, \_poller\_quit\_cb, str3));}


   \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();

\end{DoxyCodeInclude}


If you hit C\+T\+R\+L-\/C during the execution of the program, the main loop will quit, since there are some signal handlers already set by default to do this. So after the main loop begin call, we change the second poller\textquotesingle{}s interval to 16 ticks, so it will happen each 4.\+8 seconds (or each 4 times that the first poller is called).

This means\+: the program is started, the first poller is called each 4 ticks and the second is called each 8 ticks. After C\+T\+R\+L-\/C is used, the second poller will be called each 16 ticks.


\begin{DoxyCodeInclude}

   printf(\textcolor{stringliteral}{"changing poller2 interval to 16\(\backslash\)n"});

   \mbox{\hyperlink{group__Ecore__Poller__Group_gae9acf938d0e3b3ee84509d0c04ed628a}{ecore\_poller\_poller\_interval\_set}}(poller2, 16);
\textcolor{comment}{//   ecore\_poller\_interval\_set(poller2, 16, NULL);}
   \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();

\textcolor{comment}{//   efl\_unref(poller1);}
\textcolor{comment}{//   efl\_unref(poller2);}
\textcolor{comment}{//   efl\_unref(poller3);}
   \mbox{\hyperlink{group__Ecore__Poller__Group_ga52e6245cfbceaed9b721a1addd29d311}{ecore\_poller\_del}}(poller1);
   \mbox{\hyperlink{group__Ecore__Poller__Group_ga52e6245cfbceaed9b721a1addd29d311}{ecore\_poller\_del}}(poller2);
   \mbox{\hyperlink{group__Ecore__Poller__Group_ga52e6245cfbceaed9b721a1addd29d311}{ecore\_poller\_del}}(poller3);

   \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();
\}

\end{DoxyCodeInclude}


The rest of the program is just deleting the pollers and shutting down the library. 