Hello World in C
hello world in c importantdue to nacl deprecation by the chromium project, tizen tv will continue its support for nacl only until 2021-year products meanwhile, tizen tv will start focusing on high-performance, cross-browser webassembly from 2020-year products this topic describes the "hello world in c" sample application implementation samples hello world in c this tutorial describes how to implement a simple "hello world" native client nacl application in c the nacl module waits for a message from the javascript component, creates a reply message by appending the received string to a predefined string, and sends it as a reply back to the javascript component, which displays the message on the screen the following figure shows the sample application when the nacl module has loaded, the "status" field text changes to "loaded" in the "nacl messages" section, the message "echo from nacl hello world from js" appears for information on how to access the sample application cheat sheet and run the application, see sample-based tutorials to implement the "hello world" nacl application in c initialize the module a nacl module in c must implement the following 2 functions the ppp_initializemodule function is an entry point for the nacl plugin and is called when the module is loaded on the web page you must initialize interfaces and resources in this function, including the following mandatory interfaces the ppb_messaging interface implements the postmessage function, which allows the nacl plugin to send messages to the javascript component this interface is used to send the reply message to the javascript component the ppb_var interface creates and manages pp_var objects, which are wrappers for javascript variables in c code this interface is used to convert c-style strings to javascript variables, which can be sent to the javascript component using the ppb_messaging interface /* global pointers to selected browser interfaces */ const ppb_messaging* g_messaging_interface; const ppb_var* g_var_interface; /** * entry point to the nacl module, called while the module is being initialized * this function is obligatory; initialize interfaces in this function */ pp_export int32_t ppp_initializemodule pp_module module_id, ppb_getinterface get_browser_interface { // initialize pointers to used browser interfaces g_messaging_interface = const ppb_messaging* get_browser_interface ppb_messaging_interface ; g_var_interface = const ppb_var* get_browser_interface ppb_var_interface ; return pp_ok; } the ppp_shutdownmodule function is used internally by google chrome™ and must be left empty /** * implementation-specific function for internal use only * this function is obligatory, but leave it empty * @see <code>ppp_shutdownmodule</code> in ppp h file */ pp_export void ppp_shutdownmodule { } for more information on the above functions, see the "include/ppapi/c/ppp h" file in your "nacl_sdk/pepper_xx" directory importantto compile the nacl code successfully, the ppp_initializemodule and ppp_shutdownmodule functions must be implemented with these exact names implement the instance-specific functions when developing a nacl application in c, you must implement all the following functions specific to the ppp_instance interface, even if they are empty in the instance_didcreate function, define the actions to perform when the instance is created, such as its initialization logic the function returns pp_true if the instance creation was successful, or pp_false otherwise in the sample application, no initialization logic is needed, so the function always returns pp_true static pp_bool instance_didcreate pp_instance instance, uint32_t argc, const char* argn[], const char* argv[] { return pp_true; } in the instance_diddestroy function, define the actions to perform when the instance is destroyed, such as releasing any acquired instance-specific resources in the sample application, the instance has nothing to release, so the function is empty static void instance_diddestroy pp_instance instance { } in the instance_didchangeview function, define the actions to perform when the application viewport is created or changed in any way the sample application does not use graphics, so the function is empty static void instance_didchangeview pp_instance pp_instance, pp_resource view { } in the instance_didchangefocus function, define the actions to perform when the embedded nacl module gains or loses focus this functionality is not needed in the sample application, so the function is empty static void instance_didchangefocus pp_instance pp_instance, pp_bool has_focus { } the instance_handledocumentload function is not used in tizen nacl applications, and must return pp_false static pp_bool instance_handledocumentload pp_instance pp_instance, pp_resource pp_url_loader { return pp_false; } for more information on the above functions, see the "include/ppapi/c/ppp_instance h" file in your "nacl_sdk/pepper_xx" directory to receive messages, implement the messaging_handlemessage function of the ppp_messaging interface when the nacl module receives a message from the javascript component extract the message from the pp_var object concatenate the message to the predefined echo string, using the c standard library snprintf function convert the char buffer to a pp_var object using the ppb_var interface send the message to the javascript component using the postmessage function of the ppb_messaging interface const char* kecho = "echo from nacl "; /** * handles messages from js sent by the nacl_module postmessage function */ void messaging_handlemessage pp_instance instance, struct pp_var message { char str_buff[64]; uint32_t len; // extract the char array from the message pp_var structure const char* message_str = g_var_interface->vartoutf8 message, &len ; if message_str == null return; snprintf str_buff, sizeof str_buff , "%s%s\n", kecho, message_str ; // create a pp_var containing the message body struct pp_var var_response = g_var_interface->varfromutf8 str_buff, strlen str_buff ; // post the message to the javascript layer g_messaging_interface->postmessage instance, var_response ; } for more information on the handlemessage function, see the "include/ppapi/c/ppp_messaging h" file in your "nacl_sdk/pepper_xx" directory connect the interfaces to the nacl module to connect the implemented interfaces, you must implement the ppp_getinterface function, which takes a c-style string as a parameter and returns a const void pointer the function is called multiple times during application initialization, checking for ppp interfaces that have been implemented for each implemented interface, return a structure containing pointers to the interface functions /** * this mandatory function retrieves the interface implementations that this module supports */ pp_export const void* ppp_getinterface const char* interface_name { if strcmp interface_name, ppp_instance_interface == 0 { // the module must fill this structure interface in the following order // and return it in this case static ppp_instance instance_interface = { &instance_didcreate, &instance_diddestroy, &instance_didchangeview, &instance_didchangefocus, &instance_handledocumentload }; return &instance_interface; } if strcmp interface_name, ppp_messaging_interface == 0 { // to handle messages, implement the ppp_messaging interface and // return it as a pointer to this structure static ppp_messaging messaging_interface = { &messaging_handlemessage }; return &messaging_interface; } return null; } for more information on the ppp_getinterface function, see the "include/ppapi/c/ppp h" file in your "nacl_sdk/pepper_xx" directory