2
* Copyright © 2012 Keith Packard <keithp@keithp.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 2 of the License.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program; if not, write to the Free Software Foundation, Inc.,
15
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21
extend namespace Nichrome {
22
public namespace Timer {
24
public typedef widget_t + struct {
31
void draw (cairo_t cr, &timer_t widget) {
34
left = (widget.end - millis()) / 1000;
43
scale (cr, widget.geometry.width / 2, widget.geometry.height / 2);
48
- π / 2, /* start angle */
49
2 * π * (left) / 60.0 - π/2); /* end angle */
51
set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
56
void run_timer (&timer_t widget) {
57
twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
59
while ((int now = millis()) <= widget.end) {
60
int delay = (widget.end - now) % 100;
63
twixt(Mutex::release(widget.lock); Mutex::acquire(widget.lock)) {
65
Widget::redraw(&widget);
68
} catch Thread::signal (int sig) {
70
widget.running = false;
74
void start_timer (&timer_t widget) {
75
twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
79
protected void set_timer (&timer_t widget, real time) {
80
twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
81
widget.end = millis() + floor (time * 1000 + 0.5);
82
if (!widget.running) {
83
widget.running = true;
84
widget.timer = fork run_timer(&widget);
89
protected void stop_timer (&timer_t widget) {
90
widget.end = millis();
91
twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
93
Thread::send_signal(widget.timer, 0);
97
void outline (cairo_t cr, &timer_t widget) {
98
rectangle(cr, 0, 0, 0, 0);
101
void natural (cairo_t cr, &timer_t widget) {
102
rectangle(cr, 0, 0, 100, 100);
105
protected void init(*nichrome_t nichrome, &timer_t widget) {
106
Widget::init(nichrome, &widget);
108
widget.outline = outline;
109
widget.natural = natural;
110
widget.lock = Mutex::new();
111
widget.running = false;
114
protected *timer_t new(*nichrome_t nichrome) {
115
&timer_t widget = &(timer_t) {};
117
init(nichrome, &widget);