\hypertarget{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_intro}{}\doxysection{What is Ecore?}\label{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_intro}
Ecore is a clean and tiny event loop library with many modules to do lots of convenient things for a programmer to save time and effort. It\textquotesingle{}s small and lean, designed to work from embedded systems all the way up to large and powerful multi-\/cpu workstations. The main loop has a number of primitives to be used with its main loop. It serializes all the primitives and allows for great responsiveness without the need for threads(or any other concurrency).\hypertarget{Ecore_Main_Loop_Page_timers}{}\doxysubsection{Timers}\label{Ecore_Main_Loop_Page_timers}
Timers serve two main purposes\+: doing something at a specified time and repeatedly doing something with a set interval.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__Timer__Group}{Ecore Timer functions}}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_pollers}{}\doxysubsection{Pollers}\label{Ecore_Main_Loop_Page_pollers}
Pollers allow for polling to be centralized into a single place therefore alleviating the need for different parts of the program to wake up at different times to do polling, thereby making the code simpler and more efficient.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__Poller__Group}{Ecore Poll functions}}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_idler}{}\doxysubsection{Idlers}\label{Ecore_Main_Loop_Page_idler}
There are three types of idlers\+: enterers, idlers(proper) and exiters. They are called, respectively, when the program is about to enter an idle state, when the program is idle, and when the program is leaving an idle state. Idler enterers are usually a good place to update the program state. Proper idlers are the appropriate place to do heavy computational tasks thereby using what would otherwise be wasted CPU cycles. Exiters are the perfect place to do anything your program should do just before processing events (also timers, pollers, file descriptor handlers and animators)

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__Idle__Group}{Ecore Idle functions}}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_fd_handler}{}\doxysubsection{File descriptor handlers}\label{Ecore_Main_Loop_Page_fd_handler}
File descriptor handlers allow you to monitor when there is data available to read on file descriptors, when writing will not block, or if there was an error. Any valid file descriptor can be used with this API, regardless of if was gotten with an OS specific API or from ecore.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__FD__Handler__Group}{File Descriptor Handling Functions}}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_animators}{}\doxysubsection{Animators}\label{Ecore_Main_Loop_Page_animators}
Ecore provides a facility called animators, so named since the intended use was in animations, that facilitates knowing what percentage of a given interval has elapsed. This is perfect for performing animations, but is not limited to that use, it can, for example, also be used to create a progress bar.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__Animator__Group}{Ecore Animator functions}}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_ev_handlers}{}\doxysubsection{Event handlers}\label{Ecore_Main_Loop_Page_ev_handlers}
Event handlers are, arguably, the most important feature of the ecore main loop, they are what allows the programmer to easily handle user interaction. Events however are not only things the user does, events can represent anything for which a type is created.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{group__Ecore__Event__Group}{Ecore Event functions}} All of these primitives are discussed in more detail in their respective pages linked above.
\end{DoxySeeAlso}
Here is a diagram of the main loop flow of a simple program\+:

 
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{prog_flow}}
\end{DoxyImageNoCaption}
\hypertarget{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_work}{}\doxysection{How does Ecore work?}\label{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_work}
Ecore is very easy to learn and use. All the function calls are designed to be easy to remember, explicit in describing what they do, and heavily name-\/spaced. Ecore programs can start and be very simple.

For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ **argv)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Application__Group_ga70bce8ffb6e2dc2c0fdab005d566532e}{ecore\_app\_args\_set}}(argc,\ argv);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCode}


This program is very simple and doesn\textquotesingle{}t check for errors, but it does start up and begin a main loop waiting for events or timers to tick off. This program doesn\textquotesingle{}t set up any, but now we can expand on this simple program a little more by adding some event handlers and timers.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\mbox{\hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}}\ \ \ \ \ \ \ \ \ *timer1\ \ \ \ \ =\ NULL;}
\DoxyCodeLine{\mbox{\hyperlink{group__Ecore__Event__Group_ga252a2566c9c7c9094964ab8e157f0521}{Ecore\_Event\_Handler}}\ *handler1\ \ \ =\ NULL;}
\DoxyCodeLine{\textcolor{keywordtype}{double}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ start\_time\ =\ 0.0;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{timer\_func(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Tick\ timer.\ Sec:\ \%3.2f\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}}()\ -\/\ start\_time);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{exit\_func(\textcolor{keywordtype}{void}\ *data,\ \textcolor{keywordtype}{int}\ ev\_type,\ \textcolor{keywordtype}{void}\ *ev)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Event__Signal__Exit}{Ecore\_Event\_Signal\_Exit}}\ *e;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ e\ =\ (\mbox{\hyperlink{struct__Ecore__Event__Signal__Exit}{Ecore\_Event\_Signal\_Exit}}\ *)ev;}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (e-\/>\mbox{\hyperlink{struct__Ecore__Event__Signal__Exit_a75a11bba7c7e80245220a80085ad9b25}{interrupt}})\ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Exit:\ interrupt\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (e-\/>\mbox{\hyperlink{struct__Ecore__Event__Signal__Exit_a9859b9dfe71ede48069c219cdaff9e3c}{quit}})\ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Exit:\ quit\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (e-\/>\mbox{\hyperlink{struct__Ecore__Event__Signal__Exit_ac5e1afacd480efe389e699c52f6dd4d6}{terminate}})\ printf(\textcolor{stringliteral}{"{}Exit:\ terminate\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ **argv)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Application__Group_ga70bce8ffb6e2dc2c0fdab005d566532e}{ecore\_app\_args\_set}}(argc,\ argv);}
\DoxyCodeLine{\ \ \ start\_time\ =\ \mbox{\hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}}();}
\DoxyCodeLine{\ \ \ handler1\ =\ \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\_event\_handler\_add}}(\mbox{\hyperlink{group__Ecore__Event__Group_ga0dd1e36930ae8fa8decc97564d57f310}{ECORE\_EVENT\_SIGNAL\_EXIT}},\ exit\_func,\ NULL);}
\DoxyCodeLine{\ \ \ timer1\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(0.5,\ timer\_func,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCode}


In the previous example, we initialize our application and get the time at which our program has started so we can calculate an offset. We set up a timer to tick off in 0.\+5 seconds, and since it returns 1, will keep ticking off every 0.\+5 seconds until it returns 0, or is deleted by hand. An event handler is set up to call a function -\/ exit\+\_\+func(), whenever an event of type ECORE\+\_\+\+EVENT\+\_\+\+SIGNAL\+\_\+\+EXIT is received (CTRL-\/C on the command line will cause such an event to happen). If this event occurs it tells you what kind of exit signal was received, and asks the main loop to quit when it is finished by calling \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\+\_\+main\+\_\+loop\+\_\+quit()}}.

The handles returned by \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\+\_\+timer\+\_\+add()}} and \mbox{\hyperlink{group__Ecore__Event__Group_ga886c90c3f917f87b3b4c931adcbc41c5}{ecore\+\_\+event\+\_\+handler\+\_\+add()}} are only stored here as an example. If you don\textquotesingle{}t need to address the timer or event handler again you don\textquotesingle{}t need to store the result, so just call the function, and don\textquotesingle{}t assign the result to any variable.

This program looks slightly more complex than needed to do these simple things, but in principle, programs don\textquotesingle{}t get any more complex. You add more event handlers, for more events, will have more timers and such, BUT it all follows the same principles as shown in this example. 