/
mcetool.c
8079 lines (7142 loc) · 286 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).
* Copyright (C) 2012-2019 Jolla Ltd.
* Copyright (c) 2019 Open Mobile Platform LLC.
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
* @author Victor Portnov <ext-victor.portnov@nokia.com>
* @author Philippe De Swert <philippe.deswert@jollamobile.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
* @author Vesa Halttunen <vesa.halttunen@jollamobile.com>
* @author Kimmo Lindholm <kimmo.lindholm@eke.fi>
*
* 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-dbus.h"
#include "../mce-setting.h"
#include "../tklock.h"
#include "../powerkey.h"
#include "../event-input.h"
#include "../evdev.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)
/* ========================================================================= *
* Types
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* SYMBOL LOOKUP TABLES
* ------------------------------------------------------------------------- */
/** Simple string key to integer value symbol */
typedef struct
{
/** Name of the symbol, or NULL to mark end of symbol table */
const char *key;
/** Value of the symbol */
int val;
} symbol_t;
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* XDBUS
* ------------------------------------------------------------------------- */
static DBusConnection *xdbus_init (void);
static void xdbus_exit (void);
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);
/* ------------------------------------------------------------------------- *
* XMCE
* ------------------------------------------------------------------------- */
static gboolean xmce_ipc_va (const gchar *const name, DBusMessage **reply, int arg_type, va_list va);
#ifdef ENABLE_BATTERY_SIMULATION
static gboolean xmce_ipc (const gchar *const name, DBusMessage **reply, int arg_type, ...);
#endif
static gboolean xmce_ipc_no_reply (const gchar *const name, int arg_type, ...);
static gboolean xmce_ipc_message_reply (const gchar *const name, DBusMessage **reply, int arg_type, ...);
static gboolean xmce_ipc_string_reply (const gchar *const name, char **result, int arg_type, ...);
static gboolean xmce_ipc_uint_reply (const gchar *const name, guint *result, int arg_type, ...);
static gboolean xmce_ipc_int_reply (const gchar *const name, gint *result, int arg_type, ...);
static gboolean xmce_ipc_bool_reply (const gchar *const name, gboolean *result, int arg_type, ...);
static DBusMessage *xmce_setting_request (const gchar *const method);
static gboolean xmce_setting_get_bool (const gchar *const key, gboolean *value);
static gboolean xmce_setting_get_int (const gchar *const key, gint *value);
static gboolean xmce_setting_get_string (const gchar *const key, gchar **value);
static gboolean xmce_setting_get_int_array (const gchar *const key, gint **values, gint *count);
static gboolean xmce_setting_set_bool (const gchar *const key, const gboolean value);
static gboolean xmce_setting_set_int (const gchar *const key, const gint value);
static gboolean xmce_setting_set_string (const gchar *const key, const char *value);
static gboolean xmce_setting_set_int_array (const gchar *const key, const gint *values, gint count);
static int xmce_parse_powerkeyevent (const char *args);
static unsigned xmce_parse_radio_states (const char *args);
static gboolean xmce_parse_enabled (const char *args);
static int xmce_parse_integer (const char *args);
static double xmce_parse_double (const char *args);
static bool xmce_set_verbosity (const char *arg);
static void xmce_get_verbosity (void);
static bool xmce_get_color_profile_ids (const char *arg);
static bool xmce_set_color_profile (const char *args);
static void xmce_get_color_profile (void);
#ifdef ENABLE_BATTERY_SIMULATION
static bool xmce_set_charger_type (const char *type);
static bool xmce_set_charger_state (const char *state);
static bool xmce_set_battery_level (int level);
#endif
static void xmce_get_cable_state (void);
static void xmce_get_charger_state (void);
static void xmce_get_battery_status (void);
static void xmce_get_battery_state (void);
static void xmce_get_battery_level (void);
static void xmce_get_battery_info (void);
static void xmce_parse_notification_args (const char *args, char **title, dbus_int32_t *delay, dbus_int32_t *renew);
static bool xmce_notification_begin (const char *args);
static bool xmce_notification_end (const char *args);
static bool xmce_enable_radio (const char *args);
static bool xmce_disable_radio (const char *args);
static void xmce_get_radio_states (void);
static bool xmce_set_lpmui_triggering (const char *args);
static void xmce_get_lpmui_triggering (void);
static bool xmce_set_input_grab_allowed (const char *args);
static void xmce_get_input_grab_allowed (void);
static bool xmce_set_call_state (const char *args);
static void xmce_get_call_state (void);
static bool xmce_set_button_backlligut_off_delay (const char *args);
static void xmce_get_button_backlligut_off_delay (void);
static bool xmce_set_button_backlight_mode (const char *args);
static bool xmce_set_button_backlight (const char *args);
static void xmce_get_button_backlight (void);
static void xmce_set_display_state (const char *state);
static void xmce_get_display_state (void);
static bool xmce_prevent_display_blanking (const char *arg);
static bool xmce_allow_display_blanking (const char *arg);
static bool xmce_set_blank_prevent_mode (const char *args);
static void xmce_get_blank_prevent_mode (void);
static bool xmce_set_display_brightness (const char *args);
static void xmce_get_display_brightness (void);
static bool xmce_set_dimmed_brightness_static (const char *args);
static void xmce_get_dimmed_brightness_static (void);
static bool xmce_set_dimmed_brightness_dynamic (const char *args);
static void xmce_get_dimmed_brightness_dynamic (void);
static bool xmce_set_compositor_dimming_hi (const char *args);
static bool xmce_set_compositor_dimming_lo (const char *args);
static void xmce_get_compositor_dimming (void);
static bool xmce_set_cabc_mode (const char *args);
static void xmce_get_cabc_mode (void);
static bool xmce_reset_settings (const char *args);
static bool xmce_set_dim_timeout (const char *args);
static void xmce_get_dim_timeout (void);
static bool xmce_set_dim_with_kbd_timeout (const char *args);
static void xmce_get_dim_with_kbd_timeout (void);
static bool xmce_set_dim_timeouts (const char *args);
static void xmce_get_dim_timeouts (void);
static bool xmce_set_adaptive_dimming_mode (const char *args);
static void xmce_get_adaptive_dimming_mode (void);
static bool xmce_set_adaptive_dimming_time (const char *args);
static void xmce_get_adaptive_dimming_time (void);
static bool xmce_set_exception_length_call_in (const char *args);
static bool xmce_set_exception_length_call_out (const char *args);
static bool xmce_set_exception_length_alarm (const char *args);
static bool xmce_set_exception_length_usb_connect (const char *args);
static bool xmce_set_exception_length_usb_dialog (const char *args);
static bool xmce_set_exception_length_charger (const char *args);
static bool xmce_set_exception_length_battery (const char *args);
static bool xmce_set_exception_length_jack_in (const char *args);
static bool xmce_set_exception_length_jack_out (const char *args);
static bool xmce_set_exception_length_camera (const char *args);
static bool xmce_set_exception_length_volume (const char *args);
static bool xmce_set_exception_length_activity (const char *args);
static void xmce_get_exception_length (const char *tag, const char *key);
static void xmce_get_exception_lengths (void);
static bool xmce_set_filter_lid_with_als (const char *args);
static void xmce_get_filter_lid_with_als (void);
static bool xmce_set_filter_lid_als_limit (const char *args);
static void xmce_get_filter_lid_als_limit (void);
static bool xmce_set_lid_sensor_mode (const char *args);
static void xmce_get_lid_sensor_mode (void);
static bool xmce_set_lid_open_actions (const char *args);
static void xmce_get_lid_open_actions (void);
static bool xmce_set_lid_close_actions (const char *args);
static void xmce_get_lid_close_actions (void);
static bool xmce_set_kbd_slide_open_trigger (const char *args);
static void xmce_get_kbd_slide_open_trigger (void);
static bool xmce_set_kbd_slide_open_actions (const char *args);
static void xmce_get_kbd_slide_open_actions (void);
static bool xmce_set_kbd_slide_close_trigger (const char *args);
static void xmce_get_kbd_slide_close_trigger (void);
static bool xmce_set_kbd_slide_close_actions (const char *args);
static void xmce_get_kbd_slide_close_actions (void);
static bool xmce_set_orientation_sensor_mode (const char *args);
static void xmce_get_orientation_sensor_mode (void);
static bool xmce_set_orientation_change_is_activity (const char *args);
static void xmce_get_orientation_change_is_activity (void);
static bool xmce_set_flipover_gesture_detection (const char *args);
static void xmce_get_flipover_gesture_detection (void);
static bool xmce_set_ps_mode (const char *args);
static void xmce_get_ps_mode (void);
static bool xmce_set_ps_on_demand (const char *args);
static void xmce_get_ps_on_demand (void);
static bool xmce_set_ps_blocks_touch (const char *args);
static void xmce_get_ps_blocks_touch (void);
static bool xmce_set_ps_acts_as_lid (const char *args);
static void xmce_get_ps_acts_as_lid (void);
static bool xmce_set_ps_uncover_delay_sub (const char *key, const char *args);
static void xmce_get_ps_uncover_delay_sub (const char *tag, const char *key);
static bool xmce_set_default_ps_uncover_delay (const char *args);
static bool xmce_set_incall_ps_uncover_delay (const char *args);
static void xmce_get_ps_uncover_delay (void);
static bool xmce_set_inactivity_shutdown_delay (const char *args);
static void xmce_get_inactivity_shutdown_delay (void);
static bool xmce_set_als_autobrightness (const char *args);
static void xmce_get_als_autobrightness (void);
static bool xmce_set_als_mode (const char *args);
static void xmce_get_als_mode (void);
static bool xmce_is_als_filter_name (const char *name);
static bool xmce_set_als_input_filter (const char *args);
static void xmce_get_als_input_filter (void);
static bool xmce_set_als_sample_time (const char *args);
static void xmce_get_als_sample_time (void);
static bool xmce_set_autolock_mode (const char *args);
static void xmce_get_autolock_mode (void);
static bool xmce_set_autolock_delay (const char *args);
static void xmce_get_autolock_delay (void);
static bool xmce_set_devicelock_in_lockscreen (const char *args);
static void xmce_get_devicelock_in_lockscreen (void);
static bool xmce_set_lockscreen_unblank_animation (const char *args);
static void xmce_get_lockscreen_unblank_animation (void);
static bool xmce_set_blank_timeout (const char *args);
static bool xmce_set_blank_from_lockscreen_timeout (const char *args);
static bool xmce_set_blank_from_lpm_on_timeout (const char *args);
static bool xmce_set_blank_from_lpm_off_timeout (const char *args);
static void xmce_get_blank_timeout_sub (const char *tag, const char *key);
static void xmce_get_blank_timeout (void);
static bool xmce_powerkey_event (const char *args);
static bool xmce_set_powerkey_action (const char *args);
static void xmce_get_powerkey_action (void);
static bool xmce_set_powerkey_blanking (const char *args);
static void xmce_get_powerkey_blanking (void);
static bool xmce_set_powerkey_long_press_delay (const char *args);
static void xmce_get_powerkey_long_press_delay (void);
static bool xmce_set_powerkey_double_press_delay (const char *args);
static void xmce_get_powerkey_double_press_delay (void);
static bool xmce_is_powerkey_action (const char *name);
static bool xmce_is_powerkey_action_mask (const char *names);
static void xmce_set_powerkey_action_mask (const char *key, const char *names);
static bool xmce_set_powerkey_actions_while_display_off_single(const char *args);
static bool xmce_set_powerkey_actions_while_display_off_double(const char *args);
static bool xmce_set_powerkey_actions_while_display_off_long (const char *args);
static bool xmce_set_powerkey_actions_while_display_on_single (const char *args);
static bool xmce_set_powerkey_actions_while_display_on_double (const char *args);
static bool xmce_set_powerkey_actions_while_display_on_long (const char *args);
static bool xmce_set_touchscreen_gesture_action (const char *args);
static void xmce_get_powerkey_action_mask (const char *key, const char *tag);
static void xmce_get_powerkey_action_masks (void);
static bool xmce_is_powerkey_dbus_action (const char *conf);
static bool xmce_set_powerkey_dbus_action (const char *args);
static void xmce_get_powerkey_dbus_action (size_t action_id);
static void xmce_get_powerkey_dbus_actions (void);
static bool xmce_set_ps_override_count (const char *args);
static void xmce_get_ps_override_count (void);
static bool xmce_set_ps_override_timeout (const char *args);
static void xmce_get_ps_override_timeout (void);
static bool xmce_set_display_off_override (const char *args);
static void xmce_get_display_off_override (void);
static bool xmce_set_volkey_policy (const char *args);
static void xmce_get_volkey_policy (void);
static bool xmce_set_doubletap_mode (const char *args);
static bool xmce_set_doubletap_wakeup (const char *args);
static void xmce_get_doubletap_wakeup (void);
static bool xmce_set_fingerprint_wakeup_mode (const char *args);
static void xmce_get_fingerprint_wakeup_mode (void);
static bool xmce_set_fingerprint_wakeup_allow_delay (const char *args);
static void xmce_get_fingerprint_wakeup_allow_delay (void);
static bool xmce_set_fingerprint_wakeup_trigger_delay (const char *args);
static void xmce_get_fingerprint_wakeup_trigger_delay (void);
static bool xmce_set_fingerprint_wakeup_throttle_delay (const char *args);
static void xmce_get_fingerprint_wakeup_throttle_delay (void);
static bool xmce_set_power_saving_mode (const char *args);
static void xmce_get_power_saving_mode (void);
static bool xmce_set_psm_threshold (const char *args);
static void xmce_get_psm_threshold (void);
static bool xmce_set_forced_psm (const char *args);
static void xmce_get_forced_psm (void);
static bool xmce_set_low_power_mode (const char *args);
static void xmce_get_low_power_mode (void);
static bool xmce_set_inhibit_mode (const char *args);
static void xmce_get_inhibit_mode (void);
static bool xmce_set_kbd_slide_inhibit_mode (const char *args);
static void xmce_get_kbd_slide_inhibit_mode (void);
static bool xmce_set_lipstick_core_delay (const char *args);
static void xmce_get_lipstick_core_delay (void);
static bool xmce_set_brightness_fade_default (const char *args);
static bool xmce_set_brightness_fade_dimming (const char *args);
static bool xmce_set_brightness_fade_als (const char *args);
static bool xmce_set_brightness_fade_blank (const char *args);
static bool xmce_set_brightness_fade_unblank (const char *args);
static void xmce_get_brightness_fade_helper (const char *title, const char *key);
static void xmce_get_brightness_fade (void);
static bool xmce_set_memnotify_warning_used (const char *args);
static bool xmce_set_memnotify_warning_active (const char *args);
static bool xmce_set_memnotify_critical_used (const char *args);
static bool xmce_set_memnotify_critical_active (const char *args);
static void xmce_get_memnotify_helper (const char *title, const char *key);
static void xmce_get_memnotify_limits (void);
static void xmce_get_memnotify_level (void);
static bool xmce_set_input_policy_mode (const char *args);
static void xmce_get_input_policy_mode (void);
static bool xmce_set_touch_unblock_delay (const char *args);
static void xmce_get_touch_unblock_delay (void);
static bool xmce_set_cpu_scaling_governor (const char *args);
static void xmce_get_cpu_scaling_governor (void);
static bool xmce_set_never_blank (const char *args);
static void xmce_get_never_blank (void);
static bool xmce_set_suspend_policy (const char *args);
static void xmce_get_suspend_policy (void);
static bool xmce_get_suspend_stats (const char *args);
static bool xmce_get_display_stats (const char *args);
static bool xmce_set_fake_doubletap (const char *args);
static void xmce_get_fake_doubletap (void);
static bool xmce_tklock_open (const char *args);
static bool xmce_tklock_close (const char *args);
static bool xmce_tklock_callback (const char *args);
static bool xmce_set_tklock_mode (const char *args);
static void xmce_get_tklock_mode (void);
static bool xmce_set_tklock_blank (const char *args);
static void xmce_get_tklock_blank (void);
static void xmce_get_version (void);
static void xmce_get_inactivity_state (void);
static void xmce_get_keyboard_backlight_state (void);
static bool xmce_get_status (const char *args);
static bool xmce_set_demo_mode (const char *args);
/* ------------------------------------------------------------------------- *
* DBUSHELPER
* ------------------------------------------------------------------------- */
static const char *dbushelper_get_type_name (int type);
static gboolean dbushelper_require_type (DBusMessageIter *iter, int want_type);
static gboolean dbushelper_require_array_type (DBusMessageIter *iter, int want_type);
static DBusMessage *dbushelper_call_method (DBusMessage *req);
static gboolean dbushelper_read_at_end (DBusMessageIter *iter);
static gboolean dbushelper_read_int (DBusMessageIter *iter, gint *value);
static gboolean dbushelper_read_int64 (DBusMessageIter *iter, int64_t *value);
static gboolean dbushelper_read_string (DBusMessageIter *iter, gchar **value);
static gboolean dbushelper_read_boolean (DBusMessageIter *iter, gboolean *value);
static gboolean dbushelper_read_variant (DBusMessageIter *iter, DBusMessageIter *sub);
static gboolean dbushelper_read_array (DBusMessageIter *iter, DBusMessageIter *sub);
static gboolean dbushelper_read_dict (DBusMessageIter *iter, DBusMessageIter *sub);
static gboolean dbushelper_read_struct (DBusMessageIter *iter, DBusMessageIter *sub);
static gboolean dbushelper_read_int_array (DBusMessageIter *iter, gint **value, gint *count);
static gboolean dbushelper_init_read_iterator (DBusMessage *rsp, DBusMessageIter *iter);
static gboolean dbushelper_init_write_iterator(DBusMessage *req, DBusMessageIter *iter);
static gboolean dbushelper_write_int (DBusMessageIter *iter, gint value);
static gboolean dbushelper_write_string (DBusMessageIter *iter, const char *value);
static gboolean dbushelper_write_int_array (DBusMessageIter *iter, const gint *value, gint count);
static gboolean dbushelper_write_boolean (DBusMessageIter *iter, gboolean value);
static gboolean dbushelper_write_path (DBusMessageIter *iter, const gchar *value);
static gboolean dbushelper_push_variant (DBusMessageIter **stack, const char *signature);
static gboolean dbushelper_push_array (DBusMessageIter **stack, const char *signature);
static gboolean dbushelper_pop_container (DBusMessageIter **stack);
static void dbushelper_abandon_stack (DBusMessageIter *stack, DBusMessageIter *iter);
/* ------------------------------------------------------------------------- *
* UNGROUPED
* ------------------------------------------------------------------------- */
static int lookup (const symbol_t *stab, const char *key);
static const char *rlookup (const symbol_t *stab, int val);
static int parse_inhibitmode (const char *args);
static const char *repr_inhibitmode (int value);
static gint *parse_gint_array (const char *text, gint *len);
static bool is_configurable_pattern (const char *pattern);
static bool set_led_breathing_enabled(const char *args);
static void get_led_breathing_enabled(void);
static bool set_led_breathing_limit (const char *args);
static void get_led_breathing_limit (void);
static bool set_led_pattern_enabled (const char *pattern, bool enable);
static bool interactive_confirmation (const char *positive);
static char *elapsed_time_repr (char *buff, size_t size, int64_t t);
int main (int argc, char **argv);
/* ------------------------------------------------------------------------- *
* MCETOOL
* ------------------------------------------------------------------------- */
static gboolean mcetool_parse_timspec (struct timespec *ts, const char *args);
static char *mcetool_parse_token (char **ppos);
static char *mcetool_format_bitmask (const symbol_t *lut, int mask, char *buff, size_t size);
static unsigned mcetool_parse_bitmask (const symbol_t *lut, const char *args);
static bool mcetool_show_led_patterns(const char *args);
static bool mcetool_block (const char *args);
/* ------------------------------------------------------------------------- *
* MCETOOL_DO
* ------------------------------------------------------------------------- */
static bool mcetool_do_enable_led (const char *arg);
static bool mcetool_do_disable_led (const char *arg);
static bool mcetool_do_enable_pattern (const char *args);
static bool mcetool_do_disable_led_pattern(const char *args);
static bool mcetool_do_activate_pattern (const char *args);
static bool mcetool_do_deactivate_pattern (const char *args);
#ifdef ENABLE_BATTERY_SIMULATION
static bool mcetool_do_set_charger_type(const char *arg);
static bool mcetool_do_set_charger_state (const char *arg);
static bool mcetool_do_set_battery_level (const char *arg);
#endif
static bool mcetool_do_unblank_screen (const char *arg);
static bool mcetool_do_dim_screen (const char *arg);
static bool mcetool_do_blank_screen (const char *arg);
static bool mcetool_do_blank_screen_lpm (const char *arg);
static bool mcetool_do_version (const char *arg);
static bool mcetool_do_help (const char *arg);
static bool mcetool_do_long_help (const char *arg);
/* ------------------------------------------------------------------------- *
* 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 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 ... [IN] 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
*/
#ifdef ENABLE_BATTERY_SIMULATION
static gboolean xmce_ipc(const gchar *const name, DBusMessage **reply,
int arg_type, ...)
{
va_list va;
va_start(va, arg_type);
gboolean ack = xdbus_call_va(MCE_SERVICE,
MCE_REQUEST_PATH,
MCE_REQUEST_IF,
name, reply,
arg_type, va);
va_end(va);
return ack;
}
#endif // ENABLE_BATTERY_SIMULATION
/** 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;