Debian Codegraph @

src/plugins/cd-plugin-sane.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright © Christopher James Halse Rogers <[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 #include <cd-plugin.h>
24 #include <gudev/gudev.h>
25 #include <glib.h>
26
27 struct CdPluginPrivate {
28 GUdevClient *udev_client;
29 gboolean scan_in_progress;
30 };
31
32 const gchar *
33 cd_plugin_get_description (void)
34 {
35 return "Add and remove scanner devices using SANE";
36 }
37
38 gboolean
39 cd_plugin_config_enabled (void)
40 {
41 #ifdef HAVE_SANE
42 return TRUE;
43 #else
44 return FALSE;
45 #endif
46 }
47
48 /*
49 * cd_plugin_colord_sane_finished_cb
50 */
51 static void
52 cd_plugin_colord_sane_finished_cb(GPid pid,
53 gint status,
54 gpointer user_data)
55 {
56 CdPluginPrivate *priv = (CdPluginPrivate *)user_data;
57
58 priv->scan_in_progress = FALSE;
59 g_spawn_close_pid (pid);
60 }
61
62 /*
63 * cd_plugin_config_enabled_sane_devices
64 */
65 static void
66 cd_plugin_config_enabled_sane_devices(CdPluginPrivate *priv)
67 {
68 const gchar *argv[] = {COLORD_SANE_BINARY, NULL};
69 GPid colord_sane_pid;
70 g_autoptr(GError) error = NULL;
71
72 if (priv->scan_in_progress)
73 return;
74 g_spawn_async (NULL,
75 (gchar **) argv,
76 NULL,
77 G_SPAWN_DO_NOT_REAP_CHILD,
78 NULL,
79 NULL,
80 &colord_sane_pid,
81 &error);
82
83 if (error != NULL) {
84 g_warning ("CdPlugin: failed to spawn colord-sane helper: %s",
85 error->message);
86 return;
87 }
88
89 priv->scan_in_progress = TRUE;
90 g_child_watch_add (colord_sane_pid,
91 cd_plugin_colord_sane_finished_cb,
92 priv);
93 }
94
95
96 static void
97 cd_plugin_uevent_cb (GUdevClient *udev_client,
98 const gchar *action,
99 GUdevDevice *udev_device,
100 CdPlugin *plugin)
101 {
102 if (g_strcmp0 (action, "remove") == 0 ||
103 g_strcmp0 (action, "add") == 0) {
104 cd_plugin_config_enabled_sane_devices (plugin->priv);
105 }
106 }
107
108 void
109 cd_plugin_coldplug (CdPlugin *plugin)
110 {
111 cd_plugin_config_enabled_sane_devices (plugin->priv);
112
113 /* watch udev for changes */
114 g_signal_connect (plugin->priv->udev_client, "uevent",
115 G_CALLBACK (cd_plugin_uevent_cb), plugin);
116 }
117
118 void
119 cd_plugin_initialize (CdPlugin *plugin)
120 {
121 const gchar *subsystems[] = { "usb", "scsi", NULL };
122
123 /* create private */
124 plugin->priv = CD_PLUGIN_GET_PRIVATE (CdPluginPrivate);
125 plugin->priv->udev_client = g_udev_client_new (subsystems);
126 plugin->priv->scan_in_progress = FALSE;
127 }
128
129 void
130 cd_plugin_destroy (CdPlugin *plugin)
131 {
132 g_object_unref (plugin->priv->udev_client);
133 }