Add License file and comments to sources
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / main.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "dotnet/dotnet_launcher.h"
18 #include "mono/mono_launcher.h"
19 #include "utils.h"
20 #include "log.h"
21
22 #include <cstdio>
23 #include <vector>
24 #include <memory>
25
26 #include <Ecore.h>
27 #include <Eina.h>
28 #include <aul.h>
29
30 #define __XSTR(x) #x
31 #define __STR(x) __XSTR(x)
32
33 #ifndef VERSION
34 #define LAUNCHER_VERSION_STR "-Unknown-"
35 #else
36 #define LAUNCHER_VERSION_STR __STR(VERSION)
37 #endif
38
39 static std::string VersionOption("--version");
40 static std::string StandaloneOption("--standalone");
41
42 int main(int argc, char *argv[])
43 {
44   int i;
45   bool standalone = false;
46   const char* standalonePath = nullptr;
47
48   std::vector<char*> vargs;
49
50   for (i=0; i<argc; i++)
51   {
52     if (VersionOption.compare(argv[i]) == 0)
53     {
54       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
55       return 0;
56     }
57     else if (StandaloneOption.compare(argv[i]) == 0)
58     {
59       standalone = true;
60
61       if (i > argc-1)
62       {
63         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
64         return 1;
65       }
66       i++;
67       standalonePath = argv[i];
68     }
69     else
70     {
71       vargs.push_back(argv[i]);
72     }
73   }
74
75   using tizen::runtime::LauncherInterface;
76   using tizen::runtime::Launchpad;
77   using tizen::runtime::AppInfo;
78   std::unique_ptr<LauncherInterface> runtime;
79
80   bool useMono = !FileNotExist("/etc/.use_mono");
81
82   if (!useMono)
83   {
84     using tizen::runtime::dotnetcore::CoreRuntime;
85     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
86     runtime = std::move(coreRuntime);
87
88     _DBG("##### CoreCLR Launcher ######");
89   }
90   else
91   {
92     using tizen::runtime::mono::MonoRuntime;
93     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
94     runtime = std::move(monoRuntime);
95
96     _DBG("##### Mono Launcher ######");
97   }
98
99   if (standalone)
100   {
101     _DBG("##### Run it standalone #########");
102     const char* appid = getenv("AUL_APPID");
103     _DBG("AUL_APPID : %s", appid);
104     std::string approot;
105     if (appid != nullptr)
106     {
107       const char* approot_path = aul_get_app_root_path();
108       if (approot_path != nullptr)
109       {
110         approot = std::string(approot_path);
111       }
112     }
113     if (approot.empty())
114     {
115       approot = Basename(standalonePath);
116     }
117     if (runtime->Initialize(true) != 0)
118     {
119       _ERR("Failed to initialize");
120       return 1;
121     }
122
123     int args_len = vargs.size();
124     char** args = &vargs[0];
125     if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
126     {
127         _ERR("Failed to launch");
128         return 0;
129     }
130   }
131   else
132   {
133     Launchpad.OnCreate = [&runtime]()
134     {
135       if (runtime->Initialize(false) != 0)
136       {
137         _ERR("Failed to initialized");
138       }
139       else
140       {
141         auto idle_task = [](void *data) -> Eina_Bool
142         {
143           LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
144           if (runtime->RunManagedLauncher() != 0)
145           {
146             _ERR("Failed to run managed launcher");
147           }
148           return ECORE_CALLBACK_CANCEL;
149         };
150         ecore_idler_add(idle_task, runtime.get());
151       }
152     };
153
154     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
155     {
156       _DBG("terminated with app path : %s", info.path.c_str());
157       _DBG("appid : %s", info.id.c_str());
158       _DBG("pkg : %s", info.pkg.c_str());
159       _DBG("type : %s", info.type.c_str());
160
161       if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
162       {
163         _ERR("Failed to launch");
164       }
165     };
166     Launchpad.LoaderMain(argc, argv);
167   }
168
169   return 0;
170 }