1 /*
2  *  This file is part of XForms.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with XForms; see the file COPYING.  If not, write to
16  *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17  *  MA 02111-1307, USA.
18  */
19 
20 
21 /*
22  * Test the accuracy of timer
23  *
24  *  This file is part of xforms package
25  *  T.C. Zhao and M. Overmars  (1997)
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include "include/forms.h"
35 
36 
37 typedef struct {
38     FL_FORM   * form0;
39     void      * vdata;
40     long        ldata;
41     FL_OBJECT * timer;
42     FL_OBJECT * restart;
43     FL_OBJECT * report;
44 } FD_form0;
45 
46 typedef struct {
47     long sec,
48          usec;
49 } Start_Time;
50 
51 static FD_form0 * create_form_form0( void );
52 
53 
54 /***************************************
55  ***************************************/
56 
57 static void
exit_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)58 exit_cb( FL_OBJECT * obj  FL_UNUSED_ARG,
59               long        data  FL_UNUSED_ARG )
60 {
61     fl_finish( );
62     exit( 0 );
63 }
64 
65 
66 /***************************************
67  * Callback for timer expiry, 'data' is set to requested delay (in ms)
68  ***************************************/
69 
70 static void
timer_cb(FL_OBJECT * obj,long data)71 timer_cb( FL_OBJECT * obj,
72           long        data )
73 {
74     double df;
75     FD_form0 *fd = obj->form->fdui;
76     Start_Time *start_time = fd->vdata;
77     long end_sec,
78          end_usec;
79 
80     fl_gettime( &end_sec, &end_usec );
81     df = end_sec - start_time->sec + 1.0e-6 * ( end_usec - start_time->usec );
82     fl_set_object_label_f( fd->report,
83                            "Timeout: %.3f s, Actual: %.3f s, Delta: %d ms",
84                            0.001 * data, df, FL_nint( 1000 * df - data ) );
85 }
86 
87 
88 /***************************************
89  ***************************************/
90 
91 static void
start_timer(FL_OBJECT * obj,long data)92 start_timer( FL_OBJECT * obj,
93              long        data )
94 {
95     FD_form0 *fd = obj->form->fdui;
96     Start_Time *start_time = fd->vdata;
97     static long delay = 3000;
98 
99     if ( data > 0 )
100         delay = data;
101     else
102         delay += 200;
103 
104     fl_set_object_label_f( fd->report, "Timer accuracy testing %.3f sec ...",
105                            0.001 * delay );
106     fl_gettime( &start_time->sec, &start_time->usec );
107     fl_set_object_callback( fd->timer, timer_cb, delay );
108     fl_set_timer( fd->timer, 0.001 * delay );
109 }
110 
111 
112 /***************************************
113  ***************************************/
114 
115 int
main(int argc,char * argv[])116 main( int    argc,
117       char * argv[ ] )
118 {
119     FD_form0 *fd_form0;
120     Start_Time start_time;
121 
122     fl_initialize( &argc, argv, 0, 0, 0 );
123 
124     fd_form0 = create_form_form0( );
125     fd_form0->vdata = &start_time;
126 
127     start_timer( fd_form0->timer, 3000 );
128 
129     fl_show_form( fd_form0->form0, FL_PLACE_CENTER, FL_FULLBORDER,
130                   "Timer object precision" );
131     fl_do_forms( );
132     return 0;
133 }
134 
135 
136 /***************************************
137  ***************************************/
138 
139 static FD_form0 *
create_form_form0(void)140 create_form_form0( void )
141 {
142     FL_OBJECT *obj;
143     FD_form0 *fdui = fl_calloc( 1, sizeof *fdui );
144 
145     fdui->form0 = fl_bgn_form( FL_NO_BOX, 350, 130 );
146 
147     fl_add_box( FL_UP_BOX, 0, 0, 350, 130, "" );
148 
149     fdui->timer = fl_add_timer( FL_HIDDEN_TIMER, 0, 0, 0, 0, "" );
150 
151     fdui->report = obj = fl_add_text( FL_NORMAL_TEXT, 20, 20, 310, 50,"" );
152     fl_set_object_align( obj, FL_ALIGN_CENTER );
153     fl_set_object_boxtype( obj, FL_DOWN_BOX );
154 
155     fdui->restart = obj = fl_add_button( FL_NORMAL_BUTTON, 140, 80, 90, 35,
156                                          "Restart" );
157     fl_set_object_callback( obj, start_timer, 0 );
158 
159     obj = fl_add_button( FL_NORMAL_BUTTON, 240, 80, 90, 35, "Done" );
160     fl_set_object_callback( obj, exit_cb, 0 );
161 
162     fl_end_form( );
163 
164     fdui->form0->fdui = fdui;
165 
166     return fdui;
167 }
168 
169 
170 /*
171  * Local variables:
172  * tab-width: 4
173  * indent-tabs-mode: nil
174  * End:
175  */
176