I reported this bug to the feature/pgtk maintainer
https://github.com/masm11/emacs/issues/108
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=50321
and it has just been fixed in
https://git.savannah.gnu.org/cgit/emacs.git/commit/?h=feature/pgtk&id=b22323c3b66feb3c9c0f3086cc784fab9578ff7b
This is only a partial answer to the question.
I recently upgraded to Emacs 28.0.50 with feature/pgtk
to have better Wayland integration. Just like you I am connecting with both GUI and terminal clients to the same daemon, but scrolling no longer worked in the terminal.
I figured out that I need the following Elisp to restore scrolling in the terminal.
(unless window-system
(defun track-mouse (e))
(xterm-mouse-mode t)
(when (featurep 'pgtk)
(setq mouse-wheel-down-event 'mouse-4)
(setq mouse-wheel-up-event 'mouse-5)
(global-set-key (kbd "<mouse-4>") 'mwheel-scroll)
(global-set-key (kbd "<mouse-5>") 'mwheel-scroll)
(global-set-key (kbd "<C-mouse-4>") 'mouse-wheel-text-scale)
(global-set-key (kbd "<C-mouse-5>") 'mouse-wheel-text-scale)
(global-set-key (kbd "<S-mouse-4>") 'mwheel-scroll)
(global-set-key (kbd "<S-mouse-5>") 'mwheel-scroll)))
Unfortunately, this only fires once, so it breaks scrolling for GUI clients. I tried to work around this using focus-in-hook
and make-variable-frame-local
but the former crashed Emacs when switching between frames and the latter is deprecated.
EDIT: I've managed to find something that seems to do what I want. Essentially I advise mwheel-scroll
and mouse-wheel-text-scale
to locally change the event type for scrolling when invoked through mouse-4
and mouse-5
. This is probably extremely inefficient but it seem to work.
(defun track-mouse (e))
(xterm-mouse-mode t)
(when (featurep 'pgtk)
(defun user/mwheel-scroll (&rest args)
"Wraps `mwheel-scroll' for use with <mouse-4> and <mouse-5>."
(interactive (advice-eval-interactive-spec
(cadr (interactive-form 'mwheel-scroll))))
(let ((mouse-wheel-down-event 'mouse-4)
(mouse-wheel-up-event 'mouse-5))
(apply 'mwheel-scroll args)))
(defun user/mouse-wheel-text-scale (&rest args)
"Wraps `mouse-wheel-text-scale' for use with <mouse-4> and <mouse-5>."
(interactive (advice-eval-interactive-spec
(cadr (interactive-form 'mouse-wheel-text-scale))))
(let ((mouse-wheel-down-event 'mouse-4)
(mouse-wheel-up-event 'mouse-5))
(apply 'mouse-wheel-text-scale args)))
(global-set-key (kbd "<mouse-4>") 'user/mwheel-scroll)
(global-set-key (kbd "<mouse-5>") 'user/mwheel-scroll)
(global-set-key (kbd "<C-mouse-4>") 'user/mouse-wheel-text-scale)
(global-set-key (kbd "<C-mouse-5>") 'user/mouse-wheel-text-scale)
(global-set-key (kbd "<S-mouse-4>") 'user/mwheel-scroll)
(global-set-key (kbd "<S-mouse-5>") 'user/mwheel-scroll))