xforms-development
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [XForms] Looking for simple example


From: Jens Thoms Toerring
Subject: Re: [XForms] Looking for simple example
Date: Sun, 9 Nov 2014 15:09:42 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Jon,

On Sun, Nov 09, 2014 at 03:01:11AM +0000, jon wrote:
> The above code works fine, but I need a
> timer in milliseconds not seconds.

It does work with sub-second resolution - the 'delay' argument
to fl_set_timer() is a double, so if you set it to 0.1 the
timer should expire after 100 ms. If it doesn't then you've
discovered a bug!
 
> I found this example online, works fine as stand alone but does not seem
> to work if I mix it into the xforms application.
> 
> http://www.informit.com/articles/article.aspx?p=23618&seqNum=14
> 
> It must be a common task but I don't seem to making it work, how do I
> perform an action at 10Hz or better within an xforms application ?

Well, a timer object should work... Here's an example program:

===========8<==========================================

#include <forms.h>
#include <stdio.h>
#include <sys/time.h>

FL_OBJECT *timer;

static void th( FL_OBJECT * obj, long int data  FL_UNUSED_ARG )
{
        struct timeval t;

        gettimeofday( &t, NULL );
        printf( "Timer expired %ld:%ld\n", ( long ) t.tv_sec, ( long ) 
t.tv_usec );
        fl_set_timer( obj, 0.1 );
}

static FL_FORM * create_form( void )
{
        FL_FORM * f;

    f = fl_bgn_form( FL_NO_BOX, 320, 250 );
    fl_add_box( FL_FLAT_BOX, 0, 0, 320, 250, "" );
    fl_add_button( FL_NORMAL_BUTTON, 100, 110, 110, 90, "Quit" );
        timer = fl_add_timer( FL_HIDDEN_TIMER, 0, 0, 0, 0, "" );
        fl_set_object_callback( timer, th, 0 );
    fl_end_form( );

    return f;
}

int main( int    argc, char * argv[ ] )
{
    FL_FORM * f;

    fl_initialize( &argc, argv, 0, 0, 0 );
    f = create_form( );
    fl_show_form( f, FL_PLACE_CENTERFREE, FL_FULLBORDER, "aaa" );

        fl_set_timer( timer, 0.1 );   /* start the timer! */

    fl_do_forms( );               /* Only returns for "Quit" button */

    fl_finish( );
    return 0;
}

===========8<==========================================

On my machine the time resultion achieved seems to be in the
sub-millisecond range, which I would consider to be not too
bad;-) Of course, such a high resolution isn't guaranteed,
in a real program with lots of objects competing for CPU time
it probably is going to be worse.

> At the moment fl_do_forms() seems to block forever,

That's what fl_for_forms() is meant to do: it only ever returns
when there's an event from an object that does not have a call-
back. Otherwise it's supposed never to return. That's like with
all other GUI frameworks I know about - the event look never re-
turns, XForms is different in that it allows the event loop
(what you enter when calling fl_do_forms()) to stop once an
event for an object without a callback is received.

> In places the examples do things like this:
>         if ( fl_do_forms( ) == yes )
> But I cant see what changes the behaviour of fl_fo_forms() so that it
> returns control before the application ends ?

In that example 'yes' is obviously an object without a callback.
And when there's none fl_do_forms() returns. This is often used
for the "Exit" or "Quit" button or similar. So the simple rule
is that objects without a handler will make fl_do_forms() re-
turn when triggered (e.g. when a button is clicked on or a
timer expires etc.), while objects that have a callback don't.

> i've read the
> documentation and references online, it seems to contradict itself in
> several places,

Could you send me some pointers to such places? I'd be happy to
improve the documentation!

On Sun, Nov 09, 2014 at 04:41:59AM +0000, jon wrote:
> I think I made it work, found fl_set_idle_callback() - that seems good
> enough.

Fine;-) Just be aware that idle callbacks can have a much worse
time resolution than timer objects since the idle callback is
really only invoked when there's time enough. So at what moment
they get called can be rather random, which often is good enough,
but not for all purposes.
                              Best regards, Jens
-- 
  \   Jens Thoms Toerring  ________      address@hidden
   \_______________________________      http://toerring.de



reply via email to

[Prev in Thread] Current Thread [Next in Thread]