Skip to content

Commit

Permalink
Add main_win.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcwatters committed Feb 17, 2018
1 parent c0b4203 commit 8d7c844
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 18 deletions.
50 changes: 32 additions & 18 deletions cef_app_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,36 +353,50 @@ __attribute__((visibility("default"))) int cef_time_delta(const cef_time_t* cef_



typedef struct _cef_main_args_t {
int argc;
char** argv;
} cef_main_args_t;
///
// Structure representing CefExecuteProcess arguments.
///
typedef struct _cef_main_args_t { HINSTANCE instance; } cef_main_args_t;




typedef unsigned long DWORD;
struct HWND__{int unused;}; typedef struct HWND__ *HWND;
struct HMENU__{int unused;}; typedef struct HMENU__ *HMENU;
///
// Structure representing window information.
///
typedef struct _cef_window_info_t {
// Standard parameters required by CreateWindowEx()
DWORD ex_style;
cef_string_t window_name;
DWORD style;
int x;
int y;
int width;
int height;




int hidden;




void* parent_view;
HWND parent_window;
HMENU menu;

///
// Set to true (1) to create the browser using windowless (off-screen)
// rendering. No window will be created for the browser and all rendering will
// occur via the CefRenderHandler interface. The |parent_window| value will be
// used to identify monitor info and to act as the parent window for dialogs,
// context menus, etc. If |parent_window| is not provided then the main screen
// monitor will be used and some functionality that requires a parent window
// may not function correctly. In order to create windowless browsers the
// CefSettings.windowless_rendering_enabled value must be set to true.
// Transparent painting is enabled by default but can be disabled by setting
// CefBrowserSettings.background_color to an opaque value.
///
int windowless_rendering_enabled;




void* view;
///
// Handle for the new browser window. Only used with windowed rendering.
///
HWND window;
} cef_window_info_t;


Expand Down
133 changes: 133 additions & 0 deletions test/main_win.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
argc = 0
for k, v in pairs( arg ) do argc = argc + 1 end
-- CEF C API example
-- Project website: https://github.com/cztomczak/cefcapi

local ffi = require( "ffi" )
ffi.cdef [[
int printf(const char *fmt, ...);
struct HINSTANCE__{int unused;}; typedef struct HINSTANCE__ *HINSTANCE;
typedef HINSTANCE HMODULE;
typedef char CHAR;
typedef const CHAR *LPCSTR, *PCSTR;
typedef LPCSTR PCTSTR, LPCTSTR, PCUTSTR, LPCUTSTR;
HMODULE GetModuleHandleW(
LPCTSTR lpModuleName
);
]]

local cef = require( "../cef" )

-- Globals
local g_life_span_handler = ffi.new( "cef_life_span_handler_t" );


-- This executable is called many times, because it
-- is also used for subprocesses. Let's print args
-- so we can differentiate between main process and
-- subprocesses. If one of the first args is for
-- example "--type=renderer" then it means that
-- this is a Renderer process. There may be more
-- subprocesses like GPU (--type=gpu-process) and
-- others. On Linux there are also special Zygote
-- processes.
ffi.C.printf("\nProcess args: ");
if (argc == 1) then
ffi.C.printf("none (Main process)");
else
for i = -1, argc - 2 do
if (string.len(arg[i]) > 128) then
ffi.C.printf("... ");
else
ffi.C.printf("%s ", arg[i]);
end
end
end
ffi.C.printf("\n\n");

-- CEF version
if (argc == 0) then
ffi.C.printf("CEF version: %s\n", "3.3282.1733.g9091548");
end

-- Main args
local main_args = ffi.new( "cef_main_args_t" );
main_args.instance = ffi.C.GetModuleHandleW(nil);

-- Cef app
local app = ffi.new( "cef_app_t" );
-- initialize_cef_app(&app);

-- Application settings. It is mandatory to set the
-- "size" member.
local settings = ffi.new( "cef_settings_t" );
-- Specify the path for the sub-process executable.
local browser_subprocess_path = "cef.exe";
local cef_browser_subprocess_path = ffi.new( "cef_string_t" );
cef.cef_string_utf8_to_utf16(browser_subprocess_path,
string.len(browser_subprocess_path),
cef_browser_subprocess_path);
settings.browser_subprocess_path = cef_browser_subprocess_path;
settings.size = ffi.sizeof( settings );
settings.log_severity = ffi.C.LOGSEVERITY_WARNING; -- Show only warnings/errors
settings.no_sandbox = 1;

-- Initialize CEF
ffi.C.printf("cef_initialize\n");
cef.cef_initialize(main_args, settings, app, nil);

-- Window info
local window_info = ffi.new( "cef_window_info_t" );
window_info.style = bit.bor( bit.bor(0x00000000, 0x00C00000, 0x00080000,
0x00040000, 0x00020000, 0x00010000), 0x02000000, 0x04000000,
0x10000000 );
window_info.parent_window = nil;
window_info.x = 0x80000000;
window_info.y = 0x80000000;
window_info.width = 0x80000000;
window_info.height = 0x80000000;

-- Window info - window title
local window_name = "cefcapi example";
local cef_window_name = ffi.new( "cef_string_t" );
cef.cef_string_utf8_to_utf16(window_name, string.len(window_name),
cef_window_name);
window_info.window_name = cef_window_name;

-- Initial url
local url = "https://www.google.com/ncr";
local cef_url = ffi.new( "cef_string_t" );
cef.cef_string_utf8_to_utf16(url, string.len(url), cef_url);

-- Browser settings. It is mandatory to set the
-- "size" member.
local browser_settings = ffi.new( "cef_browser_settings_t" );
browser_settings.size = ffi.sizeof( browser_settings );

-- Client handlers
local client = ffi.new( "cef_client_t" );
-- initialize_cef_client(&client);
-- initialize_cef_life_span_handler(&g_life_span_handler);

-- Create browser asynchronously. There is also a
-- synchronous version of this function available.
ffi.C.printf("cef_browser_host_create_browser\n");
cef.cef_browser_host_create_browser(window_info, client, cef_url,
browser_settings, nil);

-- Message loop. There is also cef_do_message_loop_work()
-- that allow for integrating with existing message loops.
-- On Windows for best performance you should set
-- cef_settings_t.multi_threaded_message_loop to true.
-- Note however that when you do that CEF UI thread is no
-- more application main thread and using CEF API is more
-- difficult and require using functions like cef_post_task
-- for running tasks on CEF UI thread.
ffi.C.printf("cef_run_message_loop\n");
cef.cef_run_message_loop();

-- Shutdown CEF
ffi.C.printf("cef_shutdown\n");
cef.cef_shutdown();

os.exit(0);

0 comments on commit 8d7c844

Please sign in to comment.