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 timeouts
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include "include/forms.h"
30 #include <stdlib.h>
31 #include <unistd.h>
32 
33 /**** Forms and Objects ****/
34 
35 typedef struct {
36     FL_FORM   * form;
37     int         timer_id;
38     void      * vdata;
39     long        ldata;
40     FL_OBJECT * timer;
41     FL_OBJECT * restart;
42     FL_OBJECT * report;
43 } FD_form;
44 
45 static FD_form * create_form_form( void );
46 static long start_sec,
47             start_usec;
48 static long end_sec,
49             end_usec;
50 
51 
52 /***************************************
53  ***************************************/
54 
55 static void
exit_cb(FL_OBJECT * ob FL_UNUSED_ARG,long data FL_UNUSED_ARG)56 exit_cb( FL_OBJECT * ob    FL_UNUSED_ARG,
57          long        data  FL_UNUSED_ARG )
58 {
59    fl_finish( );
60    exit( 0 );
61 }
62 
63 
64 /***************************************
65  * timer expired
66  ***************************************/
67 
68 static void
timer_cb(int id FL_UNUSED_ARG,void * data)69 timer_cb( int    id  FL_UNUSED_ARG,
70           void * data )
71 {
72    char buf[ 128 ];
73    double df;
74    FD_form *fd = data;
75    double timerval =  1.0e-3 * fd->ldata;
76 
77    fd->timer_id = 0;
78 
79    fl_gettime( &end_sec, &end_usec );
80 
81    df = end_sec - start_sec + 1.0e-6 * ( end_usec - start_usec );
82 
83    sprintf( buf,"Timeout: %.3f  Actual: %.3f  DeltaE: %.3f",
84             timerval, df, df - timerval );
85 
86    fl_set_object_label( fd->report, buf );
87 
88 }
89 
90 
91 /***************************************
92  ***************************************/
93 
94 static void
start_timer(FL_OBJECT * ob,long data FL_UNUSED_ARG)95 start_timer( FL_OBJECT * ob,
96              long        data  FL_UNUSED_ARG )
97 {
98    FD_form *fd = ob->form->fdui;
99    char buf[ 128 ];
100 
101    if ( fd->timer_id )
102        fl_remove_timeout( fd->timer_id );
103 
104    fd->ldata += 200;
105    sprintf( buf, "Timer accuracy testing %.3f sec ...", fd->ldata * 0.001 );
106    fl_set_object_label( fd->report, buf );
107    fl_gettime( &start_sec, &start_usec );
108    fd->timer_id = fl_add_timeout( fd->ldata, timer_cb, fd );
109 }
110 
111 
112 /***************************************
113  ***************************************/
114 
115 int
main(int argc,char * argv[])116 main( int    argc,
117       char * argv[ ] )
118 {
119    FD_form *fd_form;
120 
121 
122    fl_initialize( &argc, argv, 0, 0, 0 );
123    fd_form = create_form_form( );
124 
125    /* fill-in form initialization code */
126 
127    fd_form->ldata = 800;
128    start_timer( fd_form->report, 0 );
129 
130    /* show the first form */
131 
132    fl_show_form( fd_form->form, FL_PLACE_CENTER, FL_FULLBORDER,
133                  "Timeout precision" );
134    fl_do_forms( );
135 
136    return 0;
137 }
138 
139 
140 /***************************************
141  ***************************************/
142 
143 static FD_form *
create_form_form(void)144 create_form_form(void)
145 {
146     FL_OBJECT *obj;
147     FD_form *fdui = fl_calloc( 1, sizeof *fdui );
148 
149     fdui->form = fl_bgn_form( FL_NO_BOX, 320, 130 );
150 
151     fl_add_box( FL_UP_BOX, 0, 0, 320, 130, "" );
152 
153     obj = fl_add_button( FL_NORMAL_BUTTON, 210, 80, 90, 35, "Done" );
154     fl_set_object_callback( obj, exit_cb, 0 );
155 
156     fdui->restart = obj = fl_add_button( FL_TOUCH_BUTTON, 110, 80, 90, 35,
157                                          "Restart" );
158     fl_set_object_callback( obj, start_timer, 0 );
159 
160     fdui->report = obj = fl_add_text( FL_NORMAL_TEXT, 10, 20, 290, 50,"" );
161 
162     fl_end_form( );
163 
164     fdui->form->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