1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WidgetUtilsGtk.h"
8 #include "mozilla/StaticPrefs_widget.h"
9 #include "mozilla/UniquePtr.h"
10 #include "nsReadableUtils.h"
17 namespace mozilla::widget
{
19 int32_t WidgetUtilsGTK::IsTouchDeviceSupportPresent() {
21 GdkDisplay
* display
= gdk_display_get_default();
26 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
32 gdk_device_manager_list_devices(manager
, GDK_DEVICE_TYPE_SLAVE
);
33 GList
* list
= devices
;
36 GdkDevice
* device
= static_cast<GdkDevice
*>(devices
->data
);
37 if (gdk_device_get_source(device
) == GDK_SOURCE_TOUCHSCREEN
) {
41 devices
= devices
->next
;
51 bool IsMainWindowTransparent() {
52 return nsWindow::IsToplevelWindowTransparent();
55 // We avoid linking gdk_*_display_get_type directly in order to avoid a runtime
56 // dependency on GTK built with both backends. Other X11- and Wayland-specific
57 // functions get stubbed out by libmozgtk and crash when called, but those
58 // should only be called when the matching backend is already in use.
60 bool GdkIsWaylandDisplay(GdkDisplay
* display
) {
61 static auto sGdkWaylandDisplayGetType
=
62 (GType(*)())dlsym(RTLD_DEFAULT
, "gdk_wayland_display_get_type");
63 return sGdkWaylandDisplayGetType
&&
64 G_TYPE_CHECK_INSTANCE_TYPE(display
, sGdkWaylandDisplayGetType());
67 bool GdkIsX11Display(GdkDisplay
* display
) {
68 static auto sGdkX11DisplayGetType
=
69 (GType(*)())dlsym(RTLD_DEFAULT
, "gdk_x11_display_get_type");
70 return sGdkX11DisplayGetType
&&
71 G_TYPE_CHECK_INSTANCE_TYPE(display
, sGdkX11DisplayGetType());
74 bool GdkIsWaylandDisplay() {
75 static bool isWaylandDisplay
= gdk_display_get_default() &&
76 GdkIsWaylandDisplay(gdk_display_get_default());
77 return isWaylandDisplay
;
80 bool GdkIsX11Display() {
81 static bool isX11Display
= gdk_display_get_default()
82 ? GdkIsX11Display(gdk_display_get_default())
87 GdkDevice
* GdkGetPointer() {
88 GdkDisplay
* display
= gdk_display_get_default();
89 GdkDeviceManager
* deviceManager
= gdk_display_get_device_manager(display
);
90 return gdk_device_manager_get_client_pointer(deviceManager
);
93 bool IsRunningUnderFlatpak() {
94 // https://gitlab.gnome.org/GNOME/gtk/-/blob/4300a5c609306ce77cbc8a3580c19201dccd8d13/gdk/gdk.c#L472
95 static bool sRunning
= [] {
96 return g_file_test("/.flatpak-info", G_FILE_TEST_EXISTS
);
101 const char* GetSnapInstanceName() {
102 static const char* sInstanceName
= []() -> const char* {
103 // Intentionally leaked, as keeping a pointer to the environment forever is
105 if (const char* instanceName
= g_getenv("SNAP_INSTANCE_NAME")) {
106 return g_strdup(instanceName
);
108 // Compatibility for snapd <= 2.35:
109 if (const char* instanceName
= g_getenv("SNAP_NAME")) {
110 return g_strdup(instanceName
);
115 return sInstanceName
;
118 bool ShouldUsePortal(PortalKind aPortalKind
) {
119 static bool sPortalEnv
= [] {
120 if (IsRunningUnderFlatpakOrSnap()) {
123 const char* portalEnvString
= g_getenv("GTK_USE_PORTAL");
124 return portalEnvString
&& atoi(portalEnvString
) != 0;
127 bool autoBehavior
= sPortalEnv
;
128 const int32_t pref
= [&] {
129 switch (aPortalKind
) {
130 case PortalKind::FilePicker
:
131 return StaticPrefs::widget_use_xdg_desktop_portal_file_picker();
132 case PortalKind::MimeHandler
:
133 // Mime portal breaks default browser handling, see bug 1516290.
134 autoBehavior
= IsRunningUnderFlatpakOrSnap();
135 return StaticPrefs::widget_use_xdg_desktop_portal_mime_handler();
136 case PortalKind::Settings
:
138 return StaticPrefs::widget_use_xdg_desktop_portal_settings();
153 nsTArray
<nsCString
> ParseTextURIList(const nsACString
& aData
) {
154 UniquePtr
<char[]> data(ToNewCString(aData
));
155 gchar
** uris
= g_uri_list_extract_uris(data
.get());
157 nsTArray
<nsCString
> result
;
158 for (size_t i
= 0; i
< g_strv_length(uris
); i
++) {
159 result
.AppendElement(nsCString(uris
[i
]));
166 } // namespace mozilla::widget