]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/zavai.vala
List log entries
[gregoa/zavai.git] / src / zavai.vala
index a8719050d6937f2cb2cf9adccc111467670295ac..29df675775917ad12bb4047c1e2c3fadd2cb73f8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * zavai - simple interface to the OpenMoko (or to the FSO stack)
  *
- * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ * Copyright (C) 2009--2010  Enrico Zini <enrico@enricozini.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 using GLib;
 
 //string VERSION = "0.1";
+static string pidfilename;
 
-void on_alarm()
+static Posix.pid_t is_running()
 {
-       stderr.printf("RETURNED\n");
+       string pidfile = zavai.config.homedir + "/" + pidfilename + ".pid";
+       string contents;
+       try {
+               if (!FileUtils.get_contents(pidfile, out contents))
+                       return 0;
+       } catch (FileError e) {
+               return 0;
+       }
+       int val = contents.to_int();
+       string procdir = "/proc/%d".printf(val);
+       if (FileUtils.test(procdir, FileTest.IS_DIR))
+               return (Posix.pid_t)val;
+       else
+               return 0;
 }
 
+static void make_pidfile()
+{
+       string pidfile = zavai.config.homedir + "/" + pidfilename + ".pid";
+    try {
+        FileUtils.set_contents(pidfile, "%d".printf(Posix.getpid()));
+    } catch (FileError e) {
+        zavai.log.error("Cannot create pidfile " + pidfile + ": " + e.message);
+    }
+}
+
+static void on_kill(int sig)
+{
+       Gtk.main_quit();
+}
+
+
 static int main (string[] args) {
-       Gtk.init (ref args);
-
-       // parser = Parser(usage="usage: %prog [options]",
-       //                 version="%prog "+ VERSION,
-       //                 description="Simple interactive interface for the OpenMoko")
-       // parser.add_option("-v", "--verbose", action="store_true", help="verbose mode")
-       // 
-       // (opts, args) = parser.parse_args()
-       // 
+       bool opt_popup = false;
+       bool opt_calendar = false;
+       GLib.OptionEntry[] entries = new GLib.OptionEntry[] {
+               OptionEntry() {
+                       long_name = "popup",
+                       short_name = 'p',
+                       flags = 0,
+                       arg = OptionArg.NONE,
+                       arg_data = &opt_popup,
+                       description = "run as a popup at the specified location on screen",
+                       arg_description = null },
+               OptionEntry() {
+                       long_name = "calendar",
+                       short_name = 0,
+                       flags = 0,
+                       arg = OptionArg.NONE,
+                       arg_data = &opt_calendar,
+                       description = "run as calendar only",
+                       arg_description = null },
+               OptionEntry()
+       };
+    try {
+        Gtk.init_with_args(ref args, "", entries, null);
+    } catch (Error e) {
+        zavai.log.error("Cannot init gtk: " + e.message);
+        return 1;
+    }
+    Gst.init (ref args);
+
+    pidfilename = "zavai";
+
        // if not opts.verbose:
        //     zavai.set_quiet()
-       // 
-       // # Read configuration
-       // zavai.info("Loading configuration")
-       // conf = zavai.Config()
-       // 
-       // # Set up dbus
-       // dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-       // dbus_system_bus = dbus.SystemBus()
 
        // Set up zavai
 
+/*
+       int id = at.earliestID();
+       stderr.printf("%d\n", id);
+       if (id != -1)
+       {
+               string? label = zavai.clock.Alarm.getLabel(id);
+               if (label == null)
+                       stdout.printf("generic at job\n");
+               else
+                       stdout.printf("zavai at job: %s\n", label);
+       }
+
+       zavai.clock.Alarm.schedule("now + 1 hour", "An\"\'\'\n\n\ttani blinda la supercazzola");
+
+       return 0;
+*/
+
+       if (args.length > 1 && args[1] == "notify")
+       {
+               stdout.printf("NOTIFY\n");
+               DBus.Connection sbus;
+               dynamic DBus.Object zav;
+               try {
+                       sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
+               } catch (DBus.Error e) {
+                       stderr.printf("Cannot access system DBus bus: %s\n", e.message);
+                       return 1;
+               }
+               // see mdbus -s org.freesmartphone.ousaged /org/freesmartphone/Usage
+               zav = sbus.get_object(
+                       "org.enricozini.zavai",
+                       "/org/enricozini/Zavai/Clock",
+                       "org.enricozini.zavai.Alarm");
+               if (args.length > 2)
+               {
+                       stdout.printf("NOTIFY %s\n", args[2]);
+                       zav.Notify(args[2]);
+               }
+               else
+                       zav.Notify("Alarm");
+               
+               return 0;
+       }
+
     // Core infrastructure
        zavai.config = new zavai.Config();
+       zavai.config.argv0 = args[0];
+
+    if (opt_calendar)
+    {
+        pidfilename = "calendar";
+
+        // User interface
+        var calendar = new zavai.widgets.Calendar();
+
+        Gtk.Window win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
+        if (opt_popup)
+        {
+            Posix.pid_t pid = is_running();
+            if (pid != 0)
+            {
+                // Kill a running calendar
+                Posix.kill(pid, Posix.SIGINT);
+                FileUtils.unlink(zavai.config.homedir + "/" + pidfilename + ".pid");
+                return 0;
+            }
+
+            make_pidfile();
+
+            win.set_decorated(false);
+            //win.set_resizable(false);
+            win.set_border_width(5);
+            win.set_skip_taskbar_hint(true);
+            win.set_skip_pager_hint(true);
+            //    gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DOCK);
+            win.set_position(Gtk.WindowPosition.MOUSE);
+            win.stick();
+        }
+
+        win.title = "Zavai calendar";
+        win.destroy += Gtk.main_quit;
+        win.add(calendar);
+        win.set_size_request(300, 500);
+        win.show_all();
+        win.show();
+
+        // Shutdown the main loop on SIGINT
+        Posix.signal(Posix.SIGINT, on_kill);
+        Posix.signal(Posix.SIGTERM, on_kill);
+
+        Gtk.main();
+
+        calendar.flush();
+
+        return 0;
+    }
+
+
        zavai.registry = new zavai.Registry();
-       zavai.app = new zavai.Zavai();
 
-       zavai.registry.register_resource("zavai", zavai.app);
+    zavai.main.init();
 
     // Additional infrastructure
        zavai.input.init();
+       zavai.uevent.init();
+       zavai.power.init();
        zavai.gps.init();
+       zavai.gsm.init();
+       zavai.clock.init();
+       zavai.audio.init();
+       zavai.log.init();
+       zavai.wifi.init();
+       zavai.bluetooth.init();
 
-       zavai.registry.register_menu("menu.main", new zavai.Menu("Main menu"));
+       /*
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(123456, "Second"));
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(1234567, "Third"));
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(12345, "First"));
+       zavai.clock.clock.check_alarms();
+       */
 
        // User interface
+       zavai.ui.music.init();
+       zavai.ui.main.init();
        zavai.ui.gps.init();
+       zavai.ui.gsm.init();
+       zavai.config.find_and_run_script("display", "init");
+       zavai.ui.power.init();
+       zavai.ui.aux.init();
        zavai.ui.kbd.init();
        zavai.ui.wm.init();
-       zavai.ui.powerbutton.init();
-       zavai.input.ui_init();
+       zavai.ui.calendar.init();
+       zavai.ui.alarm.init();
+       zavai.ui.wifi.init();
+       zavai.ui.bluetooth.init();
        zavai.ui.debug.init();
 
-       zavai.app.show_applet("menu.main");
-
-       // # Register main factories
-       // registry.register(conf, "conf")
-       // registry.register_factory(zavai.GPX, "gpx")
-       // registry.register_factory(zavai.Audio, "audio")
-
-       // # Load plugins
-       // zavai.info("Loading plugins")
-       // for p in zavai.load_plugins(nick="zavai"):
-       //     try:
-       //         p.init(conf = conf, registry = registry)
-       //     except Exception, e:
-       //         print >>sys.stderr, "Exception caught loading plugin %s: skipping plugin" % p
-       //         print >>sys.stderr, "Exception details:"
-       //         import traceback
-       //         details = traceback.format_exc()
-       //         print >>sys.stderr, "\t"+details.rstrip().replace("\n", "\n\t")
+       //zavai.app.show_applet("menu.main");
+       zavai.app.show_applet(zavai.ui.main.status);
+
+/*
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 10, "Test second"));
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 5, "Test first"));
+       zavai.clock.clock.schedule(new zavai.clock.Alarm(time_t() + 300, "Test third"));
+*/
+
+       //zavai.ui.power.power_menu.toggle();
 
        // # Shutdown the main loop on SIGINT
        // def on_kill(signum, frame):
@@ -118,8 +270,41 @@ static int main (string[] args) {
 */
 
        zavai.app.run();
+
+       if (args.length > 1 && args[1] == "alarm")
+    {
+        zavai.clock.AlarmTriggerInfo alarm = new zavai.clock.AlarmTriggerInfo("Prova");
+        zavai.clock.alarm_trigger_queue.enqueue_trigger(alarm);
+    }
+
+    if (args.length > 2 && args[1] == "showlog")
+    {
+        string[] z = zavai.log.log.list_entries(false);
+        for (int i = 0; z[i] != null; ++i)
+            stderr.printf("FALSE %s\n", z[i]);
+        z = zavai.log.log.list_entries(true);
+        for (int i = 0; z[i] != null; ++i)
+            stderr.printf("TRUE %s\n", z[i]);
+        zavai.log.Log l = zavai.log.log.load(args[2]);
+        l.dump();
+        return 0;
+    }
+
+       if (args.length > 2 && args[1] == "play")
+    {
+        zavai.audio.musicplayer.play("file://" + args[2]);
+
+        // Timeout.add(3 * 1000, () => {
+        //     zavai.audio.soundplayer.play("file:///backup/ciapino/src/openmoocow/data/moo.wav", true);
+        //     Timeout.add(4 * 1000, () => {
+        //         zavai.audio.soundplayer.stop();
+        //         return false;
+        //     });
+        //     return false;
+        // });
+    }
+
        Gtk.main();
-       //registry.loop.run();
 
        // zavai.info("Shutting down")
        zavai.registry.shutdown();