Re: fl_remove_io_callback

Stephen Langer (langer@cam.nist.gov)
Thu, 30 Jan 1997 11:22:11 -0500

To subscribers of the xforms list from "Stephen Langer" <langer@cam.nist.gov> :

--
--PART-BOUNDARY=.19701301122.ZM22467.cam.nist.gov
Content-Type: text/plain; charset=us-ascii

Hi --

Let me be more clear about the problem I'm having with io callbacks. I'm trying to read a file while continuously updating a free object. The problems are (1) the free object isn't updated on schedule unless there's an io callback routine installed, and (2) the io callback routine is called even when there's no data waiting to be read.

I've included a test program below. The program simply flashes a rectangle at a rate specified by a command line argument, while also reading from stdin. Specify the flash rate in seconds. A button allows you to install and remove the io callback routine. When installed, the rectangle flashes correctly. When not installed, the rectangle flashes very slowly, meaning that the free object isn't getting FL_STEP events at the proper rate.

When I run "testio .1 < junk" I get the behavior I've described. When I run "testio .1" so that it reads from the keyboard, the callback is called only when there's data, but the flashing is slow all the time, even when the callback is installed.

The test fails on both SGI and Sun.

Any advice would be much appreciated!

-- Steve

-- 
-- EMail: stephen.langer@nist.gov                    Phone: (301) 975-5423 --
-- WWW:  http://math.nist.gov/acmd/Staff/SLanger/    Fax:   (301) 990-4127 --
-- Mail: Building 820 Room 365; NIST; Gaithersburg, Md          20899-0001 --

--PART-BOUNDARY=.19701301122.ZM22467.cam.nist.gov X-Zm-Content-Name: testio.c Content-Description: Text Content-Type: text/plain ; name="testio.c" ; charset=us-ascii

#include "forms.h" #include <stdio.h> #include <stdlib.h> #include <time.h>

double delay; /* seconds between color changes in free object */ int installed = 0; /* start with io_callback installed? */

typedef struct { FL_FORM *test; FL_OBJECT *remove; FL_OBJECT *box; FL_OBJECT *quit; FL_OBJECT *ncalls; FL_OBJECT *eofbutton; void *vdata; long ldata; } FD_test;

FD_test *test;

/* ------------------------------------- */

void CB_quit(FL_OBJECT *obj, long arg) { exit(0); }

/* ------------------------------------- */

void iocallback(int fd, void *datum) { /* read a byte from stdin */ unsigned char input; static int ncalls = 0; char str[10]; fread(&input, sizeof(unsigned char), 1, stdin); sprintf(str, "%d", ++ncalls); fl_set_object_label(test->ncalls, str); if(feof(stdin)) fl_set_button(test->eofbutton, 1); }

void CB_remove(FL_OBJECT *obj, long arg) { /* toggle installation of callback */ if(installed) { fl_remove_io_callback(fileno(stdin), FL_READ, iocallback); fl_set_object_label(test->remove, "Add io callback"); } else { fl_add_io_callback(fileno(stdin), FL_READ, iocallback, 0); fl_set_object_label(test->remove, "Remove io callback"); } installed ^= 1; }

/* ------------------------------------- */

int freecallback(FL_OBJECT *freeobj, int event, FL_Coord mx, FL_Coord my, int key, void *xev) { static FL_COLOR color = FL_RED; if(event == FL_DRAW) { XSetForeground(fl_get_display(), fl_state[fl_get_vclass()].gc[8], fl_get_pixel(color)); XFillRectangle(fl_get_display(), fl_winget(), fl_state[fl_get_vclass()].gc[8], freeobj->x, freeobj->y, freeobj->w, freeobj->h); } else if(event == FL_STEP) { /* change color if enough time has passed */ static clock_t lasttime = 0; clock_t time = clock(); if(time - lasttime > CLOCKS_PER_SEC*delay) { if(color == FL_RED) color = FL_BLUE; else color = FL_RED; fl_redraw_object(freeobj); lasttime = time; } } return 0; }

/* ------------------------------------- */

FD_test *create_form_test(void) { FL_OBJECT *obj; FD_test *fdui = (FD_test *) fl_calloc(1, sizeof(*fdui));

fdui->test = fl_bgn_form(FL_NO_BOX, 320, 250); obj = fl_add_box(FL_UP_BOX,0,0,320,250,""); fdui->remove = obj = fl_add_button(FL_NORMAL_BUTTON,40,20,170,40,"remove io callback"); fl_set_object_callback(obj,CB_remove,0); fdui->box = obj = fl_add_box(FL_FLAT_BOX,40,70,170,100,""); fl_set_object_color(obj,FL_WHITE,FL_WHITE); fdui->quit = obj = fl_add_button(FL_NORMAL_BUTTON,240,20,70,40,"Quit"); fl_set_object_callback(obj,CB_quit,0); obj = fl_add_text(FL_NORMAL_TEXT,20,205,100,20,"calls to iocallback"); fl_set_object_lalign(obj,FL_ALIGN_LEFT|FL_ALIGN_INSIDE); fdui->ncalls = obj = fl_add_text(FL_NORMAL_TEXT,120,200,60,30,""); fl_set_object_boxtype(obj,FL_DOWN_BOX); fl_set_object_lalign(obj,FL_ALIGN_LEFT|FL_ALIGN_INSIDE); fl_set_object_lstyle(obj,FL_BOLD_STYLE); fdui->eofbutton = obj = fl_add_checkbutton(FL_PUSH_BUTTON,200,200,30,30,"EOF"); fl_set_object_color(obj,FL_COL1,FL_RED); fl_end_form();

return fdui; }

/*---------------------------------------*/

int main(int argc, char *argv[]) { FL_OBJECT *freeobj; fl_initialize(&argc, argv, "testio", 0, 0); delay = atof(argv[1]); test = create_form_test(); fl_deactivate_object(test->eofbutton); fl_hide_object(test->box); fl_addto_form(test->test); freeobj = fl_add_free(FL_CONTINUOUS_FREE, test->box->x, test->box->y, test->box->w, test->box->h, "", freecallback); fl_end_form(); fl_show_form(test->test, FL_PLACE_FREE, FL_FULLBORDER, "testio");

if(installed) { fl_add_io_callback(fileno(stdin), FL_READ, iocallback, 0); fl_set_object_label(test->remove, "Remove io callback"); } else fl_set_object_label(test->remove, "Add io callback");

for(;;) fl_do_forms(); }

--PART-BOUNDARY=.19701301122.ZM22467.cam.nist.gov--

_________________________________________________ To unsubscribe, send the message "unsubscribe" to xforms-request@bob.usuf2.usuhs.mil or see http://bob.usuf2.usuhs.mil/mailserv/xforms.html Xforms Home Page: http://bragg.phys.uwm.edu/xforms List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/xforms-archive/