2016-04-29 02:19:50 +00:00
|
|
|
// Functions for handling event triggers
|
|
|
|
//
|
|
|
|
// 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.
|
2005-10-05 22:37:08 +00:00
|
|
|
#ifndef FISH_EVENT_H
|
|
|
|
#define FISH_EVENT_H
|
|
|
|
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <unistd.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2017-01-22 00:48:07 +00:00
|
|
|
#include <memory>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <vector>
|
2012-02-10 15:55:06 +00:00
|
|
|
|
2011-12-27 08:06:07 +00:00
|
|
|
#include "common.h"
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The signal number that is used to match any signal.
|
2005-10-05 22:37:08 +00:00
|
|
|
#define EVENT_ANY_SIGNAL -1
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The process id that is used to match any process id.
|
2005-10-05 22:37:08 +00:00
|
|
|
#define EVENT_ANY_PID 0
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Enumeration of event types.
|
|
|
|
enum {
|
|
|
|
/// Matches any event type (Not always any event, as the function name may limit the choice as
|
|
|
|
/// well.
|
|
|
|
EVENT_ANY,
|
|
|
|
/// An event triggered by a signal.
|
|
|
|
EVENT_SIGNAL,
|
|
|
|
/// An event triggered by a variable update.
|
|
|
|
EVENT_VARIABLE,
|
|
|
|
/// An event triggered by a job or process exit.
|
|
|
|
EVENT_EXIT,
|
|
|
|
/// An event triggered by a job exit.
|
|
|
|
EVENT_JOB_ID,
|
|
|
|
/// A generic event.
|
|
|
|
EVENT_GENERIC,
|
|
|
|
};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 02:19:50 +00: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.
|
|
|
|
struct event_t {
|
|
|
|
public:
|
|
|
|
/// Type of event.
|
|
|
|
int type;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The type-specific parameter. The int types are one of the following:
|
|
|
|
///
|
|
|
|
/// signal: Signal number for signal-type events.Use EVENT_ANY_SIGNAL to match any signal
|
2017-05-02 04:44:30 +00:00
|
|
|
/// pid: Process id for process-type events. Use EVENT_ANY_PID to match any pid. (Negative
|
|
|
|
/// values are used for PGIDs). job_id: Job id for EVENT_JOB_ID type events
|
2016-04-29 02:19:50 +00:00
|
|
|
union {
|
2012-02-09 03:02:25 +00:00
|
|
|
int signal;
|
|
|
|
int job_id;
|
|
|
|
pid_t pid;
|
|
|
|
} param1;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The string types are one of the following:
|
|
|
|
///
|
|
|
|
/// variable: Variable name for variable-type events.
|
|
|
|
/// param: The parameter describing this generic event.
|
2012-02-09 03:02:25 +00:00
|
|
|
wcstring str_param1;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// The name of the event handler function.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring function_name;
|
2005-12-11 22:21:01 +00:00
|
|
|
|
2016-04-29 02:19:50 +00: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.
|
2012-12-20 09:52:44 +00:00
|
|
|
wcstring_list_t arguments;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-02-28 03:38:15 +00:00
|
|
|
explicit event_t(int t);
|
2013-04-13 08:32:07 +00:00
|
|
|
~event_t();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-09 03:02:25 +00:00
|
|
|
static event_t signal_event(int sig);
|
|
|
|
static event_t variable_event(const wcstring &str);
|
|
|
|
static event_t generic_event(const wcstring &str);
|
2012-02-08 10:34:31 +00:00
|
|
|
};
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Add an event handler.
|
|
|
|
///
|
|
|
|
/// May not be called by a signal handler, since it may allocate new memory.
|
2012-12-20 10:48:36 +00:00
|
|
|
void event_add_handler(const event_t &event);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Remove all events matching the specified criterion.
|
|
|
|
///
|
|
|
|
/// May not be called by a signal handler, since it may free allocated memory.
|
2012-12-22 17:38:28 +00:00
|
|
|
void event_remove(const event_t &event);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Return all events which match the specified event class
|
|
|
|
///
|
|
|
|
/// This function is safe to call from a signal handler _ONLY_ if the out parameter is null.
|
|
|
|
///
|
|
|
|
/// \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.
|
|
|
|
/// \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
|
|
|
|
///
|
|
|
|
/// \return the number of found matches
|
2017-01-22 00:48:07 +00:00
|
|
|
int event_get(const event_t &criterion, std::vector<std::shared_ptr<event_t>> *out);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Returns whether an event listener is registered for the given signal. This is safe to call from
|
|
|
|
/// a signal handler.
|
2012-06-04 21:20:01 +00:00
|
|
|
bool event_is_signal_observed(int signal);
|
|
|
|
|
2016-04-29 02:19:50 +00: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.
|
|
|
|
///
|
|
|
|
/// This function is safe to call from a signal handler _ONLY_ if the 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.
|
|
|
|
///
|
|
|
|
/// \param event the specific event whose handlers should fire. If null, then all delayed events
|
|
|
|
/// will be fired.
|
2013-04-04 00:26:02 +00:00
|
|
|
void event_fire(const event_t *event);
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Like event_fire, but takes a signal directly.
|
2017-01-22 00:48:07 +00:00
|
|
|
/// May be called from signal handlers
|
2012-06-04 21:20:01 +00:00
|
|
|
void event_fire_signal(int signal);
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Initialize the event-handling library.
|
2005-10-05 22:37:08 +00:00
|
|
|
void event_init();
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Destroy the event-handling library.
|
2005-10-05 22:37:08 +00:00
|
|
|
void event_destroy();
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Returns a string describing the specified event.
|
2012-12-20 10:48:36 +00:00
|
|
|
wcstring event_get_desc(const event_t &e);
|
|
|
|
|
2016-04-29 02:19:50 +00:00
|
|
|
/// Fire a generic event with the specified name.
|
2012-12-20 00:11:55 +00:00
|
|
|
void event_fire_generic(const wchar_t *name, wcstring_list_t *args = NULL);
|
2007-08-19 16:42:30 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
#endif
|