support launcher plugin
[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 "dotnet_signal_handler.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         registerSignalHandler();
45
46         int i;
47         bool standalone = false;
48         const char* standalonePath = nullptr;
49
50         std::vector<char*> vargs;
51
52         // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
53         for (i = 1;     i <argc; i++) {
54                 if (VersionOption.compare(argv[i]) == 0) {
55                         printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
56                         return 0;
57                 } else if (StandaloneOption.compare(argv[i]) == 0) {
58                         standalone = true;
59
60                         if (i > argc-1) {
61                                 fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
62                                 return 1;
63                         }
64                         i++;
65                         standalonePath = argv[i];
66                 } else {
67                         vargs.push_back(argv[i]);
68                 }
69         }
70
71         using tizen::runtime::LauncherInterface;
72         using tizen::runtime::Launchpad;
73         using tizen::runtime::AppInfo;
74         std::unique_ptr<LauncherInterface> runtime;
75
76         using tizen::runtime::dotnetcore::CoreRuntime;
77         std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
78         runtime = std::move(coreRuntime);
79
80         if (standalone) {
81                 _DBG("##### Run it standalone #########");
82                 const char* appId = getenv("AUL_APPID");
83                 _DBG("AUL_APPID : %s", appId);
84                 std::string appRoot;
85                 if (appId != nullptr) {
86                         const char* appRootPath = aul_get_app_root_path();
87                         if (appRootPath != nullptr)
88                                 appRoot = std::string(appRootPath);
89                 } else {
90                         appId = "dotnet-launcher";
91                 }
92
93                 if (appRoot.empty())
94                         appRoot = baseName(baseName(standalonePath));
95                 if (runtime->initialize(true) != 0) {
96                         _ERR("Failed to initialize");
97                         return 1;
98                 }
99
100                 int argsLen = vargs.size();
101                 char** args = &vargs[0];
102                 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
103                         _ERR("Failed to launch");
104                         return 0;
105                 }
106         } else {
107                 Launchpad.onCreate = [&runtime]() {
108                         if (runtime->initialize(false) != 0)
109                                 _ERR("Failed to initialized");
110                         else
111                                 _DBG("Success to initialized");
112                 };
113
114                 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
115                         _DBG("terminated with app path : %s", appInfo.path.c_str());
116                         _DBG("appId : %s", appInfo.id.c_str());
117                         _DBG("pkg : %s", appInfo.pkg.c_str());
118                         _DBG("type : %s", appInfo.type.c_str());
119
120                         // The launchpad pass the name of exe file to the first argument.
121                         // For the C# spec, we have to skip this first argument.
122
123                         if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
124                                 _ERR("Failed to launch");
125                 };
126                 Launchpad.loaderMain(argc, argv);
127         }
128
129         runtime->dispose();
130         return 0;
131 }