reddit / emacs
Change DPI when moving screens
telenieko · 2022-04-17 21:11
Hi,
I'm running Fedora 36 (beta). And my laptop has a 4K display (Dell XPS 9305) so I have it running at 200% scale + Large Fonts (GNOME Accessibility option).
But I also often use a 1080p display on the side, which runs at different DPI and 100% scale on Wayland.
Most apps when moved from one display to the other kinda work. But not Emacs, when I move Emacs to the 1080p display all letters are gigantic (like 200% gigantic).
How can I make Emacs adapt to the different scale/DPI settings on the different monitors?
I had a similar setup on Windows 11 and had not seen that happen.
Emacs 28, vanilla; With mixed-pitch fonts.
clhodapp · 2022-04-18 00:43
I suspect that you're running into the fact that xwayland is just horrible in setups where you use display scaling. This would be affecting you because (at least I belive) emacs isn't really ready for Wayland yet (possibly you can try to make something happen with the new pure gtk3 stuff, but my understanding is that it's not yet ready for prime time).
I have a similar setup to yours at work (a 4k display and a 1080p one) but I use pure x11 there. The best I've been able to do is to use supersampling on my 1080p display: I tell xorg to treat it like a 4k display and scale the image down to fit. That way, I can use my same scaling options across the entire x11 screen.
dpassen1 · 2022-04-18 02:54
Perhaps using https://github.com/WJCFerguson/textsize
lastnamebird · 2022-04-18 05:30
I've spent a decent amount of time trying to sort out fonts in Emacs w/ DPI. I have only found one way to manually set a font while determining it's DPI (partly inspired by Prot's config [Here](https://protesilaos.com/emacs/dotemacs#h:e03b6415-a18f-4058-b9b0-5721d38c6c50).
;; changes all frames and sets default font
(internal-set-lisp-face-attribute
'default :font (font-spec :family "Triplicate OT"
:size 10.0 :dpi 144) 0)
To determine the monitor you can use `frame-monitor-attributes`. So setting the dpi based on a monitor name can be done like this:
(defun set-frame-font (&optional frame)
(let ((mon (alist-get 'name (frame-monitor-attributes frame)))
(px-size (aref (font-info (face-font 'default)) 2))
(pt-per-inch 72.27))
(pcase mon
("[laptop monitor name]"
(unless (= px-size (round (+ (/ (* 10.0 144) pt-per-inch) 0.5)))
(internal-set-lisp-face-attribute
'default :font (font-spec :family "Triplicate OT"
:size 10.0 :dpi 144 frame)))
("[external monitor name]"
(unless (= px-size (round (+ (/ (* 10.0 96) pt-per-inch) 0.5)))
(internal-set-lisp-face-attribute
'default :font (font-spec :family "Triplicate OT"
:size 10.0 :dpi 96 frame))))))
The next step would be to have this run automatically. I would approach it either with a function on the `server-after-make-frame-hook` or with `window-size-change-functions` ([see docs for more info](https://www.gnu.org/software/emacs/manual/html_node/elisp/Window-Hooks.html#index-window_002dsize_002dchange_002dfunctions))
(defun frame-moved-monitors (frame)
(when (frame-size-changed-p)
(set-frame-font frame)))
(push 'frame-moved-monitors window-size-change-functions)
You could also try using a timer to monitor any changes in the current frame's monitor. This repo may be useful [dispwatch](https://github.com/mnp/dispwatch).
I hope this points you in the right direction. Also, I have to disagree with the other poster, building emacs with pgtk has been working perfectly fine for me on wayland.
telenieko · 2022-04-18 06:31
I went Wayland precisely because x11 cannot do multiple scaling. It's same options for all :(
telenieko · 2022-04-18 06:32
Will take a look at it. Thank you!
podtatranec · 2022-04-18 07:07
Indeed the issue will be at least partially due to Xwayland baggage. Emacs only starts supporting Wayland natively in 29.x (28.1 was released a week or two ago). So if you want a nicer mixed-DPI experience I believe you might need to update your emacs from copr (or compile your own).
But even then I am not sure the problem will be solved. Worth a shot a guess?
madEntro · 2022-04-18 08:13
I have nearly the same problem (just different displays), so I use a Wayland version of Emacs: [https://github.com/mpsq/emacs-gcc-wayland-devel-builder](https://github.com/mpsq/emacs-gcc-wayland-devel-builder)
telenieko · 2022-04-18 09:04
Thank you for the pointer, I found this on COPR <https://copr.fedorainfracloud.org/coprs/tpb/emacs-pgtk-nativecomp/> that is a current Emacs build with PGTK (Pure GTK) at it works as expected.
Moving Emacs between screens now adapts propertly.
Thank you everyone for all the tips!
clhodapp · 2022-04-19 06:54
It can't do multi scaling but you can fake it (very expensively) with supersampling.
That said, it sounds like you got proper native Wayland support working so it sounds like you're set!
telenieko · 2022-04-19 11:50
The Wayland / pgtk packages are working well. Had no issues yet!
RipKord42 · 2022-09-06 15:25
u/lastnamebird you are my new hero. I'm not dealing with differing DPI monitors but after searching high and low I still could not get emacs to scale properly, not through GTK settings, anything. The first snippet in your post, added to my init.el solved it instantly. THANKS!!
lastnamebird · 2022-09-06 18:08
I'm glad it could help. However, if you are not dealing with differing DPIs and you are using a gtk build of emacs a more robust solution may be to set the gtk setting `text-scaling-factor`. I used to have my DPI set to 144 on both my laptop and my external monitor, so I set my scaling factor to 1.5 (144 / 96 (default dpi) = 1.5). It worked well and covered all my gtk applications.