#include "../application.h"
#include "../debug.h"
#include "../cef/app_handler.hpp"
#include "../cef/client_handler.hpp"
#include "impl.h"
#include <include/cef_app.h>
#include <include/cef_base.h>
#include <stdlib.h>
#if defined(CEF_X11)
#include <X11/Xlib.h>
#endif
#if defined(BW_WIN32)
#pragma comment(lib, "shell32.lib")
#endif
void _bw_Application_exitProcess( int exit_code );
CefString to_string( bw_CStrSlice );
#ifdef CEF_X11
int _bw_ApplicationCef_xErrorHandler( Display* display, XErrorEvent* event );
int _bw_ApplicationCef_xIoErrorHandler( Display* display );
#endif
bw_ApplicationEngineImpl bw_ApplicationEngineImpl_initialize( bw_Application* app, int argc, char** argv, const bw_ApplicationSettings* settings ) {
bw_ApplicationEngineImpl impl;
#ifdef BW_WIN32
CefMainArgs main_args( GetModuleHandle(NULL) );
#else
CefMainArgs main_args( argc, argv );
#endif
CefRefPtr<CefApp> cef_app_handle( new AppHandler( app ) );
int exit_code = CefExecuteProcess( main_args, cef_app_handle.get(), 0 );
if ( exit_code >= 0 ) {
exit( exit_code );
return impl;
}
#if defined(CEF_X11)
XSetErrorHandler( _bw_ApplicationCef_xErrorHandler );
XSetIOErrorHandler( _bw_ApplicationCef_xIoErrorHandler );
#endif
CefSettings app_settings;
#if defined(BW_WIN32) || defined(BW_GTK)
app_settings.multi_threaded_message_loop = true;
#endif
if ( settings->resource_dir.data != 0 ) {
char* path = bw_string_copyAsNewCstr( settings->resource_dir );
CefString( &app_settings.resources_dir_path ) = path;
bw_string_freeCstr(path);
}
CefInitialize( main_args, app_settings, cef_app_handle.get(), 0 );
CefRefPtr<CefClient>* client = new CefRefPtr<CefClient>(new ClientHandler( app ));
impl.exit_code = 0;
impl.cef_client = (void*)client;
return impl;
}
void bw_ApplicationEngineImpl_finish( bw_ApplicationEngineImpl* app ) {
CefShutdown();
delete (CefRefPtr<CefClient>*)app->cef_client;
}
#ifdef CEF_X11
int _bw_ApplicationCef_xErrorHandler( Display* display, XErrorEvent* event ) {
fprintf( stderr, "X Error: type %d, serial %lu, error code %d, request code %d, mino r code %d\n", event->type, event->serial, event->error_code, event->request_code, event->minor_code );
return 0;
}
int _bw_ApplicationCef_xIoErrorHandler( Display* display ) {
return 0;
}
#endif