/
mce-dbus.c
3763 lines (3166 loc) · 91.4 KB
/
mce-dbus.c
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file mce-dbus.c
* D-Bus handling code for the Mode Control Entity
* <p>
* Copyright © 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Ismo Laitinen <ismo.laitinen@nokia.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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 mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mce-dbus.h"
#include "mce.h"
#include "mce-log.h"
#include "mce-lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <mce/dbus-names.h>
static bool introspectable_signal(const char *interface, const char *member);
/** Placeholder for any basic dbus data type */
typedef union
{
dbus_int16_t i16;
dbus_int32_t i32;
dbus_int64_t i64;
dbus_uint16_t u16;
dbus_uint32_t u32;
dbus_uint64_t u64;
dbus_bool_t b;
unsigned char o;
const char *s;
double d;
} dbus_any_t;
/** Emit one iterm from dbus message iterator to file
*
* @param file output file
* @param iter dbus message parse position
*
* @return TRUE if more items can be parsed, FALSE otherwise
*/
static dbus_bool_t
mce_dbus_message_repr_any(FILE *file, DBusMessageIter *iter)
{
dbus_any_t val = { .u64 = 0 };
DBusMessageIter sub;
switch( dbus_message_iter_get_arg_type(iter) ) {
case DBUS_TYPE_INVALID:
return FALSE;
default:
case DBUS_TYPE_UNIX_FD:
fprintf(file, " ???");
return FALSE;
case DBUS_TYPE_BYTE:
dbus_message_iter_get_basic(iter, &val.o);
fprintf(file, " byte:%d", val.o);
break;
case DBUS_TYPE_BOOLEAN:
dbus_message_iter_get_basic(iter, &val.b);
fprintf(file, " bool:%d", val.b);
break;
case DBUS_TYPE_INT16:
dbus_message_iter_get_basic(iter, &val.i16);
fprintf(file, " i16:%d", val.i16);
break;
case DBUS_TYPE_INT32:
dbus_message_iter_get_basic(iter, &val.i32);
fprintf(file, " i32:%d", val.i32);
break;
case DBUS_TYPE_INT64:
dbus_message_iter_get_basic(iter, &val.i64);
fprintf(file, " i64:%lld", (long long)val.i64);
break;
case DBUS_TYPE_UINT16:
dbus_message_iter_get_basic(iter, &val.u16);
fprintf(file, " u16:%u", val.u16);
break;
case DBUS_TYPE_UINT32:
dbus_message_iter_get_basic(iter, &val.u32);
fprintf(file, " u32:%u", val.u32);
break;
case DBUS_TYPE_UINT64:
dbus_message_iter_get_basic(iter, &val.u64);
fprintf(file, " u64:%llu", (unsigned long long)val.u64);
break;
case DBUS_TYPE_DOUBLE:
dbus_message_iter_get_basic(iter, &val.d);
fprintf(file, " dbl:%g", val.d);
break;
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " str:\"%s\"", val.s);
break;
case DBUS_TYPE_OBJECT_PATH:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " obj:\"%s\"", val.s);
break;
case DBUS_TYPE_SIGNATURE:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " sgn:\"%s\"", val.s);
break;
case DBUS_TYPE_ARRAY:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " [");
while( mce_dbus_message_repr_any(file, &sub) ) {}
fprintf(file, " ]");
break;
case DBUS_TYPE_VARIANT:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " var");
mce_dbus_message_repr_any(file, &sub);
break;
case DBUS_TYPE_STRUCT:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " {");
while( mce_dbus_message_repr_any(file, &sub) ) {}
fprintf(file, " }");
break;
case DBUS_TYPE_DICT_ENTRY:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " key");
mce_dbus_message_repr_any(file, &sub);
fprintf(file, " val");
mce_dbus_message_repr_any(file, &sub);
break;
}
return dbus_message_iter_next(iter);
}
/** Convert dbus message read iterator to string
*
* Caller must release returned string with free().
*
* @param iter dbus message iterator
*
* @returns representation of the iterator, or NULL
*/
char *
mce_dbus_message_iter_repr(DBusMessageIter *iter)
{
size_t size = 0;
char *data = 0;
FILE *file = open_memstream(&data, &size);
if( !iter )
goto EXIT;
while( mce_dbus_message_repr_any(file, iter) ) {}
EXIT:
fclose(file);
return data;
}
/** Convert dbus message to string
*
* Caller must release returned string with free().
*
* @param msg dbus message
*
* @returns representation of the dbus message, or NULL
*/
char *
mce_dbus_message_repr(DBusMessage *const msg)
{
size_t size = 0;
char *data = 0;
FILE *file = open_memstream(&data, &size);
const char *iface = dbus_message_get_interface(msg);
const char *member = dbus_message_get_member(msg);
int type = dbus_message_get_type(msg);
const char *tname = dbus_message_type_to_string(type);
const char *sender = dbus_message_get_sender(msg);
fprintf(file, "%s", tname);
if( sender ) fprintf(file, " from %s", sender);
if( iface ) fprintf(file, " %s", iface);
if( member ) fprintf(file, " %s", member);
DBusMessageIter iter;
dbus_message_iter_init(msg, &iter);
if( dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INVALID )
goto EXIT;
fprintf(file, ":");
while( mce_dbus_message_repr_any(file, &iter) ) {}
EXIT:
fclose(file);
return data;
}
/** Pointer to the DBusConnection */
static DBusConnection *dbus_connection = NULL;
/** List of all D-Bus handlers */
static GSList *dbus_handlers = NULL;
/** D-Bus handler callback function */
typedef gboolean (*handler_callback_t)(DBusMessage *const msg);
/** D-Bus handler structure */
typedef struct
{
handler_callback_t callback; /**< Handler callback */
gchar *interface; /**< The interface to listen on */
gchar *rules; /**< Additional matching rules */
gchar *name; /**< Method call or signal name */
gchar *args; /**< Introspect XML data */
int type; /**< DBUS_MESSAGE_TYPE */
} handler_struct_t;
/** Set handler type for D-Bus handler structure */
static inline void handler_struct_set_type(handler_struct_t *self, int val)
{
self->type = val;
}
/** Set interface name for D-Bus handler structure */
static inline void handler_struct_set_interface(handler_struct_t *self, const char *val)
{
g_free(self->interface), self->interface = val ? g_strdup(val) : 0;
}
/** Set introspect args for D-Bus handler structure */
static inline void handler_struct_set_args(handler_struct_t *self, const char *val)
{
g_free(self->args), self->args = val ? g_strdup(val) : 0;
}
/** Set member name for D-Bus handler structure */
static inline void handler_struct_set_name(handler_struct_t *self, const char *val)
{
g_free(self->name), self->name = val ? g_strdup(val) : 0;
}
/** Set custom rules for D-Bus handler structure */
static inline void handler_struct_set_rules(handler_struct_t *self, const char *val)
{
g_free(self->rules), self->rules = val ? g_strdup(val) : 0;
}
/** Set callback function for D-Bus handler structure */
static inline void handler_struct_set_callback(handler_struct_t *self,
handler_callback_t val)
{
self->callback = val;
}
/** Release D-Bus handler structure */
static void handler_struct_delete(handler_struct_t *self)
{
if( !self )
goto EXIT;
g_free(self->args);
g_free(self->name);
g_free(self->rules);
g_free(self->interface);
g_free(self);
EXIT:
return;
}
/** Allocate D-Bus handler structure */
static handler_struct_t *handler_struct_create(void)
{
handler_struct_t *self = g_malloc0(sizeof *self);
self->interface = 0;
self->rules = 0;
self->name = 0;
self->args = 0;
self->type = DBUS_MESSAGE_TYPE_INVALID;
return self;
}
/** Return reference to dbus connection cached at mce-dbus module
*
* For use in situations where the abstraction provided by mce-dbus
* makes things too complicated.
*
* Caller must release non-null return values with dbus_connection_unref().
*
* @return DBusConnection, or NULL if mce has no dbus connection
*/
DBusConnection *dbus_connection_get(void)
{
if( !dbus_connection ) {
mce_log(LL_WARN, "no dbus connection");
return NULL;
}
return dbus_connection_ref(dbus_connection);
}
/**
* Create a new D-Bus signal, with proper error checking
* will exit the mainloop if an error occurs
*
* @param path The signal path
* @param interface The signal interface
* @param name The name of the signal to send
* @return A new DBusMessage
*/
DBusMessage *dbus_new_signal(const gchar *const path,
const gchar *const interface,
const gchar *const name)
{
DBusMessage *msg = dbus_message_new_signal(path, interface, name);
if( !msg )
mce_abort();
if( !introspectable_signal(interface, name) ) {
mce_log(LL_ERR, "sending non-introspectable signal: %s.%s",
interface, name);
}
return msg;
}
/**
* Create a new D-Bus error message, with proper error checking
* will exit the mainloop if an error occurs
*
* @param req Method call message for which to create an error reply
* @param err D-Bus error name
* @param fmt Error message format string
* @param ... Arguments required by the format string
*
* @return A new DBusMessage
*/
static DBusMessage *dbus_new_error(DBusMessage *req,
const char *err,
const char *fmt,
...) __attribute__((format(printf, 3, 4)));
static DBusMessage *dbus_new_error(DBusMessage *req,
const char *err,
const char *fmt,
...)
{
char *msg = 0;
va_list va;
va_start(va, fmt);
if( vasprintf(&msg, fmt, va) < 0 )
msg = 0;
va_end(va);
DBusMessage *rsp = dbus_message_new_error(req, err, msg);
if( !rsp )
mce_abort();
free(msg);
return rsp;
}
/**
* Create a new D-Bus method call, with proper error checking
* will exit the mainloop if an error occurs
*
* @param service The method call service
* @param path The method call path
* @param interface The method call interface
* @param name The name of the method to call
* @return A new DBusMessage
*/
DBusMessage *dbus_new_method_call(const gchar *const service,
const gchar *const path,
const gchar *const interface,
const gchar *const name)
{
DBusMessage *msg = dbus_message_new_method_call(service, path,
interface, name);
if( !msg )
mce_abort();
return msg;
}
/**
* Create a new D-Bus method call reply, with proper error checking
* will exit the mainloop if an error occurs
*
* @param message The DBusMessage to reply to
* @return A new DBusMessage
*/
DBusMessage *dbus_new_method_reply(DBusMessage *const message)
{
DBusMessage *msg = dbus_message_new_method_return(message);
if( !msg )
mce_abort();
return msg;
}
/**
* Send a D-Bus message
* Side-effects: frees msg
*
* @param msg The D-Bus message to send
* @return TRUE on success, FALSE on out of memory
*/
gboolean dbus_send_message(DBusMessage *const msg)
{
gboolean status = FALSE;
if (dbus_connection_send(dbus_connection, msg, NULL) == FALSE) {
mce_log(LL_CRIT,
"Out of memory when sending D-Bus message");
goto EXIT;
}
status = TRUE;
EXIT:
dbus_message_unref(msg);
return status;
}
/**
* Send a D-Bus message and setup a reply callback
* Side-effects: frees msg
*
* @param msg The D-Bus message to send
* @param callback The reply callback
* @param user_data Data to pass to callback
* @param user_free Delete callback for user_data
* @return TRUE on success, FALSE on failure
*/
static gboolean
dbus_send_message_with_reply_handler(DBusMessage *const msg,
DBusPendingCallNotifyFunction callback,
void *user_data,
DBusFreeFunction user_free,
DBusPendingCall **ppc)
{
gboolean status = FALSE;
DBusPendingCall *pc = 0;
if( !msg )
goto EXIT;
if( !dbus_connection_send_with_reply(dbus_connection, msg, &pc, -1) ) {
mce_log(LL_CRIT, "Out of memory when sending D-Bus message");
goto EXIT;
}
if( !pc ) {
mce_log(LL_ERR, "D-Bus connection disconnected");
goto EXIT;
}
if( !dbus_pending_call_set_notify(pc, callback,
user_data, user_free) ) {
mce_log(LL_CRIT, "Out of memory when sending D-Bus message");
goto EXIT;
}
/* If caller asked for pending call handle, increase
* the refcount by one */
if( ppc )
*ppc = dbus_pending_call_ref(pc);
/* Ownership of user_data passed on */
user_free = 0, user_data = 0;
status = TRUE;
EXIT:
/* Release user_data if the ownership was not passed on */
if( user_free )
user_free(user_data);
/* If notification was set succesfully above, it will hold
* one reference to the pending call until a) the callback function
* gets called, or b) the pending call is canceled */
if( pc )
dbus_pending_call_unref(pc);
if( msg )
dbus_message_unref(msg);
return status;
}
static gboolean dbus_send_va(const char *service,
const char *path,
const char *interface,
const char *name,
DBusPendingCallNotifyFunction callback,
void *user_data, DBusFreeFunction user_free,
DBusPendingCall **ppc,
int first_arg_type, va_list va)
{
gboolean res = FALSE;
DBusMessage *msg = 0;
/* Method call or signal? */
if( service ) {
msg = dbus_new_method_call(service, path, interface, name);
if( !callback )
dbus_message_set_no_reply(msg, TRUE);
}
else if( callback ) {
mce_log(LL_ERR, "Programmer snafu! "
"dbus_send() called with a DBusPending "
"callback for a signal. Whoopsie!");
goto EXIT;
}
else {
msg = dbus_new_signal(path, interface, name);
}
/* Append the arguments, if any */
if( first_arg_type != DBUS_TYPE_INVALID &&
!dbus_message_append_args_valist(msg, first_arg_type, va)) {
mce_log(LL_CRIT, "Failed to append arguments to D-Bus message "
"for %s.%s", interface, name);
goto EXIT;
}
/* Send the signal / call the method */
if( !callback ) {
res = dbus_send_message(msg);
msg = 0;
}
else {
res = dbus_send_message_with_reply_handler(msg, callback,
user_data,
user_free,
ppc);
msg = 0;
/* Ownership of user_data passed on */
user_data = 0, user_free = 0;
}
EXIT:
/* Release user_data if the ownership was not passed on */
if( user_free )
user_free(user_data);
if( msg ) dbus_message_unref(msg);
return res;
}
/** Generic function to send D-Bus messages and signals
* to send a signal, call dbus_send with service == NULL
*
* @todo Make it possible to send D-Bus replies as well
*
* @param service D-Bus service; for signals, set to NULL
* @param path D-Bus path
* @param interface D-Bus interface
* @param name The D-Bus method or signal name to send to
* @param callback A reply callback, or NULL to set no reply;
* for signals, this is unused, but please use NULL
* for consistency
* @param user_data Data to pass to callback
* @param user_free Data release callback for user_data
* @param ppc Where to store pending call handle, or NULL
* @param first_arg_type The DBUS_TYPE of the first argument in the list
* @param ... The arguments to append to the D-Bus message;
* terminate with DBUS_TYPE_INVALID
* Note: the arguments MUST be passed by reference
*
* @return TRUE on success, FALSE on failure
*/
gboolean dbus_send_ex(const char *service,
const char *path,
const char *interface,
const char *name,
DBusPendingCallNotifyFunction callback,
void *user_data, DBusFreeFunction user_free,
DBusPendingCall **ppc,
int first_arg_type, ...)
{
va_list va;
va_start(va, first_arg_type);
gboolean res = dbus_send_va(service, path, interface, name,
callback, user_data, user_free,
ppc, first_arg_type, va);
va_end(va);
return res;
}
/**
* Generic function to send D-Bus messages and signals
* to send a signal, call dbus_send with service == NULL
*
* @todo Make it possible to send D-Bus replies as well
*
* @param service D-Bus service; for signals, set to NULL
* @param path D-Bus path
* @param interface D-Bus interface
* @param name The D-Bus method or signal name to send to
* @param callback A reply callback, or NULL to set no reply;
* for signals, this is unused, but please use NULL
* for consistency
* @param first_arg_type The DBUS_TYPE of the first argument in the list
* @param ... The arguments to append to the D-Bus message;
* terminate with DBUS_TYPE_INVALID
* Note: the arguments MUST be passed by reference
* @return TRUE on success, FALSE on failure
*/
gboolean dbus_send(const gchar *const service, const gchar *const path,
const gchar *const interface, const gchar *const name,
DBusPendingCallNotifyFunction callback,
int first_arg_type, ...)
{
va_list va;
va_start(va, first_arg_type);
gboolean res = dbus_send_va(service, path, interface, name,
callback, 0, 0, 0, first_arg_type, va);
va_end(va);
return res;
}
/**
* D-Bus callback for the version get method call
*
* @param msg The D-Bus message to reply to
* @return TRUE on success, FALSE on failure
*/
static gboolean version_get_dbus_cb(DBusMessage *const msg)
{
static const gchar *const versionstring = G_STRINGIFY(PRG_VERSION);
DBusMessage *reply = NULL;
gboolean status = FALSE;
mce_log(LL_DEBUG, "Received version information request");
/* Create a reply */
reply = dbus_new_method_reply(msg);
/* Append the version information */
if (dbus_message_append_args(reply,
DBUS_TYPE_STRING, &versionstring,
DBUS_TYPE_INVALID) == FALSE) {
mce_log(LL_CRIT,
"Failed to append reply argument to D-Bus message "
"for %s.%s",
MCE_REQUEST_IF, MCE_VERSION_GET);
dbus_message_unref(reply);
goto EXIT;
}
/* Send the message */
status = dbus_send_message(reply);
EXIT:
return status;
}
/** D-Bus callback for the get suspend time statistics method call
*
* @param req The D-Bus message to reply to
*
* @return TRUE
*/
static gboolean suspend_stats_get_dbus_cb(DBusMessage *const req)
{
DBusMessage *rsp = 0;
mce_log(LL_DEVEL, "suspend info request from %s",
mce_dbus_get_message_sender_ident(req));
/* get stats */
dbus_int64_t uptime_ms = mce_lib_get_boot_tick();
dbus_int64_t suspend_ms = uptime_ms - mce_lib_get_mono_tick();
/* create and send reply message */
rsp = dbus_new_method_reply(req);
if( !dbus_message_append_args(rsp,
DBUS_TYPE_INT64, &uptime_ms,
DBUS_TYPE_INT64, &suspend_ms,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to append arguments");
goto EXIT;
}
dbus_send_message(rsp), rsp = 0;
EXIT:
if( rsp )
dbus_message_unref(rsp);
return TRUE;
}
/** Helper for appending gconf string list to dbus message
*
* @param conf GConfValue of string list type
* @param pcount number of items in the returned array is stored here
* @return array of string pointers that can be easily added to DBusMessage
*/
static const char **string_array_from_gconf_value(GConfValue *conf, int *pcount)
{
const char **array = 0;
int count = 0;
GSList *list, *item;
if( conf->type != GCONF_VALUE_LIST )
goto EXIT;
if( gconf_value_get_list_type(conf) != GCONF_VALUE_STRING )
goto EXIT;
list = gconf_value_get_list(conf);
for( item = list; item; item = item->next )
++count;
array = g_malloc_n(count, sizeof *array);
count = 0;
for( item = list; item; item = item->next )
array[count++] = gconf_value_get_string(item->data);
EXIT:
return *pcount = count, array;
}
/** Helper for appending gconf int list to dbus message
*
* @param conf GConfValue of int list type
* @param pcount number of items in the returned array is stored here
* @return array of integers that can be easily added to DBusMessage
*/
static dbus_int32_t *int_array_from_gconf_value(GConfValue *conf, int *pcount)
{
dbus_int32_t *array = 0;
int count = 0;
GSList *list, *item;
if( conf->type != GCONF_VALUE_LIST )
goto EXIT;
if( gconf_value_get_list_type(conf) != GCONF_VALUE_INT )
goto EXIT;
list = gconf_value_get_list(conf);
for( item = list; item; item = item->next )
++count;
array = g_malloc_n(count, sizeof *array);
count = 0;
for( item = list; item; item = item->next )
array[count++] = gconf_value_get_int(item->data);
EXIT:
return *pcount = count, array;
}
/** Helper for appending gconf bool list to dbus message
*
* @param conf GConfValue of bool list type
* @param pcount number of items in the returned array is stored here
* @return array of booleans that can be easily added to DBusMessage
*/
static dbus_bool_t *bool_array_from_gconf_value(GConfValue *conf, int *pcount)
{
dbus_bool_t *array = 0;
int count = 0;
GSList *list, *item;
if( conf->type != GCONF_VALUE_LIST )
goto EXIT;
if( gconf_value_get_list_type(conf) != GCONF_VALUE_BOOL )
goto EXIT;
list = gconf_value_get_list(conf);
for( item = list; item; item = item->next )
++count;
array = g_malloc_n(count, sizeof *array);
count = 0;
for( item = list; item; item = item->next )
array[count++] = gconf_value_get_bool(item->data);
EXIT:
return *pcount = count, array;
}
/** Helper for appending gconf float list to dbus message
*
* @param conf GConfValue of float list type
* @param pcount number of items in the returned array is stored here
* @return array of doubles that can be easily added to DBusMessage
*/
static double *float_array_from_gconf_value(GConfValue *conf, int *pcount)
{
double *array = 0;
int count = 0;
GSList *list, *item;
if( conf->type != GCONF_VALUE_LIST )
goto EXIT;
if( gconf_value_get_list_type(conf) != GCONF_VALUE_FLOAT )
goto EXIT;
list = gconf_value_get_list(conf);
for( item = list; item; item = item->next )
++count;
array = g_malloc_n(count, sizeof *array);
count = 0;
for( item = list; item; item = item->next )
array[count++] = gconf_value_get_float(item->data);
EXIT:
return *pcount = count, array;
}
/** Helper for deducing what kind of array signature we need for a list value
*
* @param type Non-complex gconf value type
*
* @return D-Bus signature needed for adding given type to a container
*/
static const char *type_signature(GConfValueType type)
{
switch( type ) {
case GCONF_VALUE_STRING: return DBUS_TYPE_STRING_AS_STRING;
case GCONF_VALUE_INT: return DBUS_TYPE_INT32_AS_STRING;
case GCONF_VALUE_FLOAT: return DBUS_TYPE_DOUBLE_AS_STRING;
case GCONF_VALUE_BOOL: return DBUS_TYPE_BOOLEAN_AS_STRING;
default: break;
}
return 0;
}
/** Helper for deducing what kind of variant signature we need for a value
*
* @param conf GConf value
*
* @return D-Bus signature needed for adding given value to a container
*/
static const char *value_signature(GConfValue *conf)
{
if( conf->type != GCONF_VALUE_LIST ) {
return type_signature(conf->type);
}
switch( gconf_value_get_list_type(conf) ) {
case GCONF_VALUE_STRING:
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
case GCONF_VALUE_INT:
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_INT32_AS_STRING;
case GCONF_VALUE_FLOAT:
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_DOUBLE_AS_STRING;
case GCONF_VALUE_BOOL:
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BOOLEAN_AS_STRING;
default: break;
}
return 0;
}
/** Helper for appending GConfValue to dbus message
*
* @param reply DBusMessage under construction
* @param conf GConfValue to be added to the reply
*
* @return TRUE if the value was succesfully appended, or FALSE on failure
*/
static gboolean append_gconf_value_to_dbus_message(DBusMessage *reply, GConfValue *conf)
{
const char *sig = 0;
DBusMessageIter body, variant, array;
if( !(sig = value_signature(conf)) ) {
goto bailout_message;
}
dbus_message_iter_init_append(reply, &body);
if( !dbus_message_iter_open_container(&body, DBUS_TYPE_VARIANT,
sig, &variant) ) {
goto bailout_message;
}
switch( conf->type ) {
case GCONF_VALUE_STRING:
{
const char *arg = gconf_value_get_string(conf) ?: "";
dbus_message_iter_append_basic(&variant,
DBUS_TYPE_STRING,
&arg);
}
break;
case GCONF_VALUE_INT:
{
dbus_int32_t arg = gconf_value_get_int(conf);
dbus_message_iter_append_basic(&variant,
DBUS_TYPE_INT32,
&arg);
}
break;
case GCONF_VALUE_FLOAT:
{
double arg = gconf_value_get_float(conf);
dbus_message_iter_append_basic(&variant,
DBUS_TYPE_DOUBLE,
&arg);
}
break;
case GCONF_VALUE_BOOL:
{
dbus_bool_t arg = gconf_value_get_bool(conf);
dbus_message_iter_append_basic(&variant,
DBUS_TYPE_BOOLEAN,
&arg);
}
break;
case GCONF_VALUE_LIST:
if( !(sig = type_signature(gconf_value_get_list_type(conf))) ) {
goto bailout_variant;
}
if( !dbus_message_iter_open_container(&variant,
DBUS_TYPE_ARRAY,
sig, &array) ) {
goto bailout_variant;
}
switch( gconf_value_get_list_type(conf) ) {
case GCONF_VALUE_STRING:
{
int cnt = 0;
const char **arg = string_array_from_gconf_value(conf, &cnt);
for( int i = 0; i < cnt; ++i ) {
const char *str = arg[i];
dbus_message_iter_append_basic(&array,
DBUS_TYPE_STRING,
&str);
}
g_free(arg);
}
break;
case GCONF_VALUE_INT:
{
int cnt = 0;
dbus_int32_t *arg = int_array_from_gconf_value(conf, &cnt);
dbus_message_iter_append_fixed_array(&array,