#include <gtk/gtk.h>
#include <X11/Xlib.h>
#undef Success
#undef RootWindow
#include <stdlib.h>
#include <unistd.h>
#include <memory>
#include <string>
#include "include/base/cef_logging.h"
#include "include/cef_app.h"
#include "include/cef_command_line.h"
#include "include/wrapper/cef_helpers.h"
#include "tests/cefclient/browser/main_context_impl.h"
#include "tests/cefclient/browser/main_message_loop_multithreaded_gtk.h"
#include "tests/cefclient/browser/test_runner.h"
#include "tests/shared/browser/client_app_browser.h"
#include "tests/shared/browser/main_message_loop_external_pump.h"
#include "tests/shared/browser/main_message_loop_std.h"
#include "tests/shared/common/client_app_other.h"
#include "tests/shared/common/client_switches.h"
#include "tests/shared/renderer/client_app_renderer.h"
namespace client {
namespace {
int XErrorHandlerImpl(Display* display, XErrorEvent* event) {
LOG(WARNING) << "X error received: "
<< "type " << event->type << ", "
<< "serial " << event->serial << ", "
<< "error_code " << static_cast<int>(event->error_code) << ", "
<< "request_code " << static_cast<int>(event->request_code)
<< ", "
<< "minor_code " << static_cast<int>(event->minor_code);
return 0;
}
int XIOErrorHandlerImpl(Display* display) {
return 0;
}
void TerminationSignalHandler(int signatl) {
LOG(ERROR) << "Received termination signal: " << signatl;
MainContext::Get()->GetRootWindowManager()->CloseAllWindows(true);
}
int RunMain(int argc, char* argv[]) {
CefScopedArgArray scoped_arg_array(argc, argv);
char** argv_copy = scoped_arg_array.array();
CefMainArgs main_args(argc, argv);
CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
command_line->InitFromArgv(argc, argv);
CefRefPtr<CefApp> app;
ClientApp::ProcessType process_type = ClientApp::GetProcessType(command_line);
if (process_type == ClientApp::BrowserProcess) {
app = new ClientAppBrowser();
} else if (process_type == ClientApp::RendererProcess ||
process_type == ClientApp::ZygoteProcess) {
app = new ClientAppRenderer();
} else if (process_type == ClientApp::OtherProcess) {
app = new ClientAppOther();
}
int exit_code = CefExecuteProcess(main_args, app, nullptr);
if (exit_code >= 0)
return exit_code;
auto context = std::make_unique<MainContextImpl>(command_line, true);
CefSettings settings;
#if !defined(CEF_USE_SANDBOX)
settings.no_sandbox = true;
#endif
context->PopulateSettings(&settings);
if (settings.windowless_rendering_enabled) {
setenv("MESA_GL_VERSION_override", "3.1", 0);
}
std::unique_ptr<MainMessageLoop> message_loop;
if (settings.multi_threaded_message_loop)
message_loop.reset(new MainMessageLoopMultithreadedGtk);
else if (settings.external_message_pump)
message_loop = MainMessageLoopExternalPump::Create();
else
message_loop.reset(new MainMessageLoopStd);
context->Initialize(main_args, settings, app, nullptr);
gdk_set_allowed_backends("x11");
gtk_init(&argc, &argv_copy);
XSetErrorHandler(XErrorHandlerImpl);
XSetIOErrorHandler(XIOErrorHandlerImpl);
signal(SIGINT, TerminationSignalHandler);
signal(SIGTERM, TerminationSignalHandler);
test_runner::RegisterSchemeHandlers();
auto window_config = std::make_unique<RootWindowConfig>();
window_config->always_on_top =
command_line->HasSwitch(switches::kAlwaysOnTop);
window_config->with_controls =
!command_line->HasSwitch(switches::kHideControls);
window_config->with_osr =
settings.windowless_rendering_enabled ? true : false;
context->GetRootWindowManager()->CreateRootWindow(std::move(window_config));
int result = message_loop->Run();
context->Shutdown();
message_loop.reset();
context.reset();
return result;
}
}
}
int main(int argc, char* argv[]) {
return client::RunMain(argc, argv);
}