Handling Other Input Sources (XForms (Forms Library) - Version 1.2 (Rev. 4) A Graphical User Interface Toolkit for X)

Previous: Using Callback Functions, Up: Part I Doing Interaction [Contents][Index]

### 4.6 Handling Other Input Sources

It is not uncommon that X applications may require input from sources other than the X event queue. Outlined in this section are two routines in the Forms Library that provide a simple interface to handle additional input sources. Applications can define input callbacks to be invoked when input is available from a specified file descriptor.

The function

    typedef void (*FL_IO_CALLBACK)(int fd, void *data);
    void fl_add_io_callback(int fd, unsigned condition,
                            FL_IO_CALLBACK callback, void *data);

registers an input callback with the system. The argument `fd` must be a valid file descriptor on a UNIX-based system or other operating system dependent device specification while `condition` indicates under what circumstance the input callback should be invoked. The condition must be one of the following constants

`FL_READ`

  File descriptor has data available.

`FL_WRITE`

  File descriptor is available for writing.

`FL_EXCEPT`

  an I/O error has occurred.

When the given condition occurs, the Forms Library invokes the callback function specified by `callback`. The `data` argument allows the application to provide some data to be passed to the callback function when it is called (be sure that the storage pointed to by data has global (or static) scope).

To remove a callback that is no longer needed or to stop the Forms Library’s main loop from watching the file descriptor, use the following function

    void fl_remove_io_callback(int fd, unsigned condition,
                               FL_IO_CALLBACK callback);

The procedures outlined above work well with pipes and sockets, but can be a CPU hog on real files. To workaround this problem, you may wish to check the file periodically and only from within an idle callback.

Previous: Using Callback Functions, Up: Part I Doing Interaction [Contents][Index]