-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtklock.c
7434 lines (6120 loc) · 224 KB
/
tklock.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 tklock.c
* This file implements the touchscreen/keypad lock component
* of the Mode Control Entity
* <p>
* Copyright (c) 2004 - 2011 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (c) 2012 - 2019 Jolla Ltd.
* Copyright (c) 2025 Jollyboys Ltd.
* <p>
* @author David Weinehall <[email protected]>
* @author Tapio Rantala <[email protected]>
* @author Santtu Lakkala <[email protected]>
* @author Jukka Turunen <[email protected]>
* @author Irina Bezruk <[email protected]>
* @author Kalle Jokiniemi <[email protected]>
* @author Mika Laitio <[email protected]>
* @author Markus Lehtonen <[email protected]>
* @author Simo Piiroinen <[email protected]>
* @author Vesa Halttunen <[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 "tklock.h"
#include "mce-common.h"
#include "mce-log.h"
#include "mce-lib.h"
#include "mce-io.h"
#include "mce-setting.h"
#include "mce-dbus.h"
#include "mce-hbtimer.h"
#include "evdev.h"
#ifdef ENABLE_WAKELOCKS
# include "libwakelock.h"
#endif
#include "modules/doubletap.h"
#include "modules/display.h"
#include "systemui/dbus-names.h"
#include "systemui/tklock-dbus-names.h"
#include <linux/input.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include <glib/gstdio.h>
typedef enum
{
/** No autorelock triggers */
AUTORELOCK_NO_TRIGGERS,
/** Autorelock on keyboard slide closed */
AUTORELOCK_KBD_SLIDE,
/** Autorelock on lens cover */
AUTORELOCK_LENS_COVER,
} autorelock_t;
/** Helper for evaluation number of items in an array */
#define numof(a) (sizeof(a)/sizeof*(a))
/* ========================================================================= *
* CONSTANTS
* ========================================================================= */
#define MODULE_NAME "tklock"
/** Max valid time_t value in milliseconds */
#define MAX_TICK (INT_MAX * (int64_t)1000)
/** Min valid time_t value in milliseconds */
#define MIN_TICK 0
/** Maximum number of concurrent notification ui exceptions */
#define TKLOCK_NOTIF_SLOTS 32
/** How long to wait for lid close after low lux [ms] */
#define TKLOCK_LIDFILTER_SET_WAIT_FOR_CLOSE_DELAY 1500
/** How long to wait for low lux after lid close [ms] */
#define TKLOCK_LIDFILTER_SET_WAIT_FOR_DARK_DELAY 1200
/** How long to wait for high lux after lid open [ms] */
#define TKLOCK_LIDFILTER_SET_WAIT_FOR_LIGHT_DELAY 1200
/* ========================================================================= *
* DATATYPES
* ========================================================================= */
typedef struct
{
/** BOOTTIME tick when notification autostops */
int64_t ns_until;
/** Amount of ms autostop extends from user input */
int64_t ns_renew;
/** Private D-Bus name of the slot owner */
gchar *ns_owner;
/** Assumed unique identification string */
gchar *ns_name;
} tklock_notif_slot_t;
typedef struct
{
/** Array of notification slots */
tklock_notif_slot_t tn_slot[TKLOCK_NOTIF_SLOTS];
/** BOOTTIME linger tick from deactivated slots */
int64_t tn_linger;
/** Timer id for autostopping notification slots */
guint tn_autostop_id;
/** Slot owner D-Bus name monitoring list */
GSList *tn_monitor_list;
} tklock_notif_state_t;
/** Proximity sensor history */
typedef struct
{
/** Monotonic timestamp, ms resolution */
int64_t tick;
/** Proximity sensor state */
cover_state_t state;
} ps_history_t;
/** Ambient light lux value mapped into enumerated states
*
* In case the lid sensor can't be trusted for some reason, data from
* ambient light sensor heuristics can be used for avoiding incorrect
* blank/unblank actions.
*
* For this purpose the raw data from ambient light sensor is tracked
* and mapped in to three states:
*
* - TKLOCK_LIDLIGHT_NA: The data from als is not applicable for filtering.
* - TKLOCK_LIDLIGHT_LO: The als indicates darkness.
* - TKLOCK_LIDLIGHT_HI: The als indicates some amount of light.
*/
typedef enum
{
/* Light level is not applicable for state evaluation */
TKLOCK_LIDLIGHT_NA,
/* Light level equals complete darkness */
TKLOCK_LIDLIGHT_LO,
/* Light level equals at least some light */
TKLOCK_LIDLIGHT_HI,
} tklock_lidlight_t;
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
// datapipe values and triggers
static void tklock_datapipe_system_state_cb(gconstpointer data);
static void tklock_datapipe_devicelock_state_cb(gconstpointer data);
static void tklock_datapipe_devicelock_state_cb2(gpointer aptr);
static void tklock_datapipe_resume_detected_event_cb(gconstpointer data);
static void tklock_datapipe_devicelock_service_state_cb(gconstpointer data);
static void tklock_datapipe_lipstick_service_state_cb(gconstpointer data);
static void tklock_datapipe_osupdate_running_cb(gconstpointer data);
static void tklock_datapipe_shutting_down_cb(gconstpointer data);
static void tklock_datapipe_display_state_curr_cb(gconstpointer data);
static void tklock_datapipe_display_state_next_cb(gconstpointer data);
static void tklock_datapipe_proximity_eval_led(void);
static void tklock_datapipe_proximity_update(void);
static gboolean tklock_datapipe_proximity_uncover_cb(gpointer data);
static void tklock_datapipe_proximity_uncover_cancel(void);
static void tklock_datapipe_proximity_uncover_schedule(void);
static void tklock_datapipe_proximity_sensor_actual_cb(gconstpointer data);
static void tklock_datapipe_call_state_cb(gconstpointer data);
static void tklock_datapipe_music_playback_ongoing_cb(gconstpointer data);
static void tklock_datapipe_alarm_ui_state_cb(gconstpointer data);
static void tklock_datapipe_charger_state_cb(gconstpointer data);
static void tklock_datapipe_battery_status_cb(gconstpointer data);
static void tklock_datapipe_usb_cable_state_cb(gconstpointer data);
static void tklock_datapipe_jack_sense_state_cb(gconstpointer data);
static void tklock_datapipe_camera_button_state_cb(gconstpointer const data);
static void tklock_datapipe_keypress_event_cb(gconstpointer const data);
static void tklock_datapipe_uiexception_type_cb(gconstpointer data);
static void tklock_datapipe_audio_route_cb(gconstpointer data);
static void tklock_datapipe_tklock_request_cb(gconstpointer data);
static void tklock_datapipe_interaction_expected_cb(gconstpointer data);
static gpointer tklock_datapipe_submode_filter_cb(gpointer data);
static void tklock_datapipe_submode_cb(gconstpointer data);
static void tklock_datapipe_lockkey_state_cb(gconstpointer const data);
static void tklock_datapipe_heartbeat_event_cb(gconstpointer data);
static void tklock_datapipe_keyboard_slide_input_state_cb(gconstpointer const data);
static void tklock_datapipe_keyboard_slide_output_state_cb(gconstpointer const data);
static void tklock_datapipe_keyboard_available_state_cb(gconstpointer const data);
static void tklock_datapipe_mouse_available_state_cb(gconstpointer const data);
static void tklock_datapipe_light_sensor_poll_request_cb(gconstpointer const data);
static void tklock_datapipe_topmost_window_pid_cb(gconstpointer data);
static void tklock_datapipe_light_sensor_actual_cb(gconstpointer data);
static void tklock_datapipe_lid_sensor_is_working_cb(gconstpointer data);
static void tklock_datapipe_lid_sensor_actual_cb(gconstpointer data);
static void tklock_datapipe_lid_sensor_filtered_cb(gconstpointer data);
static void tklock_datapipe_lens_cover_state_cb(gconstpointer data);
static bool tklock_touch_activity_event_p(const struct input_event *ev);
static void tklock_datapipe_user_activity_event_cb(gconstpointer data);
static void tklock_datapipe_init_done_cb(gconstpointer data);
static bool tklock_datapipe_in_tklock_submode(void);
static void tklock_datapipe_set_tklock_submode(bool lock);
static void tklock_datapipe_set_devicelock_state(devicelock_state_t state);
static void tklock_datapipe_rethink_interaction_expected(void);
static void tklock_datapipe_update_interaction_expected(bool expected);
static void tklock_datapipe_init(void);
static void tklock_datapipe_quit(void);
// LID_SENSOR
static bool tklock_lidsensor_is_enabled (void);
static void tklock_lidsensor_init (void);
// LID_LIGHT
static const char *tklock_lidlight_repr (tklock_lidlight_t state);
static tklock_lidlight_t tklock_lidlight_from_lux (int lux);
// LID_FILTER
static bool tklock_lidfilter_is_enabled (void);
static void tklock_lidfilter_set_allow_close (bool allow);
static tklock_lidlight_t tklock_lidfilter_map_als_state (void);
static void tklock_lidfilter_set_als_state (tklock_lidlight_t state);
static gboolean tklock_lidfilter_wait_for_close_cb (gpointer aptr);
static bool tklock_lidfilter_get_wait_for_close (void);
static void tklock_lidfilter_set_wait_for_close (bool state);
static gboolean tklock_lidfilter_wait_for_dark_cb (gpointer aptr);
static bool tklock_lidfilter_get_wait_for_dark (void);
static void tklock_lidfilter_set_wait_for_dark (bool state);
static gboolean tklock_lidfilter_wait_for_light_cb (gpointer aptr);
static bool tklock_lidfilter_get_wait_for_light (void);
static void tklock_lidfilter_set_wait_for_light (bool state);
static void tklock_lidfilter_rethink_als_poll (void);
static void tklock_lidfilter_rethink_allow_close (void);
static void tklock_lidfilter_rethink_als_state (void);
static void tklock_lidfilter_rethink_lid_state (void);
// LID_POLICY
static void tklock_lidpolicy_rethink (void);
// keyboard slide state machine
static void tklock_keyboard_slide_opened(void);
static void tklock_keyboard_slide_opened_cb(gpointer aptr);
static void tklock_keyboard_slide_closed(void);
static void tklock_keyboard_slide_rethink(void);
// autolock state machine
static gboolean tklock_autolock_cb(gpointer aptr);
static void tklock_autolock_evaluate(void);
static void tklock_autolock_enable(void);
static void tklock_autolock_disable(void);
static void tklock_autolock_rethink(void);
static void tklock_autolock_init(void);
static void tklock_autolock_quit(void);
// proximity locking state machine
static gboolean tklock_proxlock_cb(gpointer aptr);
static void tklock_proxlock_resume(void);
static void tklock_proxlock_evaluate(void);
static void tklock_proxlock_enable(void);
static void tklock_proxlock_disable(void);
static void tklock_proxlock_rethink(void);
// autolock based on device lock changes
static void tklock_autolock_on_devlock_block(int duration_ms);
static void tklock_autolock_on_devlock_prime(void);
static void tklock_autolock_on_devlock_trigger(void);
// ui exception handling state machine
static uiexception_type_t topmost_active(uiexception_type_t mask);
static void tklock_uiexception_sync_to_datapipe(void);
static gboolean tklock_uiexception_linger_cb(gpointer aptr);
static void tklock_uiexception_begin(uiexception_type_t type, int64_t linger);
static void tklock_uiexception_end(uiexception_type_t type, int64_t linger);
static void tklock_uiexception_cancel(void);
static void tklock_uiexception_finish(void);
static bool tklock_uiexception_deny_state_restore(bool force, const char *cause);
static void tklock_uiexception_rethink(void);
// low power mode ui state machine
static void tklock_lpmui_set_state(bool enable);
static void tklock_lpmui_reset_history(void);
static void tklock_lpmui_update_history(cover_state_t state);
static bool tklock_lpmui_probe_from_pocket(void);
static bool tklock_lpmui_probe_on_table(void);
static void tklock_lpmui_rethink(void);
static void tklock_lpmui_pre_transition_actions(void);
// legacy hw event input enable/disable state machine
static void tklock_evctrl_set_state(output_state_t *output, bool enable);
static void tklock_evctrl_set_kp_state(bool enable);
static void tklock_evctrl_set_ts_state(bool enable);
static void tklock_evctrl_set_dt_state(bool enable);
static void tklock_evctrl_rethink(void);
// legacy hw double tap calibration
static void tklock_dtcalib_now(void);
static void tklock_dtcalib_from_heartbeat(void);
static gboolean tklock_dtcalib_cb(gpointer data);
static void tklock_dtcalib_start(void);
static void tklock_dtcalib_stop(void);
// DYNAMIC_SETTINGS
static void tklock_setting_sanitize_lid_open_actions(void);
static void tklock_setting_sanitize_lid_close_actions(void);
static void tklock_setting_sanitize_kbd_open_trigger(void);
static void tklock_setting_sanitize_kbd_open_actions(void);
static void tklock_setting_sanitize_kbd_close_trigger(void);
static void tklock_setting_sanitize_kbd_close_actions(void);
static void tklock_setting_cb(GConfClient *const gcc, const guint id, GConfEntry *const entry, gpointer const data);
static void tklock_setting_init(void);
static void tklock_setting_quit(void);
// sysfs probing
static void tklock_sysfs_probe(void);
// dbus ipc with systemui
static void tklock_ui_send_tklock_signal(void);
static void tklock_ui_notify_rethink_wakelock(void);
static bool tklock_ui_notify_must_be_delayed(void);
static gboolean tklock_ui_notify_end_cb(gpointer data);
static gboolean tklock_ui_notify_beg_cb(gpointer data);
static void tklock_ui_notify_schdule(void);
static gboolean tklock_ui_sync_cb(gpointer aptr);
static void tklock_ui_notify_cancel(void);
static void tklock_ui_eat_event(void);
static void tklock_ui_open(void);
static void tklock_ui_close(void);
static bool tklock_ui_is_enabled(void);
static void tklock_ui_set_enabled(bool enable);
static void tklock_ui_get_devicelock_cb(DBusPendingCall *pc, void *aptr);
static void tklock_ui_get_devicelock(void);
static void tklock_ui_send_lpm_signal(void);
static void tklock_ui_enable_lpm(void);
static void tklock_ui_disable_lpm(void);
static void tklock_ui_show_device_unlock(void);;
// dbus ipc
static void tklock_dbus_send_display_blanking_policy(DBusMessage *const req);
static gboolean tklock_dbus_display_blanking_policy_get_cb(DBusMessage *const msg);
static void tklock_dbus_send_keyboard_slide_state(DBusMessage *const req);
static gboolean tklock_dbus_keyboard_slide_state_get_req_cb(DBusMessage *const msg);
static void tklock_dbus_send_keyboard_available_state(DBusMessage *const req);
static gboolean tklock_dbus_keyboard_available_state_get_req_cb(DBusMessage *const msg);
static void tklock_dbus_send_mouse_available_state(DBusMessage *const req);
static gboolean tklock_dbus_mouse_available_state_get_req_cb(DBusMessage *const msg);
static gboolean tklock_dbus_send_tklock_mode(DBusMessage *const method_call);
static gboolean tklock_dbus_mode_get_req_cb(DBusMessage *const msg);
static tklock_request_t tklock_dbus_sanitize_requested_mode(tklock_request_t state);
static gboolean tklock_dbus_mode_change_req_cb(DBusMessage *const msg);
static gboolean tklock_dbus_interaction_expected_cb(DBusMessage *const msg);
static gboolean tklock_dbus_systemui_callback_cb(DBusMessage *const msg);
static gboolean tklock_dbus_devicelock_changed_cb(DBusMessage *const msg);
static gboolean tklock_dbus_notification_beg_cb(DBusMessage *const msg);
static gboolean tklock_dbus_notification_end_cb(DBusMessage *const msg);
static void mce_tklock_init_dbus(void);
static void mce_tklock_quit_dbus(void);
// NOTIFICATION_SLOTS
static void tklock_notif_slot_init(tklock_notif_slot_t *self);
static void tklock_notif_slot_free(tklock_notif_slot_t *self);
static void tklock_notif_slot_set(tklock_notif_slot_t *self, const char *owner, const char *name, int64_t until, int64_t renew);
static bool tklock_notif_slot_is_free(const tklock_notif_slot_t *self);
static bool tklock_notif_slot_has_name(const tklock_notif_slot_t *self, const char *name);
static bool tklock_notif_slot_validate(tklock_notif_slot_t *self, int64_t now);
static bool tklock_notif_slot_renew(tklock_notif_slot_t *self, int64_t now);
static bool tklock_notif_slot_has_owner(const tklock_notif_slot_t *self, const char *owner);
static gchar *tklock_notif_slot_steal_owner(tklock_notif_slot_t *self);
// NOTIFICATION_API
static void tklock_notif_init(void);
static void tklock_notif_quit(void);
static gboolean tklock_notif_autostop_cb(gpointer aptr);
static void tklock_notif_cancel_autostop(void);
static void tklock_notif_schedule_autostop(gint delay);
static void tklock_notif_update_state(void);
static void tklock_notif_extend_by_renew(void);
static void tklock_notif_vacate_slot(const char *owner, const char *name, int64_t linger);
static void tklock_notif_reserve_slot(const char *owner, const char *name, int64_t length, int64_t renew);
static void tklock_notif_vacate_slots_from(const char *owner);
static size_t tklock_notif_count_slots_from(const char *owner);
static gboolean tklock_notif_owner_dropped_cb(DBusMessage *const msg);
static void tklock_notif_add_owner_monitor(const char *owner);
static void tklock_notif_remove_owner_monitor(const char *owner);
static void mce_tklock_begin_notification(const char *owner, const char *name, int64_t length, int64_t renew);
static void mce_tklock_end_notification(const char *owner, const char *name, int64_t linger);
// "module" load/unload
extern gboolean mce_tklock_init(void);
extern void mce_tklock_exit(void);
extern void mce_tklock_unblank(display_state_t to_state);
/* ========================================================================= *
* DYNAMIC_SETTINGS
* ========================================================================= */
/** Flag: Devicelock is handled in lockscreen */
static gboolean tklock_devicelock_in_lockscreen = MCE_DEFAULT_TK_DEVICELOCK_IN_LOCKSCREEN;
static guint tklock_devicelock_in_lockscreen_setting_id = 0;
/** Flag: Convert denied tklock removal attempt to: show device unlock view */
static bool tklock_devicelock_want_to_unlock = false;
/** Flag: Automatically lock (after ON->DIM->OFF cycle) */
static gboolean tk_autolock_enabled = MCE_DEFAULT_TK_AUTOLOCK_ENABLED;
static guint tk_autolock_enabled_setting_id = 0;
/** Flag: Grabbing input devices is allowed */
static gboolean tk_input_policy_enabled = MCE_DEFAULT_TK_INPUT_POLICY_ENABLED;
static guint tk_input_policy_enabled_setting_id = 0;
/** Delay for automatick locking (after ON->DIM->OFF cycle) */
static gint tklock_autolock_delay = MCE_DEFAULT_TK_AUTOLOCK_DELAY;
static guint tklock_autolock_delay_setting_id = 0;
/** Flag: Proximity sensor can block touch input */
static gboolean proximity_blocks_touch = MCE_DEFAULT_TK_PROXIMITY_BLOCKS_TOUCH;
static guint proximity_blocks_touch_setting_id = 0;
/** Volume key input policy */
static gint volkey_policy = MCE_DEFAULT_TK_VOLKEY_POLICY;
static guint volkey_policy_setting_id = 0;
/** Touchscreen gesture (doubletap etc) enable mode */
static gint touchscreen_gesture_enable_mode = MCE_DEFAULT_DOUBLETAP_MODE;
static guint touchscreen_gesture_enable_mode_setting_id = 0;
/** Lid sensor open actions */
static gint tklock_lid_open_actions = MCE_DEFAULT_TK_LID_OPEN_ACTIONS;
static guint tklock_lid_open_actions_setting_id = 0;
/** Lid sensor close actions */
static gint tklock_lid_close_actions = MCE_DEFAULT_TK_LID_CLOSE_ACTIONS;
static guint tklock_lid_close_actions_setting_id = 0;
/** Flag: Is the lid sensor used for display blanking */
static gboolean lid_sensor_enabled = MCE_DEFAULT_TK_LID_SENSOR_ENABLED;
static guint lid_sensor_enabled_setting_id = 0;
/** When to react to keyboard open */
static gint tklock_kbd_open_trigger = MCE_DEFAULT_TK_KBD_OPEN_TRIGGER;
static guint tklock_kbd_open_trigger_setting_id = 0;
/** How to react to keyboard open */
static gint tklock_kbd_open_actions = MCE_DEFAULT_TK_KBD_OPEN_ACTIONS;
static guint tklock_kbd_open_actions_setting_id = 0;
/** When to react to keyboard close */
static gint tklock_kbd_close_trigger = MCE_DEFAULT_TK_KBD_CLOSE_TRIGGER;
static guint tklock_kbd_close_trigger_setting_id = 0;
/** How to react to keyboard close */
static gint tklock_kbd_close_actions = MCE_DEFAULT_TK_KBD_CLOSE_ACTIONS;
static guint tklock_kbd_close_actions_setting_id = 0;
/** Flag for: Using ALS is allowed */
static gboolean als_enabled = MCE_DEFAULT_DISPLAY_ALS_ENABLED;
static guint als_enabled_setting_id = 0;
/** Flag: Use ALS for lid close filtering */
static gboolean filter_lid_with_als = MCE_DEFAULT_TK_FILTER_LID_WITH_ALS;
static guint filter_lid_with_als_setting_id = 0;
/** Maximum amount of light ALS should report when LID is closed */
static gint filter_lid_als_limit = MCE_DEFAULT_TK_FILTER_LID_ALS_LIMIT;
static guint filter_lid_als_limit_setting_id = 0;
/** How long to keep display on after incoming call ends [ms] */
static gint exception_length_call_in = MCE_DEFAULT_TK_EXCEPT_LEN_CALL_IN;
static guint exception_length_call_in_setting_id = 0;
/** How long to keep display on after outgoing call ends [ms] */
static gint exception_length_call_out = MCE_DEFAULT_TK_EXCEPT_LEN_CALL_OUT;
static guint exception_length_call_out_setting_id = 0;
/** How long to keep display on after alarm is handled [ms] */
static gint exception_length_alarm = MCE_DEFAULT_TK_EXCEPT_LEN_ALARM;
static guint exception_length_alarm_setting_id = 0;
/** How long to keep display on when usb cable is connected [ms] */
static gint exception_length_usb_connect = MCE_DEFAULT_TK_EXCEPT_LEN_USB_CONNECT;
static guint exception_length_usb_connect_setting_id = 0;
/** How long to keep display on when usb mode dialog is shown [ms] */
static gint exception_length_usb_dialog = MCE_DEFAULT_TK_EXCEPT_LEN_USB_DIALOG;
static guint exception_length_usb_dialog_setting_id = 0;
/** How long to keep display on when charging starts [ms] */
static gint exception_length_charger = MCE_DEFAULT_TK_EXCEPT_LEN_CHARGER;
static guint exception_length_charger_setting_id = 0;
/** How long to keep display on after battery full [ms] */
static gint exception_length_battery = MCE_DEFAULT_TK_EXCEPT_LEN_BATTERY;
static guint exception_length_battery_setting_id = 0;
/** How long to keep display on when audio jack is inserted [ms] */
static gint exception_length_jack_in = MCE_DEFAULT_TK_EXCEPT_LEN_JACK_IN;
static guint exception_length_jack_in_setting_id = 0;
/** How long to keep display on when audio jack is removed [ms] */
static gint exception_length_jack_out = MCE_DEFAULT_TK_EXCEPT_LEN_JACK_OUT;
static guint exception_length_jack_out_setting_id = 0;
/** How long to keep display on when camera button is pressed [ms] */
static gint exception_length_camera = MCE_DEFAULT_TK_EXCEPT_LEN_CAMERA;
static guint exception_length_camera_setting_id = 0;
/** How long to keep display on when volume button is pressed [ms] */
static gint exception_length_volume = MCE_DEFAULT_TK_EXCEPT_LEN_VOLUME;
static guint exception_length_volume_setting_id = 0;
/** How long to extend display on when there is user activity [ms] */
static gint exception_length_activity = MCE_DEFAULT_TK_EXCEPT_LEN_ACTIVITY;
static guint exception_length_activity_setting_id = 0;
/** Flag for: Allow lockscreen animation during unblanking */
static gboolean lockscreen_anim_enabled = MCE_DEFAULT_TK_LOCKSCREEN_ANIM_ENABLED;
static guint lockscreen_anim_enabled_setting_id = 0;
/** Default delay for delaying proximity uncovered handling [ms] */
static gint tklock_proximity_delay_default = MCE_DEFAULT_TK_PROXIMITY_DELAY_DEFAULT;
static guint tklock_proximity_delay_default_setting_id = 0;
/** Delay for delaying proximity uncovered handling during calls [ms] */
static gint tklock_proximity_delay_incall = MCE_DEFAULT_TK_PROXIMITY_DELAY_INCALL;
static guint tklock_proximity_delay_incall_setting_id = 0;
/* ========================================================================= *
* probed control file paths
* ========================================================================= */
/** SysFS path to touchscreen event disable */
static output_state_t mce_touchscreen_sysfs_disable_output =
{
.context = "touchscreen_disable",
.truncate_file = TRUE,
.close_on_exit = TRUE,
};
/** SysFS path to touchscreen double-tap gesture control */
static const gchar *mce_touchscreen_gesture_enable_path = NULL;
/** SysFS path to touchscreen recalibration control */
static const gchar *mce_touchscreen_calibration_control_path = NULL;
/** SysFS path to keypad event disable */
static output_state_t mce_keypad_sysfs_disable_output =
{
.context = "keypad_disable",
.truncate_file = TRUE,
.close_on_exit = TRUE,
};
/* ========================================================================= *
* DATAPIPE VALUES AND TRIGGERS
* ========================================================================= */
/** Cached submode_pipe state; assume invalid */
static submode_t submode = MCE_SUBMODE_INVALID;
/** Cached PID of process owning the topmost window on UI */
static int topmost_window_pid = -1;
/** Cached init_done state; assume unknown */
static tristate_t init_done = TRISTATE_UNKNOWN;
/** Proximity state history for triggering low power mode ui */
static ps_history_t tklock_lpmui_hist[8];
/** Current tklock ui state
*
* Access only via tklock_ui_is_enabled() / tklock_ui_set_enabled().
*/
static bool tklock_ui_enabled_pvt = false;
/** Current tklock ui state that has been sent to lipstick */
static int tklock_ui_notified = -1; // does not match bool values
/** System state; is undefined at bootup, can't assume anything */
static system_state_t system_state = MCE_SYSTEM_STATE_UNDEF;
/** 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;
/** Call state; assume no active calls */
static call_state_t call_state = CALL_STATE_NONE;
/** Actual proximity state; assume not covered */
static cover_state_t proximity_sensor_actual = COVER_UNDEF;
/** Effective proximity state; assume not covered */
static cover_state_t proximity_sensor_effective = COVER_UNDEF;
/** Lid cover sensor state; assume unkown
*
* When in covered state, it is assumed that it is not physically
* possible to see/interact with the display and thus it should
* stay powered off.
*
* Originally was used to track Nokia N770 slidable cover. Now
* it is used also for things like the hammerhead magnetic lid
* sensor.
*/
static cover_state_t lid_sensor_actual = COVER_UNDEF;
/** Lid cover policy state; assume unknown */
static cover_state_t lid_sensor_filtered = COVER_UNDEF;
/** Change notifications for system_state
*/
static void tklock_datapipe_system_state_cb(gconstpointer data)
{
system_state_t prev = system_state;
system_state = GPOINTER_TO_INT(data);
if( prev == system_state )
goto EXIT;
mce_log(LL_DEBUG, "system_state: %s -> %s",
system_state_repr(prev),
system_state_repr(system_state));
tklock_ui_set_enabled(false);
EXIT:
return;
}
/** Device lock state; assume undefined */
static devicelock_state_t devicelock_state = DEVICELOCK_STATE_UNDEFINED;
/** Push device lock state value into devicelock_state_pipe datapipe
*/
static void tklock_datapipe_set_devicelock_state(devicelock_state_t state)
{
switch( state ) {
case DEVICELOCK_STATE_UNLOCKED:
case DEVICELOCK_STATE_UNDEFINED:
case DEVICELOCK_STATE_LOCKED:
break;
default:
mce_log(LL_WARN, "unknown device lock state=%d; assuming locked",
state);
state = DEVICELOCK_STATE_LOCKED;
break;
}
if( devicelock_state != state ) {
datapipe_exec_full(&devicelock_state_pipe,
GINT_TO_POINTER(state));
}
}
/** Change notifications for devicelock_state
*/
static void tklock_datapipe_devicelock_state_cb(gconstpointer data)
{
devicelock_state_t prev = devicelock_state;
devicelock_state = GPOINTER_TO_INT(data);
if( devicelock_state == prev )
goto EXIT;
mce_log(LL_DEVEL, "devicelock_state = %s -> %s",
devicelock_state_repr(prev),
devicelock_state_repr(devicelock_state));
tklock_uiexception_rethink();
tklock_autolock_rethink();
/* When lipstick is starting up we see device lock
* going through undefined -> locked/unlocked change.
* We must not trigger autolock due to these initial
* device lock transitions */
switch( prev ) {
case DEVICELOCK_STATE_UNDEFINED:
/* Block autolock for 60 seconds when leaving
* undefined state (= lipstick startup) */
tklock_autolock_on_devlock_block(60 * 1000);
break;
case DEVICELOCK_STATE_LOCKED:
/* Unblock autolock earlier if we see transition
* away from locked state (=unlocked by user) */
tklock_autolock_on_devlock_block(0);
break;
default:
break;
}
switch( devicelock_state ) {
case DEVICELOCK_STATE_LOCKED:
tklock_autolock_on_devlock_trigger();
break;
case DEVICELOCK_STATE_UNLOCKED:
switch( display_state_next ) {
case MCE_DISPLAY_OFF:
case MCE_DISPLAY_LPM_OFF:
case MCE_DISPLAY_LPM_ON:
/* Transitions from undefined -> unlocked state occur
* during device bootup / mce restart and should not
* cause any actions.
*/
if( prev == DEVICELOCK_STATE_UNDEFINED )
break;
/* Devicelock ui keeps fingerprint scanner active in LPM state
* and unlocks device on identify, but omits unlock feedback
* and leaves the display state as-is.
*
* As a workaround, execute unlock feedback from mce. Then
* exit from LPM by requesting display power up and removal
* of tklock submode.
*
* While this is mostly relevant to LPM, apply the same logic
* also when in actually powered off display states to guard
* against timing glitches (getting fingerprint identification
* when already decided to exit LPM, etc) and changes in device
* lock ui side logic.
*/
mce_log(LL_WARN, "device got unlocked while display is off; "
"assume fingerprint authentication occurred");
datapipe_exec_full(&ngfd_event_request_pipe,
"unlock_device");
/* Delay display state / tklock processing until proximity
* sensor state is known */
common_on_proximity_schedule(MODULE_NAME,
tklock_datapipe_devicelock_state_cb2,
0);
break;
default:
break;
}
break;
default:
break;
}
EXIT:
return;
}
/** Wait for proximity sensor -callback for fingerprint unlock handling
*
* @param aptr unused
*/
static void tklock_datapipe_devicelock_state_cb2(gpointer aptr)
{
(void)aptr;
/* Still unlocked ? */
if( devicelock_state == DEVICELOCK_STATE_UNLOCKED ) {
if( proximity_sensor_actual != COVER_OPEN ) {
mce_log(LL_WARN, "unblank skipped due to proximity sensor");
}
else {
mce_datapipe_request_display_state(MCE_DISPLAY_ON);
mce_datapipe_request_tklock(TKLOCK_REQUEST_OFF);
}
}
}
/** Resumed from suspend notification */
static void tklock_datapipe_resume_detected_event_cb(gconstpointer data)
{
(void) data;
/* Re-evaluate proximity locking after resuming from
* suspend. */
tklock_proxlock_resume();
}
/** devicelock dbus name is reserved; assume unknown */
static service_state_t devicelock_service_state = SERVICE_STATE_UNDEF;
/** Change notifications for devicelock_service_state
*/
static void tklock_datapipe_devicelock_service_state_cb(gconstpointer data)
{
service_state_t prev = devicelock_service_state;
devicelock_service_state = GPOINTER_TO_INT(data);
if( devicelock_service_state == prev )
goto EXIT;
mce_log(LL_DEBUG, "devicelock_service_state = %s -> %s",
service_state_repr(prev),
service_state_repr(devicelock_service_state));
if( devicelock_service_state == SERVICE_STATE_RUNNING ) {
/* query initial device lock state on devicelock/mce startup */
tklock_ui_get_devicelock();
}
else {
/* if device lock service is not running, the device lock
* state is undefined */
tklock_datapipe_set_devicelock_state(DEVICELOCK_STATE_UNDEFINED);
}
EXIT:
return;
}
/** Lipstick dbus name is reserved; assume false */
static service_state_t lipstick_service_state = SERVICE_STATE_UNDEF;
/** Change notifications for lipstick_service_state
*/
static void tklock_datapipe_lipstick_service_state_cb(gconstpointer data)
{
service_state_t prev = lipstick_service_state;
lipstick_service_state = GPOINTER_TO_INT(data);
if( lipstick_service_state == prev )
goto EXIT;
mce_log(LL_DEBUG, "lipstick_service_state = %s -> %s",
service_state_repr(prev),
service_state_repr(lipstick_service_state));
bool enable_tklock = false;
/* Tklock is applicable only when lipstick is running */
if( lipstick_service_state == SERVICE_STATE_RUNNING ) {
if( prev == SERVICE_STATE_UNDEF ) {
/* UNDEF -> RUNNING: Implies a mce restart while lipstick
* is running. What we would like to happen is that things
* stay exactly as they were. However there is no passive
* way to recover lockscreen state from lipstick and we
* have to do something active in order to get tklock
* state in sync between mce and lipstick.
*
* If display is in powered on state, request lockscreen
* deactivation. If device happens to be locked, this is
* transformed into show-unlock-screen request.
*
* Otherwise activate lockscreen.
*/
if( display_state_next != MCE_DISPLAY_ON )
enable_tklock = true;
}
else {
/* STOPPED -> RUNNING: Implies lipstick start during
* bootup / restart later on. In this case lockscreen
* status is decided by lipstick and it defaults to having
* lockscreen active and changes are not really accepted
* until lipstick startup has finished. Making a lockscreen
* activation request merely serves as a way to ensure that
* mce and lipstick get in sync about tklock state.
*/
enable_tklock = true;
}
}
// force tklock ipc
tklock_ui_notified = -1;
tklock_ui_set_enabled(enable_tklock);
EXIT:
return;
}
/** Update mode is active; assume false */
static bool osupdate_running = false;
/** Change notifications for osupdate_running
*/
static void tklock_datapipe_osupdate_running_cb(gconstpointer data)
{
bool prev = osupdate_running;
osupdate_running = GPOINTER_TO_INT(data);
if( osupdate_running == prev )
goto EXIT;
mce_log(LL_DEBUG, "osupdate_running = %d -> %d", prev, osupdate_running);
if( osupdate_running ) {
/* undo tklock when update mode starts */
mce_datapipe_request_tklock(TKLOCK_REQUEST_OFF);
}
EXIT:
return;
}
/** Device is shutting down; assume false */
static bool shutting_down = false;
/** Change notifications for shutting_down
*/
static void tklock_datapipe_shutting_down_cb(gconstpointer data)
{
bool prev = shutting_down;
shutting_down = GPOINTER_TO_INT(data);
if( shutting_down == prev )
goto EXIT;
mce_log(LL_DEBUG, "shutting_down = %d -> %d",
prev, shutting_down);
tklock_evctrl_rethink();
EXIT:
return;
}
/** Autorelock trigger: assume disabled */
static autorelock_t autorelock_trigger = AUTORELOCK_NO_TRIGGERS;
/** Change notifications for display_state_curr
*/
static void tklock_datapipe_display_state_curr_cb(gconstpointer data)
{
display_state_t prev = display_state_curr;
display_state_curr = GPOINTER_TO_INT(data);
if( display_state_curr == prev )