2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "wrt-client.h"
20 #include <dpl/log/log.h>
21 #include <dpl/optional_typedefs.h>
22 #include <common/application_data.h>
23 #include <core_module.h>
25 WrtClient::WrtClient(int argc, char **argv) :
26 Application(argc, argv, "wrt-client", false),
27 DPL::TaskDecl<WrtClient>(this),
30 m_initializing(false),
35 m_returnStatus(ReturnStatus::Succeeded),
36 m_widgetState(WidgetState::WidgetState_Stopped)
39 LogDebug("App Created");
42 WrtClient::~WrtClient()
44 LogDebug("App Finished");
47 WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
49 return m_returnStatus;
52 void WrtClient::OnStop()
54 LogInfo("Stopping Dummy Client");
58 void WrtClient::OnCreate()
64 void WrtClient::OnResume()
66 if (m_widgetState != WidgetState_Suspended) {
67 LogWarning("Widget is not suspended, resuming was skipped");
71 m_widgetState = WidgetState_Running;
75 void WrtClient::OnPause()
77 if (m_widgetState != WidgetState_Running) {
78 LogWarning("Widget is not running to be suspended");
82 m_widgetState = WidgetState_Suspended;
85 void WrtClient::OnReset(bundle *b)
88 // bundle argument is freed after OnReset() is returned
89 // So bundle duplication is needed
90 ApplicationDataSingleton::Instance().setBundle(bundle_dup(b));
92 if (true == m_initializing) {
93 LogDebug("can not handle reset event");
96 if (true == m_launched) {
97 if (m_widgetState == WidgetState_Stopped) {
98 LogError("Widget is not running to be reset");
102 m_widgetState = WidgetState_Running;
104 if (true == checkArgument())
115 void WrtClient::OnTerminate()
117 LogDebug("Wrt Shutdown now");
121 void WrtClient::showHelpAndQuit()
123 printf("Usage: wrt-client [OPTION]... [WIDGET: ID]...\n"
125 "Mandatory arguments to long options are mandatory for short "
127 " -h, --help show this help\n"
129 "launch widget with given ID\n"
135 bool WrtClient::checkArgument()
137 LogInfo("checkArgument");
139 std::string arg = m_argv[0];
145 if (arg.find("wrt-client") != std::string::npos)
153 if (arg == "-h" || arg == "--help") {
156 } else if (arg == "-l" || arg == "--launch") {
160 m_handle = atoi(m_argv[2]);
165 size_t pos = arg.find_last_of('/');
167 if (pos != std::string::npos) {
168 arg = arg.erase(0, pos + 1);
171 // Launch widget based on application basename
172 if (sscanf(arg.c_str(), "%i", &m_handle) != 1) {
173 printf("failed: invalid widget handle\n");
177 LogDebug("Widget Id: " << m_handle << " (" << arg << ")");
183 void WrtClient::setStep()
187 AddStep(&WrtClient::initStep);
189 setSdkLauncherDebugData();
191 AddStep(&WrtClient::launchStep);
192 AddStep(&WrtClient::shutdownStep);
194 m_initializing = true;
196 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
199 void WrtClient::setSdkLauncherDebugData()
201 LogDebug("setSdkLauncherDebugData");
203 /* check bundle from sdk launcher */
204 bundle *bundleFromSdkLauncher;
205 bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
206 const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
207 const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
208 if (bundle_debug != NULL && bundle_pid != NULL) {
209 if (strcmp(bundle_debug, "true") == 0) {
211 m_sdkLauncherPid = atoi(bundle_pid);
218 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
220 LogDebug("Executing next step");
224 void WrtClient::initStep()
227 if (WRT::CoreModule::Init()) {
228 m_initialized = true;
230 m_returnStatus = ReturnStatus::Failed;
231 SwitchToStep(&WrtClient::shutdownStep);
233 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
237 void WrtClient::LaunchCallback(bool success, void *data) {
238 WrtClient* wrtClient = static_cast<WrtClient*>(data);
240 LogDebug("Launch succesfull");
242 wrtClient->m_launched = true;
243 wrtClient->m_initializing = false;
245 printf("launched\n");
248 LogError("Launch unsuccesfull");
252 wrtClient->m_returnStatus = ReturnStatus::Failed;
254 wrtClient->DPL::Event::ControllerEventHandler<NextStepEvent>::
255 PostEvent(NextStepEvent());
257 ApplicationDataSingleton::Instance().freeBundle();
260 void WrtClient::launchStep()
262 LogDebug("Launching widget ...");
264 m_widget = WRT::CoreModule::getRunnableWidgetObject(m_handle);
266 LogError("RunnableWidgetObject is NULL, stop launchStep");
267 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
272 if (m_widgetState == WidgetState_Running) {
273 LogWarning("Widget already running, stop launchStep");
274 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
279 if (m_widgetState == WidgetState_Authorizing) {
280 LogWarning("Widget already authorizing, stop launchStep");
281 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
286 m_widgetState = WidgetState_Authorizing;
287 if (!m_widget->CheckBeforeLaunch()) {
288 LogError("CheckBeforeLaunch failed, stop launchStep");
289 DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
293 LogInfo("Widget launch accepted. Entering running state");
294 m_widgetState = WidgetState_Running;
295 m_widget->PrepareView();
296 m_widget->SetDebugMode(m_debugMode, m_sdkLauncherPid);
297 WRT::FinishedCallback callback;
298 callback.callback = &LaunchCallback;
299 callback.userData = this;
300 m_widget->Show(callback);
303 void WrtClient::shutdownStep()
305 LogDebug("Closing Wrt connection ...");
306 if (m_initialized && m_widget) {
307 m_widgetState = WidgetState_Stopped;
310 WRT::CoreModule::Terminate();
311 m_initialized = false;
319 ADD_PROFILING_POINT("main-entered", "point");
321 // Output on stdout will be flushed after every newline character,
322 // even if it is redirected to a pipe. This is useful for running
323 // from a script and parsing output.
324 // (Standard behavior of stdlib is to use full buffering when
325 // redirected to a pipe, which means even after an end of line
326 // the output may not be flushed).
329 // set evas backend type
330 if (!getenv("ELM_ENGINE")) {
331 if (setenv("ELM_ENGINE", "gl", 1)) {
332 LogDebug("Enable backend");
335 setenv("COREGL_FASTPATH", "1", 1);
337 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-CLIENT");
339 WrtClient app(argc, argv);
340 int ret = app.Exec();
341 LogDebug("App returned: " << ret);
342 ret = app.getReturnStatus();
343 LogDebug("WrtClient returned: " << ret);