\hypertarget{ecore_fd_handler_example_8c-example}{}\section{ecore\+\_\+fd\+\_\+handler\+\_\+example.\+c}
This example shows how to setup and use an fd\+\_\+handler. See \hyperlink{ecore_fd_handler_example_c}{the explanation here}.


\begin{DoxyCodeInclude}
\textcolor{comment}{/*}
\textcolor{comment}{ * gcc -o ecore\_fd\_handler\_example ecore\_fd\_handler\_example.c `pkg-config --cflags --libs ecore`}
\textcolor{comment}{ */}

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

\textcolor{keyword}{struct }context
\{
   \hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler} *handler;
   \hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}      *timer;
\};

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_fd\_prepare\_cb(\textcolor{keywordtype}{void} *data \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler} *handler EINA\_UNUSED)
\{
   printf(\textcolor{stringliteral}{"prepare\_cb called.\(\backslash\)n"});
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_fd\_handler\_cb(\textcolor{keywordtype}{void} *data, \hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler} *handler)
\{
   \textcolor{keyword}{struct }context *ctxt = data;
   \textcolor{keywordtype}{char} buf[1024];
   \textcolor{keywordtype}{size\_t} nbytes;
   \textcolor{keywordtype}{int} fd;

   \textcolor{keywordflow}{if} (\hyperlink{group__Ecore__FD__Handler__Group_gabd3040bd39c95723e42a8c48484b87de}{ecore\_main\_fd\_handler\_active\_get}(handler, 
      \hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a829d91235e3ca5eba61330c96a8074de}{ECORE\_FD\_ERROR}))
     \{
        printf(\textcolor{stringliteral}{"An error has occurred. Stop watching this fd and quit.\(\backslash\)n"});
        \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
        ctxt->handler = NULL;
        \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\_CALLBACK\_CANCEL};
     \}

   fd = \hyperlink{group__Ecore__FD__Handler__Group_ga70d418717552a5258c7dfc1153250e7d}{ecore\_main\_fd\_handler\_fd\_get}(handler);
   nbytes = read(fd, buf, \textcolor{keyword}{sizeof}(buf));
   \textcolor{keywordflow}{if} (nbytes == 0)
     \{
        printf(\textcolor{stringliteral}{"Nothing to read, exiting...\(\backslash\)n"});
        \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
        ctxt->handler = NULL;
        \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_gae0c653baad8836232c0dc27f91f875d6}{ECORE\_CALLBACK\_CANCEL};
     \}
   buf[nbytes - 1] = \textcolor{charliteral}{'\(\backslash\)0'};

   printf(\textcolor{stringliteral}{"Read %zd bytes from input: \(\backslash\)"%s\(\backslash\)"\(\backslash\)n"}, nbytes - 1, buf);

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_timer\_cb(\textcolor{keywordtype}{void} *data \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED})
\{
   printf(\textcolor{stringliteral}{"Timer expired after 5 seconds...\(\backslash\)n"});

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \textcolor{keyword}{struct }context ctxt = \{0\};

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

   ctxt.handler = \hyperlink{group__Ecore__FD__Handler__Group_ga832e68eb7f15912abeadef816bb5d2f5}{ecore\_main\_fd\_handler\_add}(STDIN\_FILENO,
                                            \hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ} | 
      \hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a829d91235e3ca5eba61330c96a8074de}{ECORE\_FD\_ERROR},
                                            \_fd\_handler\_cb,
                                            &ctxt, NULL, NULL);
   \hyperlink{group__Ecore__FD__Handler__Group_ga072d1719e94e56639abcd385f7cbc692}{ecore\_main\_fd\_handler\_prepare\_callback\_set}(ctxt.handler, 
      \_fd\_prepare\_cb, &ctxt);
   ctxt.timer = \hyperlink{group__Ecore__Timer__Group_ga7b9cb9d24ecebfdbb957436e2e669402}{ecore\_timer\_add}(5, \_timer\_cb, &ctxt);

   printf(\textcolor{stringliteral}{"Starting the main loop. Type anything and hit <enter> to "}
          \textcolor{stringliteral}{"activate the fd\_handler callback, or CTRL+d to shutdown.\(\backslash\)n"});

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

   \textcolor{keywordflow}{if} (ctxt.handler)
     \hyperlink{group__Ecore__FD__Handler__Group_gaf92ef3a4a006a02e67b9806276f7312c}{ecore\_main\_fd\_handler\_del}(ctxt.handler);

   \textcolor{keywordflow}{if} (ctxt.timer)
     \hyperlink{group__Ecore__Timer__Group_gaba2b599c7aabe3ab664dc3d47f62a33a}{ecore\_timer\_del}(ctxt.timer);

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

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

\end{DoxyCodeInclude}
 