2 * app_alarm - zavai alarm system
4 * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
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.
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.
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
28 // Compute a-b in microseconds
29 static long timediff(Posix.timeval* a, Posix.timeval* b)
31 return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec);
35 protected class AlarmNotifierDialog : ui.notify.Notifier
37 protected Gtk.Label message;
38 protected Gtk.Button message_button;
40 public AlarmNotifierDialog(string title, string text)
43 notifier_name: "alarm",
47 set_size_request(300, 500);
49 add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK);
50 add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL);
51 set_default_response(Gtk.ResponseType.OK);
53 weak Gtk.VBox vbox = (Gtk.VBox)get_content_area();
55 message = new Gtk.Label(text);
56 message_button = new Gtk.Button();
57 message_button.set_image(message);
58 message_button.clicked += (b) => { response(Gtk.ResponseType.OK); };
59 vbox.pack_start(message_button, true, true, 0);
60 message_button.show();
63 protected virtual bool push_aux_state()
65 var state = new zavai.led.LedState(name);
67 zavai.led.aux.push_state(state);
71 protected virtual bool push_vibrator_state()
73 var state = new zavai.led.LedState(name);
75 zavai.led.aux.push_state(state);
79 protected virtual bool push_ringtone_state()
85 public class AlarmNotifier : zavai.Resource, ui.notify.Notifier
87 protected weak clock.AlarmTriggerInfo current = null;
88 protected AlarmNotifierDialog dialog = null;
90 public AlarmNotifier()
92 zavai.registry.register(this);
94 clock.alarm_trigger_queue.triggered += on_trigger;
95 clock.alarm_trigger_queue.acked += on_done;
96 clock.alarm_trigger_queue.canceled += on_done;
99 public void on_trigger(clock.AlarmTriggerInfo info)
102 dialog = new AlarmNotifierDialog("Alarm", info.label);
104 uint cancel_timeout = 0;
105 cancel_timeout = Timeout.add(30 * 1000, () => {
106 dialog.response(Gtk.ResponseType.CANCEL);
110 int res = dialog.run();
111 if (cancel_timeout != 0) Source.remove(cancel_timeout);
114 case Gtk.ResponseType.OK:
115 clock.alarm_trigger_queue.ack(current);
117 case Gtk.ResponseType.CANCEL:
118 clock.alarm_trigger_queue.cancel(current);
126 public void on_done(clock.AlarmTriggerInfo info)
128 if (current == null || dialog == null || current != info) return;
129 dialog.response(Gtk.ResponseType.NONE);
132 public void shutdown()
134 if (dialog == null) return;
135 dialog.response(Gtk.ResponseType.CANCEL);
139 public AlarmNotifier notifier = null;
143 if (clock.alarm_trigger_queue != null)
144 notifier = new AlarmNotifier();