butcher butcher butcher
[gregoa/zavai.git] / src / app_keyboard.vala
1 /*
2  * app_keyboard - zavai keyboard show/hide functions
3  *
4  * Copyright (C) 2009  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 namespace zavai {
24 namespace ui {
25 namespace kbd {
26
27 public class Keyboard : Service
28 {
29     protected Pid pid;
30
31     public Keyboard()
32     {
33         Object(name: "keyboard");
34     }
35
36     private void on_child_quit(Pid pid, int status)
37     {
38         Process.close_pid(pid);
39         pid = (Pid)0;
40         stop();
41     }
42
43     protected override void start()
44     {
45         string script = zavai.config.find_script("keyboard");
46         if (script == null) return;
47         string[] args = { script, null };
48         int opid;
49         try {
50             Gdk.spawn_on_screen(
51                 Gdk.Screen.get_default(),
52                 "/",
53                 args,
54                 null,
55                 SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
56                 null,
57                 out opid);
58             pid = (Pid)opid;
59             ChildWatch.add(pid, on_child_quit);
60             base.start();
61         } catch (Error e) {
62             log.error("Running " + zavai.config.homedir + "/keyboard: " + e.message);
63             pid = (Pid)0;
64         }
65     }
66
67     protected override void stop()
68     {
69         if ((int)pid != 0)
70         {
71             Posix.kill((int)pid, 15);
72             pid = (Pid)0;
73         }
74         base.stop();
75     }
76 }
77
78 public class KeyboardIcon : Gtk.StatusIcon
79 {
80     bool requested = false;
81
82     public KeyboardIcon()
83     {
84         activate += on_activate;
85         zavai.ui.power.power.screen_lock_changed += on_screen_lock_changed;
86
87         update_icon();
88     }
89
90     private void on_activate()
91     {
92         requested = !requested;
93         update_icon();
94         if (requested)
95             keyboard.request("keyboardicon");
96         else
97             keyboard.release("keyboardicon");
98     }
99
100     protected void on_screen_lock_changed(bool val)
101     {
102         update_icon();
103     }
104
105     protected void update_icon()
106     {
107         string name = zavai.config.icondir + "/";
108         if (zavai.ui.power.power.screen_locked)
109             name += "screen_lock.png";
110         else
111             name += (requested ? "kbd_on.png" : "kbd_off.png");
112         stderr.printf("load icon from %s\n", name);
113         set_from_file(name);
114     }
115 }
116
117
118 Keyboard keyboard;
119 KeyboardIcon icon;
120
121 public void init()
122 {
123     if (zavai.config.profile != "laptop")
124     {
125         keyboard = new Keyboard();
126         icon = new KeyboardIcon();
127         icon.set_visible(true);
128     }
129 }
130
131 }
132 }
133 }