summaryrefslogtreecommitdiff
path: root/NativeLauncher/src/main.cc
blob: 9f43bfc48f9ac491d9160ec5e81443595bf9485b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "dotnet/dotnet_launcher.h"
#include "mono/mono_launcher.h"
#include "utils.h"
#include "log.h"

#include <cstdio>
#include <vector>
#include <memory>

#include <Ecore.h>
#include <Eina.h>
#include <aul.h>

#define __XSTR(x) #x
#define __STR(x) __XSTR(x)

#ifndef VERSION
#define LAUNCHER_VERSION_STR "-Unknown-"
#else
#define LAUNCHER_VERSION_STR __STR(VERSION)
#endif

static std::string VersionOption("--version");
static std::string StandaloneOption("--standalone");

int main(int argc, char *argv[])
{
  int i;
  bool standalone = false;
  const char* standalonePath = nullptr;

  std::vector<char*> vargs;

  for (i=0; i<argc; i++)
  {
    if (VersionOption.compare(argv[i]) == 0)
    {
      printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
      return 0;
    }
    else if (StandaloneOption.compare(argv[i]) == 0)
    {
      standalone = true;

      if (i > argc-1)
      {
        fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
        return 1;
      }
      i++;
      standalonePath = argv[i];
    }
    else
    {
      vargs.push_back(argv[i]);
    }
  }

  using tizen::runtime::LauncherInterface;
  using tizen::runtime::Launchpad;
  using tizen::runtime::AppInfo;
  std::unique_ptr<LauncherInterface> runtime;

  bool useMono = !FileNotExist("/etc/.use_mono");

  if (!useMono)
  {
    using tizen::runtime::dotnetcore::CoreRuntime;
    std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
    runtime = std::move(coreRuntime);

    _DBG("##### CoreCLR Launcher ######");
  }
  else
  {
    using tizen::runtime::mono::MonoRuntime;
    std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
    runtime = std::move(monoRuntime);

    _DBG("##### Mono Launcher ######");
  }

  if (standalone)
  {
    _DBG("##### Run it standalone #########");
    const char* appid = getenv("AUL_APPID");
    _DBG("AUL_APPID : %s", appid);
    std::string approot;
    if (appid != nullptr)
    {
      const char* approot_path = aul_get_app_root_path();
      if (approot_path != nullptr)
      {
        approot = std::string(approot_path);
      }
    }
    if (approot.empty())
    {
      approot = Basename(standalonePath);
    }
    if (runtime->Initialize(true) != 0)
    {
      _ERR("Failed to initialize");
      return 1;
    }
    if (runtime->RunManagedLauncher() != 0)
    {
      _ERR("Failed to run managed launcher");
      return 1;
    }

    int args_len = vargs.size();
    char** args = &vargs[0];
    if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
    {
        _ERR("Failed to launch");
        return 0;
    }
  }
  else
  {
    Launchpad.OnCreate = [&runtime]
    {
      auto idle_task = [](void *data) -> Eina_Bool 
      {
        LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
        if (runtime->RunManagedLauncher() != 0)
        {
          _ERR("Failed to run managed launcher");
        }
        return ECORE_CALLBACK_CANCEL;
      };
      if (runtime->Initialize(false) != 0)
      {
        _ERR("Failed to initialized");
        return 1;
      }
      ecore_idler_add(idle_task, runtime.get());
    };

    Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
    {
      _DBG("terminated with app path : %s", info.path.c_str());
      _DBG("appid : %s", info.id.c_str());
      _DBG("pkg : %s", info.pkg.c_str());
      _DBG("type : %s", info.type.c_str());

      if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
      {
        _ERR("Failed to launch");
      }
    };
    Launchpad.LoaderMain(argc, argv);
  }

  return 0;
}