\hypertarget{ecore_fd_handler_example_8c-example}{}\doxysection{ecore\+\_\+fd\+\_\+handler\+\_\+example.\+c}
This example shows how to setup and use an fd\+\_\+handler.

This example shows how to setup and use an fd\+\_\+handler. See \mbox{\hyperlink{ecore_fd_handler_example_c}{the explanation here}}.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{/*}}
\DoxyCodeLine{\textcolor{comment}{\ *\ gcc\ -\/o\ ecore\_fd\_handler\_example\ ecore\_fd\_handler\_example.c\ `pkg-\/config\ -\/-\/cflags\ -\/-\/libs\ ecore`}}
\DoxyCodeLine{\textcolor{comment}{\ */}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Ecore.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <unistd.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct\ }context}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler}}\ *handler;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}}\ \ \ \ \ \ *timer;}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_fd\_prepare\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler}}\ *handler\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}prepare\_cb\ called.\(\backslash\)n"{}});}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_fd\_handler\_cb(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler}}\ *handler)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }context\ *ctxt\ =\ data;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ buf[1024];}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{size\_t}\ nbytes;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ fd;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (\mbox{\hyperlink{group__Ecore__FD__Handler__Group_gabd3040bd39c95723e42a8c48484b87de}{ecore\_main\_fd\_handler\_active\_get}}(handler,\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a829d91235e3ca5eba61330c96a8074de}{ECORE\_FD\_ERROR}}))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}An\ error\ has\ occurred.\ Stop\ watching\ this\ fd\ and\ quit.\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ctxt-\/>handler\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\_CALLBACK\_CANCEL}};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ fd\ =\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga70d418717552a5258c7dfc1153250e7d}{ecore\_main\_fd\_handler\_fd\_get}}(handler);}
\DoxyCodeLine{\ \ \ nbytes\ =\ read(fd,\ buf,\ \textcolor{keyword}{sizeof}(buf));}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (nbytes\ ==\ 0)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Nothing\ to\ read,\ exiting...\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ctxt-\/>handler\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\_CALLBACK\_CANCEL}};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ buf[nbytes\ -\/\ 1]\ =\ \textcolor{charliteral}{'\(\backslash\)0'};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Read\ \%zd\ bytes\ from\ input:\ \(\backslash\)"{}\%s\(\backslash\)"{}\(\backslash\)n"{}},\ nbytes\ -\/\ 1,\ buf);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_timer\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Timer\ expired\ after\ 5\ seconds...\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\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{\ \ \ ctxt.handler\ =\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gaf8483e27f0c9fd4a2a7c97fa5539a55f}{ecore\_main\_fd\_handler\_add}}(STDIN\_FILENO,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ}}\ |\ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a829d91235e3ca5eba61330c96a8074de}{ECORE\_FD\_ERROR}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_fd\_handler\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \&ctxt,\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga072d1719e94e56639abcd385f7cbc692}{ecore\_main\_fd\_handler\_prepare\_callback\_set}}(ctxt.handler,\ \_fd\_prepare\_cb,\ \&ctxt);}
\DoxyCodeLine{\ \ \ ctxt.timer\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(5,\ \_timer\_cb,\ \&ctxt);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Starting\ the\ main\ loop.\ Type\ anything\ and\ hit\ <enter>\ to\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}activate\ the\ fd\_handler\ callback,\ or\ CTRL+d\ to\ shutdown.\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (ctxt.handler)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga259d711cf297b5c2c9dc223005fec583}{ecore\_main\_fd\_handler\_del}}(ctxt.handler);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (ctxt.timer)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga7c118b6a6d232b63a6500e599d2a1b47}{ecore\_timer\_del}}(ctxt.timer);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}

\end{DoxyCodeInclude}
 