mirror of
https://github.com/MidnightCommander/mc
synced 2024-12-22 04:22:34 +03:00
Added new engine for universal event system.
Example: gboolean mkdir_callback(const gchar *event_group, const gchar *event_name, gpointer init_data, gpointer event_data) { ... /* some action for handle event */ return TRUE; /* got chance to run other callbacks for this event or */ return FALSE; /* break execution for other callbacks */ } mc_event_add ("filesystem", "mkdir", mkdir_callback, "some init data", NULL); mc_event_add ("filesystem", "mkdir", mkdir_log_callback, "some init data", NULL); mc_event_add ("filesystem", "mkdir", third_callback, NULL, NULL); /* Last add - first run. In this case execution order is * third_callback * mkdir_log_callback * mkdir_callback if third_callback fuctions will return FALSE, then other callbacks (mkdir_log_callback and mkdir_callback) never run! */ ... /* some code */ /* for example, after pressing F7 */ mc_event_raise("filesystem", "mkdir", "some event_data... path to directory"); Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
parent
fc7142d2a0
commit
a5195d285a
@ -543,6 +543,7 @@ src/diffviewer/Makefile
|
||||
src/filemanager/Makefile
|
||||
|
||||
lib/Makefile
|
||||
lib/event/Makefile
|
||||
lib/filehighlight/Makefile
|
||||
lib/mcconfig/Makefile
|
||||
lib/search/Makefile
|
||||
|
@ -1,8 +1,9 @@
|
||||
SUBDIRS = filehighlight mcconfig search skin tty vfs strutil widget
|
||||
SUBDIRS = event filehighlight mcconfig search skin tty vfs strutil widget
|
||||
|
||||
noinst_LTLIBRARIES = libmc.la
|
||||
|
||||
SUBLIB_includes = \
|
||||
event.h \
|
||||
filehighlight.h \
|
||||
mcconfig.h \
|
||||
search.h \
|
||||
@ -40,6 +41,7 @@ endif
|
||||
libmc_la_CFLAGS= $(GLIB_CFLAGS) -I$(top_srcdir) $(PCRE_CFLAGS)
|
||||
|
||||
libmc_la_LIBADD = \
|
||||
event/libmcevent.la \
|
||||
filehighlight/libmcfilehighlight.la \
|
||||
mcconfig/libmcconfig.la \
|
||||
search/libsearch.la \
|
||||
|
43
lib/event.h
Normal file
43
lib/event.h
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef MC__EVENT_H
|
||||
#define MC__EVENT_H
|
||||
|
||||
/*** typedefs(not structures) and defined constants **********************************************/
|
||||
|
||||
typedef gboolean (*mc_event_callback_func_t) (const gchar *, const gchar *, gpointer, gpointer);
|
||||
|
||||
/*** enums ***************************************************************************************/
|
||||
|
||||
/*** structures declarations (and typedefs of structures)*****************************************/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *event_group_name;
|
||||
const char *event_name;
|
||||
mc_event_callback_func_t cb;
|
||||
gpointer init_data;
|
||||
} event_init_t;
|
||||
|
||||
/*** global variables defined in .c file *********************************************************/
|
||||
|
||||
/*** declarations of public functions ************************************************************/
|
||||
|
||||
/* event.c: */
|
||||
gboolean mc_event_init (GError **);
|
||||
gboolean mc_event_deinit (GError **);
|
||||
|
||||
|
||||
/* manage.c: */
|
||||
gboolean mc_event_add (const gchar *, const gchar *, mc_event_callback_func_t, gpointer, GError **);
|
||||
void mc_event_del (const gchar *, const gchar *, mc_event_callback_func_t);
|
||||
void mc_event_destroy (const gchar *, const gchar *);
|
||||
void mc_event_group_del (const gchar *);
|
||||
gboolean mc_event_present (const gchar *, const gchar *);
|
||||
gboolean mc_event_mass_add (event_init_t *, GError **);
|
||||
|
||||
/* raise.c: */
|
||||
gboolean mc_event_raise (const gchar *, const gchar *, gpointer);
|
||||
|
||||
|
||||
/*** inline functions ****************************************************************************/
|
||||
|
||||
#endif /* MC__EVENT_H */
|
12
lib/event/Makefile.am
Normal file
12
lib/event/Makefile.am
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
noinst_LTLIBRARIES = libmcevent.la
|
||||
|
||||
libmcevent_la_SOURCES = \
|
||||
event.c \
|
||||
internal.h \
|
||||
manage.c \
|
||||
raise.c
|
||||
|
||||
libmcevent_la_CFLAGS=-I$(top_srcdir) \
|
||||
$(GLIB_CFLAGS) $(PCRE_CFLAGS) \
|
||||
-DDATADIR=\""$(pkgdatadir)/"\" -DLOCALEDIR=\""$(localedir)"\"
|
135
lib/event/event.c
Normal file
135
lib/event/event.c
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
Handle events in application.
|
||||
Interface functions: init/deinit; start/stop
|
||||
|
||||
Copyright (C) 2011 The Free Software Foundation, Inc.
|
||||
|
||||
Written by:
|
||||
Slava Zanko <slavazanko@gmail.com>, 2011.
|
||||
|
||||
This file is part of the Midnight Commander.
|
||||
|
||||
The Midnight Commander is free software; you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
The Midnight Commander is distributed in the hope that it will be
|
||||
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "lib/global.h"
|
||||
#include "lib/event.h"
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*** global variables ****************************************************************************/
|
||||
|
||||
/*** file scope macro definitions ****************************************************************/
|
||||
|
||||
/*** file scope type declarations ****************************************************************/
|
||||
|
||||
/*** file scope variables ************************************************************************/
|
||||
|
||||
GTree *mc_event_grouplist = NULL;
|
||||
|
||||
/*** file scope functions ************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/*** public functions ****************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_init (GError ** mcerror)
|
||||
{
|
||||
if (mc_event_grouplist != NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 1,
|
||||
_("Event system already initialized")));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
mc_event_grouplist =
|
||||
g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
|
||||
NULL, (GDestroyNotify) g_free, (GDestroyNotify) g_tree_destroy);
|
||||
|
||||
if (mc_event_grouplist == NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 2,
|
||||
_("Failed to initialize event system")));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_deinit (GError ** mcerror)
|
||||
{
|
||||
if (mc_event_grouplist == NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 1,
|
||||
_("Event system not initialized")));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_tree_destroy (mc_event_grouplist);
|
||||
mc_event_grouplist = NULL;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_mass_add (event_init_t * events, GError ** mcerror)
|
||||
{
|
||||
size_t array_index;
|
||||
|
||||
for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
|
||||
{
|
||||
if (!mc_event_add (events[array_index].event_group_name,
|
||||
events[array_index].event_name,
|
||||
events[array_index].cb, events[array_index].init_data, mcerror))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_present (const gchar *event_group_name, const gchar *event_name)
|
||||
{
|
||||
GTree *event_group;
|
||||
GPtrArray *callbacks;
|
||||
|
||||
if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
|
||||
return FALSE;
|
||||
|
||||
event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
|
||||
if (event_group == NULL)
|
||||
return FALSE;
|
||||
|
||||
callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
|
||||
if (callbacks == NULL)
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
31
lib/event/internal.h
Normal file
31
lib/event/internal.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef MC_EVENT_INTERNAL_H
|
||||
#define MC_EVENT_INTERNAL_H
|
||||
|
||||
/*** typedefs(not structures) and defined constants ********************/
|
||||
|
||||
/*** enums *************************************************************/
|
||||
|
||||
/*** structures declarations (and typedefs of structures)***************/
|
||||
|
||||
typedef struct mc_event_callback_struct
|
||||
{
|
||||
gpointer init_data;
|
||||
mc_event_callback_func_t callback;
|
||||
} mc_event_callback_t;
|
||||
|
||||
|
||||
/*** global variables defined in .c file *******************************/
|
||||
|
||||
extern GTree *mc_event_grouplist;
|
||||
|
||||
/*** declarations of public functions **********************************/
|
||||
|
||||
GTree *mc_event_get_event_group_by_name (const gchar * event_group_name, gboolean create_new,
|
||||
GError ** mcerror);
|
||||
GPtrArray *mc_event_get_event_by_name (GTree * event_group, const gchar * event_name,
|
||||
gboolean create_new, GError ** mcerror);
|
||||
mc_event_callback_t *mc_event_is_callback_in_array (GPtrArray * callbacks,
|
||||
mc_event_callback_func_t event_callback);
|
||||
|
||||
/*** inline functions ****************************************************************************/
|
||||
#endif /* MC_EVENT_INTERNAL_H */
|
224
lib/event/manage.c
Normal file
224
lib/event/manage.c
Normal file
@ -0,0 +1,224 @@
|
||||
/*
|
||||
Handle any events in application.
|
||||
Manage events: add, delete, destroy, search
|
||||
|
||||
Copyright (C) 2011 The Free Software Foundation, Inc.
|
||||
|
||||
Written by:
|
||||
Slava Zanko <slavazanko@gmail.com>, 2011.
|
||||
|
||||
This file is part of the Midnight Commander.
|
||||
|
||||
The Midnight Commander is free software; you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
The Midnight Commander is distributed in the hope that it will be
|
||||
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "lib/global.h"
|
||||
#include "lib/event.h"
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*** global variables ****************************************************************************/
|
||||
|
||||
/*** file scope macro definitions ****************************************************************/
|
||||
|
||||
/*** file scope type declarations ****************************************************************/
|
||||
|
||||
/*** file scope variables ************************************************************************/
|
||||
|
||||
/*** file scope functions ************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
mc_event_group_destroy_value (gpointer data)
|
||||
{
|
||||
GPtrArray *callbacks;
|
||||
|
||||
callbacks = (GPtrArray *) data;
|
||||
g_ptr_array_foreach (callbacks, (GFunc) g_free, NULL);
|
||||
g_ptr_array_free (callbacks, TRUE);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/*** public functions ****************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_add (const gchar * event_group_name, const gchar * event_name,
|
||||
mc_event_callback_func_t event_callback, gpointer event_init_data, GError ** mcerror)
|
||||
{
|
||||
|
||||
GTree *event_group;
|
||||
GPtrArray *callbacks;
|
||||
mc_event_callback_t *cb;
|
||||
|
||||
if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
|
||||
|| event_callback == NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 1,
|
||||
_("Check input data! Some of parameters are NULL!")));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
event_group = mc_event_get_event_group_by_name (event_group_name, TRUE, mcerror);
|
||||
if (event_group == NULL)
|
||||
return FALSE;
|
||||
|
||||
callbacks = mc_event_get_event_by_name (event_group, event_name, TRUE, mcerror);
|
||||
if (callbacks == NULL)
|
||||
return FALSE;
|
||||
|
||||
cb = mc_event_is_callback_in_array (callbacks, event_callback);
|
||||
if (cb == NULL)
|
||||
{
|
||||
cb = g_new0 (mc_event_callback_t, 1);
|
||||
cb->callback = event_callback;
|
||||
g_ptr_array_add (callbacks, (gpointer) cb);
|
||||
}
|
||||
cb->init_data = event_init_data;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
void
|
||||
mc_event_del (const gchar * event_group_name, const gchar * event_name,
|
||||
mc_event_callback_func_t event_callback)
|
||||
{
|
||||
GTree *event_group;
|
||||
GPtrArray *callbacks;
|
||||
mc_event_callback_t *cb;
|
||||
|
||||
if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL
|
||||
|| event_callback == NULL)
|
||||
return;
|
||||
|
||||
event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
|
||||
if (event_group == NULL)
|
||||
return;
|
||||
|
||||
callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
|
||||
if (callbacks == NULL)
|
||||
return;
|
||||
|
||||
cb = mc_event_is_callback_in_array (callbacks, event_callback);
|
||||
|
||||
if (cb == NULL)
|
||||
return;
|
||||
|
||||
g_ptr_array_remove (callbacks, (gpointer) cb);
|
||||
g_free ((gpointer) cb);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
void
|
||||
mc_event_destroy (const gchar * event_group_name, const gchar * event_name)
|
||||
{
|
||||
GTree *event_group;
|
||||
|
||||
if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
|
||||
return;
|
||||
|
||||
event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
|
||||
g_tree_remove (event_group, (gconstpointer) event_name);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
void
|
||||
mc_event_group_del (const gchar * event_group_name)
|
||||
{
|
||||
|
||||
if (mc_event_grouplist != NULL && event_group_name != NULL)
|
||||
g_tree_remove (mc_event_grouplist, (gconstpointer) event_group_name);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
GTree *
|
||||
mc_event_get_event_group_by_name (const gchar * event_group_name, gboolean create_new,
|
||||
GError ** mcerror)
|
||||
{
|
||||
GTree *event_group;
|
||||
|
||||
event_group = (GTree *) g_tree_lookup (mc_event_grouplist, (gconstpointer) event_group_name);
|
||||
if (event_group == NULL && create_new)
|
||||
{
|
||||
event_group =
|
||||
g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp,
|
||||
NULL,
|
||||
(GDestroyNotify) g_free,
|
||||
(GDestroyNotify) mc_event_group_destroy_value);
|
||||
if (event_group == NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 1,
|
||||
_("Unable to create group '%s' for events!"),
|
||||
event_group_name));
|
||||
return NULL;
|
||||
}
|
||||
g_tree_insert (mc_event_grouplist, g_strdup (event_group_name), (gpointer) event_group);
|
||||
}
|
||||
return event_group;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
GPtrArray *
|
||||
mc_event_get_event_by_name (GTree * event_group, const gchar * event_name, gboolean create_new,
|
||||
GError ** mcerror)
|
||||
{
|
||||
GPtrArray *callbacks;
|
||||
|
||||
callbacks = (GPtrArray *) g_tree_lookup (event_group, (gconstpointer) event_name);
|
||||
if (callbacks == NULL && create_new)
|
||||
{
|
||||
callbacks = g_ptr_array_new ();
|
||||
if (callbacks == NULL)
|
||||
{
|
||||
g_propagate_error (mcerror,
|
||||
g_error_new (mc_main_error_quark (), 1,
|
||||
_("Unable to create event '%s'!"), event_name));
|
||||
return NULL;
|
||||
}
|
||||
g_tree_insert (event_group, g_strdup (event_name), (gpointer) callbacks);
|
||||
}
|
||||
return callbacks;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
mc_event_callback_t *
|
||||
mc_event_is_callback_in_array (GPtrArray * callbacks, mc_event_callback_func_t event_callback)
|
||||
{
|
||||
guint array_index;
|
||||
|
||||
for (array_index = 0; array_index < callbacks->len; array_index++)
|
||||
{
|
||||
mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index);
|
||||
if (cb->callback == event_callback)
|
||||
return cb;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
76
lib/event/raise.c
Normal file
76
lib/event/raise.c
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
Handle any events in application.
|
||||
Raise events.
|
||||
|
||||
Copyright (C) 2011 The Free Software Foundation, Inc.
|
||||
|
||||
Written by:
|
||||
Slava Zanko <slavazanko@gmail.com>, 2011.
|
||||
|
||||
This file is part of the Midnight Commander.
|
||||
|
||||
The Midnight Commander is free software; you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
The Midnight Commander is distributed in the hope that it will be
|
||||
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "lib/global.h"
|
||||
#include "lib/event.h"
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*** global variables ****************************************************************************/
|
||||
|
||||
/*** file scope macro definitions ****************************************************************/
|
||||
|
||||
/*** file scope type declarations ****************************************************************/
|
||||
|
||||
/*** file scope variables ************************************************************************/
|
||||
|
||||
/*** file scope functions ************************************************************************/
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/*** public functions ****************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
gboolean
|
||||
mc_event_raise (const gchar * event_group_name, const gchar * event_name, gpointer event_data)
|
||||
{
|
||||
GTree *event_group;
|
||||
GPtrArray *callbacks;
|
||||
guint array_index;
|
||||
|
||||
if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
|
||||
return FALSE;
|
||||
|
||||
event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
|
||||
if (event_group == NULL)
|
||||
return FALSE;
|
||||
|
||||
callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
|
||||
if (callbacks == NULL)
|
||||
return FALSE;
|
||||
|
||||
for (array_index = callbacks->len; array_index > 0; array_index--)
|
||||
{
|
||||
mc_event_callback_t *cb = g_ptr_array_index (callbacks, array_index - 1);
|
||||
if (!(*cb->callback) (event_group_name, event_name, cb->init_data, event_data))
|
||||
break;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
Loading…
Reference in New Issue
Block a user