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()
81 var state = new zavai.audio.PlayerState(name, config.ringtone_alarm, true);
82 zavai.audio.soundplayer.push_state(state);
87 public class AlarmNotifier : zavai.Resource, ui.notify.Notifier
89 protected weak clock.AlarmTriggerInfo current = null;
90 protected AlarmNotifierDialog dialog = null;
92 public AlarmNotifier()
94 zavai.registry.register(this);
96 clock.alarm_trigger_queue.triggered += on_trigger;
97 clock.alarm_trigger_queue.acked += on_done;
98 clock.alarm_trigger_queue.canceled += on_done;
101 public void on_trigger(clock.AlarmTriggerInfo info)
104 dialog = new AlarmNotifierDialog("Alarm", info.label);
106 uint cancel_timeout = 0;
107 cancel_timeout = Timeout.add(30 * 1000, () => {
108 dialog.response(Gtk.ResponseType.CANCEL);
112 int res = dialog.run();
113 if (cancel_timeout != 0) Source.remove(cancel_timeout);
116 case Gtk.ResponseType.OK:
117 clock.alarm_trigger_queue.ack(current);
119 case Gtk.ResponseType.CANCEL:
120 clock.alarm_trigger_queue.cancel(current);
128 public void on_done(clock.AlarmTriggerInfo info)
130 if (current == null || dialog == null || current != info) return;
131 dialog.response(Gtk.ResponseType.NONE);
134 public void shutdown()
136 if (dialog == null) return;
137 dialog.response(Gtk.ResponseType.CANCEL);
141 public AlarmNotifier notifier = null;
145 if (clock.alarm_trigger_queue != null)
146 notifier = new AlarmNotifier();