2005-10-06 06:37:08 +08:00
|
|
|
/** \file event.h
|
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
Functions for handling event triggers
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2007-10-02 18:09:37 +08:00
|
|
|
Because most of these functions can be called by signal
|
|
|
|
handler, it is important to make it well defined when these
|
|
|
|
functions produce output or perform memory allocations, since
|
|
|
|
such functions may not be safely called by signal handlers.
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
#ifndef FISH_EVENT_H
|
|
|
|
#define FISH_EVENT_H
|
|
|
|
|
2011-12-27 16:06:07 +08:00
|
|
|
#include "common.h"
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The signal number that is used to match any signal
|
|
|
|
*/
|
|
|
|
#define EVENT_ANY_SIGNAL -1
|
|
|
|
|
|
|
|
/**
|
|
|
|
The process id that is used to match any process id
|
|
|
|
*/
|
|
|
|
#define EVENT_ANY_PID 0
|
|
|
|
|
2005-12-15 21:59:02 +08:00
|
|
|
/**
|
|
|
|
Enumeration of event types
|
|
|
|
*/
|
2005-10-06 06:37:08 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
EVENT_ANY, /**< Matches any event type (Not always any event, as the function name may limit the choice as well */
|
|
|
|
EVENT_SIGNAL, /**< An event triggered by a signal */
|
|
|
|
EVENT_VARIABLE, /**< An event triggered by a variable update */
|
|
|
|
EVENT_EXIT, /**< An event triggered by a job or process exit */
|
2005-10-15 08:51:26 +08:00
|
|
|
EVENT_JOB_ID, /**< An event triggered by a job exit */
|
2007-08-20 00:42:30 +08:00
|
|
|
EVENT_GENERIC, /**< A generic event */
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
/**
|
|
|
|
The structure which represents an event. The event_t struct has
|
|
|
|
several event-related use-cases:
|
|
|
|
|
|
|
|
- When used as a parameter to event_add, it represents a class of events, and function_name is the name of the function which will be called whenever an event matching the specified class occurs. This is also how events are stored internally.
|
|
|
|
- When used as a parameter to event_get, event_remove and event_fire, it represents a class of events, and if the function_name field is non-zero, only events which call the specified function will be returned.
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
typedef struct
|
2005-10-06 06:37:08 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
Type of event
|
|
|
|
*/
|
|
|
|
int type;
|
2005-12-15 21:59:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The type-specific parameter
|
|
|
|
*/
|
2005-10-06 06:37:08 +08:00
|
|
|
union
|
|
|
|
{
|
|
|
|
/**
|
2005-12-12 06:21:01 +08:00
|
|
|
Signal number for signal-type events.Use EVENT_ANY_SIGNAL
|
|
|
|
to match any signal
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
int signal;
|
|
|
|
/**
|
2005-12-12 06:21:01 +08:00
|
|
|
Variable name for variable-type events.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
const wchar_t *variable;
|
|
|
|
/**
|
2005-12-12 06:21:01 +08:00
|
|
|
Process id for process-type events. Use EVENT_ANY_PID to
|
|
|
|
match any pid.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
pid_t pid;
|
2005-10-15 08:51:26 +08:00
|
|
|
/**
|
|
|
|
Job id for EVENT_JOB_ID type events
|
|
|
|
*/
|
|
|
|
int job_id;
|
2007-08-20 00:42:30 +08:00
|
|
|
/**
|
|
|
|
The parameter describing this generic event
|
|
|
|
*/
|
|
|
|
const wchar_t *param;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-12 03:31:16 +08:00
|
|
|
} param1;
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2005-12-04 03:46:18 +08:00
|
|
|
/**
|
|
|
|
The name of the event handler function
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
const wchar_t *function_name;
|
2005-12-12 06:21:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The argument list. Only used when sending a new event using
|
|
|
|
event_fire. In all other situations, the value of this variable
|
|
|
|
is ignored.
|
|
|
|
*/
|
2011-12-27 16:06:07 +08:00
|
|
|
wcstring_list_t *arguments;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
event_t;
|
|
|
|
|
|
|
|
/**
|
2011-12-27 11:18:46 +08:00
|
|
|
Add an event handler
|
2007-10-02 18:09:37 +08:00
|
|
|
|
|
|
|
May not be called by a signal handler, since it may allocate new memory.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
void event_add_handler( event_t *event );
|
|
|
|
|
|
|
|
/**
|
2011-12-27 11:18:46 +08:00
|
|
|
Remove all events matching the specified criterion.
|
2007-10-02 18:09:37 +08:00
|
|
|
|
|
|
|
May not be called by a signal handler, since it may free allocated memory.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
void event_remove( event_t *event );
|
|
|
|
|
|
|
|
/**
|
2011-12-27 11:18:46 +08:00
|
|
|
Return all events which match the specified event class
|
2005-10-06 06:37:08 +08:00
|
|
|
|
2007-04-17 04:10:53 +08:00
|
|
|
This function is safe to call from a signal handler _ONLY_ if the
|
|
|
|
out parameter is null.
|
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
\param criterion Is the class of events to return. If the criterion has a non-null function_name, only events which trigger the specified function will return.
|
2005-10-06 19:54:16 +08:00
|
|
|
\param out the list to add events to. May be 0, in which case no events will be added, but the result count will still be valid
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-10-06 19:54:16 +08:00
|
|
|
\return the number of found matches
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2005-10-06 19:54:16 +08:00
|
|
|
int event_get( event_t *criterion, array_list_t *out );
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Fire the specified event. The function_name field of the event must
|
|
|
|
be set to 0. If the event is of type EVENT_SIGNAL, no the event is
|
|
|
|
queued, and will be dispatched the next time event_fire is
|
|
|
|
called. If event is a null-pointer, all pending events are
|
|
|
|
dispatched.
|
|
|
|
|
2007-04-17 04:10:53 +08:00
|
|
|
This function is safe to call from a signal handler _ONLY_ if the
|
2007-10-02 18:09:37 +08:00
|
|
|
event parameter is for a signal. Signal events not be fired, by the
|
|
|
|
call to event_fire, instead they will be fired the next time
|
|
|
|
event_fire is called with anull argument. This is needed to make
|
|
|
|
sure that no code evaluation is ever performed by a signal handler.
|
2007-04-17 04:10:53 +08:00
|
|
|
|
2007-10-02 18:09:37 +08:00
|
|
|
\param event the specific event whose handlers should fire. If
|
|
|
|
null, then all delayed events will be fired.
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
2005-12-12 06:21:01 +08:00
|
|
|
void event_fire( event_t *event );
|
2005-10-06 06:37:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the event-handling library
|
|
|
|
*/
|
|
|
|
void event_init();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Destroy the event-handling library
|
|
|
|
*/
|
|
|
|
void event_destroy();
|
|
|
|
|
|
|
|
/**
|
2007-10-02 18:09:37 +08:00
|
|
|
Free all memory used by the specified event
|
2005-10-06 06:37:08 +08:00
|
|
|
*/
|
|
|
|
void event_free( event_t *e );
|
|
|
|
|
2006-02-01 23:49:11 +08:00
|
|
|
/**
|
|
|
|
Returns a string describing the specified event. The string should
|
|
|
|
not be freed.
|
|
|
|
*/
|
|
|
|
const wchar_t *event_get_desc( event_t *e );
|
|
|
|
|
2007-08-20 00:42:30 +08:00
|
|
|
/**
|
|
|
|
Fire a generic event with the specified name
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
#define event_fire_generic( ... ) event_fire_generic_internal( __VA_ARGS__, NULL )
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2008-01-15 06:31:24 +08:00
|
|
|
void event_fire_generic_internal(const wchar_t *name,...);
|
2007-08-20 00:42:30 +08:00
|
|
|
|
2005-10-06 06:37:08 +08:00
|
|
|
#endif
|