Subject: x-display-mm-width and x-display-mm-height both return 0 on wayland

From: [email-masked]
Date: Tue, 09 May 2023 02:08:29 +0000

The functions "x-display-mm-width" and "x-display-mm-height" both return
0 on Wayland, but on a specific display.

I also have a laptop with the same setup (Arch Linux on Hyprland as
window manager) and a similar emacs version (I compiled both on my
desktop and laptop about ~1 hour from each other, my laptop went
first). This problem is not present on my laptop.

These functions should return the dimensions of my display in
milimiters, as one would assume. This issue is causing numerous features
to fail with an "Arithmetic Overflow Error" since at some point they
divide by this number, and division by 0 is problematic of course.

Most built-in games are broken (like tetris or snake) since they depend
on these functions to compute the size of the game grid.

More importantly, latex previews on org files are also broken, since
they use the value to render the images.

If I switch to X11 (more specifically, qtile) with this same setup, the
functions return proper values, and these features are fixed.

If I launch emacs with the "GDK_BACKEND" environment variable set to
"x11" then emacs launches using xWayland, and once again, the functions
return proper values and the issue is "fixed".

This seems to be an issue with GTK, rather than emacs. I found another
user complaining about this here:
https://discourse.gnome.org/t/gdk-monitor-get-width-mm-failure-wayland/5412

Since there doesn't seem to be much the emacs developers can do about
this, I propose a workaround is set in place.

The functions that return the display size in pixels do work. Maybe
emacs could check if the mm dimensions are being reported as 0, and try
to guess appropiate values. They may be wrong, but it's a better option
than having these features outright fail with non-descriptive errors.

Alternatively, since we now know that these functions can return 0,
maybe it's more appropiate to put a check in place, and fail with a more
descriptive error message.

In the thread I linked, there's a code snippet of the xorg source code
that showcases it doing exactly that. Maybe emacs could do the same.

It is likely that my monitor is the problem here (it's a cheap one). It
may not have these values properly set in its firmware. Probably Xorg
isn't getting proper values either, and it may be relying on that code snippet.

This would also explain why it works on my laptop.
In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.37, cairo version 1.17.8) of 2023-03-21 built on Rainbow
Repository revision: 42fba8f36b19536964d6deb6a34f3fd1c02b43dd
Repository branch: makepkg
System Description: Arch Linux

Configured using:
 'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games
 --with-modules --without-libotf --without-m17n-flt --without-gconf
 --with-native-compilation=yes --with-native-compilation=aot
 --with-xinput2 --with-pgtk --without-xaw3d --with-sound=alsa
 --with-tree-sitter --without-gpm --without-compress-install
 '--program-transform-name=s/([ec]tags)/1.emacs/'
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions
 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection'
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PGTK
PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS
TREE_SITTER WEBP XIM GTK3 ZLIB

Important settings:
 value of $LANG: en_US.UTF-8
 locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
 global-undo-tree-mode: t
 undo-tree-mode: t
 org-roam-db-autosync-mode: t
 marginalia-mode: t
 which-key-mode: t
 global-tree-sitter-mode: t
 recentf-mode: t
 treemacs-filewatch-mode: t
 treemacs-follow-mode: t
 treemacs-git-mode: t
 global-git-commit-mode: t
 magit-auto-revert-mode: t
 shell-dirtrack-mode: t
 override-global-mode: t
 vertico-mouse-mode: t
 vertico-mode: t
 corfu-popupinfo-mode: t
 global-corfu-mode: t
 corfu-mode: t
 pixel-scroll-precision-mode: t
 xterm-mouse-mode: t
 global-auto-revert-mode: t
 electric-pair-mode: t
 delete-selection-mode: t
 global-display-line-numbers-mode: t
 display-line-numbers-mode: t
 tooltip-mode: t
 global-eldoc-mode: t
 eldoc-mode: t
 show-paren-mode: t
 electric-indent-mode: t
 mouse-wheel-mode: t
 file-name-shadow-mode: t
 context-menu-mode: t
 global-font-lock-mode: t
 font-lock-mode: t
 blink-cursor-mode: t
 line-number-mode: t
 transient-mark-mode: t
 auto-composition-mode: t
 auto-encryption-mode: t
 auto-compression-mode: t

Load-path shadows:
/home/tralph3/.local/share/emacs/elpa/transient-20230501.1034/transient hides /usr/share/emacs/30.0.50/lisp/transient

Features:
(shadow sort mail-extr emacsbug mule-util treemacs-bookmarks
treemacs-tags magit-bookmark bookmark time org-tempo tempo eglot
external-completion array ert ewoc debug backtrace jsonrpc xref
flymake-proc flymake project undo-tree queue org-superstar org-roam-ui
org-roam-dailies websocket bindat org-roam-migrate org-roam-log
org-roam-mode org-roam-capture org-roam-id org-roam-node org-roam-db
emacsql-sqlite-builtin sqlite org-roam-utils org-roam-compat org-roam
org-capture org-element org-persist xdg avl-tree generator org-attach
org-id org-refile org ob ob-tangle ob-ref ob-lob ob-table ob-exp
org-macro org-src ob-comint org-pcomplete org-list org-footnote
org-faces org-entities noutline outline ob-emacs-lisp ob-core ob-eval
org-cycle org-table ol org-fold org-fold-core org-keys oc org-loaddefs
find-func cal-menu calendar cal-loaddefs org-version org-compat org-macs
emacsql-sqlite emacsql-sqlite-common emacsql emacsql-compiler
simple-httpd marginalia which-key tree-sitter-langs
tree-sitter-langs-build tar-mode arc-mode archive-mode pp tree-sitter-hl
tree-sitter tree-sitter-load tree-sitter-cli tsc tsc-dyn tsc-dyn-get
dired-aux tsc-obsolete dashboard dashboard-widgets recentf tree-widget
wid-edit ffap treemacs treemacs-header-line treemacs-compatibility
treemacs-mode treemacs-interface treemacs-persistence
treemacs-filewatch-mode treemacs-follow-mode treemacs-rendering
treemacs-annotations treemacs-async treemacs-workspaces treemacs-dom
treemacs-visuals treemacs-fringe-indicator pulse treemacs-faces
treemacs-icons treemacs-scope treemacs-themes treemacs-core-utils
pfuture inline hl-line ht treemacs-logging treemacs-customization
treemacs-macros s orderless magit-submodule magit-blame magit-stash
magit-reflog magit-bisect magit-push magit-pull magit-fetch magit-clone
magit-remote magit-commit magit-sequence magit-notes magit-worktree
magit-tag magit-merge magit-branch magit-reset magit-files magit-refs
magit-status magit magit-repos magit-apply magit-wip magit-log
which-func imenu magit-diff smerge-mode diff diff-mode git-commit
log-edit message sendmail yank-media puny dired dired-loaddefs rfc822
mml mml-sec epa derived epg rfc6068 epg-config gnus-util time-date
mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util
ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils mailheader
pcvs-util add-log magit-core magit-autorevert magit-margin
magit-transient magit-process with-editor shell pcomplete server
magit-mode transient magit-git magit-base magit-section format-spec
cursor-sensor crm dash edmacro kmacro use-package-bind-key bind-key
easy-mmode vertico-mouse vertico rust-utils thingatpt rust-mode
rust-rustfmt rust-playpen rust-compile compile text-property-search
comint ansi-osc ansi-color rust-cargo kind-icon svg-lib color svg dom
xml corfu-popupinfo corfu compat all-the-icons all-the-icons-faces
data-material data-weathericons data-octicons data-fileicons
data-faicons data-alltheicons use-package-ensure use-package-core
pixel-scroll cua-base ring xt-mouse autorevert filenotify elec-pair
delsel comp comp-cstr warnings icons rx cl-extra help-mode
display-line-numbers magit-autoloads pcase tree-sitter-langs-autoloads
all-the-icons-autoloads corfu-autoloads kind-icon-autoloads
svg-lib-autoloads which-key-autoloads org-superstar-autoloads
git-commit-autoloads with-editor-autoloads vertico-autoloads
treemacs-autoloads cfrs-autoloads posframe-autoloads hydra-autoloads
pfuture-autoloads ace-window-autoloads s-autoloads rust-mode-autoloads
lv-autoloads undo-tree-autoloads org-roam-ui-autoloads
websocket-autoloads simple-httpd-autoloads org-roam-autoloads
magit-section-autoloads ht-autoloads dash-autoloads dashboard-autoloads
marginalia-autoloads avy-autoloads queue-autoloads transient-autoloads
compat-autoloads emacsql-autoloads info orderless-autoloads
tree-sitter-autoloads tsc-autoloads realgud-autoloads
realgud-recursive-autoloads test-simple-autoloads loc-changes-autoloads
load-relative-autoloads package browse-url url url-proxy url-privacy
url-expand url-methods url-history url-cookie generate-lisp-file
url-domsuf url-util mailcap url-handlers url-parse auth-source cl-seq
eieio eieio-core cl-macs password-cache json subr-x map byte-opt gv
bytecomp byte-compile url-vars cl-loaddefs cl-lib rmc iso-transl tooltip
cconv eldoc paren electric uniquify ediff-hook vc-hooks lisp-float-type
elisp-mode mwheel term/pgtk-win pgtk-win term/common-win pgtk-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer nadvice seq
simple cl-generic indonesian philippine cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button
loaddefs theme-loaddefs faces cus-face macroexp files window
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget keymap hashtable-print-readable backquote threads dbusbind
inotify dynamic-setting system-font-setting font-render-setting cairo
gtk pgtk lcms2 multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 397011 511676)
 (symbols 48 30258 126)
 (strings 32 115820 52200)
 (string-bytes 1 4250573)
 (vectors 16 67268)
 (vector-slots 8 1303883 755007)
 (floats 8 814 2552)
 (intervals 56 1037 529)
 (buffers 984 17))