HomeManualTopicsTypesSymbols

Input-Output Events

Description

Events are the Funky way to handle multiple input/output streams in a parallel way.

The event system is based on the concept of an event "loop". The event "loop" is part of the application program.

Before entering the event loop, the application program must create an input-output object and register all input-output streams that should be handled by the event loop.

To create an input-output object, simply clone the object std_types::io.

The event loop then waits for an event to occur. When an event occurs, the event loop calls the event handler function associated with the event.

The event handler function can then process the event and return to the event loop.

The event loop continues to wait for the next event.

Possible events can be registered or unregistered at any time.

To register an event call

std::start_reading_from - for reading from a stream

std::start_catching_signals - for catching signals like SIGINT

std::start_injecting_window_change_events - for catching window change events

To unregister an event call

std::stop_reading_from - to stop reading from a stream

std::stop_catching_signals - to stop catching signals

std::stop_injecting_window_change_events - to stop injecting window change events

In the event loop call

std::get_events - to get one or more events

Actual input operations are performed by std::get_events. Write operations are initiated by the application program and performed by std::get_events.

Example

$io std_types::io # create an input-output object start_reading_from &io STDIN_FILENO # register reading from STDIN write &io STDOUT_FILENO "Please enter some text:@nl;" # write a prompt text to STDOUT $input_buffer "" # create an initially empty input buffer loop: # the event loop get_events! &io $events # get one or more events for_each events # handle all received events : (event) event $type $fd $argument # destructure the event case type # check the event type READ: # event is a read event case fd # check the file descriptor STDIN_FILENO: # it was a read event from STDIN append &input_buffer argument # append the received data to the input buffer handle_input !io &input_buffer # call the input handler if io.is_undefined: # the input handler returned undefined to indicate that the input processing is finished terminate! # terminate the program next # otherwise continue processing the events next # ignore read events for other file descriptors READ_ERROR: # event is a read error Error! "Error reading from standard input!" WRITE_PENDING: # event is a write pending event # ignore next! # continue processing the events WRITE_COMPLETED: # event is a write completed event # ignore next! # continue processing the events next # ignore all other events next # check for more events $handle_input: (buffer) loop: if buffer .contains. '@nl;': # the input buffer contains at least one complete line $line buffer .before. '@nl;' # extract the line if line == "quit" # the user entered "quit" -> undefined undefined # return undefined to indicate that the input processing is finished : # Output the line write &io STDOUT_FILENO " You entered: "@(line)" behind &buffer '@nl;' # remove the processed line from the input buffer next # continue processing the buffer -> io buffer # return the (maybe) modified input-output object and buffer

See also

Input-Output
Input-Output Task Manager
std::PROCESS_JOINED used to tag "process joined" events
std::READ used to tag "read" events
std::READ_ERROR used to tag "read error" events
std::SIGNAL used to tag "signal" events
std::WRITE_COMPLETED used to tag "write completed" events
std::WRITE_ERROR used to tag "write error" events
std::WRITE_PENDING used to tag "write pending" events
std::cancel_write cancels a write operation
std::get_events gets one or more events
std::join_process joins a process
std::start_catching_signals starts catching signals
std::start_injecting_window_change_events starts receiving window change events
std::start_reading_from registers a read request for a file descriptor
std::stop_catching_signals stops catching signals
std::stop_injecting_window_change_events stops receiving window change events
std::stop_reading_from unregisters a read request for a file descriptor
std_types::event the prototype for all events
std_types::io the prototype for all input-output objects
std_types::io/std::cancel_write cancels a write operation
std_types::io/std::get_events gets one or more events
std_types::io/std::is_empty checks if the input-output object contains any event registrations
std_types::io/std::is_not_empty checks if the input-output object contains any event registrations
std_types::io/std::join_process joins a process
std_types::io/std::start_catching_signals starts catching signals
std_types::io/std::start_injecting_window_change_events starts receiving window change events
std_types::io/std::start_reading_from registers a read reqest for a file descriptor
std_types::io/std::stop_catching_signals stops catching signals
std_types::io/std::stop_injecting_window_change_events stops receiving window change events
std_types::io/std::stop_reading_from unregisters a read request for a file descriptor
std_types::io/std::write writes data to a stream