Merge "Revert "temporal patch for setting LANG environment value"" 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 #include <sys/types.h>
29 #include <unistd.h>
30
31 #define __XSTR(x) #x
32 #define __STR(x) __XSTR(x)
33
34 #ifndef VERSION
35 #define LAUNCHER_VERSION_STR "-Unknown-"
36 #else
37 #define LAUNCHER_VERSION_STR __STR(VERSION)
38 #endif
39
40 #define CMD_LINE_SIZE   24      // sizeof("/usr/bin/dotnet-launcher")
41
42 static std::string VersionOption("--version");
43 static std::string StandaloneOption("--standalone");
44
45 int main(int argc, char *argv[])
46 {
47         int i;
48         bool standalone = false;
49         char* standalonePath = nullptr;
50
51         std::vector<char*> vargs;
52
53         // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
54         for (i = 1;     i <argc; i++) {
55                 if (VersionOption.compare(argv[i]) == 0) {
56                         printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
57                         return 0;
58                 } else if (StandaloneOption.compare(argv[i]) == 0) {
59                         standalone = true;
60
61                         if (i > argc-1) {
62                                 fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
63                                 return 1;
64                         }
65                         i++;
66                         standalonePath = argv[i];
67                 } else {
68                         vargs.push_back(argv[i]);
69                 }
70         }
71
72         using tizen::runtime::LauncherInterface;
73         using tizen::runtime::Launchpad;
74         using tizen::runtime::AppInfo;
75         std::unique_ptr<LauncherInterface> runtime;
76
77         using tizen::runtime::dotnetcore::CoreRuntime;
78         std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
79         runtime = std::move(coreRuntime);
80
81         if (standalone) {
82                 _DBG("##### Run it standalone #########");
83                 char appId[1024] = {0,};
84                 std::string appRoot;
85                 if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
86                         const char* appRootPath = aul_get_app_root_path();
87                         if (appRootPath != nullptr)
88                                 appRoot = std::string(appRootPath);
89                 } else {
90                         // If appId is not set, it is executed directly by cmdline.
91                         // In this case, appRoot is passed as an argument.
92                         snprintf(appId, 16, "%s", "dotnet-launcher");
93                         appRoot = baseName(baseName(standalonePath));
94                 }
95                 _DBG("AUL_APPID : %s", appId);
96
97                 if (runtime->initialize(true) != 0) {
98                         _ERR("Failed to initialize");
99                         return 1;
100                 }
101
102                 // change cmdline from dotnet-launcher to executable path
103                 memset(argv[0], '\0', CMD_LINE_SIZE);
104                 snprintf(argv[0], CMD_LINE_SIZE - 1, "%s", appId);
105
106                 int argsLen = vargs.size();
107                 char** args = &vargs[0];
108                 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
109                         _ERR("Failed to launch");
110                         return 0;
111                 }
112         } else {
113                 Launchpad.onCreate = [&runtime]() {
114                         if (runtime->initialize(false) != 0)
115                                 _ERR("Failed to initialized");
116                         else
117                                 _DBG("Success to initialized");
118                 };
119
120                 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
121                         _DBG("terminated with app path : %s", appInfo.path.c_str());
122                         _DBG("appId : %s", appInfo.id.c_str());
123                         _DBG("pkg : %s", appInfo.pkg.c_str());
124                         _DBG("type : %s", appInfo.type.c_str());
125
126                         // The launchpad pass the name of exe file to the first argument.
127                         // For the C# spec, we have to skip this first argument.
128
129                         if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
130                                 _ERR("Failed to launch");
131                 };
132                 Launchpad.loaderMain(argc, argv);
133         }
134
135         runtime->dispose();
136         return 0;
137 }