Debian Codegraph @

libcolord-gtk/cd-window.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2012 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * SECTION:cd-window
24 * @short_description: Additional helper classes for working with GTK
25 *
26 * These functions are useful when GTK is being used alongside colord and
27 * are just provided for convenience.
28 *
29 * See also: #CdDevice
30 */
31
32 #include "config.h"
33
34 #include <gio/gio.h>
35 #include <glib.h>
36 #include <gtk/gtk.h>
37
38 #include <colord.h>
39
40 #include "cd-window.h"
41
42 static void cd_window_class_init (CdWindowClass *klass);
43 static void cd_window_init (CdWindow *window);
44 static void cd_window_finalize (GObject *object);
45
46 #define CD_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CD_TYPE_WINDOW, CdWindowPrivate))
47
48 /**
49 * CdWindowPrivate:
50 *
51 * Private #CdWindow data
52 **/
53 struct _CdWindowPrivate
54 {
55 CdClient *client;
56 CdDevice *device;
57 CdProfile *profile;
58 gchar *plug_name;
59 GtkWidget *widget;
60 guint device_changed_id;
61 };
62
63 enum {
64 SIGNAL_CHANGED,
65 SIGNAL_LAST
66 };
67
68 enum {
69 PROP_0,
70 PROP_PROFILE,
71 PROP_LAST
72 };
73
74 static guint signals [SIGNAL_LAST] = { 0 };
75
76 G_DEFINE_TYPE (CdWindow, cd_window, G_TYPE_OBJECT)
77
78 /**
79 * cd_window_error_quark:
80 *
81 * Return value: An error quark.
82 *
83 * Since: 0.1.20
84 **/
85 GQuark
86 cd_window_error_quark (void)
87 {
88 static GQuark quark = 0;
89 if (!quark) {
90 quark = g_quark_from_static_string ("cd_window_error");
91 }
92 return quark;
93 }
94
95 /**
96 * cd_window_get_last_profile:
97 * @window: a #CdWindow instance.
98 *
99 * Gets the color profile to use for this widget.
100 *
101 * Return value: (transfer none): a #CdProfile
102 *
103 * Since: 0.1.20
104 **/
105 CdProfile *
106 cd_window_get_last_profile (CdWindow *window)
107 {
108 g_return_val_if_fail (CD_IS_WINDOW (window), NULL);
109 return window->priv->profile;
110 }
111
112 typedef struct {
113 CdWindow *window;
114 GCancellable *cancellable;
115 GSimpleAsyncResult *res;
116 } CdWindowSetWidgetHelper;
117
118 /**
119 * cd_window_get_profile_finish:
120 * @window: a #CdWindow instance.
121 * @res: the #GAsyncResult
122 * @error: A #GError or %NULL
123 *
124 * Gets the result from the asynchronous function.
125 *
126 * Return value: (transfer full): a #CdProfile or %NULL
127 *
128 * Since: 0.1.20
129 **/
130 CdProfile *
131 cd_window_get_profile_finish (CdWindow *window,
132 GAsyncResult *res,
133 GError **error)
134 {
135 GSimpleAsyncResult *simple;
136
137 g_return_val_if_fail (CD_IS_WINDOW (window), NULL);
138 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), NULL);
139 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
140
141 simple = G_SIMPLE_ASYNC_RESULT (res);
142 if (g_simple_async_result_propagate_error (simple, error))
143 return NULL;
144
145 return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
146 }
147
148 static void
149 cd_window_import_free_helper (CdWindowSetWidgetHelper *helper)
150 {
151 if (helper->cancellable != NULL)
152 g_object_unref (helper->cancellable);
153 g_object_unref (helper->window);
154 g_object_unref (helper->res);
155 g_free (helper);
156 }
157
158 static void cd_window_get_profile_new_data (CdWindowSetWidgetHelper *helper);
159
160 static void
161 cd_window_get_profile_client_connect_cb (GObject *source,
162 GAsyncResult *res,
163 gpointer user_data)
164 {
165 CdClient *client = CD_CLIENT (source);
166 CdWindowSetWidgetHelper *helper = (CdWindowSetWidgetHelper *) user_data;
167 gboolean ret;
168 GError *error = NULL;
169
170 ret = cd_client_connect_finish (client, res, &error);
171 if (!ret) {
172 g_simple_async_result_set_error (helper->res,
173 CD_WINDOW_ERROR,
174 CD_WINDOW_ERROR_FAILED,
175 "failed to connect to colord: %s",
176 error->message);
177 g_simple_async_result_complete_in_idle (helper->res);
178 cd_window_import_free_helper (helper);
179 g_error_free (error);
180 return;
181 }
182 cd_window_get_profile_new_data (helper);
183 }
184
185 static void
186 cd_window_get_profile_device_connect_cb (GObject *source,
187 GAsyncResult *res,
188 gpointer user_data)
189 {
190 CdDevice *device = CD_DEVICE (source);
191 CdWindowSetWidgetHelper *helper = (CdWindowSetWidgetHelper *) user_data;
192 CdWindowPrivate *priv = helper->window->priv;
193 gboolean ret;
194 GError *error = NULL;
195
196 ret = cd_device_connect_finish (device, res, &error);
197 if (!ret) {
198 g_simple_async_result_set_error (helper->res,
199 CD_WINDOW_ERROR,
200 CD_WINDOW_ERROR_FAILED,
201 "failed to connect to device: %s",
202 error->message);
203 g_simple_async_result_complete_in_idle (helper->res);
204 cd_window_import_free_helper (helper);
205 g_error_free (error);
206 return;
207 }
208
209 /* get the default profile for the device */
210 priv->profile = cd_device_get_default_profile (priv->device);
211 if (priv->profile == NULL) {
212 g_simple_async_result_set_error (helper->res,
213 CD_WINDOW_ERROR,
214 CD_WINDOW_ERROR_FAILED,
215 "no default profile for device: %s",
216 priv->plug_name);
217 g_simple_async_result_complete_in_idle (helper->res);
218 cd_window_import_free_helper (helper);
219 return;
220 }
221
222 cd_window_get_profile_new_data (helper);
223 }
224
225 static void
226 cd_window_get_profile_profile_connect_cb (GObject *source,
227 GAsyncResult *res,
228 gpointer user_data)
229 {
230 CdProfile *profile = CD_PROFILE (source);
231 CdWindowSetWidgetHelper *helper = (CdWindowSetWidgetHelper *) user_data;
232 CdWindowPrivate *priv = helper->window->priv;
233 const gchar *filename;
234 gboolean ret;
235 GError *error = NULL;
236
237 ret = cd_profile_connect_finish (profile, res, &error);
238 if (!ret) {
239 g_simple_async_result_set_error (helper->res,
240 CD_WINDOW_ERROR,
241 CD_WINDOW_ERROR_FAILED,
242 "failed to connect to profile: %s",
243 error->message);
244 g_simple_async_result_complete_in_idle (helper->res);
245 cd_window_import_free_helper (helper);
246 g_error_free (error);
247 return;
248 }
249
250 /* get the filename of the profile */
251 filename = cd_profile_get_filename (priv->profile);
252 if (filename == NULL) {
253 g_simple_async_result_set_error (helper->res,
254 CD_WINDOW_ERROR,
255 CD_WINDOW_ERROR_FAILED,
256 "profile has no physical file, must be virtual");
257 g_simple_async_result_complete_in_idle (helper->res);
258 cd_window_import_free_helper (helper);
259 return;
260 }
261
262 /* SUCCESS! */
263 g_simple_async_result_set_op_res_gpointer (helper->res,
264 g_object_ref (priv->profile),
265 (GDestroyNotify) g_object_unref);
266 g_simple_async_result_complete_in_idle (helper->res);
267 cd_window_import_free_helper (helper);
268 }
269
270 static void
271 cd_window_get_profile_device_find_cb (GObject *source,
272 GAsyncResult *res,
273 gpointer user_data)
274 {
275 CdClient *client = CD_CLIENT (source);
276 CdWindowSetWidgetHelper *helper = (CdWindowSetWidgetHelper *) user_data;
277 CdWindowPrivate *priv = helper->window->priv;
278 GError *error = NULL;
279
280 priv->device = cd_client_find_device_by_property_finish (client,
281 res,
282 &error);
283 if (priv->device == NULL) {
284 g_simple_async_result_set_error (helper->res,
285 CD_WINDOW_ERROR,
286 CD_WINDOW_ERROR_FAILED,
287 "no device with that property: %s",
288 error->message);
289 g_simple_async_result_complete_in_idle (helper->res);
290 cd_window_import_free_helper (helper);
291 g_error_free (error);
292 return;
293 }
294 cd_window_get_profile_new_data (helper);
295 }
296
297 static void
298 cd_window_device_changed_cb (CdClient *client, CdDevice *device, CdWindow *window)
299 {
300 CdProfile *profile;
301
302 /* no device set yet */
303 if (window->priv->device == NULL)
304 return;
305
306 /* the same device */
307 if (!cd_device_equal (device, window->priv->device))
308 return;
309
310 /* get new default profile */
311 profile = cd_device_get_default_profile (window->priv->device);
312 if (profile == NULL && window->priv->profile == NULL)
313 return;
314 if (profile != NULL && cd_profile_equal (profile, window->priv->profile))
315 return;
316
317 /* replace profile instance and emit if changed */
318 g_set_object(&window->priv->profile, profile);
319 g_signal_emit (window, signals[SIGNAL_CHANGED], 0,
320 window->priv->profile);
321 }
322
323 static void
324 cd_window_get_profile_new_data (CdWindowSetWidgetHelper *helper)
325 {
326 CdWindowPrivate *priv = helper->window->priv;
327
328 /* connect to the daemon */
329 if (priv->client == NULL) {
330 priv->client = cd_client_new ();
331 priv->device_changed_id =
332 g_signal_connect (priv->client, "device-changed",
333 G_CALLBACK (cd_window_device_changed_cb),
334 helper->window);
335 cd_client_connect (priv->client,
336 helper->cancellable,
337 cd_window_get_profile_client_connect_cb,
338 helper);
339 goto out;
340 }
341
342 /* find the new device */
343 if (priv->device == NULL && priv->plug_name != NULL) {
344 cd_client_find_device_by_property (priv->client,
345 CD_DEVICE_METADATA_XRANDR_NAME,
346 priv->plug_name,
347 helper->cancellable,
348 cd_window_get_profile_device_find_cb,
349 helper);
350 goto out;
351 }
352
353 /* connect to the device */
354 if (priv->device != NULL && !cd_device_get_connected (priv->device)) {
355 cd_device_connect (priv->device,
356 helper->cancellable,
357 cd_window_get_profile_device_connect_cb,
358 helper);
359 goto out;
360 }
361
362 /* connect to the profile */
363 if (priv->profile != NULL && !cd_profile_get_connected (priv->profile)) {
364 cd_profile_connect (priv->profile,
365 helper->cancellable,
366 cd_window_get_profile_profile_connect_cb,
367 helper);
368 goto out;
369 }
370 out:
371 return;
372 }
373
374 static void
375 cd_window_update_widget_plug_name (CdWindow *window,
376 GtkWidget *widget)
377 {
378 CdWindowPrivate *priv = window->priv;
379 gchar *plug_name;
380
381 #ifndef BUILD_COLORD_GTK4
382 GdkScreen *screen;
383 GdkWindow *gdk_window;
384 gint monitor_num;
385
386 /* use the largest bounding area */
387 gdk_window = gtk_widget_get_window (widget);
388 screen = gdk_window_get_screen (gdk_window);
389
390 monitor_num = gdk_screen_get_monitor_at_window (screen,
391 gdk_window);
392 plug_name = gdk_screen_get_monitor_plug_name (screen, monitor_num);
393 #else
394 GdkSurface *surface;
395 GdkDisplay *display;
396 GdkMonitor *monitor;
397 GtkNative *native;
398
399 native = gtk_widget_get_native (widget);
400 surface = gtk_native_get_surface (native);
401 display = gtk_widget_get_display (widget);
402 monitor = gdk_display_get_monitor_at_surface (display, surface);
403 plug_name = g_strdup (gdk_monitor_get_connector (monitor));
404 #endif
405
406 /* ignoring MAP as plug_name has not changed */
407 if (g_strcmp0 (plug_name, priv->plug_name) == 0) {
408 g_free (plug_name);
409 return;
410 }
411
412 /* refresh data */
413 g_free (priv->plug_name);
414 priv->plug_name = plug_name;
415 if (priv->device != NULL) {
416 g_object_unref (priv->device);
417 priv->device = NULL;
418 }
419 if (priv->profile != NULL) {
420 g_object_unref (priv->profile);
421 priv->profile = NULL;
422 }
423 }
424
425 /**
426 * cd_window_get_profile:
427 * @window: a #CdWindow instance.
428 * @widget: a #GtkWidget
429 * @cancellable: a #GCancellable or %NULL
430 * @callback: the function to run on completion
431 * @user_data: the data to pass to @callback
432 *
433 * Gets the screen profile that should be used for the widget,
434 * which corresponds to the screen output the widget most covers.
435 *
436 * This method should be called when the widget has mapped, i.e.
437 * g_signal_connect (dialog, "map", G_CALLBACK (map_cb), priv);
438 *
439 * Note, the returned profile from cd_client_get_profile_for_widget_finish()
440 * has already been connected to, as is ready to use.
441 *
442 * Since: 0.1.20
443 **/
444 void
445 cd_window_get_profile (CdWindow *window,
446 GtkWidget *widget,
447 GCancellable *cancellable,
448 GAsyncReadyCallback callback,
449 gpointer user_data)
450 {
451 CdWindowSetWidgetHelper *helper;
452
453 g_return_if_fail (CD_IS_WINDOW (window));
454 g_return_if_fail (GTK_IS_WIDGET (widget));
455 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
456
457 helper = g_new0 (CdWindowSetWidgetHelper, 1);
458 helper->window = g_object_ref (window);
459 helper->res = g_simple_async_result_new (G_OBJECT (window),
460 callback,
461 user_data,
462 cd_window_get_profile);
463 if (cancellable != NULL)
464 helper->cancellable = g_object_ref (cancellable);
465
466 /* intially set the plug name */
467 window->priv->widget = g_object_ref (widget);
468 cd_window_update_widget_plug_name (window, widget);
469 cd_window_get_profile_new_data (helper);
470 }
471
472 /**********************************************************************/
473
474 /*
475 * cd_window_get_property:
476 */
477 static void
478 cd_window_get_property (GObject *object,
479 guint prop_id,
480 GValue *value,
481 GParamSpec *pspec)
482 {
483 CdWindow *window = CD_WINDOW (object);
484
485 switch (prop_id) {
486 case PROP_PROFILE:
487 g_value_set_object (value, window->priv->profile);
488 break;
489 default:
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
491 break;
492 }
493 }
494
495 /*
496 * cd_window_class_init:
497 */
498 static void
499 cd_window_class_init (CdWindowClass *klass)
500 {
501 GObjectClass *object_class = G_OBJECT_CLASS (klass);
502
503 object_class->get_property = cd_window_get_property;
504 object_class->finalize = cd_window_finalize;
505
506 /**
507 * CdWindow:profile:
508 *
509 * The window profile.
510 *
511 * Since: 0.1.20
512 */
513 g_object_class_install_property (object_class,
514 PROP_PROFILE,
515 g_param_spec_string ("Profile",
516 "Color profile",
517 NULL,
518 NULL,
519 G_PARAM_READABLE));
520
521 /**
522 * CdWindow::changed:
523 * @window: the #CdDevice instance that emitted the signal
524 * @profile: the #CdProfile
525 *
526 * The ::changed signal is emitted when the device profile has
527 * changed. The #CdProfile that is referenced in the callback
528 * has not been connected to, and you will need to call
529 * cd_profile_connect() if the ICC filename is required.
530 *
531 * Since: 0.1.20
532 **/
533 signals [SIGNAL_CHANGED] =
534 g_signal_new ("changed",
535 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
536 G_STRUCT_OFFSET (CdWindowClass, changed),
537 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
538 G_TYPE_NONE, 1, CD_TYPE_PROFILE);
539
540 g_type_class_add_private (klass, sizeof (CdWindowPrivate));
541 }
542
543 /*
544 * cd_window_init:
545 */
546 static void
547 cd_window_init (CdWindow *window)
548 {
549 window->priv = CD_WINDOW_GET_PRIVATE (window);
550
551 /* ensure the remote errors are registered */
552 cd_window_error_quark ();
553 }
554
555 /*
556 * cd_window_finalize:
557 */
558 static void
559 cd_window_finalize (GObject *object)
560 {
561 CdWindow *window = CD_WINDOW (object);
562
563 g_return_if_fail (CD_IS_WINDOW (object));
564
565 if (window->priv->client != NULL) {
566 g_signal_handler_disconnect (window->priv->client,
567 window->priv->device_changed_id);
568 g_object_unref (window->priv->client);
569 }
570 if (window->priv->device != NULL)
571 g_object_unref (window->priv->device);
572 if (window->priv->profile != NULL)
573 g_object_unref (window->priv->profile);
574 g_free (window->priv->plug_name);
575
576 G_OBJECT_CLASS (cd_window_parent_class)->finalize (object);
577 }
578
579 /**
580 * cd_window_new:
581 *
582 * Creates a new #CdWindow object.
583 *
584 * Return value: a new CdWindow object.
585 *
586 * Since: 0.1.20
587 **/
588 CdWindow *
589 cd_window_new (void)
590 {
591 CdWindow *window;
592 window = g_object_new (CD_TYPE_WINDOW, NULL);
593 return CD_WINDOW (window);
594 }