Power button event
[gregoa/zavai.git] / src / input.vala
index 7b99f1a1b688001698ee61c6fbaa355fb1170e50..81a07b24112cbb8f976cffb5097074bf88bb649b 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/*
-import sys
-import os
-import os.path
-import time
-import struct
-import signal
-import optparse
-import dbus
-import dbus.mainloop.glib
-import gobject
-import subprocess
-*/
-
 namespace zavai {
 namespace input {
 
-// For a list of dbus services, look in /etc/dbus-1/system.d/
 public abstract class DevInput : zavai.Service
 {
        public string device { get; construct; }
 
+       public signal bool event(LinuxInput.Event* ev);
+
     protected IOChannel fd = null;
     protected uint fd_watch = 0;
 
-       public DevInput()
-       {
-               //usage.ResourceChanged += on_resourcechanged;
-       }
-
     protected void close_fd()
     {
         if (fd != null)
@@ -62,13 +44,6 @@ public abstract class DevInput : zavai.Service
         }
     }
 
-    /*
-       public void on_resourcechanged(dynamic DBus.Object pos, string name, bool state, HashTable<string, Value?> attributes)
-       {
-               zavai.log.info("RESOURCE CHANGED " + name);
-       }
-    */
-
     protected bool on_input_data(IOChannel source, IOCondition condition)
     {
                if (condition != IOCondition.IN) return true;
@@ -103,7 +78,7 @@ public abstract class DevInput : zavai.Service
                 info("Headset plugged out")
                 self.mixer_for_handset(self)
         */
-        return true;
+        return event(ie);
     }
 
        /// Start reading from the device
@@ -138,15 +113,30 @@ public abstract class DevInput : zavai.Service
        }
 }
 
-public class Buttons : DevInput
+public class PowerButton : DevInput
 {
-    public Buttons()
+       public signal void power_button(bool pressed);
+
+    public PowerButton()
     {
-        name = "input.buttons";
+        name = "input.power_button";
         // FIXME: change to event0 for the power button
         // FIXME: change to event4 for the aux button and headset button
-        device = "/dev/input/event1";
+        //device = "/dev/input/event1";
+        device = "/dev/input/event0";
+
+               event += on_event;
     }
+
+       protected bool on_event(LinuxInput.Event* ev)
+       {
+               if (ev->type == LinuxInput.Type.KEY && 
+                       ev->code == LinuxInput.Key.POWER)
+               {
+                       power_button(ev->val == 0 ? false : true);
+               }
+               return true;
+       }
 }
 
 /*
@@ -288,348 +278,15 @@ class Audio:
         res = proc.wait()
         if res != 0:
             raise RuntimeError("Setting mixer failed")
-
-
-# For a list of dbus services, look in /etc/dbus-1/system.d/
-class GPS():
-    def __init__(self, bus = None):
-        if bus is None:
-            self.bus = dbus.SystemBus()
-        else:
-            self.bus = bus
-
-        # see mdbus -s org.freesmartphone.ousaged /org/freesmartphone/Usage
-        self.usage = self.bus.get_object('org.freesmartphone.ousaged', '/org/freesmartphone/Usage')
-        self.usage = dbus.Interface(self.usage, "org.freesmartphone.Usage")
-
-        # see mdbus -s org.freesmartphone.ogpsd /org/freedesktop/Gypsy
-        gps = self.bus.get_object('org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy') 
-        self.gps = dbus.Interface(gps, "org.freedesktop.Gypsy.Device")
-        self.gps_time = dbus.Interface(gps, "org.freedesktop.Gypsy.Time")
-        self.gps_position = dbus.Interface(gps, 'org.freedesktop.Gypsy.Position')
-        self.gps_ubx = dbus.Interface(gps, 'org.freesmartphone.GPS.UBX')
-        self.gps_debug = GPSDebug(self.gps_ubx)
-
-        # Request GPS resource
-        self.usage.RequestResource('GPS')
-        info("Acquired GPS")
-
-        self.waiting_for_fix = None
-
-    def close(self):
-        self.usage.ReleaseResource('GPS')
-        info("Released GPS")
-
-    def wait_for_fix(self, callback):
-        status = self.gps.GetFixStatus()
-        if status in [2, 3]:
-            info("We already have a fix, good.")
-            callback()
-            return True
-        else:
-            info("Waiting for a fix...")
-            self.waiting_for_fix = callback
-            self.bus.add_signal_receiver(
-                self.on_fix_status_changed, 'FixStatusChanged', 'org.freedesktop.Gypsy.Device',
-                'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-            return False
-
-    def on_fix_status_changed(self, status):
-        if status not in [2, 3]: return
-
-        info("Got GPS fix")
-        self.bus.remove_signal_receiver(
-            self.on_fix_status_changed, 'FixStatusChanged', 'org.freedesktop.Gypsy.Device',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-
-        if self.waiting_for_fix:
-            self.waiting_for_fix()
-            self.waiting_for_fix = None
-
-    def track_position(self, callback):
-        self.bus.add_signal_receiver(
-            callback, 'PositionChanged', 'org.freedesktop.Gypsy.Position',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-
-# This is taken from Zhone
-class GPSDebug():
-    def __init__(self, gps):
-        self.gps_ubx = gps.gps_ubx
-        self.busy = None
-        self.want = set( ["NAV-STATUS", "NAV-SVINFO"] )
-        self.have = set()
-        self.error = set()
-
-    def _update( self ):
-        if self.busy is None:
-            pending = self.want - self.have - self.error
-            if pending:
-                self.busy = pending.pop()
-                self.gps_ubx.SetDebugFilter(
-                    self.busy,
-                    True,
-                    reply_handler=self.on_debug_reply,
-                    error_handler=self.on_debug_error,
-                )
-
-    def request(self):
-        self.have = set()
-        self._update()
-
-    def on_debug_reply(self):
-        self.have.add(self.busy)
-        self.busy = None
-        self._update()
-
-    def on_debug_error(self, e):
-        info(e, "error while requesting debug packet %s" % self.busy)
-        self.error.add(self.busy)
-        self.busy = None
-        self._update()
-
-class GPSMonitor():
-    def __init__(self, gps):
-        self.gps = gps
-        self.gps_debug = GPSDebug(gps)
-
-    def start(self):
-        # TODO: find out how come sometimes these events are not sent
-        self.gps.bus.add_signal_receiver(
-            self.on_satellites_changed, 'SatellitesChanged', 'org.freedesktop.Gypsy.Satellite',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-        self.gps.bus.add_signal_receiver(
-            self.on_ubxdebug_packet, 'DebugPacket', 'org.freesmartphone.GPS.UBX',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-        self.gps_debug.request()
-
-    def stop(self):
-        self.gps.bus.remove_signal_receiver(
-            self.on_satellites_changed, 'SatellitesChanged', 'org.freedesktop.Gypsy.Satellite',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-        self.gps.bus.remove_signal_receiver(
-            self.on_ubxdebug_packet, 'DebugPacket', 'org.freesmartphone.GPS.UBX',
-            'org.freesmartphone.ogpsd', '/org/freedesktop/Gypsy')
-
-    def on_satellites_changed(self, satellites):
-        #if not satellites:
-        #    info("Satellite status: none")
-        #    return
-        self.gps_debug.request()
-        #info("Satellite status:")
-        #for sat in satellites:
-        #    if sat[0] not in self.sat_data:
-        #        self.sat_data[sat[0]] = [sat, None]
-        #    else:
-        #        self.sat_data[sat[0]][0] = sat
-        #self.print_sat_data()
-
-    def on_ubxdebug_packet(self, clid, length, data):
-        # In zhone it is cbUBXDebugPacket
-        #if clid == "NAV-STATUS" and data:
-        #    i = ["%s: %d" % (k, data[0][k]) for k in sorted(data[0].keys())]
-        #    info("Status:", " ".join(i))
-        ##    if data[0]['TTFF']:
-        ##        info("TTFF: %f", data[0]['TTFF']/1000.0)
-        if clid == "NAV-SVINFO":
-            self.print_ubx_sat_data(data[1:])
-        #else:
-        #    info("gps got ubxdebug packet", clid)
-        #    info("DATA:", data)
-
-    def print_ubx_sat_data(self, ubxinfo):
-        info("CH ID SN ELE AZI Used Diff Alm Eph Bad Status")
-        for sv in ubxinfo:
-            if sv["CNO"] == 0: continue
-            svid = sv["SVID"]
-            used = sv["Flags"] & 0x01
-            diff = sv["Flags"] & 0x02
-            almoreph = sv["Flags"] & 0x04
-            eph = sv["Flags"] & 0x08
-            bad = sv["Flags"] & 0x10
-            qi = ("%i: " % sv["QI"]) + {
-                0: "idle",
-                1: "searching",
-                2: "signal acquired",
-                3: "signal unusable",
-                4: "code lock",
-                5: "code&carrier lock",
-                6: "code&carrier lock",
-                7: "receiving data"
-            }[sv["QI"]]
-            info("%2d %2d %2d %3d %3d %s %s %s %s %s %s" % (
-                sv["chn"], sv["SVID"],
-                sv["CNO"], sv["Elev"], sv["Azim"],
-                used and "used" or "----",
-                diff and "diff" or "----",
-                almoreph and "alm" or "---",
-                eph and "eph" or "---",
-                bad and "bad" or "---",
-                qi))
-
-    def print_sat_data(self, satellites):
-        for sat in satellites:
-            if sat[4] == 0: continue
-            info("PRN %u" % sat[0],
-                 sat[1] and "used" or "unused",
-                 "el %u" % sat[2],
-                 "az %u" % sat[3],
-                 "snr %u" % sat[4])
-
-
-class Hub:
-    """Hub that manages all the various resources that we use, and initiates
-    operations."""
-    def __init__(self, bus = None):
-        self.bus = bus
-        self.waiting_for_fix = False
-        self.basename = None
-        self.recorder = None
-        self.gpx = None
-        self.gps = None
-        self.gps_monitor = None
-        self.audio = None
-        self.last_pos = None
-
-    def shutdown(self):
-        # Stop recording
-        if self.audio is not None:
-            self.audio.close()
-
-        # Close waypoints file
-        if self.gpx is not None:
-            self.gpx.close()
-
-        # Stop the GPS monitor
-        if self.gps_monitor:
-            self.gps_monitor.stop()
-
-        # Release the GPS
-        if self.gps is not None:
-            self.gps.close()
-
-    def levels(self):
-        self.audio = Audio()
-        self.audio.start_levels()
-
-    def record(self):
-        self.audio = Audio(self.make_waypoint)
-        self.gps = GPS()
-        # Get a fix and start recording
-        if not self.gps.wait_for_fix(self.start_recording):
-            self.gps_monitor = GPSMonitor(self.gps)
-            self.gps_monitor.start()
-
-    def monitor(self):
-        self.audio = None
-        self.gps = GPS()
-        self.gps_monitor = GPSMonitor(self.gps)
-        self.gps_monitor.start()
-
-    def start_recording(self):
-        if self.gps_monitor:
-            self.gps_monitor.stop()
-            self.gps_monitor = None
-
-        if not self.audio:
-            return
-
-        # Sync system time
-        gpstime = self.gps.gps_time.GetTime()
-        subprocess.call(["date", "-s", "@%d" % gpstime])
-        subprocess.call(["hwclock", "--systohc"])
-
-        # Compute basename for output files
-        self.basename = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(gpstime))
-        self.basename = os.path.join(AUDIODIR, self.basename)
-
-        # Start recording the GPX track
-        self.gpx = GPX(self.basename)
-        self.gps.track_position(self.on_position_changed)
-
-        # Start recording in background forking arecord
-        self.audio.set_basename(self.basename)
-        self.audio.start_recording()
-
-    def on_position_changed(self, fields, tstamp, lat, lon, alt):
-        self.last_pos = (fields, tstamp, lat, lon, alt)
-        if self.gpx:
-            self.gpx.trackpoint(tstamp, lat, lon, alt)
-
-    def make_waypoint(self):
-        if self.gpx is None:
-            return
-        if self.last_pos is None:
-            self.last_pos = self.gps.gps_position.GetPosition()
-        (fields, tstamp, lat, lon, alt) = self.last_pos
-        self.gpx.waypoint(tstamp, lat, lon, alt)
-        info("Making waypoint at %s: %f, %f, %f" % (
-            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(tstamp)), lat, lon, alt))
-
-class Parser(optparse.OptionParser):
-    def __init__(self, *args, **kwargs):
-        # Yes, in 2009 optparse from the *standard library* still uses old
-        # style classes
-        optparse.OptionParser.__init__(self, *args, **kwargs)
-
-    def error(self, msg):
-        sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
-        self.print_help(sys.stderr)
-        sys.exit(2)
-
-parser = Parser(usage="usage: %prog [options]",
-                version="%prog "+ VERSION,
-                description="Create a GPX and audio trackFind the times in the wav file when there is clear voice among the noise")
-parser.add_option("-v", "--verbose", action="store_true", help="verbose mode")
-parser.add_option("-m", "--monitor", action="store_true", help="only keep the GPS on and monitor satellite status")
-parser.add_option("-l", "--levels", action="store_true", help="only show input levels")
-
-(opts, args) = parser.parse_args()
-
-if not opts.monitor and not opts.verbose:
-    def info(*args):
-        pass
-
-# Set up dbus
-dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-mainloop = gobject.MainLoop()
-
-def on_sigint(signum, frame):
-    mainloop.quit()
-
-signal.signal(signal.SIGINT, on_sigint)
-
-hub = Hub()
-
-if opts.monitor:
-    hub.monitor()
-elif opts.levels:
-    hub.levels()
-else:
-    hub.record()
-
-mainloop.run()
-
-hub.shutdown()
-
-# Create waypoint at button press
-
-# At button press, raise window
-# Keep window until after 5 seconds it gets input, or until dismissed
-# Allow to choose "do not show"
-# Request input method when window is raised (or start a keyboard and kill it when lowered)
-
-# Release GPS
 */
 
-public Buttons buttons = null;
+public PowerButton power_button = null;
 
 public void init()
 {
-       buttons = new Buttons();
-
-       zavai.registry.register_service(buttons);
+       power_button = new PowerButton();
 
-       stderr.printf("ANTANI %d\n", LinuxInput.Key.POWER);
+       zavai.registry.register_service(power_button);
 }
 
 }