Merge "Integrate the log tag with "DOTNET_LAUNCHER"" into tizen
[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 "utils.h"
19 #include "log.h"
20
21 #include <cstdio>
22 #include <vector>
23 #include <memory>
24
25 #include <Ecore.h>
26 #include <Eina.h>
27 #include <aul.h>
28
29 #define __XSTR(x) #x
30 #define __STR(x) __XSTR(x)
31
32 #ifndef VERSION
33 #define LAUNCHER_VERSION_STR "-Unknown-"
34 #else
35 #define LAUNCHER_VERSION_STR __STR(VERSION)
36 #endif
37
38 static std::string VersionOption("--version");
39 static std::string StandaloneOption("--standalone");
40 static std::string NativeOption("--native");
41
42 int main(int argc, char *argv[])
43 {
44   int i;
45   bool standalone = false;
46   const char* standalonePath = nullptr;
47   bool nativeOnly = false;
48
49   std::vector<char*> vargs;
50
51   // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
52   for (i=1; i<argc; i++)
53   {
54     if (VersionOption.compare(argv[i]) == 0)
55     {
56       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
57       return 0;
58     }
59     else if (StandaloneOption.compare(argv[i]) == 0)
60     {
61       standalone = true;
62
63       if (i > argc-1)
64       {
65         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
66         return 1;
67       }
68       i++;
69       standalonePath = argv[i];
70     }
71     else if (NativeOption.compare(argv[i]) == 0)
72     {
73       nativeOnly = true;
74     }
75     else
76     {
77       vargs.push_back(argv[i]);
78     }
79   }
80
81   if (!standalone && nativeOnly)
82   {
83     fprintf(stderr, "\"--native\" option must be use with \"--standalone\"\n");
84     return 1;
85   }
86
87   using tizen::runtime::LauncherInterface;
88   using tizen::runtime::Launchpad;
89   using tizen::runtime::AppInfo;
90   std::unique_ptr<LauncherInterface> runtime;
91
92   using tizen::runtime::dotnetcore::CoreRuntime;
93   std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
94   runtime = std::move(coreRuntime);
95
96   if (standalone)
97   {
98     _DBG("##### Run it standalone #########");
99     const char* appid = getenv("AUL_APPID");
100     _DBG("AUL_APPID : %s", appid);
101     std::string approot;
102     if (appid != nullptr)
103     {
104       const char* approot_path = aul_get_app_root_path();
105       if (approot_path != nullptr)
106       {
107         approot = std::string(approot_path);
108       }
109     }
110     if (approot.empty())
111     {
112       approot = Basename(standalonePath);
113     }
114     if (runtime->Initialize(true) != 0)
115     {
116       _ERR("Failed to initialize");
117       return 1;
118     }
119
120     if (!nativeOnly && runtime->RunManagedLauncher() != 0)
121     {
122       _ERR("Failed to run managed launcher");
123       return 1;
124     }
125
126     int args_len = vargs.size();
127     char** args = &vargs[0];
128     if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
129     {
130         _ERR("Failed to launch");
131         return 0;
132     }
133   }
134   else
135   {
136     Launchpad.OnCreate = [&runtime]()
137     {
138       if (runtime->Initialize(false) != 0)
139       {
140         _ERR("Failed to initialized");
141       }
142       else
143       {
144         auto idle_task = [](void *data) -> Eina_Bool
145         {
146           LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
147           if (runtime->RunManagedLauncher() != 0)
148           {
149             _ERR("Failed to run managed launcher");
150           }
151           return ECORE_CALLBACK_CANCEL;
152         };
153         ecore_idler_add(idle_task, runtime.get());
154       }
155     };
156
157     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
158     {
159       _DBG("terminated with app path : %s", info.path.c_str());
160       _DBG("appid : %s", info.id.c_str());
161       _DBG("pkg : %s", info.pkg.c_str());
162       _DBG("type : %s", info.type.c_str());
163
164       // The launchpad pass the name of exe file to the first argument.
165       // For the C# spec, we have to skip this first argument.
166
167       if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc-1, argv+1))
168       {
169         _ERR("Failed to launch");
170       }
171     };
172     Launchpad.LoaderMain(argc, argv);
173   }
174
175   return 0;
176 }