refactoring launcher
[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
41 int main(int argc, char *argv[])
42 {
43   int i;
44   bool standalone = false;
45   const char* standalonePath = nullptr;
46
47   std::vector<char*> vargs;
48
49   // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
50   for (i=1; 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   using tizen::runtime::dotnetcore::CoreRuntime;
81   std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
82   runtime = std::move(coreRuntime);
83
84   if (standalone)
85   {
86     _DBG("##### Run it standalone #########");
87     const char* appid = getenv("AUL_APPID");
88     _DBG("AUL_APPID : %s", appid);
89     std::string approot;
90     if (appid != nullptr)
91     {
92       const char* approot_path = aul_get_app_root_path();
93       if (approot_path != nullptr)
94       {
95         approot = std::string(approot_path);
96       }
97     }
98     if (approot.empty())
99     {
100       approot = Basename(standalonePath);
101     }
102     if (runtime->Initialize(true) != 0)
103     {
104       _ERR("Failed to initialize");
105       return 1;
106     }
107
108     int args_len = vargs.size();
109     char** args = &vargs[0];
110     if (runtime->Launch(appid, approot.c_str(), standalonePath, args_len, args))
111     {
112         _ERR("Failed to launch");
113         return 0;
114     }
115   }
116   else
117   {
118     Launchpad.OnCreate = [&runtime]()
119     {
120       if (runtime->Initialize(false) != 0)
121       {
122         _ERR("Failed to initialized");
123       }
124       else
125       {
126         _DBG("Success to initialized");
127       }
128     };
129
130     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
131     {
132       _DBG("terminated with app path : %s", info.path.c_str());
133       _DBG("appid : %s", info.id.c_str());
134       _DBG("pkg : %s", info.pkg.c_str());
135       _DBG("type : %s", info.type.c_str());
136
137       // The launchpad pass the name of exe file to the first argument.
138       // For the C# spec, we have to skip this first argument.
139
140       if (runtime->Launch(info.id.c_str(), info.root.c_str(), info.path.c_str(), argc-1, argv+1))
141       {
142         _ERR("Failed to launch");
143       }
144     };
145     Launchpad.LoaderMain(argc, argv);
146   }
147
148   return 0;
149 }