-
Notifications
You must be signed in to change notification settings - Fork 9
/
powerkey.c
4187 lines (3507 loc) · 121 KB
/
powerkey.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 powerkey.c
* Power key logic for the Mode Control Entity
* <p>
* Copyright © 2004-2011 Nokia Corporation and/or its subsidiary(-ies).
* Copyright © 2013-2019 Jolla Ltd.
* <p>
* @author David Weinehall <[email protected]>
* @author Tapio Rantala <[email protected]>
* @author Santtu Lakkala <[email protected]>
* @author Irina Bezruk <[email protected]>
* @author Simo Piiroinen <[email protected]>
* @author Kimmo Lindholm <[email protected]>
* @author Andrew den Exter <[email protected]>
*
* 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 "powerkey.h"
#include "tklock.h"
#include "evdev.h"
#include "mce-log.h"
#include "mce-lib.h"
#include "mce-setting.h"
#include "mce-common.h"
#include "mce-dbus.h"
#include "mce-dsme.h"
#include "modules/doubletap.h"
#include "systemui/dbus-names.h"
#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
#endif
#include <linux/input.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include <libngf/ngf.h>
/* ========================================================================= *
* OVERVIEW
*
* There is a predefined set of actions. Of these two are dbus actions
* that by default make mce broadcast dbus signals, but can be configured
* to make any dbus method call with optional string argument.
*
* Any combination of these actions can be bound to:
* - single power key press
* - double power key press
* - long power key press
*
* The selected actions are executed in a fixed order and actions that
* are common to both single and double press are executed immediately
* after powerkey is released. This allows double press configuration
* to extend what would be done with single press without causing
* delays for single press handling.
*
* Separate combinations are used depending on whether the
* display is on or off during the 1st power key press.
*
* The build-in defaults are as follows
*
* From display off:
* - single press - turns display on
* - double press - turns display on and hides lockscreen (but not device lock)
* - long press - does nothing
*
* From display on:
* - single press - turns display off and activates locksreen
* - double press - turns display off, activates locksreen and locks device
* - long press - initiates shutdown (if lockscreen is not active)
*
* Effectively this is just as before, except for the double press
* actions to apply device lock / wake up to home screen.
* ========================================================================= */
/* ========================================================================= *
* CONSTANTS
* ========================================================================= */
#define MODULE_NAME "powerkey"
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* MISC_UTIL
* ------------------------------------------------------------------------- */
/* Null tolerant string equality predicate
*
* @param s1 string
* @param s2 string
*
* @return true if both s1 and s2 are null or same string, false otherwise
*/
static inline bool eq(const char *s1, const char *s2)
{
return (s1 && s2) ? !strcmp(s1, s2) : (s1 == s2);
}
/** String is NULL or empty predicate
*/
static inline bool empty(const char *s)
{
return s == 0 || *s == 0;
}
static char *pwrkey_get_token(char **ppos);
static bool pwrkey_create_flagfile(const char *path);
static bool pwrkey_delete_flagfile(const char *path);
static int64_t pwrkey_event_tick(const struct input_event *ev);
/* ------------------------------------------------------------------------- *
* PS_OVERRIDE
*
* Provides escape from stuck proximity sensor.
* ------------------------------------------------------------------------- */
/** [setting] Power key press count for proximity sensor override */
static gint pwrkey_ps_override_count = MCE_DEFAULT_POWERKEY_PS_OVERRIDE_COUNT;
static guint pwrkey_ps_override_count_setting_id = 0;
/** [setting] Maximum time between power key presses for proximity sensor override */
static gint pwrkey_ps_override_timeout = MCE_DEFAULT_POWERKEY_PS_OVERRIDE_TIMEOUT;
static guint pwrkey_ps_override_timeout_setting_id = 0;
static void pwrkey_ps_override_evaluate(void);
/* ------------------------------------------------------------------------- *
* ACTION_EXEC
*
* Individual actions that can be taken.
* ------------------------------------------------------------------------- */
static gint pwrkey_action_blank_mode = MCE_DEFAULT_POWERKEY_BLANKING_MODE;
static guint pwrkey_action_blank_mode_setting_id = 0;
static void pwrkey_action_vibrate (void);
static void pwrkey_action_shutdown (void);
static void pwrkey_action_tklock (void);
static void pwrkey_action_blank (void);
static void pwrkey_action_unblank (void);
static void pwrkey_action_unblanked(void);
static void pwrkey_action_tkunlock (void);
static void pwrkey_action_tkunlock2(void);
static void pwrkey_action_devlock (void);
static void pwrkey_action_dbus1 (void);
static void pwrkey_action_dbus2 (void);
static void pwrkey_action_dbus3 (void);
static void pwrkey_action_dbus4 (void);
static void pwrkey_action_dbus5 (void);
static void pwrkey_action_dbus6 (void);
static void pwrkey_action_dbus7 (void);
static void pwrkey_action_dbus8 (void);
static void pwrkey_action_dbus9 (void);
static void pwrkey_action_dbus10 (void);
static void pwrkey_action_nop (void);
/* ------------------------------------------------------------------------- *
* ACTION_SETS
*
* Handle sets of individual actions.
* ------------------------------------------------------------------------- */
typedef struct
{
const char *name;
void (*func)(void);
} pwrkey_bitconf_t;
static uint32_t pwrkey_mask_from_name (const char *name);
static uint32_t pwrkey_mask_from_names (const char *names);
static gchar *pwrkey_mask_to_names (uint32_t mask);
/* ------------------------------------------------------------------------- *
* GESTURE_FILTERING
* ------------------------------------------------------------------------- */
/** Touchscreen gesture (doubletap etc) enable mode */
static gint pwrkey_gestures_enable_mode = MCE_DEFAULT_DOUBLETAP_MODE;
static guint pwrkey_gestures_enable_mode_cb_id = 0;
/* ------------------------------------------------------------------------- *
* PWRKEY_PREDICATE
* ------------------------------------------------------------------------- */
/** Enumeratio of possible unblank allowed predicates */
typedef enum
{
/** Apply powerkey press rules */
PWRKEY_UNBLANK_PREDICATE_POWERKEY,
/** Apply fingerprint wakeup rules */
PWRKEY_UNBLANK_PREDICATE_FPWAKEUP,
/** Apply rules for real gesture events */
PWRKEY_UNBLANK_PREDICATE_GESTURE_REAL,
/** Apply rules for synthetized gesture events */
PWRKEY_UNBLANK_PREDICATE_GESTURE_SYNTH,
/** Rules for action sets that do not include unblank */
PWRKEY_UNBLANK_PREDICATE_DONTCARE,
PWRKEY_UNBLANK_PREDICATE_COUNT
} pwrkey_unblank_predicate_t;
/** Lookup table of unblank predicate names (for diagnostic logging) */
static const char * const pwrkey_unblank_predicate_name[PWRKEY_UNBLANK_PREDICATE_COUNT] =
{
[PWRKEY_UNBLANK_PREDICATE_POWERKEY] = "powerkey",
[PWRKEY_UNBLANK_PREDICATE_FPWAKEUP] = "fpwakeup",
[PWRKEY_UNBLANK_PREDICATE_GESTURE_REAL] = "gesture_real",
[PWRKEY_UNBLANK_PREDICATE_GESTURE_SYNTH] = "gesture_synth",
[PWRKEY_UNBLANK_PREDICATE_DONTCARE] = "dontcare",
};
static bool pwrkey_predicate_gesture (bool synthesized, bool *need_psensor);
static bool pwrkey_predicate_fpwakeup(bool *need_psensor);
static bool pwrkey_predicate_powerkey(bool *need_psensor);
static bool pwrkey_predicate (pwrkey_unblank_predicate_t predicate, bool *need_psensor);
/* ------------------------------------------------------------------------- *
* ACTION_TRIGGERING
* ------------------------------------------------------------------------- */
typedef struct
{
/** Actions common to single and double press */
uint32_t mask_common;
/** Actions for single press */
uint32_t mask_single;
/** Actions for double press */
uint32_t mask_double;
/** Actions for long press */
uint32_t mask_long;
} pwrkey_actions_t;
/** Actions when power key is pressed while display is on */
static pwrkey_actions_t pwrkey_actions_from_display_on = { 0, 0, 0, 0 };
/** Actions when power key is pressed while display is off */
static pwrkey_actions_t pwrkey_actions_from_display_off = { 0, 0, 0, 0 };
/** Actions on touch screen gestures */
static pwrkey_actions_t pwrkey_actions_from_gesture[POWERKEY_ACTIONS_GESTURE_COUNT] = {};
/** Currently selected power key actions; default to turning display on */
static pwrkey_actions_t *pwrkey_actions_now =
&pwrkey_actions_from_display_off;
static gchar *pwrkey_actions_single_on = 0;
static guint pwrkey_actions_single_on_setting_id = 0;
static gchar *pwrkey_actions_double_on = 0;
static guint pwrkey_actions_double_on_setting_id = 0;
static gchar *pwrkey_actions_long_on = 0;
static guint pwrkey_actions_long_on_setting_id = 0;
static gchar *pwrkey_actions_single_off = 0;
static guint pwrkey_actions_single_off_setting_id = 0;
static gchar *pwrkey_actions_double_off = 0;
static guint pwrkey_actions_double_off_setting_id = 0;
static gchar *pwrkey_actions_long_off = 0;
static guint pwrkey_actions_long_off_setting_id = 0;
/** Array of setting keys for configurable touchscreen gestures */
static const char * const pwrkey_actions_gesture_key[POWERKEY_ACTIONS_GESTURE_COUNT] =
{
MCE_SETTING_POWERKEY_ACTIONS_GESTURE0,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE1,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE2,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE3,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE4,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE5,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE6,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE7,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE8,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE9,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE10,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE11,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE12,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE13,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE14,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE15,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE16,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE17,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE18,
MCE_SETTING_POWERKEY_ACTIONS_GESTURE19,
};
/** Array of default values for configurable touchscreen gestures */
static const char * const pwrkey_actions_gesture_val[POWERKEY_ACTIONS_GESTURE_COUNT] =
{
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE0,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE1,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE2,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE3,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE4,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE5,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE6,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE7,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE8,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE9,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE10,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE11,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE12,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE13,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE14,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE15,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE16,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE17,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE18,
MCE_DEFAULT_POWERKEY_ACTIONS_GESTURE19,
};
/** Array of current values for configurable touchscreen gestures */
static gchar *pwrkey_actions_gesture [POWERKEY_ACTIONS_GESTURE_COUNT] = {};
static guint pwrkey_actions_gesture_setting_id[POWERKEY_ACTIONS_GESTURE_COUNT] = {};
static void pwrkey_actions_parse (pwrkey_actions_t *self, const char *names_single, const char *names_double, const char *names_long);
static void pwrkey_actions_do_gesture (size_t gesture);
static bool pwrkey_actions_do_special (void);
static void pwrkey_actions_do_common (void);
static void pwrkey_actions_do_single_press (void);
static void pwrkey_actions_do_double_press (void);
static void pwrkey_actions_do_long_press (void);
static bool pwrkey_actions_update (const pwrkey_actions_t *self, gchar **names_single, gchar **names_double, gchar **names_long);
static bool pwrkey_actions_use_double_press(void);
static void pwrkey_actions_select (bool display_is_on);
/* ------------------------------------------------------------------------- *
* LONG_PRESS_TIMEOUT
*
* timer for telling apart short and long power key presses
* ------------------------------------------------------------------------- */
static gint pwrkey_long_press_delay = MCE_DEFAULT_POWERKEY_LONG_PRESS_DELAY;
static guint pwrkey_long_press_delay_setting_id = 0;
static guint pwrkey_long_press_timer_id = 0;
static gboolean pwrkey_long_press_timer_cb (gpointer aptr);
static void pwrkey_long_press_timer_start (void);
static bool pwrkey_long_press_timer_pending (void);
static bool pwrkey_long_press_timer_cancel (void);
/* ------------------------------------------------------------------------- *
* DOUBLE_PRESS_TIMEOUT
*
* timer for telling apart single and double power key presses
* ------------------------------------------------------------------------- */
static gint pwrkey_double_press_delay = MCE_DEFAULT_POWERKEY_DOUBLE_PRESS_DELAY;
static guint pwrkey_double_press_delay_setting_id = 0;
static guint pwrkey_double_press_timer_id = 0;
static gboolean pwrkey_double_press_timer_cb(gpointer aptr);
static bool pwrkey_double_press_timer_pending(void);
static bool pwrkey_double_press_timer_cancel(void);
static void pwrkey_double_press_timer_start(void);
/* ------------------------------------------------------------------------- *
* NGFD_GLUE
* ------------------------------------------------------------------------- */
static const char *xngf_state_repr (NgfEventState state);
static void xngf_status_cb (NgfClient *client, uint32_t event_id, NgfEventState state, void *userdata);
static bool xngf_create_client (void);
static void xngf_delete_client (void);
static void xngf_play_event (const char *event_name);
static void xngf_init (void);
static void xngf_quit (void);
/* ------------------------------------------------------------------------- *
* DBUS_ACTIONS
*
* emitting dbus signal from mce / making dbus method call to some service
* ------------------------------------------------------------------------- */
/** Flag file for: Possibly dangerous dbus action in progress
*
* Used for resetting dbus action config if it causes mce to crash.
*
* Using tmpfs is problematic from permissions point of view, but we do
* not want to cause flash wear by this either.
*/
static const char pwrkey_dbus_action_flag[] =
"/tmp/mce-powerkey-dbus-action.flag";
typedef struct
{
const char *setting_key;
const char *setting_def;
gchar *setting_val;
guint setting_id;
char *destination;
char *object;
char *interface;
char *member;
char *argument;
} pwrkey_dbus_action_t;
static pwrkey_dbus_action_t pwrkey_dbus_action[POWEKEY_DBUS_ACTION_COUNT] =
{
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION1,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION1,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION2,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION2,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION3,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION3,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION4,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION4,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION5,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION5,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION6,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION6,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION7,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION7,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION8,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION8,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION9,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION9,
.setting_val = 0,
.setting_id = 0,
},
{
.setting_key = MCE_SETTING_POWERKEY_DBUS_ACTION10,
.setting_def = MCE_DEFAULT_POWERKEY_DBUS_ACTION10,
.setting_val = 0,
.setting_id = 0,
},
};
static void pwrkey_dbus_action_clear(pwrkey_dbus_action_t *self);
static void pwrkey_dbus_action_reset(pwrkey_dbus_action_t *self);
static bool pwrkey_dbus_action_is_methodcall(const pwrkey_dbus_action_t *self);
static bool pwrkey_dbus_action_is_signal(const pwrkey_dbus_action_t *self);
static void pwrkey_dbus_action_parse(pwrkey_dbus_action_t *self);
static gchar *pwrkey_dbus_action_to_string(const pwrkey_dbus_action_t *self);
static void pwrkey_dbus_action_sanitize(pwrkey_dbus_action_t *self);
static void pwrkey_dbus_action_configure(size_t action_id, bool force_reset);
static void pwrkey_dbus_action_execute(size_t index);
/* ------------------------------------------------------------------------- *
* POWER_KEY_STATE_MACHINE
*
* main logic for tracking power key presses and associated timers
*
* state transition graph can be generated from "powerkey.dot" file
* ------------------------------------------------------------------------- */
/** Diplay state when power key was pressed */
static display_state_t pwrkey_stm_display_state = MCE_DISPLAY_UNDEF;
static alarm_ui_state_t pwrkey_stm_alarm_ui_state = MCE_ALARM_UI_OFF_INT32;
static call_state_t pwrkey_stm_call_state = CALL_STATE_NONE;
static bool pwrkey_stm_call_silenced = false;
static bool pwrkey_stm_alarm_silenced = false;
/** [setting] Power key press enable mode */
static gint pwrkey_stm_enable_mode = MCE_DEFAULT_POWERKEY_MODE;
static guint pwrkey_stm_enable_mode_setting_id = 0;
static void pwrkey_stm_long_press_timeout (void);
static void pwrkey_stm_double_press_timeout (void);
static void pwrkey_stm_powerkey_pressed (void);
static void pwrkey_stm_powerkey_released (void);
static bool pwrkey_stm_pending_timers (void);
static void pwrkey_stm_rethink_wakelock (void);
static void pwrkey_stm_store_initial_state (void);
static void pwrkey_stm_terminate (void);
/* ------------------------------------------------------------------------- *
* HOME_KEY_STATE_MACHINE
* ------------------------------------------------------------------------- */
typedef enum
{
HOMEKEY_STM_WAIT_PRESS = 0,
HOMEKEY_STM_WAIT_UNBLANK = 1,
HOMEKEY_STM_SEND_SIGNAL = 2,
HOMEKEY_STM_WAIT_RELEASE = 3,
} homekey_stm_t;
static const char *homekey_stm_repr (homekey_stm_t state);
static void homekey_stm_set_state (homekey_stm_t state);
static bool homekey_stm_exec_step (void);
static void homekey_stm_eval_state (void);
static void homekey_stm_set_pressed (bool pressed);
/* ------------------------------------------------------------------------- *
* DBUS_IPC
*
* handling incoming and outgoing dbus messages
* ------------------------------------------------------------------------- */
static void pwrkey_dbus_send_signal(const char *sig, const char *arg);
static gboolean pwrkey_dbus_trigger_event_cb(DBusMessage *const req);
static gboolean pwrkey_dbus_ignore_incoming_call_cb(DBusMessage *const req);
static void pwrkey_dbus_init(void);
static void pwrkey_dbus_quit(void);
/* ------------------------------------------------------------------------- *
* DYNAMIC_SETTINGS
*
* tracking powerkey related runtime changeable settings
* ------------------------------------------------------------------------- */
static gint pwrkey_setting_sanitize_id = 0;
static void pwrkey_setting_sanitize_action_masks(void);
static void pwrkey_setting_sanitize_dbus_actions(void);
static gboolean pwrkey_setting_sanitize_cb (gpointer aptr);
static void pwrkey_setting_sanitize_now (void);
static void pwrkey_setting_sanitize_later (void);
static void pwrkey_setting_sanitize_cancel (void);
static bool pwrkey_setting_handle_gesture (const GConfValue *gcv, guint id);
static void pwrkey_setting_cb (GConfClient *const gcc, const guint id, GConfEntry *const entry, gpointer const data);
static void pwrkey_setting_init (void);
static void pwrkey_setting_quit (void);
/* ------------------------------------------------------------------------- *
* DATAPIPE_HANDLING
*
* reacting to state changes / input from other mce modules
* ------------------------------------------------------------------------- */
static void pwrkey_datapipe_keypress_event_cb(gconstpointer const data);
static void pwrkey_datapipe_ngfd_service_state_cb(gconstpointer data);
static void pwrkey_datapipe_system_state_cb(gconstpointer data);
static void pwrkey_datapipe_devicelock_state_cb(gconstpointer data);
static void pwrkey_datapipe_display_state_curr_cb(gconstpointer data);
static void pwrkey_datapipe_display_state_next_cb(gconstpointer data);
static void pwrkey_datapipe_lid_sensor_filtered_cb(gconstpointer data);
static void pwrkey_datapipe_proximity_sensor_actual_cb(gconstpointer data);
static void pwrkey_datapipe_call_state_cb(gconstpointer data);
static void pwrkey_datapipe_alarm_ui_state_cb(gconstpointer data);
static void pwrkey_datapipe_devicelock_service_state_cb(gconstpointer data);
static void pwrkey_datapipe_enroll_in_progress_cb(gconstpointer data);
static void pwrkey_datapipe_ngfd_event_request_cb(gconstpointer data);
static void pwrkey_datapipe_init(void);
static void pwrkey_datapipe_quit(void);
/* ------------------------------------------------------------------------- *
* PWRKEY_QUEUED
* ------------------------------------------------------------------------- */
typedef struct pwrkey_queued_t
{
pwrkey_unblank_predicate_t predicate;
uint32_t actions;
} pwrkey_queued_t;
static pwrkey_queued_t *pwrkey_queued_create (pwrkey_unblank_predicate_t predicate, uint32_t actions);
static void pwrkey_queued_delete (pwrkey_queued_t *self);
static void pwrkey_queued_delete_cb (gpointer self);
/* ------------------------------------------------------------------------- *
* PWRKEY_QUEUE
* ------------------------------------------------------------------------- */
static void pwrkey_queue_init (void);
static void pwrkey_queue_quit (void);
static void pwrkey_queue_add_actions (pwrkey_unblank_predicate_t predicate, uint32_t mask);
static void pwrkey_queue_execute (void);
static pwrkey_queued_t *pwrkey_queue_current_action (void);
static void pwrkey_queue_finish_action (void);
static bool pwrkey_queue_blank_delay_needed (void);
static void pwrkey_queue_start_blank_delay (guint delay);
static void pwrkey_queue_stop_blank_delay (void);
static gboolean pwrkey_queue_blank_delay_cb (gpointer aptr);
static void pwrkey_queue_start_action_delay (guint delay);
static void pwrkey_queue_stop_action_delay (void);
static gboolean pwrkey_queue_action_delay_cb (gpointer aptr);
static bool pwrkey_queue_unblank_delay_needed(void);
static void pwrkey_queue_start_unblank_delay (guint delay);
static void pwrkey_queue_stop_unblank_delay (void);
static gboolean pwrkey_queue_unblank_delay_cb (gpointer aptr);
static void pwrkey_queue_start_delay (uint32_t actions);
static void pwrkey_queue_stop_all_delays (void);
static bool pwrkey_queue_is_delayed (void);
static bool pwrkey_queue_is_busy (void);
static void pwrkey_queue_rethink_delays (void);
static void pwrkey_queue_rethink_psensor (void);
/* ------------------------------------------------------------------------- *
* MODULE_INTEFACE
* ------------------------------------------------------------------------- */
gboolean mce_powerkey_init(void);
void mce_powerkey_exit(void);
/* ========================================================================= *
* MISC_UTIL
* ========================================================================= */
/** Parse element from comma separated string list
*/
static char *
pwrkey_get_token(char **ppos)
{
char *pos = *ppos;
char *beg = pos;
if( !pos )
goto cleanup;
for( ; *pos; ++pos ) {
if( *pos != ',' )
continue;
*pos++ = 0;
break;
}
cleanup:
return *ppos = pos, beg;
}
/** Create an empty flag file
*
* @param path Path to the file to create
*
* @return true if file was created, false otherwise
*/
static bool pwrkey_create_flagfile(const char *path)
{
bool created = false;
int fd = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
if( fd != -1 ) {
close(fd);
created = true;
}
return created;
}
/** Delete a flag file
*
* @param path Path to the file to create
*
* @return true if file was removed, false otherwise
*/
static bool pwrkey_delete_flagfile(const char *path)
{
bool deleted = false;
if( unlink(path) == 0 ) {
deleted = true;
}
return deleted;
}
/** Get event time in millisecond accuracy
*
* @param ev input event pointer
*
* @return event time in millisecond accuracy
*/
static int64_t pwrkey_event_tick(const struct input_event *ev)
{
int64_t ms = 0;
if( ev ) {
ms += ev->input_event_sec;
ms *= 1000;
ms += ev->input_event_usec / 1000;
}
return ms;
}
/* ========================================================================= *
* DATAPIPE_HANDLING
* ========================================================================= */
/** System state; is undefined at bootup, can't assume anything */
static system_state_t system_state = MCE_SYSTEM_STATE_UNDEF;
/** Cached devicelock_state ; assume unknown */
static devicelock_state_t devicelock_state = DEVICELOCK_STATE_UNDEFINED;
/** Current display state; undefined initially, can't assume anything */
static display_state_t display_state_curr = MCE_DISPLAY_UNDEF;
/** Next Display state; undefined initially, can't assume anything */
static display_state_t display_state_next = MCE_DISPLAY_UNDEF;
/** Lid cover policy state; assume unknown */
static cover_state_t lid_sensor_filtered = COVER_UNDEF;
/** Actual proximity state; assume not covered */
static cover_state_t proximity_sensor_actual = COVER_UNDEF;
/** NGFD availability */
static service_state_t ngfd_service_state = SERVICE_STATE_UNDEF;
/** Cached alarm ui state */
static alarm_ui_state_t alarm_ui_state = MCE_ALARM_UI_OFF_INT32;
/** Cached call state */
static call_state_t call_state = CALL_STATE_NONE;
/** devicelock dbus name is reserved; assume unknown */
static service_state_t devicelock_service_state = SERVICE_STATE_UNDEF;
/** Cached ongoing fingerprint enroll; assume not */
static bool enroll_in_progress = false;
/** Use powerkey for blanking during incoming calls */
static bool pwrkey_ignore_incoming_call = false;
/* ========================================================================= *
* PS_OVERRIDE
* ========================================================================= */
/** Provide an emergency way out from stuck proximity sensor
*
* If the proximity sensor is dirty/faulty and stuck to "covered"
* state, it can leave the phone in a state where it is impossible
* to do anything about incoming call, ringing alarm.
*
* To offer somekind of remedy for the situation, this function
* allows user to force proximity sensor to "uncovered" state
* by rapidly pressing power button several times.
*/
static void
pwrkey_ps_override_evaluate(void)
{
static int64_t t_last = 0;
static gint count = 0;
/* If the feature is disabled, just reset the counter */
if( pwrkey_ps_override_count <= 0 ||
pwrkey_ps_override_timeout <= 0 ) {
t_last = 0, count = 0;
goto EXIT;
}
/* If neither sensor is not covered, just reset the counter */
if( proximity_sensor_actual == COVER_OPEN &&
lid_sensor_filtered != COVER_CLOSED ) {
t_last = 0, count = 0;
goto EXIT;
}
int64_t t_now = mce_lib_get_boot_tick();
/* If the previous power key press was too far in
* the past, start counting from zero again */
if( t_now > t_last + pwrkey_ps_override_timeout ) {
mce_log(LL_DEBUG, "ps override count restarted");
count = 0;
}
t_last = t_now;
/* If configured number of power key presses within the time
* limits has been reached, force proximity sensor state to
* "uncovered".
*
* This should allow touch input ungrabbing and turning
* display on during incoming call / alarm.
*
* If sensor gets unstuck and new proximity readings are
* received, this override will be automatically undone.
*/
if( ++count != pwrkey_ps_override_count ) {
mce_log(LL_DEBUG, "ps override count = %d", count);
goto EXIT;
}
if( proximity_sensor_actual == COVER_CLOSED ) {
mce_log(LL_CRIT, "assuming stuck proximity sensor;"
" faking uncover event");
/* Force cached proximity state to "open" */
datapipe_exec_full(&proximity_sensor_actual_pipe,
GINT_TO_POINTER(COVER_OPEN));
}
if( lid_sensor_filtered == COVER_CLOSED ) {
mce_log(LL_CRIT, "assuming stuck lid sensor;"
" resetting validation data");
/* Reset lid sensor validation data */
datapipe_exec_full(&lid_sensor_is_working_pipe,
GINT_TO_POINTER(false));
}
t_last = 0, count = 0;
EXIT:
return;
}
/* ========================================================================= *
* ACTION_EXEC
* ========================================================================= */
static void
pwrkey_action_vibrate(void)
{
mce_log(LL_DEBUG, "Requesting vibrate");
xngf_play_event("pwrkey");
}
static void
pwrkey_action_shutdown(void)
{
submode_t submode = mce_get_submode_int32();
/* Do not shutdown if the tklock is active */
if( submode & MCE_SUBMODE_TKLOCK )
goto EXIT;
mce_log(LL_DEVEL, "Requesting shutdown");
mce_dsme_request_normal_shutdown();
EXIT:
return;
}
static void
pwrkey_action_tklock(void)
{
tklock_request_t request = TKLOCK_REQUEST_ON;
mce_datapipe_request_tklock(request);
}
static void
pwrkey_action_tkunlock(void)
{
/* Only unlock if we are in/entering fully powered on display state */
switch( display_state_next ) {
case MCE_DISPLAY_ON:
case MCE_DISPLAY_DIM:
break;
default:
goto EXIT;
}
tklock_request_t request = TKLOCK_REQUEST_OFF;
/* Even if powerkey actions are allowed to work while proximity
* sensor is covered, we must not deactivatie the lockscreen */
if( proximity_sensor_actual != COVER_OPEN ) {
mce_log(LL_DEBUG, "Proximity sensor %s; rejecting tklock=%s",
proximity_state_repr(proximity_sensor_actual),
tklock_request_repr(request));
goto EXIT;
}
mce_datapipe_request_tklock(request);
EXIT:
return;
}
static void
pwrkey_action_tkunlock2(void)
{
if( devicelock_state != DEVICELOCK_STATE_UNLOCKED ) {
mce_log(LL_DEBUG, "devicelock_state=%s; rejecting 'tkunlock2' action",
devicelock_state_repr(devicelock_state));
}
else {
pwrkey_action_tkunlock();
}
}
static void
pwrkey_action_blank(void)
{
display_state_t request = MCE_DISPLAY_OFF;
switch( pwrkey_action_blank_mode ) {
case PWRKEY_BLANK_TO_LPM:
request = MCE_DISPLAY_LPM_ON;
break;
case PWRKEY_BLANK_TO_OFF:
default:
break;
}
/* Note: logged from datapipe functions */
mce_datapipe_request_display_state(request);
}
static void
pwrkey_action_unblank(void)
{
display_state_t request = MCE_DISPLAY_ON;
mce_log(LL_DEBUG, "Requesting display=%s",
display_state_repr(request));
mce_tklock_unblank(request);
}
static void
pwrkey_action_unblanked(void)
{
/* Placeholder dummy action.
*
* Used for preserving unblank predicate when
* action set including unblank is executed
* in multiple parts.
*/
}
static void
pwrkey_action_devlock(void)
{
static const char service[] = DEVICELOCK_SERVICE;
static const char object[] = DEVICELOCK_REQUEST_PATH;
static const char interface[] = DEVICELOCK_REQUEST_IF;
static const char method[] = "setState";
dbus_int32_t request = DEVICELOCK_STATE_LOCKED;
if( devicelock_service_state != SERVICE_STATE_RUNNING ) {
mce_log(LL_WARN, "devicelock service state is %s; skip %s request",
service_state_repr(devicelock_service_state),