eio_monitor_add() tutorial

To use eio_monitor_add(), you have to define callbacks for events declared by eio.

Available events are :

- EIO_MONITOR_FILE_CREATED
- EIO_MONITOR_FILE_DELETED
- EIO_MONITOR_FILE_MODIFIED
- EIO_MONITOR_FILE_CLOSED
- EIO_MONITOR_DIRECTORY_CREATED
- EIO_MONITOR_DIRECTORY_DELETED
- EIO_MONITOR_DIRECTORY_CLOSED
- EIO_MONITOR_SELF_RENAME
- EIO_MONITOR_SELF_DELETED

As nothing is worth an example, here it is :

#include <Eina.h>
#include <Ecore.h>
#include <Eio.h>
void file_modified(void *data, int type, void *event)
{
const char *filename = (const char *)data;
printf("file %s ", filename);
if( type == EIO_MONITOR_FILE_MODIFIED )
printf("is being modified");
else if( type == EIO_MONITOR_FILE_CLOSED )
printf("is no longer being modified");
else printf("got unexpected changes");
printf("\n");
}
int main(int argc, char **argv) {
Eio_Monitor *monitor = NULL,
*monitor2 = NULL;
eio_init();
const char *filename = eina_stringshare_add("/tmp/eio_notify_testfile");
monitor = eio_monitor_add(filename);
ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, (Ecore_Event_Handler_Cb)file_modified, filename);
ecore_event_handler_add(EIO_MONITOR_FILE_CLOSED, (Ecore_Event_Handler_Cb)file_modified, filename);
ecore_main_loop_begin();
eio_shutdown();
eina_stringshare_del(filename);
}
Eina.h
Eina Utility library.
Ecore_Event_Handler_Cb
Eina_Bool(* Ecore_Event_Handler_Cb)(void *data, int type, void *event)
A callback used by the main loop to handle events of a specified type.
Definition Ecore_Common.h:603
ecore_event_handler_add
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition ecore_events.c:13
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1311
eina_stringshare_add
EINA_API Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition eina_stringshare.c:606
eina_stringshare_del
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition eina_stringshare.c:533
eio_init
EIO_API int eio_init(void)
Initialize eio and all its required submodule.
Definition eio_main.c:276
eio_shutdown
EIO_API int eio_shutdown(void)
Shutdown eio and all its submodule if possible.
Definition eio_main.c:340
EIO_MONITOR_FILE_MODIFIED
EIO_API int EIO_MONITOR_FILE_MODIFIED
A file was modified in a watched directory.
Definition eio_monitor.c:308
EIO_MONITOR_FILE_CLOSED
EIO_API int EIO_MONITOR_FILE_CLOSED
A file was closed in a watched directory.
Definition eio_monitor.c:309
eio_monitor_add
EIO_API Eio_Monitor * eio_monitor_add(const char *path)
Adds a file/directory to monitor (inotify mechanism).
Definition eio_monitor.c:318

Build the example doing :

    gcc -o tutorial_monitor_add tutorial_monitor_add.c `pkg-config --libs --cflags eio ecore ecore-file eina`
    * then create the file /tmp/eio_notify_testfile :
    * touch /tmp/eio_notify_testfile
    * and launch tutorial_monitor_add, and in another terminal, write into /tmp/eio_notify_testfile, doing for example :
    * echo "test" >> /tmp/eio_notify_testfile
    *