forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tklock.c
3456 lines (2925 loc) · 89.6 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 © 2004-2011 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[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 <glib.h>
#include <glib/gstdio.h> /* g_access() */
#include <errno.h> /* errno */
#include <string.h> /* strcmp() */
#include <unistd.h> /* W_OK */
#include <linux/input.h> /* struct input_event */
#include <mce/mode-names.h>
#include <systemui/dbus-names.h>
#include <systemui/tklock-dbus-names.h>
#include "mce.h"
#include "tklock.h"
#include "mce-io.h" /* mce_write_string_to_file(),
* mce_write_number_string_to_file()
*/
#include "mce-log.h" /* mce_log(), LL_* */
#include "datapipe.h" /* execute_datapipe(),
* datapipe_get_gint(),
* append_input_trigger_to_datapipe(),
* append_output_trigger_to_datapipe(),
* remove_input_trigger_from_datapipe(),
* remove_output_trigger_from_datapipe()
*/
#include "mce-conf.h" /* mce_conf_get_bool() */
#include "mce-dbus.h" /* mce_dbus_handler_add(),
* mce_dbus_owner_monitor_add(),
* mce_dbus_owner_monitor_remove_all(),
* dbus_send(),
* dbus_send_message(),
* dbus_new_method_reply(),
* dbus_new_signal(),
* dbus_message_append_args(),
* dbus_message_get_args(),
* dbus_message_get_no_reply(),
* dbus_message_get_sender(),
* dbus_message_unref(),
* dbus_error_init(),
* dbus_error_free(),
* DBUS_MESSAGE_TYPE_METHOD_CALL,
* DBUS_TYPE_BOOLEAN,
* DBUS_TYPE_UINT32, DBUS_TYPE_INT32,
* DBUS_TYPE_STRING,
* DBUS_TYPE_INVALID
* DBusMessage, DBusError,
* dbus_bool_t,
* dbus_uint32_t, dbus_int32_t
*/
#include "mce-gconf.h" /* mce_gconf_notifier_add(),
* mce_gconf_get_bool(),
* gconf_entry_get_key(),
* gconf_entry_get_value(),
* gconf_value_get_bool(),
* GConfClient, GConfEntry, GConfValue
*/
/**
* TRUE if the touchscreen/keypad autolock is enabled,
* FALSE if the touchscreen/keypad autolock is disabled
*/
static gboolean tk_autolock_enabled = DEFAULT_TK_AUTOLOCK;
/** GConf callback ID for the autolock entry */
static guint tk_autolock_enabled_cb_id = 0;
/** GConf callback ID for the double tap gesture */
static guint doubletap_gesture_policy_cb_id = 0;
/** Doubletap gesture proximity timeout ID */
static guint doubletap_proximity_timeout_cb_id = 0;
/** Doubletap gesture proximity timeout ID */
static guint pocket_mode_proximity_timeout_cb_id = 0;
/** Blanking timeout ID for the visual tklock */
static guint tklock_visual_blank_timeout_cb_id = 0;
/** Dimming timeout ID for the tklock */
static guint tklock_dim_timeout_cb_id = 0;
/** ID for touchscreen/keypad unlock source */
static guint tklock_unlock_timeout_cb_id = 0;
/** Powerkey repeat emulation ID */
static guint powerkey_repeat_emulation_cb_id = 0;
/** Powerkey repeats counter */
static guint powerkey_repeat_count = 0;
/** Blank immediately on tklock instead of dim/blank */
static gboolean blank_immediately = DEFAULT_BLANK_IMMEDIATELY;
/** Dim immediately on tklock instead of timeout */
static gboolean dim_immediately = DEFAULT_DIM_IMMEDIATELY;
/** Touchscreen/keypad dim timeout */
static gint dim_delay = DEFAULT_DIM_DELAY;
/** Disable touchscreen immediately on tklock instead of at blank */
static gint disable_ts_immediately = DEFAULT_TS_OFF_IMMEDIATELY;
/** Disable keypad immediately on tklock instead of at blank */
static gint disable_kp_immediately = DEFAULT_KP_OFF_IMMEDIATELY;
/** Inhibit autolock when slide is open */
static gboolean autolock_with_open_slide = DEFAULT_AUTOLOCK_SLIDE_OPEN;
/** Inhibit proximity lock when slide is open */
static gboolean proximity_lock_with_open_slide = DEFAULT_PROXIMITY_LOCK_SLIDE_OPEN;
/** Unconditionally enable lock when keyboard slide is closed */
static gboolean always_lock_on_slide_close = DEFAULT_LOCK_ON_SLIDE_CLOSE;
/** Unlock the TKLock when the lens cover is opened */
static gboolean lens_cover_unlock = DEFAULT_LENS_COVER_UNLOCK;
/** Proximity based locking when the phone is ringing */
static gboolean proximity_lock_when_ringing = DEFAULT_PROXIMITY_LOCK_WHEN_RINGING;
/** Doubletap gesture is enabled */
static gboolean doubletap_gesture_enabled = FALSE;
/** Doubletap gesture inhibited */
static gboolean doubletap_gesture_inhibited = FALSE;
/** Trigger unlock screen when volume keys are pressed */
static gboolean volkey_visual_trigger = DEFAULT_VOLKEY_VISUAL_TRIGGER;
/** 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_control_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,
};
/** Disable automatic dim/blank from tklock */
static gint tklock_blank_disable = FALSE;
/** GConf notifier id for tracking tklock_blank_disable changes */
static guint tklock_blank_disable_id = 0;
/** Pseudo-forever dim/blank delay to use when tklock_blank_disable is set */
#define DISABLED_VISUAL_BLANK_DELAY (24*60*60) // 24h
/** Touchscreen double tap gesture policy */
static gint doubletap_gesture_policy = DEFAULT_DOUBLETAP_GESTURE_POLICY;
/** Submode at the beginning of a call */
static submode_t saved_submode = MCE_INVALID_SUBMODE;
/** List of monitored SystemUI processes (should be one or zero) */
static GSList *systemui_monitor_list = NULL;
/** Double tap recalibration delays */
static const guint doubletap_recal_delays[] = {
2, 4, 8, 16, 30
};
/** Double tap recalibration index */
static guint doubletap_recal_index = 0;
/** Double tap recalibration toumeout identifier */
static guint doubletap_recal_timeout_id = 0;
/** Do double tap recalibration on heartbeat */
static gboolean doubletap_recal_on_heartbeat = FALSE;
/** TKLock saved state type */
typedef enum {
/** TKLock was not enabled */
MCE_TKLOCK_UNLOCKED_STATE = 0,
/** Visual TKLock was enabled */
MCE_TKLOCK_VISUAL_STATE = 1,
/** Full TKLock was enabled */
MCE_TKLOCK_LOCKED_STATE = 2
} saved_tklock_state_t;
/** TKLock saved state */
static saved_tklock_state_t saved_tklock_state = MCE_TKLOCK_UNLOCKED_STATE;
/** TKLock UI state type */
typedef enum {
/** TKLock UI state unknown */
MCE_TKLOCK_UI_UNSET = -1,
/** No TKLock UI active */
MCE_TKLOCK_UI_NONE = 0,
/** Normal TKLock UI active */
MCE_TKLOCK_UI_NORMAL = 1,
/** Event eater UI active */
MCE_TKLOCK_UI_EVENT_EATER = 2,
/** Slider UI active */
MCE_TKLOCK_UI_SLIDER = 3,
/** Low power mode UI active */
MCE_TKLOCK_UI_LPM = 4
} tklock_ui_state_t;
/** TKLock UI state */
static tklock_ui_state_t tklock_ui_state = MCE_TKLOCK_UI_UNSET;
/** Touch screen state type */
typedef enum {
/** Touch screen state unknown */
MCE_TS_UNSET = -1,
/** Touch screen disabled */
MCE_TS_DISABLED,
/** Touch screen enabled */
MCE_TS_ENABLED
} ts_state_t;
/** Touch screen state */
static ts_state_t ts_state = MCE_TS_UNSET;
/** Double tap state type */
typedef enum {
/** Double tap state unknown */
MCE_DT_UNSET = -1,
/** Double tap disabled */
MCE_DT_DISABLED,
/** Double tap enabled */
MCE_DT_ENABLED
} dt_state_t;
/** Double tap state */
static dt_state_t dt_state = MCE_DT_UNSET;
/* Valid triggers for autorelock */
/** No autorelock triggers */
#define AUTORELOCK_NO_TRIGGERS 0
/** Autorelock on keyboard slide closed */
#define AUTORELOCK_KBD_SLIDE (1 << 0)
/** Autorelock on lens cover */
#define AUTORELOCK_LENS_COVER (1 << 1)
/** Autorelock on proximity sensor */
#define AUTORELOCK_ON_PROXIMITY (1 << 2)
/** Inhibit proximity relock type */
typedef enum {
/** Inhibit proximity relock */
MCE_INHIBIT_PROXIMITY_RELOCK = 0,
/** Allow proximity relock */
MCE_ALLOW_PROXIMITY_RELOCK = 1,
/** Temporarily inhibit proximity relock */
MCE_TEMP_INHIBIT_PROXIMITY_RELOCK = 2,
} inhibit_proximity_relock_t;
/** Inhibit autorelock using proximity sensor */
static inhibit_proximity_relock_t inhibit_proximity_relock = MCE_ALLOW_PROXIMITY_RELOCK;
/** Autorelock when call ends */
static gboolean autorelock_after_call_end = DEFAULT_AUTORELOCK_AFTER_CALL_END;
/** Autorelock triggers */
static gint autorelock_triggers = AUTORELOCK_NO_TRIGGERS;
static void set_doubletap_gesture(gboolean enable);
static void ts_enable(void);
static void ts_disable(void);
static void set_tklock_state(lock_state_t lock_state);
static void autorelock_touchscreen_trigger(gconstpointer const data);
static void cancel_tklock_dim_timeout(void);
static void cancel_tklock_unlock_timeout(void);
/**
* Query the event eater status
*
* @return TRUE if the event eater is enabled,
* FALSE if the event eater is disabled
*/
static gboolean is_eveater_enabled(void) G_GNUC_PURE;
static gboolean is_eveater_enabled(void)
{
return ((mce_get_submode_int32() & MCE_EVEATER_SUBMODE) != 0);
}
/**
* Query the touchscreen/keypad lock status
*
* @return TRUE if the touchscreen/keypad lock is enabled,
* FALSE if the touchscreen/keypad lock is disabled
*/
static gboolean is_tklock_enabled(void) G_GNUC_PURE;
static gboolean is_tklock_enabled(void)
{
return ((mce_get_submode_int32() & MCE_TKLOCK_SUBMODE) != 0);
}
/**
* Query whether the device is in the MALF state
*
* @return TRUE if the malf state is enabled,
* FALSE if the malf state is disabled
*/
static gboolean is_malf_state_enabled(void) G_GNUC_PURE;
static gboolean is_malf_state_enabled(void)
{
return ((mce_get_submode_int32() & MCE_MALF_SUBMODE) != 0);
}
/**
* Query the visual touchscreen/keypad lock status
*
* @return TRUE if the visual touchscreen/keypad lock is enabled,
* FALSE if the visual touchscreen/keypad lock is disabled
*/
static gboolean is_visual_tklock_enabled(void) G_GNUC_PURE;
static gboolean is_visual_tklock_enabled(void)
{
return ((mce_get_submode_int32() & MCE_VISUAL_TKLOCK_SUBMODE) != 0);
}
/**
* Query the touchscreen/keypad lock status based on proximity
*
* @return TRUE if the touchscreen/keypad lock is activated by proximity,
* FALSE if the touchscreen/keypad lock is not activated by proximity
*/
static gboolean is_tklock_enabled_by_proximity(void) G_GNUC_PURE;
static gboolean is_tklock_enabled_by_proximity(void)
{
return ((mce_get_submode_int32() & MCE_PROXIMITY_TKLOCK_SUBMODE) != 0);
}
/**
* Query the autorelock status
*
* @return TRUE if the autorelock is enabled,
* FALSE if the autorelock is disabled
*/
static gboolean is_autorelock_enabled(void) G_GNUC_PURE;
static gboolean is_autorelock_enabled(void)
{
return ((mce_get_submode_int32() & MCE_AUTORELOCK_SUBMODE) != 0);
}
/**
* Query the pocket mode status
*
* @return TRUE if the pocket mode is enabled,
* FALSE if the pocket mode is disabled
*/
static gboolean is_pocket_mode_enabled(void) G_GNUC_PURE;
static gboolean is_pocket_mode_enabled(void)
{
return ((mce_get_submode_int32() & MCE_POCKET_SUBMODE) != 0);
}
/**
* Heartbeat trigger
*
* @param data Not used
*/
static void heartbeat_trigger(gconstpointer data)
{
(void)data;
if (doubletap_recal_on_heartbeat == TRUE) {
mce_log(LL_DEBUG, "Recalibrating double tap");
(void)mce_write_string_to_file(mce_touchscreen_calibration_control_path,
"1");
}
}
/**
* Callback for doubletap recalibration
*
* @param data Not used.
* @return Always FALSE for remove event source
*/
static gboolean doubletap_recal_timeout_cb(gpointer data)
{
(void)data;
mce_log(LL_DEBUG, "Recalibrating double tap");
(void)mce_write_string_to_file(mce_touchscreen_calibration_control_path,
"1");
/* If at last delay, start recalibrating on DSME heartbeat */
if (doubletap_recal_index == G_N_ELEMENTS(doubletap_recal_delays) - 1) {
doubletap_recal_timeout_id = 0;
doubletap_recal_on_heartbeat = TRUE;
return FALSE;
}
/* Otherwise use next delay */
doubletap_recal_index++;
doubletap_recal_timeout_id =
g_timeout_add_seconds(doubletap_recal_delays[doubletap_recal_index],
doubletap_recal_timeout_cb, NULL);
return FALSE;
}
/**
* Cancel doubletap recalibration timeouts
*/
static void cancel_doubletap_recal_timeout(void)
{
if (doubletap_recal_timeout_id != 0)
g_source_remove(doubletap_recal_timeout_id);
doubletap_recal_timeout_id = 0;
doubletap_recal_on_heartbeat = FALSE;
}
/**
* Setup doubletap recalibration timeouts
*/
static void setup_doubletap_recal_timeout(void)
{
if (mce_touchscreen_calibration_control_path == NULL)
return;
cancel_doubletap_recal_timeout();
doubletap_recal_index = 0;
doubletap_recal_on_heartbeat = FALSE;
doubletap_recal_timeout_id =
g_timeout_add_seconds(doubletap_recal_delays[doubletap_recal_index],
doubletap_recal_timeout_cb, NULL);
}
/**
* Enable auto-relock
*/
static void enable_autorelock(void)
{
cover_state_t kbd_slide_state = datapipe_get_gint(keyboard_slide_pipe);
cover_state_t lens_cover_state = datapipe_get_gint(lens_cover_pipe);
if (autorelock_triggers != AUTORELOCK_ON_PROXIMITY) {
/* Reset autorelock triggers */
autorelock_triggers = AUTORELOCK_NO_TRIGGERS;
/* If the keyboard slide is closed, use it as a trigger */
if (kbd_slide_state == COVER_CLOSED)
autorelock_triggers |= AUTORELOCK_KBD_SLIDE;
/* If the lens cover is closed, use it as a trigger */
if (lens_cover_state == COVER_CLOSED)
autorelock_triggers |= AUTORELOCK_LENS_COVER;
}
/* Only setup touchscreen monitoring once,
* and only if there are autorelock triggers
* and it's not the proximity sensor
*/
if ((is_autorelock_enabled() == FALSE) &&
(autorelock_triggers != AUTORELOCK_NO_TRIGGERS) &&
(autorelock_triggers != AUTORELOCK_ON_PROXIMITY)) {
append_input_trigger_to_datapipe(&touchscreen_pipe,
autorelock_touchscreen_trigger);
}
mce_add_submode_int32(MCE_AUTORELOCK_SUBMODE);
}
/**
* Disable auto-relock
*/
static void disable_autorelock(void)
{
/* Touchscreen monitoring is only needed for the autorelock */
remove_input_trigger_from_datapipe(&touchscreen_pipe,
autorelock_touchscreen_trigger);
mce_rem_submode_int32(MCE_AUTORELOCK_SUBMODE);
/* Reset autorelock triggers */
autorelock_triggers = AUTORELOCK_NO_TRIGGERS;
}
/**
* Disable auto-relock based on policy
*/
static void disable_autorelock_policy(void)
{
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
/* Don't disable autorelock if the alarm UI is visible */
if ((alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32))
goto EXIT;
/* If the tklock is enabled
* or proximity autorelock is active, don't disable
*/
if ((is_tklock_enabled() == TRUE) ||
(autorelock_triggers == AUTORELOCK_ON_PROXIMITY))
goto EXIT;
disable_autorelock();
EXIT:
return;
}
/**
* Cancel timeout for pocket mode
*/
static void cancel_pocket_mode_timeout(void)
{
if (pocket_mode_proximity_timeout_cb_id != 0) {
g_source_remove(pocket_mode_proximity_timeout_cb_id);
pocket_mode_proximity_timeout_cb_id = 0;
}
}
/**
* Timeout callback for doubletap gesture proximity
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean doubletap_proximity_timeout_cb(gpointer data)
{
call_state_t call_state = datapipe_get_gint(call_state_pipe);
audio_route_t audio_route = datapipe_get_gint(audio_route_pipe);
(void)data;
if ((audio_route == AUDIO_ROUTE_HANDSET) &&
((call_state == CALL_STATE_RINGING) ||
(call_state == CALL_STATE_ACTIVE))) {
cancel_pocket_mode_timeout();
mce_add_submode_int32(MCE_POCKET_SUBMODE);
mce_add_submode_int32(MCE_PROXIMITY_TKLOCK_SUBMODE);
}
doubletap_proximity_timeout_cb_id = 0;
/* First disable touchscreen interrupts, then disable gesture */
ts_disable();
set_doubletap_gesture(FALSE);
doubletap_gesture_inhibited = TRUE;
return FALSE;
}
/**
* Timeout callback for pocket mode
*
* @param data Unused
* @return Always returns FALSE to disable the timeout
*/
static gboolean pocket_mode_timeout_cb(gpointer data)
{
(void)data;
pocket_mode_proximity_timeout_cb_id = 0;
mce_add_submode_int32(MCE_POCKET_SUBMODE);
return FALSE;
}
/**
* Setup a timeout for pocket mode
*/
static void setup_pocket_mode_timeout(void)
{
if (pocket_mode_proximity_timeout_cb_id != 0)
return;
pocket_mode_proximity_timeout_cb_id =
g_timeout_add_seconds(DEFAULT_POCKET_MODE_PROXIMITY_TIMEOUT,
pocket_mode_timeout_cb, NULL);
}
/**
* Cancel timeout for doubletap gesture proximity
*/
static void cancel_doubletap_proximity_timeout(void)
{
/* Remove the timer source for doubletap gesture proximity */
if (doubletap_proximity_timeout_cb_id != 0) {
g_source_remove(doubletap_proximity_timeout_cb_id);
doubletap_proximity_timeout_cb_id = 0;
}
}
/**
* Setup a timeout for doubletap gesture proximity
*/
static void setup_doubletap_proximity_timeout(void)
{
gint timeout = DEFAULT_DOUBLETAP_PROXIMITY_TIMEOUT;
call_state_t call_state = datapipe_get_gint(call_state_pipe);
audio_route_t audio_route = datapipe_get_gint(audio_route_pipe);
cancel_doubletap_proximity_timeout();
if (doubletap_gesture_enabled == FALSE)
goto EXIT;
/* Setup new timeout */
if ((audio_route == AUDIO_ROUTE_HANDSET) &&
((call_state == CALL_STATE_RINGING) ||
(call_state == CALL_STATE_ACTIVE)))
timeout = 0;
doubletap_proximity_timeout_cb_id =
g_timeout_add_seconds(timeout,
doubletap_proximity_timeout_cb, NULL);
EXIT:
return;
}
/**
* Enable/disable double tap gesture control
*
* @param enable TRUE enable gesture recognition,
* FALSE disable gesture recognition
*/
static void set_doubletap_gesture(gboolean enable)
{
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
call_state_t call_state = datapipe_get_gint(call_state_pipe);
cover_state_t proximity_sensor_state =
datapipe_get_gint(proximity_sensor_pipe);
if (mce_touchscreen_gesture_control_path == NULL)
goto EXIT;
/* If the double-tap gesture policy is 0,
* then we should just disable touchscreen interrupts instead.
* Likewise if there's a call or an alarm, and the proximity sensor
* is covered
*/
if ((enable == TRUE) &&
((doubletap_gesture_policy == 0) ||
(doubletap_gesture_inhibited == TRUE) ||
((proximity_sensor_state == COVER_CLOSED) &&
((call_state != CALL_STATE_NONE) ||
(alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32))))) {
cancel_doubletap_proximity_timeout();
doubletap_gesture_enabled = FALSE;
ts_disable();
goto EXIT;
}
doubletap_gesture_enabled = enable;
/* Adjust the touchscreen idle frequency */
if (enable == TRUE) {
mce_rem_submode_int32(MCE_POCKET_SUBMODE);
cancel_doubletap_proximity_timeout();
cancel_pocket_mode_timeout();
if (proximity_sensor_state == COVER_CLOSED) {
setup_doubletap_proximity_timeout();
setup_pocket_mode_timeout();
}
} else {
cancel_doubletap_proximity_timeout();
}
if (enable && dt_state != MCE_DT_ENABLED) {
(void)mce_write_string_to_file(mce_touchscreen_gesture_control_path, "4");
setup_doubletap_recal_timeout();
dt_state = MCE_DT_ENABLED;
} else if (!enable && dt_state != MCE_DT_DISABLED) {
(void)mce_write_string_to_file(mce_touchscreen_gesture_control_path, "0");
cancel_doubletap_recal_timeout();
/* Disabling the double tap gesture causes recalibration */
if (ts_state == MCE_TS_ENABLED) {
g_usleep(MCE_TOUCHSCREEN_CALIBRATION_DELAY);
}
dt_state = MCE_DT_DISABLED;
}
/* Finally, ensure that touchscreen interrupts are enabled
* if doubletap gestures are enabled
*/
if (enable == TRUE) {
ts_enable();
}
EXIT:
return;
}
/**
* Enable/disable touchscreen/keypad events
*
* @note Since nothing sensible can be done on error except reporting it,
* we don't return the status
* @param output control structure for enable/disable file
* @param enable TRUE enable events, FALSE disable events
*/
static void generic_event_control(output_state_t *output,
const gboolean enable)
{
if (output->path == NULL)
goto EXIT;
if (mce_write_number_string_to_file(output, !enable ? 1 : 0) == FALSE) {
mce_log(LL_ERR,
"%s: Event status *not* modified",
output->path);
goto EXIT;
}
mce_log(LL_DEBUG,
"%s: events %s",
output->path, enable ? "enabled" : "disabled");
EXIT:
return;
}
/**
* Enable touchscreen interrupts (events will be generated by kernel)
*/
static void ts_enable(void)
{
if (ts_state != MCE_TS_ENABLED) {
generic_event_control(&mce_touchscreen_sysfs_disable_output,
TRUE);
g_usleep(MCE_TOUCHSCREEN_CALIBRATION_DELAY);
ts_state = MCE_TS_ENABLED;
}
}
/**
* Disable touchscreen interrupts (no events will be generated by kernel)
*/
static void ts_disable(void)
{
if (ts_state != MCE_TS_DISABLED) {
generic_event_control(&mce_touchscreen_sysfs_disable_output,
FALSE);
ts_state = MCE_TS_DISABLED;
}
}
/**
* Enable keypress interrupts (events will be generated by kernel)
*/
static void kp_enable(void)
{
generic_event_control(&mce_keypad_sysfs_disable_output, TRUE);
}
/**
* Disable keypress interrupts (no events will be generated by kernel)
*/
static void kp_disable(void)
{
generic_event_control(&mce_keypad_sysfs_disable_output, FALSE);
}
/**
* Policy based enabling of touchscreen and keypad
*/
static void ts_kp_enable_policy(void)
{
system_state_t system_state = datapipe_get_gint(system_state_pipe);
cover_state_t lid_cover_state = datapipe_get_gint(lid_cover_pipe);
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
/* If the cover is closed, don't bother */
if (lid_cover_state == COVER_CLOSED)
goto EXIT;
if ((system_state == MCE_STATE_USER) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32)) {
set_doubletap_gesture(FALSE);
ts_enable();
kp_enable();
}
EXIT:
return;
}
/**
* Policy based disabling of touchscreen and keypad
*/
static void ts_kp_disable_policy(void)
{
display_state_t display_state = datapipe_get_gint(display_state_pipe);
system_state_t system_state = datapipe_get_gint(system_state_pipe);
alarm_ui_state_t alarm_ui_state =
datapipe_get_gint(alarm_ui_state_pipe);
submode_t submode = mce_get_submode_int32();
call_state_t call_state = datapipe_get_gint(call_state_pipe);
/* If we're in softoff submode, always disable */
if ((submode & MCE_SOFTOFF_SUBMODE) != 0) {
ts_disable();
kp_disable();
goto EXIT;
}
/* If the Alarm UI is visible, don't disable,
* unless the tklock UI is active
*/
if (((alarm_ui_state == MCE_ALARM_UI_VISIBLE_INT32) ||
(alarm_ui_state == MCE_ALARM_UI_RINGING_INT32)) &&
(tklock_ui_state != MCE_TKLOCK_UI_NORMAL)) {
mce_log(LL_DEBUG,
"Alarm UI visible; refusing to disable touchscreen "
"and keypad events");
goto EXIT;
}
if ((system_state != MCE_STATE_USER) ||
(is_malf_state_enabled() == TRUE)){
ts_disable();
kp_disable();
} else if (((display_state == MCE_DISPLAY_OFF) ||
(display_state == MCE_DISPLAY_LPM_OFF) ||
(display_state == MCE_DISPLAY_LPM_ON)) &&
(is_tklock_enabled() == TRUE)) {
/* Display is off -- we only need to check for
* disable_{ts,kp}_immediately == 2
*/
if (disable_kp_immediately == 2) {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else {
ts_disable();
}
} else {
/* Don't disable kp during call (volume must work) */
if (call_state != CALL_STATE_NONE) {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else {
ts_disable();
}
} else {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else {
ts_disable();
}
kp_disable();
}
}
} else if (is_tklock_enabled() == TRUE) {
/* Don't disable kp during call (volume keys must work) */
if (call_state != CALL_STATE_NONE) {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else if (disable_ts_immediately == 1) {
ts_disable();
}
} else if (disable_kp_immediately == 1) {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else if (disable_ts_immediately == 1) {
ts_disable();
}
kp_disable();
} else {
if (disable_ts_immediately == 2) {
set_doubletap_gesture(TRUE);
} else if (disable_ts_immediately == 1) {
ts_disable();
}
}
}
EXIT:
return;
}
/**
* Synthesise activity, since activity is filtered when tklock is active;
* also, the lock key doesn't normally generate activity
*/
static void synthesise_activity(void)
{
(void)execute_datapipe(&device_inactive_pipe,
GINT_TO_POINTER(FALSE),
USE_INDATA, CACHE_INDATA);
}
/**
* Synthesise inactivity, since we want immediate inactivity
* when the tklock is activated
*/
static void synthesise_inactivity(void)
{
(void)execute_datapipe(&device_inactive_pipe,
GINT_TO_POINTER(TRUE),
USE_INDATA, CACHE_INDATA);
}
/**
* Send the touchscreen/keypad lock mode
*
* @param method_call A DBusMessage to reply to;
* pass NULL to send a tklock mode signal instead
* @return TRUE on success, FALSE on failure
*/
static gboolean send_tklock_mode(DBusMessage *const method_call)
{
DBusMessage *msg = NULL;
const gchar *modestring;
gboolean status = FALSE;
if (is_tklock_enabled() == TRUE)
modestring = MCE_TK_LOCKED;
else
modestring = MCE_TK_UNLOCKED;
/* If method_call is set, send a reply,
* otherwise, send a signal
*/
if (method_call != NULL) {
msg = dbus_new_method_reply(method_call);
} else {
/* tklock_mode_ind */
msg = dbus_new_signal(MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_TKLOCK_MODE_SIG);
}
/* Append the new mode */
if (dbus_message_append_args(msg,
DBUS_TYPE_STRING, &modestring,
DBUS_TYPE_INVALID) == FALSE) {
mce_log(LL_CRIT,
"Failed to append %sargument to D-Bus message "
"for %s.%s",
method_call ? "reply " : "",
method_call ? MCE_REQUEST_IF :
MCE_SIGNAL_IF,
method_call ? MCE_TKLOCK_MODE_GET :
MCE_TKLOCK_MODE_SIG);
dbus_message_unref(msg);
goto EXIT;
}
/* Send the message */
status = dbus_send_message(msg);
EXIT:
return status;
}
/**
* D-Bus callback used for monitoring SystemUI; if it disappears,
* disable the tklock for reliability reasons *if* the tklock was
* active
*/
static gboolean systemui_owner_monitor_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
const gchar *old_name;
const gchar *new_name;
const gchar *service;