aboutsummaryrefslogtreecommitdiffstats
path: root/src/pgtkfns.c
diff options
context:
space:
mode:
authorDaniel Colascione2024-12-23 13:34:11 -0500
committerDaniel Colascione2024-12-28 13:28:40 -0500
commit72eb5f8bedbe441f6a3952557b9aa3ef6fd6235d (patch)
tree42d421c1de07e411efa43904a26dcee0091df962 /src/pgtkfns.c
parent0334b0743f83a3e69d1d3a426efbba1d2a5f2cc6 (diff)
downloademacs-72eb5f8bedbe441f6a3952557b9aa3ef6fd6235d.tar.gz
emacs-72eb5f8bedbe441f6a3952557b9aa3ef6fd6235d.zip
Make browser windows pop up when browsing URLs on Wayland
When a user invokes browse-url, the browser window viewing the URL should be raised in the user's desktop environment. On X11, running xdg-open as a subprocess does the job. However, on Wayland, this approach doesn't work: xdg-open makes the web browser browse a URL all right, but doesn't raise the browser window in the GUI. Therefore, if the browser window is behind Emacs, browse-url appears to do nothing. Repeated invocations of browse-url cause the browser to load multiple tabs in the background, surprising the user when he gives up in frustration and manually switches to the browser window. There's no subprocess we can run to make the right thing happen. Wayland requires that we pass along event activation information to the browser using the xdg_activation_v1 protocol. This change adds x-gtk-launch-uri to invoke GTK-native URL-dispatch machinery. This machinery DTRT on both X11 and Wayland. We fall back to the default browser machinery if we're not on a GTK frame. The logic is more complicated than it has to be because the GTK URI launch mechanism requires that we launch with respect to a specific GTK window, and in some environments (e.g., running emacs -nw in a PGTK Emacs) we don't have a GTK window. We also want to preserve the effect of customizing browse-url-browser-function, so adding an entry to browse-url-default-handlers that preempts URI open when we happen to be on a GTK frame is the wrong thing to do. * lisp/net/browse-url.el (browse-url--browser-defcustom-type): (browse-url--inhibit-pgtk): avoid infinite recursion (browse-url-default-browser): use pgtk launch (x-gtk-launch-uri): new function (browse-url-default-gtk-browser): ues it * src/pgtkfns.c (unwind_gerror_ptr): new function (Fx_gtk_launch_uri): new function (syms_of_pgtkfns): register it * etc/NEWS: mention the new feature
Diffstat (limited to 'src/pgtkfns.c')
-rw-r--r--src/pgtkfns.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/pgtkfns.c b/src/pgtkfns.c
index 0ff3262cd11..1c2b8985ee8 100644
--- a/src/pgtkfns.c
+++ b/src/pgtkfns.c
@@ -3819,6 +3819,44 @@ DEFUN ("x-gtk-debug", Fx_gtk_debug, Sx_gtk_debug, 1, 1, 0,
3819 return NILP (enable) ? Qnil : Qt; 3819 return NILP (enable) ? Qnil : Qt;
3820} 3820}
3821 3821
3822static void
3823unwind_gerror_ptr (void* data)
3824{
3825 GError* error = *(GError**)data;
3826 if (error)
3827 g_error_free (error);
3828}
3829
3830DEFUN ("x-gtk-launch-uri", Fx_gtk_launch_uri, Sx_gtk_launch_uri, 2, 2, 0,
3831 doc: /* launch URI */)
3832 (Lisp_Object frame, Lisp_Object uri)
3833{
3834 CHECK_FRAME (frame);
3835
3836 if (!FRAME_LIVE_P (XFRAME (frame)) ||
3837 !FRAME_PGTK_P (XFRAME (frame)) ||
3838 !FRAME_GTK_OUTER_WIDGET (XFRAME (frame)))
3839 error ("GTK URI launch not available for this frame");
3840
3841 CHECK_STRING (uri);
3842 guint32 timestamp = gtk_get_current_event_time ();
3843
3844 GError *err = NULL;
3845 specpdl_ref count = SPECPDL_INDEX ();
3846
3847 record_unwind_protect_ptr (unwind_gerror_ptr, &err);
3848
3849 gtk_show_uri_on_window (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (XFRAME (frame))),
3850 SSDATA (uri),
3851 timestamp,
3852 &err);
3853
3854 if (err)
3855 error ("Failed to launch URI via GTK: %s", err->message);
3856
3857 return unbind_to (count, Qnil);
3858}
3859
3822void 3860void
3823syms_of_pgtkfns (void) 3861syms_of_pgtkfns (void)
3824{ 3862{
@@ -3890,6 +3928,7 @@ syms_of_pgtkfns (void)
3890 defsubr (&Sx_close_connection); 3928 defsubr (&Sx_close_connection);
3891 defsubr (&Sx_display_list); 3929 defsubr (&Sx_display_list);
3892 defsubr (&Sx_gtk_debug); 3930 defsubr (&Sx_gtk_debug);
3931 defsubr (&Sx_gtk_launch_uri);
3893 3932
3894 defsubr (&Sx_show_tip); 3933 defsubr (&Sx_show_tip);
3895 defsubr (&Sx_hide_tip); 3934 defsubr (&Sx_hide_tip);