File: timer.c

package info (click to toggle)
libforms 1.0.93sp1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,548 kB
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (361 lines) | stat: -rw-r--r-- 9,227 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file timer.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 * Forms Object class: TIMER
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include "include/forms.h"
#include "flinternal.h"


/* Extra information need for input boxes. */

typedef struct
{
    double time_left;       /* the time (sec) left to wait */
    double timer;           /* total duration              */
    long sec,               /* start time                  */
         usec;
    int on,
        up;
    FL_TIMER_FILTER filter;
} SPEC;


static int update_only;


/***************************************
 ***************************************/

static char *
default_filter( FL_OBJECT * ob  FL_UNUSED_ARG,
                double      totalsec )
{
    static char buf[ 32 ];
    int hr,
        minutes;
    double sec;

    if ( totalsec >= 3600.0 )
    {
        hr = totalsec / 3600.0 + 0.001;
        minutes = totalsec / 60.0 + 0.001;
        minutes -= hr * 60;
        sec = totalsec - 60 * ( minutes + 60 * hr );
        sprintf( buf, "%d:%02d:%04.1f", hr, minutes, sec );
    }
    else if ( totalsec >= 60.0 )
    {
        minutes = totalsec / 60.0 + 0.001;
        sec = totalsec - minutes * 60;
        sprintf( buf, "%d:%04.1f", minutes, sec );
    }
    else
        sprintf( buf, "%.1f", totalsec );

    return buf;
}


/***************************************
 * draws the timer
 ***************************************/

static void
draw_timer( FL_OBJECT * ob )
{
    FL_COLOR col;
    char *str;
    SPEC *sp = ob->spec;

    if ( ob->type == FL_HIDDEN_TIMER )
        return;

    if ( ! sp->on || sp->time_left > 0.0 )
        col = ob->col1;
    else if ( ( int ) ( sp->time_left / FL_TIMER_BLINKRATE ) % 2 )
        col = ob->col1;
    else
        col = ob->col2;

    fl_drw_box( ob->boxtype, ob->x, ob->y, ob->w, ob->h, col, ob->bw );

    if ( ob->type == FL_VALUE_TIMER && sp->time_left > 0.0 )
    {
        double time_shown = sp->up ? sp->timer - sp->time_left : sp->time_left;

        str = ( sp->filter ? sp->filter : default_filter )( ob, time_shown );
        fl_drw_text( FL_ALIGN_CENTER, ob->x, ob->y, ob->w, ob->h,
                     ob->lcol, ob->lstyle, ob->lsize, str );
    }
}


/***************************************
 * Handles an event
 ***************************************/

static int
handle_timer( FL_OBJECT * ob,
              int         event,
              FL_Coord    mx   FL_UNUSED_ARG,
              FL_Coord    my   FL_UNUSED_ARG,
              int         key  FL_UNUSED_ARG,
              void *      ev   FL_UNUSED_ARG )
{
    SPEC *sp = ob->spec;
    long sec,
         usec;
    double lasttime_left;
    int ret = FL_RETURN_NONE;

    switch ( event )
    {
        case FL_DRAW:
            draw_timer( ob );
            /* fall through */

        case FL_DRAWLABEL:
            if ( ob->type != FL_HIDDEN_TIMER && ! update_only )
            {
                if (    ob->type == FL_VALUE_TIMER
                     && ( ob->align & ~ FL_ALIGN_INSIDE ) == FL_ALIGN_CENTER )
                    fl_drw_text_beside( FL_ALIGN_LEFT, ob->x, ob->y,
                                        ob->w, ob->h, ob->lcol, ob->lstyle,
                                        ob->lsize, ob->label );
                else
                    fl_drw_text_beside( ob->align, ob->x, ob->y, ob->w, ob->h,
                                        ob->lcol, ob->lstyle, ob->lsize,
                                        ob->label );
            }
            break;

        case FL_RELEASE:
            if ( ob->type != FL_HIDDEN_TIMER && sp->time_left < 0.0 )
                fl_set_timer( ob, 0.0 );
            break;

        case FL_STEP:
            if ( ! sp->on )
                break;
            lasttime_left = sp->time_left;
            fl_gettime( &sec, &usec );
            sp->time_left = sp->timer - ( sec - sp->sec )
                            - ( usec - sp->usec ) * 1.0e-6;
            update_only = 1;

            /* Don't check for zero. we can overshoot by as much as 50msec. try
               to split the error */

            if ( sp->time_left > 0.02 )
            {
                if ( ob->type == FL_VALUE_TIMER
                     && ( int ) ( 10.0 * sp->time_left ) !=
                                              ( int ) ( 10.0 * lasttime_left ) )
                    fl_redraw_object( ob );
            }
            else if ( lasttime_left > 0.0 )
            {
                if ( ob->type == FL_HIDDEN_TIMER )
                    fl_set_timer( ob, 0.0 );
                else
                    fl_redraw_object( ob );
                update_only = 0;
                ret = FL_RETURN_CHANGED | FL_RETURN_END;
                break;
            }
            else if ( ( int ) ( lasttime_left / FL_TIMER_BLINKRATE ) !=
                                ( int ) ( sp->time_left / FL_TIMER_BLINKRATE ) )
                fl_redraw_object( ob );

            update_only = 0;
            break;

        case FL_FREEMEM:
            fl_free( ob->spec );
            break;
    }

    return ret;
}


/***************************************
 * creates an object
 ***************************************/

FL_OBJECT *
fl_create_timer( int          type,
                 FL_Coord     x,
                 FL_Coord     y,
                 FL_Coord     w,
                 FL_Coord     h,
                 const char * l )
{
    FL_OBJECT *ob;

    ob = fl_make_object( FL_TIMER, type, x, y, w, h, l, handle_timer );
    ob->boxtype = FL_TIMER_BOXTYPE;
    ob->col1 = FL_TIMER_COL1;
    ob->col2 = FL_TIMER_COL2;
    ob->align = FL_TIMER_ALIGN;
    ob->lcol = FL_TIMER_LCOL;
    ob->spec = fl_calloc( 1, sizeof( SPEC ) );

    fl_set_timer( ob, 0.0 );       /* disabled timer */
    ( ( SPEC * ) ob->spec )->filter = default_filter;
    return ob;
}


/***************************************
 * Adds an object
 ***************************************/

FL_OBJECT *
fl_add_timer( int          type,
              FL_Coord     x,
              FL_Coord     y,
              FL_Coord     w,
              FL_Coord     h,
              const char * l )
{
    FL_OBJECT *ob= fl_create_timer( type, x, y, w, h, l );

    fl_add_object( fl_current_form, ob );
    if ( ob->type == FL_VALUE_TIMER )
        fl_set_object_dblbuffer( ob, 1 );
    return ob;
}


/***************************************
 * Sets the timer clock to the particular delay. (0.0 to reset)
 ***************************************/

void
fl_set_timer( FL_OBJECT * ob,
              double      total )
{
    SPEC *sp = ob->spec;

    sp->time_left = sp->timer = total;
    sp->on = total > 0.0;
    fl_set_object_automatic( ob, sp->on );
    fl_gettime( &sp->sec, &sp->usec );
    if ( ob->type != FL_HIDDEN_TIMER )
        fl_redraw_object( ob );
}


/***************************************
 * returns the amount of time left
 ***************************************/

double
fl_get_timer( FL_OBJECT * ob )
{
    SPEC *sp = ob->spec;

    return sp->time_left > 0.0 ? sp->time_left : 0.0;
}


/***************************************
 ***************************************/

void
fl_set_timer_countup( FL_OBJECT * ob,
                      int         yes )
{
    ( ( SPEC * ) ob->spec )->up = yes;
}


/***************************************
 ***************************************/

FL_TIMER_FILTER
fl_set_timer_filter( FL_OBJECT *     ob,
                     FL_TIMER_FILTER filter )
{
    SPEC *sp = ob->spec;
    FL_TIMER_FILTER old = sp->filter;

    if ( filter != sp->filter )
    {
        sp->filter = filter;
        fl_redraw_object( ob );
    }
    return old;
}


/***************************************
 ***************************************/

void
fl_suspend_timer( FL_OBJECT * ob )
{
    ( ( SPEC * ) ob->spec )->on = 0;
    fl_set_object_automatic( ob, 0 );
}


/***************************************
 ***************************************/

void
fl_resume_timer( FL_OBJECT * ob )
{
    SPEC *sp = ob->spec;
    long sec, usec;
    double elapsed;

    if ( sp->on )
        return;

    elapsed = sp->timer - sp->time_left;
    fl_gettime( &sec, &usec );
    sp->sec = sec - ( long ) elapsed;
    sp->usec = usec - ( long ) ( ( elapsed - ( long ) elapsed ) * 1.0e6 );
    fl_set_object_automatic( ob, 1 );
    sp->on = 1;
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */