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 /* This is the callback function for arriving signals from
35 g_file_monitor. It shall create a Lisp event, and put it into
38 dir_monitor_callback (GFileMonitor
*monitor
,
41 GFileMonitorEvent event_type
,
44 Lisp_Object symbol
, monitor_object
, watch_object
;
45 char *name
= g_file_get_parse_name (file
);
46 char *oname
= other_file
? g_file_get_parse_name (other_file
) : NULL
;
48 /* Determine event symbol. */
51 case G_FILE_MONITOR_EVENT_CHANGED
:
54 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
:
55 symbol
= Qchanges_done_hint
;
57 case G_FILE_MONITOR_EVENT_DELETED
:
60 case G_FILE_MONITOR_EVENT_CREATED
:
63 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED
:
64 symbol
= Qattribute_changed
;
66 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT
:
67 symbol
= Qpre_unmount
;
69 case G_FILE_MONITOR_EVENT_UNMOUNTED
:
72 case G_FILE_MONITOR_EVENT_MOVED
:
79 /* Determine callback function. */
80 monitor_object
= make_pointer_integer (monitor
);
81 eassert (INTEGERP (monitor_object
));
82 watch_object
= assq_no_quit (monitor_object
, watch_list
);
84 if (CONSP (watch_object
))
86 /* Construct an event. */
87 struct input_event event
;
88 Lisp_Object otail
= oname
? list1 (build_string (oname
)) : Qnil
;
90 event
.kind
= FILE_NOTIFY_EVENT
;
91 event
.frame_or_window
= Qnil
;
92 event
.arg
= list2 (Fcons (monitor_object
,
94 Fcons (build_string (name
),
98 /* Store it into the input event queue. */
99 kbd_buffer_store_event (&event
);
110 DEFUN ("gfile-add-watch", Fgfile_add_watch
, Sgfile_add_watch
, 3, 3, 0,
111 doc
: /* Add a watch for filesystem events pertaining to FILE.
113 This arranges for filesystem events pertaining to FILE to be reported
114 to Emacs. Use `gfile-rm-watch' to cancel the watch.
116 Value is a descriptor for the added watch. If the file cannot be
117 watched for some reason, this function signals a `file-notify-error' error.
119 FLAGS is a list of conditions to set what will be watched for. It can
120 include the following symbols:
122 `watch-mounts' -- watch for mount events
123 `send-moved' -- pair `deleted' and `created' events caused by file
124 renames and send a single `renamed' event instead
126 When any event happens, Emacs will call the CALLBACK function passing
127 it a single argument EVENT, which is of the form
129 (DESCRIPTOR ACTION FILE [FILE1])
131 DESCRIPTOR is the same object as the one returned by this function.
132 ACTION is the description of the event. It could be any one of the
135 `changed' -- FILE has changed
136 `changes-done-hint' -- a hint that this was probably the last change
138 `deleted' -- FILE was deleted
139 `created' -- FILE was created
140 `attribute-changed' -- a FILE attribute was changed
141 `pre-unmount' -- the FILE location will soon be unmounted
142 `unmounted' -- the FILE location was unmounted
143 `moved' -- FILE was moved to FILE1
145 FILE is the name of the file whose event is being reported. FILE1
146 will be reported only in case of the `moved' event. */)
147 (Lisp_Object file
, Lisp_Object flags
, Lisp_Object callback
)
149 Lisp_Object watch_object
;
151 GFileMonitor
*monitor
;
152 GFileMonitorFlags gflags
= G_FILE_MONITOR_NONE
;
153 GError
*gerror
= NULL
;
155 /* Check parameters. */
157 file
= Fdirectory_file_name (Fexpand_file_name (file
, Qnil
));
158 if (NILP (Ffile_exists_p (file
)))
159 report_file_error ("File does not exist", file
);
163 if (!FUNCTIONP (callback
))
164 wrong_type_argument (Qinvalid_function
, callback
);
166 /* Create GFile name. */
167 gfile
= g_file_new_for_path (SSDATA (ENCODE_FILE (file
)));
169 /* Assemble flags. */
170 if (!NILP (Fmember (Qwatch_mounts
, flags
)))
171 gflags
|= G_FILE_MONITOR_WATCH_MOUNTS
;
172 if (!NILP (Fmember (Qsend_moved
, flags
)))
173 gflags
|= G_FILE_MONITOR_SEND_MOVED
;
176 monitor
= g_file_monitor (gfile
, gflags
, NULL
, &gerror
);
180 strcpy (msg
, gerror
->message
);
181 g_error_free (gerror
);
182 xsignal1 (Qfile_notify_error
, build_string (msg
));
185 xsignal2 (Qfile_notify_error
, build_string ("Cannot watch file"), file
);
187 Lisp_Object watch_descriptor
= make_pointer_integer (monitor
);
189 /* Check the dicey assumption that make_pointer_integer is safe. */
190 if (! INTEGERP (watch_descriptor
))
192 g_object_unref (monitor
);
193 xsignal2 (Qfile_notify_error
, build_string ("Unsupported file watcher"),
197 g_signal_connect (monitor
, "changed",
198 (GCallback
) dir_monitor_callback
, NULL
);
200 /* Store watch object in watch list. */
201 watch_object
= Fcons (watch_descriptor
, callback
);
202 watch_list
= Fcons (watch_object
, watch_list
);
204 return watch_descriptor
;
207 DEFUN ("gfile-rm-watch", Fgfile_rm_watch
, Sgfile_rm_watch
, 1, 1, 0,
208 doc
: /* Remove an existing WATCH-DESCRIPTOR.
210 WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'. */)
211 (Lisp_Object watch_descriptor
)
213 Lisp_Object watch_object
= assq_no_quit (watch_descriptor
, watch_list
);
215 if (! CONSP (watch_object
))
216 xsignal2 (Qfile_notify_error
, build_string ("Not a watch descriptor"),
219 eassert (INTEGERP (watch_descriptor
));
220 GFileMonitor
*monitor
= XINTPTR (watch_descriptor
);
221 if (!g_file_monitor_cancel (monitor
))
222 xsignal2 (Qfile_notify_error
, build_string ("Could not rm watch"),
225 /* Remove watch descriptor from watch list. */
226 watch_list
= Fdelq (watch_object
, watch_list
);
229 g_object_unref (monitor
);
236 globals_of_gfilenotify (void)
238 #if ! GLIB_CHECK_VERSION (2, 36, 0)
245 syms_of_gfilenotify (void)
247 defsubr (&Sgfile_add_watch
);
248 defsubr (&Sgfile_rm_watch
);
250 /* Filter objects. */
251 DEFSYM (Qwatch_mounts
, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS */
252 DEFSYM (Qsend_moved
, "send-moved"); /* G_FILE_MONITOR_SEND_MOVED */
255 DEFSYM (Qchanged
, "changed"); /* G_FILE_MONITOR_EVENT_CHANGED */
256 DEFSYM (Qchanges_done_hint
, "changes-done-hint");
257 /* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT */
258 DEFSYM (Qdeleted
, "deleted"); /* G_FILE_MONITOR_EVENT_DELETED */
259 DEFSYM (Qcreated
, "created"); /* G_FILE_MONITOR_EVENT_CREATED */
260 DEFSYM (Qattribute_changed
, "attribute-changed");
261 /* G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED */
262 DEFSYM (Qpre_unmount
, "pre-unmount"); /* G_FILE_MONITOR_EVENT_PRE_UNMOUNT */
263 DEFSYM (Qunmounted
, "unmounted"); /* G_FILE_MONITOR_EVENT_UNMOUNTED */
264 DEFSYM (Qmoved
, "moved"); /* G_FILE_MONITOR_EVENT_MOVED */
266 staticpro (&watch_list
);
268 Fprovide (intern_c_string ("gfilenotify"), Qnil
);
272 #endif /* HAVE_GFILENOTIFY */