* lisp/progmodes/cc-*.el: Fix up commenting style
[emacs.git] / src / gfilenotify.c
blob5c6ebe65d870971c67338548dc8fbf9fb7f79f1d
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/>. */
19 #include <config.h>
21 #ifdef HAVE_GFILENOTIFY
22 #include <stdio.h>
23 #include <gio/gio.h>
24 #include "lisp.h"
25 #include "coding.h"
26 #include "frame.h"
27 #include "termhooks.h"
28 #include "keyboard.h"
29 #include "process.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
36 Emacs input queue. */
37 static gboolean
38 dir_monitor_callback (GFileMonitor *monitor,
39 GFile *file,
40 GFile *other_file,
41 GFileMonitorEvent event_type,
42 gpointer user_data)
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. */
49 switch (event_type)
51 case G_FILE_MONITOR_EVENT_CHANGED:
52 symbol = Qchanged;
53 break;
54 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
55 symbol = Qchanges_done_hint;
56 break;
57 case G_FILE_MONITOR_EVENT_DELETED:
58 symbol = Qdeleted;
59 break;
60 case G_FILE_MONITOR_EVENT_CREATED:
61 symbol = Qcreated;
62 break;
63 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
64 symbol = Qattribute_changed;
65 break;
66 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
67 symbol = Qpre_unmount;
68 break;
69 case G_FILE_MONITOR_EVENT_UNMOUNTED:
70 symbol = Qunmounted;
71 break;
72 case G_FILE_MONITOR_EVENT_MOVED:
73 symbol = Qmoved;
74 break;
75 default:
76 goto cleanup;
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;
89 EVENT_INIT (event);
90 event.kind = FILE_NOTIFY_EVENT;
91 event.frame_or_window = Qnil;
92 event.arg = list2 (Fcons (monitor_object,
93 Fcons (symbol,
94 Fcons (build_string (name),
95 otail))),
96 XCDR (watch_object));
98 /* Store it into the input event queue. */
99 kbd_buffer_store_event (&event);
102 /* Cleanup. */
103 cleanup:
104 g_free (name);
105 g_free (oname);
107 return TRUE;
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
133 following:
135 `changed' -- FILE has changed
136 `changes-done-hint' -- a hint that this was probably the last change
137 in a set of changes
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;
150 GFile *gfile;
151 GFileMonitor *monitor;
152 GFileMonitorFlags gflags = G_FILE_MONITOR_NONE;
153 GError *gerror = NULL;
155 /* Check parameters. */
156 CHECK_STRING (file);
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);
161 CHECK_LIST (flags);
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;
175 /* Enable watch. */
176 monitor = g_file_monitor (gfile, gflags, NULL, &gerror);
177 if (gerror)
179 char msg[1024];
180 strcpy (msg, gerror->message);
181 g_error_free (gerror);
182 xsignal1 (Qfile_notify_error, build_string (msg));
184 if (! monitor)
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"),
194 file);
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"),
217 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"),
223 watch_descriptor);
225 /* Remove watch descriptor from watch list. */
226 watch_list = Fdelq (watch_object, watch_list);
228 /* Cleanup. */
229 g_object_unref (monitor);
231 return Qt;
235 void
236 globals_of_gfilenotify (void)
238 #if ! GLIB_CHECK_VERSION (2, 36, 0)
239 g_type_init ();
240 #endif
241 watch_list = Qnil;
244 void
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 */
254 /* Event types. */
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 */