Now options will be pass to managed launcher
[platform/core/dotnet/launcher.git] / NativeLauncher / src / main.cc
1 #include "dotnet/dotnet_launcher.h"
2 #include "mono/mono_launcher.h"
3 #include "utils.h"
4 #include "log.h"
5
6 #include <cstdio>
7 #include <vector>
8 #include <memory>
9
10 #include <Ecore.h>
11 #include <Eina.h>
12
13 static std::string StandaloneOption("--standalone");
14
15 int main(int argc, char *argv[])
16 {
17   int i;
18   bool standalone = false;
19   const char* standalonePath = nullptr;
20
21   std::vector<char*> vargs;
22
23   for (i=1; i<argc; i++)
24   {
25     if (StandaloneOption.compare(argv[i]) == 0)
26     {
27       standalone = true;
28
29       if (i > argc-1)
30       {
31         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
32         return 1;
33       }
34       i++;
35       standalonePath = argv[i];
36     }
37     else
38     {
39       vargs.push_back(argv[i]);
40     }
41   }
42
43   using tizen::runtime::LauncherInterface;
44   using tizen::runtime::Launchpad;
45   using tizen::runtime::AppInfo;
46   std::unique_ptr<LauncherInterface> runtime;
47
48   bool useMono = !FileNotExist("/etc/.use_mono");
49
50   if (!useMono)
51   {
52     using tizen::runtime::dotnetcore::CoreRuntime;
53     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
54     runtime = std::move(coreRuntime);
55   }
56   else
57   {
58     using tizen::runtime::mono::MonoRuntime;
59     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
60     runtime = std::move(monoRuntime);
61   }
62
63   if (standalone)
64   {
65     std::string base = Basename(standalonePath);
66     if (runtime->Initialize(true) != 0)
67     {
68       _ERR("Failed to initialize");
69       return 1;
70     }
71     if (runtime->RunManagedLauncher() != 0)
72     {
73       _ERR("Failed to run managed launcher");
74       return 1;
75     }
76
77     int args_len = vargs.size();
78     char** args = &vargs[0];
79     if (!runtime->Launch(base.c_str(), standalonePath, args_len, args))
80     {
81         _ERR("Failed to launch");
82         return 1;
83     }
84   }
85   else
86   {
87     Launchpad.OnCreate = [&runtime]
88     {
89       auto idle_task = [](void *data) -> Eina_Bool 
90       {
91         LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
92         if (runtime->RunManagedLauncher() != 0)
93         {
94           _ERR("Failed to run managed launcher");
95         }
96         return ECORE_CALLBACK_CANCEL;
97       };
98       if (runtime->Initialize(false) != 0)
99       {
100         _ERR("Failed to initialized");
101         return 1;
102       }
103       ecore_idler_add(idle_task, runtime.get());
104     };
105
106     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
107     {
108       _DBG("terminated with app path : %s", info.path.c_str());
109       _DBG("appid : %s", info.id.c_str());
110       _DBG("pkg : %s", info.pkg.c_str());
111       _DBG("type : %s", info.type.c_str());
112
113       if (!runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
114       {
115         _ERR("Failed to launch");
116       }
117     };
118     Launchpad.LoaderMain(argc, argv);
119   }
120
121   return 0;
122 }