Add installer plugin and tool for create a NI
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / main.cc
1 #include "dotnet/dotnet_launcher.h"
2 #include "mono/mono_launcher.h"
3 #include "utils.h"
4 #include "log.h"
5
6 #include <cstdio>
7 #include <vector>
8 #include <memory>
9
10 #include <Ecore.h>
11 #include <Eina.h>
12 #include <aul.h>
13
14 #define __XSTR(x) #x
15 #define __STR(x) __XSTR(x)
16
17 #ifndef VERSION
18 #define LAUNCHER_VERSION_STR "-Unknown-"
19 #else
20 #define LAUNCHER_VERSION_STR __STR(VERSION)
21 #endif
22
23 static std::string VersionOption("--version");
24 static std::string StandaloneOption("--standalone");
25
26 int main(int argc, char *argv[])
27 {
28   int i;
29   bool standalone = false;
30   const char* standalonePath = nullptr;
31
32   std::vector<char*> vargs;
33
34   for (i=0; i<argc; i++)
35   {
36     if (VersionOption.compare(argv[i]) == 0)
37     {
38       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
39       return 0;
40     }
41     else if (StandaloneOption.compare(argv[i]) == 0)
42     {
43       standalone = true;
44
45       if (i > argc-1)
46       {
47         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
48         return 1;
49       }
50       i++;
51       standalonePath = argv[i];
52     }
53     else
54     {
55       vargs.push_back(argv[i]);
56     }
57   }
58
59   using tizen::runtime::LauncherInterface;
60   using tizen::runtime::Launchpad;
61   using tizen::runtime::AppInfo;
62   std::unique_ptr<LauncherInterface> runtime;
63
64   bool useMono = !FileNotExist("/etc/.use_mono");
65
66   if (!useMono)
67   {
68     using tizen::runtime::dotnetcore::CoreRuntime;
69     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
70     runtime = std::move(coreRuntime);
71
72     _DBG("##### CoreCLR Launcher ######");
73   }
74   else
75   {
76     using tizen::runtime::mono::MonoRuntime;
77     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
78     runtime = std::move(monoRuntime);
79
80     _DBG("##### Mono Launcher ######");
81   }
82
83   if (standalone)
84   {
85     _DBG("##### Run it standalone #########");
86     const char* appid = getenv("AUL_APPID");
87     _DBG("AUL_APPID : %s", appid);
88     std::string approot;
89     if (appid != nullptr)
90     {
91       const char* approot_path = aul_get_app_root_path();
92       if (approot_path != nullptr)
93       {
94         approot = std::string(approot_path);
95       }
96     }
97     if (approot.empty())
98     {
99       approot = Basename(standalonePath);
100     }
101     if (runtime->Initialize(true) != 0)
102     {
103       _ERR("Failed to initialize");
104       return 1;
105     }
106
107     int args_len = vargs.size();
108     char** args = &vargs[0];
109     if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
110     {
111         _ERR("Failed to launch");
112         return 0;
113     }
114   }
115   else
116   {
117     Launchpad.OnCreate = [&runtime]()
118     {
119       if (runtime->Initialize(false) != 0)
120       {
121         _ERR("Failed to initialized");
122       }
123       else
124       {
125         auto idle_task = [](void *data) -> Eina_Bool
126         {
127           LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
128           if (runtime->RunManagedLauncher() != 0)
129           {
130             _ERR("Failed to run managed launcher");
131           }
132           return ECORE_CALLBACK_CANCEL;
133         };
134         ecore_idler_add(idle_task, runtime.get());
135       }
136     };
137
138     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
139     {
140       _DBG("terminated with app path : %s", info.path.c_str());
141       _DBG("appid : %s", info.id.c_str());
142       _DBG("pkg : %s", info.pkg.c_str());
143       _DBG("type : %s", info.type.c_str());
144
145       if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
146       {
147         _ERR("Failed to launch");
148       }
149     };
150     Launchpad.LoaderMain(argc, argv);
151   }
152
153   return 0;
154 }