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