This topic is an introduction to creating Native Client applications for Samsung TVs. It describes the tools you need to develop and test Native Client applications, the structure of a Native Client application, and the essential C and C++ code.
Native Client (NaCl) applications are developed using HTML, JavaScript, and C or C++. The NaCl technology supports both the C and C++ languages, but using C++ is recommended, as the C++ code is easier to read and maintain.
To develop NaCl applications for Samsung Smart TVs, use the Tizen Studio with the TV Extension and Samsung NaCl SDK installed. The NaCl SDK also allows you to test your NaCl applications in Google Chrome™, on the TV emulator, or on a Samsung TV.
To test NaCl applications, you need at least 1 of the following environments:
Samsung-provided Smart TV developer model
TV emulator included in the Tizen Studio
Google Chrome™ Web browser You can launch the application in Google Chrome™ from within the Tizen Studio. You can also run the application directly from your own Web server instead of through the Tizen Studio. The Native Client flag must be enabled in the browser.
Although the Samsung NaCl toolchains produce binaries compatible with the Google Chrome™ runtime, applications requiring Samsung-specific interfaces cannot be launched in Google Chrome™.
A NaCl application package must have the following files:
The "index.html" file is a standard HTML file. To implement NaCl plugins in the application, you must implement an embed element inside the body element for each NaCl plugin you want to include:
embed
body
<embed id="nacl-app" width=0 height=0 src="application.nmf" type="application/x-nacl" />
The embed element attributes include:
id
width
height
src
type
application/x-nacl
To respond to progress events and messages, you must place the embed element within a div element:
div
<div id="listener"> <script type="text/javascript"> function moduleDidLoad(event) { var app = document.getElementById('nacl-app'); app.postMessage("Hello NaCl"); } // Display message in popup function handleMessage(message) { alert(message); } var listener = document.getElementById('listener'); listener.addEventListener('load', moduleDidLoad, true); listener.addEventListener('message', handleMessage, true); </script> <embed id="nacl-app" width=0 height=0 src="application.nmf" type="application/x-nacl" /> </div>
For more information on NaCl progress events, see Progress Events.
The manifest file is a JSON file with the ".nmf" extension. For each architecture key, the "url" key defines the location for its corresponding ".nexe" native binary file.
{ "program": { "x86-32": { "url": "application_i686.nexe" }, "x86-64": { "url": "application_x86-64.nexe" }, "arm": { "url": "application_armv7.nexe" } } }
You can include as many architectures in the manifest as you want. However, to run the application in the following scenarios, you need the modules for specific architectures:
The "config.xml" file is a Tizen-specific file that is created for every Tizen application. The file is automatically generated by the Tizen Studio. The following is an example of a "config.xml" file:
<?xml version="1.0" encoding="UTF-8"?> <widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" version="1.0.0" viewmodes="fullscreen" id="http://YourDomain.com/YourApp"> <tizen:application id="1234567890.YourAppName" package="1234567890" required_version="2.2"/> <feature name="http://tizen.org/feature/screen.size.normal"/> <tizen:setting screen-orientation="portrait" context-menu="enable" background-support="disable" encryption="disable" install-location="auto" hwkey-event="enable"/> <icon src="icon.png"/> <content src="index.html"/> <name>Your App Name Goes Here</name> </widget>
For more information on the "config.xml" file structure, see the "Configuration Document" section in the W3C Widget Packaging and XML Configuration specification.
NaCl plugins implement 2 main types of interfaces, which are sets of functionalities used by the plugin:
PPP
PPB
Within the NaCl plugin, the module is responsible for creating interfaces and is the main entry point for a NaCl application, while the instance is a special PPP interface required by NaCl plugins. The instance interface is created by the module and logically represents an instance of the NaCl application on the Web page.
A NaCl application written in C must implement 3 basic functions:
PPP_InitializeModule()
PPP_ShutdownModule()
PPP_GetInterface()
The PPP_Instance interface structure must contain pointers to the following functions, which handle important instance events:
PPP_Instance
DidCreate()
DidDestroy()
DidChangeView()
DidChangeFocus()
HandleDocumentLoad()
PP_FALSE
You can use additional features in your application, such as input event handling and 3D graphics, by implementing various interfaces. For example, to receive messages from the Web page, implement the PPP_Messaging interface using the HandleMessage() function, which is called by the browser when a message is received. For more information on the available interfaces, see the Pepper API documentation.
PPP_Messaging
HandleMessage()
To learn how to implement a basic NaCl application in C, see the Hello World in C tutorial.
A NaCl application written in C++ must implement 2 basic classes and an entry function:
pp::Module
pp::Instance
CreateModule()
Within the module object, derived from pp::Module, you must override the CreateInstance() function. This function constructs the application instance class and returns a pointer to it. To define the entry point for module creation, the CreateModule() function is implemented inside the pp namespace declaration, and returns a pointer to the created module object. These steps must be implemented in every C++ NaCl plugin.
CreateInstance()
pp
#include "ppapi/cpp/module.h" class HelloWorldModule : public pp::Module { public: virtual pp::Instance* CreateInstance(PP_Instance instance) { return new HelloWorldInstance(instance); } }; namespace pp { Module* CreateModule() { return new HelloWorldModule(); } } // namespace pp
The instance class derives from pp::Instance and defines an explicit constructor from PP_Instance that calls the base class constructor with the same argument. The pp::Instance class provides various virtual functions which can be overridden:
PP_Instance
Init()
HandleInputEvent()
For example, to react to an incoming message from the Web page, override the HandleMessage() function. The following code derives the instance class and posts the "Hello World" text when a string is received from the Web page:
#include "ppapi/cpp/instance.h" #include "ppapi/cpp/var.h" class HelloWorldInstance : public pp::Instance { public: explicit HelloWorldInstance(PP_Instance instance) : pp::Instance(instance) {} void HandleMessage(const pp::Var& message) override { if (message.is_string()) { PostMessage("Hello World\n"); } } };
To learn how to implement a basic NaCl application in C++, see the Hello World in C++ tutorial.
The package building process is automated by the Tizen Studio. For more information, see Building NaCl Projects.
If you want to test your application in Google Chrome™, you can also compile the application manually. The following sample "makefile" compiles and generates the manifest file for the "Hello World" tutorial applications: makefile (direct download)
To use the sample "makefile":
make
You can launch the application in Google Chrome™, on the emulator, or on an actual TV directly from the Tizen Studio. For more information, see Launching NaCl Projects.
If you want to run the application in Google Chrome™, you can also place the application files (the manifest, HTML, JavaScript, and native code files) in a directory on your Web server, and access the application in Google Chrome™ through its URL.