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
;
154 /* Check parameters. */
156 file
= Fdirectory_file_name (Fexpand_file_name (file
, Qnil
));
157 if (NILP (Ffile_exists_p (file
)))
158 report_file_error ("File does not exist", file
);
162 if (!FUNCTIONP (callback
))
163 wrong_type_argument (Qinvalid_function
, callback
);
165 /* Create GFile name. */
166 gfile
= g_file_new_for_path (SSDATA (ENCODE_FILE (file
)));
168 /* Assemble flags. */
169 if (!NILP (Fmember (Qwatch_mounts
, flags
)))
170 gflags
|= G_FILE_MONITOR_WATCH_MOUNTS
;
171 if (!NILP (Fmember (Qsend_moved
, flags
)))
172 gflags
|= G_FILE_MONITOR_SEND_MOVED
;
175 monitor
= g_file_monitor (gfile
, gflags
, NULL
, NULL
);
177 xsignal2 (Qfile_notify_error
, build_string ("Cannot watch file"), file
);
179 Lisp_Object watch_descriptor
= make_pointer_integer (monitor
);
181 /* Check the dicey assumption that make_pointer_integer is safe. */
182 if (! INTEGERP (watch_descriptor
))
184 g_object_unref (monitor
);
185 xsignal2 (Qfile_notify_error
, build_string ("Unsupported file watcher"),
189 g_signal_connect (monitor
, "changed",
190 (GCallback
) dir_monitor_callback
, NULL
);
192 /* Store watch object in watch list. */
193 watch_object
= Fcons (watch_descriptor
, callback
);
194 watch_list
= Fcons (watch_object
, watch_list
);
196 return watch_descriptor
;
199 DEFUN ("gfile-rm-watch", Fgfile_rm_watch
, Sgfile_rm_watch
, 1, 1, 0,
200 doc
: /* Remove an existing WATCH-DESCRIPTOR.
202 WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'. */)
203 (Lisp_Object watch_descriptor
)
205 Lisp_Object watch_object
= assq_no_quit (watch_descriptor
, watch_list
);
207 if (! CONSP (watch_object
))
208 xsignal2 (Qfile_notify_error
, build_string ("Not a watch descriptor"),
211 eassert (INTEGERP (watch_descriptor
));
212 GFileMonitor
*monitor
= XINTPTR (watch_descriptor
);
213 if (!g_file_monitor_cancel (monitor
))
214 xsignal2 (Qfile_notify_error
, build_string ("Could not rm watch"),
217 /* Remove watch descriptor from watch list. */
218 watch_list
= Fdelq (watch_object
, watch_list
);
221 g_object_unref (monitor
);
228 globals_of_gfilenotify (void)
230 #if ! GLIB_CHECK_VERSION (2, 36, 0)
237 syms_of_gfilenotify (void)
239 DEFSYM (Qgfile_add_watch
, "gfile-add-watch");
240 defsubr (&Sgfile_add_watch
);
242 DEFSYM (Qgfile_rm_watch
, "gfile-rm-watch");
243 defsubr (&Sgfile_rm_watch
);
245 /* Filter objects. */
246 DEFSYM (Qwatch_mounts
, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS */
247 DEFSYM (Qsend_moved
, "send-moved"); /* G_FILE_MONITOR_SEND_MOVED */
250 DEFSYM (Qchanged
, "changed"); /* G_FILE_MONITOR_EVENT_CHANGED */
251 DEFSYM (Qchanges_done_hint
, "changes-done-hint");
252 /* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT */
253 DEFSYM (Qdeleted
, "deleted"); /* G_FILE_MONITOR_EVENT_DELETED */
254 DEFSYM (Qcreated
, "created"); /* G_FILE_MONITOR_EVENT_CREATED */
255 DEFSYM (Qattribute_changed
, "attribute-changed");
256 /* G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED */
257 DEFSYM (Qpre_unmount
, "pre-unmount"); /* G_FILE_MONITOR_EVENT_PRE_UNMOUNT */
258 DEFSYM (Qunmounted
, "unmounted"); /* G_FILE_MONITOR_EVENT_UNMOUNTED */
259 DEFSYM (Qmoved
, "moved"); /* G_FILE_MONITOR_EVENT_MOVED */
261 staticpro (&watch_list
);
263 Fprovide (intern_c_string ("gfilenotify"), Qnil
);
267 #endif /* HAVE_GFILENOTIFY */