GCC Code Coverage Report


Directory: ./
File: panels/display/cc-display-config-manager-dbus.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 86 0.0%
Functions: 0 14 0.0%
Branches: 0 45 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20 #include "cc-display-config-dbus.h"
21 #include "cc-display-config-manager-dbus.h"
22
23 #include <gio/gio.h>
24
25 struct _CcDisplayConfigManagerDBus
26 {
27 CcDisplayConfigManager parent_instance;
28
29 GCancellable *cancellable;
30 GDBusConnection *connection;
31 guint monitors_changed_id;
32
33 GVariant *current_state;
34
35 gboolean apply_allowed;
36 gboolean night_light_supported;
37 };
38
39 G_DEFINE_TYPE (CcDisplayConfigManagerDBus,
40 cc_display_config_manager_dbus,
41 CC_TYPE_DISPLAY_CONFIG_MANAGER)
42
43 static CcDisplayConfig *
44 cc_display_config_manager_dbus_get_current (CcDisplayConfigManager *pself)
45 {
46 CcDisplayConfigManagerDBus *self = CC_DISPLAY_CONFIG_MANAGER_DBUS (pself);
47
48 if (!self->current_state)
49 return NULL;
50
51 return g_object_new (CC_TYPE_DISPLAY_CONFIG_DBUS,
52 "state", self->current_state,
53 "connection", self->connection, NULL);
54 }
55
56 static void
57 got_current_state (GObject *object,
58 GAsyncResult *result,
59 gpointer data)
60 {
61 CcDisplayConfigManagerDBus *self;
62 GVariant *variant;
63 g_autoptr(GError) error = NULL;
64
65 variant = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object),
66 result, &error);
67 if (!variant)
68 {
69 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
70 {
71 self = CC_DISPLAY_CONFIG_MANAGER_DBUS (data);
72 g_clear_pointer (&self->current_state, g_variant_unref);
73 _cc_display_config_manager_emit_changed (CC_DISPLAY_CONFIG_MANAGER (data));
74 g_warning ("Error calling GetCurrentState: %s", error->message);
75 }
76 return;
77 }
78
79 self = CC_DISPLAY_CONFIG_MANAGER_DBUS (data);
80 g_clear_pointer (&self->current_state, g_variant_unref);
81 self->current_state = variant;
82
83 _cc_display_config_manager_emit_changed (CC_DISPLAY_CONFIG_MANAGER (self));
84 }
85
86 static void
87 get_current_state (CcDisplayConfigManagerDBus *self)
88 {
89 g_dbus_connection_call (self->connection,
90 "org.gnome.Mutter.DisplayConfig",
91 "/org/gnome/Mutter/DisplayConfig",
92 "org.gnome.Mutter.DisplayConfig",
93 "GetCurrentState",
94 NULL,
95 NULL,
96 G_DBUS_CALL_FLAGS_NO_AUTO_START,
97 -1,
98 self->cancellable,
99 got_current_state,
100 self);
101 }
102
103 static void
104 monitors_changed (GDBusConnection *connection,
105 const gchar *sender_name,
106 const gchar *object_path,
107 const gchar *interface_name,
108 const gchar *signal_name,
109 GVariant *parameters,
110 gpointer data)
111 {
112 CcDisplayConfigManagerDBus *self = CC_DISPLAY_CONFIG_MANAGER_DBUS (data);
113 get_current_state (self);
114 }
115
116 static void
117 bus_gotten (GObject *object,
118 GAsyncResult *result,
119 gpointer data)
120 {
121 CcDisplayConfigManagerDBus *self;
122 GDBusConnection *connection;
123 g_autoptr(GError) error = NULL;
124 g_autoptr(GDBusProxy) proxy = NULL;
125 g_autoptr(GVariant) variant = NULL;
126
127 connection = g_bus_get_finish (result, &error);
128 if (!connection)
129 {
130 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
131 {
132 _cc_display_config_manager_emit_changed (CC_DISPLAY_CONFIG_MANAGER (data));
133 g_warning ("Error obtaining DBus connection: %s", error->message);
134 }
135 return;
136 }
137
138 self = CC_DISPLAY_CONFIG_MANAGER_DBUS (data);
139 self->connection = connection;
140 self->monitors_changed_id =
141 g_dbus_connection_signal_subscribe (self->connection,
142 "org.gnome.Mutter.DisplayConfig",
143 "org.gnome.Mutter.DisplayConfig",
144 "MonitorsChanged",
145 "/org/gnome/Mutter/DisplayConfig",
146 NULL,
147 G_DBUS_SIGNAL_FLAGS_NONE,
148 monitors_changed,
149 self,
150 NULL);
151
152 proxy = g_dbus_proxy_new_sync (self->connection,
153 G_DBUS_PROXY_FLAGS_NONE,
154 NULL,
155 "org.gnome.Mutter.DisplayConfig",
156 "/org/gnome/Mutter/DisplayConfig",
157 "org.gnome.Mutter.DisplayConfig",
158 NULL,
159 &error);
160 if (!proxy)
161 {
162 g_warning ("Failed to create D-Bus proxy to \"org.gnome.Mutter.DisplayConfig\": %s",
163 error->message);
164 return;
165 }
166
167 variant = g_dbus_proxy_get_cached_property (proxy, "ApplyMonitorsConfigAllowed");
168 if (variant)
169 self->apply_allowed = g_variant_get_boolean (variant);
170 else
171 g_warning ("Missing property 'ApplyMonitorsConfigAllowed' on DisplayConfig API");
172
173 variant = g_dbus_proxy_get_cached_property (proxy, "NightLightSupported");
174 if (variant)
175 self->night_light_supported = g_variant_get_boolean (variant);
176 else
177 g_warning ("Missing property 'NightLightSupported' on DisplayConfig API");
178
179 get_current_state (self);
180 }
181
182 static void
183 cc_display_config_manager_dbus_init (CcDisplayConfigManagerDBus *self)
184 {
185 self->apply_allowed = TRUE;
186 self->night_light_supported = TRUE;
187 self->cancellable = g_cancellable_new ();
188 g_bus_get (G_BUS_TYPE_SESSION, self->cancellable, bus_gotten, self);
189 }
190
191 static void
192 cc_display_config_manager_dbus_finalize (GObject *object)
193 {
194 CcDisplayConfigManagerDBus *self = CC_DISPLAY_CONFIG_MANAGER_DBUS (object);
195
196 g_cancellable_cancel (self->cancellable);
197 g_clear_object (&self->cancellable);
198
199 if (self->monitors_changed_id && self->connection)
200 g_dbus_connection_signal_unsubscribe (self->connection,
201 self->monitors_changed_id);
202 g_clear_object (&self->connection);
203 g_clear_pointer (&self->current_state, g_variant_unref);
204
205 G_OBJECT_CLASS (cc_display_config_manager_dbus_parent_class)->finalize (object);
206 }
207
208 static gboolean
209 cc_display_config_manager_dbus_get_apply_allowed (CcDisplayConfigManager *pself)
210 {
211 CcDisplayConfigManagerDBus *self = CC_DISPLAY_CONFIG_MANAGER_DBUS (pself);
212
213 return self->apply_allowed;
214 }
215
216 static gboolean
217 cc_display_config_manager_dbus_get_night_light_supported (CcDisplayConfigManager *pself)
218 {
219 CcDisplayConfigManagerDBus *self = CC_DISPLAY_CONFIG_MANAGER_DBUS (pself);
220
221 return self->night_light_supported;
222 }
223
224 static void
225 cc_display_config_manager_dbus_class_init (CcDisplayConfigManagerDBusClass *klass)
226 {
227 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 CcDisplayConfigManagerClass *parent_class = CC_DISPLAY_CONFIG_MANAGER_CLASS (klass);
229
230 gobject_class->finalize = cc_display_config_manager_dbus_finalize;
231
232 parent_class->get_current = cc_display_config_manager_dbus_get_current;
233 parent_class->get_apply_allowed = cc_display_config_manager_dbus_get_apply_allowed;
234 parent_class->get_night_light_supported = cc_display_config_manager_dbus_get_night_light_supported;
235 }
236
237 CcDisplayConfigManager *
238 cc_display_config_manager_dbus_new (void)
239 {
240 return g_object_new (CC_TYPE_DISPLAY_CONFIG_MANAGER_DBUS, NULL);
241 }
242