1 /*
2 Handle events in application.
3 Interface functions: init/deinit; start/stop
4
5 Copyright (C) 2011-2025
6 Free Software Foundation, Inc.
7
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2011.
10
11 This file is part of the Midnight Commander.
12
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
17
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27 #include <config.h>
28
29 #include "lib/global.h"
30 #include "lib/util.h"
31 #include "lib/event.h"
32
33 #include "internal.h"
34
35 /*** global variables ****************************************************************************/
36
37 /*** file scope macro definitions ****************************************************************/
38
39 /*** file scope type declarations ****************************************************************/
40
41 /*** file scope variables ************************************************************************/
42
43 GTree *mc_event_grouplist = NULL;
44
45 /*** file scope functions ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
47 /*** public functions ****************************************************************************/
48 /* --------------------------------------------------------------------------------------------- */
49
50 gboolean
51 mc_event_init (GError **mcerror)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
52 {
53 mc_return_val_if_error (mcerror, FALSE);
54
55 if (mc_event_grouplist != NULL)
56 {
57 mc_propagate_error (mcerror, 0, "%s", _ ("Event system already initialized"));
58 return FALSE;
59 }
60
61 mc_event_grouplist = g_tree_new_full ((GCompareDataFunc) g_ascii_strcasecmp, NULL,
62 (GDestroyNotify) g_free, (GDestroyNotify) g_tree_destroy);
63
64 if (mc_event_grouplist == NULL)
65 {
66 mc_propagate_error (mcerror, 0, "%s", _ ("Failed to initialize event system"));
67 return FALSE;
68 }
69
70 return TRUE;
71 }
72
73 /* --------------------------------------------------------------------------------------------- */
74
75 gboolean
76 mc_event_deinit (GError **mcerror)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
77 {
78 mc_return_val_if_error (mcerror, FALSE);
79
80 if (mc_event_grouplist == NULL)
81 {
82 mc_propagate_error (mcerror, 0, "%s", _ ("Event system not initialized"));
83 return FALSE;
84 }
85
86 g_tree_destroy (mc_event_grouplist);
87 mc_event_grouplist = NULL;
88 return TRUE;
89 }
90
91 /* --------------------------------------------------------------------------------------------- */
92
93 gboolean
94 mc_event_mass_add (const event_init_t *events, GError **mcerror)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
95 {
96 size_t array_index;
97
98 mc_return_val_if_error (mcerror, FALSE);
99
100 for (array_index = 0; events[array_index].event_group_name != NULL; array_index++)
101 {
102 if (!mc_event_add (events[array_index].event_group_name, events[array_index].event_name,
103 events[array_index].cb, events[array_index].init_data, mcerror))
104 {
105 return FALSE;
106 }
107 }
108 return TRUE;
109 }
110
111 /* --------------------------------------------------------------------------------------------- */
112
113 gboolean
114 mc_event_present (const gchar *event_group_name, const gchar *event_name)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
115 {
116 GTree *event_group;
117 GPtrArray *callbacks;
118
119 if (mc_event_grouplist == NULL || event_group_name == NULL || event_name == NULL)
120 return FALSE;
121
122 event_group = mc_event_get_event_group_by_name (event_group_name, FALSE, NULL);
123 if (event_group == NULL)
124 return FALSE;
125
126 callbacks = mc_event_get_event_by_name (event_group, event_name, FALSE, NULL);
127 if (callbacks == NULL)
128 return FALSE;
129
130 return TRUE;
131 }
132
133 /* --------------------------------------------------------------------------------------------- */