Bug 1751217 Part 3: Make HDR-capable macOS screens report 30 pixelDepth. r=mstange
[gecko.git] / widget / gtk / WidgetUtilsGtk.cpp
blob33faa61f1b783516ae45700f3862afa220829dec
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"
11 #include "nsWindow.h"
13 #include <gtk/gtk.h>
14 #include <dlfcn.h>
15 #include <glib.h>
17 namespace mozilla::widget {
19 int32_t WidgetUtilsGTK::IsTouchDeviceSupportPresent() {
20 int32_t result = 0;
21 GdkDisplay* display = gdk_display_get_default();
22 if (!display) {
23 return 0;
26 GdkDeviceManager* manager = gdk_display_get_device_manager(display);
27 if (!manager) {
28 return 0;
31 GList* devices =
32 gdk_device_manager_list_devices(manager, GDK_DEVICE_TYPE_SLAVE);
33 GList* list = devices;
35 while (devices) {
36 GdkDevice* device = static_cast<GdkDevice*>(devices->data);
37 if (gdk_device_get_source(device) == GDK_SOURCE_TOUCHSCREEN) {
38 result = 1;
39 break;
41 devices = devices->next;
44 if (list) {
45 g_list_free(list);
48 return result;
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())
83 : false;
84 return isX11Display;
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);
97 }();
98 return sRunning;
101 const char* GetSnapInstanceName() {
102 static const char* sInstanceName = []() -> const char* {
103 // Intentionally leaked, as keeping a pointer to the environment forever is
104 // a bit suspicious.
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);
112 return nullptr;
113 }();
115 return sInstanceName;
118 bool ShouldUsePortal(PortalKind aPortalKind) {
119 static bool sPortalEnv = [] {
120 if (IsRunningUnderFlatpakOrSnap()) {
121 return true;
123 const char* portalEnvString = g_getenv("GTK_USE_PORTAL");
124 return portalEnvString && atoi(portalEnvString) != 0;
125 }();
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:
137 autoBehavior = true;
138 return StaticPrefs::widget_use_xdg_desktop_portal_settings();
140 return 2;
141 }();
143 switch (pref) {
144 case 0:
145 return false;
146 case 1:
147 return true;
148 default:
149 return autoBehavior;
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]));
162 g_strfreev(uris);
163 return result;
166 } // namespace mozilla::widget