| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2014 Richard Hughes <[email protected]> |
| 4 |
* Copyright (C) 2013 Christopher James Halse Rogers <[email protected]> |
| 5 |
* |
| 6 |
* Licensed under the GNU General Public License Version 2 |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <gio/gio.h> |
| 26 |
#include <glib/gi18n.h> |
| 27 |
#include <locale.h> |
| 28 |
#include <sane/sane.h> |
| 29 |
#include <gudev/gudev.h> |
| 30 |
#include <dbus/dbus.h> |
| 31 |
#include <colord/colord.h> |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
gchar *argv0; |
| 35 |
CdClient *client; |
| 36 |
GMainLoop *loop; |
| 37 |
GPtrArray *array; /* of CdMainDev's */ |
| 38 |
} CdMainPrivate; |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
CdDevice *device; |
| 42 |
gchar *id; /* note: we can get this from CdDevice, but we don't wan't to connect() */ |
| 43 |
gboolean valid; |
| 44 |
} CdMainDev; |
| 45 |
|
| 46 |
static void |
| 47 |
cd_main_dev_free (CdMainDev *tmp) |
| 48 |
{ |
| 49 |
g_object_unref (tmp->device); |
| 50 |
g_free (tmp->id); |
| 51 |
g_free (tmp); |
| 52 |
} |
| 53 |
|
| 54 |
static CdMainDev * |
| 55 |
cd_main_dev_find_by_id (CdMainPrivate *priv, |
| 56 |
const gchar *id) |
| 57 |
{ |
| 58 |
CdMainDev *tmp; |
| 59 |
guint i; |
| 60 |
|
| 61 |
/* nothing to find */ |
| 62 |
if (priv->array->len == 0) |
| 63 |
return NULL; |
| 64 |
for (i = 0; i < priv->array->len; i++) { |
| 65 |
tmp = g_ptr_array_index (priv->array, i); |
| 66 |
if (g_strcmp0 (tmp->id, id) == 0) |
| 67 |
return tmp; |
| 68 |
} |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
static gchar * |
| 73 |
cd_client_get_id_for_sane_device (const SANE_Device *sane_device) |
| 74 |
{ |
| 75 |
return g_strdup_printf ("sane-%s", sane_device->model); |
| 76 |
} |
| 77 |
|
| 78 |
typedef struct { |
| 79 |
CdMainPrivate *priv; |
| 80 |
gchar *id; |
| 81 |
} CdMainCreateDeviceHelper; |
| 82 |
|
| 83 |
static void |
| 84 |
cd_main_colord_create_device_cb (GObject *source_object, |
| 85 |
GAsyncResult *res, |
| 86 |
gpointer user_data) |
| 87 |
{ |
| 88 |
CdClient *client = CD_CLIENT (source_object); |
| 89 |
g_autoptr(GError) error = NULL; |
| 90 |
g_autoptr(CdDevice) device = NULL; |
| 91 |
|
| 92 |
/* get result */ |
| 93 |
device = cd_client_create_device_finish (client, res, &error); |
| 94 |
if (device == NULL) |
| 95 |
g_warning ("failed to create device: %s", error->message); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
cd_sane_client_add (CdMainPrivate *priv, const SANE_Device *sane_device) |
| 100 |
{ |
| 101 |
CdMainDev *dev; |
| 102 |
g_autofree gchar *id = NULL; |
| 103 |
g_autofree gchar *model = NULL; |
| 104 |
g_autofree gchar *vendor = NULL; |
| 105 |
g_autoptr(GHashTable) properties = NULL; |
| 106 |
|
| 107 |
/* ignore noname, no support devices */ |
| 108 |
if (g_strcmp0 (sane_device->vendor, "Noname") == 0) { |
| 109 |
g_debug ("CdSaneClient: Ignoring sane device %s", |
| 110 |
sane_device->name); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
/* convert device_id 'plustek:libusb:004:002' to suitable id */ |
| 115 |
id = cd_client_get_id_for_sane_device (sane_device); |
| 116 |
|
| 117 |
/* see if this device already exists */ |
| 118 |
dev = cd_main_dev_find_by_id (priv, id); |
| 119 |
if (dev != NULL) { |
| 120 |
dev->valid = TRUE; |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
/* Make human readable */ |
| 125 |
model = g_strdup (sane_device->model); |
| 126 |
g_strdelimit (model, "_", ' '); |
| 127 |
vendor = g_strdup (sane_device->vendor); |
| 128 |
g_strdelimit (vendor, "_", ' '); |
| 129 |
|
| 130 |
/* create initial device properties */ |
| 131 |
properties = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 132 |
NULL, NULL); |
| 133 |
g_hash_table_insert (properties, |
| 134 |
(gpointer) CD_DEVICE_PROPERTY_KIND, |
| 135 |
(gpointer) cd_device_kind_to_string (CD_DEVICE_KIND_SCANNER)); |
| 136 |
g_hash_table_insert (properties, |
| 137 |
(gpointer) CD_DEVICE_PROPERTY_MODE, |
| 138 |
(gpointer) cd_device_mode_to_string (CD_DEVICE_MODE_PHYSICAL)); |
| 139 |
g_hash_table_insert (properties, |
| 140 |
(gpointer) CD_DEVICE_PROPERTY_COLORSPACE, |
| 141 |
(gpointer) cd_colorspace_to_string (CD_COLORSPACE_RGB)); |
| 142 |
g_hash_table_insert (properties, |
| 143 |
(gpointer) CD_DEVICE_PROPERTY_VENDOR, |
| 144 |
(gpointer) vendor); |
| 145 |
g_hash_table_insert (properties, |
| 146 |
(gpointer) CD_DEVICE_PROPERTY_MODEL, |
| 147 |
(gpointer) model); |
| 148 |
g_hash_table_insert (properties, |
| 149 |
(gpointer) CD_DEVICE_PROPERTY_SERIAL, |
| 150 |
(gpointer) sane_device->name); |
| 151 |
#if 0 |
| 152 |
g_hash_table_insert (properties, |
| 153 |
(gpointer) CD_DEVICE_METADATA_OWNER_CMDLINE, |
| 154 |
(gpointer) priv->argv0); |
| 155 |
#endif |
| 156 |
cd_client_create_device (priv->client, |
| 157 |
id, |
| 158 |
CD_OBJECT_SCOPE_NORMAL, |
| 159 |
properties, |
| 160 |
NULL, |
| 161 |
cd_main_colord_create_device_cb, |
| 162 |
NULL); |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
cd_main_colord_delete_device_cb (GObject *source_object, |
| 167 |
GAsyncResult *res, |
| 168 |
gpointer user_data) |
| 169 |
{ |
| 170 |
CdClient *client = CD_CLIENT (source_object); |
| 171 |
g_autoptr(GError) error = NULL; |
| 172 |
|
| 173 |
/* get result */ |
| 174 |
if (!cd_client_delete_device_finish (client, res, &error)) |
| 175 |
g_warning ("failed to delete device: %s", error->message); |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
cd_sane_client_remove (CdMainPrivate *priv, CdDevice *device) |
| 180 |
{ |
| 181 |
g_debug ("Deleting device: %s", cd_device_get_object_path (device)); |
| 182 |
cd_client_delete_device (priv->client, |
| 183 |
device, |
| 184 |
NULL, |
| 185 |
cd_main_colord_delete_device_cb, |
| 186 |
priv); |
| 187 |
} |
| 188 |
|
| 189 |
static void |
| 190 |
cd_sane_client_refresh (CdMainPrivate *priv) |
| 191 |
{ |
| 192 |
CdMainDev *tmp; |
| 193 |
const SANE_Device **device_list = NULL; |
| 194 |
gint idx; |
| 195 |
guint i; |
| 196 |
SANE_Status status; |
| 197 |
|
| 198 |
status = sane_init (NULL, NULL); |
| 199 |
if (status != SANE_STATUS_GOOD) { |
| 200 |
g_warning ("failed to init SANE: %s", |
| 201 |
sane_strstatus (status)); |
| 202 |
goto out; |
| 203 |
} |
| 204 |
|
| 205 |
/* get scanners on the local server */ |
| 206 |
status = sane_get_devices (&device_list, TRUE); |
| 207 |
if (status != SANE_STATUS_GOOD) { |
| 208 |
g_warning ("failed to get devices from SANE: %s", |
| 209 |
sane_strstatus (status)); |
| 210 |
goto out; |
| 211 |
} |
| 212 |
|
| 213 |
/* nothing */ |
| 214 |
if (device_list == NULL || device_list[0] == NULL) |
| 215 |
goto out; |
| 216 |
|
| 217 |
/* add them */ |
| 218 |
for (idx = 0; device_list[idx] != NULL; idx++) |
| 219 |
cd_sane_client_add (priv, device_list[idx]); |
| 220 |
|
| 221 |
/* remove any that are invalid */ |
| 222 |
for (i = 0; i < priv->array->len; i++) { |
| 223 |
tmp = g_ptr_array_index (priv->array, i); |
| 224 |
if (tmp->valid) |
| 225 |
continue; |
| 226 |
cd_sane_client_remove (priv, tmp->device); |
| 227 |
} |
| 228 |
out: |
| 229 |
g_main_loop_quit (priv->loop); |
| 230 |
} |
| 231 |
|
| 232 |
static void |
| 233 |
cd_sane_add_device_if_from_colord_sane (gpointer data, |
| 234 |
gpointer user_data) |
| 235 |
{ |
| 236 |
CdDevice *device = (CdDevice *) data; |
| 237 |
CdMainDev *sane_device; |
| 238 |
CdMainPrivate *priv = (CdMainPrivate *) user_data; |
| 239 |
const gchar *cmdline; |
| 240 |
gboolean ret; |
| 241 |
g_autoptr(GError) error = NULL; |
| 242 |
|
| 243 |
ret = cd_device_connect_sync (device, NULL, &error); |
| 244 |
|
| 245 |
if (!ret) { |
| 246 |
g_warning ("failed to receive list of devices: %s", |
| 247 |
error->message); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
cmdline = cd_device_get_metadata_item (device, |
| 252 |
CD_DEVICE_METADATA_OWNER_CMDLINE); |
| 253 |
if (g_strcmp0 (cmdline, priv->argv0) == 0) { |
| 254 |
sane_device = g_new (CdMainDev, 1); |
| 255 |
sane_device->device = g_object_ref (device); |
| 256 |
sane_device->id = (gchar *)cd_device_get_id (device); |
| 257 |
sane_device->valid = FALSE; |
| 258 |
g_ptr_array_add (priv->array, sane_device); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
cd_sane_populate_existing_devices_cb (GObject *source_object, |
| 264 |
GAsyncResult *res, |
| 265 |
gpointer user_data) |
| 266 |
{ |
| 267 |
CdMainPrivate *priv = (CdMainPrivate *) user_data; |
| 268 |
GPtrArray *devices; |
| 269 |
g_autoptr(GError) error = NULL; |
| 270 |
|
| 271 |
devices = cd_client_get_devices_by_kind_finish (priv->client, res, &error); |
| 272 |
if (error != NULL) { |
| 273 |
g_warning ("failed to receive list of devices: %s", |
| 274 |
error->message); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
g_ptr_array_foreach (devices, |
| 279 |
cd_sane_add_device_if_from_colord_sane, |
| 280 |
priv); |
| 281 |
|
| 282 |
cd_sane_client_refresh (priv); |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
cd_main_colord_connect_cb (GObject *source_object, |
| 287 |
GAsyncResult *res, |
| 288 |
gpointer user_data) |
| 289 |
{ |
| 290 |
CdMainPrivate *priv = (CdMainPrivate *) user_data; |
| 291 |
gboolean ret; |
| 292 |
g_autoptr(GError) error = NULL; |
| 293 |
|
| 294 |
/* get result */ |
| 295 |
ret = cd_client_connect_finish (priv->client, res, &error); |
| 296 |
if (!ret) { |
| 297 |
g_warning ("failed to connect to colord: %s", |
| 298 |
error->message); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
cd_client_get_devices_by_kind (priv->client, |
| 303 |
CD_DEVICE_KIND_SCANNER, |
| 304 |
NULL, |
| 305 |
cd_sane_populate_existing_devices_cb, |
| 306 |
priv); |
| 307 |
} |
| 308 |
|
| 309 |
int |
| 310 |
main (int argc, char *argv[]) |
| 311 |
{ |
| 312 |
CdMainPrivate *priv = NULL; |
| 313 |
guint retval = 1; |
| 314 |
|
| 315 |
/* We need to init DBus' threading support as libSANE uses raw DBus */ |
| 316 |
dbus_threads_init_default (); |
| 317 |
|
| 318 |
/* create new objects */ |
| 319 |
priv = g_new0 (CdMainPrivate, 1); |
| 320 |
priv->loop = g_main_loop_new (NULL, FALSE); |
| 321 |
priv->client = cd_client_new (); |
| 322 |
priv->argv0 = g_strdup (argv[0]); |
| 323 |
priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_main_dev_free); |
| 324 |
|
| 325 |
/* connect to daemon */ |
| 326 |
cd_client_connect (priv->client, |
| 327 |
NULL, |
| 328 |
cd_main_colord_connect_cb, |
| 329 |
priv); |
| 330 |
|
| 331 |
/* process */ |
| 332 |
g_main_loop_run (priv->loop); |
| 333 |
|
| 334 |
/* success */ |
| 335 |
retval = 0; |
| 336 |
|
| 337 |
if (priv != NULL) { |
| 338 |
g_free (priv->argv0); |
| 339 |
if (priv->array != NULL) |
| 340 |
g_ptr_array_unref (priv->array); |
| 341 |
if (priv->client != NULL) |
| 342 |
g_object_unref (priv->client); |
| 343 |
g_main_loop_unref (priv->loop); |
| 344 |
} |
| 345 |
return retval; |
| 346 |
} |