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