File: timeoutprec.c

package info (click to toggle)
libforms 1.2.3-1.6
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 10,896 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (175 lines) | stat: -rw-r--r-- 3,954 bytes parent folder | download | duplicates (5)
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
/*
 *  This file is part of XForms.
 *
 *  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; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 */


/*
 * Test the accuracy of timeouts
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include <stdlib.h>
#include <unistd.h>

/**** Forms and Objects ****/

typedef struct {
    FL_FORM   * form;
    int         timer_id;
    void      * vdata;
    long        ldata;
    FL_OBJECT * timer;
    FL_OBJECT * restart;
    FL_OBJECT * report;
} FD_form;

static FD_form * create_form_form( void );
static long start_sec,
            start_usec;
static long end_sec,
            end_usec;


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

static void
exit_cb( FL_OBJECT * ob    FL_UNUSED_ARG,
         long        data  FL_UNUSED_ARG )
{
   fl_finish( );
   exit( 0 );
}


/***************************************
 * timer expired
 ***************************************/

static void
timer_cb( int    id  FL_UNUSED_ARG,
          void * data )
{
   char buf[ 128 ];
   double df;
   FD_form *fd = data;
   double timerval =  1.0e-3 * fd->ldata;

   fd->timer_id = 0;

   fl_gettime( &end_sec, &end_usec );

   df = end_sec - start_sec + 1.0e-6 * ( end_usec - start_usec );

   sprintf( buf,"Timeout: %.3f  Actual: %.3f  DeltaE: %.3f",
            timerval, df, df - timerval );

   fl_set_object_label( fd->report, buf );

}


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

static void
start_timer( FL_OBJECT * ob,
             long        data  FL_UNUSED_ARG )
{
   FD_form *fd = ob->form->fdui;
   char buf[ 128 ];

   if ( fd->timer_id )
       fl_remove_timeout( fd->timer_id );

   fd->ldata += 200;
   sprintf( buf, "Timer accuracy testing %.3f sec ...", fd->ldata * 0.001 );
   fl_set_object_label( fd->report, buf );
   fl_gettime( &start_sec, &start_usec );
   fd->timer_id = fl_add_timeout( fd->ldata, timer_cb, fd );
}


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

int
main( int    argc,
      char * argv[ ] )
{
   FD_form *fd_form;

   
   fl_initialize( &argc, argv, 0, 0, 0 );
   fd_form = create_form_form( );

   /* fill-in form initialization code */

   fd_form->ldata = 800;
   start_timer( fd_form->report, 0 );

   /* show the first form */

   fl_show_form( fd_form->form, FL_PLACE_CENTER, FL_FULLBORDER,
                 "Timeout precision" );
   fl_do_forms( );

   return 0;
}


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

static FD_form *
create_form_form(void)
{
    FL_OBJECT *obj;
    FD_form *fdui = fl_calloc( 1, sizeof *fdui );

    fdui->form = fl_bgn_form( FL_NO_BOX, 320, 130 );

    fl_add_box( FL_UP_BOX, 0, 0, 320, 130, "" );

    obj = fl_add_button( FL_NORMAL_BUTTON, 210, 80, 90, 35, "Done" );
    fl_set_object_callback( obj, exit_cb, 0 );

    fdui->restart = obj = fl_add_button( FL_TOUCH_BUTTON, 110, 80, 90, 35,
                                         "Restart" );
    fl_set_object_callback( obj, start_timer, 0 );

    fdui->report = obj = fl_add_text( FL_NORMAL_TEXT, 10, 20, 290, 50,"" );

    fl_end_form( );

    fdui->form->fdui = fdui;

    return fdui;
}


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