##########################################################################
#
# Copyright 2010 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/


"""Generated an header, glproc.hpp, which does pretty much what GLEW does, but
covers all the functions we support.
"""

from __future__ import print_function
import sys

from dispatch import Dispatcher
import specs.stdapi as stdapi
from specs.gles12api import glesapi
from specs.eglapi import eglapi

def ResetFuncPtrsInAPI(api):
    for func in api.functions:
        print('    _'+func.name+'_ptr = NULL;')

def ResetGLFuncPtrs():
    print('void ResetGLFuncPtrs()')
    print('{')
    ResetFuncPtrsInAPI(glesapi)
    print('}')

if __name__ == '__main__':
    # glClientSideBufferData is a fake api to update client-side memory
    glesapi.delFunctionByName("glClientSideBufferData")
    glesapi.delFunctionByName("glClientSideBufferSubData")
    glesapi.delFunctionByName("glCreateClientSideBuffer")
    glesapi.delFunctionByName("glDeleteClientSideBuffer")
    glesapi.delFunctionByName("glCopyClientSideBuffer")
    glesapi.delFunctionByName("glPatchClientSideBuffer")
    # glGenGraphicBuffer_ARM, glGraphicBufferData_ARM and glDeleteGraphicBuffer are fake APIs to use GraphicBuffer on Android or to use DMA buffer on Linux
    glesapi.delFunctionByName("glGenGraphicBuffer_ARM")
    glesapi.delFunctionByName("glGraphicBufferData_ARM")
    glesapi.delFunctionByName("glDeleteGraphicBuffer_ARM")
    dispatcher = Dispatcher()

    #############################################################
    sys.stdout = open('eglproc_auto.hpp', 'w')
    print('// Generated by', sys.argv[0])
    print('#ifndef _DISPATCH_EGLPROC_HPP_')
    print('#define _DISPATCH_EGLPROC_HPP_')
    print()
    print('#include "eglimports.hpp"')
    print('#include "common/os.hpp"')
    print()
    print('#ifndef GLES_CALLCONVENTION')
    print('#define GLES_CALLCONVENTION')
    print('#endif')
    print()
    print('void * _getProcAddress(const char *procName);')
    print('void ResetGLFuncPtrs();')
    print()
    dispatcher.dispatchApi(eglapi)
    print()
    dispatcher.dispatchApi(glesapi)
    print()
    print('#endif /* !_DISPATCH_EGLPROC_HPP_ */')
    print()

    #############################################################
    sys.stdout = open('eglproc_auto.cpp', 'w')
    print('// Generated by', sys.argv[0])
    print('#include <dispatch/eglproc_auto.hpp>')
    print()
    dispatcher.defineFptrs(eglapi)
    print()
    dispatcher.defineFptrs(glesapi)
    print()
    ResetGLFuncPtrs()
    print()
