\hypertarget{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_intro}{}\section{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'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}{}\subsection{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}
\hyperlink{group__Ecore__Timer__Group}{Ecore Timer functions}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_pollers}{}\subsection{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}
\hyperlink{group__Ecore__Poller__Group}{Ecore Poll functions}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_idler}{}\subsection{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 C\+P\+U 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}
\hyperlink{group__Ecore__Idle__Group}{Ecore Idle functions}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_fd_handler}{}\subsection{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 A\+P\+I, regardless of if was gotten with an O\+S specific A\+P\+I or from ecore. \begin{DoxySeeAlso}{See also}
\hyperlink{group__Ecore__FD__Handler__Group}{File Descriptor Handling Functions}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_animators}{}\subsection{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}
\hyperlink{group__Ecore__Animator__Group}{Ecore Animator functions}
\end{DoxySeeAlso}
\hypertarget{Ecore_Main_Loop_Page_ev_handlers}{}\subsection{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}
\hyperlink{group__Ecore__Event__Group}{Ecore Event functions}
\end{DoxySeeAlso}
All of these primitives are discussed in more detail in their respective pages linked above.

Here is a diagram of the main loop flow of a simple program\+:

 
\begin{DoxyImage}
\includegraphics{prog_flow}
\caption{width=}
\end{DoxyImage}
\hypertarget{Ecore_Main_Loop_Page_Ecore_Main_Loop_Page_work}{}\section{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}
\textcolor{preprocessor}{#include <Ecore.h>}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} **argv)
\{
   \hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}();
   \hyperlink{group__Ecore__Application__Group_ga70bce8ffb6e2dc2c0fdab005d566532e}{ecore\_app\_args\_set}(argc, argv);
   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();
   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
   \textcolor{keywordflow}{return} 0;
\}
\end{DoxyCode}


This program is very simple and doesn'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'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}
\textcolor{preprocessor}{#include <Ecore.h>}

\hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}         *timer1     = NULL;
\hyperlink{group__Ecore__Event__Group_ga252a2566c9c7c9094964ab8e157f0521}{Ecore\_Event\_Handler} *handler1   = NULL;
\textcolor{keywordtype}{double}               start\_time = 0.0;

\textcolor{keywordtype}{int}
timer\_func(\textcolor{keywordtype}{void} *data)
\{
   printf(\textcolor{stringliteral}{"Tick timer. Sec: %3.2f\(\backslash\)n"}, \hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}() - start\_time);
   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\textcolor{keywordtype}{int}
exit\_func(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{int} ev\_type, \textcolor{keywordtype}{void} *ev)
\{
   \hyperlink{struct__Ecore__Event__Signal__Exit}{Ecore\_Event\_Signal\_Exit} *e;

   e = (\hyperlink{struct__Ecore__Event__Signal__Exit}{Ecore\_Event\_Signal\_Exit} *)ev;
   \textcolor{keywordflow}{if} (e->\hyperlink{struct__Ecore__Event__Signal__Exit_a75a11bba7c7e80245220a80085ad9b25}{interrupt})      printf(\textcolor{stringliteral}{"Exit: interrupt\(\backslash\)n"});
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (e->\hyperlink{struct__Ecore__Event__Signal__Exit_a9859b9dfe71ede48069c219cdaff9e3c}{quit})      printf(\textcolor{stringliteral}{"Exit: quit\(\backslash\)n"});
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (e->\hyperlink{struct__Ecore__Event__Signal__Exit_ac5e1afacd480efe389e699c52f6dd4d6}{terminate}) printf(\textcolor{stringliteral}{"Exit: terminate\(\backslash\)n"});
   \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
   \textcolor{keywordflow}{return} 1;
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} **argv)
\{
   \hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}();
   \hyperlink{group__Ecore__Application__Group_ga70bce8ffb6e2dc2c0fdab005d566532e}{ecore\_app\_args\_set}(argc, argv);
   start\_time = \hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}();
   handler1 = \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(
      \hyperlink{group__Ecore__Event__Group_ga0dd1e36930ae8fa8decc97564d57f310}{ECORE\_EVENT\_SIGNAL\_EXIT}, exit\_func, NULL);
   timer1 = ecore\_timer\_add(0.5, timer\_func, NULL);
   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();
   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
   \textcolor{keywordflow}{return} 0;
\}
\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 E\+C\+O\+R\+E\+\_\+\+E\+V\+E\+N\+T\+\_\+\+S\+I\+G\+N\+A\+L\+\_\+\+E\+X\+I\+T is received (C\+T\+R\+L-\/\+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 \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\+\_\+main\+\_\+loop\+\_\+quit()}.

The handles returned by ecore\+\_\+timer\+\_\+add() and \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\+\_\+event\+\_\+handler\+\_\+add()} are only stored here as an example. If you don't need to address the timer or event handler again you don't need to store the result, so just call the function, and don'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't get any more complex. You add more event handlers, for more events, will have more timers and such, B\+U\+T it all follows the same principles as shown in this example. 