Debian Codegraph @

src/cd-main.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2014 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <gio/gio.h>
26 #ifdef __unix__
27 #include <gio/gunixfdlist.h>
28 #endif
29 #include <glib/gi18n.h>
30 #include <locale.h>
31
32 #ifdef HAVE_SYSTEMD
33 #include <systemd/sd-login.h>
34 #endif
35
36 #include "cd-common.h"
37 #include "cd-debug.h"
38 #include "cd-device-array.h"
39 #include "cd-device-db.h"
40 #include "cd-device.h"
41 #include "cd-mapping-db.h"
42 #include "cd-plugin.h"
43 #include "cd-profile-array.h"
44 #include "cd-profile-db.h"
45 #include "cd-profile.h"
46 #include "cd-icc-store.h"
47 #include "cd-sensor-client.h"
48
49 #include "colord-resources.h"
50
51 typedef struct {
52 GDBusConnection *connection;
53 GDBusNodeInfo *introspection_daemon;
54 GDBusNodeInfo *introspection_device;
55 GDBusNodeInfo *introspection_profile;
56 GDBusNodeInfo *introspection_sensor;
57 CdDeviceArray *devices_array;
58 CdProfileArray *profiles_array;
59 CdIccStore *icc_store;
60 CdMappingDb *mapping_db;
61 CdDeviceDb *device_db;
62 CdProfileDb *profile_db;
63 CdSensorClient *sensor_client;
64 GPtrArray *sensors;
65 GPtrArray *plugins;
66 GMainLoop *loop;
67 gboolean create_dummy_sensor;
68 gboolean always_use_xrandr_name;
69 gchar *system_vendor;
70 gchar *system_model;
71 } CdMainPrivate;
72
73 static void
74 cd_main_profile_removed (CdMainPrivate *priv, CdProfile *profile)
75 {
76 CdDevice *device_tmp;
77 gboolean ret;
78 guint i;
79 g_autofree gchar *object_path_tmp = NULL;
80 g_autoptr(GPtrArray) devices = NULL;
81
82 /* remove from the array before emitting */
83 object_path_tmp = g_strdup (cd_profile_get_object_path (profile));
84 cd_profile_array_remove (priv->profiles_array, profile);
85
86 /* try to remove this profile from all devices */
87 devices = cd_device_array_get_array (priv->devices_array);
88 for (i = 0; i < devices->len; i++) {
89 device_tmp = g_ptr_array_index (devices, i);
90 ret = cd_device_remove_profile (device_tmp,
91 object_path_tmp,
92 NULL);
93 if (ret) {
94 g_info ("Automatic remove of %s from %s",
95 cd_profile_get_id (profile),
96 cd_device_get_id (device_tmp));
97 g_debug ("CdMain: automatically removing %s from %s as removed",
98 object_path_tmp,
99 cd_device_get_object_path (device_tmp));
100 }
101 }
102
103 /* emit signal */
104 g_debug ("CdMain: Emitting ProfileRemoved(%s)", object_path_tmp);
105 g_info ("Profile removed: %s", cd_profile_get_id (profile));
106 g_dbus_connection_emit_signal (priv->connection,
107 NULL,
108 COLORD_DBUS_PATH,
109 COLORD_DBUS_INTERFACE,
110 "ProfileRemoved",
111 g_variant_new ("(o)",
112 object_path_tmp),
113 NULL);
114 }
115
116 static void
117 cd_main_profile_invalidate_cb (CdProfile *profile,
118 gpointer user_data)
119 {
120 CdMainPrivate *priv = (CdMainPrivate *) user_data;
121
122 g_debug ("CdMain: profile '%s' invalidated",
123 cd_profile_get_id (profile));
124 cd_main_profile_removed (priv, profile);
125 }
126
127 static void
128 cd_main_device_removed (CdMainPrivate *priv, CdDevice *device)
129 {
130 GError *error = NULL;
131 gboolean ret;
132 g_autofree gchar *object_path_tmp = NULL;
133
134 /* remove from the array before emitting */
135 object_path_tmp = g_strdup (cd_device_get_object_path (device));
136 g_debug ("CdMain: Removing device %s", object_path_tmp);
137 cd_device_array_remove (priv->devices_array, device);
138
139 /* remove from the device database */
140 if (cd_device_get_scope (device) == CD_OBJECT_SCOPE_DISK) {
141 ret = cd_device_db_remove (priv->device_db,
142 cd_device_get_id (device),
143 &error);
144 if (!ret) {
145 g_warning ("CdMain: failed to remove device %s from db: %s",
146 cd_device_get_object_path (device),
147 error->message);
148 g_clear_error (&error);
149 }
150 }
151
152 /* emit signal */
153 g_debug ("CdMain: Emitting DeviceRemoved(%s)", object_path_tmp);
154 g_info ("device removed: %s", cd_device_get_id (device));
155 g_dbus_connection_emit_signal (priv->connection,
156 NULL,
157 COLORD_DBUS_PATH,
158 COLORD_DBUS_INTERFACE,
159 "DeviceRemoved",
160 g_variant_new ("(o)",
161 object_path_tmp),
162 &error);
163 }
164
165 static void
166 cd_main_device_invalidate_cb (CdDevice *device,
167 gpointer user_data)
168 {
169 CdMainPrivate *priv = (CdMainPrivate *) user_data;
170
171 g_debug ("CdMain: device '%s' invalidated",
172 cd_device_get_id (device));
173 cd_main_device_removed (priv, device);
174 }
175
176 static gboolean
177 cd_main_add_profile (CdMainPrivate *priv,
178 CdProfile *profile,
179 GError **error)
180 {
181 /* add */
182 cd_profile_array_add (priv->profiles_array, profile);
183 g_debug ("CdMain: Adding profile %s", cd_profile_get_object_path (profile));
184
185 /* profile is no longer valid */
186 g_signal_connect (profile, "invalidate",
187 G_CALLBACK (cd_main_profile_invalidate_cb),
188 priv);
189 return TRUE;
190 }
191
192 static CdProfile *
193 cd_main_create_profile (CdMainPrivate *priv,
194 const gchar *sender,
195 const gchar *profile_id,
196 guint owner,
197 CdObjectScope scope,
198 GError **error)
199 {
200 g_autoptr(CdProfile) profile_tmp = NULL;
201
202 g_assert (priv->connection != NULL);
203
204 /* create an object */
205 profile_tmp = cd_profile_new ();
206 cd_profile_set_owner (profile_tmp, owner);
207 cd_profile_set_id (profile_tmp, profile_id);
208 cd_profile_set_scope (profile_tmp, scope);
209
210 /* add the profile */
211 if (!cd_main_add_profile (priv, profile_tmp, error))
212 return NULL;
213
214 /* different persistent scope */
215 switch (scope) {
216 case CD_OBJECT_SCOPE_NORMAL:
217 g_debug ("CdMain: normal profile");
218 break;
219 case CD_OBJECT_SCOPE_TEMP:
220 g_debug ("CdMain: temporary profile");
221 /* setup DBus watcher */
222 cd_profile_watch_sender (profile_tmp, sender);
223 break;
224 case CD_OBJECT_SCOPE_DISK:
225 g_debug ("CdMain: persistent profile");
226 g_set_error_literal (error,
227 CD_CLIENT_ERROR,
228 CD_CLIENT_ERROR_NOT_SUPPORTED,
229 "persistent profiles are no yet supported");
230 return NULL;
231 default:
232 g_warning ("CdMain: Unsupported scope kind: %u", scope);
233 g_set_error (error,
234 CD_CLIENT_ERROR,
235 CD_CLIENT_ERROR_NOT_SUPPORTED,
236 "Unsupported scope kind: %u", scope);
237 return NULL;
238 }
239
240 /* success */
241 return g_object_ref (profile_tmp);
242 }
243
244 static gboolean
245 cd_main_auto_add_from_md (CdMainPrivate *priv,
246 CdDevice *device,
247 CdProfile *profile)
248 {
249 const gchar *device_id;
250 const gchar *profile_id;
251 const gchar *tmp;
252 const gchar **warnings;
253 gboolean ret;
254 guint64 timestamp;
255 g_autoptr(GError) error = NULL;
256
257 /* check device and profile hasn't been manually removed */
258 profile_id = cd_profile_get_id (profile);
259 device_id = cd_device_get_id (device);
260 timestamp = cd_mapping_db_get_timestamp (priv->mapping_db,
261 device_id,
262 profile_id,
263 &error);
264 if (timestamp == G_MAXUINT64) {
265 g_debug ("CdMain: no existing mapping found: %s",
266 error->message);
267 g_clear_error (&error);
268 } else if (timestamp == 0) {
269 g_debug ("CdMain: Not doing MD add %s to %s due to removal",
270 profile_id, device_id);
271 return FALSE;
272 }
273
274 /* if the auto-EDID profile has warnings then do not add this */
275 tmp = cd_profile_get_metadata_item (profile, CD_PROFILE_METADATA_DATA_SOURCE);
276 if (g_strcmp0 (tmp, CD_PROFILE_METADATA_DATA_SOURCE_EDID) == 0) {
277 warnings = cd_profile_get_warnings (profile);
278 if (warnings != NULL && g_strv_length ((gchar **) warnings) > 0) {
279 g_debug ("CdMain: NOT MD add %s to %s as profile has warnings",
280 profile_id, device_id);
281 return FALSE;
282 }
283 }
284
285 /* auto-add soft relationship */
286 g_debug ("CdMain: Automatically MD add %s to %s", profile_id, device_id);
287 g_info ("Automatic metadata add %s to %s", profile_id, device_id);
288 ret = cd_device_add_profile (device,
289 CD_DEVICE_RELATION_SOFT,
290 cd_profile_get_object_path (profile),
291 g_get_real_time (),
292 &error);
293 if (!ret) {
294 g_debug ("CdMain: failed to assign, non-fatal: %s",
295 error->message);
296 return FALSE;
297 }
298 return TRUE;
299 }
300
301 static gboolean
302 cd_main_auto_add_from_db (CdMainPrivate *priv,
303 CdDevice *device,
304 CdProfile *profile)
305 {
306 gboolean ret;
307 guint64 timestamp;
308 g_autoptr(GError) error = NULL;
309
310 g_debug ("CdMain: Automatically DB add %s to %s",
311 cd_profile_get_id (profile),
312 cd_device_get_object_path (device));
313 g_info ("Automatic database add %s to %s",
314 cd_profile_get_id (profile),
315 cd_device_get_id (device));
316 timestamp = cd_mapping_db_get_timestamp (priv->mapping_db,
317 cd_device_get_id (device),
318 cd_profile_get_id (profile),
319 &error);
320 if (timestamp == G_MAXUINT64) {
321 g_debug ("CdMain: failed to assign, non-fatal: %s",
322 error->message);
323 return FALSE;
324 }
325 ret = cd_device_add_profile (device,
326 CD_DEVICE_RELATION_HARD,
327 cd_profile_get_object_path (profile),
328 timestamp,
329 &error);
330 if (!ret) {
331 g_debug ("CdMain: failed to assign, non-fatal: %s",
332 error->message);
333 return FALSE;
334 }
335 return TRUE;
336 }
337
338 static void
339 cd_main_device_auto_add_from_md (CdMainPrivate *priv,
340 CdDevice *device)
341 {
342 CdProfile *profile_tmp;
343 guint i;
344 g_autoptr(GPtrArray) array = NULL;
345
346 /* get all the profiles, and check to see if any of them contain
347 * MAPPING_device_id that matches the device */
348 array = cd_profile_array_get_by_metadata (priv->profiles_array,
349 CD_PROFILE_METADATA_MAPPING_DEVICE_ID,
350 cd_device_get_id (device));
351 if (array == NULL)
352 return;
353 for (i = 0; i < array->len; i++) {
354 profile_tmp = g_ptr_array_index (array, i);
355 cd_main_auto_add_from_md (priv, device, profile_tmp);
356 }
357 }
358
359 static void
360 cd_main_device_auto_add_from_db (CdMainPrivate *priv, CdDevice *device)
361 {
362 const gchar *object_id_tmp;
363 guint64 timestamp;
364 guint i;
365 g_autoptr(GError) error = NULL;
366 g_autoptr(GPtrArray) array = NULL;
367
368 /* get data */
369 array = cd_mapping_db_get_profiles (priv->mapping_db,
370 cd_device_get_id (device),
371 &error);
372 if (array == NULL) {
373 g_warning ("CdMain: failed to get profiles for device from db: %s",
374 error->message);
375 return;
376 }
377
378 /* try to add them */
379 for (i = 0; i < array->len; i++) {
380 g_autoptr(CdProfile) profile_tmp = NULL;
381 object_id_tmp = g_ptr_array_index (array, i);
382
383 /* ensure timestamp is still valid */
384 timestamp = cd_mapping_db_get_timestamp (priv->mapping_db,
385 cd_device_get_id (device),
386 object_id_tmp,
387 &error);
388 if (timestamp == G_MAXUINT64) {
389 g_warning ("CdMain: failed to get timestamp: %s",
390 error->message);
391 g_clear_error (&error);
392 continue;
393 }
394 if (timestamp == 0) {
395 g_debug ("CdMain: timestamp zero for %s and %s",
396 cd_device_get_id (device),
397 object_id_tmp);
398 continue;
399 }
400
401 profile_tmp = cd_profile_array_get_by_id_owner (priv->profiles_array,
402 object_id_tmp,
403 cd_device_get_owner (device));
404 if (profile_tmp == NULL) {
405 g_debug ("CdMain: profile %s with owner %u is not (yet) available",
406 object_id_tmp, cd_device_get_owner (device));
407 continue;
408 }
409
410 /* does the profile have the correct device metadata */
411 cd_main_auto_add_from_db (priv, device, profile_tmp);
412 }
413 }
414
415 static gboolean
416 cd_main_device_register_on_bus (CdMainPrivate *priv,
417 CdDevice *device,
418 GError **error)
419 {
420 /* register object */
421 if (!cd_device_register_object (device,
422 priv->connection,
423 priv->introspection_device->interfaces[0],
424 error))
425 return FALSE;
426
427 /* emit signal */
428 g_debug ("CdMain: Emitting DeviceAdded(%s)",
429 cd_device_get_object_path (device));
430 g_info ("Device added: %s", cd_device_get_id (device));
431 g_dbus_connection_emit_signal (priv->connection,
432 NULL,
433 COLORD_DBUS_PATH,
434 COLORD_DBUS_INTERFACE,
435 "DeviceAdded",
436 g_variant_new ("(o)",
437 cd_device_get_object_path (device)),
438 NULL);
439 return TRUE;
440 }
441
442 static gboolean
443 cd_main_device_add (CdMainPrivate *priv,
444 CdDevice *device,
445 const gchar *sender,
446 GError **error)
447 {
448 gboolean ret;
449 CdObjectScope scope;
450
451 /* create an object */
452 g_debug ("CdMain: Adding device %s", cd_device_get_object_path (device));
453
454 /* different persistent scope */
455 scope = cd_device_get_scope (device);
456 if (scope == CD_OBJECT_SCOPE_DISK && sender != NULL) {
457 g_debug ("CdMain: persistent device");
458
459 /* add to the device database */
460 ret = cd_device_db_add (priv->device_db,
461 cd_device_get_id (device),
462 error);
463 if (!ret)
464 return FALSE;
465 }
466
467 /* profile is no longer valid */
468 if (scope == CD_OBJECT_SCOPE_TEMP) {
469 g_signal_connect (device, "invalidate",
470 G_CALLBACK (cd_main_device_invalidate_cb),
471 priv);
472 }
473
474 /* add to array */
475 cd_device_array_add (priv->devices_array, device);
476
477 /* auto add profiles from the database and metadata */
478 cd_main_device_auto_add_from_db (priv, device);
479 cd_main_device_auto_add_from_md (priv, device);
480 return TRUE;
481 }
482
483 static gchar *
484 cd_main_get_seat_for_process (guint pid)
485 {
486 gchar *seat = NULL;
487 #ifdef HAVE_SYSTEMD
488 char *sd_seat = NULL;
489 char *sd_session = NULL;
490 gint rc;
491
492 /* get session the process belongs to */
493 rc = sd_pid_get_session (pid, &sd_session);
494 if (rc < 0) {
495 g_debug ("failed to get session [pid %u]: %s",
496 pid, strerror (-rc));
497 goto out;
498 }
499
500 /* get the seat the session is on */
501 rc = sd_session_get_seat (sd_session, &sd_seat);
502 if (rc < 0) {
503 g_debug ("failed to get seat for session %s [pid %u]: %s",
504 sd_session, pid, strerror (-rc));
505 goto out;
506 }
507 seat = g_strdup (sd_seat);
508 out:
509 free (sd_seat);
510 free (sd_session);
511 #endif
512 return seat;
513 }
514
515 static CdDevice *
516 cd_main_create_device (CdMainPrivate *priv,
517 const gchar *sender,
518 const gchar *device_id,
519 guint owner,
520 guint process,
521 CdObjectScope scope,
522 CdDeviceMode mode,
523 GError **error)
524 {
525 g_autofree gchar *seat = NULL;
526 g_autoptr(CdDevice) device_tmp = NULL;
527
528 g_assert (priv->connection != NULL);
529
530 /* get the seat of the process that is creating the device */
531 seat = cd_main_get_seat_for_process (process);
532
533 /* create an object */
534 device_tmp = cd_device_new ();
535 cd_device_set_owner (device_tmp, owner);
536 cd_device_set_id (device_tmp, device_id);
537 cd_device_set_scope (device_tmp, scope);
538 cd_device_set_mode (device_tmp, mode);
539 cd_device_set_seat (device_tmp, seat);
540 if (!cd_main_device_add (priv, device_tmp, sender, error))
541 return NULL;
542
543 /* setup DBus watcher */
544 if (sender != NULL && scope == CD_OBJECT_SCOPE_TEMP) {
545 g_debug ("temporary device");
546 cd_device_watch_sender (device_tmp, sender);
547 }
548
549 /* success */
550 return g_object_ref (device_tmp);
551 }
552
553 static GVariant *
554 cd_main_device_array_to_variant (GPtrArray *array, guint uid)
555 {
556 CdDevice *device;
557 guint i;
558 guint length = 0;
559 g_autofree GVariant **variant_array = NULL;
560
561 /* copy the object paths */
562 variant_array = g_new0 (GVariant *, array->len + 1);
563 for (i = 0; i < array->len; i++) {
564 device = g_ptr_array_index (array, i);
565
566 /* only show devices created by root and the calling
567 * user, but if called *by* root return all devices
568 * from all users */
569 if (uid != 0) {
570 if (cd_device_get_owner (device) != 0 &&
571 cd_device_get_owner (device) != uid)
572 continue;
573 }
574
575 variant_array[length] = g_variant_new_object_path (
576 cd_device_get_object_path (device));
577 length++;
578 }
579
580 /* format the value */
581 return g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH,
582 variant_array,
583 length);
584 }
585
586 static GVariant *
587 cd_main_profile_array_to_variant (GPtrArray *array)
588 {
589 CdProfile *profile;
590 guint i;
591 guint length = 0;
592 g_autofree GVariant **variant_array = NULL;
593
594 /* copy the object paths */
595 variant_array = g_new0 (GVariant *, array->len + 1);
596 for (i = 0; i < array->len; i++) {
597 profile = g_ptr_array_index (array, i);
598 variant_array[length] = g_variant_new_object_path (
599 cd_profile_get_object_path (profile));
600 length++;
601 }
602
603 /* format the value */
604 return g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH,
605 variant_array,
606 length);
607 }
608
609 static GVariant *
610 cd_main_sensor_array_to_variant (GPtrArray *array)
611 {
612 CdSensor *sensor;
613 guint i;
614 guint length = 0;
615 g_autofree GVariant **variant_array = NULL;
616
617 /* copy the object paths */
618 variant_array = g_new0 (GVariant *, array->len + 1);
619 for (i = 0; i < array->len; i++) {
620 sensor = g_ptr_array_index (array, i);
621 variant_array[length] = g_variant_new_object_path (
622 cd_sensor_get_object_path (sensor));
623 length++;
624 }
625
626 /* format the value */
627 return g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH,
628 variant_array,
629 length);
630 }
631
632 static void
633 cd_main_profile_auto_add_from_db (CdMainPrivate *priv,
634 CdProfile *profile)
635 {
636 guint i;
637 g_autoptr(GError) error = NULL;
638 g_autoptr(GPtrArray) array = NULL;
639
640 /* get data */
641 array = cd_mapping_db_get_devices (priv->mapping_db,
642 cd_profile_get_id (profile),
643 &error);
644 if (array == NULL) {
645 g_warning ("CdMain: failed to get profiles for device from db: %s",
646 error->message);
647 return;
648 }
649
650 /* nothing set */
651 if (array->len == 0) {
652 g_debug ("no matched device data for profile %s",
653 cd_profile_get_id (profile));
654 return;
655 }
656
657 /* try to add them */
658 for (i = 0; i < array->len; i++) {
659 g_autoptr(CdDevice) device_tmp = NULL;
660 const gchar *device_id_tmp;
661
662 device_id_tmp = g_ptr_array_index (array, i);
663 device_tmp = cd_device_array_get_by_id_owner (priv->devices_array,
664 device_id_tmp,
665 cd_profile_get_owner (profile),
666 CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
667 if (device_tmp == NULL)
668 continue;
669
670 /* hard add */
671 cd_main_auto_add_from_db (priv, device_tmp, profile);
672 }
673 }
674
675 static void
676 cd_main_profile_auto_add_from_md (CdMainPrivate *priv,
677 CdProfile *profile)
678 {
679 g_autoptr(CdDevice) device = NULL;
680 const gchar *device_id;
681
682 /* does the device exists that matches the md */
683 device_id = cd_profile_get_metadata_item (profile,
684 CD_PROFILE_METADATA_MAPPING_DEVICE_ID);
685 if (device_id == NULL)
686 return;
687 device = cd_device_array_get_by_id_owner (priv->devices_array,
688 device_id,
689 cd_profile_get_owner (profile),
690 CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
691 if (device == NULL)
692 return;
693 cd_main_auto_add_from_md (priv, device, profile);
694 }
695
696 typedef enum {
697 CD_LOGGING_FLAG_NONE = 0,
698 CD_LOGGING_FLAG_SYSLOG = 1,
699 CD_LOGGING_FLAG_LAST
700 } CdLoggingFlags;
701
702 static gboolean
703 cd_main_profile_register_on_bus (CdMainPrivate *priv,
704 CdProfile *profile,
705 CdLoggingFlags logging,
706 GError **error)
707 {
708 /* register object */
709 if (!cd_profile_register_object (profile,
710 priv->connection,
711 priv->introspection_profile->interfaces[0],
712 error))
713 return FALSE;
714
715 /* emit signal */
716 g_debug ("CdMain: Emitting ProfileAdded(%s)",
717 cd_profile_get_object_path (profile));
718 if ((logging & CD_LOGGING_FLAG_SYSLOG) > 0)
719 g_info ("Profile added: %s", cd_profile_get_id (profile));
720 g_dbus_connection_emit_signal (priv->connection,
721 NULL,
722 COLORD_DBUS_PATH,
723 COLORD_DBUS_INTERFACE,
724 "ProfileAdded",
725 g_variant_new ("(o)",
726 cd_profile_get_object_path (profile)),
727 NULL);
728 return TRUE;
729 }
730
731 static CdProfile *
732 cd_main_get_standard_space_metadata (CdMainPrivate *priv,
733 const gchar *standard_space)
734 {
735 CdProfile *profile_best = NULL;
736 CdProfile *profile_tmp;
737 guint i;
738 guint score_best = 0;
739 guint score_tmp;
740 g_autoptr(GPtrArray) array = NULL;
741
742 /* get all the profiles with this metadata */
743 array = cd_profile_array_get_by_metadata (priv->profiles_array,
744 CD_PROFILE_METADATA_STANDARD_SPACE,
745 standard_space);
746
747 /* use the profile with the largest score */
748 for (i = 0; i < array->len; i++) {
749 profile_tmp = g_ptr_array_index (array, i);
750 score_tmp = cd_profile_get_score (profile_tmp);
751 if (score_tmp > score_best) {
752 score_best = score_tmp;
753 profile_best = profile_tmp;
754 }
755 }
756 if (profile_best == NULL)
757 return NULL;
758
759 /* success */
760 return g_object_ref (profile_best);
761 }
762
763 static gchar *
764 cd_main_get_display_fallback_id (GVariant *dict)
765 {
766 const gchar *prop_key;
767 const gchar *prop_value;
768 gchar *device_id = NULL;
769 GVariantIter *iter;
770
771 iter = g_variant_iter_new (dict);
772 while (g_variant_iter_next (iter, "{&s&s}",
773 &prop_key, &prop_value)) {
774 if (g_strcmp0 (prop_key, CD_DEVICE_METADATA_XRANDR_NAME) != 0)
775 continue;
776 if (prop_value == NULL || prop_value[0] == '\0')
777 continue;
778 device_id = g_strdup_printf ("xrandr-%s", prop_value);
779 break;
780 }
781 g_variant_iter_free (iter);
782 return device_id;
783 }
784
785 static gchar *
786 cd_main_get_cmdline_for_pid (guint pid)
787 {
788 gboolean ret;
789 gchar *cmdline = NULL;
790 gsize len = 0;
791 guint i;
792 g_autoptr(GError) error = NULL;
793 g_autofree gchar *proc_path = NULL;
794
795 /* just read the link */
796 proc_path = g_strdup_printf ("/proc/%u/cmdline", pid);
797 ret = g_file_get_contents (proc_path, &cmdline, &len, &error);
798 if (!ret) {
799 g_debug ("CdMain: failed to read %s: %s",
800 proc_path, error->message);
801 return NULL;
802 }
803 if (len == 0) {
804 g_debug ("CdMain: failed to read %s", proc_path);
805 g_free (cmdline);
806 return NULL;
807 }
808
809 /* turn all the \0's into spaces */
810 for (i = 0; i < len; i++) {
811 if (cmdline[i] == '\0')
812 cmdline[i] = ' ';
813 }
814 return cmdline;
815 }
816
817 static void
818 cd_main_daemon_method_call (GDBusConnection *connection, const gchar *sender,
819 const gchar *object_path, const gchar *interface_name,
820 const gchar *method_name, GVariant *parameters,
821 GDBusMethodInvocation *invocation, gpointer user_data)
822 {
823 CdDeviceKind device_kind;
824 CdMainPrivate *priv = (CdMainPrivate *) user_data;
825 CdObjectScope scope;
826 GVariant *tuple = NULL;
827 GVariant *value = NULL;
828 const gchar *device_id = NULL;
829 const gchar *metadata_key = NULL;
830 const gchar *metadata_value = NULL;
831 const gchar *prop_key;
832 const gchar *prop_value;
833 const gchar *scope_tmp = NULL;
834 gboolean register_on_bus = TRUE;
835 gboolean ret;
836 guint i;
837 guint pid;
838 guint uid;
839 g_autoptr(GError) error = NULL;
840 g_autofree gchar *cmdline = NULL;
841 g_autofree gchar *device_id_fallback = NULL;
842 g_autofree gchar *filename = NULL;
843 g_autoptr(CdDevice) device = NULL;
844 g_autoptr(CdProfile) profile = NULL;
845 g_autoptr(GPtrArray) array = NULL;
846 g_autoptr(GVariantIter) iter = NULL;
847 g_autoptr(GVariant) dict = NULL;
848
849 /* get the owner of the message */
850 uid = cd_main_get_sender_uid (connection, sender, &error);
851 if (uid == G_MAXUINT) {
852 g_dbus_method_invocation_return_error (invocation,
853 CD_CLIENT_ERROR,
854 CD_CLIENT_ERROR_INTERNAL,
855 "failed to get owner: %s",
856 error->message);
857 return;
858 }
859
860 /* return 'as' */
861 if (g_strcmp0 (method_name, "GetDevices") == 0) {
862
863 g_debug ("CdMain: %s:GetDevices()", sender);
864
865 /* format the value */
866 array = cd_device_array_get_array (priv->devices_array);
867 value = cd_main_device_array_to_variant (array, uid);
868 tuple = g_variant_new_tuple (&value, 1);
869 g_dbus_method_invocation_return_value (invocation, tuple);
870 return;
871 }
872
873 /* return 'as' */
874 if (g_strcmp0 (method_name, "GetSensors") == 0) {
875
876 g_debug ("CdMain: %s:GetSensors()", sender);
877
878 /* format the value */
879 value = cd_main_sensor_array_to_variant (priv->sensors);
880 tuple = g_variant_new_tuple (&value, 1);
881 g_dbus_method_invocation_return_value (invocation, tuple);
882 return;
883 }
884
885 /* return 'as' */
886 if (g_strcmp0 (method_name, "GetDevicesByKind") == 0) {
887
888 /* get all the devices that match this type */
889 g_variant_get (parameters, "(&s)", &device_id);
890 g_debug ("CdMain: %s:GetDevicesByKind(%s)",
891 sender, device_id);
892 device_kind = cd_device_kind_from_string (device_id);
893 if (device_kind == CD_DEVICE_KIND_UNKNOWN) {
894 g_dbus_method_invocation_return_error (invocation,
895 CD_CLIENT_ERROR,
896 CD_CLIENT_ERROR_INPUT_INVALID,
897 "device kind %s not recognised",
898 device_id);
899 return;
900 }
901 array = cd_device_array_get_by_kind (priv->devices_array,
902 device_kind);
903
904 /* format the value */
905 value = cd_main_device_array_to_variant (array, uid);
906 tuple = g_variant_new_tuple (&value, 1);
907 g_dbus_method_invocation_return_value (invocation, tuple);
908 return;
909 }
910
911 /* return 'as' */
912 if (g_strcmp0 (method_name, "GetProfilesByKind") == 0) {
913
914 /* get all the devices that match this type */
915 g_variant_get (parameters, "(&s)", &scope_tmp);
916 g_debug ("CdMain: %s:GetProfilesByKind(%s)",
917 sender, scope_tmp);
918 array = cd_profile_array_get_by_kind (priv->profiles_array,
919 cd_profile_kind_from_string (scope_tmp));
920
921 /* format the value */
922 value = cd_main_profile_array_to_variant (array);
923 tuple = g_variant_new_tuple (&value, 1);
924 g_dbus_method_invocation_return_value (invocation, tuple);
925 return;
926 }
927
928 /* return 'o' */
929 if (g_strcmp0 (method_name, "FindDeviceById") == 0) {
930
931 g_variant_get (parameters, "(&s)", &device_id);
932 g_debug ("CdMain: %s:FindDeviceById(%s)",
933 sender, device_id);
934 device = cd_device_array_get_by_id_owner (priv->devices_array,
935 device_id,
936 uid,
937 CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
938 if (device == NULL) {
939 g_dbus_method_invocation_return_error (invocation,
940 CD_CLIENT_ERROR,
941 CD_CLIENT_ERROR_NOT_FOUND,
942 "device id '%s' does not exist",
943 device_id);
944 return;
945 }
946
947 /* format the value */
948 value = g_variant_new ("(o)", cd_device_get_object_path (device));
949 g_dbus_method_invocation_return_value (invocation, value);
950 return;
951 }
952
953 /* return 'o' */
954 if (g_strcmp0 (method_name, "FindDeviceByProperty") == 0) {
955
956 g_variant_get (parameters, "(&s&s)",
957 &metadata_key,
958 &metadata_value);
959 g_debug ("CdMain: %s:FindDeviceByProperty(%s=%s)",
960 sender, metadata_key, metadata_value);
961 device = cd_device_array_get_by_property (priv->devices_array,
962 metadata_key,
963 metadata_value);
964 if (device == NULL) {
965 g_dbus_method_invocation_return_error (invocation,
966 CD_CLIENT_ERROR,
967 CD_CLIENT_ERROR_NOT_FOUND,
968 "property match '%s'='%s' does not exist",
969 metadata_key,
970 metadata_value);
971 return;
972 }
973
974 /* format the value */
975 value = g_variant_new ("(o)", cd_device_get_object_path (device));
976 g_dbus_method_invocation_return_value (invocation, value);
977 return;
978 }
979
980 /* return 'o' */
981 if (g_strcmp0 (method_name, "FindSensorById") == 0) {
982
983 g_variant_get (parameters, "(&s)", &device_id);
984 g_debug ("CdMain: %s:FindSensorById(%s)",
985 sender, device_id);
986
987 /* find sensor */
988 for (i = 0; i < priv->sensors->len; i++) {
989 CdSensor *sensor_tmp;
990 sensor_tmp = g_ptr_array_index (priv->sensors, i);
991 if (g_strcmp0 (cd_sensor_get_id (sensor_tmp), device_id) == 0) {
992 value = g_variant_new ("(o)", cd_sensor_get_object_path (sensor_tmp));
993 g_dbus_method_invocation_return_value (invocation, value);
994 return;
995 }
996 }
997 g_dbus_method_invocation_return_error (invocation,
998 CD_CLIENT_ERROR,
999 CD_CLIENT_ERROR_NOT_FOUND,
1000 "sensor id '%s' does not exist",
1001 device_id);
1002 return;
1003 }
1004
1005 /* return 'o' */
1006 if (g_strcmp0 (method_name, "FindProfileByProperty") == 0) {
1007
1008 g_variant_get (parameters, "(&s&s)",
1009 &metadata_key,
1010 &metadata_value);
1011 g_debug ("CdMain: %s:FindProfileByProperty(%s=%s)",
1012 sender, metadata_key, metadata_value);
1013 profile = cd_profile_array_get_by_property (priv->profiles_array,
1014 metadata_key,
1015 metadata_value);
1016 if (profile == NULL) {
1017 g_dbus_method_invocation_return_error (invocation,
1018 CD_CLIENT_ERROR,
1019 CD_CLIENT_ERROR_NOT_FOUND,
1020 "property match '%s'='%s' does not exist",
1021 metadata_key,
1022 metadata_value);
1023 return;
1024 }
1025
1026 /* format the value */
1027 value = g_variant_new ("(o)", cd_profile_get_object_path (profile));
1028 g_dbus_method_invocation_return_value (invocation, value);
1029 return;
1030 }
1031
1032 /* return 'o' */
1033 if (g_strcmp0 (method_name, "FindProfileById") == 0) {
1034
1035 g_variant_get (parameters, "(&s)", &device_id);
1036 g_debug ("CdMain: %s:FindProfileById(%s)",
1037 sender, device_id);
1038 profile = cd_profile_array_get_by_id_owner (priv->profiles_array,
1039 device_id,
1040 uid);
1041 if (profile == NULL) {
1042 g_dbus_method_invocation_return_error (invocation,
1043 CD_CLIENT_ERROR,
1044 CD_CLIENT_ERROR_NOT_FOUND,
1045 "profile id '%s' does not exist",
1046 device_id);
1047 return;
1048 }
1049
1050 /* format the value */
1051 value = g_variant_new ("(o)", cd_profile_get_object_path (profile));
1052 g_dbus_method_invocation_return_value (invocation, value);
1053 return;
1054 }
1055
1056 /* return 'o' */
1057 if (g_strcmp0 (method_name, "GetStandardSpace") == 0) {
1058
1059 g_variant_get (parameters, "(&s)", &device_id);
1060 g_debug ("CdMain: %s:GetStandardSpace(%s)",
1061 sender, device_id);
1062
1063 /* will also return overrides */
1064 profile = cd_main_get_standard_space_metadata (priv, device_id);
1065 if (profile == NULL) {
1066 g_dbus_method_invocation_return_error (invocation,
1067 CD_CLIENT_ERROR,
1068 CD_CLIENT_ERROR_NOT_FOUND,
1069 "profile space '%s' does not exist",
1070 device_id);
1071 return;
1072 }
1073
1074 /* format the value */
1075 value = g_variant_new ("(o)", cd_profile_get_object_path (profile));
1076 g_dbus_method_invocation_return_value (invocation, value);
1077 return;
1078 }
1079
1080 /* return 'o' */
1081 if (g_strcmp0 (method_name, "FindProfileByFilename") == 0) {
1082
1083 g_variant_get (parameters, "(&s)", &device_id);
1084 g_debug ("CdMain: %s:FindProfileByFilename(%s)",
1085 sender, device_id);
1086 profile = cd_profile_array_get_by_filename (priv->profiles_array,
1087 device_id);
1088 if (profile == NULL) {
1089 g_dbus_method_invocation_return_error (invocation,
1090 CD_CLIENT_ERROR,
1091 CD_CLIENT_ERROR_NOT_FOUND,
1092 "profile filename '%s' does not exist",
1093 device_id);
1094 return;
1095 }
1096
1097 /* format the value */
1098 value = g_variant_new ("(o)", cd_profile_get_object_path (profile));
1099 g_dbus_method_invocation_return_value (invocation, value);
1100 return;
1101 }
1102
1103 /* return 'as' */
1104 if (g_strcmp0 (method_name, "GetProfiles") == 0) {
1105
1106 /* format the value */
1107 g_debug ("CdMain: %s:GetProfiles()", sender);
1108 value = cd_profile_array_get_variant (priv->profiles_array);
1109 tuple = g_variant_new_tuple (&value, 1);
1110 g_dbus_method_invocation_return_value (invocation, tuple);
1111 return;
1112 }
1113
1114 /* return 's' */
1115 if (g_strcmp0 (method_name, "CreateDevice") == 0) {
1116
1117 /* require auth */
1118 ret = cd_main_sender_authenticated (connection,
1119 sender,
1120 "org.freedesktop.color-manager.create-device",
1121 &error);
1122 if (!ret) {
1123 g_dbus_method_invocation_return_gerror (invocation, error);
1124 return;
1125 }
1126
1127 /* does already exist */
1128 g_variant_get (parameters, "(&s&s@a{ss})",
1129 &device_id,
1130 &scope_tmp,
1131 &dict);
1132 g_debug ("CdMain: %s:CreateDevice(%s)", sender, device_id);
1133
1134 /* check ID is valid */
1135 if (g_strcmp0 (device_id, "") == 0) {
1136 g_dbus_method_invocation_return_error (invocation,
1137 CD_CLIENT_ERROR,
1138 CD_CLIENT_ERROR_INPUT_INVALID,
1139 "device id cannot be blank");
1140 return;
1141 }
1142
1143 /* check kind is supplied and recognised */
1144 ret = g_variant_lookup (dict,
1145 CD_DEVICE_PROPERTY_KIND,
1146 "&s", &prop_value);
1147 if (!ret) {
1148 g_dbus_method_invocation_return_error (invocation,
1149 CD_CLIENT_ERROR,
1150 CD_CLIENT_ERROR_INPUT_INVALID,
1151 "required device type not specified");
1152 return;
1153 }
1154 device_kind = cd_device_kind_from_string (prop_value);
1155 if (device_kind == CD_DEVICE_KIND_UNKNOWN) {
1156 g_dbus_method_invocation_return_error (invocation,
1157 CD_CLIENT_ERROR,
1158 CD_CLIENT_ERROR_INPUT_INVALID,
1159 "device type %s not recognised",
1160 prop_value);
1161 return;
1162 }
1163
1164 /* are we using the XRANDR_name property rather than the
1165 * sent device-id? */
1166 if (priv->always_use_xrandr_name &&
1167 device_kind == CD_DEVICE_KIND_DISPLAY) {
1168 device_id_fallback = cd_main_get_display_fallback_id (dict);
1169 if (device_id_fallback == NULL) {
1170 g_dbus_method_invocation_return_error (invocation,
1171 CD_CLIENT_ERROR,
1172 CD_CLIENT_ERROR_INPUT_INVALID,
1173 "AlwaysUseXrandrName mode enabled and %s unset",
1174 CD_DEVICE_METADATA_XRANDR_NAME);
1175 return;
1176 }
1177 device_id = device_id_fallback;
1178 }
1179
1180 /* check it does not already exist */
1181 scope = cd_object_scope_from_string (scope_tmp);
1182 if (scope == CD_OBJECT_SCOPE_UNKNOWN) {
1183 g_dbus_method_invocation_return_error (invocation,
1184 CD_CLIENT_ERROR,
1185 CD_CLIENT_ERROR_INPUT_INVALID,
1186 "scope non-valid: %s",
1187 scope_tmp);
1188 return;
1189 }
1190 device = cd_device_array_get_by_id_owner (priv->devices_array,
1191 device_id,
1192 uid,
1193 CD_DEVICE_ARRAY_FLAG_NONE);
1194 if (device != NULL) {
1195 /* where we try to manually add an existing
1196 * virtual device, which means promoting it to
1197 * an actual physical device */
1198 if (cd_device_get_mode (device) == CD_DEVICE_MODE_VIRTUAL) {
1199 cd_device_set_mode (device,
1200 CD_DEVICE_MODE_PHYSICAL);
1201 register_on_bus = FALSE;
1202 } else {
1203 g_dbus_method_invocation_return_error (invocation,
1204 CD_CLIENT_ERROR,
1205 CD_CLIENT_ERROR_ALREADY_EXISTS,
1206 "device id '%s' already exists",
1207 device_id);
1208 return;
1209 }
1210 }
1211
1212 /* get the process that sent the message */
1213 pid = cd_main_get_sender_pid (connection, sender, &error);
1214 if (pid == G_MAXUINT) {
1215 g_dbus_method_invocation_return_error (invocation,
1216 CD_CLIENT_ERROR,
1217 CD_CLIENT_ERROR_INTERNAL,
1218 "failed to get process ID: %s",
1219 error->message);
1220 return;
1221 }
1222
1223 /* create device */
1224 device = cd_main_create_device (priv,
1225 sender,
1226 device_id,
1227 uid,
1228 pid,
1229 scope,
1230 CD_DEVICE_MODE_UNKNOWN,
1231 &error);
1232 if (device == NULL) {
1233 g_warning ("CdMain: failed to create device: %s",
1234 error->message);
1235 g_dbus_method_invocation_return_gerror (invocation, error);
1236 return;
1237 }
1238
1239 /* set the properties */
1240 cd_device_set_kind (device, device_kind);
1241 iter = g_variant_iter_new (dict);
1242 while (g_variant_iter_next (iter, "{&s&s}",
1243 &prop_key, &prop_value)) {
1244 if (g_strcmp0 (prop_key, CD_DEVICE_PROPERTY_KIND) == 0)
1245 continue;
1246 ret = cd_device_set_property_internal (device,
1247 prop_key,
1248 prop_value,
1249 (scope == CD_OBJECT_SCOPE_DISK),
1250 &error);
1251 if (!ret) {
1252 g_warning ("CdMain: failed to set property on device: %s",
1253 error->message);
1254 g_dbus_method_invocation_return_gerror (invocation,
1255 error);
1256 return;
1257 }
1258 }
1259
1260 /* add any extra metadata */
1261 cmdline = cd_main_get_cmdline_for_pid (pid);
1262 if (cmdline != NULL) {
1263 ret = cd_device_set_property_internal (device,
1264 CD_DEVICE_METADATA_OWNER_CMDLINE,
1265 cmdline,
1266 (scope == CD_OBJECT_SCOPE_DISK),
1267 &error);
1268 if (!ret) {
1269 g_warning ("CdMain: failed to set property on device: %s",
1270 error->message);
1271 g_dbus_method_invocation_return_gerror (invocation,
1272 error);
1273 return;
1274 }
1275 }
1276
1277 /* register on bus */
1278 if (register_on_bus) {
1279 ret = cd_main_device_register_on_bus (priv, device, &error);
1280 if (!ret) {
1281 g_dbus_method_invocation_return_gerror (invocation,
1282 error);
1283 return;
1284 }
1285 }
1286
1287 /* format the value */
1288 value = g_variant_new_object_path (cd_device_get_object_path (device));
1289 tuple = g_variant_new_tuple (&value, 1);
1290 g_dbus_method_invocation_return_value (invocation, tuple);
1291 return;
1292 }
1293
1294 /* return 's' */
1295 if (g_strcmp0 (method_name, "DeleteDevice") == 0) {
1296
1297 /* require auth */
1298 ret = cd_main_sender_authenticated (connection,
1299 sender,
1300 "org.freedesktop.color-manager.delete-device",
1301 &error);
1302 if (!ret) {
1303 g_dbus_method_invocation_return_gerror (invocation, error);
1304 return;
1305 }
1306
1307 /* does already exist */
1308 g_variant_get (parameters, "(&o)", &device_id);
1309 g_debug ("CdMain: %s:DeleteDevice(%s)",
1310 sender, device_id);
1311 device = cd_device_array_get_by_id_owner (priv->devices_array,
1312 device_id,
1313 uid,
1314 CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
1315 if (device == NULL) {
1316 /* fall back to checking the object path */
1317 device = cd_device_array_get_by_object_path (priv->devices_array,
1318 device_id);
1319 if (device == NULL) {
1320 g_dbus_method_invocation_return_error (invocation,
1321 CD_CLIENT_ERROR,
1322 CD_CLIENT_ERROR_NOT_FOUND,
1323 "device path '%s' not found",
1324 device_id);
1325 return;
1326 }
1327 }
1328
1329 /* remove from the array, and emit */
1330 cd_main_device_removed (priv, device);
1331
1332 g_dbus_method_invocation_return_value (invocation, NULL);
1333 return;
1334 }
1335
1336 /* return 's' */
1337 if (g_strcmp0 (method_name, "DeleteProfile") == 0) {
1338
1339 /* require auth */
1340 ret = cd_main_sender_authenticated (connection,
1341 sender,
1342 "org.freedesktop.color-manager.create-profile",
1343 &error);
1344 if (!ret) {
1345 g_dbus_method_invocation_return_gerror (invocation, error);
1346 return;
1347 }
1348
1349 /* does already exist */
1350 g_variant_get (parameters, "(&o)", &device_id);
1351 g_debug ("CdMain: %s:DeleteProfile(%s)",
1352 sender, device_id);
1353 profile = cd_profile_array_get_by_id_owner (priv->profiles_array,
1354 device_id,
1355 uid);
1356 if (profile == NULL) {
1357 /* fall back to checking the object path */
1358 profile = cd_profile_array_get_by_object_path (priv->profiles_array,
1359 device_id);
1360 if (profile == NULL) {
1361 g_dbus_method_invocation_return_error (invocation,
1362 CD_CLIENT_ERROR,
1363 CD_CLIENT_ERROR_NOT_FOUND,
1364 "profile path '%s' not found",
1365 device_id);
1366 return;
1367 }
1368 }
1369
1370 /* remove from the array, and emit */
1371 cd_main_profile_removed (priv, profile);
1372
1373 g_dbus_method_invocation_return_value (invocation, NULL);
1374 return;
1375 }
1376
1377 /* return 'o' */
1378 if (g_strcmp0 (method_name, "CreateProfile") == 0 ||
1379 g_strcmp0 (method_name, "CreateProfileWithFd") == 0) {
1380
1381 #ifdef __unix__
1382 GDBusMessage *message;
1383 GUnixFDList *fd_list;
1384 #endif
1385 gint32 fd_handle = 0;
1386
1387 /* require auth */
1388 ret = cd_main_sender_authenticated (connection,
1389 sender,
1390 "org.freedesktop.color-manager.create-profile",
1391 &error);
1392 if (!ret) {
1393 g_dbus_method_invocation_return_gerror (invocation, error);
1394 return;
1395 }
1396
1397 if (g_strcmp0 (g_variant_get_type_string (parameters),
1398 "(ssha{ss})") == 0) {
1399 g_variant_get (parameters, "(&s&sha{ss})",
1400 &device_id,
1401 &scope_tmp,
1402 &fd_handle,
1403 &iter);
1404 g_debug ("CdMain: %s:CreateProfileWithFd(%s,%i)",
1405 g_dbus_method_invocation_get_sender (invocation),
1406 device_id, fd_handle);
1407 } else {
1408 g_variant_get (parameters, "(&s&sa{ss})",
1409 &device_id,
1410 &scope_tmp,
1411 &iter);
1412 g_debug ("CdMain: %s:CreateProfile(%s)", sender, device_id);
1413 }
1414
1415 /* check ID is valid */
1416 if (g_strcmp0 (device_id, "") == 0) {
1417 g_dbus_method_invocation_return_error (invocation,
1418 CD_CLIENT_ERROR,
1419 CD_CLIENT_ERROR_INPUT_INVALID,
1420 "profile id cannot be blank");
1421 return;
1422 }
1423
1424 /* check it does not already exist */
1425 profile = cd_profile_array_get_by_id_owner (priv->profiles_array,
1426 device_id,
1427 uid);
1428 if (profile != NULL) {
1429 g_dbus_method_invocation_return_error (invocation,
1430 CD_CLIENT_ERROR,
1431 CD_CLIENT_ERROR_ALREADY_EXISTS,
1432 "profile id '%s' already exists",
1433 device_id);
1434 return;
1435 }
1436
1437 /* create profile */
1438 scope = cd_object_scope_from_string (scope_tmp);
1439 if (scope == CD_OBJECT_SCOPE_UNKNOWN) {
1440 g_dbus_method_invocation_return_error (invocation,
1441 CD_CLIENT_ERROR,
1442 CD_CLIENT_ERROR_INPUT_INVALID,
1443 "scope non-valid: %s",
1444 scope_tmp);
1445 return;
1446 }
1447 profile = cd_main_create_profile (priv,
1448 sender,
1449 device_id,
1450 uid,
1451 scope,
1452 &error);
1453 if (profile == NULL) {
1454 g_dbus_method_invocation_return_gerror (invocation, error);
1455 return;
1456 }
1457
1458 /* set the properties */
1459 while (g_variant_iter_next (iter, "{&s&s}",
1460 &prop_key, &prop_value)) {
1461 if (filename == NULL &&
1462 g_strcmp0 (prop_key, CD_PROFILE_PROPERTY_FILENAME) == 0)
1463 filename = g_strdup (prop_value);
1464 ret = cd_profile_set_property_internal (profile,
1465 prop_key,
1466 prop_value,
1467 uid,
1468 &error);
1469 if (!ret) {
1470 g_dbus_method_invocation_return_gerror (invocation,
1471 error);
1472 return;
1473 }
1474 }
1475
1476 /* get any file descriptor in the message */
1477 #ifdef __unix__
1478 message = g_dbus_method_invocation_get_message (invocation);
1479 fd_list = g_dbus_message_get_unix_fd_list (message);
1480 if (fd_list != NULL && g_unix_fd_list_get_length (fd_list) == 1) {
1481 gint fd;
1482 fd = g_unix_fd_list_get (fd_list, fd_handle, &error);
1483 if (fd < 0) {
1484 g_warning ("CdMain: failed to get fd from message: %s",
1485 error->message);
1486 g_dbus_method_invocation_return_gerror (invocation,
1487 error);
1488 return;
1489 }
1490
1491 /* read from a fd, avoiding open() */
1492 ret = cd_profile_load_from_fd (profile, fd, &error);
1493 if (!ret) {
1494 g_warning ("CdMain: failed to profile from fd: %s",
1495 error->message);
1496 g_dbus_method_invocation_return_gerror (invocation,
1497 error);
1498 return;
1499 }
1500
1501 /* clients like CUPS do not use FD passing */
1502 } else if (filename != NULL) {
1503 ret = cd_profile_load_from_filename (profile,
1504 filename,
1505 &error);
1506 if (!ret) {
1507 g_warning ("CdMain: failed to profile from filename: %s",
1508 error->message);
1509 g_dbus_method_invocation_return_gerror (invocation, error);
1510 return;
1511 }
1512 }
1513 #else
1514 if (filename != NULL) {
1515 ret = cd_profile_load_from_filename (profile,
1516 filename,
1517 &error);
1518 if (!ret) {
1519 g_warning ("CdMain: failed to profile from filename: %s",
1520 error->message);
1521 g_dbus_method_invocation_return_gerror (invocation, error);
1522 return;
1523 }
1524 } else {
1525 g_dbus_method_invocation_return_error (invocation,
1526 CD_CLIENT_ERROR,
1527 CD_CLIENT_ERROR_NOT_SUPPORTED,
1528 "no FD support");
1529 return;
1530 }
1531 #endif
1532 /* auto add profiles from the database and metadata */
1533 cd_main_profile_auto_add_from_db (priv, profile);
1534 cd_main_profile_auto_add_from_md (priv, profile);
1535
1536 /* register on bus */
1537 ret = cd_main_profile_register_on_bus (priv,
1538 profile,
1539 CD_LOGGING_FLAG_SYSLOG,
1540 &error);
1541 if (!ret) {
1542 g_dbus_method_invocation_return_gerror (invocation, error);
1543 return;
1544 }
1545
1546 /* format the value */
1547 value = g_variant_new_object_path (cd_profile_get_object_path (profile));
1548 tuple = g_variant_new_tuple (&value, 1);
1549 g_dbus_method_invocation_return_value (invocation, tuple);
1550 return;
1551 }
1552
1553 /* we suck */
1554 g_critical ("failed to process method %s", method_name);
1555 }
1556
1557 static GVariant *
1558 cd_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender,
1559 const gchar *object_path, const gchar *interface_name,
1560 const gchar *property_name, GError **error,
1561 gpointer user_data)
1562 {
1563 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1564
1565 if (g_strcmp0 (property_name, CD_CLIENT_PROPERTY_DAEMON_VERSION) == 0)
1566 return g_variant_new_string (VERSION);
1567 if (g_strcmp0 (property_name, CD_CLIENT_PROPERTY_SYSTEM_VENDOR) == 0)
1568 return g_variant_new_string (priv->system_vendor);
1569 if (g_strcmp0 (property_name, CD_CLIENT_PROPERTY_SYSTEM_MODEL) == 0)
1570 return g_variant_new_string (priv->system_model);
1571
1572 /* return an error */
1573 g_set_error (error,
1574 CD_CLIENT_ERROR,
1575 CD_CLIENT_ERROR_INTERNAL,
1576 "failed to get daemon property %s",
1577 property_name);
1578 return NULL;
1579 }
1580
1581 static void
1582 cd_main_on_bus_acquired_cb (GDBusConnection *connection,
1583 const gchar *name,
1584 gpointer user_data)
1585 {
1586 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1587 guint registration_id;
1588 static const GDBusInterfaceVTable interface_vtable = {
1589 cd_main_daemon_method_call,
1590 cd_main_daemon_get_property,
1591 NULL
1592 };
1593
1594 priv->connection = g_object_ref (connection);
1595 registration_id = g_dbus_connection_register_object (connection,
1596 COLORD_DBUS_PATH,
1597 priv->introspection_daemon->interfaces[0],
1598 &interface_vtable,
1599 priv, /* user_data */
1600 NULL, /* user_data_free_func */
1601 NULL); /* GError** */
1602 g_assert (registration_id > 0);
1603 }
1604
1605 static void
1606 cd_main_icc_store_added_cb (CdIccStore *icc_store,
1607 CdIcc *icc,
1608 gpointer user_data)
1609 {
1610 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1611 const gchar *checksum;
1612 const gchar *filename;
1613 gboolean ret;
1614 g_autoptr(GError) error = NULL;
1615 g_autofree gchar *profile_id = NULL;
1616 g_autoptr(CdProfile) profile = NULL;
1617
1618 /* create profile */
1619 profile = cd_profile_new ();
1620 filename = cd_icc_get_filename (icc);
1621 if (g_str_has_prefix (filename, "/usr/share/color") ||
1622 g_str_has_prefix (filename, "/var/lib/color"))
1623 cd_profile_set_is_system_wide (profile, TRUE);
1624
1625 /* parse the profile name */
1626 ret = cd_profile_load_from_icc (profile, icc, &error);
1627 if (!ret) {
1628 g_warning ("CdIccStore: failed to add profile '%s': %s",
1629 filename, error->message);
1630 return;
1631 }
1632
1633 /* ensure profiles have the checksum metadata item */
1634 checksum = cd_profile_get_checksum (profile);
1635 cd_profile_set_property_internal (profile,
1636 CD_PROFILE_METADATA_FILE_CHECKSUM,
1637 checksum,
1638 0, /* uid unknown */
1639 NULL);
1640
1641 /* just add it to the bus with the title as the ID */
1642 profile_id = g_strdup_printf ("icc-%s", cd_icc_get_checksum (icc));
1643 cd_profile_set_id (profile, profile_id);
1644 ret = cd_main_add_profile (priv, profile, &error);
1645 if (!ret) {
1646 g_warning ("CdMain: failed to add profile: %s",
1647 error->message);
1648 return;
1649 }
1650
1651 /* register on bus */
1652 ret = cd_main_profile_register_on_bus (priv,
1653 profile,
1654 CD_LOGGING_FLAG_NONE,
1655 &error);
1656 if (!ret) {
1657 g_warning ("CdMain: failed to emit ProfileAdded: %s",
1658 error->message);
1659 return;
1660 }
1661 }
1662
1663 static void
1664 cd_main_icc_store_removed_cb (CdIccStore *icc_store,
1665 CdIcc *icc,
1666 gpointer user_data)
1667 {
1668 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1669 CdProfile *profile;
1670
1671 /* check the profile should be invalidated automatically */
1672 profile = cd_profile_array_get_by_filename (priv->profiles_array,
1673 cd_icc_get_filename (icc));
1674 if (profile == NULL)
1675 return;
1676 g_debug ("%s removed, so invalidating", cd_icc_get_filename (icc));
1677 cd_profile_array_remove (priv->profiles_array, profile);
1678 }
1679
1680 static void
1681 cd_main_add_disk_device (CdMainPrivate *priv, const gchar *device_id)
1682 {
1683 const gchar *property;
1684 gboolean ret;
1685 guint i;
1686 g_autoptr(GError) error = NULL;
1687 g_autoptr(CdDevice) device = NULL;
1688 g_autoptr(GPtrArray) array_properties = NULL;
1689
1690 device = cd_main_create_device (priv,
1691 NULL,
1692 device_id,
1693 0,
1694 0,
1695 CD_OBJECT_SCOPE_DISK,
1696 CD_DEVICE_MODE_VIRTUAL,
1697 &error);
1698 if (device == NULL) {
1699 g_warning ("CdMain: failed to create disk device: %s",
1700 error->message);
1701 return;
1702 }
1703
1704 g_debug ("CdMain: created permanent device %s",
1705 cd_device_get_object_path (device));
1706
1707 /* set properties on the device */
1708 array_properties = cd_device_db_get_properties (priv->device_db,
1709 device_id,
1710 &error);
1711 if (array_properties == NULL) {
1712 g_warning ("CdMain: failed to get props for device %s: %s",
1713 device_id, error->message);
1714 return;
1715 }
1716 for (i = 0; i < array_properties->len; i++) {
1717 g_autofree gchar *value = NULL;
1718 property = g_ptr_array_index (array_properties, i);
1719 value = cd_device_db_get_property (priv->device_db,
1720 device_id,
1721 property,
1722 &error);
1723 if (value == NULL) {
1724 g_warning ("CdMain: failed to get value: %s",
1725 error->message);
1726 g_clear_error (&error);
1727 return;
1728 }
1729 ret = cd_device_set_property_internal (device,
1730 property,
1731 value,
1732 FALSE,
1733 &error);
1734 if (!ret) {
1735 g_warning ("CdMain: failed to set internal prop: %s",
1736 error->message);
1737 g_clear_error (&error);
1738 return;
1739 }
1740 }
1741
1742 /* register on bus */
1743 ret = cd_main_device_register_on_bus (priv, device, &error);
1744 if (!ret) {
1745 g_warning ("CdMain: failed to emit DeviceAdded: %s",
1746 error->message);
1747 return;
1748 }
1749 }
1750
1751 static gboolean
1752 cd_main_sensor_register_on_bus (CdMainPrivate *priv,
1753 CdSensor *sensor,
1754 GError **error)
1755 {
1756 /* register object */
1757 if (!cd_sensor_register_object (sensor,
1758 priv->connection,
1759 priv->introspection_sensor->interfaces[0],
1760 error))
1761 return FALSE;
1762
1763 /* emit signal */
1764 g_debug ("CdMain: Emitting SensorAdded(%s)",
1765 cd_sensor_get_object_path (sensor));
1766 g_info ("Sensor added: %s", cd_sensor_get_id (sensor));
1767 g_dbus_connection_emit_signal (priv->connection,
1768 NULL,
1769 COLORD_DBUS_PATH,
1770 COLORD_DBUS_INTERFACE,
1771 "SensorAdded",
1772 g_variant_new ("(o)",
1773 cd_sensor_get_object_path (sensor)),
1774 NULL);
1775 return TRUE;
1776 }
1777
1778 static void
1779 cd_main_add_sensor (CdMainPrivate *priv, CdSensor *sensor)
1780 {
1781 const gchar *id;
1782 gboolean ret;
1783 g_autoptr(GError) error = NULL;
1784
1785 id = cd_sensor_get_id (sensor);
1786 if (id == NULL) {
1787 g_warning ("did not get an ID from the sensor");
1788 return;
1789 }
1790 g_debug ("CdMain: add sensor: %s", id);
1791 g_ptr_array_add (priv->sensors, g_object_ref (sensor));
1792
1793 /* register on bus */
1794 ret = cd_main_sensor_register_on_bus (priv, sensor, &error);
1795 if (!ret) {
1796 g_ptr_array_remove (priv->sensors, sensor);
1797 g_warning ("CdMain: failed to emit SensorAdded: %s",
1798 error->message);
1799 return;
1800 }
1801 }
1802
1803 static void
1804 cd_main_plugin_phase (CdMainPrivate *priv, CdPluginPhase phase)
1805 {
1806 CdPluginFunc plugin_func = NULL;
1807 CdPlugin *plugin;
1808 const gchar *function = NULL;
1809 gboolean ret;
1810 guint i;
1811
1812 switch (phase) {
1813 case CD_PLUGIN_PHASE_INIT:
1814 function = "cd_plugin_initialize";
1815 break;
1816 case CD_PLUGIN_PHASE_DESTROY:
1817 function = "cd_plugin_destroy";
1818 break;
1819 case CD_PLUGIN_PHASE_COLDPLUG:
1820 function = "cd_plugin_coldplug";
1821 break;
1822 case CD_PLUGIN_PHASE_STATE_CHANGED:
1823 function = "cd_plugin_state_changed";
1824 break;
1825 default:
1826 g_assert_not_reached ();
1827 break;
1828 }
1829
1830 g_assert (function != NULL);
1831
1832 /* run each plugin */
1833 for (i = 0; i < priv->plugins->len; i++) {
1834 plugin = g_ptr_array_index (priv->plugins, i);
1835 ret = g_module_symbol (plugin->module,
1836 function,
1837 (gpointer *) &plugin_func);
1838 if (!ret)
1839 continue;
1840 g_debug ("run %s on %s",
1841 function,
1842 g_module_name (plugin->module));
1843 plugin_func (plugin);
1844 g_debug ("finished %s", function);
1845 }
1846 }
1847
1848 static void
1849 cd_main_on_name_acquired_cb (GDBusConnection *connection,
1850 const gchar *name,
1851 gpointer user_data)
1852 {
1853 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1854 const gchar *device_id;
1855 gboolean ret;
1856 guint i;
1857 g_autoptr(GError) error = NULL;
1858 g_autoptr(CdSensor) sensor = NULL;
1859 g_autoptr(GPtrArray) array_devices = NULL;
1860
1861 g_debug ("CdMain: acquired name: %s", name);
1862
1863 /* add system profiles */
1864 priv->icc_store = cd_icc_store_new ();
1865 cd_icc_store_set_load_flags (priv->icc_store, CD_ICC_LOAD_FLAGS_FALLBACK_MD5);
1866 cd_icc_store_set_cache (priv->icc_store, cd_get_resource ());
1867 g_signal_connect (priv->icc_store, "added",
1868 G_CALLBACK (cd_main_icc_store_added_cb),
1869 user_data);
1870 g_signal_connect (priv->icc_store, "removed",
1871 G_CALLBACK (cd_main_icc_store_removed_cb),
1872 user_data);
1873
1874 /* search locations for ICC profiles */
1875 ret = cd_icc_store_search_kind (priv->icc_store,
1876 CD_ICC_STORE_SEARCH_KIND_SYSTEM,
1877 CD_ICC_STORE_SEARCH_FLAGS_NONE,
1878 NULL,
1879 &error);
1880 if (!ret) {
1881 g_warning ("CdMain: failed to search system directories: %s",
1882 error->message);
1883 return;
1884 }
1885 ret = cd_icc_store_search_kind (priv->icc_store,
1886 CD_ICC_STORE_SEARCH_KIND_MACHINE,
1887 CD_ICC_STORE_SEARCH_FLAGS_NONE,
1888 NULL,
1889 &error);
1890 if (!ret) {
1891 g_warning ("CdMain: failed to search machine directories: %s",
1892 error->message);
1893 return;
1894 }
1895
1896 /* add disk devices */
1897 array_devices = cd_device_db_get_devices (priv->device_db, &error);
1898 if (array_devices == NULL) {
1899 g_warning ("CdMain: failed to get the disk devices: %s",
1900 error->message);
1901 return;
1902 }
1903 for (i = 0; i < array_devices->len; i++) {
1904 device_id = g_ptr_array_index (array_devices, i);
1905 cd_main_add_disk_device (priv, device_id);
1906 }
1907
1908 /* add sensor devices */
1909 cd_sensor_client_coldplug (priv->sensor_client);
1910
1911 /* coldplug plugin devices */
1912 cd_main_plugin_phase (priv, CD_PLUGIN_PHASE_COLDPLUG);
1913
1914 /* add dummy sensor */
1915 if (priv->create_dummy_sensor) {
1916 sensor = cd_sensor_new ();
1917 cd_sensor_set_kind (sensor, CD_SENSOR_KIND_DUMMY);
1918 ret = cd_sensor_load (sensor, &error);
1919 if (!ret) {
1920 g_warning ("CdMain: failed to load dummy sensor: %s",
1921 error->message);
1922 g_clear_error (&error);
1923 } else {
1924 cd_main_add_sensor (priv, sensor);
1925 }
1926 }
1927 }
1928
1929 static void
1930 cd_main_on_name_lost_cb (GDBusConnection *connection,
1931 const gchar *name,
1932 gpointer user_data)
1933 {
1934 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1935 g_debug ("CdMain: lost name: %s", name);
1936 g_main_loop_quit (priv->loop);
1937 }
1938
1939
1940 static void
1941 cd_main_client_sensor_added_cb (CdSensorClient *sensor_client_,
1942 CdSensor *sensor,
1943 gpointer user_data)
1944 {
1945 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1946 cd_main_add_sensor (priv, sensor);
1947 }
1948
1949 static void
1950 cd_main_client_sensor_removed_cb (CdSensorClient *sensor_client_,
1951 CdSensor *sensor,
1952 gpointer user_data)
1953 {
1954 CdMainPrivate *priv = (CdMainPrivate *) user_data;
1955
1956 /* emit signal */
1957 g_debug ("CdMain: Emitting SensorRemoved(%s)",
1958 cd_sensor_get_object_path (sensor));
1959 g_info ("Sensor removed: %s", cd_sensor_get_id (sensor));
1960 g_dbus_connection_emit_signal (priv->connection,
1961 NULL,
1962 COLORD_DBUS_PATH,
1963 COLORD_DBUS_INTERFACE,
1964 "SensorRemoved",
1965 g_variant_new ("(o)",
1966 cd_sensor_get_object_path (sensor)),
1967 NULL);
1968 g_ptr_array_remove (priv->sensors, sensor);
1969 }
1970
1971 static gboolean
1972 cd_main_timed_exit_cb (gpointer user_data)
1973 {
1974 GMainLoop *loop = (GMainLoop *) user_data;
1975 g_main_loop_quit (loop);
1976 return G_SOURCE_REMOVE;
1977 }
1978
1979 static GDBusNodeInfo *
1980 cd_main_load_introspection (const gchar *filename, GError **error)
1981 {
1982 g_autoptr(GBytes) data = NULL;
1983 g_autofree gchar *path = NULL;
1984
1985 /* lookup data */
1986 path = g_build_filename ("/org/freedesktop/colord", filename, NULL);
1987 data = g_resource_lookup_data (cd_get_resource (),
1988 path,
1989 G_RESOURCE_LOOKUP_FLAGS_NONE,
1990 error);
1991 if (data == NULL)
1992 return NULL;
1993
1994 /* build introspection from XML */
1995 return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error);
1996 }
1997
1998 static void
1999 cd_main_plugin_free (CdPlugin *plugin)
2000 {
2001 g_free (plugin->priv);
2002 g_module_close (plugin->module);
2003 g_free (plugin);
2004 }
2005
2006 static void
2007 cd_main_plugin_device_added_cb (CdPlugin *plugin,
2008 CdDevice *device,
2009 gpointer user_data)
2010 {
2011 CdMainPrivate *priv = (CdMainPrivate *) user_data;
2012 gboolean ret;
2013 g_autoptr(GError) error = NULL;
2014
2015 cd_device_set_mode (device, CD_DEVICE_MODE_PHYSICAL);
2016 ret = cd_main_device_add (priv, device, NULL, &error);
2017 if (!ret) {
2018 g_warning ("CdMain: failed to add device: %s",
2019 error->message);
2020 return;
2021 }
2022
2023 /* register on bus */
2024 ret = cd_main_device_register_on_bus (priv, device, &error);
2025 if (!ret) {
2026 g_warning ("CdMain: failed to emit DeviceAdded: %s",
2027 error->message);
2028 return;
2029 }
2030 }
2031
2032 static void
2033 cd_main_plugin_device_removed_cb (CdPlugin *plugin,
2034 CdDevice *device,
2035 gpointer user_data)
2036 {
2037 CdMainPrivate *priv = (CdMainPrivate *) user_data;
2038 g_debug ("CdMain: remove device: %s", cd_device_get_id (device));
2039 cd_main_device_removed (priv, device);
2040 }
2041
2042 static gboolean
2043 cd_main_load_plugin (CdMainPrivate *priv,
2044 const gchar *filename,
2045 GError **error)
2046 {
2047 CdPluginEnabledFunc plugin_enabled = NULL;
2048 CdPluginGetDescFunc plugin_desc = NULL;
2049 CdPlugin *plugin;
2050 GModule *module;
2051
2052 /* open the plugin and import all symbols */
2053 module = g_module_open (filename, 0);
2054 if (module == NULL) {
2055 g_set_error (error,
2056 CD_CLIENT_ERROR,
2057 CD_CLIENT_ERROR_FILE_INVALID,
2058 "failed to open: %s",
2059 g_module_error ());
2060 return FALSE;
2061 }
2062
2063 /* get description */
2064 if (!g_module_symbol (module, "cd_plugin_get_description",
2065 (gpointer *) &plugin_desc)) {
2066 g_set_error_literal (error,
2067 CD_CLIENT_ERROR,
2068 CD_CLIENT_ERROR_INTERNAL,
2069 "plugin requires description");
2070 g_module_close (module);
2071 return FALSE;
2072 }
2073
2074 /* give the module the option to opt-out */
2075 if (g_module_symbol (module, "cd_plugin_enabled",
2076 (gpointer *) &plugin_enabled)) {
2077 if (!plugin_enabled ()) {
2078 g_set_error_literal (error,
2079 CD_CLIENT_ERROR,
2080 CD_CLIENT_ERROR_NOT_SUPPORTED,
2081 "plugin refused to load");
2082 g_module_close (module);
2083 return FALSE;
2084 }
2085 }
2086
2087 /* print what we know */
2088 plugin = g_new0 (CdPlugin, 1);
2089 plugin->user_data = priv;
2090 plugin->module = module;
2091 plugin->device_added = cd_main_plugin_device_added_cb;
2092 plugin->device_removed = cd_main_plugin_device_removed_cb;
2093
2094 /* add to array */
2095 g_ptr_array_add (priv->plugins, plugin);
2096 return TRUE;
2097 }
2098
2099 static void
2100 cd_main_load_plugins (CdMainPrivate *priv)
2101 {
2102 const gchar *filename_tmp;
2103 gboolean ret;
2104 g_autoptr(GDir) dir = NULL;
2105 g_autoptr(GError) error = NULL;
2106 g_autofree gchar *path = NULL;
2107
2108 /* search in the plugin directory for plugins */
2109 path = g_build_filename (LIBDIR, "colord-plugins", NULL);
2110 dir = g_dir_open (path, 0, &error);
2111 if (dir == NULL) {
2112 g_warning ("failed to open plugin directory: %s",
2113 error->message);
2114 return;
2115 }
2116
2117 /* try to open each plugin */
2118 g_debug ("searching for plugins in %s", path);
2119 do {
2120 g_autofree gchar *filename_plugin = NULL;
2121 filename_tmp = g_dir_read_name (dir);
2122 if (filename_tmp == NULL)
2123 break;
2124 if (!g_str_has_suffix (filename_tmp, ".so"))
2125 continue;
2126 filename_plugin = g_build_filename (path,
2127 filename_tmp,
2128 NULL);
2129 ret = cd_main_load_plugin (priv, filename_plugin, &error);
2130 if (ret) {
2131 g_info ("loaded plugin %s", filename_tmp);
2132 } else {
2133 if (g_error_matches (error,
2134 CD_CLIENT_ERROR,
2135 CD_CLIENT_ERROR_NOT_SUPPORTED)) {
2136 g_debug ("CdMain: %s", error->message);
2137 } else {
2138 g_warning ("CdMain: %s", error->message);
2139 }
2140 g_info ("plugin %s not loaded: %s",
2141 filename_plugin, error->message);
2142 g_clear_error (&error);
2143 }
2144 } while (TRUE);
2145 }
2146
2147 static CdEdid *
2148 cd_main_get_edid_for_output (const gchar *output_name)
2149 {
2150 gboolean ret;
2151 gsize len = 0;
2152 g_autoptr(GBytes) data = NULL;
2153 g_autoptr(GError) error = NULL;
2154 g_autofree gchar *edid_data = NULL;
2155 g_autofree gchar *edid_fn = NULL;
2156 g_autofree gchar *enabled_data = NULL;
2157 g_autofree gchar *enabled_fn = NULL;
2158 g_autoptr(CdEdid) edid = NULL;
2159
2160 /* check output is actually an output */
2161 enabled_fn = g_build_filename ("/sys/class/drm",
2162 output_name,
2163 "enabled",
2164 NULL);
2165 ret = g_file_test (enabled_fn, G_FILE_TEST_EXISTS);
2166 if (!ret)
2167 return NULL;
2168
2169 /* check output is enabled */
2170 ret = g_file_get_contents (enabled_fn, &enabled_data, NULL, &error);
2171 if (!ret) {
2172 g_warning ("failed to get enabled data: %s", error->message);
2173 return NULL;
2174 }
2175 g_strdelimit (enabled_data, "\n", '\0');
2176 if (g_strcmp0 (enabled_data, "enabled") != 0)
2177 return NULL;
2178
2179 /* get EDID data */
2180 edid_fn = g_build_filename ("/sys/class/drm",
2181 output_name,
2182 "edid",
2183 NULL);
2184 ret = g_file_get_contents (edid_fn, &edid_data, &len, &error);
2185 if (!ret) {
2186 g_warning ("failed to get edid data: %s", error->message);
2187 return NULL;
2188 }
2189
2190 /* parse EDID */
2191 edid = cd_edid_new ();
2192 data = g_bytes_new (edid_data, len);
2193 if (!cd_edid_parse (edid, data, &error)) {
2194 g_warning ("failed to get edid data: %s", error->message);
2195 return NULL;
2196 }
2197 return g_object_ref (edid);
2198 }
2199
2200 static gchar *
2201 cd_main_get_display_id (CdEdid *edid)
2202 {
2203 GString *device_id;
2204
2205 device_id = g_string_new ("xrandr");
2206 if (cd_edid_get_vendor_name (edid) != NULL)
2207 g_string_append_printf (device_id, "-%s", cd_edid_get_vendor_name (edid));
2208 if (cd_edid_get_monitor_name (edid) != NULL)
2209 g_string_append_printf (device_id, "-%s", cd_edid_get_monitor_name (edid));
2210 if (cd_edid_get_serial_number (edid) != NULL)
2211 g_string_append_printf (device_id, "-%s", cd_edid_get_serial_number (edid));
2212 return g_string_free (device_id, FALSE);
2213 }
2214
2215 static gboolean
2216 cd_main_check_duplicate_edids (void)
2217 {
2218 const gchar *fn;
2219 gboolean use_xrandr_mode = FALSE;
2220 g_autoptr(GDir) dir = NULL;
2221 g_autoptr(GHashTable) hash = NULL;
2222
2223 dir = g_dir_open ("/sys/class/drm", 0, NULL);
2224 if (dir == NULL)
2225 return FALSE;
2226
2227 /* read all the outputs in /sys/class/drm and search for duplicates */
2228 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
2229 while (!use_xrandr_mode && (fn = g_dir_read_name (dir)) != NULL) {
2230 gpointer old_output;
2231 g_autoptr(CdEdid) edid = NULL;
2232 edid = cd_main_get_edid_for_output (fn);
2233 if (edid == NULL)
2234 continue;
2235 g_debug ("display %s has ID '%s' from MD5 %s",
2236 fn, cd_main_get_display_id (edid),
2237 cd_edid_get_checksum (edid));
2238 old_output = g_hash_table_lookup (hash, cd_edid_get_checksum (edid));
2239 if (old_output != NULL) {
2240 g_debug ("output %s has duplicate EDID", fn);
2241 use_xrandr_mode = TRUE;
2242 }
2243 g_hash_table_insert (hash,
2244 g_strdup (cd_edid_get_checksum (edid)),
2245 GINT_TO_POINTER (1));
2246 }
2247 return use_xrandr_mode;
2248 }
2249
2250 static gchar *
2251 cd_main_dmi_get_from_filename (const gchar *filename)
2252 {
2253 gboolean ret;
2254 gchar *data = NULL;
2255 g_autoptr(GError) error = NULL;
2256
2257 /* get the contents */
2258 ret = g_file_get_contents (filename, &data, NULL, &error);
2259 if (!ret) {
2260 if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
2261 g_warning ("failed to get contents of %s: %s",
2262 filename, error->message);
2263 }
2264
2265 /* process the random chars and trailing spaces */
2266 if (data != NULL) {
2267 g_strdelimit (data, "\t_", ' ');
2268 g_strdelimit (data, "\n\r", '\0');
2269 g_strchomp (data);
2270 }
2271
2272 /* don't return an empty string */
2273 if (data != NULL && data[0] == '\0') {
2274 g_free (data);
2275 data = NULL;
2276 }
2277
2278 return data;
2279 }
2280
2281 static gchar *
2282 cd_main_dmi_get_from_filenames (const gchar * const * filenames)
2283 {
2284 guint i;
2285 gchar *tmp = NULL;
2286
2287 /* try each one in preference order */
2288 for (i = 0; filenames[i] != NULL; i++) {
2289 tmp = cd_main_dmi_get_from_filename (filenames[i]);
2290 if (tmp != NULL)
2291 break;
2292 }
2293 return tmp;
2294 }
2295
2296
2297 static gchar *
2298 cd_main_dmi_get_vendor (void)
2299 {
2300 const gchar *sysfs_vendor[] = {
2301 "/sys/class/dmi/id/sys_vendor",
2302 "/sys/class/dmi/id/chassis_vendor",
2303 "/sys/class/dmi/id/board_vendor",
2304 NULL};
2305 g_autofree gchar *tmp = NULL;
2306
2307 /* get vendor name */
2308 tmp = cd_main_dmi_get_from_filenames (sysfs_vendor);
2309 if (tmp == NULL)
2310 return g_strdup("Unknown");
2311 return cd_quirk_vendor_name (tmp);
2312 }
2313 static gchar *
2314 cd_main_dmi_get_model (void)
2315 {
2316 const gchar *sysfs_model[] = {
2317 "/sys/class/dmi/id/product_name",
2318 "/sys/class/dmi/id/board_name",
2319 NULL};
2320 gchar *model;
2321 g_autofree gchar *tmp = NULL;
2322
2323 /* thinkpad puts the common name in the version field, urgh */
2324 tmp = cd_main_dmi_get_from_filename ("/sys/class/dmi/id/product_version");
2325 if (tmp != NULL && g_strstr_len (tmp, -1, "ThinkPad") != NULL)
2326 return g_strdup (tmp);
2327
2328 /* get where the model should be */
2329 model = cd_main_dmi_get_from_filenames (sysfs_model);
2330 if (model == NULL)
2331 return g_strdup ("Unknown");
2332 return model;
2333 }
2334
2335 static void
2336 cd_main_dmi_setup (CdMainPrivate *priv)
2337 {
2338 priv->system_vendor = cd_main_dmi_get_vendor ();
2339 priv->system_model = cd_main_dmi_get_model ();
2340 }
2341
2342 int
2343 main (int argc, char *argv[])
2344 {
2345 CdMainPrivate *priv = NULL;
2346 gboolean immediate_exit = FALSE;
2347 gboolean create_dummy_sensor = FALSE;
2348 gboolean ret;
2349 gboolean timed_exit = FALSE;
2350 GOptionContext *context;
2351 guint owner_id = 0;
2352 guint retval = 1;
2353 const GOptionEntry options[] = {
2354 { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
2355 /* TRANSLATORS: exit after we've started up, used for user profiling */
2356 _("Exit after a small delay"), NULL },
2357 { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit,
2358 /* TRANSLATORS: exit straight away, used for automatic profiling */
2359 _("Exit after the engine has loaded"), NULL },
2360 { "create-dummy-sensor", '\0', 0, G_OPTION_ARG_NONE, &create_dummy_sensor,
2361 /* TRANSLATORS: exit straight away, used for automatic profiling */
2362 _("Create a dummy sensor for testing"), NULL },
2363 { NULL}
2364 };
2365 g_autoptr(GError) error = NULL;
2366
2367 setlocale (LC_ALL, "");
2368
2369 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2370 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2371 textdomain (GETTEXT_PACKAGE);
2372
2373 /* TRANSLATORS: program name */
2374 g_set_application_name (_("Color Management"));
2375 context = g_option_context_new (NULL);
2376 g_option_context_add_main_entries (context, options, NULL);
2377 g_option_context_add_group (context, cd_debug_get_option_group ());
2378 g_option_context_set_summary (context, _("Color Management D-Bus Service"));
2379 ret = g_option_context_parse (context, &argc, &argv, &error);
2380 if (!ret) {
2381 g_warning ("CdMain: failed to parse command line arguments: %s",
2382 error->message);
2383 goto out;
2384 }
2385
2386 /* create new objects */
2387 priv = g_new0 (CdMainPrivate, 1);
2388 priv->create_dummy_sensor = create_dummy_sensor;
2389 priv->loop = g_main_loop_new (NULL, FALSE);
2390 priv->devices_array = cd_device_array_new ();
2391 priv->profiles_array = cd_profile_array_new ();
2392 priv->sensors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
2393 priv->sensor_client = cd_sensor_client_new ();
2394 g_signal_connect (priv->sensor_client, "sensor-added",
2395 G_CALLBACK (cd_main_client_sensor_added_cb),
2396 priv);
2397 g_signal_connect (priv->sensor_client, "sensor-removed",
2398 G_CALLBACK (cd_main_client_sensor_removed_cb),
2399 priv);
2400
2401 /* connect to the mapping db */
2402 priv->mapping_db = cd_mapping_db_new ();
2403 ret = cd_mapping_db_load (priv->mapping_db,
2404 LOCALSTATEDIR "/lib/colord/mapping.db",
2405 &error);
2406 if (!ret) {
2407 g_warning ("CdMain: failed to load mapping database: %s",
2408 error->message);
2409 goto out;
2410 }
2411
2412 /* connect to the device db */
2413 priv->device_db = cd_device_db_new ();
2414 ret = cd_device_db_load (priv->device_db,
2415 LOCALSTATEDIR "/lib/colord/storage.db",
2416 &error);
2417 if (!ret) {
2418 g_warning ("CdMain: failed to load device database: %s",
2419 error->message);
2420 goto out;
2421 }
2422
2423 /* connect to the profile db */
2424 priv->profile_db = cd_profile_db_new ();
2425 ret = cd_profile_db_load (priv->profile_db,
2426 LOCALSTATEDIR "/lib/colord/storage.db",
2427 &error);
2428 if (!ret) {
2429 g_warning ("CdMain: failed to load profile database: %s",
2430 error->message);
2431 goto out;
2432 }
2433
2434 /* load introspection from file */
2435 priv->introspection_daemon = cd_main_load_introspection (COLORD_DBUS_INTERFACE ".xml",
2436 &error);
2437 if (priv->introspection_daemon == NULL) {
2438 g_warning ("CdMain: failed to load daemon introspection: %s",
2439 error->message);
2440 goto out;
2441 }
2442 priv->introspection_device = cd_main_load_introspection (COLORD_DBUS_INTERFACE_DEVICE ".xml",
2443 &error);
2444 if (priv->introspection_device == NULL) {
2445 g_warning ("CdMain: failed to load device introspection: %s",
2446 error->message);
2447 goto out;
2448 }
2449 priv->introspection_profile = cd_main_load_introspection (COLORD_DBUS_INTERFACE_PROFILE ".xml",
2450 &error);
2451 if (priv->introspection_profile == NULL) {
2452 g_warning ("CdMain: failed to load profile introspection: %s",
2453 error->message);
2454 goto out;
2455 }
2456 priv->introspection_sensor = cd_main_load_introspection (COLORD_DBUS_INTERFACE_SENSOR ".xml",
2457 &error);
2458 if (priv->introspection_sensor == NULL) {
2459 g_warning ("CdMain: failed to load sensor introspection: %s",
2460 error->message);
2461 goto out;
2462 }
2463
2464 /* own the object */
2465 owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
2466 COLORD_DBUS_SERVICE,
2467 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
2468 G_BUS_NAME_OWNER_FLAGS_REPLACE,
2469 cd_main_on_bus_acquired_cb,
2470 cd_main_on_name_acquired_cb,
2471 cd_main_on_name_lost_cb,
2472 priv, NULL);
2473
2474 /* Only timeout and close the mainloop if we have specified it
2475 * on the command line */
2476 if (immediate_exit)
2477 g_idle_add (cd_main_timed_exit_cb, priv->loop);
2478 else if (timed_exit)
2479 g_timeout_add_seconds (5, cd_main_timed_exit_cb, priv->loop);
2480
2481 /* If the user has two or more outputs attached with identical EDID data
2482 * then the client tools cannot tell them apart. By setting this value
2483 * the 'xrandr-' style device-id is always used and the monitors will
2484 * show up as seporate instances.
2485 * This does of course mean that the calibration is referenced to the
2486 * xrandr output name, rather than the monitor itself. This means that
2487 * if the monitor cables are swapped then the wrong profile would be
2488 * used. */
2489 priv->always_use_xrandr_name = cd_main_check_duplicate_edids ();
2490
2491 /* load plugins */
2492 priv->plugins = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_main_plugin_free);
2493 cd_main_load_plugins (priv);
2494 cd_main_plugin_phase (priv, CD_PLUGIN_PHASE_INIT);
2495
2496 /* get system DMI info */
2497 cd_main_dmi_setup (priv);
2498 g_debug ("System vendor: '%s', System model: '%s'",
2499 priv->system_vendor, priv->system_model);
2500
2501 /* wait */
2502 g_info ("Daemon ready for requests");
2503 g_main_loop_run (priv->loop);
2504
2505 /* run the plugins */
2506 cd_main_plugin_phase (priv, CD_PLUGIN_PHASE_DESTROY);
2507
2508 /* success */
2509 retval = 0;
2510 out:
2511 cd_debug_destroy ();
2512 g_option_context_free (context);
2513 if (owner_id > 0)
2514 g_bus_unown_name (owner_id);
2515 if (priv != NULL) {
2516 if (priv->loop != NULL)
2517 g_main_loop_unref (priv->loop);
2518 if (priv->sensors != NULL)
2519 g_ptr_array_unref (priv->sensors);
2520 if (priv->plugins != NULL)
2521 g_ptr_array_unref (priv->plugins);
2522 if (priv->sensor_client != NULL)
2523 g_object_unref (priv->sensor_client);
2524 if (priv->icc_store != NULL)
2525 g_object_unref (priv->icc_store);
2526 if (priv->mapping_db != NULL)
2527 g_object_unref (priv->mapping_db);
2528 if (priv->device_db != NULL)
2529 g_object_unref (priv->device_db);
2530 if (priv->profile_db != NULL)
2531 g_object_unref (priv->profile_db);
2532 if (priv->devices_array != NULL)
2533 g_object_unref (priv->devices_array);
2534 if (priv->profiles_array != NULL)
2535 g_object_unref (priv->profiles_array);
2536 if (priv->connection != NULL)
2537 g_object_unref (priv->connection);
2538 if (priv->introspection_daemon != NULL)
2539 g_dbus_node_info_unref (priv->introspection_daemon);
2540 if (priv->introspection_device != NULL)
2541 g_dbus_node_info_unref (priv->introspection_device);
2542 if (priv->introspection_profile != NULL)
2543 g_dbus_node_info_unref (priv->introspection_profile);
2544 if (priv->introspection_sensor != NULL)
2545 g_dbus_node_info_unref (priv->introspection_sensor);
2546 g_free (priv->system_vendor);
2547 g_free (priv->system_model);
2548 g_free (priv);
2549 }
2550 return retval;
2551 }
2552