Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore / ecore_app.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #ifndef _MSC_VER
8 # include <unistd.h>
9 #else
10 # include <process.h>
11 #endif
12
13 #ifdef HAVE_EVIL
14 # include <Evil.h>
15 #endif
16
17 #include "Ecore.h"
18 #include "ecore_private.h"
19
20 static int app_argc = 0;
21 static char **app_argv = NULL;
22
23 /**
24  * @addtogroup Ecore_Application_Group
25  *
26  * @{
27  */
28
29 /**
30  * Set up the programs command-line arguments.
31  * @param argc The same as passed as argc to the programs main() function
32  * @param argv The same as passed as argv to the programs main() function
33  *
34  * A call to this function will store the programs command-line arguments
35  * for later use by ecore_app_restart() or ecore_app_args_get().
36  */
37 EAPI void
38 ecore_app_args_set(int          argc,
39                    const char **argv)
40 {
41    EINA_MAIN_LOOP_CHECK_RETURN;
42
43    if ((argc < 1) ||
44        (!argv)) return;
45    app_argc = argc;
46    app_argv = (char **)argv;
47 }
48
49 /**
50  * Return the programs stored command-line arguments.
51  * @param argc A pointer to the return value to hold argc
52  * @param argv A pointer to the return value to hold argv
53  *
54  * When called, this funciton returns the arguments for the program stored by
55  * ecore_app_args_set(). The integer pointed to by @p argc will be filled, if
56  * the pointer is not NULL, and the string array pointer @p argv will be filled
57  * also if the pointer is not NULL. The values they are filled with will be the
58  * same set by ecore_app_args_set().
59  */
60 EAPI void
61 ecore_app_args_get(int    *argc,
62                    char ***argv)
63 {
64    EINA_MAIN_LOOP_CHECK_RETURN;
65
66    if (argc) *argc = app_argc;
67    if (argv) *argv = app_argv;
68 }
69
70 /**
71  * Restart the program executable with the command-line arguments stored.
72  *
73  * This function will restart & re-execute this program in place of itself
74  * using the command-line arguments stored by ecore_app_args_set(). This is
75  * an easy way for a program to restart itself for cleanup purposes,
76  * configuration reasons or in the event of a crash.
77  */
78 EAPI void
79 ecore_app_restart(void)
80 {
81    EINA_MAIN_LOOP_CHECK_RETURN;
82 #ifdef HAVE_EXECVP
83    char *args[4096];
84    int i;
85
86    if ((app_argc < 1) || (!app_argv)) return;
87    if (app_argc >= 4096) return;
88    for (i = 0; i < app_argc; i++) args[i] = app_argv[i];
89    args[i] = NULL;
90    execvp(app_argv[0], args);
91 #endif
92 }
93
94 /**
95  * @}
96  */