32c1b20c3c93f4836e465eea5a80d77247dd0816
[framework/uifw/ecore.git] / src / lib / ecore_file / ecore_file_monitor.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include "ecore_file_private.h"
10
11 static int init = 0;
12
13 typedef enum {
14      ECORE_FILE_MONITOR_TYPE_NONE,
15 #ifdef HAVE_INOTIFY
16      ECORE_FILE_MONITOR_TYPE_INOTIFY,
17 #endif
18 #ifdef HAVE_POLL
19      ECORE_FILE_MONITOR_TYPE_POLL
20 #endif
21 } Ecore_File_Monitor_Type;
22
23 static Ecore_File_Monitor_Type monitor_type = ECORE_FILE_MONITOR_TYPE_NONE;
24
25 int
26 ecore_file_monitor_init(void)
27 {
28    if (++init != 1) return init;
29
30 #ifdef HAVE_INOTIFY
31    monitor_type = ECORE_FILE_MONITOR_TYPE_INOTIFY;
32    if (ecore_file_monitor_inotify_init())
33      return init;
34 #endif
35 #ifdef HAVE_POLL
36    monitor_type = ECORE_FILE_MONITOR_TYPE_POLL;
37    if (ecore_file_monitor_poll_init())
38      return init;
39 #endif
40    monitor_type = ECORE_FILE_MONITOR_TYPE_NONE;
41    return --init;
42 }
43
44 int
45 ecore_file_monitor_shutdown(void)
46 {
47    if (--init != 0) return init;
48
49    switch (monitor_type)
50      {
51       case ECORE_FILE_MONITOR_TYPE_NONE:
52          break;
53 #ifdef HAVE_INOTIFY
54       case ECORE_FILE_MONITOR_TYPE_INOTIFY:
55          ecore_file_monitor_inotify_shutdown();
56          break;
57 #endif
58 #ifdef HAVE_POLL
59       case ECORE_FILE_MONITOR_TYPE_POLL:
60          ecore_file_monitor_poll_shutdown();
61          break;
62 #endif
63      }
64    return init;
65 }
66
67 /**
68  * Monitor a path using inotify or polling
69  * @param  path The path to monitor
70  * @param  func The function to call on changes
71  * @param  data The data passed to func
72  * @return An Ecore_File_Monitor pointer or NULL on failure
73  */
74 EAPI Ecore_File_Monitor *
75 ecore_file_monitor_add(const char *path,
76                 void (*func) (void *data, Ecore_File_Monitor *em,
77                       Ecore_File_Event event,
78                       const char *path),
79                 void *data)
80 {
81    switch (monitor_type)
82      {
83       case ECORE_FILE_MONITOR_TYPE_NONE:
84          return NULL;
85 #ifdef HAVE_INOTIFY
86       case ECORE_FILE_MONITOR_TYPE_INOTIFY:
87          return ecore_file_monitor_inotify_add(path, func, data);
88 #endif
89 #ifdef HAVE_POLL
90       case ECORE_FILE_MONITOR_TYPE_POLL:
91          return ecore_file_monitor_poll_add(path, func, data);
92 #endif
93      }
94    return NULL;
95 }
96
97 /**
98  * Stop monitoring a path
99  * @param  em The Ecore_File_Monitor to stop
100  */
101 EAPI void
102 ecore_file_monitor_del(Ecore_File_Monitor *em)
103 {
104    switch (monitor_type)
105      {
106       case ECORE_FILE_MONITOR_TYPE_NONE:
107          break;
108 #ifdef HAVE_INOTIFY
109       case ECORE_FILE_MONITOR_TYPE_INOTIFY:
110          ecore_file_monitor_inotify_del(em);
111          break;
112 #endif
113 #ifdef HAVE_POLL
114       case ECORE_FILE_MONITOR_TYPE_POLL:
115          ecore_file_monitor_poll_del(em);
116          break;
117 #endif
118      }
119 }
120
121 /**
122  * Get the monitored path
123  * @param  em The Ecore_File_Monitor to query
124  * @return The path that is monitored by @p em
125  */
126 EAPI const char *
127 ecore_file_monitor_path_get(Ecore_File_Monitor *em)
128 {
129    return em->path;
130 }