Chrome's Native Client backend implementation
authorGabriel Jacobo <gabomdq@gmail.com>
Fri, 06 Jun 2014 15:45:59 -0300
changeset 8833ae720d61d14d
parent 8832 408b0d563b27
child 8834 b300e097899d
Chrome's Native Client backend implementation
README-nacl.txt
WhatsNew.txt
build-scripts/config.sub
build-scripts/naclbuild.sh
configure
configure.in
include/SDL_assert.h
include/SDL_config.h.in
include/SDL_main.h
include/SDL_platform.h
include/SDL_rwops.h
src/SDL_log.c
src/audio/SDL_audio.c
src/audio/nacl/SDL_naclaudio.c
src/audio/nacl/SDL_naclaudio.h
src/dynapi/SDL_dynapi.h
src/file/SDL_rwops.c
src/main/nacl/SDL_nacl_main.c
src/render/opengles2/SDL_gles2funcs.h
src/thread/pthread/SDL_systhread.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/nacl/SDL_naclevents.c
src/video/nacl/SDL_naclevents_c.h
src/video/nacl/SDL_naclglue.c
src/video/nacl/SDL_naclopengles.c
src/video/nacl/SDL_naclopengles.h
src/video/nacl/SDL_naclvideo.c
src/video/nacl/SDL_naclvideo.h
src/video/nacl/SDL_naclwindow.c
src/video/nacl/SDL_naclwindow.h
test/nacl/background.js
test/nacl/common.js
test/nacl/index.html
test/nacl/manifest.json
test/testgles2.c
test/testrendercopyex.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/README-nacl.txt	Fri Jun 06 15:45:59 2014 -0300
     1.3 @@ -0,0 +1,60 @@
     1.4 +================================================================================
     1.5 +Simple DirectMedia Layer for Native Client
     1.6 +================================================================================
     1.7 +
     1.8 +Requirements: 
     1.9 +    
    1.10 +    * Native Client SDK (https://developer.chrome.com/native-client), 
    1.11 +      (tested with Pepper version 33 or higher).
    1.12 +
    1.13 +The SDL backend for Chrome's Native Client has been tested only with the PNaCl
    1.14 +toolchain, which generates binaries designed to run on ARM and x86_32/64 
    1.15 +platforms. This does not mean it won't work with the other toolchains!
    1.16 +
    1.17 +================================================================================
    1.18 +Building SDL for NaCl
    1.19 +================================================================================
    1.20 +
    1.21 +Set up the right environment variables (see naclbuild.sh), then configure SDL with:
    1.22 +
    1.23 +    configure --host=pnacl --prefix some/install/destination
    1.24 +    
    1.25 +Then "make". 
    1.26 +
    1.27 +As an example of how to create a deployable app a Makefile project is provided 
    1.28 +in test/nacl/Makefile, which includes some monkey patching of the common.mk file 
    1.29 +provided by NaCl, without which linking properly to SDL won't work (the search 
    1.30 +path can't be modified externally, so the linker won't find SDL's binaries unless 
    1.31 +you dump them into the SDK path, which is inconvenient).
    1.32 +Also provided in test/nacl is the required support file, such as index.html, 
    1.33 +manifest.json, etc.
    1.34 +
    1.35 +
    1.36 +================================================================================
    1.37 +Running tests
    1.38 +================================================================================
    1.39 +
    1.40 +Due to the nature of NaCl programs, building and running SDL tests is not as
    1.41 +straightforward as one would hope. The script naclbuild.sh in build-scripts 
    1.42 +automates the process and should serve as a guide for users of SDL trying to build 
    1.43 +their own applications.
    1.44 +
    1.45 +Basic usage:
    1.46 +    
    1.47 +    ./naclbuild.sh path/to/pepper/toolchain (i.e. ~/naclsdk/pepper_35)
    1.48 +    
    1.49 +This will build testgles2.c by default.
    1.50 +
    1.51 +If you want to build a different test, for example testrendercopyex.c:
    1.52 +    
    1.53 +    SOURCES=~/sdl/SDL/test/testrendercopyex.c ./naclbuild.sh ~/naclsdk/pepper_35
    1.54 +    
    1.55 +Once the build finishes, you have to serve the contents with a web server (the
    1.56 +script will give you instructions on how to do that with Python).
    1.57 +
    1.58 +================================================================================
    1.59 +TODO - Known Issues
    1.60 +================================================================================
    1.61 +* Audio backend is not usable yet.
    1.62 +* Testing of all systems with a real application (something other than SDL's tests)
    1.63 +
     2.1 --- a/WhatsNew.txt	Thu Jun 05 15:37:33 2014 -0700
     2.2 +++ b/WhatsNew.txt	Fri Jun 06 15:45:59 2014 -0300
     2.3 @@ -9,6 +9,7 @@
     2.4  * Added an event SDL_RENDER_DEVICE_RESET that is sent from the D3D renderers 
     2.5    when the D3D device is lost, and from Android's event loop when the GLES
     2.6    context had to be re created.
     2.7 +* Native Client backend
     2.8  
     2.9  ---------------------------------------------------------------------------
    2.10  2.0.3:
     3.1 --- a/build-scripts/config.sub	Thu Jun 05 15:37:33 2014 -0700
     3.2 +++ b/build-scripts/config.sub	Fri Jun 06 15:45:59 2014 -0300
     3.3 @@ -359,6 +359,19 @@
     3.4  	i*86 | x86_64)
     3.5  	  basic_machine=$basic_machine-pc
     3.6  	  ;;
     3.7 +	nacl64*)
     3.8 +		basic_machine=x86_64-pc
     3.9 +		os=-nacl
    3.10 +		;;
    3.11 +	nacl*)
    3.12 +		basic_machine=i686-pc
    3.13 +		os=-nacl
    3.14 +		;;
    3.15 +    pnacl*)
    3.16 +        # le32-unknown-pnacl comes from http://www.chromium.org/nativeclient/pnacl/stability-of-the-pnacl-bitcode-abi
    3.17 +        basic_machine=le32-unknown
    3.18 +        os=-pnacl
    3.19 +        ;;
    3.20  	# Object if more than one company name word.
    3.21  	*-*-*)
    3.22  		echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2
    3.23 @@ -843,6 +856,10 @@
    3.24  		basic_machine=le32-unknown
    3.25  		os=-nacl
    3.26  		;;
    3.27 +    pnacl)
    3.28 +        basic_machine=le32-unknown
    3.29 +        os=-pnacl
    3.30 +        ;;
    3.31  	ncr3000)
    3.32  		basic_machine=i486-ncr
    3.33  		os=-sysv4
    3.34 @@ -1384,6 +1401,12 @@
    3.35  			;;
    3.36  		esac
    3.37  		;;
    3.38 +	-nacl*)
    3.39 +		os=-nacl
    3.40 +		;;
    3.41 +    -pnacl*)
    3.42 +        os=-pnacl
    3.43 +        ;;
    3.44  	-nto-qnx*)
    3.45  		;;
    3.46  	-nto*)
    3.47 @@ -1506,6 +1529,10 @@
    3.48  		os=-dicos
    3.49  		;;
    3.50  	-nacl*)
    3.51 +        os=-nacl
    3.52 +        ;;
    3.53 +	-pnacl*)
    3.54 +        os=-pnacl
    3.55  		;;
    3.56  	-none)
    3.57  		;;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/build-scripts/naclbuild.sh	Fri Jun 06 15:45:59 2014 -0300
     4.3 @@ -0,0 +1,105 @@
     4.4 +#!/bin/bash
     4.5 +if [ -z "$1" ] && [ -z "$NACL_SDK_ROOT" ]; then
     4.6 +    echo "Usage: ./naclbuild ~/nacl/pepper_33"
     4.7 +    echo "This will build SDL for Native Client, and testgles2.c as a demo"
     4.8 +    echo "You can set env vars CC, AR, LD and RANLIB to override the default PNaCl toolchain used"
     4.9 +    echo "You can set env var SOURCES to select a different source file than testgles2.c"
    4.10 +    exit 1
    4.11 +fi
    4.12 +
    4.13 +if [ -n "$1" ]; then
    4.14 +    NACL_SDK_ROOT="$1"
    4.15 +fi
    4.16 +
    4.17 +CC=""
    4.18 +
    4.19 +if [ -n "$2" ]; then
    4.20 +    CC="$2"
    4.21 +fi
    4.22 +
    4.23 +echo "Using SDK at $NACL_SDK_ROOT"
    4.24 +
    4.25 +export NACL_SDK_ROOT="$NACL_SDK_ROOT"
    4.26 +export CFLAGS="$CFLAGS -I$NACL_SDK_ROOT/include"
    4.27 +
    4.28 +NCPUS="1"
    4.29 +case "$OSTYPE" in
    4.30 +    darwin*)
    4.31 +        NCPU=`sysctl -n hw.ncpu`
    4.32 +        ;; 
    4.33 +    linux*)
    4.34 +        if [ -n `which nproc` ]; then
    4.35 +            NCPUS=`nproc`
    4.36 +        fi  
    4.37 +        ;;
    4.38 +  *);;
    4.39 +esac
    4.40 +
    4.41 +CURDIR=`pwd -P`
    4.42 +SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
    4.43 +BUILDPATH="$SDLPATH/build/nacl"
    4.44 +TESTBUILDPATH="$BUILDPATH/test"
    4.45 +SDL2_STATIC="$BUILDPATH/build/.libs/libSDL2.a"
    4.46 +mkdir -p $BUILDPATH
    4.47 +mkdir -p $TESTBUILDPATH
    4.48 +
    4.49 +if [ -z "$CC" ]; then
    4.50 +    export CC="$NACL_SDK_ROOT/toolchain/linux_pnacl/bin/pnacl-clang"
    4.51 +fi
    4.52 +if [ -z "$AR" ]; then
    4.53 +    export AR="$NACL_SDK_ROOT/toolchain/linux_pnacl/bin/pnacl-ar"
    4.54 +fi
    4.55 +if [ -z "$LD" ]; then
    4.56 +    export LD="$NACL_SDK_ROOT/toolchain/linux_pnacl/bin/pnacl-ar"
    4.57 +fi
    4.58 +if [ -z "$RANLIB" ]; then
    4.59 +    export RANLIB="$NACL_SDK_ROOT/toolchain/linux_pnacl/bin/pnacl-ranlib"
    4.60 +fi
    4.61 +
    4.62 +if [ -z "$SOURCES" ]; then
    4.63 +    export SOURCES="$SDLPATH/test/testgles2.c"
    4.64 +fi
    4.65 +
    4.66 +if [ ! -f "$CC" ]; then
    4.67 +    echo "Could not find compiler at $CC"
    4.68 +    exit 1
    4.69 +fi
    4.70 +
    4.71 +
    4.72 +
    4.73 +
    4.74 +cd $BUILDPATH
    4.75 +$SDLPATH/configure --host=pnacl --prefix $TESTBUILDPATH
    4.76 +make -j$NCPUS CFLAGS="$CFLAGS -I./include"
    4.77 +make install
    4.78 +
    4.79 +if [ ! -f "$SDL2_STATIC" ]; then
    4.80 +    echo "Build failed! $SDL2_STATIC"
    4.81 +    exit 1
    4.82 +fi
    4.83 +
    4.84 +echo "Building test"
    4.85 +cp -f $SDLPATH/test/nacl/* $TESTBUILDPATH
    4.86 +# Some tests need these resource files
    4.87 +cp -f $SDLPATH/test/*.bmp $TESTBUILDPATH
    4.88 +cp -f $SDLPATH/test/*.wav $TESTBUILDPATH
    4.89 +cp -f $SDL2_STATIC $TESTBUILDPATH
    4.90 +
    4.91 +# Copy user sources
    4.92 +_SOURCES=($SOURCES)
    4.93 +for src in "${_SOURCES[@]}"
    4.94 +do
    4.95 +    cp $src $TESTBUILDPATH
    4.96 +done
    4.97 +export SOURCES="$SOURCES"
    4.98 +
    4.99 +cd $TESTBUILDPATH
   4.100 +make -j$NCPUS CONFIG="Release" CFLAGS="$CFLAGS -I$TESTBUILDPATH/include/SDL2 -I$SDLPATH/include"
   4.101 +make -j$NCPUS CONFIG="Debug" CFLAGS="$CFLAGS -I$TESTBUILDPATH/include/SDL2 -I$SDLPATH/include"
   4.102 +
   4.103 +echo
   4.104 +echo "Run the test with: "
   4.105 +echo "cd $TESTBUILDPATH;python -m SimpleHTTPServer"
   4.106 +echo "Then visit http://localhost:8000 with Chrome"
   4.107 +
   4.108 +cd $CURDIR
     5.1 --- a/configure	Thu Jun 05 15:37:33 2014 -0700
     5.2 +++ b/configure	Fri Jun 06 15:45:59 2014 -0300
     5.3 @@ -18984,6 +18984,55 @@
     5.4      fi
     5.5  }
     5.6  
     5.7 +CheckNativeClient()
     5.8 +{
     5.9 +        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
    5.10 +/* end confdefs.h.  */
    5.11 +
    5.12 +          #if !defined(__native_client__)
    5.13 +          #error "NO NACL"
    5.14 +          #endif
    5.15 +
    5.16 +int
    5.17 +main ()
    5.18 +{
    5.19 +
    5.20 +
    5.21 +  ;
    5.22 +  return 0;
    5.23 +}
    5.24 +_ACEOF
    5.25 +if ac_fn_c_try_compile "$LINENO"; then :
    5.26 +
    5.27 +        $as_echo "#define SDL_VIDEO_DRIVER_NACL 1" >>confdefs.h
    5.28 +
    5.29 +        $as_echo "#define SDL_AUDIO_DRIVER_NACL 1" >>confdefs.h
    5.30 +
    5.31 +
    5.32 +$as_echo "#define HAVE_POW 1" >>confdefs.h
    5.33 +
    5.34 +
    5.35 +$as_echo "#define HAVE_OPENGLES2 1" >>confdefs.h
    5.36 +
    5.37 +
    5.38 +$as_echo "#define SDL_VIDEO_OPENGL_ES2 1" >>confdefs.h
    5.39 +
    5.40 +
    5.41 +$as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
    5.42 +
    5.43 +
    5.44 +        SDL_LIBS="-lppapi_simple -lppapi_gles2 $SDL_LIBS"
    5.45 +
    5.46 +        SDLMAIN_SOURCES="$srcdir/src/main/nacl/*.c"
    5.47 +        SOURCES="$SOURCES $srcdir/src/audio/nacl/*.c"
    5.48 +        SUMMARY_audio="${SUMMARY_audio} nacl"
    5.49 +        SOURCES="$SOURCES $srcdir/src/video/nacl/*.c"
    5.50 +        SUMMARY_video="${SUMMARY_video} nacl opengles2"
    5.51 +
    5.52 +fi
    5.53 +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    5.54 +}
    5.55 +
    5.56  
    5.57  CheckX11()
    5.58  {
    5.59 @@ -23156,6 +23205,21 @@
    5.60              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit"
    5.61          fi
    5.62          ;;
    5.63 +    *-nacl|*-pnacl)
    5.64 +        ARCH=nacl
    5.65 +        CheckNativeClient
    5.66 +        CheckDummyAudio
    5.67 +        CheckDummyVideo
    5.68 +        CheckInputEvents
    5.69 +        # Set up files for the timer library
    5.70 +        if test x$enable_timers = xyes; then
    5.71 +            $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
    5.72 +
    5.73 +            SOURCES="$SOURCES $srcdir/src/timer/unix/*.c"
    5.74 +            have_timers=yes
    5.75 +        fi
    5.76 +        CheckPTHREAD
    5.77 +        ;;
    5.78      *)
    5.79          as_fn_error $? "
    5.80  *** Unsupported host:  Please add to configure.in
     6.1 --- a/configure.in	Thu Jun 05 15:37:33 2014 -0700
     6.2 +++ b/configure.in	Fri Jun 06 15:45:59 2014 -0300
     6.3 @@ -1317,6 +1317,32 @@
     6.4      fi
     6.5  }
     6.6  
     6.7 +dnl Check for Native Client stuff
     6.8 +CheckNativeClient()
     6.9 +{
    6.10 +        AC_TRY_COMPILE([
    6.11 +          #if !defined(__native_client__)
    6.12 +          #error "NO NACL"
    6.13 +          #endif
    6.14 +        ],[
    6.15 +        ],[
    6.16 +        AC_DEFINE(SDL_VIDEO_DRIVER_NACL)
    6.17 +        AC_DEFINE(SDL_AUDIO_DRIVER_NACL)
    6.18 +        AC_DEFINE(HAVE_POW, 1, [ ])
    6.19 +        AC_DEFINE(HAVE_OPENGLES2, 1, [ ])
    6.20 +        AC_DEFINE(SDL_VIDEO_OPENGL_ES2, 1, [ ])
    6.21 +        AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES2, 1, [ ])
    6.22 +
    6.23 +        SDL_LIBS="-lppapi_simple -lppapi_gles2 $SDL_LIBS"
    6.24 +
    6.25 +        SDLMAIN_SOURCES="$srcdir/src/main/nacl/*.c"
    6.26 +        SOURCES="$SOURCES $srcdir/src/audio/nacl/*.c"
    6.27 +        SUMMARY_audio="${SUMMARY_audio} nacl"
    6.28 +        SOURCES="$SOURCES $srcdir/src/video/nacl/*.c"
    6.29 +        SUMMARY_video="${SUMMARY_video} nacl opengles2"
    6.30 +        ])
    6.31 +}
    6.32 +
    6.33  
    6.34  dnl Find the X11 include and library directories
    6.35  CheckX11()
    6.36 @@ -3119,6 +3145,20 @@
    6.37              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit"
    6.38          fi
    6.39          ;;
    6.40 +    *-nacl|*-pnacl)
    6.41 +        ARCH=nacl
    6.42 +        CheckNativeClient
    6.43 +        CheckDummyAudio
    6.44 +        CheckDummyVideo
    6.45 +        CheckInputEvents
    6.46 +        # Set up files for the timer library
    6.47 +        if test x$enable_timers = xyes; then
    6.48 +            AC_DEFINE(SDL_TIMER_UNIX)
    6.49 +            SOURCES="$SOURCES $srcdir/src/timer/unix/*.c"
    6.50 +            have_timers=yes
    6.51 +        fi
    6.52 +        CheckPTHREAD      
    6.53 +        ;;
    6.54      *)
    6.55          AC_MSG_ERROR([
    6.56  *** Unsupported host:  Please add to configure.in
     7.1 --- a/include/SDL_assert.h	Thu Jun 05 15:37:33 2014 -0700
     7.2 +++ b/include/SDL_assert.h	Fri Jun 06 15:45:59 2014 -0300
     7.3 @@ -51,7 +51,7 @@
     7.4  /* Don't include intrin.h here because it contains C++ code */
     7.5      extern void __cdecl __debugbreak(void);
     7.6      #define SDL_TriggerBreakpoint() __debugbreak()
     7.7 -#elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
     7.8 +#elif (!defined(__NACL__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
     7.9      #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
    7.10  #elif defined(HAVE_SIGNAL_H)
    7.11      #include <signal.h>
     8.1 --- a/include/SDL_config.h.in	Thu Jun 05 15:37:33 2014 -0700
     8.2 +++ b/include/SDL_config.h.in	Fri Jun 06 15:45:59 2014 -0300
     8.3 @@ -208,6 +208,7 @@
     8.4  #undef SDL_AUDIO_DRIVER_DSOUND
     8.5  #undef SDL_AUDIO_DRIVER_ESD
     8.6  #undef SDL_AUDIO_DRIVER_ESD_DYNAMIC
     8.7 +#undef SDL_AUDIO_DRIVER_NACL
     8.8  #undef SDL_AUDIO_DRIVER_NAS
     8.9  #undef SDL_AUDIO_DRIVER_NAS_DYNAMIC
    8.10  #undef SDL_AUDIO_DRIVER_SNDIO
    8.11 @@ -297,6 +298,7 @@
    8.12  #undef SDL_VIDEO_DRIVER_X11_CONST_PARAM_XDATA32
    8.13  #undef SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY
    8.14  #undef SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
    8.15 +#undef SDL_VIDEO_DRIVER_NACL
    8.16  
    8.17  #undef SDL_VIDEO_RENDER_D3D
    8.18  #undef SDL_VIDEO_RENDER_D3D11
     9.1 --- a/include/SDL_main.h	Thu Jun 05 15:37:33 2014 -0700
     9.2 +++ b/include/SDL_main.h	Fri Jun 06 15:45:59 2014 -0300
     9.3 @@ -67,6 +67,15 @@
     9.4   */
     9.5  #define SDL_MAIN_NEEDED
     9.6  
     9.7 +#elif defined(__NACL__)
     9.8 +/* On NACL we use ppapi_simple to set up the application helper code,
     9.9 +   then wait for the first PSE_INSTANCE_DIDCHANGEVIEW event before 
    9.10 +   starting the user main function.
    9.11 +   All user code is run in a separate thread by ppapi_simple, thus 
    9.12 +   allowing for blocking io to take place via nacl_io
    9.13 +*/
    9.14 +#define SDL_MAIN_NEEDED
    9.15 +
    9.16  #endif
    9.17  #endif /* SDL_MAIN_HANDLED */
    9.18  
    10.1 --- a/include/SDL_platform.h	Thu Jun 05 15:37:33 2014 -0700
    10.2 +++ b/include/SDL_platform.h	Fri Jun 06 15:45:59 2014 -0300
    10.3 @@ -56,7 +56,7 @@
    10.4  #undef __IRIX__
    10.5  #define __IRIX__    1
    10.6  #endif
    10.7 -#if defined(linux) || defined(__linux) || defined(__linux__)
    10.8 +#if (defined(linux) || defined(__linux) || defined(__linux__))
    10.9  #undef __LINUX__
   10.10  #define __LINUX__   1
   10.11  #endif
   10.12 @@ -142,6 +142,23 @@
   10.13  #define __PSP__ 1
   10.14  #endif
   10.15  
   10.16 +/* The NACL compiler defines __native_client__ and __pnacl__
   10.17 + * Ref: http://www.chromium.org/nativeclient/pnacl/stability-of-the-pnacl-bitcode-abi
   10.18 + */
   10.19 +#if defined(__native_client__)
   10.20 +#undef __LINUX__
   10.21 +#undef __NACL__
   10.22 +#define __NACL__ 1
   10.23 +#endif
   10.24 +#if defined(__pnacl__)
   10.25 +#undef __LINUX__
   10.26 +#undef __PNACL__
   10.27 +#define __PNACL__ 1
   10.28 +/* PNACL with newlib supports static linking only */
   10.29 +#define __SDL_NOGETPROCADDR__
   10.30 +#endif
   10.31 +
   10.32 +
   10.33  #include "begin_code.h"
   10.34  /* Set up for C function definitions, even when using C++ */
   10.35  #ifdef __cplusplus
    11.1 --- a/include/SDL_rwops.h	Thu Jun 05 15:37:33 2014 -0700
    11.2 +++ b/include/SDL_rwops.h	Fri Jun 06 15:45:59 2014 -0300
    11.3 @@ -220,6 +220,16 @@
    11.4  extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
    11.5  /* @} *//* Write endian functions */
    11.6  
    11.7 +/**
    11.8 + *  \name Mount/umount functions
    11.9 + *
   11.10 + *  Required for RWOps on Native Client
   11.11 + */
   11.12 +/* @{ */
   11.13 +extern DECLSPEC int SDLCALL SDL_RWMount(const char* source, const char* target, 
   11.14 +                                        const char* filesystemtype, 
   11.15 +                                        unsigned long mountflags, const void *data);
   11.16 +extern DECLSPEC int SDLCALL SDL_RWUmount(const char *target);
   11.17  
   11.18  /* Ends C function definitions when using C++ */
   11.19  #ifdef __cplusplus
    12.1 --- a/src/SDL_log.c	Thu Jun 05 15:37:33 2014 -0700
    12.2 +++ b/src/SDL_log.c	Fri Jun 06 15:45:59 2014 -0300
    12.3 @@ -413,6 +413,9 @@
    12.4  #endif
    12.5  #if HAVE_STDIO_H
    12.6      fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
    12.7 +#if __NACL__
    12.8 +    fflush(stderr);
    12.9 +#endif
   12.10  #endif
   12.11  }
   12.12  
    13.1 --- a/src/audio/SDL_audio.c	Thu Jun 05 15:37:33 2014 -0700
    13.2 +++ b/src/audio/SDL_audio.c	Fri Jun 06 15:45:59 2014 -0300
    13.3 @@ -51,6 +51,9 @@
    13.4  extern AudioBootStrap SUNAUDIO_bootstrap;
    13.5  extern AudioBootStrap ARTS_bootstrap;
    13.6  extern AudioBootStrap ESD_bootstrap;
    13.7 +#if SDL_AUDIO_DRIVER_NACL
    13.8 +extern AudioBootStrap NACLAUD_bootstrap;
    13.9 +#endif
   13.10  extern AudioBootStrap NAS_bootstrap;
   13.11  extern AudioBootStrap XAUDIO2_bootstrap;
   13.12  extern AudioBootStrap DSOUND_bootstrap;
   13.13 @@ -69,6 +72,7 @@
   13.14  extern AudioBootStrap PSPAUD_bootstrap;
   13.15  extern AudioBootStrap SNDIO_bootstrap;
   13.16  
   13.17 +
   13.18  /* Available audio drivers */
   13.19  static const AudioBootStrap *const bootstrap[] = {
   13.20  #if SDL_AUDIO_DRIVER_PULSEAUDIO
   13.21 @@ -98,6 +102,9 @@
   13.22  #if SDL_AUDIO_DRIVER_ESD
   13.23      &ESD_bootstrap,
   13.24  #endif
   13.25 +#if SDL_AUDIO_DRIVER_NACL
   13.26 +   &NACLAUD_bootstrap,
   13.27 +#endif
   13.28  #if SDL_AUDIO_DRIVER_NAS
   13.29      &NAS_bootstrap,
   13.30  #endif
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/audio/nacl/SDL_naclaudio.c	Fri Jun 06 15:45:59 2014 -0300
    14.3 @@ -0,0 +1,135 @@
    14.4 +/*
    14.5 +  Simple DirectMedia Layer
    14.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    14.7 +
    14.8 +  This software is provided 'as-is', without any express or implied
    14.9 +  warranty.  In no event will the authors be held liable for any damages
   14.10 +  arising from the use of this software.
   14.11 +
   14.12 +  Permission is granted to anyone to use this software for any purpose,
   14.13 +  including commercial applications, and to alter it and redistribute it
   14.14 +  freely, subject to the following restrictions:
   14.15 +
   14.16 +  1. The origin of this software must not be misrepresented; you must not
   14.17 +     claim that you wrote the original software. If you use this software
   14.18 +     in a product, an acknowledgment in the product documentation would be
   14.19 +     appreciated but is not required.
   14.20 +  2. Altered source versions must be plainly marked as such, and must not be
   14.21 +     misrepresented as being the original software.
   14.22 +  3. This notice may not be removed or altered from any source distribution.
   14.23 +*/
   14.24 +
   14.25 +#include "../../SDL_internal.h"
   14.26 +#include "SDL_naclaudio.h"
   14.27 +
   14.28 +#include "SDL_audio.h"
   14.29 +#include "SDL_mutex.h"
   14.30 +#include "../SDL_audiomem.h"
   14.31 +#include "../SDL_audio_c.h"
   14.32 +#include "../SDL_audiodev_c.h"
   14.33 +
   14.34 +#include "ppapi/c/pp_errors.h"
   14.35 +#include "ppapi/c/pp_instance.h"
   14.36 +#include "ppapi_simple/ps.h"
   14.37 +#include "ppapi_simple/ps_interface.h"
   14.38 +#include "ppapi_simple/ps_event.h"
   14.39 +
   14.40 +/* The tag name used by NACL audio */
   14.41 +#define NACLAUD_DRIVER_NAME         "nacl"
   14.42 +
   14.43 +#define SAMPLE_FRAME_COUNT 4096
   14.44 +
   14.45 +/* Audio driver functions */
   14.46 +static int NACLAUD_OpenDevice(_THIS, const char *devname, int iscapture);
   14.47 +static void NACLAUD_CloseDevice(_THIS);
   14.48 +static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data);
   14.49 +
   14.50 +/* FIXME: Make use of latency if needed */
   14.51 +static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
   14.52 +    SDL_AudioDevice* _this = (SDL_AudioDevice*) data;
   14.53 +    
   14.54 +    SDL_LockMutex(private->mutex);
   14.55 +    /*if (private->opened) {*/
   14.56 +    SDL_memset(samples, _this->spec.silence, buffer_size);
   14.57 +    SDL_LockMutex(_this->mixer_lock);
   14.58 +    (*_this->spec.callback)(_this->spec.userdata, (Uint8*)samples, buffer_size);
   14.59 +    SDL_UnlockMutex(_this->mixer_lock);
   14.60 +    /*} else {
   14.61 +        SDL_memset(samples, 0, buffer_size);
   14.62 +    }*/
   14.63 +    SDL_UnlockMutex(private->mutex);
   14.64 +    
   14.65 +    return;
   14.66 +}
   14.67 +
   14.68 +static void NACLAUD_CloseDevice(SDL_AudioDevice *device) {
   14.69 +    const PPB_Core *core = PSInterfaceCore();
   14.70 +    const PPB_Audio *ppb_audio = PSInterfaceAudio();
   14.71 +    SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden;
   14.72 +    
   14.73 +    ppb_audio->StopPlayback(hidden->audio);
   14.74 +    SDL_DestroyMutex(hidden->mutex);
   14.75 +    core->ReleaseResource(hidden->audio);
   14.76 +}
   14.77 +
   14.78 +static int
   14.79 +NACLAUD_OpenDevice(_THIS, const char *devname, int iscapture) {
   14.80 +    PP_Instance instance = PSGetInstanceId();
   14.81 +    const PPB_Audio *ppb_audio = PSInterfaceAudio();
   14.82 +    const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig();
   14.83 +    
   14.84 +    private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
   14.85 +    if (private == NULL) {
   14.86 +        SDL_OutOfMemory();
   14.87 +        return 0;
   14.88 +    }
   14.89 +    
   14.90 +    private->mutex = SDL_CreateMutex();
   14.91 +    _this->spec.freq = 44100;
   14.92 +    _this->spec.format = AUDIO_S16LSB;
   14.93 +    _this->spec.channels = 2;
   14.94 +    _this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount(
   14.95 +        instance, 
   14.96 +        PP_AUDIOSAMPLERATE_44100, 
   14.97 +        SAMPLE_FRAME_COUNT);
   14.98 +    
   14.99 +    private->audio = ppb_audio->Create(
  14.100 +        instance,
  14.101 +        ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
  14.102 +        nacl_audio_callback, 
  14.103 +        _this);
  14.104 +    
  14.105 +    /* Start audio playback while we are still on the main thread. */
  14.106 +    ppb_audio->StartPlayback(private->audio);
  14.107 +    
  14.108 +    return 1;
  14.109 +}
  14.110 +
  14.111 +static int
  14.112 +NACLAUD_Init(SDL_AudioDriverImpl * impl)
  14.113 +{
  14.114 +    if (PSGetInstanceId() == 0) {
  14.115 +        return 0;
  14.116 +    }
  14.117 +    
  14.118 +    /* Set the function pointers */
  14.119 +    impl->OpenDevice = NACLAUD_OpenDevice;
  14.120 +    impl->CloseDevice = NACLAUD_CloseDevice;
  14.121 +    impl->HasCaptureSupport = 0;
  14.122 +    impl->OnlyHasDefaultOutputDevice = 1;
  14.123 +    impl->OnlyHasDefaultInputDevice = 1;
  14.124 +    impl->ProvidesOwnCallbackThread = 1;
  14.125 +    /*
  14.126 +     *    impl->WaitDevice = NACLAUD_WaitDevice;
  14.127 +     *    impl->GetDeviceBuf = NACLAUD_GetDeviceBuf;
  14.128 +     *    impl->PlayDevice = NACLAUD_PlayDevice;
  14.129 +     *    impl->Deinitialize = NACLAUD_Deinitialize;
  14.130 +     */
  14.131 +    
  14.132 +    return 1;
  14.133 +}
  14.134 +
  14.135 +AudioBootStrap NACLAUD_bootstrap = {
  14.136 +    NACLAUD_DRIVER_NAME, "SDL NaCl Audio Driver",
  14.137 +    NACLAUD_Init, 0
  14.138 +};
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/src/audio/nacl/SDL_naclaudio.h	Fri Jun 06 15:45:59 2014 -0300
    15.3 @@ -0,0 +1,41 @@
    15.4 +/*
    15.5 +  Simple DirectMedia Layer
    15.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    15.7 +
    15.8 +  This software is provided 'as-is', without any express or implied
    15.9 +  warranty.  In no event will the authors be held liable for any damages
   15.10 +  arising from the use of this software.
   15.11 +
   15.12 +  Permission is granted to anyone to use this software for any purpose,
   15.13 +  including commercial applications, and to alter it and redistribute it
   15.14 +  freely, subject to the following restrictions:
   15.15 +
   15.16 +  1. The origin of this software must not be misrepresented; you must not
   15.17 +     claim that you wrote the original software. If you use this software
   15.18 +     in a product, an acknowledgment in the product documentation would be
   15.19 +     appreciated but is not required.
   15.20 +  2. Altered source versions must be plainly marked as such, and must not be
   15.21 +     misrepresented as being the original software.
   15.22 +  3. This notice may not be removed or altered from any source distribution.
   15.23 +*/
   15.24 +
   15.25 +#include "../../SDL_internal.h"
   15.26 +
   15.27 +#ifndef _SDL_naclaudio_h
   15.28 +#define _SDL_naclaudio_h
   15.29 +
   15.30 +#include "SDL_audio.h"
   15.31 +#include "../SDL_sysaudio.h"
   15.32 +#include "SDL_mutex.h"
   15.33 +
   15.34 +#include "ppapi/c/ppb_audio.h"
   15.35 +
   15.36 +#define _THIS  SDL_AudioDevice *_this
   15.37 +#define private _this->hidden
   15.38 +
   15.39 +typedef struct SDL_PrivateAudioData {
   15.40 +  SDL_mutex* mutex;
   15.41 +  PP_Resource audio;
   15.42 +} SDL_PrivateAudioData;
   15.43 +
   15.44 +#endif /* _SDL_naclaudio_h */
    16.1 --- a/src/dynapi/SDL_dynapi.h	Thu Jun 05 15:37:33 2014 -0700
    16.2 +++ b/src/dynapi/SDL_dynapi.h	Fri Jun 06 15:45:59 2014 -0300
    16.3 @@ -43,7 +43,7 @@
    16.4  #include "TargetConditionals.h"
    16.5  #endif
    16.6  
    16.7 -#if TARGET_OS_IPHONE  /* probably not useful on iOS. */
    16.8 +#if TARGET_OS_IPHONE || __native_client__  /* probably not useful on iOS or NACL. */
    16.9  #define SDL_DYNAMIC_API 0
   16.10  #elif SDL_BUILDING_WINRT /* probaly not useful on WinRT, given current .dll loading restrictions */
   16.11  #define SDL_DYNAMIC_API 0
    17.1 --- a/src/file/SDL_rwops.c	Thu Jun 05 15:37:33 2014 -0700
    17.2 +++ b/src/file/SDL_rwops.c	Fri Jun 06 15:45:59 2014 -0300
    17.3 @@ -43,6 +43,10 @@
    17.4  #include "SDL_system.h"
    17.5  #endif
    17.6  
    17.7 +#if __NACL__
    17.8 +#include "nacl_io/nacl_io.h"
    17.9 +#endif
   17.10 +
   17.11  #ifdef __WIN32__
   17.12  
   17.13  /* Functions to read/write Win32 API file pointers */
   17.14 @@ -762,4 +766,28 @@
   17.15      return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
   17.16  }
   17.17  
   17.18 +
   17.19 +/* SDL_RWops on NACL are implemented using nacl_io, and require mount points
   17.20 + * to be established before actual file operations are performed
   17.21 + * 
   17.22 + * Ref: https://developers.google.com/native-client/dev/devguide/coding/nacl_io?hl=es
   17.23 + */
   17.24 +
   17.25 +int 
   17.26 +SDL_RWMount(const char* source, const char* target, const char* filesystemtype, 
   17.27 +          unsigned long mountflags, const void *data) {
   17.28 +#if __NACL__
   17.29 +    return mount(source, target, filesystemtype, mountflags, data);
   17.30 +#endif /* __NACL__ */
   17.31 +    return SDL_SetError ("Mount not supported on this platform");
   17.32 +}
   17.33 +
   17.34 +int 
   17.35 +SDL_RWUmount(const char *target) {
   17.36 +#if __NACL__
   17.37 +    return umount(target);
   17.38 +#endif /* __NACL__ */
   17.39 +    return SDL_SetError ("Umount not supported on this platform");
   17.40 +}
   17.41 +
   17.42  /* vi: set ts=4 sw=4 expandtab: */
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/src/main/nacl/SDL_nacl_main.c	Fri Jun 06 15:45:59 2014 -0300
    18.3 @@ -0,0 +1,77 @@
    18.4 +/*
    18.5 +  Simple DirectMedia Layer
    18.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    18.7 +
    18.8 +  This software is provided 'as-is', without any express or implied
    18.9 +  warranty.  In no event will the authors be held liable for any damages
   18.10 +  arising from the use of this software.
   18.11 +
   18.12 +  Permission is granted to anyone to use this software for any purpose,
   18.13 +  including commercial applications, and to alter it and redistribute it
   18.14 +  freely, subject to the following restrictions:
   18.15 +
   18.16 +  1. The origin of this software must not be misrepresented; you must not
   18.17 +     claim that you wrote the original software. If you use this software
   18.18 +     in a product, an acknowledgment in the product documentation would be
   18.19 +     appreciated but is not required.
   18.20 +  2. Altered source versions must be plainly marked as such, and must not be
   18.21 +     misrepresented as being the original software.
   18.22 +  3. This notice may not be removed or altered from any source distribution.
   18.23 +*/
   18.24 +#include "../../SDL_internal.h"
   18.25 +
   18.26 +#if SDL_VIDEO_DRIVER_NACL
   18.27 +
   18.28 +/* Include the SDL main definition header */
   18.29 +#include "SDL_main.h"
   18.30 +
   18.31 +#include "ppapi_simple/ps_main.h"
   18.32 +#include "ppapi_simple/ps_event.h"
   18.33 +#include "ppapi_simple/ps_interface.h"
   18.34 +
   18.35 +extern void NACL_SetScreenResolution(int width, int height, Uint32 format);
   18.36 +
   18.37 +int
   18.38 +nacl_main(int argc, char *argv[])
   18.39 +{
   18.40 +    int status;
   18.41 +    PSEvent* ps_event;
   18.42 +    PP_Resource event;  
   18.43 +    struct PP_Rect rect;
   18.44 +    int ready = 0;
   18.45 +    const PPB_View *ppb_view = PSInterfaceView();
   18.46 +    
   18.47 +    /* This is started in a worker thread by ppapi_simple! */
   18.48 +    
   18.49 +    /* Wait for the first PSE_INSTANCE_DIDCHANGEVIEW event before starting the app */
   18.50 +    
   18.51 +    PSEventSetFilter(PSE_INSTANCE_DIDCHANGEVIEW);
   18.52 +    while (!ready) {
   18.53 +        /* Process all waiting events without blocking */
   18.54 +        while (!ready && (ps_event = PSEventWaitAcquire()) != NULL) {
   18.55 +            event = ps_event->as_resource;
   18.56 +            switch(ps_event->type) {
   18.57 +                /* From DidChangeView, contains a view resource */
   18.58 +                case PSE_INSTANCE_DIDCHANGEVIEW:
   18.59 +                    ppb_view->GetRect(event, &rect);
   18.60 +                    NACL_SetScreenResolution(rect.size.width, rect.size.height, 0);
   18.61 +                    ready = 1;
   18.62 +                    break;
   18.63 +                default:
   18.64 +                    break;
   18.65 +            }
   18.66 +            PSEventRelease(ps_event);
   18.67 +        }
   18.68 +    }
   18.69 +    
   18.70 +    /* Everything is ready, start the user main function */
   18.71 +    SDL_SetMainReady();
   18.72 +    status = SDL_main(argc, argv);
   18.73 +
   18.74 +    return 0;
   18.75 +}
   18.76 +
   18.77 +/* ppapi_simple will start nacl_main in a worker thread */
   18.78 +PPAPI_SIMPLE_REGISTER_MAIN(nacl_main);
   18.79 +
   18.80 +#endif /* SDL_VIDEO_DRIVER_NACL */
   18.81 \ No newline at end of file
    19.1 --- a/src/render/opengles2/SDL_gles2funcs.h	Thu Jun 05 15:37:33 2014 -0700
    19.2 +++ b/src/render/opengles2/SDL_gles2funcs.h	Fri Jun 06 15:45:59 2014 -0300
    19.3 @@ -68,4 +68,4 @@
    19.4  SDL_PROC(GLenum, glCheckFramebufferStatus, (GLenum))
    19.5  SDL_PROC(void, glDeleteFramebuffers, (GLsizei, const GLuint *))
    19.6  SDL_PROC(GLint, glGetAttribLocation, (GLuint, const GLchar *))
    19.7 - 
    19.8 +SDL_PROC(void, glGetProgramInfoLog, (GLuint, GLsizei, GLsizei*, GLchar*))
    20.1 --- a/src/thread/pthread/SDL_systhread.c	Thu Jun 05 15:37:33 2014 -0700
    20.2 +++ b/src/thread/pthread/SDL_systhread.c	Fri Jun 06 15:45:59 2014 -0300
    20.3 @@ -141,12 +141,15 @@
    20.4          #endif
    20.5      }
    20.6  
    20.7 +   /* NativeClient does not yet support signals.*/
    20.8 +#ifndef __NACL__
    20.9      /* Mask asynchronous signals for this thread */
   20.10      sigemptyset(&mask);
   20.11      for (i = 0; sig_list[i]; ++i) {
   20.12          sigaddset(&mask, sig_list[i]);
   20.13      }
   20.14      pthread_sigmask(SIG_BLOCK, &mask, 0);
   20.15 +#endif
   20.16  
   20.17  #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   20.18      /* Allow ourselves to be asynchronously cancelled */
   20.19 @@ -166,7 +169,10 @@
   20.20  int
   20.21  SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
   20.22  {
   20.23 -#ifdef __LINUX__
   20.24 +#if __NACL__ 
   20.25 +    /* FIXME: Setting thread priority does not seem to be supported in NACL */
   20.26 +    return 0;
   20.27 +#elif __LINUX__
   20.28      int value;
   20.29  
   20.30      if (priority == SDL_THREAD_PRIORITY_LOW) {
    21.1 --- a/src/video/SDL_sysvideo.h	Thu Jun 05 15:37:33 2014 -0700
    21.2 +++ b/src/video/SDL_sysvideo.h	Fri Jun 06 15:45:59 2014 -0300
    21.3 @@ -381,6 +381,9 @@
    21.4  #if SDL_VIDEO_DRIVER_WAYLAND
    21.5  extern VideoBootStrap Wayland_bootstrap;
    21.6  #endif
    21.7 +#if SDL_VIDEO_DRIVER_NACL
    21.8 +extern VideoBootStrap NACL_bootstrap;
    21.9 +#endif
   21.10  
   21.11  extern SDL_VideoDevice *SDL_GetVideoDevice(void);
   21.12  extern int SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode);
    22.1 --- a/src/video/SDL_video.c	Thu Jun 05 15:37:33 2014 -0700
    22.2 +++ b/src/video/SDL_video.c	Fri Jun 06 15:45:59 2014 -0300
    22.3 @@ -92,6 +92,9 @@
    22.4  #if SDL_VIDEO_DRIVER_WAYLAND
    22.5      &Wayland_bootstrap,
    22.6  #endif
    22.7 +#if SDL_VIDEO_DRIVER_NACL
    22.8 +    &NACL_bootstrap,
    22.9 +#endif
   22.10  #if SDL_VIDEO_DRIVER_DUMMY
   22.11      &DUMMY_bootstrap,
   22.12  #endif
   22.13 @@ -253,7 +256,7 @@
   22.14                  }
   22.15              }
   22.16          }
   22.17 -
   22.18 +        
   22.19          if (!renderer) {
   22.20              for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
   22.21                  SDL_GetRenderDriverInfo(i, &info);
   22.22 @@ -1239,7 +1242,7 @@
   22.23      }
   22.24  
   22.25      /* Some platforms have OpenGL enabled by default */
   22.26 -#if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__
   22.27 +#if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__ || __NACL__
   22.28      flags |= SDL_WINDOW_OPENGL;
   22.29  #endif
   22.30      if (flags & SDL_WINDOW_OPENGL) {
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/src/video/nacl/SDL_naclevents.c	Fri Jun 06 15:45:59 2014 -0300
    23.3 @@ -0,0 +1,432 @@
    23.4 +/*
    23.5 +  Simple DirectMedia Layer
    23.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    23.7 +
    23.8 +  This software is provided 'as-is', without any express or implied
    23.9 +  warranty.  In no event will the authors be held liable for any damages
   23.10 +  arising from the use of this software.
   23.11 +
   23.12 +  Permission is granted to anyone to use this software for any purpose,
   23.13 +  including commercial applications, and to alter it and redistribute it
   23.14 +  freely, subject to the following restrictions:
   23.15 +
   23.16 + 1. The origin of this software must not be misrepresented; you must not
   23.17 +     claim that you wrote the original software. If you use this software
   23.18 +     in a product, an acknowledgment in the product documentation would be
   23.19 +     appreciated but is not required.
   23.20 + 2. Altered source versions must be plainly marked as such, and must not be
   23.21 +     misrepresented as being the original software.
   23.22 + 3. This notice may not be removed or altered from any source distribution.
   23.23 +*/
   23.24 +#include "../../SDL_internal.h"
   23.25 +
   23.26 +#include "SDL.h"
   23.27 +#include "../../events/SDL_sysevents.h"
   23.28 +#include "../../events/SDL_events_c.h"
   23.29 +#include "SDL_naclevents_c.h"
   23.30 +#include "SDL_naclvideo.h"
   23.31 +#include "ppapi_simple/ps_event.h"
   23.32 +
   23.33 +/* Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent */
   23.34 +
   23.35 +static SDL_Scancode NACL_Keycodes[] = {
   23.36 +    SDL_SCANCODE_UNKNOWN,               /* 0 */
   23.37 +    SDL_SCANCODE_UNKNOWN,               /* 1 */
   23.38 +    SDL_SCANCODE_UNKNOWN,               /* 2 */
   23.39 +    SDL_SCANCODE_CANCEL,                /* DOM_VK_CANCEL 3 */
   23.40 +    SDL_SCANCODE_UNKNOWN,               /* 4 */
   23.41 +    SDL_SCANCODE_UNKNOWN,               /* 5 */
   23.42 +    SDL_SCANCODE_HELP,                  /* DOM_VK_HELP 6 */
   23.43 +    SDL_SCANCODE_UNKNOWN,               /* 7 */
   23.44 +    SDL_SCANCODE_BACKSPACE,             /* DOM_VK_BACK_SPACE 8 */
   23.45 +    SDL_SCANCODE_TAB,                   /* DOM_VK_TAB 9 */
   23.46 +    SDL_SCANCODE_UNKNOWN,               /* 10 */
   23.47 +    SDL_SCANCODE_UNKNOWN,               /* 11 */
   23.48 +    SDL_SCANCODE_CLEAR,                 /* DOM_VK_CLEAR 12 */
   23.49 +    SDL_SCANCODE_RETURN,                /* DOM_VK_RETURN 13 */
   23.50 +    SDL_SCANCODE_RETURN,                /* DOM_VK_ENTER 14 */
   23.51 +    SDL_SCANCODE_UNKNOWN,               /* 15 */
   23.52 +    SDL_SCANCODE_LSHIFT,                /* DOM_VK_SHIFT 16 */
   23.53 +    SDL_SCANCODE_LCTRL,                 /* DOM_VK_CONTROL 17 */
   23.54 +    SDL_SCANCODE_LALT,                  /* DOM_VK_ALT 18 */
   23.55 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_PAUSE 19 */
   23.56 +    SDL_SCANCODE_CAPSLOCK,              /* DOM_VK_CAPS_LOCK 20 */
   23.57 +    SDL_SCANCODE_LANG1,                 /* DOM_VK_KANA  DOM_VK_HANGUL 21 */
   23.58 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_EISU 22 */
   23.59 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_JUNJA 23 */
   23.60 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_FINAL 24 */
   23.61 +    SDL_SCANCODE_LANG2,                 /* DOM_VK_HANJA  DOM_VK_KANJI 25 */
   23.62 +    SDL_SCANCODE_UNKNOWN,               /* 26 */
   23.63 +    SDL_SCANCODE_ESCAPE,                /* DOM_VK_ESCAPE 27 */
   23.64 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_CONVERT 28 */
   23.65 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_NONCONVERT 29 */
   23.66 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_ACCEPT 30 */
   23.67 +    SDL_SCANCODE_MODE,                  /* DOM_VK_MODECHANGE 31 */
   23.68 +    SDL_SCANCODE_SPACE,                 /* DOM_VK_SPACE 32 */
   23.69 +    SDL_SCANCODE_PAGEUP,                /* DOM_VK_PAGE_UP 33 */
   23.70 +    SDL_SCANCODE_PAGEDOWN,              /* DOM_VK_PAGE_DOWN 34 */
   23.71 +    SDL_SCANCODE_END,                   /* DOM_VK_END 35 */
   23.72 +    SDL_SCANCODE_HOME,                  /* DOM_VK_HOME 36 */
   23.73 +    SDL_SCANCODE_LEFT,                  /* DOM_VK_LEFT 37 */
   23.74 +    SDL_SCANCODE_UP,                    /* DOM_VK_UP 38 */
   23.75 +    SDL_SCANCODE_RIGHT,                 /* DOM_VK_RIGHT 39 */
   23.76 +    SDL_SCANCODE_DOWN,                  /* DOM_VK_DOWN 40 */
   23.77 +    SDL_SCANCODE_SELECT,                /* DOM_VK_SELECT 41 */
   23.78 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_PRINT 42 */
   23.79 +    SDL_SCANCODE_EXECUTE,               /* DOM_VK_EXECUTE 43 */
   23.80 +    SDL_SCANCODE_PRINTSCREEN,           /* DOM_VK_PRINTSCREEN 44 */
   23.81 +    SDL_SCANCODE_INSERT,                /* DOM_VK_INSERT 45 */
   23.82 +    SDL_SCANCODE_DELETE,                /* DOM_VK_DELETE 46 */
   23.83 +    SDL_SCANCODE_UNKNOWN,               /* 47 */
   23.84 +    SDL_SCANCODE_0,                     /* DOM_VK_0 48 */
   23.85 +    SDL_SCANCODE_1,                     /* DOM_VK_1 49 */
   23.86 +    SDL_SCANCODE_2,                     /* DOM_VK_2 50 */
   23.87 +    SDL_SCANCODE_3,                     /* DOM_VK_3 51 */
   23.88 +    SDL_SCANCODE_4,                     /* DOM_VK_4 52 */
   23.89 +    SDL_SCANCODE_5,                     /* DOM_VK_5 53 */
   23.90 +    SDL_SCANCODE_6,                     /* DOM_VK_6 54 */
   23.91 +    SDL_SCANCODE_7,                     /* DOM_VK_7 55 */
   23.92 +    SDL_SCANCODE_8,                     /* DOM_VK_8 56 */
   23.93 +    SDL_SCANCODE_9,                     /* DOM_VK_9 57 */
   23.94 +    SDL_SCANCODE_KP_COLON,              /* DOM_VK_COLON 58 */
   23.95 +    SDL_SCANCODE_SEMICOLON,             /* DOM_VK_SEMICOLON 59 */
   23.96 +    SDL_SCANCODE_KP_LESS,               /* DOM_VK_LESS_THAN 60 */
   23.97 +    SDL_SCANCODE_EQUALS,                /* DOM_VK_EQUALS 61 */
   23.98 +    SDL_SCANCODE_KP_GREATER,            /* DOM_VK_GREATER_THAN 62 */
   23.99 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_QUESTION_MARK 63 */
  23.100 +    SDL_SCANCODE_KP_AT,                 /* DOM_VK_AT 64 */
  23.101 +    SDL_SCANCODE_A,                     /* DOM_VK_A 65 */
  23.102 +    SDL_SCANCODE_B,                     /* DOM_VK_B 66 */
  23.103 +    SDL_SCANCODE_C,                     /* DOM_VK_C 67 */
  23.104 +    SDL_SCANCODE_D,                     /* DOM_VK_D 68 */
  23.105 +    SDL_SCANCODE_E,                     /* DOM_VK_E 69 */
  23.106 +    SDL_SCANCODE_F,                     /* DOM_VK_F 70 */
  23.107 +    SDL_SCANCODE_G,                     /* DOM_VK_G 71 */
  23.108 +    SDL_SCANCODE_H,                     /* DOM_VK_H 72 */
  23.109 +    SDL_SCANCODE_I,                     /* DOM_VK_I 73 */
  23.110 +    SDL_SCANCODE_J,                     /* DOM_VK_J 74 */
  23.111 +    SDL_SCANCODE_K,                     /* DOM_VK_K 75 */
  23.112 +    SDL_SCANCODE_L,                     /* DOM_VK_L 76 */
  23.113 +    SDL_SCANCODE_M,                     /* DOM_VK_M 77 */
  23.114 +    SDL_SCANCODE_N,                     /* DOM_VK_N 78 */
  23.115 +    SDL_SCANCODE_O,                     /* DOM_VK_O 79 */
  23.116 +    SDL_SCANCODE_P,                     /* DOM_VK_P 80 */
  23.117 +    SDL_SCANCODE_Q,                     /* DOM_VK_Q 81 */
  23.118 +    SDL_SCANCODE_R,                     /* DOM_VK_R 82 */
  23.119 +    SDL_SCANCODE_S,                     /* DOM_VK_S 83 */
  23.120 +    SDL_SCANCODE_T,                     /* DOM_VK_T 84 */
  23.121 +    SDL_SCANCODE_U,                     /* DOM_VK_U 85 */
  23.122 +    SDL_SCANCODE_V,                     /* DOM_VK_V 86 */
  23.123 +    SDL_SCANCODE_W,                     /* DOM_VK_W 87 */
  23.124 +    SDL_SCANCODE_X,                     /* DOM_VK_X 88 */
  23.125 +    SDL_SCANCODE_Y,                     /* DOM_VK_Y 89 */
  23.126 +    SDL_SCANCODE_Z,                     /* DOM_VK_Z 90 */
  23.127 +    SDL_SCANCODE_LGUI,                  /* DOM_VK_WIN 91 */
  23.128 +    SDL_SCANCODE_UNKNOWN,               /* 92 */
  23.129 +    SDL_SCANCODE_APPLICATION,           /* DOM_VK_CONTEXT_MENU 93 */
  23.130 +    SDL_SCANCODE_UNKNOWN,               /* 94 */
  23.131 +    SDL_SCANCODE_SLEEP,                 /* DOM_VK_SLEEP 95 */
  23.132 +    SDL_SCANCODE_KP_0,                  /* DOM_VK_NUMPAD0 96 */
  23.133 +    SDL_SCANCODE_KP_1,                  /* DOM_VK_NUMPAD1 97 */
  23.134 +    SDL_SCANCODE_KP_2,                  /* DOM_VK_NUMPAD2 98 */
  23.135 +    SDL_SCANCODE_KP_3,                  /* DOM_VK_NUMPAD3 99 */
  23.136 +    SDL_SCANCODE_KP_4,                  /* DOM_VK_NUMPAD4 100 */
  23.137 +    SDL_SCANCODE_KP_5,                  /* DOM_VK_NUMPAD5 101 */
  23.138 +    SDL_SCANCODE_KP_6,                  /* DOM_VK_NUMPAD6 102 */
  23.139 +    SDL_SCANCODE_KP_7,                  /* DOM_VK_NUMPAD7 103 */
  23.140 +    SDL_SCANCODE_KP_8,                  /* DOM_VK_NUMPAD8 104 */
  23.141 +    SDL_SCANCODE_KP_9,                  /* DOM_VK_NUMPAD9 105 */
  23.142 +    SDL_SCANCODE_KP_MULTIPLY,           /* DOM_VK_MULTIPLY 106 */
  23.143 +    SDL_SCANCODE_KP_PLUS,               /* DOM_VK_ADD 107 */
  23.144 +    SDL_SCANCODE_KP_COMMA,              /* DOM_VK_SEPARATOR 108 */
  23.145 +    SDL_SCANCODE_KP_MINUS,              /* DOM_VK_SUBTRACT 109 */
  23.146 +    SDL_SCANCODE_KP_PERIOD,             /* DOM_VK_DECIMAL 110 */
  23.147 +    SDL_SCANCODE_KP_DIVIDE,             /* DOM_VK_DIVIDE 111 */
  23.148 +    SDL_SCANCODE_F1,                    /* DOM_VK_F1 112 */
  23.149 +    SDL_SCANCODE_F2,                    /* DOM_VK_F2 113 */
  23.150 +    SDL_SCANCODE_F3,                    /* DOM_VK_F3 114 */
  23.151 +    SDL_SCANCODE_F4,                    /* DOM_VK_F4 115 */
  23.152 +    SDL_SCANCODE_F5,                    /* DOM_VK_F5 116 */
  23.153 +    SDL_SCANCODE_F6,                    /* DOM_VK_F6 117 */
  23.154 +    SDL_SCANCODE_F7,                    /* DOM_VK_F7 118 */
  23.155 +    SDL_SCANCODE_F8,                    /* DOM_VK_F8 119 */
  23.156 +    SDL_SCANCODE_F9,                    /* DOM_VK_F9 120 */
  23.157 +    SDL_SCANCODE_F10,                   /* DOM_VK_F10 121 */
  23.158 +    SDL_SCANCODE_F11,                   /* DOM_VK_F11 122 */
  23.159 +    SDL_SCANCODE_F12,                   /* DOM_VK_F12 123 */
  23.160 +    SDL_SCANCODE_F13,                   /* DOM_VK_F13 124 */
  23.161 +    SDL_SCANCODE_F14,                   /* DOM_VK_F14 125 */
  23.162 +    SDL_SCANCODE_F15,                   /* DOM_VK_F15 126 */
  23.163 +    SDL_SCANCODE_F16,                   /* DOM_VK_F16 127 */
  23.164 +    SDL_SCANCODE_F17,                   /* DOM_VK_F17 128 */
  23.165 +    SDL_SCANCODE_F18,                   /* DOM_VK_F18 129 */
  23.166 +    SDL_SCANCODE_F19,                   /* DOM_VK_F19 130 */
  23.167 +    SDL_SCANCODE_F20,                   /* DOM_VK_F20 131 */
  23.168 +    SDL_SCANCODE_F21,                   /* DOM_VK_F21 132 */
  23.169 +    SDL_SCANCODE_F22,                   /* DOM_VK_F22 133 */
  23.170 +    SDL_SCANCODE_F23,                   /* DOM_VK_F23 134 */
  23.171 +    SDL_SCANCODE_F24,                   /* DOM_VK_F24 135 */
  23.172 +    SDL_SCANCODE_UNKNOWN,               /* 136 */
  23.173 +    SDL_SCANCODE_UNKNOWN,               /* 137 */
  23.174 +    SDL_SCANCODE_UNKNOWN,               /* 138 */
  23.175 +    SDL_SCANCODE_UNKNOWN,               /* 139 */
  23.176 +    SDL_SCANCODE_UNKNOWN,               /* 140 */
  23.177 +    SDL_SCANCODE_UNKNOWN,               /* 141 */
  23.178 +    SDL_SCANCODE_UNKNOWN,               /* 142 */
  23.179 +    SDL_SCANCODE_UNKNOWN,               /* 143 */
  23.180 +    SDL_SCANCODE_NUMLOCKCLEAR,          /* DOM_VK_NUM_LOCK 144 */
  23.181 +    SDL_SCANCODE_SCROLLLOCK,            /* DOM_VK_SCROLL_LOCK 145 */
  23.182 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FJ_JISHO 146 */
  23.183 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FJ_MASSHOU 147 */
  23.184 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FJ_TOUROKU 148 */
  23.185 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FJ_LOYA 149 */
  23.186 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FJ_ROYA 150 */
  23.187 +    SDL_SCANCODE_UNKNOWN,               /* 151 */
  23.188 +    SDL_SCANCODE_UNKNOWN,               /* 152 */
  23.189 +    SDL_SCANCODE_UNKNOWN,               /* 153 */
  23.190 +    SDL_SCANCODE_UNKNOWN,               /* 154 */
  23.191 +    SDL_SCANCODE_UNKNOWN,               /* 155 */
  23.192 +    SDL_SCANCODE_UNKNOWN,               /* 156 */
  23.193 +    SDL_SCANCODE_UNKNOWN,               /* 157 */
  23.194 +    SDL_SCANCODE_UNKNOWN,               /* 158 */
  23.195 +    SDL_SCANCODE_UNKNOWN,               /* 159 */
  23.196 +    SDL_SCANCODE_GRAVE,                 /* DOM_VK_CIRCUMFLEX 160 */
  23.197 +    SDL_SCANCODE_KP_EXCLAM,             /* DOM_VK_EXCLAMATION 161 */
  23.198 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_DOUBLE_QUOTE 162 */
  23.199 +    SDL_SCANCODE_KP_HASH,               /* DOM_VK_HASH 163 */
  23.200 +    SDL_SCANCODE_CURRENCYUNIT,          /* DOM_VK_DOLLAR 164 */
  23.201 +    SDL_SCANCODE_KP_PERCENT,            /* DOM_VK_PERCENT 165 */
  23.202 +    SDL_SCANCODE_KP_AMPERSAND,          /* DOM_VK_AMPERSAND 166 */
  23.203 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_UNDERSCORE 167 */
  23.204 +    SDL_SCANCODE_KP_LEFTPAREN,          /* DOM_VK_OPEN_PAREN 168 */
  23.205 +    SDL_SCANCODE_KP_RIGHTPAREN,         /* DOM_VK_CLOSE_PAREN 169 */
  23.206 +    SDL_SCANCODE_KP_MULTIPLY,           /* DOM_VK_ASTERISK 170 */
  23.207 +    SDL_SCANCODE_KP_PLUS,               /* DOM_VK_PLUS 171 */
  23.208 +    SDL_SCANCODE_KP_PLUS,               /* DOM_VK_PIPE 172 */
  23.209 +    SDL_SCANCODE_MINUS,                 /* DOM_VK_HYPHEN_MINUS 173 */
  23.210 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_OPEN_CURLY_BRACKET 174 */
  23.211 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_CLOSE_CURLY_BRACKET 175 */
  23.212 +    SDL_SCANCODE_NONUSBACKSLASH,        /* DOM_VK_TILDE 176 */
  23.213 +    SDL_SCANCODE_UNKNOWN,               /* 177 */
  23.214 +    SDL_SCANCODE_UNKNOWN,               /* 178 */
  23.215 +    SDL_SCANCODE_UNKNOWN,               /* 179 */
  23.216 +    SDL_SCANCODE_UNKNOWN,               /* 180 */
  23.217 +    SDL_SCANCODE_MUTE,                  /* DOM_VK_VOLUME_MUTE 181 */
  23.218 +    SDL_SCANCODE_VOLUMEDOWN,            /* DOM_VK_VOLUME_DOWN 182 */
  23.219 +    SDL_SCANCODE_VOLUMEUP,              /* DOM_VK_VOLUME_UP 183 */
  23.220 +    SDL_SCANCODE_UNKNOWN,               /* 184 */
  23.221 +    SDL_SCANCODE_UNKNOWN,               /* 185 */
  23.222 +    SDL_SCANCODE_UNKNOWN,               /* 186 */
  23.223 +    SDL_SCANCODE_UNKNOWN,               /* 187 */
  23.224 +    SDL_SCANCODE_COMMA,                 /* DOM_VK_COMMA 188 */
  23.225 +    SDL_SCANCODE_UNKNOWN,               /* 189 */
  23.226 +    SDL_SCANCODE_PERIOD,                /* DOM_VK_PERIOD 190 */
  23.227 +    SDL_SCANCODE_SLASH,                 /* DOM_VK_SLASH 191 */
  23.228 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_BACK_QUOTE 192 */
  23.229 +    SDL_SCANCODE_UNKNOWN,               /* 193 */
  23.230 +    SDL_SCANCODE_UNKNOWN,               /* 194 */
  23.231 +    SDL_SCANCODE_UNKNOWN,               /* 195 */
  23.232 +    SDL_SCANCODE_UNKNOWN,               /* 196 */
  23.233 +    SDL_SCANCODE_UNKNOWN,               /* 197 */
  23.234 +    SDL_SCANCODE_UNKNOWN,               /* 198 */
  23.235 +    SDL_SCANCODE_UNKNOWN,               /* 199 */
  23.236 +    SDL_SCANCODE_UNKNOWN,               /* 200 */
  23.237 +    SDL_SCANCODE_UNKNOWN,               /* 201 */
  23.238 +    SDL_SCANCODE_UNKNOWN,               /* 202 */
  23.239 +    SDL_SCANCODE_UNKNOWN,               /* 203 */
  23.240 +    SDL_SCANCODE_UNKNOWN,               /* 204 */
  23.241 +    SDL_SCANCODE_UNKNOWN,               /* 205 */
  23.242 +    SDL_SCANCODE_UNKNOWN,               /* 206 */
  23.243 +    SDL_SCANCODE_UNKNOWN,               /* 207 */
  23.244 +    SDL_SCANCODE_UNKNOWN,               /* 208 */
  23.245 +    SDL_SCANCODE_UNKNOWN,               /* 209 */
  23.246 +    SDL_SCANCODE_UNKNOWN,               /* 210 */
  23.247 +    SDL_SCANCODE_UNKNOWN,               /* 211 */
  23.248 +    SDL_SCANCODE_UNKNOWN,               /* 212 */
  23.249 +    SDL_SCANCODE_UNKNOWN,               /* 213 */
  23.250 +    SDL_SCANCODE_UNKNOWN,               /* 214 */
  23.251 +    SDL_SCANCODE_UNKNOWN,               /* 215 */
  23.252 +    SDL_SCANCODE_UNKNOWN,               /* 216 */
  23.253 +    SDL_SCANCODE_UNKNOWN,               /* 217 */
  23.254 +    SDL_SCANCODE_UNKNOWN,               /* 218 */
  23.255 +    SDL_SCANCODE_LEFTBRACKET,           /* DOM_VK_OPEN_BRACKET 219 */
  23.256 +    SDL_SCANCODE_BACKSLASH,             /* DOM_VK_BACK_SLASH 220 */
  23.257 +    SDL_SCANCODE_RIGHTBRACKET,          /* DOM_VK_CLOSE_BRACKET 221 */
  23.258 +    SDL_SCANCODE_APOSTROPHE,            /* DOM_VK_QUOTE 222 */
  23.259 +    SDL_SCANCODE_UNKNOWN,               /* 223 */
  23.260 +    SDL_SCANCODE_RGUI,                  /* DOM_VK_META 224 */
  23.261 +    SDL_SCANCODE_RALT,                  /* DOM_VK_ALTGR 225 */
  23.262 +    SDL_SCANCODE_UNKNOWN,               /* 226 */
  23.263 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_ICO_HELP 227 */
  23.264 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_ICO_00 228 */
  23.265 +    SDL_SCANCODE_UNKNOWN,               /* 229 */
  23.266 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_ICO_CLEAR 230 */
  23.267 +    SDL_SCANCODE_UNKNOWN,               /* 231 */
  23.268 +    SDL_SCANCODE_UNKNOWN,               /* 232 */
  23.269 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_RESET 233 */
  23.270 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_JUMP 234 */
  23.271 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_PA1 235 */
  23.272 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_PA2 236 */
  23.273 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_PA3 237 */
  23.274 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_WSCTRL 238 */
  23.275 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_CUSEL 239 */
  23.276 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_ATTN 240 */
  23.277 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_FINISH 241 */
  23.278 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_COPY 242 */
  23.279 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_AUTO 243 */
  23.280 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_ENLW 244 */
  23.281 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_BACKTAB 245 */
  23.282 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_ATTN 246 */
  23.283 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_CRSEL 247 */
  23.284 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_EXSEL 248 */
  23.285 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_EREOF 249 */
  23.286 +    SDL_SCANCODE_AUDIOPLAY,             /* DOM_VK_PLAY 250 */
  23.287 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_ZOOM 251 */
  23.288 +    SDL_SCANCODE_UNKNOWN,               /* 252 */
  23.289 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_PA1 253 */
  23.290 +    SDL_SCANCODE_UNKNOWN,               /* DOM_VK_WIN_OEM_CLEAR 254 */
  23.291 +    SDL_SCANCODE_UNKNOWN,               /* 255 */
  23.292 +};
  23.293 +
  23.294 +static Uint8 SDL_NACL_translate_mouse_button(int32_t button) {
  23.295 +    switch (button) {
  23.296 +        case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
  23.297 +            return SDL_BUTTON_LEFT;
  23.298 +        case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
  23.299 +            return SDL_BUTTON_MIDDLE;
  23.300 +        case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
  23.301 +            return SDL_BUTTON_RIGHT;
  23.302 +            
  23.303 +        case PP_INPUTEVENT_MOUSEBUTTON_NONE:
  23.304 +        default:
  23.305 +            return 0;
  23.306 +    }
  23.307 +}
  23.308 +
  23.309 +static SDL_Scancode
  23.310 +SDL_NACL_translate_keycode(int keycode)
  23.311 +{
  23.312 +    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
  23.313 +
  23.314 +    if (keycode < SDL_arraysize(NACL_Keycodes)) {
  23.315 +        scancode = NACL_Keycodes[keycode];
  23.316 +    }
  23.317 +    if (scancode == SDL_SCANCODE_UNKNOWN) {
  23.318 +        SDL_Log("The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> EVDEV KeyCode %d \n", keycode);
  23.319 +    }
  23.320 +    return scancode;
  23.321 +}
  23.322 +
  23.323 +void NACL_PumpEvents(_THIS) {
  23.324 +  PSEvent* ps_event;
  23.325 +  PP_Resource event;  
  23.326 +  PP_InputEvent_Type type;
  23.327 +  PP_InputEvent_Modifier modifiers;
  23.328 +  struct PP_Rect rect;
  23.329 +  struct PP_FloatPoint fp;
  23.330 +  struct PP_Point location;
  23.331 +  struct PP_Var var;
  23.332 +  const char *str;
  23.333 +  char text[64];
  23.334 +  Uint32 str_len;
  23.335 +  SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
  23.336 +  SDL_Mouse *mouse = SDL_GetMouse();
  23.337 +  
  23.338 +  if (driverdata->window) {
  23.339 +    while ((ps_event = PSEventTryAcquire()) != NULL) {
  23.340 +        event = ps_event->as_resource;
  23.341 +        switch(ps_event->type) {
  23.342 +            /* From DidChangeView, contains a view resource */
  23.343 +            case PSE_INSTANCE_DIDCHANGEVIEW:
  23.344 +                driverdata->ppb_view->GetRect(event, &rect);
  23.345 +                NACL_SetScreenResolution(rect.size.width, rect.size.height, SDL_PIXELFORMAT_UNKNOWN);
  23.346 +                // FIXME: Rebuild context? See life.c UpdateContext
  23.347 +                break;
  23.348 +            
  23.349 +            /* From HandleInputEvent, contains an input resource. */
  23.350 +            case PSE_INSTANCE_HANDLEINPUT: 
  23.351 +                type = driverdata->ppb_input_event->GetType(event);
  23.352 +                modifiers = driverdata->ppb_input_event->GetModifiers(event);
  23.353 +                switch(type) {
  23.354 +                    case PP_INPUTEVENT_TYPE_MOUSEDOWN:
  23.355 +                        SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_PRESSED, SDL_NACL_translate_mouse_button(driverdata->ppb_mouse_input_event->GetButton(event)));
  23.356 +                        break;
  23.357 +                    case PP_INPUTEVENT_TYPE_MOUSEUP:
  23.358 +                        SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, SDL_NACL_translate_mouse_button(driverdata->ppb_mouse_input_event->GetButton(event)));
  23.359 +                        break;
  23.360 +                    case PP_INPUTEVENT_TYPE_WHEEL:
  23.361 +                        /* FIXME: GetTicks provides high resolution scroll events */
  23.362 +                        fp = driverdata->ppb_wheel_input_event->GetDelta(event);
  23.363 +                        SDL_SendMouseWheel(mouse->focus, mouse->mouseID, (int) fp.x, (int) fp.y);
  23.364 +                        break;
  23.365 +                        
  23.366 +                    case PP_INPUTEVENT_TYPE_MOUSEENTER:
  23.367 +                    case PP_INPUTEVENT_TYPE_MOUSELEAVE:
  23.368 +                        /* FIXME: Mouse Focus */
  23.369 +                        break;
  23.370 +                        
  23.371 +                        
  23.372 +                    case PP_INPUTEVENT_TYPE_MOUSEMOVE: 
  23.373 +                        location = driverdata->ppb_mouse_input_event->GetPosition(event);
  23.374 +                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, location.x, location.y);
  23.375 +                        break;
  23.376 +                  
  23.377 +                    case PP_INPUTEVENT_TYPE_TOUCHSTART:
  23.378 +                    case PP_INPUTEVENT_TYPE_TOUCHMOVE:
  23.379 +                    case PP_INPUTEVENT_TYPE_TOUCHEND:
  23.380 +                    case PP_INPUTEVENT_TYPE_TOUCHCANCEL:
  23.381 +                        /* FIXME: Touch events */
  23.382 +                        break;
  23.383 +                      
  23.384 +                    case PP_INPUTEVENT_TYPE_KEYDOWN:
  23.385 +                        SDL_SendKeyboardKey(SDL_PRESSED, SDL_NACL_translate_keycode(driverdata->ppb_keyboard_input_event->GetKeyCode(event)));
  23.386 +                        break;
  23.387 +                        
  23.388 +                    case PP_INPUTEVENT_TYPE_KEYUP:
  23.389 +                        SDL_SendKeyboardKey(SDL_RELEASED, SDL_NACL_translate_keycode(driverdata->ppb_keyboard_input_event->GetKeyCode(event)));
  23.390 +                        break;
  23.391 +                        
  23.392 +                    case PP_INPUTEVENT_TYPE_CHAR:
  23.393 +                        var = driverdata->ppb_keyboard_input_event->GetCharacterText(event);
  23.394 +                        str = driverdata->ppb_var->VarToUtf8(var, &str_len);
  23.395 +                        /* str is not null terminated! */
  23.396 +                        if ( str_len >= SDL_arraysize(text) ) {
  23.397 +                            str_len = SDL_arraysize(text) - 1;
  23.398 +                        }
  23.399 +                        SDL_strlcpy(text, str, str_len );
  23.400 +                        text[str_len] = '\0';
  23.401 +                        
  23.402 +                        SDL_SendKeyboardText(text);
  23.403 +                        /* FIXME: Do we have to handle ref counting? driverdata->ppb_var->Release(var);*/
  23.404 +                        break;
  23.405 +                        
  23.406 +                    default:
  23.407 +                        break;
  23.408 +                }
  23.409 +                break;
  23.410 +                
  23.411 +        
  23.412 +            /* From HandleMessage, contains a PP_Var. */
  23.413 +            case PSE_INSTANCE_HANDLEMESSAGE:
  23.414 +                break;
  23.415 +
  23.416 +            /* From DidChangeFocus, contains a PP_Bool with the current focus state. */
  23.417 +            case PSE_INSTANCE_DIDCHANGEFOCUS:
  23.418 +                break;
  23.419 +
  23.420 +            /* When the 3D context is lost, no resource. */
  23.421 +            case PSE_GRAPHICS3D_GRAPHICS3DCONTEXTLOST:
  23.422 +                break;
  23.423 +                
  23.424 +            /* When the mouse lock is lost. */
  23.425 +            case PSE_MOUSELOCK_MOUSELOCKLOST:
  23.426 +                break;
  23.427 +
  23.428 +            default:
  23.429 +                break;
  23.430 +        }
  23.431 +        
  23.432 +        PSEventRelease(ps_event);
  23.433 +    }
  23.434 +  }
  23.435 +}
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/src/video/nacl/SDL_naclevents_c.h	Fri Jun 06 15:45:59 2014 -0300
    24.3 @@ -0,0 +1,30 @@
    24.4 +/*
    24.5 +  Simple DirectMedia Layer
    24.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    24.7 +
    24.8 +  This software is provided 'as-is', without any express or implied
    24.9 +  warranty.  In no event will the authors be held liable for any damages
   24.10 +  arising from the use of this software.
   24.11 +
   24.12 +  Permission is granted to anyone to use this software for any purpose,
   24.13 +  including commercial applications, and to alter it and redistribute it
   24.14 +  freely, subject to the following restrictions:
   24.15 +
   24.16 +  1. The origin of this software must not be misrepresented; you must not
   24.17 +     claim that you wrote the original software. If you use this software
   24.18 +     in a product, an acknowledgment in the product documentation would be
   24.19 +     appreciated but is not required.
   24.20 +  2. Altered source versions must be plainly marked as such, and must not be
   24.21 +     misrepresented as being the original software.
   24.22 +  3. This notice may not be removed or altered from any source distribution.
   24.23 +*/
   24.24 +#include "../../SDL_internal.h"
   24.25 +
   24.26 +#ifndef _SDL_naclevents_c_h
   24.27 +#define _SDL_naclevents_c_h
   24.28 +
   24.29 +#include "SDL_naclvideo.h"
   24.30 +
   24.31 +extern void NACL_PumpEvents(_THIS);
   24.32 +
   24.33 +#endif /* _SDL_naclevents_c_h */
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/src/video/nacl/SDL_naclglue.c	Fri Jun 06 15:45:59 2014 -0300
    25.3 @@ -0,0 +1,24 @@
    25.4 +/*
    25.5 +  Simple DirectMedia Layer
    25.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    25.7 +
    25.8 +  This software is provided 'as-is', without any express or implied
    25.9 +  warranty.  In no event will the authors be held liable for any damages
   25.10 +  arising from the use of this software.
   25.11 +
   25.12 +  Permission is granted to anyone to use this software for any purpose,
   25.13 +  including commercial applications, and to alter it and redistribute it
   25.14 +  freely, subject to the following restrictions:
   25.15 +
   25.16 +  1. The origin of this software must not be misrepresented; you must not
   25.17 +     claim that you wrote the original software. If you use this software
   25.18 +     in a product, an acknowledgment in the product documentation would be
   25.19 +     appreciated but is not required.
   25.20 +  2. Altered source versions must be plainly marked as such, and must not be
   25.21 +     misrepresented as being the original software.
   25.22 +  3. This notice may not be removed or altered from any source distribution.
   25.23 +*/
   25.24 +#include "../../SDL_internal.h"
   25.25 +
   25.26 +#if SDL_VIDEO_DRIVER_NACL
   25.27 +#endif /* SDL_VIDEO_DRIVER_NACL */
   25.28 \ No newline at end of file
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/src/video/nacl/SDL_naclopengles.c	Fri Jun 06 15:45:59 2014 -0300
    26.3 @@ -0,0 +1,171 @@
    26.4 +/*
    26.5 +  Simple DirectMedia Layer
    26.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    26.7 +
    26.8 +  This software is provided 'as-is', without any express or implied
    26.9 +  warranty.  In no event will the authors be held liable for any damages
   26.10 +  arising from the use of this software.
   26.11 +
   26.12 +  Permission is granted to anyone to use this software for any purpose,
   26.13 +  including commercial applications, and to alter it and redistribute it
   26.14 +  freely, subject to the following restrictions:
   26.15 +
   26.16 +  1. The origin of this software must not be misrepresented; you must not
   26.17 +     claim that you wrote the original software. If you use this software
   26.18 +     in a product, an acknowledgment in the product documentation would be
   26.19 +     appreciated but is not required.
   26.20 +  2. Altered source versions must be plainly marked as such, and must not be
   26.21 +     misrepresented as being the original software.
   26.22 +  3. This notice may not be removed or altered from any source distribution.
   26.23 +*/
   26.24 +#include "../../SDL_internal.h"
   26.25 +
   26.26 +#if SDL_VIDEO_DRIVER_NACL
   26.27 +
   26.28 +/* NaCl SDL video GLES 2 driver implementation */
   26.29 +
   26.30 +#include "SDL_video.h"
   26.31 +#include "SDL_naclvideo.h"
   26.32 +
   26.33 +#if SDL_LOADSO_DLOPEN
   26.34 +#include "dlfcn.h"
   26.35 +#endif
   26.36 +
   26.37 +#include "ppapi/gles2/gl2ext_ppapi.h"
   26.38 +#include "ppapi_simple/ps.h"
   26.39 +
   26.40 +/* GL functions */
   26.41 +int
   26.42 +NACL_GLES_LoadLibrary(_THIS, const char *path)
   26.43 +{
   26.44 +    /* FIXME: Support dynamic linking when PNACL supports it */
   26.45 +    return glInitializePPAPI(PSGetInterface) == 0;
   26.46 +}
   26.47 +
   26.48 +void *
   26.49 +NACL_GLES_GetProcAddress(_THIS, const char *proc)
   26.50 +{
   26.51 +#if SDL_LOADSO_DLOPEN
   26.52 +    return dlsym( 0 /* RTLD_DEFAULT */, proc);
   26.53 +#else
   26.54 +    return NULL;
   26.55 +#endif
   26.56 +}
   26.57 +
   26.58 +void
   26.59 +NACL_GLES_UnloadLibrary(_THIS)
   26.60 +{
   26.61 +    /* FIXME: Support dynamic linking when PNACL supports it */
   26.62 +    glTerminatePPAPI();
   26.63 +}
   26.64 +
   26.65 +int
   26.66 +NACL_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext sdl_context)
   26.67 +{
   26.68 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
   26.69 +    /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
   26.70 +    driverdata->ppb_instance->BindGraphics(driverdata->instance, (PP_Resource) sdl_context);
   26.71 +    glSetCurrentContextPPAPI((PP_Resource) sdl_context);
   26.72 +    return 0;
   26.73 +}
   26.74 +
   26.75 +SDL_GLContext
   26.76 +NACL_GLES_CreateContext(_THIS, SDL_Window * window)
   26.77 +{
   26.78 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
   26.79 +    PP_Resource context, share_context = 0;
   26.80 +    /* 64 seems nice. */
   26.81 +    Sint32 attribs[64];
   26.82 +    int i = 0;
   26.83 +    
   26.84 +    if (_this->gl_config.share_with_current_context) {
   26.85 +        share_context = (PP_Resource) SDL_GL_GetCurrentContext();
   26.86 +    }
   26.87 +
   26.88 +    /* FIXME: Some ATTRIBS from PP_Graphics3DAttrib are not set here */
   26.89 +    
   26.90 +    attribs[i++] = PP_GRAPHICS3DATTRIB_WIDTH;
   26.91 +    attribs[i++] = window->w;
   26.92 +    attribs[i++] = PP_GRAPHICS3DATTRIB_HEIGHT;
   26.93 +    attribs[i++] = window->h;
   26.94 +    attribs[i++] = PP_GRAPHICS3DATTRIB_RED_SIZE;
   26.95 +    attribs[i++] = _this->gl_config.red_size;
   26.96 +    attribs[i++] = PP_GRAPHICS3DATTRIB_GREEN_SIZE;
   26.97 +    attribs[i++] = _this->gl_config.green_size;
   26.98 +    attribs[i++] = PP_GRAPHICS3DATTRIB_BLUE_SIZE;
   26.99 +    attribs[i++] = _this->gl_config.blue_size;
  26.100 +    
  26.101 +    if (_this->gl_config.alpha_size) {
  26.102 +        attribs[i++] = PP_GRAPHICS3DATTRIB_ALPHA_SIZE;
  26.103 +        attribs[i++] = _this->gl_config.alpha_size;
  26.104 +    }
  26.105 +    
  26.106 +    /*if (_this->gl_config.buffer_size) {
  26.107 +        attribs[i++] = EGL_BUFFER_SIZE;
  26.108 +        attribs[i++] = _this->gl_config.buffer_size;
  26.109 +    }*/
  26.110 +    
  26.111 +    attribs[i++] = PP_GRAPHICS3DATTRIB_DEPTH_SIZE;
  26.112 +    attribs[i++] = _this->gl_config.depth_size;
  26.113 +    
  26.114 +    if (_this->gl_config.stencil_size) {
  26.115 +        attribs[i++] = PP_GRAPHICS3DATTRIB_STENCIL_SIZE;
  26.116 +        attribs[i++] = _this->gl_config.stencil_size;
  26.117 +    }
  26.118 +    
  26.119 +    if (_this->gl_config.multisamplebuffers) {
  26.120 +        attribs[i++] = PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS;
  26.121 +        attribs[i++] = _this->gl_config.multisamplebuffers;
  26.122 +    }
  26.123 +    
  26.124 +    if (_this->gl_config.multisamplesamples) {
  26.125 +        attribs[i++] = PP_GRAPHICS3DATTRIB_SAMPLES;
  26.126 +        attribs[i++] = _this->gl_config.multisamplesamples;
  26.127 +    }
  26.128 +       
  26.129 +    attribs[i++] = PP_GRAPHICS3DATTRIB_NONE;
  26.130 +    
  26.131 +    context = driverdata->ppb_graphics->Create(driverdata->instance, share_context, attribs);
  26.132 +
  26.133 +    if (context) {
  26.134 +        /* We need to make the context current, otherwise nothing works */
  26.135 +        SDL_GL_MakeCurrent(window, (SDL_GLContext) context);
  26.136 +    }
  26.137 +    
  26.138 +    return (SDL_GLContext) context;
  26.139 +}
  26.140 +
  26.141 +
  26.142 +
  26.143 +int
  26.144 +NACL_GLES_SetSwapInterval(_THIS, int interval)
  26.145 +{
  26.146 +    /* STUB */
  26.147 +    return 0;
  26.148 +}
  26.149 +
  26.150 +int
  26.151 +NACL_GLES_GetSwapInterval(_THIS)
  26.152 +{
  26.153 +    /* STUB */
  26.154 +    return 0;
  26.155 +}
  26.156 +
  26.157 +void
  26.158 +NACL_GLES_SwapWindow(_THIS, SDL_Window * window)
  26.159 +{
  26.160 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
  26.161 +    struct PP_CompletionCallback callback = { NULL, 0, PP_COMPLETIONCALLBACK_FLAG_NONE };
  26.162 +    driverdata->ppb_graphics->SwapBuffers((PP_Resource) SDL_GL_GetCurrentContext(), callback );
  26.163 +}
  26.164 +
  26.165 +void
  26.166 +NACL_GLES_DeleteContext(_THIS, SDL_GLContext context)
  26.167 +{
  26.168 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
  26.169 +    driverdata->ppb_core->ReleaseResource((PP_Resource) context);
  26.170 +}
  26.171 +
  26.172 +#endif /* SDL_VIDEO_DRIVER_NACL */
  26.173 +
  26.174 +/* vi: set ts=4 sw=4 expandtab: */
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/src/video/nacl/SDL_naclopengles.h	Fri Jun 06 15:45:59 2014 -0300
    27.3 @@ -0,0 +1,38 @@
    27.4 +/*
    27.5 +  Simple DirectMedia Layer
    27.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    27.7 +
    27.8 +  This software is provided 'as-is', without any express or implied
    27.9 +  warranty.  In no event will the authors be held liable for any damages
   27.10 +  arising from the use of this software.
   27.11 +
   27.12 +  Permission is granted to anyone to use this software for any purpose,
   27.13 +  including commercial applications, and to alter it and redistribute it
   27.14 +  freely, subject to the following restrictions:
   27.15 +
   27.16 +  1. The origin of this software must not be misrepresented; you must not
   27.17 +     claim that you wrote the original software. If you use this software
   27.18 +     in a product, an acknowledgment in the product documentation would be
   27.19 +     appreciated but is not required.
   27.20 +  2. Altered source versions must be plainly marked as such, and must not be
   27.21 +     misrepresented as being the original software.
   27.22 +  3. This notice may not be removed or altered from any source distribution.
   27.23 +*/
   27.24 +#include "../../SDL_internal.h"
   27.25 +
   27.26 +#ifndef _SDL_naclgl_h
   27.27 +#define _SDL_naclgl_h
   27.28 +
   27.29 +extern int NACL_GLES_LoadLibrary(_THIS, const char *path);
   27.30 +extern void *NACL_GLES_GetProcAddress(_THIS, const char *proc);
   27.31 +extern void NACL_GLES_UnloadLibrary(_THIS);
   27.32 +extern SDL_GLContext NACL_GLES_CreateContext(_THIS, SDL_Window * window);
   27.33 +extern int NACL_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
   27.34 +extern int NACL_GLES_SetSwapInterval(_THIS, int interval);
   27.35 +extern int NACL_GLES_GetSwapInterval(_THIS);
   27.36 +extern void NACL_GLES_SwapWindow(_THIS, SDL_Window * window);
   27.37 +extern void NACL_GLES_DeleteContext(_THIS, SDL_GLContext context);
   27.38 +
   27.39 +#endif /* _SDL_naclgl_h */
   27.40 +
   27.41 +/* vi: set ts=4 sw=4 expandtab: */
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/src/video/nacl/SDL_naclvideo.c	Fri Jun 06 15:45:59 2014 -0300
    28.3 @@ -0,0 +1,183 @@
    28.4 +/*
    28.5 +  Simple DirectMedia Layer
    28.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    28.7 +
    28.8 +  This software is provided 'as-is', without any express or implied
    28.9 +  warranty.  In no event will the authors be held liable for any damages
   28.10 +  arising from the use of this software.
   28.11 +
   28.12 +  Permission is granted to anyone to use this software for any purpose,
   28.13 +  including commercial applications, and to alter it and redistribute it
   28.14 +  freely, subject to the following restrictions:
   28.15 +
   28.16 +  1. The origin of this software must not be misrepresented; you must not
   28.17 +     claim that you wrote the original software. If you use this software
   28.18 +     in a product, an acknowledgment in the product documentation would be
   28.19 +     appreciated but is not required.
   28.20 +  2. Altered source versions must be plainly marked as such, and must not be
   28.21 +     misrepresented as being the original software.
   28.22 +  3. This notice may not be removed or altered from any source distribution.
   28.23 +*/
   28.24 +#include "../../SDL_internal.h"
   28.25 +
   28.26 +#if SDL_VIDEO_DRIVER_NACL
   28.27 +
   28.28 +#include "ppapi/c/pp_errors.h"
   28.29 +#include "ppapi/c/pp_instance.h"
   28.30 +#include "ppapi_simple/ps.h"
   28.31 +#include "ppapi_simple/ps_interface.h"
   28.32 +#include "ppapi_simple/ps_event.h"
   28.33 +#include "nacl_io/nacl_io.h"
   28.34 +
   28.35 +#include "SDL_naclvideo.h"
   28.36 +#include "SDL_naclwindow.h"
   28.37 +#include "SDL_naclevents_c.h"
   28.38 +#include "SDL_naclopengles.h"   
   28.39 +#include "SDL_video.h"
   28.40 +#include "../SDL_sysvideo.h"
   28.41 +#include "../../events/SDL_events_c.h"
   28.42 +
   28.43 +#define NACLVID_DRIVER_NAME "nacl"
   28.44 +
   28.45 +/* Static init required because NACL_SetScreenResolution 
   28.46 + * may appear even before SDL starts and we want to remember
   28.47 + * the window width and height
   28.48 + */
   28.49 +static SDL_VideoData nacl = {0};
   28.50 +
   28.51 +void
   28.52 +NACL_SetScreenResolution(int width, int height, Uint32 format)
   28.53 +{
   28.54 +    PP_Resource context;
   28.55 +    
   28.56 +    nacl.w = width;
   28.57 +    nacl.h = height;   
   28.58 +    nacl.format = format;
   28.59 +    
   28.60 +    if (nacl.window) {
   28.61 +        nacl.window->w = width;
   28.62 +        nacl.window->h = height;
   28.63 +        SDL_SendWindowEvent(nacl.window, SDL_WINDOWEVENT_RESIZED, width, height);
   28.64 +    }
   28.65 +    
   28.66 +    /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
   28.67 +    context = (PP_Resource) SDL_GL_GetCurrentContext();
   28.68 +    if (context) {
   28.69 +        PSInterfaceGraphics3D()->ResizeBuffers(context, width, height);
   28.70 +    }
   28.71 +
   28.72 +}
   28.73 +
   28.74 +
   28.75 +
   28.76 +/* Initialization/Query functions */
   28.77 +static int NACL_VideoInit(_THIS);
   28.78 +static void NACL_VideoQuit(_THIS);
   28.79 +
   28.80 +static int NACL_Available(void) {
   28.81 +    return PSGetInstanceId() != 0;
   28.82 +}
   28.83 +
   28.84 +static void NACL_DeleteDevice(SDL_VideoDevice *device) {
   28.85 +    SDL_VideoData *driverdata = (SDL_VideoData*) device->driverdata;
   28.86 +    driverdata->ppb_core->ReleaseResource((PP_Resource) driverdata->ppb_message_loop);
   28.87 +    SDL_free(device->driverdata);
   28.88 +    SDL_free(device);
   28.89 +}
   28.90 +
   28.91 +static int
   28.92 +NACL_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   28.93 +{
   28.94 +    return 0;
   28.95 +}
   28.96 +
   28.97 +static SDL_VideoDevice *NACL_CreateDevice(int devindex) {
   28.98 +    SDL_VideoDevice *device;
   28.99 +    
  28.100 +    /* Initialize all variables that we clean on shutdown */
  28.101 +    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
  28.102 +    if (!device) {
  28.103 +        SDL_OutOfMemory();
  28.104 +        return NULL;
  28.105 +    }
  28.106 +    device->driverdata = &nacl;
  28.107 +  
  28.108 +    /* Set the function pointers */
  28.109 +    device->VideoInit = NACL_VideoInit;
  28.110 +    device->VideoQuit = NACL_VideoQuit;
  28.111 +    device->PumpEvents = NACL_PumpEvents;
  28.112 +    
  28.113 +    device->CreateWindow = NACL_CreateWindow;
  28.114 +    device->SetWindowTitle = NACL_SetWindowTitle;
  28.115 +    device->DestroyWindow = NACL_DestroyWindow;
  28.116 +    
  28.117 +    device->SetDisplayMode = NACL_SetDisplayMode;
  28.118 +    
  28.119 +    device->free = NACL_DeleteDevice;
  28.120 +    
  28.121 +    /* GL pointers */
  28.122 +    device->GL_LoadLibrary = NACL_GLES_LoadLibrary;
  28.123 +    device->GL_GetProcAddress = NACL_GLES_GetProcAddress;
  28.124 +    device->GL_UnloadLibrary = NACL_GLES_UnloadLibrary;
  28.125 +    device->GL_CreateContext = NACL_GLES_CreateContext;
  28.126 +    device->GL_MakeCurrent = NACL_GLES_MakeCurrent;
  28.127 +    device->GL_SetSwapInterval = NACL_GLES_SetSwapInterval;
  28.128 +    device->GL_GetSwapInterval = NACL_GLES_GetSwapInterval;
  28.129 +    device->GL_SwapWindow = NACL_GLES_SwapWindow;
  28.130 +    device->GL_DeleteContext = NACL_GLES_DeleteContext;
  28.131 +    
  28.132 +    
  28.133 +    return device;
  28.134 +}
  28.135 +
  28.136 +VideoBootStrap NACL_bootstrap = {
  28.137 +    NACLVID_DRIVER_NAME, "SDL Native Client Video Driver",
  28.138 +    NACL_Available, NACL_CreateDevice
  28.139 +};
  28.140 +
  28.141 +int NACL_VideoInit(_THIS) {
  28.142 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
  28.143 +    SDL_DisplayMode mode;
  28.144 +
  28.145 +    mode.format = driverdata->format;
  28.146 +    mode.w = driverdata->w;
  28.147 +    mode.h = driverdata->h;
  28.148 +    mode.refresh_rate = 0;
  28.149 +    mode.driverdata = NULL;
  28.150 +    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
  28.151 +        return -1;
  28.152 +    }
  28.153 +    
  28.154 +    SDL_zero(mode);
  28.155 +    SDL_AddDisplayMode(&_this->displays[0], &mode);
  28.156 +    
  28.157 +    PSInterfaceInit();
  28.158 +    driverdata->instance = PSGetInstanceId();
  28.159 +    driverdata->ppb_graphics = PSInterfaceGraphics3D();
  28.160 +    driverdata->ppb_message_loop = PSInterfaceMessageLoop();
  28.161 +    driverdata->ppb_core = PSInterfaceCore();
  28.162 +    driverdata->ppb_fullscreen = PSInterfaceFullscreen();
  28.163 +    driverdata->ppb_instance = PSInterfaceInstance();
  28.164 +    driverdata->ppb_image_data = PSInterfaceImageData();
  28.165 +    driverdata->ppb_view = PSInterfaceView();
  28.166 +    driverdata->ppb_var = PSInterfaceVar();
  28.167 +    driverdata->ppb_input_event = (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE);
  28.168 +    driverdata->ppb_keyboard_input_event = (PPB_KeyboardInputEvent*) PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE);
  28.169 +    driverdata->ppb_mouse_input_event = (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE);
  28.170 +    driverdata->ppb_wheel_input_event = (PPB_WheelInputEvent*) PSGetInterface(PPB_WHEEL_INPUT_EVENT_INTERFACE);
  28.171 +    driverdata->ppb_touch_input_event = (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE);
  28.172 +    
  28.173 +    
  28.174 +    driverdata->message_loop = driverdata->ppb_message_loop->Create(driverdata->instance);
  28.175 +    
  28.176 +    PSEventSetFilter(PSE_ALL);
  28.177 +    
  28.178 +    /* We're done! */
  28.179 +    return 0;
  28.180 +}
  28.181 +
  28.182 +void NACL_VideoQuit(_THIS) {
  28.183 +}
  28.184 +
  28.185 +#endif /* SDL_VIDEO_DRIVER_NACL */
  28.186 +/* vi: set ts=4 sw=4 expandtab: */
  28.187 \ No newline at end of file
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/src/video/nacl/SDL_naclvideo.h	Fri Jun 06 15:45:59 2014 -0300
    29.3 @@ -0,0 +1,67 @@
    29.4 +/*
    29.5 +  Simple DirectMedia Layer
    29.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    29.7 +
    29.8 +  This software is provided 'as-is', without any express or implied
    29.9 +  warranty.  In no event will the authors be held liable for any damages
   29.10 +  arising from the use of this software.
   29.11 +
   29.12 +  Permission is granted to anyone to use this software for any purpose,
   29.13 +  including commercial applications, and to alter it and redistribute it
   29.14 +  freely, subject to the following restrictions:
   29.15 +
   29.16 +  1. The origin of this software must not be misrepresented; you must not
   29.17 +     claim that you wrote the original software. If you use this software
   29.18 +     in a product, an acknowledgment in the product documentation would be
   29.19 +     appreciated but is not required.
   29.20 +  2. Altered source versions must be plainly marked as such, and must not be
   29.21 +     misrepresented as being the original software.
   29.22 +  3. This notice may not be removed or altered from any source distribution.
   29.23 +*/
   29.24 +#include "../../SDL_internal.h"
   29.25 +
   29.26 +#ifndef _SDL_naclvideo_h
   29.27 +#define _SDL_naclvideo_h
   29.28 +
   29.29 +#include "../SDL_sysvideo.h"
   29.30 +#include "ppapi_simple/ps_interface.h"
   29.31 +#include "ppapi/c/pp_input_event.h"
   29.32 +
   29.33 +
   29.34 +/* Hidden "this" pointer for the video functions */
   29.35 +#define _THIS  SDL_VideoDevice *_this
   29.36 +
   29.37 +
   29.38 +/* Private display data */
   29.39 +
   29.40 +typedef struct SDL_VideoData {
   29.41 +  Uint32 format;
   29.42 +  int w, h;
   29.43 +  SDL_Window *window;
   29.44 +
   29.45 +  const PPB_Graphics3D *ppb_graphics;
   29.46 +  const PPB_MessageLoop *ppb_message_loop;
   29.47 +  const PPB_Core *ppb_core;
   29.48 +  const PPB_Fullscreen *ppb_fullscreen;
   29.49 +  const PPB_Instance *ppb_instance;
   29.50 +  const PPB_ImageData *ppb_image_data;
   29.51 +  const PPB_View *ppb_view;
   29.52 +  const PPB_Var *ppb_var;
   29.53 +  const PPB_InputEvent *ppb_input_event;
   29.54 +  const PPB_KeyboardInputEvent *ppb_keyboard_input_event;
   29.55 +  const PPB_MouseInputEvent *ppb_mouse_input_event;
   29.56 +  const PPB_WheelInputEvent *ppb_wheel_input_event;
   29.57 +  const PPB_TouchInputEvent *ppb_touch_input_event;
   29.58 +      
   29.59 +  PP_Resource message_loop;
   29.60 +  PP_Instance instance;
   29.61 +  
   29.62 +  /* FIXME: Check threading issues...otherwise use a hardcoded _this->context across all threads */
   29.63 +  /* PP_Resource context; */
   29.64 +
   29.65 +} SDL_VideoData;
   29.66 +
   29.67 +extern void NACL_SetScreenResolution(int width, int height, Uint32 format);
   29.68 +
   29.69 +
   29.70 +#endif /* _SDL_naclvideo_h */
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/src/video/nacl/SDL_naclwindow.c	Fri Jun 06 15:45:59 2014 -0300
    30.3 @@ -0,0 +1,74 @@
    30.4 +/*
    30.5 +  Simple DirectMedia Layer
    30.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    30.7 +
    30.8 +  This software is provided 'as-is', without any express or implied
    30.9 +  warranty.  In no event will the authors be held liable for any damages
   30.10 +  arising from the use of this software.
   30.11 +
   30.12 +  Permission is granted to anyone to use this software for any purpose,
   30.13 +  including commercial applications, and to alter it and redistribute it
   30.14 +  freely, subject to the following restrictions:
   30.15 +
   30.16 +  1. The origin of this software must not be misrepresented; you must not
   30.17 +     claim that you wrote the original software. If you use this software
   30.18 +     in a product, an acknowledgment in the product documentation would be
   30.19 +     appreciated but is not required.
   30.20 +  2. Altered source versions must be plainly marked as such, and must not be
   30.21 +     misrepresented as being the original software.
   30.22 +  3. This notice may not be removed or altered from any source distribution.
   30.23 +*/
   30.24 +#include "../../SDL_internal.h"
   30.25 +
   30.26 +#if SDL_VIDEO_DRIVER_NACL
   30.27 +
   30.28 +#include "../SDL_sysvideo.h"
   30.29 +
   30.30 +#include "SDL_naclvideo.h"
   30.31 +#include "SDL_naclwindow.h"
   30.32 +
   30.33 +int
   30.34 +NACL_CreateWindow(_THIS, SDL_Window * window)
   30.35 +{
   30.36 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
   30.37 +    
   30.38 +    if (driverdata->window) {
   30.39 +        SDL_SetError("NaCl only supports one window");
   30.40 +        return -1;
   30.41 +    }
   30.42 +    driverdata->window = window;
   30.43 +
   30.44 +    /* Adjust the window data to match the screen */
   30.45 +    window->x = 0;
   30.46 +    window->y = 0;
   30.47 +    window->w = driverdata->w;
   30.48 +    window->h = driverdata->h;
   30.49 +
   30.50 +    window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
   30.51 +    window->flags |= SDL_WINDOW_FULLSCREEN;     /* window is always fullscreen */
   30.52 +    window->flags &= ~SDL_WINDOW_HIDDEN;
   30.53 +    window->flags |= SDL_WINDOW_SHOWN;          /* only one window on NaCl */
   30.54 +    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
   30.55 +    window->flags |= SDL_WINDOW_OPENGL;
   30.56 +  
   30.57 +    return 0;
   30.58 +}
   30.59 +
   30.60 +void
   30.61 +NACL_SetWindowTitle(_THIS, SDL_Window * window)
   30.62 +{
   30.63 +    /* TODO */
   30.64 +}
   30.65 +
   30.66 +void
   30.67 +NACL_DestroyWindow(_THIS, SDL_Window * window)
   30.68 +{
   30.69 +    SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
   30.70 +    if (window == driverdata->window) {
   30.71 +        driverdata->window = NULL;
   30.72 +    }
   30.73 +}
   30.74 +
   30.75 +#endif /* SDL_VIDEO_DRIVER_NACL */
   30.76 +
   30.77 +/* vi: set ts=4 sw=4 expandtab: */
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/src/video/nacl/SDL_naclwindow.h	Fri Jun 06 15:45:59 2014 -0300
    31.3 @@ -0,0 +1,32 @@
    31.4 +/*
    31.5 +  Simple DirectMedia Layer
    31.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
    31.7 +
    31.8 +  This software is provided 'as-is', without any express or implied
    31.9 +  warranty.  In no event will the authors be held liable for any damages
   31.10 +  arising from the use of this software.
   31.11 +
   31.12 +  Permission is granted to anyone to use this software for any purpose,
   31.13 +  including commercial applications, and to alter it and redistribute it
   31.14 +  freely, subject to the following restrictions:
   31.15 +
   31.16 +  1. The origin of this software must not be misrepresented; you must not
   31.17 +     claim that you wrote the original software. If you use this software
   31.18 +     in a product, an acknowledgment in the product documentation would be
   31.19 +     appreciated but is not required.
   31.20 +  2. Altered source versions must be plainly marked as such, and must not be
   31.21 +     misrepresented as being the original software.
   31.22 +  3. This notice may not be removed or altered from any source distribution.
   31.23 +*/
   31.24 +#include "../../SDL_internal.h"
   31.25 +
   31.26 +#ifndef _SDL_naclwindow_h
   31.27 +#define _SDL_naclwindow_h
   31.28 +
   31.29 +extern int NACL_CreateWindow(_THIS, SDL_Window * window);
   31.30 +extern void NACL_SetWindowTitle(_THIS, SDL_Window * window);
   31.31 +extern void NACL_DestroyWindow(_THIS, SDL_Window * window);
   31.32 +
   31.33 +#endif /* _SDL_naclwindow_h */
   31.34 +
   31.35 +/* vi: set ts=4 sw=4 expandtab: */
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/test/nacl/background.js	Fri Jun 06 15:45:59 2014 -0300
    32.3 @@ -0,0 +1,40 @@
    32.4 +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
    32.5 +// Use of this source code is governed by a BSD-style license that can be
    32.6 +// found in the LICENSE file.
    32.7 +
    32.8 +function makeURL(toolchain, config) {
    32.9 +  return 'index.html?tc=' + toolchain + '&config=' + config;
   32.10 +}
   32.11 +
   32.12 +function createWindow(url) {
   32.13 +  console.log('loading ' + url);
   32.14 +  chrome.app.window.create(url, {
   32.15 +    width: 1024,
   32.16 +    height: 800,
   32.17 +    frame: 'none'
   32.18 +  });
   32.19 +}
   32.20 +
   32.21 +function onLaunched(launchData) {
   32.22 +  // Send and XHR to get the URL to load from a configuration file.
   32.23 +  // Normally you won't need to do this; just call:
   32.24 +  //
   32.25 +  // chrome.app.window.create('<your url>', {...});
   32.26 +  //
   32.27 +  // In the SDK we want to be able to load different URLs (for different
   32.28 +  // toolchain/config combinations) from the commandline, so we to read
   32.29 +  // this information from the file "run_package_config".
   32.30 +  var xhr = new XMLHttpRequest();
   32.31 +  xhr.open('GET', 'run_package_config', true);
   32.32 +  xhr.onload = function() {
   32.33 +    var toolchain_config = this.responseText.split(' ');
   32.34 +    createWindow(makeURL.apply(null, toolchain_config));
   32.35 +  };
   32.36 +  xhr.onerror = function() {
   32.37 +    // Can't find the config file, just load the default.
   32.38 +    createWindow('index.html');
   32.39 +  };
   32.40 +  xhr.send();
   32.41 +}
   32.42 +
   32.43 +chrome.app.runtime.onLaunched.addListener(onLaunched);
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/test/nacl/common.js	Fri Jun 06 15:45:59 2014 -0300
    33.3 @@ -0,0 +1,469 @@
    33.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
    33.5 +// Use of this source code is governed by a BSD-style license that can be
    33.6 +// found in the LICENSE file.
    33.7 +
    33.8 +// Set to true when the Document is loaded IFF "test=true" is in the query
    33.9 +// string.
   33.10 +var isTest = false;
   33.11 +
   33.12 +// Set to true when loading a "Release" NaCl module, false when loading a
   33.13 +// "Debug" NaCl module.
   33.14 +var isRelease = true;
   33.15 +
   33.16 +// Javascript module pattern:
   33.17 +//   see http://en.wikipedia.org/wiki/Unobtrusive_JavaScript#Namespaces
   33.18 +// In essence, we define an anonymous function which is immediately called and
   33.19 +// returns a new object. The new object contains only the exported definitions;
   33.20 +// all other definitions in the anonymous function are inaccessible to external
   33.21 +// code.
   33.22 +var common = (function() {
   33.23 +
   33.24 +  function isHostToolchain(tool) {
   33.25 +    return tool == 'win' || tool == 'linux' || tool == 'mac';
   33.26 +  }
   33.27 +
   33.28 +  /**
   33.29 +   * Return the mime type for NaCl plugin.
   33.30 +   *
   33.31 +   * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc.
   33.32 +   * @return {string} The mime-type for the kind of NaCl plugin matching
   33.33 +   * the given toolchain.
   33.34 +   */
   33.35 +  function mimeTypeForTool(tool) {
   33.36 +    // For NaCl modules use application/x-nacl.
   33.37 +    var mimetype = 'application/x-nacl';
   33.38 +    if (isHostToolchain(tool)) {
   33.39 +      // For non-NaCl PPAPI plugins use the x-ppapi-debug/release
   33.40 +      // mime type.
   33.41 +      if (isRelease)
   33.42 +        mimetype = 'application/x-ppapi-release';
   33.43 +      else
   33.44 +        mimetype = 'application/x-ppapi-debug';
   33.45 +    } else if (tool == 'pnacl' && isRelease) {
   33.46 +      mimetype = 'application/x-pnacl';
   33.47 +    }
   33.48 +    return mimetype;
   33.49 +  }
   33.50 +
   33.51 +  /**
   33.52 +   * Check if the browser supports NaCl plugins.
   33.53 +   *
   33.54 +   * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc.
   33.55 +   * @return {bool} True if the browser supports the type of NaCl plugin
   33.56 +   * produced by the given toolchain.
   33.57 +   */
   33.58 +  function browserSupportsNaCl(tool) {
   33.59 +    // Assume host toolchains always work with the given browser.
   33.60 +    // The below mime-type checking might not work with
   33.61 +    // --register-pepper-plugins.
   33.62 +    if (isHostToolchain(tool)) {
   33.63 +      return true;
   33.64 +    }
   33.65 +    var mimetype = mimeTypeForTool(tool);
   33.66 +    return navigator.mimeTypes[mimetype] !== undefined;
   33.67 +  }
   33.68 +
   33.69 +  /**
   33.70 +   * Inject a script into the DOM, and call a callback when it is loaded.
   33.71 +   *
   33.72 +   * @param {string} url The url of the script to load.
   33.73 +   * @param {Function} onload The callback to call when the script is loaded.
   33.74 +   * @param {Function} onerror The callback to call if the script fails to load.
   33.75 +   */
   33.76 +  function injectScript(url, onload, onerror) {
   33.77 +    var scriptEl = document.createElement('script');
   33.78 +    scriptEl.type = 'text/javascript';
   33.79 +    scriptEl.src = url;
   33.80 +    scriptEl.onload = onload;
   33.81 +    if (onerror) {
   33.82 +      scriptEl.addEventListener('error', onerror, false);
   33.83 +    }
   33.84 +    document.head.appendChild(scriptEl);
   33.85 +  }
   33.86 +
   33.87 +  /**
   33.88 +   * Run all tests for this example.
   33.89 +   *
   33.90 +   * @param {Object} moduleEl The module DOM element.
   33.91 +   */
   33.92 +  function runTests(moduleEl) {
   33.93 +    console.log('runTests()');
   33.94 +    common.tester = new Tester();
   33.95 +
   33.96 +    // All NaCl SDK examples are OK if the example exits cleanly; (i.e. the
   33.97 +    // NaCl module returns 0 or calls exit(0)).
   33.98 +    //
   33.99 +    // Without this exception, the browser_tester thinks that the module
  33.100 +    // has crashed.
  33.101 +    common.tester.exitCleanlyIsOK();
  33.102 +
  33.103 +    common.tester.addAsyncTest('loaded', function(test) {
  33.104 +      test.pass();
  33.105 +    });
  33.106 +
  33.107 +    if (typeof window.addTests !== 'undefined') {
  33.108 +      window.addTests();
  33.109 +    }
  33.110 +
  33.111 +    common.tester.waitFor(moduleEl);
  33.112 +    common.tester.run();
  33.113 +  }
  33.114 +
  33.115 +  /**
  33.116 +   * Create the Native Client <embed> element as a child of the DOM element
  33.117 +   * named "listener".
  33.118 +   *
  33.119 +   * @param {string} name The name of the example.
  33.120 +   * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc.
  33.121 +   * @param {string} path Directory name where .nmf file can be found.
  33.122 +   * @param {number} width The width to create the plugin.
  33.123 +   * @param {number} height The height to create the plugin.
  33.124 +   * @param {Object} attrs Dictionary of attributes to set on the module.
  33.125 +   */
  33.126 +  function createNaClModule(name, tool, path, width, height, attrs) {
  33.127 +    var moduleEl = document.createElement('embed');
  33.128 +    moduleEl.setAttribute('name', 'nacl_module');
  33.129 +    moduleEl.setAttribute('id', 'nacl_module');
  33.130 +    moduleEl.setAttribute('width', width);
  33.131 +    moduleEl.setAttribute('height', height);
  33.132 +    moduleEl.setAttribute('path', path);
  33.133 +    moduleEl.setAttribute('src', path + '/' + name + '.nmf');
  33.134 +
  33.135 +    // Add any optional arguments
  33.136 +    if (attrs) {
  33.137 +      for (var key in attrs) {
  33.138 +        moduleEl.setAttribute(key, attrs[key]);
  33.139 +      }
  33.140 +    }
  33.141 +
  33.142 +    var mimetype = mimeTypeForTool(tool);
  33.143 +    moduleEl.setAttribute('type', mimetype);
  33.144 +
  33.145 +    // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load'
  33.146 +    // and a 'message' event listener attached.  This wrapping method is used
  33.147 +    // instead of attaching the event listeners directly to the <EMBED> element
  33.148 +    // to ensure that the listeners are active before the NaCl module 'load'
  33.149 +    // event fires.
  33.150 +    var listenerDiv = document.getElementById('listener');
  33.151 +    listenerDiv.appendChild(moduleEl);
  33.152 +
  33.153 +    // Host plugins don't send a moduleDidLoad message. We'll fake it here.
  33.154 +    var isHost = isHostToolchain(tool);
  33.155 +    if (isHost) {
  33.156 +      window.setTimeout(function() {
  33.157 +        moduleEl.readyState = 1;
  33.158 +        moduleEl.dispatchEvent(new CustomEvent('loadstart'));
  33.159 +        moduleEl.readyState = 4;
  33.160 +        moduleEl.dispatchEvent(new CustomEvent('load'));
  33.161 +        moduleEl.dispatchEvent(new CustomEvent('loadend'));
  33.162 +      }, 100);  // 100 ms
  33.163 +    }
  33.164 +
  33.165 +    // This is code that is only used to test the SDK.
  33.166 +    if (isTest) {
  33.167 +      var loadNaClTest = function() {
  33.168 +        injectScript('nacltest.js', function() {
  33.169 +          runTests(moduleEl);
  33.170 +        });
  33.171 +      };
  33.172 +
  33.173 +      // Try to load test.js for the example. Whether or not it exists, load
  33.174 +      // nacltest.js.
  33.175 +      injectScript('test.js', loadNaClTest, loadNaClTest);
  33.176 +    }
  33.177 +  }
  33.178 +
  33.179 +  /**
  33.180 +   * Add the default "load" and "message" event listeners to the element with
  33.181 +   * id "listener".
  33.182 +   *
  33.183 +   * The "load" event is sent when the module is successfully loaded. The
  33.184 +   * "message" event is sent when the naclModule posts a message using
  33.185 +   * PPB_Messaging.PostMessage() (in C) or pp::Instance().PostMessage() (in
  33.186 +   * C++).
  33.187 +   */
  33.188 +  function attachDefaultListeners() {
  33.189 +    var listenerDiv = document.getElementById('listener');
  33.190 +    listenerDiv.addEventListener('load', moduleDidLoad, true);
  33.191 +    listenerDiv.addEventListener('message', handleMessage, true);
  33.192 +    listenerDiv.addEventListener('error', handleError, true);
  33.193 +    listenerDiv.addEventListener('crash', handleCrash, true);
  33.194 +    if (typeof window.attachListeners !== 'undefined') {
  33.195 +      window.attachListeners();
  33.196 +    }
  33.197 +  }
  33.198 +
  33.199 +  /**
  33.200 +   * Called when the NaCl module fails to load.
  33.201 +   *
  33.202 +   * This event listener is registered in createNaClModule above.
  33.203 +   */
  33.204 +  function handleError(event) {
  33.205 +    // We can't use common.naclModule yet because the module has not been
  33.206 +    // loaded.
  33.207 +    var moduleEl = document.getElementById('nacl_module');
  33.208 +    updateStatus('ERROR [' + moduleEl.lastError + ']');
  33.209 +  }
  33.210 +
  33.211 +  /**
  33.212 +   * Called when the Browser can not communicate with the Module
  33.213 +   *
  33.214 +   * This event listener is registered in attachDefaultListeners above.
  33.215 +   */
  33.216 +  function handleCrash(event) {
  33.217 +    if (common.naclModule.exitStatus == -1) {
  33.218 +      updateStatus('CRASHED');
  33.219 +    } else {
  33.220 +      updateStatus('EXITED [' + common.naclModule.exitStatus + ']');
  33.221 +    }
  33.222 +    if (typeof window.handleCrash !== 'undefined') {
  33.223 +      window.handleCrash(common.naclModule.lastError);
  33.224 +    }
  33.225 +  }
  33.226 +
  33.227 +  /**
  33.228 +   * Called when the NaCl module is loaded.
  33.229 +   *
  33.230 +   * This event listener is registered in attachDefaultListeners above.
  33.231 +   */
  33.232 +  function moduleDidLoad() {
  33.233 +    common.naclModule = document.getElementById('nacl_module');
  33.234 +    updateStatus('RUNNING');
  33.235 +
  33.236 +    if (typeof window.moduleDidLoad !== 'undefined') {
  33.237 +      window.moduleDidLoad();
  33.238 +    }
  33.239 +  }
  33.240 +
  33.241 +  /**
  33.242 +   * Hide the NaCl module's embed element.
  33.243 +   *
  33.244 +   * We don't want to hide by default; if we do, it is harder to determine that
  33.245 +   * a plugin failed to load. Instead, call this function inside the example's
  33.246 +   * "moduleDidLoad" function.
  33.247 +   *
  33.248 +   */
  33.249 +  function hideModule() {
  33.250 +    // Setting common.naclModule.style.display = "None" doesn't work; the
  33.251 +    // module will no longer be able to receive postMessages.
  33.252 +    common.naclModule.style.height = '0';
  33.253 +  }
  33.254 +
  33.255 +  /**
  33.256 +   * Remove the NaCl module from the page.
  33.257 +   */
  33.258 +  function removeModule() {
  33.259 +    common.naclModule.parentNode.removeChild(common.naclModule);
  33.260 +    common.naclModule = null;
  33.261 +  }
  33.262 +
  33.263 +  /**
  33.264 +   * Return true when |s| starts with the string |prefix|.
  33.265 +   *
  33.266 +   * @param {string} s The string to search.
  33.267 +   * @param {string} prefix The prefix to search for in |s|.
  33.268 +   */
  33.269 +  function startsWith(s, prefix) {
  33.270 +    // indexOf would search the entire string, lastIndexOf(p, 0) only checks at
  33.271 +    // the first index. See: http://stackoverflow.com/a/4579228
  33.272 +    return s.lastIndexOf(prefix, 0) === 0;
  33.273 +  }
  33.274 +
  33.275 +  /** Maximum length of logMessageArray. */
  33.276 +  var kMaxLogMessageLength = 20;
  33.277 +
  33.278 +  /** An array of messages to display in the element with id "log". */
  33.279 +  var logMessageArray = [];
  33.280 +
  33.281 +  /**
  33.282 +   * Add a message to an element with id "log".
  33.283 +   *
  33.284 +   * This function is used by the default "log:" message handler.
  33.285 +   *
  33.286 +   * @param {string} message The message to log.
  33.287 +   */
  33.288 +  function logMessage(message) {
  33.289 +    logMessageArray.push(message);
  33.290 +    if (logMessageArray.length > kMaxLogMessageLength)
  33.291 +      logMessageArray.shift();
  33.292 +
  33.293 +    document.getElementById('log').textContent = logMessageArray.join('\n');
  33.294 +    console.log(message);
  33.295 +  }
  33.296 +
  33.297 +  /**
  33.298 +   */
  33.299 +  var defaultMessageTypes = {
  33.300 +    'alert': alert,
  33.301 +    'log': logMessage
  33.302 +  };
  33.303 +
  33.304 +  /**
  33.305 +   * Called when the NaCl module sends a message to JavaScript (via
  33.306 +   * PPB_Messaging.PostMessage())
  33.307 +   *
  33.308 +   * This event listener is registered in createNaClModule above.
  33.309 +   *
  33.310 +   * @param {Event} message_event A message event. message_event.data contains
  33.311 +   *     the data sent from the NaCl module.
  33.312 +   */
  33.313 +  function handleMessage(message_event) {
  33.314 +    if (typeof message_event.data === 'string') {
  33.315 +      for (var type in defaultMessageTypes) {
  33.316 +        if (defaultMessageTypes.hasOwnProperty(type)) {
  33.317 +          if (startsWith(message_event.data, type + ':')) {
  33.318 +            func = defaultMessageTypes[type];
  33.319 +            func(message_event.data.slice(type.length + 1));
  33.320 +            return;
  33.321 +          }
  33.322 +        }
  33.323 +      }
  33.324 +    }
  33.325 +
  33.326 +    if (typeof window.handleMessage !== 'undefined') {
  33.327 +      window.handleMessage(message_event);
  33.328 +      return;
  33.329 +    }
  33.330 +
  33.331 +    logMessage('Unhandled message: ' + message_event.data);
  33.332 +  }
  33.333 +
  33.334 +  /**
  33.335 +   * Called when the DOM content has loaded; i.e. the page's document is fully
  33.336 +   * parsed. At this point, we can safely query any elements in the document via
  33.337 +   * document.querySelector, document.getElementById, etc.
  33.338 +   *
  33.339 +   * @param {string} name The name of the example.
  33.340 +   * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc.
  33.341 +   * @param {string} path Directory name where .nmf file can be found.
  33.342 +   * @param {number} width The width to create the plugin.
  33.343 +   * @param {number} height The height to create the plugin.
  33.344 +   * @param {Object} attrs Optional dictionary of additional attributes.
  33.345 +   */
  33.346 +  function domContentLoaded(name, tool, path, width, height, attrs) {
  33.347 +    // If the page loads before the Native Client module loads, then set the
  33.348 +    // status message indicating that the module is still loading.  Otherwise,
  33.349 +    // do not change the status message.
  33.350 +    updateStatus('Page loaded.');
  33.351 +    if (!browserSupportsNaCl(tool)) {
  33.352 +      updateStatus(
  33.353 +          'Browser does not support NaCl (' + tool + '), or NaCl is disabled');
  33.354 +    } else if (common.naclModule == null) {
  33.355 +      updateStatus('Creating embed: ' + tool);
  33.356 +
  33.357 +      // We use a non-zero sized embed to give Chrome space to place the bad
  33.358 +      // plug-in graphic, if there is a problem.
  33.359 +      width = typeof width !== 'undefined' ? width : 200;
  33.360 +      height = typeof height !== 'undefined' ? height : 200;
  33.361 +      attachDefaultListeners();
  33.362 +      createNaClModule(name, tool, path, width, height, attrs);
  33.363 +    } else {
  33.364 +      // It's possible that the Native Client module onload event fired
  33.365 +      // before the page's onload event.  In this case, the status message
  33.366 +      // will reflect 'SUCCESS', but won't be displayed.  This call will
  33.367 +      // display the current message.
  33.368 +      updateStatus('Waiting.');
  33.369 +    }
  33.370 +  }
  33.371 +
  33.372 +  /** Saved text to display in the element with id 'statusField'. */
  33.373 +  var statusText = 'NO-STATUSES';
  33.374 +
  33.375 +  /**
  33.376 +   * Set the global status message. If the element with id 'statusField'
  33.377 +   * exists, then set its HTML to the status message as well.
  33.378 +   *
  33.379 +   * @param {string} opt_message The message to set. If null or undefined, then
  33.380 +   *     set element 'statusField' to the message from the last call to
  33.381 +   *     updateStatus.
  33.382 +   */
  33.383 +  function updateStatus(opt_message) {
  33.384 +    if (opt_message) {
  33.385 +      statusText = opt_message;
  33.386 +    }
  33.387 +    var statusField = document.getElementById('statusField');
  33.388 +    if (statusField) {
  33.389 +      statusField.innerHTML = statusText;
  33.390 +    }
  33.391 +  }
  33.392 +
  33.393 +  // The symbols to export.
  33.394 +  return {
  33.395 +    /** A reference to the NaCl module, once it is loaded. */
  33.396 +    naclModule: null,
  33.397 +
  33.398 +    attachDefaultListeners: attachDefaultListeners,
  33.399 +    domContentLoaded: domContentLoaded,
  33.400 +    createNaClModule: createNaClModule,
  33.401 +    hideModule: hideModule,
  33.402 +    removeModule: removeModule,
  33.403 +    logMessage: logMessage,
  33.404 +    updateStatus: updateStatus
  33.405 +  };
  33.406 +
  33.407 +}());
  33.408 +
  33.409 +// Listen for the DOM content to be loaded. This event is fired when parsing of
  33.410 +// the page's document has finished.
  33.411 +document.addEventListener('DOMContentLoaded', function() {
  33.412 +  var body = document.body;
  33.413 +
  33.414 +  // The data-* attributes on the body can be referenced via body.dataset.
  33.415 +  if (body.dataset) {
  33.416 +    var loadFunction;
  33.417 +    if (!body.dataset.customLoad) {
  33.418 +      loadFunction = common.domContentLoaded;
  33.419 +    } else if (typeof window.domContentLoaded !== 'undefined') {
  33.420 +      loadFunction = window.domContentLoaded;
  33.421 +    }
  33.422 +
  33.423 +    // From https://developer.mozilla.org/en-US/docs/DOM/window.location
  33.424 +    var searchVars = {};
  33.425 +    if (window.location.search.length > 1) {
  33.426 +      var pairs = window.location.search.substr(1).split('&');
  33.427 +      for (var key_ix = 0; key_ix < pairs.length; key_ix++) {
  33.428 +        var keyValue = pairs[key_ix].split('=');
  33.429 +        searchVars[unescape(keyValue[0])] =
  33.430 +            keyValue.length > 1 ? unescape(keyValue[1]) : '';
  33.431 +      }
  33.432 +    }
  33.433 +
  33.434 +    if (loadFunction) {
  33.435 +      var toolchains = body.dataset.tools.split(' ');
  33.436 +      var configs = body.dataset.configs.split(' ');
  33.437 +
  33.438 +      var attrs = {};
  33.439 +      if (body.dataset.attrs) {
  33.440 +        var attr_list = body.dataset.attrs.split(' ');
  33.441 +        for (var key in attr_list) {
  33.442 +          var attr = attr_list[key].split('=');
  33.443 +          var key = attr[0];
  33.444 +          var value = attr[1];
  33.445 +          attrs[key] = value;
  33.446 +        }
  33.447 +      }
  33.448 +
  33.449 +      var tc = toolchains.indexOf(searchVars.tc) !== -1 ?
  33.450 +          searchVars.tc : toolchains[0];
  33.451 +
  33.452 +      // If the config value is included in the search vars, use that.
  33.453 +      // Otherwise default to Release if it is valid, or the first value if
  33.454 +      // Release is not valid.
  33.455 +      if (configs.indexOf(searchVars.config) !== -1)
  33.456 +        var config = searchVars.config;
  33.457 +      else if (configs.indexOf('Release') !== -1)
  33.458 +        var config = 'Release';
  33.459 +      else
  33.460 +        var config = configs[0];
  33.461 +
  33.462 +      var pathFormat = body.dataset.path;
  33.463 +      var path = pathFormat.replace('{tc}', tc).replace('{config}', config);
  33.464 +
  33.465 +      isTest = searchVars.test === 'true';
  33.466 +      isRelease = path.toLowerCase().indexOf('release') != -1;
  33.467 +
  33.468 +      loadFunction(body.dataset.name, tc, path, body.dataset.width,
  33.469 +                   body.dataset.height, attrs);
  33.470 +    }
  33.471 +  }
  33.472 +});
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/test/nacl/index.html	Fri Jun 06 15:45:59 2014 -0300
    34.3 @@ -0,0 +1,21 @@
    34.4 +<!DOCTYPE html>
    34.5 +<html>
    34.6 +<!--
    34.7 +Copyright (c) 2012 The Chromium Authors. All rights reserved.
    34.8 +Use of this source code is governed by a BSD-style license that can be
    34.9 +found in the LICENSE file.
   34.10 +-->
   34.11 +<head>
   34.12 +  <meta http-equiv="Pragma" content="no-cache">
   34.13 +  <meta http-equiv="Expires" content="-1">
   34.14 +  <title>SDL NACL Test</title>
   34.15 +  <script type="text/javascript" src="common.js"></script>
   34.16 +</head>
   34.17 +<body data-width="640" data-height="640" data-name="sdl_app" data-tools="pnacl" data-configs="Debug Release" data-path="{tc}/{config}">
   34.18 +  <h1>SDL NACL Test</h1>
   34.19 +  <h2>Status: <code id="statusField">NO-STATUS</code></h2>
   34.20 +  <!-- The NaCl plugin will be embedded inside the element with id "listener".
   34.21 +      See common.js.-->
   34.22 +  <div id="listener"></div>
   34.23 +</body>
   34.24 +</html>
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/test/nacl/manifest.json	Fri Jun 06 15:45:59 2014 -0300
    35.3 @@ -0,0 +1,22 @@
    35.4 +{
    35.5 +  "name": "SDL testgles2",
    35.6 +  "version": "33.0.1750.117",
    35.7 +  "minimum_chrome_version": "33.0.1750.117",
    35.8 +  "manifest_version": 2,
    35.9 +  "description": "testgles2",
   35.10 +  "offline_enabled": true,
   35.11 +  "icons": {
   35.12 +    "128": "icon128.png"
   35.13 +  },
   35.14 +  "app": {
   35.15 +    "background": {
   35.16 +      "scripts": ["background.js"]
   35.17 +    }
   35.18 +  },
   35.19 +  "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMN716Qyu0l2EHNFqIJVqVysFcTR6urqhaGGqW4UK7slBaURz9+Sb1b4Ot5P1uQNE5c+CTU5Vu61wpqmSqMMxqHLWdPPMh8uRlyctsb2cxWwG6XoGSvpX29NsQVUFXd4v2tkJm3G9t+V0X8TYskrvWQmnyOW8OEIDvrBhUEfFxWQIDAQAB",
   35.20 +  "oauth2": {
   35.21 +    "client_id": "903965034255.apps.googleusercontent.com",
   35.22 +    "scopes": ["https://www.googleapis.com/auth/drive"]
   35.23 +  },
   35.24 +  "permissions": []
   35.25 +}
    36.1 --- a/test/testgles2.c	Thu Jun 05 15:37:33 2014 -0700
    36.2 +++ b/test/testgles2.c	Fri Jun 06 15:45:59 2014 -0300
    36.3 @@ -16,7 +16,7 @@
    36.4  
    36.5  #include "SDL_test_common.h"
    36.6  
    36.7 -#if defined(__IPHONEOS__) || defined(__ANDROID__)
    36.8 +#if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__NACL__)
    36.9  #define HAVE_OPENGLES2
   36.10  #endif
   36.11  
   36.12 @@ -195,6 +195,8 @@
   36.13  {
   36.14      GLint status = GL_FALSE;
   36.15      const char *shaders[1] = { NULL };
   36.16 +    char buffer[1024];
   36.17 +    GLsizei length;
   36.18  
   36.19      /* Create shader and load into GL. */
   36.20      *shader = GL_CHECK(ctx.glCreateShader(shader_type));
   36.21 @@ -212,7 +214,9 @@
   36.22  
   36.23      /* Dump debug info (source and log) if compilation failed. */
   36.24      if(status != GL_TRUE) {
   36.25 -        SDL_Log("Shader compilation failed");
   36.26 +        ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
   36.27 +        buffer[length] = '\0';
   36.28 +        SDL_Log("Shader compilation failed: %s", buffer);fflush(stderr);
   36.29          quit(-1);
   36.30      }
   36.31  }
   36.32 @@ -675,7 +679,7 @@
   36.33          SDL_Log("%2.2f frames per second\n",
   36.34                 ((double) frames * 1000) / (now - then));
   36.35      }
   36.36 -#if !defined(__ANDROID__)    
   36.37 +#if !defined(__ANDROID__) && !defined(__NACL__)  
   36.38      quit(0);
   36.39  #endif    
   36.40      return 0;
    37.1 --- a/test/testrendercopyex.c	Thu Jun 05 15:37:33 2014 -0700
    37.2 +++ b/test/testrendercopyex.c	Fri Jun 06 15:45:59 2014 -0300
    37.3 @@ -161,6 +161,16 @@
    37.4          quit(2);
    37.5      }
    37.6  
    37.7 +#if __NACL__
    37.8 +    SDL_RWUmount("/");
    37.9 +    SDL_RWMount(
   37.10 +        "",  /* source */
   37.11 +        "/",  /* target */
   37.12 +        "httpfs",  /* filesystemtype */
   37.13 +        0,  /* mountflags */
   37.14 +        "");  /* data specific to the html5fs type */
   37.15 +#endif    
   37.16 +    
   37.17      drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   37.18      for (i = 0; i < state->num_windows; ++i) {
   37.19          DrawState *drawstate = &drawstates[i];