

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}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile\ with:}}
\DoxyCodeLine{\textcolor{comment}{//\ gcc\ -\/o\ ecore\_poller\_example\ ecore\_poller\_example.c\ `pkg-\/config\ -\/-\/libs\ -\/-\/cflags\ ecore\ eo`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}
\DoxyCodeLine{\textcolor{comment}{//\#include\ <Ecore\_Eo.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\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}{ECORE\+\_\+\+CALLBACK\+\_\+\+RENEW}} to keep being called.


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

\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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_poller\_quit\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{double}\ interval\ =\ 0.3;\ \textcolor{comment}{//\ tick\ each\ 0.3\ seconds}}
\DoxyCodeLine{\ \ \ Ecore\_Poller\ *poller1,\ *poller2,\ *poller3;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str1\ =\ \textcolor{stringliteral}{"{}poller1"{}};}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str2\ =\ \textcolor{stringliteral}{"{}poller2"{}};}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str3\ =\ \textcolor{stringliteral}{"{}poller3"{}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}())}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}ERROR:\ Cannot\ init\ Ecore!\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \_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 API 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_ga9ce7d4becc2fb4cc08e07e659c332f27}{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}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Poller__Group_ga466dff0c7fb7eb0f5b4e5a3825fa10ac}{ecore\_poller\_poll\_interval\_set}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}},\ interval);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ poller1\ =\ \mbox{\hyperlink{group__Ecore__Poller__Group_ga9ce7d4becc2fb4cc08e07e659c332f27}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}},\ 4,\ \_poller\_print\_cb,\ str1);}
\DoxyCodeLine{\ \ \ poller2\ =\ \mbox{\hyperlink{group__Ecore__Poller__Group_ga9ce7d4becc2fb4cc08e07e659c332f27}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}},\ 8,\ \_poller\_print\_cb,\ str2);}
\DoxyCodeLine{\ \ \ poller3\ =\ \mbox{\hyperlink{group__Ecore__Poller__Group_ga9ce7d4becc2fb4cc08e07e659c332f27}{ecore\_poller\_add}}(\mbox{\hyperlink{group__Ecore__Poller__Group_gga40d67f547376373ee34363b825874088a022dfec751388e380cf01621bd0f797f}{ECORE\_POLLER\_CORE}},\ 30,\ \_poller\_quit\_cb,\ str3);}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ poller1\ =\ efl\_add\_ref(ECORE\_POLLER\_CLASS,\ NULL,\ //\ \ \ \ \ \ \ \ \ ecore\_poller\_constructor(efl\_added,\ ECORE\_POLLER\_CORE,\ 4,\ \_poller\_print\_cb,\ str1));}}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ poller2\ =\ efl\_add\_ref(ECORE\_POLLER\_CLASS,\ NULL,\ //\ \ \ \ \ \ \ \ \ ecore\_poller\_constructor(efl\_added,\ ECORE\_POLLER\_CORE,\ 8,\ \_poller\_print\_cb,\ str2));}}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ poller3\ =\ efl\_add\_ref(ECORE\_POLLER\_CLASS,\ NULL,\ //\ \ \ \ \ \ \ \ \ ecore\_poller\_constructor(efl\_added,\ ECORE\_POLLER\_CORE,\ 20,\ \_poller\_quit\_cb,\ str3));}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}

\end{DoxyCodeInclude}
 If you hit CTRL-\/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 CTRL-\/C is used, the second poller will be called each 16 ticks.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}changing\ poller2\ interval\ to\ 16\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Poller__Group_gae9acf938d0e3b3ee84509d0c04ed628a}{ecore\_poller\_poller\_interval\_set}}(poller2,\ 16);}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ ecore\_poller\_interval\_set(poller2,\ 16,\ NULL);}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ efl\_unref(poller1);}}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ efl\_unref(poller2);}}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ efl\_unref(poller3);}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Poller__Group_ga5284994ef29eb44342f9dc87c855139b}{ecore\_poller\_del}}(poller1);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Poller__Group_ga5284994ef29eb44342f9dc87c855139b}{ecore\_poller\_del}}(poller2);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Poller__Group_ga5284994ef29eb44342f9dc87c855139b}{ecore\_poller\_del}}(poller3);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The rest of the program is just deleting the pollers and shutting down the library. 