1 /* Filesystem notifications support with glib API.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #ifdef HAVE_GFILENOTIFY
26 #include "termhooks.h"
30 /* This is a list, elements are triples (DESCRIPTOR FILE FLAGS CALLBACK) */
31 static Lisp_Object watch_list
;
33 /* This is the callback function for arriving signals from
34 g_file_monitor. It shall create a Lisp event, and put it into
37 dir_monitor_callback (GFileMonitor
*monitor
,
40 GFileMonitorEvent event_type
,
43 Lisp_Object symbol
, monitor_object
, watch_object
, flags
;
44 char *name
= g_file_get_parse_name (file
);
45 char *oname
= other_file
? g_file_get_parse_name (other_file
) : NULL
;
47 /* Determine event symbol. */
50 case G_FILE_MONITOR_EVENT_CHANGED
:
53 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
:
54 symbol
= Qchanges_done_hint
;
56 case G_FILE_MONITOR_EVENT_DELETED
:
59 case G_FILE_MONITOR_EVENT_CREATED
:
62 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED
:
63 symbol
= Qattribute_changed
;
65 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT
:
66 symbol
= Qpre_unmount
;
68 case G_FILE_MONITOR_EVENT_UNMOUNTED
:
71 case G_FILE_MONITOR_EVENT_MOVED
:
78 /* Determine callback function. */
79 monitor_object
= make_pointer_integer (monitor
);
80 eassert (INTEGERP (monitor_object
));
81 watch_object
= assq_no_quit (monitor_object
, watch_list
);
83 if (CONSP (watch_object
))
85 struct input_event event
;
86 Lisp_Object otail
= oname
? list1 (build_string (oname
)) : Qnil
;
88 /* Check, whether event_type is expected. */
89 flags
= XCAR (XCDR (XCDR (watch_object
)));
90 if ((!NILP (Fmember (Qchange
, flags
)) &&
91 !NILP (Fmember (symbol
, list5 (Qchanged
, Qchanges_done_hint
,
92 Qdeleted
, Qcreated
, Qmoved
)))) ||
93 (!NILP (Fmember (Qattribute_change
, flags
)) &&
94 ((EQ (symbol
, Qattribute_changed
)))))
96 /* Construct an event. */
98 event
.kind
= FILE_NOTIFY_EVENT
;
99 event
.frame_or_window
= Qnil
;
100 event
.arg
= list2 (Fcons (monitor_object
,
102 Fcons (build_string (name
),
104 XCAR (XCDR (XCDR (XCDR (watch_object
)))));
106 /* Store it into the input event queue. */
107 kbd_buffer_store_event (&event
);
108 // XD_DEBUG_MESSAGE ("%s", XD_OBJECT_TO_STRING (event.arg));
111 /* Cancel monitor if file or directory is deleted. */
112 if (!NILP (Fmember (symbol
, list2 (Qdeleted
, Qmoved
))) &&
113 !g_file_monitor_is_cancelled (monitor
))
114 g_file_monitor_cancel (monitor
);
125 DEFUN ("gfile-add-watch", Fgfile_add_watch
, Sgfile_add_watch
, 3, 3, 0,
126 doc
: /* Add a watch for filesystem events pertaining to FILE.
128 This arranges for filesystem events pertaining to FILE to be reported
129 to Emacs. Use `gfile-rm-watch' to cancel the watch.
131 Value is a descriptor for the added watch. If the file cannot be
132 watched for some reason, this function signals a `file-notify-error' error.
134 FLAGS is a list of conditions to set what will be watched for. It can
135 include the following symbols:
137 `change' -- watch for file changes
138 `attribute-change' -- watch for file attributes changes, like
139 permissions or modification time
140 `watch-mounts' -- watch for mount events
141 `send-moved' -- pair `deleted' and `created' events caused by
142 file renames and send a single `renamed' event
145 When any event happens, Emacs will call the CALLBACK function passing
146 it a single argument EVENT, which is of the form
148 (DESCRIPTOR ACTION FILE [FILE1])
150 DESCRIPTOR is the same object as the one returned by this function.
151 ACTION is the description of the event. It could be any one of the
154 `changed' -- FILE has changed
155 `changes-done-hint' -- a hint that this was probably the last change
157 `deleted' -- FILE was deleted
158 `created' -- FILE was created
159 `attribute-changed' -- a FILE attribute was changed
160 `pre-unmount' -- the FILE location will soon be unmounted
161 `unmounted' -- the FILE location was unmounted
162 `moved' -- FILE was moved to FILE1
164 FILE is the name of the file whose event is being reported. FILE1
165 will be reported only in case of the `moved' event. */)
166 (Lisp_Object file
, Lisp_Object flags
, Lisp_Object callback
)
168 Lisp_Object watch_object
;
170 GFileMonitor
*monitor
;
171 GFileMonitorFlags gflags
= G_FILE_MONITOR_NONE
;
172 GError
*gerror
= NULL
;
174 /* Check parameters. */
176 file
= Fdirectory_file_name (Fexpand_file_name (file
, Qnil
));
177 if (NILP (Ffile_exists_p (file
)))
178 report_file_error ("File does not exist", file
);
182 if (!FUNCTIONP (callback
))
183 wrong_type_argument (Qinvalid_function
, callback
);
185 /* Create GFile name. */
186 gfile
= g_file_new_for_path (SSDATA (ENCODE_FILE (file
)));
188 /* Assemble flags. */
189 if (!NILP (Fmember (Qwatch_mounts
, flags
)))
190 gflags
|= G_FILE_MONITOR_WATCH_MOUNTS
;
191 if (!NILP (Fmember (Qsend_moved
, flags
)))
192 gflags
|= G_FILE_MONITOR_SEND_MOVED
;
195 monitor
= g_file_monitor (gfile
, gflags
, NULL
, &gerror
);
196 g_object_unref (gfile
);
200 strcpy (msg
, gerror
->message
);
201 g_error_free (gerror
);
202 xsignal1 (Qfile_notify_error
, build_string (msg
));
205 xsignal2 (Qfile_notify_error
, build_string ("Cannot watch file"), file
);
207 Lisp_Object watch_descriptor
= make_pointer_integer (monitor
);
209 /* Check the dicey assumption that make_pointer_integer is safe. */
210 if (! INTEGERP (watch_descriptor
))
212 g_object_unref (monitor
);
213 xsignal2 (Qfile_notify_error
, build_string ("Unsupported file watcher"),
217 /* The default rate limit is 800 msec. We adapt this. */
218 g_file_monitor_set_rate_limit (monitor
, 100);
220 /* Subscribe to the "changed" signal. */
221 g_signal_connect (monitor
, "changed",
222 (GCallback
) dir_monitor_callback
, NULL
);
224 /* Store watch object in watch list. */
225 watch_object
= list4 (watch_descriptor
, file
, flags
, callback
);
226 watch_list
= Fcons (watch_object
, watch_list
);
228 return watch_descriptor
;
231 DEFUN ("gfile-rm-watch", Fgfile_rm_watch
, Sgfile_rm_watch
, 1, 1, 0,
232 doc
: /* Remove an existing WATCH-DESCRIPTOR.
234 WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'. */)
235 (Lisp_Object watch_descriptor
)
237 Lisp_Object watch_object
= assq_no_quit (watch_descriptor
, watch_list
);
239 if (! CONSP (watch_object
))
240 xsignal2 (Qfile_notify_error
, build_string ("Not a watch descriptor"),
243 eassert (INTEGERP (watch_descriptor
));
244 GFileMonitor
*monitor
= XINTPTR (watch_descriptor
);
245 if (!g_file_monitor_is_cancelled (monitor
) &&
246 !g_file_monitor_cancel (monitor
))
247 xsignal2 (Qfile_notify_error
, build_string ("Could not rm watch"),
250 /* Remove watch descriptor from watch list. */
251 watch_list
= Fdelq (watch_object
, watch_list
);
254 g_object_unref (monitor
);
259 DEFUN ("gfile-valid-p", Fgfile_valid_p
, Sgfile_valid_p
, 1, 1, 0,
260 doc
: /* "Check a watch specified by its WATCH-DESCRIPTOR.
262 WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'.
264 A watch can become invalid if the file or directory it watches is
265 deleted, or if the watcher thread exits abnormally for any other
266 reason. Removing the watch by calling `gfile-rm-watch' also makes it
268 (Lisp_Object watch_descriptor
)
270 Lisp_Object watch_object
= Fassoc (watch_descriptor
, watch_list
);
271 if (NILP (watch_object
))
275 GFileMonitor
*monitor
= XINTPTR (watch_descriptor
);
276 return g_file_monitor_is_cancelled (monitor
) ? Qnil
: Qt
;
282 globals_of_gfilenotify (void)
284 #if ! GLIB_CHECK_VERSION (2, 36, 0)
291 syms_of_gfilenotify (void)
293 defsubr (&Sgfile_add_watch
);
294 defsubr (&Sgfile_rm_watch
);
295 defsubr (&Sgfile_valid_p
);
297 /* Filter objects. */
298 DEFSYM (Qchange
, "change");
299 DEFSYM (Qattribute_change
, "attribute-change");
300 DEFSYM (Qwatch_mounts
, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS */
301 DEFSYM (Qsend_moved
, "send-moved"); /* G_FILE_MONITOR_SEND_MOVED */
304 DEFSYM (Qchanged
, "changed"); /* G_FILE_MONITOR_EVENT_CHANGED */
305 DEFSYM (Qchanges_done_hint
, "changes-done-hint");
306 /* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT */
307 DEFSYM (Qdeleted
, "deleted"); /* G_FILE_MONITOR_EVENT_DELETED */
308 DEFSYM (Qcreated
, "created"); /* G_FILE_MONITOR_EVENT_CREATED */
309 DEFSYM (Qattribute_changed
, "attribute-changed");
310 /* G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED */
311 DEFSYM (Qpre_unmount
, "pre-unmount"); /* G_FILE_MONITOR_EVENT_PRE_UNMOUNT */
312 DEFSYM (Qunmounted
, "unmounted"); /* G_FILE_MONITOR_EVENT_UNMOUNTED */
313 DEFSYM (Qmoved
, "moved"); /* G_FILE_MONITOR_EVENT_MOVED */
315 staticpro (&watch_list
);
317 Fprovide (intern_c_string ("gfilenotify"), Qnil
);
321 #endif /* HAVE_GFILENOTIFY */