Merge branch 'master' into alarm
[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[] args = { zavai.config.homedir + "/keyboard", null };
46                 int opid;
47                 try {
48                         Gdk.spawn_on_screen(
49                                 Gdk.Screen.get_default(),
50                                 "/",
51                                 args,
52                                 null,
53                                 SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
54                                 null,
55                                 out opid);
56                         pid = (Pid)opid;
57                         ChildWatch.add(pid, on_child_quit);
58                         base.start();
59                 } catch (Error e) {
60                         log.error("Running " + zavai.config.homedir + "/keyboard: " + e.message);
61                         pid = (Pid)0;
62                 }
63         }
64
65         protected override void stop()
66         {
67                 if ((int)pid != 0)
68                 {
69                         Posix.kill((int)pid, 15);
70                         pid = (Pid)0;
71                 }
72                 base.stop();
73         }
74 }
75
76 public class KeyboardIcon : Gtk.StatusIcon
77 {
78         bool requested = false;
79
80         public KeyboardIcon()
81         {
82                 activate += on_activate;
83         zavai.ui.power.power.screen_lock_changed += on_screen_lock_changed;
84
85                 update_icon();
86         }
87
88         private void on_activate()
89         {
90                 requested = !requested;
91                 update_icon();
92                 if (requested)
93                         keyboard.request("keyboardicon");
94                 else
95                         keyboard.release("keyboardicon");
96         }
97
98     protected void on_screen_lock_changed(bool val)
99     {
100         update_icon();
101     }
102
103         protected void update_icon()
104         {
105                 string name = zavai.config.icondir + "/";
106         if (zavai.ui.power.power.screen_locked)
107             name += "screen_lock.png";
108         else
109             name += (requested ? "kbd_on.png" : "kbd_off.png");
110                 stderr.printf("load icon from %s\n", name);
111                 set_from_file(name);
112         }
113 }
114
115 /*
116 public class GPSRequestLink : Gtk.ToggleButton
117 {
118         public GPSRequestLink()
119         {
120         // GPS status icon
121         status_icon = new Gtk.StatusIcon();
122         status_icon.set_visible(true);
123         status_icon.activate += on_status_activate;
124         update_icon();
125         }
126
127     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
128     {
129         this.fix_status = fix_status;
130         update_icon();
131     }
132
133         private void on_toggled(Gtk.Button src)
134         {
135                 Service s = zavai.registry.gets(service_name);
136                 if (get_active())
137                         s.request("servicerequestlink");
138                 else
139                         s.release("servicerequestlink");
140                 set_label(get_active() ? label_stop : label_start);
141         update_icon();
142         }
143
144     private void on_status_activate()
145     {
146         set_active(!get_active());
147     }
148 }
149 */
150
151 Keyboard keyboard;
152 KeyboardIcon icon;
153
154 public void init()
155 {
156 stderr.printf("INIT KBD\n");
157         keyboard = new Keyboard();
158         zavai.registry.register_service(keyboard);
159         icon = new KeyboardIcon();
160         icon.set_visible(true);
161 }
162
163 }
164 }
165 }