File: hal-backend.c

package info (click to toggle)
libgpod 0.8.2-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,532 kB
  • sloc: ansic: 26,876; sh: 11,215; cs: 1,399; python: 1,251; makefile: 472; cpp: 296; xml: 100
file content (461 lines) | stat: -rw-r--r-- 13,209 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* Copyright (c) 2007, 2010 Christophe Fergeau  <teuf@gnome.org>
 * Part of the libgpod project.
 * 
 * URL: http://www.gtkpod.org/
 * URL: http://gtkpod.sourceforge.net/
 *
 * The code contained in this file is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * iTunes and iPod are trademarks of Apple
 *
 * This product is not supported/written/published by Apple!
 *
 */
#include "config.h"
#include "backends.h"

#include <libhal.h>
#include <itdb_device.h>
#include <glib-object.h>
#include <string.h>

#define LIBGPOD_HAL_NS "org.libgpod."

struct _HalBackend {
	ItdbBackend parent;
	LibHalContext *ctx;
	char *udi;
};
typedef struct _HalBackend HalBackend;


static void hal_backend_destroy (ItdbBackend *itdb_backend)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
	g_free (backend->udi);
	libhal_ctx_free(backend->ctx);
	g_free (backend);
}

static gboolean
hal_backend_set_version (ItdbBackend *itdb_backend, unsigned int version)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
        libhal_device_set_property_int (backend->ctx, backend->udi, 
			                LIBGPOD_HAL_NS"version", 1, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_is_unknown (ItdbBackend *itdb_backend, gboolean unknown)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
	libhal_device_set_property_bool (backend->ctx, backend->udi,
					 LIBGPOD_HAL_NS"ipod.is_unknown", 
					 TRUE, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_icon_name (ItdbBackend *itdb_backend, const char *icon_name)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
	libhal_device_set_property_string (backend->ctx, backend->udi,
					   "info.desktop.icon",
					   icon_name, NULL);

	return TRUE;
}

static gboolean
hal_backend_set_firewire_id (ItdbBackend *itdb_backend, const char *fwid)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
     	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.firewire_id",
					   fwid, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_serial_number (ItdbBackend *itdb_backend, const char *serial_number)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.serial_number",
					   serial_number,
					   NULL);
	return TRUE;
}

static gboolean hal_backend_set_firmware_version (ItdbBackend *itdb_backend,
						  const char *firmware_version)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.firmware_version",
					   firmware_version,
					   NULL);
	return TRUE;
}

static gboolean
hal_backend_set_model_name (ItdbBackend *itdb_backend, const char *model_name)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.model.device_class",
					   model_name,
					   NULL);
	return TRUE;
}

static gboolean
hal_backend_set_generation (ItdbBackend *itdb_backend, gdouble generation)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_double (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.model.generation",
					   generation,
					   NULL);
	return TRUE;
}

static gboolean
hal_backend_set_color (ItdbBackend *itdb_backend, const char *color_name)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.model.shell_color",
					   color_name,
					   NULL);
	return TRUE;
}

static gboolean
hal_backend_set_factory_id (ItdbBackend *itdb_backend,
			    const char *factory_id)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.production.factory_id",
					   factory_id,
					   NULL);
	return TRUE;
}

static gboolean
hal_backend_set_production_year (ItdbBackend *itdb_backend, guint year)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_int (backend->ctx, backend->udi, 
					LIBGPOD_HAL_NS"ipod.production.year",
					year, NULL);
	return TRUE;
}

static gboolean 
hal_backend_set_production_week (ItdbBackend *itdb_backend, guint week)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_int (backend->ctx, backend->udi, 
					LIBGPOD_HAL_NS"ipod.production.week",
					week, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_production_index (ItdbBackend *itdb_backend, guint index)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_int (backend->ctx, backend->udi, 
					LIBGPOD_HAL_NS"ipod.production.number",
					index, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_control_path (ItdbBackend *itdb_backend, const char *control_path)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   LIBGPOD_HAL_NS"ipod.model.control_path",
					   control_path, NULL);
	return TRUE;
}

static gboolean
hal_backend_set_name (ItdbBackend *itdb_backend, const char *name)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	libhal_device_set_property_string (backend->ctx, backend->udi,
					   "info.desktop.name",
					   name, NULL);
	return TRUE;
}

static char *
get_format_string (const Itdb_ArtworkFormat *format, enum ArtworkType type)
{
	const char *format_name;
	const char *artwork_type;

	g_return_val_if_fail (format != NULL, NULL);
	switch (format->format) {
            case THUMB_FORMAT_UYVY_BE:
		    format_name = "iyuv";
		    break;
            case THUMB_FORMAT_RGB565_BE:
		    format_name = "rgb565_be";
		    break;		 
	    case THUMB_FORMAT_RGB565_LE:
		    format_name = "rgb565";
		    break;
	    case THUMB_FORMAT_I420_LE:
		    format_name = "iyuv420";
		    break;
	    default:
		    g_return_val_if_reached (NULL);
	}

	switch (type) {
            case UNKNOWN:
		    artwork_type = "unknown";
		    break;
            case PHOTO:
	            artwork_type = "photo";
		    break;
	    case ALBUM:
		    artwork_type = "album";
		    break;
	    case CHAPTER:
		    artwork_type = "chapter";
		    break;
	    default:
		    g_return_val_if_reached (NULL);
	}

	return g_strdup_printf ("corr_id=%u,width=%u,height=%u,rotation=%u,pixel_format=%s,image_type=%s",
  		                format->format_id, 
				format->width, format->height, 
			        format->rotation, format_name, artwork_type);
}

static gboolean hal_backend_set_artwork_type_supported (ItdbBackend *itdb_backend,
							enum ArtworkType type,
							gboolean supported)
{
	HalBackend *backend = (HalBackend *)itdb_backend;
    	char *propname;
	switch (type) {
	    case ALBUM:
		propname = LIBGPOD_HAL_NS"ipod.images.album_art_supported";
		break;
	    case PHOTO:
		propname = LIBGPOD_HAL_NS"ipod.images.photos_supported";
		break;
	    case CHAPTER:
		propname = LIBGPOD_HAL_NS"ipod.images.chapter_images_supported";
		break;
	    default:
		g_return_val_if_reached (FALSE);
	}
        libhal_device_set_property_bool (backend->ctx, backend->udi,
                                         propname, supported, NULL);
	return TRUE;
}

static gboolean hal_backend_set_artwork_formats (ItdbBackend *itdb_backend,
						 enum ArtworkType type,
						 const GList *formats)
{
	HalBackend *backend = (HalBackend *)itdb_backend;

	const GList *it;
	hal_backend_set_artwork_type_supported (itdb_backend, type,
					       	(formats != NULL));

        for (it = formats; it != NULL; it = it->next) {
                char *format_str;

                format_str = get_format_string (it->data, type);
                libhal_device_property_strlist_append (backend->ctx, backend->udi,
                                                       LIBGPOD_HAL_NS"ipod.images.formats",
                                                       format_str, 
                                                       NULL);
                g_free (format_str);
        }

	return TRUE;
}

/* taken from libipoddevice proper */
static ItdbBackend *hal_backend_new (void) 
{
	LibHalContext *hal_context;
	HalBackend *backend;
	const char *udi;
	DBusError error;
	DBusConnection *dbus_connection;

        udi = g_getenv ("UDI");
	if (udi == NULL) {
	    	return NULL;
	}

	hal_context = libhal_ctx_new();
	if(hal_context == NULL) {
		return NULL;
	}

	dbus_error_init(&error);
	dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if (dbus_error_is_set(&error)) {
		dbus_error_free(&error);
		libhal_ctx_free(hal_context);
		return NULL;
	}

	libhal_ctx_set_dbus_connection(hal_context, dbus_connection);

	if(!libhal_ctx_init(hal_context, &error)) {
		if (dbus_error_is_set(&error)) {
			dbus_error_free(&error);
		}
		libhal_ctx_free(hal_context);
		return NULL;
	}

	backend = g_new0 (HalBackend, 1);
	backend->ctx = hal_context;
	backend->udi = g_strdup (udi);

	backend->parent.destroy = hal_backend_destroy;
	backend->parent.set_version = hal_backend_set_version;
	backend->parent.set_is_unknown = hal_backend_set_is_unknown;
	backend->parent.set_icon_name = hal_backend_set_icon_name;
	backend->parent.set_firewire_id = hal_backend_set_firewire_id;
	backend->parent.set_serial_number = hal_backend_set_serial_number;
	backend->parent.set_firmware_version = hal_backend_set_firmware_version;
	backend->parent.set_model_name = hal_backend_set_model_name;
	backend->parent.set_generation = hal_backend_set_generation;
	backend->parent.set_color = hal_backend_set_color;
	backend->parent.set_factory_id = hal_backend_set_factory_id;
	backend->parent.set_production_year = hal_backend_set_production_year;
	backend->parent.set_production_week = hal_backend_set_production_week;
	backend->parent.set_production_index = hal_backend_set_production_index;
	backend->parent.set_control_path = hal_backend_set_control_path;
	backend->parent.set_name = hal_backend_set_name;
	backend->parent.set_artwork_formats = hal_backend_set_artwork_formats;

	return (ItdbBackend *)backend;
}

#ifdef HAVE_LIBUSB
static gboolean hal_get_ipod_usb_position (LibHalContext *ctx, const char *udi,
                                           int *usb_bus_number, int *usb_device_number)
{
	char *parent_udi;
	char *subsystem;
	gboolean found_ids;
	DBusError error;


	parent_udi = NULL;
	subsystem = NULL;
	found_ids = FALSE;
	dbus_error_init (&error);
	while (TRUE) {
		parent_udi = libhal_device_get_property_string (ctx, udi,
				"info.parent", &error);
		if (parent_udi == NULL || dbus_error_is_set (&error))
			goto end;
		udi = parent_udi;
		subsystem = libhal_device_get_property_string (ctx, udi,
							       "linux.subsystem",
							       &error);
		if (subsystem == NULL || dbus_error_is_set (&error)) {
			dbus_error_free (&error);
			dbus_error_init (&error);
			continue;
		}
		if (strcmp (subsystem, "usb") == 0) {
			*usb_bus_number = libhal_device_get_property_int (ctx, udi,
									  "usb.bus_number", &error);
			if (dbus_error_is_set (&error)) {
				goto end;
			}
			*usb_device_number = libhal_device_get_property_int (ctx, udi,
									     "usb.linux.device_number", &error);
			if (dbus_error_is_set (&error)) {
				goto end;
			}
			found_ids = TRUE;
			goto end;
		}
	}
end:
	libhal_free_string (parent_udi);
	libhal_free_string (subsystem);
	if (dbus_error_is_set (&error)) {
	    g_print ("Error: %s\n", error.message);
	    dbus_error_free (&error);
	}

	return found_ids;
}
#endif

int main (int argc, char **argv)
{
	ItdbBackend *backend;
	int result;
	const char *dev;
	const char *fstype;
        gint usb_bus_number = 0;
        gint usb_device_number = 0;
        HalBackend *hal_backend;

	g_type_init ();

	backend = hal_backend_new ();
        if (backend == NULL) {
                return -1;
        }

	dev = g_getenv ("HAL_PROP_BLOCK_DEVICE");
	if (dev == NULL) {
		return -1;
	}

        fstype = g_getenv ("HAL_PROP_VOLUME_FSTYPE");
        if (fstype == NULL) {
                return -1;
        }

        hal_backend = (HalBackend *)backend;
#ifdef HAVE_LIBUSB
        hal_get_ipod_usb_position (hal_backend->ctx, hal_backend->udi,
                                   &usb_bus_number, &usb_device_number);
#endif

	result = itdb_callout_set_ipod_properties (backend, dev,
                                                   usb_bus_number,
                                                   usb_device_number,
                                                   fstype);
	backend->destroy (backend);

	return result;
}