Actually decrement usage count when releasing a service
[gregoa/zavai.git] / src / app_keyboard.vala
index 790ffa1097a682b07e8f184894d9d3dde9f39d1e..31c049276712cedc55f05c8d74b133e1eca8f3a2 100644 (file)
@@ -26,273 +26,106 @@ namespace kbd {
 
 public class Keyboard : Service
 {
-       protected Pid pid;
+    protected Pid pid;
 
-       private void on_child_quit(Pid pid, int status)
-       {
-               Process.close_pid(pid);
-               pid = (Pid)0;
-               stop();
-       }
-
-       protected override void start()
-       {
-               string[] args = { "/usr/bin/matchbox-keyboard", null };
-               int opid;
-               try {
-                       Gdk.spawn_on_screen(
-                               Gdk.Screen.get_default(),
-                               "/",
-                               args,
-                               null,
-                               SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
-                               null,
-                               out opid);
-                       pid = (Pid)opid;
-                       ChildWatch.add(pid, on_child_quit);
-                       base.start();
-               } catch (Error e) {
-                       log.error("Running matchbox-keyboard: " + e.message);
-                       pid = (Pid)0;
-               }
-       }
-
-       protected override void stop()
-       {
-               if ((int)pid != 0)
-               {
-                       Posix.kill((int)pid, 15);
-                       pid = (Pid)0;
-               }
-               base.stop();
-       }
-}
-
-/*
-class GPSOn(gtk.ToggleAction):
-    states = [_("GPS always on"), _("GPS on when needed")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        self.registry.resource("gps").connect("gps", self)
-
-    def stop(self):
-        self.registry.resource("gps").disconnect("gps", self)
-
-class GPXTracer(gtk.ToggleAction):
-    states = [_("Start GPX trace"), _("Stop GPX trace")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        zavai.info("GPX trace started")
-        self.registry.resource("gpx").connect("gpx", self)
-
-    def stop(self):
-        zavai.info("GPX trace ended")
-        self.registry.resource("gpx").disconnect("gpx", self)
+    public Keyboard()
+    {
+        Object(name: "keyboard");
+    }
 
-public class Waypoint : BigButton
-{
-    public Waypoint()
+    private void on_child_quit(Pid pid, int status)
     {
-        set_label("Take waypoint");
-        zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
-        clicked += on_clicked;
-        set_sensitive(zavai.gps.gpx.tracking);
+        Process.close_pid(pid);
+        pid = (Pid)0;
+        stop();
     }
 
-    protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
+    protected override void start()
     {
-stderr.printf("Toggled %d\n", (int)new_state);
-        set_sensitive(new_state);
+        string script = zavai.config.find_script("keyboard");
+        if (script == null) return;
+        string[] args = { script, null };
+        int opid;
+        try {
+            Gdk.spawn_on_screen(
+                Gdk.Screen.get_default(),
+                "/",
+                args,
+                null,
+                SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
+                null,
+                out opid);
+            pid = (Pid)opid;
+            ChildWatch.add(pid, on_child_quit);
+            base.start();
+        } catch (Error e) {
+            log.error("Running " + zavai.config.homedir + "/keyboard: " + e.message);
+            pid = (Pid)0;
+        }
     }
 
-    protected void on_clicked()
+    protected override void stop()
     {
-stderr.printf("Activate\n");
-        zavai.gps.gpx.waypoint();
+        if ((int)pid != 0)
+        {
+            Posix.kill((int)pid, 15);
+            pid = (Pid)0;
+        }
+        base.stop();
     }
 }
 
-class GPXAudioTracer(gtk.ToggleAction):
-    states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.recorder = zavai.Recorder(registry)
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def shutdown(self):
-        self.recorder.stop()
-        super(GPXAudioTracer, self).shutdown()
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        zavai.info("GPX trace started")
-        gpx = self.registry.resource("gpx")
-        gpx.connect("gpx", self)
-        gpx.add_activity_monitor(self.on_gpx_activity_changed)
-
-    def stop(self):
-        zavai.info("GPX trace ended")
-        gpx = self.registry.resource("gpx")
-        gpx.disconnect("gpx", self)
-        self.recorder.stop()
-        gpx.del_activity_monitor(self.on_gpx_activity_changed)
-
-    def on_gpx_activity_changed(self, gpx, state):
-        if state:
-            self.recorder.start(gpx.basename + ".wav")
-        else:
-            self.recorder.stop()
-*/
-
-/*
-public class GPSRequestLink : Gtk.ToggleButton
+public class KeyboardIcon : Gtk.StatusIcon
 {
-       protected string service_name;
-       protected string label_start;
-       protected string label_stop;
-    protected Gtk.StatusIcon status_icon;
-    protected int fix_status = 0;
+    bool requested = false;
 
-       public GPSRequestLink()
-       {
-        service_name = "gps";
-        label_start = "Keep GPS on";
-        label_stop = "Stop keeping GPS on";
-               set_size_request(0, zavai.config.min_button_height);
-               toggled += on_toggled;
-
-               set_label(get_active() ? label_stop : label_start);
-
-        //tooltip = "GPS status";
-        try {
-            fix_status = zavai.gps.gps.device.GetFixStatus();
-        } catch (Error e) {
-            fix_status = 0;
-        }
-        zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
+    public KeyboardIcon()
+    {
+        activate += on_activate;
+        zavai.ui.power.power.screen_lock_changed += on_screen_lock_changed;
 
-        // GPS status icon
-        status_icon = new Gtk.StatusIcon();
-        status_icon.set_visible(true);
-        status_icon.activate += on_status_activate;
         update_icon();
-       }
+    }
 
-    protected void update_icon()
+    private void on_activate()
     {
-        string name;
-        if (fix_status == 2 || fix_status == 3)
-            name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
+        requested = !requested;
+        update_icon();
+        if (requested)
+            keyboard.request("keyboardicon");
         else
-            name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
-stderr.printf("load icon from %s\n", name);
-        status_icon.set_from_file(name);
+            keyboard.release("keyboardicon");
     }
 
-    private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
+    protected void on_screen_lock_changed(bool val)
     {
-        this.fix_status = fix_status;
         update_icon();
     }
 
-       private void on_toggled(Gtk.Button src)
-       {
-               Service s = zavai.registry.gets(service_name);
-               if (get_active())
-                       s.request("servicerequestlink");
-               else
-                       s.release("servicerequestlink");
-               set_label(get_active() ? label_stop : label_start);
-        update_icon();
-       }
-
-    private void on_status_activate()
+    protected void update_icon()
     {
-        set_active(!get_active());
+        string name = zavai.config.icondir + "/";
+        if (zavai.ui.power.power.screen_locked)
+            name += "screen_lock.png";
+        else
+            name += (requested ? "kbd_on.png" : "kbd_off.png");
+        stderr.printf("load icon from %s\n", name);
+        set_from_file(name);
     }
 }
-*/
-
-public void init()
-{
-    /*
-    registry.register(GPXAudioTracer(registry))
-    registry.register(GPXWaypoint(registry))
 
-    // Apps
-    var useless = new Useless();
-    zavai.registry.register_applet("app.debug.useless", useless);
-    */
 
-    /*
-    var menu_waypoint = new Waypoint();
-    var menu_gpsrequest = new GPSRequestLink();
-               //label_on = "Stop GPX trace";
-               //label_off = "Start GPX trace";
-               //label_on = "Stop GPS monitor";
-               //label_off = "Start GPS monitor";
-               //label_on = "Stop GPS position tracking";
-               //label_off = "Start GPS position tracking";
-               //label_on = "Stop keeping GPS on";
-               //label_off = "Keep GPS on";
+Keyboard keyboard;
+KeyboardIcon icon;
 
-    // Menus
-    var menu_gps = new zavai.Menu("GPS");
-    //menu_gps.add_applet("app.debug.useless");
-    menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
-    menu_gps.add_widget(menu_waypoint);
-    menu_gps.add_widget(menu_gpsrequest);
-
-    zavai.registry.register_menu("menu.gps", menu_gps);
-    zavai.registry.getmenu("menu.main").add_applet("menu.gps");
-    */
+public void init()
+{
+    if (zavai.config.profile != "laptop")
+    {
+        keyboard = new Keyboard();
+        icon = new KeyboardIcon();
+        icon.set_visible(true);
+    }
 }
 
 }