Print this page
8807 More recent glib updates break hald build
Reviewed by: Dan McDonald <danmcd@joyent.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/hal/hald/device.c
+++ new/usr/src/cmd/hal/hald/device.c
1 1 /***************************************************************************
2 2 * CVSID: $Id$
3 3 *
4 4 * device.c : HalDevice methods
5 5 *
6 6 * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
7 7 * Copyright (C) 2004 Novell, Inc.
8 8 *
9 9 * Licensed under the Academic Free License version 2.1
10 10 *
11 11 * This program is free software; you can redistribute it and/or modify
12 12 * it under the terms of the GNU General Public License as published by
13 13 * the Free Software Foundation; either version 2 of the License, or
14 14 * (at your option) any later version.
15 15 *
16 16 * This program is distributed in the hope that it will be useful,
17 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 19 * GNU General Public License for more details.
20 20 *
21 21 * You should have received a copy of the GNU General Public License
22 22 * along with this program; if not, write to the Free Software
23 23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 24 *
25 25 **************************************************************************/
26 26
27 27 #ifdef HAVE_CONFIG_H
28 28 # include <config.h>
29 29 #endif
30 30
31 31 #include <stdio.h>
32 32 #include <string.h>
33 33
34 34 #include "hald.h"
35 35 #include "device.h"
36 36 #include "hald_marshal.h"
37 37 #include "logger.h"
38 38 #include "hald_runner.h"
39 39
40 40 static GObjectClass *parent_class;
41 41
42 42 enum {
43 43 PROPERTY_CHANGED,
44 44 CAPABILITY_ADDED,
45 45 CALLOUTS_FINISHED,
46 46 CANCELLED,
47 47 LAST_SIGNAL
48 48 };
49 49
50 50 static guint signals[LAST_SIGNAL] = { 0 };
51 51
52 52 #ifdef HALD_MEMLEAK_DBG
53 53 int dbg_hal_device_object_delta = 0;
54 54 #endif
55 55
56 56 static void
57 57 hal_device_finalize (GObject *obj)
58 58 {
59 59 HalDevice *device = HAL_DEVICE (obj);
60 60
61 61 runner_device_finalized (device);
62 62
63 63 #ifdef HALD_MEMLEAK_DBG
64 64 dbg_hal_device_object_delta--;
65 65 printf ("************* in finalize for udi=%s\n", device->udi);
66 66 #endif
67 67
68 68
69 69 g_slist_foreach (device->properties, (GFunc) hal_property_free, NULL);
70 70
71 71 g_slist_free (device->properties);
72 72
73 73 g_free (device->udi);
74 74
75 75 if (parent_class->finalize)
76 76 parent_class->finalize (obj);
77 77
78 78 }
79 79
80 80 static void
81 81 hal_device_class_init (HalDeviceClass *klass)
82 82 {
83 83 GObjectClass *obj_class = (GObjectClass *) klass;
84 84
85 85 parent_class = g_type_class_peek_parent (klass);
|
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
86 86
87 87 obj_class->finalize = hal_device_finalize;
88 88
89 89 signals[PROPERTY_CHANGED] =
90 90 g_signal_new ("property_changed",
91 91 G_TYPE_FROM_CLASS (klass),
92 92 G_SIGNAL_RUN_LAST,
93 93 G_STRUCT_OFFSET (HalDeviceClass,
94 94 property_changed),
95 95 NULL, NULL,
96 - hald_marshal_VOID__STRING_BOOL_BOOL,
96 + hald_marshal_VOID__STRING_BOOLEAN_BOOLEAN,
97 97 G_TYPE_NONE, 3,
98 98 G_TYPE_STRING,
99 99 G_TYPE_BOOLEAN,
100 100 G_TYPE_BOOLEAN);
101 101
102 102 signals[CAPABILITY_ADDED] =
103 103 g_signal_new ("capability_added",
104 104 G_TYPE_FROM_CLASS (klass),
105 105 G_SIGNAL_RUN_LAST,
106 106 G_STRUCT_OFFSET (HalDeviceClass,
107 107 capability_added),
108 108 NULL, NULL,
109 109 hald_marshal_VOID__STRING,
110 110 G_TYPE_NONE, 1,
111 111 G_TYPE_STRING);
112 112
113 113 signals[CALLOUTS_FINISHED] =
114 114 g_signal_new ("callouts_finished",
115 115 G_TYPE_FROM_CLASS (klass),
116 116 G_SIGNAL_RUN_LAST,
117 117 G_STRUCT_OFFSET (HalDeviceClass,
118 118 callouts_finished),
119 119 NULL, NULL,
120 120 hald_marshal_VOID__VOID,
121 121 G_TYPE_NONE, 0);
122 122
123 123 signals[CANCELLED] =
124 124 g_signal_new ("cancelled",
125 125 G_TYPE_FROM_CLASS (klass),
126 126 G_SIGNAL_RUN_LAST,
127 127 G_STRUCT_OFFSET (HalDeviceClass,
128 128 cancelled),
129 129 NULL, NULL,
130 130 hald_marshal_VOID__VOID,
131 131 G_TYPE_NONE, 0);
132 132 }
133 133
134 134 static void
135 135 hal_device_init (HalDevice *device)
136 136 {
137 137 static int temp_device_counter = 0;
138 138
139 139 device->udi = g_strdup_printf ("/org/freedesktop/Hal/devices/temp/%d",
140 140 temp_device_counter++);
141 141 device->num_addons = 0;
142 142 device->num_addons_ready = 0;
143 143 }
144 144
145 145 GType
146 146 hal_device_get_type (void)
147 147 {
148 148 static GType type = 0;
149 149
150 150 if (!type) {
151 151 static GTypeInfo type_info = {
152 152 sizeof (HalDeviceClass),
153 153 NULL, NULL,
154 154 (GClassInitFunc) hal_device_class_init,
155 155 NULL, NULL,
156 156 sizeof (HalDevice),
157 157 0,
158 158 (GInstanceInitFunc) hal_device_init,
159 159 NULL
160 160 };
161 161
162 162 type = g_type_register_static (G_TYPE_OBJECT,
163 163 "HalDevice",
164 164 &type_info,
165 165 0);
166 166 }
167 167
168 168 return type;
169 169 }
170 170
171 171
172 172 HalDevice *
173 173 hal_device_new (void)
174 174 {
175 175 HalDevice *device;
176 176
177 177 device = g_object_new (HAL_TYPE_DEVICE, NULL, NULL);
178 178
179 179 #ifdef HALD_MEMLEAK_DBG
180 180 dbg_hal_device_object_delta++;
181 181 #endif
182 182 return device;
183 183 }
184 184
185 185 /** Merge all properties from source where the key starts with
186 186 * source_namespace and put them onto target replacing source_namespace
187 187 * with target_namespace
188 188 *
189 189 * @param target Device to put properties onto
190 190 * @param source Device to retrieve properties from
191 191 * @param target_namespace Replace source namespace with this namespace
192 192 * @param source_namespace Source namespace that property keys must match
193 193 */
194 194 void
195 195 hal_device_merge_with_rewrite (HalDevice *target,
196 196 HalDevice *source,
197 197 const char *target_namespace,
198 198 const char *source_namespace)
199 199 {
200 200 GSList *iter;
201 201 size_t source_ns_len;
202 202
203 203 source_ns_len = strlen (source_namespace);
204 204
205 205 /* doesn't handle info.capabilities */
206 206
207 207 /* device_property_atomic_update_begin (); */
208 208
209 209 for (iter = source->properties; iter != NULL; iter = iter->next) {
210 210 HalProperty *p = iter->data;
211 211 int type;
212 212 const char *key;
213 213 int target_type;
214 214 gchar *target_key;
215 215
216 216 key = hal_property_get_key (p);
217 217
218 218 /* only care about properties that match source namespace */
219 219 if (strncmp(key, source_namespace, source_ns_len) != 0)
220 220 continue;
221 221
222 222 target_key = g_strdup_printf("%s%s", target_namespace,
223 223 key+source_ns_len);
224 224
225 225 type = hal_property_get_type (p);
226 226
227 227 /* only remove target if it exists with a different type */
228 228 target_type = hal_device_property_get_type (target, key);
229 229 if (target_type != HAL_PROPERTY_TYPE_INVALID && target_type != type)
230 230 hal_device_property_remove (target, key);
231 231
232 232 switch (type) {
233 233
234 234 case HAL_PROPERTY_TYPE_STRING:
235 235 hal_device_property_set_string (
236 236 target, target_key,
237 237 hal_property_get_string (p));
238 238 break;
239 239
240 240 case HAL_PROPERTY_TYPE_INT32:
241 241 hal_device_property_set_int (
242 242 target, target_key,
243 243 hal_property_get_int (p));
244 244 break;
245 245
246 246 case HAL_PROPERTY_TYPE_UINT64:
247 247 hal_device_property_set_uint64 (
248 248 target, target_key,
249 249 hal_property_get_uint64 (p));
250 250 break;
251 251
252 252 case HAL_PROPERTY_TYPE_BOOLEAN:
253 253 hal_device_property_set_bool (
254 254 target, target_key,
255 255 hal_property_get_bool (p));
256 256 break;
257 257
258 258 case HAL_PROPERTY_TYPE_DOUBLE:
259 259 hal_device_property_set_double (
260 260 target, target_key,
261 261 hal_property_get_double (p));
262 262 break;
263 263
264 264 default:
265 265 HAL_WARNING (("Unknown property type %d", type));
266 266 break;
267 267 }
268 268
269 269 g_free (target_key);
270 270 }
271 271
272 272 /* device_property_atomic_update_end (); */
273 273
274 274 }
275 275
276 276 void
277 277 hal_device_merge (HalDevice *target, HalDevice *source)
278 278 {
279 279 GSList *iter;
280 280 GSList *caps;
281 281
282 282 /* device_property_atomic_update_begin (); */
283 283
284 284 for (iter = source->properties; iter != NULL; iter = iter->next) {
285 285 HalProperty *p = iter->data;
286 286 int type;
287 287 const char *key;
288 288 int target_type;
289 289
290 290 key = hal_property_get_key (p);
291 291 type = hal_property_get_type (p);
292 292
293 293 /* handle info.capabilities in a special way */
294 294 if (strcmp (key, "info.capabilities") == 0)
295 295 continue;
296 296
297 297 /* only remove target if it exists with a different type */
298 298 target_type = hal_device_property_get_type (target, key);
299 299 if (target_type != HAL_PROPERTY_TYPE_INVALID && target_type != type)
300 300 hal_device_property_remove (target, key);
301 301
302 302 switch (type) {
303 303
304 304 case HAL_PROPERTY_TYPE_STRING:
305 305 hal_device_property_set_string (
306 306 target, key,
307 307 hal_property_get_string (p));
308 308 break;
309 309
310 310 case HAL_PROPERTY_TYPE_INT32:
311 311 hal_device_property_set_int (
312 312 target, key,
313 313 hal_property_get_int (p));
314 314 break;
315 315
316 316 case HAL_PROPERTY_TYPE_UINT64:
317 317 hal_device_property_set_uint64 (
318 318 target, key,
319 319 hal_property_get_uint64 (p));
320 320 break;
321 321
322 322 case HAL_PROPERTY_TYPE_BOOLEAN:
323 323 hal_device_property_set_bool (
324 324 target, key,
325 325 hal_property_get_bool (p));
326 326 break;
327 327
328 328 case HAL_PROPERTY_TYPE_DOUBLE:
329 329 hal_device_property_set_double (
330 330 target, key,
331 331 hal_property_get_double (p));
332 332 break;
333 333
334 334 default:
335 335 HAL_WARNING (("Unknown property type %d", type));
336 336 break;
337 337 }
338 338 }
339 339
340 340 /* device_property_atomic_update_end (); */
341 341
342 342 caps = hal_device_property_get_strlist (source, "info.capabilities");
343 343 for (iter = caps; iter != NULL; iter = iter->next) {
344 344 if (!hal_device_has_capability (target, iter->data))
345 345 hal_device_add_capability (target, iter->data);
346 346 }
347 347 }
348 348
349 349 gboolean
350 350 hal_device_matches (HalDevice *device1, HalDevice *device2,
351 351 const char *namespace)
352 352 {
353 353 int len;
354 354 GSList *iter;
355 355
356 356 len = strlen (namespace);
357 357
358 358 for (iter = device1->properties; iter != NULL; iter = iter->next) {
359 359 HalProperty *p;
360 360 const char *key;
361 361 int type;
362 362
363 363 p = (HalProperty *) iter->data;
364 364 key = hal_property_get_key (p);
365 365 type = hal_property_get_type (p);
366 366
367 367 if (strncmp (key, namespace, len) != 0)
368 368 continue;
369 369
370 370 if (!hal_device_has_property (device2, key))
371 371 return FALSE;
372 372
373 373 switch (type) {
374 374
375 375 case HAL_PROPERTY_TYPE_STRING:
376 376 if (strcmp (hal_property_get_string (p),
377 377 hal_device_property_get_string (device2,
378 378 key)) != 0)
379 379 return FALSE;
380 380 break;
381 381
382 382 case HAL_PROPERTY_TYPE_INT32:
383 383 if (hal_property_get_int (p) !=
384 384 hal_device_property_get_int (device2, key))
385 385 return FALSE;
386 386 break;
387 387
388 388 case HAL_PROPERTY_TYPE_UINT64:
389 389 if (hal_property_get_uint64 (p) !=
390 390 hal_device_property_get_uint64 (device2, key))
391 391 return FALSE;
392 392 break;
393 393
394 394 case HAL_PROPERTY_TYPE_BOOLEAN:
395 395 if (hal_property_get_bool (p) !=
396 396 hal_device_property_get_bool (device2, key))
397 397 return FALSE;
398 398 break;
399 399
400 400 case HAL_PROPERTY_TYPE_DOUBLE:
401 401 if (hal_property_get_double (p) !=
402 402 hal_device_property_get_double (device2, key))
403 403 return FALSE;
404 404 break;
405 405
406 406 default:
407 407 HAL_WARNING (("Unknown property type %d", type));
408 408 break;
409 409 }
410 410 }
411 411
412 412 return TRUE;
413 413 }
414 414
415 415 const char *
416 416 hal_device_get_udi (HalDevice *device)
417 417 {
418 418 return device->udi;
419 419 }
420 420
421 421 void
422 422 hal_device_set_udi (HalDevice *device, const char *udi)
423 423 {
424 424 if (device->udi != NULL)
425 425 g_free (device->udi);
426 426 device->udi = g_strdup (udi);
427 427 }
428 428
429 429 void
430 430 hal_device_add_capability (HalDevice *device, const char *capability)
431 431 {
432 432 if (hal_device_property_strlist_add (device, "info.capabilities", capability))
433 433 g_signal_emit (device, signals[CAPABILITY_ADDED], 0, capability);
434 434 }
435 435
436 436 gboolean
437 437 hal_device_has_capability (HalDevice *device, const char *capability)
438 438 {
439 439 GSList *caps;
440 440 GSList *iter;
441 441 gboolean matched = FALSE;
442 442
443 443 caps = hal_device_property_get_strlist (device, "info.capabilities");
444 444
445 445 if (caps == NULL)
446 446 return FALSE;
447 447
448 448 for (iter = caps; iter != NULL; iter = iter->next) {
449 449 if (strcmp (iter->data, capability) == 0) {
450 450 matched = TRUE;
451 451 break;
452 452 }
453 453 }
454 454
455 455 return matched;
456 456 }
457 457
458 458 gboolean
459 459 hal_device_has_property (HalDevice *device, const char *key)
460 460 {
461 461 g_return_val_if_fail (device != NULL, FALSE);
462 462 g_return_val_if_fail (key != NULL, FALSE);
463 463
464 464 return hal_device_property_find (device, key) != NULL;
465 465 }
466 466
467 467 int
468 468 hal_device_num_properties (HalDevice *device)
469 469 {
470 470 g_return_val_if_fail (device != NULL, -1);
471 471
472 472 return g_slist_length (device->properties);
473 473 }
474 474
475 475 HalProperty *
476 476 hal_device_property_find (HalDevice *device, const char *key)
477 477 {
478 478 GSList *iter;
479 479
480 480 g_return_val_if_fail (device != NULL, NULL);
481 481 g_return_val_if_fail (key != NULL, NULL);
482 482
483 483 for (iter = device->properties; iter != NULL; iter = iter->next) {
484 484 HalProperty *p = iter->data;
485 485
486 486 if (strcmp (hal_property_get_key (p), key) == 0)
487 487 return p;
488 488 }
489 489
490 490 return NULL;
491 491 }
492 492
493 493 char *
494 494 hal_device_property_to_string (HalDevice *device, const char *key)
495 495 {
496 496 HalProperty *prop;
497 497
498 498 prop = hal_device_property_find (device, key);
499 499 if (!prop)
500 500 return NULL;
501 501
502 502 return hal_property_to_string (prop);
503 503 }
504 504
505 505 void
506 506 hal_device_property_foreach (HalDevice *device,
507 507 HalDevicePropertyForeachFn callback,
508 508 gpointer user_data)
509 509 {
510 510 GSList *iter;
511 511
512 512 g_return_if_fail (device != NULL);
513 513 g_return_if_fail (callback != NULL);
514 514
515 515 for (iter = device->properties; iter != NULL; iter = iter->next) {
516 516 HalProperty *p = iter->data;
517 517 gboolean cont;
518 518
519 519 cont = callback (device, p, user_data);
520 520
521 521 if (cont == FALSE)
522 522 return;
523 523 }
524 524 }
525 525
526 526 int
527 527 hal_device_property_get_type (HalDevice *device, const char *key)
528 528 {
529 529 HalProperty *prop;
530 530
531 531 g_return_val_if_fail (device != NULL, HAL_PROPERTY_TYPE_INVALID);
532 532 g_return_val_if_fail (key != NULL, HAL_PROPERTY_TYPE_INVALID);
533 533
534 534 prop = hal_device_property_find (device, key);
535 535
536 536 if (prop != NULL)
537 537 return hal_property_get_type (prop);
538 538 else
539 539 return HAL_PROPERTY_TYPE_INVALID;
540 540 }
541 541
542 542 const char *
543 543 hal_device_property_get_string (HalDevice *device, const char *key)
544 544 {
545 545 HalProperty *prop;
546 546
547 547 g_return_val_if_fail (device != NULL, NULL);
548 548 g_return_val_if_fail (key != NULL, NULL);
549 549
550 550 prop = hal_device_property_find (device, key);
551 551
552 552 if (prop != NULL)
553 553 return hal_property_get_string (prop);
554 554 else
555 555 return NULL;
556 556 }
557 557
558 558 const char *
559 559 hal_device_property_get_as_string (HalDevice *device, const char *key, char *buf, size_t bufsize)
560 560 {
561 561 HalProperty *prop;
562 562
563 563 g_return_val_if_fail (device != NULL, NULL);
564 564 g_return_val_if_fail (key != NULL, NULL);
565 565 g_return_val_if_fail (buf != NULL, NULL);
566 566
567 567 prop = hal_device_property_find (device, key);
568 568
569 569 if (prop != NULL) {
570 570 switch (hal_property_get_type (prop)) {
571 571 case HAL_PROPERTY_TYPE_STRING:
572 572 strncpy (buf, hal_property_get_string (prop), bufsize);
573 573 break;
574 574 case HAL_PROPERTY_TYPE_INT32:
575 575 snprintf (buf, bufsize, "%d", hal_property_get_int (prop));
576 576 break;
577 577 case HAL_PROPERTY_TYPE_UINT64:
578 578 snprintf (buf, bufsize, "%llu", (long long unsigned int) hal_property_get_uint64 (prop));
579 579 break;
580 580 case HAL_PROPERTY_TYPE_DOUBLE:
581 581 snprintf (buf, bufsize, "%f", hal_property_get_double (prop));
582 582 break;
583 583 case HAL_PROPERTY_TYPE_BOOLEAN:
584 584 strncpy (buf, hal_property_get_bool (prop) ? "true" : "false", bufsize);
585 585 break;
586 586
587 587 case HAL_PROPERTY_TYPE_STRLIST:
588 588 /* print out as "\tval1\tval2\val3\t" */
589 589 {
590 590 GSList *iter;
591 591 guint i;
592 592
593 593 if (bufsize > 0)
594 594 buf[0] = '\t';
595 595 i = 1;
596 596 for (iter = hal_property_get_strlist (prop);
597 597 iter != NULL && i < bufsize;
598 598 iter = g_slist_next (iter)) {
599 599 guint len;
600 600 const char *str;
601 601
602 602 str = (const char *) iter->data;
603 603 len = strlen (str);
604 604 strncpy (buf + i, str, bufsize - i);
605 605 i += len;
606 606
607 607 if (i < bufsize) {
608 608 buf[i] = '\t';
609 609 i++;
610 610 }
611 611 }
612 612 }
613 613 break;
614 614 }
615 615 return buf;
616 616 } else {
617 617 buf[0] = '\0';
618 618 return NULL;
619 619 }
620 620 }
621 621
622 622 dbus_int32_t
623 623 hal_device_property_get_int (HalDevice *device, const char *key)
624 624 {
625 625 HalProperty *prop;
626 626
627 627 g_return_val_if_fail (device != NULL, -1);
628 628 g_return_val_if_fail (key != NULL, -1);
629 629
630 630 prop = hal_device_property_find (device, key);
631 631
632 632 if (prop != NULL)
633 633 return hal_property_get_int (prop);
634 634 else
635 635 return -1;
636 636 }
637 637
638 638 dbus_uint64_t
639 639 hal_device_property_get_uint64 (HalDevice *device, const char *key)
640 640 {
641 641 HalProperty *prop;
642 642
643 643 g_return_val_if_fail (device != NULL, -1);
644 644 g_return_val_if_fail (key != NULL, -1);
645 645
646 646 prop = hal_device_property_find (device, key);
647 647
648 648 if (prop != NULL)
649 649 return hal_property_get_uint64 (prop);
650 650 else
651 651 return -1;
652 652 }
653 653
654 654 dbus_bool_t
655 655 hal_device_property_get_bool (HalDevice *device, const char *key)
656 656 {
657 657 HalProperty *prop;
658 658
659 659 g_return_val_if_fail (device != NULL, FALSE);
660 660 g_return_val_if_fail (key != NULL, FALSE);
661 661
662 662 prop = hal_device_property_find (device, key);
663 663
664 664 if (prop != NULL)
665 665 return hal_property_get_bool (prop);
666 666 else
667 667 return FALSE;
668 668 }
669 669
670 670 double
671 671 hal_device_property_get_double (HalDevice *device, const char *key)
672 672 {
673 673 HalProperty *prop;
674 674
675 675 g_return_val_if_fail (device != NULL, -1.0);
676 676 g_return_val_if_fail (key != NULL, -1.0);
677 677
678 678 prop = hal_device_property_find (device, key);
679 679
680 680 if (prop != NULL)
681 681 return hal_property_get_double (prop);
682 682 else
683 683 return -1.0;
684 684 }
685 685
686 686 gboolean
687 687 hal_device_property_set_string (HalDevice *device, const char *key,
688 688 const char *value)
689 689 {
690 690 HalProperty *prop;
691 691
692 692 /* check if property already exists */
693 693 prop = hal_device_property_find (device, key);
694 694
695 695 if (prop != NULL) {
696 696 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRING)
697 697 return FALSE;
698 698
699 699 /* don't bother setting the same value */
700 700 if (value != NULL &&
701 701 strcmp (hal_property_get_string (prop), value) == 0)
702 702 return TRUE;
703 703
704 704 hal_property_set_string (prop, value);
705 705
706 706 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
707 707 key, FALSE, FALSE);
708 708
709 709 } else {
710 710
711 711 prop = hal_property_new_string (key, value);
712 712
713 713 device->properties = g_slist_prepend (device->properties, prop);
714 714
715 715 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
716 716 key, FALSE, TRUE);
717 717 }
718 718
719 719 return TRUE;
720 720 }
721 721
722 722 gboolean
723 723 hal_device_property_set_int (HalDevice *device, const char *key,
724 724 dbus_int32_t value)
725 725 {
726 726 HalProperty *prop;
727 727
728 728 /* check if property already exists */
729 729 prop = hal_device_property_find (device, key);
730 730
731 731 if (prop != NULL) {
732 732 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_INT32)
733 733 return FALSE;
734 734
735 735 /* don't bother setting the same value */
736 736 if (hal_property_get_int (prop) == value)
737 737 return TRUE;
738 738
739 739 hal_property_set_int (prop, value);
740 740
741 741 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
742 742 key, FALSE, FALSE);
743 743
744 744 } else {
745 745 prop = hal_property_new_int (key, value);
746 746
747 747 device->properties = g_slist_prepend (device->properties, prop);
748 748
749 749 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
750 750 key, FALSE, TRUE);
751 751 }
752 752
753 753 return TRUE;
754 754 }
755 755
756 756 gboolean
757 757 hal_device_property_set_uint64 (HalDevice *device, const char *key,
758 758 dbus_uint64_t value)
759 759 {
760 760 HalProperty *prop;
761 761
762 762 /* check if property already exists */
763 763 prop = hal_device_property_find (device, key);
764 764
765 765 if (prop != NULL) {
766 766 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_UINT64)
767 767 return FALSE;
768 768
769 769 /* don't bother setting the same value */
770 770 if (hal_property_get_uint64 (prop) == value)
771 771 return TRUE;
772 772
773 773 hal_property_set_uint64 (prop, value);
774 774
775 775 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
776 776 key, FALSE, FALSE);
777 777
778 778 } else {
779 779 prop = hal_property_new_uint64 (key, value);
780 780
781 781 device->properties = g_slist_prepend (device->properties, prop);
782 782
783 783 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
784 784 key, FALSE, TRUE);
785 785 }
786 786
787 787 return TRUE;
788 788 }
789 789
790 790 gboolean
791 791 hal_device_property_set_bool (HalDevice *device, const char *key,
792 792 dbus_bool_t value)
793 793 {
794 794 HalProperty *prop;
795 795
796 796 /* check if property already exists */
797 797 prop = hal_device_property_find (device, key);
798 798
799 799 if (prop != NULL) {
800 800 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_BOOLEAN)
801 801 return FALSE;
802 802
803 803 /* don't bother setting the same value */
804 804 if (hal_property_get_bool (prop) == value)
805 805 return TRUE;
806 806
807 807 hal_property_set_bool (prop, value);
808 808
809 809 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
810 810 key, FALSE, FALSE);
811 811
812 812 } else {
813 813 prop = hal_property_new_bool (key, value);
814 814
815 815 device->properties = g_slist_prepend (device->properties, prop);
816 816
817 817 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
818 818 key, FALSE, TRUE);
819 819 }
820 820
821 821 return TRUE;
822 822 }
823 823
824 824 gboolean
825 825 hal_device_property_set_double (HalDevice *device, const char *key,
826 826 double value)
827 827 {
828 828 HalProperty *prop;
829 829
830 830 /* check if property already exists */
831 831 prop = hal_device_property_find (device, key);
832 832
833 833 if (prop != NULL) {
834 834 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_DOUBLE)
835 835 return FALSE;
836 836
837 837 /* don't bother setting the same value */
838 838 if (hal_property_get_double (prop) == value)
839 839 return TRUE;
840 840
841 841 hal_property_set_double (prop, value);
842 842
843 843 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
844 844 key, FALSE, FALSE);
845 845
846 846 } else {
847 847 prop = hal_property_new_double (key, value);
848 848
849 849 device->properties = g_slist_prepend (device->properties, prop);
850 850
851 851 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
852 852 key, FALSE, TRUE);
853 853 }
854 854
855 855 return TRUE;
856 856 }
857 857
858 858 gboolean
859 859 hal_device_copy_property (HalDevice *from_device, const char *from, HalDevice *to_device, const char *to)
860 860 {
861 861 gboolean rc;
862 862
863 863 rc = FALSE;
864 864
865 865 if (hal_device_has_property (from_device, from)) {
866 866 switch (hal_device_property_get_type (from_device, from)) {
867 867 case HAL_PROPERTY_TYPE_STRING:
868 868 rc = hal_device_property_set_string (
869 869 to_device, to, hal_device_property_get_string (from_device, from));
870 870 break;
871 871 case HAL_PROPERTY_TYPE_INT32:
872 872 rc = hal_device_property_set_int (
873 873 to_device, to, hal_device_property_get_int (from_device, from));
874 874 break;
875 875 case HAL_PROPERTY_TYPE_UINT64:
876 876 rc = hal_device_property_set_uint64 (
877 877 to_device, to, hal_device_property_get_uint64 (from_device, from));
878 878 break;
879 879 case HAL_PROPERTY_TYPE_BOOLEAN:
880 880 rc = hal_device_property_set_bool (
881 881 to_device, to, hal_device_property_get_bool (from_device, from));
882 882 break;
883 883 case HAL_PROPERTY_TYPE_DOUBLE:
884 884 rc = hal_device_property_set_double (
885 885 to_device, to, hal_device_property_get_double (from_device, from));
886 886 break;
887 887 }
888 888 }
889 889
890 890 return rc;
891 891 }
892 892
893 893 gboolean
894 894 hal_device_property_remove (HalDevice *device, const char *key)
895 895 {
896 896 HalProperty *prop;
897 897
898 898 prop = hal_device_property_find (device, key);
899 899
900 900 if (prop == NULL)
901 901 return FALSE;
902 902
903 903 device->properties = g_slist_remove (device->properties, prop);
904 904
905 905 hal_property_free (prop);
906 906
907 907 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
908 908 key, TRUE, FALSE);
909 909
910 910 return TRUE;
911 911 }
912 912
913 913 gboolean
914 914 hal_device_property_set_attribute (HalDevice *device,
915 915 const char *key,
916 916 enum PropertyAttribute attr,
917 917 gboolean val)
918 918 {
919 919 HalProperty *prop;
920 920
921 921 prop = hal_device_property_find (device, key);
922 922
923 923 if (prop == NULL)
924 924 return FALSE;
925 925
926 926 return TRUE;
927 927 }
928 928
929 929 void
930 930 hal_device_print (HalDevice *device)
931 931 {
932 932 GSList *iter;
933 933
934 934 fprintf (stderr, "device udi = %s\n", hal_device_get_udi (device));
935 935
936 936 for (iter = device->properties; iter != NULL; iter = iter->next) {
937 937 HalProperty *p = iter->data;
938 938 int type;
939 939 const char *key;
940 940
941 941 key = hal_property_get_key (p);
942 942 type = hal_property_get_type (p);
943 943
944 944 switch (type) {
945 945 case HAL_PROPERTY_TYPE_STRING:
946 946 fprintf (stderr, " %s = '%s' (string)\n", key,
947 947 hal_property_get_string (p));
948 948 break;
949 949
950 950 case HAL_PROPERTY_TYPE_INT32:
951 951 fprintf (stderr, " %s = %d 0x%x (int)\n", key,
952 952 hal_property_get_int (p),
953 953 hal_property_get_int (p));
954 954 break;
955 955
956 956 case HAL_PROPERTY_TYPE_UINT64:
957 957 fprintf (stderr, " %s = %llu 0x%llx (uint64)\n", key,
958 958 (long long unsigned int) hal_property_get_uint64 (p),
959 959 (long long unsigned int) hal_property_get_uint64 (p));
960 960 break;
961 961
962 962 case HAL_PROPERTY_TYPE_DOUBLE:
963 963 fprintf (stderr, " %s = %g (double)\n", key,
964 964 hal_property_get_double (p));
965 965 break;
966 966
967 967 case HAL_PROPERTY_TYPE_BOOLEAN:
968 968 fprintf (stderr, " %s = %s (bool)\n", key,
969 969 (hal_property_get_bool (p) ? "true" :
970 970 "false"));
971 971 break;
972 972
973 973 default:
974 974 HAL_WARNING (("Unknown property type %d", type));
975 975 break;
976 976 }
977 977 }
978 978 fprintf (stderr, "\n");
979 979 }
980 980
981 981
982 982 typedef struct {
983 983 char *key;
984 984 HalDevice *device;
985 985 HalDeviceAsyncCallback callback;
986 986 gpointer user_data;
987 987
988 988 guint prop_signal_id;
989 989 guint timeout_id;
990 990 } AsyncMatchInfo;
991 991
992 992 static void
993 993 destroy_async_match_info (AsyncMatchInfo *ai)
994 994 {
995 995 g_free (ai->key);
996 996 g_signal_handler_disconnect (ai->device, ai->prop_signal_id);
997 997 g_source_remove (ai->timeout_id);
998 998 g_object_unref (ai->device);
999 999 g_free (ai);
1000 1000 }
1001 1001
1002 1002 static void
1003 1003 prop_changed_cb (HalDevice *device, const char *key,
1004 1004 gboolean removed, gboolean added, gpointer user_data)
1005 1005 {
1006 1006 AsyncMatchInfo *ai = user_data;
1007 1007
1008 1008 if (strcmp (key, ai->key) != 0)
1009 1009 return;
1010 1010
1011 1011 /* the property is no longer there */
1012 1012 if (removed)
1013 1013 goto cleanup;
1014 1014
1015 1015
1016 1016 ai->callback (ai->device, ai->user_data, TRUE);
1017 1017
1018 1018 cleanup:
1019 1019 destroy_async_match_info (ai);
1020 1020 }
1021 1021
1022 1022
1023 1023 static gboolean
1024 1024 async_wait_timeout (gpointer user_data)
1025 1025 {
1026 1026 AsyncMatchInfo *ai = (AsyncMatchInfo *) user_data;
1027 1027
1028 1028 ai->callback (ai->device, ai->user_data, FALSE);
1029 1029
1030 1030 destroy_async_match_info (ai);
1031 1031
1032 1032 return FALSE;
1033 1033 }
1034 1034
1035 1035 void
1036 1036 hal_device_async_wait_property (HalDevice *device,
1037 1037 const char *key,
1038 1038 HalDeviceAsyncCallback callback,
1039 1039 gpointer user_data,
1040 1040 int timeout)
1041 1041 {
1042 1042 HalProperty *prop;
1043 1043 AsyncMatchInfo *ai;
1044 1044
1045 1045 /* check if property already exists */
1046 1046 prop = hal_device_property_find (device, key);
1047 1047
1048 1048 if (prop != NULL || timeout==0) {
1049 1049 callback (device, user_data, prop != NULL);
1050 1050 return;
1051 1051 }
1052 1052
1053 1053 ai = g_new0 (AsyncMatchInfo, 1);
1054 1054
1055 1055 ai->device = g_object_ref (device);
1056 1056 ai->key = g_strdup (key);
1057 1057 ai->callback = callback;
1058 1058 ai->user_data = user_data;
1059 1059
1060 1060 ai->prop_signal_id = g_signal_connect (device, "property_changed",
1061 1061 G_CALLBACK (prop_changed_cb),
1062 1062 ai);
1063 1063
1064 1064 ai->timeout_id = g_timeout_add (timeout, async_wait_timeout, ai);
1065 1065 }
1066 1066
1067 1067 void
1068 1068 hal_device_callouts_finished (HalDevice *device)
1069 1069 {
1070 1070 g_signal_emit (device, signals[CALLOUTS_FINISHED], 0);
1071 1071 }
1072 1072
1073 1073 /** Used when giving up on a device, e.g. if no device file appeared
1074 1074 */
1075 1075 void
1076 1076 hal_device_cancel (HalDevice *device)
1077 1077 {
1078 1078 HAL_INFO (("udi=%s", device->udi));
1079 1079 g_signal_emit (device, signals[CANCELLED], 0);
1080 1080 }
1081 1081
1082 1082
1083 1083
1084 1084
1085 1085 GSList *
1086 1086 hal_device_property_get_strlist (HalDevice *device,
1087 1087 const char *key)
1088 1088 {
1089 1089 HalProperty *prop;
1090 1090
1091 1091 g_return_val_if_fail (device != NULL, NULL);
1092 1092 g_return_val_if_fail (key != NULL, NULL);
1093 1093
1094 1094 prop = hal_device_property_find (device, key);
1095 1095
1096 1096 if (prop != NULL)
1097 1097 return hal_property_get_strlist (prop);
1098 1098 else
1099 1099 return NULL;
1100 1100 }
1101 1101
1102 1102 const char *
1103 1103 hal_device_property_get_strlist_elem (HalDevice *device,
1104 1104 const char *key,
1105 1105 guint index)
1106 1106 {
1107 1107 GSList *strlist;
1108 1108 GSList *i;
1109 1109
1110 1110 strlist = hal_device_property_get_strlist (device, key);
1111 1111 if (strlist == NULL)
1112 1112 return NULL;
1113 1113
1114 1114 i = g_slist_nth (strlist, index);
1115 1115 if (i == NULL)
1116 1116 return NULL;
1117 1117
1118 1118 return (const char *) i->data;
1119 1119 }
1120 1120
1121 1121 gboolean
1122 1122 hal_device_property_strlist_append (HalDevice *device,
1123 1123 const char *key,
1124 1124 const char *value)
1125 1125 {
1126 1126 HalProperty *prop;
1127 1127
1128 1128 /* check if property already exists */
1129 1129 prop = hal_device_property_find (device, key);
1130 1130
1131 1131 if (prop != NULL) {
1132 1132 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1133 1133 return FALSE;
1134 1134
1135 1135 hal_property_strlist_append (prop, value);
1136 1136
1137 1137 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1138 1138 key, FALSE, FALSE);
1139 1139
1140 1140 } else {
1141 1141 prop = hal_property_new_strlist (key);
1142 1142 hal_property_strlist_append (prop, value);
1143 1143
1144 1144 device->properties = g_slist_prepend (device->properties, prop);
1145 1145
1146 1146 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1147 1147 key, FALSE, TRUE);
1148 1148 }
1149 1149
1150 1150 return TRUE;
1151 1151 }
1152 1152
1153 1153 gboolean
1154 1154 hal_device_property_strlist_prepend (HalDevice *device,
1155 1155 const char *key,
1156 1156 const char *value)
1157 1157 {
1158 1158 HalProperty *prop;
1159 1159
1160 1160 /* check if property already exists */
1161 1161 prop = hal_device_property_find (device, key);
1162 1162
1163 1163 if (prop != NULL) {
1164 1164 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1165 1165 return FALSE;
1166 1166
1167 1167 hal_property_strlist_prepend (prop, value);
1168 1168
1169 1169 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1170 1170 key, FALSE, FALSE);
1171 1171
1172 1172 } else {
1173 1173 prop = hal_property_new_strlist (key);
1174 1174 hal_property_strlist_prepend (prop, value);
1175 1175
1176 1176 device->properties = g_slist_prepend (device->properties, prop);
1177 1177
1178 1178 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1179 1179 key, FALSE, TRUE);
1180 1180 }
1181 1181
1182 1182 return TRUE;
1183 1183 }
1184 1184
1185 1185 gboolean
1186 1186 hal_device_property_strlist_remove_elem (HalDevice *device,
1187 1187 const char *key,
1188 1188 guint index)
1189 1189 {
1190 1190 HalProperty *prop;
1191 1191
1192 1192 /* check if property already exists */
1193 1193 prop = hal_device_property_find (device, key);
1194 1194
1195 1195 if (prop == NULL)
1196 1196 return FALSE;
1197 1197
1198 1198 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1199 1199 return FALSE;
1200 1200
1201 1201 if (hal_property_strlist_remove_elem (prop, index)) {
1202 1202 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1203 1203 key, FALSE, FALSE);
1204 1204 return TRUE;
1205 1205 }
1206 1206
1207 1207 return FALSE;
1208 1208 }
1209 1209
1210 1210 gboolean
1211 1211 hal_device_property_strlist_clear (HalDevice *device,
1212 1212 const char *key)
1213 1213 {
1214 1214 HalProperty *prop;
1215 1215
1216 1216 /* check if property already exists */
1217 1217 prop = hal_device_property_find (device, key);
1218 1218
1219 1219 if (prop == NULL) {
1220 1220 prop = hal_property_new_strlist (key);
1221 1221
1222 1222 device->properties = g_slist_prepend (device->properties, prop);
1223 1223
1224 1224 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1225 1225 key, FALSE, TRUE);
1226 1226
1227 1227 return TRUE;
1228 1228 }
1229 1229
1230 1230 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1231 1231 return FALSE;
1232 1232
1233 1233 if (hal_property_strlist_clear (prop)) {
1234 1234 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1235 1235 key, FALSE, FALSE);
1236 1236 return TRUE;
1237 1237 }
1238 1238
1239 1239 return FALSE;
1240 1240 }
1241 1241
1242 1242
1243 1243 gboolean
1244 1244 hal_device_property_strlist_add (HalDevice *device,
1245 1245 const char *key,
1246 1246 const char *value)
1247 1247 {
1248 1248 HalProperty *prop;
1249 1249 gboolean res;
1250 1250
1251 1251 res = FALSE;
1252 1252
1253 1253 /* check if property already exists */
1254 1254 prop = hal_device_property_find (device, key);
1255 1255
1256 1256 if (prop != NULL) {
1257 1257 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1258 1258 goto out;
1259 1259
1260 1260 res = hal_property_strlist_add (prop, value);
1261 1261 if (res) {
1262 1262 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1263 1263 key, FALSE, FALSE);
1264 1264 }
1265 1265
1266 1266 } else {
1267 1267 prop = hal_property_new_strlist (key);
1268 1268 hal_property_strlist_prepend (prop, value);
1269 1269
1270 1270 device->properties = g_slist_prepend (device->properties, prop);
1271 1271
1272 1272 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1273 1273 key, FALSE, TRUE);
1274 1274
1275 1275 res = TRUE;
1276 1276 }
1277 1277
1278 1278 out:
1279 1279 return res;
1280 1280 }
1281 1281
1282 1282 gboolean
1283 1283 hal_device_property_strlist_remove (HalDevice *device,
1284 1284 const char *key,
1285 1285 const char *value)
1286 1286 {
1287 1287 HalProperty *prop;
1288 1288
1289 1289 /* check if property already exists */
1290 1290 prop = hal_device_property_find (device, key);
1291 1291
1292 1292 if (prop == NULL)
1293 1293 return FALSE;
1294 1294
1295 1295 if (hal_property_get_type (prop) != HAL_PROPERTY_TYPE_STRLIST)
1296 1296 return FALSE;
1297 1297
1298 1298 if (hal_property_strlist_remove (prop, value)) {
1299 1299 g_signal_emit (device, signals[PROPERTY_CHANGED], 0,
1300 1300 key, FALSE, FALSE);
1301 1301 }
1302 1302
1303 1303 return TRUE;
1304 1304 }
1305 1305
1306 1306 gboolean
1307 1307 hal_device_property_strlist_is_empty (HalDevice *device,
1308 1308 const char *key)
1309 1309 {
1310 1310 GSList *strlist;
1311 1311
1312 1312 if ( hal_device_has_property (device, key)) {
1313 1313 strlist = hal_device_property_get_strlist (device, key);
1314 1314 if (strlist == NULL )
1315 1315 return TRUE;
1316 1316
1317 1317 if (g_slist_length (strlist) > 0)
1318 1318 return FALSE;
1319 1319 else
1320 1320 return TRUE;
1321 1321 }
1322 1322 return FALSE;
1323 1323 }
1324 1324
1325 1325 void
1326 1326 hal_device_inc_num_addons (HalDevice *device)
1327 1327 {
1328 1328 device->num_addons++;
1329 1329 }
1330 1330
1331 1331 gboolean
1332 1332 hal_device_inc_num_ready_addons (HalDevice *device)
1333 1333 {
1334 1334 if (hal_device_are_all_addons_ready (device)) {
1335 1335 HAL_ERROR (("In hal_device_inc_num_ready_addons for udi=%s but all addons are already ready!",
1336 1336 device->udi));
1337 1337 return FALSE;
1338 1338 }
1339 1339
1340 1340 device->num_addons_ready++;
1341 1341 return TRUE;
1342 1342 }
1343 1343
1344 1344 gboolean
1345 1345 hal_device_are_all_addons_ready (HalDevice *device)
1346 1346 {
1347 1347 if (device->num_addons_ready == device->num_addons) {
1348 1348 return TRUE;
1349 1349 } else {
1350 1350 return FALSE;
1351 1351 }
1352 1352 }
|
↓ open down ↓ |
1246 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX