This example demonstrates how to manage the idle state of the main loop.

Once a program knows that the main loop is going to enter in idle state, it could start doing some processing until getting out of this state.

To exemplify this, we also add events and a timer to this program, so we can see the idle exiter callback being called before processing the event and/or timer, the event/timer callback being called (processed), then the idle enterer being called before entering in idle state again. Once in idle, the main loop keeps calling the idler callback continuously until a new event or timer is received.

First, we declare a struct that will be used as context to be passed to every callback. It\textquotesingle{}s not useful everywhere, since this example is very simple and doesn\textquotesingle{}t do anything other than printing messages, but using this context will make it a little bit more real. Our context will be used to delete the timer, idler, idle enterer and exiter, and the event handler, and also to count how many times the idler was called.

Then we start declaring callbacks for the idle enterer, idle exiter and the idler itself. Idle enterer and exiter callbacks just print a message saying that they were called, while the idler, in addition to printing a message too, also sends an event every 10 times that it is called, incrementing the context count variable. This event will be used to make the main loop exit the idle state and call the event callback.

These callbacks return \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\+\_\+\+CALLBACK\+\_\+\+RENEW}}, since we want them to keep being called every time the main loop changes to/from idle state. Otherwise, if we didn\textquotesingle{}t want them to be called again, they should return \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\+\_\+\+CALLBACK\+\_\+\+CANCEL}}.

The next function declared is the event callback {\ttfamily \+\_\+event\+\_\+handler\+\_\+cb}. It will check if the idler was called more than 100 times already {\ttfamily }(ctxt-\/\texorpdfstring{$>$}{>}count \texorpdfstring{$>$}{>} 100), and will delete the idler, idle enterer and exiter, the timer (if it still exists), and request that the main loop stop running. Then it returns \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\+\_\+\+CALLBACK\+\_\+\+DONE}} to indicate that the event shouldn\textquotesingle{}t be handled by any other callback.

Finally, we add a callback to the timer, that will just print a message when it is called, and this will happen only once (\mbox{\hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\+\_\+\+CALLBACK\+\_\+\+CANCEL}} is being returned). This timer callback is just here to show that the main loop gets out of idle state when processing timers too.

The {\bfseries{main}} function is simple, just creates a new type of event that we will use to demonstrate the event handling together with the idle state, adds the callbacks that we declared so far, fill the context struct, and starts running the main loop.

\begin{DoxyNote}{Note}
We use timer and event callbacks to demonstrate the idle state changing, but it also happens for file descriptor handlers, pipe handlers, etc.
\end{DoxyNote}

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile\ with:}}
\DoxyCodeLine{\textcolor{comment}{//\ gcc\ -\/o\ ecore\_idler\_example\ ecore\_idler\_example.c\ `pkg-\/config\ -\/-\/libs\ -\/-\/cflags\ ecore\ eo`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ HAVE\_CONFIG\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\#\ include\ <config.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Eo.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct\ }context\ \ \ \textcolor{comment}{//\ helper\ struct\ to\ give\ some\ context\ to\ the\ callbacks}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ count;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Idle__Group_gadc4e81cb020047be0f8f36db6d942ed3}{Ecore\_Idle\_Enterer}}\ \ *enterer;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Idle__Group_gae4e7f2215a6e8cff3e2eab2582b5868e}{Ecore\_Idler}}\ \ \ \ \ \ \ \ \ *idler;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Idle__Group_gaa07d755d135454aa97625464aab4adf8}{Ecore\_Idle\_Exiter}}\ \ \ *exiter;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga252a2566c9c7c9094964ab8e157f0521}{Ecore\_Event\_Handler}}\ *handler;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}}\ \ \ \ \ \ \ \ \ *timer;}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ \_event\_type\ =\ 0;\ \textcolor{comment}{//\ a\ new\ type\ of\ event\ will\ be\ defined\ and\ stored\ here}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_enterer\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})\ \textcolor{comment}{//\ the\ idle\ enterer\ callback}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}IDLE\ ENTERER:\ Ecore\ entering\ in\ idle\ state.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};\ \textcolor{comment}{//\ same\ as\ EINA\_TRUE}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_exiter\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})\ \textcolor{comment}{//\ the\ idle\ exiter\ callback}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}IDLE\ EXITER:\ Ecore\ exiting\ idle\ state.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};\ \textcolor{comment}{//\ same\ as\ EINA\_TRUE}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_idler\_cb(\textcolor{keywordtype}{void}\ *data)\ \textcolor{comment}{//\ the\ idler\ callback\ -\/\ ran\ while\ the\ mainloop\ is\ idle}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }context\ *ctxt\ =\ data;}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}IDLER:\ executing\ idler\ callback\ while\ in\ idle\ state.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ ctxt-\/>count++;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{comment}{/*\ each\ 10\ times\ that\ the\ callback\ gets\ called,\ generate\ an\ event\ that}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ will\ wake\ up\ the\ main\ loop,\ triggering\ idle\ enterers,\ exiters,\ etc.\ */}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ ((ctxt-\/>count\ \%\ 10)\ ==\ 0)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Event__Group_ga81a4c863503927fb70fa5f35625c4352}{ecore\_event\_add}}(\_event\_type,\ NULL,\ NULL,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};\ \textcolor{comment}{//\ same\ as\ EINA\_TRUE}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_event\_handler\_cb(\textcolor{keywordtype}{void}\ *data,\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})\ \textcolor{comment}{//\ event\ callback}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }context\ *ctxt\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}EVENT:\ processing\ callback\ for\ the\ event\ received.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (ctxt-\/>count\ >\ 100)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Idle__Group_gabdce29c943a3f6092e6de3d57eec47bc}{ecore\_idle\_enterer\_del}}(ctxt-\/>enterer);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Idle__Group_ga32d53388d9eefe74a6f910d7c0e636f2}{ecore\_idle\_exiter\_del}}(ctxt-\/>exiter);}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ \ \ \ \ \ ecore\_idler\_del(ctxt-\/>idler);}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ctxt-\/>enterer\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ctxt-\/>exiter\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ctxt-\/>idler\ =\ NULL;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (ctxt-\/>timer)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga7c118b6a6d232b63a6500e599d2a1b47}{ecore\_timer\_del}}(ctxt-\/>timer);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ ctxt-\/>timer\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE}};\ \textcolor{comment}{//\ same\ as\ EINA\_FALSE}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_timer\_cb(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }context\ *ctxt\ =\ data;}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}TIMER:\ timer\ callback\ called.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (ctxt-\/>timer)}
\DoxyCodeLine{\ \ \ \ \ ctxt-\/>timer\ =\ NULL;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\_CALLBACK\_CANCEL}};\ \textcolor{comment}{//\ same\ as\ EINA\_FALSE}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }context\ ctxt\ =\ \{0\};}
\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{\ \ \ \_event\_type\ =\ \mbox{\hyperlink{group__Ecore__Event__Group_ga3fad8d8bfe3776c69d33aff1f056ba89}{ecore\_event\_type\_new}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ ctxt.enterer\ =\ \mbox{\hyperlink{group__Ecore__Idle__Group_ga2120fcb2f5a5a3ead1e2c11f5424e4b8}{ecore\_idle\_enterer\_add}}(\_enterer\_cb,\ \&ctxt);}
\DoxyCodeLine{\ \ \ ctxt.exiter\ =\ \mbox{\hyperlink{group__Ecore__Idle__Group_gade5eb53697d4d4f62d18314f2ce0d42a}{ecore\_idle\_exiter\_add}}(\_exiter\_cb,\ \&ctxt);}
\DoxyCodeLine{\ \ \ ctxt.idler\ =\ \mbox{\hyperlink{group__Ecore__Idle__Group_gaaf001b1f13fb633d0f486feec76e026b}{ecore\_idler\_add}}(\_idler\_cb,\ \&ctxt);}
\DoxyCodeLine{\textcolor{comment}{//\ \ \ ctxt.idler\ =\ efl\_add\_ref(ECORE\_IDLER\_CLASS,\ NULL,\ ecore\_idler\_constructor(efl\_added,\ \_idler\_cb,\ \&ctxt));}}
\DoxyCodeLine{\ \ \ ctxt.handler\ =\ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\_event\_type,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_event\_handler\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \&ctxt);}
\DoxyCodeLine{\ \ \ ctxt.timer\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(0.0005,\ \_timer\_cb,\ \&ctxt);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}

\end{DoxyCodeInclude}
 