\hypertarget{ecore_event_example_02_8c-example}{}\section{ecore\+\_\+event\+\_\+example\+\_\+02.\+c}
This example shows how to setup, change, and delete event handlers. See \hyperlink{ecore_event_example_02_c}{the explanation here}.


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -g -Wall -o ecore\_event\_example\_02 ecore\_event\_example\_02.c `pkg-config --cflags --libs ecore`}

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

\textcolor{keyword}{struct }context   \textcolor{comment}{// helper struct to give some context to the callbacks}
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}          *str1, *str2;
   \hyperlink{group__Ecore__Event__Group_ga252a2566c9c7c9094964ab8e157f0521}{Ecore\_Event\_Handler} *handler1;
   \hyperlink{group__Ecore__Event__Group_ga252a2566c9c7c9094964ab8e157f0521}{Ecore\_Event\_Handler} *handler2;
\};

\textcolor{keyword}{static} \textcolor{keywordtype}{int} \_event\_type = 0; \textcolor{comment}{// a new type of event will be defined and stored here}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_event\_handler1\_cb(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{int} type \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{void} *event)
\{
   \textcolor{keywordtype}{int} *number = event;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *str = data;

   printf(\textcolor{stringliteral}{"event\_handler1: number=%d, data=\(\backslash\)"%s\(\backslash\)".\(\backslash\)n"}, *number, str);

   \textcolor{keywordflow}{if} ((*number % 2) == 0)
     \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE};

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga6a2922b8aa10c60f27c8ab49d84d75f2}{ECORE\_CALLBACK\_PASS\_ON};
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_event\_handler2\_cb(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{int} type \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{void} *event) \textcolor{comment}{// event callback}
\{
   \textcolor{keyword}{struct }context *ctxt = data;
   \textcolor{keywordtype}{int} *number = event;

   printf(\textcolor{stringliteral}{"event\_handler2: number=%d.\(\backslash\)n"}, *number);

   \textcolor{keywordflow}{if} (*number == 5)
     \{
        \textcolor{keyword}{const} \textcolor{keywordtype}{char} *old = NULL;
        old = \hyperlink{group__Ecore__Event__Group_ga3ea928587c004f09a6d1b1bd7948327a}{ecore\_event\_handler\_data\_set}(ctxt->handler1, (\textcolor{keywordtype}{void} *)ctxt->str2);
        printf(\textcolor{stringliteral}{"changed handler1 data from \(\backslash\)"%s\(\backslash\)" to \(\backslash\)"%s\(\backslash\)".\(\backslash\)n"},
               old, ctxt->str2);
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (*number >= 10)
     \{
        printf(\textcolor{stringliteral}{"finish main loop.\(\backslash\)n"});
        \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
     \}

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE}; \textcolor{comment}{// same as EINA\_FALSE}
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \textcolor{keyword}{struct }context ctxt = \{0\};
   \textcolor{keywordtype}{int} i;
   ctxt.str1 = \textcolor{stringliteral}{"dataone"};
   ctxt.str2 = \textcolor{stringliteral}{"datatwo"};

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

   \_event\_type = \hyperlink{group__Ecore__Event__Group_ga3fad8d8bfe3776c69d33aff1f056ba89}{ecore\_event\_type\_new}();

   ctxt.handler1 = \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\_event\_type,
                                           \_event\_handler1\_cb,
                                           ctxt.str1);
   ctxt.handler2 = \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\_event\_type,
                                           \_event\_handler2\_cb,
                                           &ctxt);

   \textcolor{keywordflow}{for} (i = 0; i <= 15; i++)
     \{
        \textcolor{keywordtype}{int} *event\_data = malloc(\textcolor{keyword}{sizeof}(*event\_data));
        *event\_data = i;
        \hyperlink{group__Ecore__Event__Group_gacea6a144774958a7188274b79c91ee1f}{ecore\_event\_add}(\_event\_type, event\_data, NULL, NULL);
     \}

   printf(\textcolor{stringliteral}{"start the main loop.\(\backslash\)n"});

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

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

   \textcolor{keywordflow}{return} 0;
\}

\end{DoxyCodeInclude}
 