summaryrefslogtreecommitdiff
path: root/src/bin/main.c
blob: ae6ee45b18671b6766bf170204dad09f8c53b87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "gui_parser.h"
#include "simulator.h"
#include "generator.h"
#include "egui_log.h"
#include <Elementary.h>
#ifdef GUI_MODE
#include "egui_logic.h"
#endif

static Eina_Bool
_generate(const char *in_file, const char *out_path, Generator_Export_Format format)
{
   Gui_Context *ctx;
   const char *path;
   ctx = gui_context_new();
   if (!ctx)
     {
        ERR("Can not create context: %p", ctx);
        return EINA_FALSE;
     }

   if (!gui_parser_json_file_read(ctx, in_file))
     {
        gui_context_del(ctx);
        return EINA_FALSE;
     }

   char *out_dir = NULL;
   const char *out_file;
   char cur_dir[PATH_MAX];
   out_dir = ecore_file_dir_get(out_path);
   out_file = ecore_file_file_get(out_path);
   if (!strcmp(".", out_dir))
     {
        getcwd(cur_dir, PATH_MAX);
        path = cur_dir;
     }
   else
     {
        path = out_dir;
     }
   /* if new_project_name is no NULL, we want to put new project name. */
   if (format == GENERATE_JSON)
     {
        gui_context_project_path_set(ctx, path);
        gui_context_project_filename_set(ctx, out_file);
     }
   else
     {
        gui_context_export_path_set(ctx, path);
        gui_context_export_filename_set(ctx, out_file);
     }
   generator_ctx_source_generate(ctx, format);
   gui_context_del(ctx);
   free(out_dir);
   return EINA_TRUE;
}

static void
_population_zero_cb(Gui_Session *session, void *data)
{
   Gui_Context *ctx = data;
   session_target_win_unset(session);
   /* Remove session from list of sessions in progress. */
   gui_context_simulate_session_remove(ctx, session);
   session_del(session);
}

static int
_simulate(const char *filename, const char *target_name)
{
   Gui_Context *ctx;
   ctx = gui_context_new();;
   if (!ctx)
     {
        ERR("Can not create context: %p", ctx);
        goto end;
     }

   if (!gui_parser_json_file_read(ctx, filename))
     goto end;

   /* This session will be deleted by callbacks,
    * when simulated windows or target win will be closed. */
   Gui_Session *session = simulator_ctx_call(ctx, target_name);
   if (!session) goto end;
   session_population_zero_cb_set(session, _population_zero_cb, ctx);

   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
   elm_run();
   elm_exit();

end:
   gui_context_del(ctx);
   return 0;
}

/* Declare only this function,
 * because ffi_glue.h don't need to be included. */
Eina_Bool
_manager_init_check();

enum
{
   MODE_GUI,
   MODE_HELP,
   MODE_SIMULATE,
   MODE_EXPORT
};

int
main(int argc, char **argv)
{
   const char *new_filename = NULL, *target_name = NULL;
   char *filename = NULL;
   int mode = MODE_GUI;
   Generator_Export_Format export_format = GENERATE_NONE;

   static struct option long_options[] =
     {
          {"export", required_argument, 0, 'e'},
          {"simulate", required_argument, 0, 's'},
          {"help", no_argument, 0, 'h'},
          {0, 0, 0, 0}
     };
   int long_index = 0, opt;
   while ((opt = getopt_long(argc, argv, "hs:e:o:", long_options, &long_index)) != -1)
     {
        switch (opt)
          {
           case 0: break;
           case 'e':
                   {
                      mode = MODE_EXPORT;
                      if (!strcmp(optarg, "cpp"))
                        {
                           export_format = GENERATE_CPP;
                        }
                      else if (!strcmp(optarg, "c"))
                        {
                           export_format = GENERATE_C;
                        }
                      else if (!strcmp(optarg, "json"))
                        {
                           export_format = GENERATE_JSON;
                        }
                      else
                        {
                           mode = MODE_HELP;
                        }
                      break;
                   }
           case 'o':
                   {
                      new_filename = argv[optind - 1];
                      break;
                   }
           case 's':
                   {
                      target_name = argv[optind - 1];
                      mode = MODE_SIMULATE;
                      break;
                   }
           default:
                   {
                      mode = MODE_HELP;
                   }
          }
     };

   if (mode == MODE_HELP)
     {
        printf("Usage: %s [-h/--help] [-e/--export export_format] [-s/--simulate target_name] [-o OUTFILE] filename\n", argv[0]);
        printf("       --help/-h Print this help.\n");
        printf("       -o Provide output filename.\n");
        printf("       --export/-e Export format: (c, cpp, json).\n");
        printf("                                  - in case of \"json\" format OUTFILE will be generated;\n");
        printf("                                  - in case of other format, files with all required extensions will be generated.\n");
        printf("                                    T.e. OUTFILE.cpp/OUTFILE.hh for cpp.\n");
        printf("       --simulate/-s Target name: (Desktop, Phone, Tablet).\n");
        goto end;
     }

   if (argc == 1)
     {
        /* start Egui without input file. */
     }
   /* Filename must be the first argument after the options. */
   else if (optind != argc)
     {
        /* if file exists, everything is easy */
        if (ecore_file_exists(argv[optind]))
          {
             filename = ecore_file_realpath(argv[optind]);
             if (ecore_file_is_dir(filename))
               {
                  printf("File: \"%s\" is a directory\n", filename);
                  goto end;
               }
          }
        /* but if not, and user can provide it as "./filename" "~/filename" or "filename",
         * we need resolve path before last / */
        else
          {
             char dd[PATH_MAX] = {'\0'};
             char ff[PATH_MAX] = {'\0'};
             const char *p = strrchr(argv[optind], '/');
             if (p)
               {
                  strncpy(dd, argv[optind], p - argv[optind] + 1);
                  strcpy(ff, p + 1);
               }
             else
               {
                  strcpy(dd, "./");
                  strcpy(ff, argv[optind]);
               }
             if (!ecore_file_exists(dd))
               {
                  printf("Path can not exist: \"%s\"\n", argv[optind]);
                  goto end;
               }
             char *dd_realpath = ecore_file_realpath(dd);

             filename = malloc(strlen(dd_realpath) + 1 + strlen(ff) + 1); /* path/file\0 */
             sprintf(filename, "%s/%s", dd_realpath, ff);
             free(dd_realpath);
          }
     }
   else if (optind == argc)
     {
        printf("Error: probably input filename was not provided.\n");
        goto end;
     }


   if (!egui_init(PACKAGE_DATA_DIR"/config/func_names.json"))
     {
        EINA_LOG_ERR("Could not start egui");
        egui_shutdown();
        return 1;
     }
   _manager_init_check();

   if (mode == MODE_GUI)
     {
#ifdef GUI_MODE
        elm_init(argc, argv);
        egui_start();
        egui_project_open(filename);
        elm_run();
        egui_project_close();
        egui_stop();
        elm_shutdown();
#else
        printf("You are trying to use \"erigo_cmd\" in graphic mode. Nothing is going to happen :)\n");
#endif
     }
   else if (mode == MODE_SIMULATE)
     {
        if (!target_name) target_name = "Desktop";
        elm_init(argc, argv);
        _simulate(filename, target_name);
     }
   else if (mode == MODE_EXPORT)
     {
        _generate(filename, new_filename, export_format);
     }
   if (mode == MODE_SIMULATE)
     elm_shutdown();
   egui_shutdown();

end:
   if (filename) free(filename);
   return 0;
}