Embedding CEF into GTK

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Embedding CEF into GTK

Postby bamilab » Sun Dec 06, 2020 7:53 am

I'm looking around on the internet and am finding two different ways to do this. Is one or the other outdated? Or are both options still available?

I'm reading that you can embed CEF if you get an X window handle from your GTK window.
I'm also reading that you can embed CEF into GTK when you're building CEF with an option "--use-gtk3" for automate.py.

Does the second option also work correctly if you're running the app on a system running wayland instead of X?

Thank you.
bamilab
Techie
 
Posts: 14
Joined: Fri Oct 23, 2020 12:32 pm

Re: Embedding CEF into GTK

Postby magreenblatt » Sun Dec 06, 2020 12:35 pm

The cefclient application demonstrates how to embed CEF in GTK2. There is also this PR for GTK3. Wayland requires GTK3 and a special ozone build of CEF.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Embedding CEF into GTK

Postby bamilab » Fri Dec 18, 2020 10:09 am

I've gone and just tried embedding CEF into GTK3 anyway.
I'm using the binary distribution for linux 64 bit right now.

The CefWindowInfo::SetAsChild function seems to ask for an X window handle.
So what I do is that I create an GTK window, and get its X window handle, and then pass it to the above function.
What strikes me as odd is that when I call CreateBrowser, I get GTK related errors, even though I'm passing an X window handle, which I've seen the cefclient sample do.

Code: Select all
[1218/160835.121296:WARNING:browser_main_loop.cc(272)] GLib-GObject: invalid cast from 'GtkApplicationWindow' to 'GdkWindow'
[1218/160835.121400:ERROR:browser_main_loop.cc(270)] Gdk: gdk_window_show_internal: assertion 'GDK_IS_WINDOW (window)' failed
[1218/160835.148525:FATAL:cef_ref_counted.h(325)] Assert failed: ptr_ != __null.
[1218/160835.149306:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
[1218/160835.165204:ERROR:gl_surface_egl.cc(773)] EGL Driver message (Error) eglCreateWindowSurface: Bad native window.
[1218/160835.165354:ERROR:gl_surface_egl.cc(1443)] eglCreateWindowSurface failed with error EGL_BAD_NATIVE_WINDOW
[1218/160835.168773:ERROR:x11_software_bitmap_presenter.cc(134)] XGetWindowAttributes failed for window 62914568
[0100/000000.391640:INFO:child_thread_impl.cc(838)] ChildThreadImpl::EnsureConnected()


I thought CEF wanted an X window handle because it uses its own windowing toolkit Aura. But apparantly it uses GTK internally? I'm confused.
bamilab
Techie
 
Posts: 14
Joined: Fri Oct 23, 2020 12:32 pm

Re: Embedding CEF into GTK

Postby magreenblatt » Fri Dec 18, 2020 11:15 am

It uses X11 internally. Look at cefclient and the PR linked above for reference.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Embedding CEF into GTK

Postby bamilab » Sat Dec 19, 2020 8:37 am

Alright. I've found an error within my own GTK code. I'm just still confused how CEF was catching and redirecting those errors to the console as if it was comming from CEF...

Anyway, I'm still getting the following errors:
Code: Select all
[1219/140601.888881:FATAL:cef_ref_counted.h(325)] Assert failed: ptr_ != __null.
[1219/140601.894259:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
[1219/140601.902136:ERROR:gl_surface_egl.cc(773)] EGL Driver message (Error) eglCreateWindowSurface: Bad native window.
[1219/140601.902241:ERROR:gl_surface_egl.cc(1443)] eglCreateWindowSurface failed with error EGL_BAD_NATIVE_WINDOW
[1219/140601.905347:WARNING:ipc_message_attachment_set.cc(49)] MessageAttachmentSet destroyed with unconsumed attachments: 0/1
[1219/140601.905608:WARNING:ipc_message_attachment_set.cc(49)] MessageAttachmentSet destroyed with unconsumed attachments: 0/1
[1219/140601.906561:ERROR:x11_software_bitmap_presenter.cc(134)] XGetWindowAttributes failed for window 94371848


What seems interresting is that the X window handle I give to CefWindowInfo::SetAsChild is different than 94371848.
What also seems weird is that I make one call to CefBrowserHost::CreateBrowser, causes two calls to be made to CefApp::OnBrowserCreated. :?:

I've been looking at cefclient, and also what the PR changed about it. However, I'm currently only interrested at the standard browser implementation, not the OSR version. It seems rather straightforward, however, I seem to run in problems nevertheless.

Here is some code of mine that constructs the GTK window:
Code: Select all
   //GtkWidget* gtk_handle = gtk_application_window_new( window->app->impl.handle );
   GtkWidget* gtk_handle = gtk_window_new( GTK_WINDOW_TOPLEVEL );
 
   gtk_widget_hide_on_delete( gtk_handle );

   if ( window->parent != 0 ) {
      gtk_widget_set_parent_window( gtk_handle, GDK_WINDOW( window->parent->impl.handle ) );
      gtk_window_set_destroy_with_parent( GTK_WINDOW(gtk_handle), FALSE );
   }

   // Title
   gchar* title = bw_string_copyAsNewCstr( _title );
   gtk_window_set_title( GTK_WINDOW(gtk_handle), title );
   free( title );

   if ( width != -1 || height != -1 ) {
      if ( width < 1 )
         width = 800;
      if ( height < 1 )
         height = 600;

      // Width and height
      gtk_window_resize( GTK_WINDOW(gtk_handle), width, height );
   }

   gtk_window_set_resizable( GTK_WINDOW(gtk_handle), options->resizable );

   gtk_application_add_window( window->app->impl.handle, GTK_WINDOW(gtk_handle) );
   
   gtk_widget_realize( gtk_handle );


and here is some code of mine that tries to embed CEF into the window (the GtkWidget* handle is now accessible through bw->window->impl.handle):
Code: Select all
   gtk_widget_show_all( bw->window->impl.handle );
   GdkWindow* gdk_window = gtk_widget_get_window( bw->window->impl.handle );
   Window x_window = GDK_WINDOW_XID( gdk_window );
   CefRect rect( 0, 0, width, height );

   info.SetAsChild( x_window, rect );


Also, I'm pretty confident that all other CEF code is fine, since it seems to work on Windows with win32 code.
On my linux system, I'm just getting an empty GTK window though.
bamilab
Techie
 
Posts: 14
Joined: Fri Oct 23, 2020 12:32 pm

Re: Embedding CEF into GTK

Postby bamilab » Sat Dec 19, 2020 12:20 pm

Could it be that multi_threaded_message_loop doesn't actually work properly on Linux?

The APi docs say it does though https://magpcss.org/ceforum/apidocs3/projects/(default)/_cef_settings_t.html#multi_threaded_message_loop
bamilab
Techie
 
Posts: 14
Joined: Fri Oct 23, 2020 12:32 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 20 guests