Files
andros 4c35e112a4 Add native Wayland support to the OpenGL backend via PGTK
Third platform binding, alongside NS/Metal (macOS) and X11/GL: PGTK
frames reuse gfxterm.c (drawing policy) and glterm.c (EGL/GLES driver)
with new window plumbing only.  Works both under native Wayland and
under X11 via GDK.

- configure: --with-gpu now also accepts --with-pgtk builds; detects
  wayland-egl (HAVE_WAYLAND_EGL).
- EGL surface glue for three cases: X11 (Xlib window), PGTK/X11 (GDK
  XID via dlsym, no gdkx.h), PGTK/Wayland (wl_egl_window on a desync
  wl_subsurface anchored at the edit area, so GDK's own commits and the
  GTK menu bar never conflict with GPU frames).
- Wayland present timing by wl_surface.frame callback; HiDPI scale fed
  from the GDK scale factor; double surfaceless fallback when the
  native EGL display has no usable configs.
- Toolkit scroll bars: native windows on X11 sessions (the server
  clips presents under them); on Wayland an alpha-bearing EGL config
  plus transparent holes punched at the bar rects let GTK's scroll
  bars show through the subsurface.
- Glyph atlas rasterized with FreeType flags derived from the cairo
  scaled font options, matching GTK's slight hinting on PGTK while
  keeping X11 output unchanged.
- gpu-transition-active-p on both backends (GL and Metal): reports a
  running buffer-switch cross-fade without advancing it.
- pgtk-win.el: auto-enable the backend on startup, honoring
  EMACS_GPU_DISABLE, like x-win.el and ns-win.el.

Verified with the emacs-gpu-qa layer-1 suite: 16/16 PASS on PGTK+X11
(Xvfb), 16/16 PASS on native Wayland (sway headless), 16/16 PASS
unchanged on plain X11, plus an 18/18 functional smoke on Wayland
(transitions, cursor effects, pump, GStreamer video, captures).  All
under llvmpipe in containers: not yet validated on real Wayland
hardware.  macOS (Metal) remains the primary, fully supported
platform.
2026-07-24 18:39:41 +02:00

405 lines
16 KiB
EmacsLisp

;;; pgtk-win.el --- parse relevant switches and set up for Pure-GTK -*- lexical-binding: t -*-
;; Copyright (C) 1995, 2001-2020, 2022-2026 Free Software Foundation,
;; Inc.
;; Author: FSF
;; Keywords: terminals
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(eval-when-compile (require 'cl-lib))
(unless (featurep 'pgtk)
(error "%s: Loading pgtk-win.el but not compiled with PGTK"
invocation-name))
;; Documentation-purposes only: actually loaded in loadup.el.
(require 'term/common-win)
(require 'frame)
(require 'mouse)
(require 'scroll-bar)
(require 'faces)
(require 'menu-bar)
(require 'fontset)
(require 'dnd)
(require 'pgtk-dnd)
(defvar x-invocation-args)
(defvar x-command-line-resources)
(defvar pgtk-input-file)
(defvar pgtk-use-im-context-on-new-connection)
(declare-function pgtk-use-im-context "pgtkim.c")
(defun pgtk-suspend-error ()
"Don't allow suspending if any of the frames are PGTK frames."
(if (memq 'pgtk (mapcar 'window-system (frame-list)))
(error "Cannot suspend Emacs while a PGTK GUI frame exists")))
(defvar pgtk-initialized nil
"Non-nil if pure-GTK windowing has been initialized.")
(declare-function x-handle-args "common-win" (args))
(declare-function x-open-connection "pgtkfns.c"
(display &optional xrm-string must-succeed))
(declare-function pgtk-set-resource "pgtkfns.c" (attribute value))
;; Do the actual window system setup here; the above code just defines
;; functions and variables that we use now.
(cl-defmethod window-system-initialization (&context (window-system pgtk)
&optional display)
"Initialize the PGTK window system.
WINDOW-SYSTEM is, aptly, `pgtk'.
DISPLAY is the name of the display Emacs should connect to."
(cl-assert (not pgtk-initialized))
;; PENDING: not needed?
(setq command-line-args (x-handle-args command-line-args))
;; Make sure we have a valid resource name.
(when (boundp 'x-resource-name)
(unless (stringp x-resource-name)
(let (i)
(setq x-resource-name (copy-sequence invocation-name))
;; Change any . or * characters in x-resource-name to hyphens,
;; so as not to choke when we use it in X resource queries.
(while (setq i (string-match "[.*]" x-resource-name))
(aset x-resource-name i ?-)))))
;; Setup the default fontset.
(create-default-fontset)
;; Create the standard fontset.
(condition-case err
(create-fontset-from-fontset-spec standard-fontset-spec t)
(error (display-warning
'initialization
(format "Creation of the standard fontset failed: %s" err)
:error)))
(x-open-connection (or display
x-display-name)
x-command-line-resources
;; Exit Emacs with fatal error if this fails and we
;; are the initial display.
(= (length (frame-list)) 0))
(x-apply-session-resources)
;; Don't let Emacs suspend under PGTK.
(add-hook 'suspend-hook 'pgtk-suspend-error)
(setq pgtk-initialized t))
;; Any display name is OK.
(add-to-list 'display-format-alist '(".*" . pgtk))
(cl-defmethod handle-args-function (args &context (window-system pgtk))
(x-handle-args args))
(cl-defmethod frame-creation-function (params &context (window-system pgtk))
(x-create-frame-with-faces params))
(declare-function pgtk-own-selection-internal "pgtkselect.c" (selection value &optional frame))
(declare-function pgtk-disown-selection-internal "pgtkselect.c" (selection &optional terminal))
(declare-function pgtk-selection-owner-p "pgtkselect.c" (&optional selection terminal))
(declare-function pgtk-selection-exists-p "pgtkselect.c" (&optional selection terminal))
(declare-function pgtk-get-selection-internal "pgtkselect.c" (selection-symbol target-type &optional terminal))
(cl-defmethod gui-backend-set-selection (selection value
&context (window-system pgtk))
(if value (pgtk-own-selection-internal selection value)
(pgtk-disown-selection-internal selection)))
(cl-defmethod gui-backend-selection-owner-p (selection
&context (window-system pgtk))
(pgtk-selection-owner-p selection))
(cl-defmethod gui-backend-selection-exists-p (selection
&context (window-system pgtk))
(pgtk-selection-exists-p selection))
(cl-defmethod gui-backend-get-selection (selection-symbol target-type
&context (window-system pgtk))
(pgtk-get-selection-internal selection-symbol target-type))
(defvar pgtk-preedit-overlay nil)
(defun pgtk-preedit-text (event)
"An internal function to display preedit text from input method.
EVENT is a `preedit-text' event."
(interactive "e")
(when pgtk-preedit-overlay
(delete-overlay pgtk-preedit-overlay))
(setq pgtk-preedit-overlay nil)
(let ((ovstr "")
(idx 0)
atts ov str color face-name)
(dolist (part (nth 1 event))
(setq str (car part))
(setq face-name (intern (format "pgtk-im-%d" idx)))
(eval
`(defface ,face-name nil "face of input method preedit"))
(setq atts nil)
(when (setq color (cdr-safe (assq 'fg (cdr part))))
(setq atts (append atts `(:foreground ,color))))
(when (setq color (cdr-safe (assq 'bg (cdr part))))
(setq atts (append atts `(:background ,color))))
(when (setq color (cdr-safe (assq 'ul (cdr part))))
(setq atts (append atts `(:underline ,color))))
(face-spec-set face-name `((t . ,atts)))
(add-text-properties 0 (length str) `(face ,face-name) str)
(setq ovstr (concat ovstr str))
(setq idx (1+ idx)))
(setq ov (make-overlay (point) (point)))
(overlay-put ov 'before-string ovstr)
(setq pgtk-preedit-overlay ov)))
(define-key special-event-map [preedit-text] 'pgtk-preedit-text)
(defun pgtk-use-im-context-handler ()
"Set up input context usage after Emacs initialization."
(when (eq window-system 'pgtk)
(pgtk-use-im-context pgtk-use-im-context-on-new-connection)))
(add-hook 'after-init-hook #'pgtk-use-im-context-handler)
(defcustom x-gtk-stock-map
'(
("etc/images/new" . ("document-new" "gtk-new"))
("etc/images/open" . ("document-open" "gtk-open"))
("etc/images/diropen" . "gtk-directory")
("etc/images/close" . ("window-close" "gtk-close"))
("etc/images/save" . ("document-save" "gtk-save"))
("etc/images/saveas" . ("document-save-as" "gtk-save-as"))
("etc/images/undo" . ("edit-undo" "gtk-undo"))
("etc/images/redo" . ("edit-redo" "gtk-redo"))
("etc/images/cut" . ("edit-cut" "gtk-cut"))
("etc/images/copy" . ("edit-copy" "gtk-copy"))
("etc/images/paste" . ("edit-paste" "gtk-paste"))
("etc/images/search" . ("edit-find" "gtk-find"))
("etc/images/print" . ("document-print" "gtk-print"))
("etc/images/preferences" . ("preferences-system" "gtk-preferences"))
("etc/images/help" . ("help-browser" "gtk-help"))
("etc/images/left-arrow" . ("go-previous" "gtk-go-back"))
("etc/images/right-arrow" . ("go-next" "gtk-go-forward"))
("etc/images/home" . ("go-home" "gtk-home"))
("etc/images/jump-to" . ("go-jump" "gtk-jump-to"))
("etc/images/index" . ("gtk-search" "gtk-index"))
("etc/images/exit" . ("application-exit" "gtk-quit"))
("etc/images/cancel" . "gtk-cancel")
("etc/images/info" . ("dialog-information" "gtk-info"))
("etc/images/bookmark_add" . "n:bookmark_add")
;; Used in package-menu
("images/package-menu/execute" . "gtk-apply")
("images/package-menu/info" . ("dialog-information" "gtk-info"))
("images/package-menu/install" . ("archive-insert" "list-add"))
("images/package-menu/delete" . ("archive-remove" "gtk-remove"))
("images/package-menu/unmark" . ("gnumeric-object-checkbox" "box"))
("images/package-menu/url" . "globe")
("images/package-menu/upgrade" . ("archive-extract" "go-bottom"))
;; Used in Gnus and/or MH-E:
("etc/images/attach" . ("mail-attachment" "gtk-attach"))
("etc/images/connect" . "gtk-connect")
("etc/images/contact" . "gtk-contact")
("etc/images/delete" . ("edit-delete" "gtk-delete"))
("etc/images/describe" . ("document-properties" "gtk-properties"))
("etc/images/disconnect" . "gtk-disconnect")
;; ("etc/images/exit" . "gtk-exit")
("etc/images/lock-broken" . "gtk-lock_broken")
("etc/images/lock-ok" . "gtk-lock_ok")
("etc/images/lock" . "gtk-lock")
("etc/images/next-page" . "gtk-next-page")
("etc/images/refresh" . ("view-refresh" "gtk-refresh"))
("etc/images/search-replace" . "edit-find-replace")
("etc/images/sort-ascending" . ("view-sort-ascending" "gtk-sort-ascending"))
("etc/images/sort-column-ascending" . "gtk-sort-column-ascending")
("etc/images/sort-criteria" . "gtk-sort-criteria")
("etc/images/sort-descending" . ("view-sort-descending"
"gtk-sort-descending"))
("etc/images/sort-row-ascending" . "gtk-sort-row-ascending")
("etc/images/spell" . ("tools-check-spelling" "gtk-spell-check"))
("images/gnus/toggle-subscription" . "gtk-task-recurring")
("images/mail/compose" . ("mail-message-new" "gtk-mail-compose"))
("images/mail/copy" . "gtk-mail-copy")
("images/mail/forward" . "gtk-mail-forward")
("images/mail/inbox" . "gtk-inbox")
("images/mail/move" . "gtk-mail-move")
("images/mail/not-spam" . "gtk-not-spam")
("images/mail/outbox" . "gtk-outbox")
("images/mail/reply-all" . "gtk-mail-reply-to-all")
("images/mail/reply" . "gtk-mail-reply")
("images/mail/save-draft" . "gtk-mail-handling")
("images/mail/send" . ("mail-send" "gtk-mail-send"))
("images/mail/spam" . "gtk-spam")
;; Used for GDB Graphical Interface
("images/gud/break" . "gtk-no")
("images/gud/recstart" . ("media-record" "gtk-media-record"))
("images/gud/recstop" . ("media-playback-stop" "gtk-media-stop"))
;; No themed versions available:
;; mail/preview (combining stock_mail and stock_zoom)
;; mail/save (combining stock_mail, stock_save and stock_convert)
("images/mpc/prev" . "media-skip-backward")
("images/mpc/rewind" . "media-seek-backward")
("images/mpc/pause" . "media-playback-pause")
("images/mpc/play" . "media-playback-start")
("images/mpc/ffwd" . "media-seek-forward")
("images/mpc/next" . "media-skip-forward")
("images/mpc/stop" . "media-playback-stop")
("images/mpc/add" . "list-add")
)
"How icons for tool bars are mapped to Gtk+ stock items.
Emacs must be compiled with the Gtk+ toolkit for this to have any effect.
A value that begins with n: denotes a named icon instead of a stock icon."
:version "31.1"
:type '(choice (repeat
(choice symbol
(cons (string :tag "Emacs icon")
(choice (group (string :tag "Named")
(string :tag "Stock"))
(string :tag "Stock/named"))))))
:group 'pgtk)
(defcustom icon-map-list '(x-gtk-stock-map)
"A list of alists that map icon file names to stock/named icons.
The alists are searched in the order they appear. The first match is used.
The keys in the alists are file names without extension and with two directory
components. For example, to map /usr/share/emacs/22.1.1/etc/images/open.xpm
to stock item gtk-open, use:
(\"etc/images/open\" . \"gtk-open\")
Themes also have named icons. To map to one of those, use n: before the name:
(\"etc/images/diropen\" . \"n:system-file-manager\")
The list elements are either the symbol name for the alist or the
alist itself.
If you don't want stock icons, set the variable to nil."
:version "22.2"
:type '(choice (const :tag "Don't use stock icons" nil)
(repeat (choice symbol
(cons (string :tag "Emacs icon")
(choice
(group (string "Named")
(string "Stock"))
(string :tag "Stock/named"))))))
:group 'pgtk)
(defconst x-gtk-stock-cache (make-hash-table :weakness t :test 'equal))
(defun x-gtk-map-stock (file)
"Map icon with file name FILE to a Gtk+ stock name.
This uses `icon-map-list' to map icon file names to stock icon names."
(when (stringp file)
(or (gethash file x-gtk-stock-cache)
(puthash
file
(save-match-data
(let* ((file-sans (file-name-sans-extension file))
(key (and (string-match "/\\([^/]+/[^/]+/[^/]+$\\)"
file-sans)
(match-string 1 file-sans)))
(icon-map icon-map-list)
elem value)
(while (and (null value) icon-map)
(setq elem (car icon-map)
value (assoc-string (or key file-sans)
(if (symbolp elem)
(symbol-value elem)
elem))
icon-map (cdr icon-map)))
(and value (cdr value))))
x-gtk-stock-cache))))
(declare-function accelerate-menu "pgtkmenu.c" (&optional frame) t)
(defun pgtk-menu-bar-open (&optional frame)
"Open the menu bar if it is shown.
`popup-menu' is used if it is off."
(interactive "i")
(cond
((and (not (zerop (or (frame-parameter nil 'menu-bar-lines) 0)))
(fboundp 'accelerate-menu))
(accelerate-menu frame))
(t
(popup-menu (mouse-menu-bar-map) last-nonmenu-event))))
(defun pgtk-device-class (name)
"Return the device class of NAME.
Users should not call this function; see `device-class' instead."
(cond
((not name) nil)
((string-match-p "XTEST" name) 'test)
((string= "Virtual core pointer" name) 'core-pointer)
((string= "Virtual core keyboard" name) 'core-keyboard)
(t (let ((number (ignore-errors
(string-to-number name))))
(when number
(cl-case number
(0 'mouse)
(1 'pen)
(2 'eraser)
(3 'puck)
(4 'keyboard)
(5 'touchscreen)
(6 'touchpad)
(7 'trackpoint)
(8 'pad)))))))
(defvaralias 'x-gtk-use-system-tooltips 'use-system-tooltips)
(define-key special-event-map [drag-n-drop] #'pgtk-dnd-handle-drag-n-drop-event)
(add-hook 'after-make-frame-functions #'pgtk-dnd-init-frame)
;; OpenGL GPU backend (emacs-gpu). When Emacs is built --with-gpu (which
;; enables the OpenGL driver on PGTK, both under Wayland and under X11 via
;; GDK) and the backend is available, load the `gpu' module and enable it
;; on the initial frame at startup, mirroring the X11 and Metal auto-enable.
;; Builds without the backend lack `gpu-backend-p', so this is a no-op
;; there. Set EMACS_GPU_DISABLE to a non-empty value to start with the
;; stock CPU renderer instead.
(declare-function gpu-backend-p "glfns.c")
(declare-function gpu-enable "gpu" (&optional frame))
(defvar gpu-enable-on-startup)
(defun pgtk--maybe-enable-gpu ()
"Load and enable the OpenGL GPU backend on startup when available.
Honors the EMACS_GPU_DISABLE environment variable as an opt-out."
(when (and (member (getenv "EMACS_GPU_DISABLE") '(nil ""))
(fboundp 'gpu-backend-p)
(gpu-backend-p))
(require 'gpu)
;; Also enable graphic frames created later in this session.
(setq gpu-enable-on-startup t)
(when (display-graphic-p)
(ignore-errors (gpu-enable)))))
(add-hook 'window-setup-hook #'pgtk--maybe-enable-gpu)
(provide 'pgtk-win)
(provide 'term/pgtk-win)
;;; pgtk-win.el ends here