Pepper49 pnacl SDL2 main() doesn't run

81 views
Skip to first unread message

SagaraWMW

unread,
Aug 6, 2016, 2:10:53 PM8/6/16
to Native-Client-Discuss
Hi all

I'm trying to get SDL2 working on a C-based Portable Native Client.

It compiles clean, runs on the web page, can see the PostMessage from the pnacl, but the main() function does not get executed to initialize SDL2.

I'm using pepper49 on Apple Mac OSX.

The sample program is included below.

Appreciate very much if somebody could help on this.

Thanks.

Regards
Sagara


#include <stdio.h>
#include <string.h>

#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppp.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/c/ppp_messaging.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/c/ppb_messaging.h"
#include "ppapi/c/pp_var.h"

#include <SDL2/SDL.h>

#if (defined(__NACL__))
#include <SDL2/SDL_main.h>
#endif


/* Global pointers to selected browser interfaces. */
const PPB_Messaging* g_ppb_messaging_interface;
const PPB_Var* g_ppb_var_interface;
PP_Instance g_instance;

const char* kEcho = "Echo from NaCl: ";


SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;


static PP_Bool Instance_DidCreate(PP_Instance instance, uint32_t argc,
const char* argn[], const char* argv[])
{
g_instance = instance;

return PP_TRUE;
}

static void Instance_DidDestroy(PP_Instance instance) {
}

static void Instance_DidChangeView(PP_Instance pp_instance, PP_Resource view) {
}

static void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) {
}

static PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance,
PP_Resource pp_url_loader) {
return PP_FALSE;
}

PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id,
PPB_GetInterface get_browser_interface) {
// Initializing pointers to used browser interfaces.
g_ppb_messaging_interface = (const PPB_Messaging*) get_browser_interface(
PPB_MESSAGING_INTERFACE);
g_ppb_var_interface = (const PPB_Var*) get_browser_interface(PPB_VAR_INTERFACE);

return PP_OK;
}

PP_EXPORT void PPP_ShutdownModule() {
}

void Messaging_HandleMessage(PP_Instance instance, struct PP_Var message) {
uint32_t len;
char str_buff[256];

// Extract char array from message <code>PP_Var</code> structure.
const char* message_str = g_ppb_var_interface->VarToUtf8(message, &len);
if (message_str == NULL)
return;

snprintf(str_buff, sizeof(str_buff), "%s%s\n", kEcho, message_str);

// Create <code>PP_Var</code> containing the message body.
struct PP_Var var_response = g_ppb_var_interface->VarFromUtf8(str_buff,
strlen(str_buff));

// Post message to the JavaScript layer.
g_ppb_messaging_interface->PostMessage(instance, var_response);
}

PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {

if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) {
// Your module must fill this structure (interface) with proper order
// (@see ppp.h file) 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 you have to implement PPP_Messaging interface and
// return it as a pointer to this structure.
static PPP_Messaging messaging_interface = {
&Messaging_HandleMessage
};
return &messaging_interface;
}
return NULL;
}

/*********************************************************
* main()
*********************************************************/

int main(int argc, char *argv[])
{
struct PP_Var var_response;
char str_buff[256];

snprintf(str_buff, sizeof(str_buff), "main() started\n");

// Create <code>PP_Var</code> containing the message body.
var_response = g_ppb_var_interface->VarFromUtf8(str_buff, strlen(str_buff));

// Post message to the JavaScript layer.
g_ppb_messaging_interface->PostMessage(g_instance, var_response);


return 0;
}


Make file:

VALID_TOOLCHAINS := pnacl glibc clang-newlib mac

NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../../..)

TARGET = sdl-test-1


include $(NACL_SDK_ROOT)/tools/common.mk


DEPS = ppapi_simple nacl_io ppapi sdk_util ppapi_gles2
LIBS = SDL2main SDL2 ppapi_simple nacl_io ppapi pthread ppapi_simple_real ppapi_gles2

CFLAGS = -Wall -D__NACL__ -D_REENTRANT -Wl,--undefined=PSUserMainGet
SOURCES = sdl-test-1.c

# Build rules generated by macros from common.mk:

$(foreach dep,$(DEPS),$(eval $(call DEPEND_RULE,$(dep))))
$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

# The PNaCl workflow uses both an unstripped and finalized/stripped binary.
# On NaCl, only produce a stripped binary for Release configs (not Debug).
ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

--- end of file ---

Aaron Michaux

unread,
Aug 6, 2016, 10:50:42 PM8/6/16
to Native-Client-Discuss
There's a bug in SDL for pnacl. It has something to do with the correct SDL_main not being compiled into the actual pexe. I cannot remember the specifics, but I figured out how to get it fixed and running, and wrote about the bug, and my solution, on this forum about 3 months ago. Never heard from anybody about it. Sorry, but don't have time to find my old post, but hopefully you find it.

SagaraWMW

unread,
Aug 7, 2016, 1:38:11 PM8/7/16
to Native-Client-Discuss
I upgraded the SDL2 to version 2.0.4 and gave it a try but still the main() function does not run.

I gave a try to testgles2.c in SDL, it runs, no graphical output but shows a console output on browser as follows:

PpapiPluginStart: No AT_SYSINFO item found in auxv, so cannot start PPAPI.  Is the IRT library not present?

ps: Got View change: 640,640

NativeClient: NaCl module crashed

Any idea? Does it require to set any permission to run with SDL2?

Regards
Sagara

David Meyer

unread,
Aug 25, 2016, 6:18:11 AM8/25/16
to native-cli...@googlegroups.com
That crash happens when the pexe does not contain a main() function.

I had a similar problem w/ SDL1. The SDL library was providing nacl_main instead of main.

My fix was to create a fake main which just called nacl_main:

// C version, if C++ add extern "C" block around code.
extern int nacl_main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
    return nacl_main(argc, argv);
}

Alternatively, there were recently multiple commits to webports to fix this problem, so you might want to try rebuilding everything using the latest webports.

--
You received this message because you are subscribed to the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to native-client-discuss+unsub...@googlegroups.com.
To post to this group, send email to native-client-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/native-client-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages