1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
25 #include "eina_config.h"
26 #include "eina_private.h"
27 #include "eina_types.h"
28 #include "eina_main.h"
29 #include "eina_error.h"
31 #include "eina_hash.h"
32 #include "eina_stringshare.h"
33 #include "eina_list.h"
34 #include "eina_matrixsparse.h"
35 #include "eina_array.h"
36 #include "eina_counter.h"
37 #include "eina_benchmark.h"
38 #include "eina_magic.h"
39 #include "eina_rectangle.h"
40 #include "eina_safety_checks.h"
42 /*============================================================================*
44 *============================================================================*/
50 static int _eina_main_count = 0;
51 static int _eina_log_dom = -1;
52 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
53 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
56 /* place module init/shutdown functions here to avoid other modules
57 * calling them by mistake.
59 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
78 struct eina_desc_setup
81 Eina_Bool (*init)(void);
82 Eina_Bool (*shutdown)(void);
85 static const struct eina_desc_setup _eina_desc_setup[] = {
86 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
87 /* log is a special case as it needs printf */
105 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
108 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
110 for (itr--; itr >= _eina_desc_setup; itr--)
112 if (!itr->shutdown())
113 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
116 eina_log_domain_unregister(_eina_log_dom);
125 /*============================================================================*
127 *============================================================================*/
129 /*============================================================================*
131 *============================================================================*/
134 * @addtogroup Eina_Main_Group Main
136 * @brief These functions provide general initialisation and shut down
143 * @brief Initialize the Eina library.
145 * @return 1 or greater on success, 0 on error.
147 * This function sets up all the eina modules. It returns 0 on
148 * failure (that is, when one of the module fails to initialize),
149 * otherwise it returns the number of times it has already been
152 * When Eina is not used anymore, call eina_shutdown() to shut down
158 const struct eina_desc_setup *itr, *itr_end;
160 if (EINA_LIKELY(_eina_main_count > 0))
161 return ++_eina_main_count;
163 if (!eina_log_init())
165 fprintf(stderr, "Could not initialize eina logging system.\n");
168 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
169 if (_eina_log_dom < 0)
171 EINA_LOG_ERR("Could not register log domain: eina");
176 itr = _eina_desc_setup;
177 itr_end = itr + _eina_desc_setup_len;
178 for (; itr < itr_end; itr++)
182 ERR("Could not initialize eina module '%s'.", itr->name);
183 _eina_shutdown_from_desc(itr);
188 _eina_main_count = 1;
193 * @brief Shut down the Eina library.
195 * @return 0 when all the modules is completely shut down, 1 or
198 * This function shuts down the Eina library. It returns 0 when it has
199 * been called the same number of times than eina_init(). In that case
200 * it shut down all the Eina modules.
202 * Once this function succeeds (that is, @c 0 is returned), you must
203 * not call any of the Eina function anymore. You must call
204 * eina_init() again to use the Eina functions again.
210 if (EINA_UNLIKELY(_eina_main_count == 0))
211 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
212 return _eina_main_count;