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
27 #include "termhooks.h"
32 static Lisp_Object watch_list
;
34 /* Convert a monitor to a Lisp integer and back. On all known glib
35 platforms, converting the sum of MONITOR and Lisp_Int0 directly to
36 a Lisp_Object value results in a Lisp integer, which is safe. */
39 monitor_to_lisp (GFileMonitor
*monitor
)
41 return XIL ((intptr_t) monitor
+ Lisp_Int0
);
45 lisp_to_monitor (Lisp_Object watch_descriptor
)
47 intptr_t int_monitor
= XLI (watch_descriptor
) - Lisp_Int0
;
48 return (GFileMonitor
*) int_monitor
;
51 /* This is the callback function for arriving signals from
52 g_file_monitor. It shall create a Lisp event, and put it into
55 dir_monitor_callback (GFileMonitor
*monitor
,
58 GFileMonitorEvent event_type
,
61 Lisp_Object symbol
, monitor_object
, watch_object
;
62 char *name
= g_file_get_parse_name (file
);
63 char *oname
= other_file
? g_file_get_parse_name (other_file
) : NULL
;
65 /* Determine event symbol. */
68 case G_FILE_MONITOR_EVENT_CHANGED
:
71 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
:
72 symbol
= Qchanges_done_hint
;
74 case G_FILE_MONITOR_EVENT_DELETED
:
77 case G_FILE_MONITOR_EVENT_CREATED
:
80 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED
:
81 symbol
= Qattribute_changed
;
83 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT
:
84 symbol
= Qpre_unmount
;
86 case G_FILE_MONITOR_EVENT_UNMOUNTED
:
89 case G_FILE_MONITOR_EVENT_MOVED
:
96 /* Determine callback function. */
97 monitor_object
= monitor_to_lisp (monitor
);
98 eassert (INTEGERP (monitor_object
));
99 watch_object
= assq_no_quit (monitor_object
, watch_list
);
101 if (CONSP (watch_object
))
103 /* Construct an event. */
104 struct input_event event
;
105 Lisp_Object otail
= oname
? list1 (build_string (oname
)) : Qnil
;
107 event
.kind
= FILE_NOTIFY_EVENT
;
108 event
.frame_or_window
= Qnil
;
109 event
.arg
= list2 (Fcons (monitor_object
,
111 Fcons (build_string (name
),
113 XCDR (watch_object
));
115 /* Store it into the input event queue. */
116 kbd_buffer_store_event (&event
);
127 DEFUN ("gfile-add-watch", Fgfile_add_watch
, Sgfile_add_watch
, 3, 3, 0,
128 doc
: /* Add a watch for filesystem events pertaining to FILE.
130 This arranges for filesystem events pertaining to FILE to be reported
131 to Emacs. Use `gfile-rm-watch' to cancel the watch.
133 Value is a descriptor for the added watch. If the file cannot be
134 watched for some reason, this function signals a `file-notify-error' error.
136 FLAGS is a list of conditions to set what will be watched for. It can
137 include the following symbols:
139 'watch-mounts' -- watch for mount events
140 'send-moved' -- pair 'deleted' and 'created' events caused by file
141 renames and send a single 'renamed' event instead
143 When any event happens, Emacs will call the CALLBACK function passing
144 it a single argument EVENT, which is of the form
146 (DESCRIPTOR ACTION FILE [FILE1])
148 DESCRIPTOR is the same object as the one returned by this function.
149 ACTION is the description of the event. It could be any one of the
152 'changed' -- FILE has changed
153 'changes-done-hint' -- a hint that this was probably the last change
155 'deleted' -- FILE was deleted
156 'created' -- FILE was created
157 'attribute-changed' -- a FILE attribute was changed
158 'pre-unmount' -- the FILE location will soon be unmounted
159 'unmounted' -- the FILE location was unmounted
160 'moved' -- FILE was moved to FILE1
162 FILE is the name of the file whose event is being reported. FILE1
163 will be reported only in case of the 'moved' event. */)
164 (Lisp_Object file
, Lisp_Object flags
, Lisp_Object callback
)
166 Lisp_Object watch_object
;
168 GFileMonitor
*monitor
;
169 GFileMonitorFlags gflags
= G_FILE_MONITOR_NONE
;
171 /* Check parameters. */
173 file
= Fdirectory_file_name (Fexpand_file_name (file
, Qnil
));
174 if (NILP (Ffile_exists_p (file
)))
175 report_file_error ("File does not exist", file
);
179 if (!FUNCTIONP (callback
))
180 wrong_type_argument (Qinvalid_function
, callback
);
182 /* Create GFile name. */
183 gfile
= g_file_new_for_path (SSDATA (ENCODE_FILE (file
)));
185 /* Assemble flags. */
186 if (!NILP (Fmember (Qwatch_mounts
, flags
)))
187 gflags
|= G_FILE_MONITOR_WATCH_MOUNTS
;
188 if (!NILP (Fmember (Qsend_moved
, flags
)))
189 gflags
|= G_FILE_MONITOR_SEND_MOVED
;
192 monitor
= g_file_monitor (gfile
, gflags
, NULL
, NULL
);
194 xsignal2 (Qfile_notify_error
, build_string ("Cannot watch file"), file
);
196 Lisp_Object watch_descriptor
= monitor_to_lisp (monitor
);
198 /* Check the dicey assumption that monitor_to_lisp is safe. */
199 if (! INTEGERP (watch_descriptor
))
201 g_object_unref (monitor
);
202 xsignal2 (Qfile_notify_error
, build_string ("Unsupported file watcher"),
206 g_signal_connect (monitor
, "changed",
207 (GCallback
) dir_monitor_callback
, NULL
);
209 /* Store watch object in watch list. */
210 watch_object
= Fcons (watch_descriptor
, callback
);
211 watch_list
= Fcons (watch_object
, watch_list
);
213 return watch_descriptor
;
216 DEFUN ("gfile-rm-watch", Fgfile_rm_watch
, Sgfile_rm_watch
, 1, 1, 0,
217 doc
: /* Remove an existing WATCH-DESCRIPTOR.
219 WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'. */)
220 (Lisp_Object watch_descriptor
)
222 Lisp_Object watch_object
= assq_no_quit (watch_descriptor
, watch_list
);
224 if (! CONSP (watch_object
))
225 xsignal2 (Qfile_notify_error
, build_string ("Not a watch descriptor"),
228 eassert (INTEGERP (watch_descriptor
));
229 GFileMonitor
*monitor
= lisp_to_monitor (watch_descriptor
);
230 if (!g_file_monitor_cancel (monitor
))
231 xsignal2 (Qfile_notify_error
, build_string ("Could not rm watch"),
234 /* Remove watch descriptor from watch list. */
235 watch_list
= Fdelq (watch_object
, watch_list
);
238 g_object_unref (monitor
);
245 globals_of_gfilenotify (void)
247 #if ! GLIB_CHECK_VERSION (2, 36, 0)
254 syms_of_gfilenotify (void)
256 DEFSYM (Qgfile_add_watch
, "gfile-add-watch");
257 defsubr (&Sgfile_add_watch
);
259 DEFSYM (Qgfile_rm_watch
, "gfile-rm-watch");
260 defsubr (&Sgfile_rm_watch
);
262 /* Filter objects. */
263 DEFSYM (Qwatch_mounts
, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS */
264 DEFSYM (Qsend_moved
, "send-moved"); /* G_FILE_MONITOR_SEND_MOVED */
267 DEFSYM (Qchanged
, "changed"); /* G_FILE_MONITOR_EVENT_CHANGED */
268 DEFSYM (Qchanges_done_hint
, "changes-done-hint");
269 /* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT */
270 DEFSYM (Qdeleted
, "deleted"); /* G_FILE_MONITOR_EVENT_DELETED */
271 DEFSYM (Qcreated
, "created"); /* G_FILE_MONITOR_EVENT_CREATED */
272 DEFSYM (Qattribute_changed
, "attribute-changed");
273 /* G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED */
274 DEFSYM (Qpre_unmount
, "pre-unmount"); /* G_FILE_MONITOR_EVENT_PRE_UNMOUNT */
275 DEFSYM (Qunmounted
, "unmounted"); /* G_FILE_MONITOR_EVENT_UNMOUNTED */
276 DEFSYM (Qmoved
, "moved"); /* G_FILE_MONITOR_EVENT_MOVED */
278 staticpro (&watch_list
);
280 Fprovide (intern_c_string ("gfilenotify"), Qnil
);
284 #endif /* HAVE_GFILENOTIFY */