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