/
mcetool.c
7299 lines (6421 loc) · 243 KB
/
mcetool.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 mcetool.c
* Tool to test and remote control the Mode Control Entity
* <p>
* Copyright © 2005-2011 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <david.weinehall@nokia.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-command-line.h"
#include "../mce-setting.h"
#include "../tklock.h"
#include "../powerkey.h"
#include "../event-input.h"
#include "../modules/display.h"
#include "../modules/doubletap.h"
#include "../modules/inactivity.h"
#include "../modules/powersavemode.h"
#include "../modules/proximity.h"
#include "../modules/memnotify.h"
#include "../modules/led.h"
#include "../systemui/dbus-names.h"
#include "../systemui/tklock-dbus-names.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <syslog.h>
#include <dbus/dbus.h>
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
/** Whether to enable development time debugging */
#define MCETOOL_ENABLE_EXTRA_DEBUG 0
/** Name shown by --help etc. */
#define PROG_NAME "mcetool"
/** Default padding for left column of status reports */
#define PAD1 "36"
/** Padding used for radio state bits */
#define PAD2 "28"
#if MCETOOL_ENABLE_EXTRA_DEBUG
# define debugf(FMT, ARGS...) fprintf(stderr, PROG_NAME": D: "FMT, ##ARGS)
#else
# define debugf(FMT, ARGS...) do { }while(0)
#endif
# define errorf(FMT, ARGS...) fprintf(stderr, PROG_NAME": E: "FMT, ##ARGS)
/* ------------------------------------------------------------------------- *
* GENERIC DBUS HELPERS
* ------------------------------------------------------------------------- */
/** Cached D-Bus connection */
static DBusConnection *xdbus_con = NULL;
/** Initialize D-Bus system bus connection
*
* Makes a cached connection to system bus and checks if mce is present
*
* @return System bus connection on success, terminates on failure
*/
static DBusConnection *xdbus_init(void)
{
if( !xdbus_con ) {
DBusError err = DBUS_ERROR_INIT;
DBusBusType bus_type = DBUS_BUS_SYSTEM;
if( !(xdbus_con = dbus_bus_get(bus_type, &err)) ) {
errorf("Failed to open connection to message bus; %s: %s\n",
err.name, err.message);
dbus_error_free(&err);
exit(EXIT_FAILURE);
}
debugf("connected to system bus\n");
if( !dbus_bus_name_has_owner(xdbus_con, MCE_SERVICE, &err) ) {
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", MCE_SERVICE,
err.name, err.message);
}
errorf("MCE not running, terminating\n");
dbus_error_free(&err);
exit(EXIT_FAILURE);
}
debugf("mce is running\n");
}
return xdbus_con;
}
/** Disconnect from D-Bus system bus
*/
static void xdbus_exit(void)
{
/* If there is an established D-Bus connection, unreference it */
if (xdbus_con != NULL) {
dbus_connection_unref(xdbus_con);
xdbus_con = NULL;
debugf("disconnected from system bus\n");
}
}
/** Make sure the cached dbus connection is not used directly */
#define xdbus_con something_that_will_generate_error
/** Generic synchronous D-Bus method call wrapper function
*
* If reply pointer is NULL, the method call is sent without
* waiting for method return message.
*
* @param service [IN] D-Bus service name
* @param path [IN] D-Bus object path
* @param interface [IN] D-Bus interface name
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store method_return message, or NULL
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param va [IN] va_list of D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent (and optionally reply received),
* or FALSE in case of errors or if error reply is received
*/
static gboolean xdbus_call_va(const gchar *const service, const gchar *const path,
const gchar *const interface, const gchar *const name,
DBusMessage **reply, int arg_type, va_list va)
{
debugf("%s(%s,%s,%s,%s)\n", __FUNCTION__, service, path, interface, name);
gboolean ack = FALSE;
DBusMessage *msg = 0;
DBusMessage *rsp = 0;
DBusConnection *bus = xdbus_init();
DBusError err = DBUS_ERROR_INIT;
msg = dbus_message_new_method_call(service, path, interface, name);
if( !dbus_message_append_args_valist(msg, arg_type, va) ) {
errorf("%s.%s: failed to construct message\n", interface, name);
goto EXIT;
}
dbus_message_set_auto_start(msg, FALSE);
if( reply ) {
rsp = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err);
if( rsp == 0 ) {
errorf("%s.%s send message: %s: %s\n",
interface, name, err.name, err.message);
goto EXIT;
}
if( dbus_set_error_from_message(&err, rsp) ) {
errorf("%s.%s call failed: %s: %s\n",
interface, name, err.name, err.message);
dbus_message_unref(rsp), rsp = 0;
goto EXIT;
}
}
else {
dbus_message_set_no_reply(msg, TRUE);
if( !dbus_connection_send(bus, msg, NULL) ) {
errorf("Failed to send method call\n");
goto EXIT;
}
dbus_connection_flush(bus);
}
ack = TRUE;
EXIT:
if( reply ) *reply = rsp, rsp = 0;
dbus_error_free(&err);
if( rsp ) dbus_message_unref(rsp);
if( msg ) dbus_message_unref(msg);
return ack;
}
/* ------------------------------------------------------------------------- *
* MCE DBUS IPC HELPERS
* ------------------------------------------------------------------------- */
/** Wrapper for making synchronous D-Bus method calls to MCE
*
* If reply pointer is NULL, the method call is sent without
* waiting for method return message.
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store method_return message, or NULL
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param va [IN] va_list of D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent (and optionally reply received),
* or FALSE in case of errors
*/
static gboolean xmce_ipc_va(const gchar *const name, DBusMessage **reply,
int arg_type, va_list va)
{
return xdbus_call_va(MCE_SERVICE,
MCE_REQUEST_PATH,
MCE_REQUEST_IF,
name, reply,
arg_type, va);
}
/** Wrapper for making MCE D-Bus method calls without waiting for reply
*
* @param name [IN] D-Bus method call name
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent, or FALSE in case of errors
*/
static gboolean xmce_ipc_no_reply(const gchar *const name,
int arg_type, ...)
{
DBusMessage *reply = 0;
va_list va;
va_start(va, arg_type);
gboolean ack = xmce_ipc_va(name, &reply, arg_type, va);
va_end(va);
if( reply )
dbus_message_unref(reply);
return ack;
}
/** Wrapper for making synchronous MCE D-Bus method calls
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store method_return message
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_message_reply(const gchar *const name, DBusMessage **reply,
int arg_type, ...)
{
va_list va;
va_start(va, arg_type);
gboolean ack = xmce_ipc_va(name, reply, arg_type, va);
va_end(va);
return ack;
}
/** Wrapper for making synchronous MCE D-Bus method calls that return STRING
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store string from method return
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received and parsed,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_string_reply(const gchar *const name,
char **result,
int arg_type, ...)
{
gboolean ack = FALSE;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
const char *dta = 0;
va_list va;
va_start(va, arg_type);
if( !xmce_ipc_va(name, &rsp, arg_type, va) )
goto EXIT;
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_STRING, &dta,
DBUS_TYPE_INVALID) )
goto EXIT;
*result = strdup(dta);
ack = TRUE;
EXIT:
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", name, err.name, err.message);
dbus_error_free(&err);
}
if( rsp ) dbus_message_unref(rsp);
va_end(va);
return ack;
}
/** Wrapper for making synchronous MCE D-Bus method calls that return UINT32
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store uint from method return
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received and parsed,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_uint_reply(const gchar *const name,
guint *result,
int arg_type, ...)
{
gboolean ack = FALSE;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_uint32_t dta = 0;
va_list va;
va_start(va, arg_type);
if( !xmce_ipc_va(name, &rsp, arg_type, va) )
goto EXIT;
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_UINT32, &dta,
DBUS_TYPE_INVALID) )
goto EXIT;
*result = dta;
ack = TRUE;
EXIT:
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", name, err.name, err.message);
dbus_error_free(&err);
}
if( rsp ) dbus_message_unref(rsp);
va_end(va);
return ack;
}
/** Wrapper for making synchronous MCE D-Bus method calls that return INT32
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store int from method return
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received and parsed,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_int_reply(const gchar *const name,
gint *result,
int arg_type, ...)
{
gboolean ack = FALSE;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_int32_t dta = 0;
va_list va;
va_start(va, arg_type);
if( !xmce_ipc_va(name, &rsp, arg_type, va) )
goto EXIT;
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_INT32, &dta,
DBUS_TYPE_INVALID) )
goto EXIT;
*result = dta;
ack = TRUE;
EXIT:
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", name, err.name, err.message);
dbus_error_free(&err);
}
if( rsp ) dbus_message_unref(rsp);
va_end(va);
return ack;
}
/** Wrapper for making synchronous MCE D-Bus method calls that return BOOLEAN
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store bool from method return
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received and parsed,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_bool_reply(const gchar *const name,
gboolean *result,
int arg_type, ...)
{
gboolean ack = FALSE;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_bool_t dta = 0;
va_list va;
va_start(va, arg_type);
if( !xmce_ipc_va(name, &rsp, arg_type, va) )
goto EXIT;
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_BOOLEAN, &dta,
DBUS_TYPE_INVALID) )
goto EXIT;
*result = dta;
ack = TRUE;
EXIT:
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", name, err.name, err.message);
dbus_error_free(&err);
}
if( rsp ) dbus_message_unref(rsp);
va_end(va);
return ack;
}
/* ------------------------------------------------------------------------- *
* MCE IPC HELPERS
* ------------------------------------------------------------------------- */
/** Helper for getting dbus data type as string
*
* @param type dbus data type (DBUS_TYPE_BOOLEAN etc)
*
* @return type name with out common prefix (BOOLEAN etc)
*/
static const char *dbushelper_get_type_name(int type)
{
const char *res = "UNKNOWN";
switch( type ) {
case DBUS_TYPE_INVALID: res = "INVALID"; break;
case DBUS_TYPE_BYTE: res = "BYTE"; break;
case DBUS_TYPE_BOOLEAN: res = "BOOLEAN"; break;
case DBUS_TYPE_INT16: res = "INT16"; break;
case DBUS_TYPE_UINT16: res = "UINT16"; break;
case DBUS_TYPE_INT32: res = "INT32"; break;
case DBUS_TYPE_UINT32: res = "UINT32"; break;
case DBUS_TYPE_INT64: res = "INT64"; break;
case DBUS_TYPE_UINT64: res = "UINT64"; break;
case DBUS_TYPE_DOUBLE: res = "DOUBLE"; break;
case DBUS_TYPE_STRING: res = "STRING"; break;
case DBUS_TYPE_OBJECT_PATH: res = "OBJECT_PATH"; break;
case DBUS_TYPE_SIGNATURE: res = "SIGNATURE"; break;
case DBUS_TYPE_UNIX_FD: res = "UNIX_FD"; break;
case DBUS_TYPE_ARRAY: res = "ARRAY"; break;
case DBUS_TYPE_VARIANT: res = "VARIANT"; break;
case DBUS_TYPE_STRUCT: res = "STRUCT"; break;
case DBUS_TYPE_DICT_ENTRY: res = "DICT_ENTRY"; break;
default: break;
}
return res;
}
/** Helper for testing that iterator points to expected data type
*
* @param iter D-Bus message iterator
* @param want_type D-Bus data type
*
* @return TRUE if iterator points to give data type, FALSE otherwise
*/
static gboolean dbushelper_require_type(DBusMessageIter *iter,
int want_type)
{
int have_type = dbus_message_iter_get_arg_type(iter);
if( want_type != have_type ) {
errorf("expected DBUS_TYPE_%s, got %s\n",
dbushelper_get_type_name(want_type),
dbushelper_get_type_name(have_type));
return FALSE;
}
return TRUE;
}
/** Helper for testing that iterator points to array of expected data type
*
* @param iter D-Bus message iterator
* @param want_type D-Bus data type
*
* @return TRUE if iterator points to give data type, FALSE otherwise
*/
static gboolean dbushelper_require_array_type(DBusMessageIter *iter,
int want_type)
{
if( !dbushelper_require_type(iter, DBUS_TYPE_ARRAY) )
return FALSE;
int have_type = dbus_message_iter_get_element_type(iter);
if( want_type != have_type ) {
errorf("expected array of DBUS_TYPE_%s, got %s\n",
dbushelper_get_type_name(want_type),
dbushelper_get_type_name(have_type));
return FALSE;
}
return TRUE;
}
/** Helper for making blocking D-Bus method calls
*
* @param req D-Bus method call message to send
*
* @return D-Bus method reply message, or NULL on failure
*/
static DBusMessage *dbushelper_call_method(DBusMessage *req)
{
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
rsp = dbus_connection_send_with_reply_and_block(xdbus_init(),
req, -1, &err);
if( !rsp ) {
errorf("%s.%s: %s: %s\n",
dbus_message_get_interface(req),
dbus_message_get_member(req),
err.name, err.message);
goto EXIT;
}
EXIT:
dbus_error_free(&err);
return rsp;
}
/** Helper for detecting end of data from D-Bus message iterator
*
* @param iter D-Bus message iterator
*
* @return TRUE if iterator points to DBUS_TYPE_INVALID, FALSE otherwise
*/
static gboolean dbushelper_read_at_end(DBusMessageIter *iter)
{
int have_type = dbus_message_iter_get_arg_type(iter);
return have_type == DBUS_TYPE_INVALID;
}
/** Helper for parsing int value from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param value Where to store the value (not modified on failure)
*
* @return TRUE if value could be read, FALSE on failure
*/
static gboolean dbushelper_read_int(DBusMessageIter *iter, gint *value)
{
dbus_int32_t data = 0;
if( !dbushelper_require_type(iter, DBUS_TYPE_INT32) )
return FALSE;
dbus_message_iter_get_basic(iter, &data);
dbus_message_iter_next(iter);
return *value = data, TRUE;
}
/** Helper for parsing int64 value from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param value Where to store the value (not modified on failure)
*
* @return TRUE if value could be read, FALSE on failure
*/
static gboolean dbushelper_read_int64(DBusMessageIter *iter, int64_t *value)
{
dbus_int64_t data = 0;
if( !dbushelper_require_type(iter, DBUS_TYPE_INT64) )
return FALSE;
dbus_message_iter_get_basic(iter, &data);
dbus_message_iter_next(iter);
return *value = data, TRUE;
}
/** Helper for parsing string value from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param value Where to store the value (not modified on failure)
*
* @return TRUE if value could be read, FALSE on failure
*/
static gboolean dbushelper_read_string(DBusMessageIter *iter, gchar **value)
{
char *data = 0;
if( !dbushelper_require_type(iter, DBUS_TYPE_STRING) )
return FALSE;
dbus_message_iter_get_basic(iter, &data);
dbus_message_iter_next(iter);
return *value = g_strdup(data ?: ""), TRUE;
}
/** Helper for parsing boolean value from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param value Where to store the value (not modified on failure)
*
* @return TRUE if value could be read, FALSE on failure
*/
static gboolean dbushelper_read_boolean(DBusMessageIter *iter, gboolean *value)
{
dbus_bool_t data = 0;
if( !dbushelper_require_type(iter, DBUS_TYPE_BOOLEAN) )
return FALSE;
dbus_message_iter_get_basic(iter, &data);
dbus_message_iter_next(iter);
return *value = data, TRUE;
}
/** Helper for entering variant container from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param sub D-Bus message iterator for variant (not modified on failure)
*
* @return TRUE if container could be entered, FALSE on failure
*/
static gboolean dbushelper_read_variant(DBusMessageIter *iter, DBusMessageIter *sub)
{
if( !dbushelper_require_type(iter, DBUS_TYPE_VARIANT) )
return FALSE;
dbus_message_iter_recurse(iter, sub);
dbus_message_iter_next(iter);
return TRUE;
}
/** Helper for entering array container from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param sub D-Bus message iterator for array (not modified on failure)
*
* @return TRUE if container could be entered, FALSE on failure
*/
static gboolean dbushelper_read_array(DBusMessageIter *iter, DBusMessageIter *sub)
{
if( !dbushelper_require_type(iter, DBUS_TYPE_ARRAY) )
return FALSE;
dbus_message_iter_recurse(iter, sub);
dbus_message_iter_next(iter);
return TRUE;
}
/** Helper for entering dict entry container from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param sub D-Bus message iterator for dict entry (not modified on failure)
*
* @return TRUE if container could be entered, FALSE on failure
*/
static gboolean dbushelper_read_dict(DBusMessageIter *iter, DBusMessageIter *sub)
{
if( !dbushelper_require_type(iter, DBUS_TYPE_DICT_ENTRY) )
return FALSE;
dbus_message_iter_recurse(iter, sub);
dbus_message_iter_next(iter);
return TRUE;
}
/** Helper for entering struct container from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param sub D-Bus message iterator for struct (not modified on failure)
*
* @return TRUE if container could be entered, FALSE on failure
*/
static gboolean dbushelper_read_struct(DBusMessageIter *iter, DBusMessageIter *sub)
{
if( !dbushelper_require_type(iter, DBUS_TYPE_STRUCT) )
return FALSE;
dbus_message_iter_recurse(iter, sub);
dbus_message_iter_next(iter);
return TRUE;
}
/** Helper for parsing int array from D-Bus message iterator
*
* @param iter D-Bus message iterator
* @param value Where to store the array pointer (not modified on failure)
* @param count Where to store the array length (not modified on failure)
*
* @return TRUE if value could be read, FALSE on failure
*/
static gboolean dbushelper_read_int_array(DBusMessageIter *iter,
gint **value, gint *count)
{
debugf("@%s()\n", __FUNCTION__);
dbus_int32_t *arr_dbus = 0;
gint *arr_glib = 0;
int cnt = 0;
DBusMessageIter tmp;
if( !dbushelper_require_array_type(iter, DBUS_TYPE_INT32) )
return FALSE;
if( !dbushelper_read_array(iter, &tmp) )
return FALSE;
dbus_message_iter_get_fixed_array(&tmp, &arr_dbus, &cnt);
dbus_message_iter_next(iter);
arr_glib = g_malloc0(cnt * sizeof *arr_glib);
for( gint i = 0; i < cnt; ++i )
arr_glib[i] = arr_dbus[i];
return *value = arr_glib, *count = cnt, TRUE;
}
/** Helper for initializing D-Bus message read iterator
*
* @param rsp D-Bus message
* @param iter D-Bus iterator for parsing message (not modified on failure)
*
* @return TRUE if read iterator could be initialized, FALSE on failure
*/
static gboolean dbushelper_init_read_iterator(DBusMessage *rsp,
DBusMessageIter *iter)
{
if( !dbus_message_iter_init(rsp, iter) ) {
errorf("failed to initialize dbus read iterator\n");
return FALSE;
}
return TRUE;
}
/** Helper for initializing D-Bus message write iterator
*
* @param rsp D-Bus message
* @param iter D-Bus iterator for appending message (not modified on failure)
*
* @return TRUE if append iterator could be initialized, FALSE on failure
*/
static gboolean dbushelper_init_write_iterator(DBusMessage *req,
DBusMessageIter *iter)
{
dbus_message_iter_init_append(req, iter);
return TRUE;
}
/** Helper for adding int value to D-Bus iterator
*
* @param iter Write iterator where to add the value
* @param value the value to add
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_write_int(DBusMessageIter *iter, gint value)
{
dbus_int32_t data = value;
int type = DBUS_TYPE_INT32;
if( !dbus_message_iter_append_basic(iter, type, &data) ) {
errorf("failed to add %s data\n",
dbushelper_get_type_name(type));
return FALSE;
}
return TRUE;
}
/** Helper for adding string value to D-Bus iterator
*
* @param iter Write iterator where to add the value
* @param value the value to add
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_write_string(DBusMessageIter *iter, const char *value)
{
const char *data = value ?: "";
int type = DBUS_TYPE_STRING;
if( !dbus_message_iter_append_basic(iter, type, &data) ) {
errorf("failed to add %s data\n",
dbushelper_get_type_name(type));
return FALSE;
}
return TRUE;
}
/** Helper for adding int value array to D-Bus iterator
*
* @param iter Write iterator where to add the value
* @param value the value to add
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_write_int_array(DBusMessageIter *iter,
const gint *value, gint count)
{
gboolean res = FALSE;
int type = DBUS_TYPE_INT32;
dbus_int32_t *data = g_malloc0(count * sizeof *data);
for( gint i = 0; i < count; ++i )
data[i] = value[i];
if( !dbus_message_iter_append_fixed_array(iter, type, &data, count) ) {
errorf("failed to add array of %s data\n",
dbushelper_get_type_name(type));
goto cleanup;
}
res = TRUE;
cleanup:
g_free(data);
return res;
}
/** Helper for adding boolean value to D-Bus iterator
*
* @param iter Write iterator where to add the value
* @param value the value to add
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_write_boolean(DBusMessageIter *iter, gboolean value)
{
dbus_bool_t data = value;
int type = DBUS_TYPE_BOOLEAN;
if( !dbus_message_iter_append_basic(iter, type, &data) ) {
errorf("failed to add %s data\n",
dbushelper_get_type_name(type));
return FALSE;
}
return TRUE;
}
/** Helper for adding object path value to D-Bus iterator
*
* @param iter Write iterator where to add the value
* @param value the value to add
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_write_path(DBusMessageIter *iter, const gchar *value)
{
const char *data = value;
int type = DBUS_TYPE_OBJECT_PATH;
if( !dbus_message_iter_append_basic(iter, type, &data) ) {
errorf("failed to add %s data\n",
dbushelper_get_type_name(type));
return FALSE;
}
return TRUE;
}
/** Helper for opening a variant container
*
* @param stack pointer to D-Bus message iterator pointer (not
modified on failure)
*
* @param signature signature string of the data that will be added to the
* variant container
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_push_variant(DBusMessageIter **stack,
const char *signature)
{
DBusMessageIter *iter = *stack;
DBusMessageIter *sub = iter + 1;
if( !dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
signature, sub) ) {
errorf("failed to initialize variant write iterator\n");
return FALSE;
}
*stack = sub;
return TRUE;
}
/** Helper for opening a array container
*
* @param stack pointer to D-Bus message iterator pointer (not
modified on failure)
*
* @param signature signature string of the data that will be added to the
* variant container
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_push_array(DBusMessageIter **stack,
const char *signature)
{
DBusMessageIter *iter = *stack;
DBusMessageIter *sub = iter + 1;
if( !dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
signature, sub) ) {
errorf("failed to initialize array write iterator\n");
return FALSE;
}
*stack = sub;
return TRUE;
}
/** Helper for closing a container
*
* @param stack pointer to D-Bus message iterator pointer
*
* @return TRUE on success, FALSE on failure
*/
static gboolean dbushelper_pop_container(DBusMessageIter **stack)
{
DBusMessageIter *sub = *stack;
DBusMessageIter *iter = sub - 1;
gboolean res = dbus_message_iter_close_container(iter, sub);
*stack = iter;
return res;
}
/** Helper for abandoning message iterator stack
*
* @param stack Start of iterator stack
* @param iter Current iterator within the stack
*/
static void dbushelper_abandon_stack(DBusMessageIter *stack,
DBusMessageIter *iter)
{
while( iter-- > stack )
dbus_message_iter_abandon_container(iter, iter+1);
}
/* ------------------------------------------------------------------------- *
* MCE SETTING IPC HELPERS
* ------------------------------------------------------------------------- */
/** Helper for making MCE D-Bus method calls
*
* @param method name of the method in mce request interface
* @param arg_type as with dbus_message_append_args()
* @param ... must be terminated with DBUS_TYPE_INVALID
*/
static DBusMessage *xmce_setting_request(const gchar *const method)
{
DBusMessage *req = 0;