EZwgl (7.3 File Events)

## 7.3 File Events

EZwgl provides a timer-like mechanism for reading and writing files in a event-driven fashion.

To queue file events, use

    EZ_Input *EZ_AddInput(int fd, int mask, EZ_InputCallBack callback, void *data);

This function registers a new source of events: the status change of the specifed file descripter. From the time a `fd` is registered on, the specified status of `fd` is watched by the main event dispatcher. Whenever the specified status changes, `callback` is invoked.

Here `fd` specifies a file descriptor. `mask` specifies what status to watch for. It is an or'ed combination of `EZ_READABLE_MASK`, `EZ_WRITABLE_MASK` and `EZ_EXCEPTION_MASK`. `callback` spcifies a procedure. It will be invoked whenever the specified status of `fd` changes. `data` specifies an arbitary client data to be passed to `callback` when invoked.

`EZ_InputCallBack` is a procedure of the following type.

`void inputcallback(EZ_Input *obj, void *data, int fd, int mask)`

To remove file events, use

    void EZ_RemoveInput(EZ_Input inputId);

### 7.3.1 An Example

In this example, we watch the readable status of stdin, if there are anything to read, we read up to 1024 characters a time and append the string at the end of the text widget.

    /******************* ExampleInput ***************************************/
    #include

    #include "EZ.h"
    static void readStdin(EZ_Input *id, void *data, int fd, int mask)
    {
      EZ_Widget *tw = (EZ_Widget *)data;
      char buf[1024];
      int n;
      if(mask & EZ_READABLE_MASK)
        {
          n = read(fd, buf, 1023);
          if(n > 0)
            {
              buf[n] = 0;
              EZ_TextEndOfBuffer(tw);
              EZ_TextInsertString(tw,buf);
            }
        }
    }

    main(int ac, char **av)
    {
      EZ_Widget *textW;

      EZ_Initialize(ac,av,0);

      textW = EZ_CreateWidget(EZ_WIDGET_TEXT, frame,
                              EZ_TEXT_BACKGROUND, "white",
                              EZ_BORDER_WIDTH,     1,
                              EZ_SIZE,             300, 300,
                              0);

      EZ_DisplayWidget(textW);
      EZ_AddInput(0, EZ_READABLE_MASK, readStdin, textW);
      EZ_EventMainLoop();
    }
    /******************* ExampleInput ***************************************/

HTML Documentation Maintainance:`Arturo Espinosa <arturo@nuclecu.unam.mx>`