2 * app - zavai main window
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 public interface Resource : Object {
27 * Shut down this resource.
29 * Normally one does nothing here, but it is important to give resources a
30 * chance to do cleanup when the program quits.
32 * This can be used for tasks like closing the tags on a GPX track,
33 * releasing a FSO resource, restoring mixer settings and so on.
35 public abstract void shutdown();
38 public abstract class Service : Object, Resource {
39 public string name { get; construct; }
43 get { return _started; }
44 set { _started = value; }
47 public signal void toggled(bool new_state);
49 protected class Request
51 public string requestor;
53 public Request(string requestor)
55 this.requestor = requestor;
60 protected List<Request> requests;
66 zavai.registry.register(this);
69 public void shutdown()
74 /// Activate the service
75 protected virtual void start()
79 zavai.log.info("Service " + name + " started\n");
85 /// Deactivate the service
86 protected virtual void stop()
90 zavai.log.info("Service " + name + " stopped\n");
97 Request a resource using the given ID.
99 * If it is the first time the resource is requested, start it and
100 * return true. Else, take note of the request and return false.
102 * If a resource is requested multiple times with the same ID, it will
103 * need to be released multiple times with that ID.
105 public bool request(string id)
107 bool res = (requests == null);
109 for (weak List<Request> i = requests; i != null; i = i.next)
110 if (i.data.requestor == id)
117 requests.prepend(new Request(id));
123 * Release a resource using the given ID.
125 * If after the call nothing is requesting the resource, stop it and
126 * return true. Else, take note of the release and return false.
128 * If a resource is requested multiple times with the same ID, it will
129 * need to be released multiple times with that ID.
131 public bool release(string id)
133 weak List<Request> el = null;
134 for (weak List<Request> i = requests; i != null; i = i.next)
135 if (i.data.requestor == id)
144 requests.delete_link(el);
146 if (requests != null)
154 public abstract class ScriptService : Service
156 protected string script;
158 protected bool script_start()
160 string script = zavai.config.find_script(name);
163 zavai.log.error("Hook " + name + " does not exist");
167 // Then run our own script
168 zavai.config.run_script(script + " start");
171 zavai.log.error("Running " + script + " start: " + e.message);
176 protected bool script_stop()
178 string script = zavai.config.find_script(name);
181 zavai.log.error("Hook " + name + " does not exist");
185 // Then run our own script
186 zavai.config.run_script(script + " stop");
189 zavai.log.error("Running " + script + " stop: " + e.message);
194 protected bool script_status()
198 string script = zavai.config.find_script(name);
201 zavai.log.error("Hook " + name + " does not exist");
204 string command = script + " status";
205 int res = zavai.config.run_script_sync(command, out std_out, out std_err);
208 zavai.log.error("Running " + command + ": " + std_err);
214 return (std_out == "on");
218 public abstract class ScriptMonitorService : Service
220 protected Pid child_pid;
221 protected int child_watch_id;
223 ScriptMonitorService()
229 protected bool script_start()
231 string command = zavai.config.homedir + "/" + name + " pre";
233 // Then run our own script
234 zavai.config.run_script(command);
236 zavai.log.error("Running " + command + ": " + e.message);
240 command = zavai.config.homedir + "/" + name + " run";
241 zavai.log.info("Run program: " + command);
242 string[] args = command.split(" ");
245 Environment.get_home_dir(),
248 SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
251 } catch (SpawnError e) {
252 zavai.log.error("Running " + command + ": " + e.message);
256 // Add a child watch source to know when it ends
257 ChildWatch.add(child_pid, on_child);
262 protected bool script_stop()
264 Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
268 protected void on_child(Pid pid, int status)
270 zavai.log.info("Exited");
271 stderr.printf("STATUS %d\n", status);
272 Process.close_pid(pid);
274 string command = zavai.config.homedir + "/" + name + " post";
276 // Then run our own script
277 zavai.config.run_script(command);
279 zavai.log.error("Running " + command + ": " + e.message);
283 cleanup_after_script_stop();
288 protected virtual void cleanup_after_script_stop()
293 protected bool script_status()