Debian Codegraph @

examples/cd-libcolord-find-device-by-property.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011-2012 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 //gcc -o cd-libcolord-find-device-by-property cd-libcolord-find-device-by-property.c `pkg-config --cflags --libs colord` -Wall
23
24 #include <stdlib.h>
25 #include <colord.h>
26
27 int
28 main (int argc, char *argv[])
29 {
30 CdClient *client = NULL;
31 CdDevice *device = NULL;
32 CdProfile *profile = NULL;
33 const gchar *filename;
34 gboolean ret;
35 gint retval = EXIT_FAILURE;
36 GError *error = NULL;
37
38 /* check user sanity */
39 if (argc < 2) {
40 g_warning ("usage: cd-libcolord-find-device-by-property.c <output-name>, e.g. LVDS1");
41 goto out;
42 }
43
44 /* setup GType system */
45 g_type_init ();
46
47 /* connects to the daemon */
48 client = cd_client_new ();
49 ret = cd_client_connect_sync (client, NULL, &error);
50 if (!ret) {
51 g_warning ("failed to contact colord: %s", error->message);
52 g_error_free (error);
53 goto out;
54 }
55
56 /* finds the colord device which has a specific property */
57 device = cd_client_find_device_by_property_sync (client,
58 CD_DEVICE_METADATA_XRANDR_NAME,
59 argv[1],
60 NULL,
61 &error);
62 if (device == NULL) {
63 g_warning ("no device with that property: %s", error->message);
64 g_error_free (error);
65 goto out;
66 }
67
68 /* get details about the device */
69 ret = cd_device_connect_sync (device, NULL, &error);
70 if (!ret) {
71 g_warning ("failed to get properties from the device: %s",
72 error->message);
73 g_error_free (error);
74 goto out;
75 }
76
77 /* get the default profile for the device */
78 profile = cd_device_get_default_profile (device);
79 if (profile == NULL) {
80 g_warning ("no default profile for device: %s", argv[1]);
81 goto out;
82 }
83
84 /* get details about the profile */
85 ret = cd_profile_connect_sync (profile, NULL, &error);
86 if (!ret) {
87 g_warning ("failed to get properties from the profile: %s",
88 error->message);
89 g_error_free (error);
90 goto out;
91 }
92
93 /* get the filename of the profile */
94 filename = cd_profile_get_filename (profile);
95 if (filename == NULL) {
96 g_warning ("profile has no physical file, must be virtual");
97 goto out;
98 }
99
100 /* SUCCESS! */
101 g_debug ("profile filename for %s: %s", argv[1], filename);
102 retval = EXIT_SUCCESS;
103
104 out:
105 if (device != NULL)
106 g_object_unref (device);
107 if (profile != NULL)
108 g_object_unref (profile);
109 if (client != NULL)
110 g_object_unref (client);
111 return retval;
112 }