Find script for gsm
[gregoa/zavai.git] / src / zavai.vala
1 /*
2  * zavai - simple interface to the OpenMoko (or to the FSO stack)
3  *
4  * Copyright (C) 2009--2010  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 //string VERSION = "0.1";
24 static string pidfilename;
25
26 static Posix.pid_t is_running()
27 {
28         string pidfile = zavai.config.homedir + "/" + pidfilename + ".pid";
29         string contents;
30         try {
31                 if (!FileUtils.get_contents(pidfile, out contents))
32                         return 0;
33         } catch (FileError e) {
34                 return 0;
35         }
36         int val = contents.to_int();
37         string procdir = "/proc/%d".printf(val);
38         if (FileUtils.test(procdir, FileTest.IS_DIR))
39                 return (Posix.pid_t)val;
40         else
41                 return 0;
42 }
43
44 static void make_pidfile()
45 {
46         string pidfile = zavai.config.homedir + "/" + pidfilename + ".pid";
47         FileUtils.set_contents(pidfile, "%d".printf(Posix.getpid()));
48 }
49
50 static void on_kill(int sig)
51 {
52         Gtk.main_quit();
53 }
54
55
56 static int main (string[] args) {
57         bool opt_popup = false;
58         bool opt_calendar = false;
59         GLib.OptionEntry[] entries = new GLib.OptionEntry[] {
60                 OptionEntry() {
61                         long_name = "popup",
62                         short_name = 'p',
63                         flags = 0,
64                         arg = OptionArg.NONE,
65                         arg_data = &opt_popup,
66                         description = "run as a popup at the specified location on screen",
67                         arg_description = null },
68                 OptionEntry() {
69                         long_name = "calendar",
70                         short_name = 0,
71                         flags = 0,
72                         arg = OptionArg.NONE,
73                         arg_data = &opt_calendar,
74                         description = "run as calendar only",
75                         arg_description = null },
76                 OptionEntry()
77         };
78     Gtk.init_with_args(ref args, "", entries, null);
79     Gst.init (ref args);
80
81     pidfilename = "zavai";
82
83         // if not opts.verbose:
84         //     zavai.set_quiet()
85
86         // Set up zavai
87
88 /*
89         int id = at.earliestID();
90         stderr.printf("%d\n", id);
91         if (id != -1)
92         {
93                 string? label = zavai.clock.Alarm.getLabel(id);
94                 if (label == null)
95                         stdout.printf("generic at job\n");
96                 else
97                         stdout.printf("zavai at job: %s\n", label);
98         }
99
100         zavai.clock.Alarm.schedule("now + 1 hour", "An\"\'\'\n\n\ttani blinda la supercazzola");
101
102         return 0;
103 */
104
105         if (args.length > 1 && args[1] == "notify")
106         {
107                 stdout.printf("NOTIFY\n");
108                 DBus.Connection sbus;
109                 dynamic DBus.Object zav;
110                 try {
111                         sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
112                 } catch (DBus.Error e) {
113                         stderr.printf("Cannot access system DBus bus: %s\n", e.message);
114                         return 1;
115                 }
116                 // see mdbus -s org.freesmartphone.ousaged /org/freesmartphone/Usage
117                 zav = sbus.get_object(
118                         "org.enricozini.zavai",
119                         "/org/enricozini/Zavai/Clock",
120                         "org.enricozini.zavai.Alarm");
121                 if (args.length > 2)
122                 {
123                         stdout.printf("NOTIFY %s\n", args[2]);
124                         zav.Notify(args[2]);
125                 }
126                 else
127                         zav.Notify("Alarm");
128                 
129                 return 0;
130         }
131
132     // Core infrastructure
133         zavai.config = new zavai.Config();
134         zavai.config.argv0 = args[0];
135
136     if (opt_calendar)
137     {
138         pidfilename = "calendar";
139
140         // User interface
141         var calendar = new zavai.widgets.Calendar();
142
143         Gtk.Window win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
144         if (opt_popup)
145         {
146             Posix.pid_t pid = is_running();
147             if (pid != 0)
148             {
149                 // Kill a running calendar
150                 Posix.kill(pid, Posix.SIGINT);
151                 FileUtils.unlink(zavai.config.homedir + "/" + pidfilename + ".pid");
152                 return 0;
153             }
154
155             make_pidfile();
156
157             win.set_decorated(false);
158             //win.set_resizable(false);
159             win.set_border_width(5);
160             win.set_skip_taskbar_hint(true);
161             win.set_skip_pager_hint(true);
162             //    gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DOCK);
163             win.set_position(Gtk.WindowPosition.MOUSE);
164             win.stick();
165         }
166
167         win.title = "Zavai calendar";
168         win.destroy += Gtk.main_quit;
169         win.add(calendar);
170         win.set_size_request(300, 500);
171         win.show_all();
172         win.show();
173
174         // Shutdown the main loop on SIGINT
175         Posix.signal(Posix.SIGINT, on_kill);
176         Posix.signal(Posix.SIGTERM, on_kill);
177
178         Gtk.main();
179
180         calendar.flush();
181
182         return 0;
183     }
184
185
186         zavai.registry = new zavai.Registry();
187
188     zavai.main.init();
189
190     // Additional infrastructure
191         zavai.input.init();
192         zavai.uevent.init();
193         zavai.power.init();
194         zavai.gps.init();
195         zavai.gsm.init();
196         zavai.clock.init();
197         zavai.audio.init();
198         zavai.log.init();
199         zavai.wifi.init();
200         zavai.bluetooth.init();
201
202         /*
203         zavai.clock.clock.schedule(new zavai.clock.Alarm(123456, "Second"));
204         zavai.clock.clock.schedule(new zavai.clock.Alarm(1234567, "Third"));
205         zavai.clock.clock.schedule(new zavai.clock.Alarm(12345, "First"));
206         zavai.clock.clock.check_alarms();
207         */
208
209         // User interface
210         zavai.ui.music.init();
211         zavai.ui.main.init();
212         zavai.ui.gps.init();
213         zavai.ui.gsm.init();
214         zavai.config.find_and_run_script("display", "init");
215         zavai.ui.power.init();
216         zavai.ui.aux.init();
217         zavai.ui.kbd.init();
218         zavai.ui.wm.init();
219         zavai.ui.calendar.init();
220         zavai.ui.alarm.init();
221         zavai.ui.wifi.init();
222         zavai.ui.bluetooth.init();
223         zavai.ui.debug.init();
224
225         //zavai.app.show_applet("menu.main");
226         zavai.app.show_applet(zavai.ui.main.status);
227
228 /*
229         zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 10, "Test second"));
230         zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 5, "Test first"));
231         zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 300, "Test third"));
232 */
233
234         //zavai.ui.power.power_menu.toggle();
235
236         // # Shutdown the main loop on SIGINT
237         // def on_kill(signum, frame):
238         //     gtk.main_quit()
239         // signal.signal(signal.SIGINT, on_kill)
240         // signal.signal(signal.SIGTERM, on_kill)
241
242         // zavai.info("Starting")
243         // app = registry.resource("app")
244         // app.connect("destroy", gtk.main_quit)
245         // app.run()
246
247 /*
248         dynamic DBus.Object otime = zavai.registry.sbus.get_object(
249                         "org.freesmartphone.otimed",
250                         "/org/freesmartphone/Time/Alarm",
251                         "org.freesmartphone.Time.Alarm");
252
253         otime.ClearAlarm();
254         otime.SetAlarm(time_t() + 10);
255
256         dynamic DBus.Object notification = zavai.registry.sbus.get_object(
257                         "org.freesmartphone",
258                         "org/freesmartphone/Notification",
259                         "org.freesmartphone.Notification");
260         notification.Alarm += on_alarm;
261 */
262
263         zavai.app.run();
264
265         if (args.length > 1 && args[1] == "alarm")
266     {
267         zavai.clock.AlarmTriggerInfo alarm = new zavai.clock.AlarmTriggerInfo("Prova");
268         zavai.clock.alarm_trigger_queue.enqueue_trigger(alarm);
269     }
270
271         if (args.length > 2 && args[1] == "play")
272     {
273         zavai.audio.musicplayer.play("file://" + args[2]);
274
275         // Timeout.add(3 * 1000, () => {
276         //     zavai.audio.soundplayer.play("file:///backup/ciapino/src/openmoocow/data/moo.wav", true);
277         //     Timeout.add(4 * 1000, () => {
278         //         zavai.audio.soundplayer.stop();
279         //         return false;
280         //     });
281         //     return false;
282         // });
283     }
284
285         Gtk.main();
286
287         // zavai.info("Shutting down")
288         zavai.registry.shutdown();
289
290         return 0;
291 }