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)
145 if (el.data.count == 0)
146 requests.delete_link(el);
148 if (requests != null)
156 public abstract class ScriptService : Service
158 protected string script;
160 protected bool script_start()
163 zavai.config.find_and_run_script(name, "start");
166 zavai.log.error("Running " + name + " start: " + e.message);
171 protected bool script_stop()
174 zavai.config.find_and_run_script(name, "stop");
177 zavai.log.error("Running " + name + " stop: " + e.message);
182 protected bool script_status()
186 string script = zavai.config.find_script(name);
189 zavai.log.error("Hook " + name + " does not exist");
192 string command = script + " status";
194 int res = zavai.config.run_script_sync(command, out std_out, out std_err);
197 zavai.log.error("Running " + command + ": " + std_err);
200 } catch (SpawnError e) {
201 zavai.log.error("Running " + command + ": " + e.message);
207 return (std_out == "on");
211 public abstract class ScriptMonitorService : Service
213 protected Pid child_pid;
214 protected int child_watch_id;
216 ScriptMonitorService()
222 protected bool script_start()
224 string script = zavai.config.find_script(name);
225 zavai.log.info("Run program: " + script + " pre");
227 // Then run our own script
228 zavai.config.run_script(script + " pre");
230 zavai.log.error("Running " + script + " pre: " + e.message);
234 zavai.log.info("Run program: " + script + " run");
235 string[] args = { script, "run" };
238 Environment.get_home_dir(),
241 SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
244 } catch (SpawnError e) {
245 zavai.log.error("Running " + script + " run: " + e.message);
249 // Add a child watch source to know when it ends
250 ChildWatch.add(child_pid, on_child);
255 protected bool script_stop()
257 Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
261 protected void on_child(Pid pid, int status)
263 zavai.log.info("Exited");
264 stderr.printf("STATUS %d\n", status);
265 Process.close_pid(pid);
268 // Then run our own script
269 zavai.config.find_and_run_script(name, "post");
271 zavai.log.error("Running " + name + " post: " + e.message);
275 cleanup_after_script_stop();
280 protected virtual void cleanup_after_script_stop()
285 protected bool script_status()