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