~ubuntu-branches/ubuntu/saucy/ricochet/saucy-proposed

« back to all changes in this revision

Viewing changes to ricochet-0.1/debian/ricochet/usr/share/ricochet/nichrome-timer.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
3
 
 *
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.
7
 
 *
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.
12
 
 *
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.
16
 
 */
17
 
 
18
 
autoload Nichrome;
19
 
autoload Mutex;
20
 
 
21
 
extend namespace Nichrome {
22
 
        public namespace Timer {
23
 
 
24
 
                public typedef widget_t + struct {
25
 
                        Mutex::mutex            lock;
26
 
                        bool                    running;
27
 
                        int                     end;
28
 
                        thread                  timer;
29
 
                } timer_t;
30
 
 
31
 
                void draw (cairo_t cr, &timer_t widget) {
32
 
                        real    left;
33
 
                        if (widget.running) {
34
 
                                left = (widget.end - millis()) / 1000;
35
 
 
36
 
                                if (left < 0)
37
 
                                        left = 0;
38
 
                                if (left > 60)
39
 
                                        left = 60;
40
 
                        } else
41
 
                                left = 0;
42
 
                        save (cr);
43
 
                        scale (cr, widget.geometry.width / 2, widget.geometry.height / 2);
44
 
                        move_to (cr, 1, 1);
45
 
                        arc (cr,
46
 
                             1, 1,                              /* center */
47
 
                             0.9,                               /* radius */
48
 
                             - π / 2,                           /* start angle */
49
 
                             2 * π * (left) / 60.0 - π/2);      /* end angle */
50
 
                        close_path (cr);
51
 
                        set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
52
 
                        fill (cr);
53
 
                        restore (cr);
54
 
                }
55
 
 
56
 
                void run_timer (&timer_t widget) {
57
 
                        twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
58
 
                                try {
59
 
                                        while ((int now = millis()) <= widget.end) {
60
 
                                                int     delay = (widget.end - now) % 100;
61
 
                                                if (delay == 0)
62
 
                                                        delay = 100;
63
 
                                                twixt(Mutex::release(widget.lock); Mutex::acquire(widget.lock)) {
64
 
                                                        sleep(delay);
65
 
                                                        Widget::redraw(&widget);
66
 
                                                }
67
 
                                        }
68
 
                                } catch Thread::signal (int sig) {
69
 
                                }
70
 
                                widget.running = false;
71
 
                        }
72
 
                }
73
 
 
74
 
                void start_timer (&timer_t widget) {
75
 
                        twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
76
 
                        }
77
 
                }
78
 
 
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);
85
 
                                }
86
 
                        }
87
 
                }
88
 
 
89
 
                protected void stop_timer (&timer_t widget) {
90
 
                        widget.end = millis();
91
 
                        twixt(Mutex::acquire(widget.lock); Mutex::release(widget.lock)) {
92
 
                                if (widget.running)
93
 
                                        Thread::send_signal(widget.timer, 0);
94
 
                        }
95
 
                }
96
 
 
97
 
                void outline (cairo_t cr, &timer_t widget) {
98
 
                        rectangle(cr, 0, 0, 0, 0);
99
 
                }
100
 
 
101
 
                void natural (cairo_t cr, &timer_t widget) {
102
 
                        rectangle(cr, 0, 0, 100, 100);
103
 
                }
104
 
 
105
 
                protected void init(*nichrome_t nichrome, &timer_t widget) {
106
 
                        Widget::init(nichrome, &widget);
107
 
                        widget.draw = draw;
108
 
                        widget.outline = outline;
109
 
                        widget.natural = natural;
110
 
                        widget.lock = Mutex::new();
111
 
                        widget.running = false;
112
 
                }
113
 
 
114
 
                protected *timer_t new(*nichrome_t nichrome) {
115
 
                        &timer_t        widget = &(timer_t) {};
116
 
 
117
 
                        init(nichrome, &widget);
118
 
                        return &widget;
119
 
                }
120
 
        }
121
 
}