forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindow_utils_linux.pyx
More file actions
159 lines (135 loc) · 7.21 KB
/
Copy pathwindow_utils_linux.pyx
File metadata and controls
159 lines (135 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright (c) 2013 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
include "cefpython.pyx"
class WindowUtils:
# You have to overwrite this class and provide implementations
# for these methods.
@classmethod
def OnSetFocus(cls, WindowHandle windowHandle, long msg, long wparam,
long lparam):
# Available only on Windows, but have it available on other
# platforms so that PyCharm doesn't warn about unresolved reference.
pass
@classmethod
def OnSize(cls, WindowHandle windowHandle, long msg, long wparam,
long lparam):
# Available only on Windows, but have it available on other
# platforms so that PyCharm doesn't warn about unresolved reference.
pass
@classmethod
def OnEraseBackground(cls, WindowHandle windowHandle, long msg,
long wparam, long lparam):
# Available only on Windows, but have it available on other
# platforms so that PyCharm doesn't warn about unresolved reference.
pass
@classmethod
def GetParentHandle(cls, WindowHandle windowHandle):
Debug("WindowUtils::GetParentHandle() not implemented (returns 0)")
return 0
@classmethod
def IsWindowHandle(cls, WindowHandle windowHandle):
Debug("WindowUtils::IsWindowHandle() not implemented (always True)")
return True
@classmethod
def gtk_plug_new(cls, WindowHandle gdkNativeWindow):
return <WindowHandle>gtk_plug_new(<unsigned long>gdkNativeWindow)
@classmethod
def gtk_widget_show(cls, WindowHandle gtkWidgetPtr):
with nogil:
gtk_widget_show(<GtkWidget*>gtkWidgetPtr)
@classmethod
def InstallX11ErrorHandlers(cls):
with nogil:
x11.InstallX11ErrorHandlers()
# ---------------------------------------------------------------------------
# Linux platform helpers — called from Initialize().
# ---------------------------------------------------------------------------
def _linux_gtk_init():
"""Initialize GTK so GDK has an open display connection before CEF.
cefpython provides GTK-based dialogs (file open/save, print) through the
native client-handler layer (dialog_handler_gtk.cpp). Like upstream
cefclient, GTK must be initialised in the browser process before
CefInitialize(). GTK runs on Chromium's own GLib-based UI message loop
(base::MessagePumpGlib) once cef.MessageLoop() / CefRunMessageLoop()
starts, so no separate gtk_main() is needed.
"""
try:
import os as _os, ctypes as _ct
# Must be set before gtk_init() so GDK opens an X11/Xwayland display.
_os.environ.setdefault("GDK_BACKEND", "x11")
_gtk = _ct.CDLL("libgtk-3.so.0")
_gtk.gtk_disable_setlocale()
_gtk.gtk_init(None, None)
except Exception as _e:
Debug("_linux_gtk_init() failed: " + str(_e))
def _linux_apply_initialize_defaults(app_settings, cmd_switches):
"""Auto-apply Linux defaults that every cefpython app needs.
cefpython embeds the browser via X11 window handles, so Chromium's Ozone
backend is forced to X11 on all Linux systems, including Wayland sessions
(where it runs under XWayland).
Uses setdefault so users can still override any individual entry by passing
it explicitly to cef.Initialize(switches={...}). Each setting kept here
has been individually retested against current CEF/Chromium — anything
that did not regress when removed has been dropped.
"""
import os as _os
# Force Chrome's Ozone backend to X11. This is the *only* thing keeping
# Chromium off the Wayland display on a Wayland session — cefpython embeds
# via X11 window handles (CefWindowInfo.SetAsChild) and drives X11 window
# geometry directly. GDK_BACKEND=x11 is set separately in
# _linux_gtk_init() before gtk_init().
#
# Root cause (upstream CEF): native windowed embedding into a client
# parent_window is implemented for X11 only — CreateHostWindow() in CEF's
# libcef/browser/native/browser_platform_delegate_native_linux.cc is wrapped
# entirely in `#if BUILDFLAG(SUPPORTS_OZONE_X11)` (creating a CefWindowX11)
# with no Wayland branch, and there is no window_wayland implementation.
# Wayland has no cross-process window embedding (no X11-style window IDs /
# XReparent), so CEF cannot parent the browser into a foreign Wayland
# surface; embedders must run under X11/XWayland. Verified on CEF 147:
# without this switch, a Wayland session selects the Wayland Ozone backend
# and the embedding path crashes.
cmd_switches.setdefault("ozone-platform", "x11")
# Vulkan ICD fallback for systems with no system-installed driver.
#
# On systems with no Vulkan ICD (typical for VMs and minimal containers),
# Chromium's GPU process fails its Vulkan probe and the renderer logs a
# transient
# ContextResult::kTransientFailure: Failed to send
# GpuControl.CreateCommandBuffer
# before falling back to software rendering. Pointing VK_ICD_FILENAMES
# at the SwiftShader manifest bundled with CEF makes the probe succeed
# immediately and silences the line.
#
# Only apply the fallback when no system ICD is present in the standard
# loader search paths — overriding a working Mesa/NVIDIA/AMD ICD with
# SwiftShader would force software rendering for no reason on real GPUs.
# Honors a pre-set VK_ICD_FILENAMES (setdefault) so users can override.
import glob as _glob
_system_icds = (_glob.glob("/usr/share/vulkan/icd.d/*.json") +
_glob.glob("/etc/vulkan/icd.d/*.json") +
_glob.glob("/usr/local/share/vulkan/icd.d/*.json"))
if not _system_icds:
import cefpython3 as _cef3_pkg
_cef3_dir = _os.path.dirname(_cef3_pkg.__file__)
_vk_icd = _os.path.join(_cef3_dir, "vk_swiftshader_icd.json")
if _os.path.exists(_vk_icd):
_os.environ.setdefault("VK_ICD_FILENAMES", _vk_icd)
# NOTE: external_message_pump is intentionally NOT forced here. On Linux
# cef.MessageLoop() runs CefRunMessageLoop() (same as Windows/macOS and
# upstream cefsimple/cefclient), and Chromium's UI loop is GLib-based so
# GTK works without a separate pump. Apps that integrate CEF into their
# own GUI loop via cef.MessageLoopWork() may still opt in explicitly.
# Allow per-browser opt-in to off-screen rendering. Required by examples
# that pass WindowInfo.SetAsOffscreen() (e.g. pysdl2.py) and by JS-created
# popup browsers, which are destroyed immediately when DoClose returns
# False — no delete_event would be dispatched on a windowed popup.
app_settings.setdefault("windowless_rendering_enabled", True)
# Chromium's Linux sandbox. CEF Linux builds default sandbox-ON and refuse
# to start unless a SUID-root chrome-sandbox helper is installed or
# --no-sandbox is passed. A pip wheel cannot install a chown-root helper,
# so cefpython disables the sandbox on Linux, as it always has. Added only
# when the caller has not set the switch explicitly.
if "no-sandbox" not in cmd_switches:
cmd_switches["no-sandbox"] = ""