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 override bool push_aux_state()
65 var state = new zavai.led.LedState(notifier_name);
67 zavai.led.aux.push_state(state);
71 protected override bool push_vibrator_state()
73 var state = new zavai.led.LedState(notifier_name);
75 zavai.led.vibrator.push_state(state);
79 protected override bool push_ringtone_state()
81 var state = new zavai.audio.PlayerState(notifier_name, config.ringtone_alarm, true);
82 zavai.audio.soundplayer.push_state(state);
87 public class AlarmNotifier : zavai.Resource, Object
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 if (zavai.led.auxbutton != null)
107 zavai.led.auxbutton.event += on_auxbutton;
109 uint cancel_timeout = 0;
110 cancel_timeout = Timeout.add(30 * 1000, () => {
111 dialog.response(Gtk.ResponseType.CANCEL);
115 int res = dialog.run();
116 if (cancel_timeout != 0) Source.remove(cancel_timeout);
119 case Gtk.ResponseType.OK:
120 clock.alarm_trigger_queue.ack(current);
122 case Gtk.ResponseType.CANCEL:
123 clock.alarm_trigger_queue.cancel(current);
127 if (zavai.led.auxbutton != null)
128 zavai.led.auxbutton.event -= on_auxbutton;
135 protected bool on_auxbutton(ulong time, bool pressed)
137 if (dialog == null) return false;
138 dialog.response(Gtk.ResponseType.OK);
142 public void on_done(clock.AlarmTriggerInfo info)
144 if (current == null || dialog == null || current != info) return;
145 dialog.response(Gtk.ResponseType.NONE);
148 public void shutdown()
150 if (dialog == null) return;
151 dialog.response(Gtk.ResponseType.CANCEL);
155 public AlarmNotifier notifier = null;
159 if (clock.alarm_trigger_queue != null)
160 notifier = new AlarmNotifier();